forked from uTensor/uTensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tensor_test.hpp
108 lines (93 loc) · 3.18 KB
/
tensor_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
#ifndef UTENSOR_impl_TESTS
#define UTENSOR_impl_TESTS
#include <algorithm>
#include <random>
#include "NnOps.hpp"
#include "tensorIdxImporter.hpp"
#include "test.hpp"
class transTest : public Test {
public:
void runShapeTest() {
bool res = false;
for (int i = 0; i < 10; i++) {
testStart("transtest");
std::default_random_engine gen;
vector<uint32_t> tmp({2, 3, 4, 5});
Tensor<int> inputTensor(tmp);
vector<uint8_t> permute = {2, 3, 1, 0};
vector<uint32_t> g = inputTensor.getShape();
std::shuffle(permute.begin(), permute.end(), gen);
permuteIndexTransform trans(inputTensor.getShape(), permute);
Tensor<int> output(trans.getNewShape());
vector<uint32_t> s = output.getShape();
res = testshape<uint32_t>(g, s, permute);
if (!res) {
passed(res);
}
}
passed(res);
}
void runPermuteTest() {
// source numpy array dim (2, 3, 4)
vector<int> input_1({2, 5, 4, 5, 2, 6, 5, 1, 3, 6, 7, 9,
1, 2, 3, 4, 3, 5, 6, 9, 2, 3, 3, 2});
// target numpy array (after np.transpose(0, 2, 1))
vector<int> output_1({2, 2, 3, 5, 6, 6, 4, 5, 7, 5, 1, 9,
1, 3, 2, 2, 5, 3, 3, 6, 3, 4, 9, 2});
Tensor<int> inputTensor({2, 3, 4});
vector<uint8_t> permute = {0, 2, 1};
permuteIndexTransform trans(inputTensor.getShape(), permute);
size_t out_index = 0;
bool res = false;
for (uint32_t i = 0; i < input_1.size(); i++) {
testStart("test vec 1 for transform");
out_index = trans[i];
res = testval(input_1[i], output_1[out_index]);
if (!res) {
passed(res);
}
}
passed(res);
res = false;
// source numpy array dim (2, 4, 3)
vector<int> input_2({2, 2, 3, 5, 6, 6, 4, 5, 7, 5, 1, 9,
1, 3, 2, 2, 5, 3, 3, 6, 3, 4, 9, 2});
// target numpy arrar (after transpose(1, 2, 0))
vector<int> output_2({2, 1, 2, 3, 3, 2, 5, 2, 6, 5, 6, 3,
4, 3, 5, 6, 7, 3, 5, 4, 1, 9, 9, 2});
Tensor<int> inputTensor2({2, 4, 3});
vector<uint8_t> permute2 = {1, 2, 0};
permuteIndexTransform trans2(inputTensor2.getShape(), permute2);
for (uint32_t i = 0; i < input_2.size(); i++) {
testStart("test vec 2 for transform");
out_index = trans2[i];
res = testval(input_2[i], output_2[out_index]);
if (!res) {
passed(res);
}
}
passed(res);
res = false;
vector<int> input_3({8, 6, 0, 1, 3, 9, 4, 7, 3, 2, 0, 4, 0, 9,
0, 6, 0, 6, 8, 6, 8, 3, 2, 4, 2, 7, 8});
vector<int> output_3({8, 2, 8, 1, 0, 3, 4, 6, 2, 6, 0, 6, 3, 9,
2, 7, 0, 7, 0, 4, 8, 9, 0, 4, 3, 6, 8});
Tensor<int> inputTensor3({1, 3, 3, 3});
vector<uint8_t> permute3 = {0, 3, 2, 1};
permuteIndexTransform trans3(inputTensor3.getShape(), permute3);
for (uint32_t i = 0; i < input_3.size(); i++) {
testStart("test vec 4d for transform");
out_index = trans3[i];
res = testval(input_3[i], output_3[out_index]);
if (!res) {
passed(res);
}
}
passed(res);
}
void runAll() {
runPermuteTest();
runShapeTest();
}
};
#endif // UTENSOR_impl_TESTS