|
45 | 45 | def test_add_simulated_linear_bn_fusing(input_shape, linear_bias): |
46 | 46 | calibration_inputs = get_random_calibration_inputs(to_model_input_spec(input_shape)) |
47 | 47 | input_sample = calibration_inputs[0] |
48 | | - model = models.LinearBNModule( |
49 | | - in_features=input_shape[-1], |
50 | | - out_features=5, |
51 | | - linear_bias=linear_bias, |
| 48 | + model = models.LinearBatchNormModule( |
| 49 | + bias=linear_bias, |
| 50 | + input_rank=len(input_shape), |
| 51 | + fc_in_features=input_shape[-1], |
| 52 | + fc_out_features=5, |
| 53 | + bn_in_features=5, |
52 | 54 | ) |
53 | 55 | model.train() |
54 | 56 | raw_output = model(input_sample[0]) |
@@ -110,10 +112,12 @@ def test_full_linear_bn_fusing(input_shape, linear_bias): |
110 | 112 |
|
111 | 113 | calibration_inputs = get_random_calibration_inputs(to_model_input_spec(input_shape)) |
112 | 114 | input_sample = calibration_inputs[0] |
113 | | - model = models.LinearBNModule( |
114 | | - in_features=input_shape[-1], |
115 | | - out_features=5, |
116 | | - linear_bias=linear_bias, |
| 115 | + model = models.LinearBatchNormModule( |
| 116 | + bias=linear_bias, |
| 117 | + input_rank=len(input_shape), |
| 118 | + fc_in_features=input_shape[-1], |
| 119 | + fc_out_features=5, |
| 120 | + bn_in_features=5, |
117 | 121 | ) |
118 | 122 | model.train() |
119 | 123 | raw_output = model(input_sample[0]) |
@@ -175,11 +179,13 @@ def test_input_output_graph_equivalence(input_shape, linear_bias, bn_eps): |
175 | 179 |
|
176 | 180 | calibration_inputs = get_random_calibration_inputs(to_model_input_spec(input_shape)) |
177 | 181 | input_sample = calibration_inputs[0] |
178 | | - model = models.LinearBNModule( |
179 | | - in_features=input_shape[-1], |
180 | | - out_features=5, |
181 | | - linear_bias=linear_bias, |
182 | | - bn_eps=bn_eps, |
| 182 | + model = models.LinearBatchNormModule( |
| 183 | + bias=linear_bias, |
| 184 | + input_rank=len(input_shape), |
| 185 | + fc_in_features=input_shape[-1], |
| 186 | + fc_out_features=5, |
| 187 | + bn_in_features=5, |
| 188 | + eps=bn_eps, |
183 | 189 | ) |
184 | 190 | model.eval() |
185 | 191 |
|
@@ -216,11 +222,13 @@ def test_linear_bn_full_qat_pipeline_conversion( |
216 | 222 | "The graph currently produces Linear layer without quantized bias which is incorrect." |
217 | 223 | ) |
218 | 224 |
|
219 | | - model = models.LinearBNModule( |
220 | | - in_features=input_shape[-1], |
221 | | - out_features=5, |
222 | | - linear_bias=linear_bias, |
223 | | - bn_eps=bn_eps, |
| 225 | + model = models.LinearBatchNormModule( |
| 226 | + bias=linear_bias, |
| 227 | + input_rank=len(input_shape), |
| 228 | + fc_in_features=input_shape[-1], |
| 229 | + fc_out_features=5, |
| 230 | + bn_in_features=5, |
| 231 | + eps=bn_eps, |
224 | 232 | ) |
225 | 233 | model.eval() |
226 | 234 |
|
@@ -258,3 +266,51 @@ def test_linear_bn_full_qat_pipeline_conversion( |
258 | 266 | input_data=input_data, |
259 | 267 | atol=0.0, |
260 | 268 | ) |
| 269 | + |
| 270 | + |
| 271 | +@pytest.mark.parametrize("input_shape", [(2, 3, 5), (2, 3, 5, 5), (2, 3, 3, 5, 5)]) |
| 272 | +@pytest.mark.parametrize("linear_bias", [True, False]) |
| 273 | +@pytest.mark.parametrize("bn_eps", [1e-5, 1e-6]) |
| 274 | +def test_incompatible_linear_bn_not_fused(mocker, input_shape, linear_bias, bn_eps): |
| 275 | + """ |
| 276 | + Test cases ensuring Linear+BN are not fused when not compatible for fusion. |
| 277 | + Linear+BN are only fusable when using BatchNorm1d and input has 2 dims (N, L). |
| 278 | + """ |
| 279 | + |
| 280 | + # TODO: Add pass for quantizing bias node when Linear has bias=False |
| 281 | + if not linear_bias: |
| 282 | + pytest.skip( |
| 283 | + "Linear with bias=False is not yet supported. " |
| 284 | + "The graph currently produces Linear layer without quantized bias which is incorrect." |
| 285 | + ) |
| 286 | + |
| 287 | + model = models.LinearBatchNormModule( |
| 288 | + bias=linear_bias, |
| 289 | + input_rank=len(input_shape), |
| 290 | + fc_in_features=input_shape[-1], |
| 291 | + fc_out_features=5, |
| 292 | + bn_in_features=input_shape[1], |
| 293 | + eps=bn_eps, |
| 294 | + ) |
| 295 | + model.eval() |
| 296 | + model(torch.randn(input_shape)) |
| 297 | + |
| 298 | + # Run conversion |
| 299 | + edge_program = to_quantized_edge_program( |
| 300 | + model, input_shape, use_qat=True, use_neutron_for_format_conversion=False |
| 301 | + ).exported_program() |
| 302 | + |
| 303 | + assert graph_contains_any_of_ops( |
| 304 | + graph=edge_program.graph, |
| 305 | + ops=[ |
| 306 | + exir_ops.edge.aten.addmm.default, |
| 307 | + exir_ops.edge.aten.linear.default, |
| 308 | + ], |
| 309 | + ) |
| 310 | + assert graph_contains_any_of_ops( |
| 311 | + graph=edge_program.graph, |
| 312 | + ops=batch_norm_target_ops, |
| 313 | + ) |
| 314 | + assert not graph_contains_any_of_ops( |
| 315 | + edge_program.graph, [torch.ops.higher_order.executorch_call_delegate] |
| 316 | + ) |
0 commit comments