infer_engine.hpp 1.71 KB
Newer Older
1
2
#pragma once

3
#include "../models/infinilm_model.hpp"
Jiacheng Huang's avatar
Jiacheng Huang committed
4
#include "../models/llama/llama_config.hpp"
5
6
7
8
#include "distributed/distributed.hpp"
#include "infinicore/tensor.hpp"
#include "rank_worker.hpp"

9
#include <optional>
10
11
12
13
14
15
#include <vector>

namespace infinilm::engine {

class InferEngine {
public:
16
    using Input = RankWorker::Input;
PanZezhong's avatar
PanZezhong committed
17

18
    using Output = RankWorker::Output;
19

20
    // Updated constructor: accept CacheConfig instead of CacheType
21
    InferEngine(
Jiacheng Huang's avatar
Jiacheng Huang committed
22
        const InfinilmModel::Config &config,
23
        const distributed::DistConfig &distributed_config = distributed::DistConfig(),
24
        infinicore::Device::Type device_type = infinicore::context::getDevice().getType(),
25
26
        const cache::CacheConfig *cache_config = nullptr,
        bool enable_graph_compiling = false);
27
28
29
30

    // Load a parameter to all workers (each can extract its shard inside RankWorker)
    void load_param(const std::string &name, const infinicore::Tensor &param);

31
    // return the parameters (i.e. weights and biases).
32
    std::vector<std::unordered_map<std::string, infinicore::nn::Parameter>> state_dict();
33

34
    // Run a single forward pass on all workers and return the outputs from all ranks
35
    Output forward(const Input &input);
36

PanZezhong's avatar
PanZezhong committed
37
    void reset_cache(const cache::CacheConfig *new_config);
Ceng's avatar
Ceng committed
38

39
40
41
42
    ~InferEngine();

    const distributed::DistConfig &get_dist_config() const;

43
    // Get current KV configuration
PanZezhong's avatar
PanZezhong committed
44
    const cache::CacheConfig *get_cache_config() const { return cache_config_.get(); }
45

46
47
48
protected:
    std::vector<std::unique_ptr<RankWorker>> workers_;
    distributed::CommunicationGroup communication_group_;
Jiacheng Huang's avatar
Jiacheng Huang committed
49
    const InfinilmModel::Config &model_config_;
PanZezhong's avatar
PanZezhong committed
50
    std::unique_ptr<cache::CacheConfig> cache_config_;
51
52
53
};

} // namespace infinilm::engine