[Mlir-commits] [mlir] [mlir][tosa] Add check for invalid tosa.rescale parameters (PR #96480)

Tom Allsop llvmlistbot at llvm.org
Mon Jun 24 05:37:24 PDT 2024


https://github.com/tom-arm created https://github.com/llvm/llvm-project/pull/96480

* mlir::tosa::computeMultiplierAndShift returns a boolean, depending on validity of the shift value

>From a527d3a4afec3868548b5fa5533c77c442007db1 Mon Sep 17 00:00:00 2001
From: Tom Allsop <tom.allsop at arm.com>
Date: Wed, 20 Dec 2023 13:27:41 +0000
Subject: [PATCH] [mlir][tosa] Add check for invalid tosa.rescale parameters

* mlir::tosa::computeMultiplierAndShift returns a boolean, depending on validity of the shift value
---
 .../mlir/Dialect/Tosa/Utils/QuantUtils.h      |  2 +-
 mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp    | 19 ++++++++++++++++---
 2 files changed, 17 insertions(+), 4 deletions(-)

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;
   }
 }
 



More information about the Mlir-commits mailing list