|
| 1 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 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 executorch.backends.arm.tosa.dialect # noqa: F401 |
| 7 | +import pytest |
| 8 | +import torch |
| 9 | +from executorch.backends.arm.tosa.dialect.lib import TosaValueError |
| 10 | +from executorch.backends.arm.tosa.dialect.ops_registration import ( |
| 11 | + get_registered_tosa_ops, |
| 12 | +) |
| 13 | +from executorch.backends.arm.tosa.specification import ( |
| 14 | + TosaLoweringContext, |
| 15 | + TosaSpecification, |
| 16 | +) |
| 17 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 18 | +from torch._subclasses.fake_tensor import FakeTensorMode |
| 19 | + |
| 20 | + |
| 21 | +def _to_fake(mode: FakeTensorMode, *values): |
| 22 | + return [ |
| 23 | + mode.from_tensor(value) if isinstance(value, torch.Tensor) else value |
| 24 | + for value in values |
| 25 | + ] |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize( |
| 29 | + ("op_name", "spec", "input_tensor", "args", "kwargs"), |
| 30 | + [ |
| 31 | + pytest.param( |
| 32 | + "CLAMP", |
| 33 | + "TOSA-1.1+INT", |
| 34 | + torch.randint(-8, 8, (2, 3, 4), dtype=torch.int8), |
| 35 | + (-3, 3), |
| 36 | + {}, |
| 37 | + id="CLAMP", |
| 38 | + ), |
| 39 | + pytest.param( |
| 40 | + "ERF", |
| 41 | + "TOSA-1.1+FP", |
| 42 | + torch.randn((2, 3, 4), dtype=torch.float32), |
| 43 | + (), |
| 44 | + {}, |
| 45 | + id="ERF", |
| 46 | + ), |
| 47 | + pytest.param( |
| 48 | + "SIGMOID", |
| 49 | + "TOSA-1.1+FP", |
| 50 | + torch.randn((2, 3, 4), dtype=torch.float32), |
| 51 | + (), |
| 52 | + {}, |
| 53 | + id="SIGMOID", |
| 54 | + ), |
| 55 | + pytest.param( |
| 56 | + "TANH", |
| 57 | + "TOSA-1.1+FP", |
| 58 | + torch.randn((2, 3, 4), dtype=torch.float32), |
| 59 | + (), |
| 60 | + {}, |
| 61 | + id="TANH", |
| 62 | + ), |
| 63 | + ], |
| 64 | +) |
| 65 | +def test_tosa_activation_ops( |
| 66 | + op_name: str, |
| 67 | + spec: str, |
| 68 | + input_tensor: torch.Tensor, |
| 69 | + args: tuple[object, ...], |
| 70 | + kwargs: dict[str, object], |
| 71 | +) -> None: |
| 72 | + with TosaLoweringContext( |
| 73 | + TosaSpecification.create_from_string(spec) |
| 74 | + ), FakeTensorMode() as mode: |
| 75 | + output = getattr(exir_ops.backend.tosa, op_name).default( |
| 76 | + *_to_fake(mode, input_tensor, *args), |
| 77 | + **kwargs, |
| 78 | + ) |
| 79 | + |
| 80 | + assert output.dtype == input_tensor.dtype |
| 81 | + assert tuple(output.shape) == tuple(input_tensor.shape) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize( |
| 85 | + ("op", "spec", "expected"), |
| 86 | + [ |
| 87 | + pytest.param( |
| 88 | + exir_ops.backend.tosa.ERF.default, "TOSA-1.1+INT", False, id="erf_int" |
| 89 | + ), |
| 90 | + pytest.param( |
| 91 | + exir_ops.backend.tosa.SIGMOID.default, |
| 92 | + "TOSA-1.1+INT", |
| 93 | + False, |
| 94 | + id="sigmoid_int", |
| 95 | + ), |
| 96 | + pytest.param( |
| 97 | + exir_ops.backend.tosa.TANH.default, "TOSA-1.1+INT", False, id="tanh_int" |
| 98 | + ), |
| 99 | + pytest.param( |
| 100 | + exir_ops.backend.tosa.ERF.default, "TOSA-1.1+FP", True, id="erf_fp" |
| 101 | + ), |
| 102 | + pytest.param( |
| 103 | + exir_ops.backend.tosa.SIGMOID.default, "TOSA-1.1+FP", True, id="sigmoid_fp" |
| 104 | + ), |
| 105 | + pytest.param( |
| 106 | + exir_ops.backend.tosa.TANH.default, "TOSA-1.1+FP", True, id="tanh_fp" |
| 107 | + ), |
| 108 | + ], |
| 109 | +) |
| 110 | +def test_tosa_transcendentals_registered_for_fp_profile_only( |
| 111 | + op, |
| 112 | + spec: str, |
| 113 | + expected: bool, |
| 114 | +) -> None: |
| 115 | + with TosaLoweringContext(TosaSpecification.create_from_string(spec)): |
| 116 | + registered_ops = get_registered_tosa_ops() |
| 117 | + |
| 118 | + assert (op in registered_ops) is expected |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.parametrize( |
| 122 | + ("op_name", "input_tensor"), |
| 123 | + [ |
| 124 | + pytest.param( |
| 125 | + "ERF", |
| 126 | + torch.randn((2, 3, 4), dtype=torch.bfloat16), |
| 127 | + id="ERF", |
| 128 | + ), |
| 129 | + pytest.param( |
| 130 | + "SIGMOID", |
| 131 | + torch.randn((2, 3, 4), dtype=torch.bfloat16), |
| 132 | + id="SIGMOID", |
| 133 | + ), |
| 134 | + pytest.param( |
| 135 | + "TANH", |
| 136 | + torch.randn((2, 3, 4), dtype=torch.bfloat16), |
| 137 | + id="TANH", |
| 138 | + ), |
| 139 | + ], |
| 140 | +) |
| 141 | +def test_tosa_transcendentals_accept_bfloat16_with_bf16_extension( |
| 142 | + op_name: str, |
| 143 | + input_tensor: torch.Tensor, |
| 144 | +) -> None: |
| 145 | + with TosaLoweringContext( |
| 146 | + TosaSpecification.create_from_string("TOSA-1.1+FP+bf16") |
| 147 | + ), FakeTensorMode() as mode: |
| 148 | + output = getattr(exir_ops.backend.tosa, op_name).default( |
| 149 | + mode.from_tensor(input_tensor) |
| 150 | + ) |
| 151 | + |
| 152 | + assert output.dtype == torch.bfloat16 |
| 153 | + assert tuple(output.shape) == tuple(input_tensor.shape) |
| 154 | + |
| 155 | + |
| 156 | +def test_clamp_rejects_invalid_range() -> None: |
| 157 | + sample_input = torch.randint(-8, 8, (2, 3, 4), dtype=torch.int8) |
| 158 | + |
| 159 | + with TosaLoweringContext( |
| 160 | + TosaSpecification.create_from_string("TOSA-1.1+INT") |
| 161 | + ), FakeTensorMode() as mode: |
| 162 | + with pytest.raises( |
| 163 | + TosaValueError, |
| 164 | + match="max_val must be greater than or equal to min_val", |
| 165 | + ): |
| 166 | + exir_ops.backend.tosa.CLAMP.default( |
| 167 | + mode.from_tensor(sample_input), |
| 168 | + 4, |
| 169 | + -4, |
| 170 | + ) |
| 171 | + |
| 172 | + |
| 173 | +@pytest.mark.parametrize( |
| 174 | + ("min_val", "max_val", "match"), |
| 175 | + [ |
| 176 | + pytest.param(-1.5, 1.5, "must be an integer", id="non_integral"), |
| 177 | + pytest.param(-200, 200, "must be in \\[-128, 127\\]", id="out_of_range"), |
| 178 | + ], |
| 179 | +) |
| 180 | +def test_clamp_rejects_invalid_integer_bounds( |
| 181 | + min_val: int | float, |
| 182 | + max_val: int | float, |
| 183 | + match: str, |
| 184 | +) -> None: |
| 185 | + sample_input = torch.randint(-8, 8, (2, 3, 4), dtype=torch.int8) |
| 186 | + |
| 187 | + with TosaLoweringContext( |
| 188 | + TosaSpecification.create_from_string("TOSA-1.1+INT") |
| 189 | + ), FakeTensorMode() as mode: |
| 190 | + with pytest.raises(TosaValueError, match=match): |
| 191 | + exir_ops.backend.tosa.CLAMP.default( |
| 192 | + mode.from_tensor(sample_input), |
| 193 | + min_val, |
| 194 | + max_val, |
| 195 | + ) |
0 commit comments