Skip to content

Introduce Collapse Pass - #11713

Merged
grypp merged 1 commit into
iree-org:mainfrom
grypp:collapsePass
Jan 10, 2023
Merged

grypp merged 1 commit into
iree-org:mainfrom
grypp:collapsePass

Conversation

@grypp

@grypp grypp commented Jan 5, 2023

Copy link
Copy Markdown
Contributor

This is a follow-up PR. #11295 had already implemented the collapsing logic for linalg.generic operations in dispatch.region. This PR extracts that logic in a new Collapse Pass.

The new pass performs the following steps. The first and third steps here are new compared to the previous one as we are creating the Pass.

  1. Finds linalg.generic that has no producer in dispatch.region, which means there will only be one linalg.generic in the region.
  2. Collapse dimensions of the tensors in linalg.generic if possible
  3. Generates new workload region for the collapsed Op and new dispatch.region

PR also removes collapse flag and changes a test that was using accordingly.

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice! Overall looks good to me. Just a few nits!


// IndexOp allows accesing induction variables. Collapsing might cause
// performance regression, so we disable it.
if (genericOp.hasIndexSemantics()) return false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Are we sure about this? I think the core transformation supports this. We should just drop this constraint....

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.

Yes you are right, the core transformation supports this. But it causes a performance regression in one CPU benchmark. I turned this off for a benchmark.

Here is the reproducer of slowdown.
https://gist.github.com/grypp/b8c3f8fd6c2720b5a4f57c497e829812#file-collapse-mlir-L64-L67

I can look at the data access pattern instead of the hasIndexSemantics, but I am not sure what's the best way to do that.

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.

The collapsing shouldn’t happen when the loop index is used directly. So we should figure out why the upstream patterns allows it to happen. We don’t need to investigate the regression itself which is most likely due to wrong a miscompile.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I dont see a reason why collapsing should happen. Upstream pattern handles it correctly, I dont see any problem with it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

W.R.T regression, if it causes regression we can leave it off for now. Upstream it handles it for completeness. It is up to the user to either handle it or not.

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.

Let's file an issue and put the github issue ID to TODO here?

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.

As an alternative idea, I could create a flag and use it to turn off collapse in this regressed benchmark.

static FailureOr<linalg::GenericOp> findRootGenericOp(
DispatchRegionOp regionOp) {
auto &ops = regionOp.getBody().front().getOperations();
if (ops.size() != 2) return failure();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is too specific. I think we should just walk through the region and find the number of ops that are "compute" ops. You can treat all ops that implement TilingInterface as compute ops. So then you can check the number of these ops is 1?

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 implemented what you described here. On top of that I added if (ops.size() != 2) to make sure the root has no producer. The original code was doing similar.

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you!


// IndexOp allows accesing induction variables. Collapsing might cause
// performance regression, so we disable it.
if (genericOp.hasIndexSemantics()) return false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

W.R.T regression, if it causes regression we can leave it off for now. Upstream it handles it for completeness. It is up to the user to either handle it or not.


// IndexOp allows accesing induction variables. Collapsing might cause
// performance regression, so we disable it.
if (genericOp.hasIndexSemantics()) return false;

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.

Let's file an issue and put the github issue ID to TODO here?

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

@grypp is there anything missing for this pass to land? FYI #11770 seems related to the collapsing.

@grypp
grypp force-pushed the collapsePass branch 2 times, most recently from d3f7e5f to 425e34b Compare January 10, 2023 09:51
This is a follow-up PR. iree-org#11295 had already implemented the collapsing logic. This PR introduces a new Collapse Pass for `linalg.generic` operations in `dispatch.region`.

The new Pass implements the following steps:
1. Finds `linalg.generic` that has no producer in `dispatch.region`, which means there will only be one `linalg.generic` in the region.
2. Collapse dimensions of the tensors in `linalg.generic` if possible
3. Generates new workload region for the collapsed Op and new `dispatch.region`

PR also removes collapse flag and changes a test that was using accordingly.
@grypp
grypp merged commit 37983c6 into iree-org:main Jan 10, 2023
pzread pushed a commit to pzread/iree that referenced this pull request Jan 11, 2023
jpienaar pushed a commit that referenced this pull request May 1, 2023
This is a follow-up PR. #11295 had already implemented the collapsing
logic for `linalg.generic` operations in `dispatch.region`. This PR
extracts that logic in a new Collapse Pass.

The new pass performs the following steps. The first and third steps
here are new compared to the previous one as we are creating the Pass.
1. Finds `linalg.generic` that has no producer in `dispatch.region`,
which means there will only be one `linalg.generic` in the region.
2. Collapse dimensions of the tensors in `linalg.generic` if possible
3. Generates new workload region for the collapsed Op and new
`dispatch.region`

PR also removes collapse flag and changes a test that was using
accordingly.
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.

4 participants