|
| 1 | +# Copyright 2026 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 | + |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +# noinspection PyUnusedImports |
| 9 | +import pytest |
| 10 | +import torch |
| 11 | + |
| 12 | +from executorch.backends.nxp.tests.dataset_creator import RandomDatasetCreator |
| 13 | +from executorch.backends.nxp.tests.executorch_pipeline import ( |
| 14 | + ModelInputSpec, |
| 15 | + to_quantized_edge_program, |
| 16 | +) |
| 17 | +from executorch.backends.nxp.tests.executors import graph_contains_any_of_ops |
| 18 | +from executorch.backends.nxp.tests.graph_verifier import DetailedGraphVerifier |
| 19 | +from executorch.backends.nxp.tests.model_output_comparator import ( |
| 20 | + AllCloseOutputComparator, |
| 21 | +) |
| 22 | +from executorch.backends.nxp.tests.models import MaxPoolMinimumModule, MinimumModule |
| 23 | +from executorch.backends.nxp.tests.nsys_testing import lower_run_compare |
| 24 | +from executorch.backends.nxp.tests.ops_aliases import ( |
| 25 | + ExecutorchDelegateCall, |
| 26 | + GetItem, |
| 27 | + MaxPool2DWithIndices, |
| 28 | + Minimum, |
| 29 | +) |
| 30 | +from executorch.backends.nxp.tests.use_qat import * # noqa F403 |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(autouse=True) |
| 34 | +def reseed_model_per_test_run(): |
| 35 | + torch.manual_seed(23) |
| 36 | + np.random.seed(23) |
| 37 | + |
| 38 | + |
| 39 | +class TestMinimum: |
| 40 | + # noinspection PyMethodMayBeStatic |
| 41 | + def assert_delegated( |
| 42 | + self, |
| 43 | + model, |
| 44 | + input_spec, |
| 45 | + mocker, |
| 46 | + request, |
| 47 | + expected_delegated_ops=None, |
| 48 | + use_qat=False, |
| 49 | + ): |
| 50 | + graph_verifier = DetailedGraphVerifier( |
| 51 | + mocker, |
| 52 | + expected_delegated_ops=expected_delegated_ops or {Minimum: 1}, |
| 53 | + expected_non_delegated_ops={}, |
| 54 | + ) |
| 55 | + dataset_creator = RandomDatasetCreator(low=-1.0, high=1.0) |
| 56 | + |
| 57 | + # Quantize the dataset and allow a single bit error. |
| 58 | + remove_quant_io_ops = True |
| 59 | + comparator = AllCloseOutputComparator(atol=1) |
| 60 | + |
| 61 | + lower_run_compare( |
| 62 | + model, |
| 63 | + input_spec, |
| 64 | + graph_verifier, |
| 65 | + request, |
| 66 | + dataset_creator, |
| 67 | + comparator, |
| 68 | + remove_quant_io_ops=remove_quant_io_ops, |
| 69 | + use_qat=use_qat, |
| 70 | + ) |
| 71 | + |
| 72 | + # noinspection PyMethodMayBeStatic |
| 73 | + def assert_not_delegated(self, model, input_shape): |
| 74 | + delegated_ep = to_quantized_edge_program(model, input_shape).exported_program() |
| 75 | + |
| 76 | + # Make sure the `sum` was NOT delegated. |
| 77 | + assert not graph_contains_any_of_ops( |
| 78 | + delegated_ep.graph, [ExecutorchDelegateCall] |
| 79 | + ) |
| 80 | + assert graph_contains_any_of_ops(delegated_ep.graph, [Minimum]) |
| 81 | + |
| 82 | + @pytest.mark.parametrize( |
| 83 | + "x_input_shape", |
| 84 | + [ |
| 85 | + pytest.param((1,), id="1D."), |
| 86 | + pytest.param((6, 5), id="2D."), |
| 87 | + pytest.param((6, 82), id="2D alt."), |
| 88 | + pytest.param((1, 4, 7), id="3D."), |
| 89 | + pytest.param((1, 68, 7), id="3D alt."), |
| 90 | + pytest.param((2, 4, 3, 15), id="4D."), |
| 91 | + pytest.param((1, 4, 9, 11, 4), id="5D."), |
| 92 | + ], |
| 93 | + ) |
| 94 | + def test__basic_nsys_inference(self, mocker, request, x_input_shape): |
| 95 | + input_spec = [ModelInputSpec(x_input_shape), ModelInputSpec(x_input_shape)] |
| 96 | + model = MinimumModule() |
| 97 | + |
| 98 | + self.assert_delegated(model, input_spec, mocker, request) |
| 99 | + |
| 100 | + def test__basic_nsys_inference_qat(self, mocker, request): |
| 101 | + input_spec = [ModelInputSpec((1, 4, 7)), ModelInputSpec((1, 4, 7))] |
| 102 | + model = MinimumModule() |
| 103 | + |
| 104 | + self.assert_delegated(model, input_spec, mocker, request, use_qat=True) |
| 105 | + |
| 106 | + @pytest.mark.parametrize( |
| 107 | + "input_spec", |
| 108 | + [ |
| 109 | + pytest.param( |
| 110 | + [ModelInputSpec((4, 6)), ModelInputSpec((1, 6))], id="2 inputs 2D." |
| 111 | + ), |
| 112 | + pytest.param( |
| 113 | + [ModelInputSpec((69, 73)), ModelInputSpec((1, 73))], |
| 114 | + id="2 inputs 2D alt.", |
| 115 | + ), |
| 116 | + pytest.param( |
| 117 | + [ModelInputSpec((5, 3, 4)), ModelInputSpec((1, 3, 1))], |
| 118 | + id="2 inputs 3D.", |
| 119 | + ), |
| 120 | + pytest.param( |
| 121 | + [ModelInputSpec((4,)), ModelInputSpec((4, 4))], |
| 122 | + id="2 inputs 1D + 2D.", |
| 123 | + marks=pytest.mark.xfail( |
| 124 | + reason="AIR-14864: conversion fails", strict=True |
| 125 | + ), |
| 126 | + ), |
| 127 | + pytest.param( |
| 128 | + [ModelInputSpec((1, 4, 8, 8)), ModelInputSpec((8, 8))], |
| 129 | + id="2 inputs 4D + 2D.", |
| 130 | + ), |
| 131 | + pytest.param( |
| 132 | + [ModelInputSpec((10,)), ModelInputSpec((1, 1))], |
| 133 | + id="2 inputs 1D + 2D, num_elems of input == num_elems of output", |
| 134 | + ), |
| 135 | + ], |
| 136 | + ) |
| 137 | + def test__broadcast(self, mocker, request, input_spec): |
| 138 | + model = MinimumModule() |
| 139 | + |
| 140 | + self.assert_delegated(model, input_spec, mocker, request) |
| 141 | + |
| 142 | + @pytest.mark.parametrize( |
| 143 | + "input_spec", |
| 144 | + [ |
| 145 | + pytest.param( |
| 146 | + [ModelInputSpec((4, 1)), ModelInputSpec((1, 6))], id="2 inputs 2D." |
| 147 | + ), |
| 148 | + pytest.param( |
| 149 | + [ModelInputSpec((1, 3, 4)), ModelInputSpec((5, 3, 1))], |
| 150 | + id="2 inputs 3D.", |
| 151 | + ), |
| 152 | + pytest.param( |
| 153 | + [ModelInputSpec((6, 4)), ModelInputSpec((6, 6, 1))], |
| 154 | + id="2 inputs 2D + 3D.", |
| 155 | + ), |
| 156 | + ], |
| 157 | + ) |
| 158 | + def test__broadcast_unsupported(self, input_spec): |
| 159 | + # Unsupported broadcasts: an input dimension differs from the output dimension |
| 160 | + # and is not equal to 1 or not omitted. |
| 161 | + model = MinimumModule() |
| 162 | + |
| 163 | + self.assert_not_delegated(model, input_spec) |
| 164 | + |
| 165 | + @pytest.mark.parametrize( |
| 166 | + "input_spec", |
| 167 | + [ |
| 168 | + pytest.param( |
| 169 | + ModelInputSpec((1, 4, 5, 5)), |
| 170 | + id="4D, product of dims is not a multiple of 8.", |
| 171 | + ), |
| 172 | + ], |
| 173 | + ) |
| 174 | + def test__channels_first_input(self, mocker, request, input_spec): |
| 175 | + model = MaxPoolMinimumModule() |
| 176 | + expected_delegated_ops = {Minimum: 1, MaxPool2DWithIndices: 1, GetItem: 1} |
| 177 | + |
| 178 | + self.assert_delegated( |
| 179 | + model, [input_spec, input_spec], mocker, request, expected_delegated_ops |
| 180 | + ) |
| 181 | + |
| 182 | + @pytest.mark.parametrize( |
| 183 | + "input_spec", |
| 184 | + [ |
| 185 | + pytest.param( |
| 186 | + [ModelInputSpec((1, 8, 5, 5)), ModelInputSpec((1, 8, 5, 1))], |
| 187 | + id="2 inputs 4D + 4D.", |
| 188 | + ), |
| 189 | + pytest.param( |
| 190 | + [ModelInputSpec((1, 8, 1, 67)), ModelInputSpec((1, 8, 5, 67))], |
| 191 | + id="2 inputs 4D + 4D same width.", |
| 192 | + marks=pytest.mark.xfail( |
| 193 | + reason="AIR-14864: conversion fails", strict=True |
| 194 | + ), |
| 195 | + ), |
| 196 | + pytest.param( |
| 197 | + [ModelInputSpec((1, 8, 5, 5)), ModelInputSpec((1, 1))], |
| 198 | + id="2 inputs 4D + 2D ones tensor.", |
| 199 | + ), |
| 200 | + pytest.param( |
| 201 | + [ModelInputSpec((1, 8, 8, 8)), ModelInputSpec((8, 8))], |
| 202 | + id="2 inputs 4D + 2D both dims 8.", |
| 203 | + ), |
| 204 | + pytest.param( |
| 205 | + [ModelInputSpec((1, 8, 5, 5)), ModelInputSpec((1, 5))], |
| 206 | + id="2 inputs 4D + 2D one dim 5.", |
| 207 | + ), |
| 208 | + pytest.param( |
| 209 | + [ModelInputSpec((1, 8, 12, 10)), ModelInputSpec((8, 1, 10))], |
| 210 | + id="2 inputs 4D + 3D channels dim 1.", |
| 211 | + ), |
| 212 | + pytest.param( |
| 213 | + [ModelInputSpec((1, 8, 4, 10)), ModelInputSpec((1, 4, 1))], |
| 214 | + id="2 inputs 4D + 3D channels dim 4.", |
| 215 | + ), |
| 216 | + pytest.param( |
| 217 | + [ModelInputSpec((1, 8, 25, 18)), ModelInputSpec((4, 1, 8, 25, 18))], |
| 218 | + id="2 inputs 4D + 5D conversion fails.", |
| 219 | + marks=pytest.mark.xfail( |
| 220 | + reason="AIR-14864: conversion fails", strict=True |
| 221 | + ), |
| 222 | + ), |
| 223 | + ], |
| 224 | + ) |
| 225 | + def test__broadcast__channels_first_input(self, mocker, request, input_spec): |
| 226 | + model = MaxPoolMinimumModule() |
| 227 | + expected_delegated_ops = {Minimum: 1, MaxPool2DWithIndices: 1, GetItem: 1} |
| 228 | + |
| 229 | + self.assert_delegated( |
| 230 | + model, input_spec, mocker, request, expected_delegated_ops |
| 231 | + ) |
0 commit comments