-
Notifications
You must be signed in to change notification settings - Fork 341
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
Add runtime check for Gather Op #3069
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
392f718
implemented
chentong319 c7325c3
Merge remote-tracking branch 'upstream/main' into gather-check
chentong319 6961067
Merge remote-tracking branch 'upstream/main' into gather-check
chentong319 f6b3e7a
add node name
chentong319 00352c5
format
chentong319 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,10 @@ | |
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/ControlFlow/IR/ControlFlow.h" | ||
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h" | ||
|
||
#include "src/Compiler/CompilerOptions.hpp" | ||
#include "src/Conversion/ONNXToKrnl/ONNXToKrnlCommon.hpp" | ||
#include "src/Dialect/ONNX/ONNXOps/ShapeHelper.hpp" | ||
|
||
|
@@ -31,7 +35,8 @@ struct ONNXGatherElementsOpLowering | |
Location loc = ONNXLoc<ONNXGatherElementsOp>(op); | ||
ValueRange operands = adaptor.getOperands(); | ||
|
||
MultiDialectBuilder<KrnlBuilder, IndexExprBuilderForKrnl, MemRefBuilder> | ||
MultiDialectBuilder<KrnlBuilder, IndexExprBuilderForKrnl, MemRefBuilder, | ||
MathBuilder> | ||
create(rewriter, loc); | ||
|
||
// Get shape. | ||
|
@@ -93,6 +98,32 @@ struct ONNXGatherElementsOpLowering | |
index = index.selectOrSelf(index < zero, index + axisDim); | ||
} | ||
|
||
// Check the dynamic requirement of GatherElement Op | ||
// Refer to the comments in Gather.cpp | ||
if (enableSafeCodeGen) { | ||
// From onnx document: | ||
// All index values are expected to be within bounds [-s, s-1] | ||
// along axis of size s. It is an error if any of the index values | ||
// are out of bounds. | ||
// After the negative correction, the range should be [0, s-1] | ||
Value upperBound = create.mem.dim(data, axis); | ||
Value compareUpperBound = | ||
create.math.slt(index.getValue(), upperBound); | ||
std::string nodeNameStr = op->getName().getStringRef().str() + " "; | ||
StringAttr nodeName = | ||
op->getAttrOfType<mlir::StringAttr>("onnx_node_name"); | ||
if (nodeName && !nodeName.getValue().empty()) { | ||
nodeNameStr = nodeNameStr + nodeName.getValue().str(); | ||
} | ||
rewriter.create<cf::AssertOp>(loc, compareUpperBound, | ||
"indices of GatherOp is larger than the upper bound"); | ||
LiteralIndexExpr zero(0); | ||
Value compareLowerBound = | ||
create.math.sge(index.getValue(), zero.getValue()); | ||
rewriter.create<cf::AssertOp>(loc, compareLowerBound, | ||
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. same here, can use |
||
"indices of GatherOp is less than the lower bound"); | ||
} | ||
|
||
// Access function for the 'data' tensor. | ||
DimsExpr dataAccessFct; | ||
for (int64_t i = 0; i < dataRank; ++i) | ||
|
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.
@chentong319 I know it is not optimized for speed, "anding" both condition and calling assert only once would speed the check up a bit.
create.math.andi(compareUpperBound, compareLowerBound)
.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.
Separate the check to provide more accurate error message.