-
Notifications
You must be signed in to change notification settings - Fork 4.4k
DeepSeek V3.2 support #2440
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
+2,377
−23
Merged
DeepSeek V3.2 support #2440
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e003f8d
Add support for DSA
kunlunl 1a1522e
Merge branch 'main' into kunlunl/deepseek_v3.2_main
Phlip79 31f4120
Update transformer_config.py
Phlip79 571754a
Fix UT
kunlunl d9f7795
Merge branch 'main' into kunlunl/deepseek_v3.2_main
kunlunl 0615f80
Fix UT
kunlunl d52a24a
Unify usage in MLA
kunlunl a297f23
Merge branch 'main' into kunlunl/deepseek_v3.2_main
Phlip79 b8f9d0a
Merge branch 'main' into kunlunl/deepseek_v3.2_main
Phlip79 2ec8a8f
Fix linting
Phlip79 15f1fac
Merge branch 'main' into kunlunl/deepseek_v3.2_main
Phlip79 87d7e70
Update MLA test for return compressed tensors by get_query_key_value_…
kunlunl 271f29b
Merge branch 'main' into kunlunl/deepseek_v3.2_main
kunlunl 30b970e
Fix linting error
kunlunl 6f0f976
Fix UT error
kunlunl 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
114 changes: 114 additions & 0 deletions
114
megatron/core/models/gpt/experimental_attention_variant_module_specs.py
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 |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from megatron.core.fusions.fused_bias_dropout import get_bias_dropout_add | ||
| from megatron.core.models.backends import BackendSpecProvider | ||
| from megatron.core.transformer.enums import AttnMaskType | ||
| from megatron.core.transformer.experimental_attention_variant.dsa import ( | ||
| DSAIndexer, | ||
| DSAIndexerSubmodules, | ||
| DSAttention, | ||
| DSAttentionSubmodules, | ||
| ) | ||
| from megatron.core.transformer.identity_op import IdentityOp | ||
| from megatron.core.transformer.multi_latent_attention import ( | ||
| MLASelfAttention, | ||
| MLASelfAttentionSubmodules, | ||
| ) | ||
| from megatron.core.transformer.spec_utils import ModuleSpec | ||
| from megatron.core.transformer.transformer_layer import TransformerLayer, TransformerLayerSubmodules | ||
|
|
||
|
|
||
| def get_dsa_module_spec_for_backend( | ||
| backend: BackendSpecProvider, | ||
| qk_layernorm: Optional[bool] = False, | ||
| qk_l2_norm: Optional[bool] = False, | ||
| multi_latent_attention: Optional[bool] = False, | ||
| num_experts: Optional[int] = None, | ||
| mlp: Optional[ModuleSpec] = None, | ||
| ) -> ModuleSpec: | ||
| """Helper function to get module spec for Sparse Attention.""" | ||
| assert multi_latent_attention, "Currently only MLA supports sparse attention." | ||
| assert qk_l2_norm is False, "qk_l2_norm is not supported with MLA." | ||
|
|
||
| linear_q_up_proj = ( | ||
| backend.column_parallel_layer_norm_linear() | ||
| if qk_layernorm | ||
| else backend.column_parallel_linear() | ||
| ) | ||
| linear_kv_up_proj = ( | ||
| backend.column_parallel_layer_norm_linear() | ||
| if 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( | ||
| module=DSAttention, | ||
| submodules=DSAttentionSubmodules( | ||
| indexer=ModuleSpec( | ||
| module=DSAIndexer, | ||
| submodules=DSAIndexerSubmodules( | ||
| linear_wq_b=backend.linear(), | ||
| linear_wk=backend.linear(), | ||
| k_norm=backend.layer_norm(rms_norm=False, for_qk=True), | ||
| linear_weights_proj=backend.linear(), | ||
| ), | ||
| ) | ||
| ), | ||
| ) | ||
|
|
||
| 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_kv_down_proj=backend.linear(), | ||
| linear_kv_up_proj=linear_kv_up_proj, | ||
| core_attention=core_attention, | ||
| linear_proj=backend.row_parallel_linear(), | ||
| q_layernorm=IdentityOp, | ||
| kv_layernorm=IdentityOp, | ||
| ), | ||
| ) | ||
|
|
||
| return ModuleSpec( | ||
| module=TransformerLayer, | ||
| submodules=TransformerLayerSubmodules( | ||
| input_layernorm=backend.layer_norm(), | ||
| self_attention=attention, | ||
| self_attn_bda=get_bias_dropout_add, | ||
| pre_mlp_layernorm=backend.layer_norm() if num_experts else IdentityOp, | ||
| mlp=mlp, | ||
| mlp_bda=get_bias_dropout_add, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def get_experimental_attention_variant_module_spec_for_backend( | ||
| backend: BackendSpecProvider, | ||
| experimental_attention_variant: Optional[str] = None, | ||
| qk_layernorm: Optional[bool] = False, | ||
| qk_l2_norm: Optional[bool] = False, | ||
| multi_latent_attention: Optional[bool] = False, | ||
| num_experts: Optional[int] = None, | ||
| mlp: Optional[ModuleSpec] = None, | ||
| ) -> ModuleSpec: | ||
| """Helper function to get module spec for Attention""" | ||
| if experimental_attention_variant == "dsa": | ||
| return get_dsa_module_spec_for_backend( | ||
| backend=backend, | ||
| qk_layernorm=qk_layernorm, | ||
| qk_l2_norm=qk_l2_norm, | ||
| multi_latent_attention=multi_latent_attention, | ||
| num_experts=num_experts, | ||
| mlp=mlp, | ||
| ) | ||
| else: | ||
| raise ValueError( | ||
| f"Invalid experimental attention variant: {experimental_attention_variant}" | ||
| ) | ||
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.