Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dispatch] Add pattern to bubble expand through extract 1/2 #19325

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,77 @@ struct BubbleUpExpandShapesPass final
void runOnOperation() override;
};

// TODO: move this upstream with other tensor bubbling patterns.
struct BubbleExpandThroughExtract final
IanWood1 marked this conversation as resolved.
Show resolved Hide resolved
: public OpRewritePattern<tensor::ExpandShapeOp> {

using OpRewritePattern<tensor::ExpandShapeOp>::OpRewritePattern;

LogicalResult matchAndRewrite(tensor::ExpandShapeOp expandOp,
PatternRewriter &rewriter) const override {
auto extractOp = expandOp.getSrc().getDefiningOp<tensor::ExtractSliceOp>();
if (!extractOp) {
return failure();
}

auto origType = extractOp.getSourceType();
IanWood1 marked this conversation as resolved.
Show resolved Hide resolved
auto extractedType = extractOp.getType();
auto expandedType = expandOp.getType();

if (origType.getRank() != extractedType.getRank()) {
return rewriter.notifyMatchFailure(
extractOp, "Rank reducing extract_slice not supported");
}

if (!origType.hasStaticShape() || !extractedType.hasStaticShape() ||
!expandedType.hasStaticShape()) {
return failure();
}

auto reassoc = expandOp.getReassociationIndices();
for (auto i : llvm::seq<uint64_t>(0, extractedType.getRank())) {
if (reassoc[i].size() == 1) {
continue;
}

if (origType.getShape()[i] != extractedType.getShape()[i]) {
return rewriter.notifyMatchFailure(
extractOp, "Extract modifies the expanded dimension");
}
}

SmallVector<int64_t> newExpandShape;
SmallVector<int64_t> offsets;
SmallVector<int64_t> sizes;
SmallVector<int64_t> strides;
for (auto [inDim, outDims] : llvm::enumerate(reassoc)) {
if (outDims.size() == 1) {
newExpandShape.push_back(origType.getShape()[inDim]);
offsets.push_back(extractOp.getStaticOffsets()[inDim]);
sizes.push_back(extractOp.getStaticSizes()[inDim]);
strides.push_back(extractOp.getStaticStrides()[inDim]);
} else {
for (auto outDim : outDims) {
newExpandShape.push_back(expandedType.getShape()[outDim]);
offsets.push_back(0);
sizes.push_back(expandedType.getShape()[outDim]);
strides.push_back(1);
}
}
}

Type newExpandType =
RankedTensorType::get(newExpandShape, expandedType.getElementType());
auto newExpand = rewriter.create<tensor::ExpandShapeOp>(
expandOp.getLoc(), newExpandType, extractOp.getSource(), reassoc);

rewriter.replaceOpWithNewOp<tensor::ExtractSliceOp>(
expandOp, expandedType, newExpand, ValueRange{}, ValueRange{},
ValueRange{}, offsets, sizes, strides);
return success();
}
};

} // namespace

void BubbleUpExpandShapesPass::runOnOperation() {
Expand Down Expand Up @@ -87,6 +158,7 @@ void BubbleUpExpandShapesPass::runOnOperation() {
// Add patterns to do some additional cleanup (on top of canonicalizations
// that can be done later) of reshape ops.
tensor::populateFoldTensorEmptyPatterns(bubbleExpandShapePatterns);
bubbleExpandShapePatterns.insert<BubbleExpandThroughExtract>(context);

GreedyRewriteConfig rewriteConfig;
rewriteConfig.maxIterations = GreedyRewriteConfig::kNoLimit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ iree_lit_test_suite(
"form_dispatch_regions.mlir",
"dispatch_linalg_on_tensors.mlir",
"convert_region_to_workgroups.mlir",
"bubble_up_expand_shapes.mlir",
"bubble_up_extract_slice.mlir",
"form_dispatch_workgroups.mlir",
"dispatch_linalg_ext_fusion.mlir",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ iree_lit_test_suite(
lit
SRCS
"attention_fuse_by_expansion.mlir"
"bubble_up_expand_shapes.mlir"
"bubble_up_extract_slice.mlir"
"clone_producers_into_dispatch_regions.mlir"
"collapse_dimensions.mlir"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: iree-opt --split-input-file --mlir-print-local-scope --pass-pipeline="builtin.module(util.func(iree-dispatch-creation-bubble-up-expand-shapes, canonicalize, cse, canonicalize))" %s | FileCheck %s
IanWood1 marked this conversation as resolved.
Show resolved Hide resolved

util.func public @bubbble_expand_through_extract(%arg0 : tensor<2x4096x5120xf16>) -> (tensor<2x64x64x2560xf16>) {
%extracted_slice_237 = tensor.extract_slice %arg0[0, 0, 0] [2, 4096, 2560] [1, 1, 1] : tensor<2x4096x5120xf16> to tensor<2x4096x2560xf16>
%expanded_239 = tensor.expand_shape %extracted_slice_237 [[0], [1, 2], [3]] output_shape [2, 64, 64, 2560] : tensor<2x4096x2560xf16> into tensor<2x64x64x2560xf16>
util.return %expanded_239 : tensor<2x64x64x2560xf16>
}

// CHECK-LABEL: @bubbble_expand_through_extract
// CHECK: %[[EXPAND:.+]] = tensor.expand_shape
// CHECK: %[[EXTRACT:.+]] = tensor.extract_slice %[[EXPAND]]

// -----


IanWood1 marked this conversation as resolved.
Show resolved Hide resolved
util.func public @unsupported_bubbble_expand_through_extract(%arg0 : tensor<2x4096x5120xf16>) -> (tensor<2x32x64x2560xf16>) {
%extracted_slice_237 = tensor.extract_slice %arg0[0, 0, 0] [2, 2048, 2560] [1, 1, 1] : tensor<2x4096x5120xf16> to tensor<2x2048x2560xf16>
%expanded_239 = tensor.expand_shape %extracted_slice_237 [[0], [1, 2], [3]] output_shape [2, 32, 64, 2560] : tensor<2x2048x2560xf16> into tensor<2x32x64x2560xf16>
util.return %expanded_239 : tensor<2x32x64x2560xf16>
}

// CHECK-LABEL: @unsupported_bubbble_expand_through_extract
// CHECK: %[[EXTRACT:.+]] = tensor.extract_slice
// CHECK: %[[EXPAND:.+]] = tensor.expand_shape %[[EXTRACT]]
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ util.func public @bubble_up_extract_with_use(%arg0 : tensor<1024x7x7x2xi8>) -> (
// CHECK-SAME: ins(%[[EXTRACT0]] : tensor<1024x7x7xi8>)
// CHECK: util.return %[[GENERIC1]], %[[GENERIC0]]

// -----

util.func public @bubble_up_extract_fill_multi_use() -> tensor<2x320x130x130xf8E4M3FNUZ> {
%cst_1 = arith.constant 1.000000e+00 : f8E4M3FNUZ
%cst_2 = arith.constant 2.000000e+00 : f8E4M3FNUZ
Expand Down
Loading