Skip to content

Collapse linalg.generic - #11295

Merged
grypp merged 10 commits into
iree-org:mainfrom
grypp:parallel4d
Dec 16, 2022
Merged

grypp merged 10 commits into
iree-org:mainfrom
grypp:parallel4d

Conversation

@grypp

@grypp grypp commented Nov 25, 2022 •

Copy link
Copy Markdown
Contributor

PR implements collapsing linalg.generic. It first identifies the collapsible parallel dimensions and collapses them all. It does collapsing on the shapes rather than loops. Therefore, it does not introduce any arithmetic to calculate loop indices and etc. When there are reduction it can still collapse parallel loops but it does not mix them.

It finds the longest same sequence in each AffineMap. There can be multiple. For example, for the following case it is d1, d3, d0. Here the d1, d3, d0 loops are not nested; there are other loop(s) in between. But they're all parallel loops, so it's safe to interchange them out. After interchanging, it is also safe to collapse them.

indexing_maps = [affine_map<(d0, d1, d2, d3, d4) -> (d1, d3, d0)>, 
                 affine_map<(d0, d1, d2, d3, d4) -> (d2, d1, d3, d0, d4)>]

After collapsing, iree can parallelize more dimensions of the linalg.generic, this yields significant performance improvement.

Current limitations:

  1. Dynamic tensor shapes: Generating tensor.expand_shape with dynamic tensors is ambiguous. It is a known limitation of MLIR. It is possible to solve that. See RFC: https://discourse.llvm.org/t/rfc-add-explicit-shape-inputs-to-tensor-expand-shape/65952

  2. Non-contiguous loops. Current mechanism does collapsing on tensor shapes, not on the loops. If there is non-contiguous loops (like transpose), collapsing tensor would change behavior. Support distribution to more than 3 dimension at the workgroup level #11385 tackles this problem by linearizing the workgroup id. Alternative idea is to linearizing loops by using https://reviews.llvm.org/D129699.

@grypp
grypp marked this pull request as draft November 25, 2022 13:00
@stellaraccident

Copy link
Copy Markdown
Collaborator

Nice! Interested in any quantification you have of the performance impact when you have it (no rush)!

@grypp

grypp commented Nov 25, 2022

Copy link
Copy Markdown
Contributor Author

Thank you for looking at it. I first need to fix the limitations to test it with real benchmarks. Right now collapsing is very conservative :) I park this PR here to show my way to @MaheshRavishankar and @ThomasRaoux .

Nevertheless I can show a sneak peek for linalg.generic below

linalg.generic {indexing_maps = [#map, #map1], 
iterator_types = ["parallel", "parallel", "parallel", "parallel", "parallel", "parallel"]} 
ins(%0 : tensor<32x4x16x32x32x32xf32>) outs(%1 : tensor<32x4x16x32x32x32xf32>) {
    ^bb0(%in: f32, %out: f32):
      linalg.yield %in : f32
    } -> tensor<32x4x16x32x32x32xf32>

If I run this code on nvidia Quadro P1000, below is the result of baseline iree. A problem here is that iree only parallelizes the 3 innermost loops, the remaining 3 outermost loops of linalg.generic are run sequentially, which increases the number of alive registers.

Duration            Grid Size      Block Size     Regs* 
24.365ms            (1 32 32)        (64 1 1)        20

If I collapse linalg.generic to 1D I obtain the following result. It runs much faster. Now iree selects larger grid size which can parallelize all loops of linalg.generic. This also reduces number registers because there is no sequential outermost loops anymore.

Duration            Grid Size      Block Size     Regs* 
8.2951ms        (1048576 1 1)        (64 1 1)         8        

Note: As far as I can see from the profiler, the Duration includes page migration (host to device and vice versa), so it's not just the duration of the cuda kernel. I guess iree is using cuda managed memory here. Anyway, I can't just calculate the speedup.

@nicolasvasilache

Copy link
Copy Markdown
Contributor

You could try to add --iree-hal-benchmark-dispatch-repeat-count=5 to the iree-compile command so that the kernel runs 5 times and ignore the 1st result.

Not sure what your flow is but you could reverse-engineer the commands in this script to get what yo uwant: https://github.com/nicolasvasilache/nicolas.vasilache.github.io/blob/master/.venv/mlirdev/bin/activate

@grypp
grypp force-pushed the parallel4d branch 3 times, most recently from 31962bd to 4ce26e3 Compare December 2, 2022 09:52
@grypp
grypp marked this pull request as ready for review December 2, 2022 10:32

@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.

I am assuming this goes in after #11313 . If that is the case there needs to be a linearization of the workgroup counts as well. Should be easy to add.

@grypp

grypp commented Dec 5, 2022

Copy link
Copy Markdown
Contributor Author

I am assuming this goes in after #11313 . If that is the case there needs to be a linearization of the workgroup counts as well. Should be easy to add.

It is better to merge this after #11313. Yes you are right, in this case, the PR needs to linearize workgroup counts.

@hanhanW

hanhanW commented Dec 6, 2022

Copy link
Copy Markdown
Contributor

@dcaballe FYI that this is the thing we've discussed before. It collapses 3D+ elementwise generic ops.

@grypp
grypp force-pushed the parallel4d branch 3 times, most recently from 89c52ee to c9b43cd Compare December 8, 2022 10:25
@grypp

grypp commented Dec 8, 2022

Copy link
Copy Markdown
Contributor Author

Thanks for everyone for reviewing this PR. I addressed your comments. Here are the changes:

  1. Check isProjectedPermutation
  2. Can collapse cases like below
  affine_map<(d0, d1, d2, d3) -> (d1, d2, d3)>, 
  affine_map<(d0, d1, d2, d3) -> (d1, d2, d3, d0)>
  1. Added collapse flag to FormDispatchRegionsPass. It can be opt-out.

PS: I have a spirv fix in cherry-pick llvm. I put it here to see if the tf_integration tests would pass, and all passed :) I will submit the cherry-pick in PR #11469.

@grypp

grypp commented Dec 8, 2022

Copy link
Copy Markdown
Contributor Author

@hanhanW and @dcaballe , I'm interested if you have any cases where collapsing 3D+ is useful. I can add them as tests. Or if it's not supported I can take a look at why.

@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.

Overall looks good to me, just some nits.

@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.

Overall looks good to me, just few nits!

IREE parallelizes up to 3 dimensions; if a `linalg.generic` has more than 3 dimensions, the outer dimensions are not parallelized and this leads to poor performance for many reasons: many alive registers, non utilized lanes in a warp, and etc.

PR implements collapsing for 3D+ `linalg.generic`. It tries to collapse all `parallel` and contiguous loops. If there are `reduction` and non-contiguous loops, PR collapses the appropriate loop range.

For the example below, it can collapse `(d0, d1)` and `(d5, d6)` loops.
```
indexing_maps = [affine_map<(d0, d1, d2, d3, d4, d5, d6) -> (d0, d1, d2, d4, d3, d5, d6)>,...]
iterator_types = ["parallel", "parallel", "reduction", "parallel", "parallel", "parallel", "parallel"]
```

After collapsing, iree can parallelize more dimensions of the `linalg.generic`, this yields significant performance improvement.

Here are the current limitations:
1. Dynamic tensor shapes: Generating `tensor.expand_shape` with dynamic tensors is ambiguous. It is a known limitation of MLIR. It is possible to solve that. See RFC: https://discourse.llvm.org/t/rfc-add-explicit-shape-inputs-to-tensor-expand-shape/65952

2. Non-contiguous dimensions. Current mechanism does collapsing on tensor shapes, not on the loops. If there is non-contiguous loops (like transpose), collapsing tensor would change behavior. It is possible to solve this by collapsing the loops.
- Improved collapsing algorithm, now it covers more cases.
- Linearized any linalg.generic when possible (before it was 3D+ linalg.generic)
- Added a flag to disable it (--iree-flow-form-dispatch-regions-collapse=false), becase transform dialect does not know collapsing.
@grypp
grypp merged commit 1f98cc5 into iree-org:main Dec 16, 2022
@grypp

grypp commented Dec 16, 2022

Copy link
Copy Markdown
Contributor Author

Thanks to everyone who reviewed my code over and over, I've actually changed it quite a bit. It was a educative journey towards linalg.generic for me 😄

pzread pushed a commit to pzread/iree that referenced this pull request Jan 4, 2023
grypp added a commit to grypp/iree that referenced this pull request Jan 5, 2023
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 mentioned this pull request Jan 5, 2023
grypp added a commit to grypp/iree that referenced this pull request Jan 10, 2023
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 added a commit to grypp/iree that referenced this pull request Jan 10, 2023
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 added a commit to grypp/iree that referenced this pull request Jan 10, 2023
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 added a commit to grypp/iree that referenced this pull request Jan 10, 2023
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 added a commit that referenced this pull request Jan 10, 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.
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.

9 participants