Skip to content

Commit c3d3d0d

Browse files
xiaoyao0115claude
andcommitted
Check layouts of module-state outputs across slot branches
Outputs that view module parameters or buffers stay in the module's storage, and the branch family check skipped them, so branches of one slot could return such views with different shapes or strides although _graph_memory_slots requires identical output layouts. They are now compared like arena outputs; only their storage may differ. The docstring states that exception, and the layout-rejection test adds a module-state case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tailai Ma <tailaim@nvidia.com>
1 parent 6282d0d commit c3d3d0d

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

‎tests/pytorch/test_cuda_graphs.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3730,22 +3730,26 @@ def forward(self, inp):
37303730
reset_graphs(graphed)
37313731

37323732

3733-
@pytest.mark.parametrize("different_layout", ("shape", "stride"))
3733+
@pytest.mark.parametrize("different_layout", ("shape", "stride", "module_state"))
37343734
def test_slot_memory_rejects_incompatible_branch_output_layouts(different_layout) -> None:
3735-
"""Mutually exclusive branches must expose the same output shape and strides."""
3735+
"""Mutually exclusive branches must expose the same output shapes and strides.
3736+
3737+
This includes outputs that view module state, which stay outside the slot arenas.
3738+
"""
37363739

37373740
class Module(torch.nn.Module):
37383741
def __init__(self, layout):
37393742
super().__init__()
37403743
self.layout = layout
3744+
self.register_buffer("state", torch.zeros(4, device="cuda"))
37413745

37423746
def forward(self, inp):
37433747
output = inp * 2.0
37443748
if self.layout == "shape":
3745-
return output[:2]
3746-
if self.layout == "stride":
3747-
return output.t()
3748-
return output
3749+
output = output[:2]
3750+
elif self.layout == "stride":
3751+
output = output.t()
3752+
return output, self.state[: 3 if self.layout == "module_state" else 2]
37493753

37503754
modules = (Module("same").cuda(), Module(different_layout).cuda())
37513755
samples = tuple((torch.randn(4, 4, device="cuda", requires_grad=True),) for _ in modules)

‎transformer_engine/pytorch/graph.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,9 +1845,8 @@ def storage_alias_topology(plan):
18451845
f"at position {tensor_idx}."
18461846
)
18471847
modes = {spec[0] for spec in specs}
1848-
if modes == {"external_output"}:
1849-
continue
1850-
if modes != {kind}:
1848+
# Module-state views keep their own storage but still need one layout.
1849+
if modes not in ({kind}, {"external_output"}):
18511850
raise RuntimeError(
18521851
f"CUDA graph {kind} family {family} has incompatible storage modes "
18531852
f"at position {tensor_idx}: {sorted(modes)}."
@@ -3382,7 +3381,8 @@ def make_graphed_callables(
33823381
arena only if their forward-to-backward lifetimes never overlap in any schedule the
33833382
caller replays. Graph inputs with equal ``(io_slot, chunk, layer)`` are branches of one
33843383
slot: they must name the same ``saved_arena`` and ``grad_arena``, expose identical output
3385-
and gradient layouts, are told apart by ``branch``, and share output and gradient storage.
3384+
and gradient layouts, are told apart by ``branch``, and share output and gradient storage
3385+
(outputs that view module parameters or buffers keep that storage instead).
33863386
Graph inputs with equal ``warmup_group`` share one warmup run.
33873387
Only tensors saved for backward (``ctx.save_for_backward`` or saved-tensor hooks) are
33883388
protected between a forward replay and its backward replay. A tensor that backward reads

0 commit comments

Comments
 (0)