|
| 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 | + ( |
| 30 | + "spec", |
| 31 | + "condition", |
| 32 | + "input1", |
| 33 | + "input2", |
| 34 | + "expected_shape", |
| 35 | + "expected_dtype", |
| 36 | + ), |
| 37 | + [ |
| 38 | + pytest.param( |
| 39 | + "TOSA-1.1+INT", |
| 40 | + torch.randint(0, 2, (2, 1, 3), dtype=torch.bool), |
| 41 | + torch.randint(-8, 8, (1, 4, 3), dtype=torch.int32), |
| 42 | + torch.randint(-8, 8, (1, 4, 3), dtype=torch.int32), |
| 43 | + (2, 4, 3), |
| 44 | + torch.int32, |
| 45 | + id="int32", |
| 46 | + ), |
| 47 | + pytest.param( |
| 48 | + "TOSA-1.1+FP", |
| 49 | + torch.randint(0, 2, (2, 1, 3), dtype=torch.bool), |
| 50 | + torch.randn((1, 4, 3), dtype=torch.float32), |
| 51 | + torch.randn((1, 4, 3), dtype=torch.float32), |
| 52 | + (2, 4, 3), |
| 53 | + torch.float32, |
| 54 | + id="fp32", |
| 55 | + ), |
| 56 | + pytest.param( |
| 57 | + "TOSA-1.1+FP", |
| 58 | + torch.randint(0, 2, (2, 1, 3), dtype=torch.bool), |
| 59 | + torch.randint(0, 2, (1, 4, 3), dtype=torch.bool), |
| 60 | + torch.randint(0, 2, (1, 4, 3), dtype=torch.bool), |
| 61 | + (2, 4, 3), |
| 62 | + torch.bool, |
| 63 | + id="bool", |
| 64 | + ), |
| 65 | + ], |
| 66 | +) |
| 67 | +def test_tosa_select( |
| 68 | + spec: str, |
| 69 | + condition: torch.Tensor, |
| 70 | + input1: torch.Tensor, |
| 71 | + input2: torch.Tensor, |
| 72 | + expected_shape: tuple[int, ...], |
| 73 | + expected_dtype: torch.dtype, |
| 74 | +) -> None: |
| 75 | + with TosaLoweringContext( |
| 76 | + TosaSpecification.create_from_string(spec) |
| 77 | + ), FakeTensorMode() as mode: |
| 78 | + output = exir_ops.backend.tosa.SELECT.default( |
| 79 | + *_to_fake(mode, condition, input1, input2) |
| 80 | + ) |
| 81 | + |
| 82 | + assert output.dtype == expected_dtype |
| 83 | + assert tuple(output.shape) == expected_shape |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize("spec", ["TOSA-1.1+INT", "TOSA-1.1+FP"]) |
| 87 | +def test_tosa_select_registered_for_all_profiles(spec: str) -> None: |
| 88 | + with TosaLoweringContext(TosaSpecification.create_from_string(spec)): |
| 89 | + registered_ops = get_registered_tosa_ops() |
| 90 | + |
| 91 | + assert exir_ops.backend.tosa.SELECT.default in registered_ops |
| 92 | + |
| 93 | + |
| 94 | +def test_tosa_select_accepts_bfloat16_with_bf16_extension() -> None: |
| 95 | + condition = torch.randint(0, 2, (2, 3), dtype=torch.bool) |
| 96 | + input1 = torch.randn((2, 3), dtype=torch.bfloat16) |
| 97 | + input2 = torch.randn((2, 3), dtype=torch.bfloat16) |
| 98 | + |
| 99 | + with TosaLoweringContext( |
| 100 | + TosaSpecification.create_from_string("TOSA-1.1+FP+bf16") |
| 101 | + ), FakeTensorMode() as mode: |
| 102 | + output = exir_ops.backend.tosa.SELECT.default( |
| 103 | + *_to_fake(mode, condition, input1, input2) |
| 104 | + ) |
| 105 | + |
| 106 | + assert output.dtype == torch.bfloat16 |
| 107 | + assert tuple(output.shape) == tuple(input1.shape) |
| 108 | + |
| 109 | + |
| 110 | +def test_tosa_select_rejects_non_bool_condition() -> None: |
| 111 | + condition = torch.ones((2, 3), dtype=torch.int32) |
| 112 | + input1 = torch.randn((2, 3), dtype=torch.float32) |
| 113 | + input2 = torch.randn((2, 3), dtype=torch.float32) |
| 114 | + |
| 115 | + with TosaLoweringContext( |
| 116 | + TosaSpecification.create_from_string("TOSA-1.1+FP") |
| 117 | + ), FakeTensorMode() as mode: |
| 118 | + with pytest.raises(TosaValueError, match="requires bool condition"): |
| 119 | + exir_ops.backend.tosa.SELECT.default( |
| 120 | + *_to_fake(mode, condition, input1, input2) |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +def test_tosa_select_rejects_mismatched_value_dtypes() -> None: |
| 125 | + condition = torch.randint(0, 2, (2, 3), dtype=torch.bool) |
| 126 | + input1 = torch.randn((2, 3), dtype=torch.float32) |
| 127 | + input2 = torch.randn((2, 3), dtype=torch.float16) |
| 128 | + |
| 129 | + with TosaLoweringContext( |
| 130 | + TosaSpecification.create_from_string("TOSA-1.1+FP") |
| 131 | + ), FakeTensorMode() as mode: |
| 132 | + with pytest.raises(TosaValueError, match="Expected matching dtypes"): |
| 133 | + exir_ops.backend.tosa.SELECT.default( |
| 134 | + *_to_fake(mode, condition, input1, input2) |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | +def test_tosa_select_rejects_bfloat16_without_bf16_extension() -> None: |
| 139 | + condition = torch.randint(0, 2, (2, 3), dtype=torch.bool) |
| 140 | + input1 = torch.randn((2, 3), dtype=torch.bfloat16) |
| 141 | + input2 = torch.randn((2, 3), dtype=torch.bfloat16) |
| 142 | + |
| 143 | + with TosaLoweringContext( |
| 144 | + TosaSpecification.create_from_string("TOSA-1.1+FP") |
| 145 | + ), FakeTensorMode() as mode: |
| 146 | + with pytest.raises(TosaValueError, match="doesn't support"): |
| 147 | + exir_ops.backend.tosa.SELECT.default( |
| 148 | + *_to_fake(mode, condition, input1, input2) |
| 149 | + ) |
0 commit comments