forked from karpathy/llm.c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_gpt2.rs
5017 lines (5016 loc) · 184 KB
/
train_gpt2.rs
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
#![feature(label_break_value)]
#![feature(linkage)]
#![feature(extern_types)]
use ::libc;
extern "C" {
pub type _IO_wide_data;
pub type _IO_codecvt;
pub type _IO_marker;
pub type __dirstream;
static mut stdout: *mut FILE;
static mut stderr: *mut FILE;
fn fclose(__stream: *mut FILE) -> libc::c_int;
fn fflush(__stream: *mut FILE) -> libc::c_int;
fn fopen(_: *const libc::c_char, _: *const libc::c_char) -> *mut FILE;
fn fprintf(_: *mut FILE, _: *const libc::c_char, _: ...) -> libc::c_int;
fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
fn fread(
_: *mut libc::c_void,
_: libc::c_ulong,
_: libc::c_ulong,
_: *mut FILE,
) -> libc::c_ulong;
fn fwrite(
_: *const libc::c_void,
_: libc::c_ulong,
_: libc::c_ulong,
_: *mut FILE,
) -> libc::c_ulong;
fn fseek(
__stream: *mut FILE,
__off: libc::c_long,
__whence: libc::c_int,
) -> libc::c_int;
fn ftell(__stream: *mut FILE) -> libc::c_long;
fn ferror(__stream: *mut FILE) -> libc::c_int;
fn feof(__stream: *mut FILE) -> libc::c_int;
fn strtol(
_: *const libc::c_char,
_: *mut *mut libc::c_char,
_: libc::c_int,
) -> libc::c_long;
fn malloc(_: libc::c_ulong) -> *mut libc::c_void;
fn calloc(_: libc::c_ulong, _: libc::c_ulong) -> *mut libc::c_void;
fn free(_: *mut libc::c_void);
fn exit(_: libc::c_int) -> !;
fn __ctype_b_loc() -> *mut *const libc::c_ushort;
fn __assert_fail(
__assertion: *const libc::c_char,
__file: *const libc::c_char,
__line: libc::c_uint,
__function: *const libc::c_char,
) -> !;
fn cosf(_: libc::c_float) -> libc::c_float;
fn sinf(_: libc::c_float) -> libc::c_float;
fn coshf(_: libc::c_float) -> libc::c_float;
fn tanhf(_: libc::c_float) -> libc::c_float;
fn expf(_: libc::c_float) -> libc::c_float;
fn logf(_: libc::c_float) -> libc::c_float;
fn powf(_: libc::c_float, _: libc::c_float) -> libc::c_float;
fn sqrtf(_: libc::c_float) -> libc::c_float;
fn clock_gettime(__clock_id: clockid_t, __tp: *mut timespec) -> libc::c_int;
fn memcpy(
_: *mut libc::c_void,
_: *const libc::c_void,
_: libc::c_ulong,
) -> *mut libc::c_void;
fn memset(
_: *mut libc::c_void,
_: libc::c_int,
_: libc::c_ulong,
) -> *mut libc::c_void;
fn strncmp(
_: *const libc::c_char,
_: *const libc::c_char,
_: libc::c_ulong,
) -> libc::c_int;
fn strlen(_: *const libc::c_char) -> libc::c_ulong;
fn access(__name: *const libc::c_char, __type: libc::c_int) -> libc::c_int;
fn close(__fd: libc::c_int) -> libc::c_int;
fn opendir(__name: *const libc::c_char) -> *mut DIR;
fn closedir(__dirp: *mut DIR) -> libc::c_int;
fn readdir(__dirp: *mut DIR) -> *mut dirent;
fn stat(__file: *const libc::c_char, __buf: *mut stat) -> libc::c_int;
fn mkdir(__path: *const libc::c_char, __mode: __mode_t) -> libc::c_int;
fn glob(
__pattern: *const libc::c_char,
__flags: libc::c_int,
__errfunc: Option::<
unsafe extern "C" fn(*const libc::c_char, libc::c_int) -> libc::c_int,
>,
__pglob: *mut glob_t,
) -> libc::c_int;
fn globfree(__pglob: *mut glob_t);
}
pub type size_t = libc::c_ulong;
pub type __uint16_t = libc::c_ushort;
pub type __uint32_t = libc::c_uint;
pub type __int64_t = libc::c_long;
pub type __uint64_t = libc::c_ulong;
pub type __dev_t = libc::c_ulong;
pub type __uid_t = libc::c_uint;
pub type __gid_t = libc::c_uint;
pub type __ino_t = libc::c_ulong;
pub type __mode_t = libc::c_uint;
pub type __nlink_t = libc::c_ulong;
pub type __off_t = libc::c_long;
pub type __off64_t = libc::c_long;
pub type __time_t = libc::c_long;
pub type __clockid_t = libc::c_int;
pub type __blksize_t = libc::c_long;
pub type __blkcnt_t = libc::c_long;
pub type __syscall_slong_t = libc::c_long;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _IO_FILE {
pub _flags: libc::c_int,
pub _IO_read_ptr: *mut libc::c_char,
pub _IO_read_end: *mut libc::c_char,
pub _IO_read_base: *mut libc::c_char,
pub _IO_write_base: *mut libc::c_char,
pub _IO_write_ptr: *mut libc::c_char,
pub _IO_write_end: *mut libc::c_char,
pub _IO_buf_base: *mut libc::c_char,
pub _IO_buf_end: *mut libc::c_char,
pub _IO_save_base: *mut libc::c_char,
pub _IO_backup_base: *mut libc::c_char,
pub _IO_save_end: *mut libc::c_char,
pub _markers: *mut _IO_marker,
pub _chain: *mut _IO_FILE,
pub _fileno: libc::c_int,
pub _flags2: libc::c_int,
pub _old_offset: __off_t,
pub _cur_column: libc::c_ushort,
pub _vtable_offset: libc::c_schar,
pub _shortbuf: [libc::c_char; 1],
pub _lock: *mut libc::c_void,
pub _offset: __off64_t,
pub _codecvt: *mut _IO_codecvt,
pub _wide_data: *mut _IO_wide_data,
pub _freeres_list: *mut _IO_FILE,
pub _freeres_buf: *mut libc::c_void,
pub __pad5: size_t,
pub _mode: libc::c_int,
pub _unused2: [libc::c_char; 20],
}
pub type _IO_lock_t = ();
pub type FILE = _IO_FILE;
pub type clockid_t = __clockid_t;
pub type int64_t = __int64_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct timespec {
pub tv_sec: __time_t,
pub tv_nsec: __syscall_slong_t,
}
pub type C2RustUnnamed = libc::c_uint;
pub const _ISalnum: C2RustUnnamed = 8;
pub const _ISpunct: C2RustUnnamed = 4;
pub const _IScntrl: C2RustUnnamed = 2;
pub const _ISblank: C2RustUnnamed = 1;
pub const _ISgraph: C2RustUnnamed = 32768;
pub const _ISprint: C2RustUnnamed = 16384;
pub const _ISspace: C2RustUnnamed = 8192;
pub const _ISxdigit: C2RustUnnamed = 4096;
pub const _ISdigit: C2RustUnnamed = 2048;
pub const _ISalpha: C2RustUnnamed = 1024;
pub const _ISlower: C2RustUnnamed = 512;
pub const _ISupper: C2RustUnnamed = 256;
pub type uint16_t = __uint16_t;
pub type uint32_t = __uint32_t;
pub type uint64_t = __uint64_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct stat {
pub st_dev: __dev_t,
pub st_ino: __ino_t,
pub st_nlink: __nlink_t,
pub st_mode: __mode_t,
pub st_uid: __uid_t,
pub st_gid: __gid_t,
pub __pad0: libc::c_int,
pub st_rdev: __dev_t,
pub st_size: __off_t,
pub st_blksize: __blksize_t,
pub st_blocks: __blkcnt_t,
pub st_atim: timespec,
pub st_mtim: timespec,
pub st_ctim: timespec,
pub __glibc_reserved: [__syscall_slong_t; 3],
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct dirent {
pub d_ino: __ino_t,
pub d_off: __off_t,
pub d_reclen: libc::c_ushort,
pub d_type: libc::c_uchar,
pub d_name: [libc::c_char; 256],
}
pub type DIR = __dirstream;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct Tokenizer {
pub vocab_size: uint32_t,
pub token_table: *mut *mut libc::c_char,
pub init_ok: libc::c_int,
pub eot_token: libc::c_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct mt19937_state {
pub seed_: libc::c_ulonglong,
pub left_: libc::c_int,
pub next_: libc::c_uint,
pub state_: [libc::c_uint; 624],
pub MATRIX_A: [libc::c_uint; 2],
}
pub type __size_t = libc::c_ulong;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct glob_t {
pub gl_pathc: __size_t,
pub gl_pathv: *mut *mut libc::c_char,
pub gl_offs: __size_t,
pub gl_flags: libc::c_int,
pub gl_closedir: Option::<unsafe extern "C" fn(*mut libc::c_void) -> ()>,
pub gl_readdir: Option::<
unsafe extern "C" fn(*mut libc::c_void) -> *mut libc::c_void,
>,
pub gl_opendir: Option::<
unsafe extern "C" fn(*const libc::c_char) -> *mut libc::c_void,
>,
pub gl_lstat: Option::<
unsafe extern "C" fn(*const libc::c_char, *mut libc::c_void) -> libc::c_int,
>,
pub gl_stat: Option::<
unsafe extern "C" fn(*const libc::c_char, *mut libc::c_void) -> libc::c_int,
>,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct DataLoader {
pub process_rank: libc::c_int,
pub num_processes: libc::c_int,
pub B: size_t,
pub T: size_t,
pub num_tokens: size_t,
pub shard_num_samples: size_t,
pub glob_result: glob_t,
pub current_shard_idx: size_t,
pub current_sample_idx: size_t,
pub tokens_file: *mut FILE,
pub buffer: *mut uint16_t,
pub inputs: *mut libc::c_int,
pub targets: *mut libc::c_int,
pub shuffle_rng: mt19937_state,
pub should_shuffle: libc::c_int,
pub shard_indices: *mut libc::c_int,
pub intra_shard_indices: *mut libc::c_int,
pub total_batch_size_bytes: size_t,
pub local_batch_offset_bytes: size_t,
pub header_bytes: size_t,
pub file_size_bytes: int64_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct EvalLoader {
pub process_rank: libc::c_int,
pub num_processes: libc::c_int,
pub B: size_t,
pub T: size_t,
pub eval_file: *mut FILE,
pub buffer: *mut uint16_t,
pub num_examples: libc::c_int,
pub num_batches: libc::c_int,
pub start_example_index: libc::c_int,
pub end_example_index: libc::c_int,
pub current_example_index: libc::c_int,
pub inputs: *mut libc::c_int,
pub targets: *mut libc::c_int,
pub mask: *mut libc::c_char,
pub label: *mut libc::c_int,
pub num_completions: libc::c_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct GPT2Config {
pub max_seq_len: libc::c_int,
pub vocab_size: libc::c_int,
pub padded_vocab_size: libc::c_int,
pub num_layers: libc::c_int,
pub num_heads: libc::c_int,
pub channels: libc::c_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct ParameterTensors {
pub wte: *mut libc::c_float,
pub wpe: *mut libc::c_float,
pub ln1w: *mut libc::c_float,
pub ln1b: *mut libc::c_float,
pub qkvw: *mut libc::c_float,
pub qkvb: *mut libc::c_float,
pub attprojw: *mut libc::c_float,
pub attprojb: *mut libc::c_float,
pub ln2w: *mut libc::c_float,
pub ln2b: *mut libc::c_float,
pub fcw: *mut libc::c_float,
pub fcb: *mut libc::c_float,
pub fcprojw: *mut libc::c_float,
pub fcprojb: *mut libc::c_float,
pub lnfw: *mut libc::c_float,
pub lnfb: *mut libc::c_float,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct ActivationTensors {
pub encoded: *mut libc::c_float,
pub ln1: *mut libc::c_float,
pub ln1_mean: *mut libc::c_float,
pub ln1_rstd: *mut libc::c_float,
pub qkv: *mut libc::c_float,
pub atty: *mut libc::c_float,
pub preatt: *mut libc::c_float,
pub att: *mut libc::c_float,
pub attproj: *mut libc::c_float,
pub residual2: *mut libc::c_float,
pub ln2: *mut libc::c_float,
pub ln2_mean: *mut libc::c_float,
pub ln2_rstd: *mut libc::c_float,
pub fch: *mut libc::c_float,
pub fch_gelu: *mut libc::c_float,
pub fcproj: *mut libc::c_float,
pub residual3: *mut libc::c_float,
pub lnf: *mut libc::c_float,
pub lnf_mean: *mut libc::c_float,
pub lnf_rstd: *mut libc::c_float,
pub logits: *mut libc::c_float,
pub probs: *mut libc::c_float,
pub losses: *mut libc::c_float,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct GPT2 {
pub config: GPT2Config,
pub params: ParameterTensors,
pub param_sizes: [size_t; 16],
pub params_memory: *mut libc::c_float,
pub num_parameters: size_t,
pub grads: ParameterTensors,
pub grads_memory: *mut libc::c_float,
pub m_memory: *mut libc::c_float,
pub v_memory: *mut libc::c_float,
pub acts: ActivationTensors,
pub act_sizes: [size_t; 23],
pub acts_memory: *mut libc::c_float,
pub num_activations: size_t,
pub grads_acts: ActivationTensors,
pub grads_acts_memory: *mut libc::c_float,
pub batch_size: libc::c_int,
pub seq_len: libc::c_int,
pub inputs: *mut libc::c_int,
pub targets: *mut libc::c_int,
pub mean_loss: libc::c_float,
}
#[inline]
unsafe extern "C" fn atoi(mut __nptr: *const libc::c_char) -> libc::c_int {
return strtol(
__nptr,
0 as *mut libc::c_void as *mut *mut libc::c_char,
10 as libc::c_int,
) as libc::c_int;
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn fopen_check(
mut path: *const libc::c_char,
mut mode: *const libc::c_char,
mut file: *const libc::c_char,
mut line: libc::c_int,
) -> *mut FILE {
let mut fp: *mut FILE = fopen(path, mode);
if fp.is_null() {
fprintf(
stderr,
b"Error: Failed to open file '%s' at %s:%d\n\0" as *const u8
as *const libc::c_char,
path,
file,
line,
);
fprintf(stderr, b"Error details:\n\0" as *const u8 as *const libc::c_char);
fprintf(stderr, b" File: %s\n\0" as *const u8 as *const libc::c_char, file);
fprintf(stderr, b" Line: %d\n\0" as *const u8 as *const libc::c_char, line);
fprintf(stderr, b" Path: %s\n\0" as *const u8 as *const libc::c_char, path);
fprintf(stderr, b" Mode: %s\n\0" as *const u8 as *const libc::c_char, mode);
fprintf(
stderr,
b"---> HINT 1: dataset files/code have moved to dev/data recently (May 20, 2024). You may have to mv them from the legacy data/ dir to dev/data/(dataset), or re-run the data preprocessing script. Refer back to the main README\n\0"
as *const u8 as *const libc::c_char,
);
fprintf(
stderr,
b"---> HINT 2: possibly try to re-run `python train_gpt2.py`\n\0"
as *const u8 as *const libc::c_char,
);
exit(1 as libc::c_int);
}
return fp;
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn fread_check(
mut ptr: *mut libc::c_void,
mut size: size_t,
mut nmemb: size_t,
mut stream: *mut FILE,
mut file: *const libc::c_char,
mut line: libc::c_int,
) {
let mut result: size_t = fread(ptr, size, nmemb, stream);
if result != nmemb {
if feof(stream) != 0 {
fprintf(
stderr,
b"Error: Unexpected end of file at %s:%d\n\0" as *const u8
as *const libc::c_char,
file,
line,
);
} else if ferror(stream) != 0 {
fprintf(
stderr,
b"Error: File read error at %s:%d\n\0" as *const u8
as *const libc::c_char,
file,
line,
);
} else {
fprintf(
stderr,
b"Error: Partial read at %s:%d. Expected %zu elements, read %zu\n\0"
as *const u8 as *const libc::c_char,
file,
line,
nmemb,
result,
);
}
fprintf(stderr, b"Error details:\n\0" as *const u8 as *const libc::c_char);
fprintf(stderr, b" File: %s\n\0" as *const u8 as *const libc::c_char, file);
fprintf(stderr, b" Line: %d\n\0" as *const u8 as *const libc::c_char, line);
fprintf(
stderr,
b" Expected elements: %zu\n\0" as *const u8 as *const libc::c_char,
nmemb,
);
fprintf(
stderr,
b" Read elements: %zu\n\0" as *const u8 as *const libc::c_char,
result,
);
exit(1 as libc::c_int);
}
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn fclose_check(
mut fp: *mut FILE,
mut file: *const libc::c_char,
mut line: libc::c_int,
) {
if fclose(fp) != 0 as libc::c_int {
fprintf(
stderr,
b"Error: Failed to close file at %s:%d\n\0" as *const u8
as *const libc::c_char,
file,
line,
);
fprintf(stderr, b"Error details:\n\0" as *const u8 as *const libc::c_char);
fprintf(stderr, b" File: %s\n\0" as *const u8 as *const libc::c_char, file);
fprintf(stderr, b" Line: %d\n\0" as *const u8 as *const libc::c_char, line);
exit(1 as libc::c_int);
}
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn sclose_check(
mut sockfd: libc::c_int,
mut file: *const libc::c_char,
mut line: libc::c_int,
) {
if close(sockfd) != 0 as libc::c_int {
fprintf(
stderr,
b"Error: Failed to close socket at %s:%d\n\0" as *const u8
as *const libc::c_char,
file,
line,
);
fprintf(stderr, b"Error details:\n\0" as *const u8 as *const libc::c_char);
fprintf(stderr, b" File: %s\n\0" as *const u8 as *const libc::c_char, file);
fprintf(stderr, b" Line: %d\n\0" as *const u8 as *const libc::c_char, line);
exit(1 as libc::c_int);
}
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn fseek_check(
mut fp: *mut FILE,
mut off: libc::c_long,
mut whence: libc::c_int,
mut file: *const libc::c_char,
mut line: libc::c_int,
) {
if fseek(fp, off, whence) != 0 as libc::c_int {
fprintf(
stderr,
b"Error: Failed to seek in file at %s:%d\n\0" as *const u8
as *const libc::c_char,
file,
line,
);
fprintf(stderr, b"Error details:\n\0" as *const u8 as *const libc::c_char);
fprintf(stderr, b" Offset: %ld\n\0" as *const u8 as *const libc::c_char, off);
fprintf(stderr, b" Whence: %d\n\0" as *const u8 as *const libc::c_char, whence);
fprintf(stderr, b" File: %s\n\0" as *const u8 as *const libc::c_char, file);
fprintf(stderr, b" Line: %d\n\0" as *const u8 as *const libc::c_char, line);
exit(1 as libc::c_int);
}
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn fwrite_check(
mut ptr: *mut libc::c_void,
mut size: size_t,
mut nmemb: size_t,
mut stream: *mut FILE,
mut file: *const libc::c_char,
mut line: libc::c_int,
) {
let mut result: size_t = fwrite(ptr, size, nmemb, stream);
if result != nmemb {
if feof(stream) != 0 {
fprintf(
stderr,
b"Error: Unexpected end of file at %s:%d\n\0" as *const u8
as *const libc::c_char,
file,
line,
);
} else if ferror(stream) != 0 {
fprintf(
stderr,
b"Error: File write error at %s:%d\n\0" as *const u8
as *const libc::c_char,
file,
line,
);
} else {
fprintf(
stderr,
b"Error: Partial write at %s:%d. Expected %zu elements, wrote %zu\n\0"
as *const u8 as *const libc::c_char,
file,
line,
nmemb,
result,
);
}
fprintf(stderr, b"Error details:\n\0" as *const u8 as *const libc::c_char);
fprintf(stderr, b" File: %s\n\0" as *const u8 as *const libc::c_char, file);
fprintf(stderr, b" Line: %d\n\0" as *const u8 as *const libc::c_char, line);
fprintf(
stderr,
b" Expected elements: %zu\n\0" as *const u8 as *const libc::c_char,
nmemb,
);
fprintf(
stderr,
b" Written elements: %zu\n\0" as *const u8 as *const libc::c_char,
result,
);
exit(1 as libc::c_int);
}
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn malloc_check(
mut size: size_t,
mut file: *const libc::c_char,
mut line: libc::c_int,
) -> *mut libc::c_void {
let mut ptr: *mut libc::c_void = malloc(size);
if ptr.is_null() {
fprintf(
stderr,
b"Error: Memory allocation failed at %s:%d\n\0" as *const u8
as *const libc::c_char,
file,
line,
);
fprintf(stderr, b"Error details:\n\0" as *const u8 as *const libc::c_char);
fprintf(stderr, b" File: %s\n\0" as *const u8 as *const libc::c_char, file);
fprintf(stderr, b" Line: %d\n\0" as *const u8 as *const libc::c_char, line);
fprintf(
stderr,
b" Size: %zu bytes\n\0" as *const u8 as *const libc::c_char,
size,
);
exit(1 as libc::c_int);
}
return ptr;
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn token_check(
mut tokens: *const libc::c_int,
mut token_count: libc::c_int,
mut vocab_size: libc::c_int,
mut file: *const libc::c_char,
mut line: libc::c_int,
) {
let mut i: libc::c_int = 0 as libc::c_int;
while i < token_count {
if !(0 as libc::c_int <= *tokens.offset(i as isize)
&& *tokens.offset(i as isize) < vocab_size)
{
fprintf(
stderr,
b"Error: Token out of vocabulary at %s:%d\n\0" as *const u8
as *const libc::c_char,
file,
line,
);
fprintf(stderr, b"Error details:\n\0" as *const u8 as *const libc::c_char);
fprintf(stderr, b" File: %s\n\0" as *const u8 as *const libc::c_char, file);
fprintf(stderr, b" Line: %d\n\0" as *const u8 as *const libc::c_char, line);
fprintf(
stderr,
b" Token: %d\n\0" as *const u8 as *const libc::c_char,
*tokens.offset(i as isize),
);
fprintf(
stderr,
b" Position: %d\n\0" as *const u8 as *const libc::c_char,
i,
);
fprintf(
stderr,
b" Vocab: %d\n\0" as *const u8 as *const libc::c_char,
vocab_size,
);
exit(1 as libc::c_int);
}
i += 1;
i;
}
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn create_dir_if_not_exists(mut dir: *const libc::c_char) {
if dir.is_null() {
return;
}
let mut st: stat = {
let mut init = stat {
st_dev: 0 as libc::c_int as __dev_t,
st_ino: 0,
st_nlink: 0,
st_mode: 0,
st_uid: 0,
st_gid: 0,
__pad0: 0,
st_rdev: 0,
st_size: 0,
st_blksize: 0,
st_blocks: 0,
st_atim: timespec { tv_sec: 0, tv_nsec: 0 },
st_mtim: timespec { tv_sec: 0, tv_nsec: 0 },
st_ctim: timespec { tv_sec: 0, tv_nsec: 0 },
__glibc_reserved: [0; 3],
};
init
};
if stat(dir, &mut st) == -(1 as libc::c_int) {
if mkdir(dir, 0o700 as libc::c_int as __mode_t) == -(1 as libc::c_int) {
printf(
b"ERROR: could not create directory: %s\n\0" as *const u8
as *const libc::c_char,
dir,
);
exit(1 as libc::c_int);
}
printf(b"created directory: %s\n\0" as *const u8 as *const libc::c_char, dir);
}
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn find_max_step(
mut output_log_dir: *const libc::c_char,
) -> libc::c_int {
if output_log_dir.is_null() {
return -(1 as libc::c_int);
}
let mut dir: *mut DIR = 0 as *mut DIR;
let mut entry: *mut dirent = 0 as *mut dirent;
let mut max_step: libc::c_int = -(1 as libc::c_int);
dir = opendir(output_log_dir);
if dir.is_null() {
return -(1 as libc::c_int);
}
loop {
entry = readdir(dir);
if entry.is_null() {
break;
}
if strncmp(
((*entry).d_name).as_mut_ptr(),
b"DONE_\0" as *const u8 as *const libc::c_char,
5 as libc::c_int as libc::c_ulong,
) == 0 as libc::c_int
{
let mut step: libc::c_int = atoi(
((*entry).d_name).as_mut_ptr().offset(5 as libc::c_int as isize),
);
if step > max_step {
max_step = step;
}
}
}
closedir(dir);
return max_step;
}
#[no_mangle]
#[inline]
#[linkage = "external"]
pub unsafe extern "C" fn ends_with_bin(mut str: *const libc::c_char) -> libc::c_int {
if str.is_null() {
return 0 as libc::c_int;
}
let mut len: size_t = strlen(str);
let mut suffix: *const libc::c_char = b".bin\0" as *const u8 as *const libc::c_char;
let mut suffix_len: size_t = strlen(suffix);
if len < suffix_len {
return 0 as libc::c_int;
}
let mut suffix_matches: libc::c_int = (strncmp(
str.offset(len as isize).offset(-(suffix_len as isize)),
suffix,
suffix_len,
) == 0 as libc::c_int) as libc::c_int;
return suffix_matches;
}
#[no_mangle]
pub unsafe extern "C" fn safe_printf(mut piece: *const libc::c_char) {
if piece.is_null() {
return;
}
if *piece.offset(0 as libc::c_int as isize) as libc::c_int == '\0' as i32 {
return;
}
if *piece.offset(1 as libc::c_int as isize) as libc::c_int == '\0' as i32 {
let mut byte_val: libc::c_uchar = *piece.offset(0 as libc::c_int as isize)
as libc::c_uchar;
if !(*(*__ctype_b_loc()).offset(byte_val as libc::c_int as isize) as libc::c_int
& _ISprint as libc::c_int as libc::c_ushort as libc::c_int != 0
|| *(*__ctype_b_loc()).offset(byte_val as libc::c_int as isize)
as libc::c_int & _ISspace as libc::c_int as libc::c_ushort as libc::c_int
!= 0)
{
return;
}
}
printf(b"%s\0" as *const u8 as *const libc::c_char, piece);
}
#[no_mangle]
pub unsafe extern "C" fn tokenizer_init(
mut tokenizer: *mut Tokenizer,
mut filename: *const libc::c_char,
) {
let mut file: *mut FILE = fopen(
filename,
b"rb\0" as *const u8 as *const libc::c_char,
);
if file.is_null() {
printf(b"---\n\0" as *const u8 as *const libc::c_char);
printf(
b"WARNING: Failed to open the tokenizer file %s\n\0" as *const u8
as *const libc::c_char,
filename,
);
printf(
b"The Tokenizer is a new feature added April 14 2024.\n\0" as *const u8
as *const libc::c_char,
);
printf(
b"Re-run `python train_gpt2.py` to write it\n\0" as *const u8
as *const libc::c_char,
);
printf(b"---\n\0" as *const u8 as *const libc::c_char);
(*tokenizer).init_ok = 0 as libc::c_int;
return;
}
let mut header: [uint32_t; 256] = [0; 256];
fread_check(
header.as_mut_ptr() as *mut libc::c_void,
::core::mem::size_of::<uint32_t>() as libc::c_ulong,
256 as libc::c_int as size_t,
file,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
55 as libc::c_int,
);
if header[0 as libc::c_int as usize] == 20240328 as libc::c_int as libc::c_uint
{} else {
__assert_fail(
b"header[0] == 20240328\0" as *const u8 as *const libc::c_char,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
56 as libc::c_int as libc::c_uint,
(*::core::mem::transmute::<
&[u8; 47],
&[libc::c_char; 47],
>(b"void tokenizer_init(Tokenizer *, const char *)\0"))
.as_ptr(),
);
}
'c_6707: {
if header[0 as libc::c_int as usize] == 20240328 as libc::c_int as libc::c_uint
{} else {
__assert_fail(
b"header[0] == 20240328\0" as *const u8 as *const libc::c_char,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
56 as libc::c_int as libc::c_uint,
(*::core::mem::transmute::<
&[u8; 47],
&[libc::c_char; 47],
>(b"void tokenizer_init(Tokenizer *, const char *)\0"))
.as_ptr(),
);
}
};
let mut version: libc::c_int = header[1 as libc::c_int as usize] as libc::c_int;
(*tokenizer).vocab_size = header[2 as libc::c_int as usize];
if version == 1 as libc::c_int {
if (*tokenizer).vocab_size == 50257 as libc::c_int as libc::c_uint {} else {
__assert_fail(
b"tokenizer->vocab_size == 50257\0" as *const u8 as *const libc::c_char,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
62 as libc::c_int as libc::c_uint,
(*::core::mem::transmute::<
&[u8; 47],
&[libc::c_char; 47],
>(b"void tokenizer_init(Tokenizer *, const char *)\0"))
.as_ptr(),
);
}
'c_6652: {
if (*tokenizer).vocab_size == 50257 as libc::c_int as libc::c_uint {} else {
__assert_fail(
b"tokenizer->vocab_size == 50257\0" as *const u8
as *const libc::c_char,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
62 as libc::c_int as libc::c_uint,
(*::core::mem::transmute::<
&[u8; 47],
&[libc::c_char; 47],
>(b"void tokenizer_init(Tokenizer *, const char *)\0"))
.as_ptr(),
);
}
};
(*tokenizer).eot_token = 50256 as libc::c_int;
} else if version == 2 as libc::c_int {
(*tokenizer).eot_token = header[3 as libc::c_int as usize] as libc::c_int;
} else {
fprintf(
stderr,
b"Tokenizer model file %s has bad version: %d\n\0" as *const u8
as *const libc::c_char,
filename,
version,
);
exit(1 as libc::c_int);
}
let mut length: libc::c_uchar = 0;
(*tokenizer)
.token_table = malloc_check(
((*tokenizer).vocab_size as libc::c_ulong)
.wrapping_mul(::core::mem::size_of::<*mut libc::c_char>() as libc::c_ulong),
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
72 as libc::c_int,
) as *mut *mut libc::c_char;
let mut i: uint32_t = 0 as libc::c_int as uint32_t;
while i < (*tokenizer).vocab_size {
fread_check(
&mut length as *mut libc::c_uchar as *mut libc::c_void,
::core::mem::size_of::<libc::c_uchar>() as libc::c_ulong,
1 as libc::c_int as size_t,
file,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
74 as libc::c_int,
);
if length as libc::c_int > 0 as libc::c_int {} else {
__assert_fail(
b"length > 0\0" as *const u8 as *const libc::c_char,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
75 as libc::c_int as libc::c_uint,
(*::core::mem::transmute::<
&[u8; 47],
&[libc::c_char; 47],
>(b"void tokenizer_init(Tokenizer *, const char *)\0"))
.as_ptr(),
);
}
'c_6516: {
if length as libc::c_int > 0 as libc::c_int {} else {
__assert_fail(
b"length > 0\0" as *const u8 as *const libc::c_char,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
75 as libc::c_int as libc::c_uint,
(*::core::mem::transmute::<
&[u8; 47],
&[libc::c_char; 47],
>(b"void tokenizer_init(Tokenizer *, const char *)\0"))
.as_ptr(),
);
}
};
let mut token_bytes: *mut libc::c_char = malloc_check(
(length as libc::c_int + 1 as libc::c_int) as size_t,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
76 as libc::c_int,
) as *mut libc::c_char;
fread_check(
token_bytes as *mut libc::c_void,
::core::mem::size_of::<libc::c_char>() as libc::c_ulong,
length as size_t,
file,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
77 as libc::c_int,
);
*token_bytes.offset(length as isize) = '\0' as i32 as libc::c_char;
let ref mut fresh0 = *((*tokenizer).token_table).offset(i as isize);
*fresh0 = token_bytes;
i = i.wrapping_add(1);
i;
}
fclose_check(
file,
b"./llmc/tokenizer.h\0" as *const u8 as *const libc::c_char,
82 as libc::c_int,
);
(*tokenizer).init_ok = 1 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn tokenizer_decode(
mut tokenizer: *mut Tokenizer,
mut token_id: uint32_t,
) -> *const libc::c_char {
if (*tokenizer).init_ok == 0 as libc::c_int {
return 0 as *const libc::c_char;
}
if token_id < (*tokenizer).vocab_size {
return *((*tokenizer).token_table).offset(token_id as isize)
} else {
printf(
b"invalid token id %u!\n\0" as *const u8 as *const libc::c_char,
token_id,
);
return 0 as *const libc::c_char;
};
}
#[no_mangle]
pub unsafe extern "C" fn tokenizer_free(mut tokenizer: *mut Tokenizer) {
if (*tokenizer).init_ok != 0 {
let mut i: uint32_t = 0 as libc::c_int as uint32_t;
while i < (*tokenizer).vocab_size {
free(*((*tokenizer).token_table).offset(i as isize) as *mut libc::c_void);
i = i.wrapping_add(1);
i;
}
free((*tokenizer).token_table as *mut libc::c_void);
}
}
#[no_mangle]
pub unsafe extern "C" fn dataloader_load_shard_(
mut loader: *mut DataLoader,
mut shard_index: libc::c_int,