Skip to content
Prev Previous commit
Next Next commit
Own delayed gradients during CUDA Graph replay
Capture deferred contributions in the graph pool and accumulate them into caller-owned gradients before delayed hooks. Cover both capture orders, default and explicit gradient resets, tied weights, microbatches, bias, retained gradients, and fused main_grad with independent PyTorch references. Native GPU validation is pending on the coordinated test machine.

Signed-off-by: 0z5a <dezhen.lu@student.uni-tuebingen.de>
  • Loading branch information
0z5a authored and denera committed Oct 30, 2026
commit 73aec37b5d71c5c1685b6c127ea1ef653572f14f
174 changes: 174 additions & 0 deletions tests/pytorch/test_cuda_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,180 @@ def test_make_graphed_callables_with_kwargs(
assert_all_equal(outputs, graph_outputs)


@pytest.mark.parametrize("with_order", (False, True))
@pytest.mark.parametrize("set_to_none", (False, True))
@pytest.mark.parametrize("bias", (False, True))
@pytest.mark.parametrize("dtype", (torch.float32, torch.bfloat16))
def test_make_graphed_callables_with_delayed_wgrad(with_order, set_to_none, bias, dtype):
"""Delayed replay survives zero_grad and matches independent optimizer steps."""
reset_rng_states()
hidden_size, batch_size = 16, 8
module = Linear(
hidden_size,
hidden_size,
bias=bias,
params_dtype=dtype,
delay_wgrad_compute=True,
fuse_wgrad_accumulation=False,
)
reference = torch.nn.Linear(hidden_size, hidden_size, bias=bias, device="cuda", dtype=dtype)
with torch.no_grad():
module.weight.fill_(0.25)
reference.weight.copy_(module.weight)
if bias:
module.bias.fill_(0.125)
reference.bias.copy_(module.bias)
graph_options = {}
if with_order:
graph_options = {"_order": [1, -1, -1.5], "_num_layers_per_chunk": [1]}
graphed = make_graphed_callables(
module,
(torch.ones(batch_size, hidden_size, device="cuda", dtype=dtype, requires_grad=True),),
**graph_options,
)
optimizers = [torch.optim.SGD(model.parameters(), lr=1 / 64) for model in (module, reference)]
saved_grads = []
try:
for _ in range(3):
for optimizer in optimizers:
optimizer.zero_grad(set_to_none=set_to_none)
# Bound the ordinary-allocator reuse pressure and check every buffer.
guards = [torch.full_like(module.weight, 17 + index) for index in range(32)]
input_ = torch.randint(-2, 3, (batch_size, hidden_size), device="cuda").to(dtype) / 4
graph_input = input_.detach().requires_grad_(True)
reference_input = input_.detach().clone().requires_grad_(True)
grad_output = (
torch.randint(-2, 3, (batch_size, hidden_size), device="cuda").to(dtype) / 8
)
output, expected = graphed(graph_input), reference(reference_input)
torch.testing.assert_close(output, expected, rtol=0, atol=0)
output.backward(grad_output)
expected.backward(grad_output)
graphed.backward_dw()
for index, guard in enumerate(guards):
torch.testing.assert_close(
guard, torch.full_like(guard, 17 + index), rtol=0, atol=0
)
torch.testing.assert_close(graph_input.grad, reference_input.grad, rtol=0, atol=0)
for param, reference_param in zip(module.parameters(), reference.parameters()):
assert param.grad is not None
torch.testing.assert_close(param.grad, reference_param.grad, rtol=0, atol=0)
if set_to_none:
for grad, snapshot in saved_grads:
torch.testing.assert_close(grad, snapshot, rtol=0, atol=0)
saved_grads.extend(
(param.grad, param.grad.clone()) for param in module.parameters()
)
for optimizer in optimizers:
optimizer.step()
for param, reference_param in zip(module.parameters(), reference.parameters()):
torch.testing.assert_close(param, reference_param, rtol=0, atol=0)
finally:
graphed.reset()


@pytest.mark.parametrize("with_order", (False, True))
@pytest.mark.parametrize("set_to_none", (False, True))
@pytest.mark.parametrize("tied", (False, True))
def test_graphed_delayed_wgrad_accumulates_microbatches(with_order, set_to_none, tied):
"""Separate delayed graphs contribute to shared weights across microbatches."""
reset_rng_states()
modules = tuple(
Linear(16, 16, bias=False, params_dtype=torch.float32, delay_wgrad_compute=True)
for _ in range(2)
)
reference = torch.nn.Sequential(
torch.nn.Linear(16, 16, bias=False, device="cuda"),
torch.nn.Linear(16, 16, bias=False, device="cuda"),
)
if tied:
modules[1].weight = modules[0].weight
reference[1].weight = reference[0].weight
with torch.no_grad():
for module, reference_module in zip(modules, reference):
module.weight.fill_(0.25)
reference_module.weight.copy_(module.weight)
graph_options = {}
if with_order:
graph_options = {"_order": [1, 2, -2, -2.5, -1, -1.5], "_num_layers_per_chunk": [1, 1]}
graphed = make_graphed_callables(
modules,
tuple((torch.ones(8, 16, device="cuda", requires_grad=True),) for _ in modules),
**graph_options,
)
optimizer = torch.optim.SGD(reference.parameters(), lr=1 / 64)
parameters = tuple(dict.fromkeys(param for module in modules for param in module.parameters()))
graph_optimizer = torch.optim.SGD(parameters, lr=1 / 64)
try:
for _ in range(3):
optimizer.zero_grad(set_to_none=set_to_none)
graph_optimizer.zero_grad(set_to_none=set_to_none)
for _ in range(2):
input_ = torch.randint(-2, 3, (8, 16), device="cuda").float() / 4
graph_input = input_.detach().requires_grad_(True)
reference_input = input_.detach().clone().requires_grad_(True)
output = graphed[1](graphed[0](graph_input))
expected = reference(reference_input)
torch.testing.assert_close(output, expected, rtol=0, atol=0)
output.sum().backward()
expected.sum().backward()
for callable_ in reversed(graphed):
callable_.backward_dw()
torch.testing.assert_close(graph_input.grad, reference_input.grad, rtol=0, atol=0)
for module, reference_module in zip(modules, reference):
assert module.weight.grad is not None
torch.testing.assert_close(
module.weight.grad, reference_module.weight.grad, rtol=0, atol=0
)
graph_optimizer.step()
optimizer.step()
for module, reference_module in zip(modules, reference):
torch.testing.assert_close(module.weight, reference_module.weight, rtol=0, atol=0)
finally:
reset_graphs(graphed)


@pytest.mark.parametrize("with_order", (False, True))
def test_graphed_delayed_wgrad_preserves_main_grad(with_order):
"""Fused accumulation continues to write main_grad across optimizer boundaries."""
reset_rng_states()
module = Linear(
16,
16,
bias=False,
params_dtype=torch.float32,
delay_wgrad_compute=True,
fuse_wgrad_accumulation=True,
)
module.weight.main_grad = torch.zeros_like(module.weight)
reference = torch.nn.Linear(16, 16, bias=False, device="cuda")
with torch.no_grad():
module.weight.fill_(0.25)
reference.weight.copy_(module.weight)
graph_options = {}
if with_order:
graph_options = {"_order": [1, -1, -1.5], "_num_layers_per_chunk": [1]}
graphed = make_graphed_callables(
module, (torch.ones(8, 16, device="cuda", requires_grad=True),), **graph_options
)
try:
for _ in range(2):
module.zero_grad()
module.weight.main_grad.zero_()
reference.zero_grad()
for _ in range(2):
input_ = torch.randint(-2, 3, (8, 16), device="cuda").float() / 4
graphed(input_.detach().requires_grad_(True)).sum().backward()
reference(input_.detach().clone().requires_grad_(True)).sum().backward()
graphed.backward_dw()
assert module.weight.grad is None
torch.testing.assert_close(
module.weight.main_grad, reference.weight.grad, rtol=0, atol=0
)
finally:
graphed.reset()


def test_make_graphed_callables_returns_owned_parameter_grads() -> None:
"""Parameter grads returned from graph replay must not alias static graph buffers."""
reset_rng_states()
Expand Down
46 changes: 34 additions & 12 deletions transformer_engine/pytorch/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,22 @@ def _returned_param_grad_clone_slots(static_grad_inputs, module_params):
# Filter the TE modules that cudagraph can access.
visited_te_modules = {}
need_bwd_dw_graph = {}
per_callable_static_wgrads = [()] * len(flatten_sample_args)

def _capture_backward_dw(func_idx, bwd_dw_graph):
"""Capture deferred contributions without retaining ordinary-allocator grad targets."""
modules = tuple(
module
for module in visited_te_modules[func_idx]
if hasattr(module, "need_backward_dw") and module.need_backward_dw()
)
params = tuple(dict.fromkeys(param for module in modules for param in module.parameters()))
# Warmup grads belong to the ordinary allocator. Capture fresh results
# in the graph pool so zero_grad() cannot free an accumulation target.
with _none_grad_context_wrapper(params), _graph_context_wrapper(bwd_dw_graph, pool=mempool):
for module in modules:
module.backward_dw()
return tuple((param, param.grad) for param in params if param.grad is not None)

def _run_warmup_forward(func_idx, func, callable_idx):
"""Run forward for one callable during warmup; returns flattened outputs."""
Expand Down Expand Up @@ -806,13 +822,9 @@ def _run_warmup_backward(func_idx, func, outputs, warmup_iter, callable_idx):
"No module needs wgrad computation but get float in order"
)
bwd_dw_graph = bwd_dw_graphs[per_callable_bwd_idx]
with _graph_context_wrapper(bwd_dw_graph, pool=mempool):
for module in visited_te_modules[per_callable_bwd_idx]:
if (
hasattr(module, "need_backward_dw")
and module.need_backward_dw()
):
module.backward_dw()
per_callable_static_wgrads[per_callable_bwd_idx] = _capture_backward_dw(
per_callable_bwd_idx, bwd_dw_graph
)
continue

static_input_surface = per_callable_static_input_surfaces[per_callable_bwd_idx]
Expand Down Expand Up @@ -1005,10 +1017,9 @@ def _run_warmup_backward(func_idx, func, outputs, warmup_iter, callable_idx):
)

if need_bwd_dw_graph[bwd_idx]:
with _graph_context_wrapper(bwd_dw_graph, pool=mempool):
for module in visited_te_modules[bwd_idx]:
if hasattr(module, "need_backward_dw") and module.need_backward_dw():
module.backward_dw()
per_callable_static_wgrads[bwd_idx] = _capture_backward_dw(
bwd_idx, bwd_dw_graph
)
# Constructs a tuple suitable for returning from Graphed.backward:
# Pads out the actually-needed grads with Nones in gradient slots for inputs that
# don't require grad. I couldn't think of a slick one-liner for this pattern.
Expand Down Expand Up @@ -1237,13 +1248,23 @@ def make_graphed_attribute_functions(graph_idx, helpers):
bwd_dw_graph = bwd_dw_graphs[graph_idx]
need_bwd_dw = need_bwd_dw_graph.get(graph_idx, False)
te_modules = visited_te_modules.get(graph_idx, set())
static_wgrads = per_callable_static_wgrads[graph_idx]

# Attach backward_dw as an attribute to the graphed callable.
def backward_dw():
helpers.ensure_not_reset()
if need_bwd_dw:
bwd_dw_graph.replay()

# Python assignments made during capture are not replayed. Give
# callers owned grads and accumulate tied/microbatch contributions
# before running their delayed-grad hooks.
for param, grad in static_wgrads:
if param.grad is None:
param.grad = grad.detach().clone()
else:
param.grad.add_(grad)

# Trigger the grad accumulation hook for wgrad graphs.
for module in te_modules:
if (
Expand All @@ -1254,7 +1275,7 @@ def backward_dw():

# Attach reset as an attribute to the graphed callable.
def reset():
nonlocal fwd_graph, bwd_graph, bwd_dw_graph, te_modules
nonlocal fwd_graph, bwd_graph, bwd_dw_graph, te_modules, static_wgrads

for graph in (fwd_graph, bwd_graph, bwd_dw_graph):
if graph is not None:
Expand All @@ -1264,6 +1285,7 @@ def reset():
bwd_graph = None
bwd_dw_graph = None
te_modules = ()
static_wgrads = ()
helpers.release_static_state()

return backward_dw, reset
Expand Down