[Mlir-commits] [mlir] [mlir][tosa] Add missing verifier for `tosa.pad` (PR #120934)

Longsheng Mou llvmlistbot at llvm.org
Mon Dec 23 03:42:30 PST 2024


https://github.com/CoTinker updated https://github.com/llvm/llvm-project/pull/120934

>From bab92d0f5d203a97f9ad45fe09be0040ca6ba5c5 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Mon, 23 Dec 2024 14:46:50 +0800
Subject: [PATCH 1/7] [mlir][tosa] Add missing verifier for `tosa.pad`

This PR adds a missing verifier for `tosa.pad`, ensuring that the padding shape matches [rank(input), 2].
---
 mlir/lib/Dialect/Tosa/IR/TosaOps.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
index 631d3c48f2df02..6d9d2badc2970b 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -831,6 +831,11 @@ LogicalResult tosa::PadOp::verify() {
   if (paddingType.hasRank() && paddingType.getRank() != 2)
     return emitOpError() << "expect 'padding' tensor rank equal to 2.";
 
+  auto paddingShape = paddingType.getShape();
+  if (paddingShape.front() != inputType.getRank() || paddingShape.back() != 2)
+    return emitOpError()
+           << "expect 'padding' tensor shape to match [rank(input), 2]";
+
   return success();
 }
 

>From 4f51fbdd6a69fe6f585e323abc8826ba46368ade Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Mon, 23 Dec 2024 14:49:05 +0800
Subject: [PATCH 2/7] Add tests

---
 mlir/test/Dialect/Tosa/invalid.mlir | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/mlir/test/Dialect/Tosa/invalid.mlir b/mlir/test/Dialect/Tosa/invalid.mlir
index cca50b25d14d6b..8a8fcbed6a67f3 100644
--- a/mlir/test/Dialect/Tosa/invalid.mlir
+++ b/mlir/test/Dialect/Tosa/invalid.mlir
@@ -119,6 +119,22 @@ func.func @test_pad_invalid_padConst_rank(%arg0: tensor<13x21xf32>, %arg1: tenso
 
 // -----
 
+func.func @test_pad_padding_shape_mismatch(%arg0: tensor<13x21x3xf32>, %arg1: tensor<2x2xi32>) -> tensor<13x21x3xf32> {
+  // expected-error at +1 {{'tosa.pad' op expect 'padding' tensor shape to match [rank(input), 2]}}
+  %0 = tosa.pad %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<2x2xi32>) -> tensor<13x21x3xf32>
+  return %0 : tensor<13x21x3xf32>
+}
+
+// -----
+
+func.func @test_pad_padding_shape_mismatch(%arg0: tensor<13x21x3xf32>, %arg1: tensor<3x1xi32>) -> tensor<13x21x3xf32> {
+  // expected-error at +1 {{'tosa.pad' op expect 'padding' tensor shape to match [rank(input), 2]}}
+  %0 = tosa.pad %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<3x1xi32>) -> tensor<13x21x3xf32>
+  return %0 : tensor<13x21x3xf32>
+}
+
+// -----
+
 func.func @test_transpose_non_const(%arg0: tensor<13x21x3xf32>, %arg1: tensor<3xi32>) -> tensor<3x13x21xf32> {
   // expected-error at +1 {{'tosa.transpose' op perms of transpose is not constant}}
   %0 = tosa.transpose %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<3xi32>) -> tensor<3x13x21xf32>

>From 222dd4a9fdb31eda69c99ffeb6a7fb8737ffddf3 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Mon, 23 Dec 2024 16:19:19 +0800
Subject: [PATCH 3/7] Update padding

---
 mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
index e3c725801d1629..9ca5f9f959c891 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
@@ -1566,7 +1566,7 @@ def Tosa_PadOp : Tosa_InferShapedTypeOp<"pad"> {
 
   let arguments = (ins
     Tosa_RankedTensor:$input1,
-    Tosa_Int32Or64Tensor:$padding,
+    2DTensorOf<[Tosa_Int32Or64]>:$padding,
     Optional<Tosa_ScalarTensor>:$pad_const,
     OptionalAttr<Tosa_PadOpQuantizationAttr>:$quantization_info
   );

>From e1ffaed2f1410757ef0ff981340e48a62f7b9d5e Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Mon, 23 Dec 2024 16:21:42 +0800
Subject: [PATCH 4/7] Update verify

---
 mlir/lib/Dialect/Tosa/IR/TosaOps.cpp | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
index 6d9d2badc2970b..ef495115e98381 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -823,18 +823,20 @@ LogicalResult tosa::PadOp::inferReturnTypeComponents(
 LogicalResult tosa::PadOp::verify() {
   RankedTensorType inputType = getInput1().getType();
   RankedTensorType outputType = getOutput().getType();
-  TensorType paddingType = getPadding().getType();
+  RankedTensorType paddingType = getPadding().getType();
 
   if (inputType.getRank() != outputType.getRank())
     return emitOpError() << "expect same input and output tensor rank.";
 
-  if (paddingType.hasRank() && paddingType.getRank() != 2)
-    return emitOpError() << "expect 'padding' tensor rank equal to 2.";
+  if (!paddingType.isDynamicDim(0) &&
+      paddingType.getDimSize(0) != inputType.getRank())
+    return emitOpError() << "expected padding tensor dim 0 to have size "
+                         << inputType.getRank() << " (input rank) but got size "
+                         << paddingType.getDimSize(0);
 
-  auto paddingShape = paddingType.getShape();
-  if (paddingShape.front() != inputType.getRank() || paddingShape.back() != 2)
-    return emitOpError()
-           << "expect 'padding' tensor shape to match [rank(input), 2]";
+  if (!paddingType.isDynamicDim(1) && paddingType.getDimSize(1) != 2)
+    return emitOpError() << "expected padding tensor dim 1 to have size 2 "
+                         << "but got size " << paddingType.getDimSize(1);
 
   return success();
 }

>From b081d5f7a2dd585488e267f80a8bb81f6008c2ec Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Mon, 23 Dec 2024 16:23:39 +0800
Subject: [PATCH 5/7] Update invalid.mlir

---
 mlir/test/Dialect/Tosa/invalid.mlir | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mlir/test/Dialect/Tosa/invalid.mlir b/mlir/test/Dialect/Tosa/invalid.mlir
index 8a8fcbed6a67f3..f0c673b9f3b47c 100644
--- a/mlir/test/Dialect/Tosa/invalid.mlir
+++ b/mlir/test/Dialect/Tosa/invalid.mlir
@@ -103,7 +103,7 @@ func.func @test_pad_io_rank_mismatch(%arg0: tensor<13x21xf32>, %arg1: tensor<2x2
 // -----
 
 func.func @test_pad_invalid_padding_rank(%arg0: tensor<13x21xf32>, %arg1: tensor<2xi32>) {
-  // expected-error at +1 {{'tosa.pad' op expect 'padding' tensor rank equal to 2.}}
+  // expected-error at +1 {{'tosa.pad' op operand #1 must be 2D tensor of 32-bit signless integer or 64-bit signless integer values, but got 'tensor<2xi32>'}}
   %1 = tosa.pad %arg0, %arg1 : (tensor<13x21xf32>, tensor<2xi32>) -> tensor<13x21xf32>
   return
 }
@@ -120,7 +120,7 @@ func.func @test_pad_invalid_padConst_rank(%arg0: tensor<13x21xf32>, %arg1: tenso
 // -----
 
 func.func @test_pad_padding_shape_mismatch(%arg0: tensor<13x21x3xf32>, %arg1: tensor<2x2xi32>) -> tensor<13x21x3xf32> {
-  // expected-error at +1 {{'tosa.pad' op expect 'padding' tensor shape to match [rank(input), 2]}}
+  // expected-error at +1 {{'tosa.pad' op expected padding tensor dim 0 to have size 3 (input rank) but got size 2}}
   %0 = tosa.pad %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<2x2xi32>) -> tensor<13x21x3xf32>
   return %0 : tensor<13x21x3xf32>
 }
@@ -128,7 +128,7 @@ func.func @test_pad_padding_shape_mismatch(%arg0: tensor<13x21x3xf32>, %arg1: te
 // -----
 
 func.func @test_pad_padding_shape_mismatch(%arg0: tensor<13x21x3xf32>, %arg1: tensor<3x1xi32>) -> tensor<13x21x3xf32> {
-  // expected-error at +1 {{'tosa.pad' op expect 'padding' tensor shape to match [rank(input), 2]}}
+  // expected-error at +1 {{'tosa.pad' op expected padding tensor dim 1 to have size 2 but got size 1}}
   %0 = tosa.pad %arg0, %arg1 : (tensor<13x21x3xf32>, tensor<3x1xi32>) -> tensor<13x21x3xf32>
   return %0 : tensor<13x21x3xf32>
 }

>From abdf2da9cc48b6c73b84f140b16aafc6b941d022 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Mon, 23 Dec 2024 19:39:46 +0800
Subject: [PATCH 6/7] Update TosaOps.td

---
 mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
index 9ca5f9f959c891..6c3583b7922a57 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
@@ -1698,7 +1698,8 @@ def Tosa_TileOp : Tosa_InferShapedTypeOp<"tile"> {
 // Operator: transpose
 //===----------------------------------------------------------------------===//
 def Tosa_TransposeOp : Tosa_InferShapedTypeOp<"transpose",
-                [DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>]> {
+                [DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>,
+                 AllElementTypesMatch<["input1", "output"]>]> {
   let summary = "Transpose operator";
 
   let description = [{

>From 99879eeaf262e592c510061b48e0ba574c31b97c Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Mon, 23 Dec 2024 19:42:19 +0800
Subject: [PATCH 7/7] Update TosaCanonicalizations.cpp

---
 mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index 39d0ee122b1630..f51c3dbce6eefe 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
@@ -1002,10 +1002,6 @@ OpFoldResult TransposeOp::fold(FoldAdaptor adaptor) {
       return input.reshape(resultTy);
   }
 
-  // Transpose does not change the input type.
-  if (getInput1().getType() != getType())
-    return {};
-
   // Transpose is not the identity transpose.
   SmallVector<int32_t> perms;
   if (getConstantPerms(perms).failed())



More information about the Mlir-commits mailing list