Skip to content

Commit 83fb0f9

Browse files
committed
[mlir][tosa] Enhance verify checks for PAD Op
* add padding shape verification * add and update LIT test Change-Id: Ie77ba21d271362906618389cf90cf0af20e2fcae Signed-off-by: Peng Sun <[email protected]>
1 parent 8b2d269 commit 83fb0f9

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

mlir/lib/Dialect/Tosa/IR/TosaOps.cpp

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,15 +1534,49 @@ LogicalResult tosa::PadOp::verify() {
15341534
if (!inputType || !outputType)
15351535
return success();
15361536

1537-
auto paddingRank = cast<tosa::shapeType>(getPadding().getType()).getRank();
1537+
auto inputRank = inputType.getRank();
1538+
auto outputRank = outputType.getRank();
1539+
if (inputRank != outputRank)
1540+
return emitOpError() << "expect same input and output tensor rank, but got "
1541+
<< "inputRank: " << inputRank
1542+
<< ", outputRank: " << outputRank;
1543+
1544+
DenseIntElementsAttr paddingAttr;
1545+
if (!matchPattern(getPadding(), m_Constant(&paddingAttr)))
1546+
return failure();
1547+
1548+
auto paddingValues = paddingAttr.getValues<APInt>();
1549+
if (paddingValues.size() != static_cast<size_t>(inputRank * 2))
1550+
return emitOpError() << "padding tensor must have " << inputRank
1551+
<< " * 2 = " << inputRank * 2 << " elements, but got "
1552+
<< paddingValues.size();
1553+
1554+
auto inputShape = inputType.getShape();
1555+
auto outputShape = outputType.getShape();
1556+
1557+
for (int64_t i = 0; i < inputRank; ++i) {
1558+
// Skip shape verification for dynamic dims
1559+
if (inputShape[i] == ShapedType::kDynamic ||
1560+
outputShape[i] == ShapedType::kDynamic)
1561+
continue;
1562+
1563+
int64_t padStart = paddingValues[i * 2].getSExtValue();
1564+
int64_t padEnd = paddingValues[i * 2 + 1].getSExtValue();
15381565

1539-
if (inputType.getRank() != outputType.getRank())
1540-
return emitOpError() << "expect same input and output tensor rank.";
1566+
if (padStart < 0 || padEnd < 0) {
1567+
return emitOpError() << "padding values must be non-negative, got ["
1568+
<< padStart << ", " << padEnd << "] for dimension "
1569+
<< i;
1570+
}
15411571

1542-
if (paddingRank != inputType.getRank() * 2)
1543-
return emitOpError() << "expected padding tensor dim 0 to have size "
1544-
<< inputType.getRank() * 2
1545-
<< " (2*rank(shape1)) but got size " << paddingRank;
1572+
if (outputShape[i] != inputShape[i] + padStart + padEnd) {
1573+
return emitOpError() << "mismatch in output shape at dimension " << i
1574+
<< ": expected " << inputShape[i] << " + "
1575+
<< padStart << " + " << padEnd << " = "
1576+
<< (inputShape[i] + padStart + padEnd)
1577+
<< ", but got " << outputShape[i];
1578+
}
1579+
}
15461580

15471581
return success();
15481582
}

mlir/test/Dialect/Tosa/dynamic_extension.mlir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ func.func @test_table_non_const(%arg0 : tensor<4x5xi8>, %arg1 : tensor<256xi8>)
2020

2121
// -----
2222

23-
func.func @test_pad_non_const(%arg0: tensor<13x21x3xi8>, %arg1: tensor<1xi8>) -> tensor<13x21x3xi8> {
23+
func.func @test_pad_non_const(%arg0: tensor<13x21x3xi8>, %arg1: tensor<1xi8>) -> tensor<13x22x4xi8> {
2424
%0 = tosa.const_shape {values = dense<[0, 0, 0, 1, 0, 1]> : tensor<6xindex>} : () -> !tosa.shape<6>
25-
%1 = tosa.pad %arg0, %0, %arg1 : (tensor<13x21x3xi8>, !tosa.shape<6>, tensor<1xi8>) -> tensor<13x21x3xi8>
26-
return %1 : tensor<13x21x3xi8>
25+
%1 = tosa.pad %arg0, %0, %arg1 : (tensor<13x21x3xi8>, !tosa.shape<6>, tensor<1xi8>) -> tensor<13x22x4xi8>
26+
return %1 : tensor<13x22x4xi8>
2727
}
2828

2929
// -----

mlir/test/Dialect/Tosa/invalid.mlir

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func.func @test_concat(%arg0 : tensor<2x1xf32>, %arg1 : tensor<2x2xf32>) -> tens
272272

273273
// -----
274274

275-
func.func @test_pad_non_const(%arg0: tensor<13x21x3xf32>, %arg1: !tosa.shape<6>) -> tensor<13x21x3xf32> {
275+
func.func @test_pad_padding_non_const(%arg0: tensor<13x21x3xf32>, %arg1: !tosa.shape<6>) -> tensor<13x21x3xf32> {
276276
%pad_const = "tosa.const"() {values = dense<3.14> : tensor<1xf32>} : () -> tensor<1xf32>
277277
// expected-error@+1 {{'tosa.pad' op shape operand is not compile time resolvable}}
278278
%0 = tosa.pad %arg0, %arg1, %pad_const : (tensor<13x21x3xf32>, !tosa.shape<6>, tensor<1xf32>) -> tensor<13x21x3xf32>
@@ -281,9 +281,18 @@ func.func @test_pad_non_const(%arg0: tensor<13x21x3xf32>, %arg1: !tosa.shape<6>)
281281

282282
// -----
283283

284-
func.func @test_pad_non_const(%arg0: tensor<13x21x3xi8>, %arg1: tensor<1xi8>) -> tensor<13x21x3xi8> {
284+
func.func @test_pad_pad_const_non_const(%arg0: tensor<13x21x3xi8>, %arg1: tensor<1xi8>) -> tensor<13x22x4xi8> {
285285
%0 = tosa.const_shape {values = dense<[0, 0, 0, 1, 0, 1]> : tensor<6xindex>} : () -> !tosa.shape<6>
286286
// expected-error@+1 {{'tosa.pad' op expected compile time resolvable constant, but got variable value for operand #2}}
287+
%1 = tosa.pad %arg0, %0, %arg1 : (tensor<13x21x3xi8>, !tosa.shape<6>, tensor<1xi8>) -> tensor<13x22x4xi8>
288+
return %1 : tensor<13x22x4xi8>
289+
}
290+
291+
// -----
292+
293+
func.func @test_pad_output_mismatch(%arg0: tensor<13x21x3xi8>, %arg1: tensor<1xi8>) -> tensor<13x21x3xi8> {
294+
%0 = tosa.const_shape {values = dense<[0, 0, 0, 1, 0, 1]> : tensor<6xindex>} : () -> !tosa.shape<6>
295+
// expected-error@+1 {{mismatch in output shape at dimension 1: expected 21 + 0 + 1 = 22, but got 21}}
287296
%1 = tosa.pad %arg0, %0, %arg1 : (tensor<13x21x3xi8>, !tosa.shape<6>, tensor<1xi8>) -> tensor<13x21x3xi8>
288297
return %1 : tensor<13x21x3xi8>
289298
}
@@ -293,7 +302,7 @@ func.func @test_pad_non_const(%arg0: tensor<13x21x3xi8>, %arg1: tensor<1xi8>) ->
293302
func.func @test_pad_io_rank_mismatch(%arg0: tensor<13x21xf32>) {
294303
%0 = tosa.const_shape {values = dense<1> : tensor<4xindex>} : () -> !tosa.shape<4>
295304
%pad_const = "tosa.const"() {values = dense<3.14> : tensor<1xf32>} : () -> tensor<1xf32>
296-
// expected-error@+1 {{'tosa.pad' op expect same input and output tensor rank.}}
305+
// expected-error@+1 {{'tosa.pad' op expect same input and output tensor rank}}
297306
%1 = tosa.pad %arg0, %0, %pad_const : (tensor<13x21xf32>, !tosa.shape<4>, tensor<1xf32>) -> tensor<13x21x3xf32>
298307
}
299308

@@ -310,7 +319,7 @@ func.func @test_concat_input_rank_mismatch(%arg0: tensor<1x2x3xf32>, %arg1: tens
310319
func.func @test_pad_invalid_padding_rank(%arg0: tensor<13x21xf32>) {
311320
%0 = tosa.const_shape {values = dense<1> : tensor<6xindex>} : () -> !tosa.shape<6>
312321
%pad_const = "tosa.const"() {values = dense<3.14> : tensor<1xf32>} : () -> tensor<1xf32>
313-
// expected-error@+1 {{'tosa.pad' op expected padding tensor dim 0 to have size 4 (2*rank(shape1)) but got size 6}}
322+
// expected-error@+1 {{'tosa.pad' op padding tensor must have 2 * 2 = 4 elements, but got 6}}
314323
%1 = tosa.pad %arg0, %0, %pad_const : (tensor<13x21xf32>, !tosa.shape<6>, tensor<1xf32>) -> tensor<13x21xf32>
315324
return
316325
}
@@ -330,7 +339,7 @@ func.func @test_pad_invalid_padConst_rank(%arg0: tensor<13x21xf32>, %arg1: tenso
330339
func.func @test_pad_padding_shape_mismatch(%arg0: tensor<13x21x3xf32>) -> tensor<13x21x3xf32> {
331340
%0 = tosa.const_shape {values = dense<1> : tensor<4xindex>} : () -> !tosa.shape<4>
332341
%pad_const = "tosa.const"() {values = dense<3.14> : tensor<1xf32>} : () -> tensor<1xf32>
333-
// expected-error@+1 {{'tosa.pad' op expected padding tensor dim 0 to have size 6 (2*rank(shape1)) but got size 4}}
342+
// expected-error@+1 {{'tosa.pad' op padding tensor must have 3 * 2 = 6 elements, but got 4}}
334343
%1 = tosa.pad %arg0, %0, %pad_const : (tensor<13x21x3xf32>, !tosa.shape<4>, tensor<1xf32>) -> tensor<13x21x3xf32>
335344
return %1 : tensor<13x21x3xf32>
336345
}

mlir/test/Dialect/Tosa/invalid_extension.mlir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,11 @@ func.func @test_inexact_round_rescale(%arg0: tensor<13x21x3xi8>) -> tensor<13x21
407407

408408
// -----
409409

410-
func.func @test_pad_non_const(%arg0: tensor<13x21x3xi8>, %arg1: tensor<1xi8>) -> tensor<13x21x3xi8> {
410+
func.func @test_pad_non_const(%arg0: tensor<13x21x3xi8>, %arg1: tensor<1xi8>) -> tensor<13x22x4xi8> {
411411
%0 = tosa.const_shape {values = dense<[0, 0, 0, 1, 0, 1]> : tensor<6xindex>} : () -> !tosa.shape<6>
412412
// expected-error@+1 {{'tosa.pad' op expected compile time resolvable constant, but got variable value for operand #2}}
413-
%1 = tosa.pad %arg0, %0, %arg1 : (tensor<13x21x3xi8>, !tosa.shape<6>, tensor<1xi8>) -> tensor<13x21x3xi8>
414-
return %1 : tensor<13x21x3xi8>
413+
%1 = tosa.pad %arg0, %0, %arg1 : (tensor<13x21x3xi8>, !tosa.shape<6>, tensor<1xi8>) -> tensor<13x22x4xi8>
414+
return %1 : tensor<13x22x4xi8>
415415
}
416416

417417
// -----

0 commit comments

Comments
 (0)