|
ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
|
Distributed coordinator for multi-node LLM inference. More...
#include <distributed_coordinator.h>

Classes | |
| struct | InferenceRequest |
| struct | InferenceResult |
| struct | LoadStats |
| Get load balancing statistics. More... | |
| struct | NodeInfo |
Public Types | |
| enum class | NodeRole { MASTER , WORKER , HYBRID } |
Public Member Functions | |
| DistributedCoordinator (NodeRole role, const std::string &model_path, const std::string &tokenizer_path, int gpu_layers=-1) | |
| Initialize the distributed coordinator. | |
| ~DistributedCoordinator () | |
| bool | initialize () |
| Initialize MPI and discover network topology. | |
| void | shutdown () |
| Shutdown MPI and cleanup resources. | |
| bool | submit_request (const InferenceRequest &request, std::function< void(const InferenceResult &)> callback) |
| Submit an inference request (master node only) | |
| void | run_worker () |
| Process requests as a worker node This function blocks and processes incoming requests. | |
| void | run_master () |
| Run as master node, coordinating requests This function blocks and coordinates the distributed system. | |
| 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. | |
| int | get_size () const |
| Get the total number of nodes. | |
| bool | is_mpi_available () const |
| Check if MPI is available and initialized. | |
| LoadStats | get_load_stats () const |
Private Member Functions | |
| void | discover_cluster () |
| Discover cluster topology and node capabilities. | |
| int | select_optimal_node (const InferenceRequest &request) |
| Select optimal node for processing request. | |
| bool | send_request_to_worker (int worker_rank, const InferenceRequest &request) |
| Send request to worker node. | |
| bool | receive_and_process_request () |
| Receive and process request (worker node) | |
| bool | send_result_to_master (const InferenceResult &result) |
| Send result back to master. | |
| bool | receive_result_from_worker () |
| Receive result from worker (master node) | |
| std::vector< char > | serialize_request (const InferenceRequest &request) |
| Serialize request for MPI transmission. | |
| InferenceRequest | deserialize_request (const std::vector< char > &data) |
| Deserialize request from MPI transmission. | |
| std::vector< char > | serialize_result (const InferenceResult &result) |
| Serialize result for MPI transmission. | |
| InferenceResult | deserialize_result (const std::vector< char > &data) |
| Deserialize result from MPI transmission. | |
| InferenceResult | process_inference_locally (const InferenceRequest &request) |
| Process inference request locally. | |
| void | send_heartbeat () |
| Send heartbeat to all nodes. | |
| void | handle_heartbeat (int source_rank) |
| Handle heartbeat from other nodes. | |
Private Attributes | |
| NodeRole | role_ |
| std::string | model_path_ |
| std::string | tokenizer_path_ |
| int | gpu_layers_ |
| int | rank_ = -1 |
| int | size_ = -1 |
| bool | mpi_initialized_ = false |
| std::vector< NodeInfo > | cluster_nodes_ |
| std::map< std::string, std::function< void(const InferenceResult &)> > | pending_callbacks_ |
| std::atomic< int > | total_requests_ {0} |
| std::atomic< int > | completed_requests_ {0} |
| std::atomic< int > | failed_requests_ {0} |
Static Private Attributes | |
| static constexpr int | TAG_REQUEST = 1 |
| static constexpr int | TAG_RESULT = 2 |
| static constexpr int | TAG_HEARTBEAT = 3 |
| static constexpr int | TAG_SHUTDOWN = 4 |
| static constexpr int | TAG_NODE_INFO = 5 |
Distributed coordinator for multi-node LLM inference.
This class manages distributed inference across multiple nodes using MPI. It handles model sharding, request routing, and result aggregation.
Definition at line 22 of file distributed_coordinator.h.
| tinyllama::DistributedCoordinator::DistributedCoordinator | ( | NodeRole | role, |
| const std::string & | model_path, | ||
| const std::string & | tokenizer_path, | ||
| int | gpu_layers = -1 |
||
| ) |
Initialize the distributed coordinator.
| role | The role of this node |
| model_path | Path to the model file |
| tokenizer_path | Path to the tokenizer file |
| gpu_layers | Number of GPU layers for this node |
Definition at line 16 of file distributed_coordinator.cpp.
| tinyllama::DistributedCoordinator::~DistributedCoordinator | ( | ) |
Definition at line 26 of file distributed_coordinator.cpp.
References shutdown().
|
private |
Deserialize request from MPI transmission.
Definition at line 466 of file distributed_coordinator.cpp.
References j, tinyllama::DistributedCoordinator::InferenceRequest::max_tokens, and str.
Referenced by receive_and_process_request().
|
private |
Deserialize result from MPI transmission.
Definition at line 509 of file distributed_coordinator.cpp.
References j, str, and tinyllama::DistributedCoordinator::InferenceResult::tokens_generated.
Referenced by receive_result_from_worker().
|
private |
Discover cluster topology and node capabilities.
Definition at line 87 of file distributed_coordinator.cpp.
References cluster_nodes_, gpu_layers_, Logger::INFO, j, mpi_initialized_, tinyllama::DistributedCoordinator::NodeInfo::rank, rank_, role_, size_, and TAG_NODE_INFO.
Referenced by initialize().
| std::vector< DistributedCoordinator::NodeInfo > tinyllama::DistributedCoordinator::get_cluster_info | ( | ) | const |
Get information about all nodes in the cluster.
Definition at line 586 of file distributed_coordinator.cpp.
References cluster_nodes_.
| DistributedCoordinator::LoadStats tinyllama::DistributedCoordinator::get_load_stats | ( | ) | const |
Definition at line 590 of file distributed_coordinator.cpp.
References cluster_nodes_, completed_requests_, failed_requests_, j, tinyllama::DistributedCoordinator::LoadStats::total_requests, and total_requests_.
|
inline |
Get the rank of this node.
Definition at line 116 of file distributed_coordinator.h.
References rank_.
|
inline |
Get the total number of nodes.
Definition at line 121 of file distributed_coordinator.h.
References size_.
Handle heartbeat from other nodes.
Definition at line 579 of file distributed_coordinator.cpp.
References cluster_nodes_, and j.
| bool tinyllama::DistributedCoordinator::initialize | ( | ) |
Initialize MPI and discover network topology.
Definition at line 30 of file distributed_coordinator.cpp.
References discover_cluster(), Logger::ERROR, Logger::INFO, j, mpi_initialized_, rank_, size_, and Logger::WARNING.
| bool tinyllama::DistributedCoordinator::is_mpi_available | ( | ) | const |
Check if MPI is available and initialized.
Definition at line 79 of file distributed_coordinator.cpp.
References mpi_initialized_.
|
private |
Process inference request locally.
Definition at line 537 of file distributed_coordinator.cpp.
References j, rank_, and tinyllama::DistributedCoordinator::InferenceResult::request_id.
Referenced by receive_and_process_request(), and submit_request().
|
private |
Receive and process request (worker node)
Definition at line 346 of file distributed_coordinator.cpp.
References cluster_nodes_, deserialize_request(), j, process_inference_locally(), rank_, send_result_to_master(), and TAG_REQUEST.
Referenced by run_worker().
|
private |
Receive result from worker (master node)
Definition at line 391 of file distributed_coordinator.cpp.
References completed_requests_, deserialize_result(), failed_requests_, j, pending_callbacks_, and TAG_RESULT.
Referenced by run_master().
| void tinyllama::DistributedCoordinator::run_master | ( | ) |
Run as master node, coordinating requests This function blocks and coordinates the distributed system.
Definition at line 317 of file distributed_coordinator.cpp.
References Logger::ERROR, Logger::INFO, j, mpi_initialized_, rank_, receive_result_from_worker(), and send_heartbeat().
| void tinyllama::DistributedCoordinator::run_worker | ( | ) |
Process requests as a worker node This function blocks and processes incoming requests.
Definition at line 283 of file distributed_coordinator.cpp.
References Logger::ERROR, Logger::INFO, j, mpi_initialized_, rank_, receive_and_process_request(), and TAG_SHUTDOWN.
|
private |
Select optimal node for processing request.
Definition at line 241 of file distributed_coordinator.cpp.
References cluster_nodes_, HYBRID, j, size_, and WORKER.
Referenced by submit_request().
|
private |
Send heartbeat to all nodes.
Definition at line 570 of file distributed_coordinator.cpp.
References j, size_, and TAG_HEARTBEAT.
Referenced by run_master().
|
private |
Send request to worker node.
Definition at line 264 of file distributed_coordinator.cpp.
References j, serialize_request(), and TAG_REQUEST.
Referenced by submit_request().
|
private |
Send result back to master.
Definition at line 432 of file distributed_coordinator.cpp.
References j, serialize_result(), and TAG_RESULT.
Referenced by receive_and_process_request().
|
private |
Serialize request for MPI transmission.
Definition at line 451 of file distributed_coordinator.cpp.
Referenced by send_request_to_worker().
|
private |
Serialize result for MPI transmission.
Definition at line 495 of file distributed_coordinator.cpp.
Referenced by send_result_to_master().
| void tinyllama::DistributedCoordinator::shutdown | ( | ) |
Shutdown MPI and cleanup resources.
Definition at line 61 of file distributed_coordinator.cpp.
References Logger::INFO, j, mpi_initialized_, rank_, size_, and TAG_SHUTDOWN.
Referenced by ~DistributedCoordinator().
| bool tinyllama::DistributedCoordinator::submit_request | ( | const InferenceRequest & | request, |
| std::function< void(const InferenceResult &)> | callback | ||
| ) |
Submit an inference request (master node only)
| request | The inference request |
| callback | Callback function for result |
Definition at line 196 of file distributed_coordinator.cpp.
References Logger::ERROR, j, mpi_initialized_, pending_callbacks_, process_inference_locally(), rank_, tinyllama::DistributedCoordinator::InferenceResult::request_id, select_optimal_node(), send_request_to_worker(), and total_requests_.
|
private |
Definition at line 150 of file distributed_coordinator.h.
Referenced by discover_cluster(), get_cluster_info(), get_load_stats(), handle_heartbeat(), receive_and_process_request(), and select_optimal_node().
|
private |
Definition at line 155 of file distributed_coordinator.h.
Referenced by get_load_stats(), and receive_result_from_worker().
|
private |
Definition at line 156 of file distributed_coordinator.h.
Referenced by get_load_stats(), and receive_result_from_worker().
|
private |
Definition at line 144 of file distributed_coordinator.h.
Referenced by discover_cluster().
|
private |
Definition at line 142 of file distributed_coordinator.h.
Definition at line 148 of file distributed_coordinator.h.
Referenced by discover_cluster(), initialize(), is_mpi_available(), run_master(), run_worker(), shutdown(), and submit_request().
|
private |
Definition at line 151 of file distributed_coordinator.h.
Referenced by receive_result_from_worker(), and submit_request().
|
private |
Definition at line 146 of file distributed_coordinator.h.
Referenced by discover_cluster(), get_rank(), initialize(), process_inference_locally(), receive_and_process_request(), run_master(), run_worker(), shutdown(), and submit_request().
|
private |
Definition at line 141 of file distributed_coordinator.h.
Referenced by discover_cluster().
|
private |
Definition at line 147 of file distributed_coordinator.h.
Referenced by discover_cluster(), get_size(), initialize(), select_optimal_node(), send_heartbeat(), and shutdown().
Definition at line 161 of file distributed_coordinator.h.
Referenced by send_heartbeat().
Definition at line 163 of file distributed_coordinator.h.
Referenced by discover_cluster().
Definition at line 159 of file distributed_coordinator.h.
Referenced by receive_and_process_request(), and send_request_to_worker().
Definition at line 160 of file distributed_coordinator.h.
Referenced by receive_result_from_worker(), and send_result_to_master().
Definition at line 162 of file distributed_coordinator.h.
Referenced by run_worker(), and shutdown().
|
private |
Definition at line 143 of file distributed_coordinator.h.
|
private |
Definition at line 154 of file distributed_coordinator.h.
Referenced by get_load_stats(), and submit_request().