ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
distributed_coordinator.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5#include <vector>
6#include <functional>
7#include <map>
8#include <atomic>
9
10#ifdef HAS_MPI
11#include <mpi.h>
12#endif
13
14namespace tinyllama {
15
23public:
24 enum class NodeRole {
25 MASTER, // Coordinates requests and aggregates results
26 WORKER, // Processes inference requests
27 HYBRID // Can act as both master and worker
28 };
29
30 struct NodeInfo {
31 int rank;
32 std::string hostname;
34 bool has_gpu;
37 std::atomic<bool> is_busy{false};
38 std::atomic<int> active_requests{0};
39 };
40
42 std::string request_id;
43 std::string prompt;
46 int top_k;
47 float top_p;
48 std::string system_prompt;
49 int target_node = -1; // -1 for auto-selection
50 };
51
61
70 const std::string& model_path,
71 const std::string& tokenizer_path,
72 int gpu_layers = -1);
73
75
80 bool initialize();
81
85 void shutdown();
86
94 std::function<void(const InferenceResult&)> callback);
95
100 void run_worker();
101
106 void run_master();
107
111 std::vector<NodeInfo> get_cluster_info() const;
112
116 int get_rank() const { return rank_; }
117
121 int get_size() const { return size_; }
122
126 bool is_mpi_available() const;
127
139
140private:
142 std::string model_path_;
143 std::string tokenizer_path_;
145
146 int rank_ = -1;
147 int size_ = -1;
148 bool mpi_initialized_ = false;
149
150 std::vector<NodeInfo> cluster_nodes_;
151 std::map<std::string, std::function<void(const InferenceResult&)>> pending_callbacks_;
152
153 // Load balancing
154 std::atomic<int> total_requests_{0};
155 std::atomic<int> completed_requests_{0};
156 std::atomic<int> failed_requests_{0};
157
158 // MPI message tags
159 static constexpr int TAG_REQUEST = 1;
160 static constexpr int TAG_RESULT = 2;
161 static constexpr int TAG_HEARTBEAT = 3;
162 static constexpr int TAG_SHUTDOWN = 4;
163 static constexpr int TAG_NODE_INFO = 5;
164
168 void discover_cluster();
169
174
179
184
189
194
198 std::vector<char> serialize_request(const InferenceRequest& request);
199
203 InferenceRequest deserialize_request(const std::vector<char>& data);
204
208 std::vector<char> serialize_result(const InferenceResult& result);
209
213 InferenceResult deserialize_result(const std::vector<char>& data);
214
219
223 void send_heartbeat();
224
229};
230
231} // namespace tinyllama
Distributed coordinator for multi-node LLM inference.
InferenceResult process_inference_locally(const InferenceRequest &request)
Process inference request locally.
bool send_result_to_master(const InferenceResult &result)
Send result back to master.
void discover_cluster()
Discover cluster topology and node capabilities.
void shutdown()
Shutdown MPI and cleanup resources.
bool receive_and_process_request()
Receive and process request (worker node)
bool initialize()
Initialize MPI and discover network topology.
std::vector< char > serialize_request(const InferenceRequest &request)
Serialize request for MPI transmission.
int get_size() const
Get the total number of nodes.
bool submit_request(const InferenceRequest &request, std::function< void(const InferenceResult &)> callback)
Submit an inference request (master node only)
bool receive_result_from_worker()
Receive result from worker (master node)
void run_worker()
Process requests as a worker node This function blocks and processes incoming requests.
void handle_heartbeat(int source_rank)
Handle heartbeat from other nodes.
bool send_request_to_worker(int worker_rank, const InferenceRequest &request)
Send request to worker node.
void send_heartbeat()
Send heartbeat to all nodes.
std::map< std::string, std::function< void(const InferenceResult &)> > pending_callbacks_
std::vector< NodeInfo > get_cluster_info() const
Get information about all nodes in the cluster.
int get_rank() const
Get the rank of this node.
std::vector< char > serialize_result(const InferenceResult &result)
Serialize result for MPI transmission.
InferenceRequest deserialize_request(const std::vector< char > &data)
Deserialize request from MPI transmission.
void run_master()
Run as master node, coordinating requests This function blocks and coordinates the distributed system...
int select_optimal_node(const InferenceRequest &request)
Select optimal node for processing request.
InferenceResult deserialize_result(const std::vector< char > &data)
Deserialize result from MPI transmission.
bool is_mpi_available() const
Check if MPI is available and initialized.
size_t j