forked from uTensor/uTensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.hpp
199 lines (158 loc) · 4.08 KB
/
test.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
#ifndef UTENSOR_TEST
#define UTENSOR_TEST
#include <math.h>
#include <limits>
#include <string>
#include <vector>
#include "mbed.h"
#include "uTensor_util.hpp"
class Test {
private:
Timer timer;
string testName;
string summary;
protected:
void printStatus(string status) {
int pLen = std::max(1, 30 - (int)testName.length());
string msg = testName;
for (int i = 0; i < pLen; i++) {
msg += " ";
}
msg += "[ " + status + " ]";
float lapsed_time = timer.read();
if (lapsed_time != 0) {
msg += " (" + std::to_string(lapsed_time * 1000) + " ms)";
}
summary += msg + "\r\n";
if (print_test) printf("%s\r\n", msg.c_str());
}
void timer_start() { timer.start(); }
void timer_stop() { timer.stop(); }
void testStart(string _testName) {
timer.reset();
testName = _testName;
numTotal++;
}
void passed(bool res = true) {
timer.stop();
if (!res) {
failed();
return;
}
if (testName == "") ERR_EXIT("Error: test name not cleared piror to test run\r\n");
numOk++;
printStatus(" OK ");
testName = "";
}
void failed() {
timer.stop();
if (testName == "") ERR_EXIT("Error: testStart is not called prior to test start\r\n");
numFailed++;
printStatus("** FAILED **");
testName = "";
}
void warn() {
timer.stop();
if (testName == "") ERR_EXIT("Error: testStart is not called prior to test start\r\n");
numWarn++;
printStatus(" * WARN * ");
testName = "";
}
public:
unsigned int numOk;
unsigned int numFailed;
unsigned int numWarn;
unsigned int numTotal;
bool print_test;
Test() {
numOk = 0;
numFailed = 0;
numWarn = 0;
testName = "";
summary = "";
print_test = false;
}
void printSummary(void) { printf("%s\r\n", summary.c_str()); }
virtual void runAll(void) = 0;
template <typename U>
double sum(Tensor<U> input) {
U* elem = input.getPointer({});
double accm = 0.0;
for (uint32_t i = 0; i < input.getSize(); i++) {
accm += (double)elem[i];
}
return accm;
}
template <typename T>
bool testshape(vector<T> src, vector<T> res, vector<uint8_t> permute) {
bool pass = true;
for (size_t i = 0; i < permute.size(); i++) {
if (src[permute[i]] != res[i]) {
pass = false;
return pass;
}
}
return pass;
}
template <typename T>
bool testval(T src, T res) {
bool pass = true;
if (src == res) {
return pass;
}
return false;
}
template <typename U>
static double meanAbsErr(Tensor<U> A, Tensor<U> B) {
if (A.getSize() != B.getSize()) {
ERR_EXIT("Test.meanAbsErr(): dimension mismatch\r\n");
}
U* elemA = A.getPointer({});
U* elemB = B.getPointer({});
double accm = 0.0;
for (uint32_t i = 0; i < A.getSize(); i++) {
accm += (double)fabs((float)elemB[i] - (float)elemA[i]);
}
return accm;
}
// A being the reference
template <typename U>
static double sumPercentErr(Tensor<U> A, Tensor<U> B) {
if (A.getSize() != B.getSize()) {
ERR_EXIT("Test.sumPercentErr(): dimension mismatch\r\n");
}
U* elemA = A.getPointer({});
U* elemB = B.getPointer({});
double accm = 0.0;
for (uint32_t i = 0; i < A.getSize(); i++) {
if (elemA[i] != 0.0f) {
accm += (double)fabs(((float)elemB[i] - (float)elemA[i]) /
fabs((float)elemA[i]));
} else {
if (elemB[i] != 0) {
accm += std::numeric_limits<float>::quiet_NaN();
}
}
}
return accm;
}
template <typename U>
static double meanPercentErr(Tensor<U> A, Tensor<U> B) {
double sum = sumPercentErr(A, B);
return sum / A.getSize();
}
};
// https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format
void printBits(size_t const size, void const* const ptr) {
unsigned char* b = (unsigned char*)ptr;
unsigned char byte;
int i, j;
for (i = size - 1; i >= 0; i--) {
for (j = 7; j >= 0; j--) {
byte = (b[i] >> j) & 1;
printf("%d", byte);
}
}
puts("");
}
#endif