Skip to content

Commit a527d3a

Browse files
committed
[mlir][tosa] Add check for invalid tosa.rescale parameters
* mlir::tosa::computeMultiplierAndShift returns a boolean, depending on validity of the shift value
1 parent 3b6462c commit a527d3a

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace tosa {
2828

2929
/// From a scale value, computes multiplier and shift values
3030
/// for 16 or 32-bit scale widths.
31-
void computeMultiplierAndShift(double scale, int32_t &multiplier,
31+
bool computeMultiplierAndShift(double scale, int32_t &multiplier,
3232
int32_t &shift, int32_t scaleWidth);
3333

3434
//// Builds ConvOpQuantizationAttr from input and weight.

mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,31 @@ static void computeMultiplierAndShiftTosaScale32(double scale,
9292
}
9393

9494
/// Generates a quantized multiplier/shift from double.
95-
void mlir::tosa::computeMultiplierAndShift(double scale, int32_t &multiplier,
95+
bool mlir::tosa::computeMultiplierAndShift(double scale, int32_t &multiplier,
9696
int32_t &shift, int32_t scaleWidth) {
9797

9898
switch (scaleWidth) {
9999
case 16:
100100
computeMultiplierAndShiftTosaScale16(scale, multiplier, shift);
101-
return;
101+
102+
// In some cases computeMultiplierAndShiftTosaScale16 can return
103+
// a value less then 2, which is not valid in the TOSA spec.
104+
if (shift < 2)
105+
return false;
106+
else
107+
return true;
102108
case 32:
103109
computeMultiplierAndShiftTosaScale32(scale, multiplier, shift);
104-
return;
110+
111+
// In some cases computeMultiplierAndShiftTosaScale32 can return
112+
// a value less then 2, which is not valid in the TOSA spec.
113+
if (shift < 2)
114+
return false;
115+
else
116+
return true;
105117
default:
106118
assert(0 && "Unsupported Tosa quantized_scale regime specified!");
119+
return false;
107120
}
108121
}
109122

0 commit comments

Comments
 (0)