Skip to content

Fusion on Linalg doesn't work in IREE. #3761

Closed

Description

@hanhanW

Describe the bug
This works with linalg-fusion-for-tensor-ops but not iree-codegen-fusion-of-tensor-ops. The main difference is that iree-codegen-fusion-of-tensor-ops fold a tensor_reshape op into hal.interface.store.tensor.

To Reproduce

Input file:

func @foo() {
  %c0 = constant 0 : index
  %0 = hal.interface.load.tensor @legacy_io::@arg0, offset = %c0 {operand_result_index = 0 : i32} : tensor<8192x4xf32>
  %1 = hal.interface.load.tensor @legacy_io::@arg1, offset = %c0 {operand_result_index = 1 : i32} : tensor<8x33xi32>
  %2 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d1, d0)>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"]} ins(%1 : tensor<8x33xi32>) {
  ^bb0(%arg0: i32):  // no predecessors
    linalg.yield %arg0 : i32
  } -> tensor<33x8xi32>
  %3 = linalg.tensor_reshape %2 [affine_map<(d0, d1) -> (d0, d1)>] : tensor<33x8xi32> into tensor<264xi32>
  %4 = linalg.indexed_generic {indexing_maps = [affine_map<(d0, d1) -> (d0)>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"]} ins(%3 : tensor<264xi32>) {
  ^bb0(%arg0: index, %arg1: index, %arg2: i32):  // no predecessors
    %8 = index_cast %arg2 : i32 to index
    %9 = extract_element %0[%8, %arg1] : tensor<8192x4xf32>
    linalg.yield %9 : f32
  } -> tensor<264x4xf32>
  %5 = linalg.tensor_reshape %4 [affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1, d2) -> (d2)>] : tensor<264x4xf32> into tensor<33x8x4xf32>
  hal.interface.store.tensor %5, @legacy_io::@ret0, offset = %c0 {operand_result_index = 2 : i32} : tensor<33x8x4xf32>
  return
}

$ iree-opt -iree-codegen-fusion-of-tensor-ops a.mlir

will get result:

#map0 = affine_map<(d0, d1) -> (d1, d0)>
#map1 = affine_map<(d0, d1) -> (d0, d1)>
#map2 = affine_map<(d0, d1) -> (d0)>
module {
  func @foo() {
    %c0 = constant 0 : index
    %0 = hal.interface.load.tensor @legacy_io::@arg0, offset = %c0 {operand_result_index = 0 : i32} : tensor<8192x4xf32>
    %1 = hal.interface.load.tensor @legacy_io::@arg1, offset = %c0 {operand_result_index = 1 : i32} : tensor<8x33xi32>
    %2 = linalg.generic {indexing_maps = [#map0, #map1], iterator_types = ["parallel", "parallel"]} ins(%1 : tensor<8x33xi32>) {
    ^bb0(%arg0: i32):  // no predecessors
      linalg.yield %arg0 : i32
    } -> tensor<33x8xi32>
    %3 = linalg.tensor_reshape %2 [#map1] : tensor<33x8xi32> into tensor<264xi32>
    %4 = linalg.indexed_generic {indexing_maps = [#map2, #map1], iterator_types = ["parallel", "parallel"]} ins(%3 : tensor<264xi32>) {
    ^bb0(%arg0: index, %arg1: index, %arg2: i32):  // no predecessors
      %5 = index_cast %arg2 : i32 to index
      %6 = extract_element %0[%5, %arg1] : tensor<8192x4xf32>
      linalg.yield %6 : f32
    } -> tensor<264x4xf32>
    hal.interface.store.tensor %4, @legacy_io::@ret0, offset = %c0 {operand_result_index = 2 : i32} : tensor<264x4xf32>
    return
  }
}

If run with:

$ iree-opt -linalg-fusion-for-tensor-ops a.mlir

we will get a fused result

#map0 = affine_map<(d0, d1, d2) -> (d1, d0)>
#map1 = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
module {
  func @foo() {
    %c0 = constant 0 : index
    %0 = hal.interface.load.tensor @legacy_io::@arg0, offset = %c0 {operand_result_index = 0 : i32} : tensor<8192x4xf32>
    %1 = hal.interface.load.tensor @legacy_io::@arg1, offset = %c0 {operand_result_index = 1 : i32} : tensor<8x33xi32>
    %2 = linalg.indexed_generic {indexing_maps = [#map0, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%1 : tensor<8x33xi32>) {
    ^bb0(%arg0: index, %arg1: index, %arg2: index, %arg3: i32):  // no predecessors
      %3 = index_cast %arg3 : i32 to index
      %4 = extract_element %0[%3, %arg2] : tensor<8192x4xf32>
      linalg.yield %4 : f32
    } -> tensor<33x8x4xf32>
    hal.interface.store.tensor %2, @legacy_io::@ret0, offset = %c0 {operand_result_index = 2 : i32} : tensor<33x8x4xf32>
    return
  }
}

I think we will land the fix upstream, so we can use the output of iree-codegen-fusion-of-tensor-ops as test case:

#map0 = affine_map<(d0, d1) -> (d1, d0)>
#map1 = affine_map<(d0, d1) -> (d0, d1)>
#map2 = affine_map<(d0, d1) -> (d0)>
module {
  func @foo() {
    %c0 = constant 0 : index
    %0 = hal.interface.load.tensor @legacy_io::@arg0, offset = %c0 {operand_result_index = 0 : i32} : tensor<8192x4xf32>
    %1 = hal.interface.load.tensor @legacy_io::@arg1, offset = %c0 {operand_result_index = 1 : i32} : tensor<8x33xi32>
    %2 = linalg.generic {indexing_maps = [#map0, #map1], iterator_types = ["parallel", "parallel"]} ins(%1 : tensor<8x33xi32>) {
    ^bb0(%arg0: i32):  // no predecessors
      linalg.yield %arg0 : i32
    } -> tensor<33x8xi32>
    %3 = linalg.tensor_reshape %2 [#map1] : tensor<33x8xi32> into tensor<264xi32>
    %4 = linalg.indexed_generic {indexing_maps = [#map2, #map1], iterator_types = ["parallel", "parallel"]} ins(%3 : tensor<264xi32>) {
    ^bb0(%arg0: index, %arg1: index, %arg2: i32):  // no predecessors
      %5 = index_cast %arg2 : i32 to index
      %6 = extract_element %0[%5, %arg1] : tensor<8192x4xf32>
      linalg.yield %6 : f32
    } -> tensor<264x4xf32>
    hal.interface.store.tensor %4, @legacy_io::@ret0, offset = %c0 {operand_result_index = 2 : i32} : tensor<264x4xf32>
    return
  }
}

Activity

  1. added
    bug 🐞
    codegen
    on Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug 🐞codegen

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions