-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.cpp
More file actions
executable file
·370 lines (332 loc) · 9.95 KB
/
Copy pathtensor.cpp
File metadata and controls
executable file
·370 lines (332 loc) · 9.95 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
#include "tensor.hpp"
#include <algorithm>
Tensor::Tensor(const std::vector<int> &shape) {
// jsp si faut pas bloquer le cas {}
this->shape = shape;
for (int i = 0; i < shape.size(); i++) {
assert(shape[i] != 0);
if (i == 0) {
stride.push_back(1);
} else {
stride.push_back(stride[i - 1] * shape[i - 1]);
}
}
dim = shape.empty() ? 1 : stride.back() * shape.back();
data = std::make_shared<std::vector<double>>(dim, 0.0);
}
Tensor::Tensor() {
this->shape = {};
this->stride = {};
this->dim = 1;
data = std::make_shared<std::vector<double>>(1, 0.0);
}
double &Tensor::get(const std::vector<int> &pos) {
int dim = pos.size();
assert(dim == this->shape.size());
for (int i = 0; i < dim; i++) {
assert(pos[i] < this->shape[i]);
}
int sum = 0;
for (int i = 0; i < dim; i++) {
sum += pos[i] * this->stride[i];
}
return (*data)[sum];
}
const double &Tensor::get(const std::vector<int> &pos) const {
int dim = pos.size();
assert(dim == this->shape.size());
for (int i = 0; i < dim; i++) {
assert(pos[i] < this->shape[i]);
}
int sum = 0;
for (int i = 0; i < dim; i++) {
sum += pos[i] * this->stride[i];
}
return (*data)[sum];
}
// for scalars
double &Tensor::get() {
assert(this->dim == 1);
return (*data)[0];
}
const double &Tensor::get() const {
assert(this->dim == 1);
return (*data)[0];
}
template <typename Func>
void iterate(const std::vector<int> &shape, const std::vector<int> &stride,
Func &&fn) {
int ndim = shape.size();
std::vector<int> x(ndim, 0);
int offset = 0;
while (true) {
fn(offset, x);
int dim = ndim - 1;
while (dim >= 0) {
x[dim]++;
offset += stride[dim];
if (x[dim] < shape[dim]) {
break;
}
offset -= shape[dim] * stride[dim];
x[dim] = 0;
dim--;
}
if (dim < 0) {
break;
}
}
}
int Tensor::effectiveDim() const{
int dim = this->shape.size();
int trailingOnes = 0;
while(this->shape[trailingOnes] == 1){
trailingOnes ++;
}
return dim - trailingOnes;
}
// until is used to ignore the n last dims (ex: for matmul )
// this is not deffensive at all, shapes and strides should be cleared
// also until should be checked
void Tensor::broadcast(const Tensor& other, int until, Tensor* out1, Tensor* out2) const{
//std::cout << "br en cours\n";
int dimThis = this->shape.size();
int dimOther= other.shape.size();
int outDim = std::max(dimThis,dimOther);
int smallDim = std::min(dimThis,dimOther);
bool thisIsSmaller = dimOther==outDim;
int thisOffset = outDim - dimThis;
int otherOffset = outDim - dimOther;
assert(smallDim >= until);
if (thisIsSmaller){
out1->stride.insert(out1->stride.begin(),thisOffset,0);
}
else{
out2->stride.insert(out2->stride.begin(),otherOffset,0);
}
out1->stride.insert(out1->stride.end(),this->stride.begin(),this->stride.end());
out2->stride.insert(out2->stride.end(),other.stride.begin(),other.stride.end());
// first part
for(int i = 0; i<outDim-smallDim; i++){
if(thisIsSmaller){
out1->shape.push_back(other.shape[i]);
out2->shape.push_back(other.shape[i]);
}
else{
out1->shape.push_back(this->shape[i]);
out2->shape.push_back(this->shape[i]);
}
}
//seconde part (main part)
for(int i = outDim-smallDim; i<outDim-until; i++){
int dim1,dim2;
dim1 = this->shape[i-thisOffset];
dim2 = other.shape[i-otherOffset];
if (dim1 == 1){
out1->shape.push_back(dim2);
out2->shape.push_back(dim2);
out1->stride[i] = 0;
}
else if (dim2== 1){
out1->shape.push_back(dim1);
out2->shape.push_back(dim1);
out2->stride[i] = 0;
}
else if (dim1 == dim2){
out1->shape.push_back(dim1);
out2->shape.push_back(dim2);
}
else{
std::cout << "could not broadcast shapes !" << dim1 << "!=" << dim2 << "\n";
std::exit(1);
}
}
//third part (ignored by until) (mainly for matrices)
for(int i = outDim-until; i<outDim; i++ ){
out1->shape.push_back(this->shape[i-thisOffset]);
out2->shape.push_back(other.shape[i-otherOffset]);
}
out1->data = this->data;
out2->data = other.data;
// std::cout << "br finie\n";
}
// TODO: do try catch block for broadcasting,
// rn we only broadcast other so first arg will always be the final shape
Tensor Tensor::operator+(const Tensor &other) {
Tensor* br1 = new Tensor();
Tensor* br2 = new Tensor();
this->broadcast(other, 0, br1, br2);
Tensor out(br1->shape);
iterate(this->shape, this->stride,
[&](int offset, const std::vector<int> &x) {
out.get(x) = br1->get(x) + br2->get(x);
});
delete br1;
delete br2;
return out;
}
Tensor Tensor::operator-(const Tensor &other) {
Tensor* br1 = new Tensor();
Tensor* br2 = new Tensor();
this->broadcast(other, 0, br1, br2);
Tensor out(br1->shape);
iterate(this->shape, this->stride,
[&](int offset, const std::vector<int> &x) {
out.get(x) = br1->get(x) - br2->get(x);
});
delete br1;
delete br2;
return out;
}
Tensor Tensor::operator-() {
Tensor out(this->shape);
iterate(this->shape, this->stride,
[&](int offset, const std::vector<int> &x) {
out.get(x) = -this->get(x);
});
return out;
}
Tensor Tensor::operator+(const double other) {
Tensor out(this->shape);
iterate(this->shape, this->stride,
[&](int offset, const std::vector<int> &x) {
out.get(x) = this->get(x) + other;
});
return out;
}
Tensor Tensor::operator*(const double other) {
Tensor out(this->shape);
iterate(this->shape, this->stride,
[&](int offset, const std::vector<int> &x) {
out.get(x) = this->get(x) * other;
});
return out;
}
Tensor Tensor::operator*(const Tensor &other) {
int other_dim = other.shape.size();
Tensor* br1 = new Tensor();
Tensor* br2 = new Tensor();
this->broadcast(other, 0, br1, br2);
Tensor out(br1->shape);
iterate(this->shape, this->stride,
[&](int offset, const std::vector<int> &x) {
out.get(x) = br1->get(x) * br2->get(x);
});
delete br1;
delete br2;
return out;
}
// TODO implement matrix multiplication but for tensors (repeating matrix mult)
Tensor Tensor::mm(const Tensor &other) {
assert(this->shape.size() >= 2 && other.shape.size() >= 2);
auto start = std::chrono::steady_clock::now();
int ed1 = this->effectiveDim();
int ed2 = other.effectiveDim();
Tensor* mat1_broadcasted = new Tensor();
Tensor* mat2_broadcasted = new Tensor();
this->broadcast(other, 2, mat1_broadcasted, mat2_broadcasted);
int dim = mat2_broadcasted->shape.size();
int k = mat1_broadcasted->shape[dim-1];
assert(k == mat2_broadcasted->shape[dim - 2]);
std::vector<int> newShape = mat2_broadcasted->shape;
newShape[dim - 2] = mat1_broadcasted->shape[dim -2];
Tensor out(newShape);
int thisMovingStride = mat1_broadcasted->stride[dim-1];
int otherMovingStride = mat2_broadcasted->stride[dim-2];
iterate(out.shape, out.stride, [&](int offset, const std::vector<int> &x) {
int thisPos =0 ,otherPos = 0;
for(int i = 0; i<dim; i++){
otherPos += x[i]*mat2_broadcasted->stride[i];
thisPos += x[i]*mat1_broadcasted->stride[i];
}
thisPos -= x[dim-1]*thisMovingStride;
otherPos -= x[dim-2]*otherMovingStride;
for (int i = 0; i < k; i++) {
(*out.data)[offset] += (*mat1_broadcasted->data)[thisPos + thisMovingStride*i] * (*mat2_broadcasted->data)[otherPos + otherMovingStride*i];
}
});
delete mat1_broadcasted;
delete mat2_broadcasted;
auto end = std::chrono::steady_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::milliseconds>(end- start);
std::cout << "mm took :" << duration.count() << '\n';
return out;
}
Tensor Tensor::power(double expo) {
Tensor out(this->shape);
iterate(out.shape, out.stride, [&](int offset, const std::vector<int> &x) {
out.get(x) = pow(this->get(x), expo);
});
return out;
}
Tensor Tensor::sigmoid() {
Tensor out(this->shape);
iterate(out.shape, out.stride, [&](int offset, const std::vector<int> &x) {
out.get(x) = 1 / (1 + exp(-this->get(x)));
});
return out;
}
Tensor Tensor::sigmoidDeriv() {
Tensor out(this->shape);
iterate(out.shape, out.stride, [&](int offset, const std::vector<int> &x) {
out.get(x) = exp(-this->get(x)) / pow(1 + exp(-this->get(x)), 2);
});
return out;
}
Tensor Tensor::sum() {
double acc = 0;
iterate(this->shape, this->stride,
[&](int offset, const std::vector<int> &x) { acc += this->get(x); });
Tensor out;
out.get() = acc;
return out;
}
Tensor Tensor::sum(std::vector<int> axes) {
std::sort(axes.begin(),axes.end());
std::vector<int> outShape;
for(int v: outShape){
outShape.push_back(this->shape[v]);
}
Tensor out(outShape);
double acc = 0;
iterate(this->shape, this->stride,
[&](int offset, const std::vector<int> &x) {
std::vector<int> currentIndex;
for(int v: outShape){
currentIndex.push_back(x[v]);
}
out.get(currentIndex) = this->get(x);
});
return out;
}
// not sure this makes a reference to data.
Tensor Tensor::transpose() {
assert(this->shape.size() >= 2);
std::vector<int> newShape = this->shape;
int dim = this->shape.size();
int n = newShape[dim-2];
int m = newShape[dim-1];
newShape[dim- 2] = m;
newShape[dim- 1] = n;
Tensor out(newShape);
out.stride[dim-2] = (dim-2)?out.stride[dim-3]*out.shape[dim-3]:1; //c horrible
out.stride[dim-1]= out.stride[dim-2]*m;
out.data = this->data;
return out;
}
void Tensor::printShape() {
std::cout << "[";
for (const int &i : this->shape) {
std::cout << i << ", ";
}
std::cout << "]";
}
void Tensor::fillRandom() {
std::mt19937_64 rng(std::random_device{}());
std::uniform_real_distribution<double> dist(-0.1, 0.1);
iterate(
this->shape, this->stride,
[&](int offset, const std::vector<int> &x) { this->get(x) = dist(rng); });
std::cout << "\n";
}