infer_engine.cpp 4.11 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

    // Compile the model on all workers
    this->compile();
36
37
38
39
40
41
42
43
44
45
46
}

//------------------------------------------------------
// 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);
    }
}
47

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

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

63
//------------------------------------------------------
64
// forward
65
//------------------------------------------------------
66
67
infinilm::InfinilmModel::Input
InferEngine::Input::to_model_input(infinicore::Device device) const {
68

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

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

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

95
    return workers_[0]->get_output();
96
97
}

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

108
109
110
111
112
113
114
115
116
117
118
119
120
121
//------------------------------------------------------
// 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();
}

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

    this->compile();
Ceng's avatar
Ceng committed
134
135
}

136
} // namespace infinilm::engine