fix: attach CP attention-mask hooks for dense (non-TE) context parallelism#1470
Open
hemildesai wants to merge 10 commits intomainfrom
Open
fix: attach CP attention-mask hooks for dense (non-TE) context parallelism#1470hemildesai wants to merge 10 commits intomainfrom
hemildesai wants to merge 10 commits intomainfrom
Conversation
Contributor
Author
|
/ok to test e3fb07e |
Contributor
Author
|
/ok to test 5165660 |
adil-a
reviewed
Mar 6, 2026
Collaborator
There was a problem hiding this comment.
can we move the changes in this file into model_init.py
…elism Strip the 4D attention_mask from the batch and register forward pre-hooks on self_attn modules to set is_causal=True, so that SDPA handles causal masking internally when using dense context parallelism without TE. Signed-off-by: hemildesai <hemild@nvidia.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: hemildesai <hemild@nvidia.com>
Replace functools.partial(F.scaled_dot_product_attention, ...) with a closure that resolves F.scaled_dot_product_attention at call time. This ensures CP's runtime monkey-patch of the function is picked up by all custom models instead of being bypassed by the early-bound reference. Also make _attach_context_parallel_hooks public (renamed to attach_context_parallel_hooks). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: hemildesai <hemild@nvidia.com>
…ends Extract SDPA backend selection into a resolve_sdpa_method() helper that accepts string names from YAML config (e.g. ["flash_attention", "efficient_attention"]) and converts them to SDPBackend enum members. When no explicit config is provided, auto-selects based on CP and activation checkpointing constraints. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: hemildesai <hemild@nvidia.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: hemildesai <hemild@nvidia.com>
Replace the assert that required all attention modules to be TE DotProductAttention with a continue, so dense (SDPA) attention modules are gracefully skipped. This allows MoE models to use context parallelism with non-TE attention backends. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: hemildesai <hemild@nvidia.com>
59deabe to
1d2838b
Compare
Contributor
Author
|
/ok to test 1d2838b |
Move the resolve_sdpa_method helper from train_ft.py to _transformers/model_init.py per review feedback. The config resolution (reading sdpa_method from YAML and passing it to build_model) remains in train_ft.py. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: hemildesai <hemild@nvidia.com>
Contributor
Author
|
/ok to test 274666d |
Contributor
|
/ok to test 9098ba2 |
Move the attach_context_parallel_hooks call from train_ft.py into apply_model_infrastructure in infrastructure.py, which already has access to the device mesh. Add _uses_te_attention helper that inspects the model's self_attn.attn_module instances to determine if TE DotProductAttention is used, replacing the config-based check. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: hemildesai <hemild@nvidia.com>
When the model is an AutoPipeline, iterate over model.parts to inspect self_attn modules instead of only the pipeline wrapper itself. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: hemildesai <hemild@nvidia.com>
Contributor
Author
|
/ok to test 32e55a4 |
akoumpa
reviewed
Mar 7, 2026
The SDPA attn_func changed from functools.partial to a closure, so .keywords no longer exists. Mock F.scaled_dot_product_attention and inspect call kwargs instead. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Hemil Desai <hemild@nvidia.com>
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
_attach_context_parallel_hooksto register forward pre-hooks onself_attnmodules that stripattention_maskand setis_causal=True, fixing shape mismatches when dense (non-TE) context parallelism shards Q/K/V as DTensorsTrainFinetuneRecipeForNextTokenPredictionwhencp_size > 1and TE attention is not usedattention_maskremoval inmake_cp_batch_and_ctxTest plan
pytest tests/unit_tests/distributed/test_cp_utils.py(12 tests, all passing)🤖 Generated with Claude Code