NCCL M2N Engine¶
The nccl_m2n weight transfer engine uses NCCL M2N to reshard weights between two disjoint meshes of GPUs — the trainer and the inference workers — in a single collective per parameter. Unlike the broadcast-based NCCL engine, the trainer sends its local shard as-is: the transport does not require a trainer-side gather. A source may still gather internally when its declared layout requires it.
When to Use NCCL M2N¶
- The trainer and the inference engine use different parallelism layouts (e.g. trainer FSDP/EP, inference TP) and gathering full tensors on the trainer is expensive
- Training and inference are on separate GPUs, possibly across nodes
- You want weight sync off the critical path of an async RL loop
Requirements¶
This backend depends on the nccl-extensions Python package (and its nccl4py dependency), which vLLM does not install. Install both against a compatible NCCL 2.30.5 or newer runtime. The backend creates its data-plane communicator through nccl4py and passes that communicator directly to nccl.m2n.
The backend imports the runtime lazily, so vLLM is unaffected unless you select it.
How It Works¶
- Init: trainers declare their source layout. All ranks agree on the source plan, the NCCL unique ID and runtime readiness before creating the communicator. Workers refuse pipeline parallelism, models that
check_plannablerejects, and parameters with no placement on the declared mesh within M2N limits. Init does not touch the model. - First update: workers plan destinations from the model's own loaders, and all ranks agree one placement per parameter on the declared mesh. Later updates reuse the plan.
- Updates:
trainer_initperforms the first full update before returning. Every update streams weights in declaration order betweenstart_reloadandfinish_reload.
Pause generation during weight updates, including the initial sync in trainer_init.
- A failed init leaves the current weights serving.
- A failed update leaves the model incomplete until a full update succeeds; resend every parameter, since retrying finish alone is not supported.
- If the initial placement agreement is rejected, both sides release their transport and
trainer_initcan be retried. After any other failure, callshutdown_weight_transfer_engineon the workers before initializing again; workers still inside an update refuse it untilfinish_weight_updateends that update.
Receiver loading¶
The shared plan determines where checkpoint bytes belong, including fused parameters:
- Direct: receive into one contiguous destination in the wire dtype.
- Staged: receive a supported box into reusable storage, then copy into the planned views. Copies preserve loader casts and strides.
- Fallback: receive a full tensor and yield it to a single lazy
model.load_weights(...)call.
All declared names keep their place in the collective sequence. A worker that needs nothing of a name receives into scratch storage without copying it into the model. Trainers consume all of iter_local(), including any owner-side collectives.
The destination mesh is common to every parameter. M2NTrainerInitInfo.dst_mesh_dims declares it, with axis 0 replicating and axis 1 sharding; omitted, it is (num_workers, 1). Parameters can use different placements on that mesh or replicated receives. Per-parameter destination meshes and automatic mesh factorization are not supported.
With the default (num_workers, 1) mesh every TP rank receives full tensors through staging; pass dst_mesh_dims=(dp, tp) for direct TP-sharded receives.
Configuration¶
from vllm import LLM
from vllm.config import WeightTransferConfig
llm = LLM(
model="my-model",
weight_transfer_config=WeightTransferConfig(backend="nccl_m2n"),
)
Trainer Side¶
The trainer uses the stateful trainer engine. m2n plans each reshard from the parameter's source layout, which a WeightSource declares on its ParamMeta (owners, mesh_shape, placements), and sends what iter_local() yields: each rank's local tensor. ModuleSource covers the common case: it reads each parameter's device mesh and placements.
from vllm.distributed.weight_transfer import (
ModuleSource,
RayVLLMWeightSyncClient,
WeightTransferTrainerFactory,
)
from vllm.distributed.weight_transfer.m2n_trainer import M2NTrainerInitInfo
# Called on every trainer rank; rank 0 is the sender.
engine = WeightTransferTrainerFactory.trainer_init(
init_info=M2NTrainerInitInfo(
master_address=master_address,
master_port=master_port,
world_size=num_trainer_ranks + num_inference_workers,
num_trainer_ranks=num_trainer_ranks,
rank=my_rank,
),
client=RayVLLMWeightSyncClient(llm_handle),
source=ModuleSource(model),
)
# Each round, on every trainer rank:
engine.send_weights()
send_weights() drives the whole round — start_weight_update, update_weights, finish_weight_update. Every rank in the communicator must be inside the same reshard, so update_weights has to be in flight while the trainer sends; the engine runs it on a helper thread so the trainer loop does not have to open-code that.
Trainers with a custom producer (a Megatron export, MoE re-fusing) subclass
WeightSource, declare each layout on ParamMeta and implement iter_local().
A 1-D mesh is described over m2n's 2-D mesh with a size-1 second axis. Each
yielded tensor must match its declared dtype and local shape, and is kept alive
in stream order until its reshard completes.
See examples/rl/rlhf_m2n.py for a runnable FSDP → TP example.
Limitations¶
- vLLM validates tensor rank and dtype support at init; the installed M2N runtime validates them again for each reshard.
- No pipeline-parallel trainers. Every parameter must be owned by all trainer ranks, in rank order (
ownersunset or(0, ..., T-1)), with a 1-D or 2-D mesh. Partitioned owners, such as one group per pipeline stage, are rejected at init. Partialplacements cannot be sent as shards.ModuleSourcedeclares such a parameter replicated and gathers it initer_local().- Requires a model that reloads directly. Layerwise reload, online quantization, EPLB, draft updates and pipeline-parallel inference workers are refused.
- Each update must resend all declared weights.
- A model that
plan_loadsorcheck_plannablerefuses cannot usenccl_m2n; there is no whole-modelload_weightsfallback. - A reshard cannot be issued from inside a CUDA graph capture (weight updates run outside capture, so this only matters for custom callers).
- vLLM checks every parameter's shard overlap against conservative 16-source and 64-target capacities at init. The installed M2N runtime remains authoritative and validates each reshard again.
- Each communicator holds a cached staging pool (~256 MiB by default: 4 channels x 64 MiB).