Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
eb44f74
fix(dsv4): add gb300 train replay patches
May 8, 2026
b35cf26
fix(dsv4): preserve swiglu tp stride contract
May 8, 2026
6e6bf83
feat(dsv4): absorb DSV4 GB300 Megatron fixes from radixark/Megatron-L…
May 8, 2026
f5d23fb
fix(dsv4): static audit fixes — remove duplicate _init_routing_mode, …
May 8, 2026
e8dd100
fix(dsv4): add moe_latent_size field to TransformerConfig
May 8, 2026
00ea963
fix(dsv4): initialize local_tokens_per_expert=None in TopKRouter.__in…
May 8, 2026
fc2fa0a
fix(dsv4): fix router local_tokens_per_expert/expert_bias init — use …
May 8, 2026
39c0e85
fix(dsv4): remove wrong early-return in hash routing probs shape
May 8, 2026
73b298f
fix(dsv4): switch moe_utils topk replay to replay_base manager path
May 8, 2026
2fe649e
fix(dsv4): propagate bshd padding through moe routing
May 11, 2026
2c63b90
fix(dsv4): remove debug grad bucket hook
May 11, 2026
ce61712
fix(dsv4): preserve router and optimizer contracts
May 11, 2026
9aece18
feat(qat): add MXFP4 fake-QAT STE on TEGroupedLinear expert weights
tonic-scitix May 13, 2026
aaf3ad2
feat(dsv4): fake quant experts before fp8 param cast
May 13, 2026
55d0470
fix(dsv4): preserve main grad after mxfp4 qat copy
May 13, 2026
0fbf89e
feat(dsv4): trace mxfp4 qat fp8 copy loss
May 15, 2026
09e6ad0
feat(dsv4): expand mxfp4 qat copy trace
May 15, 2026
2effed0
feat(dsv4): tag qat fp8 copy trace calls
May 15, 2026
71d5fc4
docs(dsv4): point mxfp4 qat probe reference
May 18, 2026
b42d952
fix(dsv4): match mxfp4 fake qat rounding
May 18, 2026
2ee692f
feat(dsv4): sample qat fp8 copy boundaries
May 18, 2026
f6c6425
fix(dsv4): add mxfp4 qat scale headroom
May 18, 2026
9558e24
feat(dsv4): gate mxfp4 qat by copy call
May 23, 2026
928625e
fix(dsv4): remove mxfp4 qat headroom projection
May 27, 2026
1b097e4
fix(dsv4): restore mxfp4 forward qat for fp8 params
May 28, 2026
deba530
fix(dsv4): dequantize primary fp8 weights for mxfp4 qat
May 28, 2026
aee9347
fix(dsv4): gate fp8 checkpoint dequant dtype
Jun 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion megatron/core/dist_checkpointing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

""" Helpers for manipulating sharded tensors and sharded state dicts. """
import logging
import os
from contextlib import contextmanager
from time import time
from typing import Dict, Optional, Tuple

import torch

from .dict_utils import dict_list_map_inplace, extract_matching_values, nested_values
from .mapping import (
LocalNonpersistentObject,
Expand Down Expand Up @@ -233,6 +236,27 @@ def _replace_prefixes(x):
dict_list_map_inplace(_replace_prefixes, sharded_state_dict)


def _dist_ckpt_fp8_dequant_dtype() -> Optional[torch.dtype]:
value = os.getenv("MEGATRON_DIST_CKPT_FP8_DEQUANT_DTYPE", "").strip().lower()
if value in ("", "default", "none"):
return None
dtype_by_name = {
"bf16": torch.bfloat16,
"bfloat16": torch.bfloat16,
"fp16": torch.float16,
"float16": torch.float16,
"half": torch.float16,
"fp32": torch.float32,
"float32": torch.float32,
}
if value not in dtype_by_name:
raise ValueError(
"MEGATRON_DIST_CKPT_FP8_DEQUANT_DTYPE must be one of "
f"{sorted(dtype_by_name)} plus default/none, got {value!r}"
)
return dtype_by_name[value]


def force_all_tensors_to_non_fp8(sharded_state_dict: ShardedStateDict):
"""Force all tensors in state dict to be non-fp8.

Expand All @@ -241,9 +265,11 @@ def force_all_tensors_to_non_fp8(sharded_state_dict: ShardedStateDict):
"""
from ..fp8_utils import dequantize_fp8_tensor, is_float8tensor # Avoid circular import

dequant_dtype = _dist_ckpt_fp8_dequant_dtype()

for v in nested_values(sharded_state_dict):
if hasattr(v, "data") and is_float8tensor(v.data):
v.data = dequantize_fp8_tensor(v.data)
v.data = dequantize_fp8_tensor(v.data, dtype=dequant_dtype)


fallback_logger = logging.getLogger(__name__)
Expand Down
6 changes: 3 additions & 3 deletions megatron/core/distributed/finalize_model_grads.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def reset_model_temporary_tensors(config: TransformerConfig, model: List[torch.n
"""
for model_chunk in model:
for module in get_attr_wrapped_model(model_chunk, 'modules')():
if config.moe_router_enable_expert_bias and hasattr(module, 'expert_bias'):
if config.moe_router_enable_expert_bias and getattr(module, 'expert_bias', None) is not None:
module.local_tokens_per_expert.zero_()
if (
config.moe_router_load_balancing_type == "global_aux_loss"
Expand All @@ -299,7 +299,7 @@ def _update_router_expert_bias(model: List[torch.nn.Module], config: Transformer
expert_bias_list = []
for model_chunk in model:
for module in get_attr_wrapped_model(model_chunk, 'modules')():
if hasattr(module, 'expert_bias'):
if getattr(module, 'expert_bias', None) is not None:
tokens_per_expert_list.append(module.local_tokens_per_expert)
expert_bias_list.append(module.expert_bias)
# For hybrid models with both MoE and Dense layers, this list can be empty.
Expand Down Expand Up @@ -472,7 +472,7 @@ def finalize_model_grads(
if config.timers is not None:
config.timers('embedding-grads-all-reduce').stop()

if config.moe_router_enable_expert_bias:
if config.moe_router_enable_expert_bias and not config.freeze_e_score_correction_bias:
_update_router_expert_bias(model, config)

reset_model_temporary_tensors(config, model)
Expand Down
Loading