This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Linalg] Avoid collapsing dimensions of linalg op that arent foldable.
ClosedPublic

Authored by mravishankar on May 8 2023, 11:36 PM.

Details

Summary

The collapsing dimensions transformation is limited to only those
cases where the sequence of dimensions are contiguous in all the
ranges of the indexing maps of the operation. Add this check before
applying the transformation.

Diff Detail

Event Timeline

mravishankar created this revision.May 8 2023, 11:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 8 2023, 11:36 PM
mravishankar requested review of this revision.May 8 2023, 11:36 PM
mravishankar edited the summary of this revision. (Show Details)May 8 2023, 11:41 PM
guraypp requested changes to this revision.EditedMay 9 2023, 2:25 AM

I might be missing something but why do we disable collapsing for this case? For the example your provided we can still collapse d0, d1, d2. I put the collapsed version below. Do you think that is it wrong?

#map = affine_map<(d0, d1) -> (d1, d0)>
#map1 = affine_map<(d0, d1) -> (d0, d1)>
module {
  func.func @uncollapsable(%arg0: tensor<41x3x1x57xf32>, %arg1: tensor<3x1x57x41xf32>) -> tensor<3x1x57x41xf32> {
    %collapsed = tensor.collapse_shape %arg0 [[0], [1, 2, 3]] : tensor<41x3x1x57xf32> into tensor<41x171xf32>
    %collapsed_0 = tensor.collapse_shape %arg1 [[0, 1, 2], [3]] : tensor<3x1x57x41xf32> into tensor<171x41xf32>
    %1 = linalg.generic {indexing_maps = [#map, #map1], iterator_types = ["parallel", "parallel"]} ins(%collapsed : tensor<41x171xf32>) outs(%collapsed_0 : tensor<171x41xf32>) {
    ^bb0(%in: f32, %out: f32):
      linalg.yield %in : f32
    } -> tensor<171x41xf32>
    %expanded = tensor.expand_shape %1 [[0, 1, 2], [3]] : tensor<171x41xf32> into tensor<3x1x57x41xf32>
    return %expanded : tensor<3x1x57x41xf32>
  }
}
This revision now requires changes to proceed.May 9 2023, 2:25 AM

I might be missing something but why do we disable collapsing for this case? For the example your provided we can still collapse d0, d1, d2. I put the collapsed version below. Do you think that is it wrong?

#map = affine_map<(d0, d1) -> (d1, d0)>
#map1 = affine_map<(d0, d1) -> (d0, d1)>
module {
  func.func @uncollapsable(%arg0: tensor<41x3x1x57xf32>, %arg1: tensor<3x1x57x41xf32>) -> tensor<3x1x57x41xf32> {
    %collapsed = tensor.collapse_shape %arg0 [[0], [1, 2, 3]] : tensor<41x3x1x57xf32> into tensor<41x171xf32>
    %collapsed_0 = tensor.collapse_shape %arg1 [[0, 1, 2], [3]] : tensor<3x1x57x41xf32> into tensor<171x41xf32>
    %1 = linalg.generic {indexing_maps = [#map, #map1], iterator_types = ["parallel", "parallel"]} ins(%collapsed : tensor<41x171xf32>) outs(%collapsed_0 : tensor<171x41xf32>) {
    ^bb0(%in: f32, %out: f32):
      linalg.yield %in : f32
    } -> tensor<171x41xf32>
    %expanded = tensor.expand_shape %1 [[0, 1, 2], [3]] : tensor<171x41xf32> into tensor<3x1x57x41xf32>
    return %expanded : tensor<3x1x57x41xf32>
  }
}

The test is trying to fold dimensions 2 and 3 (-test-linalg-elementwise-fusion-patterns=collapse-dimensions-control=2,3 -split-input-file) . Those cant be folded. This is just adding a check and bailing on illegal collapse dimensions list .

See https://github.com/llvm/llvm-project/issues/62315

guraypp accepted this revision.May 9 2023, 11:41 AM

Thanks for explaining. I didn’t see that the pass takes arguments.

This revision is now accepted and ready to land.May 9 2023, 11:41 AM