-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
235 lines (195 loc) · 6.08 KB
/
Copy pathtest.cpp
File metadata and controls
235 lines (195 loc) · 6.08 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
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
#include "tensor.h"
#include <iostream>
using namespace std;
void testConstructor();
void testOperation();
void testMath();
void testDot();
void printTensor(const ts::Tensor &t);
int main()
{
// ts::Tensor t = ts::Tensor({{0.1, 1.2}, {2.2, 3.1}, {4.9, 5.2}});
// cout << t << endl;
// ts::Tensor t1 = t(1);
// cout << "Indexed Tensor (2nd element):\n" << t1 << endl;
// ts::Tensor t2 = t(2, {2, 1});
// cout << "Sliced Tensor (3rd to 4th elements in the 3rd dimension):\n" << t2 << endl;
// testConstructor();
testOperation();
// testMath();
//testDot();
return 0;
}
void testConstructor()
{
// Test 1: Using the data constructor
cout << "Test 1: Data Constructor" << endl;
vector<vector<double>> data = {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}};
ts::Tensor tensor1(data);
cout << "Tensor 1 size: ";
for (auto s : tensor1.size())
cout << s << " ";
cout << "\nTensor 1 data type: " << tensor1.type() << endl;
cout << "Tensor 1 data: ";
for (size_t i = 0; i < tensor1.size()[0]; ++i)
{
for (size_t j = 0; j < tensor1.size()[1]; ++j)
{
cout << tensor1.data_ptr()[i * tensor1.size()[1] + j] << " ";
}
}
cout << "\n\n";
// Test 2: Using the shape-type constructor
cout << "Test 2: Shape-Type Constructor" << endl;
vector<size_t> shape = {2, 3};
string dtype = "double";
double init_value = 0.5;
ts::Tensor tensor2(shape, dtype, init_value);
cout << "Tensor 2 size: ";
for (auto s : tensor2.size())
cout << s << " ";
cout << "\nTensor 2 data type: " << tensor2.type() << endl;
cout << "Tensor 2 data: ";
for (size_t i = 0; i < tensor2.size()[0]; ++i)
{
for (size_t j = 0; j < tensor2.size()[1]; ++j)
{
cout << tensor2.data_ptr()[i * tensor2.size()[1] + j] << " ";
}
}
cout << "\n\n";
// Test 3: Using the third constructor with data_vector
cout << "Test: Third Constructor" << endl;
vector<double> data_vector = {1.2, 3.4, 5.6, 7.8, 9.0, 10.1};
try
{
ts::Tensor tensor(shape, dtype, data_vector);
cout << "Tensor size: ";
for (auto s : tensor.size())
cout << s << " ";
cout << "\nTensor data type: " << tensor.type() << endl;
cout << "Tensor data: ";
for (size_t i = 0; i < tensor.size()[0]; ++i)
{
for (size_t j = 0; j < tensor.size()[1]; ++j)
{
cout << tensor.data_ptr()[i * tensor.size()[1] + j] << " ";
}
}
cout << endl;
}
catch (const std::exception &e)
{
cout << "Exception occurred: " << e.what() << endl;
}
}
void testOperation()
{
// test 3:using slicing
cout << "Test 3: Slicing" << endl;
vector<size_t> shape = {2, 3, 4};
string dtype = "double";
std::vector<double> a(24);
for (int i = 0; i < 24; ++i)
{
a[i] = i + 1;
}
vector<size_t> shape1 = {2, 4, 4};
std::vector<double> b(32);
for (int i = 0; i < 32; ++i)
{
b[i] = i + 1;
}
ts::Tensor t(shape, dtype, a);
ts::Tensor t1(shape1, dtype, b);
ts::Tensor t_index = t(1, 2);
ts::Tensor t_slicing = t(vector{0, 1}, 0, vector{1, 3});
ts::Tensor t_cat = cat(vector{t, t1}, 1);
ts::Tensor t_tile = tile(t, vector{1, 2, 3});
t(0) = 1;
t(0, vector{0, 2}, 0) = vector{3.0, 2.0};
ts::Tensor t_tra1 = transpose(t, 0, 2);
ts::Tensor t_tra2 = t.transpose(0, 2);
ts::Tensor t_per1 = permute(t, {2, 0, 1});
ts::Tensor t_per2 = t.permute({2, 0, 1});
ts::Tensor t_view1 = view(t, {4, 3, 2});
ts::Tensor t_view2 = t.view({3, 4, 2});
cout << "Tensor t size: ";
for (auto s : t.size())
{
std::cout << s << " ";
}
cout << "Tensor data: \n";
for (size_t i = 0; i < t.size()[0]; ++i)
{
for (size_t j = 0; j < t.size()[1]; ++j)
{
for (int k = 0; k < t.size()[2]; ++k)
{
cout << t.data_ptr()[i * t.size()[1] * t.size()[2] + j * t.size()[2] + k] << " ";
}
cout << endl;
}
}
cout << endl;
}
void testMath()
{
// 创建测试用的 Tensor 对象
ts::Tensor t1({2, 3}, "double", 1.0); // 创建一个 2x3 的 Tensor,初始化为 1.0
ts::Tensor t2({2, 3}, "double", 2.0); // 创建另一个 2x3 的 Tensor,初始化为 2.0
// 使用非成员函数进行加法
ts::Tensor t3 = ts::add(t1, t2);
std::cout << "Tensor t1 + t2 is: ";
printTensor(t3);
// 使用成员函数和标量进行加法
ts::Tensor t4 = t1.add(5.0);
std::cout << "Tensor t1 + 5.0 is: ";
printTensor(t4);
// 使用运算符重载进行加法
ts::Tensor t5 = t1 + t2;
std::cout << "Tensor t1 + t2 (operator+) is: ";
printTensor(t5);
// t6 t7 t8为减法测试
ts::Tensor t6 = ts::sub(t1, t2);
std::cout << "Tensor t1 - t2 is: ";
printTensor(t6);
ts::Tensor t7 = t1.sub(5.0);
std::cout << "Tensor t1 - 5.0 is: ";
printTensor(t7);
ts::Tensor t8 = t1 - t2;
std::cout << "Tensor t1 - t2 (operator+) is: ";
printTensor(t8);
}
void printTensor(const ts::Tensor &t)
{
size_t total_size = 1;
for (auto dim : t.size())
{
total_size *= dim;
}
double *data = t.data_ptr();
for (size_t i = 0; i < total_size; ++i)
{
std::cout << data[i] << " ";
}
std::cout << std::endl;
}
void testDot()
{
vector<vector<double>> data1 = {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}};
vector<vector<double>> data2 = {{4.0, 5.0}, {6.0, 7.0}, {4.0, 5.0}};
ts::Tensor tensor1(data1);
ts::Tensor tensor2(data2);
cout<<"Dot product of tensors"<<endl;
ts::Tensor tensor3 = dot(tensor1,tensor2);
printTensor(tensor3);
std::cout<<"Dot product of tensors in high dimensions"<<std::endl;
std::vector<double> dataA(2 * 3 * 4, 1.0); // 填充1.0
std::vector<double> dataB(4 * 5 * 3, 2.0); // 填充2.0
ts::Tensor A({2, 3, 4}, "double", dataA);
ts::Tensor B({4, 5, 3}, "double", dataB);
// 执行点积操作
ts::Tensor C = ts::dot(A, B);
printTensor(C);
}