|
| 1 | +# Copyright 2026 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 | +from typing import Set, Type |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.arm._passes import ArmOpTargetedPass |
| 10 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 11 | +from executorch.exir.pass_base import ExportPass |
| 12 | + |
| 13 | +edge_ops = (exir_ops.edge.aten.prelu.default,) |
| 14 | +torch_ops = (torch.ops.aten.prelu.default,) |
| 15 | + |
| 16 | + |
| 17 | +def _get_prelu_ops(op) -> tuple: |
| 18 | + if op in edge_ops: |
| 19 | + return ( |
| 20 | + exir_ops.edge.aten.clamp.default, |
| 21 | + exir_ops.edge.aten.mul.Tensor, |
| 22 | + exir_ops.edge.aten.add.Tensor, |
| 23 | + exir_ops.edge.aten.view_copy.default, |
| 24 | + ) |
| 25 | + if op in torch_ops: |
| 26 | + return ( |
| 27 | + torch.ops.aten.clamp.default, |
| 28 | + torch.ops.aten.mul.Tensor, |
| 29 | + torch.ops.aten.add.Tensor, |
| 30 | + torch.ops.aten.view_copy.default, |
| 31 | + ) |
| 32 | + raise RuntimeError(f"Can't get decomposition ops for op {op}") |
| 33 | + |
| 34 | + |
| 35 | +def _weight_shape(input_rank: int, weight_shape: torch.Size) -> tuple[int, ...] | None: |
| 36 | + weight_dims = tuple(int(dim) for dim in weight_shape) |
| 37 | + if len(weight_dims) == 0 or weight_dims == (1,): |
| 38 | + return None |
| 39 | + if len(weight_dims) != 1: |
| 40 | + raise RuntimeError(f"Unsupported PReLU weight shape: {weight_dims}") |
| 41 | + if input_rank < 2: |
| 42 | + raise RuntimeError( |
| 43 | + f"Per-channel PReLU weight requires input rank >= 2, got {input_rank}" |
| 44 | + ) |
| 45 | + return (1, weight_dims[0], *([1] * (input_rank - 2))) |
| 46 | + |
| 47 | + |
| 48 | +class DecomposePReLUPass(ArmOpTargetedPass): |
| 49 | + """Decompose PReLU into primitive TOSA-supported operations. |
| 50 | +
|
| 51 | + PReLU(x, weight) = max(0, x) + weight * min(0, x) |
| 52 | +
|
| 53 | + Example: |
| 54 | + %op1 = clamp(x,0,None) (equivalent to max(0,x)) |
| 55 | + %op2 = clamp(x,None,0) (equivalent to min(0,x)) |
| 56 | + %op3 = weight |
| 57 | + %op4 = mul(%op3,%op2) |
| 58 | + %op5 = add(%op1,%op4) |
| 59 | +
|
| 60 | + """ |
| 61 | + |
| 62 | + _passes_required_after: Set[Type[ExportPass]] = set() |
| 63 | + target_ops = edge_ops + torch_ops |
| 64 | + check_allowed_to_transform = True |
| 65 | + |
| 66 | + def call_operator(self, op, args, kwargs, meta): |
| 67 | + if ( |
| 68 | + op not in self.target_ops |
| 69 | + or not self.allowed_to_transform(meta) |
| 70 | + or self._is_quantized_meta(meta) |
| 71 | + ): |
| 72 | + return super().call_operator(op, args, kwargs, meta) |
| 73 | + |
| 74 | + x, weight = args |
| 75 | + clamp, mul, add, view = _get_prelu_ops(op) |
| 76 | + |
| 77 | + positive = super().call_operator( |
| 78 | + op=clamp, args=(x, 0, None), kwargs=kwargs, meta=meta, updated=True |
| 79 | + ) |
| 80 | + negative = super().call_operator( |
| 81 | + op=clamp, args=(x, None, 0), kwargs=kwargs, meta=meta, updated=True |
| 82 | + ) |
| 83 | + |
| 84 | + input_rank = len(x.data.shape) |
| 85 | + reshape_shape = _weight_shape(input_rank, weight.data.shape) |
| 86 | + if reshape_shape is not None: |
| 87 | + weight = super().call_operator( |
| 88 | + op=view, |
| 89 | + args=(weight, reshape_shape), |
| 90 | + kwargs={}, |
| 91 | + meta=meta, |
| 92 | + ) |
| 93 | + |
| 94 | + scaled_negative = super().call_operator( |
| 95 | + op=mul, |
| 96 | + args=(negative, weight), |
| 97 | + kwargs=kwargs, |
| 98 | + meta=meta, |
| 99 | + updated=True, |
| 100 | + ) |
| 101 | + return super().call_operator( |
| 102 | + op=add, |
| 103 | + args=(positive, scaled_negative), |
| 104 | + kwargs=kwargs, |
| 105 | + meta=meta, |
| 106 | + updated=True, |
| 107 | + ) |
0 commit comments