|
3 | 3 | # This source code is licensed under the BSD-style license found in the |
4 | 4 | # LICENSE file in the root directory of this source tree. |
5 | 5 |
|
| 6 | +import pytest |
6 | 7 | import torch |
7 | 8 | import torch.nn as nn |
8 | 9 | import torch.nn.functional as F |
|
21 | 22 | DWConvsModule, |
22 | 23 | ) |
23 | 24 | from executorch.backends.arm.test.tester.test_pipeline import PassPipeline |
24 | | -from executorch.backends.arm.tosa.specification import TosaLoweringContext |
| 25 | +from executorch.backends.arm.tosa.specification import ( |
| 26 | + TosaLoweringContext, |
| 27 | + TosaSpecification, |
| 28 | +) |
25 | 29 | from executorch.backends.arm.vgf import VgfCompileSpec, VgfPartitioner |
26 | 30 | from executorch.exir import EdgeCompileConfig, to_edge, to_edge_transform_and_lower |
27 | 31 | from executorch.exir.dialects._ops import ops as exir_ops |
| 32 | +from torch.export import Dim, export |
| 33 | +from torch.export.exported_program import _get_shape_env |
28 | 34 |
|
29 | 35 |
|
30 | 36 | class TinyConvReluCat(nn.Module): |
@@ -95,12 +101,45 @@ def _get_call_function_node(gm: torch.fx.GraphModule, target): |
95 | 101 | raise AssertionError(f"Node with target {target} not found") |
96 | 102 |
|
97 | 103 |
|
| 104 | +class ConvModule(torch.nn.Module): |
| 105 | + def __init__(self): |
| 106 | + super().__init__() |
| 107 | + self.conv = torch.nn.Conv2d(3, 16, kernel_size=3, stride=3, padding=0) |
| 108 | + |
| 109 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 110 | + return self.conv(x) |
| 111 | + |
| 112 | + |
| 113 | +def _make_rewrite_pass( |
| 114 | + example_inputs: tuple[torch.Tensor, ...], |
| 115 | + dynamic_shapes: dict[int, object] | None = None, |
| 116 | +) -> tuple[RewriteConvPass, object, int | torch.SymInt]: |
| 117 | + if dynamic_shapes is None: |
| 118 | + ep = export(ConvModule(), example_inputs) |
| 119 | + else: |
| 120 | + ep = export(ConvModule(), example_inputs, dynamic_shapes={"x": dynamic_shapes}) |
| 121 | + edge_model = to_edge(ep) |
| 122 | + gm = edge_model.exported_program().graph_module |
| 123 | + conv_node = next( |
| 124 | + n for n in gm.graph.nodes if n.target == exir_ops.edge.aten.convolution.default |
| 125 | + ) |
| 126 | + input_len = conv_node.args[0].meta["val"].shape[2] |
| 127 | + return RewriteConvPass(edge_model.exported_program()), _get_shape_env(gm), input_len |
| 128 | + |
| 129 | + |
| 130 | +def _multiples_of_three_dynamic_shapes() -> dict[int, object]: |
| 131 | + return { |
| 132 | + 2: Dim("height", min=2, max=6) * 3, |
| 133 | + 3: Dim("width", min=2, max=6) * 3, |
| 134 | + } |
| 135 | + |
| 136 | + |
98 | 137 | def test_rewrite_conv_tosa_FP(): |
99 | 138 | module = DWConvsModule() |
100 | 139 | pipeline = PassPipeline( |
101 | 140 | module, module.get_inputs(), passes_with_exported_program=[RewriteConvPass] |
102 | 141 | ) |
103 | | - # We can't run TOSA backend dialect operators in eager mode |
| 142 | + # We cannot run TOSA backend dialect operators in eager mode. |
104 | 143 | pipeline.pop_stage("run_method_and_compare_outputs") |
105 | 144 | pipeline.run() |
106 | 145 |
|
@@ -149,3 +188,152 @@ def test_rewrite_conv_vgf_quant_infers_quantized_bias_dtype_from_inputs() -> Non |
149 | 188 |
|
150 | 189 | assert len(bias_nodes) == 1 |
151 | 190 | assert bias_nodes[0].meta["val"].dtype == torch.int32 |
| 191 | + |
| 192 | + |
| 193 | +def test_rewrite_conv_dynamic_keeps_static_padding_when_symbolic_remainder_is_zero(): |
| 194 | + model = ConvModule() |
| 195 | + example_inputs = (torch.randn(1, 3, 9, 12),) |
| 196 | + ep = export( |
| 197 | + model, |
| 198 | + example_inputs, |
| 199 | + dynamic_shapes={"x": _multiples_of_three_dynamic_shapes()}, |
| 200 | + ) |
| 201 | + edge_model = to_edge(ep) |
| 202 | + shape_env = _get_shape_env(edge_model.exported_program().graph_module) |
| 203 | + with TosaLoweringContext( |
| 204 | + TosaSpecification.create_from_string("TOSA-1.1+FP+shape"), shape_env=shape_env |
| 205 | + ): |
| 206 | + edge_model = edge_model.transform( |
| 207 | + [RewriteConvPass(edge_model.exported_program())] |
| 208 | + ) |
| 209 | + |
| 210 | + conv_node = next( |
| 211 | + n |
| 212 | + for n in edge_model.exported_program().graph.nodes |
| 213 | + if n.target == exir_ops.backend.tosa.CONV2D.default |
| 214 | + ) |
| 215 | + padding = conv_node.args[4] |
| 216 | + assert padding == [0, 0, 0, 0] |
| 217 | + assert all(not isinstance(p, torch.SymInt) for p in padding) |
| 218 | + |
| 219 | + |
| 220 | +def test_rewrite_conv_adjust_pad_if_needed_static_raises_before_negative_padding(): |
| 221 | + rewrite_pass, _, _ = _make_rewrite_pass((torch.randn(1, 3, 9, 12),)) |
| 222 | + |
| 223 | + with pytest.raises(RuntimeError, match="SizeAdjustInputPass"): |
| 224 | + rewrite_pass._adjust_pad_if_needed(6, 2, 3, 0, 1) |
| 225 | + |
| 226 | + |
| 227 | +def test_rewrite_conv_adjust_pad_if_needed_static_positive_padding_stays_non_negative(): |
| 228 | + rewrite_pass, _, _ = _make_rewrite_pass((torch.randn(1, 3, 9, 12),)) |
| 229 | + |
| 230 | + adjusted_pad = rewrite_pass._adjust_pad_if_needed(8, 2, 3, 2, 1) |
| 231 | + |
| 232 | + assert adjusted_pad == 1 |
| 233 | + |
| 234 | + |
| 235 | +def test_rewrite_conv_adjust_pad_if_needed_static_exact_remainder_matches_pad(): |
| 236 | + rewrite_pass, _, _ = _make_rewrite_pass((torch.randn(1, 3, 9, 12),)) |
| 237 | + |
| 238 | + adjusted_pad = rewrite_pass._adjust_pad_if_needed(6, 1, 3, 1, 1) |
| 239 | + |
| 240 | + assert adjusted_pad == 0 |
| 241 | + |
| 242 | + |
| 243 | +def test_rewrite_conv_adjust_pad_if_needed_symbolic_exact_zero_keeps_zero_pad(): |
| 244 | + rewrite_pass, shape_env, input_len = _make_rewrite_pass( |
| 245 | + (torch.randn(1, 3, 9, 12),), |
| 246 | + dynamic_shapes=_multiples_of_three_dynamic_shapes(), |
| 247 | + ) |
| 248 | + |
| 249 | + with TosaLoweringContext( |
| 250 | + TosaSpecification.create_from_string("TOSA-1.1+FP+shape"), shape_env=shape_env |
| 251 | + ): |
| 252 | + adjusted_pad = rewrite_pass._adjust_pad_if_needed(input_len, 3, 3, 0, 1) |
| 253 | + |
| 254 | + assert adjusted_pad == 0 |
| 255 | + |
| 256 | + |
| 257 | +def test_rewrite_conv_adjust_pad_if_needed_symbolic_exact_zero_keeps_positive_pad(): |
| 258 | + rewrite_pass, shape_env, input_len = _make_rewrite_pass( |
| 259 | + (torch.randn(1, 3, 9, 12),), |
| 260 | + dynamic_shapes=_multiples_of_three_dynamic_shapes(), |
| 261 | + ) |
| 262 | + |
| 263 | + with TosaLoweringContext( |
| 264 | + TosaSpecification.create_from_string("TOSA-1.1+FP+shape"), shape_env=shape_env |
| 265 | + ): |
| 266 | + adjusted_pad = rewrite_pass._adjust_pad_if_needed(input_len, 2, 3, 1, 1) |
| 267 | + |
| 268 | + assert adjusted_pad == 1 |
| 269 | + |
| 270 | + |
| 271 | +def test_rewrite_conv_adjust_pad_if_needed_symbolic_positive_padding_range_raises_before_negative_padding(): |
| 272 | + rewrite_pass, shape_env, input_len = _make_rewrite_pass( |
| 273 | + (torch.randn(1, 3, 8, 8),), |
| 274 | + dynamic_shapes={ |
| 275 | + 2: Dim("height", min=6, max=10), |
| 276 | + 3: Dim("width", min=6, max=10), |
| 277 | + }, |
| 278 | + ) |
| 279 | + |
| 280 | + with TosaLoweringContext( |
| 281 | + TosaSpecification.create_from_string("TOSA-1.1+FP+shape"), shape_env=shape_env |
| 282 | + ): |
| 283 | + with pytest.raises(RuntimeError, match="SizeAdjustInputPass"): |
| 284 | + rewrite_pass._adjust_pad_if_needed(input_len, 2, 3, 1, 1) |
| 285 | + |
| 286 | + |
| 287 | +def test_rewrite_conv_symbolic_comparison_with_int_specializes_to_hint(): |
| 288 | + rewrite_pass, shape_env, input_len = _make_rewrite_pass( |
| 289 | + (torch.randn(1, 3, 8, 8),), |
| 290 | + dynamic_shapes={ |
| 291 | + 2: Dim("height", min=6, max=10), |
| 292 | + 3: Dim("width", min=6, max=10), |
| 293 | + }, |
| 294 | + ) |
| 295 | + |
| 296 | + def unsafe_adjust(input_len, input_weight, stride, pad, dilation): |
| 297 | + mod_remainder = ( |
| 298 | + input_len + 2 * pad - dilation * (input_weight - 1) - 1 |
| 299 | + ) % stride |
| 300 | + if mod_remainder == 0: |
| 301 | + return pad |
| 302 | + if mod_remainder > pad: |
| 303 | + raise RuntimeError("SizeAdjustInputPass") |
| 304 | + return pad - mod_remainder |
| 305 | + |
| 306 | + mod_remainder = (input_len - 2) % 3 |
| 307 | + value_ranges = shape_env.bound_sympy(mod_remainder.node.expr) |
| 308 | + |
| 309 | + assert value_ranges.lower == 0 |
| 310 | + assert value_ranges.upper == 2 |
| 311 | + assert len(shape_env.guards) == 0 |
| 312 | + assert unsafe_adjust(input_len, 2, 3, 0, 1) == 0 |
| 313 | + assert len(shape_env.guards) == 1 |
| 314 | + assert shape_env.guards[-1].expr in { |
| 315 | + (mod_remainder == 0).node.expr, |
| 316 | + (mod_remainder <= 0).node.expr, |
| 317 | + } |
| 318 | + |
| 319 | + with TosaLoweringContext( |
| 320 | + TosaSpecification.create_from_string("TOSA-1.1+FP+shape"), shape_env=shape_env |
| 321 | + ): |
| 322 | + with pytest.raises(RuntimeError, match="SizeAdjustInputPass"): |
| 323 | + rewrite_pass._adjust_pad_if_needed(input_len, 2, 3, 0, 1) |
| 324 | + |
| 325 | + |
| 326 | +def test_rewrite_conv_adjust_pad_if_needed_symbolic_zero_padding_range_raises_before_negative_padding(): |
| 327 | + rewrite_pass, shape_env, input_len = _make_rewrite_pass( |
| 328 | + (torch.randn(1, 3, 8, 8),), |
| 329 | + dynamic_shapes={ |
| 330 | + 2: Dim("height", min=6, max=10), |
| 331 | + 3: Dim("width", min=6, max=10), |
| 332 | + }, |
| 333 | + ) |
| 334 | + |
| 335 | + with TosaLoweringContext( |
| 336 | + TosaSpecification.create_from_string("TOSA-1.1+FP+shape"), shape_env=shape_env |
| 337 | + ): |
| 338 | + with pytest.raises(RuntimeError, match="SizeAdjustInputPass"): |
| 339 | + rewrite_pass._adjust_pad_if_needed(input_len, 2, 3, 0, 1) |
0 commit comments