-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtf.c
1457 lines (1241 loc) · 53.2 KB
/
tf.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 <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define tf_validation 1
#define tf_enc_file_size 722883
#define tf_data_size_max 1000000
#define tf_safetensor_file_size 548105171
#define tf_safetensor_json_size 14283
#define tf_d_vocab 50257
#define tf_d_seq 1024
#define tf_d_model 768
#define tf_d_k 64
#define tf_n_heads 12
#define tf_n_layers 12
#define tf_rsqrt_d_k 0.125f
struct tf_decoder_item {
uint32_t offset;
uint32_t size;
};
struct tf_decoder {
struct tf_decoder_item items[tf_d_vocab];
char raw[tf_enc_file_size - tf_d_vocab * sizeof(struct tf_decoder_item)];
};
struct tf_parameters {
struct {
float* weight;
} wte;
struct {
float* weight;
} wpe;
struct {
struct {
float* bias;
float* weight;
} ln_1;
struct {
struct {
float* bias;
float* weight;
} c_attn;
struct {
float* bias;
float* weight;
} c_proj;
} attn;
struct {
float* bias;
float* weight;
} ln_2;
struct {
struct {
float* bias;
float* weight;
} c_fc;
struct {
float* bias;
float* weight;
} c_proj;
} mlp;
} h[12];
struct {
float* bias;
float* weight;
} ln_f;
};
struct tf_activations {
struct {
float out[tf_d_seq][tf_d_model];
} embedding;
struct {
struct {
float r_std[tf_d_seq];
float mean[tf_d_seq];
float out[tf_d_seq][tf_d_model];
} ln_1;
struct {
struct {
float out[tf_d_seq][3 * tf_d_model];
} c_attn;
struct {
float out[tf_n_heads][tf_d_seq][tf_d_seq];
} softmax;
struct {
float out[tf_d_seq][tf_d_model];
} z;
struct {
float out[tf_d_seq][tf_d_model];
} c_proj;
} attn;
struct {
float out[tf_d_seq][tf_d_model];
} res_1;
struct {
float r_std[tf_d_seq];
float mean[tf_d_seq];
float out[tf_d_seq][tf_d_model];
} ln_2;
struct {
struct {
float out[tf_d_seq][4 * tf_d_model];
} c_fc;
struct {
float out[tf_d_seq][4 * tf_d_model];
} gelu;
struct {
float out[tf_d_seq][tf_d_model];
} c_proj;
} mlp;
struct {
float out[tf_d_seq][tf_d_model];
} res_2;
} h[12];
struct {
float r_std[tf_d_seq];
float mean[tf_d_seq];
float out[tf_d_seq][tf_d_model];
} ln_f;
struct {
float out[tf_d_seq][tf_d_vocab];
} unembedding;
};
struct tf_gradients {
struct {
float weight[tf_d_vocab][tf_d_model];
} wte;
struct {
float weight[tf_d_seq][tf_d_model];
} wpe;
struct {
struct {
float weight[tf_d_model];
float bias[tf_d_model];
} ln_1;
struct {
struct {
float weight[tf_d_model][3 * tf_d_model];
float bias[3 * tf_d_model];
} c_attn;
struct {
float weight[tf_d_model][tf_d_model];
float bias[tf_d_model];
} c_proj;
} attn;
struct {
float weight[tf_d_model];
float bias[tf_d_model];
} ln_2;
struct {
struct {
float weight[tf_d_model][4 * tf_d_model];
float bias[4 * tf_d_model];
} c_fc;
struct {
float weight[4 * tf_d_model][tf_d_model];
float bias[tf_d_model];
} c_proj;
} mlp;
} h[12];
struct {
float weight[tf_d_model];
float bias[tf_d_model];
} ln_f;
};
struct tf_activations_back {
struct {
float out[tf_d_seq][tf_d_model];
} embedding;
struct {
float out[tf_d_seq][tf_d_model];
} ln_1;
struct {
struct {
float out[tf_d_seq][3 * tf_d_model];
} c_attn;
struct {
float out[tf_d_seq];
} softmax;
struct {
float out[tf_d_seq][tf_d_model];
} z;
} attn;
struct {
float in_2[tf_d_seq][tf_d_model];
float out[tf_d_seq][tf_d_model];
} res_1;
struct {
float out[tf_d_seq][tf_d_model];
} ln_2;
struct {
struct {
float out[tf_d_seq][4 * tf_d_model];
} c_fc;
struct {
float out[tf_d_seq][4 * tf_d_model];
} gelu;
} mlp;
struct {
float in_2[tf_d_seq][tf_d_model];
float out[tf_d_seq][tf_d_model];
} res_2;
struct {
float out[tf_d_seq][tf_d_model];
} ln_f;
struct {
float out[tf_d_seq][tf_d_vocab];
} unembedding;
};
struct tf_add {
const float* in_1;
const float* in_2;
float* out;
size_t count;
};
struct tf_fc {
const float* weight; // (v: in_count, u: out_count)
const float* bias; // (v: 1, u: out_count)
const float* in; // (v: sample_count, u: in_count)
float* out; // (v: sample_count, u: out_count)
size_t in_count;
size_t out_count;
size_t sample_count;
};
struct tf_fc_back {
const float* weight; // (v: in_count, u: out_count)
const float* in; // (v: sample_count, u: in_count)
const float* dL_dout; // (v: sample_count, u: out_count)
float* dL_dweight; // (v: in_count, u: out_count)
float* dL_dbias; // (v: 1, u: out_count)
float* dL_din; // (v: sample_count, u: in_count)
size_t in_count;
size_t out_count;
size_t sample_count;
};
struct tf_ln {
const float* weight; // (v: 1, u: in_count)
const float* bias; // (v: 1, u: in_count)
const float* in; // (v: sample_count, u: in_count)
float* r_std; // (v: sample_count, u: 1)
float* mean; // (v: sample_count, u: 1)
float* out; // (v: sample_count, u: in_count)
size_t in_count;
size_t sample_count;
};
struct tf_ln_back {
const float* weight; // (v: 1, u: in_count)
const float* in; // (v: sample_count, u: in_count)
const float* r_std; // (v: sample_count, u: 1)
const float* mean; // (v: sample_count, u: 1)
const float* dL_dout; // (v: sample_count, u: in_count)
float* dL_dweight; // (v: 1, u: in_count)
float* dL_dbias; // (v: 1, u: in_count)
float* dL_din; // (v: sample_count, u: in_count)
size_t in_count;
size_t sample_count;
};
struct tf_validation_time {
double t_start;
double t_last;
double embedding;
double ln_1;
struct {
double c_attn;
double z;
double c_proj;
} attn;
double res_1;
double ln_2;
struct {
double c_fc;
double gelu;
double c_proj;
} mlp;
double res_2;
double ln_f;
double unembedding;
double total;
};
static double tf_current_time(void) {
struct timespec t;
clock_gettime(CLOCK_REALTIME, &t);
return (double)t.tv_sec + (double)t.tv_nsec * 1e-9;
}
static void tf_validation_time(double* target, double* t_last) {
if (tf_validation) {
double t = tf_current_time();
*target += t - *t_last;
*t_last = t;
}
}
static void tf_validation_sum(float* in, size_t in_count, double expected_sum) {
if (tf_validation) {
float* in_end = in + in_count;
double sum = 0.0;
for (; in != in_end; in++) {
sum += (double)*in;
}
if (expected_sum != sum) {
fprintf(stderr, "expected: %.24f (%a), got %.24f (%a)\n", expected_sum, expected_sum, sum, sum);
abort();
}
}
}
static void tf_offset_size(const char* json_raw, const char* str, size_t* out_offset, size_t* out_size) {
char temp[32];
char* start = strstr(json_raw, str);
start = strstr(start, "data_offsets") + 15;
char* end = strstr(start, ",");
memcpy(temp, start, end - start);
temp[end - start] = 0;
size_t offset = (size_t)atoi(start);
start = end + 1;
end = strstr(start, "]");
memcpy(temp, start, end - start);
temp[end - start] = 0;
size_t offset_end = (size_t)atoi(temp);
size_t size = offset_end - offset;
*out_offset = offset;
*out_size = size;
}
static void tf_add(const struct tf_add* add) {
const float* in_1 = add->in_1;
const float* in_2 = add->in_2;
float* out = add->out;
float* out_end = out + add->count;
for (; out != out_end; out++, in_1++, in_2++) {
*out = *in_1 + *in_2;
}
}
static void tf_fc(const struct tf_fc* fc) {
for (size_t i = 0; i < fc->sample_count; i++) {
float* out = fc->out + i * fc->out_count;
float* out_end = out + fc->out_count;
float* out_reset = out;
const float* weight = fc->weight;
const float* weight_end = weight + fc->in_count * fc->out_count;
const float* in = fc->in + i * fc->in_count;
memcpy(out, fc->bias, fc->out_count * sizeof(float));
while (true) {
*out += *in * *weight;
out++;
weight++;
if (out == out_end) {
out = out_reset;
in++;
if (weight == weight_end) {
break;
}
}
}
}
}
static void tf_fc_back(const struct tf_fc_back* fc) {
for (size_t i = 0; i < fc->sample_count; i++) {
const float* dL_dout = fc->dL_dout + i * fc->out_count;
const float* dL_dout_end = dL_dout + fc->out_count;
const float* dL_dout_reset = dL_dout;
float* dL_dbias = fc->dL_dbias;
for (; dL_dout != dL_dout_end; dL_dbias++, dL_dout++) {
*dL_dbias += *dL_dout;
}
dL_dout = dL_dout_reset;
const float* in = fc->in + i * fc->in_count;
const float* in_end = in + fc->in_count;
float* dL_dweight = fc->dL_dweight;
const float* weight = fc->weight;
float* dL_din = fc->dL_din + i * fc->in_count;
float dot = 0.0f;
while (true) {
*dL_dweight += *dL_dout * *in;
dot += *dL_dout * *weight;
dL_dweight++;
dL_dout++;
weight++;
if (dL_dout == dL_dout_end) {
dL_dout = dL_dout_reset;
in++;
*dL_din = dot;
dot = 0.0f;
dL_din++;
if (in == in_end) {
break;
}
}
}
}
}
static void tf_ln(const struct tf_ln* ln) {
for (size_t i = 0; i < ln->sample_count; i++) {
const float* in = ln->in + i * ln->in_count;
const float* in_end = in + ln->in_count;
const float* in_reset = in;
float total = 0.0f;
for (; in != in_end; in++) {
total += *in;
}
float mean = total / (float)ln->in_count;
ln->mean[i] = mean;
float total_diff_squared = 0.0f;
for (in = in_reset; in != in_end; in++) {
float diff = (*in - mean);
total_diff_squared += diff * diff;
}
float variance = total_diff_squared / (float)ln->in_count;
float r_std = 1.0f / sqrtf(variance + 1e-5f);
ln->r_std[i] = r_std;
const float* weight = ln->weight;
const float* bias = ln->bias;
float* out = ln->out + i * ln->in_count;
for (in = in_reset; in != in_end; in++, weight++, bias++, out++) {
float n = (*in - mean) * r_std;
*out = n * *weight + *bias;
}
}
}
static void tf_ln_back(const struct tf_ln_back* ln) {
// L = s(y), s: everything after layernorm
// y = layernorm(weight, bias, x)
// layernorm(weight, bias, x) = in_norm * weight + bias
// in_norm = (x - mean) / var;
// dL/dweight = dL/dy * dy/dweight
// dy/dweight = in_norm
// dL/dbias = dL/dy * dy/dbias
// dy/dbias = 1
// dL/din_norm = dL/dy * dy/din_norm
// dy/din_norm = weight
for (size_t i = 0; i < ln->sample_count; i++) {
float r_std = ln->r_std[i];
float mean = ln->mean[i];
float mean_partial = 0.0f;
float var_partial = 0.0f;
const float* dL_dout = ln->dL_dout + i * ln->in_count;
const float* dL_dout_end = dL_dout + ln->in_count;
const float* weight = ln->weight;
const float* in = ln->in + i * ln->in_count;
const float* dL_dout_reset = dL_dout;
const float* weight_reset = weight;
const float* in_reset = in;
float* dL_dweight = ln->dL_dweight;
float* dL_dbias = ln->dL_dbias;
for (; dL_dout != dL_dout_end; dL_dout++, weight++, in++, dL_dweight++, dL_dbias++) {
float dL_din_norm = *dL_dout * *weight;
float in_norm = (*in - mean) * r_std;
*dL_dweight += *dL_dout * in_norm;
*dL_dbias += *dL_dout;
mean_partial += dL_din_norm;
var_partial += dL_din_norm * in_norm;
}
mean_partial /= ln->in_count;
var_partial /= ln->in_count;
dL_dout = dL_dout_reset;
weight = weight_reset;
in = in_reset;
float* dL_din = ln->dL_din + i * ln->in_count;
for (; dL_dout != dL_dout_end; dL_dout++, weight++, in++, dL_din++) {
float dL_din_norm = *dL_dout * *weight;
float in_norm = (*in - mean) * r_std;
float dL_din_ = 0.0f;
dL_din_ += dL_din_norm;
dL_din_ -= mean_partial;
dL_din_ -= var_partial * in_norm;
dL_din_ *= r_std;
*dL_din = dL_din_;
}
}
}
static void tf_process(struct tf_parameters* parameters, struct tf_activations* activations, uint16_t* input, size_t input_size, uint16_t* out_token, bool is_training, struct tf_gradients* gradients, struct tf_activations_back* activations_back, uint16_t* expected) {
struct tf_validation_time time = { 0 };
time.t_start = tf_current_time();
time.t_last = time.t_start;
for (size_t i = 0; i < input_size; i++) {
float* wte = parameters->wte.weight + input[i] * tf_d_model;
float* wte_end = wte + tf_d_model;
float* wpe = parameters->wpe.weight + i * tf_d_model;
float* out = (float*)activations->embedding.out + i * tf_d_model;
for (; wte != wte_end; wte++, wpe++, out++) {
*out = *wte + *wpe;
}
}
tf_validation_time(&time.embedding, &time.t_last);
tf_validation_sum((float*)activations->embedding.out, input_size * tf_d_model, -0x1.e86f2c2adep+4);
for (int layer_i = 0; layer_i < tf_n_layers; layer_i++) {
tf_ln(&(struct tf_ln) {
.weight = parameters->h[layer_i].ln_1.weight,
.bias = parameters->h[layer_i].ln_1.bias,
.in = layer_i == 0 ? (float*)activations->embedding.out : (float*)activations->h[layer_i - 1].res_2.out,
.r_std = (float*)activations->h[layer_i].ln_1.r_std,
.mean = (float*)activations->h[layer_i].ln_1.mean,
.out = (float*)activations->h[layer_i].ln_1.out,
.in_count = tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.ln_1, &time.t_last);
if (layer_i == 0) {
tf_validation_sum((float*)activations->h[layer_i].ln_1.out, input_size * tf_d_model, -0x1.4e34ee18da56ap+8);
}
tf_fc(&(struct tf_fc) {
.weight = parameters->h[layer_i].attn.c_attn.weight,
.bias = parameters->h[layer_i].attn.c_attn.bias,
.in = (float*)activations->h[layer_i].ln_1.out,
.out = (float*)activations->h[layer_i].attn.c_attn.out,
.in_count = tf_d_model,
.out_count = 3 * tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.attn.c_attn, &time.t_last);
if (layer_i == 0) {
tf_validation_sum((float*)activations->h[layer_i].attn.c_attn.out, input_size * 3 * tf_d_model, -0x1.9f967d2b7f151p+11);
}
memset(activations->h[layer_i].attn.z.out, 0, sizeof(activations->h[layer_i].attn.z.out));
for (size_t head_i = 0; head_i < tf_n_heads; head_i++) {
for (size_t q_i = 0; q_i < input_size; q_i++) {
float* softmax_out = activations->h[layer_i].attn.softmax.out[head_i][q_i];
float softmax_max = -INFINITY;
for (size_t k_i = 0; k_i <= q_i; k_i++) {
float* q = (float*)activations->h[layer_i].attn.c_attn.out + q_i * 3 * tf_d_model + head_i * tf_d_k;
float* q_end = q + tf_d_k;
float* k = (float*)activations->h[layer_i].attn.c_attn.out + k_i * 3 * tf_d_model + tf_d_model + head_i * tf_d_k;
float dot = 0.0f;
for (; q != q_end; q++, k++) {
dot += *q * *k;
}
dot *= tf_rsqrt_d_k;
softmax_out[k_i] = dot;
if (dot > softmax_max) {
softmax_max = dot;
}
}
float softmax_sum = 0.0f;
for (size_t k_i = 0; k_i <= q_i; k_i++) {
float softmax_exp = expf(softmax_out[k_i] - softmax_max);
softmax_sum += softmax_exp;
softmax_out[k_i] = softmax_exp;
}
float r_softmax_sum = 1.0f / softmax_sum;
for (size_t k_i = 0; k_i <= q_i; k_i++) {
softmax_out[k_i] *= r_softmax_sum;
}
for (size_t v_i = 0; v_i <= q_i; v_i++) {
float* v = (float*)activations->h[layer_i].attn.c_attn.out + v_i * 3 * tf_d_model + 2 * tf_d_model + head_i * tf_d_k;
float* v_end = v + tf_d_k;
float* z = (float*)activations->h[layer_i].attn.z.out + q_i * tf_d_model + head_i * tf_d_k;
float softmax_i = softmax_out[v_i];
for (; v != v_end; v++, z++) {
*z += softmax_i * *v;
}
}
}
}
tf_validation_time(&time.attn.z, &time.t_last);
if (layer_i == 0) {
tf_validation_sum((float*)activations->h[layer_i].attn.z.out, input_size * tf_d_model, 0x1.c64a4db1bfcdep+8);
}
tf_fc(&(struct tf_fc) {
.weight = parameters->h[layer_i].attn.c_proj.weight,
.bias = parameters->h[layer_i].attn.c_proj.bias,
.in = (float*)activations->h[layer_i].attn.z.out,
.out = (float*)activations->h[layer_i].attn.c_proj.out,
.in_count = tf_d_model,
.out_count = tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.attn.c_proj, &time.t_last);
if (layer_i == 0) {
tf_validation_sum((float*)activations->h[layer_i].attn.c_proj.out, input_size * tf_d_model, 0x1.850b3ffab297bp+8);
}
tf_add(&(struct tf_add) {
.in_1 = layer_i == 0 ? (float*)activations->embedding.out : (float*)activations->h[layer_i - 1].res_2.out,
.in_2 = (float*)activations->h[layer_i].attn.c_proj.out,
.out = (float*)activations->h[layer_i].res_1.out,
.count = input_size * tf_d_model,
});
tf_validation_time(&time.res_1, &time.t_last);
tf_ln(&(struct tf_ln) {
.weight = parameters->h[layer_i].ln_2.weight,
.bias = parameters->h[layer_i].ln_2.bias,
.in = (float*)activations->h[layer_i].res_1.out,
.r_std = (float*)activations->h[layer_i].ln_2.r_std,
.mean = (float*)activations->h[layer_i].ln_2.mean,
.out = (float*)activations->h[layer_i].ln_2.out,
.in_count = tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.ln_2, &time.t_last);
if (layer_i == 0) {
tf_validation_sum((float*)activations->h[layer_i].ln_2.out, input_size * tf_d_model, 0x1.188ffb5000f3dp+8);
}
tf_fc(&(struct tf_fc) {
.weight = parameters->h[layer_i].mlp.c_fc.weight,
.bias = parameters->h[layer_i].mlp.c_fc.bias,
.in = (float*)activations->h[layer_i].ln_2.out,
.out = (float*)activations->h[layer_i].mlp.c_fc.out,
.in_count = tf_d_model,
.out_count = 4 * tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.mlp.c_fc, &time.t_last);
{
const float* in = (float*)activations->h[layer_i].mlp.c_fc.out;
const float* in_end = in + input_size * 4 * tf_d_model;
float* out = (float*)activations->h[layer_i].mlp.gelu.out;
for (; in != in_end; in++, out++) {
float phi = 0.5f * (1.0f + erff(*in * (float)M_SQRT1_2));
*out = *in * phi;
}
}
tf_validation_time(&time.mlp.gelu, &time.t_last);
tf_fc(&(struct tf_fc) {
.weight = parameters->h[layer_i].mlp.c_proj.weight,
.bias = parameters->h[layer_i].mlp.c_proj.bias,
.in = (float*)activations->h[layer_i].mlp.gelu.out,
.out = (float*)activations->h[layer_i].mlp.c_proj.out,
.in_count = 4 * tf_d_model,
.out_count = tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.mlp.c_proj, &time.t_last);
if (layer_i == 0) {
tf_validation_sum((float*)activations->h[layer_i].mlp.c_proj.out, input_size * tf_d_model, -0x1.012ce31d82fb8p+9);
}
tf_add(&(struct tf_add) {
.in_1 = (float*)activations->h[layer_i].res_1.out,
.in_2 = (float*)activations->h[layer_i].mlp.c_proj.out,
.out = (float*)activations->h[layer_i].res_2.out,
.count = input_size * tf_d_model,
});
tf_validation_time(&time.res_2, &time.t_last);
}
tf_ln(&(struct tf_ln) {
.weight = parameters->ln_f.weight,
.bias = parameters->ln_f.bias,
.in = (float*)activations->h[11].res_2.out,
.r_std = (float*)activations->ln_f.r_std,
.mean = (float*)activations->ln_f.mean,
.out = (float*)activations->ln_f.out,
.in_count = tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.ln_f, &time.t_last);
tf_validation_sum((float*)activations->ln_f.out, input_size * tf_d_model, 0x1.0437f5b8f47d8p+14);
for (size_t i = is_training ? 0 : input_size - 1; i < input_size; i++) {
float* out = activations->unembedding.out[i];
float* out_end = out + tf_d_vocab;
float* out_reset = out;
const float* weight = parameters->wte.weight;
const float* weight_end = weight + tf_d_vocab * tf_d_model;
const float* in = (float*)activations->ln_f.out[i];
const float* in_end = in + tf_d_model;
const float* in_reset = in;
float dot = 0.0f;
float softmax_max = -INFINITY;
while (true) {
dot += *weight * *in;
weight++;
in++;
if (in == in_end) {
in = in_reset;
*out = dot;
out++;
if (dot > softmax_max) {
softmax_max = dot;
}
dot = 0.0f;
if (weight == weight_end) {
break;
}
}
}
float softmax_exp_sum = 0.0f;
for (out = out_reset; out != out_end; out++) {
float softmax_exp_i = expf(*out - softmax_max);
softmax_exp_sum += softmax_exp_i;
*out = softmax_exp_i;
}
float r_softmax_exp_sum = 1.0f / softmax_exp_sum;
for (out = out_reset; out != out_end; out++) {
*out *= r_softmax_exp_sum;
}
}
tf_validation_time(&time.unembedding, &time.t_last);
tf_validation_sum((float*)activations->unembedding.out, input_size * tf_d_vocab, 0x1.0008be62ee50cp+6);
tf_validation_time(&time.total, &time.t_start);
if (tf_validation) {
__builtin_dump_struct(&time, printf);
}
if (!is_training) {
const float* out = activations->unembedding.out[input_size - 1];
const float* out_end = out + tf_d_vocab;
const float* out_max = out;
for (; out != out_end; out++) {
if (*out > *out_max) {
out_max = out;
}
}
*out_token = out_max - activations->unembedding.out[input_size - 1];
return;
}
memset(&time, 0, sizeof(time));
time.t_start = tf_current_time();
time.t_last = time.t_start;
// L = crossentropy(softmax(x))
// dL/dx = p - 1, for the token that was expected
// dL/dx = p, for the token that was not expected
// x is the result of unembedding with wte
memcpy(activations_back->unembedding.out, activations->unembedding.out, input_size * tf_d_vocab * sizeof(float));
float loss_total = 0.0f;
for (size_t i = 0; i < input_size; i++) {
uint16_t correct = expected[i];
float p_correct = activations_back->unembedding.out[i][correct];
activations_back->unembedding.out[i][correct] = p_correct - 1.0f;
float cross_entropy_loss = -logf(p_correct);
loss_total += cross_entropy_loss;
}
tf_validation_sum(&loss_total, 1, 0x1.08868ep+8);
float r_input_size = 1.0f / (float)input_size;
memset(activations_back->ln_f.out, 0, sizeof(activations_back->ln_f.out));
for (size_t i = 0; i < input_size; i++) {
float* dL_din = (float*)activations_back->ln_f.out[i];
float* dL_din_end = dL_din + tf_d_model;
float* dL_din_reset = dL_din;
const float* in = activations->ln_f.out[i];
const float* in_end = in + tf_d_model;
const float* in_reset = in;
const float* dL_dout = activations_back->unembedding.out[i];
const float* dL_dout_end = dL_dout + tf_d_vocab;
float* dL_dweight = (float*)gradients->wte.weight;
const float* weight = parameters->wte.weight;
while (true) {
*dL_dweight += *dL_dout * *in * r_input_size;
*dL_din += *dL_dout * *weight;
dL_din++;
in++;
dL_dweight++;
weight++;
if (in == in_end) {
in = in_reset;
dL_din = dL_din_reset;
dL_dout++;
if (dL_dout == dL_dout_end) {
break;
}
}
}
for (; dL_din != dL_din_end; dL_din++) {
*dL_din *= r_input_size;
}
}
tf_validation_time(&time.unembedding, &time.t_last);
tf_validation_sum((float*)gradients->wte.weight, tf_d_vocab * tf_d_model, 0x1.7f5c4d133539p-6);
tf_validation_sum((float*)activations_back->ln_f.out, input_size * tf_d_model, -0x1.4adb0746d50fap-5);
tf_ln_back(&(struct tf_ln_back) {
.weight = parameters->ln_f.weight,
.in = (float*)activations->h[11].res_2.out,
.r_std = (float*)activations->ln_f.r_std,
.mean = (float*)activations->ln_f.mean,
.dL_dout = (float*)activations_back->ln_f.out,
.dL_dweight = (float*)gradients->ln_f.weight,
.dL_dbias = (float*)gradients->ln_f.bias,
.dL_din = (float*)activations_back->res_2.out,
.in_count = tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.ln_f, &time.t_last);
tf_validation_sum((float*)gradients->ln_f.weight, tf_d_model, -0x1.1264bf0ff8p-1);
tf_validation_sum((float*)gradients->ln_f.bias, tf_d_model, -0x1.4adaf7184p-5);
tf_validation_sum((float*)activations_back->res_2.out, input_size * tf_d_model, -0x1.690e462bp-27);
for (int layer_i = tf_n_layers - 1; layer_i >= 0; layer_i--) {
tf_fc_back(&(struct tf_fc_back) {
.weight = (float*)parameters->h[layer_i].mlp.c_proj.weight,
.in = (float*)activations->h[layer_i].mlp.gelu.out,
.dL_dout = (float*)activations_back->res_2.out,
.dL_dweight = (float*)gradients->h[layer_i].mlp.c_proj.weight,
.dL_dbias = gradients->h[layer_i].mlp.c_proj.bias,
.dL_din = (float*)activations_back->mlp.gelu.out,
.in_count = 4 * tf_d_model,
.out_count = tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.mlp.c_proj, &time.t_last);
if (layer_i == 11) {
tf_validation_sum((float*)gradients->h[layer_i].mlp.c_proj.weight, 4 * tf_d_model * tf_d_model, -0x1.3d7e7fe8p-23);
tf_validation_sum((float*)gradients->h[layer_i].mlp.c_proj.bias, tf_d_model, -0x1.6264p-27);
tf_validation_sum((float*)activations_back->mlp.gelu.out, input_size * 4 * tf_d_model, -0x1.627d327e64f6cp-1);
}
{
const float* in = (float*)activations->h[layer_i].mlp.c_fc.out;
const float* dL_dout = (float*)activations_back->mlp.gelu.out;
float* dL_din = (float*)activations_back->mlp.c_fc.out;
float* dL_din_end = dL_din + input_size * 4 * tf_d_model;
for (; dL_din != dL_din_end; in++, dL_dout++, dL_din++) {
float phi = 0.5f * (1.0f + erff(*in * (float)M_SQRT1_2));
float dgelu_din = phi + *in * (1.0f / sqrtf(2.0f * (float)M_PI)) * expf(-0.5f * *in * *in);
*dL_din = *dL_dout * dgelu_din;
}
}
tf_validation_time(&time.mlp.gelu, &time.t_last);
if (layer_i == 11) {
tf_validation_sum((float*)activations_back->mlp.c_fc.out, input_size * 4 * tf_d_model, 0x1.2e4527c197bcbp-5);
}
tf_fc_back(&(struct tf_fc_back) {
.weight = (float*)parameters->h[layer_i].mlp.c_fc.weight,
.in = (float*)activations->h[layer_i].ln_2.out,
.dL_dout = (float*)activations_back->mlp.c_fc.out,
.dL_dweight = (float*)gradients->h[layer_i].mlp.c_fc.weight,
.dL_dbias = gradients->h[layer_i].mlp.c_fc.bias,
.dL_din = (float*)activations_back->ln_2.out,
.in_count = tf_d_model,
.out_count = 4 * tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.mlp.c_fc, &time.t_last);
if (layer_i == 11) {
tf_validation_sum((float*)gradients->h[layer_i].mlp.c_fc.weight, tf_d_model * 4 * tf_d_model, -0x1.200760d8e8494p+1);
tf_validation_sum((float*)gradients->h[layer_i].mlp.c_fc.bias, 4 * tf_d_model, 0x1.2e452d5568p-5);
tf_validation_sum((float*)activations_back->ln_2.out, input_size * tf_d_model, 0x1.e762c70de442dp+0);
}
tf_ln_back(&(struct tf_ln_back) {
.weight = parameters->h[layer_i].ln_2.weight,
.in = (float*)activations->h[layer_i].res_1.out,
.r_std = (float*)activations->h[layer_i].ln_2.r_std,
.mean = (float*)activations->h[layer_i].ln_2.mean,
.dL_dout = (float*)activations_back->ln_2.out,
.dL_dweight = (float*)gradients->h[layer_i].ln_2.weight,
.dL_dbias = (float*)gradients->h[layer_i].ln_2.bias,
.dL_din = (float*)activations_back->res_2.in_2,
.in_count = tf_d_model,
.sample_count = input_size,
});
tf_validation_time(&time.ln_2, &time.t_last);
if (layer_i == 11) {
tf_validation_sum((float*)gradients->h[layer_i].ln_2.weight, tf_d_model, -0x1.f2a6e6d9a1p-1);
tf_validation_sum((float*)gradients->h[layer_i].ln_2.bias, tf_d_model, 0x1.e762c5ac36p+0);
tf_validation_sum((float*)activations_back->res_2.in_2, input_size * tf_d_model, -0x1.3869712ap-27);
}
tf_add(&(struct tf_add) {
.in_1 = (float*)activations_back->res_2.out,
.in_2 = (float*)activations_back->res_2.in_2,
.out = (float*)activations_back->res_1.out,
.count = input_size * tf_d_model,
});
tf_validation_time(&time.res_2, &time.t_last);
if (layer_i == 11) {
tf_validation_sum((float*)activations_back->res_1.out, input_size * tf_d_model, -0x1.50b057dap-26);
}
tf_fc_back(&(struct tf_fc_back) {
.weight = (float*)parameters->h[layer_i].attn.c_proj.weight,
.in = (float*)activations->h[layer_i].attn.z.out,
.dL_dout = (float*)activations_back->res_1.out,
.dL_dweight = (float*)gradients->h[layer_i].attn.c_proj.weight,
.dL_dbias = gradients->h[layer_i].attn.c_proj.bias,
.dL_din = (float*)activations_back->attn.z.out,