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

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

#include <any>
#include <vector>

namespace infinilm::engine {

class InferEngine {
public:
15
    // Updated constructor: accept CacheConfig instead of CacheType
16
    InferEngine(
Jiacheng Huang's avatar
Jiacheng Huang committed
17
        const InfinilmModel::Config &config,
18
        const distributed::DistConfig &distributed_config = distributed::DistConfig(),
19
20
        infinicore::Device::Type device_type = infinicore::context::getDevice().getType(),
        const cache::CacheConfig &cache_config = cache::CacheConfig());
21
22
23
24

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

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

28
29
    // Run a single forward pass on all workers and return the outputs from all ranks
    infinicore::Tensor generate(const infinicore::Tensor &input_ids,
30
                                const infinicore::Tensor &position_ids);
31

32
33
34
35
36
    // Reset the internal cache pos in all workers (clears state between generations)
    void reset_cache(size_t pos = 0);

    // Overload: reset cache with new KV configuration
    void reset_cache(const cache::CacheConfig &new_config, size_t pos = 0);
Ceng's avatar
Ceng committed
37

38
39
40
41
    ~InferEngine();

    const distributed::DistConfig &get_dist_config() const;

42
43
44
    // Get current KV configuration
    const cache::CacheConfig &get_cache_config() const { return cache_config_; }

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_;
49
    cache::CacheConfig cache_config_;
50
51
52
};

} // namespace infinilm::engine