Skip to content

[mlir][tosa] Add verifier checks for Gather #137204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged
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
48 changes: 46 additions & 2 deletions mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2262,8 +2262,52 @@ LogicalResult tosa::GatherOp::inferReturnTypeComponents(
}

LogicalResult tosa::GatherOp::verify() {
return verifySameElementTypes(*this, /* inType = */ getValues().getType(),
/* outType = */ getOutput().getType());
if (verifySameElementTypes(*this, /* inType = */ getValues().getType(),
/* outType = */ getOutput().getType())
.failed()) {
return failure();
}

const ShapeAdaptor valuesShape(getValues().getType());
const ShapeAdaptor indicesShape(getIndices().getType());
const ShapeAdaptor outputShape(getOutput().getType());

int64_t N = ShapedType::kDynamic;
int64_t W = ShapedType::kDynamic;
int64_t C = ShapedType::kDynamic;

if (valuesShape.hasRank()) {
N = valuesShape.getDimSize(0);
C = valuesShape.getDimSize(2);
}
if (indicesShape.hasRank()) {
const int64_t indicesN = indicesShape.getDimSize(0);
W = indicesShape.getDimSize(1);
if (N == ShapedType::kDynamic)
N = indicesN;
else if (indicesN != ShapedType::kDynamic && N != indicesN)
return emitOpError() << "requires indices dimension 0 to have size " << N
<< ", got " << indicesN;
}
if (outputShape.hasRank()) {
const int64_t outputN = outputShape.getDimSize(0);
const int64_t outputW = outputShape.getDimSize(1);
const int64_t outputC = outputShape.getDimSize(2);
if (N != ShapedType::kDynamic && outputN != ShapedType::kDynamic &&
N != outputN)
return emitOpError() << "requires output dimension 0 to have size " << N
<< ", got " << outputN;

if (W != ShapedType::kDynamic && outputW != ShapedType::kDynamic &&
W != outputW)
return emitOpError() << "requires output dimension 1 to have size " << W
<< ", got " << outputW;
if (C != ShapedType::kDynamic && outputC != ShapedType::kDynamic &&
C != outputC)
return emitOpError() << "requires output dimension 2 to have size " << C
<< ", got " << outputC;
}
return success();
}

LogicalResult tosa::ResizeOp::inferReturnTypeComponents(
Expand Down
33 changes: 33 additions & 0 deletions mlir/test/Dialect/Tosa/verifier.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,36 @@ func.func @test_error_scalar_input_with_per_channel(%arg0: tensor<i8>) -> tensor
%0 = tosa.rescale %arg0, %multiplier, %shift, %input_zp, %output_zp {scale32 = true, rounding_mode = "SINGLE_ROUND", per_channel = true, input_unsigned = false, output_unsigned = false} : (tensor<i8>, tensor<1xi32>, tensor<1xi8>, tensor<1xi8>, tensor<1xi16>) -> tensor<i16>
return %0 : tensor<i16>
}

// -----

// CHECK-LABEL: @test_gather_invalid_indices_N
func.func @test_gather_invalid_indices_N(%arg0: tensor<13x21x3xf32>, %arg1: tensor<12x26xi32>) -> tensor<13x26x3xf32> {
// expected-error@+1 {{'tosa.gather' op requires indices dimension 0 to have size 13, got 12}}
%0 = tosa.gather %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<12x26xi32>) -> tensor<13x26x3xf32>
return %0 : tensor<13x26x3xf32>
}

// -----
// CHECK-LABEL: test_gather_invalid_out_N
func.func @test_gather_invalid_out_N(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x26xi32>) -> tensor<12x26x3xf32> {
// expected-error@+1 {{'tosa.gather' op requires output dimension 0 to have size 13, got 12}}
%0 = tosa.gather %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<13x26xi32>) -> tensor<12x26x3xf32>
return %0 : tensor<12x26x3xf32>
}

// -----
// CHECK-LABEL: test_gather_invalid_out_W
func.func @test_gather_invalid_out_W(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x26xi32>) -> tensor<13x28x3xf32> {
// expected-error@+1 {{'tosa.gather' op requires output dimension 1 to have size 26, got 28}}
%0 = tosa.gather %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<13x26xi32>) -> tensor<13x28x3xf32>
return %0 : tensor<13x28x3xf32>
}

// -----
// CHECK-LABEL: test_gather_invalid_out_C
func.func @test_gather_invalid_out_C(%arg0: tensor<13x21x3xf32>, %arg1: tensor<13x26xi32>) -> tensor<13x26x8xf32> {
// expected-error@+1 {{'tosa.gather' op requires output dimension 2 to have size 3, got 8}}
%0 = tosa.gather %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<13x26xi32>) -> tensor<13x26x8xf32>
return %0 : tensor<13x26x8xf32>
}