Repository navigation
[graph_trainer] Save tensors written by forward in-place ops instead of recomputing them - #5622
andrewliu2001 wants to merge 1 commit into
Conversation
…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.
|
Hi @andrewliu2001! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
|
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
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_policysaves mutation nodes and their users, which works when make_fx rebinds the tensor to the mutation output. Two cases where that doesn't happen:(), e.g. clone -> custom in-place kernel -> return clone, as in the issuey[:k] = vory.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 likezeros_like(...).scatter_(...)in MoE routing recomputable.Test plan:
minimal_fx_tracerunder default / full / min_cut: wrong before, bitwise equal to eager aftermemory_policy="default": max grad error ~963 before, 0 aftertest_bitwise_deterministicpasses (DSv3 skipped locally, needs FA3)TestApplySACPass, which fail on main and pass heretest_passes,test_cpu_offload,test_remat_metadata,test_sac_peak_memory,test_numerics,test_paged_stash: 265 passed, 6 skipped (existing skips)sac_and_offloadstill gives wrong grads for the view case, but that also happens on main without this change, so I'll file it separately.