@@ -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+
68103def _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