Skip to content

Add support for fusing generic ops that have reduction loop #4219

Closed

Description

@hanhanW
#map0 = affine_map<(d0, d1) -> (d0, d1)>
#map1 = affine_map<(d0) -> (0, d0)>
#map2 = affine_map<(d0) -> (0)>
module  {
  func @foo(%arg0: tensor<1x10xf32>, %arg1: tensor<1x10xf32>, %arg2: tensor<1xf32>) -> tensor<1xf32> {
    %0 = linalg.generic {indexing_maps = [#map0, #map0, #map0], iterator_types = ["parallel", "parallel"]} ins(%arg0, %arg1 : tensor<1x10xf32>, tensor<1x10xf32>) {
    ^bb0(%arg3: f32, %arg4: f32):  // no predecessors
      %2 = addf %arg3, %arg4 : f32
      linalg.yield %2 : f32
    } -> tensor<1x10xf32>
    %1 = linalg.generic {indexing_maps = [#map1, #map2], iterator_types = ["reduction"]} ins(%0 : tensor<1x10xf32>) init(%arg2 : tensor<1xf32>)  {
    ^bb0(%arg3: f32, %arg4: f32):  // no predecessors
      %2 = addf %arg3, %arg4 : f32
      linalg.yield %2 : f32
    } -> tensor<1xf32>
    return %1 : tensor<1xf32>
  }
}

The current implementation does not handle the case. This issue is for tracking it.

Activity

  1. added
    enhancement ➕
    codegen
    on Dec 15, 2020
  2. hanhanW commented on Dec 16, 2020

    @hanhanW
    ContributorAuthor

    I feel the #map0 and #map1 could rewrite to 2D form. Then, we can fuse the op into:

    #map0 = affine_map<(d0, d1) -> (d0, d1)>
    #map1 = affine_map<(d0, d1) -> (d0)>
    module  {
      func @foo(%arg0: tensor<1x10xf32>, %arg1: tensor<1x10xf32>, %arg2: tensor<1xf32>) -> tensor<1xf32> {
        %0 = linalg.generic {
            indexing_maps = [#map0, #map0, #map1],
            iterator_types = ["parallel", "reduction"]
          } ins(%arg0, %arg1 : tensor<1x10xf32>, tensor<1x10xf32>)
            init(%arg2 : tensor<1xf32>)  {
        ^bb0(%arg3: f32, %arg4: f32, %arg5: f32):  // no predecessors
          %1 = addf %arg3, %arg4 : f32
          %2 = addf %1, %arg5 : f32
          linalg.yield %2 : f32
        } -> tensor<1xf32>
        return %0 : tensor<1xf32>
      }
    }

    The second parallel loop of the first op can be combined with the reduction loop of the second op. After computing the result of the first op, do the reduction computation.

    After converting to memref, then to the loop:

      func @foo(%arg0: memref<1x10xf32>, %arg1: memref<1x10xf32>, %arg2: memref<1xf32>) {
        %c10 = constant 10 : index
        %c0 = constant 0 : index
        %c1 = constant 1 : index
        scf.for %arg3 = %c0 to %c1 step %c1 {
          scf.for %arg4 = %c0 to %c10 step %c1 {
            %0 = load %arg0[%arg3, %arg4] : memref<1x10xf32>
            %1 = load %arg1[%arg3, %arg4] : memref<1x10xf32>
            %2 = load %arg2[%arg3] : memref<1xf32>
            %3 = addf %0, %1 : f32
            %4 = addf %3, %2 : f32
            store %4, %arg2[%arg3] : memref<1xf32>
          }
        }
        return
      }

    I think the concern here is "do we want to fuse these operations?" The reason is that we will lose the parallel opportunity for the second loop in the first op. If that is a big chunk and is element-wise operation, we probably still want to fully utilize cores. And do the reduction later.

    E.g.,

      func @foo(%arg0: memref<1x10xf32>, %arg1: memref<1x10xf32>, %arg2: memref<1x10xf32>, %arg3: memref<1xf32>) {
        %c10 = constant 10 : index
        %c0 = constant 0 : index
        %c1 = constant 1 : index
        scf.parallel (%arg4, %arg5) = (%c0, %c0) to (%c1, %c10) step (%c1, %c1) {
          %0 = load %arg0[%arg4, %arg5] : memref<1x10xf32>
          %1 = load %arg1[%arg4, %arg5] : memref<1x10xf32>
          %2 = addf %0, %1 : f32
          store %2, %arg2[%arg4, %arg5] : memref<1x10xf32>
          scf.yield
        }
        scf.parallel (%arg4) = (%c0) to (%c1) step (%c1) {
          scf.for %arg5 = %c0 to %c10 step %c1 {
            %0 = load %arg2[%arg4, %arg5] : memref<1x10xf32>
            %1 = load %arg3[%arg4] : memref<1xf32>
            %2 = addf %0, %1 : f32
            store %2, %arg3[%arg4] : memref<1xf32>
          }
          scf.yield
        }
        return
      }

    However, we can still support the fusion, and leave the decision to analysis pass.

    @nicolasvasilache

  3. MaheshRavishankar commented on Dec 16, 2020

    @MaheshRavishankar
    Collaborator

    One way to look at this is as map -> reduce fusion. Typically this is done when the reduction is also executed in parallel (using tree reduction), but when the reduction is serialized, I agree there is a problem.

    For the most case though, I dont think its common to see cases with just a single "reduction" iterator type. We usually see ["parallel", "parallel", ..., "reduce"]. In that case we can loose some degree of parallelism, but might still have enough to justify the fusion.

    My suggestion is to implement the fusion (for more general case where the consumer might have mix of "parallel" and "reduction" iterator types). We can then make a decision in IREE if we want to use this pattern or not.

    I think its fair to say we can rule out fusion producers that have "reduction" with their consumers. That will probably be done through tile + fuse cause it needs imperfectly nested loops.

  4. hanhanW commented on Jan 5, 2021

    @hanhanW
    ContributorAuthor

    I just found that the case is supported at some point. I think this is because we have "init_tensor" for linalg ops that only have parallel loops, so it's magically supported. Added a test in https://reviews.llvm.org/D94093

  5. hanhanW commented on Jan 5, 2021

    @hanhanW
    ContributorAuthor

    The fused result is:

    #map0 = affine_map<(d0) -> (0, d0)>
    #map1 = affine_map<(d0) -> (0)>
    module  {
      func @foo(%arg0: tensor<1x10xf32>, %arg1: tensor<1x10xf32>, %arg2: tensor<1xf32>) -> tensor<1xf32> {
        %0 = linalg.generic {indexing_maps = [#map0, #map0, #map1], iterator_types = ["reduction"]} ins(%arg0, %arg1 : tensor<1x10xf32>, tensor<1x10xf32>) outs(%arg2 : tensor<1xf32>) {
        ^bb0(%arg3: f32, %arg4: f32, %arg5: f32):  // no predecessors
          %1 = addf %arg3, %arg4 : f32
          %2 = addf %1, %arg5 : f32
          linalg.yield %2 : f32
        } -> tensor<1xf32>
        return %0 : tensor<1xf32>
      }
    }
  6. MaheshRavishankar commented on Jan 5, 2021

    @MaheshRavishankar
    Collaborator

    Cool! Do you mind making sure the necessary checks are added upstream?

  7. hanhanW commented on Jan 22, 2021

    @hanhanW
    ContributorAuthor

    Sorry that I missed the issue...

    Yes, the necessary checks were added. They can only be fused iff producer doesn't have "reduction" loops as we've discussed:

    https://github.com/llvm/llvm-project/blob/4edb63bbbe338b890119a307bc323c24d0a1afc7/mlir/lib/Dialect/Linalg/Transforms/FusionOnTensors.cpp#L30-L51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

codegenenhancement ➕

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions