Skip to content

Support distribution to more than 3 dimension at the workgroup level - #11385

Merged
ThomasRaoux merged 3 commits into
iree-org:mainfrom
ThomasRaoux:distribute_nd_op
Dec 1, 2022
Merged

ThomasRaoux merged 3 commits into
iree-org:mainfrom
ThomasRaoux:distribute_nd_op

Conversation

@ThomasRaoux

Copy link
Copy Markdown
Contributor

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.

@ThomasRaoux
ThomasRaoux requested a review from grypp December 1, 2022 04:38
@ThomasRaoux
ThomasRaoux force-pushed the distribute_nd_op branch 3 times, most recently from 81b9936 to 169c387 Compare December 1, 2022 06:08
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.

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

Just a few minor comments. THanks for finishing this up. I had left this one piece undone, but good that you didnt hit any footguns. @dcaballe and @vmurali something to add to the CPU side as well.

if (maxNumPartitionedLoops.has_value() &&
parallelLoops.size() > maxNumPartitionedLoops.value()) {
parallelLoops =
llvm::to_vector(llvm::ArrayRef<unsigned>(parallelLoops)

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.

Nit: You can use llvm::makeArrayRef

partitionableLoops.size() - maxNumPartitionedLoops));
if (maxNumPartitionedLoops.has_value() &&
partitionableLoops.size() > maxNumPartitionedLoops.value()) {
partitionableLoops.erase(partitionableLoops.begin(),

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.

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() &&

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.

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() &&

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.

(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) {

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.

Could we use kNumMaxParallelDims instead of hard coding it?

@grypp

grypp commented Dec 1, 2022

Copy link
Copy Markdown
Contributor

Thanks Thomas implementing this after our discussion.

One small note, I think we should linearize the X dimension instead of the Z. Because in CUDA the limit of X is 2^31-1 while the limit of Z is 65535. So X can cover larger iterations. As I understand it, the iree schedules X dimension to the innermost loops, maybe loop scheduling needs to be changed as well.

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 rem or div.

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

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.

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)

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.

Right, this should be done in each target config as we don't really hav target info there.

@ThomasRaoux

Copy link
Copy Markdown
Contributor Author

Thanks Thomas implementing this after our discussion.

One small note, I think we should linearize the X dimension instead of the Z. Because in CUDA the limit of X is 2^31-1 while the limit of Z is 65535. So X can cover larger iterations. As I understand it, the iree schedules X dimension to the innermost loops, maybe loop scheduling needs to be changed as well.

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.

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 rem or div.

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.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

Awesome!!

@ThomasRaoux
ThomasRaoux merged commit 0169788 into iree-org:main Dec 1, 2022
@grypp grypp mentioned this pull request Dec 2, 2022
grypp added a commit that referenced this pull request Dec 16, 2022
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.
pzread pushed a commit to pzread/iree that referenced this pull request Jan 11, 2023
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.

3 participants