Skip to content

Commit 43eb53e

Browse files
committed
[model] fix: restore position_embedding_type and gradient_accumulation_fusion for MLA models
Two regressions caused a ~30% perf drop on DSV3 GB300 MXFP8 (1292 → 914 TFLOPs, March 14 → March 18), introduced by PRs #2599 and #2739. Fix 1 (model_bridge.py): MLA providers with YaRN rope_scaling were getting position_embedding_type="learned_absolute". The base provider_bridge() only set "rope" when rope_type != "yarn", but for MLA models YaRN scaling is handled via mla_rope_params — position_embedding_type must always be "rope". Add is_mla_provider check to handle this correctly. Fix 2 (deepseek_v2/v3_bridge.py): gradient_accumulation_fusion was hardcoded True before #2739 removed it in favor of the dynamic can_enable_gradient_accumulation_fusion() check. That check returns False if fused_weight_gradient_mlp_cuda (APEX CUDA extension) is not importable, silently disabling the fusion. Restore explicit True for DeepSeek V2/V3. Signed-off-by: Yu Yao <yaoyu.094@gmail.com> Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
1 parent 877779a commit 43eb53e

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

‎src/megatron/bridge/models/conversion/model_bridge.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ def provider_bridge(self, hf_pretrained: HFPreTrained) -> ModelProviderTarget:
448448
ModelProviderTarget: A configured model provider instance
449449
"""
450450
from megatron.bridge.models.gpt_provider import GPTModelProvider
451+
from megatron.bridge.models.mla_provider import MLAModelProvider
451452

452453
hf_config = hf_pretrained.config
453454

@@ -458,22 +459,25 @@ def provider_bridge(self, hf_pretrained: HFPreTrained) -> ModelProviderTarget:
458459

459460
# Use specified provider class, defaulting to GPTModelProvider
460461
provider_class = self.PROVIDER_CLASS if self.PROVIDER_CLASS is not None else GPTModelProvider
462+
is_mla_provider = issubclass(provider_class, MLAModelProvider)
461463
# Filter kwargs to only fields the provider dataclass accepts, so that MLA-only None
462464
# values (q_lora_rank, kv_lora_rank, …) are silently dropped for non-MLA providers
463465
# while still being passed through for MLA providers that declare them as fields.
464466
valid_fields = provider_class.__dataclass_fields__
465467
provider = provider_class(**{k: v for k, v in provider_kwargs.items() if k in valid_fields})
466468

467-
# All models that flow through the base provider_bridge use RoPE.
468-
# YARN is already handled above (hf_config_to_provider_kwargs sets
469-
# position_embedding_type="yarn"), so we only need to guard against
470-
# overwriting it here. Every other rope_type value (None, "default",
471-
# "llama3", "longrope", …) means plain RoPE.
469+
# Determine position_embedding_type from HF rope_scaling.
470+
# For GPT providers: rope_type=="yarn" → "yarn"; everything else → "rope".
471+
# For MLA providers: always "rope" — YaRN scaling parameters are applied
472+
# separately via mla_rope_params (rotary_scaling_factor, mscale, etc.) and
473+
# position_embedding_type="yarn" is not a valid MLA config value.
472474
hf_rope_scaling = getattr(hf_config, "rope_scaling", None)
473475
rope_type = None
474476
if hf_rope_scaling:
475477
rope_type = hf_rope_scaling.get("type") or hf_rope_scaling.get("rope_type")
476-
if rope_type != "yarn":
478+
if rope_type == "yarn" and not is_mla_provider:
479+
provider.position_embedding_type = "yarn"
480+
else:
477481
provider.position_embedding_type = "rope"
478482

479483
# Apply MLA rope params via setattr (for MLA models like DeepSeek, Kimi)

‎src/megatron/bridge/models/deepseek/deepseek_v2_bridge.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def provider_bridge(self, hf_pretrained: PreTrainedCausalLM) -> MLAModelProvider
6262
provider.moe_permute_fusion = True
6363

6464
provider.apply_rope_fusion = False
65+
provider.gradient_accumulation_fusion = True
6566
provider.bias_activation_fusion = True
6667
provider.bias_dropout_fusion = True
6768
provider.cross_entropy_fusion_impl = "te"

‎src/megatron/bridge/models/deepseek/deepseek_v3_bridge.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def provider_bridge(self, hf_pretrained: PreTrainedCausalLM) -> MLAModelProvider
6969
provider.moe_aux_loss_coeff = 0.0001
7070

7171
provider.apply_rope_fusion = False
72+
provider.gradient_accumulation_fusion = True
7273
provider.bias_activation_fusion = True
7374
provider.bias_dropout_fusion = True
7475
provider.cross_entropy_fusion_impl = "te"

0 commit comments

Comments
 (0)