Skip to content

[graph_trainer] Save tensors written by forward in-place ops instead of recomputing them - #5622

Open
andrewliu2001 wants to merge 1 commit into
pytorch:mainfrom
andrewliu2001:fix-sar-inplace-write-remat
Open

andrewliu2001 wants to merge 1 commit into
pytorch:mainfrom
andrewliu2001:fix-sar-inplace-write-remat

Conversation

@andrewliu2001

Copy link
Copy Markdown
Contributor

Fixes #4688

SAR recomputes a node by re-running whatever produced it, but doesn't replay in-place writes made into it later. Pass 1b in tag_sac_policy saves mutation nodes and their users, which works when make_fx rebinds the tensor to the mutation output. Two cases where that doesn't happen:

  1. A mutable op that returns (), e.g. clone -> custom in-place kernel -> return clone, as in the issue
  2. A write through a view of a recomputable tensor, e.g. y[:k] = v or y.view(-1)[:k].add_(1)

In both, reads after the write still point at the pre-write node. That node stays recomputable, and backward gets a copy without the write.

This adds _force_save_forward_mutation_bases, run right after Pass 1b and after min_cut tagging. For each forward mutation, it marks the base of the written tensor MUST_SAVE if the base is recomputable and read outside the mutation. The read check keeps things like zeros_like(...).scatter_(...) in MoE routing recomputable.

Test plan:

  • issue repro: grad 4 -> 6
  • custom op, view write and slice assignment, traced with minimal_fx_tracer under default / full / min_cut: wrong before, bitwise equal to eager after
  • 3-layer toy model on GPU with memory_policy="default": max grad error ~963 before, 0 after
  • no tag changes on the llama3 and qwen3 MoE debug models; test_bitwise_deterministic passes (DSv3 skipped locally, needs FA3)
  • two new tests in TestApplySACPass, which fail on main and pass here
  • test_passes, test_cpu_offload, test_remat_metadata, test_sac_peak_memory, test_numerics, test_paged_stash: 265 passed, 6 skipped (existing skips)

sac_and_offload still gives wrong grads for the view case, but that also happens on main without this change, so I'll file it separately.

…of recomputing them

Fixes pytorch#4688

SAR recomputes a node by re-running what produced it, but doesn't replay
in-place writes made into it later. Pass 1b saves mutation nodes and their
users, which covers the usual case where make_fx rebinds the tensor to the
mutation output. It misses mutable ops that return () and writes through a
view (y[:k] = v), where later reads still point at the pre-write node, so
backward gets a recomputed copy without the write.

Add a step after Pass 1b (and after min_cut tagging) that marks the base of
each written tensor MUST_SAVE when it's recomputable and read outside the
mutation. The read check keeps zeros_like(...).scatter_(...) in MoE routing
recomputable, so tags on the llama3 / qwen3 MoE debug models don't change.
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 25, 2026
@meta-cla

meta-cla Bot commented Oct 25, 2026

Copy link
Copy Markdown

Hi @andrewliu2001!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@pytorch-bot

pytorch-bot Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@meta-cla

meta-cla Bot commented Oct 25, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests 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.

[graph_trainer] SAR omits in-place writes to recomputed buffers, producing incorrect gradients

1 participant