|
| 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 | +import torch |
| 8 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 9 | +from executorch.exir.pass_base import ExportPass |
| 10 | + |
| 11 | +edge_sqrt_ops = (exir_ops.edge.aten.sqrt.default,) |
| 12 | +aten_sqrt_ops = ( |
| 13 | + torch.ops.aten.sqrt.default, |
| 14 | + torch.ops.aten.sqrt_.default, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def get_sqrt_decomposition(op) -> tuple: |
| 19 | + # TODO : "MLETORCH-863 : Replace current sqrt -> pow.Tensor_Scalar workaround with pow.Tensor_Tensor" |
| 20 | + if op in edge_sqrt_ops: |
| 21 | + return exir_ops.edge.aten.pow.Tensor_Scalar |
| 22 | + if op in aten_sqrt_ops: |
| 23 | + return torch.ops.aten.pow.Tensor_Scalar |
| 24 | + raise RuntimeError(f"Can't get sqrt decomposition for op {op}") |
| 25 | + |
| 26 | + |
| 27 | +class DecomposeSqrtPass(ExportPass): |
| 28 | + |
| 29 | + def call_operator(self, op, args, kwargs, meta): |
| 30 | + """ |
| 31 | + Decomposes `sqrt(x)` into `pow(x, 0.5)` for backend support. |
| 32 | + """ |
| 33 | + |
| 34 | + if op not in (edge_sqrt_ops + aten_sqrt_ops): |
| 35 | + return super().call_operator(op, args, kwargs, meta) |
| 36 | + |
| 37 | + pow_op = get_sqrt_decomposition(op) |
| 38 | + |
| 39 | + return super().call_operator(pow_op, (args[0], 0.5), {}, meta) |
0 commit comments