Skip to content

Use tile and distribute for all compute operations #5045

Description

@MaheshRavishankar

The current dispatch region creation uses the tile + distribute for only named ops. For elemntwise operations represented as linalg.generic and linalg.indexed_generic operations they are not tiled during dispatch region creation. Instead they are left as is and

  • Lowered to loops on CPU side to execute sequentially
  • On GPU side the patterns that lower an untiled operations to scalar code by distributing them to invocations using GlobalInvocationId kick in to distribute one iteration per invocation.

Both of this is not satisfactory. We need to parallelize the work on CPU side, and we also should have such low computation per thread on the GPU side. Instead it might be worth using tile + distribute on these operations as well to do some coarse-grain partitioning of the work. On the GPU side we can then try to use vectorization to get vector loads/stores (which also can be done on the CPU side).

Points to consider

  • On GPU side we might map this directly to invocations (using global invocation id) so that we dont add unnecessary loop overhead (by default the tiles created at the flow level are mapped to workgroups)

Activity

  1. added
    codegen
    on Mar 10, 2021
  2. benvanik commented on Mar 10, 2021

    @benvanik
    Collaborator

    +1 to this! Not sure we've ever explicitly mentioned it but that was always my hope for elementwise/transfer/etc operations: parallelized copies or permutes woo!

  3. antiagainst commented on Mar 10, 2021

    @antiagainst
    Member

    I think if we can handle fusion with consumer elementwise ops (#5040) , we might not seeing a lot of such cases being standalone?

  4. added a commit that references this issue on Apr 23, 2021
    a10cbfe
  5. MaheshRavishankar commented on Apr 26, 2021

    @MaheshRavishankar
    CollaboratorAuthor

    There are still some missing cases.

  6. changed the title [-]Use tile and distribute for `linalg.generic`/`linalg.indexed_generic` operations[/-] [+]Use tile and distribute for all compute operations[/+] on Sep 2, 2021
  7. MaheshRavishankar commented on Sep 5, 2021

    @MaheshRavishankar
    CollaboratorAuthor

    Picking this up again to fill out places that are still missing. With the TiledOpInterface a lot more ops are tiled and distributed through multiple levels of processor hierarchy (1 level on CPU to target threads, 2 levels on GPUs to target workgroups and workitems). There are still a few operations that do not go through this path. Following are a list of these.

    • tensor.insert_slice : The basic tiling and distribution of these is prototyped using the TiledOpInterface (here). This needs to be plumbed through the CPU and GPU backends. Ideally, instead of doing this in a piecewise manner per op, all the places that need tile + distribution just use the TiledOpInterface. (Note: This is WIP already, and is exposing some very hairy issues upstream with rank-reducing subviews).
    • linalg.pad_tensor : Currently these operations are converted to a fill + tensor.insert_slice (here). This is inefficient. It is better to leave it as is and tile and distribute these operations. There is a pattern upstream (here) that can be used to tile and distribute this operation. This pattern uses the TilingInterface which is the upstream version of the TiledOpInterface. So it is also possible to just implement the TiledOpInterface for linalg.pad_tensor as an ExternalModel and redirect to the implementation of TilingInterface upstream. With the use of TiledOpInterface plumbed through this "should just work".
    • linalg.generic where indexing_maps are not all project permutations (see here) : The main operation that falls into this category from our inputs is the reverse operation. The issue is the getLoopsToShapesMap. Just using a concatenation of the indexing_maps as done here to compute the shapes of operands does not work for reverse operation since an expression of type 10-d0 results in a shape of 0.
    • linalg.*conv* : While most operations have a default configuration set that allows them to be tiled and distributed, the default configuration does not work well for convolution operations. A default configuration is needed for conv operations. Ideally we should add a LinalgConvolutionOpInterface (similar to LinalgContractionOpInterface) and set the configuration based on that. Note that the SPIR-V backend does have a default configuration for some versions of the convolution operations, but its very specific to the MobileNet use cases that have been looked at deeply.

    AFAIK, these are the only remaining ops that dont get tiled and distributed. Once these are done, several things can be cleaned up

    • Currently DispatchLinalgOnTensors pass sets the workgroup marker on operations that are tiled and distributed (here). If all ops are tiled and distributed, this marker can be dropped. The workgroup marker can actually be dropped completely from the codebase.
    • The SPIRVDistributeToGlobalID pass can be finally deleted. For now this is well siloed since only dispatches that contain the above operation go through this path. The pipeline that contains this path can be deleted also.
  8. added this to the milestone on Sep 5, 2021
  9. self-assigned this
    on Sep 22, 2021
  10. added 3 commits that reference this issue on Nov 2, 2021
    4fd89f5
    3ceca66
    83f4bd1
  11. added a commit that references this issue on Nov 4, 2021
    fe23053
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