-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][tosa] Introduce accumulator type for reduce_sum
on bf16
#158389
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
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1160,6 +1160,12 @@ static LogicalResult reduceMatchAndRewriteHelper(OpTy op, uint64_t axis, | |
auto elementTy = resultTy.getElementType(); | ||
Value input = op->getOperand(0); | ||
|
||
// Figure out the accType if needed | ||
bool widenAccTy = std::is_same_v<OpTy, tosa::ReduceSumOp> && | ||
isa<FloatType>(elementTy) && | ||
cast<FloatType>(elementTy).isBF16(); | ||
Type accTy = widenAccTy ? rewriter.getF32Type() : elementTy; | ||
|
||
SmallVector<int64_t> reduceShape; | ||
SmallVector<Value> dynDims; | ||
for (unsigned i = 0; i < inputTy.getRank(); i++) { | ||
|
@@ -1174,11 +1180,11 @@ static LogicalResult reduceMatchAndRewriteHelper(OpTy op, uint64_t axis, | |
inputs.push_back(input); | ||
|
||
// First fill the output buffer with the init value. | ||
auto emptyTensor = tensor::EmptyOp::create(rewriter, loc, reduceShape, | ||
resultTy.getElementType(), dynDims) | ||
.getResult(); | ||
auto emptyTensor = | ||
tensor::EmptyOp::create(rewriter, loc, reduceShape, accTy, dynDims) | ||
.getResult(); | ||
|
||
auto fillValueAttr = createInitialValueForReduceOp(op, elementTy, rewriter); | ||
auto fillValueAttr = createInitialValueForReduceOp(op, accTy, rewriter); | ||
if (!fillValueAttr) | ||
return rewriter.notifyMatchFailure( | ||
op, "No initial value found for reduction operation"); | ||
|
@@ -1231,8 +1237,14 @@ static LogicalResult reduceMatchAndRewriteHelper(OpTy op, uint64_t axis, | |
[&](OpBuilder &nestedBuilder, Location nestedLoc, ValueRange blockArgs) { | ||
std::array<Value, 2> binaryArgs{ | ||
blockArgs[0], isNanIgnoreMode ? blockArgs[2] : blockArgs[1]}; | ||
auto result = createLinalgBodyCalculationForReduceOp( | ||
op, binaryArgs, elementTy, rewriter); | ||
|
||
// If reduction type differs then extend (applicable to reduce_sum) | ||
if (binaryArgs[0].getType() != accTy) | ||
binaryArgs[0] = arith::ExtFOp::create(nestedBuilder, nestedLoc, accTy, | ||
binaryArgs[0]); | ||
|
||
auto result = createLinalgBodyCalculationForReduceOp(op, binaryArgs, | ||
accTy, rewriter); | ||
if (result) | ||
didEncounterError = true; | ||
|
||
|
@@ -1273,12 +1285,11 @@ static LogicalResult reduceMatchAndRewriteHelper(OpTy op, uint64_t axis, | |
|
||
// Create a tensor full of NaNs. | ||
auto nanValueAttr = rewriter.getFloatAttr( | ||
elementTy, | ||
accTy, | ||
APFloat::getNaN(cast<FloatType>(elementTy).getFloatSemantics(), false)); | ||
auto nanValue = arith::ConstantOp::create(rewriter, loc, nanValueAttr); | ||
auto emptyNanTensor = | ||
tensor::EmptyOp::create(rewriter, loc, reduceShape, | ||
resultTy.getElementType(), dynDims) | ||
tensor::EmptyOp::create(rewriter, loc, reduceShape, accTy, dynDims) | ||
.getResult(); | ||
auto nanFilledTensor = | ||
linalg::FillOp::create(rewriter, loc, ValueRange{nanValue}, | ||
|
@@ -1288,8 +1299,7 @@ static LogicalResult reduceMatchAndRewriteHelper(OpTy op, uint64_t axis, | |
// Create an empty tensor, non need to fill this since it will be | ||
// overwritten by the select. | ||
auto finalEmptyTensor = | ||
tensor::EmptyOp::create(rewriter, loc, reduceShape, | ||
resultTy.getElementType(), dynDims) | ||
tensor::EmptyOp::create(rewriter, loc, reduceShape, accTy, dynDims) | ||
.getResult(); | ||
|
||
// Do a selection between the tensors akin to: | ||
|
@@ -1304,9 +1314,32 @@ static LogicalResult reduceMatchAndRewriteHelper(OpTy op, uint64_t axis, | |
linalgOp = linalgSelect; | ||
} | ||
|
||
// Truncate back to resultTy if needed | ||
Value reducedRes = linalgOp->getResult(0); | ||
if (widenAccTy) { | ||
auto resEmptyOp = | ||
tensor::EmptyOp::create(rewriter, loc, reduceShape, elementTy, dynDims) | ||
.getResult(); | ||
|
||
const unsigned reducedRank = | ||
cast<ShapedType>(reducedRes.getType()).getRank(); | ||
auto identityMap = rewriter.getMultiDimIdentityMap(reducedRank); | ||
reducedRes = | ||
linalg::GenericOp::create( | ||
rewriter, loc, resEmptyOp.getType(), ValueRange{reducedRes}, | ||
ValueRange{resEmptyOp}, | ||
ArrayRef<AffineMap>{identityMap, identityMap}, | ||
getNParallelLoopsAttrs(reducedRank), | ||
[&](OpBuilder &nestedBuilder, Location nestedLoc, ValueRange args) { | ||
Value truncf = arith::TruncFOp::create(nestedBuilder, nestedLoc, | ||
elementTy, args[0]); | ||
linalg::YieldOp::create(nestedBuilder, nestedLoc, truncf); | ||
}) | ||
.getResults()[0]; | ||
} | ||
|
||
SmallVector<ReassociationExprs, 4> reassociationMap; | ||
uint64_t expandInputRank = | ||
cast<ShapedType>(linalgOp->getResults()[0].getType()).getRank(); | ||
uint64_t expandInputRank = cast<ShapedType>(reducedRes.getType()).getRank(); | ||
reassociationMap.resize(expandInputRank); | ||
|
||
for (uint64_t i = 0; i < expandInputRank; i++) { | ||
|
@@ -1324,8 +1357,8 @@ static LogicalResult reduceMatchAndRewriteHelper(OpTy op, uint64_t axis, | |
// since here we know which dimension to expand, and `tosa::ReshapeOp` would | ||
// not have access to such information. This matters when handling dynamically | ||
// sized tensors. | ||
rewriter.replaceOpWithNewOp<tensor::ExpandShapeOp>( | ||
op, resultTy, linalgOp->getResults()[0], reassociationMap); | ||
rewriter.replaceOpWithNewOp<tensor::ExpandShapeOp>(op, resultTy, reducedRes, | ||
reassociationMap); | ||
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. strange whitespace 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. Not sure. Just ran clang-format. The |
||
return success(); | ||
} | ||
|
||
|
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
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.
Do you need an opType guard here if anything later mandates different handling of other reductions ?
Uh oh!
There was an error while loading. Please reload this page.
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.
It is already kind of guarded by OpType as this is only true when the operation is a reduce sum. We might need to rework this all-together if it mandates different handling other reductions.