|
| 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 | +from typing import Tuple |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.arm._passes.decompose_layernorm_pass import ( |
| 10 | + DecomposeLayerNormPass, |
| 11 | +) |
| 12 | + |
| 13 | +from executorch.backends.arm.test.tester.test_pipeline import PassPipeline |
| 14 | + |
| 15 | +input_t = Tuple[torch.Tensor] # Input x |
| 16 | + |
| 17 | + |
| 18 | +class LayerNorm(torch.nn.Module): |
| 19 | + """ |
| 20 | + Basic layer_norm model using torch.nn.layer_norm layer |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self): |
| 24 | + super(LayerNorm, self).__init__() |
| 25 | + self.layer_norm = torch.nn.LayerNorm(10) |
| 26 | + |
| 27 | + def forward(self, x): |
| 28 | + x = self.layer_norm(x) |
| 29 | + return x |
| 30 | + |
| 31 | + def get_inputs(self) -> input_t: |
| 32 | + return (torch.rand(10),) |
| 33 | + |
| 34 | + |
| 35 | +def test_decompose_layernorm_tosa_MI(): |
| 36 | + module = LayerNorm() |
| 37 | + pipeline = PassPipeline[input_t]( |
| 38 | + module, |
| 39 | + module.get_inputs(), |
| 40 | + tosa_version="TOSA-0.80+MI", |
| 41 | + ops_before_pass={ |
| 42 | + "executorch_exir_dialects_edge__ops_aten_native_layer_norm_default": 1, |
| 43 | + }, |
| 44 | + ops_not_before_pass=[ |
| 45 | + "executorch_exir_dialects_edge__ops_aten_add_Tensor", |
| 46 | + "executorch_exir_dialects_edge__ops_aten_view_copy_default", |
| 47 | + "executorch_exir_dialects_edge__ops_aten_mul_Tensor", |
| 48 | + "executorch_exir_dialects_edge__ops_aten_full_default", |
| 49 | + "executorch_exir_dialects_edge__ops_aten_rsqrt_default", |
| 50 | + "executorch_exir_dialects_edge__ops_aten_var_correction", |
| 51 | + "executorch_exir_dialects_edge__ops_aten_sub_Tensor", |
| 52 | + "executorch_exir_dialects_edge__ops_aten_mean_dim", |
| 53 | + ], |
| 54 | + ops_after_pass={ |
| 55 | + "executorch_exir_dialects_edge__ops_aten_add_Tensor": 2, |
| 56 | + "executorch_exir_dialects_edge__ops_aten_view_copy_default": 2, |
| 57 | + "executorch_exir_dialects_edge__ops_aten_mul_Tensor": 2, |
| 58 | + "executorch_exir_dialects_edge__ops_aten_full_default": 1, |
| 59 | + "executorch_exir_dialects_edge__ops_aten_rsqrt_default": 1, |
| 60 | + "executorch_exir_dialects_edge__ops_aten_var_correction": 1, |
| 61 | + "executorch_exir_dialects_edge__ops_aten_sub_Tensor": 1, |
| 62 | + "executorch_exir_dialects_edge__ops_aten_mean_dim": 1, |
| 63 | + }, |
| 64 | + ops_not_after_pass=[ |
| 65 | + "executorch_exir_dialects_edge__ops_aten_expand_copy_default" |
| 66 | + ], |
| 67 | + pass_list=[DecomposeLayerNormPass], |
| 68 | + ) |
| 69 | + pipeline.run() |
0 commit comments