Fix division by zero when float min == max.
PiperOrigin-RevId: 230583756
This commit is contained in:
parent
dbf08f7e20
commit
5215b322c1
tensorflow/lite/tools/optimize
@ -55,7 +55,11 @@ void GetAsymmetricQuantizationParams(
|
||||
min = std::min(static_cast<float>(min), 0.0f);
|
||||
max = std::max(static_cast<float>(max), 0.0f);
|
||||
const float scale = (max - min) / (quant_max_float - quant_min_float);
|
||||
const float zero_point_from_min = quant_min_float - min / scale;
|
||||
// Scale can be zero if min and max are exactly 0.0f.
|
||||
float zero_point_from_min = quant_min_float;
|
||||
if (scale != 0) {
|
||||
zero_point_from_min = quant_min_float - min / scale;
|
||||
}
|
||||
int64_t zero_point;
|
||||
if (zero_point_from_min < quant_min_float) {
|
||||
zero_point = static_cast<int64_t>(quant_min);
|
||||
|
@ -126,6 +126,28 @@ TEST(QuantizationUtilsTest, AsymmetricQuantizationParamsWithZeroInRange) {
|
||||
EXPECT_LT(zero_point, quant_max);
|
||||
}
|
||||
|
||||
TEST(QuantizationUtilsTest, AsymmetricQuantizationParamsWithZeroMinMax) {
|
||||
const float float_min = 0;
|
||||
const float float_max = 0;
|
||||
const int quant_min = -128;
|
||||
const int quant_max = 127;
|
||||
QuantizationParametersT params;
|
||||
GetAsymmetricQuantizationParams(float_min, float_max, quant_min, quant_max,
|
||||
¶ms);
|
||||
ASSERT_EQ(params.max.size(), 1);
|
||||
ASSERT_EQ(params.min.size(), 1);
|
||||
ASSERT_EQ(params.scale.size(), 1);
|
||||
ASSERT_EQ(params.zero_point.size(), 1);
|
||||
EXPECT_EQ(params.max[0], float_max);
|
||||
EXPECT_EQ(params.min[0], float_min);
|
||||
int64_t zero_point = params.zero_point[0];
|
||||
float scale = params.scale[0];
|
||||
const float eps = 1e-7f;
|
||||
EXPECT_NEAR(scale, 0, eps);
|
||||
EXPECT_NEAR(zero_point, quant_min, eps);
|
||||
EXPECT_LT(zero_point, quant_max);
|
||||
}
|
||||
|
||||
TEST(QuantizationUtilsTest, SymmetricPerChannelQuantization) {
|
||||
// Set up an input with [3, 2, 2, 2] size and 0 is the channel index.
|
||||
const std::vector<float> input = {
|
||||
|
Loading…
Reference in New Issue
Block a user