Skip to content

[mlir][tosa] Add error if verification to pooling operators #130052

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 1 commit into from
Mar 12, 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
98 changes: 96 additions & 2 deletions mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,95 @@ LogicalResult tosa::ArgMaxOp::verify() {
return success();
}

template <typename T>
static LogicalResult verifyPoolingOp(T op) {
const llvm::ArrayRef<int64_t> kernel = op.getKernel();
if (llvm::any_of(kernel, [](int64_t s) { return s < 1; }))
return op.emitOpError("expect all kernel values to be >= 1, got ")
<< kernel;

const llvm::ArrayRef<int64_t> strides = op.getStride();
if (llvm::any_of(strides, [](int64_t s) { return s < 1; }))
return op.emitOpError("expect all stride values to be >= 1, got ")
<< strides;

const llvm::ArrayRef<int64_t> padding = op.getPad();
if (llvm::any_of(padding, [](int64_t p) { return p < 0; }))
return op.emitOpError("expect all padding values to be >= 0, got ")
<< padding;

// Padding must be less than kernel size to avoid a divide-by-zero
const int64_t kernelX = kernel[1];
const int64_t padLeft = padding[2];
const int64_t padRight = padding[3];
if (padRight >= kernelX || padLeft >= kernelX)
return op.emitOpError("expected left/right padding to be less than the "
"width of the kernel, got pad_left=")
<< padLeft << ", pad_right=" << padRight << ", kernel_x=" << kernelX;

const int64_t kernelY = kernel[0];
const int64_t padTop = padding[0];
const int64_t padBottom = padding[1];
if (padTop >= kernelY || padBottom >= kernelY)
return op.emitOpError("expected top/bottom padding to be less than the "
"height of the kernel, got pad_top=")
<< padTop << ", pad_bottom=" << padBottom
<< ", kernel_y=" << kernelY;

const auto inputType =
llvm::dyn_cast<RankedTensorType>(op.getInput().getType());
const auto outputType =
llvm::dyn_cast<RankedTensorType>(op.getResult().getType());
if (!inputType || !outputType)
return success();

const auto verifyOutputSize =
[&op](const int64_t inputSize, const int64_t outputSize,
const int64_t kernelSize, const int64_t strideSize,
const int64_t padBefore, const int64_t padAfter,
const llvm::StringRef dimName, const llvm::StringRef dimAxis,
const llvm::StringRef padBeforeName,
const llvm::StringRef padAfterName) -> LogicalResult {
if (ShapedType::isDynamic(inputSize))
return success();

const std::optional<int64_t> calculatedOutSizeMinusOne =
idivCheck(inputSize + padBefore + padAfter - kernelSize, strideSize);
if (!calculatedOutSizeMinusOne.has_value())
return op.emitOpError("expected input_")
<< dimName << " + pad_" << padBeforeName << " + pad_"
<< padAfterName << " - kernel_" << dimAxis
<< " to be wholly divisible by stride_" << dimAxis << ", got ("
<< inputSize << " + " << padBefore << " + " << padAfter << " - "
<< kernelSize << ") / " << strideSize;

const int64_t calculatedOutSize = calculatedOutSizeMinusOne.value() + 1;
if (!ShapedType::isDynamic(outputSize) && calculatedOutSize != outputSize)
return op.emitOpError("calculated output ")
<< dimName << " did not match expected: "
<< "calculated=" << calculatedOutSize
<< ", expected=" << outputSize;

return success();
};

if (failed(verifyOutputSize(inputType.getDimSize(1), outputType.getDimSize(1),
kernel[0], strides[0], padding[0], padding[1],
"height", "y", "top", "bottom")))
return failure();

if (failed(verifyOutputSize(inputType.getDimSize(2), outputType.getDimSize(2),
kernel[1], strides[1], padding[2], padding[3],
"width", "x", "left", "right")))
return failure();

return success();
}

LogicalResult tosa::AvgPool2dOp::verify() {
if (failed(verifyPoolingOp(*this)))
return failure();

const Type inputETy = getStorageElementTypeOrSelf(getInput().getType());
const Type resultETy = getStorageElementTypeOrSelf(getOutput().getType());
const Type inputZpETy = getStorageElementTypeOrSelf(getInputZp().getType());
Expand Down Expand Up @@ -2603,8 +2691,14 @@ LogicalResult MaxPool2dOp::inferReturnTypeComponents(
}

LogicalResult MaxPool2dOp::verify() {
return verifySameElementTypes(*this, /* intype = */ getInput().getType(),
/* outType = */ getOutput().getType());
if (failed(verifySameElementTypes(*this, /* intype = */ getInput().getType(),
/* outType = */ getOutput().getType())))
return failure();

if (failed(verifyPoolingOp(*this)))
return failure();

return success();
}

LogicalResult DepthwiseConv2DOp::inferReturnTypeComponents(
Expand Down
99 changes: 99 additions & 0 deletions mlir/test/Dialect/Tosa/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1717,3 +1717,102 @@ func.func @test_negate_output_zp_non_zero(%arg0: tensor<1x16x16x8xf32>) -> tenso
: (tensor<1x16x16x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x16x16x8xf32>
return %0 : tensor<1x16x16x8xf32>
}

// -----

func.func @test_avgpool2d_invalid_kernel(%arg0: tensor<1x32x32x8xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1x32x32x8xf32> {
// expected-error@+1 {{'tosa.avg_pool2d' op expect all kernel values to be >= 1, got 0, -1}}
%0 = "tosa.avg_pool2d"(%arg0, %arg1, %arg2) {kernel = array<i64: 0, -1>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 1, 1>, acc_type = f32} :
(tensor<1x32x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x32x32x8xf32>
return %0 : tensor<1x32x32x8xf32>
}

// -----

func.func @test_avgpool2d_invalid_stride(%arg0: tensor<1x32x32x8xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1x32x32x8xf32> {
// expected-error@+1 {{'tosa.avg_pool2d' op expect all stride values to be >= 1, got 1, 0}}
%0 = "tosa.avg_pool2d"(%arg0, %arg1, %arg2) {kernel = array<i64: 1, 1>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 1, 0>, acc_type = f32} :
(tensor<1x32x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x32x32x8xf32>
return %0 : tensor<1x32x32x8xf32>
}

// -----

func.func @test_avgpool2d_invalid_padding(%arg0: tensor<1x32x32x8xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1x32x32x8xf32> {
// expected-error@+1 {{'tosa.avg_pool2d' op expect all padding values to be >= 0, got 0, 0, 0, -1}}
%0 = "tosa.avg_pool2d"(%arg0, %arg1, %arg2) {kernel = array<i64: 1, 1>, pad = array<i64: 0, 0, 0, -1>, stride = array<i64: 1, 1>, acc_type = f32} :
(tensor<1x32x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x32x32x8xf32>
return %0 : tensor<1x32x32x8xf32>
}

// -----

func.func @test_avgpool2d_padding_not_less_than_kernel_x(%arg0: tensor<1x32x32x8xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1x32x32x8xf32> {
// expected-error@+1 {{'tosa.avg_pool2d' op expected left/right padding to be less than the width of the kernel, got pad_left=0, pad_right=1, kernel_x=1}}
%0 = "tosa.avg_pool2d"(%arg0, %arg1, %arg2) {kernel = array<i64: 1, 1>, pad = array<i64: 0, 0, 0, 1>, stride = array<i64: 1, 1>, acc_type = f32} :
(tensor<1x32x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x32x32x8xf32>
return %0 : tensor<1x32x32x8xf32>
}

// -----

func.func @test_avgpool2d_padding_not_less_than_kernel_y(%arg0: tensor<1x32x32x8xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1x32x32x8xf32> {
// expected-error@+1 {{'tosa.avg_pool2d' op expected top/bottom padding to be less than the height of the kernel, got pad_top=2, pad_bottom=0, kernel_y=1}}
%0 = "tosa.avg_pool2d"(%arg0, %arg1, %arg2) {kernel = array<i64: 1, 1>, pad = array<i64: 2, 0, 0, 0>, stride = array<i64: 1, 1>, acc_type = f32} :
(tensor<1x32x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x32x32x8xf32>
return %0 : tensor<1x32x32x8xf32>
}

// -----

func.func @test_avgpool2d_wholly_divisible_height(%arg0: tensor<1x32x32x8xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1x32x32x8xf32> {
// expected-error@+1 {{'tosa.avg_pool2d' op expected input_height + pad_top + pad_bottom - kernel_y to be wholly divisible by stride_y, got (32 + 0 + 0 - 1) / 2}}
%0 = "tosa.avg_pool2d"(%arg0, %arg1, %arg2) {kernel = array<i64: 1, 1>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 2, 1>, acc_type = f32} :
(tensor<1x32x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x32x32x8xf32>
return %0 : tensor<1x32x32x8xf32>
}

// -----

func.func @test_avgpool2d_wholly_divisible_width(%arg0: tensor<1x32x32x8xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1x32x32x8xf32> {
// expected-error@+1 {{'tosa.avg_pool2d' op expected input_width + pad_left + pad_right - kernel_x to be wholly divisible by stride_x, got (32 + 0 + 0 - 1) / 2}}
%0 = "tosa.avg_pool2d"(%arg0, %arg1, %arg2) {kernel = array<i64: 1, 1>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 1, 2>, acc_type = f32} :
(tensor<1x32x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x32x32x8xf32>
return %0 : tensor<1x32x32x8xf32>
}

// -----

func.func @test_avgpool2d_unexpected_output_height(%arg0: tensor<1x32x32x8xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1x33x32x8xf32> {
// expected-error@+1 {{'tosa.avg_pool2d' op calculated output height did not match expected: calculated=32, expected=33}}
%0 = "tosa.avg_pool2d"(%arg0, %arg1, %arg2) {kernel = array<i64: 1, 1>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 1, 1>, acc_type = f32} :
(tensor<1x32x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x33x32x8xf32>
return %0 : tensor<1x33x32x8xf32>
}

// -----

func.func @test_avgpool2d_unexpected_output_width(%arg0: tensor<1x32x32x8xf32>, %arg1: tensor<1xf32>, %arg2: tensor<1xf32>) -> tensor<1x?x33x8xf32> {
// expected-error@+1 {{'tosa.avg_pool2d' op calculated output width did not match expected: calculated=32, expected=33}}
%0 = "tosa.avg_pool2d"(%arg0, %arg1, %arg2) {kernel = array<i64: 1, 1>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 1, 1>, acc_type = f32} :
(tensor<1x32x32x8xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x?x33x8xf32>
return %0 : tensor<1x?x33x8xf32>
}

// -----

func.func @test_maxpool2d_invalid_kernel(%arg0: tensor<1x32x32x8xf32>) -> tensor<1x2x32x8xf32> {
// expected-error@+1 {{'tosa.max_pool2d' op expect all kernel values to be >= 1, got 0, 1}}
%0 = "tosa.max_pool2d"(%arg0) {kernel = array<i64: 0, 1>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 1, 1>} :
(tensor<1x32x32x8xf32>) -> tensor<1x2x32x8xf32>
return %0 : tensor<1x2x32x8xf32>
}

// -----

func.func @test_maxpool2d_unexpected_output_width(%arg0: tensor<1x32x32x8xf32>) -> tensor<1x32x2x8xf32> {
// expected-error@+1 {{'tosa.max_pool2d' op calculated output width did not match expected: calculated=32, expected=2}}
%0 = "tosa.max_pool2d"(%arg0) {kernel = array<i64: 1, 1>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 1, 1>} :
(tensor<1x32x32x8xf32>) -> tensor<1x32x2x8xf32>
return %0 : tensor<1x32x2x8xf32>
}
Loading