|
| 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 | + ToChannelFirstPreprocess, |
| 18 | + ToChannelLastPreprocess, |
| 19 | +) |
| 20 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(autouse=True) |
| 24 | +def reseed_model_per_test_run(): |
| 25 | + torch.manual_seed(42) |
| 26 | + np.random.seed(23) |
| 27 | + |
| 28 | + |
| 29 | +# noinspection PyProtectedMember |
| 30 | +ExecutorchDelegateCall = torch._higher_order_ops.executorch_call_delegate |
| 31 | +Neg = exir_ops.edge.aten.neg.default |
| 32 | + |
| 33 | + |
| 34 | +class NegModule(torch.nn.Module): |
| 35 | + |
| 36 | + def __init__(self): |
| 37 | + super().__init__() |
| 38 | + |
| 39 | + # noinspection PyMethodMayBeStatic |
| 40 | + def forward(self, x): |
| 41 | + return -x |
| 42 | + |
| 43 | + |
| 44 | +class ConvNegModule(torch.nn.Module): |
| 45 | + |
| 46 | + def __init__(self): |
| 47 | + super().__init__() |
| 48 | + self.conv = torch.nn.Conv2d(3, 3, 1) |
| 49 | + |
| 50 | + # noinspection PyMethodMayBeStatic |
| 51 | + def forward(self, x): |
| 52 | + x = self.conv(x) |
| 53 | + return -x |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize( |
| 57 | + "input_shape", |
| 58 | + [ |
| 59 | + pytest.param((8,), id="1D"), |
| 60 | + pytest.param((4, 2), id="2D"), |
| 61 | + pytest.param((1, 2, 3), id="3D"), |
| 62 | + pytest.param((1, 2, 3, 4), id="4D"), |
| 63 | + ], |
| 64 | +) |
| 65 | +def test_convert_neg(mocker, input_shape): |
| 66 | + model = NegModule() |
| 67 | + |
| 68 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 69 | + delegated_ep = to_quantized_edge_program(model, input_shape).exported_program() |
| 70 | + |
| 71 | + # Make sure the `neg` was delegated. |
| 72 | + assert graph_contains_any_of_ops(delegated_ep.graph, [ExecutorchDelegateCall]) |
| 73 | + assert not graph_contains_any_of_ops(delegated_ep.graph, [Neg]) |
| 74 | + |
| 75 | + # Verify correct behavior of the converted NeutronIR model. |
| 76 | + intermediate_ep = converter_spy.call_args.args[1] |
| 77 | + neutron_ir_model, _ = converter_spy.spy_return |
| 78 | + |
| 79 | + input_data = ( |
| 80 | + np.random.random(input_shape).astype(np.float32) * 256.0 - 128.0 |
| 81 | + ).astype(np.int8) |
| 82 | + |
| 83 | + # Make sure the tested program contains the `neg`. |
| 84 | + assert graph_contains_any_of_ops(intermediate_ep.graph, [Neg]) |
| 85 | + |
| 86 | + convert_run_compare( |
| 87 | + intermediate_ep, |
| 88 | + tfl_model=neutron_ir_model, |
| 89 | + input_data=input_data, |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def test_convert_neg__channels_last(mocker): |
| 94 | + model = ConvNegModule() |
| 95 | + input_shape = (1, 3, 4, 5) |
| 96 | + |
| 97 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 98 | + delegated_ep = to_quantized_edge_program( |
| 99 | + model, input_shape, use_neutron_for_format_conversion=False |
| 100 | + ).exported_program() |
| 101 | + |
| 102 | + # Make sure the `neg` was delegated. |
| 103 | + assert graph_contains_any_of_ops(delegated_ep.graph, [ExecutorchDelegateCall]) |
| 104 | + assert not graph_contains_any_of_ops(delegated_ep.graph, [Neg]) |
| 105 | + |
| 106 | + # Verify correct behavior of the converted NeutronIR model. |
| 107 | + intermediate_ep = converter_spy.call_args.args[1] |
| 108 | + neutron_ir_model, _ = converter_spy.spy_return |
| 109 | + |
| 110 | + input_data = ( |
| 111 | + np.random.random(input_shape).astype(np.float32) * 256.0 - 128.0 |
| 112 | + ).astype(np.int8) |
| 113 | + |
| 114 | + # Make sure the tested program contains the `neg`. |
| 115 | + assert graph_contains_any_of_ops(intermediate_ep.graph, [Neg]) |
| 116 | + |
| 117 | + convert_run_compare( |
| 118 | + intermediate_ep, |
| 119 | + tfl_model=neutron_ir_model, |
| 120 | + input_data=input_data, |
| 121 | + tflite_input_preprocess=ToChannelLastPreprocess(), |
| 122 | + tflite_output_preprocess=ToChannelFirstPreprocess(), |
| 123 | + ) |
0 commit comments