-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[MLIR] Add allow Insert/extract slice option to pack/unpack op #117340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
012f6d4
[NFC] Add allowInsertSliceLowering to packOp and allowExtractSliceLow…
jerryyin 46b7202
This commit add test cases to allowInsertSliceLowering and
jerryyin 0fa5401
Address review requests
jerryyin 671829e
Adding test cases to allowInsertSliceLowering and allowExtractSliceLo…
jerryyin 3ad8cd6
Add test to verify pack/producer unpack/consumer fusion
jerryyin 68b5328
Adding additional negative test cases
jerryyin f6c54e8
Add additional LIT variable
jerryyin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
mlir/test/Dialect/Linalg/transform-tile-and-fuse-pack-unpack.mlir
jerryyin marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // RUN: mlir-opt %s --transform-interpreter --split-input-file -canonicalize | FileCheck %s | ||
|
|
||
| // For pack op, we use lowerPadLikeWithInsertSlice = false to ensure no insert_slice is generated. | ||
| // This allows linalg.transpose to be fused as a producer operation. Alternatively, without this attribute | ||
| // insert_slice will be generated and fusion blocked. | ||
|
|
||
| module { | ||
| // CHECK-label: func @fuse_pack_as_producer | ||
| // CHECK: scf.forall {{.*}} { | ||
| // CHECK: linalg.transpose | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // CHECK: linalg.generic | ||
| // CHECK: scf.forall.in_parallel | ||
| // CHECK: } | ||
| func.func @fuse_pack_as_producer(%src: tensor<128x256xf32>, %other: tensor<4x4x128x256xf32>) | ||
| -> tensor<4x4x128x256xf32> { | ||
| %dest = tensor.empty() : tensor<1x1x128x256xf32> | ||
| %pack = tensor.pack %src inner_dims_pos = [0, 1] inner_tiles = [128, 256] | ||
| into %dest : tensor<128x256xf32> -> tensor<1x1x128x256xf32> | ||
|
|
||
| %out = tensor.empty() : tensor<4x4x128x256xf32> | ||
| %res = linalg.generic | ||
| {indexing_maps = [affine_map<(i, j, k, l) -> (0, 0, k, l)>, | ||
| affine_map<(i, j, k, l) -> (i, j, k, l)>, | ||
| affine_map<(i, j, k, l) -> (i, j, k, l)>], | ||
| iterator_types = ["parallel", "parallel", "parallel", "parallel"]} | ||
| ins(%pack, %other: tensor<1x1x128x256xf32>, tensor<4x4x128x256xf32>) | ||
| outs(%out: tensor<4x4x128x256xf32>) { | ||
| ^bb0(%pack_elem: f32, %other_elem: f32, %out_elem: f32): | ||
| %r = arith.addf %pack_elem, %other_elem : f32 | ||
| linalg.yield %r : f32 | ||
| } -> tensor<4x4x128x256xf32> | ||
|
|
||
| return %res : tensor<4x4x128x256xf32> | ||
| } | ||
|
|
||
| module attributes {transform.with_named_sequence} { | ||
| transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { | ||
| // Find and lower pack operation. | ||
| %pack = transform.structured.match ops{["tensor.pack"]} in %arg1 | ||
| : (!transform.any_op) -> !transform.op<"tensor.pack"> | ||
| %paded, %expanded, %transpose = transform.structured.lower_pack %pack {lowerPadLikeWithInsertSlice = false} | ||
| : (!transform.op<"tensor.pack">) | ||
| -> (!transform.op<"tensor.pad">, | ||
| !transform.op<"tensor.expand_shape">, | ||
| !transform.op<"linalg.transpose">) | ||
|
|
||
| %root = transform.structured.match ops{["linalg.generic"]} in %arg1 | ||
| : (!transform.any_op) -> !transform.any_op | ||
| // Tile the lialg operation with parallel forall loop tiling [4, 4]. | ||
| %tiled_op, %forall_op = transform.structured.tile_using_forall %root num_threads [4, 4] | ||
| : (!transform.any_op) -> (!transform.any_op, !transform.any_op) | ||
|
|
||
| // Fuse the transpose operation into the tiled loop. | ||
| transform.structured.fuse_into_containing_op %transpose into %forall_op | ||
| : (!transform.op<"linalg.transpose">, !transform.any_op) -> (!transform.any_op, !transform.any_op) | ||
| transform.yield | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ----- | ||
| // For unpack op, we use lowerUnpadLikeWithExtractSlice = false to ensure no extract_slice is generated. | ||
| // This allows linalg.transpose to be fused as a consumer operation. Alternatively, without this attribute | ||
| // extract_slice will be generated and fusion blocked. | ||
|
|
||
| module { | ||
| // CHECK-label: func @fuse_unpack_as_consumer | ||
| // CHECK: scf.forall {{.*}} { | ||
| // CHECK: linalg.generic | ||
| // CHECK: linalg.transpose | ||
| // CHECK: scf.forall.in_parallel | ||
| // CHECK: } | ||
| func.func @fuse_unpack_as_consumer(%src: tensor<4x4x128x256xf32>, %other: tensor<4x4x128x256xf32>) | ||
| -> tensor<128x256xf32> { | ||
| %out = tensor.empty() : tensor<1x1x128x256xf32> | ||
| %res = linalg.generic | ||
| {indexing_maps = [affine_map<(i, j, k, l) -> (i, j, k, l)>, | ||
| affine_map<(i, j, k, l) -> (i, j, k, l)>, | ||
| affine_map<(i, j, k, l) -> (0, 0, k, l)>], | ||
| iterator_types = ["parallel", "parallel", "parallel", "parallel"]} | ||
| ins(%src, %other: tensor<4x4x128x256xf32>, tensor<4x4x128x256xf32>) | ||
| outs(%out: tensor<1x1x128x256xf32>) { | ||
| ^bb0(%unpack_elem: f32, %other_elem: f32, %out_elem: f32): | ||
| %r = arith.addf %unpack_elem, %other_elem : f32 | ||
| linalg.yield %r : f32 | ||
| } -> tensor<1x1x128x256xf32> | ||
|
|
||
| %dest = tensor.empty() : tensor<128x256xf32> | ||
| %unpack = tensor.unpack %res inner_dims_pos = [0, 1] inner_tiles = [128, 256] | ||
| into %dest : tensor<1x1x128x256xf32> -> tensor<128x256xf32> | ||
|
|
||
| return %unpack : tensor<128x256xf32> | ||
| } | ||
|
|
||
| module attributes {transform.with_named_sequence} { | ||
| transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { | ||
| // Find and lower unpack operation. | ||
| %unpack = transform.structured.match ops{["tensor.unpack"]} in %arg1 | ||
| : (!transform.any_op) -> !transform.op<"tensor.unpack"> | ||
| transform.structured.lower_unpack %unpack {lowerUnpadLikeWithExtractSlice = false} | ||
| : (!transform.op<"tensor.unpack">) | ||
| -> (!transform.op<"tensor.empty">, | ||
| !transform.op<"linalg.transpose">, | ||
| !transform.op<"tensor.collapse_shape">, | ||
| !transform.op<"tensor.extract_slice">) | ||
|
|
||
| %root = transform.structured.match ops{["linalg.generic"]} in %arg1 | ||
| : (!transform.any_op) -> !transform.any_op | ||
| // Tile the lialg operation with parallel forall loop tiling [4, 4]. | ||
| %tiled_op, %forall_op = transform.structured.tile_using_forall %root num_threads [4, 4] | ||
| : (!transform.any_op) -> (!transform.any_op, !transform.any_op) | ||
|
|
||
| // Fuse the consumer operation into the tiled loop. | ||
| %slice_op = transform.structured.match ops{["tensor.parallel_insert_slice"]} in %forall_op | ||
| : (!transform.any_op) -> !transform.op<"tensor.parallel_insert_slice"> | ||
| transform.test.fuse_consumer %slice_op | ||
| : (!transform.op<"tensor.parallel_insert_slice">) -> (!transform.any_op, !transform.any_op) | ||
| transform.yield | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.