-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_mnist.c
1052 lines (940 loc) · 39.9 KB
/
train_mnist.c
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
#ifdef OMP
#include <omp.h>
#endif
#define MAX_STEPS 70
#define EVAL_STEPS 10
#define BATCH_SIZE 8
#define X_OFFSET 0x10
#define Y_OFFSET 8
#define IMAGE_SIZE 28
// UTILS
// returns an allocated array which must be freed
void *tensor_from_disk(const char *path, const size_t offset, const size_t item_size, size_t *len)
{
FILE *f = fopen(path, "rb"); // open file at path
fseek(f, 0L, SEEK_END); // get length
int f_size = ftell(f);
rewind(f); // go back to the beginning
*len = f_size - offset; // set read array length
char *arr = malloc(*len); // get some memory to store read bytes
fseek(f, offset, SEEK_SET); // seek to offset
fread(arr, 1, *len, f); // copy "length" bytes from file into the array
fclose(f); // close the file
*len /= item_size;
return arr;
}
float random_float()
{
return (float)rand() / (float)RAND_MAX;
}
void printn(const float *in, const size_t N)
{
printf("[");
for (int i = 0; i < N; i++)
{
printf("%f", in[i]);
if (i != N - 1)
{
printf(", ");
}
}
printf("]\n");
}
// modified from https://stackoverflow.com/questions/11641629/generating-a-uniform-distribution-of-integers-in-c
// guess I'll find out if it's a good enough uniform distribution we're sampling from
// update: I now know it's not (compared to pytorch initialized untrained model)
float uniform_distribution(float rangeLow, float rangeHigh) {
double myRand = rand()/(1.0 + RAND_MAX);
float range = rangeHigh - rangeLow + 1.0;
float myRand_scaled = (myRand * range) + rangeLow;
return myRand_scaled;
}
void fill_uniform(float *out, const float low, const float high, const int N) {
for (int i = 0; i < N; i++) {
out[i] = uniform_distribution(low, high);
}
}
// DATALOADER
struct DataLoader {
int batch_size;
float *inputs;
int *targets;
size_t len;
unsigned char *imgs;
unsigned char *labels;
};
void dataloader_init(struct DataLoader *dl, unsigned char *imgs, unsigned char *labels, const size_t len, const int B) {
dl->batch_size = B;
dl->inputs = (float *)malloc(B * IMAGE_SIZE * IMAGE_SIZE * sizeof(float));
dl->targets = (int *)malloc(B * sizeof(int));
dl->imgs = imgs;
dl->labels = labels;
dl->len = len;
}
void dataloader_next_batch(struct DataLoader *self) {
for (int i = 0; i < self->batch_size; i++) {
int idx = random_float() * self->len;
for (int j = 0; j < IMAGE_SIZE * IMAGE_SIZE; j++) {
self->inputs[i * IMAGE_SIZE * IMAGE_SIZE + j] = (float)self->imgs[idx * IMAGE_SIZE * IMAGE_SIZE + j];
}
self->targets[i] = self->labels[idx];
}
}
void dataloader_free(struct DataLoader *self) {
free(self->inputs);
free(self->targets);
}
// OPS
void _conv2d_forward(
// out_H = H - K_H + 1
// out_W = W - K_W + 1
float *out, // (B, K_C, out_H, out_W)
const float *in, // (B, C, H, W)
const float *kernels, // (K_C, C, K_H, K_W)
const float *bias, // (K_C)
const int B, const int C, const int H, const int W,
const int K_C, const int K_H, const int K_W,
const bool flip_kernels // should be false by default
)
{
int out_H = H - K_H + 1;
int out_W = W - K_W + 1;
#pragma omp parallel for
for (int b = 0; b < B; b++)
{
for (int k_c = 0; k_c < K_C; k_c++)
{
// correlation
for (int j = 0; j < out_H; j++)
{
for (int i = 0; i < out_W; i++)
{
float channeled_correlation_sum = 0.0;
for (int c = 0; c < C; c++)
{
// element wise multiplication
for (int k_j = 0; k_j < K_H; k_j++)
{
for (int k_i = 0; k_i < K_W; k_i++)
{
float a = in[(b * C * H * W) + (c * H * W) + ((j + k_j) * W) + (i + k_i)];
int k_j_maybe_t = (flip_kernels) ? (K_H - k_j - 1) : k_j;
int k_i_maybe_t = (flip_kernels) ? (K_W - k_i - 1) : k_i;
float b = kernels[(k_c * C * K_H * K_W) + (c * K_H * K_W) + (k_j_maybe_t * K_W) + k_i_maybe_t];
channeled_correlation_sum += a * b;
}
}
}
out[(b * K_C * out_H * out_W) + (k_c * out_H * out_W) + (j * out_W) + i] = channeled_correlation_sum;
}
}
}
}
}
void conv2d_forward(
// out_H = H - K_H + 1
// out_W = W - K_W + 1
float *out, // (B, K_C, out_H, out_W)
const float *in, // (B, C, H, W)
const float *kernels, // (K_C, C, K_H, K_W)
const float *bias, // (K_C)
const int B, const int C, const int H, const int W,
const int K_C, const int K_H, const int K_W
) {
_conv2d_forward(out, in, kernels, bias, B, C, H, W, K_C, K_H, K_W, false);
}
void conv2d_backward(
// out_H = H - K_H + 1
// out_W = W - K_W + 1
float *din, // (B, C, H, W)
float *dkernels, // (K_C, C, K_H, K_W)
float *dbias, // (K_C)
const float *dout, // (B, K_C, out_H, out_W)
const float *in, // (B, C, H, W)
const float *kernels, // (K_C, C, K_H, K_W)
const float *bias, // (K_C)
const int B, const int C, const int H, const int W,
const int K_C, const int K_H, const int K_W)
{
int out_H = H - K_H + 1;
int out_W = W - K_W + 1;
// din = conv2dfull(in, rot180(dout))
if (din != NULL) {
// _conv2d_forward(din, in, dout, NULL, B, C, H, W, K_C, K_H, K_W, true);
int pad_h = (K_H - 1);
int pad_w = (K_W - 1);
float * doutp = calloc(B * K_C * (out_H + pad_h * 2) * (out_W + pad_w * 2), sizeof(float));
for(int b = 0; b < B; b++) {
for (int k_c = 0; k_c < K_C; k_c++) {
for (int i = 0; i < out_H; i++) {
for (int j = 0; j < out_W; j++) {
doutp[(b * K_C * out_H * out_W) + (k_c * out_H * out_W) + ((i + pad_h) * out_W) + (j + pad_w)] = dout[(b * K_C * out_H * out_W) + (k_c * out_H * out_W) + (i * out_W) + j];
}
}
}
}
for (int b = 0; b < B; b++) {
for (int k_c = 0; k_c < K_C; k_c++) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
for (int k_h = 0; k_h < K_H; k_h++) {
for (int k_w = 0; k_w < K_W; k_w++) {
for (int c = 0; c < C; c++) {
// if ((i + k_h - pad_h) > pad_h && (i + k_h - pad_h) < H - pad_h
// && (j + k_w - pad_w) > pad_w && (j + k_w - pad_w) < W - pad_w) {
// din[(b * C * H * W) + (c * H * W) + (i * W) + j] = dout[(b * K_C * out_H * out_W) + (k_c * out_H * out_W) + ((i + k_h - pad_h) * out_W) + (j + k_w - pad_w)]
// * kernels[(k_c * C * K_H * K_W) + (c * K_H * K_W) + ((K_H - k_h - 1) * K_W) + (K_W - k_w - 1)];
// }
din[(b * C * H * W) + (c * H * W) + (i * W) + j] += doutp[(b * K_C * out_H * out_W) + (k_c * out_H * out_W) + ((i + k_h) * out_W) + (j + k_w)]
* kernels[(k_c * C * K_H * K_W) + (c * K_H * K_W) + ((K_H - k_h - 1) * K_W) + (K_W - k_w - 1)];
}
}
}
}
}
}
}
free(doutp);
}
// dkernels = sum(conv2d(in, dout), dim=0)
// for n in range(N): # On parcourt toutes les images
// for f in range(F): # On parcourt tous les filtres
// for i in range(HH): # indices du résultat
// for j in range(WW):
// for k in range(H_): # indices du filtre
// for l in range(W_):
// for c in range(C): # profondeur
// dw[f,c,i,j] += xp[n, c, stride*i+k, stride*j+l] * dout[n, f, k, l]
for (int b = 0; b < B; b++) {
for (int k_c = 0; k_c < K_C; k_c++) {
for (int k_h = 0; k_h < K_H; k_h++) {
for (int k_w = 0; k_w < K_W; k_w++) {
for (int i = 0; i < out_H; i++) {
for (int j = 0; j < out_W; j++) {
for (int c = 0; c < C; c++) {
dkernels[(k_c * C * K_H * K_W) + (c * K_H * K_W) + (k_h * K_W) + k_w] += in[(b * C * H * W) + (c * H * W) + ((i + k_h) * W) + (j + k_w)]
* dout[(b * K_C * out_H * out_W) + (k_c * out_H * out_W) + (i * out_W) + j];
}
}
}
}
}
}
}
// dbias[k_c] = sum(dout[b][k_c][out_h][out_w]) (sum on all axis except filters)
for (int k_c = 0; k_c < K_C; k_c++) {
for (int b = 0; b < B; b++) {
for (int j = 0; j < out_H; j++) {
for (int i = 0; i < out_W; i++) {
dbias[k_c] += dout[(b * K_C * out_H * out_W) + (k_c * out_H * out_W) + (j * out_W) + i];
}
}
}
}
}
void relu_forward(float *out, const float *in, const size_t N)
{
for (size_t i = 0; i < N; i++)
{
float tmp = in[i];
out[i] = (tmp > 0) ? tmp : 0;
}
}
void relu_backward(float *din, const float *dout, const float *in, const size_t N) {
for (size_t i = 0; i < N; i++) {
din[i] = (in[i] > 0.0) * dout[i];
}
}
void maxpool2d_forward(
// we are using stride = kernel size here. e.g: (2, 2) kernel => (2, 2) stride
// out_H = H / K_H
// out_W = W / K_W
float *out, // (B, C, out_H, out_W)
const float *in, // (B, C, H, W)
const int B, const int C, const int H, const int W,
const int K_W, const int K_H)
{
int out_H = H / K_H;
int out_W = W / K_W;
#pragma omp parallel for
for (int b = 0; b < B; b++)
{
for (int c = 0; c < C; c++)
{
for (int j = 0; j < out_H; j++)
{
for (int i = 0; i < out_W; i++)
{
float max = in[(b * C * H * W) + (c * H * W) + (j * K_H * W) + (i * K_W)]; // init to first or NEG_INF ?
for (int k_j = 0; k_j < K_H; k_j++)
{
for (int k_i = 0; k_i < K_H; k_i++)
{
float v = in[(b * C * H * W) + (c * H * W) + ((j * K_H + k_j) * W) + (i * K_W + k_i)];
if (v > max)
{
max = v;
}
}
}
out[(b * C * out_H * out_W) + (c * out_H * out_W) + (j * out_W) + i] = max;
}
}
}
}
}
void maxpool2d_backward(
// we are using stride = kernel size here. e.g: (2, 2) kernel => (2, 2) stride
// out_H = H / K_H
// out_W = W / K_W
float *din, // (B, C, H, W)
const float *dout, // (B, C, out_H, out_W)
const float *in, // (B, C, H, W)
const int B, const int C, const int H, const int W,
const int K_W, const int K_H)
{
int out_H = H / K_H;
int out_W = W / K_W;
// here, "din" index which has the max in "in" gets assigned the gradient
#pragma omp parallel for
for (int b = 0; b < B; b++)
{
for (int c = 0; c < C; c++)
{
for (int j = 0; j < out_H; j++)
{
for (int i = 0; i < out_W; i++)
{
int argmax = (b * C * H * W) + (c * H * W) + (j * K_H * W) + (i * K_W); // init to first or NEG_INF ?
for (int k_j = 0; k_j < K_H; k_j++)
{
for (int k_i = 0; k_i < K_H; k_i++)
{
int v_i = (b * C * H * W) + (c * H * W) + ((j * K_H + k_j) * W) + (i * K_W + k_i);
if (in[v_i] > in[argmax])
{
argmax = v_i;
}
}
}
din[argmax] = dout[(b * C * out_H * out_W) + (c * out_H * out_W) + (j * out_W) + i];
}
}
}
}
}
// out = x @ weight.T + bias
void linear_forward(
float *out, // (B, out_features)
const float *x, // (B, in_features)
const float *weight, // (out_features, in_features)
const float *bias, // (out_features)
const int B, const int in_features, const int out_features)
{
#pragma omp parallel for
for (int b = 0; b < B; b++)
{
for (int i = 0; i < out_features; i++)
{
float acc = (bias != NULL) ? bias[i] : 0.0;
for (int k = 0; k < in_features; k++)
{
acc += x[(b * in_features) + k] * weight[i * in_features + k];
}
out[b * out_features + i] = acc;
}
}
}
void linear_backward(
float *din, // (B, in_features)
float *dweight, // (out_features, in_features)
float *dbias, // (out_features)
const float *dout, // (B, out_features)
const float *in, // (B, in_features)
const float *weight, // (out_features, in_features)
const int B, const int in_features, const int out_features)
{
// din = dout @ weight
#pragma omp parallel for
for (int b = 0; b < B; b++) {
for (int i = 0; i < in_features; i++) {
float acc = 0.0;
for (int o = 0; o < out_features; o++) {
acc += dout[b * out_features + o] * weight[o * in_features + i];
}
din[b * out_features + i] = acc;
}
}
// dweight = dout.T @ in
#pragma omp parallel for
for (int o = 0; o < out_features; o++) {
for (int i = 0; i < in_features; i++) {
float acc = 0.0;
for (int b = 0; b < B; b++) {
acc += dout[b * out_features + o] * in[b * in_features + i];
}
dweight[o * in_features + i] = acc;
}
}
// dbias = sum(dout, axis=1) // sum on the out_features axis
#pragma omp parallel for
for (int b = 0; b < B; b++)
{
for (int o = 0; o < out_features; o++)
{
dbias[o] += dout[b * out_features + o];
}
}
}
void argmax_forward(
int *out, // (B,)
const float *in, // (B, N)
const int B, const int N)
{
for (int b = 0; b < B; b++)
{
int argmax = 0;
float max = in[b * N];
for (int i = 0; i < N; i++)
{
float tmp = in[b * N + i];
if (tmp > max)
{
argmax = i;
max = tmp;
}
}
out[b] = argmax;
}
}
void softmax_forward(
float *probs, // (B, C)
const float *logits, // (B, C)
const int B, const int C // C is for classes
) {
// for each batch
// probs = exp(x) / sum(exp(x))
for (int b = 0; b < B; b++) {
float *probs_b = probs + b * C;
const float *logits_b = logits + b * C;
// maxval is only calculated and subtracted for numerical stability
float maxval = -10000.0f; // TODO something better
for (int i = 0; i < C; i++) {
if (logits_b[i] > maxval) {
maxval = logits_b[i];
}
}
float sum = 0.0f;
for (int i = 0; i < C; i++) {
probs_b[i] = expf(logits_b[i] - maxval);
sum += probs_b[i];
}
for (int i = 0; i < C; i++) {
probs_b[i] /= sum;
}
}
}
// computes the mean loss over the batch
void sparse_categorical_crossentropy_forward(
float *losses, // (B,)
const float *probs, // (B, C)
const int *targets, // (B,)
const int B, const int C // C is for classes
) {
for (int b = 0; b < B; b++) {
int target_class = targets[b];
losses[b] = -logf(probs[b * C + target_class]);
}
}
void sparse_categorical_crossentropy_softmax_backward(
float* dlogits, // (B,C)
const float* dlosses, // (B,)
const float* probs, // (B,C)
const int* targets, // (B,)
const int B, const int C // C is for classes
) {
// backwards through both softmax and crossentropy
for (int b = 0; b < B; b++) {
float* dlogits_b = dlogits + b * C;
const float* probs_b = probs + b * C;
float dloss = dlosses[b]; // dloss for this batch index
int ix = targets[b]; // target class for this batch index
for (int i = 0; i < C; i++) {
float p = probs_b[i];
float indicator = i == ix ? 1.0f : 0.0f;
dlogits_b[i] += (p - indicator) * dloss;
}
}
}
// ----------------------------------------------------------------------------
// Mnist model definition
// C = input channels
// OC = output channels
// KS = kernel size
// OS = output size
#define CONV2D_1_C 1 // conv1
#define CONV2D_1_OC 32
#define CONV2D_1_KS 5
#define CONV2D_1_OS (IMAGE_SIZE - CONV2D_1_KS + 1)
#define CONV2D_2_C 32 // conv2
#define CONV2D_2_OC 32
#define CONV2D_2_KS 5
#define CONV2D_2_OS (CONV2D_1_OS - CONV2D_2_KS + 1)
#define MAXPOOL2D_1_KS 2 // maxpool1
#define MAXPOOL2D_1_OS (CONV2D_2_OS / MAXPOOL2D_1_KS)
#define CONV2D_3_C 32 // conv3
#define CONV2D_3_OC 64
#define CONV2D_3_KS 3
#define CONV2D_3_OS (MAXPOOL2D_1_OS - CONV2D_3_KS + 1)
#define CONV2D_4_C 64 // conv4
#define CONV2D_4_OC 64
#define CONV2D_4_KS 3
#define CONV2D_4_OS (CONV2D_3_OS - CONV2D_4_KS + 1)
#define MAXPOOL2D_2_KS 2 // maxpool1
#define MAXPOOL2D_2_OS (CONV2D_4_OS / MAXPOOL2D_2_KS)
#define LINEAR_1_IF 576 // linear
#define LINEAR_1_OF 10
// the parameters of the model
#define NUM_PARAMETER_TENSORS 10
struct ParameterTensors
{
float *conv1w; // (CONV2D_1_OC, CONV2D_1_C, CONV2D_1_KS, CONV2D_1_KS)
float *conv1b; // (CONV2D_1_OC)
float *conv2w; // (CONV2D_2_OC, CONV2D_2_C, CONV2D_2_KS, CONV2D_2_KS)
float *conv2b; // (CONV2D_2_OC)
float *conv3w; // (CONV2D_3_OC, CONV2D_3_C, CONV2D_3_KS, CONV2D_3_KS)
float *conv3b; // (CONV2D_3_OC)
float *conv4w; // (CONV2D_4_OC, CONV2D_4_C, CONV2D_4_KS, CONV2D_4_KS)
float *conv4b; // (CONV2D_4_OC)
float *linear1w; // (LINEAR_1_OF, LINEAR_1_IF)
float *linear1b; // (LINEAR_1_OF)
};
void fill_in_parameter_sizes(size_t *param_sizes)
{
param_sizes[0] = CONV2D_1_OC * CONV2D_1_C * CONV2D_1_KS * CONV2D_1_KS; // conv1w
param_sizes[1] = CONV2D_1_OC; // conv1b
param_sizes[2] = CONV2D_2_OC * CONV2D_2_C * CONV2D_2_KS * CONV2D_2_KS; // conv2w
param_sizes[3] = CONV2D_2_OC; // conv2b
param_sizes[4] = CONV2D_3_OC * CONV2D_3_C * CONV2D_3_KS * CONV2D_3_KS; // conv3w
param_sizes[5] = CONV2D_3_OC; // conv3b
param_sizes[6] = CONV2D_4_OC * CONV2D_4_C * CONV2D_4_KS * CONV2D_4_KS; // conv4w
param_sizes[7] = CONV2D_4_OC; // conv4b
param_sizes[8] = LINEAR_1_OF * LINEAR_1_IF; // linear1w
param_sizes[9] = LINEAR_1_OF; // linear1b
}
// allocate memory for the parameters and point the individual tensors to the right places
float *malloc_and_point_parameters(struct ParameterTensors *params, size_t *param_sizes)
{
size_t num_parameters = 0;
for (size_t i = 0; i < NUM_PARAMETER_TENSORS; i++)
{
num_parameters += param_sizes[i];
}
// malloc all parameters all at once
float *params_memory = (float *)malloc(num_parameters * sizeof(float));
// assign all the tensors
float **ptrs[] = {
¶ms->conv1w, ¶ms->conv1b,
¶ms->conv2w, ¶ms->conv2b,
¶ms->conv3w, ¶ms->conv3b,
¶ms->conv4w, ¶ms->conv4b,
¶ms->linear1w, ¶ms->linear1b};
float *params_memory_iterator = params_memory;
for (size_t i = 0; i < NUM_PARAMETER_TENSORS; i++)
{
*(ptrs[i]) = params_memory_iterator;
params_memory_iterator += param_sizes[i];
}
return params_memory;
}
#define NUM_ACTIVATION_TENSORS 13
struct ActivationTensors
{
float *conv2d_1; // (B, CONV2D_1_OC, CONV2D_1_OS, CONV2D_1_OS)
float *conv2d_1_relu; // (B, CONV2D_1_OC, CONV2D_1_OS, CONV2D_1_OS)
float *conv2d_2; // (B, CONV2D_2_OC, CONV2D_2_OS, CONV2D_2_OS)
float *conv2d_2_relu; // (B, CONV2D_2_OC, CONV2D_2_OS, CONV2D_2_OS)
float *maxpool2d_1; // (B, CONV2D_2_OC, MAXPOOL2D_1_OS, MAXPOOL2D_1_OS)
float *conv2d_3; // (B, CONV2D_3_OC, CONV2D_3_OS, CONV2D_3_OS1)
float *conv2d_3_relu; // (B, CONV2D_3_OC, CONV2D_3_OS, CONV2D_3_OS1)
float *conv2d_4; // (B, CONV2D_4_OC, CONV2D_4_OS, CONV2D_4_OS)
float *conv2d_4_relu; // (B, CONV2D_4_OC, CONV2D_4_OS, CONV2D_4_OS)
float *maxpool2d_2; // (B, CONV2D_4_OC, MAXPOOL2D_2_OS, MAXPOOL2D_2_OS)
float *linear_1; // (B, LINEAR_1_OF)
float *probs; // (B, LINEAR_1_OF)
float* losses; // (B,)
};
void fill_in_activation_sizes(size_t *act_sizes, int B)
{
act_sizes[0] = B * CONV2D_1_OC * CONV2D_1_OS * CONV2D_1_OS; // conv1
act_sizes[1] = B * CONV2D_1_OC * CONV2D_1_OS * CONV2D_1_OS; // conv1 relu
act_sizes[2] = B * CONV2D_2_OC * CONV2D_2_OS * CONV2D_2_OS; // conv2
act_sizes[3] = B * CONV2D_2_OC * CONV2D_2_OS * CONV2D_2_OS; // conv2 relu
act_sizes[4] = B * CONV2D_2_OC * MAXPOOL2D_1_OS * MAXPOOL2D_1_OS; // maxpool1
act_sizes[5] = B * CONV2D_3_OC * CONV2D_3_OS * CONV2D_3_OS; // conv3
act_sizes[6] = B * CONV2D_3_OC * CONV2D_3_OS * CONV2D_3_OS; // conv3 relu
act_sizes[7] = B * CONV2D_4_OC * CONV2D_4_OS * CONV2D_4_OS; // conv4
act_sizes[8] = B * CONV2D_4_OC * CONV2D_4_OS * CONV2D_4_OS; // conv4 relu
act_sizes[9] = B * CONV2D_4_OC * MAXPOOL2D_2_OS * MAXPOOL2D_2_OS; // maxpool2
act_sizes[10] = B * LINEAR_1_OF; // linear
act_sizes[11] = B * LINEAR_1_OF; // softmax
act_sizes[12] = B; // losses
}
float *malloc_and_point_activations(struct ActivationTensors *acts, size_t *act_sizes)
{
size_t num_activations = 0;
for (size_t i = 0; i < NUM_ACTIVATION_TENSORS; i++)
{
num_activations += act_sizes[i];
}
float *acts_memory = (float *)malloc(num_activations * sizeof(float));
float **ptrs[] = {
&acts->conv2d_1,
&acts->conv2d_1_relu,
&acts->conv2d_2,
&acts->conv2d_2_relu,
&acts->maxpool2d_1,
&acts->conv2d_3,
&acts->conv2d_3_relu,
&acts->conv2d_4,
&acts->conv2d_4_relu,
&acts->maxpool2d_2,
&acts->linear_1,
&acts->probs,
&acts->losses
};
float *acts_memory_iterator = acts_memory;
for (size_t i = 0; i < NUM_ACTIVATION_TENSORS; i++)
{
*(ptrs[i]) = acts_memory_iterator;
acts_memory_iterator += act_sizes[i];
}
return acts_memory;
}
struct Model {
// the weights (parameters) of the model, and their sizes
struct ParameterTensors params;
size_t param_sizes[NUM_PARAMETER_TENSORS];
float* params_memory;
size_t num_parameters;
// gradients of the weights
struct ParameterTensors grads;
float* grads_memory;
// buffers for the AdamW optimizer
float* m_memory;
float* v_memory;
// the activations of the model, and their sizes
struct ActivationTensors acts;
size_t act_sizes[NUM_ACTIVATION_TENSORS];
float* acts_memory;
size_t num_activations;
// gradients of the activations
struct ActivationTensors grads_acts;
float* grads_acts_memory;
// other run state configuration
int batch_size; // the batch size (B) of current forward pass
float* inputs; // the input images for the current forward pass
int* targets; // the target labels for the current forward pass
float mean_loss; // after a forward pass with targets, will be populated with the mean loss
};
void model_forward(struct Model *model, const float *inputs, const int* targets, const int B) {
// targets are optional and could be NULL
// ensure the model was initialized or error out
if (model->params_memory == NULL) {
printf("Error: model was not initialized properly.\n");
exit(1);
}
// allocate space for all the activations if needed (done here, lazily)
if(model->acts_memory == NULL) {
// record the current B,T as well
model->batch_size = B;
// and now allocate the space
fill_in_activation_sizes(model->act_sizes, B);
size_t num_activations = 0;
for (size_t i = 0; i < NUM_ACTIVATION_TENSORS; i++) {
num_activations += model->act_sizes[i];
}
printf("num_activations: %zu\n", num_activations);
model->num_activations = num_activations;
model->acts_memory = malloc_and_point_activations(&model->acts, model->act_sizes);
// also create memory for caching inputs and targets
model->inputs = (float*)malloc(B * sizeof(float));
model->targets = (int*)malloc(B * sizeof(int)); // might be unused if we never have targets but it's small
} else {
// validate B,T is consistent with how we've allocated the memory before
// in principle we could get more clever here in the future, for now this is safest
if (B != model->batch_size) {
printf("Model: B=%d, Desired: B=%d\n", model->batch_size, (int)B);
exit(EXIT_FAILURE);
}
}
// cache the inputs/targets
memcpy(model->inputs, inputs, B * sizeof(float));
if (targets != NULL) {
memcpy(model->targets, targets, B * sizeof(int));
}
// forward pass
struct ParameterTensors params = model->params; // for brevity
struct ActivationTensors acts = model->acts;
conv2d_forward(acts.conv2d_1, inputs, params.conv1w, params.conv1b, B, CONV2D_1_C, IMAGE_SIZE, IMAGE_SIZE, CONV2D_1_OC, CONV2D_1_KS, CONV2D_1_KS);
relu_forward(acts.conv2d_1_relu, acts.conv2d_1, B * CONV2D_1_OC * CONV2D_1_OS * CONV2D_1_OS);
conv2d_forward(acts.conv2d_2, acts.conv2d_1_relu, params.conv2w, params.conv2b, B, CONV2D_2_C, CONV2D_1_OS, CONV2D_1_OS, CONV2D_2_OC, CONV2D_2_KS, CONV2D_2_KS);
relu_forward(acts.conv2d_2_relu, acts.conv2d_2, B * CONV2D_2_OC * CONV2D_2_OS * CONV2D_2_OS);
maxpool2d_forward(acts.maxpool2d_1, acts.conv2d_2_relu, B, CONV2D_2_OC, CONV2D_2_OS, CONV2D_2_OS, MAXPOOL2D_1_KS, MAXPOOL2D_1_KS);
conv2d_forward(acts.conv2d_3, acts.maxpool2d_1, params.conv3w, params.conv3b, B, CONV2D_3_C, MAXPOOL2D_1_OS, MAXPOOL2D_1_OS, CONV2D_3_OC, CONV2D_3_KS, CONV2D_3_KS);
relu_forward(acts.conv2d_3_relu, acts.conv2d_3, B * CONV2D_3_OC * CONV2D_3_OS * CONV2D_3_OS);
conv2d_forward(acts.conv2d_4, acts.conv2d_3_relu, params.conv4w, params.conv4b, B, CONV2D_4_C, CONV2D_3_OS, CONV2D_3_OS, CONV2D_4_OC, CONV2D_4_KS, CONV2D_4_KS);
relu_forward(acts.conv2d_4_relu, acts.conv2d_4, B * CONV2D_4_OC * CONV2D_4_OS * CONV2D_4_OS);
maxpool2d_forward(acts.maxpool2d_2, acts.conv2d_4_relu, B, CONV2D_4_OC, CONV2D_4_OS, CONV2D_4_OS, MAXPOOL2D_2_KS, MAXPOOL2D_2_KS);
linear_forward(acts.linear_1, acts.maxpool2d_2, params.linear1w, params.linear1b, B, LINEAR_1_IF, LINEAR_1_OF);
softmax_forward(acts.probs, acts.linear_1, B, LINEAR_1_OF);
// also forward the cross-entropy loss function if we have the targets
if (targets != NULL) {
sparse_categorical_crossentropy_forward(model->acts.losses, acts.probs, targets, B, LINEAR_1_OF);
// for convenience also evaluate the mean loss
float mean_loss = 0.0f;
for (int i=0; i<B; i++) { mean_loss += model->acts.losses[i]; }
mean_loss /= B;
model->mean_loss = mean_loss;
} else {
// if we don't have targets, we don't have a loss
model->mean_loss = -1.0f;
}
}
void model_zero_grad(struct Model *model) {
if(model->grads_memory != NULL) { memset(model->grads_memory, 0, model->num_parameters * sizeof(float)); }
if(model->grads_acts_memory != NULL) { memset(model->grads_acts_memory, 0, model->num_activations * sizeof(float)); }
}
void model_backward(struct Model *model) {
// double check we forwarded previously, with targets
if (model->mean_loss == -1.0f) {
printf("Error: must forward with targets before backward\n");
exit(1);
}
// lazily allocate the memory for gradients of the weights and activations, if needed
if (model->grads_memory == NULL) {
model->grads_memory = malloc_and_point_parameters(&model->grads, model->param_sizes);
model->grads_acts_memory = malloc_and_point_activations(&model->grads_acts, model->act_sizes);
model_zero_grad(model);
}
// convenience shortcuts (and size_t to help prevent int overflow)
size_t B = model->batch_size;
// backward pass: go in the reverse order of the forward pass, and call backward() functions
struct ParameterTensors params = model->params; // for brevity
struct ParameterTensors grads = model->grads;
struct ActivationTensors acts = model->acts;
struct ActivationTensors grads_acts = model->grads_acts;
// we kick off the chain rule by filling in dlosses with 1.0f/(B)
// technically this is a small, inline backward() pass of calculating
// total, final loss as the mean over all losses over all (B,) positions in the batch
float dloss_mean = 1.0f / B;
for (int i = 0; i < B; i++) { grads_acts.losses[i] = dloss_mean; }
sparse_categorical_crossentropy_softmax_backward(grads_acts.linear_1, grads_acts.losses, acts.probs, model->targets, B, LINEAR_1_OF);
linear_backward(grads_acts.maxpool2d_2, grads.linear1w, grads.linear1b, grads_acts.linear_1, acts.maxpool2d_2, params.linear1w, B, LINEAR_1_IF, LINEAR_1_OF);
maxpool2d_backward(grads_acts.conv2d_4_relu, grads_acts.maxpool2d_2, acts.conv2d_4_relu, B, CONV2D_4_OC, CONV2D_4_OS, CONV2D_4_OS, MAXPOOL2D_2_KS, MAXPOOL2D_2_KS);
relu_backward(grads_acts.conv2d_4, grads_acts.conv2d_4_relu, acts.conv2d_4, B * CONV2D_4_OC * CONV2D_4_OS * CONV2D_4_OS);
conv2d_backward(grads_acts.conv2d_3_relu, grads.conv4w, grads.conv4b, grads_acts.conv2d_4, acts.conv2d_3_relu, params.conv4w, params.conv4b, B, CONV2D_4_C, CONV2D_3_OS, CONV2D_3_OS, CONV2D_4_OC, CONV2D_4_KS, CONV2D_4_KS);
relu_backward(grads_acts.conv2d_3, grads_acts.conv2d_3_relu, acts.conv2d_3, B * CONV2D_3_OC * CONV2D_3_OS * CONV2D_3_OS);
conv2d_backward(grads_acts.maxpool2d_1, grads.conv3w, grads.conv3b, grads_acts.conv2d_3, acts.maxpool2d_1, params.conv3w, params.conv3b, B, CONV2D_3_C, MAXPOOL2D_1_OS, MAXPOOL2D_1_OS, CONV2D_3_OC, CONV2D_3_KS, CONV2D_3_KS);
maxpool2d_backward(grads_acts.conv2d_2_relu, grads_acts.maxpool2d_1, acts.conv2d_2_relu, B, CONV2D_2_OC, CONV2D_2_OS, CONV2D_2_OS, MAXPOOL2D_1_KS, MAXPOOL2D_1_KS);
relu_backward(grads_acts.conv2d_2, grads_acts.conv2d_2_relu, acts.conv2d_2, B * CONV2D_2_OC * CONV2D_2_OS * CONV2D_2_OS);
conv2d_backward(grads_acts.conv2d_1_relu, grads.conv2w, grads.conv2b, grads_acts.conv2d_2, acts.conv2d_1_relu, params.conv2w, params.conv2b, B, CONV2D_2_C, CONV2D_1_OS, CONV2D_1_OS, CONV2D_2_OC, CONV2D_2_KS, CONV2D_2_KS);
relu_backward(grads_acts.conv2d_1, grads_acts.conv2d_1_relu, acts.conv2d_1, B * CONV2D_1_OC * CONV2D_1_OS * CONV2D_1_OS);
conv2d_backward(NULL, grads.conv1w, grads.conv1b, grads_acts.conv2d_1, model->inputs, params.conv1w, params.conv1b, B, CONV2D_1_C, IMAGE_SIZE, IMAGE_SIZE, CONV2D_1_OC, CONV2D_1_KS, CONV2D_1_KS);
}
void model_build_from_checkpoint(struct Model *model, const char* checkpoint_path) {
// read in model from a checkpoint file
FILE *model_file = fopen(checkpoint_path, "rb");
// allocate space for all the parameters and read them in
fill_in_parameter_sizes(model->param_sizes);
// count the number of parameters
size_t num_parameters = 0;
for (size_t i = 0; i < NUM_PARAMETER_TENSORS; i++) {
num_parameters += model->param_sizes[i];
}
printf("loaded num_parameters: %zu\n", num_parameters);
model->num_parameters = num_parameters;
// read in all the parameters from file
model->params_memory = malloc_and_point_parameters(&model->params, model->param_sizes);
fread(model->params_memory, sizeof(float), num_parameters, model_file);
fclose(model_file);
// other inits
model->acts_memory = NULL;
model->grads_memory = NULL;
model->m_memory = NULL;
model->v_memory = NULL;
model->grads_acts_memory = NULL;
model->inputs = NULL;
model->targets = NULL;
model->batch_size = 0;
model->mean_loss = -1.0f; // -1.0f will designate no loss
}
void model_build_init_weights(struct Model *model) {
// allocate space for all the parameters and read them in
fill_in_parameter_sizes(model->param_sizes);
// count the number of parameters
size_t num_parameters = 0;
for (size_t i = 0; i < NUM_PARAMETER_TENSORS; i++) {
num_parameters += model->param_sizes[i];
}
printf("loaded num_parameters: %zu\n", num_parameters);
model->num_parameters = num_parameters;
// read in all the parameters from file
model->params_memory = malloc_and_point_parameters(&model->params, model->param_sizes);
// init conv2d 1
{
float k = 1.0/(CONV2D_1_C * CONV2D_1_KS * CONV2D_1_KS);
float sqrt_k = sqrtf(k);
fill_uniform(model->params.conv1w, -sqrt_k, sqrt_k, CONV2D_1_OC * CONV2D_1_C * CONV2D_1_KS * CONV2D_1_KS);
fill_uniform(model->params.conv1b, -sqrt_k, sqrt_k, CONV2D_1_OC);
}
// init conv2d 2
{
float k = 1.0/(CONV2D_2_C * CONV2D_2_KS * CONV2D_2_KS);
float sqrt_k = sqrtf(k);
fill_uniform(model->params.conv1w, -sqrt_k, sqrt_k, CONV2D_2_OC * CONV2D_2_C * CONV2D_2_KS * CONV2D_2_KS);
fill_uniform(model->params.conv1b, -sqrt_k, sqrt_k, CONV2D_2_OC);
}
// init conv2d 3
{
float k = 1.0/(CONV2D_3_C * CONV2D_3_KS * CONV2D_3_KS);
float sqrt_k = sqrtf(k);
fill_uniform(model->params.conv1w, -sqrt_k, sqrt_k, CONV2D_3_OC * CONV2D_3_C * CONV2D_3_KS * CONV2D_3_KS);
fill_uniform(model->params.conv1b, -sqrt_k, sqrt_k, CONV2D_3_OC);
}
// init conv2d 4
{
float k = 1.0/(CONV2D_4_C * CONV2D_4_KS * CONV2D_4_KS);
float sqrt_k = sqrtf(k);
fill_uniform(model->params.conv1w, -sqrt_k, sqrt_k, CONV2D_4_OC * CONV2D_4_C * CONV2D_4_KS * CONV2D_4_KS);
fill_uniform(model->params.conv1b, -sqrt_k, sqrt_k, CONV2D_4_OC);
}
// init linear
{
float k = 1.0/LINEAR_1_IF;
float sqrt_k = sqrtf(k);
fill_uniform(model->params.linear1w, -sqrt_k, sqrt_k, LINEAR_1_OF * LINEAR_1_IF);
fill_uniform(model->params.linear1b, -sqrt_k, sqrt_k, LINEAR_1_OF);
}
// other inits
model->acts_memory = NULL;
model->grads_memory = NULL;
model->m_memory = NULL;
model->v_memory = NULL;
model->grads_acts_memory = NULL;
model->inputs = NULL;
model->targets = NULL;
model->batch_size = 0;
model->mean_loss = -1.0f; // -1.0f will designate no loss
}
void model_free(struct Model *model) {
free(model->params_memory);
free(model->grads_memory);
free(model->m_memory);
free(model->v_memory);
free(model->acts_memory);
free(model->grads_acts_memory);
free(model->inputs);
free(model->targets);
}
void model_update(struct Model *model, float learning_rate, float beta1, float beta2, float eps, float weight_decay, int t) {
// reference: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
// lazily allocate the memory for m_memory and v_memory
if (model->m_memory == NULL) {
model->m_memory = (float*)calloc(model->num_parameters, sizeof(float));
model->v_memory = (float*)calloc(model->num_parameters, sizeof(float));
}
for (size_t i = 0; i < model->num_parameters; i++) {
float param = model->params_memory[i];
float grad = model->grads_memory[i];
// update the first moment (momentum)
float m = beta1 * model->m_memory[i] + (1.0f - beta1) * grad;
// update the second moment (RMSprop)
float v = beta2 * model->v_memory[i] + (1.0f - beta2) * grad * grad;
// bias-correct both moments
float m_hat = m / (1.0f - powf(beta1, t));
float v_hat = v / (1.0f - powf(beta2, t));
// update
model->m_memory[i] = m;
model->v_memory[i] = v;
model->params_memory[i] -= learning_rate * (m_hat / (sqrtf(v_hat) + eps) + weight_decay * param);
}
}
// end model
int main()
{
struct Model model;
model_build_from_checkpoint(&model, "params.bin");
// model_build_init_weights(&model);
size_t X_train_len;
unsigned char *X_train = tensor_from_disk("./downloads/X_train.gunzip", X_OFFSET, sizeof(unsigned char), &X_train_len);
size_t Y_train_len;
unsigned char *Y_train = tensor_from_disk("./downloads/Y_train.gunzip", Y_OFFSET, sizeof(unsigned char), &Y_train_len);
size_t X_test_len;
unsigned char *X_test = tensor_from_disk("./downloads/X_test.gunzip", X_OFFSET, sizeof(unsigned char), &X_test_len);
size_t Y_test_len;
unsigned char *Y_test = tensor_from_disk("./downloads/Y_test.gunzip", Y_OFFSET, sizeof(unsigned char), &Y_test_len);
int train_len = X_train_len / (IMAGE_SIZE * IMAGE_SIZE);
assert(train_len == Y_train_len); // we should have as many images as labels
int test_len = X_test_len / (IMAGE_SIZE * IMAGE_SIZE);
assert(test_len == Y_test_len);
printf("train set size: %d | test set size: %d\n", train_len, test_len);
int B = BATCH_SIZE;
struct DataLoader train_loader, test_loader;
dataloader_init(&train_loader, X_train, Y_train, train_len, B);
dataloader_init(&test_loader, X_test, Y_test, test_len, B);