UniformSolversTest.cpp
4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//===- UniformSolversTest.cpp - Tests for uniform solvers -----------------===//
//
// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Quantizer/Support/UniformSolvers.h"
#include "gtest/gtest.h"
using namespace mlir;
using namespace mlir::quantizer;
namespace {
const double kEpsilon = 1e-12;
TEST(UniformMathTest, testAsym) {
UniformParamsFromMinMaxSolver s(UniformStorageParams::getQuint8(), -8, 8.123);
ASSERT_TRUE(s.compute());
llvm::errs() << "testBasic Results: " << s << "\n";
EXPECT_EQ(0.0, s.dequantize(s.getZp())); // Exact.
EXPECT_EQ(s.getZp(), s.quantize(0.0));
EXPECT_GE(s.getAdjMax() + kEpsilon, s.getBoundingMax());
EXPECT_LE(s.getAdjMin() - kEpsilon, s.getBoundingMin());
}
TEST(UniformMathTest, testPOT) {
UniformParamsFromMinMaxSolver s(UniformStorageParams::getQuint8(), -8,
7.9375);
ASSERT_TRUE(s.compute());
llvm::errs() << "testPOT Results: " << s << "\n";
// POT ranges should be exact.
EXPECT_EQ(128, s.getZp());
EXPECT_NEAR(6.25e-2, s.getScale(), kEpsilon);
EXPECT_EQ(0.0, s.dequantize(s.getZp())); // Exact.
EXPECT_EQ(s.getZp(), s.quantize(0.0));
EXPECT_GE(s.getAdjMax() + kEpsilon, s.getBoundingMax());
EXPECT_LE(s.getAdjMin() - kEpsilon, s.getBoundingMin());
}
TEST(UniformMathTest, testLopsidedPositive) {
UniformParamsFromMinMaxSolver s(UniformStorageParams::getQuint8(), 1.0, 8.0);
ASSERT_TRUE(s.compute());
llvm::errs() << "testLopsidedPositive Results: " << s << "\n";
EXPECT_EQ(0, s.getZp());
EXPECT_EQ(0.0, s.dequantize(s.getZp())); // Exact.
EXPECT_EQ(0, s.quantize(0.0));
EXPECT_GE(s.getAdjMax() + kEpsilon, s.getBoundingMax());
EXPECT_LE(s.getAdjMin() - kEpsilon, s.getBoundingMin());
}
TEST(UniformMathTest, testLopsidedNegative) {
UniformParamsFromMinMaxSolver s(UniformStorageParams::getQuint8(), -72.0,
-4.0);
ASSERT_TRUE(s.compute());
llvm::errs() << "testLopsidedNegative Results: " << s << "\n";
EXPECT_EQ(255, s.getZp());
EXPECT_EQ(0.0, s.dequantize(s.getZp())); // Exact.
EXPECT_EQ(255, s.quantize(0.0));
EXPECT_GE(s.getAdjMax() + kEpsilon, s.getBoundingMax());
EXPECT_LE(s.getAdjMin() - kEpsilon, s.getBoundingMin());
}
TEST(UniformMathTest, testLargeRange) {
UniformParamsFromMinMaxSolver s(UniformStorageParams::getQuint8(), -123.23389,
231.1289);
ASSERT_TRUE(s.compute());
llvm::errs() << "testLargeRange Results: " << s << "\n";
// EXPECT_EQ(255, s.getZp());
EXPECT_EQ(0.0, s.dequantize(s.getZp())); // Exact.
EXPECT_EQ(s.getZp(), s.quantize(0.0));
EXPECT_GE(s.getAdjMax() + kEpsilon, s.getBoundingMax());
EXPECT_LE(s.getAdjMin() - kEpsilon, s.getBoundingMin());
}
TEST(UniformMathTest, test16BitLargeRange) {
UniformParamsFromMinMaxSolver s(UniformStorageParams::getQuint16(),
-123.23389, 231.1289);
ASSERT_TRUE(s.compute());
llvm::errs() << "test16BitLargeRange Results: " << s << "\n";
EXPECT_EQ(0.0, s.dequantize(s.getZp())); // Exact.
EXPECT_EQ(s.getZp(), s.quantize(0.0));
EXPECT_GE(s.getAdjMax() + kEpsilon, s.getBoundingMax());
EXPECT_LE(s.getAdjMin() - kEpsilon, s.getBoundingMin());
}
TEST(UniformMathTest, testQuint8SymmetricRight) {
UniformParamsFromMinMaxSolver s(
UniformStorageParams::getQuint8SymmetricRight(), -123.23389, 231.1289);
ASSERT_TRUE(s.compute());
llvm::errs() << "testQuint8SymmetricRight Results: " << s << "\n";
EXPECT_EQ(0.0, s.dequantize(s.getZp())); // Exact.
EXPECT_EQ(s.getZp(), s.quantize(0.0));
EXPECT_GE(s.getAdjMax() + kEpsilon, s.getBoundingMax());
EXPECT_LE(s.getAdjMin() - kEpsilon, s.getBoundingMin());
}
TEST(UniformMathTest, testQuint4) {
UniformParamsFromMinMaxSolver s({15, 0}, -1.0, 1.0);
ASSERT_TRUE(s.compute());
llvm::errs() << "testQuint4 Results: " << s << "\n";
EXPECT_EQ(0.0, s.dequantize(s.getZp())); // Exact.
EXPECT_EQ(s.getZp(), s.quantize(0.0));
EXPECT_GE(s.getAdjMax() + kEpsilon, s.getBoundingMax());
EXPECT_LE(s.getAdjMin() - kEpsilon, s.getBoundingMin());
}
TEST(UniformMathTest, testNan) {
UniformParamsFromMinMaxSolver s({0, 0}, -1.0, 1.0);
ASSERT_FALSE(s.compute());
}
TEST(UniformMathTest, testBadBounds) {
UniformParamsFromMinMaxSolver s(UniformStorageParams::getQuint16(), 123.23389,
-231.1289);
ASSERT_FALSE(s.compute());
}
TEST(UniformMathTest, testZeroBounds) {
UniformParamsFromMinMaxSolver s(UniformStorageParams::getQuint16(), 0, 0);
ASSERT_FALSE(s.compute());
}
} // end namespace