Skip to content

[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

Merged
merged 5 commits into from
Oct 24, 2023

Conversation

ubfx
Copy link
Member

@ubfx ubfx commented Oct 21, 2023

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 #68187

ubfx added 2 commits October 21, 2023 13:06
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
@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2023

@llvm/pr-subscribers-mlir-tosa

@llvm/pr-subscribers-mlir

Author: Felix Schneider (ubfx)

Changes

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 #68187


Full diff: https://github.com/llvm/llvm-project/pull/69843.diff

1 Files Affected:

  • (modified) mlir/lib/Dialect/Tosa/IR/TosaOps.cpp (+2-2)
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();

Copy link
Collaborator

@joker-eph joker-eph left a 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?

@ubfx
Copy link
Member Author

ubfx commented Oct 22, 2023

When writing the test, I noticed that the ReduceOp folder has a similar issue where it crashes when the axis argument of the Op is too large for the shape.
What is the desired behaviour for a tosa.reduce_* Op when the axis is out of range? Is it supposed to be a No-Op, i.e. folded away or should we emit an error?

@GeorgeARM
Copy link
Contributor

When writing the test, I noticed that the ReduceOp folder has a similar issue where it crashes when the axis argument of the Op is too large for the shape. What is the desired behaviour for a tosa.reduce_* Op when the axis is out of range? Is it supposed to be a No-Op, i.e. folded away or should we emit an error?

Behavior is debatable but my assumption would be that an error should be triggered if the axis parameter is >= than the input rank. Find it cleaner with well defined preconditions.

@ubfx
Copy link
Member Author

ubfx commented Oct 23, 2023

my assumption would be that an error should be triggered if the axis parameter is >= than the input rank. Find it cleaner with well defined preconditions.

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.

@eric-k256
Copy link
Contributor

The spec calls for an error if axis > the rank of the input tensor: https://www.mlplatform.org/tosa/tosa_spec.html#_reduce_all

ERROR_IF(axis < 0 || axis >= rank(shape1));

Adding it as a verifier for the op seems like a good option.

@ubfx ubfx force-pushed the fix-tosa-infer-shape-crash branch from ef5bf7f to c68f170 Compare October 23, 2023 20:34
@github-actions
Copy link

github-actions bot commented Oct 23, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@eric-k256 eric-k256 left a 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)

@ubfx ubfx changed the title [mlir][tosa] Fix crash in inferReturnTypes for ReduceOps [mlir][tosa] Add verifiers to ReduceOps, fix crash Oct 24, 2023
@ubfx ubfx changed the title [mlir][tosa] Add verifiers to ReduceOps, fix crash [mlir][tosa] Add verifiers to ReduceOps, fix shape inference crash Oct 24, 2023
@ubfx ubfx merged commit 8a57bc0 into llvm:main Oct 24, 2023
@ubfx ubfx deleted the fix-tosa-infer-shape-crash branch October 24, 2023 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[mlir][tosa] --tosa-infer-shapes crashed with assertion failure `idx < size()'
5 participants