|
| 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 | +import torch |
| 7 | +from executorch.backends.arm._passes import ArmPass |
| 8 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 9 | +from executorch.exir.dialects.edge._ops import EdgeOpOverload |
| 10 | +from torch._ops import OpOverload |
| 11 | + |
| 12 | + |
| 13 | +Op = OpOverload | EdgeOpOverload |
| 14 | + |
| 15 | + |
| 16 | +def _get_round_decomposition_ops(op) -> tuple[Op, Op, Op, Op, Op, Op, Op]: |
| 17 | + """ |
| 18 | + Returns the (full_op, ge_op, add_op, sub_op, floor_op, ceil_op, where_op) for the |
| 19 | + given round operation. The ops depend on whether the round op is an aten or edge op. |
| 20 | + """ |
| 21 | + if op == exir_ops.edge.aten.round.default: |
| 22 | + return ( |
| 23 | + exir_ops.edge.aten.full.default, |
| 24 | + exir_ops.edge.aten.ge.Tensor, |
| 25 | + exir_ops.edge.aten.add.Scalar, |
| 26 | + exir_ops.edge.aten.sub.Scalar, |
| 27 | + exir_ops.edge.aten.floor.default, |
| 28 | + exir_ops.edge.aten.ceil.default, |
| 29 | + exir_ops.edge.aten.where.self, |
| 30 | + ) |
| 31 | + elif op == torch.ops.aten.round.default: |
| 32 | + return ( |
| 33 | + torch.ops.aten.full.default, |
| 34 | + torch.ops.aten.ge.Tensor, |
| 35 | + torch.ops.aten.add.Scalar, |
| 36 | + torch.ops.aten.sub.Scalar, |
| 37 | + torch.ops.aten.floor.default, |
| 38 | + torch.ops.aten.ceil.default, |
| 39 | + torch.ops.aten.where.self, |
| 40 | + ) |
| 41 | + raise RuntimeError(f"Can't get round decomposition ops for op {op}") |
| 42 | + |
| 43 | + |
| 44 | +class DecomposeRoundPass(ArmPass): |
| 45 | + """ |
| 46 | + For inputs >= 0, round(x) is equivalent to floor(x + 0.5), and for inputs < 0, |
| 47 | + round(x) is equivalent to ceil(x - 0.5). This pass decomposes the round operation into |
| 48 | + a sequence of more primitive operations. |
| 49 | + Example: |
| 50 | + %zero = full((1,), 0.0, dtype=torch.float32) |
| 51 | + %is_non_negative = ge(x, %zero) |
| 52 | + %plus_half = add(x, 0.5) |
| 53 | + %minus_half = sub(x, 0.5) |
| 54 | + %floor = floor(%plus_half) |
| 55 | + %ceil = ceil(%minus_half) |
| 56 | + %result = where(%is_non_negative, %floor, %ceil) |
| 57 | + """ |
| 58 | + |
| 59 | + def call_operator(self, op, args, kwargs, meta, updated=False): |
| 60 | + if op not in (exir_ops.edge.aten.round.default, torch.ops.aten.round.default): |
| 61 | + return super().call_operator(op, args, kwargs, meta, updated) |
| 62 | + x = args[0] |
| 63 | + full, ge, add, sub, floor, ceil, where = _get_round_decomposition_ops(op) |
| 64 | + zero = super().call_operator( |
| 65 | + full, |
| 66 | + args=((1,), 0.0), |
| 67 | + kwargs={"dtype": torch.float32}, |
| 68 | + meta=meta, |
| 69 | + updated=True, |
| 70 | + ) |
| 71 | + is_non_negative = super().call_operator( |
| 72 | + ge, (x, zero), kwargs, meta, updated=True |
| 73 | + ) |
| 74 | + plus_half = super().call_operator(add, (x, 0.5), kwargs, meta, updated=True) |
| 75 | + minus_half = super().call_operator(sub, (x, 0.5), kwargs, meta, updated=True) |
| 76 | + floor = super().call_operator(floor, (plus_half,), kwargs, meta, updated=True) |
| 77 | + ceil = super().call_operator(ceil, (minus_half,), kwargs, meta, updated=True) |
| 78 | + return super().call_operator( |
| 79 | + where, |
| 80 | + (is_non_negative, floor, ceil), |
| 81 | + kwargs, |
| 82 | + meta, |
| 83 | + updated=True, |
| 84 | + ) |
0 commit comments