Skip to content

[aot_autograd] specialize backwards for undefined output tangents - #194523

Closed
bobrenjc93 wants to merge 9 commits into
gh/bobrenjc93/1051/basefrom
gh/bobrenjc93/1051/head
Closed

bobrenjc93 wants to merge 9 commits into
gh/bobrenjc93/1051/basefrom
gh/bobrenjc93/1051/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Human note:

This is the AOTAutograd prerequisite for precompiled training artifacts. I reviewed the change and kept it separate because it fixes undefined output tangents for ordinary AOTAutograd and compiled autograd as well as precompile.

AI-assisted review summary:

Autograd supplies None for differentiable outputs that do not contribute to a loss, but AOTAutograd previously materialized those tangents as zero tensors before entering the compiled backward. That ran dead backward branches, changed set_materialize_grads(False) semantics, and could pass None to a lowering that expected every input to be a Tensor.

Make tangent presence an explicit integer specialization key. Preserve storage-free output prototypes for fallback materialization, retrace supported backwards with literal None tangents, prune the specialized calling convention, and retain the structural fallback for tensor subclasses and already-specialized Dynamo autograd.Function graphs. Carry the same specialization through compiled autograd and retain a config switch for the previous materialization behavior.

A backend can also return the same Tensor object for a differentiable output and a slot that AOTAutograd marks non-differentiable. Because mark_non_differentiable is keyed by TensorImpl, detach only marked slots that collide with unmarked returned slots before marking them, and emit the same operation in standalone generated code.

Test Plan:

PYTHONPATH=$PWD/test/functorch PRECOMPILE_OVERLAY_ROOT=$PWD TORCH_USE_RTLD_GLOBAL=1 LD_LIBRARY_PATH=/home/bobren/local/e/pytorch-env/lib:/home/bobren/local/e/pytorch-env/lib/python3.10/site-packages/nvidia/cudnn/lib:/usr/local/cuda-12.8/lib64 /home/bobren/local/e/pytorch-env/bin/python /home/bobren/local/e/pytorch/agent_space/run_precompile_root_overlay.py test_file test/functorch/test_aotdispatch.py TestAOTAutograd.test_single_none_tangent_materialization TestAOTAutograd.test_dynamic_single_grad_output_prototype_cache
PYTHONPATH=$PWD/test/functorch PRECOMPILE_OVERLAY_ROOT=$PWD TORCH_USE_RTLD_GLOBAL=1 LD_LIBRARY_PATH=/home/bobren/local/e/pytorch-env/lib:/home/bobren/local/e/pytorch-env/lib/python3.10/site-packages/nvidia/cudnn/lib:/usr/local/cuda-12.8/lib64 /home/bobren/local/e/pytorch-env/bin/python /home/bobren/local/e/pytorch/agent_space/run_precompile_root_overlay.py test_file test/functorch/test_aotdispatch.py -k detach_output_aliasing
PRECOMPILE_OVERLAY_ROOT=$PWD TORCH_USE_RTLD_GLOBAL=1 LD_LIBRARY_PATH=/home/bobren/local/e/pytorch-env/lib:/home/bobren/local/e/pytorch-env/lib/python3.10/site-packages/nvidia/cudnn/lib:/usr/local/cuda-12.8/lib64 /home/bobren/local/e/pytorch-env/bin/python /home/bobren/local/e/pytorch/agent_space/run_precompile_root_overlay.py test_file test/functorch/test_codegen_mutation_epilogue.py
PRECOMPILE_OVERLAY_ROOT=$PWD TORCH_USE_RTLD_GLOBAL=1 LD_LIBRARY_PATH=/home/bobren/local/e/pytorch-env/lib:/home/bobren/local/e/pytorch-env/lib/python3.10/site-packages/nvidia/cudnn/lib:/usr/local/cuda-12.8/lib64 /home/bobren/local/e/pytorch-env/bin/python /home/bobren/local/e/pytorch/agent_space/run_precompile_root_overlay.py test_file test/functorch/test_compile_to_python.py
UV_OFFLINE=1 PATH=/home/bobren/.cache/uv/archive-v0/_EbPanZVazvI1jXYMOQC9/bin:/home/bobren/local/e/pytorch-env/bin:/home/bobren/.local/bin:/usr/local/bin:/usr/bin:/bin /home/bobren/.cache/uv/archive-v0/_EbPanZVazvI1jXYMOQC9/bin/lintrunner -a

This change was authored with assistance from an AI coding tool.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 23, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/194523

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot

pytorch-bot Bot commented Aug 23, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Superseded by the newer opt-in iteration of the same change, #195689. Closing in its favour.

@bobrenjc93 bobrenjc93 closed this Sep 3, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/1051/head branch November 10, 2026 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant