Skip to content

Commit

Permalink
[mlir][tosa] Add verifier for ArgMax operator (#68410)
Browse files Browse the repository at this point in the history
Verifier ensures that operator is valid by checking:

* Output type is I32
* Axis is within the rank of the tensor

Signed-off-by: Georgios Pinitas <georgios.pinitas@arm.com>
  • Loading branch information
GeorgeARM authored Oct 9, 2023
1 parent b7188d2 commit 414709e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def Tosa_ArgMaxOp : Tosa_InferShapedTypeOp<"argmax"> {
let results = (outs
Tosa_Tensor: $output
);

let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
15 changes: 15 additions & 0 deletions mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ template <typename T> static LogicalResult verifyConvOp(T op) {
return success();
}

LogicalResult tosa::ArgMaxOp::verify() {
// Ensure output is of 32-bit integer
const auto resultETy = llvm::cast<ShapedType>(getType()).getElementType();
if (!resultETy.isIntOrIndex())
return emitOpError("result tensor is not of integer type");

// Ensure axis is within the tensor rank
const auto inputType = llvm::cast<ShapedType>(getInput().getType());
const int64_t axis = getAxisAttr().getInt();
if (inputType.hasRank() && ((axis < 0) || axis >= inputType.getRank()))
return emitOpError("specified axis is outside the rank of the tensor");

return success();
}

LogicalResult tosa::AvgPool2dOp::verify() {
auto inputType = llvm::cast<ShapedType>(getInput().getType());
if (hasZeroDimension(inputType))
Expand Down
6 changes: 3 additions & 3 deletions mlir/test/Dialect/Tosa/canonicalize.mlir
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// RUN: mlir-opt -canonicalize="test-convergence" %s | FileCheck %s

// CHECK-LABEL: @argmax_nofold
func.func @argmax_nofold(%arg0: tensor<?x1xf32>) -> tensor<?x1xf32> {
func.func @argmax_nofold(%arg0: tensor<?x1xf32>) -> tensor<?x1xi32> {
// CHECK: tosa.argmax
%0 = tosa.argmax %arg0 {axis = 0 : i32}: (tensor<?x1xf32>) -> tensor<?x1xf32>
return %0 : tensor<?x1xf32>
%0 = tosa.argmax %arg0 {axis = 0 : i32}: (tensor<?x1xf32>) -> tensor<?x1xi32>
return %0 : tensor<?x1xi32>
}

// CHECK-LABEL: @add_bcast_zero_int
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/Tosa/constrained_shapes.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
// Uses argmax as canonical example to validate constrained TOSA tensor shapes.
// CHECK-LABEL: argmax
func.func @test_argmax(%arg0: tensor<?xf32>) -> tensor<?xi32> {
%0 = "tosa.argmax"(%arg0) {axis = 1 : i32} : (tensor<?xf32>) -> tensor<?xi32>
%0 = "tosa.argmax"(%arg0) {axis = 0 : i32} : (tensor<?xf32>) -> tensor<?xi32>
return %0 : tensor<?xi32>
}
6 changes: 3 additions & 3 deletions mlir/test/Dialect/Tosa/level_check.mlir
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// RUN: mlir-opt %s -split-input-file -verify-diagnostics --tosa-validate


func.func @test_argmax(%arg0: tensor<1x1x1x1x29x29x4xf32>) -> tensor<1x1x1x1x29x4xf32> {
func.func @test_argmax(%arg0: tensor<1x1x1x1x29x29x4xf32>) -> tensor<1x1x1x1x29x4xi32> {
// expected-error@+1 {{'tosa.argmax' op failed level check: operand rank(shape) <= MAX_RANK}}
%0 = "tosa.argmax"(%arg0) {axis = 4 : i32} : (tensor<1x1x1x1x29x29x4xf32>) -> tensor<1x1x1x1x29x4xf32>
return %0 : tensor<1x1x1x1x29x4xf32>
%0 = "tosa.argmax"(%arg0) {axis = 4 : i32} : (tensor<1x1x1x1x29x29x4xf32>) -> tensor<1x1x1x1x29x4xi32>
return %0 : tensor<1x1x1x1x29x4xi32>
}

// -----
Expand Down

0 comments on commit 414709e

Please sign in to comment.