Repository navigation
[PT2E][X86] Fall back to plain linear annotation for reused nn.Linear with fusable post op - #4480
Conversation
… with fusable post op Fixes pytorch#4478 Under non-strict export, get_source_partitions groups all call sites of a reused nn.Linear module into a single SourcePartition with one output node per call site. The linear fusion annotation passes assumed a single output node and raised ValueError, aborting prepare_pt2e for the whole model whenever any call site was followed by a fusable add/relu. - _annotate_linear_unary / _annotate_linear_binary_unary: skip fused partition candidates where any partition has more than one output node instead of raising. - _annotate_linear: annotate every linear call site (output node) of a partition instead of raising on multi-output partitions. Reused linear call sites are now quantized without fusion; fusion behavior for single-call-site modules is unchanged. Co-Authored-By: Claude Fable 5 <[email protected]>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/4480
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ✅ You can merge normally! (2 Unrelated Failures)As of commit 0b35d4d with merge base c42673d ( FLAKY - The following job failed but was likely due to flakiness present on trunk:
BROKEN TRUNK - The following job failed but was present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @waynehacking8! 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! |
|
@claude review this. And also check: is |
|
Claude finished @Xia-Weiwen's task in 4m 27s —— View job Code Review
SummaryThe fix is correct and well-scoped. The root-cause analysis is accurate: under non-strict export, Correctness
No issues with the core logic. The behavior change (reused linear + fusable neighbor → quantized without fusion) is reasonable and clearly documented. Suggestions (non-blocking)
Answer: Is
|
|
@waynehacking8 Thanks for the fix. Can you please take a look at the comments from claude? Seems the test case needs improvement. |
The relu variant of SharedLinearModule used functional torch.relu, which never forms a [Linear, nn.ReLU] sequential partition, so it only covered the plain _annotate_linear fallback and left the new multi-output guard in _annotate_linear_unary untested. Switch to a reused nn.ReLU module so a [Linear, ReLU] partition forms and the guard is exercised. Verified coverage: with the guard temporarily removed, the relu case fails with "Input partition has more than one output node"; with it in place the test passes. node_occurrence / annotation expectations are unchanged (relu stays unannotated as the reused linear falls back to plain annotation). Signed-off-by: Wayne Chiu <[email protected]>
|
Thanks @Xia-Weiwen, and thanks @claude for the careful review — both points are addressed in 0b35d4d. Test coverage fix (claude's point #1). You're right that the I verified the coverage is real, not just that the test still passes: with the
Conv paths (claude's point #2). Agreed the conv annotation paths share the same single-output assumption ( |
|
Opened the conv follow-up as #4482 (stacked on this branch, since its test needs the |
|
@claude review again |
|
Claude finished @Xia-Weiwen's task in 56s —— View job Re-review
SummaryThe follow-up commit ( What changed since the last reviewPoint #1 — Point #3 — docstring clarified. The Point #2 — conv paths. Tracked as a follow-up in #4482, stacked on this branch. Agreed this PR stays scoped to linear. Correctness re-check
The
|
|
CI note: the failing |
Follow-up to the reused nn.Linear fix (pytorch#4480): the conv annotation paths share the same single-call-site assumption, so a reused nn.Conv2d (e.g. called inside an unrolled loop) under non-strict export still crashes prepare_pt2e with "conv partition has more than one output node" (plain path) or "Input partition has more than one output node" (fusion paths). Mirror the linear fix: - _annotate_conv2d_binary_unary / _annotate_conv2d_binary / _annotate_conv2d_unary: skip fusion when any partition has multiple output nodes, so the reused conv falls back to plain annotation. - _annotate_conv2d: iterate partition.output_nodes and annotate each call site instead of raising on multi-output partitions. Adds SharedConvModule and test_conv2d_reused_with_fusable_post_op covering the unary (relu), binary (add) and binary+unary (add+relu) fusion guards plus the plain fallback. Verified the coverage is real: reverting the fix makes the test crash with "more than one output node"; with the fix each call site is annotated as plain conv and prepare_pt2e succeeds. Signed-off-by: Wayne Chiu <[email protected]>
Fixes #4478
Problem
prepare_pt2ewithX86InductorQuantizerraises aValueErrorwhenever a singlenn.Linearmodule has multiple call sites in the exported graph (e.g. it is called inside an unrolled loop — common for unrolled sampling loops of diffusion / flow-matching models) and any call site is followed by a fusableadd/relu:Root cause: under non-strict export (the current
torch.export.exportdefault),get_source_partitionsgroups all call sites of one module instance into a singleSourcePartition, so the partition has one output node per call site. The linear fusion annotation passes (_annotate_linear_unary,_annotate_linear_binary_unaryvia_get_output_nodes_of_partitions) assume exactly one output node and raise, aborting quantization of the whole model. (Understrict=Trueeach call site gets its own partition, which is why existing tests never hit this.)Fix
_annotate_linear_unary/_annotate_linear_binary_unary: skip fused partition candidates where any partition has more than one output node (samecontinuestyle as the other pattern checks in this file) instead of raising._annotate_linear: annotate each linear call site (output node) of a partition instead of raising on multi-output partitions, so the skipped call sites still get plain (non-fused) linear annotation.Behavior change: a reused linear with a fusable neighbor is now quantized without fusion (input q/dq per call site, per-channel weight dq per call site) instead of crashing
prepare_pt2e. Fusion behavior for single-call-site modules is unchanged.Note: the conv fusion paths share the same single-call-site assumption and would crash the same way for a reused conv; this PR is scoped to the linear paths reported in the issue, and I'm happy to extend to conv in a follow-up if desired.
Before / after
Minimal repro from the issue (
x = x + self.projection(x)twice with one sharednn.Linear):ValueError: Input partition has more than one output node(and the relu variant fails withLinear partition cannot have more than one output node)prepare_pt2e/convert_pt2esucceed; converted graph contains 2 quantized linear call sites (2 input q/dq pairs, 2 per-channel weight dq)Test
TestHelperModules.SharedLinearModuleandtest_linear_reused_with_fusable_post_opintest/quantization/pt2e/test_x86inductor_quantizer.py, covering bothaddandrelupost ops. The test checks node occurrence and annotation statistics (both call sites annotated as plain quantized linears, post ops unannotated)._test_quantizergained astrictparameter (defaultTrue, all existing callers unchanged); the new test usesstrict=Falsesince only non-strict export produces multi-output partitions.ValueErrors from the issue without the source change and passes with it.test/quantization/pt2e/test_x86inductor_quantizer.py: 51 passed (CPU, torch 2.12).ruff check/ruff format(0.11.6, per pre-commit) clean.This PR was written with the assistance of Claude (AI); I have reviewed and validated all changes.
Generated with Claude Code