Skip to content

Use tile and fuse upto vector level. #6973

Closed

Description

@MaheshRavishankar

While IREE currently generates reasonable code for GEMM operations through use of multi-level tile and distribute followed by vectorization, it is potentially still leaving some performance on the table based on how the linalg.fill operation (that typically accompanies the linalg.matmul operation) is handled. The dispatch region formation tiles and distributes and fuses the linalg.fill with the linalg.matmul for the first level of tiling. Once it gets to the codegen passes the linalg.fill and linalg.matmul. dont get fused till the vector level.

The goal of the code-generation would be to get to this code form

%fill = vector.splat %zerp : vector<4x4xf32>
%0 = scf.for %iv = %c0 to %k step %c4 iter_args (%acc = %fill) {
   %next = ...
   scf.yield %next
} -> vector<4x4xf32>
vector.transfer_write %0, %result...

This is essentially doing the fusion at the register level. This naturally extends to fusion of GEMMs etc. with elementwise operations as well since you could just do further operations after the reduction loop (k loop above) before writing the result out to memory.
The tileAndFuseLinalgOps utility method allows you to do tile and fuse operations as required by the description above. This is being reworked though to be more modular and controllable.
There is already some option in IREE to fuse GEMMS with its producers. This also suffers from the same problem as above that fusion is limited to a single level of tiling and is also limited in its usage (albeit covers a lot of common cases).

The specific steps involved in getting all of this working is

  • Have a separate CPU lowering pipeline that goes from Tensors -> Vectors -> bufferization, right now the two are conflated and controlled by an LLVM flag (we should avoid LLVM style flags)
  • Use the new approach to fusion being developed in core to tile and fuse until vector level and then vectorize to get the required code structure
  • Enable fusion of GEMM with elementwise operations, and vectorize the elementwise operations as well. Since this makes the elementwise operation go through the vector dialect, and not rely on LLVM auto-vectorization (as is done for elementwise operations right now), this can cause regressions due to missing canonicalizations.
  • Once prototyped on LLVM CPU, move the SPIR-V pipeline to use tensors -> vectors. This will enable the same fusion on SPIR-V backends. Since mobile hardware in general do not typically benefit from use of scratchpad memory, the tensor -> vectors approach is expected to work well for SPIR-V.
  • Deprecate the existing fusion requirements.
  • The CUDA backend is slightly different in that the use of scratch pad memory is critical in achieving performance. It is not immediately clear that the modeling promotions in the tensor world give the desired result. This is TBD and a separate question to answer. In any case, the use scratchpad/shared memory means that the one-level of tile + fuse done at the Flow level is good enough to allow the CUDA backend to fuse these operations as well.

Activity

  1. added
    codegen
    on Sep 5, 2021
  2. added this to the milestone on Sep 5, 2021
  3. self-assigned this
    on Sep 14, 2021
  4. MaheshRavishankar commented on Sep 16, 2021

    @MaheshRavishankar
    CollaboratorAuthor

    Apparently linking an issue closes it :)

  5. asaadaldien commented on Sep 16, 2021

    @asaadaldien
    Contributor

    Apparently linking an issue closes it :)

    ¯_(ツ)_/¯

  6. self-assigned this
    on Nov 4, 2021
  7. MaheshRavishankar commented on Dec 10, 2021

    @MaheshRavishankar
    CollaboratorAuthor

    With the CPU backend having moved to tile + fuse + vectorize and the SPIR-V backend having moved to tensors -> vectors -> bufferize. most of the work here is done. We need eventually move the CUDA backend as well to this approach, but thats a harder lift. So closing this bug for now since the main AIs have been resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

codegen

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions