Repository navigation
Closed
Labels
codegenShared code generation infrastructure and dialects
Description
After running the pass the flow.dispatch.store is deleted :
This happens in #5362 if the FoldTensorCastIntoDispatchTensorStore wasn't applied
see:
// *** IR Dump Before mlir::iree_compiler::(anonymous namespace)::LinalgBufferizePass ***
func @large_dot_general_dispatch_0() {
%c0 = constant 0 : index
%cst = constant 0.000000e+00 : f32
%c4 = constant 4 : index
%c64 = constant 64 : index
%c1 = constant 1 : index
%c32 = constant 32 : index
%0 = hal.interface.binding.subspan @io::@s0b0_ro_external[%c0] : !flow.dispatch.tensor<readonly:4x32x1024xf32>
%1 = hal.interface.binding.subspan @io::@s0b1_ro_external[%c0] : !flow.dispatch.tensor<readonly:4x1024x64xf32>
%2 = hal.interface.binding.subspan @io::@s0b2_xw_external[%c0] : !flow.dispatch.tensor<writeonly:4x32x64xf32>
%workgroup_id_x = hal.interface.workgroup.id[0] : index
%workgroup_count_x = hal.interface.workgroup.count[0] : index
%workgroup_id_y = hal.interface.workgroup.id[1] : index
%workgroup_count_y = hal.interface.workgroup.count[1] : index
%workgroup_id_z = hal.interface.workgroup.id[2] : index
%workgroup_count_z = hal.interface.workgroup.count[2] : index
scf.for %arg0 = %workgroup_id_z to %c4 step %workgroup_count_z {
%3 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_id_y]
%4 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_count_y]
scf.for %arg1 = %3 to %c32 step %4 {
%5 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_id_x]
%6 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_count_x]
scf.for %arg2 = %5 to %c64 step %6 {
%7 = flow.dispatch.tensor.load %0, offsets = [%arg0, %arg1, 0], sizes = [%c1, %c32, 1024], strides = [1, 1, 1] : !flow.dispatch.tensor<readonly:4x32x1024xf32> -> tensor<?x?x1024xf32>
%8 = flow.dispatch.tensor.load %1, offsets = [%arg0, 0, %arg2], sizes = [%c1, 1024, %c32], strides = [1, 1, 1] : !flow.dispatch.tensor<readonly:4x1024x64xf32> -> tensor<?x1024x?xf32>
%9 = linalg.init_tensor [1, 32, 32] : tensor<1x32x32xf32>
%10 = linalg.fill(%9, %cst) {__internal_linalg_transform__ = "workgroup"} : tensor<1x32x32xf32>, f32 -> tensor<1x32x32xf32>
%11 = linalg.batch_matmul {__internal_linalg_transform__ = "workgroup", is_root_op} ins(%7, %8 : tensor<?x?x1024xf32>, tensor<?x1024x?xf32>) outs(%10 : tensor<1x32x32xf32>) -> tensor<1x32x32xf32>
%12 = tensor.cast %11 : tensor<1x32x32xf32> to tensor<?x?x?xf32>
flow.dispatch.tensor.store %12, %2, offsets = [%arg0, %arg1, %arg2], sizes = [%c1, %c32, %c32], strides = [1, 1, 1] : tensor<?x?x?xf32> -> !flow.dispatch.tensor<writeonly:4x32x64xf32>
}
}
}
return
}
// *** IR Dump After mlir::iree_compiler::(anonymous namespace)::LinalgBufferizePass ***
func @large_dot_general_dispatch_0() {
%c0 = constant 0 : index
%cst = constant 0.000000e+00 : f32
%c4 = constant 4 : index
%c64 = constant 64 : index
%c1 = constant 1 : index
%c32 = constant 32 : index
%0 = hal.interface.binding.subspan @io::@s0b0_ro_external[%c0] : memref<4x32x1024xf32>
%1 = hal.interface.binding.subspan @io::@s0b0_ro_external[%c0] : !flow.dispatch.tensor<readonly:4x32x1024xf32>
%2 = hal.interface.binding.subspan @io::@s0b1_ro_external[%c0] : memref<4x1024x64xf32>
%3 = hal.interface.binding.subspan @io::@s0b1_ro_external[%c0] : !flow.dispatch.tensor<readonly:4x1024x64xf32>
%4 = hal.interface.binding.subspan @io::@s0b2_xw_external[%c0] : memref<4x32x64xf32>
%5 = hal.interface.binding.subspan @io::@s0b2_xw_external[%c0] : !flow.dispatch.tensor<writeonly:4x32x64xf32>
%workgroup_id_x = hal.interface.workgroup.id[0] : index
%workgroup_count_x = hal.interface.workgroup.count[0] : index
%workgroup_id_y = hal.interface.workgroup.id[1] : index
%workgroup_count_y = hal.interface.workgroup.count[1] : index
%workgroup_id_z = hal.interface.workgroup.id[2] : index
%workgroup_count_z = hal.interface.workgroup.count[2] : index
scf.for %arg0 = %workgroup_id_z to %c4 step %workgroup_count_z {
%6 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_id_y]
%7 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_count_y]
scf.for %arg1 = %6 to %c32 step %7 {
%8 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_id_x]
%9 = affine.apply affine_map<()[s0] -> (s0 * 32)>()[%workgroup_count_x]
scf.for %arg2 = %8 to %c64 step %9 {
%10 = memref.subview %4[%arg0, %arg1, %arg2] [%c1, %c32, %c32] [1, 1, 1] : memref<4x32x64xf32> to memref<?x?x?xf32, affine_map<(d0, d1, d2)[s0] -> (d0 * 2048 + s0 + d1 * 64 + d2)>>
%11 = memref.subview %0[%arg0, %arg1, 0] [%c1, %c32, 1024] [1, 1, 1] : memref<4x32x1024xf32> to memref<?x?x1024xf32, affine_map<(d0, d1, d2)[s0] -> (d0 * 32768 + s0 + d1 * 1024 + d2)>>
%12 = flow.dispatch.tensor.load %1, offsets = [%arg0, %arg1, 0], sizes = [%c1, %c32, 1024], strides = [1, 1, 1] : !flow.dispatch.tensor<readonly:4x32x1024xf32> -> tensor<?x?x1024xf32>
%13 = memref.subview %2[%arg0, 0, %arg2] [%c1, 1024, %c32] [1, 1, 1] : memref<4x1024x64xf32> to memref<?x1024x?xf32, affine_map<(d0, d1, d2)[s0] -> (d0 * 65536 + s0 + d1 * 64 + d2)>>
%14 = flow.dispatch.tensor.load %3, offsets = [%arg0, 0, %arg2], sizes = [%c1, 1024, %c32], strides = [1, 1, 1] : !flow.dispatch.tensor<readonly:4x1024x64xf32> -> tensor<?x1024x?xf32>
%15 = linalg.init_tensor [1, 32, 32] : tensor<1x32x32xf32>
%16 = memref.alloca() : memref<1x32x32xf32>
%17 = memref.alloca() : memref<1x32x32xf32>
linalg.fill(%17, %cst) {__internal_linalg_transform__ = "workgroup"} : memref<1x32x32xf32>, f32
%18 = linalg.fill(%15, %cst) {__internal_linalg_transform__ = "workgroup"} : tensor<1x32x32xf32>, f32 -> tensor<1x32x32xf32>
linalg.batch_matmul {__internal_linalg_transform__ = "workgroup", is_root_op} ins(%11, %13 : memref<?x?x1024xf32, affine_map<(d0, d1, d2)[s0] -> (d0 * 32768 + s0 + d1 * 1024 + d2)>>, memref<?x1024x?xf32, affine_map<(d0, d1, d2)[s0] -> (d0 * 65536 + s0 + d1 * 64 + d2)>>) outs(%17 : memref<1x32x32xf32>)
%19 = linalg.batch_matmul {__internal_linalg_transform__ = "workgroup", is_root_op} ins(%12, %14 : tensor<?x?x1024xf32>, tensor<?x1024x?xf32>) outs(%18 : tensor<1x32x32xf32>) -> tensor<1x32x32xf32>
%20 = tensor.cast %19 : tensor<1x32x32xf32> to tensor<?x?x?xf32>
}
}
}
return
}
iree-opt -iree-codegen-linalg-bufferize
Metadata
Metadata
Assignees
Labels
codegenShared code generation infrastructure and dialects
Activity
MaheshRavishankar commentedon Apr 8, 2021
It doesnt handle tensor_cast at all. It needs to handle it during pre-processing to use the same buffer for the source as the result.
asaadaldien commentedon Apr 8, 2021
I see this is equivalent but I think better than folding
tensor.castinto store which requires having every consumer type to do the folding. I will try that. thanks!asaadaldien commentedon Apr 9, 2021
So that worked (forward tensor.cast input buffer) sending a PR shortly, but it introduces another an alloc-copy that isn't getting aliased to the output buffer, see:
https://gist.github.com/asaadaldien/1932380337402c1ca22b37ee6eca670b
MaheshRavishankar commentedon Apr 9, 2021
You should be using
createAliasingBufferOrAllocationForResulthttps://github.com/google/iree/blob/8fb65cbc1595daa2f5385b60f45c3a4428017eec/iree/compiler/Conversion/Common/LinalgBufferizePass.cpp#L292 . See other uses of it in that pass