Skip to content

Commit 6f2ba47

Browse files
authored
[mlir] Fix ComposeExpandOfCollapseOp for dynamic case (#142663)
Changes `findCollapsingReassociation` to return nullopt in all cases where source shape has `>=2` dynamic dims. `expand(collapse)` can reshape to in any valid output shape but a collapse can only collapse contiguous dimensions. When there are `>=2` dynamic dimensions it is impossible to determine if it can be simplified to a collapse or if it is preforming a more advanced reassociation. This problem was uncovered by #137963 --------- Signed-off-by: Ian Wood <[email protected]>
1 parent 7838fc0 commit 6f2ba47

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

mlir/include/mlir/Dialect/Utils/ReshapeOpsUtils.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,14 @@ struct ComposeExpandOfCollapseOp : public OpRewritePattern<ExpandOpTy> {
387387
auto resultSubShape =
388388
resultShape.slice(resultIndices.front(), resultIndices.size());
389389

390+
if (llvm::count_if(srcSubShape, ShapedType::isDynamic) >= 2 &&
391+
llvm::count_if(resultSubShape, ShapedType::isDynamic) >= 2)
392+
return std::nullopt;
393+
390394
if (srcSubShape.size() == resultSubShape.size()) {
391-
if (srcSubShape != resultSubShape ||
392-
llvm::count_if(srcSubShape, ShapedType::isDynamic) >= 2) {
395+
if (srcSubShape != resultSubShape)
393396
return std::nullopt;
394-
}
397+
395398
for (auto index : llvm::seq<int64_t>(0, srcSubShape.size())) {
396399
composedReassociation.emplace_back(1, srcIndices.front() + index);
397400
}

mlir/test/Dialect/Tensor/canonicalize.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,20 @@ func.func @compose_expand_of_collapse_dynamic(%arg0 : tensor<4x?x10x64x2xf16>, %
12721272

12731273
// -----
12741274

1275+
func.func @no_compose_collapse_of_expand_dynamic(%arg0 : tensor<?x8x128x?xf16>, %arg1: index) -> tensor<?x128x?xf16> {
1276+
%collapse = tensor.collapse_shape %arg0 [[0, 1, 2, 3]] : tensor<?x8x128x?xf16> into tensor<?xf16>
1277+
%expanded_19 = tensor.expand_shape %collapse [[0, 1, 2]] output_shape [%arg1, 8, %arg1] : tensor<?xf16> into tensor<?x128x?xf16>
1278+
return %expanded_19 : tensor<?x128x?xf16>
1279+
}
1280+
// CHECK-LABEL: func @no_compose_collapse_of_expand_dynamic
1281+
// CHECK-SAME: %[[ARG0:.+]]: tensor
1282+
// CHECK-SAME: %[[ARG1:.+]]: index
1283+
// CHECK: %[[COLLAPSE:.+]] = tensor.collapse_shape %[[ARG0]]
1284+
// CHECK: %[[EXPAND:.+]] = tensor.expand_shape %[[COLLAPSE]]
1285+
// CHECK: return %[[EXPAND]]
1286+
1287+
// -----
1288+
12751289
// CHECK-LABEL: func @zero_rank_reshape_multi
12761290
func.func @zero_rank_reshape_multi(%arg0: tensor<f32>) -> tensor<f32> {
12771291
// CHECK: return %arg0

0 commit comments

Comments
 (0)