File size: 77,765 Bytes
3a7a00c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
import os
import math
import time
import json
import random
import inspect
import shutil
import subprocess
import argparse

# ─────────────────────────────────────────────────────────────
# 1. CORE PIPELINE FUNCTION AND HELPERS
# ─────────────────────────────────────────────────────────────

def is_zero_shot_cache_valid(cache_dir):
    if not os.path.exists(cache_dir):
        return False
    pred_count = 0
    for root, dirs, files in os.walk(cache_dir):
        if "predictions.json" in files:
            pred_count += 1
    return pred_count >= 3

def is_finetune_cache_valid(cache_dir):
    if not os.path.exists(cache_dir):
        return False
    pred_count = 0
    for root, dirs, files in os.walk(cache_dir):
        if "predictions.json" in files:
            pred_count += 1
    return pred_count >= 3

def verify_eval_run(path_to_check, description):
    print(f"[Eval] Verifying {description} path: {path_to_check}...")
    if not os.path.exists(path_to_check):
        raise RuntimeError(f"CRITICAL ERROR: {description} directory was NOT created at: {path_to_check}")
    
    # Check if predictions.json or results.txt or surprisal.json exists and is non-empty
    found_valid = False
    for root, dirs, files in os.walk(path_to_check):
        for f in files:
            if f in ["predictions.json", "results.txt", "surprisal.json"]:
                file_path = os.path.join(root, f)
                if os.path.getsize(file_path) > 0:
                    found_valid = True
                    break
        if found_valid:
            break
            
    if not found_valid:
        raise RuntimeError(f"CRITICAL ERROR: {description} completed but no valid prediction/result files were written in: {path_to_check}")
    print(f"[Eval] Success! Verified {description} results are stored correctly.")

def run_pipeline(model_name: str, epochs: int = 10, skip_eval: bool = False, skip_aoa: bool = True, skip_glue: bool = False):
    # Configure persistent cache paths locally to avoid duplicate downloads
    os.environ["HF_HOME"] = os.path.abspath("./hf_cache")
    os.environ["NLTK_DATA"] = os.path.abspath("./nltk_data")
    os.makedirs("./hf_cache", exist_ok=True)
    os.makedirs("./nltk_data", exist_ok=True)

    # Programmatic Hugging Face Hub Login if HF_TOKEN is in environment
    hf_token = os.environ.get("HF_TOKEN")

    if hf_token:
        try:
            from huggingface_hub import login
            login(token=hf_token)
            print("[HF] Programmatic login successful using HF_TOKEN.")
        except Exception as e:
            print(f"[HF] Warning: Programmatic login failed: {e}")

    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    from datasets import load_dataset
    from tokenizers import Tokenizer
    from transformers import PreTrainedTokenizerFast
    
    # Import model architecture
    from modeling_xpertgpt import (
        XpertGPTModel,
        XpertGPTModelConfig,
        XpertGPTConfig
    )

    # Print GPU details
    if torch.cuda.is_available():
        gpu_name = torch.cuda.get_device_name(0)
        print(f"\n[GPU] CUDA is available! Using GPU: {gpu_name}\n")
    else:
        print("\n[GPU] Warning: CUDA is NOT available! Running on CPU.\n")

    # ─────────────────────────────────────────────────────────────
    # GLOBAL HYPERPARAMETERS
    # ─────────────────────────────────────────────────────────────
    VOCAB_SIZE        = 16384          
    MASK_TOKEN_ID     = 16383          
    BLOCK_SIZE        = 512
    BATCH_SIZE        = 16
    GRAD_ACCUM_STEPS  = 1  # grad_acc_step = 1
    EPOCHS            = epochs
    LEARNING_RATE     = 3e-4  # lr = 3e-4
    LR_MIN            = LEARNING_RATE * 0.05
    WARMUP_STEPS      = 800  # warmup_steps = 800
    WEIGHT_DECAY      = 0.1
    GRAD_CLIP         = 1.0

    NUM_THIN_BLOCKS   = 4             
    EC_CAPACITY_FACTOR = 2.0           
    CAUSAL_RATIO      = 1 / 1        

    MASK_PROB_START   = 0.20
    MASK_PROB_END     = 0.10

    # Output directories locally
    model_dir = os.path.abspath(f"./checkpoints/{model_name}")
    os.makedirs(model_dir, exist_ok=True)
    local_results_dir = os.path.abspath(f"./results/{model_name}")
    os.makedirs(local_results_dir, exist_ok=True)

    # ─────────────────────────────────────────────────────────────
    # Helper: Save Hugging Face Compliant Checkpoint
    # ─────────────────────────────────────────────────────────────
    def save_hf_checkpoint(raw_model, checkpoint_dir_name, tokenizer):
        save_dir = os.path.join(model_dir, checkpoint_dir_name)
        os.makedirs(save_dir, exist_ok=True)
        print(f"\n[Checkpoint] Saving Hugging Face format checkpoint to '{save_dir}'...")
        
        # A. Convert state dict keys to CausalLM wrapper naming
        state_dict = raw_model.state_dict()
        new_state_dict = {}
        for k, v in state_dict.items():
            name = k
            if name.startswith("_orig_mod."):
                name = name[10:]
            if name.startswith("model."):
                name = name[6:]
            
            if name == "lm_head.weight":
                new_state_dict["lm_head.weight"] = v
            else:
                new_state_dict[f"transformer.{name}"] = v
                
        torch.save(new_state_dict, os.path.join(save_dir, "pytorch_model.bin"))
        
        # B. Copy modeling.py and configuration.py
        shutil.copy("modeling_xpertgpt.py", os.path.join(save_dir, "modeling_xpertgpt.py"))
        shutil.copy("configuration_xpertgpt.py", os.path.join(save_dir, "configuration_xpertgpt.py"))
        
        # C. Create config.json
        config_dict = {
            "auto_map": {
                "AutoConfig": "configuration_xpertgpt.XpertGPTConfig",
                "AutoModel": "modeling_xpertgpt.XpertGPTModelWrapper",
                "AutoModelForCausalLM": "modeling_xpertgpt.XpertGPTForCausalLM"
            },
            "vocab_size": VOCAB_SIZE,
            "block_size": BLOCK_SIZE,
            "d_model": 256,        # d_model = 256
            "hidden_size": 256,    # hidden_size = 256
            "d_thin": 384,
            "num_layers": 6,
            "num_blocks": NUM_THIN_BLOCKS,
            "capacity_factor": EC_CAPACITY_FACTOR,
            "dropout": 0.1,
            "model_type": "xpertgpt"
        }
        with open(os.path.join(save_dir, "config.json"), "w") as f:
            json.dump(config_dict, f, indent=2)
            
        # D. Save tokenizer config files
        fast_tokenizer = PreTrainedTokenizerFast(
            tokenizer_object=tokenizer,
            bos_token="[CLS]",
            eos_token="[SEP]",
            unk_token="[UNK]",
            pad_token="[PAD]",
            mask_token="[MASK]"
        )
        fast_tokenizer.save_pretrained(save_dir)
        print(f"[Checkpoint] Checkpoint '{checkpoint_dir_name}' successfully saved.")

    # ─────────────────────────────────────────────────────────────
    # Tokenizer Training
    # ─────────────────────────────────────────────────────────────
    def build_and_train_tokenizer(texts: list) -> Tokenizer:
        from tokenizers.models import BPE
        from tokenizers.trainers import BpeTrainer
        from tokenizers.pre_tokenizers import Whitespace
        
        vocab_path = os.path.join(model_dir, "bpe_vocab_16k.json")
        if os.path.exists(vocab_path):
            print(f"[Tokenizer] Loading trained BPE model layout from '{vocab_path}'...")
            return Tokenizer.from_file(vocab_path)
            
        print(f"[Tokenizer] Generating fresh HuggingFace BPE Tokenizer model with {VOCAB_SIZE} slots...")
        tokenizer = Tokenizer(BPE(unk_token="[UNK]"))
        tokenizer.pre_tokenizer = Whitespace()
        
        trainer = BpeTrainer(
            vocab_size=VOCAB_SIZE, 
            special_tokens=["[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]"]
        )
        tokenizer.train_from_iterator(texts, trainer)
        tokenizer.save(vocab_path)
        print(f"[Tokenizer] Tokenizer training completed and saved to '{vocab_path}'.")
        return tokenizer

    # ─────────────────────────────────────────────────────────────
    # Data Loader Setup
    # ─────────────────────────────────────────────────────────────
    class DataLoaderLite:
        def __init__(self, B: int, T: int, texts: list, tokenizer: Tokenizer, name: str):
            self.B = B
            self.T = T
            
            print(f"[DataLoader:{name}] Tokenising dataset sequences...")
            all_ids = []
            for t in texts:
                if t.strip():
                    encoded = tokenizer.encode(t).ids
                    all_ids.extend(encoded)

            self.tokens = torch.tensor(all_ids, dtype=torch.long)
            self.chunk_size = B * T
            self.n_chunks = (len(self.tokens) - 1) // self.chunk_size
            self.indices = list(range(self.n_chunks))
            self.pos = 0
            self._shuffle()
            
            print(f"[DataLoader:{name}] Total tokens: {len(self.tokens):,} | Epoch steps: {self.n_chunks:,}")

        def _shuffle(self):
            random.shuffle(self.indices)
            self.pos = 0

        def steps_per_epoch(self) -> int:
            return self.n_chunks

        def next_batch(self):
            B, T = self.B, self.T
            if self.pos >= len(self.indices):
                self._shuffle()
                
            chunk_idx = self.indices[self.pos]
            self.pos += 1
            
            start_pos = chunk_idx * self.chunk_size
            temp = self.tokens[start_pos : start_pos + self.chunk_size + 1]
            
            x = temp[:-1].view(B, T)
            y = temp[1:].view(B, T)
            return x, y

    # ─────────────────────────────────────────────────────────────
    # Batch preparation and schedules
    # ─────────────────────────────────────────────────────────────
    def get_current_mask_prob(global_step: int, total_steps: int) -> float:
        ratio = min(1.0, global_step / total_steps)
        return MASK_PROB_START + ratio * (MASK_PROB_END - MASK_PROB_START)

    def prepare_causal_batch(x: torch.Tensor, y: torch.Tensor):
        return x, y, False

    def prepare_masked_batch(x: torch.Tensor, y: torch.Tensor, mask_prob: float, mask_token_id: int):
        B, T = x.size()
        mask = torch.rand(B, T, device=x.device) < mask_prob
        masked_x = x.clone()
        masked_x[mask] = mask_token_id

        targets = torch.full_like(y, -100)
        targets[mask] = y[mask]

        return masked_x, targets, True

    def get_hybrid_batch(train_loader: DataLoaderLite, global_step: int, total_steps: int, device: torch.device):
        x, y = train_loader.next_batch()
        x, y = x.to(device), y.to(device)

        if random.random() < CAUSAL_RATIO:
            input_ids, targets, bidir = prepare_causal_batch(x, y)
        else:
            mask_prob = get_current_mask_prob(global_step, total_steps)
            input_ids, targets, bidir = prepare_masked_batch(x, y, mask_prob, MASK_TOKEN_ID)

        return input_ids, targets, bidir

    def get_lr(it: int, total_steps: int) -> float:
        if it < WARMUP_STEPS:
            return LEARNING_RATE * (it + 1) / WARMUP_STEPS
        if it >= total_steps:
            return LR_MIN
        decay_ratio = (it - WARMUP_STEPS) / (total_steps - WARMUP_STEPS)
        coeff        = 0.5 * (1.0 + math.cos(math.pi * decay_ratio))
        return LR_MIN + coeff * (LEARNING_RATE - LR_MIN)

    # ─────────────────────────────────────────────────────────────
    # Dataset Preparation
    # ─────────────────────────────────────────────────────────────
    print("\n[Data] Loading BabyLM-2026-Strict-Small ...")
    ds = load_dataset("BabyLM-community/BabyLM-2026-Strict-Small")
    all_text = list(ds['train']['text'])

    tokenizer = build_and_train_tokenizer(all_text)

    split       = int(len(all_text) * 0.95)
    train_texts = all_text[:split]
    val_texts   = all_text[split:]

    train_loader = DataLoaderLite(BATCH_SIZE, BLOCK_SIZE, train_texts, tokenizer, "train")
    val_loader   = DataLoaderLite(BATCH_SIZE, BLOCK_SIZE, val_texts, tokenizer, "val")

    chunks_per_epoch = train_loader.steps_per_epoch()
    steps_per_epoch  = chunks_per_epoch // GRAD_ACCUM_STEPS 
    total_steps      = steps_per_epoch * EPOCHS

    cfg   = XpertGPTModelConfig()
    device = "cuda" if torch.cuda.is_available() else "cpu"
    torch.manual_seed(42)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(42)
    random.seed(42)
    if hasattr(torch, 'set_float32_matmul_precision'):
        torch.set_float32_matmul_precision('high')

    model = XpertGPTModel(cfg).to(device)

    # ─────────────────────────────────────────────────────────────
    # Training Resume Check
    # ─────────────────────────────────────────────────────────────
    words_trained = 0
    next_milestone_idx = 0
    global_step = 0
    
    milestones = sorted(list(set([i * 1_000_000 for i in range(1, 11)] + [i * 10_000_000 for i in range(1, 11)])))
    
    resume_checkpoint_dir = None
    for idx in range(len(milestones) - 1, -1, -1):
        m = milestones[idx]
        ckpt_name = f"chck_{m // 1_000_000}M"
        ckpt_path = os.path.join(model_dir, ckpt_name)
        if os.path.exists(os.path.join(ckpt_path, "pytorch_model.bin")):
            config_json_path = os.path.join(ckpt_path, "config.json")
            if os.path.exists(config_json_path):
                try:
                    with open(config_json_path, "r") as f:
                        saved_config = json.load(f)
                    if saved_config.get("d_model") == 256:
                        resume_checkpoint_dir = ckpt_path
                        next_milestone_idx = idx + 1
                        words_trained = m
                        global_step = words_trained // (BATCH_SIZE * BLOCK_SIZE)
                        print(f"[Training] Found existing milestone checkpoint '{ckpt_name}'. Resuming from step {global_step:,} ({words_trained:,} tokens trained)...")
                        break
                    else:
                        print(f"[Training] Found checkpoint '{ckpt_name}' but it has mismatch d_model={saved_config.get('d_model')}. Starting fresh.")
                except Exception as e:
                    pass

    # Load weights if resuming
    if resume_checkpoint_dir is not None:
        print(f"[Model] Loading weights from checkpoint '{resume_checkpoint_dir}'...")
        state_dict = torch.load(os.path.join(resume_checkpoint_dir, "pytorch_model.bin"), map_location=device)
        model_state_dict = {}
        for k, v in state_dict.items():
            name = k
            if name.startswith("transformer."):
                name = name[12:]
            model_state_dict[name] = v
        model.load_state_dict(model_state_dict)

    # Check if final main model exists
    main_ckpt_path = os.path.join(model_dir, "main")
    if os.path.exists(os.path.join(main_ckpt_path, "pytorch_model.bin")):
        print("\n[Pipeline] Final checkpoint 'main' already exists. Skipping training phase and transitioning directly to evaluations!")
    else:
        # torch.compile
        try:
            model = torch.compile(model)
            print("[Model] torch.compile() successfully verified graph optimizations")
        except Exception as e:
            print(f"[Model] torch.compile() skipped ({e})")

        # Optimizer
        param_dict     = {n: p for n, p in model.named_parameters() if p.requires_grad}
        decay_params   = [p for p in param_dict.values() if p.dim() >= 2]
        nodecay_params = [p for p in param_dict.values() if p.dim() < 2]
        groups = [
            {'params': decay_params,   'weight_decay': WEIGHT_DECAY},
            {'params': nodecay_params, 'weight_decay': 0.0},
        ]
        fused_ok  = 'fused' in inspect.signature(torch.optim.AdamW).parameters
        use_fused = fused_ok and ('cuda' in device)
        optimizer = torch.optim.AdamW(groups, lr=LEARNING_RATE, betas=(0.9, 0.95), eps=1e-8, fused=use_fused)

        # Helper for validation loss calculation
        def evaluate_validation_loss(model_eval, val_loader_eval, dev, autocast):
            was_training = model_eval.training
            model_eval.eval()
            
            from modeling_xpertgpt import ROUTER_TRACKER
            old_tracker_enabled = ROUTER_TRACKER["enabled"]
            ROUTER_TRACKER["enabled"] = False
            
            val_loss_accum = 0.0
            val_steps = min(val_loader_eval.steps_per_epoch(), 50)
            with torch.no_grad():
                for _ in range(val_steps):
                    x, y = val_loader_eval.next_batch()
                    x, y = x.to(dev), y.to(dev)
                    with autocast:
                        _, loss = model_eval(x, y, bidirectional=False)
                    val_loss_accum += loss.item()
            if was_training:
                model_eval.train()
                
            ROUTER_TRACKER["enabled"] = old_tracker_enabled
            return val_loss_accum / val_steps

        loss_records = []
        checkpoint_perplexities = []
        
        from modeling_xpertgpt import ROUTER_TRACKER
        ROUTER_TRACKER["enabled"] = True
        ROUTER_TRACKER["expert_counts"] = []

        model.train()
        autocast_ctx = torch.autocast(device_type="cuda" if "cuda" in device else "cpu", dtype=torch.bfloat16, enabled=True)

        start_epoch = global_step // steps_per_epoch
        start_chunk = (global_step % steps_per_epoch) * GRAD_ACCUM_STEPS

        print(f"\n[Training] Starting XpertGPT MoEP training for {EPOCHS} epochs...")
        for epoch in range(start_epoch, EPOCHS):
            train_loader._shuffle()
            if epoch == start_epoch and start_chunk > 0:
                print(f"[Training] Fast-forwarding dataloader to chunk index {start_chunk}...")
                train_loader.pos = start_chunk

            optimizer.zero_grad(set_to_none=True)
            loss_accum = 0.0
            
            start_chunk_idx = start_chunk if epoch == start_epoch else 0
            for chunk_step in range(start_chunk_idx, chunks_per_epoch):
                t0 = time.perf_counter()

                lr = get_lr(global_step, total_steps)
                for pg in optimizer.param_groups:
                    pg['lr'] = lr

                input_ids, targets, bidir = get_hybrid_batch(train_loader, global_step, total_steps, device)
                words_trained += input_ids.numel()

                with autocast_ctx:
                    _, loss = model(input_ids, targets, bidirectional=bidir)
                scaled_loss  = loss / GRAD_ACCUM_STEPS
                loss_accum  += scaled_loss.item()
                scaled_loss.backward()

                # Optimizer Step
                if (chunk_step + 1) % GRAD_ACCUM_STEPS == 0:
                    norm = torch.nn.utils.clip_grad_norm_(model.parameters(), GRAD_CLIP)
                    optimizer.step()
                    optimizer.zero_grad(set_to_none=True)
                    if "cuda" in device:
                        torch.cuda.synchronize()

                    dt = (time.perf_counter() - t0) * 1000
                    mode_tag = "MLM" if bidir else "CLM"
                    mask_p   = get_current_mask_prob(global_step, total_steps)
                    
                    current_step = (chunk_step + 1) // GRAD_ACCUM_STEPS
                    print(
                        f"[E{epoch+1:02d} {current_step:>5d}/{steps_per_epoch} G{global_step:>7d}|{mode_tag}] "
                        f"train={loss_accum:.4f}  mask={mask_p:.1%}  norm={norm:.3f}  lr={lr:.2e}  dt={dt:6.1f}ms  words={words_trained:,}"
                    )
                    
                    # Record validation and training loss every 100 steps
                    if (global_step + 1) % 100 == 0:
                        val_loss = evaluate_validation_loss(model, val_loader, device, autocast_ctx)
                        loss_records.append({
                            "step": global_step + 1,
                            "train_loss": loss_accum,
                            "val_loss": val_loss
                        })
                        with open("loss_records.json", "w") as f:
                            json.dump(loss_records, f, indent=2)
                        if os.path.exists(model_dir):
                            with open(os.path.join(model_dir, "loss_records.json"), "w") as f:
                                json.dump(loss_records, f, indent=2)
                        print(f"[Metrics G{global_step+1}] Recorded train_loss={loss_accum:.4f}, val_loss={val_loss:.4f}")
                        
                    loss_accum = 0.0
                    global_step += 1

                # Check if we passed a milestone for checkpointing
                if next_milestone_idx < len(milestones) and words_trained >= milestones[next_milestone_idx]:
                    milestone_val = milestones[next_milestone_idx]
                    if milestone_val < 10_000_000:
                        milestone_name = f"chck_{milestone_val // 1_000_000}M"
                    else:
                        milestone_name = f"chck_{(milestone_val // 10_000_000) * 10}M"
                    
                    raw_model = model._orig_mod if hasattr(model, '_orig_mod') else model
                    save_hf_checkpoint(raw_model, milestone_name, tokenizer)
                    
                    # Calculate validation perplexity at this milestone
                    val_loss = evaluate_validation_loss(model, val_loader, device, autocast_ctx)
                    val_ppl = math.exp(val_loss)
                    checkpoint_perplexities.append({
                        "checkpoint": milestone_name,
                        "words_trained": milestone_val,
                        "global_step": global_step,
                        "val_loss": val_loss,
                        "val_perplexity": val_ppl
                    })
                    with open("checkpoint_perplexities.json", "w") as f:
                        json.dump(checkpoint_perplexities, f, indent=2)
                    if os.path.exists(model_dir):
                        with open(os.path.join(model_dir, "checkpoint_perplexities.json"), "w") as f:
                            json.dump(checkpoint_perplexities, f, indent=2)
                    print(f"[Milestone {milestone_name}] Evaluated val_perplexity={val_ppl:.2f}")

                    next_milestone_idx += 1

        # Save final model as 'main'
        raw_model = model._orig_mod if hasattr(model, '_orig_mod') else model
        save_hf_checkpoint(raw_model, "main", tokenizer)
        
        # Calculate validation perplexity for final checkpoint
        val_loss = evaluate_validation_loss(model, val_loader, device, autocast_ctx)
        val_ppl = math.exp(val_loss)
        checkpoint_perplexities.append({
            "checkpoint": "main",
            "words_trained": words_trained,
            "global_step": global_step,
            "val_loss": val_loss,
            "val_perplexity": val_ppl
        })
        with open("checkpoint_perplexities.json", "w") as f:
            json.dump(checkpoint_perplexities, f, indent=2)
        if os.path.exists(model_dir):
            with open(os.path.join(model_dir, "checkpoint_perplexities.json"), "w") as f:
                json.dump(checkpoint_perplexities, f, indent=2)
        print(f"[Final Checkpoint main] Evaluated val_perplexity={val_ppl:.2f}")
        
        print("\n[Training] Training phase complete!")

        # ─────────────────────────────────────────────────────────────
        # Router utilization report and plot generation
        # ─────────────────────────────────────────────────────────────
        from modeling_xpertgpt import ROUTER_TRACKER
        total_expert_counts = [0, 0, 0, 0, 0]
        for cnt in ROUTER_TRACKER["expert_counts"]:
            for i in range(5):
                total_expert_counts[i] += cnt[i]
                
        total_tokens = sum(total_expert_counts)
        if total_tokens > 0:
            fractions = [total_expert_counts[i] / total_tokens for i in range(5)]
        else:
            fractions = [0.0] * 5
            
        fraction_no_expert = fractions[0]
        fraction_multiple_experts = sum(fractions[2:])
        
        report_text = f"""=== ROUTER UTILIZATION REPORT ===

Total tokens routed: {total_tokens:,}



Number of experts selected per token (0, 1, 2, 3, 4):

- 0 experts: {total_expert_counts[0]:,} tokens ({fractions[0]:.2%})

- 1 expert:  {total_expert_counts[1]:,} tokens ({fractions[1]:.2%})

- 2 experts: {total_expert_counts[2]:,} tokens ({fractions[2]:.2%})

- 3 experts: {total_expert_counts[3]:,} tokens ({fractions[3]:.2%})

- 4 experts: {total_expert_counts[4]:,} tokens ({fractions[4]:.2%})



Summary metrics:

- Fraction of tokens receiving NO expert: {fraction_no_expert:.2%}

- Fraction of tokens receiving MULTIPLE experts: {fraction_multiple_experts:.2%}

"""
        with open("router_report.txt", "w") as f:
            f.write(report_text)
        if os.path.exists(model_dir):
            with open(os.path.join(model_dir, "router_report.txt"), "w") as f:
                f.write(report_text)
        print(report_text)

        # Generate bar chart
        try:
            import matplotlib
            matplotlib.use('Agg')
            import matplotlib.pyplot as plt
            
            categories = ['0', '1', '2', '3', '4']
            plt.figure(figsize=(8, 5))
            plt.bar(categories, [f * 100 for f in fractions], color='#10b981', edgecolor='#059669', width=0.6)
            plt.xlabel('Number of Experts Selected per Token')
            plt.ylabel('Percentage of Tokens (%)')
            plt.title('Router Utilization / Expert Choices per Token')
            plt.grid(axis='y', linestyle='--', alpha=0.6)
            
            for i, f in enumerate(fractions):
                plt.text(i, f * 100 + 1, f"{f:.2%}", ha='center', fontweight='bold')
                
            plt.ylim(0, max([f * 100 for f in fractions]) + 10)
            plt.savefig('router_utilization.png', dpi=150)
            if os.path.exists(model_dir):
                plt.savefig(os.path.join(model_dir, 'router_utilization.png'), dpi=150)
            plt.close()
            print("[Plots] Router utilization plot saved successfully.")
        except Exception as e:
            print(f"[Plots] Warning: Could not generate plots: {e}")

    if skip_eval:
        print("[Pipeline] Skipping evaluations phase as requested.")
        return

    # ─────────────────────────────────────────────────────────────
    # 2. RUN EVALUATION PIPELINE
    # ─────────────────────────────────────────────────────────────
    local_results_dir = os.path.abspath(f"./results/{model_name}")
    os.makedirs(local_results_dir, exist_ok=True)
    local_main_res = os.path.join(local_results_dir, "main")

    # Ensure clone_dir exists and has the global_piqa files
    clone_parent = os.path.abspath("./babylm_eval_repo")
    
    # Self-healing check for global_piqa presence
    has_global_piqa = False
    for potential_strict in [os.path.join(clone_parent, "babylm-eval", "strict"), os.path.join(clone_parent, "strict")]:
        if os.path.exists(os.path.join(potential_strict, "evaluation_pipeline", "global_piqa")):
            has_global_piqa = True
            break
            
    if not has_global_piqa:
        print("[Eval] Cloned repository does not contain global_piqa tasks.")
        print("[Eval] Deleting and cloning official main branch...")
        if os.path.exists(clone_parent):
            shutil.rmtree(clone_parent)
        subprocess.run([
            "git", "clone", "-b", "main",
            "https://github.com/babylm-org/babylm-eval.git",
            clone_parent
        ], check=True)

    # Determine strict_dir path dynamically
    if os.path.exists(os.path.join(clone_parent, "strict")):
        strict_dir = os.path.join(clone_parent, "strict")
    else:
        strict_dir = os.path.join(clone_parent, "babylm-eval", "strict")
        
    print(f"[Eval] Using strict directory: {strict_dir}")
    os.environ["PYTHONPATH"] = strict_dir

    def patch_evaluation_run_script(strict_dir):
        import pathlib
        run_file = os.path.join(strict_dir, "evaluation_pipeline", "sentence_zero_shot", "run.py")
        if not os.path.exists(run_file):
            print(f"[GlobalPIQA] Warning: {run_file} not found. Cannot patch.")
            return
            
        print(f"[GlobalPIQA] Patching local checkpoint loader in {run_file}...")
        with open(run_file, "r") as f:
            content = f.read()
            
        # Check if already patched
        if "Local checkpoint directory patch" in content:
            print("[GlobalPIQA] Script already patched.")
            return
            
        target_str = """def main():

    args = _parse_arguments()

    if args.images_path is not None:

        assert args.batch_size == 1, "Multimodal only works in batch size 1!"

    dataset = args.data_path.stem

    args.model_name = pathlib.Path(args.model_path_or_name).stem

    if args.revision_name is None:

        revision_name = "main"

    else:

        revision_name = args.revision_name"""
            
        patch_str = """def main():

    args = _parse_arguments()

    if args.images_path is not None:

        assert args.batch_size == 1, "Multimodal only works in batch size 1!"

    dataset = args.data_path.stem

    

    # Local checkpoint directory patch

    import os

    model_path = args.model_path_or_name

    args.model_name = pathlib.Path(model_path).stem

    revision_name = args.revision_name if args.revision_name else "main"

    

    if os.path.isdir(model_path):

        target_revision = args.revision_name if args.revision_name else "main"

        if os.path.exists(os.path.join(model_path, target_revision)):

            args.model_path_or_name = os.path.join(model_path, target_revision)

            args.revision_name = None"""
                
        if target_str in content:
            new_content = content.replace(target_str, patch_str)
            with open(run_file, "w") as f:
                f.write(new_content)
            print("[GlobalPIQA] Successfully patched run.py")
        else:
            print("[GlobalPIQA] Warning: Could not find target pattern in run.py. Manual patch may be needed.")

    # Patch sentence zero shot loader inside cloned repo
    patch_evaluation_run_script(strict_dir)

    print("[Eval] Stripping Windows-specific packages from requirements.txt...")
    req_file_path = os.path.join(strict_dir, "requirements.txt")
    if os.path.exists(req_file_path):
        with open(req_file_path, "r") as f:
            lines = f.readlines()
        with open(req_file_path, "w") as f:
            for line in lines:
                if "pywin" not in line.lower() and "wintypes" not in line.lower():
                    f.write(line)

    print("[Eval] Verifying and installing evaluation dependencies programmatically...")
    required_packages = {
        "nltk": "nltk",
        "pandas": "pandas",
        "statsmodels": "statsmodels",
        "sklearn": "scikit-learn",
        "scipy": "scipy"
    }
    for pkg_import, pkg_install in required_packages.items():
        try:
            __import__(pkg_import)
        except ImportError:
            print(f"[Eval] Package '{pkg_install}' not found. Installing it programmatically...")
            import sys
            subprocess.run([sys.executable, "-m", "pip", "install", pkg_install], check=True)
    
    print("[Eval] Downloading NLTK tokenizer resources...")
    import nltk
    nltk.download('punkt', download_dir=os.environ["NLTK_DATA"])
    nltk.download('punkt_tab', download_dir=os.environ["NLTK_DATA"])

    # Ensure standard zero-shot datasets are downloaded
    blimp_fast_dir = os.path.join(strict_dir, "evaluation_data", "fast_eval", "blimp_fast")
    if not os.path.exists(blimp_fast_dir) or not os.listdir(blimp_fast_dir):
        print("[Eval] Standard zero-shot datasets not found. Downloading...")
        subprocess.run(["python", "-m", "scripts.download_evals"], cwd=strict_dir, check=True)
        
        # Unzip EWoK fast
        ewok_zip = os.path.join(strict_dir, "evaluation_data/fast_eval/ewok_fast.zip")
        if os.path.exists(ewok_zip):
            print("[Eval] Unzipping EWoK fast data...")
            bad_nested_dir = os.path.join(strict_dir, "evaluation_data/fast_eval/evaluation_data")
            if os.path.exists(bad_nested_dir):
                shutil.rmtree(bad_nested_dir)
            subprocess.run(["unzip", "-o", "-P", "BabyLM2025", "evaluation_data/fast_eval/ewok_fast.zip", "-d", "."], cwd=strict_dir, check=True)
            
        # Download EWoK full
        print("[Eval] Downloading and filtering full EWoK dataset...")
        subprocess.run(["python", "-m", "evaluation_pipeline.ewok.dl_and_filter"], cwd=strict_dir, check=True)

    # Download GlobalPIQA dataset
    global_piqa_parallel_dir = os.path.join(strict_dir, "evaluation_data", "fast_eval", "global_piqa_parallel")
    if not os.path.exists(global_piqa_parallel_dir) or not os.listdir(global_piqa_parallel_dir):
        print("[Eval] GlobalPIQA dataset not found. Downloading...")
        subprocess.run(["python", "evaluation_pipeline/global_piqa/dl.py"], cwd=strict_dir, check=True)

    # Ensure all scripts are executable
    print("[Eval] Making evaluation shell scripts executable...")
    subprocess.run("chmod +x scripts/*.sh", shell=True, cwd=strict_dir, check=True)

    def run_task_with_cache(checkpoint, task, output_subpath, cmd):
        # Determine paths
        local_cache_path = os.path.join("./results", model_name, checkpoint, "zero_shot", "causal", task, output_subpath)
        if task == "reading":
            local_cache_path = os.path.join("./results", model_name, checkpoint, "zero_shot", "causal", "reading")
        elif task == "comps":
            local_cache_path = os.path.join("./results", model_name, checkpoint, "zero_shot", "causal", "comps", "comps")
            
        target_results_dir = os.path.join(strict_dir, "results", model_name, checkpoint, "zero_shot", "causal", task, output_subpath)
        if task == "reading":
            target_results_dir = os.path.join(strict_dir, "results", model_name, checkpoint, "zero_shot", "causal", "reading")
        elif task == "comps":
            target_results_dir = os.path.join(strict_dir, "results", model_name, checkpoint, "zero_shot", "causal", "comps", "comps")

        # If cached, copy it over
        cache_file = os.path.join(local_cache_path, "predictions.json")
        
        # Self-healing: invalidate old unfiltered entity_tracking caches
        if task == "entity_tracking" and os.path.exists(cache_file):
            try:
                import json
                with open(cache_file, "r") as f:
                    preds = json.load(f)
                is_valid_cache = True
                for k, v in preds.items():
                    if len(v.get("predictions", [])) in [605, 606, 607, 615, 529, 156, 187, 159]:
                        is_valid_cache = False
                        break
                if not is_valid_cache:
                    print(f"[Eval] Cached entity_tracking for '{checkpoint}' has incorrect old sizes. Invalidate and re-run fresh...")
                    shutil.rmtree(local_cache_path, ignore_errors=True)
            except Exception:
                pass

        if os.path.exists(cache_file):
            print(f"[Eval] Task '{task}' ({output_subpath}) for checkpoint '{checkpoint}' is cached. Restoring...")
            if os.path.exists(target_results_dir):
                shutil.rmtree(target_results_dir)
            os.makedirs(target_results_dir, exist_ok=True)
            for item in os.listdir(local_cache_path):
                s = os.path.join(local_cache_path, item)
                d = os.path.join(target_results_dir, item)
                if os.path.isdir(s):
                    shutil.copytree(s, d)
                else:
                    shutil.copy2(s, d)
            return

        print(f"[Eval] Running task '{task}' ({output_subpath}) for checkpoint '{checkpoint}'...")
        subprocess.run(cmd, cwd=strict_dir, check=True)

        # Relocate from results/main if needed
        possible_main_path = os.path.join(strict_dir, "results", "main", checkpoint, "zero_shot", "causal", task, output_subpath)
        if task == "reading":
            possible_main_path = os.path.join(strict_dir, "results", "main", checkpoint, "zero_shot", "causal", "reading")
        elif task == "comps":
            possible_main_path = os.path.join(strict_dir, "results", "main", checkpoint, "zero_shot", "causal", "comps", "comps")

        if os.path.exists(possible_main_path) and possible_main_path != target_results_dir:
            print(f"[Eval] Relocating results from {possible_main_path} to {target_results_dir}...")
            if os.path.exists(target_results_dir):
                shutil.rmtree(target_results_dir)
            os.makedirs(os.path.dirname(target_results_dir), exist_ok=True)
            shutil.move(possible_main_path, target_results_dir)

        # Verify
        verify_eval_run(target_results_dir, f"{checkpoint} {task} ({output_subpath})")

        # Save to local cache
        if os.path.exists(local_cache_path):
            shutil.rmtree(local_cache_path)
        os.makedirs(local_cache_path, exist_ok=True)
        for item in os.listdir(target_results_dir):
            s = os.path.join(target_results_dir, item)
            d = os.path.join(local_cache_path, item)
            if os.path.isdir(s):
                shutil.copytree(s, d)
            else:
                shutil.copy2(s, d)

    def run_finetune_task_with_cache(task, cmd):
        local_cache_path = os.path.join("./results", model_name, "main", "finetune", task)
        target_results_dir = os.path.join(strict_dir, "results", model_name, "main", "finetune", task)

        if os.path.exists(os.path.join(local_cache_path, "predictions.json")):
            print(f"[Eval] GLUE task '{task}' is cached. Restoring...")
            if os.path.exists(target_results_dir):
                shutil.rmtree(target_results_dir)
            os.makedirs(target_results_dir, exist_ok=True)
            for item in os.listdir(local_cache_path):
                s = os.path.join(local_cache_path, item)
                d = os.path.join(target_results_dir, item)
                if os.path.isdir(s):
                    shutil.copytree(s, d)
                else:
                    shutil.copy2(s, d)
            return

        print(f"[Eval] Running GLUE task '{task}'...")
        subprocess.run(cmd, cwd=strict_dir, check=True)

        # Relocate from results/main/main if needed
        possible_main_path = os.path.join(strict_dir, "results", "main", "main", "finetune", task)
        if os.path.exists(possible_main_path) and possible_main_path != target_results_dir:
            print(f"[Eval] Relocating results from {possible_main_path} to {target_results_dir}...")
            if os.path.exists(target_results_dir):
                shutil.rmtree(target_results_dir)
            os.makedirs(os.path.dirname(target_results_dir), exist_ok=True)
            shutil.move(possible_main_path, target_results_dir)

        # Verify
        verify_eval_run(target_results_dir, f"GLUE task {task}")

        # Cache locally
        if os.path.exists(local_cache_path):
            shutil.rmtree(local_cache_path)
        os.makedirs(local_cache_path, exist_ok=True)
        for item in os.listdir(target_results_dir):
            s = os.path.join(target_results_dir, item)
            d = os.path.join(local_cache_path, item)
            if os.path.isdir(s):
                shutil.copytree(s, d)
            else:
                shutil.copy2(s, d)

    def run_aoa_with_cache(cmd):
        local_cache_path = os.path.join("./results", model_name, "main", "aoa")
        target_results_dir = os.path.join(strict_dir, "results", model_name, "main", "aoa")

        if os.path.exists(os.path.join(local_cache_path, "aoa_score.json")) or os.path.exists(os.path.join(local_cache_path, "surprisal.json")):
            print(f"[Eval] AoA task is cached. Restoring...")
            if os.path.exists(target_results_dir):
                shutil.rmtree(target_results_dir)
            os.makedirs(target_results_dir, exist_ok=True)
            for item in os.listdir(local_cache_path):
                s = os.path.join(local_cache_path, item)
                d = os.path.join(target_results_dir, item)
                if os.path.isdir(s):
                    shutil.copytree(s, d)
                else:
                    shutil.copy2(s, d)
            return

        print("[Eval] Running AoA task...")
        subprocess.run(cmd, cwd=strict_dir, check=True)

        # Relocate from results/main/main if needed
        possible_main_path = os.path.join(strict_dir, "results", "main", "main", "aoa")
        if os.path.exists(possible_main_path) and possible_main_path != target_results_dir:
            print(f"[Eval] Relocating results from {possible_main_path} to {target_results_dir}...")
            if os.path.exists(target_results_dir):
                shutil.rmtree(target_results_dir)
            os.makedirs(os.path.dirname(target_results_dir), exist_ok=True)
            shutil.move(possible_main_path, target_results_dir)

        # Verify
        verify_eval_run(target_results_dir, "AoA task")

        # Cache locally
        if os.path.exists(local_cache_path):
            shutil.rmtree(local_cache_path)
        os.makedirs(local_cache_path, exist_ok=True)
        for item in os.listdir(target_results_dir):
            s = os.path.join(target_results_dir, item)
            d = os.path.join(local_cache_path, item)
            if os.path.isdir(s):
                shutil.copytree(s, d)
            else:
                shutil.copy2(s, d)

    # ─────────────────────────────────────────────────────────────
    # B. FINAL MODEL 'main' FULL ZERO-SHOT EVALUATION
    # ─────────────────────────────────────────────────────────────
    main_ckpt_path = os.path.join(model_dir, "main")
    if os.path.exists(main_ckpt_path):
        print(f"[Eval] Running full zero-shot evaluation on main...")
        # blimp filtered
        run_task_with_cache(
            "main", "blimp", "blimp_filtered",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", main_ckpt_path, "--backend", "causal", "--task", "blimp", "--data_path", "evaluation_data/full_eval/blimp_filtered", "--save_predictions"]
        )
        # supplement filtered
        run_task_with_cache(
            "main", "blimp", "supplement_filtered",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", main_ckpt_path, "--backend", "causal", "--task", "blimp", "--data_path", "evaluation_data/full_eval/supplement_filtered", "--save_predictions"]
        )
        # ewok filtered
        run_task_with_cache(
            "main", "ewok", "ewok_filtered",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", main_ckpt_path, "--backend", "causal", "--task", "ewok", "--data_path", "evaluation_data/full_eval/ewok_filtered", "--save_predictions"]
        )
        # entity tracking
        run_task_with_cache(
            "main", "entity_tracking", "entity_tracking",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", main_ckpt_path, "--backend", "causal", "--task", "entity_tracking", "--data_path", "evaluation_data/full_eval/entity_tracking", "--save_predictions"]
        )
        # comps
        run_task_with_cache(
            "main", "comps", "comps",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", main_ckpt_path, "--backend", "causal", "--task", "comps", "--data_path", "evaluation_data/full_eval/comps", "--save_predictions"]
        )
        # reading
        run_task_with_cache(
            "main", "reading", "reading",
            ["python", "-m", "evaluation_pipeline.reading.run", "--model_path_or_name", main_ckpt_path, "--backend", "causal", "--data_path", "evaluation_data/full_eval/reading/reading_data.csv"]
        )
        # global piqa parallel
        run_task_with_cache(
            "main", "global_piqa_parallel", "global_piqa_parallel",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", main_ckpt_path, "--backend", "causal", "--task", "global_piqa_parallel", "--data_path", "evaluation_data/full_eval/global_piqa_parallel", "--save_predictions"]
        )
        # global piqa nonparallel
        run_task_with_cache(
            "main", "global_piqa_nonparallel", "global_piqa_nonparallel",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", main_ckpt_path, "--backend", "causal", "--task", "global_piqa_nonparallel", "--data_path", "evaluation_data/full_eval/global_piqa_nonparallel", "--save_predictions"]
        )

    # ─────────────────────────────────────────────────────────────
    # C. FINAL MODEL GLUE AND AOA EVALUATION
    # ─────────────────────────────────────────────────────────────
    if os.path.exists(main_ckpt_path):
        # 1. GLUE fine-tuning
        if skip_glue:
            print("[Eval] Skipping GLUE fine-tuning evaluations as requested.")
        else:
            print("[Eval] Running GLUE fine-tuning evaluations on main task-by-task...")
            glue_tasks = {
                "boolq": ["boolq", "16", "10"],
                "multirc": ["multirc", "16", "10"],
                "rte": ["rte", "32", "10"],
                "wsc": ["wsc", "32", "30"],
                "mrpc": ["mrpc", "32", "10"],
                "qqp": ["qqp", "32", "10"],
                "mnli": ["mnli", "32", "10"]
            }
            for task_name, (task, bsz, max_epochs) in glue_tasks.items():
                num_labels = "3" if task == "mnli" else "2"
                metric_for_valid = "accuracy"
                if task in ["mrpc", "qqp"]:
                    metric_for_valid = "f1"
                metrics = ["accuracy"]
                if task != "mnli":
                    metrics = ["accuracy", "f1", "mcc"]
                
                cmd = [
                    "python", "-m", "evaluation_pipeline.finetune.run",
                    "--model_name_or_path", main_ckpt_path,
                    "--train_data", f"evaluation_data/full_eval/glue_filtered/{task}.train.jsonl",
                    "--valid_data", f"evaluation_data/full_eval/glue_filtered/{task}.valid.jsonl",
                    "--predict_data", f"evaluation_data/full_eval/glue_filtered/{task}.valid.jsonl",
                    "--task", task,
                    "--num_labels", num_labels,
                    "--batch_size", bsz,
                    "--learning_rate", "3e-5",
                    "--num_epochs", max_epochs,
                    "--sequence_length", "512",
                    "--results_dir", "results",
                    "--save",
                    "--save_dir", "models",
                    "--metric_for_valid", metric_for_valid,
                    "--seed", "42",
                    "--verbose",
                    "--padding_side", "left",
                    "--take_final"
                ]
                cmd.append("--metrics")
                cmd.extend(metrics)
                
                run_finetune_task_with_cache(task_name, cmd)

        # 2. AoA
        if skip_aoa:
            print("[Eval] Skipping AoA evaluations as requested.")
        else:
            run_aoa_with_cache([
                "python", "-m", "evaluation_pipeline.AoA_word.run",
                "--model_name", model_dir,
                "--backend", "causal",
                "--track_name", "strict-small",
                "--word_path", "evaluation_data/full_eval/aoa/cdi_childes.json",
                "--output_dir", "results"
            ])

    # ─────────────────────────────────────────────────────────────
    # A. INTERMEDIATE CHECKPOINTS FAST EVALUATION
    # ─────────────────────────────────────────────────────────────
    print(f"[Eval] Running zero-shot fast evaluations on intermediate checkpoints...")
    checkpoints = [f"chck_{i}M" for i in range(1, 10)] + [f"chck_{i}M" for i in range(10, 110, 10)]
    
    for checkpoint in checkpoints:
        ckpt_full_path = os.path.join(model_dir, checkpoint)
        if not os.path.exists(ckpt_full_path):
            continue
            
        # blimp fast
        run_task_with_cache(
            checkpoint, "blimp", "blimp_fast",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", model_dir, "--backend", "causal", "--task", "blimp", "--data_path", "evaluation_data/fast_eval/blimp_fast", "--save_predictions", "--revision_name", checkpoint]
        )
        # supplement fast
        run_task_with_cache(
            checkpoint, "blimp", "supplement_fast",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", model_dir, "--backend", "causal", "--task", "blimp", "--data_path", "evaluation_data/fast_eval/supplement_fast", "--save_predictions", "--revision_name", checkpoint]
        )
        # ewok fast
        run_task_with_cache(
            checkpoint, "ewok", "ewok_fast",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", model_dir, "--backend", "causal", "--task", "ewok", "--data_path", "evaluation_data/fast_eval/ewok_fast", "--save_predictions", "--revision_name", checkpoint]
        )
        # entity tracking fast
        run_task_with_cache(
            checkpoint, "entity_tracking", "entity_tracking_fast",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", model_dir, "--backend", "causal", "--task", "entity_tracking", "--data_path", "evaluation_data/fast_eval/entity_tracking_fast", "--save_predictions", "--revision_name", checkpoint]
        )
        # reading fast
        run_task_with_cache(
            checkpoint, "reading", "reading",
            ["python", "-m", "evaluation_pipeline.reading.run", "--model_path_or_name", model_dir, "--backend", "causal", "--data_path", "evaluation_data/fast_eval/reading/reading_data.csv", "--revision_name", checkpoint]
        )
        # global piqa parallel fast
        run_task_with_cache(
            checkpoint, "global_piqa_parallel", "global_piqa_parallel",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", model_dir, "--backend", "causal", "--task", "global_piqa_parallel", "--data_path", "evaluation_data/fast_eval/global_piqa_parallel", "--save_predictions", "--revision_name", checkpoint]
        )
        # global piqa nonparallel fast
        run_task_with_cache(
            checkpoint, "global_piqa_nonparallel", "global_piqa_nonparallel",
            ["python", "-m", "evaluation_pipeline.sentence_zero_shot.run", "--model_path_or_name", model_dir, "--backend", "causal", "--task", "global_piqa_nonparallel", "--data_path", "evaluation_data/fast_eval/global_piqa_nonparallel", "--save_predictions", "--revision_name", checkpoint]
        )

    # ─────────────────────────────────────────────────────────────
    # D. COLLATE RESULTS AND CLEANUP
    # ─────────────────────────────────────────────────────────────
    print("[Eval] Collating predictions into submission file...")
    # Clean collation destination in evaluation repo
    collate_results_dir = os.path.join(strict_dir, "results", model_name)
    if os.path.exists(collate_results_dir):
        shutil.rmtree(collate_results_dir)
    os.makedirs(os.path.dirname(collate_results_dir), exist_ok=True)
    
    # Copy from local cache results to strict results for collation
    shutil.copytree(local_results_dir, collate_results_dir)

    # Run collation
    subprocess.run([
        "python", "-m", "evaluation_pipeline.collate_preds",
        "--model_path_or_name", model_name,
        "--backend", "causal",
        "--track", "strict-small",
        "--fast"
    ], cwd=strict_dir, check=True)
    
    # Save results to local folder
    results_src = os.path.join(strict_dir, "results")
    results_dest = os.path.abspath("./results")
    if os.path.exists(results_dest):
        shutil.rmtree(results_dest)
    shutil.copytree(results_src, results_dest)
    
    # Copy final collated json to current folder
    collated_json = os.path.join(strict_dir, "all_full_preds_and_fast_scores_causal.json")
    if os.path.exists(collated_json):
        shutil.copy(collated_json, "./all_full_preds_and_fast_scores_causal.json")
        print("\n[Eval] Success! Collation completed! Final file is at './all_full_preds_and_fast_scores_causal.json'")

    print("\n[Eval] Pipeline evaluation run finished.")

def upload_pipeline(model_name, repo_name, token=None):
    import os
    import shutil
    import json
    import hashlib
    from huggingface_hub import HfApi, create_repo

    if not token:
        token = os.environ.get("HF_TOKEN")

    api = HfApi(token=token)
    try:
        user_info = api.whoami()
        username = user_info["name"]
        print(f"[HF] Authenticated successfully as user: {username}")
    except Exception as e:
        print(f"[HF] Authentication failed. Error: {e}")
        return

    repo_id = f"{username}/{repo_name}"
    print(f"[HF] Target Repository ID: {repo_id}")
    
    # Create the repository if it doesn't exist
    try:
        create_repo(repo_id=repo_id, repo_type="model", token=token, exist_ok=True)
        print(f"[HF] Repository '{repo_id}' is ready.")
    except Exception as e:
        print(f"[HF] Failed to verify or create repository. Error: {e}")
        return

    checkpoint_dir = os.path.abspath(f"./checkpoints/{model_name}")
    
    # Resolve the main checkpoint directory using self-healing rules
    revisions = {}
    if os.path.exists("pytorch_model.bin") and os.path.exists("config.json"):
        print("[HF] Detected weight and config files in the current working directory. Using current folder as 'main' checkpoint.")
        revisions = {"main": os.getcwd()}
    elif os.path.exists(os.path.join(checkpoint_dir, "main")):
        revisions = {"main": os.path.join(checkpoint_dir, "main")}
    elif os.path.exists(os.path.abspath("./checkpoints/msit_gptbert_fresh/main")):
        print("[HF] Using fallback checkpoint folder './checkpoints/msit_gptbert_fresh/main'...")
        revisions = {"main": os.path.abspath("./checkpoints/msit_gptbert_fresh/main")}
    elif os.path.exists(os.path.abspath("./checkpoints/main")):
        revisions = {"main": os.path.abspath("./checkpoints/main")}
    else:
        print(f"[HF] Error: Could not locate the 'main' checkpoint weights. Checked: {checkpoint_dir}/main, current directory, and fallbacks.")
        return

    # Check for intermediate checkpoints relative to the main checkpoint's parent folder
    main_dir = revisions["main"]
    parent_dir = os.path.dirname(main_dir)
    
    for m in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100]:
        ckpt_name = f"chck_{m}M"
        ckpt_path = os.path.join(parent_dir, ckpt_name)
        if os.path.exists(ckpt_path):
            revisions[ckpt_name] = ckpt_path
        else:
            # Check if there is a .pt file in the parent folder
            pt_path = os.path.join(parent_dir, f"{ckpt_name}.pt")
            if os.path.exists(pt_path):
                print(f"[HF] Found legacy checkpoint file '{ckpt_name}.pt'. Converting to HF format for upload...")
                import torch
                from transformers import AutoTokenizer
                from tokenizers import Tokenizer
                try:
                    from modeling_xpertgpt import XpertGPTForCausalLM, XpertGPTConfig
                    cfg = XpertGPTConfig(d_model=256, d_thin=384, num_layers=6, num_blocks=4)
                    model_to_save = XpertGPTForCausalLM(cfg)
                    sd = torch.load(pt_path, map_location="cpu")
                    clean_sd = {}
                    for k, v in sd.items():
                        new_k = k.replace("module.", "")
                        clean_sd[new_k] = v
                    model_to_save.load_state_dict(clean_sd)
                    
                    vocab_path = os.path.join(parent_dir, "bpe_vocab_16k.json")
                    if not os.path.exists(vocab_path):
                        vocab_path = os.path.join(main_dir, "bpe_vocab_16k.json")
                    if os.path.exists(vocab_path):
                        tok = Tokenizer.from_file(vocab_path)
                    else:
                        tok = None
                        
                    os.makedirs(ckpt_path, exist_ok=True)
                    state_dict = model_to_save.state_dict()
                    new_state_dict = {}
                    for k, v in state_dict.items():
                        name = k
                        if name.startswith("_orig_mod."):
                            name = name[10:]
                        if name.startswith("model."):
                            name = name[6:]
                        if name == "lm_head.weight":
                            new_state_dict["lm_head.weight"] = v
                        else:
                            new_state_dict[f"transformer.{name}"] = v
                    torch.save(new_state_dict, os.path.join(ckpt_path, "pytorch_model.bin"))
                    
                    config_dict = {
                        "auto_map": {
                            "AutoConfig": "configuration_xpertgpt.XpertGPTConfig",
                            "AutoModel": "modeling_xpertgpt.XpertGPTModelWrapper",
                            "AutoModelForCausalLM": "modeling_xpertgpt.XpertGPTForCausalLM"
                        },
                        "vocab_size": 16384,
                        "block_size": 512,
                        "d_model": 256,
                        "hidden_size": 256,
                        "d_thin": 384,
                        "num_layers": 6,
                        "num_blocks": 4,
                        "capacity_factor": 2.0,
                        "dropout": 0.1,
                        "model_type": "xpertgpt",
                        "num_hidden_layers": 6
                    }
                    with open(os.path.join(ckpt_path, "config.json"), "w") as f:
                        json.dump(config_dict, f, indent=2)
                        
                    if tok:
                        from transformers import PreTrainedTokenizerFast
                        fast_tokenizer = PreTrainedTokenizerFast(
                            tokenizer_object=tok,
                            bos_token="[CLS]",
                            eos_token="[SEP]",
                            unk_token="[UNK]",
                            pad_token="[PAD]",
                            mask_token="[MASK]"
                        )
                        fast_tokenizer.save_pretrained(ckpt_path)
                    
                    revisions[ckpt_name] = ckpt_path
                except Exception as ex:
                    print(f"[HF] Failed to convert legacy checkpoint file '{ckpt_name}.pt': {ex}")

    # Temporary directory for staging uploads
    temp_dir = os.path.abspath("./temp_hf_upload")
    
    # LICENSE text
    license_text = """Creative Commons Attribution-NonCommercial 4.0 International Public License



By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-NonCommercial 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions.



Section 1 -- Definitions.

a. Licensed Material means the artistic or literary work, database, or other material to which the Licensor applied this Public License.

b. Licensed Rights means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license.

c. NonCommercial means not primarily intended for or directed towards commercial advantage or monetary compensation.

d. Share means to provide material to the public by any means or process that requires permission under the Licensed Rights.

e. You means the individual or entity exercising the Licensed Rights under this Public License.



Section 2 -- Scope.

a. License grant.

   1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to:

      A. reproduce and Share the Licensed Material, in whole or in part, for NonCommercial purposes only; and

      B. Produce, reproduce, and Share Adapted Material for NonCommercial purposes only.

   2. Attribution. As a condition of the license, You must attribute the Licensor and keep intact copyright notices.

"""

    for revision_name, local_path in revisions.items():
        print(f"\n[HF] Staging files for revision '{revision_name}' from '{local_path}'...")
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir)
        os.makedirs(temp_dir)

        # 1. Copy weight file and save as model.safetensors if possible, otherwise pytorch_model.bin
        src_bin = os.path.join(local_path, "pytorch_model.bin")
        weight_file_dest = None
        if os.path.exists(src_bin):
            # Try to convert to safetensors
            try:
                import torch
                from safetensors.torch import save_file
                state_dict = torch.load(src_bin, map_location="cpu")
                # Clone tensors to break memory sharing (prevents shared weight memory error in safetensors)
                state_dict = {k: v.clone() for k, v in state_dict.items()}
                weight_file_dest = os.path.join(temp_dir, "model.safetensors")
                save_file(state_dict, weight_file_dest)
                print(f"[HF] Converted weights to safetensors format.")
            except Exception as e:
                print(f"[HF] Conversion to safetensors failed ({e}). Staging raw pytorch_model.bin...")
                weight_file_dest = os.path.join(temp_dir, "pytorch_model.bin")
                shutil.copy2(src_bin, weight_file_dest)
        else:
            print(f"[HF] Error: No weight file found in '{local_path}'!")
            continue

        # Calculate weight file hash
        sha256_hash = hashlib.sha256()
        with open(weight_file_dest, "rb") as f:
            for byte_block in iter(lambda: f.read(4096), b""):
                sha256_hash.update(byte_block)
        weight_hash = sha256_hash.hexdigest()
        print(f"[HF] Weight file SHA-256: {weight_hash}")

        # 2. Copy and patch config.json
        src_config = os.path.join(local_path, "config.json")
        if os.path.exists(src_config):
            with open(src_config, "r") as f:
                cfg_data = json.load(f)
            # Patch config
            cfg_data["auto_map"] = {
                "AutoConfig": "configuration_xpertgpt.XpertGPTConfig",
                "AutoModel": "modeling_xpertgpt.XpertGPTModelWrapper",
                "AutoModelForCausalLM": "modeling_xpertgpt.XpertGPTForCausalLM"
            }
            cfg_data["architectures"] = ["XpertGPTForCausalLM"]
            with open(os.path.join(temp_dir, "config.json"), "w") as f:
                json.dump(cfg_data, f, indent=2)
        else:
            # Fallback configuration
            cfg_data = {
                "auto_map": {
                    "AutoConfig": "configuration_xpertgpt.XpertGPTConfig",
                    "AutoModel": "modeling_xpertgpt.XpertGPTModelWrapper",
                    "AutoModelForCausalLM": "modeling_xpertgpt.XpertGPTForCausalLM"
                },
                "architectures": ["XpertGPTForCausalLM"],
                "vocab_size": 16384,
                "block_size": 512,
                "d_model": 256,
                "hidden_size": 256,
                "d_thin": 384,
                "num_layers": 6,
                "num_blocks": 4,
                "capacity_factor": 2.0,
                "dropout": 0.1,
                "model_type": "xpertgpt"
            }
            with open(os.path.join(temp_dir, "config.json"), "w") as f:
                json.dump(cfg_data, f, indent=2)

        # 3. Copy tokenizers
        for tok_file in ["tokenizer.json", "tokenizer_config.json", "special_tokens_map.json"]:
            src_tok = os.path.join(local_path, tok_file)
            if os.path.exists(src_tok):
                shutil.copy2(src_tok, os.path.join(temp_dir, tok_file))

        # 4. Copy custom code
        shutil.copy2("modeling_xpertgpt.py", os.path.join(temp_dir, "modeling_xpertgpt.py"))
        shutil.copy2("configuration_xpertgpt.py", os.path.join(temp_dir, "configuration_xpertgpt.py"))

        # 5. Write metadata files
        with open(os.path.join(temp_dir, ".gitattributes"), "w") as f:
            f.write("*.safetensors filter=lfs diff=lfs merge=lfs -text\n")
            f.write("*.bin filter=lfs diff=lfs merge=lfs -text\n")

        with open(os.path.join(temp_dir, "LICENSE"), "w") as f:
            f.write(license_text)

        with open(os.path.join(temp_dir, "CITATION.cff"), "w") as f:
            citation_yaml = f"""cff-version: 1.2.0

message: "If you use this model or software, please cite it as below."

authors:

  - family-names: "Jain"

    given-names: "Soham"

  - family-names: "Singh"

    given-names: "Harsh"

  - family-names: "Dewan"

    given-names: "Divija"

  - family-names: "Dev"

    given-names: "Atul"

title: "XpertGPT: Mixture of Experts with Parallelized Multi-Scale Information Transmission for Data-Constrained Pretraining"

year: 2026

url: "https://huggingface.co/{repo_id}"

"""
            f.write(citation_yaml)

        with open(os.path.join(temp_dir, "PROVENANCE.md"), "w") as f:
            provenance_md = f"""# Provenance and Weight Integrity Record



This file records the provenance, cryptographic hash, and reproducibility metadata of the model weights.



## Verification Fingerprints

- **Model Weight File**: {"model.safetensors" if weight_file_dest.endswith(".safetensors") else "pytorch_model.bin"}

- **Weight SHA-256**: {weight_hash}

- **Tokenizer Vocab Size**: 16,384



## Training Run Details

- **Training Word Budget**: 10M words (BabyLM 2026 Strict-Small track)

- **Model Parameters**: ~48.4M non-embedding parameters / ~52.6M total tied parameters

- **Optimizer**: AdamW

- **Epochs**: 8

"""
            f.write(provenance_md)

        # Build README
        readme_md = f"""---

license: cc-by-nc-4.0

language:

  - en

tags:

  - babylm

  - babylm-2026

  - mixture-of-experts

  - msit

  - xpertgpt

  - custom_code

  - safetensors

library_name: transformers

pipeline_tag: text-generation

---



# XpertGPT Strict-Small



XpertGPT (Mixture of Experts with Parallelized Multi-Scale Information Transmission) is a sparse, data-efficient recurrent language model for the BabyLM 2026 challenge (Strict-Small (10M) track, 10M words). It combines sliding window attention global streams with sparse parallel expert blocks using Expert Choice routing. ~48.4M non-embedding parameters / ~52.6M total parameters. Custom code (`trust_remote_code=True`).



- **Architecture:** 6 layers of MoEP-MSIT blocks. Each block combines a lower-dimensional dense global sliding window attention layer (`dim = 256`) with 4 parallel high-dimensional sparse expert blocks (`dim = 384`) routed via Expert Choice gating.

- **Track:** BabyLM 2026 Strict-Small (10M) (10M words).

- **Tokenizer:** Custom BPE tokenizer (vocab size: 16384).

- **Revision / Checkpoint:** {revision_name}



## Usage



```python

import torch

from transformers import AutoModelForCausalLM, AutoTokenizer



model = AutoModelForCausalLM.from_pretrained("{repo_id}", revision="{revision_name}", trust_remote_code=True).eval()

tok = AutoTokenizer.from_pretrained("{repo_id}", revision="{revision_name}")

ids = tok("The quick brown fox", return_tensors="pt").input_ids

with torch.no_grad():

    logits = model(ids).logits

```



## Intermediate checkpoints



Intermediate training checkpoints are provided as git revisions named `chck_<N>M` for the BabyLM challenge fast-eval.



## License and citation



Released under CC BY-NC 4.0 (attribution required, non-commercial only). If you use this model or code, please cite (see `CITATION.cff`):



```bibtex

@misc{{jain2026xpertgpt,

  title        = {{XpertGPT: Mixture of Experts with Parallelized Multi-Scale Information Transmission for Data-Constrained Pretraining}},

  author       = {{Jain, Soham and Singh, Harsh and Dewan, Divija and Dev, Atul}},

  year         = {{2026}},

  howpublished = {{Hugging Face Repository}},

  note         = {{XpertGPT MoE language model, BabyLM 2026}}

}}

```



Provenance and integrity fingerprints are documented in `PROVENANCE.md`.

"""
        with open(os.path.join(temp_dir, "README.md"), "w") as f:
            f.write(readme_md)

        # 6. For main branch only: also upload collated predictions
        if revision_name == "main":
            for pred_file in ["all_full_preds_and_fast_scores_causal.json", "all_full_preds_and_fast_scores_causal (3).json"]:
                if os.path.exists(pred_file):
                    shutil.copy2(pred_file, os.path.join(temp_dir, "all_full_preds_and_fast_scores_causal.json"))
                    print(f"[HF] Copied predictions file '{pred_file}' to staging area.")
                    break

        # 7. Create branch if it does not exist, then upload staged files to HF under the revision
        if revision_name != "main":
            try:
                api.create_branch(
                    repo_id=repo_id,
                    repo_type="model",
                    branch=revision_name,
                    exist_ok=True
                )
                print(f"[HF] Created branch/revision '{revision_name}' on repository.")
            except Exception as branch_err:
                print(f"[HF] Info: Branch creation failed or exists: {branch_err}")

        print(f"[HF] Uploading staged folder to '{repo_id}' revision '{revision_name}'...")
        try:
            api.upload_folder(
                folder_path=temp_dir,
                repo_id=repo_id,
                repo_type="model",
                revision=revision_name
            )
            print(f"[HF] Successfully uploaded revision '{revision_name}' to repository.")
        except Exception as e:
            print(f"[HF] Failed to upload revision '{revision_name}': {e}")

    # Cleanup temp dir
    if os.path.exists(temp_dir):
        shutil.rmtree(temp_dir)
    print(f"\n[HF] All uploads finished! View your repository at https://huggingface.co/{repo_id}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--model-name", type=str, default="xpertgpt_fresh")
    parser.add_argument("--epochs", type=int, default=10)
    parser.add_argument("--skip-eval", action="store_true", help="Skip evaluation phase after training")
    parser.add_argument("--skip-aoa", action="store_true", default=True, help="Skip AoA evaluation")
    parser.add_argument("--skip-glue", action="store_true", default=False, help="Skip GLUE fine-tuning")
    parser.add_argument("--upload", action="store_true", help="Upload model repository to Hugging Face")
    parser.add_argument("--upload-repo", type=str, default="XpertGPT-BabyLM2026-Strict-Small", help="Hugging Face repository name")
    parser.add_argument("--upload-token", type=str, default=None, help="Hugging Face API token")
    args = parser.parse_args()
    
    if args.upload:
        upload_pipeline(args.model_name, args.upload_repo, args.upload_token)
    else:
        run_pipeline(args.model_name, epochs=args.epochs, skip_eval=args.skip_eval, skip_aoa=args.skip_aoa, skip_glue=args.skip_glue)