-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
[mlir][tosa] Add check for invalid tosa.rescale parameters #96480
base: main
Are you sure you want to change the base?
Conversation
tom-arm
commented
Jun 24, 2024
- mlir::tosa::computeMultiplierAndShift returns a boolean, depending on validity of the shift value
* mlir::tosa::computeMultiplierAndShift returns a boolean, depending on validity of the shift value
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-tosa Author: Tom Allsop (tom-arm) Changes
Full diff: https://github.com/llvm/llvm-project/pull/96480.diff 2 Files Affected:
diff --git a/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h b/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h
index 298c97015fe2e..7fba62e9d002b 100644
--- a/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h
+++ b/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h
@@ -28,7 +28,7 @@ namespace tosa {
/// From a scale value, computes multiplier and shift values
/// for 16 or 32-bit scale widths.
-void computeMultiplierAndShift(double scale, int32_t &multiplier,
+bool computeMultiplierAndShift(double scale, int32_t &multiplier,
int32_t &shift, int32_t scaleWidth);
//// Builds ConvOpQuantizationAttr from input and weight.
diff --git a/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp b/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp
index 5c546f59cde41..c318fe3240520 100644
--- a/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp
+++ b/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp
@@ -92,18 +92,31 @@ static void computeMultiplierAndShiftTosaScale32(double scale,
}
/// Generates a quantized multiplier/shift from double.
-void mlir::tosa::computeMultiplierAndShift(double scale, int32_t &multiplier,
+bool mlir::tosa::computeMultiplierAndShift(double scale, int32_t &multiplier,
int32_t &shift, int32_t scaleWidth) {
switch (scaleWidth) {
case 16:
computeMultiplierAndShiftTosaScale16(scale, multiplier, shift);
- return;
+
+ // In some cases computeMultiplierAndShiftTosaScale16 can return
+ // a value less then 2, which is not valid in the TOSA spec.
+ if (shift < 2)
+ return false;
+ else
+ return true;
case 32:
computeMultiplierAndShiftTosaScale32(scale, multiplier, shift);
- return;
+
+ // In some cases computeMultiplierAndShiftTosaScale32 can return
+ // a value less then 2, which is not valid in the TOSA spec.
+ if (shift < 2)
+ return false;
+ else
+ return true;
default:
assert(0 && "Unsupported Tosa quantized_scale regime specified!");
+ return false;
}
}
|
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.
Minor comment
|
||
// In some cases computeMultiplierAndShiftTosaScale16 can return | ||
// a value less then 2, which is not valid in the TOSA spec. | ||
if (shift < 2) |
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.
Similar code in the two cases. Maybe worth pulling it into a lambda? e.g. isShiftWithinRange
and check that is between 2 and 62?