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

3
#include "../cache/cache.hpp"
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "../models/model_factory.hpp"
#include "distributed/distributed.hpp"

#include <any>
#include <condition_variable>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

namespace infinilm::engine {

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

public:
    RankWorker(const std::any &model_config,
28
29
               const distributed::RankInfo &rank_info,
               const cache::CacheConfig &cache_config);
30
31
32
33
34

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

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

PanZezhong's avatar
PanZezhong committed
38
    // Submit a run (forward) job.
39
40
    void run(const std::vector<std::any> &args);

Ceng's avatar
Ceng committed
41
    // Reset the internal cache in the model (clears state between generations)
42
43
44
45
    void reset_cache(size_t pos = 0);

    // Reset the internal cache with a new configuration
    void reset_cache(const cache::CacheConfig &new_config, size_t pos = 0);
Ceng's avatar
Ceng committed
46

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

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    // Request worker shutdown and join the thread.
    void close();

    // Thread-safe accessor for last output produced by RUN.
    infinicore::Tensor get_output();

    std::string info() const;

private:
    void thread_loop();

private:
    // Worker properties
    std::any model_config_;
    distributed::RankInfo rank_info_;
    std::shared_ptr<InfinilmModel> model_;
66
    std::shared_ptr<cache::DynamicCache> cache_ptr_;
67
68
69
70
71
72
73
74
75
76
77
78
79
80

    // 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_;
    std::vector<std::any> pending_args_;
Ceng's avatar
Ceng committed
81
    size_t pending_reset_pos_ = 0;
82
    cache::CacheConfig pending_cache_config_;
83
84
85
86
87
88
89
90
91
92
93

    // Output (protected by mutex)
    infinicore::Tensor output_;

    // Thread sync
    std::thread thread_;
    std::mutex mutex_;
    std::condition_variable cv_;
};

} // namespace infinilm::engine