vllm.distributed.weight_transfer ¶
Weight transfer engines for syncing model weights from trainers to inference workers.
Modules:
-
base–Base class for weight transfer engines.
-
clients–Built-in
VLLMWeightSyncClientimplementations. -
factory–Factory for weight transfer engines with lazy loading.
-
ipc_engine–IPC-based weight transfer engine using CUDA IPC for communication.
-
nccl_common–Shared NCCL initialization helpers for weight transfer engines.
-
nccl_engine–NCCL-based (dense) weight transfer engine.
-
packed_tensor–Packed tensor utilities for efficient weight transfer.
-
sparse_nccl_engine–Sparse NCCL weight transfer engine.
Classes:
-
HTTPVLLMWeightSyncClient–Talks to a vLLM server over the RLHF HTTP routes.
-
ModuleSource–WeightSourceovermodule.named_parameters()— the common case. -
ParamMeta–Name / wire dtype / full (HF) shape for one output parameter.
-
RayVLLMWeightSyncClient–Talks to one or more vLLM
AsyncLLM/LLMRay actors. -
TrainerWeightTransferEngine–Trainer-side weight transfer engine.
-
VLLMWeightSyncClient–Trainer-side stub for the inference engine's weight-sync control plane.
-
WeightSource–A re-iterable source of the trainer's weights, handed to a trainer engine.
-
WeightTransferEngine–Base class for weight transfer engines that handle transport of model weights
-
WeightTransferEngineFactory–Factory for creating weight transfer engines with lazy loading.
-
WeightTransferTrainerFactory–Factory for creating trainer-side weight transfer engines.
HTTPVLLMWeightSyncClient ¶
Talks to a vLLM server over the RLHF HTTP routes.
Mirrors vllm/entrypoints/serve/dev/rlhf/api_router.py: /init_weight_transfer_engine, /start_weight_update, /update_weights, /finish_weight_update.
Source code in vllm/distributed/weight_transfer/clients.py
ModuleSource ¶
Bases: WeightSource
WeightSource over module.named_parameters() — the common case.
Handles both plain dense modules and FSDP-sharded ones with no special casing: iteration all-gathers each DTensor via full_tensor() (a collective) and passes regular tensors through. metadata() reads the global .shape / .dtype, so it never triggers a gather.
Source code in vllm/distributed/weight_transfer/base.py
ParamMeta dataclass ¶
RayVLLMWeightSyncClient ¶
Talks to one or more vLLM AsyncLLM/LLM Ray actors.
Each call fans out to every handle and blocks on all of them, so a multi-actor (e.g. multi-DP) deployment is driven as one unit.
Source code in vllm/distributed/weight_transfer/clients.py
TrainerWeightTransferEngine ¶
Bases: ABC, Generic[TConfig, TInitInfo]
Trainer-side weight transfer engine.
Symmetric to WeightTransferEngine but lives in the training process. Constructed via the trainer_init factory classmethod; carries any backend-specific state (NCCL communicators, IPC device info, transfer plans) on self. The WeightSource is required at trainer_init, then replayed each round by the no-argument send_weights().
Multi-rank trainers: trainer_init and send_weights are called on every trainer rank. Rank 0 is the sender, resolved once at trainer_init into is_sender. Non-sender ranks still run every collective (iterating the source, metadata export, IPC handle all-gather) so the group stays aligned, but each engine explicitly guards the control-plane RPCs and the transmit on self.is_sender, so only the sender touches the client.
Subclasses should define
init_info_cls: Type of backend-specific trainer init info config_cls: Type of backend-specific config
Methods:
-
send_weights–Push
self.source's weights to inference workers and drive the full -
shutdown–Tear down communicators / process groups. Default no-op.
-
trainer_init–Rendezvous with the inference side and return a ready instance.
Source code in vllm/distributed/weight_transfer/base.py
send_weights() abstractmethod ¶
Push self.source's weights to inference workers and drive the full update round trip: start_weight_update, update_weights (run concurrently with the trainer-side broadcast when the backend requires it), then finish_weight_update. Called on every trainer rank.
Source code in vllm/distributed/weight_transfer/base.py
shutdown() ¶
trainer_init(config, init_info, *, client, source) abstractmethod classmethod ¶
Rendezvous with the inference side and return a ready instance.
Called on every trainer rank. The sender drives the full handshake via client (build the worker-side init info, call client.init_weight_transfer_engine, open the trainer-side endpoint); non-sender ranks skip the rendezvous and the RPC. source is stored on self.source; after return, send_weights() is callable.
Source code in vllm/distributed/weight_transfer/base.py
VLLMWeightSyncClient ¶
Bases: Protocol
Trainer-side stub for the inference engine's weight-sync control plane.
Mirrors the weight-sync methods that the inference engine exposes (EngineClient / the HTTP RLHF routes / Ray actors). A TrainerWeightTransferEngine drives the full handshake through this protocol so trainer code never has to know the transport.
All methods are synchronous and accept plain dicts (matching what the inference side already accepts). Concurrency that some backends need (e.g. NCCL must run update_weights concurrently with the trainer-side broadcast) is the engine's responsibility, not the client's, so the protocol stays a flat four-method surface that any wrapper can implement.
The protocol is structural (PEP 544), so user implementations need only define these four methods — no import or subclassing required.
Source code in vllm/distributed/weight_transfer/base.py
WeightSource ¶
Bases: ABC
A re-iterable source of the trainer's weights, handed to a trainer engine.
Two channels:
metadata()—(name, wire dtype, full shape)for every parameter, without transferring. Cheap when shapes are known locally (FSDPDTensorglobal shape); may be expensive on first call for backends that must materialize to learn shapes (e.g. a Megatron-Bridge export), in which case it should cache.- iteration — yields fully-materialized
(name, tensor)pairs, one at a time. Materializing is typically a collective (FSDPfull_tensor(), a Megatron export), so every trainer rank must iterate the same source in the same order in lockstep, or ranks deadlock. Under pipeline parallelism a rank may not own a parameter at all — iterating still drives the collective and the yielded tensor is only meaningful on the sender.
iter(source) must yield a fresh pass each round. Backends with custom producer logic (Megatron export, RDT plans, MoE re-fusing) subclass this.
Source code in vllm/distributed/weight_transfer/base.py
WeightTransferEngine ¶
Bases: ABC, Generic[TInitInfo, TUpdateInfo]
Base class for weight transfer engines that handle transport of model weights from a trainer to inference workers.
This abstraction separates weight transfer transport logic from the worker implementation, allowing different backends (NCCL, CUDA IPC, RDMA[TODO]) to be plugged in.
Each engine owns its full weight-update lifecycle: start_weight_update, update_weights, and finish_weight_update. Layerwise reloading (used by checkpoint-format engines) is opted into per engine by running it inside start_weight_update/finish_weight_update. Engines that apply weights in place (e.g. sparse patches) leave those methods as no-ops.
Subclasses should define
init_info_cls: Type of backend-specific initialization info update_info_cls: Type of backend-specific update info
Methods:
-
__init__–Initialize the weight transfer engine.
-
finish_weight_update–Finalize the current weight update.
-
init_transfer_engine–Initialize the weight transfer mechanism.
-
parse_init_info–Construct typed init info from dict with validation.
-
parse_update_info–Construct typed update info from dict with validation.
-
receive_weights–Receive weights from the trainer and load them into the model.
-
reset_weight_update_target–Restore weight updates to the engine's default target model.
-
set_weight_update_target–Set the model that will receive the active weight update.
-
shutdown–Shutdown the weight transfer engine.
-
start_weight_update–Prepare the engine for a new weight update.
-
trainer_send_weights–Send weights from trainer to inference workers.
-
update_weights–Receive one weight update chunk and load it into the model.
Source code in vllm/distributed/weight_transfer/base.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
__init__(config, vllm_config, device, model) ¶
Initialize the weight transfer engine.
Parameters:
-
(config¶WeightTransferConfig) –The configuration for the weight transfer engine
-
(vllm_config¶VllmConfig) –The full vLLM config (provides parallel/model config)
-
(device¶device) –The device this worker's model lives on
-
(model¶Module) –The local model instance which will receive the weights
Source code in vllm/distributed/weight_transfer/base.py
finish_weight_update() abstractmethod ¶
Finalize the current weight update.
Checkpoint-format engines finalize layerwise reloading here; engines that apply weights in place leave this as a no-op.
Source code in vllm/distributed/weight_transfer/base.py
init_transfer_engine(init_info) abstractmethod ¶
Initialize the weight transfer mechanism. This is called once at the beginning of training.
Parameters:
-
(init_info¶TInitInfo) –Backend-specific initialization info
Source code in vllm/distributed/weight_transfer/base.py
parse_init_info(init_dict) ¶
Construct typed init info from dict with validation.
Parameters:
Returns:
-
TInitInfo–Typed backend-specific init info dataclass
Raises:
-
ValueError–If init_dict is invalid for this backend
Source code in vllm/distributed/weight_transfer/base.py
parse_update_info(update_dict) ¶
Construct typed update info from dict with validation.
Parameters:
Returns:
-
TUpdateInfo–Typed backend-specific update info dataclass
Raises:
-
ValueError–If update_dict is invalid for this backend
Source code in vllm/distributed/weight_transfer/base.py
receive_weights(update_info) abstractmethod ¶
Receive weights from the trainer and load them into the model.
Parameters:
-
(update_info¶TUpdateInfo) –Backend-specific update info containing parameter metadata and any backend-specific data
Source code in vllm/distributed/weight_transfer/base.py
reset_weight_update_target() ¶
Restore weight updates to the engine's default target model.
set_weight_update_target(model, model_config) ¶
Set the model that will receive the active weight update.
shutdown() abstractmethod ¶
Shutdown the weight transfer engine. This should be called when the worker is shutting down.
start_weight_update() abstractmethod ¶
Prepare the engine for a new weight update.
Engines that receive weights in checkpoint format initialize layerwise reloading here, else this is typically a no-op. See: https://docs.vllm.ai/en/latest/training/layerwise/ for more details.
Source code in vllm/distributed/weight_transfer/base.py
trainer_send_weights(iterator, trainer_args) abstractmethod staticmethod ¶
Send weights from trainer to inference workers.
This is a static method that can be called from the trainer process to send weights to all inference workers.
Parameters:
-
(iterator¶Iterator[Any]) –Iterator of backend-specific items to send.
-
(trainer_args¶dict[str, Any] | Any) –Dictionary containing backend-specific arguments needed to send weights. The structure depends on the backend: - NCCL: Contains 'group', 'src', 'packed', etc. - IPC: Contains 'mode' ('http' or 'ray'), 'llm_handle' (for Ray), 'url' (for HTTP), etc.
Example
param_iter = ((n, p) for n, p in model.named_parameters()) engine.trainer_send_weights(param_iter, trainer_args)
Source code in vllm/distributed/weight_transfer/base.py
update_weights(update_info) ¶
Receive one weight update chunk and load it into the model.
Parameters:
Source code in vllm/distributed/weight_transfer/base.py
WeightTransferEngineFactory ¶
Factory for creating weight transfer engines with lazy loading.
This factory implements a registry pattern that supports: - Lazy loading: Engine modules are only imported when actually needed - Extensibility: Custom engines can be registered at runtime - Centralized registration: All built-in engines registered in one place
Methods:
-
create_engine–Create a weight transfer engine instance.
-
register_engine–Register an engine with lazy-loading or direct class reference.
Source code in vllm/distributed/weight_transfer/factory.py
create_engine(config, vllm_config, device, model) classmethod ¶
Create a weight transfer engine instance.
Parameters:
-
(config¶WeightTransferConfig) –Weight transfer configuration containing the backend name
-
(vllm_config¶VllmConfig) –The full vLLM config (provides parallel/model config)
-
(device¶device) –The device this worker's model lives on
-
(model¶Module) –The local model instance which will receive the weights
Returns:
-
WeightTransferEngine–An initialized weight transfer engine instance
Raises:
-
ValueError–If the backend is not registered
Source code in vllm/distributed/weight_transfer/factory.py
register_engine(name, module_path_or_cls, class_name=None) classmethod ¶
Register an engine with lazy-loading or direct class reference.
Supports two calling conventions: 1. Lazy loading: register_engine(name, module_path, class_name) 2. Direct class: register_engine(name, engine_cls)
Parameters:
-
(name¶str) –The name to register the engine under (e.g., "nccl")
-
(module_path_or_cls¶str | type[WeightTransferEngine]) –Either a module path string for lazy loading, or the engine class directly
-
(class_name¶str | None, default:None) –Name of the engine class (required if module_path is string)
Raises:
-
ValueError–If an engine with the same name is already registered
Source code in vllm/distributed/weight_transfer/factory.py
WeightTransferTrainerFactory ¶
Factory for creating trainer-side weight transfer engines.
Parallel to WeightTransferEngineFactory, with its own lazy-import registry. The trainer-side and worker-side registries are kept separate: they share backend names by convention, but the trainer process never instantiates a worker engine and vice versa, so unifying them would only couple the import graphs.
Methods:
-
register_engine–Register a trainer engine. Same conventions as
-
trainer_init–Build and rendezvous a ready-to-send trainer engine.
Source code in vllm/distributed/weight_transfer/factory.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
register_engine(name, module_path_or_cls, class_name=None) classmethod ¶
Register a trainer engine. Same conventions as WeightTransferEngineFactory.register_engine.
Source code in vllm/distributed/weight_transfer/factory.py
trainer_init(backend, config, init_info, *, client, source) classmethod ¶
Build and rendezvous a ready-to-send trainer engine.
Called on every trainer rank (multi-rank trainers construct on all ranks; the sender is resolved inside the engine's trainer_init).
Parameters:
-
(backend¶str) –Backend name (must be registered).
-
(config¶WeightTransferConfig) –Backend-specific weight transfer config.
-
(init_info¶WeightTransferInitInfo) –Backend-specific trainer init info.
-
(client¶VLLMWeightSyncClient) –Inference-side control-plane client.
-
(source¶WeightSource) –WeightSourceof(name, tensor)pairs to send each round.
Raises:
-
ValueError–If the backend is not registered.