-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][tosa] Add verifiers to ReduceOps, fix shape inference crash #69843
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
Conversation
The `tosa.reduce_*` ops take an `axis` Attribute that determines along which dimension the reduction takes place. A crash can occur during shape inference when the input tensor rank is so low that the given axis doesn't exist. Fix llvm#68187
@llvm/pr-subscribers-mlir-tosa @llvm/pr-subscribers-mlir Author: Felix Schneider (ubfx) ChangesThe Fix #68187 Full diff: https://github.com/llvm/llvm-project/pull/69843.diff 1 Files Affected:
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
index e03904a1611fc42..5292465477b1094 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -1109,14 +1109,14 @@ LogicalResult tosa::ScatterOp::inferReturnTypeComponents(
static LogicalResult ReduceInferReturnTypes(
ShapeAdaptor operandShape, Type inputType, IntegerAttr axis,
SmallVectorImpl<ShapedTypeComponents> &inferredReturnShapes) {
- if (!operandShape.hasRank() || operandShape.getRank() == 0) {
+ int64_t axisVal = axis.getValue().getSExtValue();
+ if (!operandShape.hasRank() || operandShape.getRank() <= axisVal) {
inferredReturnShapes.push_back(ShapedTypeComponents(inputType));
return success();
}
SmallVector<int64_t> outputShape;
operandShape.getDims(outputShape);
- int64_t axisVal = axis.getValue().getSExtValue();
outputShape[axisVal] = 1;
inferredReturnShapes.push_back(ShapedTypeComponents(outputShape, inputType));
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.
Can we get a test for this?
When writing the test, I noticed that the ReduceOp folder has a similar issue where it crashes when the |
Behavior is debatable but my assumption would be that an error should be triggered if the |
Makes sense, we could implement this as a verifier for the Op? Then we wouldn't emit the error in the shape inference pass itself, but in the verification afterwards. |
The spec calls for an error if axis > the rank of the input tensor: https://www.mlplatform.org/tosa/tosa_spec.html#_reduce_all
Adding it as a verifier for the op seems like a good option. |
ef5bf7f
to
c68f170
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
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.
Thanks for the test updates, looks good to me. (assuming builds finish ok)
The
tosa.reduce_*
ops take anaxis
Attribute that determines along which dimension the reduction takes place. A crash can occur during shape inference when the input tensor rank is so low that the given axis doesn't exist.Fix #68187