|
| 1 | +# Copyright 2025 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 | + |
| 8 | + |
| 9 | +import torch |
| 10 | +from executorch.backends.arm._passes import ArmPass |
| 11 | +from executorch.backends.arm._passes.arm_pass_utils import get_node_arg |
| 12 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 13 | + |
| 14 | + |
| 15 | +def _get_decorated_ops(op): |
| 16 | + if op in DecorateFp32toInt32CastingPass.targets: |
| 17 | + return ( |
| 18 | + exir_ops.edge.aten.full.default, |
| 19 | + exir_ops.edge.aten.ge.Tensor, |
| 20 | + exir_ops.edge.aten.floor.default, |
| 21 | + exir_ops.edge.aten.ceil.default, |
| 22 | + exir_ops.edge.aten.where.self, |
| 23 | + ) |
| 24 | + else: |
| 25 | + raise RuntimeError(f"Can't get decorated ops for op {op}") |
| 26 | + |
| 27 | + |
| 28 | +class DecorateFp32toInt32CastingPass(ArmPass): |
| 29 | + """ |
| 30 | + To lower pytorch fp32 -> int32 casting to TOSA, |
| 31 | + we need to transform the value with Ceil, Floor, and Where. |
| 32 | + Before: |
| 33 | + output = to_copy(x, dtype=torch.int32) |
| 34 | + After: |
| 35 | + %zero = full((1,), 0.0, dtype=torch.float32) |
| 36 | + is_non_negative = x >= %zero |
| 37 | + floor_x = floor(x) |
| 38 | + ceil_x = ceil(x) |
| 39 | + decorated_x = where(is_non_negative, floor_x, ceil_x) |
| 40 | + output = to_copy(decorated_x, dtype=torch.int32) |
| 41 | + """ |
| 42 | + |
| 43 | + targets = [ |
| 44 | + exir_ops.edge.aten._to_copy.default, |
| 45 | + exir_ops.edge.dim_order_ops._to_dim_order_copy.default, |
| 46 | + ] |
| 47 | + |
| 48 | + def call_operator(self, op, args, kwargs, meta): |
| 49 | + if op not in self.targets: |
| 50 | + return super().call_operator(op, args, kwargs, meta) |
| 51 | + |
| 52 | + input = get_node_arg(args, 0) |
| 53 | + input_dtype = input.node.meta["val"].dtype |
| 54 | + output_dtype = meta["val"].dtype |
| 55 | + |
| 56 | + if not (input_dtype == torch.float32 and output_dtype == torch.int32): |
| 57 | + return super().call_operator(op, args, kwargs, meta) |
| 58 | + |
| 59 | + op_full, op_ge, op_floor, op_ceil, op_where = _get_decorated_ops(op) |
| 60 | + |
| 61 | + zero = super().call_operator( |
| 62 | + op_full, |
| 63 | + args=((1,) * len(meta["val"].size()), 0.0), |
| 64 | + kwargs={"dtype": torch.float32}, |
| 65 | + meta=meta, |
| 66 | + updated=True, |
| 67 | + ) |
| 68 | + |
| 69 | + is_non_negative = super().call_operator( |
| 70 | + op_ge, (input, zero), {}, meta, updated=True |
| 71 | + ) |
| 72 | + floor_x = super().call_operator(op_floor, (input,), {}, meta, updated=True) |
| 73 | + ceil_x = super().call_operator(op_ceil, (input,), {}, meta, updated=True) |
| 74 | + decorated_x = super().call_operator( |
| 75 | + op_where, (is_non_negative, floor_x, ceil_x), {}, meta, updated=True |
| 76 | + ) |
| 77 | + |
| 78 | + return super().call_operator(op, (decorated_x,), kwargs, meta, updated=True) |
0 commit comments