|
| 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.graph_verifier import DetailedGraphVerifier |
| 14 | +from executorch.backends.nxp.tests.model_output_comparator import ( |
| 15 | + AllCloseOutputComparator, |
| 16 | +) |
| 17 | +from executorch.backends.nxp.tests.nsys_testing import lower_run_compare |
| 18 | +from executorch.backends.nxp.tests.ops_aliases import Rsqrt |
| 19 | +from executorch.backends.nxp.tests.use_qat import * # noqa F403 |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(autouse=True) |
| 23 | +def reseed_model_per_test_run(): |
| 24 | + torch.manual_seed(23) |
| 25 | + np.random.seed(23) |
| 26 | + |
| 27 | + |
| 28 | +class RsqrtModule(torch.nn.Module): |
| 29 | + def __init__(self): |
| 30 | + super().__init__() |
| 31 | + |
| 32 | + def forward(self, x): |
| 33 | + return torch.rsqrt(x) |
| 34 | + |
| 35 | + |
| 36 | +class TestRsqrt: |
| 37 | + def assert_delegated(self, model, input_shape, mocker, request, use_qat=False): |
| 38 | + graph_verifier = DetailedGraphVerifier( |
| 39 | + mocker, |
| 40 | + expected_delegated_ops={Rsqrt: 1}, |
| 41 | + expected_non_delegated_ops={}, |
| 42 | + ) |
| 43 | + |
| 44 | + # Use positive-only values because rsqrt is only defined for x > 0. |
| 45 | + dataset_creator = RandomDatasetCreator(low=0.1, high=2.0) |
| 46 | + |
| 47 | + # Allow a single quantization bit error in the output. |
| 48 | + comparator = AllCloseOutputComparator(atol=1) |
| 49 | + |
| 50 | + lower_run_compare( |
| 51 | + model, |
| 52 | + input_shape, |
| 53 | + graph_verifier, |
| 54 | + request, |
| 55 | + dataset_creator, |
| 56 | + comparator, |
| 57 | + use_qat=use_qat, |
| 58 | + ) |
| 59 | + |
| 60 | + def test__basic_nsys_inference(self, mocker, request): |
| 61 | + input_shape = (2, 13, 7, 9) |
| 62 | + model = RsqrtModule() |
| 63 | + self.assert_delegated(model, input_shape, mocker, request) |
| 64 | + |
| 65 | + def test__basic_nsys_inference__qat(self, mocker, request, use_qat): |
| 66 | + input_shape = (3, 5, 7, 11) |
| 67 | + model = RsqrtModule() |
| 68 | + self.assert_delegated(model, input_shape, mocker, request, use_qat=use_qat) |
| 69 | + |
| 70 | + @pytest.mark.parametrize( |
| 71 | + "input_shape", |
| 72 | + [ |
| 73 | + pytest.param((2,), id="1D"), |
| 74 | + pytest.param((2, 3), id="2D"), |
| 75 | + pytest.param((2, 3, 5), id="3D"), |
| 76 | + pytest.param((2, 3, 5, 7), id="4D"), |
| 77 | + ], |
| 78 | + ) |
| 79 | + def test__input_shapes(self, mocker, request, input_shape): |
| 80 | + model = RsqrtModule() |
| 81 | + self.assert_delegated(model, input_shape, mocker, request) |
0 commit comments