Repository navigation
Description
Request description
Currently unaligned elementwise operations are going through a slow path. In order to generalize we would want to make it go through vectorization, and this is a step toward supporting dynamic shapes.
1st step is to handle 1D op:
%result = linalg.generic {
indexing_maps = [affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>,
affine_map<(d0) -> (d0)>],
iterator_types = ["parallel", "parallel"]}
ins(%a, %b : tensor<10627xf32>, tensor<10627xf32>)
outs(%init : tensor<10627xf32>) {
^bb0(%arg0 : f32, %arg1 : f32, %arg2 : f32):
%0 = arith.addf %arg0, %arg1 : f32
linalg.yield %0 : f32
} -> tensor<10627xf32>
This op would already go through LLVMGPUVectorize pass pipeline but we should force the tile size to target vectorsize of 4:
| unsigned vectorSize = 4; |
This will apply two level of tile and distribute to group and thread. After tiling the linalg operation will have dynamic shapes that will prevent vectorization.
One possible solution is to apply peeling to make the linalg op have a static shape.
Then we should be able to convert the control flow to:
if()
fastpath vectorized
else
slowpath scalar
-An alternative solution is to use the masking solution developed by @dcaballe but I expect that this will be less efficient in this case
Then we should benchmark some basic cases and confirm that the performance of aligned op is similar to the performance of unaligned case.
To handle N-D we can either generalize this solution to N-D but a simpler solution could be linearizing the linalg op which would be a separate task.
What component(s) does this issue relate to?
Compiler
Additional context
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dcaballe commentedon Aug 4, 2022
Just to be sure, alignment here means that the dimension to be vectorized is not multiple of the vector length and does not refer to memory ops not being aligned to vector length boundaries, right?
So far, we've been using padding and peeling to address this problem on CPUs . Padding has the overhead of copies, which could be pretty significant in some cases. Peeling could work well for some cases but your
slowpath scalarpath may represent a significant amount of the computation time if the vectorized dimension is small. This problem gets worse for n-D cases: if we peel an outer dimension, a larger part of the computation will go to the slowpath.Not quite sure how vector masking works on GPUs but masking would allow us to just vectorize any size using a "single path" or even apply peeling and vectorize the peeled iterations using masking. Also note that there is no single lowering for masked operations. Instead of being lowered to masked vector ops they could be lowered to control flow (i.e.,
scf.if) or something else.In any case, it's definitely worth having multiple approaches and compare.
@hanhanW
benvanik commentedon Aug 4, 2022
(where possible the best solution here is still to fuse this into producers - when we are doing that we should only need to pad/swizzle in very rare cases)
ThomasRaoux commentedon Aug 4, 2022
Yes, unaligned is overloaded I should have wrote more details.
If the vector size is small then the border is small right? I agree N-D case is more complex which is why I'm suggesting to concentrate on 1D and we should try to use linearization to handle N-D (separate task)
Different GPUs have different way to mask load/store but in general it does come with a non negligeable cost (especially for vector load/store) which is why I'm suspecting for this case this won't be the best solution.
Yes I think we'll try both and measure.
ThomasRaoux commentedon Aug 4, 2022
Yes definitely, we will still need to handle the unaligned cases though. In CUDA padding is often going to be done in shared memory so we would still need to handle the unaligned part when when storing to global memory.
dcaballe commentedon Aug 4, 2022
Yeah, this is the collapsing approach that we also discussed for CPUs (@MaheshRavishankar, @hanhanW). Perhaps we should coordinate on this one since it should be a target-independent transformation.
ThomasRaoux commentedon Aug 4, 2022
Yes, definitely, I believe we have discussed quickly it in the past. It should definitely be target independent.
MaheshRavishankar commentedon Aug 4, 2022
This should cover a majority of our cases (#8053 (comment)) and doing it at Flow level means that it would work out of the box on all backends... There are others that are not convered by this, those can be handled separately.
okkwon commentedon Aug 4, 2022
IIUC, elementwise ops are fused to its producer whenever available. I am wondering if the problem here is the case where the elementwise op is not fused into producer and how likely we have such a case.
ThomasRaoux commentedon Aug 4, 2022
The first step is to handle ops that are not fused. This should generalize to case where the op is fused in producer once we support unaligned cases there as well.
Set vectorSize back to 4 for non-divisible by 4 dimensions
Set vectorSize back to 4 for non-divisible by 4 dimensions (#11210)
Set vectorSize back to 4 for non-divisible by 4 dimensions