|
| 1 | +# Copyright 2025 NXP |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | +import torch |
| 8 | + |
| 9 | +from executorch.backends.nxp.backend.edge_program_converter import ( |
| 10 | + EdgeProgramToIRConverter, |
| 11 | +) |
| 12 | +from executorch.backends.nxp.tests.executorch_pipeline import to_quantized_edge_program |
| 13 | +from executorch.backends.nxp.tests.executors import ( |
| 14 | + convert_run_compare, |
| 15 | + ToChannelFirstPreprocess, |
| 16 | + ToChannelLastPreprocess, |
| 17 | +) |
| 18 | +from executorch.backends.nxp.tests.models import ( |
| 19 | + SubTensorConvModule, |
| 20 | + SubTensorModule, |
| 21 | + SubTensorOneInputModule, |
| 22 | +) |
| 23 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 24 | +from torch.export import ExportedProgram |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(autouse=True) |
| 28 | +def reseed_model_per_test_run(): |
| 29 | + torch.manual_seed(23) |
| 30 | + np.random.seed(23) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize( |
| 34 | + "input_shape", |
| 35 | + [ |
| 36 | + pytest.param((4,), id="1D."), |
| 37 | + pytest.param((6, 6), id="2D."), |
| 38 | + pytest.param((1, 4, 8), id="3D."), |
| 39 | + pytest.param((1, 4, 8, 8), id="4D."), |
| 40 | + ], |
| 41 | +) |
| 42 | +def test_sub_tensor_quant_conversion(mocker, input_shape): |
| 43 | + model = SubTensorModule() |
| 44 | + |
| 45 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 46 | + |
| 47 | + # Run conversion |
| 48 | + _ = to_quantized_edge_program(model, [input_shape, input_shape]) |
| 49 | + |
| 50 | + # Capture generated model |
| 51 | + tflite_flatbuffers_model, io_formats = converter_spy.spy_return |
| 52 | + |
| 53 | + # Capture converted program |
| 54 | + exported_program: ExportedProgram = converter_spy.call_args.args[1] |
| 55 | + |
| 56 | + input_data_1 = (np.random.random(input_shape).astype(np.float32) * 50).astype( |
| 57 | + np.int8 |
| 58 | + ) |
| 59 | + input_data_2 = (np.random.random(input_shape).astype(np.float32) * 50).astype( |
| 60 | + np.int8 |
| 61 | + ) |
| 62 | + input_data = {0: input_data_1, 1: input_data_2} |
| 63 | + |
| 64 | + nodes = list(exported_program.graph.nodes) |
| 65 | + assert nodes[4].target == exir_ops.edge.aten.sub.Tensor |
| 66 | + |
| 67 | + convert_run_compare( |
| 68 | + exported_program, tfl_model=tflite_flatbuffers_model, input_data=input_data |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize( |
| 73 | + "input_shape", |
| 74 | + [ |
| 75 | + pytest.param((4,), id="1D."), |
| 76 | + pytest.param((6, 6), id="2D."), |
| 77 | + pytest.param((1, 4, 8), id="3D."), |
| 78 | + pytest.param((1, 4, 8, 8), id="4D."), |
| 79 | + ], |
| 80 | +) |
| 81 | +def test_sub_tensor_one_input_quant_conversion(mocker, input_shape): |
| 82 | + model = SubTensorOneInputModule() |
| 83 | + |
| 84 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 85 | + |
| 86 | + # Run conversion |
| 87 | + _ = to_quantized_edge_program(model, input_shape) |
| 88 | + |
| 89 | + # Capture generated model |
| 90 | + tflite_flatbuffers_model, io_formats = converter_spy.spy_return |
| 91 | + |
| 92 | + # Capture converted program |
| 93 | + exported_program: ExportedProgram = converter_spy.call_args.args[1] |
| 94 | + |
| 95 | + input_data = (np.random.random(input_shape).astype(np.float32) * 50).astype(np.int8) |
| 96 | + |
| 97 | + nodes = list(exported_program.graph.nodes) |
| 98 | + assert nodes[2].target == exir_ops.edge.aten.sub.Tensor |
| 99 | + |
| 100 | + convert_run_compare( |
| 101 | + exported_program, tfl_model=tflite_flatbuffers_model, input_data=input_data |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.parametrize( |
| 106 | + "x_input_shape", |
| 107 | + [ |
| 108 | + pytest.param((1, 4, 8, 8), id="4D."), |
| 109 | + pytest.param((1, 4, 5, 5), id="4D, product of dims is not a multiple of 8."), |
| 110 | + ], |
| 111 | +) |
| 112 | +def test_sub_tensor_w_conv_quant_conversion(mocker, x_input_shape): |
| 113 | + model = SubTensorConvModule() |
| 114 | + |
| 115 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 116 | + |
| 117 | + n, c, h, w = x_input_shape |
| 118 | + y_input_shape = (n, 8, h, w) |
| 119 | + |
| 120 | + # Run conversion |
| 121 | + _ = to_quantized_edge_program(model, [x_input_shape, y_input_shape]) |
| 122 | + |
| 123 | + # Capture generated model |
| 124 | + tflite_flatbuffers_model, io_formats = converter_spy.spy_return |
| 125 | + |
| 126 | + # Capture converted program |
| 127 | + exported_program: ExportedProgram = converter_spy.call_args.args[1] |
| 128 | + |
| 129 | + input_data_1 = (np.random.random(x_input_shape).astype(np.float32) * 50).astype( |
| 130 | + np.int8 |
| 131 | + ) |
| 132 | + input_data_2 = (np.random.random(y_input_shape).astype(np.float32) * 50).astype( |
| 133 | + np.int8 |
| 134 | + ) |
| 135 | + input_data = {0: input_data_1, 1: input_data_2} |
| 136 | + |
| 137 | + nodes = list(exported_program.graph.nodes) |
| 138 | + assert nodes[15].target == exir_ops.edge.aten.sub.Tensor |
| 139 | + |
| 140 | + convert_run_compare( |
| 141 | + exported_program, |
| 142 | + input_data=input_data, |
| 143 | + tflite_input_preprocess=ToChannelLastPreprocess(), |
| 144 | + tfl_model=tflite_flatbuffers_model, |
| 145 | + tflite_output_preprocess=ToChannelFirstPreprocess(), |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.parametrize( |
| 150 | + "x_input_shape, y_input_shape", |
| 151 | + [ |
| 152 | + pytest.param((1, 4, 7), (4, 7), id="3D -> 2D."), |
| 153 | + pytest.param((1, 4, 8), (1, 4, 4, 8), id="3D -> 4D."), |
| 154 | + pytest.param((1, 1, 4, 4, 8), (1, 4, 4, 8), id="5D -> 4D."), |
| 155 | + pytest.param((4,), (4, 4), id="1D -> 2D."), |
| 156 | + pytest.param((4,), (4, 4, 4), id="1D -> 3D."), |
| 157 | + pytest.param((6, 6), (1, 8, 6, 6), id="2D -> 4D."), |
| 158 | + pytest.param((6, 6), (6,), id="2D -> 1D."), |
| 159 | + ], |
| 160 | +) |
| 161 | +def test_sub_tensor_broadcasting_unsupported_quant_conversion( |
| 162 | + x_input_shape, y_input_shape |
| 163 | +): |
| 164 | + model = SubTensorModule() |
| 165 | + |
| 166 | + # Run conversion |
| 167 | + edge_program = to_quantized_edge_program( |
| 168 | + model, [x_input_shape, y_input_shape] |
| 169 | + ).exported_program() |
| 170 | + nodes = list(edge_program.graph.nodes) |
| 171 | + |
| 172 | + # Broadcast is not supported, node is not converted |
| 173 | + assert ( |
| 174 | + nodes[6].target == exir_ops.edge.aten.sub.Tensor |
| 175 | + ) # Sub Tensor is not delegated. |
0 commit comments