|
| 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 | +Declare operator support for ``edge.aten.gather`` in TOSA. |
| 7 | +
|
| 8 | +This support check matches the subset accepted by CanonicalizeGatherPass: |
| 9 | +
|
| 10 | +- target: exir_ops.edge.aten.gather.default |
| 11 | +- args: exactly (x, dim, index) (i.e. len(node.args) == 3) |
| 12 | +- dim must be 1 or -1 |
| 13 | +- x must be rank-2 |
| 14 | +- index must be rank-2 |
| 15 | +- index dtype must be int32 |
| 16 | +- batch dim must match: x.shape[0] == index.shape[0] |
| 17 | +
|
| 18 | +Dtype gating is capability-based: |
| 19 | +
|
| 20 | +- int8/int16/int32 values require INT profile. |
| 21 | +- bool values require INT profile (handled via casts: bool -> int8 -> bool). |
| 22 | +- fp16/fp32 values are supported via FP profile directly, or via quantization |
| 23 | + when running under an INT profile. |
| 24 | +
|
| 25 | +Note: |
| 26 | +- CanonicalizeGatherPass reshapes values to [N, K, 1] and keeps indices as [N, W], |
| 27 | + then lowers via the TOSA gather dialect. |
| 28 | +""" |
| 29 | + |
| 30 | +import torch |
| 31 | +import torch.fx as fx |
| 32 | + |
| 33 | +from executorch.backends.arm.operator_support.tosa_supported_operators import ( |
| 34 | + register_tosa_support_check, |
| 35 | + SupportedTOSAOperatorCheck, |
| 36 | +) |
| 37 | +from executorch.backends.arm.tosa import TosaSpecification |
| 38 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 39 | + |
| 40 | + |
| 41 | +@register_tosa_support_check |
| 42 | +class GatherSupported(SupportedTOSAOperatorCheck): |
| 43 | + """Provide TOSA support check for ``edge.aten.gather``.""" |
| 44 | + |
| 45 | + targets = [exir_ops.edge.aten.gather.default] |
| 46 | + |
| 47 | + tosa_specs = [ |
| 48 | + TosaSpecification.create_from_string("TOSA-1.0+INT"), |
| 49 | + TosaSpecification.create_from_string("TOSA-1.0+FP"), |
| 50 | + ] |
| 51 | + |
| 52 | + def is_node_tosa_supported( |
| 53 | + self, node: fx.Node, tosa_spec: TosaSpecification |
| 54 | + ) -> bool: # type: ignore[override, misc] |
| 55 | + if len(node.args) != 3: |
| 56 | + self.reporter.report_reject( |
| 57 | + node, |
| 58 | + f"{node.target}: expected 3 args (x, dim, index), got " |
| 59 | + f"{len(node.args)}.", |
| 60 | + ) |
| 61 | + return False |
| 62 | + |
| 63 | + x_arg, dim, index_arg = node.args[0], node.args[1], node.args[2] |
| 64 | + x_val = x_arg.meta["val"] # type: ignore[union-attr] |
| 65 | + index_val = index_arg.meta["val"] # type: ignore[union-attr] |
| 66 | + |
| 67 | + x_shape = tuple(x_val.shape) |
| 68 | + index_shape = tuple(index_val.shape) |
| 69 | + |
| 70 | + # ---- index dtype ---- |
| 71 | + if index_val.dtype != torch.int32: |
| 72 | + self.reporter.report_reject( |
| 73 | + node, |
| 74 | + f"{node.target}: index dtype {index_val.dtype} not supported; " |
| 75 | + "expected int32.", |
| 76 | + ) |
| 77 | + return False |
| 78 | + |
| 79 | + # ---- dim + rank ---- |
| 80 | + if not ( |
| 81 | + (dim == 1 or dim == -1) and len(x_shape) == 2 and len(index_shape) == 2 |
| 82 | + ): |
| 83 | + self.reporter.report_reject( |
| 84 | + node, |
| 85 | + f"{node.target}: unsupported dim/rank; got {dim=}, " |
| 86 | + f"x_rank={len(x_shape)}, index_rank={len(index_shape)}; " |
| 87 | + "supported: dim in {1, -1} with rank-2 x and rank-2 index.", |
| 88 | + ) |
| 89 | + return False |
| 90 | + |
| 91 | + # ---- batch dim compatibility ---- |
| 92 | + if x_shape[0] != index_shape[0]: |
| 93 | + self.reporter.report_reject( |
| 94 | + node, |
| 95 | + f"{node.target}: batch mismatch {x_shape[0]=} vs {index_shape[0]=}.", |
| 96 | + ) |
| 97 | + return False |
| 98 | + |
| 99 | + # ---- values dtype ---- |
| 100 | + values_dtype = x_val.dtype |
| 101 | + # ints (and bool via casts) require INT profile |
| 102 | + if values_dtype in (torch.bool, torch.int8, torch.int16, torch.int32): |
| 103 | + if not tosa_spec.support_integer(): |
| 104 | + self.reporter.report_reject( |
| 105 | + node, |
| 106 | + f"{node.target}: dtype {values_dtype} requires INT profile.", |
| 107 | + ) |
| 108 | + return False |
| 109 | + # fp16/fp32: either FP profile, or INT profile (via quantization) |
| 110 | + elif values_dtype in (torch.float16, torch.float32): |
| 111 | + if not (tosa_spec.support_float() or tosa_spec.support_integer()): |
| 112 | + self.reporter.report_reject( |
| 113 | + node, |
| 114 | + f"{node.target}: dtype {values_dtype} requires FP profile or " |
| 115 | + "INT profile (with quantization).", |
| 116 | + ) |
| 117 | + return False |
| 118 | + else: |
| 119 | + self.reporter.report_reject( |
| 120 | + node, |
| 121 | + f"{node.target}: unsupported values dtype {values_dtype}; " |
| 122 | + "expected bool/int8/int16/int32/float16/float32.", |
| 123 | + ) |
| 124 | + return False |
| 125 | + |
| 126 | + return True |
0 commit comments