-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][linalg] Add patterns to convert matmul to transposed variants #89075
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
Merged
c-rhodes
merged 8 commits into
llvm:main
from
c-rhodes:mlir-linalg-matmul-to-matmul-transpose-a-pass
Apr 23, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3bd53f1
[mlir][linalg] Add pass to transpose A matrix of matmul op
c-rhodes 2009cd0
run clang-format
c-rhodes e407d6e
address comments
c-rhodes 329f945
clarify expectations in pass
c-rhodes 49cbd5d
add batch_matmul
c-rhodes 7891d7d
address comments
c-rhodes 7216af3
address comments
c-rhodes a5b91d0
add RUN line to transform sequences
c-rhodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//===- TransposeMatmul.cpp - Convert Linalg matmul to transposed variants -===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// This is intended to be a simple high-level (target-agnostic) matmul | ||
// transposition transformation. | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/Linalg/Transforms/Transforms.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
||
#define DEBUG_TYPE "linalg-transpose-matmul" | ||
|
||
using namespace mlir; | ||
using namespace mlir::linalg; | ||
|
||
namespace { | ||
/// Pattern to replace | ||
/// | ||
/// linalg.matmul(a, b) | ||
/// | ||
/// with | ||
/// | ||
/// linalg.matmul_transpose_a(linalg.transpose(a), b) | ||
/// | ||
/// By default the LHS is transposed. Set `transposeLHS=false` to | ||
/// transpose RHS instead. | ||
struct TransposeMatmul final : public OpRewritePattern<linalg::MatmulOp> { | ||
TransposeMatmul(MLIRContext *ctx, bool transposeLHS) | ||
: OpRewritePattern(ctx), transposeLHS(transposeLHS) {} | ||
|
||
LogicalResult matchAndRewrite(linalg::MatmulOp matmulOp, | ||
PatternRewriter &rewriter) const override { | ||
if (!bufferization::hasTensorSemantics(matmulOp)) | ||
return rewriter.notifyMatchFailure( | ||
matmulOp, "only matmul ops with tensors are supported"); | ||
|
||
Location loc = matmulOp.getLoc(); | ||
Value input = matmulOp.getInputs()[transposeLHS ? 0 : 1]; | ||
auto type = cast<ShapedType>(input.getType()); | ||
|
||
SmallVector<Value> dynamicDims; | ||
if (type.isDynamicDim(1)) | ||
dynamicDims.push_back(rewriter.create<tensor::DimOp>(loc, input, 1)); | ||
if (type.isDynamicDim(0)) | ||
dynamicDims.push_back(rewriter.create<tensor::DimOp>(loc, input, 0)); | ||
|
||
ArrayRef<int64_t> shape = type.getShape(); | ||
Value empty = rewriter.create<tensor::EmptyOp>( | ||
loc, ArrayRef<int64_t>{shape[1], shape[0]}, type.getElementType(), | ||
dynamicDims); | ||
auto transposeOp = rewriter.create<linalg::TransposeOp>( | ||
loc, input, empty, ArrayRef<int64_t>{1, 0}); | ||
if (transposeLHS) { | ||
rewriter.replaceOpWithNewOp<linalg::MatmulTransposeAOp>( | ||
matmulOp, matmulOp.getResultTypes(), | ||
ValueRange{transposeOp->getResult(0), matmulOp.getInputs()[1]}, | ||
matmulOp.getOutputs()); | ||
} else { | ||
rewriter.replaceOpWithNewOp<linalg::MatmulTransposeBOp>( | ||
matmulOp, matmulOp.getResultTypes(), | ||
ValueRange{matmulOp.getInputs()[0], transposeOp->getResult(0)}, | ||
matmulOp.getOutputs()); | ||
} | ||
|
||
return success(); | ||
} | ||
|
||
private: | ||
bool transposeLHS; | ||
}; | ||
|
||
/// Pattern to replace | ||
/// | ||
/// linalg.batch_matmul(a, b) | ||
/// | ||
/// with | ||
/// | ||
/// linalg.batch_matmul_transpose_a(linalg.transpose(a), b) | ||
/// | ||
/// Only the non-batch dimensions are transposed. By default the LHS is | ||
/// transposed. Set `transposeLHS=false` to transpose RHS instead. | ||
struct TransposeBatchMatmul final | ||
: public OpRewritePattern<linalg::BatchMatmulOp> { | ||
TransposeBatchMatmul(MLIRContext *ctx, bool transposeLHS) | ||
: OpRewritePattern(ctx), transposeLHS(transposeLHS) {} | ||
|
||
LogicalResult matchAndRewrite(linalg::BatchMatmulOp batchMatmulOp, | ||
PatternRewriter &rewriter) const override { | ||
if (!bufferization::hasTensorSemantics(batchMatmulOp)) | ||
return rewriter.notifyMatchFailure( | ||
batchMatmulOp, "only matmul ops with tensors are supported"); | ||
|
||
Location loc = batchMatmulOp.getLoc(); | ||
Value input = batchMatmulOp.getInputs()[transposeLHS ? 0 : 1]; | ||
auto type = cast<ShapedType>(input.getType()); | ||
|
||
SmallVector<Value> dynamicDims; | ||
if (type.isDynamicDim(0)) | ||
dynamicDims.push_back(rewriter.create<tensor::DimOp>(loc, input, 0)); | ||
if (type.isDynamicDim(2)) | ||
dynamicDims.push_back(rewriter.create<tensor::DimOp>(loc, input, 2)); | ||
if (type.isDynamicDim(1)) | ||
dynamicDims.push_back(rewriter.create<tensor::DimOp>(loc, input, 1)); | ||
|
||
ArrayRef<int64_t> shape = type.getShape(); | ||
Value empty = rewriter.create<tensor::EmptyOp>( | ||
loc, ArrayRef<int64_t>{shape[0], shape[2], shape[1]}, | ||
type.getElementType(), dynamicDims); | ||
auto transposeOp = rewriter.create<linalg::TransposeOp>( | ||
loc, input, empty, ArrayRef<int64_t>{0, 2, 1}); | ||
if (transposeLHS) { | ||
rewriter.replaceOpWithNewOp<linalg::BatchMatmulTransposeAOp>( | ||
batchMatmulOp, batchMatmulOp.getResultTypes(), | ||
ValueRange{transposeOp->getResult(0), batchMatmulOp.getInputs()[1]}, | ||
batchMatmulOp.getOutputs()); | ||
} else { | ||
rewriter.replaceOpWithNewOp<linalg::BatchMatmulTransposeBOp>( | ||
batchMatmulOp, batchMatmulOp.getResultTypes(), | ||
ValueRange{batchMatmulOp.getInputs()[0], transposeOp->getResult(0)}, | ||
batchMatmulOp.getOutputs()); | ||
} | ||
|
||
return success(); | ||
} | ||
|
||
private: | ||
bool transposeLHS; | ||
}; | ||
} // namespace | ||
|
||
void mlir::linalg::populateTransposeMatmulPatterns(RewritePatternSet &patterns, | ||
bool transposeLHS) { | ||
patterns.add<TransposeMatmul, TransposeBatchMatmul>(patterns.getContext(), | ||
transposeLHS); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// RUN: mlir-opt %s | ||
|
||
module attributes {transform.with_named_sequence} { | ||
c-rhodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { | ||
%0 = transform.structured.match ops{["func.func"]} in %arg1 : (!transform.any_op) -> !transform.any_op | ||
transform.apply_patterns to %0 { | ||
transform.apply_patterns.linalg.transpose_matmul | ||
} : !transform.any_op | ||
transform.apply_cse to %0 : !transform.any_op | ||
transform.apply_patterns to %0 { | ||
transform.apply_patterns.canonicalization | ||
} : !transform.any_op | ||
transform.yield | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// RUN: mlir-opt %s | ||
|
||
module attributes {transform.with_named_sequence} { | ||
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) { | ||
%0 = transform.structured.match ops{["func.func"]} in %arg1 : (!transform.any_op) -> !transform.any_op | ||
transform.apply_patterns to %0 { | ||
transform.apply_patterns.linalg.transpose_matmul <rhs> | ||
} : !transform.any_op | ||
transform.apply_cse to %0 : !transform.any_op | ||
transform.apply_patterns to %0 { | ||
transform.apply_patterns.canonicalization | ||
} : !transform.any_op | ||
transform.yield | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.