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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
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
#!/usr/bin/env python3
import argparse
import json
import math
import os
import random
import time
from pathlib import Path
from typing import Any, Dict, Iterator, List, Optional, Tuple

import numpy as np
import pyarrow.parquet as pq
import torch
import torch.nn as nn
import torch.nn.functional as F
from tokenizers import Tokenizer
from torch.utils.data import DataLoader, IterableDataset, get_worker_info
from tqdm import tqdm


PAD_ID = 0
IGNORE_INDEX = -100

DEFAULT_BOS_MARKER = "<|BOS|>"
DEFAULT_EOS_MARKER = "<|EOS|>"
DEFAULT_BOS_TOKEN = "[BOS]"
DEFAULT_EOS_TOKEN = "[EOS]"

BOUNDARY_MODE = "marker_aware_generic_special_markers_v11_window_mode_rope"

_FLASH2_KERNEL = None
_FLASH3_KERNEL = None


def get_flash2_kernel():
    global _FLASH2_KERNEL

    if _FLASH2_KERNEL is None:
        from kernels import get_kernel

        _FLASH2_KERNEL = get_kernel(
            "kernels-community/flash-attn2",
            version=1,
        )

    return _FLASH2_KERNEL


def get_flash3_kernel():
    global _FLASH3_KERNEL

    if _FLASH3_KERNEL is None:
        from kernels import get_kernel

        _FLASH3_KERNEL = get_kernel(
            "kernels-community/flash-attn3",
            version=1,
        )

    return _FLASH3_KERNEL


def format_tokens(n: int) -> str:
    if n >= 1_000_000_000:
        return f"{n / 1_000_000_000:.2f}B"
    if n >= 1_000_000:
        return f"{n / 1_000_000:.2f}M"
    if n >= 1_000:
        return f"{n / 1_000:.2f}K"
    return str(n)


def resolve_tokenizer_path(path: str) -> str:
    p = Path(path)

    if p.is_dir():
        candidate = p / "tokenizer.json"
        if candidate.exists():
            return str(candidate)

    return str(p)


def stable_row_score(row_index: int, seed: int) -> float:
    x = (row_index + 1) & 0xFFFFFFFFFFFFFFFF
    x ^= (seed + 0x9E3779B97F4A7C15) & 0xFFFFFFFFFFFFFFFF
    x = (x * 0xBF58476D1CE4E5B9) & 0xFFFFFFFFFFFFFFFF
    x ^= x >> 30
    x = (x * 0x94D049BB133111EB) & 0xFFFFFFFFFFFFFFFF
    x ^= x >> 31

    return (x & 0xFFFFFFFF) / 0x100000000


def normalize_activity_value(value: Any) -> Optional[str]:
    if value is None:
        return None

    if isinstance(value, str):
        text = value.strip()
        return text if text else None

    if isinstance(value, (list, tuple)):
        parts = []

        for item in value:
            if item is None:
                continue

            s = str(item).strip()

            if s:
                parts.append(s)

        text = " ; ".join(parts).strip()

        return text if text else None

    if isinstance(value, dict):
        text = json.dumps(
            value,
            ensure_ascii=False,
            sort_keys=True,
        ).strip()

        return text if text else None

    text = str(value).strip()

    return text if text else None


def canonical_special_token(value: str) -> str:
    value = str(value).strip()

    if not value:
        raise ValueError("Special token vide.")

    if value.startswith("[") and value.endswith("]"):
        inner = value[1:-1].strip()
        if not inner:
            raise ValueError(f"Token spécial invalide: {value}")
        return "[" + inner.upper() + "]"

    return "[" + value.upper() + "]"


def parse_special_marker_spec(spec: str) -> Tuple[str, str]:
    spec = str(spec).strip()

    if "=" not in spec:
        raise ValueError(
            f"Format --special-marker invalide: {spec}. Format attendu: '<|BOC|>=[BOC]'"
        )

    marker, token = spec.split("=", 1)
    marker = marker.strip()
    token = token.strip()

    if not marker:
        raise ValueError(f"Marker vide dans: {spec}")

    if not token:
        raise ValueError(f"Token vide dans: {spec}")

    token = canonical_special_token(token)

    return marker, token


def build_marker_token_map(custom_specs: List[str]) -> Dict[str, str]:
    marker_token_map: Dict[str, str] = {
        DEFAULT_BOS_MARKER: DEFAULT_BOS_TOKEN,
        DEFAULT_EOS_MARKER: DEFAULT_EOS_TOKEN,
    }

    for spec in custom_specs:
        marker, token = parse_special_marker_spec(spec)
        marker_token_map[marker] = token

    return marker_token_map


class RNETokenCache:
    def __init__(
        self,
        src: str,
        tokenizer_path: str,
        cache_dir: str,
        activity_column: str = "activites",
        row_batch_size: int = 100_000,
        val_ratio: float = 0.01,
        seed: int = 42,
        lowercase: bool = False,
        append_special_tokens: bool = True,
        rebuild_cache: bool = False,
        shuffle_before_tokenize: bool = True,
        shuffle_buffer_size: int = 500_000,
        special_marker_specs: Optional[List[str]] = None,
        window_mode: str = "stream",
        ctx_len: int = 512,
    ):
        if not 0.0 < val_ratio < 0.5:
            raise ValueError("--val-ratio must be > 0 and < 0.5")

        if shuffle_buffer_size <= 0:
            raise ValueError("--shuffle-buffer-size must be > 0")

        if window_mode not in ("stream", "row"):
            raise ValueError("--window-mode must be 'stream' or 'row'")

        if ctx_len <= 0:
            raise ValueError("--ctx-len must be > 0")

        self.src = str(src)
        self.tokenizer_path = resolve_tokenizer_path(tokenizer_path)
        self.cache_dir = Path(cache_dir)
        self.activity_column = activity_column
        self.row_batch_size = int(row_batch_size)
        self.val_ratio = float(val_ratio)
        self.seed = int(seed)
        self.lowercase = bool(lowercase)
        self.append_special_tokens = bool(append_special_tokens)
        self.rebuild_cache = bool(rebuild_cache)
        self.shuffle_before_tokenize = bool(shuffle_before_tokenize)
        self.shuffle_buffer_size = int(shuffle_buffer_size)
        self.special_marker_specs = list(special_marker_specs or [])
        self.window_mode = str(window_mode)
        self.ctx_len = int(ctx_len)
        self.need = self.ctx_len + 1

        self.cache_dir.mkdir(parents=True, exist_ok=True)

        self.train_bin = self.cache_dir / "train_tokens.uint32.bin"
        self.val_bin = self.cache_dir / "val_tokens.uint32.bin"
        self.train_windows_bin = self.cache_dir / "train_windows.uint64.bin"
        self.val_windows_bin = self.cache_dir / "val_windows.uint64.bin"
        self.meta_path = self.cache_dir / "meta.json"

        self.tokenizer = Tokenizer.from_file(self.tokenizer_path)
        self.vocab_size = self.tokenizer.get_vocab_size()

        self.marker_token_map = build_marker_token_map(self.special_marker_specs)
        self.marker_id_map = self._build_marker_id_map()

        self.bos_id = self._find_bos_id() if self.append_special_tokens else None
        self.eos_id = self._find_eos_id() if self.append_special_tokens else None
        self.sep_id = self._find_sep_id() if self.append_special_tokens else None

        if self.append_special_tokens:
            if self.bos_id is None:
                raise RuntimeError(
                    "BOS token introuvable. Le tokenizer doit contenir [BOS], <bos>, <BOS>, <s>, [CLS] ou équivalent."
                )

            if self.eos_id is None:
                raise RuntimeError(
                    "EOS token introuvable. Le tokenizer doit contenir [EOS], <eos>, <EOS>, </s>, [SEP] ou équivalent."
                )

        self.shuffle_rng_train = random.Random(self.seed + 123_456_789)
        self.shuffle_rng_val = random.Random(self.seed + 987_654_321)

    def _find_token_id(self, candidates: List[str]) -> Optional[int]:
        for token in candidates:
            token_id = self.tokenizer.token_to_id(token)

            if token_id is not None:
                return int(token_id)

        return None

    def _find_bos_id(self) -> Optional[int]:
        explicit_token = self.marker_token_map.get(DEFAULT_BOS_MARKER, DEFAULT_BOS_TOKEN)

        return self._find_token_id(
            [
                explicit_token,
                "[BOS]",
                "<bos>",
                "<BOS>",
                "<s>",
                "[CLS]",
                DEFAULT_BOS_MARKER,
            ]
        )

    def _find_eos_id(self) -> Optional[int]:
        explicit_token = self.marker_token_map.get(DEFAULT_EOS_MARKER, DEFAULT_EOS_TOKEN)

        return self._find_token_id(
            [
                explicit_token,
                "[EOS]",
                "<eos>",
                "<EOS>",
                "</s>",
                "[SEP]",
                "<sep>",
                "<SEP>",
                DEFAULT_EOS_MARKER,
            ]
        )

    def _find_sep_id(self) -> Optional[int]:
        return self._find_token_id(
            [
                "[SEP]",
                "</s>",
                "<eos>",
                "<EOS>",
                "[EOS]",
                "<sep>",
                "<SEP>",
                DEFAULT_EOS_MARKER,
            ]
        )

    def _build_marker_id_map(self) -> Dict[str, int]:
        marker_id_map: Dict[str, int] = {}

        for marker, token in self.marker_token_map.items():
            token_id = self.tokenizer.token_to_id(token)

            if token_id is None:
                raise RuntimeError(
                    f"Token spécial introuvable dans le tokenizer: marker {repr(marker)} -> token {repr(token)}. "
                    f"Ajoute-le au tokenizer avec --add-special-token."
                )

            marker_id_map[marker] = int(token_id)

        return marker_id_map

    def _cache_is_valid(self) -> bool:
        if self.rebuild_cache:
            return False

        if not self.train_bin.exists():
            return False

        if not self.val_bin.exists():
            return False

        if self.window_mode == "row":
            if not self.train_windows_bin.exists():
                return False
            if not self.val_windows_bin.exists():
                return False

        if not self.meta_path.exists():
            return False

        try:
            meta = json.loads(self.meta_path.read_text(encoding="utf-8"))
        except Exception:
            return False

        expected = {
            "src": os.path.abspath(self.src),
            "tokenizer_path": os.path.abspath(self.tokenizer_path),
            "activity_column": self.activity_column,
            "val_ratio": self.val_ratio,
            "seed": self.seed,
            "lowercase": self.lowercase,
            "append_special_tokens": self.append_special_tokens,
            "bos_id": self.bos_id,
            "eos_id": self.eos_id,
            "sep_id": self.sep_id,
            "vocab_size": self.vocab_size,
            "shuffle_before_tokenize": self.shuffle_before_tokenize,
            "shuffle_buffer_size": self.shuffle_buffer_size,
            "boundary_mode": BOUNDARY_MODE,
            "default_bos_marker": DEFAULT_BOS_MARKER,
            "default_eos_marker": DEFAULT_EOS_MARKER,
            "marker_token_map": self.marker_token_map,
            "marker_id_map": self.marker_id_map,
            "window_mode": self.window_mode,
            "ctx_len": self.ctx_len,
            "need": self.need,
        }

        for key, value in expected.items():
            if meta.get(key) != value:
                return False

        return True

    def _write_meta(
        self,
        train_tokens: int,
        val_tokens: int,
        rows_seen: int,
        rows_used: int,
        rows_with_mapped_markers: int,
        rows_with_explicit_boundaries: int,
        rows_with_legacy_boundaries: int,
        train_windows: int,
        val_windows: int,
        rows_dropped_window: int,
        rows_dropped_too_short: int,
        rows_dropped_too_long: int,
    ):
        payload = {
            "src": os.path.abspath(self.src),
            "tokenizer_path": os.path.abspath(self.tokenizer_path),
            "activity_column": self.activity_column,
            "val_ratio": self.val_ratio,
            "seed": self.seed,
            "lowercase": self.lowercase,
            "append_special_tokens": self.append_special_tokens,
            "bos_id": self.bos_id,
            "eos_id": self.eos_id,
            "sep_id": self.sep_id,
            "vocab_size": self.vocab_size,
            "shuffle_before_tokenize": self.shuffle_before_tokenize,
            "shuffle_buffer_size": self.shuffle_buffer_size,
            "boundary_mode": BOUNDARY_MODE,
            "default_bos_marker": DEFAULT_BOS_MARKER,
            "default_eos_marker": DEFAULT_EOS_MARKER,
            "marker_token_map": self.marker_token_map,
            "marker_id_map": self.marker_id_map,
            "window_mode": self.window_mode,
            "ctx_len": self.ctx_len,
            "need": self.need,
            "train_tokens": int(train_tokens),
            "val_tokens": int(val_tokens),
            "train_windows": int(train_windows),
            "val_windows": int(val_windows),
            "rows_seen": int(rows_seen),
            "rows_used": int(rows_used),
            "rows_dropped_window": int(rows_dropped_window),
            "rows_dropped_too_short": int(rows_dropped_too_short),
            "rows_dropped_too_long": int(rows_dropped_too_long),
            "rows_with_mapped_markers": int(rows_with_mapped_markers),
            "rows_with_explicit_boundaries": int(rows_with_explicit_boundaries),
            "rows_with_legacy_boundaries": int(rows_with_legacy_boundaries),
            "token_dtype": "uint32",
            "window_dtype": "uint64_pair_start_length",
            "row_mode_rule": "row mode keeps rows with 2 <= token_count <= ctx_len+1, pads shorter rows in dataloader, drops rows longer than ctx_len+1",
        }

        self.meta_path.write_text(
            json.dumps(payload, ensure_ascii=False, indent=2),
            encoding="utf-8",
        )

    def _shuffle_buffer_with_progress(
        self,
        buffer: List[str],
        rng: random.Random,
        desc: str,
    ):
        n = len(buffer)

        if n <= 1:
            return

        pbar = tqdm(
            total=n - 1,
            desc=desc,
            dynamic_ncols=True,
            unit="swap",
        )

        for i in range(n - 1, 0, -1):
            j = rng.randint(0, i)
            buffer[i], buffer[j] = buffer[j], buffer[i]
            pbar.update(1)

        pbar.close()

    def _has_explicit_bos_and_eos_markers(self, text: str) -> bool:
        return DEFAULT_BOS_MARKER in text and DEFAULT_EOS_MARKER in text

    def _has_any_mapped_marker(self, text: str) -> bool:
        for marker in self.marker_id_map.keys():
            if marker in text:
                return True
        return False

    def _encode_plain_chunk(self, text: str) -> List[int]:
        if not text:
            return []

        if self.lowercase:
            text = text.lower()

        ids = self.tokenizer.encode(
            text,
            add_special_tokens=False,
        ).ids

        return [int(x) for x in ids]

    def _find_next_marker(self, text: str, start: int) -> Tuple[int, Optional[str], Optional[int]]:
        best_pos = -1
        best_marker = None
        best_id = None

        for marker, marker_id in self.marker_id_map.items():
            pos = text.find(marker, start)

            if pos == -1:
                continue

            if best_pos == -1 or pos < best_pos:
                best_pos = pos
                best_marker = marker
                best_id = marker_id

        return best_pos, best_marker, best_id

    def _encode_text_replacing_markers(self, text: str) -> Tuple[List[int], bool]:
        ids: List[int] = []
        i = 0
        n = len(text)
        used_marker = False

        while i < n:
            marker_pos, marker, marker_id = self._find_next_marker(text, i)

            if marker_pos == -1 or marker is None or marker_id is None:
                chunk = text[i:]
                ids.extend(self._encode_plain_chunk(chunk))
                break

            chunk = text[i:marker_pos]
            ids.extend(self._encode_plain_chunk(chunk))
            ids.append(int(marker_id))
            used_marker = True

            i = marker_pos + len(marker)

        return ids, used_marker

    def _encode_text_with_boundaries(self, text: str) -> Tuple[List[int], bool, bool]:
        has_explicit_boundaries = self._has_explicit_bos_and_eos_markers(text)
        has_any_marker = self._has_any_mapped_marker(text)

        if not self.append_special_tokens:
            ids, used_marker = self._encode_text_replacing_markers(text)
            return ids, used_marker, has_explicit_boundaries

        if has_any_marker:
            ids, used_marker = self._encode_text_replacing_markers(text)

            if has_explicit_boundaries:
                return ids, used_marker, True

            ids = [int(self.bos_id)] + ids + [int(self.eos_id)]
            return ids, used_marker, False

        if self.lowercase:
            text = text.lower()

        ids = self.tokenizer.encode(
            text,
            add_special_tokens=False,
        ).ids

        ids = [int(x) for x in ids]
        ids = [int(self.bos_id)] + ids + [int(self.eos_id)]

        return ids, False, False

    def _tokenize_to_file(
        self,
        texts: List[str],
        token_file_obj,
        window_file_obj,
        desc: str,
    ) -> Tuple[int, int, int, int, int, int, int, int]:
        written_tokens = 0
        used_texts = 0
        mapped_marker_rows = 0
        explicit_boundary_rows = 0
        legacy_boundary_rows = 0
        windows_written = 0
        row_drop = 0
        row_drop_short = 0
        row_drop_long = 0

        pbar = tqdm(
            total=len(texts),
            desc=desc,
            dynamic_ncols=True,
            unit="texts",
        )

        for text in texts:
            ids, used_mapped_marker, used_explicit_boundaries = self._encode_text_with_boundaries(text)
            token_count = len(ids)

            if self.window_mode == "row":
                if token_count < 2:
                    row_drop += 1
                    row_drop_short += 1
                    pbar.update(1)
                    continue

                if token_count > self.need:
                    row_drop += 1
                    row_drop_long += 1
                    pbar.update(1)
                    continue

                start = written_tokens
                arr = np.asarray(ids, dtype=np.uint32)
                arr.tofile(token_file_obj)

                if window_file_obj is None:
                    raise RuntimeError("window_file_obj is required in row mode")

                win = np.asarray([start, token_count], dtype=np.uint64)
                win.tofile(window_file_obj)

                written_tokens += int(arr.size)
                windows_written += 1
                used_texts += 1

            else:
                if token_count >= 2:
                    arr = np.asarray(ids, dtype=np.uint32)
                    arr.tofile(token_file_obj)
                    written_tokens += int(arr.size)
                    used_texts += 1

            if token_count >= 2 and not (self.window_mode == "row" and token_count > self.need):
                if used_mapped_marker:
                    mapped_marker_rows += 1

                if used_explicit_boundaries:
                    explicit_boundary_rows += 1
                else:
                    legacy_boundary_rows += 1

            pbar.update(1)

            if (used_texts > 0 and used_texts % 10_000 == 0) or (row_drop > 0 and row_drop % 10_000 == 0):
                postfix = {
                    "used": f"{used_texts:,}",
                    "tokens": format_tokens(written_tokens),
                    "markers": f"{mapped_marker_rows:,}",
                    "explicit": f"{explicit_boundary_rows:,}",
                    "legacy": f"{legacy_boundary_rows:,}",
                }

                if self.window_mode == "row":
                    postfix["windows"] = f"{windows_written:,}"
                    postfix["row_drop"] = f"{row_drop:,}"
                    postfix["too_long"] = f"{row_drop_long:,}"

                pbar.set_postfix(**postfix)

        pbar.close()

        return (
            written_tokens,
            mapped_marker_rows,
            explicit_boundary_rows,
            legacy_boundary_rows,
            windows_written,
            row_drop,
            row_drop_short,
            row_drop_long,
        )

    def _flush_text_buffer(
        self,
        buffer: List[str],
        token_file_obj,
        window_file_obj,
        rng: random.Random,
        name: str,
    ) -> Tuple[int, int, int, int, int, int, int, int]:
        if not buffer:
            return 0, 0, 0, 0, 0, 0, 0, 0

        print()
        print(f"[FLUSH] {name}")
        print(f"[FLUSH] texts in buffer: {len(buffer):,}")

        if self.shuffle_before_tokenize:
            self._shuffle_buffer_with_progress(
                buffer=buffer,
                rng=rng,
                desc=f"Shuffling {name}",
            )

        (
            written_tokens,
            mapped_marker_rows,
            explicit_boundary_rows,
            legacy_boundary_rows,
            windows_written,
            row_drop,
            row_drop_short,
            row_drop_long,
        ) = self._tokenize_to_file(
            texts=buffer,
            token_file_obj=token_file_obj,
            window_file_obj=window_file_obj,
            desc=f"Tokenizing {name}",
        )

        print(f"[FLUSH] {name} tokens written: {written_tokens:,}")
        print(f"[FLUSH] {name} mapped marker rows: {mapped_marker_rows:,}")
        print(f"[FLUSH] {name} explicit boundary rows: {explicit_boundary_rows:,}")
        print(f"[FLUSH] {name} legacy boundary rows: {legacy_boundary_rows:,}")

        if self.window_mode == "row":
            print(f"[FLUSH] {name} windows written: {windows_written:,}")
            print(f"[FLUSH] {name} row_drop: {row_drop:,}")
            print(f"[FLUSH] {name} row_drop_short: {row_drop_short:,}")
            print(f"[FLUSH] {name} row_drop_long: {row_drop_long:,}")

        print()

        buffer.clear()

        return (
            written_tokens,
            mapped_marker_rows,
            explicit_boundary_rows,
            legacy_boundary_rows,
            windows_written,
            row_drop,
            row_drop_short,
            row_drop_long,
        )

    def build_if_needed(self):
        if self._cache_is_valid():
            print("[INFO] Token cache found.")
            meta = json.loads(self.meta_path.read_text(encoding="utf-8"))
            print(f"[INFO] Train tokens:             {meta['train_tokens']:,}")
            print(f"[INFO] Val tokens:               {meta['val_tokens']:,}")
            print(f"[INFO] Train windows:            {meta.get('train_windows', 0):,}")
            print(f"[INFO] Val windows:              {meta.get('val_windows', 0):,}")
            print(f"[INFO] Rows seen:                {meta.get('rows_seen', 0):,}")
            print(f"[INFO] Rows used:                {meta.get('rows_used', 0):,}")
            print(f"[INFO] Rows dropped/window:      {meta.get('rows_dropped_window', 0):,}")
            print(f"[INFO] Rows dropped too short:   {meta.get('rows_dropped_too_short', 0):,}")
            print(f"[INFO] Rows dropped too long:    {meta.get('rows_dropped_too_long', 0):,}")
            print(f"[INFO] Mapped marker rows:       {meta.get('rows_with_mapped_markers', 0):,}")
            print(f"[INFO] Explicit boundary rows:   {meta.get('rows_with_explicit_boundaries', 0):,}")
            print(f"[INFO] Legacy boundary rows:     {meta.get('rows_with_legacy_boundaries', 0):,}")
            print(f"[INFO] Vocab size:               {meta['vocab_size']:,}")
            print(f"[INFO] BOS id:                   {meta.get('bos_id')}")
            print(f"[INFO] EOS id:                   {meta.get('eos_id')}")
            print(f"[INFO] SEP id:                   {meta.get('sep_id')}")
            print(f"[INFO] Boundary mode:            {meta.get('boundary_mode')}")
            print(f"[INFO] Window mode:              {meta.get('window_mode')}")
            print(f"[INFO] Ctx len in cache:         {meta.get('ctx_len')}")
            print(f"[INFO] Marker token map:         {meta.get('marker_token_map')}")
            print(f"[INFO] Marker id map:            {meta.get('marker_id_map')}")
            print(f"[INFO] Shuffle before tok:       {meta.get('shuffle_before_tokenize')}")
            print(f"[INFO] Shuffle buffer:           {meta.get('shuffle_buffer_size'):,}")
            return

        print("[INFO] Building token cache from parquet.")
        print(f"[INFO] Source:                   {self.src}")
        print(f"[INFO] Column:                   {self.activity_column}")
        print(f"[INFO] Tokenizer:                {self.tokenizer_path}")
        print(f"[INFO] Cache dir:                {self.cache_dir}")
        print(f"[INFO] Vocab size:               {self.vocab_size:,}")
        print(f"[INFO] Append special:           {self.append_special_tokens}")
        print(f"[INFO] BOS id:                   {self.bos_id}")
        print(f"[INFO] EOS id:                   {self.eos_id}")
        print(f"[INFO] SEP id:                   {self.sep_id}")
        print(f"[INFO] Boundary mode:            {BOUNDARY_MODE}")
        print(f"[INFO] Window mode:              {self.window_mode}")
        print(f"[INFO] Ctx len:                  {self.ctx_len}")
        print(f"[INFO] Need tokens/window:       {self.need}")
        print(f"[INFO] Marker token map:         {self.marker_token_map}")
        print(f"[INFO] Marker id map:            {self.marker_id_map}")
        print(f"[INFO] Explicit boundary rule:   if <|BOS|> and <|EOS|> are present, no auto BOS/EOS")
        print(f"[INFO] Legacy boundary rule:     otherwise BOS + text + EOS")
        if self.window_mode == "row":
            print(f"[INFO] Row window rule:          keep rows with 2 <= tokens <= ctx_len+1; pad shorter rows in loader; drop longer rows")
        else:
            print(f"[INFO] Stream window rule:       old behavior, continuous token stream split into ctx_len+1 blocks")
        print(f"[INFO] Shuffle before tok:       {self.shuffle_before_tokenize}")
        print(f"[INFO] Shuffle buffer size:      {self.shuffle_buffer_size:,}")
        print()

        pf = pq.ParquetFile(self.src)

        if self.activity_column not in pf.schema.names:
            raise ValueError(
                f"Column '{self.activity_column}' not found. Available columns: {pf.schema.names}"
            )

        total_rows = pf.metadata.num_rows

        train_tmp = self.train_bin.with_suffix(".tmp")
        val_tmp = self.val_bin.with_suffix(".tmp")
        train_windows_tmp = self.train_windows_bin.with_suffix(".tmp")
        val_windows_tmp = self.val_windows_bin.with_suffix(".tmp")

        for p in [train_tmp, val_tmp, train_windows_tmp, val_windows_tmp]:
            if p.exists():
                p.unlink()

        train_tokens = 0
        val_tokens = 0
        train_windows = 0
        val_windows = 0
        rows_seen = 0
        rows_used = 0
        rows_with_mapped_markers = 0
        rows_with_explicit_boundaries = 0
        rows_with_legacy_boundaries = 0
        rows_dropped_window = 0
        rows_dropped_too_short = 0
        rows_dropped_too_long = 0

        train_text_buffer: List[str] = []
        val_text_buffer: List[str] = []

        if self.window_mode == "row":
            train_windows_cm = train_windows_tmp.open("wb")
            val_windows_cm = val_windows_tmp.open("wb")
        else:
            train_windows_cm = None
            val_windows_cm = None

        try:
            with train_tmp.open("wb") as f_train, val_tmp.open("wb") as f_val:
                pbar = tqdm(
                    total=total_rows,
                    desc="Reading + shuffling + tokenizing rows",
                    dynamic_ncols=True,
                    unit="rows",
                )

                for batch in pf.iter_batches(
                    batch_size=self.row_batch_size,
                    columns=[self.activity_column],
                ):
                    d = batch.to_pydict()
                    values = d[self.activity_column]

                    for value in values:
                        row_index = rows_seen
                        rows_seen += 1

                        text = normalize_activity_value(value)

                        if text is None:
                            pbar.update(1)
                            continue

                        if not text:
                            pbar.update(1)
                            continue

                        if stable_row_score(row_index, self.seed) < self.val_ratio:
                            val_text_buffer.append(text)
                        else:
                            train_text_buffer.append(text)

                        rows_used += 1

                        if len(train_text_buffer) >= self.shuffle_buffer_size:
                            (
                                written,
                                marker_rows,
                                explicit_rows,
                                legacy_rows,
                                windows,
                                row_drop,
                                row_drop_short,
                                row_drop_long,
                            ) = self._flush_text_buffer(
                                buffer=train_text_buffer,
                                token_file_obj=f_train,
                                window_file_obj=train_windows_cm,
                                rng=self.shuffle_rng_train,
                                name="train buffer",
                            )

                            train_tokens += written
                            train_windows += windows
                            rows_with_mapped_markers += marker_rows
                            rows_with_explicit_boundaries += explicit_rows
                            rows_with_legacy_boundaries += legacy_rows
                            rows_dropped_window += row_drop
                            rows_dropped_too_short += row_drop_short
                            rows_dropped_too_long += row_drop_long

                        if len(val_text_buffer) >= max(1_000, self.shuffle_buffer_size // 10):
                            (
                                written,
                                marker_rows,
                                explicit_rows,
                                legacy_rows,
                                windows,
                                row_drop,
                                row_drop_short,
                                row_drop_long,
                            ) = self._flush_text_buffer(
                                buffer=val_text_buffer,
                                token_file_obj=f_val,
                                window_file_obj=val_windows_cm,
                                rng=self.shuffle_rng_val,
                                name="val buffer",
                            )

                            val_tokens += written
                            val_windows += windows
                            rows_with_mapped_markers += marker_rows
                            rows_with_explicit_boundaries += explicit_rows
                            rows_with_legacy_boundaries += legacy_rows
                            rows_dropped_window += row_drop
                            rows_dropped_too_short += row_drop_short
                            rows_dropped_too_long += row_drop_long

                        pbar.update(1)

                        if rows_used % 10_000 == 0:
                            postfix = {
                                "used": f"{rows_used:,}",
                                "train_tok": format_tokens(train_tokens),
                                "val_tok": format_tokens(val_tokens),
                                "tr_buf": f"{len(train_text_buffer):,}",
                                "va_buf": f"{len(val_text_buffer):,}",
                                "markers": f"{rows_with_mapped_markers:,}",
                                "explicit": f"{rows_with_explicit_boundaries:,}",
                                "legacy": f"{rows_with_legacy_boundaries:,}",
                            }

                            if self.window_mode == "row":
                                postfix["tr_win"] = f"{train_windows:,}"
                                postfix["va_win"] = f"{val_windows:,}"
                                postfix["row_drop"] = f"{rows_dropped_window:,}"

                            pbar.set_postfix(**postfix)

                (
                    written,
                    marker_rows,
                    explicit_rows,
                    legacy_rows,
                    windows,
                    row_drop,
                    row_drop_short,
                    row_drop_long,
                ) = self._flush_text_buffer(
                    buffer=train_text_buffer,
                    token_file_obj=f_train,
                    window_file_obj=train_windows_cm,
                    rng=self.shuffle_rng_train,
                    name="final train buffer",
                )

                train_tokens += written
                train_windows += windows
                rows_with_mapped_markers += marker_rows
                rows_with_explicit_boundaries += explicit_rows
                rows_with_legacy_boundaries += legacy_rows
                rows_dropped_window += row_drop
                rows_dropped_too_short += row_drop_short
                rows_dropped_too_long += row_drop_long

                (
                    written,
                    marker_rows,
                    explicit_rows,
                    legacy_rows,
                    windows,
                    row_drop,
                    row_drop_short,
                    row_drop_long,
                ) = self._flush_text_buffer(
                    buffer=val_text_buffer,
                    token_file_obj=f_val,
                    window_file_obj=val_windows_cm,
                    rng=self.shuffle_rng_val,
                    name="final val buffer",
                )

                val_tokens += written
                val_windows += windows
                rows_with_mapped_markers += marker_rows
                rows_with_explicit_boundaries += explicit_rows
                rows_with_legacy_boundaries += legacy_rows
                rows_dropped_window += row_drop
                rows_dropped_too_short += row_drop_short
                rows_dropped_too_long += row_drop_long

                pbar.close()
        finally:
            if train_windows_cm is not None:
                train_windows_cm.close()
            if val_windows_cm is not None:
                val_windows_cm.close()

        train_tmp.replace(self.train_bin)
        val_tmp.replace(self.val_bin)

        if self.window_mode == "row":
            train_windows_tmp.replace(self.train_windows_bin)
            val_windows_tmp.replace(self.val_windows_bin)
        else:
            if train_windows_tmp.exists():
                train_windows_tmp.unlink()
            if val_windows_tmp.exists():
                val_windows_tmp.unlink()

        self._write_meta(
            train_tokens=train_tokens,
            val_tokens=val_tokens,
            rows_seen=rows_seen,
            rows_used=rows_used,
            rows_with_mapped_markers=rows_with_mapped_markers,
            rows_with_explicit_boundaries=rows_with_explicit_boundaries,
            rows_with_legacy_boundaries=rows_with_legacy_boundaries,
            train_windows=train_windows,
            val_windows=val_windows,
            rows_dropped_window=rows_dropped_window,
            rows_dropped_too_short=rows_dropped_too_short,
            rows_dropped_too_long=rows_dropped_too_long,
        )

        print()
        print("[INFO] Token cache built.")
        print(f"[INFO] Rows seen:                {rows_seen:,}")
        print(f"[INFO] Rows used:                {rows_used:,}")
        print(f"[INFO] Rows dropped/window:      {rows_dropped_window:,}")
        print(f"[INFO] Rows dropped too short:   {rows_dropped_too_short:,}")
        print(f"[INFO] Rows dropped too long:    {rows_dropped_too_long:,}")
        print(f"[INFO] Mapped marker rows:       {rows_with_mapped_markers:,}")
        print(f"[INFO] Explicit boundary rows:   {rows_with_explicit_boundaries:,}")
        print(f"[INFO] Legacy boundary rows:     {rows_with_legacy_boundaries:,}")
        print(f"[INFO] Train tokens:             {train_tokens:,}")
        print(f"[INFO] Val tokens:               {val_tokens:,}")
        print(f"[INFO] Train windows:            {train_windows:,}")
        print(f"[INFO] Val windows:              {val_windows:,}")
        print()


class LocalUint32BlockStream(IterableDataset):
    def __init__(
        self,
        bin_path: str,
        block_size: int,
        seed: int = 42,
        shuffle_blocks: bool = False,
        max_tokens: int = 0,
        window_mode: str = "stream",
        windows_path: Optional[str] = None,
        label_only_loss: bool = False,
        loss_delimiter_ids: Optional[List[int]] = None,
        bos_id: Optional[int] = None,
        eos_id: Optional[int] = None,
    ):
        super().__init__()

        if window_mode not in ("stream", "row"):
            raise ValueError("window_mode must be 'stream' or 'row'")

        self.bin_path = str(bin_path)
        self.block_size = int(block_size)
        self.seed = int(seed)
        self.shuffle_blocks = bool(shuffle_blocks)
        self.max_tokens = int(max_tokens)
        self.window_mode = str(window_mode)
        self.windows_path = str(windows_path) if windows_path is not None else None
        self.label_only_loss = bool(label_only_loss)
        self.loss_delimiter_ids = [int(x) for x in (loss_delimiter_ids or [])]
        self.bos_id = int(bos_id) if bos_id is not None else None
        self.eos_id = int(eos_id) if eos_id is not None else None
        self._epoch = 0

        file_size = os.path.getsize(self.bin_path)

        if file_size % 4 != 0:
            raise ValueError(f"Token file size is not divisible by 4: {self.bin_path}")

        self.num_tokens_total = file_size // 4

        if self.max_tokens > 0:
            self.num_tokens = min(self.num_tokens_total, self.max_tokens)
        else:
            self.num_tokens = self.num_tokens_total

        if self.window_mode == "row":
            if self.windows_path is None:
                raise ValueError("windows_path is required when window_mode='row'")

            window_file_size = os.path.getsize(self.windows_path)

            if window_file_size % 16 != 0:
                raise ValueError(f"Window file size is not divisible by 16: {self.windows_path}")

            self.num_windows_total = window_file_size // 16

            if self.num_windows_total <= 0:
                raise ValueError(f"No row windows available in {self.windows_path}")

            self.num_blocks = self._count_valid_row_windows_for_budget()

            if self.num_blocks <= 0:
                raise ValueError("No valid row windows available for current max_tokens budget.")
        else:
            if self.num_tokens <= self.block_size + 1:
                raise ValueError(
                    f"Not enough tokens in {self.bin_path}: "
                    f"{self.num_tokens} <= block_size+1={self.block_size + 1}"
                )

            self.num_windows_total = 0
            self.num_blocks = self.num_tokens // (self.block_size + 1)

            if self.num_blocks <= 0:
                raise ValueError("No full blocks available.")

    def _count_valid_row_windows_for_budget(self) -> int:
        if self.max_tokens <= 0:
            return int(self.num_windows_total)

        windows = np.memmap(
            self.windows_path,
            dtype=np.uint64,
            mode="r",
            shape=(self.num_windows_total, 2),
        )

        count = 0

        for i in range(self.num_windows_total):
            start = int(windows[i, 0])
            length = int(windows[i, 1])

            if start + length <= self.num_tokens:
                count += 1

        return count

    def set_epoch(self, epoch: int):
        self._epoch = int(epoch)

    def _make_block_ids(self, total: int) -> List[int]:
        block_ids = list(range(total))

        if self.shuffle_blocks:
            rng = random.Random(self.seed + 1_000_003 * self._epoch)
            rng.shuffle(block_ids)

        return block_ids

    @staticmethod
    def _find_subsequence(seq: List[int], sub: List[int]) -> int:
        if not sub:
            return -1

        n = len(seq)
        m = len(sub)

        if m > n:
            return -1

        for i in range(0, n - m + 1):
            if seq[i:i + m] == sub:
                return i

        return -1

    def _apply_label_only_loss_mask(
        self,
        raw_tokens: np.ndarray,
        tgt_arr: np.ndarray,
    ) -> np.ndarray:
        if not self.label_only_loss:
            return tgt_arr

        masked = np.full(tgt_arr.shape, IGNORE_INDEX, dtype=np.int64)

        raw = [int(x) for x in raw_tokens.tolist()]
        eos_id = self.eos_id
        bos_id = self.bos_id

        usable = min(len(tgt_arr), max(0, len(raw) - 1))

        # Compatible stream :
        # une fenêtre peut contenir plusieurs lignes :
        # [BOS] texte === Label [EOS] [BOS] texte === Label [EOS] ...
        #
        # On applique donc le masque par segment BOS/EOS.
        # Pour chaque segment, on garde seulement :
        # - les tokens cible après le delimiter
        # - EOS
        # - BOS, pour apprendre proprement la reprise de ligne en stream
        segment_start = 0

        while segment_start < len(raw):
            if bos_id is not None:
                next_bos = -1

                for i in range(segment_start, len(raw)):
                    if int(raw[i]) == bos_id:
                        next_bos = i
                        break

                if next_bos == -1:
                    seg_start = segment_start
                else:
                    seg_start = next_bos
            else:
                seg_start = segment_start

            if eos_id is not None:
                seg_end = len(raw)

                for i in range(seg_start + 1, len(raw)):
                    if int(raw[i]) == eos_id:
                        seg_end = i + 1
                        break
            else:
                seg_end = len(raw)

            if seg_start >= seg_end:
                break

            segment = raw[seg_start:seg_end]
            local_delim_pos = self._find_subsequence(segment, self.loss_delimiter_ids)

            if local_delim_pos == -1:
                keep_start = None
            else:
                keep_start = seg_start + local_delim_pos + len(self.loss_delimiter_ids)

            for j in range(usable):
                target_raw_index = j + 1

                if target_raw_index < seg_start or target_raw_index >= seg_end:
                    continue

                target_id = int(raw[target_raw_index])
                keep = False

                if keep_start is not None and target_raw_index >= keep_start:
                    keep = True

                if eos_id is not None and target_id == eos_id:
                    keep = True

                if bos_id is not None and target_id == bos_id:
                    keep = True

                if keep:
                    masked[j] = int(tgt_arr[j])

            if seg_end <= segment_start:
                break

            segment_start = seg_end

        return masked

    def _iter_stream(self, worker_id: int, num_workers: int) -> Iterator[Dict[str, torch.Tensor]]:
        mm = np.memmap(
            self.bin_path,
            dtype=np.uint32,
            mode="r",
            shape=(self.num_tokens_total,),
        )

        block_ids = self._make_block_ids(self.num_blocks)
        block_ids = block_ids[worker_id::num_workers]

        need = self.block_size + 1

        for block_id in block_ids:
            start = block_id * need
            end = start + need

            if end > self.num_tokens:
                continue

            window = np.asarray(mm[start:end], dtype=np.uint32)

            src_arr = window[:-1].astype(np.int64, copy=False)
            tgt_arr = window[1:].astype(np.int64, copy=False)

            if self.label_only_loss:
                tgt_arr = self._apply_label_only_loss_mask(
                    raw_tokens=window,
                    tgt_arr=tgt_arr,
                )

            src = torch.from_numpy(src_arr)
            tgt = torch.from_numpy(tgt_arr)
            padding_mask = torch.zeros((self.block_size,), dtype=torch.bool)

            yield {
                "src": src,
                "tgt": tgt,
                "padding_mask": padding_mask,
                "length": torch.tensor(self.block_size, dtype=torch.long),
            }

    def _iter_row(self, worker_id: int, num_workers: int) -> Iterator[Dict[str, torch.Tensor]]:
        mm = np.memmap(
            self.bin_path,
            dtype=np.uint32,
            mode="r",
            shape=(self.num_tokens_total,),
        )

        windows = np.memmap(
            self.windows_path,
            dtype=np.uint64,
            mode="r",
            shape=(self.num_windows_total, 2),
        )

        block_ids = self._make_block_ids(self.num_windows_total)
        block_ids = block_ids[worker_id::num_workers]

        max_len = self.block_size + 1

        for block_id in block_ids:
            start = int(windows[block_id, 0])
            length = int(windows[block_id, 1])

            if length < 2 or length > max_len:
                continue

            end = start + length

            if end > self.num_tokens:
                continue

            raw = np.asarray(mm[start:end], dtype=np.uint32)

            real_len = int(raw.size) - 1

            if real_len <= 0:
                continue

            src_arr = np.full((self.block_size,), PAD_ID, dtype=np.int64)
            tgt_arr = np.full((self.block_size,), IGNORE_INDEX, dtype=np.int64)
            pad_arr = np.ones((self.block_size,), dtype=np.bool_)

            src_arr[:real_len] = raw[:-1].astype(np.int64, copy=False)
            tgt_arr[:real_len] = raw[1:].astype(np.int64, copy=False)
            pad_arr[:real_len] = False

            if self.label_only_loss:
                tgt_arr = self._apply_label_only_loss_mask(
                    raw_tokens=raw,
                    tgt_arr=tgt_arr,
                )

            src = torch.from_numpy(src_arr)
            tgt = torch.from_numpy(tgt_arr)
            padding_mask = torch.from_numpy(pad_arr)

            yield {
                "src": src,
                "tgt": tgt,
                "padding_mask": padding_mask,
                "length": torch.tensor(real_len, dtype=torch.long),
            }

    def __iter__(self) -> Iterator[Dict[str, torch.Tensor]]:
        wi = get_worker_info()

        if wi is None:
            worker_id = 0
            num_workers = 1
        else:
            worker_id = wi.id
            num_workers = wi.num_workers

        if self.window_mode == "row":
            yield from self._iter_row(worker_id=worker_id, num_workers=num_workers)
        else:
            yield from self._iter_stream(worker_id=worker_id, num_workers=num_workers)


def collate_lm_fixed(batch):
    src = torch.stack([item["src"] for item in batch], dim=0)
    tgt = torch.stack([item["tgt"] for item in batch], dim=0)

    if "padding_mask" in batch[0]:
        padding_mask = torch.stack([item["padding_mask"] for item in batch], dim=0)
    else:
        padding_mask = torch.zeros(
            src.shape,
            dtype=torch.bool,
        )

    return src, tgt, padding_mask


class GPTConfig:
    def __init__(
        self,
        vocab_size: int,
        ctx_len: int = 512,
        n_layer: int = 4,
        n_head: int = 4,
        n_embd: int = 384,
        dropout: float = 0.0,
        attention_backend: str = "sage",
        rope_base: float = 10000.0,
    ):
        if attention_backend not in ("sage", "torch", "flash2", "flash3"):
            raise ValueError("--attention-backend must be 'sage', 'torch', 'flash2' or 'flash3'")

        if n_embd % n_head != 0:
            raise ValueError("n_embd must be divisible by n_head")

        head_dim = n_embd // n_head

        if head_dim % 2 != 0:
            raise ValueError(
                f"RoPE requires even head_dim, got {head_dim}. "
                "Use n_embd/n_head producing an even head dimension."
            )

        if rope_base <= 0:
            raise ValueError("--rope-base must be > 0")

        if attention_backend == "sage" and head_dim not in (64, 96, 128):
            raise ValueError(
                f"SageAttention requires head_dim in [64, 96, 128], got {head_dim}. "
                "Examples: 384/4=96, 384/6=64, 256/4=64, 128/2=64."
            )

        if attention_backend == "sage" and dropout != 0.0:
            raise ValueError("SageAttention strict mode requires --dropout 0.0")

        if attention_backend == "flash3" and dropout != 0.0:
            raise ValueError("FlashAttention3 backend requires --dropout 0.0")

        if attention_backend in ("flash2", "flash3") and head_dim % 8 != 0:
            raise ValueError(
                f"FlashAttention requires head_dim multiple of 8, got {head_dim}."
            )

        self.vocab_size = int(vocab_size)
        self.ctx_len = int(ctx_len)
        self.n_layer = int(n_layer)
        self.n_head = int(n_head)
        self.n_embd = int(n_embd)
        self.dropout = float(dropout)
        self.attention_backend = str(attention_backend)
        self.rope_base = float(rope_base)
        self.positional_encoding = "rope"


def rotate_half(x: torch.Tensor) -> torch.Tensor:
    x_even = x[..., ::2]
    x_odd = x[..., 1::2]
    x_rot = torch.stack((-x_odd, x_even), dim=-1)
    return x_rot.flatten(start_dim=-2)


def apply_rope(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
    return (x * cos) + (rotate_half(x) * sin)


class RotaryEmbedding(nn.Module):
    def __init__(
        self,
        dim: int,
        max_position_embeddings: int,
        base: float = 10000.0,
    ):
        super().__init__()

        if dim % 2 != 0:
            raise ValueError(f"RoPE dim must be even, got {dim}")

        self.dim = int(dim)
        self.max_position_embeddings = int(max_position_embeddings)
        self.base = float(base)

        inv_freq = 1.0 / (
            self.base
            ** (
                torch.arange(
                    0,
                    self.dim,
                    2,
                    dtype=torch.float32,
                )
                / self.dim
            )
        )

        self.register_buffer(
            "inv_freq",
            inv_freq,
            persistent=False,
        )

        self._cos_cached = None
        self._sin_cached = None
        self._seq_len_cached = 0
        self._device_cached = None
        self._dtype_cached = None

    def _build_cache(
        self,
        seq_len: int,
        device: torch.device,
        dtype: torch.dtype,
    ):
        t = torch.arange(
            seq_len,
            device=device,
            dtype=torch.float32,
        )

        freqs = torch.einsum(
            "i,j->ij",
            t,
            self.inv_freq.to(device=device, dtype=torch.float32),
        )

        emb = torch.repeat_interleave(freqs, repeats=2, dim=-1)

        cos = emb.cos().to(dtype=dtype).view(1, 1, seq_len, self.dim)
        sin = emb.sin().to(dtype=dtype).view(1, 1, seq_len, self.dim)

        self._cos_cached = cos
        self._sin_cached = sin
        self._seq_len_cached = int(seq_len)
        self._device_cached = device
        self._dtype_cached = dtype

    def forward(
        self,
        seq_len: int,
        device: torch.device,
        dtype: torch.dtype,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        if (
            self._cos_cached is None
            or self._sin_cached is None
            or self._seq_len_cached < seq_len
            or self._device_cached != device
            or self._dtype_cached != dtype
        ):
            self._build_cache(
                seq_len=seq_len,
                device=device,
                dtype=dtype,
            )

        return (
            self._cos_cached[:, :, :seq_len, :],
            self._sin_cached[:, :, :seq_len, :],
        )


class CausalSelfAttention(nn.Module):
    def __init__(self, cfg: GPTConfig):
        super().__init__()

        self.n_head = cfg.n_head
        self.head_dim = cfg.n_embd // cfg.n_head
        self.attention_backend = cfg.attention_backend
        self.dropout_p = float(cfg.dropout)

        self.qkv = nn.Linear(cfg.n_embd, 3 * cfg.n_embd, bias=False)
        self.proj = nn.Linear(cfg.n_embd, cfg.n_embd, bias=False)
        self.dropout = nn.Dropout(cfg.dropout)

        self.rope = RotaryEmbedding(
            dim=self.head_dim,
            max_position_embeddings=cfg.ctx_len,
            base=cfg.rope_base,
        )

        mask = torch.tril(torch.ones(cfg.ctx_len, cfg.ctx_len))
        self.register_buffer(
            "mask",
            mask.view(1, 1, cfg.ctx_len, cfg.ctx_len),
            persistent=False,
        )

        self.sageattn = None
        self.flash_kernel = None

        if self.attention_backend == "sage":
            try:
                from sageattention import sageattn
            except Exception as exc:
                raise RuntimeError(
                    "SageAttention demandé, mais impossible d'importer : "
                    "from sageattention import sageattn"
                ) from exc

            self.sageattn = sageattn

        if self.attention_backend == "flash2":
            try:
                self.flash_kernel = get_flash2_kernel()
            except Exception as exc:
                raise RuntimeError(
                    "FlashAttention2 demandé, mais impossible de charger : "
                    'get_kernel("kernels-community/flash-attn2", version=1)'
                ) from exc

        if self.attention_backend == "flash3":
            try:
                self.flash_kernel = get_flash3_kernel()
            except Exception as exc:
                raise RuntimeError(
                    "FlashAttention3 demandé, mais impossible de charger : "
                    'get_kernel("kernels-community/flash-attn3", version=1)'
                ) from exc

    def _torch_attention(
        self,
        q: torch.Tensor,
        k: torch.Tensor,
        v: torch.Tensor,
        t: int,
    ) -> torch.Tensor:
        scores = (q @ k.transpose(-2, -1)) / math.sqrt(self.head_dim)

        scores = scores.masked_fill(
            self.mask[:, :, :t, :t] == 0,
            float("-inf"),
        )

        att = F.softmax(scores.float(), dim=-1).to(q.dtype)
        att = self.dropout(att)
        y = att @ v

        return y

    def _sage_attention(
        self,
        q: torch.Tensor,
        k: torch.Tensor,
        v: torch.Tensor,
    ) -> torch.Tensor:
        if self.sageattn is None:
            raise RuntimeError("SageAttention demandé mais sageattn est None")

        if not q.is_cuda:
            raise RuntimeError("SageAttention exige CUDA")

        q = q.contiguous()
        k = k.contiguous()
        v = v.contiguous()

        y = self.sageattn(
            q,
            k,
            v,
            tensor_layout="HND",
            is_causal=True,
        )

        return y

    def _flash2_attention(
        self,
        q: torch.Tensor,
        k: torch.Tensor,
        v: torch.Tensor,
    ) -> torch.Tensor:
        if self.flash_kernel is None:
            raise RuntimeError("FlashAttention2 demandé mais flash_kernel est None")

        if not q.is_cuda:
            raise RuntimeError("FlashAttention2 exige CUDA")

        q = q.transpose(1, 2).contiguous()
        k = k.transpose(1, 2).contiguous()
        v = v.transpose(1, 2).contiguous()

        dropout_p = self.dropout_p if self.training else 0.0

        y = self.flash_kernel.flash_attn_func(
            q,
            k,
            v,
            dropout_p=dropout_p,
            causal=True,
        )

        y = y.transpose(1, 2).contiguous()

        return y

    def _flash3_attention(
        self,
        q: torch.Tensor,
        k: torch.Tensor,
        v: torch.Tensor,
    ) -> torch.Tensor:
        if self.flash_kernel is None:
            raise RuntimeError("FlashAttention3 demandé mais flash_kernel est None")

        if not q.is_cuda:
            raise RuntimeError("FlashAttention3 exige CUDA")

        q = q.transpose(1, 2).contiguous()
        k = k.transpose(1, 2).contiguous()
        v = v.transpose(1, 2).contiguous()

        y = self.flash_kernel.flash_attn_func(
            q,
            k,
            v,
            causal=True,
        )

        y = y.transpose(1, 2).contiguous()

        return y

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        b, t, c = x.shape

        qkv = self.qkv(x)
        q, k, v = qkv.chunk(3, dim=-1)

        q = q.view(b, t, self.n_head, self.head_dim).transpose(1, 2).contiguous()
        k = k.view(b, t, self.n_head, self.head_dim).transpose(1, 2).contiguous()
        v = v.view(b, t, self.n_head, self.head_dim).transpose(1, 2).contiguous()

        cos, sin = self.rope(
            seq_len=t,
            device=q.device,
            dtype=q.dtype,
        )

        q = apply_rope(q, cos, sin)
        k = apply_rope(k, cos, sin)

        if self.attention_backend == "sage":
            y = self._sage_attention(q, k, v)
        elif self.attention_backend == "flash2":
            y = self._flash2_attention(q, k, v)
        elif self.attention_backend == "flash3":
            y = self._flash3_attention(q, k, v)
        else:
            y = self._torch_attention(q, k, v, t)

        y = y.transpose(1, 2).contiguous().view(b, t, c)
        y = self.proj(y)

        return y


class MLP(nn.Module):
    def __init__(self, cfg: GPTConfig):
        super().__init__()

        self.fc = nn.Linear(cfg.n_embd, 4 * cfg.n_embd, bias=False)
        self.proj = nn.Linear(4 * cfg.n_embd, cfg.n_embd, bias=False)
        self.dropout = nn.Dropout(cfg.dropout)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.fc(x)
        x = F.gelu(x)
        x = self.proj(x)
        x = self.dropout(x)

        return x


class Block(nn.Module):
    def __init__(self, cfg: GPTConfig):
        super().__init__()

        self.ln1 = nn.LayerNorm(cfg.n_embd)
        self.attn = CausalSelfAttention(cfg)
        self.ln2 = nn.LayerNorm(cfg.n_embd)
        self.mlp = MLP(cfg)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = x + self.attn(self.ln1(x))
        x = x + self.mlp(self.ln2(x))

        return x


class TinyGPT(nn.Module):
    def __init__(self, cfg: GPTConfig):
        super().__init__()

        self.cfg = cfg

        self.tok_emb = nn.Embedding(cfg.vocab_size, cfg.n_embd)
        self.drop = nn.Dropout(cfg.dropout)

        self.blocks = nn.ModuleList(
            [Block(cfg) for _ in range(cfg.n_layer)]
        )

        self.ln_f = nn.LayerNorm(cfg.n_embd)
        self.head = nn.Linear(cfg.n_embd, cfg.vocab_size, bias=False)

        self.head.weight = self.tok_emb.weight

        self.apply(self._init_weights)

    def _init_weights(self, module):
        if isinstance(module, nn.Linear):
            nn.init.normal_(
                module.weight,
                mean=0.0,
                std=0.02,
            )

        if isinstance(module, nn.Embedding):
            nn.init.normal_(
                module.weight,
                mean=0.0,
                std=0.02,
            )

    def forward(
        self,
        idx: torch.Tensor,
        return_hidden: bool = False,
    ):
        b, t = idx.shape

        if t > self.cfg.ctx_len:
            raise ValueError(f"Input length {t} > ctx_len {self.cfg.ctx_len}")

        x = self.tok_emb(idx)
        x = self.drop(x)

        for block in self.blocks:
            x = block(x)

        hidden = self.ln_f(x)
        logits = self.head(hidden)

        if return_hidden:
            return logits, hidden

        return logits

    def embed_mean_pool(self, idx: torch.Tensor) -> torch.Tensor:
        _, hidden = self.forward(idx, return_hidden=True)

        mask = idx.ne(PAD_ID).unsqueeze(-1).to(hidden.dtype)
        summed = (hidden * mask).sum(dim=1)
        denom = mask.sum(dim=1).clamp(min=1.0)

        emb = summed / denom
        emb = F.normalize(emb, p=2, dim=-1)

        return emb


def param_count(model: nn.Module) -> int:
    return int(sum(p.numel() for p in model.parameters()))


class RNETrainer:
    def __init__(
        self,
        model: TinyGPT,
        train_loader: DataLoader,
        val_loader: DataLoader,
        out_dir: str,
        max_steps: int,
        lr: float,
        weight_decay: float,
        save_every: int,
        log_every: int,
        val_every: int,
        val_batches: int,
        dtype: str,
        grad_clip: float,
        device: torch.device,
        compile_model: bool = False,
    ):
        self.model = model
        self.train_loader = train_loader
        self.val_loader = val_loader
        self.out_dir = Path(out_dir)
        self.max_steps = int(max_steps)
        self.lr = float(lr)
        self.weight_decay = float(weight_decay)
        self.save_every = int(save_every)
        self.log_every = int(log_every)
        self.val_every = int(val_every)
        self.val_batches = int(val_batches)
        self.dtype = dtype
        self.grad_clip = float(grad_clip)
        self.device = device

        if dtype == "float16":
            self.amp_dtype = torch.float16
        elif dtype == "bfloat16":
            self.amp_dtype = torch.bfloat16
        else:
            self.amp_dtype = torch.float32

        self.use_amp = self.device.type == "cuda" and dtype in ("float16", "bfloat16")

        self.optimizer = torch.optim.AdamW(
            self.model.parameters(),
            lr=self.lr,
            betas=(0.9, 0.95),
            weight_decay=self.weight_decay,
        )

        self.scaler = torch.amp.GradScaler(
            "cuda",
            enabled=self.use_amp,
        )

        self.criterion = nn.CrossEntropyLoss(ignore_index=IGNORE_INDEX)

        if compile_model:
            self.model = torch.compile(self.model)

        self.tokens_seen_total = 0
        self.tokens_seen_since = 0
        self.steps_since = 0
        self.amp_overflow_count = 0
        self.rate_t0 = time.perf_counter()

    def _set_lr(self, lr: float):
        for group in self.optimizer.param_groups:
            group["lr"] = lr

    def _get_lr(self, step: int) -> float:
        return self.lr

    def _reset_rate_window(self):
        self.rate_t0 = time.perf_counter()
        self.tokens_seen_since = 0
        self.steps_since = 0
        self.amp_overflow_count = 0

    def _rate_info(self) -> Tuple[float, float]:
        now = time.perf_counter()
        dt = max(now - self.rate_t0, 1e-9)
        tok_s = self.tokens_seen_since / dt
        step_s = self.steps_since / dt
        return tok_s, step_s

    def _save(self, step: int):
        self.out_dir.mkdir(parents=True, exist_ok=True)

        raw_model = self.model._orig_mod if hasattr(self.model, "_orig_mod") else self.model

        payload = {
            "step": int(step),
            "model": raw_model.state_dict(),
            "optimizer": self.optimizer.state_dict(),
            "config": {
                "vocab_size": raw_model.cfg.vocab_size,
                "ctx_len": raw_model.cfg.ctx_len,
                "n_layer": raw_model.cfg.n_layer,
                "n_head": raw_model.cfg.n_head,
                "n_embd": raw_model.cfg.n_embd,
                "dropout": raw_model.cfg.dropout,
                "attention_backend": raw_model.cfg.attention_backend,
                "positional_encoding": raw_model.cfg.positional_encoding,
                "rope_base": raw_model.cfg.rope_base,
                "PAD_ID": PAD_ID,
                "IGNORE_INDEX": IGNORE_INDEX,
                "boundary_mode": BOUNDARY_MODE,
                "default_bos_marker": DEFAULT_BOS_MARKER,
                "default_eos_marker": DEFAULT_EOS_MARKER,
            },
            "tokens_seen_total": int(self.tokens_seen_total),
        }

        ckpt = self.out_dir / f"checkpoint_step_{step}.pt"
        latest = self.out_dir / "latest.pt"

        torch.save(payload, ckpt)
        torch.save(payload, latest)

        print(f"\n[SAVE] {ckpt}")

    def evaluate(self) -> float:
        self.model.eval()

        total_loss = 0.0
        seen = 0

        with torch.no_grad():
            for batch in self.val_loader:
                src, tgt, padding_mask = batch

                src = src.to(self.device, non_blocking=True)
                tgt = tgt.to(self.device, non_blocking=True)

                with torch.autocast(
                    device_type="cuda",
                    dtype=self.amp_dtype,
                    enabled=self.use_amp,
                ):
                    logits = self.model(src)

                loss = self.criterion(
                    logits.reshape(-1, logits.size(-1)).float(),
                    tgt.reshape(-1),
                )

                total_loss += float(loss.item())
                seen += 1

                if seen >= self.val_batches:
                    break

        self.model.train()

        return total_loss / max(1, seen)

    def train(self):
        self.model.train()

        step = 0
        running_loss = 0.0
        running_count = 0
        last_val_loss = None

        train_iter = iter(self.train_loader)
        self._reset_rate_window()

        pbar = tqdm(
            total=self.max_steps,
            desc="Training/LM-SAGE11-WINDOWS-ROPE",
            dynamic_ncols=True,
        )

        while step < self.max_steps:
            try:
                src, tgt, padding_mask = next(train_iter)
            except StopIteration:
                train_iter = iter(self.train_loader)
                src, tgt, padding_mask = next(train_iter)

            src = src.to(self.device, non_blocking=True)
            tgt = tgt.to(self.device, non_blocking=True)

            batch_tokens = int(tgt.ne(IGNORE_INDEX).sum().item())
            lr = self._get_lr(step + 1)
            self._set_lr(lr)

            self.optimizer.zero_grad(set_to_none=True)

            with torch.autocast(
                device_type="cuda",
                dtype=self.amp_dtype,
                enabled=self.use_amp,
            ):
                logits = self.model(src)

            loss = self.criterion(
                logits.reshape(-1, logits.size(-1)).float(),
                tgt.reshape(-1),
            )

            if not torch.isfinite(loss):
                raise RuntimeError(f"Non-finite loss detected: {loss.item()}")

            self.scaler.scale(loss).backward()
            self.scaler.unscale_(self.optimizer)

            if self.grad_clip > 0:
                nn.utils.clip_grad_norm_(
                    self.model.parameters(),
                    max_norm=self.grad_clip,
                )

            scale_before = float(self.scaler.get_scale())
            self.scaler.step(self.optimizer)
            self.scaler.update()
            scale_after = float(self.scaler.get_scale())

            if self.use_amp and scale_after < scale_before:
                self.amp_overflow_count += 1
                self.optimizer.zero_grad(set_to_none=True)

                if self.amp_overflow_count <= 3:
                    print(
                        f"[amp] overflow detected: scale {scale_before:.1f} -> {scale_after:.1f}; skipping update"
                    )

                continue

            step += 1
            pbar.update(1)

            self.tokens_seen_total += batch_tokens
            self.tokens_seen_since += batch_tokens
            self.steps_since += 1

            running_loss += float(loss.item())
            running_count += 1

            if step % self.val_every == 0:
                last_val_loss = self.evaluate()

            if step % self.log_every == 0:
                avg_loss = running_loss / max(1, running_count)
                ppl = math.exp(min(avg_loss, 20.0))
                tok_s, step_s = self._rate_info()

                postfix = {
                    "loss": f"{avg_loss:.4f}",
                    "ppl": f"{ppl:.2f}",
                    "lr": f"{lr:.2e}",
                    "seen": format_tokens(self.tokens_seen_total),
                    "tok_s": f"{tok_s:,.0f}",
                    "step_s": f"{step_s:.2f}",
                }

                if last_val_loss is not None:
                    postfix["val_loss"] = f"{last_val_loss:.4f}"
                    postfix["val_ppl"] = f"{math.exp(min(last_val_loss, 20.0)):.2f}"

                if self.amp_overflow_count > 0:
                    postfix["amp_of"] = str(self.amp_overflow_count)

                pbar.set_postfix(**postfix)

                running_loss = 0.0
                running_count = 0
                self._reset_rate_window()

            if step % self.save_every == 0:
                self._save(step)

        pbar.close()
        self._save(step)

        print()
        print("[DONE] Training finished.")
        print(f"[DONE] Steps:              {step:,}")
        print(f"[DONE] Tokens seen:        {self.tokens_seen_total:,}")
        print(f"[DONE] Tokens compact:     {format_tokens(self.tokens_seen_total)}")

        if last_val_loss is not None:
            print(f"[DONE] Last val loss:      {last_val_loss:.6f}")
            print(f"[DONE] Last val ppl:       {math.exp(min(last_val_loss, 20.0)):.6f}")


def parse_args():
    parser = argparse.ArgumentParser(
        description="LM trainer with pretokenization cache, generic marker->special-token mapping, BOS/EOS boundaries, row/stream window modes, RoPE positional encoding, SageAttention, torch attention, FlashAttention2 and FlashAttention3 via HF kernels."
    )

    parser.add_argument("--src", required=True)
    parser.add_argument("--tokenizer", required=True)
    parser.add_argument("--out-dir", default="LM_SAGE11_ROPE")
    parser.add_argument("--cache-dir", default="lm_token_cache_sage11_marker_special_windows_rope")

    parser.add_argument("--activity-column", default="activites")
    parser.add_argument("--row-batch-size", type=int, default=100_000)
    parser.add_argument("--rebuild-cache", action="store_true")

    parser.add_argument("--shuffle-before-tokenize", action="store_true")
    parser.add_argument("--no-shuffle-before-tokenize", action="store_true")
    parser.add_argument("--shuffle-buffer-size", type=int, default=500_000)

    parser.add_argument("--ctx-len", type=int, default=512)
    parser.add_argument(
        "--window-mode",
        default="stream",
        choices=["stream", "row"],
        help="stream = old continuous-token behavior. row = one parquet row cannot cross context; rows longer than ctx_len+1 are dropped; shorter rows are padded and ignored in loss.",
    )

    parser.add_argument("--batch-size", type=int, default=4)
    parser.add_argument("--num-workers", type=int, default=0)

    parser.add_argument("--shuffle-blocks", action="store_true")
    parser.add_argument("--max-train-tokens", type=int, default=0)
    parser.add_argument("--max-val-tokens", type=int, default=0)

    parser.add_argument("--val-ratio", type=float, default=0.01)
    parser.add_argument("--val-every", type=int, default=2000)
    parser.add_argument("--val-batches", type=int, default=10)

    parser.add_argument("--n-layer", type=int, default=4)
    parser.add_argument("--n-head", type=int, default=4)
    parser.add_argument("--n-embd", type=int, default=384)
    parser.add_argument("--dropout", type=float, default=0.0)

    parser.add_argument(
        "--attention-backend",
        default="sage",
        choices=["sage", "torch", "flash2", "flash3"],
    )

    parser.add_argument(
        "--rope-base",
        type=float,
        default=10000.0,
        help="RoPE base theta. Default 10000.0.",
    )

    parser.add_argument("--lr", type=float, default=3e-4)
    parser.add_argument("--weight-decay", type=float, default=0.1)
    parser.add_argument("--max-steps", type=int, default=50_000)
    parser.add_argument("--save-every", type=int, default=10_000)
    parser.add_argument("--log-every", type=int, default=20)
    parser.add_argument("--grad-clip", type=float, default=1.0)

    parser.add_argument("--dtype", default="bfloat16", choices=["float32", "float16", "bfloat16"])
    parser.add_argument("--device", default="cuda")
    parser.add_argument("--seed", type=int, default=42)
    parser.add_argument("--lowercase", action="store_true")

    parser.add_argument(
        "--special-marker",
        action="append",
        default=[],
        help='Map a dataset marker to a tokenizer special token. Example: --special-marker "<|BOC|>=[BOC]". Can be repeated.',
    )

    parser.add_argument(
        "--no-special-boundaries",
        action="store_true",
        help="Disable BOS/EOS insertion and marker replacement during pretokenization.",
    )

    parser.add_argument(
        "--no-append-sep",
        action="store_true",
        help="Legacy alias: disables BOS/EOS insertion too.",
    )

    parser.add_argument(
        "--label-only-loss",
        action="store_true",
        help="Mask loss to -100 everywhere except the label segment after the delimiter and EOS. Designed for rows like: <|BOS|> text === Label <|EOS|>.",
    )

    parser.add_argument(
        "--target-delimiter",
        default="===",
        help="Delimiter before the supervised target label. Default: ===",
    )

    parser.add_argument("--compile", action="store_true")

    return parser.parse_args()


def main():
    args = parse_args()

    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)

    if args.device == "cuda" and not torch.cuda.is_available():
        print("[WARN] CUDA unavailable, using CPU.")
        args.device = "cpu"

    if args.attention_backend in ("sage", "flash2", "flash3") and args.device != "cuda":
        raise RuntimeError(f"--attention-backend {args.attention_backend} requires --device cuda")

    if args.no_shuffle_before_tokenize:
        shuffle_before_tokenize = False
    else:
        shuffle_before_tokenize = True

    if args.shuffle_before_tokenize:
        shuffle_before_tokenize = True

    append_special_tokens = True

    if args.no_special_boundaries:
        append_special_tokens = False

    if args.no_append_sep:
        append_special_tokens = False

    token_cache = RNETokenCache(
        src=args.src,
        tokenizer_path=args.tokenizer,
        cache_dir=args.cache_dir,
        activity_column=args.activity_column,
        row_batch_size=args.row_batch_size,
        val_ratio=args.val_ratio,
        seed=args.seed,
        lowercase=args.lowercase,
        append_special_tokens=append_special_tokens,
        rebuild_cache=args.rebuild_cache,
        shuffle_before_tokenize=shuffle_before_tokenize,
        shuffle_buffer_size=args.shuffle_buffer_size,
        special_marker_specs=args.special_marker,
        window_mode=args.window_mode,
        ctx_len=args.ctx_len,
    )

    token_cache.build_if_needed()

    train_windows_path = str(token_cache.train_windows_bin) if args.window_mode == "row" else None
    val_windows_path = str(token_cache.val_windows_bin) if args.window_mode == "row" else None

    loss_delimiter_ids: List[int] = []

    if args.label_only_loss:
        loss_delimiter_ids = token_cache._encode_plain_chunk(args.target_delimiter)

        if not loss_delimiter_ids:
            raise RuntimeError(f"Impossible de tokenizer le delimiter: {repr(args.target_delimiter)}")

        print(f"[INFO] Label-only loss:          enabled")
        print(f"[INFO] Target delimiter:         {repr(args.target_delimiter)}")
        print(f"[INFO] Target delimiter ids:     {loss_delimiter_ids}")
        print(f"[INFO] Loss rule:                stream-safe per BOS/EOS segment; -100 before and including delimiter; keep label target + EOS/BOS")
    else:
        print(f"[INFO] Label-only loss:          disabled")

    train_ds = LocalUint32BlockStream(
        bin_path=str(token_cache.train_bin),
        block_size=args.ctx_len,
        seed=args.seed,
        shuffle_blocks=args.shuffle_blocks,
        max_tokens=args.max_train_tokens,
        window_mode=args.window_mode,
        windows_path=train_windows_path,
        label_only_loss=args.label_only_loss,
        loss_delimiter_ids=loss_delimiter_ids,
        bos_id=token_cache.bos_id,
        eos_id=token_cache.eos_id,
    )

    val_ds = LocalUint32BlockStream(
        bin_path=str(token_cache.val_bin),
        block_size=args.ctx_len,
        seed=args.seed + 10_000_000,
        shuffle_blocks=False,
        max_tokens=args.max_val_tokens,
        window_mode=args.window_mode,
        windows_path=val_windows_path,
        label_only_loss=args.label_only_loss,
        loss_delimiter_ids=loss_delimiter_ids,
        bos_id=token_cache.bos_id,
        eos_id=token_cache.eos_id,
    )

    train_loader = DataLoader(
        train_ds,
        batch_size=args.batch_size,
        num_workers=args.num_workers,
        collate_fn=collate_lm_fixed,
        drop_last=True,
        pin_memory=(args.device == "cuda"),
        persistent_workers=(args.num_workers > 0),
    )

    val_loader = DataLoader(
        val_ds,
        batch_size=args.batch_size,
        num_workers=max(0, args.num_workers // 2),
        collate_fn=collate_lm_fixed,
        drop_last=True,
        pin_memory=(args.device == "cuda"),
        persistent_workers=(args.num_workers > 1),
    )

    cfg = GPTConfig(
        vocab_size=token_cache.vocab_size,
        ctx_len=args.ctx_len,
        n_layer=args.n_layer,
        n_head=args.n_head,
        n_embd=args.n_embd,
        dropout=args.dropout,
        attention_backend=args.attention_backend,
        rope_base=args.rope_base,
    )

    device = torch.device(args.device)
    model = TinyGPT(cfg).to(device)

    params = param_count(model)

    target_tokens = args.max_steps * args.batch_size * args.ctx_len
    train_epoch_steps = max(1, train_ds.num_blocks // max(1, args.batch_size))
    approx_epochs = args.max_steps / train_epoch_steps

    print("[INFO] LM SAGE11 GENERIC SPECIAL MARKERS + FLASH KERNELS + WINDOW MODE + ROPE")
    print(f"[INFO] Source:                  {args.src}")
    print(f"[INFO] Activity column:         {args.activity_column}")
    print(f"[INFO] Tokenizer:               {token_cache.tokenizer_path}")
    print(f"[INFO] Cache dir:               {args.cache_dir}")
    print(f"[INFO] Vocab size:              {token_cache.vocab_size:,}")
    print(f"[INFO] Append special tokens:   {append_special_tokens}")
    print(f"[INFO] BOS id:                  {token_cache.bos_id}")
    print(f"[INFO] EOS id:                  {token_cache.eos_id}")
    print(f"[INFO] SEP id:                  {token_cache.sep_id}")
    print(f"[INFO] Boundary mode:           {BOUNDARY_MODE}")
    print(f"[INFO] Window mode:             {args.window_mode}")
    print(f"[INFO] Positional encoding:     RoPE")
    print(f"[INFO] RoPE base:               {args.rope_base}")
    print(f"[INFO] Marker token map:        {token_cache.marker_token_map}")
    print(f"[INFO] Marker id map:           {token_cache.marker_id_map}")
    print(f"[INFO] Boundary rule:           explicit <|BOS|> + <|EOS|> => no auto BOS/EOS")
    print(f"[INFO] Legacy rule:             otherwise BOS + text + EOS")
    if args.window_mode == "row":
        print(f"[INFO] Row rule:                no crossing rows; rows longer than ctx_len+1 dropped; shorter rows padded + loss ignored")
    else:
        print(f"[INFO] Stream rule:             continuous token stream, old behavior")
    print(f"[INFO] Shuffle before tok:      {shuffle_before_tokenize}")
    print(f"[INFO] Shuffle buffer size:     {args.shuffle_buffer_size:,}")
    print(f"[INFO] Ctx len:                 {args.ctx_len}")
    print(f"[INFO] Batch size:              {args.batch_size}")
    print(f"[INFO] Num workers:             {args.num_workers}")
    print(f"[INFO] Shuffle blocks:          {args.shuffle_blocks}")
    print(f"[INFO] Tokens / step max:       {args.batch_size * args.ctx_len:,}")
    print(f"[INFO] Train tokens file:       {train_ds.num_tokens:,}")
    print(f"[INFO] Val tokens file:         {val_ds.num_tokens:,}")
    print(f"[INFO] Train blocks/windows:    {train_ds.num_blocks:,}")
    print(f"[INFO] Steps / epoch:           {train_epoch_steps:,}")
    print(f"[INFO] Approx epochs:           {approx_epochs:.2f}")
    print(f"[INFO] Target tokens seen max:  {target_tokens:,}")
    print(f"[INFO] Target compact max:      {format_tokens(target_tokens)}")
    print(f"[INFO] Val ratio:               {args.val_ratio}")
    print(f"[INFO] Val every:               {args.val_every}")
    print(f"[INFO] Val batches:             {args.val_batches}")
    print(f"[INFO] Params:                  {params:,}")
    print(f"[INFO] Device:                  {device}")
    print(f"[INFO] Dtype:                   {args.dtype}")
    print(f"[INFO] Attention backend:       {args.attention_backend}")
    print(f"[INFO] Head dim:                {args.n_embd // args.n_head}")
    print(f"[INFO] LR fixed:                {args.lr}")
    print(f"[INFO] Output dir:              {args.out_dir}")
    print()

    trainer = RNETrainer(
        model=model,
        train_loader=train_loader,
        val_loader=val_loader,
        out_dir=args.out_dir,
        max_steps=args.max_steps,
        lr=args.lr,
        weight_decay=args.weight_decay,
        save_every=args.save_every,
        log_every=args.log_every,
        val_every=args.val_every,
        val_batches=args.val_batches,
        dtype=args.dtype,
        grad_clip=args.grad_clip,
        device=device,
        compile_model=args.compile,
    )

    trainer.train()


if __name__ == "__main__":
    main()