Skip to content

Vectorize unaligned elementwise in LLVMGPU backend #10003

Open

Description

@ThomasRaoux

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:

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

Activity

  1. added
    performance ⚡
    codegen/nvvm
    on Aug 4, 2022
  2. dcaballe commented on Aug 4, 2022

    @dcaballe
    Contributor

    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 scalar path 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

  3. benvanik commented on Aug 4, 2022

    @benvanik
    Collaborator

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

  4. ThomasRaoux commented on Aug 4, 2022

    @ThomasRaoux
    ContributorAuthor

    Just to be sure, alignment here means that the dimension to be vectorized is not multiple of the vector length and do not refer to memory ops not being aligned to vector length boundaries, right?

    Yes, unaligned is overloaded I should have wrote more details.

    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 scalar path 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.

    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)

    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.

    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.

    In any case, it's definitely worth having multiple approaches and compare.

    Yes I think we'll try both and measure.

  5. ThomasRaoux commented on Aug 4, 2022

    @ThomasRaoux
    ContributorAuthor

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

    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.

  6. dcaballe commented on Aug 4, 2022

    @dcaballe
    Contributor

    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)

    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.

  7. ThomasRaoux commented on Aug 4, 2022

    @ThomasRaoux
    ContributorAuthor

    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)

    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.

    Yes, definitely, I believe we have discussed quickly it in the past. It should definitely be target independent.

  8. MaheshRavishankar commented on Aug 4, 2022

    @MaheshRavishankar
    Collaborator

    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)

    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.

    Yes, definitely, I believe we have discussed quickly it in the past. It should definitely be target independent.

    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.

  9. okkwon commented on Aug 4, 2022

    @okkwon
    Member

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

    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.

  10. ThomasRaoux commented on Aug 4, 2022

    @ThomasRaoux
    ContributorAuthor

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

    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.

    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.

  11. removed their assignment
    on Apr 11, 2023
  12. moved this from Not Started to Backlog in (Deprecated) IREEon Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

codegen/nvvmperformance ⚡

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions