Repository navigation
Collapse linalg.generic - #11295
Collapse linalg.generic#11295
linalg.generic#11295Conversation
|
Nice! Interested in any quantification you have of the performance impact when you have it (no rush)! |
|
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 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 If I collapse Note: As far as I can see from the profiler, the |
|
You could try to add 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 |
31962bd to
4ce26e3
Compare
There was a problem hiding this comment.
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.
|
@dcaballe FYI that this is the thing we've discussed before. It collapses 3D+ elementwise generic ops. |
89c52ee to
c9b43cd
Compare
|
Thanks for everyone for reviewing this PR. I addressed your comments. Here are the changes:
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. |
hanhanW
left a comment
There was a problem hiding this comment.
Overall looks good to me, just some nits.
hanhanW
left a comment
There was a problem hiding this comment.
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.
|
Thanks to everyone who reviewed my code over and over, I've actually changed it quite a bit. It was a educative journey towards |
This reverts commit 1f98cc5.
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.
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.
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.
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.
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.
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.
This reverts commit 1f98cc5.
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.
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 arereductionit can still collapseparallelloops 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 isd1, d3, d0. Here thed1, d3, d0loops 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.After collapsing, iree can parallelize more dimensions of the
linalg.generic, this yields significant performance improvement.Current limitations:
Dynamic tensor shapes: Generating
tensor.expand_shapewith 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/65952Non-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.