|
| 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 | +import torch |
| 9 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 10 | +from executorch.exir.pass_base import ExportPass |
| 11 | + |
| 12 | +edge_div_mode_ops = (exir_ops.edge.aten.div.Tensor_mode,) |
| 13 | +aten_div_mode_ops = (torch.ops.aten.div.Tensor_mode,) |
| 14 | + |
| 15 | +edge_unary = { |
| 16 | + "div": exir_ops.edge.aten.div.Tensor, |
| 17 | + "floor": exir_ops.edge.aten.floor.default, |
| 18 | + "ceil": exir_ops.edge.aten.ceil.default, |
| 19 | + "full": exir_ops.edge.aten.full.default, |
| 20 | + "lt": exir_ops.edge.aten.lt.Tensor, |
| 21 | + "where": exir_ops.edge.aten.where.self, |
| 22 | +} |
| 23 | + |
| 24 | +aten_unary = { |
| 25 | + "div": torch.ops.aten.div.Tensor, |
| 26 | + "floor": torch.ops.aten.floor.default, |
| 27 | + "ceil": torch.ops.aten.ceil.default, |
| 28 | + "full": torch.ops.aten.full.default, |
| 29 | + "lt": torch.ops.aten.lt.Tensor, |
| 30 | + "where": torch.ops.aten.where.self, |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def _get_opset(op): |
| 35 | + if op in edge_div_mode_ops: |
| 36 | + return edge_unary |
| 37 | + if op in aten_div_mode_ops: |
| 38 | + return aten_unary |
| 39 | + raise RuntimeError(f"div.Tensor_mode not supported for op {op}") |
| 40 | + |
| 41 | + |
| 42 | +class DecomposeDivTensorModePass(ExportPass): |
| 43 | + """ |
| 44 | + Rewrites aten.div.Tensor_mode into |
| 45 | +
|
| 46 | + rounding_mode=None -> div(a, b) |
| 47 | + rounding_mode='floor' -> floor(div(a, b)) |
| 48 | + rounding_mode='trunc' -> where(div(a,b) < 0, ceil(div(a,b)), floor(div(a,b))) |
| 49 | + """ |
| 50 | + |
| 51 | + def call_operator(self, op, args, kwargs, meta): |
| 52 | + if op not in (edge_div_mode_ops + aten_div_mode_ops): |
| 53 | + return super().call_operator(op, args, kwargs, meta) |
| 54 | + |
| 55 | + opset = _get_opset(op) |
| 56 | + |
| 57 | + a, b = args[0], args[1] |
| 58 | + rounding_mode = kwargs.get("rounding_mode", None) |
| 59 | + if rounding_mode is None and len(args) > 2: |
| 60 | + rounding_mode = args[2] |
| 61 | + |
| 62 | + q = super().call_operator(opset["div"], (a, b), {}, meta) |
| 63 | + |
| 64 | + if rounding_mode is None: |
| 65 | + return q |
| 66 | + |
| 67 | + if rounding_mode == "floor": |
| 68 | + return super().call_operator(opset["floor"], (q,), {}, meta) |
| 69 | + |
| 70 | + if rounding_mode == "trunc": |
| 71 | + zero = super().call_operator( |
| 72 | + opset["full"], |
| 73 | + args=((1,) * len(meta["val"].size()), 0.0), |
| 74 | + kwargs={"dtype": torch.float32}, |
| 75 | + meta=meta, |
| 76 | + ) |
| 77 | + lt0 = self.call_operator(opset["lt"], (q, zero), {}, meta) |
| 78 | + ceilq = self.call_operator(opset["ceil"], (q,), {}, meta) |
| 79 | + floorq = self.call_operator(opset["floor"], (q,), {}, meta) |
| 80 | + return self.call_operator(opset["where"], (lt0, ceilq, floorq), {}, meta) |
| 81 | + |
| 82 | + raise RuntimeError( |
| 83 | + f"Unsupported rounding_mode for div.Tensor_mode: {rounding_mode!r}" |
| 84 | + ) |
0 commit comments