Fix constant_fold deleting attrs still referenced by live get_attr nodes - #4789
Open
amanyagami wants to merge 1 commit into
Open
Fix constant_fold deleting attrs still referenced by live get_attr nodes#4789amanyagami wants to merge 1 commit into
amanyagami wants to merge 1 commit into
Conversation
constant_fold's cleanup pass deleted the underlying module attribute for
a get_attr node as soon as it found one dead get_attr node for that
target, even if another get_attr node with the same target was still
live. That left the graph with a live get_attr pointing at an attribute
that no longer existed, which graph.lint() (called from
eliminate_dead_code) rejects with:
RuntimeError: Node <n> target <t> references nonexistent attribute
<t> of ...
Fix: compute the set of targets that still have at least one live
get_attr node before deleting anything, and only delattr a target that
isn't in that set - i.e. only when every get_attr node for that target
is dead.
Added test/quantization/pt2e/test_constant_fold.py using the exact repro
from the issue (two get_attr nodes sharing one buffer target, one folded
away, one live). Verified it fails with the issue's exact RuntimeError
on the unpatched code and passes with the fix, and that the pre-existing
test/quantization/pt2e/test_quantize_pt2e.py suite (70 tests) still
passes.
Fixes pytorch#4420
amanyagami
requested review from
andrewor14,
jerryzh168 and
vkuzo
as code owners
August 19, 2026 19:51
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/4789
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. |
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.
Summary
torchao.quantization.pt2e.constant_fold.constant_fold's cleanup pass deleted the underlying module attribute for aget_attrnode as soon as it found one deadget_attrnode for that target — even if anotherget_attrnode with the same target was still live elsewhere in the graph. That leaves the graph with a liveget_attrnode pointing at an attribute that no longer exists, whichgraph.lint()(called fromeliminate_dead_code) rejects:This was hit in ExecuTorch on a model where the same buffer/parameter is referenced through multiple
get_attrnodes.Fix
Compute the set of targets that still have at least one live
get_attrnode before deleting anything, and onlydelattra target when it isn't in that set — i.e. only once everyget_attrnode for that target is dead.Test
Added
test/quantization/pt2e/test_constant_fold.pyusing the exact repro from the issue: twoget_attrnodes sharing one buffer target, one folded away (dead), one live.RuntimeErroron the unpatched code.test/quantization/pt2e/test_quantize_pt2e.pysuite (70 tests, which exercisesconstant_foldindirectly via lowering/quantize_pt2e) still passes.Fixes #4420
🤖 Generated with Claude Code