infer_engine.cpp 2.93 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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

namespace infinilm::engine {

//------------------------------------------------------
// Constructor
//------------------------------------------------------
InferEngine::InferEngine(
    const std::any &config,
    const distributed::DistConfig &distributed_config,
    infinicore::Device::Type device_type)
    : communication_group_(distributed_config, device_type),
      model_config_(config) {
    spdlog::info("Launch InferEngine with {}", std::string(distributed_config));
    // 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) {
        workers_.emplace_back(std::make_unique<RankWorker>(model_config_, communication_group_.get_rank_info(r)));
    }
}

//------------------------------------------------------
// 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);
    }
}
33
34
35
//------------------------------------------------------
// state_dict
//------------------------------------------------------
36
37
38
std::vector<std::unordered_map<std::string, infinicore::nn::Parameter>> InferEngine::state_dict() {

    std::vector<std::unordered_map<std::string, infinicore::nn::Parameter>> results;
39
40
41
    if (0 == workers_.size()) {
        throw std::runtime_error(" Model object not found. ");
    }
42
43
44
45
46

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

49
50
51
52
53
54
55
56
57
//------------------------------------------------------
// generate
//------------------------------------------------------
infinicore::Tensor InferEngine::generate(const infinicore::Tensor &input_ids,
                                         const infinicore::Tensor &position_ids) {
    // Trigger each worker to run inference
    for (auto &worker : workers_) {
        worker->run(std::vector<std::any>({input_ids, position_ids}));
    }
PanZezhong's avatar
PanZezhong committed
58
59
60
61
    // Wait for all workers
    for (auto &worker : workers_) {
        worker->wait();
    }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

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

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

Ceng's avatar
Ceng committed
80
81
82
83
84
85
86
87
88
89
//------------------------------------------------------
// reset_cache
//------------------------------------------------------
void InferEngine::reset_cache(size_t pos, bool async) {
    // Reset cache on all workers
    for (auto &worker : workers_) {
        worker->reset_cache(pos, async);
    }
}

90
} // namespace infinilm::engine