Skip to content

More opportunities about fusion with transpose in MiniLM Bert model #9259

Closed

Description

@hanhanW

I've tried using the work in #9103 and found that there are more opportunities about fusion.

#map1 = affine_map<(d0, d1, d2) -> (d0, d1)>
#map2 = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
...
  flow.executable private @predict_dispatch_11 {
    flow.dispatch.entry public @predict_dispatch_11 attributes {workgroup_rank = 3 : index}
    builtin.module {
      func.func @predict_dispatch_11(%arg0: !flow.dispatch.tensor<readonly:12x128x128xf32>, %arg1: !flow.dispatch.tensor<writeonly:12x128xf32>) {
        %cst = arith.constant -0.000000e+00 : f32
        %0 = flow.dispatch.tensor.load %arg0, offsets = [0, 0, 0], sizes = [12, 128, 128], strides = [1, 1, 1] : !flow.dispatch.tensor<readonly:12x128x128xf32> -> tensor<12x128x128xf32>
        %1 = linalg.init_tensor [12, 128] : tensor<12x128xf32>
        %2 = linalg.fill ins(%cst : f32) outs(%1 : tensor<12x128xf32>) -> tensor<12x128xf32>
        %3 = linalg.generic {indexing_maps = [#map2, #map1], iterator_types = ["parallel", "parallel", "reduction"]}
          ins(%0 : tensor<12x128x128xf32>)
          outs(%2 : tensor<12x128xf32>) {
        ^bb0(%arg2: f32, %arg3: f32):
          %4 = arith.addf %arg2, %arg3 : f32
          linalg.yield %4 : f32
        } -> tensor<12x128xf32>
        flow.dispatch.tensor.store %3, %arg1, offsets = [0, 0], sizes = [12, 128], strides = [1, 1] : tensor<12x128xf32> -> !flow.dispatch.tensor<writeonly:12x128xf32>
        return
      }
    }
  }
  flow.executable private @predict_dispatch_12 {
    flow.dispatch.entry public @predict_dispatch_12 attributes {workgroup_rank = 3 : index}
    builtin.module {
      func.func @predict_dispatch_12(%arg0: !flow.dispatch.tensor<readonly:12x128x128xf32>, %arg1: !flow.dispatch.tensor<readonly:12x128xf32>, %arg2: !flow.dispatch.tensor<writeonly:12x128x128xf32>) {
        %0 = flow.dispatch.tensor.load %arg0, offsets = [0, 0, 0], sizes = [12, 128, 128], strides = [1, 1, 1] : !flow.dispatch.tensor<readonly:12x128x128xf32> -> tensor<12x128x128xf32>
        %1 = flow.dispatch.tensor.load %arg1, offsets = [0, 0], sizes = [12, 128], strides = [1, 1] : !flow.dispatch.tensor<readonly:12x128xf32> -> tensor<12x128xf32>
        %2 = linalg.init_tensor [12, 128, 128] : tensor<12x128x128xf32>
        %3 = linalg.generic {indexing_maps = [#map2, #map1, #map2], iterator_types = ["parallel", "parallel", "parallel"]}
           ins(%0, %1 : tensor<12x128x128xf32>, tensor<12x128xf32>)
           outs(%2 : tensor<12x128x128xf32>) {
        ^bb0(%arg3: f32, %arg4: f32, %arg5: f32):
          %4 = arith.divf %arg3, %arg4 : f32
          linalg.yield %4 : f32
        } -> tensor<12x128x128xf32>
        flow.dispatch.tensor.store %3, %arg2, offsets = [0, 0, 0], sizes = [12, 128, 128], strides = [1, 1, 1] : tensor<12x128x128xf32> -> !flow.dispatch.tensor<writeonly:12x128x128xf32>
        return
      }
    }
  }

  func.func @predict(%arg0: !hal.buffer_view, %arg1: !hal.buffer_view, %arg2: !hal.buffer_view) -> (!hal.buffer_view, !hal.buffer_view) attributes {iree.abi.stub, iree.reflection = {iree.abi = "{\22a\22:[[\22ndarray\22,\22i32\22,2,1,128],[\22ndarray\22,\22i32\22,2,1,128],[\22ndarray\22,\22i32\22,2,1,128]],\22r\22:[[\22sdict\22,[\22last_hidden_state\22,[\22ndarray\22,\22f32\22,3,1,128,384]],[\22pooler_output\22,[\22ndarray\22,\22f32\22,2,1,384]]]],\22v\22:1}"}} {
    ...
    %18 = flow.dispatch @predict_dispatch_11::@predict_dispatch_11[%c128, %c12, %c1](%17) : (tensor<12x128x128xf32>) -> tensor<12x128xf32>
    %19 = flow.dispatch @predict_dispatch_12::@predict_dispatch_12[%c128, %c128, %c12](%17, %18) : (tensor<12x128x128xf32>, tensor<12x128xf32>) -> tensor<12x128x128xf32>
    ...
  }

%18 is only used in %19. They both take %17 as inputs. I think this is related to transpose fusion but not handled well yet.

Activity

  1. added
    bug 🐞
    on Jun 1, 2022
  2. added
    codegen
    and removed
    bug 🐞
    on Jun 1, 2022
  3. okkwon commented on Jun 1, 2022

    @okkwon
    Member

    Thanks Hanhan for the issue.

    The previous work is targeting at a named linalg op + generic w/ transpose.
    The issue here is more general and involves reduction + elementwise fusion.
    Also, %18 is used by dispatch 13 too.

    See https://github.com/google/iree/files/8808765/miniLM.pdf

    The pattern is Softmax, and LayerNorm also has a similar pattern.

  4. hanhanW commented on Jun 1, 2022

    @hanhanW
    ContributorAuthor

    Cool graph! Is the graph generated w/ or w/o transpose fusion? The IR I reported is w/ transpose fusion. I search %18 in dump and found that it's only used by dispatch_12.

  5. okkwon commented on Jun 1, 2022

    @okkwon
    Member

    It is generated w/o the transpose fusion, so ymmv, but the pattern is reduction, so %18 must be %19 in the graph. The reduction result is only used by the element-wise op. Since it is a Softmax, the reduction would be the sum part.

  6. okkwon commented on Jun 1, 2022

    @okkwon
    Member

    The goal of the softmax optimization by fusion is to fuse dispatch 9, 10, 11, 12, and 13 (in the graph) into a single dispatch.

    image

  7. okkwon commented on Jun 1, 2022

    @okkwon
    Member

    Here is another instance. (There are many repetitions.)

    image

  8. MaheshRavishankar commented on Jun 1, 2022

    @MaheshRavishankar
    Collaborator

    Thanks @hanhanW for this bug. Have had this on my radar, but didnt file an issue for this. To enable the fusion you are talking about is a one line change here

    if (!consumerIndexingMap.isIdentity()) return false;
    

    needs to be changed to

    if (!consumerIndexingMap.isPermutation()) return false;
    

    This along with fusion of multi-use producers with one of its consumer (i.e. enabling this by default ) should fuse softmax, layer-norm etc. into a single dispatch. Then its about evaluating if the backends can handle this.

  9. removed their assignment
    on Aug 3, 2022
  10. allieculp commented on Aug 17, 2022

    @allieculp

    @MaheshRavishankar @hanhanW Bumping this up as a slightly stale P1 (3 months old). Any updates?

  11. MaheshRavishankar commented on Aug 17, 2022

    @MaheshRavishankar
    Collaborator

    I am looking into these and associated bugs. Nothing to report right now, but maybe a couple of weeks, Ill either close these as fixed or WNF.

  12. MaheshRavishankar commented on Sep 29, 2022

    @MaheshRavishankar
    Collaborator

    Should be implemented now at the Flow level under the --iree-flow-enable-aggressive-fusion flag.

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions