Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions backends/arm/test/misc/test_tosa_dialect_argmax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

import executorch.backends.arm.tosa.dialect # noqa: F401
import pytest
import torch
from executorch.backends.arm.tosa.dialect.lib import TosaValueError
from executorch.backends.arm.tosa.specification import (
TosaLoweringContext,
TosaSpecification,
)
from executorch.exir.dialects._ops import ops as exir_ops
from torch._subclasses.fake_tensor import FakeTensorMode


def test_argmax_tosa_fp() -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be placed in misc/tosa_dlalect/

sample_input = torch.randn((2, 3, 4), dtype=torch.float32)

with TosaLoweringContext(
TosaSpecification.create_from_string("TOSA-1.1+FP")
), FakeTensorMode() as mode:
output = exir_ops.backend.tosa.ARGMAX.default(
mode.from_tensor(sample_input),
axis=1,
)

assert output.dtype == torch.int32
assert tuple(output.shape) == (2, 4)


def test_argmax_rejects_bfloat16_without_extension() -> None:
sample_input = torch.randn((2, 3, 4), dtype=torch.bfloat16)

with TosaLoweringContext(
TosaSpecification.create_from_string("TOSA-1.1+FP")
), FakeTensorMode() as mode:
with pytest.raises(TosaValueError, match="doesn't support bfloat16"):
exir_ops.backend.tosa.ARGMAX.default(
mode.from_tensor(sample_input),
axis=1,
)
1 change: 1 addition & 0 deletions backends/arm/tosa/dialect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from executorch.backends.arm.tosa.dialect.ops import ( # noqa F401
activation,
argmax,
avg_pool2d,
avg_pool2d_adaptive,
conv2d,
Expand Down
78 changes: 78 additions & 0 deletions backends/arm/tosa/dialect/ops/argmax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

import torch
from executorch.backends.arm.tosa.dialect.lib import TosaValueError
from executorch.backends.arm.tosa.dialect.ops._common import validate_nan_mode
from executorch.backends.arm.tosa.dialect.ops_registration import register_fake_tosa_op
from executorch.backends.arm.tosa.specification import (
get_context_spec,
TosaSpecification,
)


def _validate_argmax_dtype(dtype: torch.dtype) -> None:
tosa_spec = get_context_spec()

if dtype == torch.int8:
if not tosa_spec.support_integer():
raise TosaValueError(
f"TOSA spec {tosa_spec} doesn't support int8 for ARGMAX",
op="ARGMAX",
)
return

if dtype == torch.int16:
if not (tosa_spec.support_integer() and tosa_spec.support_extension("int16")):
raise TosaValueError(
f"TOSA spec {tosa_spec} doesn't support int16 for ARGMAX",
op="ARGMAX",
)
return

if dtype in (torch.float16, torch.float32):
if not tosa_spec.support_float():
raise TosaValueError(
f"TOSA spec {tosa_spec} doesn't support {dtype} for ARGMAX",
op="ARGMAX",
)
return

if dtype == torch.bfloat16:
if not (tosa_spec.support_float() and tosa_spec.support_extension("bf16")):
raise TosaValueError(
f"TOSA spec {tosa_spec} doesn't support bfloat16 for ARGMAX",
op="ARGMAX",
)
return

raise TosaValueError(f"Unsupported dtype {dtype} for ARGMAX", op="ARGMAX")


@register_fake_tosa_op(
'ARGMAX(Tensor input, int axis, *, str nan_mode="PROPAGATE") -> Tensor',
TosaSpecification.all_versions_and_profiles(),
)
def ARGMAX(
input: torch.Tensor,
axis: int,
*,
nan_mode: str = "PROPAGATE",
) -> torch.Tensor:
validate_nan_mode(nan_mode, "ARGMAX")
_validate_argmax_dtype(input.dtype)

if input.dim() == 0:
raise TosaValueError(
"ARGMAX requires an input with rank at least 1", op="ARGMAX"
)
if axis < 0 or axis >= input.dim():
raise TosaValueError(
f"axis must be in [0, {input.dim() - 1}] but got {axis}",
op="ARGMAX",
)

output_shape = tuple(input.shape[:axis]) + tuple(input.shape[axis + 1 :])
return torch.empty(output_shape, dtype=torch.int32)
Loading