nemo_rl.models.generation.trtllm.trtllm_backend#

TRT-LLM WorkerExtension for NCCL / IPC weight synchronisation.

Injected into TRT-LLM’s RayGPUWorker via ray_worker_extension_cls.

  • update_weights_from_collective — NCCL broadcast via packed_broadcast_consumer, used in non-colocated mode.

  • update_weights_via_ipc_zmq — CUDA IPC handles streamed over a per-GPU ZMQ socket, used in colocated mode (NCCL can’t form a group when train and inference processes share the same physical GPU).

Module Contents#

Classes#

NcclExtension

NCCL-based weight update extension for TRT-LLM Ray workers.

Functions#

_require_fp8_refit_hooks

Require TRT-LLM hooks for transactional FP8 refits.

API#

nemo_rl.models.generation.trtllm.trtllm_backend._require_fp8_refit_hooks(model_loader: Any) → None#

Require TRT-LLM hooks for transactional FP8 refits.

class nemo_rl.models.generation.trtllm.trtllm_backend.NcclExtension#

Bases: tensorrt_llm.llmapi.rlhf_utils.WorkerExtension

NCCL-based weight update extension for TRT-LLM Ray workers.

Attributes set by TRT-LLM’s mixin injection (from RayGPUWorker): self.engine – PyExecutor instance self.device_id – int GPU ordinal

The extension has no constructor of its own, so the refit state below is declared here and populated by the refit setup calls.

_local_expert_lookup: Optional[nemo_rl.models.generation.trtllm.quantization.fp8.LocalExpertLookup]#

None

pp_comm_groups: Optional[dict[int, Any]]#

None

nccl_reshard_refit_info: Optional[dict[str, Any]]#

None

misc_state_dict_info: Optional[dict[str, Any]]#

None

hf_to_local_param_map: Optional[Any]#

None

_pending_bulk_reload: Optional[dict[str, Any]]#

None

_moe_weight_owner_map: Optional[dict[str, Any]]#

None

_reload_batch_bytes: int#

None

_refit_num_streams: int#

2

init_collective(
rank_prefix: int,
ip: str,
port: int,
world_size: int,
train_world_size: int,
) → None#
start_gpu_profiling() → None#

Start CUDA profiler on this GPU worker (nsys capture-range trigger).

stop_gpu_profiling() → None#

Stop CUDA profiler on this GPU worker.

prepare_refit_info(state_dict_info: dict[str, Any]) → None#
update_weights_from_collective(
*,
drain: bool = True,
recompute_kv: bool = False,
) → bool#

Receive weights via NCCL broadcast and update model parameters.

Parameters:
  • drain – If True (default), wait for all in-flight requests to drain before applying weights — exclusive engine access. If False, the swap happens at a scheduler step boundary with in-flight requests still in the engine (in-flight weight update).

  • recompute_kv – If True, call recompute_active_requests() after the refit: in-flight requests are re-prefilled under the new weights and the prefix cache is reset. If False, in-flight requests keep decoding with their current KV cache. With drain=True there are no in-flight requests, so True reduces to a prefix-cache reset.

get_zmq_address() → str#
maybe_init_zmq() → None#
update_weights_via_ipc_zmq() → bool#

Receive weights via CUDA-IPC + ZMQ, reload model.

Trainer sends (ipc_handle, list_keys, used_bytes) chunks; end of refit is signalled by IPCProtocol.COMPLETE.

cleanup_zmq() → None#

Close ZMQ socket if open — called from worker shutdown.

report_device_id() → str#
_finish_refit(recompute_kv: bool) → None#

Finalize a refit inside control_action.

_reload_bucket(weight_list) → None#

Convert routed experts if the engine is FP8/MXFP8, then reload.

init_nccl_reshard_comm_group(
rank_prefix: int,
pp_ips: list[str],
pp_ports: list[int],
pp_size: int,
train_ranks_per_stage: int,
sub_world_size: int,
) → None#

Join the bulk-path communicator of every PP stage.

Parked at a step boundary for the same reason as init_collective: ncclCommInitRank across train + inference ranks deadlocks against the executor loop’s own NCCL object collectives.

prepare_nccl_reshard_refit_info(refit_info: dict) → None#

Keep the per-layer transfer plan and map its bulk params to local slots.

_build_expert_local_param_map(refit_info: dict)#

One LocalParamSpec per grouped routed-expert projection.

pre allocates a canonical [E_local, out, in] bf16 staging buffer for this rank’s EP slice, post turns it into per-expert HF entries (FP8/MXFP8 for quantized engines) and hands them to TRT-LLM’s reload. The EP slice must be the slots TRT-LLM loads (initial_local_expert_ids); a mismatch is an error, never a silent partial update.

_convert_received_experts(
prefix: str,
projection: str,
expert_ids: list[int],
stack: torch.Tensor,
) → dict#

Turn one received [E_local, out, in] stack into loader-ready tensors.

_queue_received_experts(
prefix: str,
projection: str,
expert_ids: list[int],
stack: torch.Tensor,
) → None#

Convert one received stack and reload it, batched under the byte budget.

static _direct_expert_load_enabled() → bool#

Load received expert stacks straight into their MoE modules.

model_loader.reload walks every module of the model per call and ends with a stream synchronize, so each call stalls the receive loop until every transfer enqueued so far has landed. The direct path replays the loader’s MoE branch for just the modules the queued stacks belong to and leaves the copies enqueued. NRL_TRTLLM_REFIT_DIRECT_EXPERT_LOAD=0 falls back to the loader.

_moe_weight_owners() → dict#

expert prefix -> module for every MoE weight owner, built once.

_moe_weight_owner(prefix: str) → Any#

The MoE weight owner of an expert prefix, matched as a dotted suffix.

Owners are named from the engine’s root module, while weight prefixes are named from the module the mapper loads into: a VLM wrapper (Qwen3.5) nests its decoder under llm., and the mapper is bound to that decoder only once load_weights has run.

_load_expert_weights_direct(weights: dict) → dict#

Replay the loader’s per-module MoE branch for the queued stacks.

This copies upstream TRT-LLM code at the pinned [tool.trtllm].ref: the per-module branch of _load_weights_impl_v2 (tensorrt_llm/_torch/models/modeling_utils.py) and, for mappers that take allow_partial_loading (Qwen3.5), the preprocess_weights call of the model’s load_weights. Other mappers (Qwen3 MoE) inherit the base no-op that returns None, and their models never call it, so it is skipped. It skips the params_map rename, should_skip_module, does_require_special_handling and mark_consumed steps, which do not apply to routed-expert stacks; re-check this copy on every TRT-LLM bump.

Returns the weights that no MoE module claimed: only entries the mapper filtered out of every owner’s prefix (loaded by the generic loader afterwards).

_flush_received_experts() → None#

Reload every queued converted stack in one loader call.

_recv_bulk_params() → None#

Receive every bulk param of every PP stage into its local slots.

_receive_and_load_misc_params() → None#

Misc params: the collective path’s packed broadcast + per-bucket reload.

nccl_reshard_refit(
refit_timeout_s=None,
*,
drain: bool = True,
recompute_kv: bool = False,
) → bool#

Receive a refit shard-to-shard, then the misc broadcast, then finalize.

Same engine bracket as update_weights_from_collective: drain picks exclusive access at a step boundary (True) or the in-flight weight update (False, recompute_kv as there); the refit lifecycle (torch.compile unwrap, deferred weight processing, prefix-cache reset) is delegated to WorkerExtension via begin_weight_update / finalize_weight_update / finish_weight_update.