|
| 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 torch |
| 7 | + |
| 8 | +from executorch.backends.arm.tosa.dialect.lib import TosaValueError |
| 9 | +from executorch.backends.arm.tosa.dialect.ops_registration import register_fake_tosa_op |
| 10 | +from executorch.backends.arm.tosa.specification import ( |
| 11 | + get_context_spec, |
| 12 | + TosaSpecification, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def _validate_axis(x: torch.Tensor, axis: int, op: str) -> None: |
| 17 | + if x.dim() < 1: |
| 18 | + raise TosaValueError(f"{op} requires rank >= 1 input", op=op) |
| 19 | + if axis < 0 or axis >= x.dim(): |
| 20 | + raise TosaValueError( |
| 21 | + f"{op} axis {axis} is out of range for rank {x.dim()}", |
| 22 | + op=op, |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +def _reduce_shape(x: torch.Tensor, axis: int) -> list[int | torch.SymInt]: |
| 27 | + output_shape: list[int | torch.SymInt] = list(x.shape) |
| 28 | + output_shape[axis] = 1 |
| 29 | + return output_shape |
| 30 | + |
| 31 | + |
| 32 | +def _validate_bool_dtype(x: torch.Tensor, op: str) -> None: |
| 33 | + if x.dtype != torch.bool: |
| 34 | + raise TosaValueError(f"{op} requires bool input, got {x.dtype}", op=op) |
| 35 | + |
| 36 | + |
| 37 | +def _validate_float_integer_dtype(x: torch.Tensor, op: str) -> None: |
| 38 | + tosa_spec = get_context_spec() |
| 39 | + supported_int_dtypes = {torch.int8, torch.int16, torch.int32} |
| 40 | + supported_float_dtypes = {torch.float16, torch.float32} |
| 41 | + |
| 42 | + if tosa_spec.support_extension("int64"): |
| 43 | + supported_int_dtypes.add(torch.int64) |
| 44 | + if tosa_spec.support_extension("bf16"): |
| 45 | + supported_float_dtypes.add(torch.bfloat16) |
| 46 | + |
| 47 | + if x.dtype in supported_int_dtypes: |
| 48 | + if not tosa_spec.support_integer(): |
| 49 | + raise TosaValueError( |
| 50 | + f"TOSA spec {tosa_spec} doesn't support integer reductions", |
| 51 | + op=op, |
| 52 | + ) |
| 53 | + return |
| 54 | + |
| 55 | + if x.dtype in supported_float_dtypes: |
| 56 | + if not tosa_spec.support_float(): |
| 57 | + raise TosaValueError( |
| 58 | + f"TOSA spec {tosa_spec} doesn't support floating-point reductions", |
| 59 | + op=op, |
| 60 | + ) |
| 61 | + return |
| 62 | + |
| 63 | + raise TosaValueError(f"Unsupported dtype {x.dtype} for {op}", op=op) |
| 64 | + |
| 65 | + |
| 66 | +def _validate_reduce_sum_dtype(x: torch.Tensor) -> None: |
| 67 | + tosa_spec = get_context_spec() |
| 68 | + supported_int_dtypes = {torch.int32} |
| 69 | + supported_float_dtypes = {torch.float16, torch.float32} |
| 70 | + |
| 71 | + if tosa_spec.support_extension("int64"): |
| 72 | + supported_int_dtypes.add(torch.int64) |
| 73 | + if tosa_spec.support_extension("bf16"): |
| 74 | + supported_float_dtypes.add(torch.bfloat16) |
| 75 | + |
| 76 | + if x.dtype in supported_int_dtypes: |
| 77 | + if not tosa_spec.support_integer(): |
| 78 | + raise TosaValueError( |
| 79 | + f"TOSA spec {tosa_spec} doesn't support integer reductions", |
| 80 | + op="REDUCE_SUM", |
| 81 | + ) |
| 82 | + return |
| 83 | + |
| 84 | + if x.dtype in supported_float_dtypes: |
| 85 | + if not tosa_spec.support_float(): |
| 86 | + raise TosaValueError( |
| 87 | + f"TOSA spec {tosa_spec} doesn't support floating-point reductions", |
| 88 | + op="REDUCE_SUM", |
| 89 | + ) |
| 90 | + return |
| 91 | + |
| 92 | + raise TosaValueError( |
| 93 | + f"Unsupported dtype {x.dtype} for REDUCE_SUM", |
| 94 | + op="REDUCE_SUM", |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +def _validate_product_dtype(x: torch.Tensor, op: str) -> None: |
| 99 | + tosa_spec = get_context_spec() |
| 100 | + supported_dtypes = {torch.float16, torch.float32} |
| 101 | + if tosa_spec.support_extension("bf16"): |
| 102 | + supported_dtypes.add(torch.bfloat16) |
| 103 | + |
| 104 | + if x.dtype not in supported_dtypes: |
| 105 | + raise TosaValueError( |
| 106 | + f"{op} requires floating-point input, got {x.dtype}", op=op |
| 107 | + ) |
| 108 | + if not tosa_spec.support_float(): |
| 109 | + raise TosaValueError( |
| 110 | + f"TOSA spec {tosa_spec} doesn't support floating-point reductions", |
| 111 | + op=op, |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | +def _validate_nan_mode(nan_mode: str, op: str) -> None: |
| 116 | + if nan_mode not in ("PROPAGATE", "IGNORE"): |
| 117 | + raise TosaValueError( |
| 118 | + f"Invalid nan_mode {nan_mode}, must be PROPAGATE or IGNORE", |
| 119 | + op=op, |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +@register_fake_tosa_op( |
| 124 | + "REDUCE_ALL(Tensor input, *, int axis) -> Tensor", |
| 125 | + TosaSpecification.all_versions_and_profiles(), |
| 126 | +) |
| 127 | +def REDUCE_ALL(x: torch.Tensor, *, axis: int) -> torch.Tensor: |
| 128 | + _validate_axis(x, axis, "REDUCE_ALL") |
| 129 | + _validate_bool_dtype(x, "REDUCE_ALL") |
| 130 | + return torch.empty(size=_reduce_shape(x, axis), dtype=x.dtype) |
| 131 | + |
| 132 | + |
| 133 | +@register_fake_tosa_op( |
| 134 | + "REDUCE_ANY(Tensor input, *, int axis) -> Tensor", |
| 135 | + TosaSpecification.all_versions_and_profiles(), |
| 136 | +) |
| 137 | +def REDUCE_ANY(x: torch.Tensor, *, axis: int) -> torch.Tensor: |
| 138 | + _validate_axis(x, axis, "REDUCE_ANY") |
| 139 | + _validate_bool_dtype(x, "REDUCE_ANY") |
| 140 | + return torch.empty(size=_reduce_shape(x, axis), dtype=x.dtype) |
| 141 | + |
| 142 | + |
| 143 | +@register_fake_tosa_op( |
| 144 | + 'REDUCE_MAX(Tensor input, *, int axis, str nan_mode="PROPAGATE") -> Tensor', |
| 145 | + TosaSpecification.all_versions_and_profiles(), |
| 146 | +) |
| 147 | +def REDUCE_MAX( |
| 148 | + x: torch.Tensor, *, axis: int, nan_mode: str = "PROPAGATE" |
| 149 | +) -> torch.Tensor: |
| 150 | + _validate_axis(x, axis, "REDUCE_MAX") |
| 151 | + _validate_float_integer_dtype(x, "REDUCE_MAX") |
| 152 | + _validate_nan_mode(nan_mode, "REDUCE_MAX") |
| 153 | + return torch.empty(size=_reduce_shape(x, axis), dtype=x.dtype) |
| 154 | + |
| 155 | + |
| 156 | +@register_fake_tosa_op( |
| 157 | + 'REDUCE_MIN(Tensor input, *, int axis, str nan_mode="PROPAGATE") -> Tensor', |
| 158 | + TosaSpecification.all_versions_and_profiles(), |
| 159 | +) |
| 160 | +def REDUCE_MIN( |
| 161 | + x: torch.Tensor, *, axis: int, nan_mode: str = "PROPAGATE" |
| 162 | +) -> torch.Tensor: |
| 163 | + _validate_axis(x, axis, "REDUCE_MIN") |
| 164 | + _validate_float_integer_dtype(x, "REDUCE_MIN") |
| 165 | + _validate_nan_mode(nan_mode, "REDUCE_MIN") |
| 166 | + return torch.empty(size=_reduce_shape(x, axis), dtype=x.dtype) |
| 167 | + |
| 168 | + |
| 169 | +@register_fake_tosa_op( |
| 170 | + "REDUCE_PRODUCT(Tensor input, *, int axis) -> Tensor", |
| 171 | + TosaSpecification.all_versions_and_profiles(), |
| 172 | +) |
| 173 | +def REDUCE_PRODUCT(x: torch.Tensor, *, axis: int) -> torch.Tensor: |
| 174 | + _validate_axis(x, axis, "REDUCE_PRODUCT") |
| 175 | + _validate_product_dtype(x, "REDUCE_PRODUCT") |
| 176 | + return torch.empty(size=_reduce_shape(x, axis), dtype=x.dtype) |
| 177 | + |
| 178 | + |
| 179 | +@register_fake_tosa_op( |
| 180 | + "REDUCE_SUM(Tensor input, *, int axis) -> Tensor", |
| 181 | + TosaSpecification.all_versions_and_profiles(), |
| 182 | +) |
| 183 | +def REDUCE_SUM(x: torch.Tensor, *, axis: int) -> torch.Tensor: |
| 184 | + _validate_axis(x, axis, "REDUCE_SUM") |
| 185 | + _validate_reduce_sum_dtype(x) |
| 186 | + return torch.empty(size=_reduce_shape(x, axis), dtype=x.dtype) |
0 commit comments