|
| 1 | +# Copyright 2024 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 | +# pyre-unsafe |
| 7 | +import logging |
| 8 | + |
| 9 | +import torch |
| 10 | + |
| 11 | +import torch.fx as fx |
| 12 | + |
| 13 | +from executorch.backends.arm.operator_support.tosa_supported_operators import ( |
| 14 | + register_tosa_support_check, |
| 15 | + SupportedTOSAOperatorCheck, |
| 16 | +) |
| 17 | +from executorch.backends.arm.tosa_specification import TosaSpecification |
| 18 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +@register_tosa_support_check |
| 24 | +class ToCopySupported(SupportedTOSAOperatorCheck): |
| 25 | + targets = [exir_ops.edge.aten._to_copy.default] |
| 26 | + |
| 27 | + tosa_specs = [ |
| 28 | + TosaSpecification.create_from_string("TOSA-0.80.0+BI"), |
| 29 | + TosaSpecification.create_from_string("TOSA-0.80.0+MI"), |
| 30 | + ] |
| 31 | + |
| 32 | + SupportedTypeDict = dict[torch.dtype, list[torch.dtype]] |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def _merge_supported_types( |
| 36 | + dtypes1: SupportedTypeDict, dtypes2: SupportedTypeDict |
| 37 | + ) -> SupportedTypeDict: |
| 38 | + merged_dtypes = dtypes1 |
| 39 | + for k, v in dtypes2.items(): |
| 40 | + merged_dtypes[k] = merged_dtypes.get(k, []) + v |
| 41 | + return merged_dtypes |
| 42 | + |
| 43 | + SUPPORTED_INT_TYPES: SupportedTypeDict = { |
| 44 | + torch.bool: [torch.int8, torch.int16, torch.int32], |
| 45 | + torch.int8: [torch.bool, torch.int16, torch.int32], |
| 46 | + torch.int16: [torch.bool, torch.int8, torch.int32], |
| 47 | + torch.int32: [torch.bool, torch.int8, torch.int16], |
| 48 | + } |
| 49 | + SUPPORTED_FLOAT_TYPES: SupportedTypeDict = { |
| 50 | + torch.int8: [torch.float16, torch.bfloat16, torch.float32], |
| 51 | + torch.int16: [torch.float16, torch.bfloat16, torch.float32], |
| 52 | + torch.int32: [torch.float16, torch.bfloat16, torch.float32], |
| 53 | + torch.bfloat16: [torch.int8, torch.int16, torch.int32, torch.float32], |
| 54 | + torch.float16: [torch.int8, torch.int16, torch.int32, torch.float32], |
| 55 | + torch.float32: [ |
| 56 | + torch.int8, |
| 57 | + torch.int16, |
| 58 | + torch.int32, |
| 59 | + torch.bfloat16, |
| 60 | + torch.float16, |
| 61 | + ], |
| 62 | + } |
| 63 | + ALL_SUPPORTED_TYPES = _merge_supported_types( |
| 64 | + SUPPORTED_INT_TYPES, SUPPORTED_FLOAT_TYPES |
| 65 | + ) |
| 66 | + POSSIBLE_TYPE_CONVERSIONS = {torch.int64: torch.int32} |
| 67 | + |
| 68 | + def is_node_supported(self, node: fx.Node, tosa_spec: TosaSpecification) -> bool: |
| 69 | + assert node.target in self.targets |
| 70 | + |
| 71 | + if tosa_spec not in self.tosa_specs: |
| 72 | + return False |
| 73 | + |
| 74 | + assert tosa_spec.support_integer() |
| 75 | + supported_dtypes = ( |
| 76 | + self.ALL_SUPPORTED_TYPES |
| 77 | + if tosa_spec.support_float() |
| 78 | + else self.SUPPORTED_INT_TYPES |
| 79 | + ) |
| 80 | + # Take into account possible type conversions |
| 81 | + supported_dtypes.update( |
| 82 | + (k, supported_dtypes[v]) |
| 83 | + for k, v in self.POSSIBLE_TYPE_CONVERSIONS.items() |
| 84 | + if v in supported_dtypes |
| 85 | + ) |
| 86 | + |
| 87 | + # Check input type |
| 88 | + assert len(node.all_input_nodes) == 1 |
| 89 | + input_val = node.all_input_nodes[0].meta["val"] |
| 90 | + assert isinstance(input_val, torch._subclasses.FakeTensor) |
| 91 | + input_dtype = input_val.dtype |
| 92 | + if input_dtype not in supported_dtypes: |
| 93 | + logger.info( |
| 94 | + f"Input dtype {input_val.dtype} is not supported in " |
| 95 | + f"{node.target.name()}." |
| 96 | + ) |
| 97 | + return False |
| 98 | + |
| 99 | + # Check output type |
| 100 | + output_val = node.meta["val"] |
| 101 | + assert isinstance(output_val, torch._subclasses.FakeTensor) |
| 102 | + if output_val.dtype not in supported_dtypes[input_dtype]: |
| 103 | + logger.info( |
| 104 | + f"Output dtype {output_val.dtype} is not supported in " |
| 105 | + f"{node.target.name()} for input dtype {input_dtype}. " |
| 106 | + f"Supported output types: " |
| 107 | + f"{''.join(str(t) for t in supported_dtypes[input_dtype])}" |
| 108 | + ) |
| 109 | + return False |
| 110 | + |
| 111 | + # Check memory format |
| 112 | + if "memory_format" in node.kwargs: |
| 113 | + if node.kwargs["memory_format"] in (torch.preserve_format,): |
| 114 | + logger.info( |
| 115 | + f"Argument 'memory_format' is not supported for " |
| 116 | + f"{node.target.name()} right now." |
| 117 | + ) |
| 118 | + return False |
| 119 | + |
| 120 | + return True |
0 commit comments