Skip to content

Conversation

@andrewor14
Copy link
Contributor

@andrewor14 andrewor14 commented Nov 13, 2025

Summary: In pt2e QAT, we first annotate the nodes to be quantized and then perform the pattern replacement (i.e. QAT fusion) in the prepare step. After this pattern replacement, old nodes are replaced with new nodes, and any references to the old nodes must also be updated to refer to the new nodes.

This commit fixes a bug where, for special qspecs like the SharedQuantizationSpec and DerivedQuantizationSpec, we only update the values of a node's input_qspec_map, not the keys. As a result, the keys still refer to the old nodes that do not exist anymore after the QAT fusion.

Test Plan:

python test/quantization/pt2e/test_quantize_pt2e_qat.py -k test_qat_shared_qspec

@pytorch-bot
Copy link

pytorch-bot bot commented Nov 13, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit c19b8cd with merge base e2aab90 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 13, 2025
@meta-codesync
Copy link

meta-codesync bot commented Nov 13, 2025

@andrewor14 has imported this pull request. If you are a Meta employee, you can view this in D86983409.

@andrewor14 andrewor14 added the topic: improvement Use this tag if this PR is an improvement (doesn't fit into any of the other categories) label Nov 13, 2025
Copy link
Contributor

@jerryzh168 jerryzh168 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah makes sense, can you add a test

@andrewor14
Copy link
Contributor Author

ah makes sense, can you add a test

Yep, this is in progress, just wanted to push first for Xing to try

@andrewor14 andrewor14 force-pushed the pt2e-special-qspec-fix branch from 646cd49 to d76110c Compare November 13, 2025 22:56
for n in nodes_to_check:
if n.target == torch.ops.aten.batch_norm.default:
num_batch_norm_nodes_checked += 1
self.assertTrue(n not in old_nodes, "found old node in qspec")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to also check they are all in new_nodes?

Copy link
Contributor

@jerryzh168 jerryzh168 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lg, thanks for the fix, had one inline comment

**Summary:** In pt2e QAT, we first annotate the nodes to be
quantized and then perform the pattern replacement (i.e. QAT
fusion) in the prepare step. After this pattern replacement,
old nodes are replaced with new nodes, and any references to
the old nodes must also be updated to refer to the new nodes.

This commit fixes a bug where, for special qspecs like the
`SharedQuantizationSpec` and `DerivedQuantizationSpec`, we only
update the values of a node's `input_qspec_map`, not the keys.
As a result, the keys still refer to the old nodes that do
not exist anymore after the QAT fusion.

**Test Plan:**
```
python test/quantization/pt2e/test_quantize_pt2e_qat.py -k test_qat_shared_qspec
```
@andrewor14 andrewor14 force-pushed the pt2e-special-qspec-fix branch from d76110c to c19b8cd Compare November 14, 2025 15:04
@andrewor14 andrewor14 merged commit 8496b55 into main Nov 14, 2025
21 checks passed
namgyu-youn pushed a commit to namgyu-youn/ao that referenced this pull request Nov 21, 2025
**Summary:** In pt2e QAT, we first annotate the nodes to be
quantized and then perform the pattern replacement (i.e. QAT
fusion) in the prepare step. After this pattern replacement,
old nodes are replaced with new nodes, and any references to
the old nodes must also be updated to refer to the new nodes.

This commit fixes a bug where, for special qspecs like the
`SharedQuantizationSpec` and `DerivedQuantizationSpec`, we only
update the values of a node's `input_qspec_map`, not the keys.
As a result, the keys still refer to the old nodes that do
not exist anymore after the QAT fusion.

**Test Plan:**
```
python test/quantization/pt2e/test_quantize_pt2e_qat.py -k test_qat_shared_qspec
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. topic: improvement Use this tag if this PR is an improvement (doesn't fit into any of the other categories)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants