-
Notifications
You must be signed in to change notification settings - Fork 183
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
[Gemmini] Fuse linalg.matmul and linalg.transpose when lowering to Gemmini DIalect #300
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// RUN: buddy-opt %s \ | ||
// RUN: --convert-linalg-to-gemmini | \ | ||
// RUN: FileCheck %s | ||
|
||
memref.global "private" @gv1 : memref<3x4xi8> = dense<[[1, 2, 3, 4], | ||
[5, 6, 7, 8], | ||
[9, 10, 11, 12]]> | ||
memref.global "private" @gv2 : memref<4x3xi8> = dense<[[1, 1, 1], | ||
[1, 1, 1], | ||
[1, 1, 1], | ||
[1, 1, 1]]> | ||
|
||
func.func @main() -> i8 { | ||
%arrayA = memref.get_global @gv1 : memref<3x4xi8> | ||
%arrayB = memref.get_global @gv2 : memref<4x3xi8> | ||
%arrayC = memref.alloc() : memref<3x3xi8> | ||
%cst0 = arith.constant 0 : i8 | ||
gemmini.print %arrayC : memref<3x3xi8> | ||
// Matrix-matrix multiplication | ||
// CHECK: gemmini.tile_matmul %1 %0 %alloc %alloc_0 {aTranspose = true, bTranspose = true} : | ||
// CHECK-SAME: memref<4x3xi8> memref<3x4xi8> memref<3x3xi8> memref<3x4xi32> | ||
linalg.matmul | ||
ins(%arrayA, %arrayB: memref<3x4xi8>, memref<4x3xi8>) | ||
outs(%arrayC: memref<3x3xi8>) | ||
|
||
// transpose | ||
linalg.transpose | ||
ins(%arrayC: memref<3x3xi8>) | ||
outs(%arrayC: memref<3x3xi8>) | ||
permutation = [1, 0] | ||
|
||
gemmini.print %arrayC : memref<3x3xi8> | ||
memref.dealloc %arrayC : memref<3x3xi8> | ||
|
||
return %cst0 : i8 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,10 +63,42 @@ class MatmulLowering : public OpRewritePattern<linalg::MatmulOp> { | |
Value fillOpInputValue = | ||
rewriter.create<arith::ConstantOp>(loc, fillOpInsType, fillOpInputAttr); | ||
rewriter.create<linalg::FillOp>(loc, fillOpInputValue, bias); | ||
rewriter.replaceOpWithNewOp<gemmini::TileMatMulOp>( | ||
matMulOp, input0, input1, output0, bias, /*aScaleFactor = */ scale1, | ||
/*bScaleFactor = */ scale1, /*dScaleFactor = */ scale1, /*act = */ 0, | ||
/*accScale = */ scale1, /*bertScale = */ scale0); | ||
|
||
// llvm::outs() << " has " | ||
// << std::distance(output0.getUses().begin(), | ||
// output0.getUses().end()) | ||
// << " uses:\n"; | ||
// for (Operation *userOp : output0.getUsers()) { | ||
// llvm::outs() << " - " << userOp->getName() << "\n"; | ||
// } | ||
|
||
// If this matmul operation is followed by a transpose operation, do fusion. | ||
// We should make sure that the result of this matmul op only has one user. | ||
Operation* fuseOp = *output0.user_begin(); | ||
int output0Use = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a serious error in logic here.I would suggest writing the pattern inside the canonicalizer of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, thanks for your suggestions. I agree that it is indeed possible to do the match during There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
first
second
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this happens in the model not sure if it does. |
||
for (auto userOp : output0.getUsers()) { | ||
if (auto transposeOp = dyn_cast<linalg::TransposeOp>(userOp)) { | ||
fuseOp = transposeOp; | ||
output0Use ++; | ||
} | ||
} | ||
|
||
if (output0Use) { | ||
// llvm::outs() << "Fuse linalg.matmul and linalg.transpose. \n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comments |
||
rewriter.replaceOpWithNewOp<gemmini::TileMatMulOp>( | ||
matMulOp, input1, input0, output0, bias, /*aScaleFactor = */ scale1, | ||
/*bScaleFactor = */ scale1, /*dScaleFactor = */ scale1, /*act = */0, | ||
/*accScale = */ scale1, /*bertScale = */ scale0, | ||
/*repeatingBias = */ false, /*aTranspose = */ true, | ||
/*bTranspose = */ true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If so, the reasoning is the same, you just need to make |
||
rewriter.eraseOp(fuseOp); | ||
} else { | ||
rewriter.replaceOpWithNewOp<gemmini::TileMatMulOp>( | ||
matMulOp, input0, input1, output0, bias, /*aScaleFactor = */ scale1, | ||
/*bScaleFactor = */ scale1, /*dScaleFactor = */ scale1, /*act = */ 0, | ||
/*accScale = */ scale1, /*bertScale = */ scale0); | ||
} | ||
|
||
rewriter.create<memref::DeallocOp>(loc, bias); | ||
return success(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clear the comments here.