Skip to content

Commit a72fcc7

Browse files
committed
Update(2) [mlir][linalg] Update pack and unpack documentation
Signed-off-by: Christopher McGirr <[email protected]>
1 parent 565ee3e commit a72fcc7

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

mlir/include/mlir/Dialect/Linalg/IR/LinalgRelayoutOps.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def Linalg_PackOp : Linalg_RelayoutOp<"pack", [
100100
`inner_dims_pos` (mandatory) specifies `k` source tensor dimensions that are
101101
being tiled, where `0 <= k <= n`.
102102
- `inner_dims_pos[i]` specifies the source tensor dimension tiled by
103-
`inner_tiles[i]` where `0 <= i < k`.
103+
`inner_tiles[i]` where `0 <= i < k`. All the values in `inner_dims_pos` are
104+
within [0, n).
104105
- The tiled dimensions (of size `inner_tiles`) are added to the end of the
105106
result tensor in the order in which they appear, i.e.
106107
`shape(result)[rank(result) + i] = inner_tiles[i]` for `0 <= i < k`.
@@ -275,7 +276,7 @@ def Linalg_UnPackOp : Linalg_RelayoutOp<"unpack"> {
275276
packed source tensor. The source tensor (i.e. packed tensor) dimensions can
276277
be unpacked given `inner_dims_pos` as follows.
277278
- For `0 <= i < k` the following relationship holds:
278-
`shape(result)[inner_dims_pos[i]] = shape(source)[n-k+i] + shape(source)[inner_dims_pos[i]]`.
279+
`shape(result)[inner_dims_pos[i]] <= shape(source)[n-k+i] * shape(source)[inner_dims_pos[i]]`.
279280
- For `0 <= j < n-k` and `j` not in `inner_dims_pos` the following relationship holds:
280281
`shape(result)[j] = shape(source)[j]`.
281282

mlir/test/Dialect/Linalg/invalid.mlir

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,9 +1824,8 @@ func.func @unpack_invalid_outer_dims_perm(%source: tensor<128x256xf32>, %dest: t
18241824

18251825
// -----
18261826

1827-
// Here we have the source tensor being tiled as: `source[1] / 32` and `source[0] / 16` but the inner_dims_pos does not imply
1828-
// a transpose of the outer dimensions for the result tensor. The tiled dimensions appear in the result tensor in the order
1829-
// they appear in the source tensor, i.e. 16x4x32x16
1827+
// The outer dims in the output tensor are incorrectly/unexpectedly transposed.
1828+
// This could be fixed by adding `outer_dims_perm = [1, 0]` (the default value assumes no transpose).
18301829
func.func @pack_invalid_result_shape(%input: tensor<256x128xf32>, %output: tensor<4x16x32x16xf32>) -> tensor<4x16x32x16xf32> {
18311830
// expected-error@+1 {{the shape of output is not large enough to hold the packed data. Expected at least 'tensor<16x4x32x16xf32>', got 'tensor<4x16x32x16xf32>'}}
18321831
%0 = linalg.pack %input inner_dims_pos = [1, 0] inner_tiles = [32, 16] into %output : tensor<256x128xf32> -> tensor<4x16x32x16xf32>

mlir/test/Dialect/Linalg/named-ops.mlir

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,12 +2771,12 @@ func.func @pad_and_pack_partially_dynamic(%source: tensor<?x?xf32>, %dest: tenso
27712771

27722772
// -----
27732773

2774-
func.func @pack_descending_inner_dims_with_padding(%source: tensor<1x5x7xf32>, %dest: tensor<1x3x2x4x2xf32>, %pad: f32) -> tensor<1x3x2x4x2xf32> {
2774+
func.func @pack_transposed_inner_dims_with_padding(%source: tensor<1x5x7xf32>, %dest: tensor<1x3x2x4x2xf32>, %pad: f32) -> tensor<1x3x2x4x2xf32> {
27752775
%0 = linalg.pack %source padding_value(%pad : f32) inner_dims_pos = [2, 1] inner_tiles = [4, 2] into %dest : tensor<1x5x7xf32> -> tensor<1x3x2x4x2xf32>
27762776
return %0 : tensor<1x3x2x4x2xf32>
27772777
}
27782778

2779-
// CHECK-LABEL: func.func @pack_descending_inner_dims_with_padding(
2779+
// CHECK-LABEL: func.func @pack_transposed_inner_dims_with_padding(
27802780
// CHECK-SAME: %[[SOURCE:.*]]: tensor<1x5x7xf32>,
27812781
// CHECK-SAME: %[[DEST:.*]]: tensor<1x3x2x4x2xf32>,
27822782
// CHECK-SAME: %[[PAD:.*]]: f32)
@@ -2834,6 +2834,38 @@ func.func @unpack_non_adjacent_inner_dims(%source: tensor<10x1x3x4x2xf32>, %dest
28342834

28352835
// -----
28362836

2837+
func.func @pack_implementing_transpose(%source: tensor<3x5x7xf32>, %dest: tensor<3x7x5xf32>) -> tensor<3x7x5xf32> {
2838+
%0 = linalg.pack %source outer_dims_perm = [0, 2, 1] inner_dims_pos = [] inner_tiles = [] into %dest : tensor<3x5x7xf32> -> tensor<3x7x5xf32>
2839+
return %0 : tensor<3x7x5xf32>
2840+
}
2841+
2842+
// CHECK-LABEL: func.func @pack_implementing_transpose(
2843+
// CHECK-SAME: %[[SOURCE:.*]]: tensor<3x5x7xf32>,
2844+
// CHECK-SAME: %[[DEST:.*]]: tensor<3x7x5xf32>)
2845+
// CHECK: %{{.*}} = linalg.pack
2846+
// CHECK-SAME: outer_dims_perm = [0, 2, 1]
2847+
// CHECK-SAME: inner_dims_pos = []
2848+
// CHECK-SAME: inner_tiles = []
2849+
// CHECK-SAME: into %[[DEST]] : tensor<3x5x7xf32> -> tensor<3x7x5xf32>
2850+
2851+
// -----
2852+
2853+
func.func @unpack_implementing_transpose(%source: tensor<3x7x5xf32>, %dest: tensor<3x5x7xf32>) -> tensor<3x5x7xf32> {
2854+
%0 = linalg.unpack %source outer_dims_perm = [0, 2, 1] inner_dims_pos = [] inner_tiles = [] into %dest : tensor<3x7x5xf32> -> tensor<3x5x7xf32>
2855+
return %0 : tensor<3x5x7xf32>
2856+
}
2857+
2858+
// CHECK-LABEL: func.func @unpack_implementing_transpose(
2859+
// CHECK-SAME: %[[SOURCE:.*]]: tensor<3x7x5xf32>,
2860+
// CHECK-SAME: %[[DEST:.*]]: tensor<3x5x7xf32>)
2861+
// CHECK: %{{.*}} = linalg.unpack
2862+
// CHECK-SAME: outer_dims_perm = [0, 2, 1]
2863+
// CHECK-SAME: inner_dims_pos = []
2864+
// CHECK-SAME: inner_tiles = []
2865+
// CHECK-SAME: into %[[DEST]] : tensor<3x7x5xf32> -> tensor<3x5x7xf32>
2866+
2867+
// -----
2868+
28372869
func.func @unpack_fully_dynamic(%source: tensor<?x?x?x?xf32>, %dest: tensor<?x?xf32>, %tile_n : index, %tile_m : index) -> tensor<?x?xf32> {
28382870
%0 = linalg.unpack %source inner_dims_pos = [0, 1] inner_tiles = [%tile_n, %tile_m] into %dest : tensor<?x?x?x?xf32> -> tensor<?x?xf32>
28392871
return %0 : tensor<?x?xf32>

0 commit comments

Comments
 (0)