infer_engine.cpp 3.72 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
23
24
    // Create one RankWorker per rank
    int world_size = communication_group_.get_world_size();
    workers_.reserve(world_size);
    for (int r = 0; r < world_size; ++r) {
25
26
27
        workers_.emplace_back(std::make_unique<RankWorker>(
            model_config_,
            communication_group_.get_rank_info(r),
28
29
            cache_config_ != nullptr ? cache_config_.get() : nullptr,
            enable_graph_compiling));
30
31
32
33
34
35
36
37
38
39
40
41
    }
}

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

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

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

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

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

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

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

90
    return workers_[0]->get_output();
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
}

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

107
108
109
//------------------------------------------------------
// reset_cache (overloaded with CacheConfig)
//------------------------------------------------------
PanZezhong's avatar
PanZezhong committed
110
void InferEngine::reset_cache(const cache::CacheConfig *new_config) {
Ceng's avatar
Ceng committed
111
    for (auto &worker : workers_) {
PanZezhong's avatar
PanZezhong committed
112
        worker->reset_cache(new_config);
113
114
115
    }
    for (auto &worker : workers_) {
        worker->wait();
Ceng's avatar
Ceng committed
116
117
118
    }
}

119
} // namespace infinilm::engine