Skip to content

fix(vllm): router replay for MoE models with >255 experts - #4543

Open
pthombre wants to merge 5 commits into
mainfrom
pthombre/router-replay-uint16-chunk-cap
Open

pthombre wants to merge 5 commits into
mainfrom
pthombre/router-replay-uint16-chunk-cap

Conversation

@pthombre

@pthombre pthombre commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Two small changes needed to run router replay on MoE models with more than 255 experts (Nemotron 3.5 Super, 512 experts, top-22, 88 layers) in the single-controller async GRPO path. Both were validated on two 4-hour 92-node legs on oci-aga (32 train + 36 generation + 4 Gym + 20 judge nodes, 8192 sequences per step, 81,920 context): 12 steps per leg, routed-expert row coverage 1.0 on every step, no host OOM.

They are independent of #4263 (leader-broadcast layout and int16 byte-view fixes) and apply cleanly on top of it.

1. fix(vllm): reduce routed-expert ids in int64 for models with >255 experts

Behavior on main. vLLM returns per-token routed-expert ids in its own storage type: uint8 up to 256 experts, uint16 above. nemo_rl/models/generation/vllm/utils.py then narrows them to the resolved signed carry dtype (int16 for 512 experts). One call on that path assumes a fully supported dtype: _as_routed_experts_tensor runs a one-time overflow guard, int(tensor.max()), before the narrowing cast.

PyTorch's unsigned 16/32-bit dtypes have no max reduction, so on a 512-expert model the very first captured batch raised NotImplementedError: "max_all" not implemented for 'UInt16' and no rollout ever reached the trainer. Every model run through router replay before had at most 256 experts, which is why main never hit this.

Change. Reduce in int64 (tensor.to(torch.int64).max()) before narrowing to the carry dtype. The stored and transported dtype is unchanged: it is still the one resolve_routed_experts_dtype returns (int16 for 512 experts). The guard has to reduce in a type wider than that carry dtype, otherwise a uint16 id >= 32768 would wrap negative before max() and the guard would miss exactly the overflow it checks for. The only new cost is a one-time int64 copy of the checked slice.

The default padding route (torch.arange(top_k, dtype=routed_experts_dtype)) is untouched: routed_experts_dtype is always a signed carry dtype, which arange supports.

2. feat(sc): cap the streaming train chunk with async_rl.max_groups_for_streaming_train

Behavior on main. The train pump in SingleControllerActor streams a step as chunks: each iteration calls sampler.select(min=min_groups_for_streaming_train, max=target_groups - groups_dispatched), and _finalize_selection is greedy, returning every eligible group up to max without waiting. The upper bound is therefore the remainder of the step. The first chunk is small because the buffer is empty, but by the second chunk generation has run ahead, and the pump hands over everything that is ready, up to the full step. With a 512-prompt step and 36 generation nodes that was a chunk of ~250 groups (~4,000 rows).

Without router replay this is harmless: a row is a few kilobytes. With router replay each DP-replica leader fetches its slice of the chunk and densifies it on the host before the NCCL broadcast, and the routed-expert field is int16 [rows, padded_len, layers, top_k], 3,872 bytes per token for Super. A ~926-row slice padded to 55k tokens is a single 185 GiB host tensor (233 GiB with a longer tail row), which OOM-killed the leader's node on every attempt at the second chunk of step 0.

Change. Add an optional async_rl.max_groups_for_streaming_train (PositiveInt, default None) and clamp the pump's max_prompt_groups to min(step_remainder, max_groups_for_streaming_train) when the cap is set. Unset (the default) leaves the chunk bounded only by the step remainder, exactly as before, so existing streaming configs see no behavior change. A validator rejects a max below the min; min_groups_for_streaming_train is tightened to PositiveInt. The sampler, staleness window and training claims are untouched.

Sizing the cap. Per-leader host bytes ≈ rows_per_leader × padded_len × layers × top_k × 2, with rows_per_leader ≈ chunk_groups × generations / dp_replicas. Measured on Super at DP4, 81,920 context:

cap max rows per leader max routed_experts tensor peak host memory, head / train node steady step time
16 118 35 GiB 549 / ~400 GB 1003 s
32 218 64 GiB 688 / ~520 GB 1005 s
256 (previous default) ~1000 185–233 GiB OOM —

Step time is unchanged because the step is training-bound (policy training ≈ 97 % of the step); chunking only changes fetch cadence.

Usage

async_rl:
  min_groups_for_streaming_train: 8
  max_groups_for_streaming_train: 16   # optional; unset -> no cap (step remainder, as before)

Testing

  • 2 × 4 h legs on 92 nodes (jobs 1009240 cap 16, 1027693 cap 32): 12 steps each, reward 1.0–1.1, routed_experts_row_coverage 1.0, token_mult_prob_error 1.007, no OOM kills.
  • Unit tests, run in the same nightly-gym container as the 92-node legs:
    • tests/unit/models/generation/test_vllm_utils.py: uint8/uint16/uint32 backend ids through pad_and_align_routed_expert_indices (prompt and completion routes, default padding route), and the overflow guard seeing the unwrapped uint16 id.
    • tests/unit/single_controller/test_resiliency_config.py: max_groups_for_streaming_train default None, max < min rejected, max == min allowed, non-positive bounds rejected.

Checklist

  • Change is backward compatible: new field defaults to None, which keeps the previous step-remainder chunk bound; existing configs are unaffected.
  • Unit tests added (see Testing)
  • Docs: docs/guides/single-controller.md field definitions and the grpo_math_1B_megatron_single_controller.yaml exemplar

🤖 Generated with Claude Code

…erts

Routed-expert ids ride as uint16 when a model has more than 255 experts.
torch has no max() or arange() kernel for the unsigned carry dtypes, so the
range check and the default-route construction raised NotImplementedError
on the 512-expert Nemotron 3.5 Super. Reduce and build in int64, then
narrow back to the carry dtype.

Carried from smahdavi's nemo-rl-2026-09-29 working tree (uncommitted there).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
(cherry picked from commit 5918e89)
Signed-off-by: pthombre <[email protected]>
…streaming_train

The train pump dispatched target_groups - groups_dispatched, i.e. every ready
group up to the rest of the step, so a rollout backlog produced chunks of
hundreds of groups. With router replay the replica-leader broadcast
materialises sequences x padded_len x layers x topk int16 routes on every GPU
of the replica, so unbounded chunks OOM on large MoE models. Add an optional
upper bound (validated >= min) and clamp the chunk to it.

Carried from smahdavi's nemo-rl-2026-09-29 working tree (uncommitted there).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
(cherry picked from commit 953060f)
Signed-off-by: pthombre <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Oct 6, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ZhiyuLi-Nvidia

ZhiyuLi-Nvidia commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Thank you @pthombre

Do you think we can do something like dtype autoset or resuse the function based on the num_experts?

def resolve_routed_experts_dtype(num_experts: Optional[int]) -> torch.dtype:
"""Return the narrowest signed dtype that fits expert ids and the -1 sentinel."""
if num_experts is None:
return ROUTED_EXPERTS_FALLBACK_DTYPE
if num_experts - 1 <= torch.iinfo(torch.int8).max:
return torch.int8
if num_experts - 1 <= torch.iinfo(torch.int16).max:
return torch.int16
return torch.int32

So we can efficiently use minimal dtype for router indices. That's really helpful as if we use int32 instead of int64 then we halve the communication.

I think the reason we don't use uint is that we need -1 for pad index.

…signed ids and chunk bounds

routed_experts_dtype is always a signed carry dtype from
resolve_routed_experts_dtype, which torch.arange supports, so the int64 build
of the default padding route was unnecessary. Only the one-time overflow guard
needs the wider reduction: it must see the unwrapped backend id to notice an
id that does not fit the carry dtype.

Expose the streaming chunk cap as AsyncRLConfig.streaming_chunk_cap() so the
train pump's clamp is testable without a Megatron worker, document
async_rl.max_groups_for_streaming_train in the single-controller guide and
the exemplar YAML, and add unit tests for uint8/uint16/uint32 backend ids
through pad_and_align_routed_expert_indices, the overflow guard on an
unwrapped uint16 id, and the config bounds.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: pthombre <[email protected]>
@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Oct 6, 2026
@pthombre pthombre changed the title Router replay for MoE models with >255 experts: int64 route-id reduction and streaming chunk cap fix(vllm): router replay for MoE models with >255 experts (int64 route-id reduction, streaming chunk cap) Oct 6, 2026
@pthombre pthombre added the CI:L0 Run doctests and unit tests label Oct 6, 2026
@pthombre

pthombre commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 92b28eb

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team October 6, 2026 23:17
@pthombre

pthombre commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test c8dbd5a

@pthombre pthombre changed the title fix(vllm): router replay for MoE models with >255 experts (int64 route-id reduction, streaming chunk cap) fix(vllm): router replay for MoE models with >255 experts Oct 6, 2026
@pthombre
pthombre force-pushed the pthombre/router-replay-uint16-chunk-cap branch from c8dbd5a to 507d322 Compare October 7, 2026 07:22
@pthombre

pthombre commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 507d322

…ming_train is unset

An unset cap used to fall back to min_groups_for_streaming_train, which turned
every existing streaming config's chunks into exactly-min-sized ones and broke
the checkpoint tests that pin one chunk per step. None now means no cap: the
chunk stays bounded by the step remainder as before, and only an explicit
max_groups_for_streaming_train clamps it. Drop the streaming_chunk_cap()
helper, read the field directly in the pump, and give the pump test stub the
new field.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: pthombre <[email protected]>
@pthombre

pthombre commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test efab9b8

@pthombre

pthombre commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @ZhiyuLi-Nvidia. The int64 only appears as a transient inside the one-time overflow guard in _as_routed_experts_tensor (utils.py#L161). That copy exists just to compute max() and is dropped; the unchanged last line still narrows the real tensor to the resolved dtype (utils.py#L171), and the worker still picks that dtype from num_experts (vllm_worker.py#L688). Because the guard runs once per process (G_ROUTED_EXPERTS_RANGE_CHECKED), the cost is a single int64 copy of one request's routes, not per-request.

Why the guard can't reduce in the carry dtype itself: its job is to catch a mis-detected expert count, i.e. an id that doesn't fit the resolved dtype. Casting uint16 -> int16 first would wrap any id >= 32768 negative, so max() could never exceed iinfo(int16).max and the check would pass silently. It has to reduce in something wider than the type it validates; int64 is the simplest choice that covers uint8/uint16/uint32 inputs. The new test test_pad_and_align_overflow_guard_sees_unwrapped_unsigned_ids pins exactly this case (a uint16 id of 40000 against int16 must raise).

@cmunley1

Copy link
Copy Markdown
Contributor

i hit the same issue and came to the same fix as here

@terrykong terrykong added CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) and removed CI:L0 Run doctests and unit tests labels Nov 5, 2026
@terrykong terrykong removed the Documentation Improvements or additions to documentation label Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants