-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_operation.cpp
More file actions
379 lines (333 loc) · 11.6 KB
/
Copy pathmath_operation.cpp
File metadata and controls
379 lines (333 loc) · 11.6 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "tensor.h"
#include <algorithm>
namespace ts
{
std::vector<size_t> calculateBroadcastShape(const std::vector<size_t> &shapeA, const std::vector<size_t> &shapeB)
{
std::vector<size_t> resultShape;
// Start from the last dimension and move backwards
auto itA = shapeA.rbegin();
auto itB = shapeB.rbegin();
while (itA != shapeA.rend() || itB != shapeB.rend())
{
// If one tensor is shorter, prepend 1 to its shape
size_t dimA = itA != shapeA.rend() ? *itA : 1;
size_t dimB = itB != shapeB.rend() ? *itB : 1;
// Check for broadcast compatibility
if (dimA != dimB && dimA != 1 && dimB != 1)
{
throw std::invalid_argument("Shapes are not compatible for broadcasting");
}
// Append the maximum dimension to the result shape
resultShape.push_back(std::max(dimA, dimB));
// Move to the next dimension
if (itA != shapeA.rend())
++itA;
if (itB != shapeB.rend())
++itB;
}
// Reverse to get the correct order
std::reverse(resultShape.begin(), resultShape.end());
return resultShape;
}
size_t calculateBroadcastIndex(size_t index, const std::vector<size_t> &shape, const std::vector<size_t> &stride)
{
size_t originalIndex = 0;
for (size_t i = 0; i < shape.size(); ++i)
{
// Calculate the index in the current dimension
size_t dimIndex = (index / stride[i]) % shape[i];
// Accumulate the index considering the stride
originalIndex += dimIndex * stride[i];
}
return originalIndex;
}
Tensor operator+(const Tensor &a, const Tensor &b)
{
if (a.size() != b.size())
{
throw std::invalid_argument("Shapes of the tensors must match for addition.");
}
// Tensor result(a.size(), a.type());
// size_t total_size = 1;
// for (size_t i = 0; i < a.dimension; ++i)
// {
// total_size *= a.shape[i];
// }
// for (size_t i = 0; i < total_size; ++i)
// {
// result.data_[i] = a.data_[i] + b.data_[i];
// }
std::vector<size_t> result_shape = calculateBroadcastShape(a.shape, b.shape);
// Create result tensor with broadcasted shape
Tensor result(result_shape, a.type());
// Iterate over elements in the broadcasted shape
for (size_t i = 0; i < result.total_size(); ++i)
{
size_t idx_a = calculateBroadcastIndex(i, a.shape, a.stride);
size_t idx_b = calculateBroadcastIndex(i, b.shape, b.stride);
// Perform the addition
result.data_[i] = a.data_[idx_a] + b.data_[idx_b];
}
return result;
}
Tensor operator-(const Tensor &a, const Tensor &b)
{
if (a.size() != b.size())
{
throw std::invalid_argument("Shapes of the tensors must match for addition.");
}
// Tensor result(a.size(), a.type());
// size_t total_size = 1;
// for (size_t i = 0; i < a.dimension; ++i)
// {
// total_size *= a.shape[i];
// }
// for (size_t i = 0; i < total_size; ++i)
// {
// result.data_[i] = a.data_[i] + b.data_[i];
// }
std::vector<size_t> result_shape = calculateBroadcastShape(a.shape, b.shape);
// Create result tensor with broadcasted shape
Tensor result(result_shape, a.type());
// Iterate over elements in the broadcasted shape
for (size_t i = 0; i < result.total_size(); ++i)
{
size_t idx_a = calculateBroadcastIndex(i, a.shape, a.stride);
size_t idx_b = calculateBroadcastIndex(i, b.shape, b.stride);
// Perform the addition
result.data_[i] = a.data_[idx_a] - b.data_[idx_b];
}
return result;
}
Tensor operator*(const Tensor &a, const Tensor &b)
{
if (a.size() != b.size())
{
throw std::invalid_argument("Shapes of the tensors must match for addition.");
}
// Tensor result(a.size(), a.type());
// size_t total_size = 1;
// for (size_t i = 0; i < a.dimension; ++i)
// {
// total_size *= a.shape[i];
// }
// for (size_t i = 0; i < total_size; ++i)
// {
// result.data_[i] = a.data_[i] + b.data_[i];
// }
std::vector<size_t> result_shape = calculateBroadcastShape(a.shape, b.shape);
// Create result tensor with broadcasted shape
Tensor result(result_shape, a.type());
// Iterate over elements in the broadcasted shape
for (size_t i = 0; i < result.total_size(); ++i)
{
size_t idx_a = calculateBroadcastIndex(i, a.shape, a.stride);
size_t idx_b = calculateBroadcastIndex(i, b.shape, b.stride);
// Perform the addition
result.data_[i] = a.data_[idx_a] * b.data_[idx_b];
}
return result;
}
Tensor operator/(const Tensor &a, const Tensor &b)
{
if (a.size() != b.size())
{
throw std::invalid_argument("Shapes of the tensors must match for addition.");
}
// Tensor result(a.size(), a.type());
// size_t total_size = 1;
// for (size_t i = 0; i < a.dimension; ++i)
// {
// total_size *= a.shape[i];
// }
// for (size_t i = 0; i < total_size; ++i)
// {
// result.data_[i] = a.data_[i] + b.data_[i];
// }
std::vector<size_t> result_shape = calculateBroadcastShape(a.shape, b.shape);
// Create result tensor with broadcasted shape
Tensor result(result_shape, a.type());
// Iterate over elements in the broadcasted shape
for (size_t i = 0; i < result.total_size(); ++i)
{
size_t idx_a = calculateBroadcastIndex(i, a.shape, a.stride);
size_t idx_b = calculateBroadcastIndex(i, b.shape, b.stride);
// Perform the addition
result.data_[i] = a.data_[idx_a] / b.data_[idx_b];
}
return result;
}
// 成员函数,实现Tensor加Tensor
Tensor Tensor::add(const Tensor &other) const
{
return *this + other; // Reuse the operator+ for Tensor objects
}
// 成员函数,实现Tensor加标量
Tensor Tensor::add(double value) const
{
Tensor result(shape, dtype_);
size_t total_size = 1;
for (size_t i = 0; i < dimension; ++i)
{
total_size *= shape[i];
}
for (size_t i = 0; i < total_size; ++i)
{
result.data_[i] = this->data_[i] + value;
}
return result;
}
Tensor add(const Tensor &a, const Tensor &b)
{
return a + b; // Reuse the operator+ for Tensor objects
}
Tensor add(const Tensor &a, double value)
{
return a.add(value); // Reuse the Tensor's member function for scalar addition
}
// Tensor operator-(const Tensor &a, const Tensor &b)
// {
// if (a.size() != b.size())
// {
// throw std::invalid_argument("Shapes of the tensors must match for addition.");
// }
// Tensor result(a.size(), a.type());
// size_t total_size = 1;
// for (size_t i = 0; i < a.dimension; ++i)
// {
// total_size *= a.shape[i];
// }
// for (size_t i = 0; i < total_size; ++i)
// {
// result.data_[i] = a.data_[i] - b.data_[i];
// }
// return result;
// }
// 成员函数,实现Tensor加Tensor
Tensor Tensor::sub(const Tensor &other) const
{
return *this - other; // Reuse the operator+ for Tensor objects
}
// 成员函数,实现Tensor加标量
Tensor Tensor::sub(double value) const
{
Tensor result(shape, dtype_);
size_t total_size = 1;
for (size_t i = 0; i < dimension; ++i)
{
total_size *= shape[i];
}
for (size_t i = 0; i < total_size; ++i)
{
result.data_[i] = this->data_[i] - value;
}
return result;
}
Tensor sub(const Tensor &a, const Tensor &b)
{
return a - b; // Reuse the operator+ for Tensor objects
}
Tensor sub(const Tensor &a, double value)
{
return a.sub(value); // Reuse the Tensor's member function for scalar addition
}
Tensor Tensor::mul(const Tensor &other) const
{
return *this * other; // Reuse the operator+ for Tensor objects
}
// 成员函数,实现Tensor加标量
Tensor Tensor::mul(double value) const
{
Tensor result(shape, dtype_);
size_t total_size = 1;
for (size_t i = 0; i < dimension; ++i)
{
total_size *= shape[i];
}
for (size_t i = 0; i < total_size; ++i)
{
result.data_[i] = this->data_[i] * value;
}
return result;
}
Tensor mul(const Tensor &a, const Tensor &b)
{
return a * b; // Reuse the operator+ for Tensor objects
}
Tensor mul(const Tensor &a, double value)
{
return a.mul(value); // Reuse the Tensor's member function for scalar addition
}
Tensor Tensor::div(const Tensor &other) const
{
return *this / other; // Reuse the operator+ for Tensor objects
}
// 成员函数,实现Tensor加标量
Tensor Tensor::div(double value) const
{
Tensor result(shape, dtype_);
size_t total_size = 1;
for (size_t i = 0; i < dimension; ++i)
{
total_size *= shape[i];
}
for (size_t i = 0; i < total_size; ++i)
{
result.data_[i] = this->data_[i] / value;
}
return result;
}
Tensor div(const Tensor &a, const Tensor &b)
{
return a / b; // Reuse the operator+ for Tensor objects
}
Tensor div(const Tensor &a, double value)
{
return a.div(value); // Reuse the Tensor's member function for scalar addition
}
Tensor dot(const Tensor &a, const Tensor &b)
{
if (a.dimens() < 1 || b.dimens() < 1)
{
throw std::invalid_argument("Tensors must have at least 1 dimension for dot product.");
}
if (a.size()[a.dimens() - 1] != b.size()[0])
{
throw std::invalid_argument("Incompatible dimensions for dot product.");
}
// 确定结果张量的形状
std::vector<size_t> result_shape;
// 添加张量A的形状,除去最后一个维度
for (size_t i = 0; i < a.dimens() - 1; ++i)
{
result_shape.push_back(a.size()[i]);
}
// 添加张量B的形状,除去第一个维度
for (size_t i = 1; i < b.dimens(); ++i)
{
result_shape.push_back(b.size()[i]);
}
// 创建结果张量
Tensor result(result_shape, a.type());
// 计算点积
// M = ? N = ?
int M = a.total_size() / a.get_shape()[a.dimens() - 1];
int N = b.total_size() / b.get_shape()[0];
int step = b.get_shape()[0];
int stride = b.get_stride()[0];
for (int i = 0; i < M; ++i)
{
for (int j = 0; j < N; j++)
{
int ans = 0;
for (int k = 0; k < step; k++)
{
ans += a.get_element(i * step + k) * b.get_element(k * stride + j);
}
result.set_element(i * N + j, ans);
}
}
return result;
}
}