Skip to content

LinalgBufferizePass doesn't handle tensor.cast well  #5364

Closed

Description

@asaadaldien

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

Activity

  1. added
    codegen
    on Apr 8, 2021
  2. MaheshRavishankar commented on Apr 8, 2021

    @MaheshRavishankar
    Collaborator

    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.

  3. asaadaldien commented on Apr 8, 2021

    @asaadaldien
    ContributorAuthor

    I see this is equivalent but I think better than folding tensor.cast into store which requires having every consumer type to do the folding. I will try that. thanks!

  4. asaadaldien commented on Apr 9, 2021

    @asaadaldien
    ContributorAuthor

    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

  5. MaheshRavishankar commented on Apr 9, 2021

    @MaheshRavishankar
    Collaborator
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

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions