|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | + |
| 9 | +# This file contains all the functions that decompose one op into simpler ops in the |
| 10 | +# graph. The functions decomposing ops for models deployed with Jarvis are grouped |
| 11 | +# together in class 'DecomposeOpsInGraph'. Some examples of functions in the class are |
| 12 | +# 1. functions that decompose an ATen gelu op into an equivalent series of simpler ops |
| 13 | + |
| 14 | +# pyre-strict |
| 15 | + |
| 16 | +from typing import Dict |
| 17 | + |
| 18 | +from executorch.backends.cadence.aot.pass_utils import ( |
| 19 | + CadencePassAttribute, |
| 20 | + register_cadence_pass, |
| 21 | +) |
| 22 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 23 | +from executorch.exir.dialects.edge._ops import EdgeOpOverload |
| 24 | +from executorch.exir.pass_base import ExportPass, NodeMetadata, ProxyValue |
| 25 | +from torch.fx.node import Argument |
| 26 | + |
| 27 | + |
| 28 | +@register_cadence_pass(CadencePassAttribute(opt_level=0)) |
| 29 | +class DecomposeAtenApproxGeluPass(ExportPass): |
| 30 | + """ |
| 31 | + Decompose the aten gelu op with an approximate arg to a series of simpler ops |
| 32 | + """ |
| 33 | + |
| 34 | + def call_operator( |
| 35 | + self, |
| 36 | + op: EdgeOpOverload, |
| 37 | + args: tuple[Argument, ...], |
| 38 | + kwargs: Dict[str, Argument], |
| 39 | + meta: NodeMetadata, |
| 40 | + ) -> ProxyValue: |
| 41 | + # compute the approximate gelu (0.7978845608028654 is sqrt(2 / pi)) |
| 42 | + # as 0.5 * x * (1 + torch.tanh(0.7978845608028654 * ( x + 0.044715 * x^3))) |
| 43 | + |
| 44 | + # Get 0.5 * x |
| 45 | + half = super().call_operator( |
| 46 | + exir_ops.edge.aten.mul.Tensor, |
| 47 | + (args[0], 0.5), |
| 48 | + {}, |
| 49 | + meta, |
| 50 | + ) |
| 51 | + |
| 52 | + scaled = super().call_operator( |
| 53 | + exir_ops.edge.aten.mul.Tensor, |
| 54 | + (args[0], 0.044715), |
| 55 | + {}, |
| 56 | + meta, |
| 57 | + ) |
| 58 | + |
| 59 | + # Get x^2 (note that we use mul.Tensor twice instead of pow.Tensor because |
| 60 | + # it is much more efficient on DSP backends) |
| 61 | + scaled_square = super().call_operator( |
| 62 | + exir_ops.edge.aten.mul.Tensor, |
| 63 | + (scaled, args[0]), |
| 64 | + {}, |
| 65 | + meta, |
| 66 | + ) |
| 67 | + |
| 68 | + # Get x^3 |
| 69 | + scaled_cubed = super().call_operator( |
| 70 | + exir_ops.edge.aten.mul.Tensor, |
| 71 | + (scaled_square, args[0]), |
| 72 | + {}, |
| 73 | + meta, |
| 74 | + ) |
| 75 | + |
| 76 | + # Get x + 0.044715 * x^3 |
| 77 | + inner_sum = super().call_operator( |
| 78 | + exir_ops.edge.aten.add.Tensor, |
| 79 | + (scaled_cubed, args[0]), |
| 80 | + {}, |
| 81 | + meta, |
| 82 | + ) |
| 83 | + |
| 84 | + # Get 0.7978845608028654 * ( x + 0.044715 * x^3) |
| 85 | + scaled_sum = super().call_operator( |
| 86 | + exir_ops.edge.aten.mul.Tensor, |
| 87 | + (inner_sum, 0.7978845608028654), |
| 88 | + {}, |
| 89 | + meta, |
| 90 | + ) |
| 91 | + |
| 92 | + # Get torch.tanh(0.7978845608028654 * ( x + 0.044715 * x^3)) |
| 93 | + tanh = super().call_operator( |
| 94 | + exir_ops.edge.aten.tanh.default, |
| 95 | + (scaled_sum,), |
| 96 | + {}, |
| 97 | + meta, |
| 98 | + ) |
| 99 | + |
| 100 | + # Get 1 + torch.tanh(0.79788456 * ( x + 0.044715 * x^3)) |
| 101 | + # TODO(): Check why this is not working properly with integer values (e.g. 1 instead of 1.) |
| 102 | + outer_sum = super().call_operator( |
| 103 | + exir_ops.edge.aten.add.Tensor, |
| 104 | + (tanh, 1.0), |
| 105 | + {}, |
| 106 | + meta, |
| 107 | + ) |
| 108 | + |
| 109 | + # Return the final result |
| 110 | + return super().call_operator( |
| 111 | + exir_ops.edge.aten.mul.Tensor, |
| 112 | + (half, outer_sum), |
| 113 | + {}, |
| 114 | + meta, |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +# This class encapsulates all the functions that decompose one op in the graph. |
| 119 | +class CadenceDecomposeOpsInGraph: |
| 120 | + passes = [ |
| 121 | + DecomposeAtenApproxGeluPass, |
| 122 | + ] |
0 commit comments