-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[main] fix(moe): Fix several bugs for DSA rope and spec. #3026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yuzhongw-nvidia
merged 18 commits into
NVIDIA:main
from
yuzhongw-nvidia:yuzhongw/fix_exp_spec
Jun 2, 2026
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2ce8b33
Fix several bugs of experimental attention variant
yuzhongw-nvidia e01fdd3
Update experimental_attention_variant_module_specs.py
yuzhongw-nvidia 6e13bf8
add comment about why using unfused qk layernorm
yuzhongw-nvidia 6196585
fix
yuzhongw-nvidia 2b63493
fix rope order
yuzhongw-nvidia 865801c
refactor apply_rotary_pos_emb and disable rope interleaving for DSA i…
yuzhongw-nvidia 835724d
add a UT for apply rope refactor
yuzhongw-nvidia 19e2b9b
add a UT for exp spec
yuzhongw-nvidia feef749
update exp spec UT
yuzhongw-nvidia 06f5913
add a functional test
yuzhongw-nvidia 7aed830
fix
yuzhongw-nvidia 2fb2d1a
update dependency
yuzhongw-nvidia c72087f
fix absorbed mla
yuzhongw-nvidia 082c0bb
test: fix experimental attention spec mocks
8eb22b7
build: refresh uv lock
c0f93cf
test: stabilize weighted squared relu fusion
a5ea4ea
fix: accept stage args in decoder layer specs
8477131
Update gpt_layer_specs.py
yuzhongw-nvidia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,17 +83,6 @@ def get_dsa_module_spec_for_backend( | |
| assert config.multi_latent_attention, "Currently only MLA supports sparse attention." | ||
| assert config.qk_l2_norm is False, "qk_l2_norm is not supported with MLA." | ||
|
|
||
| linear_q_up_proj = ( | ||
| backend.column_parallel_layer_norm_linear() | ||
| if config.qk_layernorm | ||
| else backend.column_parallel_linear() | ||
| ) | ||
| linear_kv_up_proj = ( | ||
| backend.column_parallel_layer_norm_linear() | ||
| if config.qk_layernorm | ||
| else backend.column_parallel_linear() | ||
| ) | ||
|
|
||
| # Because TransformerEngine does not support sparse attention yet, we use local | ||
| # implementation whether the backend is TransformerEngine or not. | ||
| core_attention = ModuleSpec( | ||
|
|
@@ -111,19 +100,27 @@ def get_dsa_module_spec_for_backend( | |
| ), | ||
| ) | ||
|
|
||
| # Adjust for RMS norm. | ||
| rms_norm = config.normalization == "RMSNorm" | ||
| # DSA indexer requires normalized q as input, so here we cannot fuse qk layernorm | ||
| # with linear projection and have to use unfused qk layernorm. | ||
| qk_norm = ( | ||
| backend.layer_norm(rms_norm=rms_norm, for_qk=True) if config.qk_layernorm else IdentityOp | ||
| ) | ||
|
|
||
| attention = ModuleSpec( | ||
| module=MLASelfAttention, | ||
| params={"attn_mask_type": AttnMaskType.causal}, | ||
| submodules=MLASelfAttentionSubmodules( | ||
| linear_q_proj=backend.column_parallel_linear(), | ||
| linear_q_down_proj=backend.linear(), | ||
| linear_q_up_proj=linear_q_up_proj, | ||
| linear_q_up_proj=backend.column_parallel_linear(), | ||
| linear_kv_down_proj=backend.linear(), | ||
| linear_kv_up_proj=linear_kv_up_proj, | ||
| linear_kv_up_proj=backend.column_parallel_linear(), | ||
| core_attention=core_attention, | ||
| linear_proj=backend.row_parallel_linear(), | ||
| q_layernorm=IdentityOp, | ||
| kv_layernorm=IdentityOp, | ||
| q_layernorm=qk_norm, | ||
| kv_layernorm=qk_norm, | ||
| ), | ||
| metainfo={"fuse_input_layernorm": False}, | ||
| ) | ||
|
|
@@ -154,12 +151,12 @@ def get_experimental_attention_variant_module_spec( | |
| ########## | ||
|
|
||
|
|
||
| def get_transformer_block_with_experimental_attention_variant_spec( | ||
| config: TransformerConfig, vp_stage: Optional[int] = None, pp_rank: Optional[int] = None | ||
| ) -> TransformerBlockSubmodules: | ||
| """Build transformer block spec with experimental attention variants (e.g., linear attention). | ||
| def get_transformer_layer_with_experimental_attention_variant_spec( | ||
| config: TransformerConfig, backend: BackendSpecProvider = None | ||
| ) -> List[ModuleSpec]: | ||
| """Build transformer layer specs with experimental attention variants (e.g., linear attention). | ||
|
|
||
| This function constructs a heterogeneous transformer block that supports mixing different | ||
| This function is for constructing a heterogeneous transformer that supports mixing different | ||
| attention mechanisms (experimental vs standard) and MLP types (MoE vs dense) across layers. | ||
| **Note that, this API is a experimental API in the short term, and might be deprecated in the | ||
| future. In the long run, we will move to a new design that better support hybrid models.** | ||
|
|
@@ -175,22 +172,19 @@ def get_transformer_block_with_experimental_attention_variant_spec( | |
| 2. Per-Layer Spec Construction: Iterates through layers, constructing transformer | ||
| layer specs based on attention and MLP patterns. | ||
|
|
||
| 3. Pipeline Slicing: Extracts layer specs for the current pipeline stage. | ||
|
|
||
| Args: | ||
| config: Transformer configuration containing model hyperparameters and feature flags. | ||
| vp_stage: Virtual pipeline stage index for interleaved pipeline parallelism. | ||
| pp_rank: Pipeline model parallel rank. | ||
|
|
||
| Returns: | ||
| TransformerBlockSubmodules containing per-layer specs and final layer norm. | ||
| List[ModuleSpec] containing per-layer specs. | ||
|
|
||
| Note: | ||
| Currently only supports transformer_engine backend. Kitchen backend can be used as a | ||
| wrapper with TE fallback for unsupported operations. | ||
| """ | ||
|
|
||
| backend = _get_backend_spec_provider(config=config) | ||
| if backend is None: | ||
| backend = _get_backend_spec_provider(config=config) | ||
|
|
||
| # Get attention patterns and specs | ||
| experimental_attention_pattern = [0] * config.num_layers | ||
|
|
@@ -271,6 +265,42 @@ def get_transformer_block_with_experimental_attention_variant_spec( | |
| ) | ||
| ) | ||
|
|
||
| return layer_specs | ||
|
|
||
|
|
||
| def get_transformer_block_with_experimental_attention_variant_spec( | ||
| config: TransformerConfig, vp_stage: Optional[int] = None, pp_rank: Optional[int] = None | ||
| ) -> TransformerBlockSubmodules: | ||
| """Build transformer block spec with experimental attention variants (e.g., linear attention). | ||
|
|
||
| This function constructs a heterogeneous transformer block that supports mixing different | ||
| attention mechanisms (experimental vs standard) and MLP types (MoE vs dense) across layers. | ||
| **Note that, this API is a experimental API in the short term, and might be deprecated in the | ||
| future. In the long run, we will move to a new design that better support hybrid models.** | ||
|
|
||
| Constructing transformer layer specs by | ||
| `get_transformer_layer_with_experimental_attention_variant_spec` and then slicing the | ||
| layer specs to only include the layers that are built in this pipeline stage. | ||
|
|
||
| Args: | ||
| config: Transformer configuration containing model hyperparameters and feature flags. | ||
| vp_stage: Virtual pipeline stage index for interleaved pipeline parallelism. | ||
| pp_rank: Pipeline model parallel rank. | ||
|
|
||
| Returns: | ||
| TransformerBlockSubmodules containing per-layer specs and final layer norm. | ||
|
|
||
| Note: | ||
| Currently only supports transformer_engine backend. Kitchen backend can be used as a | ||
| wrapper with TE fallback for unsupported operations. | ||
| """ | ||
|
|
||
| backend = _get_backend_spec_provider(config=config) | ||
|
|
||
| layer_specs = get_transformer_layer_with_experimental_attention_variant_spec( | ||
| config=config, backend=backend | ||
| ) | ||
|
|
||
| # Slice the layer specs to only include the layers that are built in this pipeline stage. | ||
| if config.pipeline_model_parallel_layout is not None: | ||
| local_layer_ids = config.pipeline_model_parallel_layout.get_layer_id_list( | ||
|
|
@@ -284,6 +314,7 @@ def get_transformer_block_with_experimental_attention_variant_spec( | |
| layer_specs = [layer_specs[layer_id] for layer_id in local_layer_ids] | ||
|
|
||
| # Get GPT decoder block spec | ||
| rms_norm = config.normalization == "RMSNorm" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like rms_norm var is just added now, does this mean this codepath was broken on main before this change ? |
||
| gpt_decoder_block_spec = TransformerBlockSubmodules( | ||
| layer_specs=layer_specs, layer_norm=backend.layer_norm(rms_norm=rms_norm, for_qk=False) | ||
| ) | ||
|
|
||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.