Skip to content

perf(data-plane): broadcast unpadded rows and pad on each rank - #4530

Merged
ZhiyuLi-Nvidia merged 8 commits into
mainfrom
zhiyul/fix/jagged-fetch-broadcast
Oct 12, 2026
Merged

ZhiyuLi-Nvidia merged 8 commits into
mainfrom
zhiyul/fix/jagged-fetch-broadcast

Conversation

@ZhiyuLi-Nvidia

@ZhiyuLi-Nvidia ZhiyuLi-Nvidia commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #4526: please review and merge that first.

What

On the replica-group fetch path, the leader used to pad every row to the cross-DP GLOBAL_FORWARD_PAD_SEQLEN before broadcasting the DP batch to its TP×CP×PP peers. With this change:

  • The leader materializes the batch unpadded (layout="jagged", no pad target).
  • _broadcast_batched_data_dict gains a nested branch: each nested tensor is sent as its rows flattened, plus per-row shapes and layout, and rebuilt on every rank. int16 is sent as bytes.
  • Every rank pads after the broadcast with exactly the materialize rules. Those rules moved into codec.pad_field, and codec.pad_batch applies them to a whole batch.
  • NRL_ROUTE_BCAST=dense keeps the fully padded fetch, because that path builds the routes table on the leader before the broadcast.

Downstream code is unchanged: it still receives the same padded batch.

Why

Padding was being sent over the broadcast and staged on the GPU, only to be stripped again by sequence packing per microbatch. The data path is documented in the new section "Inside one DP rank — fetch, broadcast, padding, packing" in nemo_rl/data_plane/README.md.

Tests (GB300, NCCL)

  • test_codec_jagged.py, test_leader_broadcast.py, test_rollout_reassembler.py, test_local_sft.py, test_seqpack_equivalence.py: 44 passed, 1 skipped. New tests:
    • test_pad_batch_matches_padded_materialize: padding afterwards equals padded materialization. Covers an unpadded field, an already-dense field, a 3-D field, a full-vocab (OPD) field, and per-field pad values.
    • test_nested_tensors_survive_nccl_leader_broadcast: int64, int16 and 3-D float nested fields plus a dense field. Checks values, layout, and that every field lands on the same device as the regular tensors.
  • 2-node sync GRPO (Qwen3-30B-A3B, TP2/EP4, router replay, workplace_assistant), 8 steps per arm, compared with the fix(data-plane): broadcast route fragments instead of the dense routes table #4526-only runs:
#4526 only + this PR
packing on: step time after warm-up 35.7–43.6 s 36.5–42.3 s
packing off: step time after warm-up 66.5–72.8 s 67.1–78.6 s
token_mult_prob_error 1.004–1.038 1.003–1.084
masked seqs / routes row coverage / sentinel 0 / 1.0 / 0.0 0 / 1.0 / 0.0

Update: routes padded per microbatch (75515de)

  • _assemble_routed_experts now returns routed_experts as jagged per-row tensors (one flat int16 buffer + row offsets), not a padded [rows, pad_seqlen, 56, 6] table.
  • Fields listed in schema.MICROBATCH_PADDED_FIELDS (today only routed_experts, pad value -1) skip the full-batch padding in _fetch. The Megatron microbatch iterator pads them with codec.pad_field to the microbatch's input_ids width right before .to("cuda"), so the model sees the same values as before.
  • BatchedDataDict.slice / select_indices handle nested rows (zero-copy view for contiguous slices), and truncate_tensors skips them.

Tests on the final code (GB300):

Check Result
data-plane suite + test_batched_data_dict (base venv) 96 passed, 1 skipped
test_worker_route_assembly, test_rollout_reassembler (--nemo-gym-only, vLLM worker venv) 17 passed
test_routed_experts_padding: per-microbatch padding reproduces the old sentinel table passed
2-node sync A/B, packing on: step time after warm-up 36.1–43.3 s
2-node sync A/B, packing off: step time after warm-up 67.0–71.6 s
2-node route metrics (masked / coverage / sentinel) 0 / 1.0 / 0.0
2-node token_mult_prob_error 1.003–1.115

12-node A/B vs #4526 (4f0400b)

nano35 RLVR with router replay on 12 nodes: TP4×CP4, PP1, EP16, SP, DP=1, so all 16 training ranks form one replica group. cluster.segment_size=4, 7 steps per arm. Each fetch is timed with torch.cuda.synchronize and a GPU peak-memory reset; this logging is not part of either PR.

#4526 + this PR
bytes sent per row 24.1 MB 7.9 MB (3.1× less)
largest single fetch 5.18 GB 1.87 GB
GPU memory added by a fetch, leader (max / mean) 18.7 / 7.5 GiB 1.9 / 0.7 GiB
GPU memory added by a fetch, peers (max) 18.6 GiB 1.7 GiB
fetch + broadcast + pad, per fetch (mean / max) 0.162 / 0.344 s 0.110 / 0.213 s
broadcast alone (mean) 0.047 s 0.014 s
total step time, 7 steps 1121 s 1110 s (within noise)
masked seqs / gen_kl_error / token_mult_prob_error 0 / 4.7e-4 / 1.013 0 / 4.9e-4 / 1.014
  • W&B: #4526, this PR. Both runs finished all steps with exit code 0.
  • "GPU memory added by a fetch" is the peak allocated during the fetch minus memory allocated just before it, on the same rank.
  • Means leave out each arm's first fetch (warm-up). The batches are not paired: async sampling gives each fetch a different batch of 16–192 rows. Bytes per row is the like-for-like comparison.
  • Step time is unchanged, since a fetch takes well under 1 s of a 100–230 s step. The gain is peak memory and bytes sent.
  • An earlier 12-node smoke that masked every sequence used cluster.segment_size=2. Every PR and main masked the same way at segment size 2, and every segment-4 run was clean, so the PRs were not the cause.

Nightly coverage (from #4526)

#4526 adds the ...-token_capture-r3-defer nightly (deferred routes, CP=2 replica group). This PR adds nothing new to it. The real .sh on top of this PR passes every gate with exit 0 (W&B). The direct-routes path (defer=false) that this PR broadcasts jagged is covered by the existing grpo-qwen3-30ba3b-*-megatron-cp2-r3* nightlies.

Ablation on that recipe, 4 nodes × 4 GPUs, 6 steps per arm:

Arm Bytes sent / row Broadcast p50 GPU peak added Routes held / row W&B
NRL_ROUTE_BCAST=dense 1.76 MB 0.141 s 0.15 GiB 1.71 MB 3bmrjtyj
#4526 fragments 0.40 MB 0.132 s 0.02 GiB 1.77 MB gdfnjwxq
+ this PR 0.37 MB 0.142 s 0.02 GiB 0.68 MB gh9fye7x

The new part from this PR is the last column: routes stay jagged after assembly and get padded per microbatch, so each rank holds 0.68 MB/row of routes instead of 1.77 MB. All arms: 0 masked, no NaN, token_mult_prob_error ≈ 1.02.

Not in this PR

  • Every rank still materializes a padded batch on CPU. The microbatch iterator and _pack_sequences_for_megatron consume padded [B, S] rows. Removing that means packing directly from unpadded rows, which is a follow-up.
  • The broadcast still stages each field whole on GPU (no chunking).

🤖 Generated with Claude Code

@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 added this pull request to stack #4531 October 6, 2026 08:12
@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor Author

/ok to test 4f0400b

@ZhiyuLi-Nvidia ZhiyuLi-Nvidia added Run CICD CI:L1 Run doctests, unit tests, and functional tests labels Oct 7, 2026
zyzhou5
zyzhou5 previously approved these changes Oct 8, 2026

@zyzhou5 zyzhou5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ZhiyuLi-Nvidia
ZhiyuLi-Nvidia force-pushed the zhiyul/fix/jagged-fetch-broadcast branch 2 times, most recently from 66e2f62 to 1071fb4 Compare October 9, 2026 18:03
@ZhiyuLi-Nvidia
ZhiyuLi-Nvidia force-pushed the zhiyul/fix/jagged-fetch-broadcast branch from 1071fb4 to a0e443a Compare October 9, 2026 18:35
@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor Author

/ok to test a0e443a

@ZhiyuLi-Nvidia
ZhiyuLi-Nvidia force-pushed the zhiyul/fix/jagged-fetch-broadcast branch 2 times, most recently from 61aff80 to ebbad87 Compare October 11, 2026 01:37
@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor Author

/ok to test ebbad87

yuki-97
yuki-97 previously approved these changes Oct 11, 2026

@yuki-97 yuki-97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sending rows unpadded and padding on each rank is a clear win for the replica-group fetch: on the 12-node TP4 x CP4 run, the GPU memory a fetch adds drops from 18.7 to 1.9 GiB on the leader and from 18.6 to 1.7 GiB on peers, with 3.1x fewer bytes per row sent and step time within noise. Keeping routed_experts jagged until the microbatch iterator pads it takes the padded routes table off the fetch path entirely.

The 12-node A/B against #4526, with the per-fetch memory and timing instrumentation spelled out, makes the gain easy to trust.

Scope: the replica-group _fetch path (jagged fetch, nested broadcast, per-rank pad_batch), the pad_field / pad_batch split of materialize, the jagged _assemble_routed_experts, and the nested-row handling in BatchedDataDict.

Checked:

  • Padding parity — pad_field reproduces the old materialize rules key by key, and no production caller used layout="jagged" before, so nothing relied on the old widening.
  • Nested broadcast — every rank enters the same collectives (sizes come from the shared descriptor) and int16 goes out as bytes.
  • Route padding — padding positions never reach the model: the non-packed path repairs them with _fill_routed_experts_padding, and the packed path shards by per-row seq_lengths / cu_seqlens.
  • Nested rows through BatchedDataDict — slice, select_indices, chunk, reorder_data and from_batches keep rows aligned with input_ids.

LGTM.

@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor Author

/ok to test 4ac15c4

@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor Author

/ok to test 7fc2b94

Base automatically changed from zhiyul/fix/route-fragment-replica-broadcast to main October 12, 2026 15:03
ZhiyuLi-Nvidia and others added 3 commits October 12, 2026 23:03
The replica-group leader now materializes the fetched batch unpadded
(layout="jagged", no pad target) and _broadcast_batched_data_dict carries
nested tensors as flat rows plus per-row shapes. Every rank pads afterwards
with the same rules as materialize (codec.pad_batch / _pad_leaf), to the
cross-DP GLOBAL_FORWARD_PAD_SEQLEN the microbatch iterator expects.
NRL_ROUTE_BCAST=dense keeps the fully padded fetch, since that path builds
the routes table on the leader before the broadcast.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
Route assembly now returns routed_experts as jagged per-row tensors (one
flat int16 buffer plus row offsets) instead of a padded
[rows, pad_seqlen, L, K] table. Fields listed in
schema.MICROBATCH_PADDED_FIELDS (today: routed_experts, pad -1) skip the
full-batch pad in _fetch; the Megatron microbatch iterator pads them with
codec.pad_batch to the microbatch's input_ids width right before .to("cuda"),
so the model sees the same values as before.

BatchedDataDict.slice/select_indices handle nested rows (zero-copy view for
contiguous slices) and truncate_tensors skips them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
ZhiyuLi-Nvidia and others added 5 commits October 12, 2026 23:03
The microbatch iterator now pads MICROBATCH_PADDED_FIELDS with a dedicated
BatchedDataDict-level pad_nested_fields instead of codec.pad_batch, so the
routes path no longer runs through the wire-field rule that carries the
multimodal and opd_full exclusions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
…ad_batch

Replace the extracted _pad_leaf (duplicated pad assignment, re-checked
conditions) with _pad_field: multimodal rows return as-is, nested rows are
padded, and only non-opd_full 2D+ fields narrower than pad_to_seqlen are
widened. materialize applies it for layout="padded" only; layout="jagged"
now passes fields through untouched (its only caller passes no pad target).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
…width

- BatchedDataDict.chunk/reorder_data/shard_by_batch_size/from_batches keep
  nested routes per row (empty selections included).
- pad_field sizes nested output from offsets: NJTs built with
  nested_tensor_from_jagged pad to total tokens otherwise.
- Replica broadcast ships values()+offsets instead of per-row copies.
- One pad helper (codec.pad_field) for batch and microbatch padding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Zhiyu Li <[email protected]>
@yuki-97
yuki-97 force-pushed the zhiyul/fix/jagged-fetch-broadcast branch from 7fc2b94 to 17f9ac8 Compare October 12, 2026 15:03
@yuki-97

yuki-97 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

/ok to test 17f9ac8

@ZhiyuLi-Nvidia
ZhiyuLi-Nvidia merged commit 3df254d into main Oct 12, 2026
117 of 118 checks passed
@ZhiyuLi-Nvidia
ZhiyuLi-Nvidia deleted the zhiyul/fix/jagged-fetch-broadcast branch October 12, 2026 20:10
joyang-nv added a commit that referenced this pull request Oct 14, 2026
Since #4530 the data plane leaves routed_experts jagged when a replica
group has more than one rank (automodel TP > 1 on the single-controller
path), and only the Megatron data path pads it per microbatch. Mirror
that in the automodel data path:
- make_processed_microbatch_iterator pads MICROBATCH_PADDED_FIELDS to the
  microbatch's input_ids width. Pad rows get -1, the missing-route
  sentinel, which keeps the live router selection.
- check_sequence_dim skips nested tensors.
Routes that already arrive padded (the driver path) are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jonas Yang <[email protected]>
joyang-nv added a commit that referenced this pull request Oct 14, 2026
Since #4530 the data plane leaves routed_experts jagged when a replica
group has more than one rank (automodel TP > 1 on the single-controller
path), and only the Megatron data path pads it per microbatch. Mirror
that in the automodel data path:
- make_processed_microbatch_iterator pads MICROBATCH_PADDED_FIELDS to the
  microbatch's input_ids width. Pad rows get -1, the missing-route
  sentinel, which keeps the live router selection.
- check_sequence_dim skips nested tensors.
Routes that already arrive padded (the driver path) are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jonas Yang <[email protected]>
joyang-nv added a commit that referenced this pull request Oct 14, 2026
Since #4530 the data plane leaves routed_experts jagged when a replica
group has more than one rank (automodel TP > 1 on the single-controller
path), and only the Megatron data path pads it per microbatch. Mirror
that in the automodel data path:
- make_processed_microbatch_iterator pads MICROBATCH_PADDED_FIELDS to the
  microbatch's input_ids width. Pad rows get -1, the missing-route
  sentinel, which keeps the live router selection.
- check_sequence_dim skips nested tensors.
Routes that already arrive padded (the driver path) are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jonas Yang <[email protected]>
yuki-97 pushed a commit that referenced this pull request Oct 17, 2026
Since #4530 the data plane leaves routed_experts jagged when a replica
group has more than one rank (automodel TP > 1 on the single-controller
path), and only the Megatron data path pads it per microbatch. Mirror
that in the automodel data path:
- make_processed_microbatch_iterator pads MICROBATCH_PADDED_FIELDS to the
  microbatch's input_ids width. Pad rows get -1, the missing-route
  sentinel, which keeps the live router selection.
- check_sequence_dim skips nested tensors.
Routes that already arrive padded (the driver path) are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jonas Yang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI:L1 Run doctests, unit tests, and functional tests Run CICD

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants