|
| 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 | +from executorch.backends.arm.tosa.specification import get_context_spec |
| 8 | +from executorch.backends.transforms.aten_to_dialect_pass import ( |
| 9 | + AtenToDialectPass, |
| 10 | + DialectNodeSpec, |
| 11 | +) |
| 12 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 13 | +from torch.fx import Node |
| 14 | + |
| 15 | + |
| 16 | +# Each rewrite returns the TOSA dialect node spec for one supported ATen |
| 17 | +# activation op, preserving args unless TOSA requires normalized attributes. |
| 18 | +def rewrite_erf(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec: |
| 19 | + return DialectNodeSpec( |
| 20 | + exir_ops.backend.tosa.ERF.default, |
| 21 | + node.args, |
| 22 | + dict(node.kwargs), |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +def rewrite_sigmoid(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec: |
| 27 | + return DialectNodeSpec( |
| 28 | + exir_ops.backend.tosa.SIGMOID.default, |
| 29 | + node.args, |
| 30 | + dict(node.kwargs), |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def rewrite_tanh(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec: |
| 35 | + return DialectNodeSpec( |
| 36 | + exir_ops.backend.tosa.TANH.default, |
| 37 | + node.args, |
| 38 | + dict(node.kwargs), |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def _extract_dtype(node: Node) -> torch.dtype | None: |
| 43 | + value = node.meta.get("val") |
| 44 | + if isinstance(value, tuple): |
| 45 | + value = value[0] |
| 46 | + if isinstance(value, list): |
| 47 | + if not value: |
| 48 | + return None |
| 49 | + value = value[0] |
| 50 | + return getattr(value, "dtype", None) |
| 51 | + |
| 52 | + |
| 53 | +def _dtype_bounds(dtype: torch.dtype) -> tuple[int | float, int | float]: |
| 54 | + if dtype.is_floating_point: |
| 55 | + fp_info = torch.finfo(dtype) |
| 56 | + return fp_info.min, fp_info.max |
| 57 | + |
| 58 | + int_info = torch.iinfo(dtype) |
| 59 | + return int_info.min, int_info.max |
| 60 | + |
| 61 | + |
| 62 | +def _is_tosa_clamp_dtype_supported(dtype: torch.dtype) -> bool: |
| 63 | + tosa_spec = get_context_spec() |
| 64 | + |
| 65 | + if dtype == torch.int8: |
| 66 | + return tosa_spec.support_integer() |
| 67 | + |
| 68 | + if dtype == torch.int16: |
| 69 | + return tosa_spec.support_integer() and tosa_spec.support_extension("int16") |
| 70 | + |
| 71 | + if dtype in (torch.float16, torch.float32): |
| 72 | + return tosa_spec.support_float() |
| 73 | + |
| 74 | + if dtype == torch.bfloat16: |
| 75 | + return tosa_spec.support_float() and tosa_spec.support_extension("bf16") |
| 76 | + |
| 77 | + return False |
| 78 | + |
| 79 | + |
| 80 | +def _normalize_clamp_bound( |
| 81 | + bound, |
| 82 | + *, |
| 83 | + dtype: torch.dtype, |
| 84 | + default: int | float, |
| 85 | +) -> int | float | None: |
| 86 | + if bound is None: |
| 87 | + return default |
| 88 | + if isinstance(bound, bool): |
| 89 | + return None |
| 90 | + if dtype.is_floating_point: |
| 91 | + if isinstance(bound, (int, float)): |
| 92 | + return float(bound) |
| 93 | + return None |
| 94 | + if isinstance(bound, int): |
| 95 | + return bound |
| 96 | + return None |
| 97 | + |
| 98 | + |
| 99 | +def _get_min_max_arguments( |
| 100 | + node: Node, dtype: torch.dtype |
| 101 | +) -> tuple[int | float, int | float] | None: |
| 102 | + dtype_min, dtype_max = _dtype_bounds(dtype) |
| 103 | + min_val = _normalize_clamp_bound( |
| 104 | + node.args[1] if len(node.args) > 1 else node.kwargs.get("min"), |
| 105 | + dtype=dtype, |
| 106 | + default=dtype_min, |
| 107 | + ) |
| 108 | + max_val = _normalize_clamp_bound( |
| 109 | + node.args[2] if len(node.args) > 2 else node.kwargs.get("max"), |
| 110 | + dtype=dtype, |
| 111 | + default=dtype_max, |
| 112 | + ) |
| 113 | + if min_val is None or max_val is None: |
| 114 | + return None |
| 115 | + return min_val, max_val |
| 116 | + |
| 117 | + |
| 118 | +def rewrite_clamp(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec | None: |
| 119 | + dtype = _extract_dtype(node) |
| 120 | + if dtype is None or not _is_tosa_clamp_dtype_supported(dtype): |
| 121 | + return None |
| 122 | + |
| 123 | + min_max_args = _get_min_max_arguments(node, dtype) |
| 124 | + if min_max_args is None: |
| 125 | + return None |
| 126 | + |
| 127 | + return DialectNodeSpec( |
| 128 | + exir_ops.backend.tosa.CLAMP.default, |
| 129 | + (node.args[0], *min_max_args), |
| 130 | + ) |
0 commit comments