Repository navigation
[graph_trainer] Port remat_using_tags_for_fwd_loss_bwd_graph pass locally - #3260
Merged
Merged
Conversation
…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.
SherlockNoMad
requested review from
aditvenk,
sanketpurandare,
tianyu-l,
xmfan and
yiming0416
as code owners
May 7, 2026 16:13
IvanKobzarev
approved these changes
May 7, 2026
tugsbayasgalan
approved these changes
May 7, 2026
sanketpurandare
approved these changes
May 7, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All credit goes to original author: @tugsbayasgalan
Why
Summary
remat_using_tags_for_fwd_loss_bwd_graphpass fromtorch._functorch._activation_checkpointingintograph_trainer/selective_activation_remat.pyso we can iterate on it independently without waiting on PyTorch releases.passes.pyto use the local version.Test plan
pytest tests/for graph_trainer)--debug.seed=42 --debug.deterministic