|
| 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.arm_pass import ArmPass |
| 10 | +from executorch.backends.arm.tosa.mapping import TosaSpecialDtype |
| 11 | +from executorch.exir import to_edge |
| 12 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 13 | +from executorch.exir.pass_base import ExportPass |
| 14 | + |
| 15 | + |
| 16 | +class _EmitShapePass(ArmPass): |
| 17 | + @property |
| 18 | + def _passes_required_after(self) -> Set[Type[ExportPass]]: |
| 19 | + return set() |
| 20 | + |
| 21 | + def call_operator(self, op, args, kwargs, meta, updated: bool | None = False): |
| 22 | + # Inject a CONST_SHAPE once, then proceed normally. |
| 23 | + print(f"op: {op}") |
| 24 | + if op == exir_ops.edge.aten.add.Tensor: |
| 25 | + print("Injecting CONST_SHAPE") |
| 26 | + shape = super().call_shape_operator( |
| 27 | + exir_ops.backend.tosa.CONST_SHAPE.default, |
| 28 | + ([1, 3],), |
| 29 | + {}, |
| 30 | + meta, |
| 31 | + True, |
| 32 | + ) |
| 33 | + return shape |
| 34 | + else: |
| 35 | + return super().call_operator(op, args, kwargs, meta, updated) |
| 36 | + |
| 37 | + |
| 38 | +def test_const_shape_injects_meta_no_target(): |
| 39 | + class M(torch.nn.Module): |
| 40 | + def forward(self, x): |
| 41 | + return x + 1 |
| 42 | + |
| 43 | + exported = torch.export.export(M(), (torch.randn(1),)) |
| 44 | + edge = to_edge(exported).transform([_EmitShapePass()]) |
| 45 | + |
| 46 | + gm = edge.exported_program().graph_module |
| 47 | + |
| 48 | + const_shape_nodes = [ |
| 49 | + n |
| 50 | + for n in gm.graph.nodes |
| 51 | + if n.target == exir_ops.backend.tosa.CONST_SHAPE.default |
| 52 | + ] |
| 53 | + |
| 54 | + assert const_shape_nodes |
| 55 | + for n in const_shape_nodes: |
| 56 | + assert n.meta[TosaSpecialDtype.meta_key()] == TosaSpecialDtype.SHAPE |
0 commit comments