[Mlir-commits] [mlir] [mlir][tosa] Check same input/output types in pooling ops verifier (PR #203565)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Jun 12 08:28:07 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: Luke Hutton (lhutton1)
<details>
<summary>Changes</summary>
Adds a missing check to make sure the input and output types of pooling ops have the same element type.
---
Full diff: https://github.com/llvm/llvm-project/pull/203565.diff
2 Files Affected:
- (modified) mlir/lib/Dialect/Tosa/IR/TosaOps.cpp (+8-5)
- (modified) mlir/test/Dialect/Tosa/verifier.mlir (+22)
``````````diff
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
index f05399cf6b00b..9b5219343ee69 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -1073,18 +1073,18 @@ static LogicalResult verifyVariableOpErrorIf(T op, Type type, StringRef name) {
}
// verify that inType and outType have same element types
-template <typename T>
-static LogicalResult verifySameElementTypes(T op, Type aType, Type bType,
+static LogicalResult verifySameElementTypes(Operation *op, Type aType,
+ Type bType,
StringRef aName = "input",
StringRef bName = "output") {
auto aTType = llvm::dyn_cast<TensorType>(aType);
auto bTType = llvm::dyn_cast<TensorType>(bType);
if (!aTType) {
- op.emitOpError("expect shaped tensor for") << aName << ", got " << aType;
+ op->emitOpError("expect shaped tensor for") << aName << ", got " << aType;
return failure();
}
if (!bTType) {
- op.emitOpError("expect shaped tensor for") << bName << ", got" << bType;
+ op->emitOpError("expect shaped tensor for") << bName << ", got" << bType;
return failure();
}
auto aElementType = aTType.getElementType();
@@ -1100,7 +1100,7 @@ static LogicalResult verifySameElementTypes(T op, Type aType, Type bType,
// eg, not sure how to check quant::QuantizedType
// this happens in test_conv2d_q_grouped_convolution in
// tfl-to-tosa-pipeline.mlir
- op.emitOpError("expect ")
+ op->emitOpError("expect ")
<< aName << " and " << bName << " to have same element type, got "
<< aElementType << " and " << bElementType;
return failure();
@@ -1144,6 +1144,9 @@ static LogicalResult verifyPoolingOpImpl(Operation *op,
ArrayRef<int64_t> strides,
ArrayRef<int64_t> padding, Value input,
Value output) {
+ if (failed(verifySameElementTypes(op, input.getType(), output.getType())))
+ return failure();
+
const bool hasKernel = kernel.size() > 0;
const bool hasStrides = strides.size() > 0;
const bool hasPad = padding.size() > 0;
diff --git a/mlir/test/Dialect/Tosa/verifier.mlir b/mlir/test/Dialect/Tosa/verifier.mlir
index 9b5faf575971b..164ad37177cff 100644
--- a/mlir/test/Dialect/Tosa/verifier.mlir
+++ b/mlir/test/Dialect/Tosa/verifier.mlir
@@ -2060,6 +2060,28 @@ func.func @test_maxpool2d_adaptive_unexpected_output_width(%arg0: tensor<1x32x32
// -----
+func.func @test_avg_pool2d_same_input_output_type(%arg0: tensor<1x7x7x9xf32>) -> tensor<1x7x7x9xf16> {
+ %input_zp = "tosa.const"() <{values = dense<0.0> : tensor<1xf32>}> : () -> tensor<1xf32>
+ %output_zp = "tosa.const"() <{values = dense<0.0> : tensor<1xf32>}> : () -> tensor<1xf32>
+ // expected-error at +1 {{'tosa.avg_pool2d' op expect input and output to have same element type, got 'f32' and 'f16'}}
+ %0 = tosa.avg_pool2d %arg0, %input_zp, %output_zp {acc_type = f32, kernel = array<i64: 2, 2>, pad = array<i64: 0, 1, 0, 1>, stride = array<i64: 1, 1>} : (tensor<1x7x7x9xf32>, tensor<1xf32>, tensor<1xf32>) -> tensor<1x7x7x9xf16>
+ return %0 : tensor<1x7x7x9xf16>
+}
+
+// -----
+
+func.func @test_maxpool2d_adaptive_unexpected_output_width(%arg0: tensor<1x32x32x8xf32>) -> tensor<1x32x32x8xf16> {
+ %kernel = tosa.const_shape {values = dense<[1, 1]> : tensor<2xindex>} : () -> !tosa.shape<2>
+ %stride = tosa.const_shape {values = dense<[1, 1]> : tensor<2xindex>} : () -> !tosa.shape<2>
+ %pad = tosa.const_shape {values = dense<[0, 0, 0, 0]> : tensor<4xindex>} : () -> !tosa.shape<4>
+ // expected-error at +1 {{'tosa.max_pool2d_adaptive' op expect input and output to have same element type, got 'f32' and 'f16'}}
+ %0 = tosa.max_pool2d_adaptive %arg0, %kernel, %stride, %pad :
+ (tensor<1x32x32x8xf32>, !tosa.shape<2>, !tosa.shape<2>, !tosa.shape<4>) -> tensor<1x32x32x8xf16>
+ return %0 : tensor<1x32x32x8xf16>
+}
+
+// -----
+
func.func @test_const_mxint8_uint8(%arg0 : index) -> tensor<2x!tosa.mxint8> {
// expected-error at +1 {{incompatible attribute for element type}}
%0 = "tosa.const"() {values = dense<tensor<2x!tosa.mxint8> : [127: ui8, 245: ui8]>} : () -> tensor<2x!tosa.mxint8>
``````````
</details>
https://github.com/llvm/llvm-project/pull/203565
More information about the Mlir-commits
mailing list