Skip to content

[graph_trainer] Port remat_using_tags_for_fwd_loss_bwd_graph pass locally - #3260

Merged
SherlockNoMad merged 1 commit into
mainfrom
graph_trainer/port-selective-activation-remat
May 7, 2026
Merged

SherlockNoMad merged 1 commit into
mainfrom
graph_trainer/port-selective-activation-remat

Conversation

@SherlockNoMad

@SherlockNoMad SherlockNoMad commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

All credit goes to original author: @tugsbayasgalan

Why

  • we have some coupling between cpu_offload_pass and remat_using_tags_for_fwd_loss_bwd_graph, let's have them in the same repo.
  • we don't have real user in core anyway.

Summary

  • Copy the upstream remat_using_tags_for_fwd_loss_bwd_graph pass from torch._functorch._activation_checkpointing into graph_trainer/selective_activation_remat.py so we can iterate on it independently without waiting on PyTorch releases.
  • Update the import in passes.py to use the local version.

Test plan

  • Existing integration tests pass (pytest tests/ for graph_trainer)
  • Verify numerical parity with upstream pass using --debug.seed=42 --debug.deterministic

…ally

Copy the upstream remat pass from torch._functorch._activation_checkpointing
into graph_trainer/selective_activation_remat.py so we can iterate on it
independently without waiting on PyTorch releases. Update the import in
passes.py to use the local version.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 7, 2026
@SherlockNoMad
SherlockNoMad merged commit 466da8c into main May 7, 2026
19 of 20 checks passed
tugsbayasgalan added a commit that referenced this pull request May 7, 2026
Summary:
The remat pass previously rebuilt the graph wholesale (fx.Graph() +
node_copy of every node) and relied on whole-graph DCE to remove dead
must_recompute originals. Refactor to mutate gm.graph in place: dups
are inserted in front of their first backward consumer, backward args
are redirected to the dups, and only originals whose users became
empty are erased. Original node identities and names are preserved,
the topological-order assumption is explicit (input graph order
drives insertion, validated by gm.graph.lint() at the end), and the
underlying function takes the standard (gm, example_inputs) graph
pass signature.

CPU-offload reload chains are handled by hoisting (in-place move) the
chain in front of the earliest dup that needs it - the in-place
equivalent of upstream's "eagerly copy reload chain into the new
graph" trick. This restores offload + remat support that the local
port commit (D-equivalent of GitHub PR #3260) silently dropped: on
main today, --compile.memory_policy=budget_limited_offload crashes
with CUBLAS_STATUS_EXECUTION_FAILED because dups reference the
freed-GPU offloaded forward tensors.

Review order:

selective_activation_remat.py - the core refactor. A pre-pass
claim_for records the first backward node that transitively reaches
each must_recompute forward node (setdefault gives first-write-wins).
The insertion loop walks gm.graph.nodes in forward order, so each
dup's must_recompute deps are already in recomputed_nodes by the
time remat_input resolves them - no explicit sort. Dups are tagged
autograd_backward=True so downstream passes treat them as
backward-region nodes. remat_input also handles the
cpu_offload_reload_node case: when a dup references an offloaded
forward node, ensure_offload_chain_before walks the bwd-region
reload chain via an explicit DFS stack, then hoists chain members in
graph (topological) order by walking the all_nodes snapshot - again
no explicit sort. moved_offload bookkeeping ensures the chain ends
up before the earliest target across multiple iterations.
raise_getitems still runs at the end for the multi-output-op memory
benefit; gm.graph.lint() validates the final graph.

common_utils.py / fsdp_passes.py - drops the _is_recomputed_node
name-suffix workaround now that dups carry autograd_backward=True
directly.

passes.py - removes the selective_activation_remat_pass wrapper
since the underlying function matches the standard signature; the
pass list references it directly.

tests/test_passes.py - adds TestSelectiveActivationRematPass with
three cases: the reviewer scenario from pytorch/pytorch#168082
(pins topological insertion order), a forward+backward consumer mix
(targeted erase keeps originals with surviving forward users), and
the offload reload chain hoist - synthesizing the actual
ao.offload / ao.reload / ao.wait_tensor graph the offload pass
produces and pinning that the chain ends up before the dup with the
dup reading from the backward wait_tensor instead of F.

Authored with Claude.

Test Plan:
Unit tests:
  pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
  -> 68 passed, 1 skipped (3 new in TestSelectiveActivationRematPass)

End-to-end on 8xH100 / Llama3 8B / FSDP=4 + TP=2 / aot_fx_trace /
no cudagraph, with --debug.seed=42 --debug.deterministic:

  No offload (memory_policy=default):
    loss / grad_norm / peak memory at step 10 are bitwise identical
    to the main baseline (1.65096 / 21.9799 / 30.78 GiB).

  --compile.memory_policy=budget_limited_offload:
    refactor matches a baseline patched to invoke upstream remat
    directly (1.65096 / 21.9799 / 29.42 GiB). main HEAD (which calls
    the local port) crashes with CUBLAS_STATUS_EXECUTION_FAILED on
    this same configuration; the refactor incidentally fixes that
    regression.
tugsbayasgalan added a commit that referenced this pull request May 7, 2026
Summary:
The remat pass previously rebuilt the graph wholesale (fx.Graph() +
node_copy of every node) and relied on whole-graph DCE to remove dead
must_recompute originals. Refactor to mutate gm.graph in place: dups
are inserted in front of their first backward consumer, backward args
are redirected to the dups, and only originals whose users became
empty are erased. Original node identities and names are preserved,
the topological-order assumption is explicit (input graph order
drives insertion, validated by gm.graph.lint() at the end), and the
underlying function takes the standard (gm, example_inputs) graph
pass signature.

CPU-offload reload chains are handled by hoisting (in-place move) the
chain in front of the earliest dup that needs it - the in-place
equivalent of upstream's "eagerly copy reload chain into the new
graph" trick. This restores offload + remat support that the local
port commit (D-equivalent of GitHub PR #3260) silently dropped: on
main today, --compile.memory_policy=budget_limited_offload crashes
with CUBLAS_STATUS_EXECUTION_FAILED because dups reference the
freed-GPU offloaded forward tensors.

Review order:

selective_activation_remat.py - the core refactor. A pre-pass
claim_for records the first backward node that transitively reaches
each must_recompute forward node (setdefault gives first-write-wins).
The insertion loop walks gm.graph.nodes in forward order, so each
dup's must_recompute deps are already in recomputed_nodes by the
time remat_input resolves them - no explicit sort. Dups are tagged
autograd_backward=True so downstream passes treat them as
backward-region nodes. remat_input also handles the
cpu_offload_reload_node case: when a dup references an offloaded
forward node, ensure_offload_chain_before walks the bwd-region
reload chain via an explicit DFS stack, then hoists chain members in
graph (topological) order by walking the all_nodes snapshot - again
no explicit sort. moved_offload bookkeeping ensures the chain ends
up before the earliest target across multiple iterations.
raise_getitems still runs at the end for the multi-output-op memory
benefit; gm.graph.lint() validates the final graph.

common_utils.py / fsdp_passes.py - drops the _is_recomputed_node
name-suffix workaround now that dups carry autograd_backward=True
directly.

passes.py - removes the selective_activation_remat_pass wrapper
since the underlying function matches the standard signature; the
pass list references it directly.

tests/test_passes.py - adds TestSelectiveActivationRematPass with
three cases: the reviewer scenario from pytorch/pytorch#168082
(pins topological insertion order), a forward+backward consumer mix
(targeted erase keeps originals with surviving forward users), and
the offload reload chain hoist - synthesizing the actual
ao.offload / ao.reload / ao.wait_tensor graph the offload pass
produces and pinning that the chain ends up before the dup with the
dup reading from the backward wait_tensor instead of F.

Authored with Claude.

Test Plan:
Unit tests:
  pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
  -> 68 passed, 1 skipped (3 new in TestSelectiveActivationRematPass)

End-to-end on 8xH100 / Llama3 8B / FSDP=4 + TP=2 / aot_fx_trace /
no cudagraph, with --debug.seed=42 --debug.deterministic:

  No offload (memory_policy=default):
    loss / grad_norm / peak memory at step 10 are bitwise identical
    to the main baseline (1.65096 / 21.9799 / 30.78 GiB).

  --compile.memory_policy=budget_limited_offload:
    refactor matches a baseline patched to invoke upstream remat
    directly (1.65096 / 21.9799 / 29.42 GiB). main HEAD (which calls
    the local port) crashes with CUBLAS_STATUS_EXECUTION_FAILED on
    this same configuration; the refactor incidentally fixes that
    regression.
tugsbayasgalan added a commit that referenced this pull request May 7, 2026
Summary:
The remat pass previously rebuilt the graph wholesale (fx.Graph() +
node_copy of every node) and relied on whole-graph DCE to remove dead
must_recompute originals. Refactor to mutate gm.graph in place: dups
are inserted in front of their first backward consumer, backward args
are redirected to the dups, and only originals whose users became
empty are erased. Original node identities and names are preserved,
the topological-order assumption is explicit (input graph order
drives insertion, validated by gm.graph.lint() at the end), and the
underlying function takes the standard (gm, example_inputs) graph
pass signature.

CPU-offload reload chains are handled by hoisting (in-place move) the
chain in front of the earliest dup that needs it - the in-place
equivalent of upstream's "eagerly copy reload chain into the new
graph" trick. This restores offload + remat support that the local
port commit (D-equivalent of GitHub PR #3260) silently dropped: on
main today, --compile.memory_policy=budget_limited_offload crashes
with CUBLAS_STATUS_EXECUTION_FAILED because dups reference the
freed-GPU offloaded forward tensors.

Review order:

selective_activation_remat.py - the core refactor. A pre-pass
claim_for records the first backward node that transitively reaches
each must_recompute forward node (setdefault gives first-write-wins).
The insertion loop walks gm.graph.nodes in forward order, so each
dup's must_recompute deps are already in recomputed_nodes by the
time remat_input resolves them - no explicit sort. Dups are tagged
autograd_backward=True so downstream passes treat them as
backward-region nodes. remat_input also handles the
cpu_offload_reload_node case: when a dup references an offloaded
forward node, ensure_offload_chain_before walks the bwd-region
reload chain via an explicit DFS stack, then hoists chain members in
graph (topological) order by walking the all_nodes snapshot - again
no explicit sort. moved_offload bookkeeping ensures the chain ends
up before the earliest target across multiple iterations.
raise_getitems still runs at the end for the multi-output-op memory
benefit; gm.graph.lint() validates the final graph.

common_utils.py / fsdp_passes.py - drops the _is_recomputed_node
name-suffix workaround now that dups carry autograd_backward=True
directly.

passes.py - removes the selective_activation_remat_pass wrapper
since the underlying function matches the standard signature; the
pass list references it directly.

tests/test_passes.py - adds TestSelectiveActivationRematPass with
three cases: the reviewer scenario from pytorch/pytorch#168082
(pins topological insertion order), a forward+backward consumer mix
(targeted erase keeps originals with surviving forward users), and
the offload reload chain hoist - synthesizing the actual
ao.offload / ao.reload / ao.wait_tensor graph the offload pass
produces and pinning that the chain ends up before the dup with the
dup reading from the backward wait_tensor instead of F.

Authored with Claude.

Test Plan:
Unit tests:
  pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
  -> 68 passed, 1 skipped (3 new in TestSelectiveActivationRematPass)

End-to-end on 8xH100 / Llama3 8B / FSDP=4 + TP=2 / aot_fx_trace /
no cudagraph, with --debug.seed=42 --debug.deterministic:

  No offload (memory_policy=default):
    loss / grad_norm / peak memory at step 10 are bitwise identical
    to the main baseline (1.65096 / 21.9799 / 30.78 GiB).

  --compile.memory_policy=budget_limited_offload:
    refactor matches a baseline patched to invoke upstream remat
    directly (1.65096 / 21.9799 / 29.42 GiB). main HEAD (which calls
    the local port) crashes with CUBLAS_STATUS_EXECUTION_FAILED on
    this same configuration; the refactor incidentally fixes that
    regression.
tugsbayasgalan added a commit that referenced this pull request May 8, 2026
Summary:
The remat pass previously rebuilt the graph wholesale (fx.Graph() +
node_copy of every node) and relied on whole-graph DCE to remove dead
must_recompute originals. Refactor to mutate gm.graph in place: dups
are inserted in front of their first backward consumer, backward args
are redirected to the dups, and only originals whose users became
empty are erased. Original node identities and names are preserved,
the topological-order assumption is explicit (input graph order
drives insertion, validated by gm.graph.lint() at the end), and the
underlying function takes the standard (gm, example_inputs) graph
pass signature.

CPU-offload reload chains are handled by hoisting (in-place move) the
chain in front of the earliest dup that needs it - the in-place
equivalent of upstream's "eagerly copy reload chain into the new
graph" trick. This restores offload + remat support that the local
port commit (D-equivalent of GitHub PR #3260) silently dropped: on
main today, --compile.memory_policy=budget_limited_offload crashes
with CUBLAS_STATUS_EXECUTION_FAILED because dups reference the
freed-GPU offloaded forward tensors.

Review order:

selective_activation_remat.py - the core refactor. A pre-pass
claim_for records the first backward node that transitively reaches
each must_recompute forward node (setdefault gives first-write-wins).
The insertion loop walks gm.graph.nodes in forward order, so each
dup's must_recompute deps are already in recomputed_nodes by the
time remat_input resolves them - no explicit sort. Dups are tagged
autograd_backward=True so downstream passes treat them as
backward-region nodes. remat_input also handles the
cpu_offload_reload_node case: when a dup references an offloaded
forward node, ensure_offload_chain_before walks the bwd-region
reload chain via an explicit DFS stack, then hoists chain members in
graph (topological) order by walking the all_nodes snapshot - again
no explicit sort. moved_offload bookkeeping ensures the chain ends
up before the earliest target across multiple iterations.
raise_getitems still runs at the end for the multi-output-op memory
benefit; gm.graph.lint() validates the final graph.

common_utils.py / fsdp_passes.py - drops the _is_recomputed_node
name-suffix workaround now that dups carry autograd_backward=True
directly.

passes.py - removes the selective_activation_remat_pass wrapper
since the underlying function matches the standard signature; the
pass list references it directly.

tests/test_passes.py - adds TestSelectiveActivationRematPass with
three cases: the reviewer scenario from pytorch/pytorch#168082
(pins topological insertion order), a forward+backward consumer mix
(targeted erase keeps originals with surviving forward users), and
the offload reload chain hoist - synthesizing the actual
ao.offload / ao.reload / ao.wait_tensor graph the offload pass
produces and pinning that the chain ends up before the dup with the
dup reading from the backward wait_tensor instead of F.

Authored with Claude.

Test Plan:
Unit tests:
  pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
  -> 68 passed, 1 skipped (3 new in TestSelectiveActivationRematPass)

End-to-end on 8xH100 / Llama3 8B / FSDP=4 + TP=2 / aot_fx_trace /
no cudagraph, with --debug.seed=42 --debug.deterministic:

  No offload (memory_policy=default):
    loss / grad_norm / peak memory at step 10 are bitwise identical
    to the main baseline (1.65096 / 21.9799 / 30.78 GiB).

  --compile.memory_policy=budget_limited_offload:
    refactor matches a baseline patched to invoke upstream remat
    directly (1.65096 / 21.9799 / 29.42 GiB). main HEAD (which calls
    the local port) crashes with CUBLAS_STATUS_EXECUTION_FAILED on
    this same configuration; the refactor incidentally fixes that
    regression.
tugsbayasgalan added a commit that referenced this pull request May 8, 2026
Summary:
The remat pass previously rebuilt the graph wholesale (fx.Graph() +
node_copy of every node) and relied on whole-graph DCE to remove dead
must_recompute originals. Refactor to mutate gm.graph in place: dups
are inserted in front of their first backward consumer, backward args
are redirected to the dups, and only originals whose users became
empty are erased. Original node identities and names are preserved,
the topological-order assumption is explicit (input graph order
drives insertion, validated by gm.graph.lint() at the end), and the
underlying function takes the standard (gm, example_inputs) graph
pass signature.

CPU-offload reload chains are handled by hoisting (in-place move) the
chain in front of the earliest dup that needs it - the in-place
equivalent of upstream's "eagerly copy reload chain into the new
graph" trick. This restores offload + remat support that the local
port commit (D-equivalent of GitHub PR #3260) silently dropped: on
main today, --compile.memory_policy=budget_limited_offload crashes
with CUBLAS_STATUS_EXECUTION_FAILED because dups reference the
freed-GPU offloaded forward tensors.

Review order:

selective_activation_remat.py - the core refactor. A pre-pass
claim_for records the first backward node that transitively reaches
each must_recompute forward node (setdefault gives first-write-wins).
The insertion loop walks gm.graph.nodes in forward order, so each
dup's must_recompute deps are already in recomputed_nodes by the
time remat_input resolves them - no explicit sort. Dups are tagged
autograd_backward=True so downstream passes treat them as
backward-region nodes. remat_input also handles the
cpu_offload_reload_node case: when a dup references an offloaded
forward node, ensure_offload_chain_before walks the bwd-region
reload chain via an explicit DFS stack, then hoists chain members in
graph (topological) order by walking the all_nodes snapshot - again
no explicit sort. moved_offload bookkeeping ensures the chain ends
up before the earliest target across multiple iterations.
raise_getitems still runs at the end for the multi-output-op memory
benefit; gm.graph.lint() validates the final graph.

common_utils.py / fsdp_passes.py - drops the _is_recomputed_node
name-suffix workaround now that dups carry autograd_backward=True
directly.

passes.py - removes the selective_activation_remat_pass wrapper
since the underlying function matches the standard signature; the
pass list references it directly.

tests/test_passes.py - adds TestSelectiveActivationRematPass with
three cases: the reviewer scenario from pytorch/pytorch#168082
(pins topological insertion order), a forward+backward consumer mix
(targeted erase keeps originals with surviving forward users), and
the offload reload chain hoist - synthesizing the actual
ao.offload / ao.reload / ao.wait_tensor graph the offload pass
produces and pinning that the chain ends up before the dup with the
dup reading from the backward wait_tensor instead of F.

Authored with Claude.

Test Plan:
Unit tests:
  pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
  -> 68 passed, 1 skipped (3 new in TestSelectiveActivationRematPass)

End-to-end on 8xH100 / Llama3 8B / FSDP=4 + TP=2 / aot_fx_trace /
no cudagraph, with --debug.seed=42 --debug.deterministic:

  No offload (memory_policy=default):
    loss / grad_norm / peak memory at step 10 are bitwise identical
    to the main baseline (1.65096 / 21.9799 / 30.78 GiB).

  --compile.memory_policy=budget_limited_offload:
    refactor matches a baseline patched to invoke upstream remat
    directly (1.65096 / 21.9799 / 29.42 GiB). main HEAD (which calls
    the local port) crashes with CUBLAS_STATUS_EXECUTION_FAILED on
    this same configuration; the refactor incidentally fixes that
    regression.
tugsbayasgalan added a commit that referenced this pull request May 8, 2026
Summary:
The remat pass previously rebuilt the graph wholesale (fx.Graph() +
node_copy of every node) and relied on whole-graph DCE to remove dead
must_recompute originals. Refactor to mutate gm.graph in place: dups
are inserted in front of their first backward consumer, backward args
are redirected to the dups, and only originals whose users became
empty are erased. Original node identities and names are preserved,
the topological-order assumption is explicit (input graph order
drives insertion, validated by gm.graph.lint() at the end), and the
underlying function takes the standard (gm, example_inputs) graph
pass signature.

CPU-offload reload chains are handled by hoisting (in-place move) the
chain in front of the earliest dup that needs it - the in-place
equivalent of upstream's "eagerly copy reload chain into the new
graph" trick. This restores offload + remat support that the local
port commit (D-equivalent of GitHub PR #3260) silently dropped: on
main today, --compile.memory_policy=budget_limited_offload crashes
with CUBLAS_STATUS_EXECUTION_FAILED because dups reference the
freed-GPU offloaded forward tensors.

Review order:

selective_activation_remat.py - the core refactor. A pre-pass
claim_for records the first backward node that transitively reaches
each must_recompute forward node (setdefault gives first-write-wins).
The insertion loop walks gm.graph.nodes in forward order, so each
dup's must_recompute deps are already in recomputed_nodes by the
time remat_input resolves them - no explicit sort. Dups are tagged
autograd_backward=True so downstream passes treat them as
backward-region nodes. remat_input also handles the
cpu_offload_reload_node case: when a dup references an offloaded
forward node, ensure_offload_chain_before walks the bwd-region
reload chain via an explicit DFS stack, then hoists chain members in
graph (topological) order by walking the all_nodes snapshot - again
no explicit sort. moved_offload bookkeeping ensures the chain ends
up before the earliest target across multiple iterations.
raise_getitems still runs at the end for the multi-output-op memory
benefit; gm.graph.lint() validates the final graph.

common_utils.py / fsdp_passes.py - drops the _is_recomputed_node
name-suffix workaround now that dups carry autograd_backward=True
directly.

passes.py - removes the selective_activation_remat_pass wrapper
since the underlying function matches the standard signature; the
pass list references it directly.

tests/test_passes.py - adds TestSelectiveActivationRematPass with
three cases: the reviewer scenario from pytorch/pytorch#168082
(pins topological insertion order), a forward+backward consumer mix
(targeted erase keeps originals with surviving forward users), and
the offload reload chain hoist - synthesizing the actual
ao.offload / ao.reload / ao.wait_tensor graph the offload pass
produces and pinning that the chain ends up before the dup with the
dup reading from the backward wait_tensor instead of F.

Authored with Claude.

Test Plan:
Unit tests:
  pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
  -> 68 passed, 1 skipped (3 new in TestSelectiveActivationRematPass)

End-to-end on 8xH100 / Llama3 8B / FSDP=4 + TP=2 / aot_fx_trace /
no cudagraph, with --debug.seed=42 --debug.deterministic:

  No offload (memory_policy=default):
    loss / grad_norm / peak memory at step 10 are bitwise identical
    to the main baseline (1.65096 / 21.9799 / 30.78 GiB).

  --compile.memory_policy=budget_limited_offload:
    refactor matches a baseline patched to invoke upstream remat
    directly (1.65096 / 21.9799 / 29.42 GiB). main HEAD (which calls
    the local port) crashes with CUBLAS_STATUS_EXECUTION_FAILED on
    this same configuration; the refactor incidentally fixes that
    regression.
tugsbayasgalan added a commit that referenced this pull request May 11, 2026
Summary:
The remat pass previously rebuilt the graph wholesale (fx.Graph() +
node_copy of every node) and relied on whole-graph DCE to remove dead
must_recompute originals. Refactor to mutate gm.graph in place: dups
are inserted in front of their first backward consumer, backward args
are redirected to the dups, and only originals whose users became
empty are erased. Original node identities and names are preserved,
the topological-order assumption is explicit (input graph order
drives insertion, validated by gm.graph.lint() at the end), and the
underlying function takes the standard (gm, example_inputs) graph
pass signature.

CPU-offload reload chains are handled by hoisting (in-place move) the
chain in front of the earliest dup that needs it - the in-place
equivalent of upstream's "eagerly copy reload chain into the new
graph" trick. This restores offload + remat support that the local
port commit (D-equivalent of GitHub PR #3260) silently dropped: on
main today, --compile.memory_policy=budget_limited_offload crashes
with CUBLAS_STATUS_EXECUTION_FAILED because dups reference the
freed-GPU offloaded forward tensors.

Review order:

selective_activation_remat.py - the core refactor. A pre-pass
claim_for records the first backward node that transitively reaches
each must_recompute forward node (setdefault gives first-write-wins).
The insertion loop walks gm.graph.nodes in forward order, so each
dup's must_recompute deps are already in recomputed_nodes by the
time remat_input resolves them - no explicit sort. Dups are tagged
autograd_backward=True so downstream passes treat them as
backward-region nodes. remat_input also handles the
cpu_offload_reload_node case: when a dup references an offloaded
forward node, ensure_offload_chain_before walks the bwd-region
reload chain via an explicit DFS stack, then hoists chain members in
graph (topological) order by walking the all_nodes snapshot - again
no explicit sort. moved_offload bookkeeping ensures the chain ends
up before the earliest target across multiple iterations.
raise_getitems still runs at the end for the multi-output-op memory
benefit; gm.graph.lint() validates the final graph.

common_utils.py / fsdp_passes.py - drops the _is_recomputed_node
name-suffix workaround now that dups carry autograd_backward=True
directly.

passes.py - removes the selective_activation_remat_pass wrapper
since the underlying function matches the standard signature; the
pass list references it directly.

tests/test_passes.py - adds TestSelectiveActivationRematPass with
three cases: the reviewer scenario from pytorch/pytorch#168082
(pins topological insertion order), a forward+backward consumer mix
(targeted erase keeps originals with surviving forward users), and
the offload reload chain hoist - synthesizing the actual
ao.offload / ao.reload / ao.wait_tensor graph the offload pass
produces and pinning that the chain ends up before the dup with the
dup reading from the backward wait_tensor instead of F.

Authored with Claude.

Test Plan:
Unit tests:
  pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
  -> 68 passed, 1 skipped (3 new in TestSelectiveActivationRematPass)

End-to-end on 8xH100 / Llama3 8B / FSDP=4 + TP=2 / aot_fx_trace /
no cudagraph, with --debug.seed=42 --debug.deterministic:

  No offload (memory_policy=default):
    loss / grad_norm / peak memory at step 10 are bitwise identical
    to the main baseline (1.65096 / 21.9799 / 30.78 GiB).

  --compile.memory_policy=budget_limited_offload:
    refactor matches a baseline patched to invoke upstream remat
    directly (1.65096 / 21.9799 / 29.42 GiB). main HEAD (which calls
    the local port) crashes with CUBLAS_STATUS_EXECUTION_FAILED on
    this same configuration; the refactor incidentally fixes that
    regression.
tugsbayasgalan added a commit that referenced this pull request May 11, 2026
Summary:
The remat pass previously rebuilt the graph wholesale (fx.Graph() +
node_copy of every node) and relied on whole-graph DCE to remove dead
must_recompute originals. Refactor to mutate gm.graph in place: dups
are inserted in front of their first backward consumer, backward args
are redirected to the dups, and only originals whose users became
empty are erased. Original node identities and names are preserved,
the topological-order assumption is explicit (input graph order
drives insertion, validated by gm.graph.lint() at the end), and the
underlying function takes the standard (gm, example_inputs) graph
pass signature.

CPU-offload reload chains are handled by hoisting (in-place move) the
chain in front of the earliest dup that needs it - the in-place
equivalent of upstream's "eagerly copy reload chain into the new
graph" trick. This restores offload + remat support that the local
port commit (D-equivalent of GitHub PR #3260) silently dropped: on
main today, --compile.memory_policy=budget_limited_offload crashes
with CUBLAS_STATUS_EXECUTION_FAILED because dups reference the
freed-GPU offloaded forward tensors.

Review order:

selective_activation_remat.py - the core refactor. A pre-pass
claim_for records the first backward node that transitively reaches
each must_recompute forward node (setdefault gives first-write-wins).
The insertion loop walks gm.graph.nodes in forward order, so each
dup's must_recompute deps are already in recomputed_nodes by the
time remat_input resolves them - no explicit sort. Dups are tagged
autograd_backward=True so downstream passes treat them as
backward-region nodes. remat_input also handles the
cpu_offload_reload_node case: when a dup references an offloaded
forward node, ensure_offload_chain_before walks the bwd-region
reload chain via an explicit DFS stack, then hoists chain members in
graph (topological) order by walking the all_nodes snapshot - again
no explicit sort. moved_offload bookkeeping ensures the chain ends
up before the earliest target across multiple iterations.
raise_getitems still runs at the end for the multi-output-op memory
benefit; gm.graph.lint() validates the final graph.

common_utils.py / fsdp_passes.py - drops the _is_recomputed_node
name-suffix workaround now that dups carry autograd_backward=True
directly.

passes.py - removes the selective_activation_remat_pass wrapper
since the underlying function matches the standard signature; the
pass list references it directly.

tests/test_passes.py - adds TestSelectiveActivationRematPass with
three cases: the reviewer scenario from pytorch/pytorch#168082
(pins topological insertion order), a forward+backward consumer mix
(targeted erase keeps originals with surviving forward users), and
the offload reload chain hoist - synthesizing the actual
ao.offload / ao.reload / ao.wait_tensor graph the offload pass
produces and pinning that the chain ends up before the dup with the
dup reading from the backward wait_tensor instead of F.

Authored with Claude.

Test Plan:
Unit tests:
  pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
  -> 68 passed, 1 skipped (3 new in TestSelectiveActivationRematPass)

End-to-end on 8xH100 / Llama3 8B / FSDP=4 + TP=2 / aot_fx_trace /
no cudagraph, with --debug.seed=42 --debug.deterministic:

  No offload (memory_policy=default):
    loss / grad_norm / peak memory at step 10 are bitwise identical
    to the main baseline (1.65096 / 21.9799 / 30.78 GiB).

  --compile.memory_policy=budget_limited_offload:
    refactor matches a baseline patched to invoke upstream remat
    directly (1.65096 / 21.9799 / 29.42 GiB). main HEAD (which calls
    the local port) crashes with CUBLAS_STATUS_EXECUTION_FAILED on
    this same configuration; the refactor incidentally fixes that
    regression.
tugsbayasgalan added a commit that referenced this pull request May 11, 2026
Summary:
The remat pass previously rebuilt the graph wholesale (fx.Graph() +
node_copy of every node) and relied on whole-graph DCE to remove dead
must_recompute originals. Refactor to mutate gm.graph in place: dups
are inserted in front of their first backward consumer, backward args
are redirected to the dups, and only originals whose users became
empty are erased. Original node identities and names are preserved,
the topological-order assumption is explicit (input graph order
drives insertion, validated by gm.graph.lint() at the end), and the
underlying function takes the standard (gm, example_inputs) graph
pass signature.

CPU-offload reload chains are handled by hoisting (in-place move) the
chain in front of the earliest dup that needs it - the in-place
equivalent of upstream's "eagerly copy reload chain into the new
graph" trick. This restores offload + remat support that the local
port commit (D-equivalent of GitHub PR #3260) silently dropped: on
main today, --compile.memory_policy=budget_limited_offload crashes
with CUBLAS_STATUS_EXECUTION_FAILED because dups reference the
freed-GPU offloaded forward tensors.

Review order:

selective_activation_remat.py - the core refactor. A pre-pass
claim_for records the first backward node that transitively reaches
each must_recompute forward node (setdefault gives first-write-wins).
The insertion loop walks gm.graph.nodes in forward order, so each
dup's must_recompute deps are already in recomputed_nodes by the
time remat_input resolves them - no explicit sort. Dups are tagged
autograd_backward=True so downstream passes treat them as
backward-region nodes. remat_input also handles the
cpu_offload_reload_node case: when a dup references an offloaded
forward node, ensure_offload_chain_before walks the bwd-region
reload chain via an explicit DFS stack, then hoists chain members in
graph (topological) order by walking the all_nodes snapshot - again
no explicit sort. moved_offload bookkeeping ensures the chain ends
up before the earliest target across multiple iterations.
raise_getitems still runs at the end for the multi-output-op memory
benefit; gm.graph.lint() validates the final graph.

common_utils.py / fsdp_passes.py - drops the _is_recomputed_node
name-suffix workaround now that dups carry autograd_backward=True
directly.

passes.py - removes the selective_activation_remat_pass wrapper
since the underlying function matches the standard signature; the
pass list references it directly.

tests/test_passes.py - adds TestSelectiveActivationRematPass with
three cases: the reviewer scenario from pytorch/pytorch#168082
(pins topological insertion order), a forward+backward consumer mix
(targeted erase keeps originals with surviving forward users), and
the offload reload chain hoist - synthesizing the actual
ao.offload / ao.reload / ao.wait_tensor graph the offload pass
produces and pinning that the chain ends up before the dup with the
dup reading from the backward wait_tensor instead of F.

Authored with Claude.

Test Plan:
Unit tests:
  pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
  -> 68 passed, 1 skipped (3 new in TestSelectiveActivationRematPass)

End-to-end on 8xH100 / Llama3 8B / FSDP=4 + TP=2 / aot_fx_trace /
no cudagraph, with --debug.seed=42 --debug.deterministic:

  No offload (memory_policy=default):
    loss / grad_norm / peak memory at step 10 are bitwise identical
    to the main baseline (1.65096 / 21.9799 / 30.78 GiB).

  --compile.memory_policy=budget_limited_offload:
    refactor matches a baseline patched to invoke upstream remat
    directly (1.65096 / 21.9799 / 29.42 GiB). main HEAD (which calls
    the local port) crashes with CUBLAS_STATUS_EXECUTION_FAILED on
    this same configuration; the refactor incidentally fixes that
    regression.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants