Repository navigation
Closed
Labels
codegenShared code generation infrastructure and dialects
Description
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.
Metadata
Metadata
Assignees
Labels
codegenShared code generation infrastructure and dialects
Type
Projects
- StatusShow more project fieldsDone
Activity
okkwon commentedon Jun 1, 2022
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.
hanhanW commentedon Jun 1, 2022
Cool graph! Is the graph generated w/ or w/o transpose fusion? The IR I reported is w/ transpose fusion. I search
%18in dump and found that it's only used bydispatch_12.okkwon commentedon Jun 1, 2022
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.
okkwon commentedon Jun 1, 2022
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.
okkwon commentedon Jun 1, 2022
Here is another instance. (There are many repetitions.)
MaheshRavishankar commentedon Jun 1, 2022
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
needs to be changed to
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.
allieculp commentedon Aug 17, 2022
@MaheshRavishankar @hanhanW Bumping this up as a slightly stale P1 (3 months old). Any updates?
MaheshRavishankar commentedon Aug 17, 2022
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.
MaheshRavishankar commentedon Sep 29, 2022
Should be implemented now at the Flow level under the
--iree-flow-enable-aggressive-fusionflag.