|
4 | 4 | # LICENSE file in the root directory of this source tree. |
5 | 5 |
|
6 | 6 |
|
| 7 | +import itertools |
| 8 | +import unittest |
| 9 | + |
| 10 | +import kgb |
7 | 11 | import numpy as np |
8 | | -import pytest |
9 | 12 | import torch |
10 | 13 |
|
11 | 14 | from executorch.backends.nxp.backend.edge_program_converter import ( |
12 | 15 | EdgeProgramToIRConverter, |
13 | 16 | ) |
14 | | -from executorch.backends.nxp.tests.executorch_pipeline import to_quantized_edge_program |
| 17 | +from executorch.backends.nxp.tests.executorch_pipeline import ( |
| 18 | + to_edge_program, |
| 19 | + to_quantized_edge_program, |
| 20 | +) |
15 | 21 | from executorch.backends.nxp.tests.executors import ( |
16 | 22 | convert_run_compare, |
| 23 | + graph_contains_any, |
17 | 24 | graph_contains_any_of_ops, |
18 | 25 | ToNCHWPreprocess, |
19 | 26 | ToNHWCPreprocess, |
20 | 27 | ) |
21 | 28 | from executorch.exir.dialects._ops import ops as exir_ops |
| 29 | +from parameterized import parameterized |
22 | 30 | from torch import nn |
23 | 31 | from torch.export import ExportedProgram |
24 | 32 |
|
25 | 33 |
|
26 | | -@pytest.fixture(autouse=True) |
27 | | -def reseed_model_per_test_run(): |
28 | | - torch.manual_seed(23) |
29 | | - np.random.seed(23) |
30 | | - |
31 | | - |
32 | 34 | class SingleConvBlockWithDropout(torch.nn.Module): |
33 | 35 | def __init__( |
34 | 36 | self, conv_in_channels: int = 3, perform_inplace_dropout: bool = False |
@@ -74,57 +76,117 @@ def forward(self, x): |
74 | 76 | return self.block(x) |
75 | 77 |
|
76 | 78 |
|
77 | | -@pytest.mark.parametrize("inplace_dropout", [False, True]) |
78 | | -@pytest.mark.parametrize("input_shape", [(1, 3, 128, 128), (1, 3, 256, 256)]) |
79 | | -def test_conv_dropout_quant(mocker, inplace_dropout: bool, input_shape: tuple[int]): |
80 | | - model = SingleConvBlockWithDropout( |
81 | | - conv_in_channels=input_shape[1], perform_inplace_dropout=inplace_dropout |
82 | | - ).eval() |
| 79 | +class TestCloneConverter(unittest.TestCase): |
| 80 | + __test__ = False # Prevent interfering with PyTest tests |
83 | 81 |
|
84 | | - converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 82 | + @staticmethod |
| 83 | + def _node_is_clone(node) -> bool: |
| 84 | + clone_ops = [ |
| 85 | + exir_ops.edge.aten.clone.default, |
| 86 | + exir_ops.edge.dim_order_ops._clone_dim_order.default, |
| 87 | + ] |
85 | 88 |
|
86 | | - quantized_program = to_quantized_edge_program(model, input_shape).exported_program() |
| 89 | + def target_can_be_clone(node): |
| 90 | + if hasattr(node, "op") and node.op == "call_function": |
| 91 | + return "clone" in node.target.__name__ |
87 | 92 |
|
88 | | - tflite_flatbuffers_model, io_formats = converter_spy.spy_return |
89 | | - exported_program: ExportedProgram = converter_spy.call_args.args[1] |
| 93 | + return False |
90 | 94 |
|
91 | | - assert not graph_contains_any_of_ops( |
92 | | - graph=quantized_program.graph, ops=[exir_ops.edge.aten.clone.default] |
93 | | - ) |
| 95 | + return node in clone_ops or target_can_be_clone(node) |
94 | 96 |
|
95 | | - input_data = (np.random.random(input_shape) * 50).astype(np.int8) |
96 | | - convert_run_compare( |
97 | | - exported_program, |
98 | | - tfl_model=tflite_flatbuffers_model, |
99 | | - tflite_input_preprocess=ToNHWCPreprocess(), |
100 | | - tflite_output_preprocess=ToNCHWPreprocess(), |
101 | | - input_data=input_data, |
102 | | - atol=1.0, |
| 97 | + @parameterized.expand( |
| 98 | + list(itertools.product([True, False], [(1, 3, 128, 128), (1, 3, 256, 256)])) |
103 | 99 | ) |
| 100 | + def test_conv_dropout_quant(self, inplace_dropout: bool, input_shape: tuple[int]): |
| 101 | + model = SingleConvBlockWithDropout( |
| 102 | + conv_in_channels=input_shape[1], perform_inplace_dropout=inplace_dropout |
| 103 | + ).eval() |
| 104 | + |
| 105 | + with kgb.spy_on( |
| 106 | + EdgeProgramToIRConverter.convert_program, call_original=True |
| 107 | + ) as converter_spy: |
| 108 | + quantized_program = to_quantized_edge_program( |
| 109 | + model, input_shape |
| 110 | + ).exported_program() |
| 111 | + |
| 112 | + tflite_flatbuffers_model, _ = converter_spy.calls[-1].return_value |
| 113 | + exported_program: ExportedProgram = converter_spy.calls[-1].args[0] |
| 114 | + |
| 115 | + assert not graph_contains_any( |
| 116 | + graph=quantized_program.graph, |
| 117 | + condition=TestCloneConverter._node_is_clone, |
| 118 | + ) |
| 119 | + |
| 120 | + input_data = (np.random.random(input_shape) * 50).astype(np.int8) |
| 121 | + convert_run_compare( |
| 122 | + exported_program, |
| 123 | + tfl_model=tflite_flatbuffers_model, |
| 124 | + tflite_input_preprocess=ToNHWCPreprocess(), |
| 125 | + tflite_output_preprocess=ToNCHWPreprocess(), |
| 126 | + input_data=input_data, |
| 127 | + atol=1.0, |
| 128 | + ) |
| 129 | + |
| 130 | + @parameterized.expand( |
| 131 | + list(itertools.product([True, False], [(1, 3, 128, 128), (1, 3, 256, 256)])) |
| 132 | + ) |
| 133 | + def test_conv_dropout_no_quant( |
| 134 | + self, inplace_dropout: bool, input_shape: tuple[int] |
| 135 | + ): |
| 136 | + model = SingleConvBlockWithDropout( |
| 137 | + conv_in_channels=input_shape[1], perform_inplace_dropout=inplace_dropout |
| 138 | + ).eval() |
| 139 | + |
| 140 | + edge_program = to_edge_program(model, input_shape).exported_program() |
| 141 | + |
| 142 | + has_clone = graph_contains_any_of_ops( |
| 143 | + graph=edge_program.graph, |
| 144 | + ops=[ |
| 145 | + exir_ops.edge.aten.clone.default, |
| 146 | + exir_ops.edge.dim_order_ops._clone_dim_order.default, |
| 147 | + ], |
| 148 | + ) |
104 | 149 |
|
| 150 | + # Clone with inplace=True should not produce clone edge op and vice versa |
| 151 | + assert inplace_dropout ^ has_clone |
105 | 152 |
|
106 | | -@pytest.mark.parametrize("inplace_dropout", [False, True]) |
107 | | -def test_clone_pool_view_copy_quant( |
108 | | - mocker, inplace_dropout: bool, input_shape: tuple[int] = (1, 64, 25, 5) |
109 | | -): |
110 | | - model = KWSFinalBlock(input_shape).eval() |
111 | | - |
112 | | - converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
113 | | - |
114 | | - quantized_program = to_quantized_edge_program(model, input_shape).exported_program() |
115 | | - |
116 | | - tflite_flatbuffers_model, io_formats = converter_spy.spy_return |
117 | | - exported_program: ExportedProgram = converter_spy.call_args.args[1] |
118 | | - |
119 | | - assert not graph_contains_any_of_ops( |
120 | | - graph=quantized_program.graph, ops=[exir_ops.edge.aten.clone.default] |
121 | | - ) |
| 153 | + input_data = np.random.random(input_shape).astype(np.float32) |
| 154 | + convert_run_compare( |
| 155 | + edge_program, |
| 156 | + input_data, |
| 157 | + tflite_input_preprocess=ToNHWCPreprocess(), |
| 158 | + tflite_output_preprocess=ToNCHWPreprocess(), |
| 159 | + atol=1.0e-7, |
| 160 | + ) |
122 | 161 |
|
123 | | - input_data = (np.random.random(input_shape) * 50).astype(np.int8) |
124 | | - convert_run_compare( |
125 | | - exported_program, |
126 | | - tfl_model=tflite_flatbuffers_model, |
127 | | - tflite_input_preprocess=ToNHWCPreprocess(), |
128 | | - input_data=input_data, |
129 | | - atol=1.0, |
130 | | - ) |
| 162 | + def test_clone_pool_view_copy_quant(self, input_shape: tuple[int] = (1, 64, 25, 5)): |
| 163 | + model = KWSFinalBlock(input_shape).eval() |
| 164 | + |
| 165 | + with kgb.spy_on( |
| 166 | + EdgeProgramToIRConverter.convert_program, call_original=True |
| 167 | + ) as converter_spy: |
| 168 | + quantized_program = to_quantized_edge_program( |
| 169 | + model, input_shape |
| 170 | + ).exported_program() |
| 171 | + |
| 172 | + tflite_flatbuffers_model, _ = converter_spy.calls[-1].return_value |
| 173 | + exported_program: ExportedProgram = converter_spy.calls[-1].args[0] |
| 174 | + |
| 175 | + assert not graph_contains_any( |
| 176 | + graph=quantized_program.graph, |
| 177 | + condition=TestCloneConverter._node_is_clone, |
| 178 | + ) |
| 179 | + |
| 180 | + input_data = (np.random.random(input_shape) * 50).astype(np.int8) |
| 181 | + convert_run_compare( |
| 182 | + exported_program, |
| 183 | + tfl_model=tflite_flatbuffers_model, |
| 184 | + tflite_input_preprocess=ToNHWCPreprocess(), |
| 185 | + input_data=input_data, |
| 186 | + atol=1.0, |
| 187 | + ) |
| 188 | + |
| 189 | + @classmethod |
| 190 | + def setUpClass(cls): |
| 191 | + torch.manual_seed(23) |
| 192 | + np.random.seed(23) |
0 commit comments