Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
3b4a0ae
add mcore cg updates
jiemingz Dec 5, 2025
e68effc
fix rebase
jiemingz Dec 5, 2025
ce0ffdc
cleanup
jiemingz Dec 5, 2025
da52f50
fix param cache skip
jiemingz Dec 5, 2025
a36e533
works with expandable segs
jiemingz Dec 7, 2025
3fd175a
tensor cache
jiemingz Dec 8, 2025
b9beeda
get_connected_params
jiemingz Dec 8, 2025
02f8247
cleanup more
jiemingz Dec 8, 2025
d92c246
fix rebase
jiemingz Dec 16, 2025
13b1951
fix rebase
jiemingz Dec 16, 2025
c62cc99
works again
jiemingz Dec 18, 2025
233f6a9
treemap
jiemingz Dec 18, 2025
53d88ec
cg_artifcats
jiemingz Dec 18, 2025
5935884
cleanup
jiemingz Dec 18, 2025
e37537b
fix rotary
jiemingz Dec 18, 2025
68a71a3
fix moe recompute
jiemingz Dec 18, 2025
d8bf12e
address comments
jiemingz Dec 18, 2025
d42e39e
fix rebase
jiemingz Dec 18, 2025
14b937d
address comments
jiemingz Dec 23, 2025
59fdc3c
run autoformat
jiemingz Dec 23, 2025
ad516c9
log rank 0
jiemingz Dec 23, 2025
598d54c
cleanup docstrings
jiemingz Dec 23, 2025
f8fdf5f
rebase fixes
jiemingz Dec 31, 2025
7c32498
ln recomp
jiemingz Dec 31, 2025
d884650
dealloc fwd outputs
jiemingz Jan 2, 2026
387593d
capture pre post process
jiemingz Jan 2, 2026
3a29ed8
Update autoformat.sh
jiemingz Jan 5, 2026
f71f1fe
guard metadata attr for inference
jiemingz Jan 5, 2026
48b3aaa
guard can_skip_replay_copy
jiemingz Jan 5, 2026
f4acc0d
cleanup logging
jiemingz Jan 6, 2026
6813c59
fix lint
jiemingz Jan 6, 2026
7793a20
fix logging bug
jiemingz Jan 6, 2026
16c9686
fix fine grained callable test
jiemingz Jan 7, 2026
474587e
fix dynamic_inference_engine tests
jiemingz Jan 7, 2026
91abb60
address comments, cleanup args
jiemingz Jan 9, 2026
34e792d
address comments, revert random.py
jiemingz Jan 12, 2026
864ec47
Update megatron/core/transformer/moe/moe_layer.py
jiemingz Jan 13, 2026
17c4a94
address comments
jiemingz Jan 13, 2026
273605c
fix back compat for inference, cleanup args again
jiemingz Jan 14, 2026
79e3d63
fix rebase with #2452
jiemingz Jan 14, 2026
2e47f7d
Merge branch 'main' into jiemingz/mcore_cudagraph_improvements
jiemingz Jan 21, 2026
976f167
small lint fix
jiemingz Jan 21, 2026
c26636e
fix args again
jiemingz Jan 21, 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
4 changes: 3 additions & 1 deletion megatron/core/models/gpt/fine_grained_callables.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,9 @@ def submodule_combine_forward(node: ScheduleNode, output: torch.Tensor):
"""
residual = node.layer_state.residual
shared_expert_output = getattr(node.layer_state, 'shared_expert_output', None)
output = layer.mlp.combine(output, shared_expert_output)
output = layer.mlp.combine(output)
output = layer.mlp.postprocess(output, shared_expert_output)

mlp_output_with_bias = (output, None)
if hasattr(layer, 'cuda_graphs') and layer.cuda_graphs:
layer.mlp.cudagraph_tensor_store.clear()
Expand Down
9 changes: 6 additions & 3 deletions megatron/core/models/mamba/mamba_layer_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
from megatron.core.transformer.enums import AttnMaskType
from megatron.core.transformer.mlp import MLP, MLPSubmodules
from megatron.core.transformer.spec_utils import ModuleSpec
from megatron.core.transformer.transformer_layer import TransformerLayer, TransformerLayerSubmodules
from megatron.core.transformer.transformer_layer import (
MoETransformerLayer,
TransformerLayer,
TransformerLayerSubmodules,
)

moe = get_moe_module_spec(
use_te=True,
Expand Down Expand Up @@ -78,8 +82,7 @@
),
),
moe_layer=ModuleSpec(
# TODO (rwaleffe): change this to be an "MoELayer" to work with CudaGraphs?
module=TransformerLayer,
module=MoETransformerLayer,
Comment thread
jiemingz marked this conversation as resolved.
submodules=TransformerLayerSubmodules(
pre_mlp_layernorm=TENorm, mlp=moe, mlp_bda=get_bias_dropout_add
),
Expand Down
8 changes: 8 additions & 0 deletions megatron/core/ssm/mamba_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from megatron.core.inference.contexts import BaseInferenceContext
from megatron.core.packed_seq_params import PackedSeqParams
from megatron.core.process_groups_config import ProcessGroupCollection
from megatron.core.transformer.enums import CudaGraphScope
from megatron.core.transformer.identity_op import IdentityOp
from megatron.core.transformer.module import GraphableMegatronModule
from megatron.core.transformer.spec_utils import ModuleSpec, build_module
Expand Down Expand Up @@ -85,6 +86,13 @@ def __init__(
self.mamba_bda = build_module(submodules.mamba_bda)
self.bias_dropout_add_exec_handler = torch.enable_grad

def create_mcore_cudagraph_manager(self, config):
"""Register the mamba layer for cudagraphs."""
from megatron.core.transformer.cuda_graphs import CudaGraphManager

if not self.config.cuda_graph_scope or CudaGraphScope.mamba in self.config.cuda_graph_scope:
self.cudagraph_manager = CudaGraphManager(config)

def mamba_state_shapes_per_request(self) -> Tuple[Tuple[int], Tuple[int]]:
"""Returns the Mamba conv and ssm states shapes per request."""
return self.mixer.mamba_state_shapes_per_request()
Expand Down
51 changes: 48 additions & 3 deletions megatron/core/tensor_parallel/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,27 @@ def _fork_rng():
_set_all_rng_states(*current_states)


# Global flag that's toggled whenever inside a checkpointing context
IS_CHECKPOINTING = False


def _set_checkpointing():
"""Set state to checkpointing enabled."""
global IS_CHECKPOINTING
IS_CHECKPOINTING = True


def _unset_checkpointing():
"""Unset state to checkpointing enabled."""
global IS_CHECKPOINTING
IS_CHECKPOINTING = False


def is_checkpointing():
"""Check if currently in a checkpoint context."""
return IS_CHECKPOINTING


class CheckpointFunction(torch.autograd.Function):
"""Checkpoint Function

Expand All @@ -484,6 +505,8 @@ class CheckpointFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, run_function, distribute_saved_activations, *args):
"""Forward pass."""
_set_checkpointing()

ctx.run_function = run_function
ctx.distribute_saved_activations = distribute_saved_activations

Expand All @@ -504,6 +527,7 @@ def forward(ctx, run_function, distribute_saved_activations, *args):
# Store everything.
ctx.save_for_backward(*args)

_unset_checkpointing()
return outputs

# pylint: disable=missing-function-docstring
Expand All @@ -515,6 +539,8 @@ def backward(ctx, *args):
"Checkpointing is not compatible with .grad(), "
"please use .backward() if possible"
)
_set_checkpointing()

inputs = ctx.saved_tensors
if ctx.distribute_saved_activations:
safely_set_viewless_tensor_data(
Expand All @@ -539,6 +565,8 @@ def backward(ctx, *args):
)
torch.autograd.backward(outputs, args)
grads = tuple(inp.grad if isinstance(inp, torch.Tensor) else inp for inp in detached_inputs)

_unset_checkpointing()
return (None, None) + grads


Expand Down Expand Up @@ -615,6 +643,14 @@ def __init__(self, fp8=False):

def checkpoint(self, run_function, *args):
"""Checkpoint function."""

# If in cuda graph warmup, disable checkpointing, as 'discard_output_and_register_recompute'
# may be called in a separate graph warmup.
from megatron.core.transformer.cuda_graphs import is_graph_warmup

if is_graph_warmup():
return run_function(*args)

self.run_function = run_function

self.rng_states = _get_all_rng_states()
Expand All @@ -628,11 +664,14 @@ def checkpoint(self, run_function, *args):
def _recompute(self, _):
"""Used as a hook to recompute the output."""

if self.ctx is None:
Comment thread
jiemingz marked this conversation as resolved.
# The recomputation has been triggered already. Just return.
from megatron.core.transformer.cuda_graphs import is_graph_capturing, is_graph_warmup

# The recomputation has been triggered already. Just return.
# Handle cudagraphs, do nothing if currently in graph warmup
if self.ctx is None or is_graph_warmup():
return

if not torch.autograd._is_checkpoint_valid():
if not torch.autograd._is_checkpoint_valid() and not is_graph_capturing():
Comment thread
jiemingz marked this conversation as resolved.
raise RuntimeError(
"Checkpointing is not compatible with .grad(), "
"please use .backward() if possible"
Expand Down Expand Up @@ -691,6 +730,12 @@ def discard_output_and_register_recompute(self, hook_tensor):
in the forward pass and the gradient of the hook_tensor is computed before the recomputed
tensors are used.
"""

from megatron.core.transformer.cuda_graphs import is_graph_warmup

if is_graph_warmup():
return

# use resize to release the output tensor memory and still keep the metadata in the tensors.
# the metadata is still needed for backward
for output in self.outputs:
Expand Down
Loading
Loading