|
| 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 | +import torch |
| 7 | + |
| 8 | +from executorch.backends.arm._passes.remove_redundant_type_as_pass import ( |
| 9 | + RemoveRedundantTypeAsPass, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class ParameterTypeAs(torch.nn.Module): |
| 14 | + def __init__(self, dtype: torch.dtype) -> None: |
| 15 | + super().__init__() |
| 16 | + self.weight = torch.nn.Parameter(torch.ones(4, dtype=dtype)) |
| 17 | + |
| 18 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 19 | + return x * self.weight.type_as(x) |
| 20 | + |
| 21 | + |
| 22 | +class InputTypeAs(torch.nn.Module): |
| 23 | + def forward(self, x: torch.Tensor, other: torch.Tensor) -> torch.Tensor: |
| 24 | + return x.type_as(other) |
| 25 | + |
| 26 | + |
| 27 | +def _export(module: torch.nn.Module, inputs: tuple[torch.Tensor, ...]): |
| 28 | + return torch.export.export(module.eval(), inputs, strict=True).module() |
| 29 | + |
| 30 | + |
| 31 | +def _type_as_count(graph_module: torch.fx.GraphModule) -> int: |
| 32 | + return sum( |
| 33 | + node.target == torch.ops.aten.type_as.default |
| 34 | + for node in graph_module.graph.nodes |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +def test_removes_same_dtype_parameter_type_as() -> None: |
| 39 | + inputs = (torch.ones(1, 4),) |
| 40 | + graph_module = _export(ParameterTypeAs(torch.float32), inputs) |
| 41 | + assert _type_as_count(graph_module) == 1 |
| 42 | + |
| 43 | + result = RemoveRedundantTypeAsPass()(graph_module) |
| 44 | + |
| 45 | + assert result is not None |
| 46 | + assert result.modified |
| 47 | + assert _type_as_count(graph_module) == 0 |
| 48 | + |
| 49 | + |
| 50 | +def test_preserves_parameter_type_as_with_dtype_change() -> None: |
| 51 | + inputs = (torch.ones(1, 4, dtype=torch.float16),) |
| 52 | + graph_module = _export(ParameterTypeAs(torch.float32), inputs) |
| 53 | + |
| 54 | + result = RemoveRedundantTypeAsPass()(graph_module) |
| 55 | + |
| 56 | + assert result is not None |
| 57 | + assert not result.modified |
| 58 | + assert _type_as_count(graph_module) == 1 |
| 59 | + |
| 60 | + |
| 61 | +def test_preserves_parameter_type_as_with_device_change() -> None: |
| 62 | + inputs = (torch.ones(1, 4, device="meta"),) |
| 63 | + graph_module = _export(ParameterTypeAs(torch.float32), inputs) |
| 64 | + |
| 65 | + result = RemoveRedundantTypeAsPass()(graph_module) |
| 66 | + |
| 67 | + assert result is not None |
| 68 | + assert not result.modified |
| 69 | + assert _type_as_count(graph_module) == 1 |
| 70 | + |
| 71 | + |
| 72 | +def test_preserves_runtime_input_type_as() -> None: |
| 73 | + inputs = (torch.ones(1, 4), torch.ones(1, 4)) |
| 74 | + graph_module = _export(InputTypeAs(), inputs) |
| 75 | + |
| 76 | + result = RemoveRedundantTypeAsPass()(graph_module) |
| 77 | + |
| 78 | + assert result is not None |
| 79 | + assert not result.modified |
| 80 | + assert _type_as_count(graph_module) == 1 |
0 commit comments