Skip to content
21 changes: 12 additions & 9 deletions megatron/core/models/gpt/fine_grained_callables.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def should_free_input(name, is_moe, config):
# The input and output of A2A are not needed anymore after the forward pass,
# so we can free the input memory after the forward pass.
free_input_nodes = {
"mlp": not enable_hybridep,
"mlp": not (enable_hybridep and config.fp8 is None),
Comment thread
yaox12 marked this conversation as resolved.
Resolved
"moe_combine": True,
# For non-DeepEP and non-HybridEP dispatcher mode, the input is the un-dispatched tokens
# and probs before dispatch A2A and it's not needed anymore after the forward pass
Expand Down Expand Up @@ -316,7 +316,7 @@ def backward_dw(self):
"""Computes the weight gradients for the transformer layer node."""
if not self.delay_wgrad_compute:
return
with torch.cuda.nvtx.range(f"{self.name} wgrad"):
with self.stream_acquire_context(f"{self.name} wgrad"):
for module in self.bwd_dw_callables:
module.backward_dw()

Expand Down Expand Up @@ -514,15 +514,15 @@ def submodule_dispatch_forward(
token_dispatcher._comm_manager.token_probs = probs

dispatched_tokens, dispatched_probs = layer.mlp.dispatch(local_tokens, probs)
node.layer_state.dispatched_probs = node.detach(dispatched_probs)
return dispatched_tokens
return dispatched_tokens, dispatched_probs

def submodule_moe_forward(node: ScheduleNode, dispatched_tokens: torch.Tensor):
def submodule_moe_forward(
node: ScheduleNode, dispatched_tokens: torch.Tensor, dispatched_probs: torch.Tensor
):
"""
Run forward pass for computations between dispatch and combine:
post dispatch->experts->combine preprocess
"""
dispatched_probs = node.layer_state.dispatched_probs
token_dispatcher = layer.mlp.token_dispatcher
if enable_deepep or enable_hybridep:
# update dispatched_probs to be detached version, prevents
Expand All @@ -531,13 +531,16 @@ def submodule_moe_forward(node: ScheduleNode, dispatched_tokens: torch.Tensor):

expert_output, _ = layer.mlp.routed_experts_compute(dispatched_tokens, dispatched_probs)

# For HybridEP, tokens_per_expert is generated on comm stream, as the input to
# `routed_experts_compute`, it needs to be recorded to comp stream.
if enable_hybridep:
tokens_per_expert = token_dispatcher._comm_manager.get_number_of_tokens_per_expert()
tokens_per_expert.record_stream(torch.cuda.current_stream())

if layer.recompute_pre_mlp_layernorm:
# discard the output of the pre-mlp layernorm and register the recompute
# as a gradient hook of expert_output
layer.pre_mlp_norm_checkpoint.discard_output_and_register_recompute(expert_output)
# release tensor reference after use
node.layer_state.dispatched_probs = None
node.layer_state.pre_mlp_layernorm_output = None

return expert_output

Expand Down
86 changes: 47 additions & 39 deletions megatron/core/pipeline_parallel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,6 @@ def set_ideal_affinity_for_current_gpu():
)


@contextmanager
def stream_acquire_context(stream, event):
"""Stream acquire context"""
event.wait(stream)
try:
yield
finally:
event.record(stream)


class NoopScheduleNode:
"""A placeholder node in the computation graph that simply passes through inputs and outputs.

Expand Down Expand Up @@ -208,26 +198,21 @@ def forward(self, inputs=()):
return self._forward(*inputs)

def _forward(self, *inputs):
with stream_acquire_context(self.stream, self.event):
torch.cuda.nvtx.range_push(f"{self.name} forward")
with torch.cuda.stream(self.stream):
self.inputs = [make_viewless(e).detach() if e is not None else None for e in inputs]
for i, input in enumerate(self.inputs):
if input is not None:
input.requires_grad = inputs[i].requires_grad
with self.stream_acquire_context(f"{self.name} forward"):
self.inputs = [make_viewless(e).detach() if e is not None else None for e in inputs]
for i, input in enumerate(self.inputs):
if input is not None:
input.requires_grad = inputs[i].requires_grad

data = tuple(self.inputs)
data = self.forward_func(*data)
data = tuple(self.inputs)
data = self.forward_func(*data)

if not isinstance(data, tuple):
data = make_viewless(data)
else:
data = tuple(
[make_viewless(e) if isinstance(e, torch.Tensor) else e for e in data]
)
if not isinstance(data, tuple):
data = make_viewless(data)
else:
data = tuple([make_viewless(e) if isinstance(e, torch.Tensor) else e for e in data])

self.output = data
torch.cuda.nvtx.range_pop()
self.output = data

# Immediately frees input tensors after they are used for nodes
# where inputs are no longer needed after computation.
Expand All @@ -250,18 +235,15 @@ def backward(self, output_grad):
return self._backward(*output_grad)

def _backward(self, *output_grad):
with stream_acquire_context(self.stream, self.event):
torch.cuda.nvtx.range_push(f"{self.name} backward")
with torch.cuda.stream(self.stream):
outputs = self.output
if not isinstance(outputs, tuple):
outputs = (outputs,)
assert len(outputs) == len(output_grad), (
f"{len(outputs)} of {type(outputs[0])} is not equal to "
f"{len(output_grad)} of {type(output_grad[0])}"
)
output_grad = self.backward_func(outputs, output_grad)
torch.cuda.nvtx.range_pop()
with self.stream_acquire_context(f"{self.name} backward"):
outputs = self.output
if not isinstance(outputs, tuple):
outputs = (outputs,)
assert len(outputs) == len(output_grad), (
f"{len(outputs)} of {type(outputs[0])} is not equal to "
f"{len(output_grad)} of {type(output_grad[0])}"
)
output_grad = self.backward_func(outputs, output_grad)

# output_grad maybe from another stream
if output_grad:
Expand All @@ -288,6 +270,32 @@ def get_grad(self):
grad = grad[0]
return grad

@contextmanager
def stream_acquire_context(self, name=None):
"""Stream acquire context that handles event synchronization,
NVTX profiling, and stream context.

This context manager consolidates:
1. Event wait/record for synchronization between streams
2. NVTX range for profiling (if name is provided)
3. torch.cuda.stream context for execution on the specified stream

Args:
stream: The CUDA stream to execute on
event: The CUDA event for synchronization
name: Optional name for NVTX range profiling
"""
self.event.wait(self.stream)
if name:
torch.cuda.nvtx.range_push(name)
Comment thread
Wohox marked this conversation as resolved.
Resolved
try:
with torch.cuda.stream(self.stream):
yield
finally:
if name:
torch.cuda.nvtx.range_pop()
self.event.record(self.stream)

def _release_state(self):
"""Clear the state of the node"""
self.inputs = None
Expand Down