infer_engine.hpp 1.67 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(),
PanZezhong's avatar
PanZezhong committed
25
        const cache::CacheConfig *cache_config = nullptr);
26
27
28
29

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

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

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

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

38
39
40
41
    ~InferEngine();

    const distributed::DistConfig &get_dist_config() const;

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

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

} // namespace infinilm::engine