Repository navigation
Support distribution to more than 3 dimension at the workgroup level - #11385
Conversation
81b9936 to
169c387
Compare
So far we were not distributing higher level dimensions for operations with more than 3 parallel dimensions. This delinearize the z dimension for case with more than 3 dimension and allow better distribution if the kernel config asks for it. Also change the default config of llvmgpu and spirv backend to take advantage of that.
169c387 to
4a05dce
Compare
| if (maxNumPartitionedLoops.has_value() && | ||
| parallelLoops.size() > maxNumPartitionedLoops.value()) { | ||
| parallelLoops = | ||
| llvm::to_vector(llvm::ArrayRef<unsigned>(parallelLoops) |
There was a problem hiding this comment.
Nit: You can use llvm::makeArrayRef
| partitionableLoops.size() - maxNumPartitionedLoops)); | ||
| if (maxNumPartitionedLoops.has_value() && | ||
| partitionableLoops.size() > maxNumPartitionedLoops.value()) { | ||
| partitionableLoops.erase(partitionableLoops.begin(), |
There was a problem hiding this comment.
Style nit. Just the indentations gotten a bit unreadable. Maybe flip the condition, so
if (!maxNumPartitionableLoops.has_value() || partitionableLoops.size() <= maxNumPartitionedLoops.value()) {
return partitionableLoops;
}
partitionableLoops.erase(...)
| partitionableLoops.begin(), | ||
| std::next(partitionableLoops.begin(), | ||
| partitionableLoops.size() - maxNumPartitionedLoops)); | ||
| if (maxNumPartitionedLoops.has_value() && |
There was a problem hiding this comment.
Same here . Style nit. Just the indentations gotten a bit unreadable. Maybe flip the condition, so
if (!maxNumPartitionableLoops.has_value() || partitionableLoops.size() <= maxNumPartitionedLoops.value()) {
return partitionableLoops;
}
partitionableLoops.erase(...)
| partitionableLoops.begin(), | ||
| std::next(partitionableLoops.begin(), | ||
| partitionableLoops.size() - maxNumPartitionedLoops)); | ||
| if (maxNumPartitionedLoops.has_value() && |
There was a problem hiding this comment.
(and here as well)
Style nit. Just the indentations gotten a bit unreadable. Maybe flip the condition, so
if (!maxNumPartitionableLoops.has_value() || partitionableLoops.size() <= maxNumPartitionedLoops.value()) {
return partitionableLoops;
}
partitionableLoops.erase(...)
| SmallVector<linalg::ProcInfo, 3> procInfo(numParallelDims); | ||
| Value splitDim; | ||
| for (size_t dim = 0; dim < numParallelDims; ++dim) { | ||
| if (numParallelDims > 3 && dim >= 2) { |
There was a problem hiding this comment.
Could we use kNumMaxParallelDims instead of hard coding it?
|
Thanks Thomas implementing this after our discussion. One small note, I think we should linearize the Another small note, when the trip count is known and very small (very small needs to be defined) maybe it's better to unroll the small loop. This way, there will be no need for generating instructions such as |
| @@ -41,9 +42,11 @@ llvm::SmallVector<unsigned> getPartitionableLoopsImpl( | |||
| parallelLoops = pruneUnitTripParallelLoops(parallelLoops, staticLoopRanges); | |||
| // TODO(ravishankarm): For now the outer parallel loops are dropped. This is | |||
| // a pragmatic choice for now but might need to be revisited. | |||
There was a problem hiding this comment.
Nit: we can check the static loop trip counts here in case they exceed the limit of targeting architecture. If it exceeds, we probably cannot parallelize that loop(s)
There was a problem hiding this comment.
Right, this should be done in each target config as we don't really hav target info there.
the problem is that X is the fastest moving dimension so we want to map it to the most inner tile sizes. Most likely the best solution would be to only use X and split it for all the dimension. This is probably too much of a change for this PR. Let’s discuss about changing that in the future.
Right, this is a decision that should be taken by the configuration. The configuration decides on the tile size which effectively allows to control how much of the loop is unrolled vs distributed. |
|
Awesome!! |
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. #11385 tackles this
problem by linearizing the workgroup id. Alternative idea is to
linearize loops.
…p level (iree-org#11385)" This reverts commit 0169788.
So far we were not distributing higher level dimensions for operations with more than 3 parallel dimensions. This delinearize the z dimension for case with more than 3 dimension and allow better distribution if the kernel config asks for it.
Also change the default config of llvmgpu and spirv backend to take advantage of that.