Skip to content

Commit 3ffd297

Browse files
committed
Arm backend: Remove redundant type_as calls
type_as converts a tensor to match another tensor's dtype and device. When these already match, the call is a no-op but still creates a runtime dependency that prevents constant folding. Remove these calls for parameters and buffers while preserving those that change dtype or device. Signed-off-by: Per Held <per.held@arm.com> Change-Id: Ib30ffd07eff7e6fe1efa2aa80771e5535d933f86
1 parent bcf01c9 commit 3ffd297

5 files changed

Lines changed: 137 additions & 1 deletion

File tree

‎backends/arm/_passes/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
from .remove_permutes_around_elementwise_tosa_ops import ( # noqa
199199
RemovePermutesAroundElementwiseTosaOps,
200200
)
201+
from .remove_redundant_type_as_pass import RemoveRedundantTypeAsPass # noqa
201202
from .remove_safe_softmax_guard_pass import RemoveSafeSoftmaxGuardPass # noqa
202203
from .replace_scalar_with_tensor_pass import ( # noqa
203204
ReplaceScalarWithTensorByProfilePass,

‎backends/arm/_passes/arm_pass_manager.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
RemoveGetItemPass,
158158
RemoveGraphAssertsPass,
159159
RemoveNoopPass,
160+
RemoveRedundantTypeAsPass,
160161
RemoveSafeSoftmaxGuardPass,
161162
ReplaceInfAndLimitValuesPass,
162163
ReplaceScalarWithTensorByProfilePass,
@@ -783,6 +784,7 @@ def transform_for_annotation_pipeline(self, graph_module: GraphModule):
783784
with self._tosa_context(graph_module):
784785
# Preprocessing passes
785786
self.add_pass(RemoveGraphAssertsPass(tfa_pass=True))
787+
self.add_pass(RemoveRedundantTypeAsPass())
786788
self.add_pass(ConstantFoldingPass())
787789

788790
# Transformation passes (pre scalar -> tensor)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
10+
from executorch.backends.arm._passes import ArmPass
11+
from executorch.exir.pass_base import ExportPass, PassResult
12+
13+
14+
class RemoveRedundantTypeAsPass(ArmPass):
15+
"""Remove no-op type_as calls on parameters and buffers."""
16+
17+
_passes_required_after: Set[Type[ExportPass]] = set()
18+
19+
def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
20+
modified = False
21+
for node in list(graph_module.graph.nodes):
22+
if (
23+
node.op != "call_function"
24+
or node.target != torch.ops.aten.type_as.default
25+
):
26+
continue
27+
28+
source = node.args[0]
29+
if not isinstance(source, torch.fx.Node) or source.op != "get_attr":
30+
continue
31+
32+
source_value = source.meta.get("val")
33+
output_value = node.meta.get("val")
34+
if not isinstance(source_value, torch.Tensor) or not isinstance(
35+
output_value, torch.Tensor
36+
):
37+
continue
38+
if (
39+
source_value.dtype != output_value.dtype
40+
or source_value.device != output_value.device
41+
):
42+
continue
43+
44+
node.replace_all_uses_with(source)
45+
graph_module.graph.erase_node(node)
46+
modified = True
47+
48+
if modified:
49+
graph_module.graph.eliminate_dead_code()
50+
graph_module.graph.lint()
51+
graph_module.recompile()
52+
53+
return PassResult(graph_module, modified)

‎backends/arm/test/models/test_swin2sr_arm.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"executorch_exir_dialects_edge__ops_aten_clamp_default": 4,
4141
"executorch_exir_dialects_edge__ops_aten_expand_copy_default": 4,
4242
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 5,
43-
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 6,
43+
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 5,
4444
}
4545
swin2sr_vgf_quant_lowered_outer_graph_ops = {
4646
"torch.ops.higher_order.executorch_call_delegate": 1,
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)