|
| 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 | +ExecutorchDelegateCall = torch.ops.higher_order.executorch_call_delegate |
| 28 | +LeakyRelu2D = exir_ops.edge.aten.leaky_relu.default |
| 29 | + |
| 30 | + |
| 31 | +def _assert_successful_delegation(model, input_shape, mocker, atol=0): |
| 32 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 33 | + delegated_ep = to_quantized_edge_program(model, input_shape).exported_program() |
| 34 | + |
| 35 | + # Make sure the `leaky_relu` was delegated. |
| 36 | + assert graph_contains_any_of_ops(delegated_ep.graph, [ExecutorchDelegateCall]) |
| 37 | + assert not graph_contains_any_of_ops(delegated_ep.graph, [LeakyRelu2D]) |
| 38 | + |
| 39 | + # Verify correct behavior of the converted NeutronIR model. |
| 40 | + intermediate_ep = converter_spy.call_args.args[1] |
| 41 | + neutron_ir_model, _ = converter_spy.spy_return |
| 42 | + |
| 43 | + input_data = ( |
| 44 | + np.random.random(input_shape).astype(np.float32) * 256.0 - 128.0 |
| 45 | + ).astype(np.int8) |
| 46 | + |
| 47 | + # Make sure the tested program contains the `leaky_relu`. |
| 48 | + assert graph_contains_any_of_ops(intermediate_ep.graph, [LeakyRelu2D]) |
| 49 | + |
| 50 | + convert_run_compare( |
| 51 | + intermediate_ep, tfl_model=neutron_ir_model, input_data=input_data, atol=atol |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +class LeakyReluModule(torch.nn.Module): |
| 56 | + |
| 57 | + def __init__(self, *args, **kwargs): |
| 58 | + super().__init__() |
| 59 | + self.leaky_relu = torch.nn.LeakyReLU(*args, **kwargs) |
| 60 | + |
| 61 | + def forward(self, x): |
| 62 | + return self.leaky_relu(x) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize( |
| 66 | + "alpha", |
| 67 | + [ |
| 68 | + 0.01, # Default value. |
| 69 | + 0.1, |
| 70 | + 3.14159, |
| 71 | + 0.0, |
| 72 | + 1.0, |
| 73 | + ], |
| 74 | + ids=lambda alpha: f"alpha = {alpha}", |
| 75 | +) |
| 76 | +def test_convert_leaky_relu__alpha(mocker, alpha): |
| 77 | + _assert_successful_delegation( |
| 78 | + LeakyReluModule(negative_slope=alpha), |
| 79 | + (23,), |
| 80 | + mocker, |
| 81 | + atol=1, # Common quantization rounding error. |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def test_convert_leaky_relu__default_alpha(mocker): |
| 86 | + _assert_successful_delegation( |
| 87 | + LeakyReluModule(), # Leave the default alpha. |
| 88 | + (23,), |
| 89 | + mocker, |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize( |
| 94 | + "inplace", |
| 95 | + [False, True], |
| 96 | + ids=lambda inplace: f"inplace = {inplace}", |
| 97 | +) |
| 98 | +def test_convert_leaky_relu__inplace(mocker, inplace): |
| 99 | + _assert_successful_delegation( |
| 100 | + LeakyReluModule(inplace=inplace), |
| 101 | + (23,), |
| 102 | + mocker, |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize( |
| 107 | + "input_shape", |
| 108 | + [ |
| 109 | + (5,), |
| 110 | + (4, 5), |
| 111 | + (3, 4, 5), |
| 112 | + (2, 3, 4, 5), |
| 113 | + (1, 2, 3, 4, 5), |
| 114 | + ], |
| 115 | + ids=lambda input_shape: f"{len(input_shape)}D", |
| 116 | +) |
| 117 | +def test_convert_leaky_relu__ranks(mocker, input_shape: tuple[int, ...]): |
| 118 | + _assert_successful_delegation( |
| 119 | + LeakyReluModule(), |
| 120 | + input_shape, |
| 121 | + mocker, |
| 122 | + atol=1, # Common quantization rounding error. |
| 123 | + ) |
0 commit comments