Skip to content

Commit 517e56a

Browse files
authored
Only claim conv overloads the QAT fold can actually match
Differential Revision: D117778197 Pull Request resolved: pytorch#22241
1 parent a5f15b5 commit 517e56a

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

‎backends/qualcomm/quantizer/conv_bn.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,25 @@
4747
from .qconfig import QuantizationConfig
4848
from .rules import _is_annotated, _mark_nodes_as_annotated, Q_ANNOTATION_KEY
4949

50+
# Only the overloads torchao's `_fuse_conv_bn_qat` / `_fold_conv_bn_qat` can
51+
# actually match. Both trace their patterns from `F.conv1d`, `F.conv2d`,
52+
# `F.conv_transpose1d` and `F.conv_transpose2d`, which export to exactly these
53+
# four targets.
54+
#
55+
# Claiming a conv the fold cannot match is worse than not claiming it: this pass
56+
# drops the conv's output qspec on the assumption the fold will move the bn
57+
# output observer onto it, so if the fold misses, the conv is left emitting
58+
# float32 into a quantized graph and QNN rejects it (`0x232 != 0x408`, FLOAT_32
59+
# meeting UFIXED_POINT_8). `aten.conv2d.padding` -- produced by
60+
# `nn.Conv2d(..., padding="same")` -- hit exactly that. `aten.convolution.default`
61+
# only appears after decomposition, well after this pass runs, so it never
62+
# matched anything here either.
63+
#
64+
# Note `_is_conv_or_conv_transpose_node` is NOT a usable guard: it returns True
65+
# for `conv2d.padding`.
5066
CONV_TARGETS = (
5167
torch.ops.aten.conv1d.default,
5268
torch.ops.aten.conv2d.default,
53-
torch.ops.aten.conv2d.padding,
54-
torch.ops.aten.convolution.default,
5569
torch.ops.aten.conv_transpose1d.default,
5670
torch.ops.aten.conv_transpose2d.input,
5771
)

‎backends/qualcomm/tests/test_conv_bn_qat.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,41 @@ def forward(self, x):
6565
return torch.relu(self.bn2(self.pw(x)))
6666

6767

68+
class ConvBnSamePadding(torch.nn.Module):
69+
"""`padding="same"` exports to `aten.conv2d.padding`, which torchao cannot fold."""
70+
71+
def __init__(self):
72+
super().__init__()
73+
self.conv = torch.nn.Conv2d(3, 8, 3, padding="same", bias=False)
74+
self.bn = torch.nn.BatchNorm2d(8)
75+
self.relu = torch.nn.ReLU()
76+
77+
def forward(self, x):
78+
return self.relu(self.bn(self.conv(x)))
79+
80+
81+
class Conv1dBn(torch.nn.Module):
82+
def __init__(self):
83+
super().__init__()
84+
self.conv = torch.nn.Conv1d(3, 8, 3, padding=1, bias=False)
85+
self.bn = torch.nn.BatchNorm1d(8)
86+
self.relu = torch.nn.ReLU()
87+
88+
def forward(self, x):
89+
return self.relu(self.bn(self.conv(x)))
90+
91+
92+
class ConvTranspose2dBn(torch.nn.Module):
93+
def __init__(self):
94+
super().__init__()
95+
self.conv = torch.nn.ConvTranspose2d(3, 8, 3, padding=1, bias=False)
96+
self.bn = torch.nn.BatchNorm2d(8)
97+
self.relu = torch.nn.ReLU()
98+
99+
def forward(self, x):
100+
return self.relu(self.bn(self.conv(x)))
101+
102+
68103
def _qat_convert(module: torch.nn.Module, example_inputs):
69104
quantizer = QnnQuantizer()
70105
quantizer.set_default_quant_config(
@@ -145,6 +180,37 @@ def test_depthwise_separable_conv_bn_folds(self):
145180
self._assert_conv_biases_quantized(converted)
146181
self._assert_conv_outputs_quantized(converted)
147182

183+
def test_conv1d_bn_folds(self):
184+
example_inputs = (torch.randn(1, 3, 16),)
185+
converted = _qat_convert(Conv1dBn().eval(), example_inputs)
186+
self._assert_no_batch_norm(converted)
187+
self._assert_conv_outputs_quantized(converted)
188+
189+
def test_conv_transpose2d_bn_folds(self):
190+
example_inputs = (torch.randn(1, 3, 16, 16),)
191+
converted = _qat_convert(ConvTranspose2dBn().eval(), example_inputs)
192+
self._assert_no_batch_norm(converted)
193+
self._assert_conv_outputs_quantized(converted)
194+
195+
def test_unfoldable_conv_overload_keeps_quantized_output(self):
196+
"""A conv the fold cannot match must keep its own output qspec.
197+
198+
`padding="same"` exports to `aten.conv2d.padding`. torchao's
199+
`_fuse_conv_bn_qat` / `_fold_conv_bn_qat` trace their patterns from
200+
`F.conv1d`/`F.conv2d`/`F.conv_transpose{1,2}d`, so only those four
201+
overloads can ever match and BatchNorm survives here.
202+
203+
That is a torchao limitation this pass cannot fix, but it must not make it
204+
worse: if the partition pass claims the conv it drops the conv's output
205+
qspec expecting the fold to move the bn output observer onto it, and the
206+
conv is left emitting float32 into a quantized graph -- which QNN rejects
207+
with `0x232 != 0x408`. So the conv must stay quantized even when the fold
208+
misses.
209+
"""
210+
example_inputs = (torch.randn(1, 3, 16, 16),)
211+
converted = _qat_convert(ConvBnSamePadding().eval(), example_inputs)
212+
self._assert_conv_outputs_quantized(converted)
213+
148214
def test_conv_without_bn_is_still_annotated(self):
149215
"""The partition pass must not starve the per-node conv annotator."""
150216
example_inputs = (torch.randn(1, 3, 16, 16),)

0 commit comments

Comments
 (0)