|
| 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 | +import pytest |
| 8 | +import torch |
| 9 | + |
| 10 | +from executorch.backends.nxp.backend.edge_program_converter import ( |
| 11 | + EdgeProgramToIRConverter, |
| 12 | +) |
| 13 | +from executorch.backends.nxp.tests.executorch_pipeline import to_quantized_edge_program |
| 14 | +from executorch.backends.nxp.tests.executors import ( |
| 15 | + convert_run_compare, |
| 16 | + graph_contains_any_of_ops, |
| 17 | +) |
| 18 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(autouse=True) |
| 22 | +def reseed_model_per_test_run(): |
| 23 | + torch.manual_seed(42) |
| 24 | + np.random.seed(23) |
| 25 | + |
| 26 | + |
| 27 | +# noinspection PyProtectedMember |
| 28 | +ExecutorchDelegateCall = torch.ops.higher_order.executorch_call_delegate |
| 29 | +Clamp = exir_ops.edge.aten.clamp.default |
| 30 | + |
| 31 | + |
| 32 | +class ClampModule(torch.nn.Module): |
| 33 | + |
| 34 | + # noinspection PyShadowingBuiltins |
| 35 | + def __init__(self, min=None, max=None): |
| 36 | + super().__init__() |
| 37 | + self.min = min |
| 38 | + self.max = max |
| 39 | + |
| 40 | + # noinspection PyMethodMayBeStatic |
| 41 | + def forward(self, x): |
| 42 | + return torch.clamp(x, self.min, self.max) |
| 43 | + |
| 44 | + |
| 45 | +class AddClampModule(torch.nn.Module): |
| 46 | + |
| 47 | + # noinspection PyShadowingBuiltins |
| 48 | + def __init__(self, min=None, max=None): |
| 49 | + super().__init__() |
| 50 | + self.clamp = ClampModule(min, max) |
| 51 | + |
| 52 | + def forward(self, x): |
| 53 | + x = x + x |
| 54 | + return self.clamp(x) |
| 55 | + |
| 56 | + |
| 57 | +# noinspection PyShadowingBuiltins |
| 58 | +@pytest.mark.parametrize( |
| 59 | + "min, max", |
| 60 | + [ |
| 61 | + pytest.param(0, 6, id="min = 0, max = 6 (Relu6)"), |
| 62 | + pytest.param(0, 1, id="min = 0, max = 1 (Relu0To1)"), |
| 63 | + pytest.param(-1, 1, id="min = -1, max = 1 (ReluN1To1)"), |
| 64 | + pytest.param(0, None, id="min = 0, max = None (Relu)"), |
| 65 | + # float bounds. |
| 66 | + pytest.param(0.0, 6.0, id="min = 0.0, max = 6.0 (Relu6)"), |
| 67 | + pytest.param(0.0, 1.0, id="min = 0.0, max = 1.0 (Relu0To1)"), |
| 68 | + pytest.param(-1.0, 1.0, id="min = -1.0, max = 1.0 (ReluN1To1)"), |
| 69 | + pytest.param(0.0, None, id="min = 0.0, max = None (Relu)"), |
| 70 | + ], |
| 71 | +) |
| 72 | +def test_convert_clamp__supported(mocker, min, max): |
| 73 | + input_shape = (23,) |
| 74 | + model = AddClampModule(min, max) |
| 75 | + |
| 76 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 77 | + delegated_ep = to_quantized_edge_program(model, input_shape).exported_program() |
| 78 | + |
| 79 | + # Make sure the `clamp` was delegated. |
| 80 | + assert graph_contains_any_of_ops(delegated_ep.graph, [ExecutorchDelegateCall]) |
| 81 | + assert not graph_contains_any_of_ops(delegated_ep.graph, [Clamp]) |
| 82 | + |
| 83 | + # Verify correct behavior of the converted NeutronIR model. |
| 84 | + intermediate_ep = converter_spy.call_args.args[1] |
| 85 | + neutron_ir_model, _ = converter_spy.spy_return |
| 86 | + |
| 87 | + input_data = ( |
| 88 | + np.random.random(input_shape).astype(np.float32) * 256.0 - 128.0 |
| 89 | + ).astype(np.int8) |
| 90 | + |
| 91 | + # Make sure the tested program contains the `clamp`. |
| 92 | + assert graph_contains_any_of_ops(intermediate_ep.graph, [Clamp]) |
| 93 | + |
| 94 | + convert_run_compare( |
| 95 | + intermediate_ep, |
| 96 | + tfl_model=neutron_ir_model, |
| 97 | + input_data=input_data, |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +# noinspection PyShadowingBuiltins |
| 102 | +@pytest.mark.parametrize( |
| 103 | + "min, max", |
| 104 | + [ |
| 105 | + pytest.param(0, 6, id="min = 0, max = 6 (Relu6)"), |
| 106 | + pytest.param(0, None, id="min = 0, max = None (Relu)"), |
| 107 | + ], |
| 108 | +) |
| 109 | +def test_convert_clamp__single_op__not_delegated_variants(min, max): |
| 110 | + # Test that Clamp representable as Relu6 or Relu is NOT delegated, because it is a single op model which is not |
| 111 | + # supported by Neutron. |
| 112 | + input_shape = (23,) |
| 113 | + model = ClampModule(min, max) |
| 114 | + |
| 115 | + delegated_ep = to_quantized_edge_program(model, input_shape).exported_program() |
| 116 | + |
| 117 | + # Make sure the `clamp` was NOT delegated (single op model). |
| 118 | + assert not graph_contains_any_of_ops(delegated_ep.graph, [ExecutorchDelegateCall]) |
| 119 | + assert graph_contains_any_of_ops(delegated_ep.graph, [Clamp]) |
| 120 | + |
| 121 | + |
| 122 | +# noinspection PyShadowingBuiltins |
| 123 | +@pytest.mark.parametrize( |
| 124 | + "min, max", |
| 125 | + [ |
| 126 | + pytest.param(0, 1, id="min = 0, max = 1 (Relu0To1)"), |
| 127 | + pytest.param(-1, 1, id="min = -1, max = 1 (ReluN1To1)"), |
| 128 | + ], |
| 129 | +) |
| 130 | +def test_convert_clamp__single_op__delegated_variants(mocker, min, max): |
| 131 | + # Test that Clamp representable as Relu0To1 or ReluN1To1 is delegated, even though it is a single op model. |
| 132 | + input_shape = (23,) |
| 133 | + model = ClampModule(min, max) |
| 134 | + |
| 135 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 136 | + delegated_ep = to_quantized_edge_program(model, input_shape).exported_program() |
| 137 | + |
| 138 | + # Make sure the `clamp` was delegated. |
| 139 | + assert graph_contains_any_of_ops(delegated_ep.graph, [ExecutorchDelegateCall]) |
| 140 | + assert not graph_contains_any_of_ops(delegated_ep.graph, [Clamp]) |
| 141 | + |
| 142 | + # Verify correct behavior of the converted NeutronIR model. |
| 143 | + intermediate_ep = converter_spy.call_args.args[1] |
| 144 | + neutron_ir_model, _ = converter_spy.spy_return |
| 145 | + |
| 146 | + input_data = ( |
| 147 | + np.random.random(input_shape).astype(np.float32) * 256.0 - 128.0 |
| 148 | + ).astype(np.int8) |
| 149 | + |
| 150 | + # Make sure the tested program contains the `clamp`. |
| 151 | + assert graph_contains_any_of_ops(intermediate_ep.graph, [Clamp]) |
| 152 | + |
| 153 | + convert_run_compare( |
| 154 | + intermediate_ep, |
| 155 | + tfl_model=neutron_ir_model, |
| 156 | + input_data=input_data, |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +# noinspection PyShadowingBuiltins |
| 161 | +@pytest.mark.parametrize( |
| 162 | + "min, max", |
| 163 | + [ |
| 164 | + pytest.param(-3, 3, id="min = -3, max = 3"), |
| 165 | + pytest.param(None, 5, id="min = None, max = 5"), |
| 166 | + ], |
| 167 | +) |
| 168 | +def test_convert_clamp__no_delegation__unsupported_bounds(min, max): |
| 169 | + input_shape = (23,) |
| 170 | + model = AddClampModule(min, max) |
| 171 | + |
| 172 | + delegated_ep = to_quantized_edge_program(model, input_shape).exported_program() |
| 173 | + |
| 174 | + # Make sure the `clamp` was NOT delegated. |
| 175 | + assert graph_contains_any_of_ops(delegated_ep.graph, [Clamp]) |
0 commit comments