Repository navigation
feat: context-parallel with nemotron v3 - #1441
Merged
Merged
Conversation
Signed-off-by: adil-a <adil.asif2000@hotmail.com>
Signed-off-by: adil-a <adil.asif2000@hotmail.com>
Resolve conflicts in cp_utils.py and test_context_parallel.py by keeping additions from both sides (DualChunkSwap CP functions from this branch, attach_context_parallel_hooks and qwen3.5 tests from main). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: adil-a <adil.asif2000@hotmail.com>
Support both THD (TE, with/without packing) and BSHD (TE or SDPA, no packing) context parallelism for hybrid Mamba-Attention models. Changes: - Add 2D THD format detection in NemotronV3Attention (Qwen3MoE pattern) and NemotronV3Mamba2Mixer (unsqueeze/squeeze) - Add uses_te_cp flag to MambaContextParallel to conditionally apply DualChunkSwap reordering (skip for DTensor/SDPA CP) - Add deinterleave/reinterleave for packed multi-sequence all-to-all - Pass cu_seqlens through to MambaCP for THD-mode reordering - Update supports_cp to allow hybrid models with SDPA backend - Add 4-config functional test grid: BSHD+TE, THD+TE, THD+TE+packing, BSHD+SDPA across Mamba, Attention, and Hybrid test scripts Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: adil-a <adil.asif2000@hotmail.com>
The MoE parallelizer's apply_cp didn't handle mamba blocks, causing mixer.cp to stay None when EP is active. Add layer_type/self_attn properties to NemotronV3Block for compatibility with the MoE parallelizer, and add a "mamba" branch in apply_cp that creates MambaContextParallel. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: adil-a <adil.asif2000@hotmail.com>
Signed-off-by: adil-a <adil.asif2000@hotmail.com>
Bug fixes: - Guard padding_mask for pad_token_id=None (NemotronV3 tokenizer) in thd_utils.py, cp_utils.py, and model_utils.py - Fix cu_seqlens global/local mismatch in Mamba CP (was doubling already-global values), remove debug instrumentation - Fix seq_idx computation in NemotronV3 mixer (remove erroneous * cp_size) - Fix padding_idx=None crash in packed_sequence packing (train_ft.py) Test improvements: - Merge run_nemotron_v3_attention_cp.py into run_attention_cp.py as --model_type nemotron_v3 with --configs arg for multi-format testing - Fix pre-existing test bugs: rope_theta access (use get_rope_config), missing cp_size/cp_rank in attention forward, missing param grad all-reduce, DummyBlock missing mlp attr, DeepSeek freqs_cis format - Add hybrid NemotronV3 CP test shell script - Fix Qwen3.5MoE shell script (was using pytest on non-pytest file) - Add L2_Context_Parallel to CI workflow matrix Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
Author
|
/ok to test 192556a |
adil-a
marked this pull request as ready for review
March 29, 2026 07:22
adil-a
requested review from
a team,
HuiyingLi,
ZhiyuLi-Nvidia,
akoumpa,
hemildesai and
pthombre
as code owners
March 29, 2026 07:22
Signed-off-by: adil-a <adil.asif2000@hotmail.com>
Collaborator
Author
|
/ok to test 6708890 |
Contributor
|
/claude review |
…d validation - Fix _repeat_group_state to handle 2D THD input by normalizing to 3D before expand() and squeezing back (triggered when n_groups < cp_size) - Fix hybrid SDPA test to validate actual gradients (q_proj.weight) instead of comparing zeros vs zeros (embed_tokens is detached) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
Author
|
/claude review |
Collaborator
Author
|
/ok to test c147997 |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: adil-a <adil.asif2000@hotmail.com>
Collaborator
Author
|
/ok to test 028580a |
hemildesai
reviewed
Mar 31, 2026
… model for TE detection - Add _ensure_pad_token_id() that defaults pad_token_id to 0 with a warning when the tokenizer doesn't define one (e.g. NemotronV3). Called in both NeMoAutoTokenizerWithBosEosEnforced and the registry dispatch path. - Revert scattered pad_token_id workarounds in cp_utils, thd_utils, and train_ft back to their original form since the tokenizer now guarantees a non-None pad_token_id. - Change _uses_te_dot_product_attention() to inspect actual model modules for TE DotProductAttention instead of only checking the YAML config string, with config fallback for pre-model call sites. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: adil-a <adil.asif2000@hotmail.com>
Collaborator
Author
|
/ok to test 8cf1a95 |
Resolve conflict in import_utils.py (take main's version with three-way transformers API version detection). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: adil-a <adil.asif2000@hotmail.com>
Collaborator
Author
|
/ok to test d240310 |
hemildesai
approved these changes
Apr 3, 2026
thomasdhc
approved these changes
Apr 3, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds context parallelism (CP) support for NemotronV3 hybrid models (interleaved attention + Mamba2 + MoE). NemotronV3 requires a dual-path CP strategy because attention layers and Mamba/SSM layers have fundamentally different parallelism requirements:
Supported configurations
context_parallel()Key changes
Mamba hidden-parallel CP (
mamba_cp.py— new file):MambaContextParallelclass orchestrating CP for Mamba2 mixer layers_all_to_all_cp2hp/_all_to_all_hp2cp) to redistribute between sequence-sharded and hidden-sharded layouts_undo_attention_load_balancing/_redo_attention_load_balancing) so SSM kernels see tokens in sequential order_deinterleave_packed_seqs/_reinterleave_packed_seqsfor correct token ordering after all-to-all with multiple packed sequencesNemotronV3 model changes (
layers.py,model.py):DotProductAttention.set_context_parallel_group()MambaContextParallelattached asmixer.cpseq_idxcomputation fromcu_seqlensfor correct SSM state resets at sequence boundariesBug fixes:
padding_maskforpad_token_id=None(NemotronV3 tokenizer returnsNone) inthd_utils.py,cp_utils.py,model_utils.py—tensor == Nonereturns PythonFalse(a scalar bool), not a tensorcu_seqlensglobal/local mismatch in Mamba CP — batch cu_seqlens are GLOBAL (pre-TE-partitioning), so derive local ascu_seqlens // cp_sizeinstead of erroneously computingcu_seqlens * cp_sizeseq_idxin mixer layer — remove erroneous* cp_sizesince cu_seqlens is already globalpadding_idx=Nonecrash inpack_datasetwhen packing with CP > 1 (getattr(tokenizer, "pad_token_id", 0)returnsNonewhen the attribute exists but isNone)Test infrastructure:
run_nemotron_v3_attention_cp.pyintorun_attention_cp.pyas--model_type nemotron_v3with--configsarg for multi-format testing (bshd_te, thd_te, bshd_sdpa)rope_thetaaccess viaget_rope_config(), missingcp_size/cp_rankparams, missing param grad all-reduce, DeepSeekfreqs_cisformatrun_hybrid_nemotron_v3_cp.py— end-to-end hybrid model test (4 configs)run_mamba_cp.py— Mamba2Mixer layer test (4 configs including THD packed)L2_Context_Parallelto CI workflow matrix (cicd-main.yml)THD format and sequence packing with CP
In THD format, the batch is flattened to a 1D
[T, H]tensor withcu_seqlenstracking sequence boundaries. For CP with DualChunkSwap:thd_get_partitioned_indices_deinterleave_packed_seqsrearranges to sequence-major so each sequence's tokens are contiguous_undo_attention_load_balancingpermutes from DualChunkSwap order[c0, c3, c1, c2]back to sequential[c0, c1, c2, c3]for the SSM kernel_redo+_reinterleave) restore the layout for attentionNote: With CP > 1 and sequence packing, each sequence is padded to be divisible by
2 * cp_sizebefore packing. This changes pack composition compared to CP=1 (fewer sequences per pack due to padding overhead), resulting in differentnum_label_tokensper step — this is expected behavior, not a correctness issue.Convergence results
https://api.wandb.ai/links/nvidia-nemo-fw-public/f7qpoo9i