|
| 1 | +//===- TransposeMatmul.cpp - Convert Linalg matmul to transposed variants -===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// This is intended to be a simple high-level (target-agnostic) matmul |
| 9 | +// transposition transformation. |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#include "mlir/Dialect/Linalg/Transforms/Transforms.h" |
| 13 | +#include "mlir/IR/PatternMatch.h" |
| 14 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 15 | + |
| 16 | +#define DEBUG_TYPE "linalg-transpose-matmul" |
| 17 | + |
| 18 | +using namespace mlir; |
| 19 | +using namespace mlir::linalg; |
| 20 | + |
| 21 | +namespace { |
| 22 | +/// Pattern to replace |
| 23 | +/// |
| 24 | +/// linalg.matmul(a, b) |
| 25 | +/// |
| 26 | +/// with |
| 27 | +/// |
| 28 | +/// linalg.matmul_transpose_a(linalg.transpose(a), b) |
| 29 | +/// |
| 30 | +/// By default the LHS is transposed. Set `transposeLHS=false` to |
| 31 | +/// transpose RHS instead. |
| 32 | +struct TransposeMatmul final : public OpRewritePattern<linalg::MatmulOp> { |
| 33 | + TransposeMatmul(MLIRContext *ctx, bool transposeLHS) |
| 34 | + : OpRewritePattern(ctx), transposeLHS(transposeLHS) {} |
| 35 | + |
| 36 | + LogicalResult matchAndRewrite(linalg::MatmulOp matmulOp, |
| 37 | + PatternRewriter &rewriter) const override { |
| 38 | + if (!bufferization::hasTensorSemantics(matmulOp)) |
| 39 | + return rewriter.notifyMatchFailure( |
| 40 | + matmulOp, "only matmul ops with tensors are supported"); |
| 41 | + |
| 42 | + Location loc = matmulOp.getLoc(); |
| 43 | + Value input = matmulOp.getInputs()[transposeLHS ? 0 : 1]; |
| 44 | + auto type = cast<ShapedType>(input.getType()); |
| 45 | + |
| 46 | + SmallVector<Value> dynamicDims; |
| 47 | + if (type.isDynamicDim(1)) |
| 48 | + dynamicDims.push_back(rewriter.create<tensor::DimOp>(loc, input, 1)); |
| 49 | + if (type.isDynamicDim(0)) |
| 50 | + dynamicDims.push_back(rewriter.create<tensor::DimOp>(loc, input, 0)); |
| 51 | + |
| 52 | + ArrayRef<int64_t> shape = type.getShape(); |
| 53 | + Value empty = rewriter.create<tensor::EmptyOp>( |
| 54 | + loc, ArrayRef<int64_t>{shape[1], shape[0]}, type.getElementType(), |
| 55 | + dynamicDims); |
| 56 | + auto transposeOp = rewriter.create<linalg::TransposeOp>( |
| 57 | + loc, input, empty, ArrayRef<int64_t>{1, 0}); |
| 58 | + if (transposeLHS) { |
| 59 | + rewriter.replaceOpWithNewOp<linalg::MatmulTransposeAOp>( |
| 60 | + matmulOp, matmulOp.getResultTypes(), |
| 61 | + ValueRange{transposeOp->getResult(0), matmulOp.getInputs()[1]}, |
| 62 | + matmulOp.getOutputs()); |
| 63 | + } else { |
| 64 | + rewriter.replaceOpWithNewOp<linalg::MatmulTransposeBOp>( |
| 65 | + matmulOp, matmulOp.getResultTypes(), |
| 66 | + ValueRange{matmulOp.getInputs()[0], transposeOp->getResult(0)}, |
| 67 | + matmulOp.getOutputs()); |
| 68 | + } |
| 69 | + |
| 70 | + return success(); |
| 71 | + } |
| 72 | + |
| 73 | +private: |
| 74 | + bool transposeLHS; |
| 75 | +}; |
| 76 | + |
| 77 | +/// Pattern to replace |
| 78 | +/// |
| 79 | +/// linalg.batch_matmul(a, b) |
| 80 | +/// |
| 81 | +/// with |
| 82 | +/// |
| 83 | +/// linalg.batch_matmul_transpose_a(linalg.transpose(a), b) |
| 84 | +/// |
| 85 | +/// Only the non-batch dimensions are transposed. By default the LHS is |
| 86 | +/// transposed. Set `transposeLHS=false` to transpose RHS instead. |
| 87 | +struct TransposeBatchMatmul final |
| 88 | + : public OpRewritePattern<linalg::BatchMatmulOp> { |
| 89 | + TransposeBatchMatmul(MLIRContext *ctx, bool transposeLHS) |
| 90 | + : OpRewritePattern(ctx), transposeLHS(transposeLHS) {} |
| 91 | + |
| 92 | + LogicalResult matchAndRewrite(linalg::BatchMatmulOp batchMatmulOp, |
| 93 | + PatternRewriter &rewriter) const override { |
| 94 | + if (!bufferization::hasTensorSemantics(batchMatmulOp)) |
| 95 | + return rewriter.notifyMatchFailure( |
| 96 | + batchMatmulOp, "only matmul ops with tensors are supported"); |
| 97 | + |
| 98 | + Location loc = batchMatmulOp.getLoc(); |
| 99 | + Value input = batchMatmulOp.getInputs()[transposeLHS ? 0 : 1]; |
| 100 | + auto type = cast<ShapedType>(input.getType()); |
| 101 | + |
| 102 | + SmallVector<Value> dynamicDims; |
| 103 | + if (type.isDynamicDim(0)) |
| 104 | + dynamicDims.push_back(rewriter.create<tensor::DimOp>(loc, input, 0)); |
| 105 | + if (type.isDynamicDim(2)) |
| 106 | + dynamicDims.push_back(rewriter.create<tensor::DimOp>(loc, input, 2)); |
| 107 | + if (type.isDynamicDim(1)) |
| 108 | + dynamicDims.push_back(rewriter.create<tensor::DimOp>(loc, input, 1)); |
| 109 | + |
| 110 | + ArrayRef<int64_t> shape = type.getShape(); |
| 111 | + Value empty = rewriter.create<tensor::EmptyOp>( |
| 112 | + loc, ArrayRef<int64_t>{shape[0], shape[2], shape[1]}, |
| 113 | + type.getElementType(), dynamicDims); |
| 114 | + auto transposeOp = rewriter.create<linalg::TransposeOp>( |
| 115 | + loc, input, empty, ArrayRef<int64_t>{0, 2, 1}); |
| 116 | + if (transposeLHS) { |
| 117 | + rewriter.replaceOpWithNewOp<linalg::BatchMatmulTransposeAOp>( |
| 118 | + batchMatmulOp, batchMatmulOp.getResultTypes(), |
| 119 | + ValueRange{transposeOp->getResult(0), batchMatmulOp.getInputs()[1]}, |
| 120 | + batchMatmulOp.getOutputs()); |
| 121 | + } else { |
| 122 | + rewriter.replaceOpWithNewOp<linalg::BatchMatmulTransposeBOp>( |
| 123 | + batchMatmulOp, batchMatmulOp.getResultTypes(), |
| 124 | + ValueRange{batchMatmulOp.getInputs()[0], transposeOp->getResult(0)}, |
| 125 | + batchMatmulOp.getOutputs()); |
| 126 | + } |
| 127 | + |
| 128 | + return success(); |
| 129 | + } |
| 130 | + |
| 131 | +private: |
| 132 | + bool transposeLHS; |
| 133 | +}; |
| 134 | +} // namespace |
| 135 | + |
| 136 | +void mlir::linalg::populateTransposeMatmulPatterns(RewritePatternSet &patterns, |
| 137 | + bool transposeLHS) { |
| 138 | + patterns.add<TransposeMatmul, TransposeBatchMatmul>(patterns.getContext(), |
| 139 | + transposeLHS); |
| 140 | +} |
0 commit comments