Skip to content

Commit e4344c9

Browse files
authored
Arm backend: Fix Argmax for VGF backend (pytorch#21735)
Fix Argmax for VGF backend cc @digantdesai @freddan80 @per @zingo @oscarandersson8218 @mansnils @Sebastian-Larsson @robell @rascani Signed-off-by: Elena Zhelezina <elena.zhelezina@arm.com>
1 parent bd89c17 commit e4344c9

2 files changed

Lines changed: 65 additions & 45 deletions

File tree

‎backends/arm/operator_support/tosa_supported_operators.py‎

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -374,37 +374,45 @@ def _negative_checks(
374374
return checks
375375

376376

377+
_ARGMAX_OPS = (
378+
torch.ops.aten.argmax.default,
379+
exir_ops.edge.aten.argmax.default,
380+
)
381+
382+
_TO_DIM_ORDER_COPY_OPS = (
383+
torch.ops.dim_order_ops._to_dim_order_copy.default,
384+
exir_ops.edge.dim_order_ops._to_dim_order_copy.default,
385+
)
386+
387+
388+
def _argmax_all_users_cast_to_int32(node: fx.Node) -> bool:
389+
# TOSA ARGMAX produces int32 indices. This is only a faithful lowering
390+
# when every user immediately narrows aten.argmax's int64 result:
391+
#
392+
# aten.argmax -> _to_dim_order_copy(dtype=int32) -> users
393+
if node.target not in _ARGMAX_OPS or not node.users:
394+
return False
395+
396+
return all(
397+
user.target in _TO_DIM_ORDER_COPY_OPS
398+
and user.kwargs.get("dtype") == torch.int32
399+
for user in node.users
400+
)
401+
402+
377403
class CheckKnownUnsupportedTOSASemantics(OperatorSupportBase):
378404
"""Reject ops whose TOSA lowering is known to differ from PyTorch."""
379405

380406
def __init__(self, reporter: WhyNoPartitionReporter):
381407
self.reporter = reporter
382408

383-
def _argmax_all_users_cast_to_int32(self, node: fx.Node) -> bool:
384-
# TOSA ARGMAX produces int32 indices. This is only a faithful lowering
385-
# when every user immediately narrows aten.argmax's int64 result:
386-
#
387-
# aten.argmax -> _to_dim_order_copy(dtype=int32) -> users
388-
cast_ops = (
389-
torch.ops.dim_order_ops._to_dim_order_copy.default,
390-
exir_ops.edge.dim_order_ops._to_dim_order_copy.default,
391-
)
392-
# A live argmax should normally have users. Treat a no-user node as not
393-
# proving the int64 result is intentionally narrowed to int32.
394-
if not node.users:
395-
return False
396-
for user in node.users:
397-
if user.target not in cast_ops:
398-
return False
399-
if user.kwargs.get("dtype") != torch.int32:
400-
return False
401-
return True
402-
403409
def _check_argmax(self, node: fx.Node) -> bool:
404-
if self._argmax_all_users_cast_to_int32(node):
410+
if _argmax_all_users_cast_to_int32(node):
405411
return True
412+
406413
self.reporter.report_reject(
407-
node, "TOSA ARGMAX produces int32 but aten.argmax returns int64."
414+
node,
415+
"TOSA ARGMAX produces int32 but aten.argmax returns int64.",
408416
)
409417
return False
410418

@@ -773,17 +781,34 @@ def inside_int32_bounds(self, node: torch.fx.Node) -> bool:
773781
def has_rejected_int64_output(
774782
self, node: torch.fx.Node, tensor_list: Sequence[typing.Any]
775783
) -> bool:
776-
if node.target in (
777-
torch.ops.aten.argmax.default,
778-
exir_ops.edge.aten.argmax.default,
779-
):
784+
if node.target in _ARGMAX_OPS:
780785
return not self._is_tosa_argmax_supported(node)
786+
781787
return any(
782788
tensor.dtype == torch.int64
783789
for tensor in tensor_list
784790
if isinstance(tensor, FakeTensor)
785791
)
786792

793+
def _is_argmax_int32_cast(
794+
self,
795+
node: torch.fx.Node,
796+
input_node: torch.fx.Node,
797+
) -> bool:
798+
if node.target not in _TO_DIM_ORDER_COPY_OPS:
799+
return False
800+
801+
if node.kwargs.get("dtype") != torch.int32:
802+
return False
803+
804+
if input_node.target not in _ARGMAX_OPS:
805+
return False
806+
807+
if not _argmax_all_users_cast_to_int32(input_node):
808+
return False
809+
810+
return self._is_tosa_argmax_supported(input_node)
811+
787812
def _is_tosa_argmax_dtype_supported(
788813
self, node: torch.fx.Node, input_dtype: torch.dtype
789814
) -> bool:
@@ -882,6 +907,13 @@ def _check_int64_input_nodes(self, node: torch.fx.Node) -> bool:
882907
tensor_in = get_first_fake_tensor(input_node)
883908
if tensor_in.dtype != torch.int64:
884909
continue
910+
911+
# aten.argmax is nominally int64, but TOSA ARGMAX produces int32.
912+
# Allow the explicit argmax -> int32 narrowing pattern so both nodes
913+
# can be placed in the same delegate.
914+
if self._is_argmax_int32_cast(node, input_node):
915+
continue
916+
885917
# Constant placeholder
886918
if (
887919
input_node.op != "call_function"

‎backends/arm/test/ops/test_argmax.py‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from typing import Tuple
77

8-
import pytest
98
import torch
109
from executorch.backends.arm.test import common
1110
from executorch.backends.arm.test.tester.test_pipeline import (
@@ -17,6 +16,9 @@
1716

1817
aten_op = "torch.ops.aten.argmax.default"
1918
exir_op = "executorch_exir_dialects_edge__ops_aten_argmax_default"
19+
to_copy_exir_op = (
20+
"executorch_exir_dialects_edge__ops_dim_order_ops__to_dim_order_copy_default"
21+
)
2022
input_t = Tuple[torch.Tensor]
2123

2224

@@ -88,7 +90,7 @@ def test_argmax_tosa_FP(test_data: Tuple[input_t, int]):
8890
Argmax(dim),
8991
data,
9092
aten_op,
91-
exir_op,
93+
[exir_op, to_copy_exir_op],
9294
tosa_extensions=["bf16"],
9395
)
9496
pipeline.count_tosa_ops({"ARGMAX": 1})
@@ -129,19 +131,12 @@ def test_argmax_tosa_INT(test_data: Tuple[input_t, int]):
129131
Argmax(dim),
130132
data,
131133
aten_op,
132-
exir_op,
134+
[exir_op, to_copy_exir_op],
133135
)
134136
pipeline.count_tosa_ops({"ARGMAX": 1})
135137
pipeline.run()
136138

137139

138-
@pytest.mark.xfail(
139-
reason=(
140-
"VGF ARGMAX output contract mismatch: MLTORCH-2372"
141-
"ExecuTorch output is allocated as int64 while TOSA/VGF emits int32."
142-
),
143-
strict=True,
144-
)
145140
@common.parametrize(
146141
"test_data",
147142
Argmax.test_data | Argmax.test_data_fp16 | Argmax.test_data_bf16,
@@ -153,20 +148,13 @@ def test_argmax_vgf_no_quant(test_data: Tuple[input_t, int]):
153148
Argmax(dim),
154149
data,
155150
aten_op,
156-
exir_op,
151+
[exir_op, to_copy_exir_op],
157152
quantize=False,
158153
tosa_extensions=["bf16"],
159154
)
160155
pipeline.run()
161156

162157

163-
@pytest.mark.xfail(
164-
reason=(
165-
"VGF ARGMAX output contract mismatch: MLTORCH-2372"
166-
"ExecuTorch output is allocated as int64 while TOSA/VGF emits int32."
167-
),
168-
strict=True,
169-
)
170158
@common.parametrize("test_data", Argmax.test_data)
171159
@common.SkipIfNoModelConverter
172160
def test_argmax_vgf_quant(test_data: Tuple[input_t, int]):
@@ -175,7 +163,7 @@ def test_argmax_vgf_quant(test_data: Tuple[input_t, int]):
175163
Argmax(dim),
176164
data,
177165
aten_op,
178-
exir_op,
166+
[exir_op, to_copy_exir_op],
179167
quantize=True,
180168
)
181169
pipeline.run()

0 commit comments

Comments
 (0)