-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[BACKEND] Implement 3xTF32 trick #3234
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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 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 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 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 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 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 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,91 @@ | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
#include "triton/Dialect/TritonGPU/Transforms/Passes.h" | ||
|
||
using namespace mlir; | ||
namespace tt = mlir::triton; | ||
|
||
#define GEN_PASS_CLASSES | ||
#include "triton/Dialect/TritonGPU/Transforms/Passes.h.inc" | ||
|
||
namespace { | ||
|
||
// nb. We call the trick TF32x3 as C++ disallows varaibles starting with numbers | ||
// Implement 3xTF32 trick https://github.com/NVIDIA/cutlass/discussions/385 | ||
// For a, b f32 | ||
// dot(a, b, inputPrecision="tf32x3") -> | ||
// let aBig = f32ToTF32(a), aSmall = a - aBig; | ||
// let bBig = f32ToTF32(b), bSmall = b - bBig; | ||
// dot(aSmall, bBig, inputPrecision="tf32") + | ||
// dot(aBig, bSmall, inputPrecision="tf32") + | ||
// dot(aBig, bBig, inputPrecision="tf32") | ||
class TF32x3 : public OpRewritePattern<tt::DotOp> { | ||
public: | ||
using OpRewritePattern::OpRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(tt::DotOp dotOp, | ||
PatternRewriter &rewriter) const override { | ||
|
||
auto isF32 = [](Value operand) { | ||
return operand.getType() | ||
.cast<RankedTensorType>() | ||
.getElementType() | ||
.isF32(); | ||
}; | ||
|
||
if (!(dotOp.getInputPrecision() == tt::InputPrecision::TF32x3 && | ||
isF32(dotOp.getA()) && isF32(dotOp.getB()))) { | ||
return failure(); | ||
} | ||
|
||
// Aux functions | ||
auto f32ToTF32 = [&](Value value) -> Value { | ||
return rewriter | ||
.create<tt::ElementwiseInlineAsmOp>( | ||
dotOp.getLoc(), value.getType(), "cvt.rna.tf32.f32 $0, $1;", | ||
"=r,r", | ||
/*isPure=*/true, /*pack=*/1, ArrayRef<Value>{value}) | ||
.getResult()[0]; | ||
}; | ||
auto sub = [&](Value a, Value b) -> Value { | ||
return rewriter.create<arith::SubFOp>(dotOp.getLoc(), a, b); | ||
}; | ||
auto dot = [&](Value a, Value b, Value c) -> Value { | ||
return rewriter.create<tt::DotOp>(dotOp->getLoc(), c.getType(), a, b, c, | ||
tt::InputPrecision::TF32, | ||
dotOp.getMaxNumImpreciseAcc()); | ||
}; | ||
|
||
auto aBig = f32ToTF32(dotOp.getA()); | ||
auto aSmall = sub(dotOp.getA(), aBig); | ||
|
||
auto bBig = f32ToTF32(dotOp.getB()); | ||
auto bSmall = sub(dotOp.getB(), bBig); | ||
|
||
auto dot1 = dot(aSmall, bBig, dotOp.getC()); | ||
auto dot2 = dot(aBig, bSmall, dot1); | ||
auto dot3 = dot(aBig, bBig, dot2); | ||
|
||
rewriter.replaceOp(dotOp, dot3); | ||
return success(); | ||
} | ||
}; | ||
|
||
struct F32DotTCPass : public TritonGPUF32DotTCBase<F32DotTCPass> { | ||
void runOnOperation() override { | ||
MLIRContext *context = &getContext(); | ||
ModuleOp m = getOperation(); | ||
|
||
RewritePatternSet decomposePatterns(context); | ||
decomposePatterns.add<TF32x3>(context); | ||
if (applyPatternsAndFoldGreedily(m, std::move(decomposePatterns)) | ||
.failed()) { | ||
signalPassFailure(); | ||
} | ||
} | ||
}; | ||
} // anonymous namespace | ||
|
||
std::unique_ptr<Pass> mlir::triton::gpu::createF32DotTCPass() { | ||
return std::make_unique<F32DotTCPass>(); | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.
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.
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.
Presumably these descriptions should be on the enum now.
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.
I think this comment still applies?
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.
I figured it'd be better to leave them in the place where one would first look at when finding what this flag really does, but sure, I can move them
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.
For example the enum is also used on dot_async.
(If you wanted a "see X" comment I'd be fine with that, although it's probably pretty obvious where to look.)