Skip to content

Add secondary dispatch region fusion/retensoring pass in flow dialect. #6948

Closed

Description

@benvanik

In addition to the linalg fusion we do today there's another level of fusion we could be performing that combines entire dispatch regions. This can happen just based on the flow.dispatch.workgroups op and using the flow.dispatch.tensor interface.

Conceptually each dispatch region is just a function called at runtime - the call of which may be happen on a thread pool, GPU command processor, etc. The benefit of having these calls modeled in Flow/HAL is that if the execution engine can execute the work in parallel (same call on may execution units) or concurrently (overlapping with other calls) we reap those benefits. There are cases where this cannot happen, though, and we have those encoded in the flow IR use-def chains.

As an example, this is a great separation of work:

// these two run concurrently and are each parallelized:
%0 = flow.dispatch.workgroups[1024, 1, 1](%arg0)
%1 = flow.dispatch.workgroups[1024, 512, 1](%arg1)
// this reduces the inputs and runs after both complete, but uses far fewer workgroups to do so:
%2 = flow.dispatch.workgroups[8, 1, 1](%0, %1)

While this is not:

// each runs sequentially:
%0 = flow.dispatch.workgroups[1, 1, 1](%arg0)
%1 = flow.dispatch.workgroups[1, 1, 1](%1)
%2 = flow.dispatch.workgroups[1, 1, 1](%2)

In the second case we are getting no benefits from the separation and only incurring overhead: we are paying the cost of encoding and performing multiple calls, round-tripping the results/arguments through memory, and requiring additional transient memory.

The aspirational case here is https://www.shadertoy.com/view/wsByWV (which I will bring up til the end of time :) - that could be done in 400 dispatches or like it is here 4 with loops, conditionals, etc. We (as something executing functions) aren't restricted to what linalg can model in a single loop nest but only what data traversal hazards we have. It's all just code :)

We should be able to identify some of these cases where the separation is pessimizing performance and fix them up by folding consumers up into producers. Conceptually it's like identifying mergable dispatches, creating a new dispatch, and inlining both dispatch regions into it. The flow.dispatch.tensor arguments can be elided through and the rest of the code outside of the dispatches gets its use-def chains patched up. Being able to safely do this is one of the reasons we require tensors at this level.

There are gotchas, like if the work is parallelized it may be doing things that prevent workgroups from running within the same dispatch (such as a scatter followed by a gather). We should be able to identify this by either adding traversal information to the flow ops (similar and derived from how linalg has parallel/reduce/etc tags) or inspecting the flow.dispatch.tensor load and store ops. To start, even just combining dispatches that are all 1x1x1 would help in the situations where we can't or don't want to detensor and are performing very thin sequences of sequential work.

The major work is this cost modeling (of which to start could just be "is possible?"), and there may be some cleanup work or generalization required lower into the codegen stack if there are any lingering assumptions from back in the old pipeline around whether there is a single root linalg loop/etc. The pass to perform this work where it is possible, though, is likely just a hundred lines or so of replacing ops, using mlir::inlineRegion, and removing unneeded args/results (which in theory is handled by the closure optimizations already on the dispatch region op).

This will improve raw performance (less memory round-tripping), latency (less command processor round-tripping), memory utilization (fewer materialized transient buffers), and code size (less code needed to setup buffers/dispatch unique entry points). It may harm deduplication but that's a cost we can balance (and we need better solutions than "bitwise identical" for deduplication anyway).

Activity

  1. added
    compiler/dialects
    performance ⚡
    on Sep 2, 2021
  2. MaheshRavishankar commented on Sep 5, 2021

    @MaheshRavishankar
    Collaborator

    Ideally this could be done before dispatch region formation. Its easier to analyse operations at Linalg on tensors instead of at dispatch region formation level, and treat dispatch region formation as a one-to-one mapping of a single or a sequence of linalg operations. It would be useful to determine the root causes of these different dispatches created (what ops are in these dispatches). Some examples are

    • Currently elementwise operations are fused in a producer-consumer fashion. It is also possible to fuse "horizontally", i.e.
    #map0 = affine_map<(d0, d1) -> (d0, d1)>
    #2d_elementwise = {indexing_maps = [#map0, #map0, #map0], iteratort_types = ["parallel", "parallel"]}
    %add = linalg.generic #2d_elementwise
        ins(%arg0, %arg1 : tensor<?x?xf32>) outs(%shape : tensor<?x?xf32>) {
        ^bb0(%arg4 : f32, %arg5 : f32, %arg6 : f32):
          %0 = addf %arg4, %arg5 : f32
          linalg.yield %0 : f32
        } -> tensor<?x?xf32>
    %mul = linalg.generic #2d_elementwise
        ins(%arg2, %arg3 : tensor<?x?xf32>) outs(%shape : tensor<?x?xf32>) {
        ^bb0(%arg4 : f32, %arg5 : f32, %arg6 : f32):
          %1 = mulf %arg4, %arg5 : f32
          linalg.yield %1 : f32
        } -> tensor<?x?xf32>
    

    could be fused to

    %fused:2 = linalg.generic #2d_elementwise
        ins(%arg0, %arg1 : tensor<?x?xf32>) outs(%shape : tensor<?x?xf32>, %shape : tensor<?x?xf32>) {
        ^bb0(%arg4 : f32, %arg5 : f32, %arg6 : f32):
          %0 = addf %arg4, %arg5 : f32
          %1 = mulf %arg4, %arg5 : f32
          linalg.yield %0 : f32
        } -> tensor<?x?xf32>, tensor<?x?xf32>
    

    This effectively fuses the two dispatch region created by these operations.

    Would be good to study the models we have running through IREE to find a list of missing fusions.

  3. benvanik commented on Sep 5, 2021

    @benvanik
    CollaboratorAuthor

    Agreed that would be a good approach! It's incredibly more efficient to compute two things at once than it is two things in sequence - can think of fusing horizontally as a way of enabling concurrent execution without another dispatch/barriers/etc.

  4. hanhanW commented on Sep 14, 2021

    @hanhanW
    Contributor

    Minor issues, I think it should be fused in

    %fused:2 = linalg.generic #2d_elementwise
        ins(%arg0, %arg1, %arg2, %arg3 : tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32>)
       outs(%shape : tensor<?x?xf32>, %shape : tensor<?x?xf32>) {
        ^bb0(%arg4 : f32, %arg5 : f32, %arg6 : f32, %arg7: f32, %arg8: f32, %arg9: f32):
          %0 = addf %arg4, %arg5 : f32
          %1 = mulf %arg6, %arg7 : f32
          linalg.yield %0, %1 : f32
        } -> tensor<?x?xf32>, tensor<?x?xf32>
  5. benvanik commented on Oct 25, 2022

    @benvanik
    CollaboratorAuthor

    While looking at #10903 I found that the model suffers deeply from really narrow tensor operations that don't get fused:
    image
    Detensoring didn't seem to help (or work), but even if it did there's data dependencies that we can't have host/device roundtrips around. Note that some of the tensors are not scalars but still a small value (~2-4 elements) that don't need any kind of large scale tiling.

  6. MaheshRavishankar commented on Oct 26, 2022

    @MaheshRavishankar
    Collaborator

    Is it just a lot of parallel work that don't have producer-consumer relationship?

  7. benvanik commented on Oct 26, 2022

    @benvanik
    CollaboratorAuthor

    There's likely some of that but the above is mostly sequential. I suspect it's the reuse that's breaking things. The original code likely looks like a normal program instead of ML, so it's going to be a tangle of use-def chains.

  8. MaheshRavishankar commented on Oct 26, 2022

    @MaheshRavishankar
    Collaborator

    Ok. Hopefully we can drop that requirement soon. It's under a flag currently cause of backend issues.

  9. benvanik commented on Oct 26, 2022

    @benvanik
    CollaboratorAuthor

    That'd be awesome!

  10. benvanik commented on Oct 26, 2022

    @benvanik
    CollaboratorAuthor

    (it's exciting to have a model that actually does this - it's been a hypothetical scenario for so long - when bored/waiting for a CI run I recommend perusing out what phoenix posted in #10903 because there's a bunch of fun stuff in it :)

  11. MaheshRavishankar commented on Oct 26, 2022

    @MaheshRavishankar
    Collaborator

    You can try iree-flow-enable-aggressive-fusion to see if it helps. Right now that might not compile. But at least will give an idea of if it is covered by that or not. I'll take a look when I get time

  12. 24 remaining items

  13. added a commit that references this issue on May 17, 2023
  14. moved this from Needs Scheduling to Done in (Deprecated) IREEon May 17, 2023
  15. benvanik commented on May 17, 2023

    @benvanik
    CollaboratorAuthor

    (whoops, forgot putting issues in titles closes them 🤦)

  16. moved this from Done to Not Started in (Deprecated) IREEon May 18, 2023
  17. MaheshRavishankar commented on May 18, 2023

    @MaheshRavishankar
    Collaborator

    From #13637 I've noticed this is going to be extremely important for overall performance there. There's a large number of dispatches in these LLM-like programs that are currently running afoul of detensoring and our current formation that likely needs to be specialized for small dispatches (lots of tensor and tensor<16xf32> that get split into lots of dispatch regions/host<->device interop).

    gist.github.com/benvanik/a6846aa7ad6999ce167705fe3f6f0853 has some decent input IR to --pass-pipeline=builtin.module(func.func(iree-flow-form-dispatch-regions)) showing these cases.

    Until this issue is fixed I'm going to be flag guarding detensoring and disabling by default.

    @benvanik can you share the input IR for this example (the input to iree-compile)

  18. benvanik commented on May 18, 2023

    @benvanik
    CollaboratorAuthor

    #13543, #13459, and #13648 have the IR I've been playing with - they're all roughly the same

  19. MaheshRavishankar commented on May 18, 2023

    @MaheshRavishankar
    Collaborator

    This file has a lot of debug metadata here. Is there a flag to strip it while printing?

  20. benvanik commented on May 18, 2023

    @benvanik
    CollaboratorAuthor

    good q - I sometimes do a regex replace of the #loc attributes with location(unknown), but I think passing the file through iree-opt with --mlir-print-debuginfo=false would strip them too

  21. moved this from Not Started to In Progress in (Deprecated) IREEon Jun 21, 2023
  22. moved this from In Progress to Done in (Deprecated) IREEon Jun 29, 2023
  23. MaheshRavishankar commented on Jun 29, 2023

    @MaheshRavishankar
    Collaborator

    #13711 adds the basic mechanism... any other issues can be done as a follow up.

  24. added a commit that references this issue on Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

compiler/dialectsperformance ⚡

Type

No type

Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions