infer_engine.cpp 4.04 KB
Newer Older
1
#include "infer_engine.hpp"
Ceng's avatar
Ceng committed
2
#include "spdlog/spdlog.h"
3
4
5
6
7
8
9

namespace infinilm::engine {

//------------------------------------------------------
// Constructor
//------------------------------------------------------
InferEngine::InferEngine(
Jiacheng Huang's avatar
Jiacheng Huang committed
10
    const InfinilmModel::Config &config,
11
    const distributed::DistConfig &distributed_config,
12
    infinicore::Device::Type device_type,
13
14
    const cache::CacheConfig *cache_config,
    bool enable_graph_compiling) // Changed parameter
15
    : communication_group_(distributed_config, device_type),
PanZezhong's avatar
PanZezhong committed
16
      model_config_(config) {
17

PanZezhong's avatar
PanZezhong committed
18
19
    if (cache_config != nullptr) {
        cache_config_ = cache_config->unique_copy();
20
    }
21
22
    // Create one RankWorker per rank
    int world_size = communication_group_.get_world_size();
23
    barrier_ = std::make_unique<RankBarrier>((size_t)world_size);
24
25
    workers_.reserve(world_size);
    for (int r = 0; r < world_size; ++r) {
26
27
28
        workers_.emplace_back(std::make_unique<RankWorker>(
            model_config_,
            communication_group_.get_rank_info(r),
29
            cache_config_ != nullptr ? cache_config_.get() : nullptr,
30
            barrier_.get(),
31
            enable_graph_compiling));
32
33
34
35
36
37
38
39
40
41
42
43
    }
}

//------------------------------------------------------
// load_param
//------------------------------------------------------
void InferEngine::load_param(const std::string &name, const infinicore::Tensor &param) {
    // Load the parameter on all workers
    for (auto &worker : workers_) {
        worker->load_param(name, param);
    }
}
44

45
46
47
//------------------------------------------------------
// state_dict
//------------------------------------------------------
48
49
std::vector<std::unordered_map<std::string, infinicore::nn::Parameter>> InferEngine::state_dict() {
    std::vector<std::unordered_map<std::string, infinicore::nn::Parameter>> results;
50
51
52
    if (0 == workers_.size()) {
        throw std::runtime_error(" Model object not found. ");
    }
53
54
55
56
57

    for (auto &worker : workers_) {
        results.push_back(worker->state_dict());
    }
    return results;
58
59
}

60
//------------------------------------------------------
61
// forward
62
//------------------------------------------------------
63
64
infinilm::InfinilmModel::Input
InferEngine::Input::to_model_input(infinicore::Device device) const {
65

66
67
68
69
    auto to_device = [&](const std::optional<infinicore::Tensor> &t)
        -> std::optional<infinicore::Tensor> {
        return t.has_value() ? t.value()->to(device) : t;
    };
70
71

    return {
72
        to_device(input_ids), // @todo: on device in the future
73
        to_device(position_ids),
74
        to_device(past_sequence_lengths), // @todo: on device in the future
75
76
77
78
79
        to_device(total_sequence_lengths),
        to_device(input_offsets),
        to_device(block_tables),
        to_device(slot_mapping),
    };
PanZezhong's avatar
PanZezhong committed
80
}
81

PanZezhong's avatar
PanZezhong committed
82
InferEngine::Output InferEngine::forward(const InferEngine::Input &input) {
83
84
    // Trigger each worker to run inference
    for (auto &worker : workers_) {
85
        worker->run(input);
86
    }
PanZezhong's avatar
PanZezhong committed
87
88
89
90
    // Wait for all workers
    for (auto &worker : workers_) {
        worker->wait();
    }
91

92
    return workers_[0]->get_output();
93
94
}

95
96
97
98
99
100
101
102
103
104
void InferEngine::compile() {
    for (auto &worker : workers_) {
        worker->compile();
    }
    // Wait for all workers
    for (auto &worker : workers_) {
        worker->wait();
    }
}

105
106
107
108
109
110
111
112
113
114
115
116
117
118
//------------------------------------------------------
// Destructor
//------------------------------------------------------
InferEngine::~InferEngine() {
    // Close all workers
    for (auto &worker : workers_) {
        worker->close();
    }
}

const distributed::DistConfig &InferEngine::get_dist_config() const {
    return communication_group_.get_dist_config();
}

119
120
121
//------------------------------------------------------
// reset_cache (overloaded with CacheConfig)
//------------------------------------------------------
PanZezhong's avatar
PanZezhong committed
122
void InferEngine::reset_cache(const cache::CacheConfig *new_config) {
Ceng's avatar
Ceng committed
123
    for (auto &worker : workers_) {
PanZezhong's avatar
PanZezhong committed
124
        worker->reset_cache(new_config);
125
126
127
    }
    for (auto &worker : workers_) {
        worker->wait();
Ceng's avatar
Ceng committed
128
    }
129
130

    this->compile();
Ceng's avatar
Ceng committed
131
132
}

133
} // namespace infinilm::engine