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

Jiacheng Huang's avatar
Jiacheng Huang committed
3
#include "../models/llama/llama_config.hpp"
4
5
6
#include "distributed/distributed.hpp"
#include "infinicore/tensor.hpp"
#include "rank_worker.hpp"
PanZezhong's avatar
PanZezhong committed
7
#include "../models/infinilm_model.hpp"
8
9
10
11
12
13
14

#include <vector>

namespace infinilm::engine {

class InferEngine {
public:
15
16
17
18
    struct Input {
        infinicore::Tensor input_ids;

        infinicore::Tensor position_ids;
PanZezhong's avatar
PanZezhong committed
19
20
21
22

        infinicore::Tensor cache_positions;

        infinilm::InfinilmModel::Input to_model_input() const;
23
24
25
26
27
28
    };

    struct Output {
        infinicore::Tensor logits;
    };

29
    // Updated constructor: accept CacheConfig instead of CacheType
30
    InferEngine(
Jiacheng Huang's avatar
Jiacheng Huang committed
31
        const InfinilmModel::Config &config,
32
        const distributed::DistConfig &distributed_config = distributed::DistConfig(),
33
        infinicore::Device::Type device_type = infinicore::context::getDevice().getType(),
PanZezhong's avatar
PanZezhong committed
34
        const cache::CacheConfig *cache_config = nullptr);
35
36
37
38

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

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

42
    // Run a single forward pass on all workers and return the outputs from all ranks
43
    Output forward(const Input &input);
44

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

47
48
49
50
    ~InferEngine();

    const distributed::DistConfig &get_dist_config() const;

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

54
55
56
protected:
    std::vector<std::unique_ptr<RankWorker>> workers_;
    distributed::CommunicationGroup communication_group_;
Jiacheng Huang's avatar
Jiacheng Huang committed
57
    const InfinilmModel::Config &model_config_;
PanZezhong's avatar
PanZezhong committed
58
    std::unique_ptr<cache::CacheConfig> cache_config_;
59
60
61
};

} // namespace infinilm::engine