-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.h
More file actions
558 lines (454 loc) · 19.6 KB
/
tensor.h
File metadata and controls
558 lines (454 loc) · 19.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#include <vector>
#include <random>
#include <cmath>
#include "tensor_util.h"
using std::vector;
using std::string;
class Tensor {
public:
vector<float> data;
bool requires_grad = false;
vector<float> grad;
vector<int> shape;
int size;
Tensor() {
this->data = {};
this->shape = {0};
}
Tensor(vector<float> data, vector<int> shape) {
// Set the shape
this->shape = shape;
// Calculate the size
this->size = 1;
for (int i = 0; i < shape.size(); i++) {
this->size *= shape[i];
}
if (this->size != data.size()) {
throw std::invalid_argument("data and shape do not match");
}
// Set the data
this->data = data;
// Create a vector to hold the gradients
this->grad = vector<float>(this->size);
}
static Tensor Arange(int start, int end, int step=1) {
vector<float> data;
for (int i = start; i < end; i += step) {
data.push_back(i);
}
return Tensor(data, {int(data.size())});
}
static Tensor Randn(vector<int> shape) {
// Create a vector to hold the data
vector<float> data = vector<float>(ShapeToSize(shape));
// Loop through the shape
std::random_device rd;
std::mt19937 gen(rd());
for (int i = 0; i < shape[0]; i++) {
for (int j = 0; j < shape[1]; j++) {
// Add a random number to the data
std::normal_distribution<> d(0, 1);
data[i] += d(gen);
}
}
// Create and return the tensor
return Tensor(data, shape);
}
static Tensor Full(vector<int> shape, float value) {
// Create a vector to hold the data
vector<float> data = vector<float>(ShapeToSize(shape));
for (int i = 0; i < data.size(); i++) {
data[i] = value;
}
// Create and return the tensor
return Tensor(data, shape);
}
static Tensor FromCsv(string file_name) {
string file_content = ReadFile(file_name);
vector<vector<float>> matrix = ReadCsvMatrix(file_content);
vector<float> data = vector<float>();
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[i].size(); j++) {
data.push_back(matrix[i][j]);
}
}
return Tensor(data, {int(matrix.size()), int(matrix[0].size())});
}
Tensor PermuteDims(vector<int> new_order) {
// Calculate the new shape
vector<int> new_shape = vector<int>(new_order.size());
for (int i = 0; i < new_order.size(); i++) {
new_shape[i] = this->shape[new_order[i]];
}
// Create a vector to hold the data
vector<float> new_data = vector<float>(this->size);
// Loop through the data
for (int i = 0; i < this->size; i++) {
// Get the index
vector<int> index = FromLinearIndex(this->shape, i);
// Permute the index
vector<int> new_index = vector<int>(new_order.size());
for (int j = 0; j < new_order.size(); j++) {
new_index[j] = index[new_order[j]];
}
// Set the new data
new_data[ToLinearIndex(new_shape, new_index)] = this->data[i];
}
// Create and return the tensor
return Tensor(new_data, new_shape);
}
vector<vector<float>> BuildReductionMatrix(int dim) {
// permute dims to {0, 1, ... , dim}
vector<int> new_order = vector<int>(this->shape.size());
for (int i = 0; i < this->shape.size()-1; i++) {
if (i < dim) {
new_order[i] = i;
} else {
new_order[i] = i + 1;
}
}
new_order[this->shape.size()-1] = dim;
Tensor permuted_tensor = this->PermuteDims(new_order);
permuted_tensor.Reshape({this->size / this->shape[dim], this->shape[dim]});
// Create the reduction matrix
vector<vector<float>> reduction_matrix = vector<vector<float>>(this->size / this->shape[dim], vector<float>(this->shape[dim]));
// Fill in the reduction matrix
for (int r = 0; r < this->size / this->shape[dim]; r++) {
for (int c = 0; c < this->shape[dim]; c++) {
reduction_matrix[r][c] = permuted_tensor.Get({r, c});
}
}
return reduction_matrix;
}
static Tensor FromReductionMatrix(vector<vector<float>> reduction_matrix, int dim, vector<int> out_shape) {
// Build a tensor such that the rows of the reduction matrix are in the dim dimension
// verify num_cols == out_shape[dim]
if (reduction_matrix[0].size() != out_shape[dim]) {
throw std::invalid_argument("reduction_matrix and out_shape do not match" + std::to_string(reduction_matrix[0].size()) + " " + std::to_string(out_shape[dim]));
}
// Create a vector to hold the data
vector<float> new_data = vector<float>(ShapeToSize(out_shape));
// copy reduction matrix into new_data
for (int r = 0; r < reduction_matrix.size(); r++) {
for (int c = 0; c < reduction_matrix[0].size(); c++) {
new_data[r * reduction_matrix[0].size() + c] = reduction_matrix[r][c];
}
}
Tensor new_tensor = Tensor(new_data, {int(reduction_matrix.size()), out_shape[dim]});
// if (dim == 0) {
// new_tensor = new_tensor.PermuteDims({1, 0});
// }
// return new_tensor;
// reshape to {out_shape[0], out_shape[1], ..., out_shape[size], out_shape[dim]}
vector<int> new_order = vector<int>(out_shape.size());
for (int i = 0; i < out_shape.size()-1; i++) {
if (i < dim) {
new_order[i] = i;
} else {
new_order[i] = i + 1;
}
}
new_order[out_shape.size()-1] = dim;
vector<int> new_shape = vector<int>(out_shape.size());
for (int i = 0; i < out_shape.size(); i++) {
new_shape[i] = out_shape[new_order[i]];
}
new_tensor.Reshape(new_shape);
// Tensor new_tensor = Tensor(new_data, new_shape);
// permute dims to {0, 1, ..., dim, ..., size}
vector<int> old_order = vector<int>(out_shape.size());
for (int i = 0; i < out_shape.size(); i++) {
if (i < dim) {
old_order[i] = i;
} else if (i > dim) {
old_order[i] = i - 1;
} else {
old_order[i] = out_shape.size() - 1;
}
}
return new_tensor.PermuteDims(old_order);
}
Tensor Reduce(float (*func)(vector<float>), int dim) {
vector<vector<float>> reduction_matrix = this->BuildReductionMatrix(dim);
vector<vector<float>> reduced_matrix = vector<vector<float>>(reduction_matrix.size(), vector<float>(1));
// fill in the reduced matrix
for (int r = 0; r < reduction_matrix.size(); r++) {
reduced_matrix[r][0] = func(reduction_matrix[r]);
}
// Calculate the shape of the output
vector<int> output_shape;
for (int i = 0; i < this->shape.size(); i++) {
if (i != dim) {
output_shape.push_back(this->shape[i]);
} else {
output_shape.push_back(1);
}
}
return Tensor::FromReductionMatrix(reduced_matrix, dim, output_shape);
}
Tensor Apply(vector<float> (*func)(vector<float>), int dim) {
vector<vector<float>> reduction_matrix = this->BuildReductionMatrix(dim);
// vector<vector<float>> applied_matrix = vector<vector<float>>(reduction_matrix.size(), vector<float>(this->shape[dim]));
// fill in the applied matrix
for (int r = 0; r < reduction_matrix.size(); r++) {
vector<float> result = func(reduction_matrix[r]);
for (int c = 0; c < this->shape[dim]; c++) {
reduction_matrix[r][c] = result[c];
}
}
return Tensor::FromReductionMatrix(reduction_matrix, dim, this->shape);
}
Tensor ReduceSum(int dim) {
return this->Reduce([](vector<float> v) {
float sum = 0;
for (int i = 0; i < v.size(); i++) {
sum += v[i];
}
return sum;
}, dim);
}
Tensor Max(int dim) {
return this->Reduce([](vector<float> v) {
float max = v[0];
for (int i = 1; i < v.size(); i++) {
if (v[i] > max) {
max = v[i];
}
}
return max;
}, dim);
}
Tensor ArgMax(int dim) {
return this->Reduce([](vector<float> v) {
float max = v[0];
int max_index = 0;
for (int i = 1; i < v.size(); i++) {
if (v[i] > max) {
max = v[i];
max_index = i;
}
}
return float(max_index);
}, dim);
}
Tensor Broadcast(float (*func)(float, float), Tensor other) {
// // get the shapes but as a copy
// vector<int> this_shape = vector<int>(this->shape);
// vector<int> other_shape = vector<int>(other.shape);
// for (int i = 0; i < this_shape.size(); i++) {
// this_shape[i] = this->shape[i];
// }
// for (int i = 0; i < other_shape.size(); i++) {
// other_shape[i] = other.shape[i];
// }
// // while the shapes are not the same length, prepend a 1 to the smaller shape
// while (this_shape.size() < other_shape.size()) {
// this_shape.insert(this_shape.begin(), 1);
// }
// while (other_shape.size() < this_shape.size()) {
// other_shape.insert(other_shape.begin(), 1);
// }
vector<vector<int>> padded_shapes = PadShapes(this->shape, other.shape);
vector<int> this_shape = padded_shapes[0];
vector<int> other_shape = padded_shapes[1];
bool equal = true;
vector<int> new_shape = vector<int>(this_shape.size());
for (int i = 0; i < this_shape.size(); i++) {
if (this_shape[i] != other_shape[i] && this_shape[i] != 1 && other_shape[i] != 1) {
equal = false;
} else {
new_shape[i] = this_shape[i] > other_shape[i] ? this_shape[i] : other_shape[i];
}
}
if (!equal) {
throw std::invalid_argument("Tensors must have the same shape or axis must be 1");
}
vector<float> new_data = vector<float>(ShapeToSize(new_shape));
Tensor new_tensor = Tensor(new_data, new_shape);
for (vector<int> index : AllIndicies(new_shape)) {
vector<int> this_index = vector<int>(index.size());
vector<int> other_index = vector<int>(index.size());
for (int i = 0; i < index.size(); i++) {
if (this_shape[i] == 1) {
this_index[i] = 0;
} else {
this_index[i] = index[i];
}
if (other_shape[i] == 1) {
other_index[i] = 0;
} else {
other_index[i] = index[i];
}
}
new_tensor.Set(index, func(this->Get(this_index), other.Get(other_index)));
}
return new_tensor;
}
Tensor AddTensor(Tensor other) {
return this->Broadcast([](float a, float b) {
return a + b;
}, other);
}
Tensor MulTensor(Tensor other) {
return this->Broadcast([](float a, float b) {
return a * b;
}, other);
}
Tensor SubTensor(Tensor other) {
return this->Broadcast([](float a, float b) {
return a - b;
}, other);
}
Tensor DivTensor(Tensor other) {
return this->Broadcast([](float a, float b) {
return a / b;
}, other);
}
Tensor MatMul(Tensor other) {
// mat mul between the last two dimensions of each tensor
if (this->shape.size() < 2 || other.shape.size() < 2) {
throw std::invalid_argument("Tensors must have at least 2 dimensions");
}
int this_dim = this->shape.size() - 1;
int other_dim = other.shape.size() - 2;
if (this->shape[this_dim] != other.shape[other_dim]) {
throw std::invalid_argument("Dimensions not compatible for matmul");
}
vector<vector<int>> padded_shapes = PadShapes(this->shape, other.shape);
vector<int> this_shape = padded_shapes[0];
vector<int> other_shape = padded_shapes[1];
vector<int> new_shape = vector<int>(this_shape.size());
for (int i = 0; i < this_shape.size() - 2; i++) {
new_shape[i] = this_shape[i] > other_shape[i] ? this_shape[i] : other_shape[i];
}
new_shape[this_shape.size() - 2] = this_shape[this_shape.size() - 2];
new_shape[this_shape.size() - 1] = other_shape[other_shape.size() - 1];
vector<float> new_data = vector<float>(ShapeToSize(new_shape));
Tensor new_tensor = Tensor(new_data, new_shape);
for (vector<int> index : AllIndicies(new_shape)) {
vector<int> this_index = vector<int>(index.size());
vector<int> other_index = vector<int>(index.size());
int row = index[index.size() - 2];
int col = index[index.size() - 1];
for (int i = 0; i < index.size() - 2; i++) {
this_index[i] = index[i];
other_index[i] = index[i];
}
this_index[this_index.size() - 2] = row;
this_index[this_index.size() - 1] = 0;
other_index[other_index.size() - 2] = 0;
other_index[other_index.size() - 1] = col;
float sum = 0;
for (int i = 0; i < this->shape[this_dim]; i++) {
this_index[this_index.size() - 1] = i;
other_index[other_index.size() - 2] = i;
sum += this->Get(this_index) * other.Get(other_index);
}
new_tensor.Set(index, sum);
}
return new_tensor;
}
Tensor MulScaler(float scaler) {
vector<float> new_data = vector<float>(this->data);
for (int i = 0; i < this->data.size(); i++) {
new_data[i] = this->data[i] * scaler;
}
return Tensor(new_data, this->shape);
}
Tensor PowScaler(float scaler) {
vector<float> new_data = vector<float>(this->data);
for (int i = 0; i < this->data.size(); i++) {
new_data[i] = pow(this->data[i], scaler);
}
return Tensor(new_data, this->shape);
}
Tensor Exp() {
vector<float> new_data = vector<float>(this->data);
for (int i = 0; i < this->data.size(); i++) {
new_data[i] = exp(this->data[i]);
}
return Tensor(new_data, this->shape);
}
// Tensor ApplyMul(Tensor other, int dim) {
// return this->Apply([&other, dim](vector<float> v) {
// vector<float> result = vector<float>(v.size());
// for (int i = 0; i < v.size(); i++) {
// result[i] = v[i] * other.data[i];
// }
// return result;
// }, dim);
// }
Tensor Normalize(int dim) {
// divide each element by the sum of the row
return this->Apply([](vector<float> v) {
float sum = 0;
for (int i = 0; i < v.size(); i++) {
sum += v[i];
}
vector<float> new_row = vector<float>(v.size());
for (int i = 0; i < v.size(); i++) {
new_row[i] = v[i] / sum;
}
return new_row;
}, dim);
}
void Reshape(vector<int> shape) {
// Assert that the new shape has the same size as the old shape
int new_size = 1;
for (int i = 0; i < shape.size(); i++) {
new_size *= shape[i];
}
if (new_size != this->size) {
// error message with the original shape and the new shape
string original_shape = "";
for (int i = 0; i < this->shape.size(); i++) {
original_shape += std::to_string(this->shape[i]) + " ";
}
string new_shape = "";
for (int i = 0; i < shape.size(); i++) {
new_shape += std::to_string(shape[i]) + " ";
}
throw std::invalid_argument("The new shape must have the same size as the old shape.\nOriginal shape: " + original_shape + "\n" + "New shape: " + new_shape);
}
// Set the new shape
this->shape = shape;
}
float Get(vector<int> index) {
return this->data[ToLinearIndex(this->shape, index)];
}
void Set(vector<int> index, float value) {
this->data[ToLinearIndex(this->shape, index)] = value;
}
// ToString
string ToString() {
// Create a string to hold the tensor
string tensor_string = "";
// add the shape
tensor_string += "Shape: ";
for (int i = 0; i < this->shape.size(); i++) {
tensor_string += std::to_string(this->shape[i]);
if (i < this->shape.size() - 1) {
tensor_string += ", ";
}
}
tensor_string += "\nData: ";
// Loop through the data
for (int i = 0; i < this->data.size(); i++) {
// Add the value to the string
tensor_string += std::to_string(this->data[i]);
// Add a comma if it is not the last value
if (i != this->data.size() - 1) {
tensor_string += ", ";
}
}
// Return the string
return tensor_string;
}
static Tensor Zeros(vector<int> shape) {
int size = 1;
for (int i = 0; i < shape.size(); i++) {
size *= shape[i];
}
return Tensor(vector<float>(size), shape);
}
};