-
Notifications
You must be signed in to change notification settings - Fork 0
/
tensor2.cpp
596 lines (518 loc) · 16.5 KB
/
tensor2.cpp
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include <cstddef>
#include <cstdlib>
#include <format>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
// TODO: compile with flag to turn off exceptions
#define INVALID_ARGUMENT(MSG) \
cout << MSG << endl; \
exit(1);
#define HEAP_MEMORY_SIZE 2 << 16
#define STACK_MEMORY_SIZE 2 << 10
#define SEED 0
// TODO: could we line this up with L1 line sizes?
// To look at the program memory map compile with -Wl,-map,output.map
// Why do this? We want our fast data structure to have its pointers on the
// stack & it's potentially large tensors on the heap. We want those tensors to
// be in contiguous memory & preferably in the L1 cache which will save us 100s
// of cycles per cache miss.
struct Memory {
size_t HeapLen; // sizeof preallocated heap buffer
char* T; // beginning of memory holding tensors
char* TP; // beginning of free heap memory
// The only way this works is if the Memory object is in main fn scope, i.e.
// as soon as it goes out of scope, our stack pointers are destroyed.
size_t StackLen; // sizeof preallocated stack buffer
char* S; // beginning of stack memory holding pointers to tensors
char* SP; // beginning of free stack memory
};
Memory& initMemory(Memory& M, size_t HeapMemorySize, size_t StackMemorySize,
char* StackBuffer) {
M.T = (char*)malloc(HeapMemorySize);
M.TP = M.T;
M.HeapLen = HeapMemorySize;
M.S = StackBuffer;
M.SP = M.S;
M.StackLen = StackMemorySize;
return M;
};
template <typename D> struct Tensor;
void cleanMemory(Memory& M) { free(M.T); }
static Memory ProgramMemory;
template <typename D> struct Tensor {
size_t Rows;
size_t Cols;
size_t Len;
D* Data;
Tensor<D>* Grad = NULL;
void (*BackProp)(Tensor<D>&) = NULL;
vector<Tensor<D>*> Parents;
vector<Tensor<D>*> Children;
D& operator[](int Index) {
if (Index >= Len) {
stringstream SS;
SS << "Tensor array index out of bounds. IDX: " << Index
<< " LEN: " << Len << endl;
INVALID_ARGUMENT(SS.str());
}
return *(Data + Index);
};
friend ostream& operator<<(ostream& OStream, Tensor<D>& O) {
OStream << "\n";
OStream << "RowsS ColsS LenS DataS DataH" << endl;
OStream << &O.Rows << ' ' << &O.Cols << ' ' << &O.Len << ' ' << &O.Data
<< ' ' << O.Data << endl;
for (int I = 0; I < O.Len; I++) {
OStream << setw(8) << format("{:.4f}", O[I]) << ' ';
if (I % O.Cols == O.Cols - 1) {
OStream << "\n";
}
}
return OStream;
};
void backward() { this->BackProp(*this); }
Tensor<D>& t() const {
Tensor<D> I = *this;
Tensor<D>& Trns = createTensor(I.Cols, I.Rows);
for (int R = 0; R < I.Rows; R++) {
for (int C = 0; C < I.Cols; C++) {
Trns[C * I.Rows + R] = I[C + R * I.Cols];
}
}
return Trns;
}
static void mmBackward(Tensor<D>& Logits) {
Tensor<D>& L = *Logits.Parents[0];
Tensor<D>& R = *Logits.Parents[1];
*L.Grad += Logits.Grad->mm(R.t());
*R.Grad += L.t().mm(*Logits.Grad);
for (auto P : Logits.Parents) {
if (P->BackProp != NULL)
P->BackProp(*P);
}
}
static void tanHBackward(Tensor<D>& A) {
Tensor<D>& One = createTensor(A.Rows, A.Cols, ones);
Tensor<D>& Diff = One - A.squares();
Tensor<D>& Logits = *A.Parents[0];
*Logits.Grad += Diff * *A.Grad;
// TODO: this will break down-> use reverse topological order
for (auto P : A.Parents) {
if (P->BackProp != NULL)
P->BackProp(*P);
}
};
Tensor<D>& tanH() {
Tensor<D>& I = *this;
vector<Tensor<D>*> Parents = {&I};
Tensor<D>& O = createTensor(I.Rows, I.Cols, Parents, tanHBackward, true);
for (int R = 0; R < I.Rows; R++) {
for (int C = 0; C < I.Cols; C++) {
O[R * I.Cols + C] = tanh(I[R * I.Cols + C]);
}
}
return O;
};
Tensor<D>& squares() {
Tensor<D>& I = *this;
vector<Tensor<D>*> Parents = {&I};
// TODO: squares backprop?
Tensor<D>& S = createTensor(I.Rows, I.Cols, Parents, nullptr, false);
for (int R = 0; R < I.Rows; R++) {
for (int C = 0; C < I.Cols; C++) {
S[R * I.Cols + C] += I[R * I.Cols + C] * I[R * I.Cols + C];
}
}
return S;
}
friend Tensor<D>& operator*(D Data, Tensor<D>& RS) { return RS * Data; };
friend Tensor<D>& operator*(Tensor<D>& LS, D Data) {
Tensor<D>& O = createTensor(LS.Rows, LS.Cols);
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
O[R * LS.Cols + C] = LS[R * LS.Cols + C] * Data;
}
}
return O;
};
friend Tensor<D>& operator*(Tensor<D>& LS, Tensor<D>& RS) {
// TODO: elementwize multiplication backprop?
// TODO: broadcasting?
if (LS.Rows != RS.Rows || LS.Cols != RS.Cols) {
stringstream SS;
SS << "Dimensions of input tensors do not match: (" << LS.Rows << ", "
<< LS.Cols << ") RS (" << RS.Rows << ", " << RS.Cols << ")";
INVALID_ARGUMENT(SS.str());
}
Tensor<D>& O = createTensor(LS.Rows, LS.Cols);
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
O[R * LS.Cols + C] += LS[R * LS.Cols + C] * RS[R * LS.Cols + C];
}
}
return O;
};
Tensor<D>& operator/(D RS) {
Tensor<D>& LS = *this;
Tensor<D>& O = createTensor(LS.Rows, LS.Cols);
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
O[R * LS.Cols + C] = LS[R * LS.Cols + C] / RS;
}
}
return O;
};
Tensor<D>& operator/(Tensor<D>& RS) {
Tensor<D>& LS = *this;
Tensor<D>& O = createTensor(LS.Rows, RS.Rows);
// TODO: adapt to handle 3D tensors (later ndim tensors)
// tensor to be broadcast must be on the RHS, should that change?
if (RS.Cols == 1 && RS.Cols == 1) {
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
O[R * LS.Cols + C] = LS[R * LS.Cols + C] / RS[0];
}
}
} else if (LS.Rows == RS.Rows && LS.Cols == RS.Cols) {
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
O[R * LS.Cols + C] = LS[R * LS.Cols + C] / RS[R * LS.Cols + C];
}
}
} else if (LS.Rows == RS.Rows && RS.Cols == 1) {
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
O[R * LS.Cols + C] = LS[R * LS.Cols + C] / RS[R];
}
}
} else if (LS.Cols == RS.Cols && RS.Rows == 1) {
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
O[R * LS.Cols + C] = LS[R * LS.Cols + C] / RS[C];
}
}
} else {
INVALID_ARGUMENT("The dimension to be broadcast must be of length 1.");
}
return O;
};
Tensor<D>& sum() {
Tensor<D>& T = *this;
D S = 0;
for (int R = 0; R < T.Rows; R++) {
for (int C = 0; C < T.Cols; C++) {
S += T[R * T.Cols + C];
}
}
return createTensor(S);
};
static void lossBackward(Tensor<D>& L) {
L.Grad = &createTensor(L.Rows, L.Cols, ones);
Tensor<D>& A = *L.Parents[0];
for (auto P : L.Parents) {
if (P->BackProp != NULL)
P->BackProp(*P);
}
}
Tensor<D>& mse(Tensor<D>& Targets) {
Tensor<D>& A = *this;
Tensor<D>& Diff = (Targets - A) / A.Len;
Tensor<D>& L = Diff.squares().sum();
*A.Grad -= 2.f * Diff;
L.Parents.push_back(this);
L.BackProp = lossBackward;
return L;
};
// tensor to be broadcast must be on the RHS, should that change?
friend Tensor<D>& operator-=(Tensor<D>& LS, Tensor<D>& RS) {
if (LS.Rows == RS.Rows && LS.Cols == RS.Cols) {
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
LS[R * LS.Cols + C] -= RS[R * LS.Cols + C];
}
}
} else if (LS.Cols == RS.Cols && RS.Rows == 1) {
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
LS[R * LS.Cols + C] -= RS[C];
}
}
} else if (LS.Rows == RS.Rows && RS.Cols == 1) {
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
LS[R * LS.Cols + C] -= RS[R];
}
}
}
// TODO: only possible for operator-, not operator-= -> rethink - & -=
// operator overloads
// else if (LS.cols() == RS.cols() && LS.rows() == 1) {
// for (int R = 0; R < RS.rows(); R++) {
// for (int C = 0; C < LS.cols(); C++) {
// LS[C] -= RS[R * LS.cols() + C];
// }
// }
// } else if (LS.rows() == RS.rows() && LS.cols() == 1) {
// for (int R = 0; R < LS.rows(); R++) {
// for (int C = 0; C < RS.cols(); C++) {
// LS[R] -= RS[R * LS.cols() + C];
// }
// }
// }
else {
stringstream SS;
SS << "Dimensions of input tensors do not match & are "
"not broadcastable. The "
"dimension to be broadcast must be of length 1. LS ("
<< LS.Rows << ", " << LS.Cols << ") RS (" << RS.Rows << ", " << RS.Cols
<< ")";
throw invalid_argument(SS.str());
}
return LS;
}
friend Tensor<D>& operator-(Tensor<D>& LS, Tensor<D>& RS) {
vector<Tensor<D>*> Parents = {&LS, &RS};
// TODO: subtraction backprop?
Tensor<D>& O = createTensor(LS);
O -= RS;
return O;
};
// tensor to be broadcast must be on the RHS, should that change?
Tensor<D>& operator+=(Tensor<D>& RS) {
Tensor<D>& LS = *this;
if (LS.Rows == RS.Rows && LS.Cols == RS.Cols) {
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
LS[R * LS.Cols + C] += RS[R * LS.Cols + C];
}
}
} else if (LS.Cols == RS.Cols && RS.Rows == 1) {
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
LS[R * LS.Cols + C] += RS[C];
}
}
} else if (LS.Rows == RS.Rows && RS.Cols == 1) {
for (int R = 0; R < LS.Rows; R++) {
for (int C = 0; C < LS.Cols; C++) {
LS[R * LS.Cols + C] += RS[R];
}
}
} else {
stringstream SS;
SS << "Dimensions of input tensors do not match & are "
"not broadcastable. The "
"dimension to be broadcast must be of length 1. LS ("
<< LS.Rows << ", " << LS.Cols << ") RS (" << RS.Rows << ", " << RS.Cols
<< ")";
INVALID_ARGUMENT(SS.str());
}
return LS;
}
Tensor<D>& mm(Tensor<D>& R) {
Tensor<D>& L = *this;
if (L.Cols != R.Rows) {
stringstream SS;
SS << "Inner dimension of input tensors does not match. (" << L.Rows
<< "," << L.Cols << ") (" << R.Rows << "," << R.Cols << ")" << endl;
INVALID_ARGUMENT(SS.str());
}
vector<Tensor<D>*> Parents = {&L, &R};
Tensor<D>& O =
createTensor(L.Rows, R.Cols, Parents, Tensor<D>::mmBackward, true);
for (int RW = 0; RW < L.Rows; RW++) {
for (int C = 0; C < R.Cols; C++) {
for (int I = 0; I < L.Cols; I++) {
O[RW * R.Cols + C] += L[RW * L.Cols + I] * R[C + R.Cols * I];
}
}
}
return O;
};
static Tensor<D>& createTensor(D Data) {
Tensor<D>& T = createTensor(1, 1);
T[0] = Data;
return T;
};
static Tensor<D>& createTensor(Tensor<D>& C) {
Memory& M = ProgramMemory;
Tensor<D>* TP = new (M.SP) Tensor<D>{};
M.SP += sizeof(*TP);
Tensor<D>& T = *TP;
T.Rows = C.Rows;
T.Cols = C.Cols;
T.Len = C.Rows * C.Cols;
T.Data = new (M.TP) float[T.Len];
M.TP += sizeof(D) * T.Len;
for (int I = 0; I < C.Len; I++) {
T[I] = C[I];
}
if (C.Grad != NULL) {
T.Grad = &createTensor(*C.Grad);
} else {
T.Grad = NULL;
}
if (C.BackProp != NULL) {
T.BackProp = C.BackProp;
}
return T;
};
static Tensor<D>& createTensor(size_t R, size_t C,
Tensor<D>& (*Generator)(Tensor<D>&) = NULL,
bool Gradient = false) {
Memory& M = ProgramMemory;
Tensor<D>* TP = new (M.SP) Tensor<D>{};
M.SP += sizeof(*TP);
Tensor<D>& T = *TP;
T.Rows = R;
T.Cols = C;
T.Len = R * C;
T.Data = new (M.TP) float[T.Len];
M.TP += sizeof(D) * T.Len;
if (Gradient) {
T.Grad = &createTensor(R, C, vector<Tensor<D>*>(), zeros);
} else {
T.Grad = NULL;
}
if (Generator != NULL) {
T = Generator(T);
}
return T;
};
static Tensor<D>& createTensor(size_t R, size_t C, vector<Tensor<D>*> Parents,
void (*BackProp)(Tensor<D>&) = NULL,
Tensor<D>& (*Generator)(Tensor<D>&) = NULL,
bool Gradient = false) {
// TODO: handle allocation of too much memory
Memory& M = ProgramMemory;
Tensor<D>* TP = new (M.SP) Tensor<D>{};
M.SP += sizeof(*TP);
Tensor<D>& T = *TP;
T.Rows = R;
T.Cols = C;
T.Len = R * C;
size_t DataSize = sizeof(D) * T.Len;
T.Data = new (M.TP) float[T.Len];
M.TP += DataSize;
T.Parents = Parents;
T.BackProp = BackProp;
if (Gradient) {
T.Grad = &createTensor(R, C, vector<Tensor<D>*>(), zeros);
} else {
T.Grad = NULL;
}
if (Generator != NULL) {
T = Generator(T);
}
return T;
};
static Tensor<D>& createTensor(size_t R, size_t C, vector<Tensor<D>*> Parents,
void (*BackProp)(Tensor<D>&) = NULL,
bool Gradient = false) {
// TODO: handle allocation of too much memory
Memory& M = ProgramMemory;
Tensor<D>* TP = new (M.SP) Tensor<D>{};
M.SP += sizeof(*TP);
Tensor<D>& T = *TP;
T.Rows = R;
T.Cols = C;
T.Len = R * C;
size_t DataSize = sizeof(D) * T.Len;
T.Data = new (M.TP) float[T.Len];
M.TP += DataSize;
T.Parents = Parents;
T.BackProp = BackProp;
if (Gradient) {
T.Grad = &createTensor(R, C, vector<Tensor<D>*>(), zeros);
} else {
T.Grad = NULL;
}
return T;
};
static Tensor<D>& createTensor(size_t R, size_t C, vector<Tensor<D>*> Parents,
Tensor<D>& (*Generator)(Tensor<D>&) = NULL,
void (*BackProp)(Tensor<D>&) = NULL,
bool Gradient = false) {
// TODO: handle allocation of too much memory
Memory& M = ProgramMemory;
Tensor<D>* TP = new (M.SP) Tensor<D>{};
M.SP += sizeof(*TP);
Tensor<D>& T = *TP;
T.Rows = R;
T.Cols = C;
T.Len = R * C;
size_t DataSize = sizeof(D) * T.Len;
T.Data = new (M.TP) float[T.Len];
M.TP += DataSize;
T.Parents = Parents;
T.BackProp = BackProp;
if (Gradient) {
T.Grad = &createTensor(R, C, vector<Tensor<D>*>(), zeros);
} else {
T.Grad = NULL;
}
if (Generator != NULL) {
T = Generator(T);
}
return T;
};
// TODO: improve random number generation quality
static Tensor<D>& normal(Tensor<D>& T) {
float L = -1.0f;
float R = 1.0f;
if ((long long)(L - R) - (L - R) != 0) {
throw invalid_argument(
"the range must have an integer length, e.g. 1.5 - .5 = 1");
}
for (int I = 0; I < T.Len; I++) {
T[I] = L + (rand() % (int)(R - L)) + (rand() / (D)RAND_MAX);
}
return T;
}
static Tensor<D>& ones(Tensor<D>& T) {
for (int I = 0; I < T.Len; I++) {
T[I] = 1.0f;
}
return T;
}
static Tensor<D>& zeros(Tensor<D>& T) {
for (int I = 0; I < T.Len; I++) {
T[I] = 0.0f;
}
return T;
}
};
int main() {
int Seed = SEED;
size_t HeapMemorySize = HEAP_MEMORY_SIZE;
size_t StackMemorySize = STACK_MEMORY_SIZE;
char StackBuffer[STACK_MEMORY_SIZE];
srand(Seed);
ProgramMemory =
initMemory(ProgramMemory, HeapMemorySize, StackMemorySize, StackBuffer);
Tensor<float> X;
Tensor<float> W;
Tensor<float> Logits;
Tensor<float> A;
Tensor<float> Targets;
Tensor<float> Loss;
X = Tensor<float>::createTensor(10, 2, Tensor<float>::normal, true);
W = Tensor<float>::createTensor(2, 10, Tensor<float>::normal, true);
Targets = Tensor<float>::createTensor(10, 10, Tensor<float>::ones, false);
Logits = X.mm(W);
A = Logits.tanH();
Loss = A.mse(Targets);
cout << "X:" << X << "\nW:" << W;
cout << "\nLogits:" << Logits;
cout << "\nActivations:" << A;
cout << "\nLoss:" << Loss << endl;
Loss.backward();
cout << "\nLoss.Grad:" << *Loss.Grad;
cout << "\nActivations.Grad:" << *A.Grad;
cout << "\nLogits.Grad:" << *Logits.Grad;
cout << "\nX.Grad:" << *X.Grad << "\nW.Grad:" << *W.Grad;
cleanMemory(ProgramMemory);
}