forked from uTensor/uTensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathTests.hpp
299 lines (241 loc) · 9.59 KB
/
MathTests.hpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#ifndef UTENSOR_MATH_TESTS
#define UTENSOR_MATH_TESTS
#include "MathOps.hpp"
#include "tensorIdxImporter.hpp"
#include "test.hpp"
class MathOpsTest : public Test {
public:
void requantization_rangeTest(void) {
testStart("requantization_range");
TensorIdxImporter t_import;
// reference inputs
Tensor<int> a =
t_import.int_import("/fs/testData/rqRange/in/qMatMul_0.idx");
Tensor<float> a_min =
t_import.float_import("/fs/testData/rqRange/in/qMatMul_1.idx");
Tensor<float> a_max =
t_import.float_import("/fs/testData/rqRange/in/qMatMul_2.idx");
// reference outputs
Tensor<float> ref_min =
t_import.float_import("/fs/testData/rqRange/out/rqRange_0.idx");
Tensor<float> ref_max =
t_import.float_import("/fs/testData/rqRange/out/rqRange_1.idx");
// Implementation goes here
// modify the checks below:
Tensor<float> out_min(ref_min.getShape());
Tensor<float> out_max(ref_max.getShape());
timer_start();
Requantization_Range<int, float>(a, a_min, a_max, out_min, out_max);
timer_stop();
double result =
meanPercentErr(ref_min, out_min) + meanPercentErr(ref_max, out_max);
// passed(result < 0.0001);
passed(result == 0);
}
void requantizeTest(void) {
testStart("requantize");
TensorIdxImporter t_import;
// reference inputs
Tensor<int> a = t_import.int_import("/fs/testData/rQ/in/qMatMul_0.idx");
Tensor<float> a_min =
t_import.float_import("/fs/testData/rQ/in/qMatMul_1.idx");
Tensor<float> a_max =
t_import.float_import("/fs/testData/rQ/in/qMatMul_2.idx");
Tensor<float> r_a_min =
t_import.float_import("/fs/testData/rQ/in/rqRange_0.idx");
Tensor<float> r_a_max =
t_import.float_import("/fs/testData/rQ/in/rqRange_1.idx");
// tf.quint8
// reference outputs
Tensor<unsigned char> ref_a_q =
t_import.ubyte_import("/fs/testData/rQ/out/rQ_0.idx");
Tensor<float> ref_a_min =
t_import.float_import("/fs/testData/rQ/out/rQ_1.idx");
Tensor<float> ref_a_max =
t_import.float_import("/fs/testData/rQ/out/rQ_2.idx");
// modify the checks below:
Tensor<unsigned char> a_q(ref_a_q.getShape());
Tensor<float> a_min_q(ref_a_min.getShape());
Tensor<float> a_max_q(ref_a_max.getShape());
// Implementation goes here
timer_start();
Requantize<int, float, unsigned char>(a, a_min, a_max, r_a_min, r_a_max,
a_q, a_min_q, a_max_q);
timer_stop();
double result = meanPercentErr(ref_a_q, a_q) +
meanPercentErr(ref_a_min, a_min_q) +
meanPercentErr(ref_a_max, a_max_q);
// passed(result < 0.0001);
passed(result == 0);
}
void requantizeTest2(void) {
testStart("requantize2");
TensorIdxImporter t_import;
// reference inputs
Tensor<int> a = t_import.int_import("/fs/testData/import-MatMul_eightbit_requantize/in/import-MatMul_eightbit_quantized_mat_mul_0.idx");
Tensor<float> a_min =
t_import.float_import("/fs/testData/import-MatMul_eightbit_requantize/in/import-MatMul_eightbit_quantized_mat_mul_1.idx");
Tensor<float> a_max =
t_import.float_import("/fs/testData/import-MatMul_eightbit_requantize/in/import-MatMul_eightbit_quantized_mat_mul_2.idx");
Tensor<float> r_a_min =
t_import.float_import("/fs/testData/import-MatMul_eightbit_requantize/in/import-MatMul_eightbit_requant_range_0.idx");
Tensor<float> r_a_max =
t_import.float_import("/fs/testData/import-MatMul_eightbit_requantize/in/import-MatMul_eightbit_requant_range_1.idx");
// tf.quint8
// reference outputs
Tensor<unsigned char> ref_a_q =
t_import.ubyte_import("/fs/testData/import-MatMul_eightbit_requantize/out/import-MatMul_eightbit_requantize_0.idx");
Tensor<float> ref_a_min =
t_import.float_import("/fs/testData/import-MatMul_eightbit_requantize/out/import-MatMul_eightbit_requantize_1.idx");
Tensor<float> ref_a_max =
t_import.float_import("/fs/testData/import-MatMul_eightbit_requantize/out/import-MatMul_eightbit_requantize_2.idx");
// modify the checks below:
Tensor<unsigned char> a_q(ref_a_q.getShape());
Tensor<float> a_min_q(ref_a_min.getShape());
Tensor<float> a_max_q(ref_a_max.getShape());
// Implementation goes here
timer_start();
Requantize<int, float, unsigned char>(a, a_min, a_max, r_a_min, r_a_max,
a_q, a_min_q, a_max_q);
timer_stop();
double result;
if((result = meanPercentErr(ref_a_q, a_q)) != 0) {
printf("Requantize a_q failed (%.6f)\r\n", result);
unsigned char* ref_ptr = ref_a_q.getPointer({});
unsigned char* test_ptr = a_q.getPointer({});
for(uint32_t i = 0; i < ref_a_q.getSize(); i++) {
if(ref_ptr[i] != test_ptr[i]) {
printf("%lu: %d != %d\r\n", i, ref_ptr[i], test_ptr[i]);
} else {
printf("%lu: %d == %d\r\n", i, ref_ptr[i], test_ptr[i]);
}
}
}
if((result = meanPercentErr(ref_a_min, a_min_q)) != 0) printf("Requantize a_min_q failed (%.6f)\r\n", result);
if((result = meanPercentErr(ref_a_max, a_max_q)) != 0) printf("Requantize a_max_q failed (%.6f)\r\n", result);
result = meanPercentErr(ref_a_q, a_q) +
meanPercentErr(ref_a_min, a_min_q) +
meanPercentErr(ref_a_max, a_max_q);
// passed(result < 0.0001);
passed(result == 0);
}
void argmaxTest(void) { // NT: WIP do not use t_import int 64 here
testStart("argmax");
TensorIdxImporter t_import;
// reference inputs
Tensor<float> ref_a = t_import.float_import("/fs/testData/ArgMax/in/ArgMax-input_0.idx");
Tensor<int> ref_dim = t_import.int_import("/fs/testData/ArgMax/in/ArgMax-dimension_0.idx");
// reference outputs
/// NT: FIXME: argmax outputs int64 tensor which isn't supported by
/// int_import.
Tensor<float> ref_out = t_import.float_import("/fs/testData/ArgMax/out/ArgMax_0.idx");
// Implementation goes here
// modify the checks below:
Tensor<int> out(ref_out.getShape());
timer_start();
ArgMax(ref_a, ref_dim, out);
timer_stop();
Tensor<float> out_float = TensorCast<int, float>(out);
double result = meanPercentErr(ref_out, out_float);
// passed(result < 0.0001);
passed(result == 0);
}
void argmaxTest2(void) { // NT: WIP do not use t_import int 64 here
testStart("argmax2");
Tensor<float> test_input = TensorConstant<float>({10, 5}, 0.0f);
*(test_input.getPointer({5,0})) = 1.0f;
*(test_input.getPointer({5,1})) = 1.0f;
*(test_input.getPointer({1,2})) = 1.0f;
*(test_input.getPointer({9,3})) = 1.0f;
*(test_input.getPointer({2,4})) = 1.0f;
Tensor<int> test_dim({1});
*(test_dim.getPointer({0})) = 0;
Tensor<float> test_out_ref({5});
*(test_out_ref.getPointer({0})) = 5.0f;
*(test_out_ref.getPointer({1})) = 5.0f;
*(test_out_ref.getPointer({2})) = 1.0f;
*(test_out_ref.getPointer({3})) = 9.0f;
*(test_out_ref.getPointer({4})) = 2.0f;
Tensor<float> test_out(test_out_ref.getShape());
timer_start();
ArgMax(test_input, test_dim, test_out);
timer_stop();
double result = meanPercentErr(test_out_ref, test_out);
// passed(result < 0.0001);
passed(result == 0);
}
void addTest(void) {
testStart("add");
TensorIdxImporter t_import;
// reference inputs
Tensor<float> a =
t_import.float_import("/fs/testData/ref_add/in/Const_5_0.idx");
Tensor<float> b =
t_import.float_import("/fs/testData/ref_add/in/Const_6_0.idx");
// reference outputs
Tensor<float> ref_out =
t_import.float_import("/fs/testData/ref_add/out/ref_add_0.idx");
// Implementation goes here
// modify the checks below:
Tensor<float> out(ref_out.getShape());
timer_start();
Add<float, float>(a, b, out);
timer_stop();
double result = meanPercentErr(ref_out, out);
// passed(result < 0.0001);
passed(result == 0);
}
void minTest(void) {
testStart("min");
TensorIdxImporter t_import;
// reference inputs
Tensor<float> a =
t_import.float_import("/fs/testData/ref_min/in/Const_2_0.idx");
Tensor<int> dim =
t_import.int_import("/fs/testData/ref_min/in/Const_3_0.idx");
// reference outputs
Tensor<float> ref_out =
t_import.float_import("/fs/testData/ref_min/out/ref_min_0.idx");
// Implementation goes here
// modify the checks below:
Tensor<float> out(ref_out.getShape());
timer_start();
Min<float, int, float>(a, dim, out);
timer_stop();
double result = meanPercentErr(ref_out, out);
// passed(result < 0.0001);
passed(result == 0);
}
void maxTest(void) {
testStart("max");
TensorIdxImporter t_import;
// reference inputs
Tensor<float> a =
t_import.float_import("/fs/testData/ref_max/in/Const_2_0.idx");
Tensor<int> dim =
t_import.int_import("/fs/testData/ref_max/in/Const_4_0.idx");
// reference outputs
Tensor<float> ref_out =
t_import.float_import("/fs/testData/ref_max/out/ref_max_0.idx");
// Implementation goes here
// modify the checks below:
Tensor<float> out(ref_out.getShape());
timer_start();
Max<float, int, float>(a, dim, out);
timer_stop();
double result = meanPercentErr(ref_out, out);
// passed(result < 0.0001);
passed(result == 0);
}
void runAll(void) {
argmaxTest();
argmaxTest2();
requantization_rangeTest();
requantizeTest();
requantizeTest2();
addTest();
minTest();
maxTest();
}
};
#endif // UTENSOR_MATH_TESTS