[Mlir-commits] [mlir] e04f4cc - [mlir][tosa] Add check for invalid tosa.rescale parameters (#129606)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Mar 5 08:33:43 PST 2025
Author: Jerry-Ge
Date: 2025-03-05T08:33:40-08:00
New Revision: e04f4cce85e08760d513abca4276542aa1363593
URL: https://github.com/llvm/llvm-project/commit/e04f4cce85e08760d513abca4276542aa1363593
DIFF: https://github.com/llvm/llvm-project/commit/e04f4cce85e08760d513abca4276542aa1363593.diff
LOG: [mlir][tosa] Add check for invalid tosa.rescale parameters (#129606)
* mlir::tosa::computeMultiplierAndShift returns a boolean, depending on
validity of the shift value
Signed-off-by: Jerry Ge <jerry.ge at arm.com>
Co-authored-by: Tom Allsop <tom.allsop at arm.com>
Added:
Modified:
mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h
mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp
mlir/test/lib/Dialect/Tosa/TosaTestPasses.cpp
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h b/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h
index 10dc5dd36cfa9..cb0df7aff35f7 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 0f7562767001c..02c86a090e6d4 100644
--- a/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp
+++ b/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp
@@ -92,18 +92,25 @@ 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.
+ return (!(shift < 2));
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.
+ return (!(shift < 2));
default:
assert(0 && "Unsupported Tosa quantized_scale regime specified!");
+ return false;
}
}
diff --git a/mlir/test/lib/Dialect/Tosa/TosaTestPasses.cpp b/mlir/test/lib/Dialect/Tosa/TosaTestPasses.cpp
index a97711b51ff7e..1297596ef193a 100644
--- a/mlir/test/lib/Dialect/Tosa/TosaTestPasses.cpp
+++ b/mlir/test/lib/Dialect/Tosa/TosaTestPasses.cpp
@@ -163,7 +163,8 @@ ConvertTosaConv2DOp::matchAndRewrite(Operation *op,
int32_t shift;
// Obtain the quantized scale = multiplier and shift.
- computeMultiplierAndShift(opTensorScale, multiplier, shift, 32);
+ if (!computeMultiplierAndShift(opTensorScale, multiplier, shift, 32))
+ return failure();
bool input_unsigned =
newTosaConv2DOp.getResult().getType().isUnsignedInteger();
More information about the Mlir-commits
mailing list