rank_worker.hpp 3.74 KB
Newer Older
1
2
#pragma once

3
#include "../cache/cache.hpp"
4
#include "../models/model_factory.hpp"
5
#include "compiler/general_compiler.hpp"
6
7
8
9
10
#include "distributed/distributed.hpp"

#include <any>
#include <condition_variable>
#include <mutex>
11
#include <random>
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <thread>
#include <vector>

namespace infinilm::engine {

class RankWorker {
    enum class Command {
        INIT,
        LOAD,
        RUN,
Ceng's avatar
Ceng committed
23
        RESET_CACHE,
24
25
26
27
        STOP
    };

public:
28
29
30
31
32
33
    struct Input {
        /// Token IDs tensor of shape `[batch, seq_len]`.
        std::optional<infinicore::Tensor> input_ids;
        /// Position IDs tensor of shape `[batch, seq_len]` or `[seq_len]`.
        std::optional<infinicore::Tensor> position_ids;
        /// Past Lengths of cached sequence for each request, of shape `[num_requests]`.
34
35
36
        std::optional<infinicore::Tensor> past_sequence_lengths;
        /// ToTal Lengths for each request sequence, of shape `[num_requests]`.
        std::optional<infinicore::Tensor> total_sequence_lengths;
37
38
39
40
41
42
43
44
45
46
47
48
49
        /// Offsets of each request in a continous-batched sequence, of shape `[num_requests]`.
        std::optional<infinicore::Tensor> input_offsets;
        /// Block ids for each request `[batch, max_block_table_length]`. Used for paged cache.
        std::optional<infinicore::Tensor> block_tables;
        /// Slot ids for each token `[seq]`. Used for paged cache.
        std::optional<infinicore::Tensor> slot_mapping;

        float temperature{1};

        int top_k{50};

        float top_p{1};

50
        infinilm::InfinilmModel::Input to_model_input(infinicore::Device device) const;
51
52
53
54
55
56
    };

    struct Output {
        infinicore::Tensor output_ids;
    };

Jiacheng Huang's avatar
Jiacheng Huang committed
57
    RankWorker(const InfinilmModel::Config &model_config,
58
               const distributed::RankInfo &rank_info,
59
60
               const cache::CacheConfig *cache_config,
               bool enable_graph_compiling);
61
62
63
64
65

    // Submit a parameter load job and wait until the load completes on the worker thread.
    void load_param(const std::string &name,
                    const infinicore::Tensor &param);

66
67
68
    // return the parameters (i.e. weights and biases).
    std::unordered_map<std::string, infinicore::nn::Parameter> state_dict();

PanZezhong's avatar
PanZezhong committed
69
    // Submit a run (forward) job.
70
    void run(const Input &args);
71

72
    // Reset the internal cache with a new configuration
PanZezhong's avatar
PanZezhong committed
73
    void reset_cache(const cache::CacheConfig *new_config);
Ceng's avatar
Ceng committed
74

PanZezhong's avatar
PanZezhong committed
75
76
77
    // Wait until run job completes. The result can be retrieved with get_output().
    void wait();

78
79
80
81
    // Request worker shutdown and join the thread.
    void close();

    // Thread-safe accessor for last output produced by RUN.
82
    Output get_output();
83
84
85
86
87
88
89
90

    std::string info() const;

private:
    void thread_loop();

private:
    // Worker properties
Jiacheng Huang's avatar
Jiacheng Huang committed
91
    const InfinilmModel::Config &model_config_;
92
93
    distributed::RankInfo rank_info_;
    std::shared_ptr<InfinilmModel> model_;
PanZezhong's avatar
PanZezhong committed
94
    std::shared_ptr<cache::Cache> cache_;
95

96
97
98
99
    // Graph Compiling
    bool enable_graph_compiling_;
    std::unique_ptr<GraphCompiler> compiler_;

100
101
102
103
104
105
106
107
108
109
110
111
    // Command for the pending job (protected by mutex_)
    Command job_cmd_;

    // State flags (protected by mutex_)
    bool has_job_ = false;     // a job is pending
    bool job_done_ = false;    // last job completed
    bool should_exit_ = false; // request to stop
    bool init_done_ = false;   // initialization finished

    // Task payloads (protected by mutex)
    std::string pending_param_name_;
    infinicore::Tensor pending_param_;
112
    Input pending_args_;
PanZezhong's avatar
PanZezhong committed
113
    std::unique_ptr<cache::CacheConfig> pending_cache_config_;
114
115

    // Output (protected by mutex)
116
    Output output_;
117
118
119
120
121

    // Thread sync
    std::thread thread_;
    std::mutex mutex_;
    std::condition_variable cv_;
122
123
124

    // Random
    std::mt19937 rng_;
125
126
127
};

} // namespace infinilm::engine