Skip to content

Commit a79a0c5

Browse files
authored
[mlir][tensor] Simplify pad-like tensor pack and unpack (#92388)
Extend existing tensor patterns to simplify pad-like tensor pack/unpack into expand/collapse shape operations.
1 parent e3ed9e3 commit a79a0c5

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

Diff for: mlir/lib/Dialect/Tensor/Transforms/PackAndUnpackPatterns.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ struct SimplifyPackToExpandShape : public OpRewritePattern<PackOp> {
9191
RankedTensorType sourceType = packOp.getSourceType();
9292
if (failed(isPackOnInnerMostDim(rewriter, packOp)) &&
9393
failed(isPackOn1D(rewriter, packOp, sourceType.getShape(),
94-
packOp.getStaticTiles()))) {
94+
packOp.getStaticTiles())) &&
95+
!packOp.isLikePad()) {
9596
return failure();
9697
}
9798

@@ -152,7 +153,8 @@ struct SimplifyUnPackToCollapseShape : public OpRewritePattern<UnPackOp> {
152153
RankedTensorType destType = unpackOp.getDestType();
153154
if (failed(isUnpackOnInnerMostDim(rewriter, unpackOp)) &&
154155
failed(isPackOn1D(rewriter, unpackOp, destType.getShape(),
155-
unpackOp.getStaticTiles()))) {
156+
unpackOp.getStaticTiles())) &&
157+
!unpackOp.isLikeUnPad()) {
156158
return failure();
157159
}
158160

Diff for: mlir/test/Dialect/Tensor/simplify-pack-unpack.mlir

+128
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,131 @@ func.func @unpack_16x1x1x2_to_32x1(%arg0 : tensor<16x1x1x2xf32>) -> tensor<32x1x
266266
: tensor<16x1x1x2xf32> -> tensor<32x1xf32>
267267
return %unpack : tensor<32x1xf32>
268268
}
269+
270+
// -----
271+
272+
// CHECK-LABEL: func.func @pad_like_pack(
273+
// CHECK-SAME: %[[ARG0:.+]]: tensor<32x64xf32>)
274+
// CHECK: %[[EXPANDED:.+]] = tensor.expand_shape %[[ARG0]] {{\[}}[0, 1, 2], [3]] output_shape [1, 1, 32, 64] : tensor<32x64xf32> into tensor<1x1x32x64xf32>
275+
// CHECK: return %[[EXPANDED]] : tensor<1x1x32x64xf32>
276+
func.func @pad_like_pack(%arg0: tensor<32x64xf32>) -> tensor<1x1x32x64xf32> {
277+
%empty = tensor.empty() : tensor<1x1x32x64xf32>
278+
%0 = tensor.pack %arg0 inner_dims_pos = [0, 1] inner_tiles = [32, 64] into %empty : tensor<32x64xf32> -> tensor<1x1x32x64xf32>
279+
return %0 : tensor<1x1x32x64xf32>
280+
}
281+
282+
// -----
283+
284+
// CHECK-LABEL: func.func @pad_like_pack_with_outer_dims_perm(
285+
// CHECK-SAME: %[[ARG0:.+]]: tensor<32x64xf32>)
286+
// CHECK: %[[EXPANDED:.+]] = tensor.expand_shape %[[ARG0]] {{\[}}[0, 1, 2], [3]] output_shape [1, 1, 32, 64] : tensor<32x64xf32> into tensor<1x1x32x64xf32>
287+
// CHECK: return %[[EXPANDED]] : tensor<1x1x32x64xf32>
288+
func.func @pad_like_pack_with_outer_dims_perm(%arg0: tensor<32x64xf32>) -> tensor<1x1x32x64xf32> {
289+
%empty = tensor.empty() : tensor<1x1x32x64xf32>
290+
%0 = tensor.pack %arg0 outer_dims_perm = [1, 0] inner_dims_pos = [0, 1] inner_tiles = [32, 64] into %empty : tensor<32x64xf32> -> tensor<1x1x32x64xf32>
291+
return %0 : tensor<1x1x32x64xf32>
292+
}
293+
294+
// -----
295+
296+
// CHECK-LABEL: func.func @inner_pad_like_pack(
297+
// CHECK-SAME: %[[ARG0:.+]]: tensor<32x64xf32>)
298+
// CHECK: %[[EXPANDED:.+]] = tensor.expand_shape %[[ARG0]] {{\[}}[0], [1, 2]] output_shape [32, 1, 64] : tensor<32x64xf32> into tensor<32x1x64xf32>
299+
// CHECK: return %[[EXPANDED]] : tensor<32x1x64xf32>
300+
func.func @inner_pad_like_pack(%arg0: tensor<32x64xf32>) -> tensor<32x1x64xf32> {
301+
%empty = tensor.empty() : tensor<32x1x64xf32>
302+
%0 = tensor.pack %arg0 inner_dims_pos = [1] inner_tiles = [64] into %empty : tensor<32x64xf32> -> tensor<32x1x64xf32>
303+
return %0 : tensor<32x1x64xf32>
304+
}
305+
306+
// -----
307+
308+
// Do not simplify pack with inner dimension shuffling.
309+
// CHECK-LABEL: func.func @pad_and_inner_dim_shuffle_pack(
310+
// CHECK-SAME: %[[ARG0:.+]]: tensor<32x64xf32>)
311+
// CHECK: %[[EMPTY:.+]] = tensor.empty() : tensor<1x1x64x32xf32>
312+
// CHECK: %[[PACK:.+]] = tensor.pack %[[ARG0]] inner_dims_pos = [1, 0] inner_tiles = [64, 32] into %[[EMPTY]] : tensor<32x64xf32> -> tensor<1x1x64x32xf32>
313+
// CHECK: return %[[PACK]] : tensor<1x1x64x32xf32>
314+
func.func @pad_and_inner_dim_shuffle_pack(%arg0: tensor<32x64xf32>) -> tensor<1x1x64x32xf32> {
315+
%empty = tensor.empty() : tensor<1x1x64x32xf32>
316+
%0 = tensor.pack %arg0 inner_dims_pos = [1, 0] inner_tiles = [64, 32] into %empty : tensor<32x64xf32> -> tensor<1x1x64x32xf32>
317+
return %0 : tensor<1x1x64x32xf32>
318+
}
319+
320+
// -----
321+
322+
// Do not simplify pack with inner dimension transpose.
323+
// CHECK-LABEL: func.func @pad_like_pack_with_transpose(
324+
// CHECK-SAME: %[[ARG0:.+]]: tensor<32x64x16xf32>)
325+
// CHECK: %[[EMPTY:.+]] = tensor.empty() : tensor<32x1x16x64xf32>
326+
// CHECK: %[[PACK:.+]] = tensor.pack %[[ARG0]] inner_dims_pos = [1] inner_tiles = [64] into %[[EMPTY]] : tensor<32x64x16xf32> -> tensor<32x1x16x64xf32>
327+
// CHECK: return %[[PACK]] : tensor<32x1x16x64xf32>
328+
func.func @pad_like_pack_with_transpose(%arg0: tensor<32x64x16xf32>) -> tensor<32x1x16x64xf32> {
329+
%empty = tensor.empty() : tensor<32x1x16x64xf32>
330+
%0 = tensor.pack %arg0 inner_dims_pos = [1] inner_tiles = [64] into %empty : tensor<32x64x16xf32> -> tensor<32x1x16x64xf32>
331+
return %0 : tensor<32x1x16x64xf32>
332+
}
333+
334+
// -----
335+
336+
// CHECK-LABEL: func.func @unpad_like_unpack(
337+
// CHECK-SAME: %[[ARG0:.+]]: tensor<1x1x32x64xf32>)
338+
// CHECK: %[[COLLAPSED:.+]] = tensor.collapse_shape %[[ARG0]] {{\[}}[0, 1, 2], [3]] : tensor<1x1x32x64xf32> into tensor<32x64xf32>
339+
// CHECK: return %[[COLLAPSED]] : tensor<32x64xf32>
340+
func.func @unpad_like_unpack(%arg0: tensor<1x1x32x64xf32>) -> tensor<32x64xf32> {
341+
%empty = tensor.empty() : tensor<32x64xf32>
342+
%0 = tensor.unpack %arg0 inner_dims_pos = [0, 1] inner_tiles = [32, 64] into %empty : tensor<1x1x32x64xf32> -> tensor<32x64xf32>
343+
return %0 : tensor<32x64xf32>
344+
}
345+
346+
// -----
347+
348+
// CHECK-LABEL: func.func @unpad_like_unpack_with_outer_dims_perm(
349+
// CHECK-SAME: %[[ARG0:.+]]: tensor<1x1x32x64xf32>)
350+
// CHECK: %[[COLLAPSED:.+]] = tensor.collapse_shape %[[ARG0]] {{\[}}[0, 1, 2], [3]] : tensor<1x1x32x64xf32> into tensor<32x64xf32>
351+
// CHECK: return %[[COLLAPSED]] : tensor<32x64xf32>
352+
func.func @unpad_like_unpack_with_outer_dims_perm(%arg0: tensor<1x1x32x64xf32>) -> tensor<32x64xf32> {
353+
%empty = tensor.empty() : tensor<32x64xf32>
354+
%0 = tensor.unpack %arg0 outer_dims_perm = [1, 0] inner_dims_pos = [0, 1] inner_tiles = [32, 64] into %empty : tensor<1x1x32x64xf32> -> tensor<32x64xf32>
355+
return %0 : tensor<32x64xf32>
356+
}
357+
358+
// -----
359+
360+
// CHECK-LABEL: func.func @inner_unpad_like_unpack(
361+
// CHECK-SAME: %[[ARG0:.+]]: tensor<32x1x64xf32>)
362+
// CHECK: %[[COLLAPSED:.+]] = tensor.collapse_shape %[[ARG0]] {{\[}}[0], [1, 2]] : tensor<32x1x64xf32> into tensor<32x64xf32>
363+
// CHECK: return %[[COLLAPSED]] : tensor<32x64xf32>
364+
func.func @inner_unpad_like_unpack(%arg0: tensor<32x1x64xf32>) -> tensor<32x64xf32> {
365+
%empty = tensor.empty() : tensor<32x64xf32>
366+
%0 = tensor.unpack %arg0 inner_dims_pos = [1] inner_tiles = [64] into %empty : tensor<32x1x64xf32> -> tensor<32x64xf32>
367+
return %0 : tensor<32x64xf32>
368+
}
369+
370+
// -----
371+
372+
// Do not simplify unpack with inner dimension shuffling.
373+
// CHECK-LABEL: func.func @unpad_and_inner_dim_shuffle_pack(
374+
// CHECK-SAME: %[[ARG0:.+]]: tensor<1x1x32x64xf32>)
375+
// CHECK: %[[EMPTY:.+]] = tensor.empty() : tensor<64x32xf32>
376+
// CHECK: %[[UNPACK:.+]] = tensor.unpack %[[ARG0]] inner_dims_pos = [1, 0] inner_tiles = [32, 64] into %[[EMPTY]] : tensor<1x1x32x64xf32> -> tensor<64x32xf32>
377+
// CHECK: return %[[UNPACK]] : tensor<64x32xf32>
378+
func.func @unpad_and_inner_dim_shuffle_pack(%arg0: tensor<1x1x32x64xf32>) -> tensor<64x32xf32> {
379+
%empty = tensor.empty() : tensor<64x32xf32>
380+
%0 = tensor.unpack %arg0 inner_dims_pos = [1, 0] inner_tiles = [32, 64] into %empty : tensor<1x1x32x64xf32> -> tensor<64x32xf32>
381+
return %0 : tensor<64x32xf32>
382+
}
383+
384+
// -----
385+
386+
// Do not simplify unpack with inner dimension transpose.
387+
// CHECK-LABEL: func.func @unpad_like_unpack_with_transpose(
388+
// CHECK-SAME: %[[ARG0:.+]]: tensor<32x1x16x64xf32>)
389+
// CHECK: %[[EMPTY:.+]] = tensor.empty() : tensor<32x64x16xf32>
390+
// CHECK: %[[UNPACK:.+]] = tensor.unpack %[[ARG0]] inner_dims_pos = [1] inner_tiles = [64] into %[[EMPTY]] : tensor<32x1x16x64xf32> -> tensor<32x64x16xf32>
391+
// CHECK: return %[[UNPACK]] : tensor<32x64x16xf32>
392+
func.func @unpad_like_unpack_with_transpose(%arg0: tensor<32x1x16x64xf32>) -> tensor<32x64x16xf32> {
393+
%empty = tensor.empty() : tensor<32x64x16xf32>
394+
%0 = tensor.unpack %arg0 inner_dims_pos = [1] inner_tiles = [64] into %empty : tensor<32x1x16x64xf32> -> tensor<32x64x16xf32>
395+
return %0 : tensor<32x64x16xf32>
396+
}

0 commit comments

Comments
 (0)