Skip to content

Improve JAXey LLMish model compilation through flow/stream. #13637

Open

Description

@benvanik

JAX inputs have a lot of quirks that need to be fixed at the frontend level (like passing in/returning thousands of tensors to each invocation, not having non-tensor operations, etc). Looking at the large models in #13543 and #13459 there are several patterns we could handle better post-HLO, though, and that would be good to have even if frontend improvements are made.

Some things found in an initial skim through:

Detensoring creates many bad load + arith + splat sequences

Should probably disable detensoring as it was never finished - or a retensoring pass in flow should be able to find slices of load -> splat and move them into dispatch regions

    // 476 blocking loads total
    %950 = stream.async.transfer %211 : !stream.resource<transient>{%c4} -> !stream.resource<staging>{%c4}
    %951 = stream.async.load %950[%c0] : !stream.resource<staging>{%c4} -> f32
    %952 = arith.addf %951, %cst_18 : f32
    %953 = arith.bitcast %952 : f32 to i32
    %8167 = stream.async.splat %953 : i32 -> !stream.resource<external>{%c4}

Splats are used to pass scalar values into dispatches that could instead be done via normal dispatch operands

Need a pattern in flow to capture the splat operand and drop the tensor use

    %3766 = stream.async.splat %3765 : i32 -> !stream.resource<transient>{%c4}
    %3767 = stream.async.splat %3756 : i32 -> !stream.resource<transient>{%c4}
    %3768 = stream.async.dispatch @_main_dispatch_2777::@_main_dispatch_2777_generic_1x8192_f32(%3766[%c0 to %c4 for %c4], %3569[%c0 to %c32768 for %c32768], %3767[%c0 to %c4 for %c4], %arg199[%c0 to %c32768 for %c32768]) : (!stream.resource<transient>{%c4}, !stream.resource<transient>{%c32768}, !stream.resource<transient>{%c4}, !stream.resource<external>{%c32768}) -> !stream.resource<transient>{%c32768}

Splats that later become fills don't coalesce due to resource padding

Could special case pack any allocation under the resource config alignment so these could be dense, then pull all into a single dispatch to perform at once (scatter) instead of one transfer operation per 4 byte splat

    %8402 = stream.async.splat %8144 : i32 -> !stream.resource<external>{%c4}
    %8403 = stream.async.splat %8149 : i32 -> !stream.resource<external>{%c4}
    %8404 = stream.async.splat %8154 : i32 -> !stream.resource<external>{%c4}
->
      stream.cmd.fill %3307, %arg922[%c7317683072 for %c4] : i32 -> !stream.resource<external>{%c7317692032}
      stream.cmd.fill %3311, %arg922[%c7317683136 for %c4] : i32 -> !stream.resource<external>{%c7317692032}
      stream.cmd.fill %3315, %arg922[%c7317683200 for %c4] : i32 -> !stream.resource<external>{%c7317692032}
      stream.cmd.fill %3319, %arg922[%c7317683264 for %c4] : i32 -> !stream.resource<external>{%c7317692032}
      stream.cmd.fill %3323, %arg922[%c7317683328 for %c4] : i32 -> !stream.resource<external>{%c7317692032}
      stream.cmd.fill %3327, %arg922[%c7317683392 for %c4] : i32 -> !stream.resource<external>{%c7317692032}

Dispatch region formation puts lots of scalar arithmetic in their own dispatches

Need much more aggressive dispatch region formation, or aggressive detensoring so that retensoring makes its own dispatch regions, etc - these are ~100k to ~1m times more expensive than they should be

    // 200 splat + scalar dispatches
    %1390 = stream.async.splat %1047 : i32 -> !stream.resource<transient>{%c4} attributes {util.ordinal = 2133 : index}
    %1391 = stream.async.dispatch @_main_dispatch_468::@_main_dispatch_468[%1387](%1390[%c0 to %c4 for %c4], %764[%c0 to %c4 for %c4], %1387) {util.ordinal = 2134 : index} : (!stream.resource<transient>{%c4}, !stream.resource<transient>{%c4}, index) -> %764{%c4}
    %1392 = stream.async.splat %1058 : i32 -> !stream.resource<transient>{%c4} attributes {util.ordinal = 2135 : index}
    %1393 = stream.async.dispatch @_main_dispatch_468::@_main_dispatch_468[%1387](%1392[%c0 to %c4 for %c4], %765[%c0 to %c4 for %c4], %1387) {util.ordinal = 2136 : index} : (!stream.resource<transient>{%c4}, !stream.resource<transient>{%c4}, index) -> %765{%c4}
    %1394 = stream.async.splat %1066 : i32 -> !stream.resource<transient>{%c4} attributes {util.ordinal = 2137 : index}
    %1395 = stream.async.dispatch @_main_dispatch_468::@_main_dispatch_468[%1387](%1394[%c0 to %c4 for %c4], %766[%c0 to %c4 for %c4], %1387) {util.ordinal = 2138 : index} : (!stream.resource<transient>{%c4}, !stream.resource<transient>{%c4}, index) -> %766{%c4}

Dispatch region formation isn't aggressive enough around reductions so 100's of elementwise + reduction dispatches on 2048 elements

Likely because of multiple users of %5564, but all users are reductions of the same input that could be fused together

// elementwise add of 2048 elements
%5564 = stream.async.dispatch @_main_dispatch_4395::@_main_dispatch_4395_generic_2048_f32(%arg121[%c0 to %c8192 for %c8192], %4631[%c0 to %c8192 for %c8192]) : (!stream.resource<external>{%c8192}, !stream.resource<transient>{%c8192}) -> !stream.resource<external>{%c8192}
// sum reduction of 2048 elements -> scalar
%6712 = stream.async.dispatch @_main_dispatch_4578::@_main_dispatch_4578_generic_2048_f32(%5564[%c0 to %c8192 for %c8192], %6711[%c0 to %c4 for %c4]) : (!stream.resource<external>{%c8192}, !stream.resource<transient>{%c4}) -> !stream.resource<transient>{%c4}

Some transient allocations end up host-ordered (stream.resource.alloc vs stream.async.alloca)

%632:36 = stream.resource.alloc uninitialized : !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c8192}, !stream.resource<transient>{%c4096}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c8388608}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}, !stream.resource<transient>{%c16777216}

This TODO is the cause: https://github.com/openxla/iree/blob/6f81ceba9a65f22eab32aa9b980c824f688e111d/compiler/src/iree/compiler/Dialect/Stream/Transforms/ScheduleAllocation.cpp#L1420-L1427

This explains why #13440 didn't help.

There's a mysterious tf.aliasing_output attribute on function args that could be useful

// I think this means that output 0 aliases arg0
%arg0: !hal.buffer_view {jax.arg_info = "state.step", mhlo.sharding = "{replicated}", tf.aliasing_output = 0 : i32}

Activity

  1. added
    compiler/dialects
    performance ⚡
    on May 16, 2023
  2. self-assigned this
    on May 16, 2023
  3. moved this from Inbox to Not Started in (Deprecated) IREEon May 16, 2023
  4. silvasean commented on May 16, 2023

    @silvasean
    Contributor

    Hey Ben, this is great. Right now I'm not seeing this on the profiles though. FYI we're currently 10x off on performance on this workload due to first-order codegen issues mainly. I think the most actionable thing in this vein that I see is a very large amount of time spent in memory allocation (~200-300ms out of ~1.1s, with a strong baseline for this workload being ~100ms).

    I filed #13566 for that, but it sounds like we need more ownership and input from higher up in the stack on that one -- I'm wondering if you can think of anything we can do to move that one forward on our end here in IREE, or are we completely blocked without further from higher in the stack? Even something like a concrete decision tree or forward-looking set of options that seem feasible to implement on our end here, that higher up can consider, could be useful.

  5. benvanik commented on May 17, 2023

    @benvanik
    CollaboratorAuthor

    Will respond on that issue - next step is capturing a trace and posting it there so we can see the allocation behavior at runtime.

  6. ScottTodd commented on May 17, 2023

    @ScottTodd
    Member

    Right now I'm not seeing this on the profiles though. FYI we're currently 10x off on performance on this workload due to first-order codegen issues mainly.

    Some of these issues are structural, so simplifying could unlock more of our existing compiler optimizations, or at least make it easier to read the IR to see what to focus on. Directly going after the profiled hotspots might just find a local minimum (e.g. we might not need to optimize a convolution with striding if we can instead convert it into a matmul or skip running it altogether).

  7. benvanik commented on May 17, 2023

    @benvanik
    CollaboratorAuthor

    Yep! This is why instrumented tracing is so useful - looking at the top 5 hotspots is useful for driving codegen work but not useful for non-codegen work that is much more about the entire workload instead of any particular work item.

  8. benvanik commented on May 17, 2023

    @benvanik
    CollaboratorAuthor

    Good example from immediately before --pass-pipeline=builtin.module(func.func(iree-flow-form-dispatch-regions)):
    https://gist.github.com/benvanik/a6846aa7ad6999ce167705fe3f6f0853

    As an example here's one where we are going device->host->device but shouldn't be; there's several things we can do but I'll file an issue for the real solution to this particular issue:

    func.func @folding() -> tensor<f32> {
      %cst_13 = arith.constant 0.000000e+00 : f32
      %cst_14 = arith.constant 0xFF800000 : f32
      %cst_17 = arith.constant 1.536000e+03 : f32
      %cst_63 = arith.constant 1.000000e+00 : f32
      %33 = tensor.empty() : tensor<f32>
      %34 = linalg.fill ins(%cst_17 : f32) outs(%33 : tensor<f32>) -> tensor<f32>
      %847 = tensor.empty() : tensor<4194304xf32>
      %848 = linalg.fill ins(%cst_14 : f32) outs(%847 : tensor<4194304xf32>) -> tensor<4194304xf32>
    
      %876 = linalg.fill ins(%cst_14 : f32) outs(%33 : tensor<f32>) -> tensor<f32>
      %883 = flow.dispatch.region -> (tensor<f32>) {
        %989 = linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> ()>], iterator_types = ["reduction"]} ins(%848 : tensor<4194304xf32>) outs(%876 : tensor<f32>) {
        ^bb0(%in: f32, %out: f32):
          %990 = math.absf %in : f32
          %991 = arith.maxf %out, %990 : f32
          linalg.yield %991 : f32
        } -> tensor<f32>
        flow.return %989 : tensor<f32>
      }
    
      // this extract + maxf should be folded into either the producer or the consumer
      // in this case this max should be able to fold with the producer above as
      // max(max(reduction), cst) == max(reduction, cst)
      %extracted_852 = tensor.extract %883[] : tensor<f32>
      %884 = arith.maxf %extracted_852, %cst_63 : f32
    
      // this splat should be eliminated if the maxf is folded above
      // if not, the scalar %884 should be captured by the dispatch region instead
      %from_elements_853 = tensor.from_elements %884 : tensor<f32>
    
      %885 = flow.dispatch.region -> (tensor<f32>) {
        %989 = linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> ()>, affine_map<(d0) -> ()>], iterator_types = ["reduction"]} ins(%848, %from_elements_853 : tensor<4194304xf32>, tensor<f32>) outs(%34 : tensor<f32>) {
        ^bb0(%in: f32, %in_910: f32, %out: f32):
          %990 = arith.divf %in, %in_910 : f32
          %991 = arith.mulf %990, %990 : f32
          %992 = arith.addf %out, %991 : f32
          linalg.yield %992 : f32
        } -> tensor<f32>
    
        flow.return %989 : tensor<f32>
      }
    
      return %885 : tensor<f32>
    }
  9. added a commit that references this issue on May 17, 2023
  10. pjannaty commented on May 30, 2023

    @pjannaty
    Contributor

    @MaheshRavishankar if there are any parcelable/concrete sub tasks, please let me and @Young768 know.

  11. MaheshRavishankar commented on May 30, 2023

    @MaheshRavishankar
    Collaborator

    There is a lot of content here and in other bugs that is a bit hard to parse and categorize... #13545 is a place where we have some tasks... I have to look at everything listed on this and related bugs and organize them a bit. So far I am going through this list here #13545 (comment) . Once I land that I can look at what is needed after that to get a better picture...
    For a more deep dive list of tasks waiting for an inference model to help better prioritize.

  12. moved this from Not Started to Backlog in (Deprecated) IREEon Aug 29, 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

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions