Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 16 additions & 3 deletions mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

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?

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

Expand Down