Skip to content

[Codegen] Add pass to decompose pack unpack ops at dispatch boundaries - #18852

Merged
Max191 merged 3 commits into
iree-org:mainfrom
Max191-agents:decompose-boundary-pack-unpack
Oct 24, 2024
Merged

Max191 merged 3 commits into
iree-org:mainfrom
Max191-agents:decompose-boundary-pack-unpack

Conversation

@Max191

@Max191 Max191 commented Oct 21, 2024

Copy link
Copy Markdown
Contributor

This PR adds a wrapper pass around DecomposePackUnPackOps, which adds a control function for decomposing only packs and unpacks whose reshapes can be folded with dispatch tensor loads/stores.

This PR also removes the public pass constructor with a control function, opting to use wrapper passes in place of constructing passes with arbitrary control functions. This is better for creating simple bug repros, since the control function is part of the pass.

@Max191

Max191 commented Oct 21, 2024

Copy link
Copy Markdown
Contributor Author

Currently depends on #18843 for some StaticValueUtils.

Comment on lines +120 to +121
using impl::DecomposeBoundaryPackUnPackOpsPassBase<
DecomposeBoundaryPackUnPackOpsPass>::

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using impl::DecomposeBoundaryPackUnPackOpsPassBase<
DecomposeBoundaryPackUnPackOpsPass>::
using impl::DecomposeBoundaryPackUnPackOpsPassBase::

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this works?

OpPassManager &funcPassManager = maybeFuncPassManager.value();
funcPassManager.addPass(createDecomposePackUnPackOpsPass(
tileOuterToOne, /*useOnlyReshapes=*/true,
/*controlFn=*/isFoldableIntoInterfaceTensor));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need a whole pass for this? You're just using the control function on the existing pass; I would factor out this pass and move the control function into a shared location.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason to have a separate pass is so that it is possible to run the pass using the command line. The other way to do this would be to add an option pointing to a specific control function, but I opted for adding a new pass because useOnlyReshapes does not exist in DecomposeBoundaryPackUnPackOps (since it should always be true for this new pass), so I thought it was more clear this way.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invoking a pass inside another pass is not the best way to do that then because it leads to confusing IR dumps. Factor DecomposePackUnPackOpsPass::runOnOperation into its own function and call the same function from this pass with different options. The other way to do it is with custom test pass pipelines, but the logic in the control function is involved enough that a separate pass makes sense

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a shared runOnOperation function, so now there is no nested pass call. PTAL

Signed-off-by: Max Dawkins <[email protected]>
@Max191
Max191 force-pushed the decompose-boundary-pack-unpack branch from 20ef780 to 34c56e4 Compare October 21, 2024 23:38
@Max191
Max191 merged commit e1469b2 into iree-org:main Oct 24, 2024
Max191 added a commit that referenced this pull request Oct 24, 2024
…18565)

This switches the TileAndFuse pipeline to use scf.forall distribution.
Using scf.forall distribution also requires some changes to the pass
ordering in the TileAndFuse pipeline, which is also handled by this PR:
1. The main difference is that PackToIntrinsics happens before workgroup
distribution. Otherwise, collapse_shape ops can end up at the end of the
workgroup forall, and an extra buffer is created.
2. Pack decomposition is now staged, with packs/unpacks at the function
boundaries being decomposed early before workgroup decomposition, and
the rest being decomposed after reduction tiling as before. This
prevents unpacks being fused into the workgroup forall and causing the
same problem as in (1).
3. `ConcretizeMmaShapes` now runs before workgroup tiling as well,
so the resulting collapse_shape on the multi_mma op result can be
propagated to the function boundary before any tiling. This is also to
avoid the same problem as in (1).

The lowering configs on the MMA path have also changed, since they now
need to account for inner tile sizes of packing.

depends on #18852

Signed-off-by: Max Dawkins <[email protected]>
@Max191
Max191 deleted the decompose-boundary-pack-unpack branch October 25, 2024 14:20

@hanhanW hanhanW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we at least add a test for dynamic shapes? @Max191 can you send a follow-up PR?

%1 = hal.interface.binding.subspan layout(#pipeline_layout) binding(0) alignment(64) offset(%c0) : !flow.dispatch.tensor<readwrite:tensor<16x16xf32>>
%src = flow.dispatch.tensor.load %0, offsets = [0, 0, 0, 0], sizes = [4, 4, 4, 4], strides = [1, 1, 1, 1] : !flow.dispatch.tensor<readonly:tensor<4x4x4x4xf32>> -> tensor<4x4x4x4xf32>
%dest = flow.dispatch.tensor.load %1, offsets = [0, 0], sizes = [16, 16], strides = [1, 1] : !flow.dispatch.tensor<readwrite:tensor<16x16xf32>> -> tensor<16x16xf32>
%unpack = tensor.unpack %src inner_dims_pos = [0, 1] inner_tiles = [4, 4] into %dest : tensor<4x4x4x4xf32> -> tensor<16x16xf32>

@hanhanW hanhanW Oct 25, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps we should add a util.optimization_barrier on the %src which makes sure that the test is for folding unpack into the dest.

EDIT: I did not check all the test, but please take a look at other tests as well.

%src = flow.dispatch.tensor.load %0, offsets = [0, 0], sizes = [16, 16], strides = [1, 1] : !flow.dispatch.tensor<readonly:tensor<16x16xf32>> -> tensor<16x16xf32>
%dest = flow.dispatch.tensor.load %1, offsets = [0, 0, 0, 0], sizes = [4, 4, 4, 4], strides = [1, 1, 1, 1] : !flow.dispatch.tensor<readwrite:tensor<4x4x4x4xf32>> -> tensor<4x4x4x4xf32>
%empty = tensor.empty() : tensor<16x16xf32>
%copy = linalg.copy ins(%src : tensor<16x16xf32>) outs(%empty : tensor<16x16xf32>) -> tensor<16x16xf32>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: we can use optimization barrier instead of linalg.copy.

@Max191

Max191 commented Oct 25, 2024

Copy link
Copy Markdown
Contributor Author

Can we at least add a test for dynamic shapes? @Max191 can you send a follow-up PR?

Sure, I'll add some more tests

Eliasj42 pushed a commit that referenced this pull request Oct 31, 2024
#18852)

This PR adds a wrapper pass around DecomposePackUnPackOps, which adds a
control function for decomposing only packs and unpacks whose reshapes
can be folded with dispatch tensor loads/stores.

This PR also removes the public pass constructor with a control
function, opting to use wrapper passes in place of constructing passes
with arbitrary control functions. This is better for creating simple bug
repros, since the control function is part of the pass.

---------

Signed-off-by: Max Dawkins <[email protected]>
Signed-off-by: Elias Joseph <[email protected]>
Eliasj42 pushed a commit that referenced this pull request Oct 31, 2024
…18565)

This switches the TileAndFuse pipeline to use scf.forall distribution.
Using scf.forall distribution also requires some changes to the pass
ordering in the TileAndFuse pipeline, which is also handled by this PR:
1. The main difference is that PackToIntrinsics happens before workgroup
distribution. Otherwise, collapse_shape ops can end up at the end of the
workgroup forall, and an extra buffer is created.
2. Pack decomposition is now staged, with packs/unpacks at the function
boundaries being decomposed early before workgroup decomposition, and
the rest being decomposed after reduction tiling as before. This
prevents unpacks being fused into the workgroup forall and causing the
same problem as in (1).
3. `ConcretizeMmaShapes` now runs before workgroup tiling as well,
so the resulting collapse_shape on the multi_mma op result can be
propagated to the function boundary before any tiling. This is also to
avoid the same problem as in (1).

The lowering configs on the MMA path have also changed, since they now
need to account for inner tile sizes of packing.

depends on #18852

Signed-off-by: Max Dawkins <[email protected]>
Signed-off-by: Elias Joseph <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants