Repository navigation
perf(data-plane): broadcast unpadded rows and pad on each rank - #4530
Conversation
|
/ok to test 4f0400b |
66e2f62 to
1071fb4
Compare
1071fb4 to
a0e443a
Compare
|
/ok to test a0e443a |
61aff80 to
ebbad87
Compare
|
/ok to test ebbad87 |
yuki-97
left a comment
There was a problem hiding this comment.
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_fieldreproduces the oldmaterializerules key by key, and no production caller usedlayout="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-rowseq_lengths/cu_seqlens. - Nested rows through
BatchedDataDict—slice,select_indices,chunk,reorder_dataandfrom_batcheskeep rows aligned withinput_ids.
LGTM.
ebbad87 to
4ac15c4
Compare
|
/ok to test 4ac15c4 |
4ac15c4 to
7fc2b94
Compare
|
/ok to test 7fc2b94 |
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]>
This reverts commit 26aaf66. Signed-off-by: Zhiyu Li <[email protected]>
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Zhiyu Li <[email protected]>
7fc2b94 to
17f9ac8
Compare
|
/ok to test 17f9ac8 |
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]>
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]>
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]>
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]>
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_SEQLENbefore broadcasting the DP batch to its TP×CP×PP peers. With this change:layout="jagged", no pad target)._broadcast_batched_data_dictgains anestedbranch: 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.materializerules. Those rules moved intocodec.pad_field, andcodec.pad_batchapplies them to a whole batch.NRL_ROUTE_BCAST=densekeeps 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.token_mult_prob_errorUpdate: routes padded per microbatch (75515de)
_assemble_routed_expertsnow returnsrouted_expertsas jagged per-row tensors (one flat int16 buffer + row offsets), not a padded[rows, pad_seqlen, 56, 6]table.schema.MICROBATCH_PADDED_FIELDS(today onlyrouted_experts, pad value -1) skip the full-batch padding in_fetch. The Megatron microbatch iterator pads them withcodec.pad_fieldto the microbatch'sinput_idswidth right before.to("cuda"), so the model sees the same values as before.BatchedDataDict.slice/select_indiceshandle nested rows (zero-copy view for contiguous slices), andtruncate_tensorsskips them.Tests on the final code (GB300):
test_batched_data_dict(base venv)test_worker_route_assembly,test_rollout_reassembler(--nemo-gym-only, vLLM worker venv)test_routed_experts_padding: per-microbatch padding reproduces the old sentinel tabletoken_mult_prob_error12-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 withtorch.cuda.synchronizeand a GPU peak-memory reset; this logging is not part of either PR.gen_kl_error/token_mult_prob_errorcluster.segment_size=2. Every PR andmainmasked 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-defernightly (deferred routes, CP=2 replica group). This PR adds nothing new to it. The real.shon 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 existinggrpo-qwen3-30ba3b-*-megatron-cp2-r3*nightlies.Ablation on that recipe, 4 nodes × 4 GPUs, 6 steps per arm:
NRL_ROUTE_BCAST=denseThe 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
_pack_sequences_for_megatronconsume padded[B, S]rows. Removing that means packing directly from unpadded rows, which is a follow-up.🤖 Generated with Claude Code