Skip to content

[fusion_heuristic] Dispatch formation is broken #10359

Closed

Description

@pzread

Note this is an issue on the WIP fusion_heuristic branch.

With the change in #10354, some generic ops are not in the flow.dispatch.workgroups.

  %23 = flow.dispatch.workgroups[%c512, %c384, %c1](%22, %cst_30) : (tensor<512x384xf32>, tensor<384x384xf32>) -> tensor<512x384xf32> =
      (%arg3: !flow.dispatch.tensor<readonly:512x384xf32>, %arg4: !flow.dispatch.tensor<readonly:384x384xf32>, %arg5: !flow.dispatch.tensor<writeonly:512x384xf32>) {
    %cst_181 = arith.constant 0.000000e+00 : f32
    %291 = flow.dispatch.tensor.load %arg3, offsets = [0, 0], sizes = [512, 384], strides = [1, 1] : !flow.dispatch.tensor<readonly:512x384xf32> -> tensor<512x384xf32>
    %292 = flow.dispatch.tensor.load %arg4, offsets = [0, 0], sizes = [384, 384], strides = [1, 1] : !flow.dispatch.tensor<readonly:384x384xf32> -> tensor<384x384xf32>
    %293 = linalg.init_tensor [512, 384] : tensor<512x384xf32>
    %294 = linalg.fill ins(%cst_181 : f32) outs(%293 : tensor<512x384xf32>) -> tensor<512x384xf32>
    %295 = linalg.matmul ins(%291, %292 : tensor<512x384xf32>, tensor<384x384xf32>) outs(%294 : tensor<512x384xf32>) -> tensor<512x384xf32>
    flow.dispatch.tensor.store %295, %arg5, offsets = [0, 0], sizes = [512, 384], strides = [1, 1] : tensor<512x384xf32> -> !flow.dispatch.tensor<writeonly:512x384xf32>
    flow.return
  } count(%arg3: index, %arg4: index, %arg5: index) -> (index, index, index) {
    %x, %y, %z = flow.dispatch.default_workgroup_count %arg3, %arg4, %arg5
    flow.return %x, %y, %z : index, index, index
  }
  %24 = flow.tensor.splat %cst_25 : tensor<512xf32>
  %25:2 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d1)>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0)>], iterator_types = ["parallel", "reduction"]} ins(%23, %cst_29, %7 : tensor<512x384xf32>, tensor<384xf32>, tensor<512x384xf32>) outs(%3, %24 : tensor<512x384xf32>, tensor<512xf32>) {
  ^bb0(%arg3: f32, %arg4: f32, %arg5: f32, %arg6: f32, %arg7: f32):
    %291 = arith.addf %arg3, %arg4 : f32
    %292 = arith.addf %291, %arg5 : f32
    %293 = arith.addf %arg7, %292 : f32
    linalg.yield %292, %293 : f32, f32
  } -> (tensor<512x384xf32>, tensor<512xf32>

Activity

  1. MaheshRavishankar commented on Sep 9, 2022

    @MaheshRavishankar
    Collaborator

    Do you have a repro I could use?

  2. self-assigned this
    on Sep 9, 2022
  3. MaheshRavishankar commented on Sep 10, 2022

    @MaheshRavishankar
    Collaborator

    I was able to repro this... This is a reduced repro

    #map0 = affine_map<(d0, d1) -> (d0, d1)>
    #map1 = affine_map<(d0, d1) -> (d1)>
    #map2 = affine_map<(d0, d1) -> (d0)>
    module {
      func.func @repro(%arg0: tensor<512x384xf32>, %arg1: tensor<512x384xf32>, %arg2: tensor<512x384xf32>) -> tensor<512x384xf32> {
        %cst = arith.constant 0.000000e+00 : f32
        %cst_0 = arith.constant 2.000000e+01 : f32
        %cst_1 = arith.constant 3.000000e+01 : f32
        %cst_2 = arith.constant dense_resource<__elided__> : tensor<384xf32>
        %cst_3 = arith.constant dense_resource<__elided__> : tensor<384xf32>
        %cst_4 = arith.constant dense_resource<__elided__> : tensor<384xf32>
        %0 = linalg.init_tensor [512] : tensor<512xf32>
        %1 = linalg.fill {__fused_op__ = [125]} ins(%cst : f32) outs(%0 : tensor<512xf32>) -> tensor<512xf32>
        %2:2 = linalg.generic {indexing_maps = [#map0, #map1, #map0, #map0, #map2], iterator_types = ["parallel", "reduction"]} ins(%arg1, %cst_2, %arg2 : tensor<512x384xf32>, tensor<384xf32>, tensor<512x384xf32>) outs(%arg0, %1 : tensor<512x384xf32>, tensor<512xf32>) attrs =  {__fuse\
    d_op__ = [125]} {
        ^bb0(%arg3: f32, %arg4: f32, %arg5: f32, %arg6: f32, %arg7: f32):
          %6 = arith.addf %arg3, %arg4 : f32
          %7 = arith.addf %6, %arg5 : f32
          %8 = arith.addf %arg7, %7 : f32
          linalg.yield %7, %8 : f32, f32
        } -> (tensor<512x384xf32>, tensor<512xf32>)
        %3 = linalg.fill {__fused_op__ = [125]} ins(%cst : f32) outs(%0 : tensor<512xf32>) -> tensor<512xf32>
        %4:2 = linalg.generic {indexing_maps = [#map0, #map2, #map2, #map2], iterator_types = ["parallel", "reduction"]} ins(%2#0, %2#1 : tensor<512x384xf32>, tensor<512xf32>) outs(%0, %3 : tensor<512xf32>, tensor<512xf32>) attrs =  {__fused_op__ = [125]} {
        ^bb0(%arg3: f32, %arg4: f32, %arg5: f32, %arg6: f32):
          %6 = arith.divf %arg4, %cst_0 : f32
          %7 = arith.subf %arg3, %6 : f32
          %8 = arith.mulf %7, %7 : f32
          %9 = arith.addf %arg6, %8 : f32
          linalg.yield %6, %9 : f32, f32
        } -> (tensor<512xf32>, tensor<512xf32>)
        %5 = linalg.generic {indexing_maps = [#map0, #map2, #map1, #map1, #map2, #map0], iterator_types = ["parallel", "parallel"]} ins(%2#0, %4#1, %cst_3, %cst_4, %4#0 : tensor<512x384xf32>, tensor<512xf32>, tensor<384xf32>, tensor<384xf32>, tensor<512xf32>) outs(%arg0 : tensor<512x3\
    84xf32>) attrs =  {__root_op__ = 125 : i64} {
        ^bb0(%arg3: f32, %arg4: f32, %arg5: f32, %arg6: f32, %arg7: f32, %arg8: f32):
          %6 = arith.divf %arg4, %cst_0 : f32
          %7 = arith.addf %6, %cst_1 : f32
          %8 = math.rsqrt %7 : f32
          %9 = arith.mulf %8, %arg5 : f32
          %10 = arith.mulf %arg7, %9 : f32
          %11 = arith.subf %arg6, %10 : f32
          %12 = arith.mulf %arg3, %9 : f32
          %13 = arith.addf %12, %11 : f32
          linalg.yield %13 : f32
        } -> tensor<512x384xf32>
        return %5 : tensor<512x384xf32>
      }
    }
    
  4. MaheshRavishankar commented on Sep 10, 2022

    @MaheshRavishankar
    Collaborator

    Some triaging information. THis is the output for the small repro

    module {
      func.func @repro(%arg0: tensor<512x384xf32>, %arg1: tensor<512x384xf32>, %arg2: tensor<512x384xf32>) -> tensor<512x384xf32> {
        %c512 = arith.constant 512 : index
        %c384 = arith.constant 384 : index
        %cst = arith.constant 0.000000e+00 : f32
        %cst_0 = arith.constant dense_resource<__elided__> : tensor<384xf32>
        %0 = flow.tensor.splat %cst : tensor<512xf32>
        %1:2 = linalg.generic {indexing_maps = [#map0, #map1, #map0, #map0, #map2], iterator_types = ["parallel", "reduction"]} ins(%arg1, %cst_0, %arg2 : tensor<512x384xf32>, tensor<384xf32>, tensor<512x384xf32>) outs(%arg0, %0 : tensor<512x384xf32>, tensor<512xf32>) {
        ^bb0(%arg3: f32, %arg4: f32, %arg5: f32, %arg6: f32, %arg7: f32):
          %3 = arith.addf %arg3, %arg4 : f32
          %4 = arith.addf %3, %arg5 : f32
          %5 = arith.addf %arg7, %4 : f32
          linalg.yield %4, %5 : f32, f32
        } -> (tensor<512x384xf32>, tensor<512xf32>)
        %2 = flow.dispatch.workgroups[%c512, %c384](%1#0, %1#1, %arg1, %cst_0, %arg2, %arg0) : (tensor<512x384xf32>, tensor<512xf32>, tensor<512x384xf32>, tensor<384xf32>, tensor<512x384xf32>, tensor<512x384xf32>) -> %arg0 =
            (%arg3: !flow.dispatch.tensor<readonly:512x384xf32>, %arg4: !flow.dispatch.tensor<readonly:512xf32>, %arg5: !flow.dispatch.tensor<readonly:512x384xf32>, %arg6: !flow.dispatch.tensor<readonly:384xf32>, %arg7: !flow.dispatch.tensor<readonly:512x384xf32>, %arg8: !flow.dispatc\
    h.tensor<readwrite:512x384xf32>) {
          %cst_1 = arith.constant 0.000000e+00 : f32
          %cst_2 = arith.constant 2.000000e+01 : f32
          %cst_3 = arith.constant 3.000000e+01 : f32
          %3 = flow.dispatch.tensor.load %arg3, offsets = [0, 0], sizes = [512, 384], strides = [1, 1] : !flow.dispatch.tensor<readonly:512x384xf32> -> tensor<512x384xf32>
          %4 = flow.dispatch.tensor.load %arg4, offsets = [0], sizes = [512], strides = [1] : !flow.dispatch.tensor<readonly:512xf32> -> tensor<512xf32>
          %5 = flow.dispatch.tensor.load %arg5, offsets = [0, 0], sizes = [512, 384], strides = [1, 1] : !flow.dispatch.tensor<readonly:512x384xf32> -> tensor<512x384xf32>
          %6 = flow.dispatch.tensor.load %arg6, offsets = [0], sizes = [384], strides = [1] : !flow.dispatch.tensor<readonly:384xf32> -> tensor<384xf32>
          %7 = flow.dispatch.tensor.load %arg7, offsets = [0, 0], sizes = [512, 384], strides = [1, 1] : !flow.dispatch.tensor<readonly:512x384xf32> -> tensor<512x384xf32>
          %8 = flow.dispatch.tensor.load %arg8, offsets = [0, 0], sizes = [512, 384], strides = [1, 1] : !flow.dispatch.tensor<readwrite:512x384xf32> -> tensor<512x384xf32>
          %9 = linalg.init_tensor [512] : tensor<512xf32>
          %10 = linalg.fill ins(%cst_1 : f32) outs(%9 : tensor<512xf32>) -> tensor<512xf32>
          %11 = linalg.fill ins(%cst_1 : f32) outs(%9 : tensor<512xf32>) -> tensor<512xf32>
          %12:2 = linalg.generic {indexing_maps = [#map0, #map2, #map2, #map2], iterator_types = ["parallel", "reduction"]} ins(%3, %4 : tensor<512x384xf32>, tensor<512xf32>) outs(%9, %10 : tensor<512xf32>, tensor<512xf32>) {
          ^bb0(%arg9: f32, %arg10: f32, %arg11: f32, %arg12: f32):
            %15 = arith.divf %arg10, %cst_2 : f32
            %16 = arith.subf %arg9, %15 : f32
            %17 = arith.mulf %16, %16 : f32
            %18 = arith.addf %arg12, %17 : f32
            linalg.yield %15, %18 : f32, f32
          } -> (tensor<512xf32>, tensor<512xf32>)
          %13:2 = linalg.generic {indexing_maps = [#map0, #map1, #map0, #map0, #map2], iterator_types = ["parallel", "reduction"]} ins(%5, %6, %7 : tensor<512x384xf32>, tensor<384xf32>, tensor<512x384xf32>) outs(%8, %11 : tensor<512x384xf32>, tensor<512xf32>) {
          ^bb0(%arg9: f32, %arg10: f32, %arg11: f32, %arg12: f32, %arg13: f32):
            %15 = arith.addf %arg9, %arg10 : f32
            %16 = arith.addf %15, %arg11 : f32
            %17 = arith.addf %arg13, %16 : f32
            linalg.yield %16, %17 : f32, f32
          } -> (tensor<512x384xf32>, tensor<512xf32>)
          %14 = linalg.generic {indexing_maps = [#map0, #map2, #map1, #map1, #map2, #map0], iterator_types = ["parallel", "parallel"]} ins(%13#0, %12#1, %6, %6, %12#0 : tensor<512x384xf32>, tensor<512xf32>, tensor<384xf32>, tensor<384xf32>, tensor<512xf32>) outs(%8 : tensor<512x384xf3\
    2>) {
          ^bb0(%arg9: f32, %arg10: f32, %arg11: f32, %arg12: f32, %arg13: f32, %arg14: f32):
            %15 = arith.divf %arg10, %cst_2 : f32
            %16 = arith.addf %15, %cst_3 : f32
            %17 = math.rsqrt %16 : f32
            %18 = arith.mulf %17, %arg11 : f32
            %19 = arith.mulf %arg13, %18 : f32
            %20 = arith.subf %arg12, %19 : f32
            %21 = arith.mulf %arg9, %18 : f32
            %22 = arith.addf %21, %20 : f32
            linalg.yield %22 : f32
          } -> tensor<512x384xf32>
          flow.dispatch.tensor.store %14, %arg8, offsets = [0, 0], sizes = [512, 384], strides = [1, 1] : tensor<512x384xf32> -> !flow.dispatch.tensor<readwrite:512x384xf32>
          flow.return
        } count(%arg3: index, %arg4: index) -> (index, index, index) {
          %x, %y, %z = flow.dispatch.default_workgroup_count %arg3, %arg4
          flow.return %x, %y, %z : index, index, index
        }
        return %2 : tensor<512x384xf32>
      }
    }
    

    IIUC, the op that is left outside (%1:2) is a clone of the op already moved inside (%13:2). So its a matter of insertion order of the linalg.generic ops. When moved into the dispatch it should have been

    %13:2 =  ...
    %12:2 = ...
    %14
    

    Then the op outside wont exist cause all its uses are coming from within the dispatch

    This method collects the ops

    static SmallVector<Operation *> getOperationsToMoveIntoDispatch(
    .

    But it probably needs to be sorted topologically. This method should help there

    static SmallVector<Operation *> orderOperations(ArrayRef<Operation *> ops) {

  5. MaheshRavishankar commented on Sep 10, 2022

    @MaheshRavishankar
    Collaborator

    That was it. #10362 Fixes the issue (I checked miniLM as well, the pass works as expected now)

  6. pzread commented on Sep 12, 2022

    @pzread
    ContributorAuthor

    Confirmed the issue is fixed

  7. moved this to Done in IREEon Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions