bridge.models.deepseek.attention#
MLA attention spec helpers for the DeepSeek family.
Module Contents#
Classes#
MLA self-attention that does not add a query norm when there is no query LoRA. |
Functions#
Build the decoder block spec, omitting the query norm when |
|
Swap MLA self-attention for the query-norm-free variant, in place, on every layer. |
API#
- class bridge.models.deepseek.attention.MLASelfAttentionWithoutQueryNorm#
Bases:
megatron.core.transformer.multi_latent_attention.MLASelfAttentionMLA self-attention that does not add a query norm when there is no query LoRA.
MCore derives Q and KV normalization from a single
qk_layernormflag. DeepSeek needs it enabled forkv_a_layernorm, which every checkpoint ships. Whenq_lora_rankis None, that same flag also makes MCore fuse a query normalization intolinear_q_proj(QKNormConfigResolver._resolve_mla_qk_layernorm), but the HF architecture defines no query-side norm in that case:DeepseekV3Attentionbuilds a bareq_proj.The result is a trainable parameter with no HF counterpart, which cannot be loaded and is silently dropped on export. This subclass keeps the KV norm and drops the query norm so the converted model matches the source architecture.
Transformer Engine is required for the no-query-LoRA case. MCore builds
linear_q_projfrom the backend’s fused norm+linear implementation, which only Transformer Engine provides, so the local backend is rejected with an explicit message rather than an internal one.- _resolve_qk_norm_config(submodules)#
Replace the fused query projection with a plain one when there is no query LoRA.
The standalone-
q_layernormcase is neutralised before delegating: an MLA spec may setq_layernormto a real norm wheneverqk_layernormis on, and the parent resolver rejects that outright when there is no query LoRA to consume it (_raise_unused_q_norm). Dropping the query norm is exactly what this class exists to do, so the rejection would fire on a configuration this class already knows how to satisfy.
- bridge.models.deepseek.attention.get_deepseek_decoder_block_spec(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- use_transformer_engine: bool,
- normalization: Optional[str] = None,
- qk_l2_norm: Optional[bool] = False,
- vp_stage: Optional[int] = None,
- pp_rank: Optional[int] = None,
Build the decoder block spec, omitting the query norm when
q_lora_rankis None.The signature mirrors
get_gpt_decoder_block_specexactly, includingvp_stageandpp_rank.GPTModelProvider.provide()inspects the callable’s parameters and only forwardsvp_stagewhen it is declared, so dropping it here would leave interleaved pipeline parallelism calling MCore’s layer-offset helper without a virtual stage, which asserts.- Parameters:
config – The model provider / transformer config.
use_transformer_engine – Whether to build Transformer Engine submodules.
normalization – Optional normalization override, forwarded unchanged.
qk_l2_norm – Optional QK L2 norm flag, forwarded unchanged.
vp_stage – Virtual pipeline stage, forwarded unchanged.
pp_rank – Pipeline rank, forwarded unchanged.
- Returns:
The decoder block spec, with MLA self-attention replaced by
- class:
MLASelfAttentionWithoutQueryNormwhen there is no query LoRA.
- bridge.models.deepseek.attention.replace_mla_self_attention(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- spec: megatron.core.transformer.spec_utils.ModuleSpec,
Swap MLA self-attention for the query-norm-free variant, in place, on every layer.
Shared with the MTP path: a standalone MTP pipeline stage owns no decoder layers, so the provider re-derives a layer spec straight from MCore and never passes through
- Func:
get_deepseek_decoder_block_spec. Without this the MTP layer regains the query norm that the decoder layers just dropped.
Accepts either a block spec (
.layer_specs) or a single layer spec.