File size: 74,411 Bytes
77d1918
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
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
#@title Model — ready
"""TAMELM model definitions: routed attention, depth attention, and dropless MoE.

This module stays self-contained for notebook execution and portable Hugging Face
exports. Definitions are ordered from configuration and low-level operations to
model components, cache/output interfaces, and the complete language model.

Routing uses shared GQA projections and original top-k softmax probabilities.
Tile routing and graph selection are causal; cached decoding retains tile state.
CUDA FlexAttention and Triton grouped GEMM are optional acceleration backends.
"""

from __future__ import annotations

import ast as _ast
import inspect as _inspect
import linecache as _linecache
import math
from dataclasses import dataclass
from typing import Dict, Optional, Tuple

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import (
    AutoConfig,
    AutoModelForCausalLM,
    GenerationMixin,
    PretrainedConfig,
    PreTrainedModel,
)
from transformers.cache_utils import Cache
from transformers.utils import ModelOutput

try:
    from torch.nn.attention.flex_attention import (
        BlockMask,
        create_block_mask,
        flex_attention,
    )

    HAS_FLEX = True
except ImportError:  # pragma: no cover
    HAS_FLEX = False

try:
    import triton  # type: ignore
    import triton.language as tl  # type: ignore

    HAS_TRITON = True
except ImportError:
    HAS_TRITON = False


# Notebook source capture and semantic fingerprints.
# Capture source independently of execution counts, paths, and timestamps.
_NOTEBOOK_SOURCES = globals().get("_NOTEBOOK_SOURCES", {})


# =============================================================================
# Source identity helpers
# =============================================================================


def _capture_cell_source(component):
    caller = _inspect.currentframe().f_back
    source = "".join(_linecache.getlines(caller.f_code.co_filename))
    if not source:
        try:
            source = get_ipython().history_manager.input_hist_raw[-1]
        except (NameError, AttributeError, IndexError):
            source = ""
    if not source.strip():
        raise RuntimeError("Cannot capture executed source; run the definition cells in Jupyter/Colab.")
    _NOTEBOOK_SOURCES[component] = source


class _WithoutDocstrings(_ast.NodeTransformer):
    def _strip(self, node):
        self.generic_visit(node)
        if (
            node.body
            and isinstance(node.body[0], _ast.Expr)
            and isinstance(node.body[0].value, _ast.Constant)
            and isinstance(node.body[0].value.value, str)
        ):
            node.body = node.body[1:] or [_ast.Pass()]
        return node

    visit_Module = _strip
    visit_ClassDef = _strip
    visit_FunctionDef = _strip
    visit_AsyncFunctionDef = _strip


def _semantic_source(source):
    return _ast.dump(
        _WithoutDocstrings().visit(_ast.parse(source)), include_attributes=False
    )


# Routing mechanism IDs are part of the attention/cache contract.
GLOBAL, LOCAL, STRIDED, GRAPH = 0, 1, 2, 3
MECH_NAMES = ("global", "local", "strided", "graph")


# =============================================================================
# Configuration
# =============================================================================


@dataclass
class RoutedAttentionConfig:
    d_model: int
    n_q_heads: int = 16
    n_kv_heads: int = 4
    head_dim: Optional[int] = None
    top_k: int = 2
    tile: int = 128  # routing granularity == FlexAttention block size
    local_window: int = 256  # tokens
    strided_recent: int = 128  # tokens of dense recent context for 'strided'
    strided_every: int = 4  # plus every S-th tile back (tile-relative)
    graph_top_r: int = 0  # past tiles picked by 'graph'; 0 = legacy 3-way router
    graph_recent: int = 128  # tokens of dense recent context for 'graph'
    max_seq_len: int = 2048
    router_init_std: float = 1e-2
    balance_coef: float = 1e-2
    backend: str = "flex"  # "flex" | "dense"; decode/cached paths always dense





# =============================================================================
# Shared routing precision
# =============================================================================


def router_logits_fp32(router, x):
    """Keep routing decisions out of AMP; casting rounded BF16 logits is too late."""
    with torch.autocast(device_type=x.device.type, enabled=False):
        return F.linear(
            x.float(),
            router.weight.float(),
            None if router.bias is None else router.bias.float(),
        )


# =============================================================================
# Position transforms
# =============================================================================


class RotaryPos(nn.Module):
    """Standard RoPE with interleaved pairs (same pairing convention as BlackHoleRoPE)."""

    def __init__(self, head_dim: int, base: float = 10_000.0):
        super().__init__()
        half = head_dim // 2
        self.register_buffer(
            "inv", 1.0 / base ** (torch.arange(half).float() / half), persistent=False
        )

    def forward(self, x, start_pos: int = 0, **_):  # x: [B, H, T, dh]
        T = x.shape[2]
        t = torch.arange(start_pos, start_pos + T, device=x.device, dtype=torch.float32)
        ang = t[:, None] * self.inv[None]
        cos, sin = ang.cos(), ang.sin()
        xf = x.float()
        x1, x2 = xf[..., 0::2], xf[..., 1::2]
        return (
            torch.stack([x1 * cos - x2 * sin, x1 * sin + x2 * cos], -1)
            .flatten(-2)
            .to(x.dtype)
        )


class WarpedRoPE(nn.Module):
    """RoPE on a content-defined clock, one clock per KV group.

        tau_t = softplus(w . x_t + b) > 0        clock rate of token t
        T_t   = sum_{s<t} tau_s                  token time (exclusive prefix sum)
        angle = omega_f * T_t

    q_m . k_n then depends on T_m - T_n = sum_{n<=s<m} tau_s: only the tokens between
    them, so attention is relative in warped time and independent of where a document
    sits in a packed window. Causal, cumsum-parallel, O(1) decode state (the running
    time). At init (w = 0, softplus(b) = 1) it is exactly standard RoPE.
    """

    def __init__(
        self,
        d_model: int,
        head_dim: int,
        n_q_heads: int,
        n_kv_heads: int,
        base: float = 10_000.0,
    ):
        super().__init__()
        if head_dim % 2 or n_q_heads % n_kv_heads:
            raise ValueError(
                "WarpedRoPE needs an even head_dim and grouped query heads"
            )
        self.half, self.base = head_dim // 2, float(base)
        self.group = n_q_heads // n_kv_heads
        self.rate = nn.Linear(d_model, n_kv_heads)
        self.reset_parameters()

    def reset_parameters(self):
        nn.init.zeros_(self.rate.weight)
        nn.init.constant_(self.rate.bias, math.log(math.e - 1.0))  # softplus(b) = 1

    def clock(self, x, t0=None):
        """x [B,T,d] (normed). t0 [B,Hkv] time already elapsed, or None.
        Returns token times [B,T,Hkv] and inclusive times [B,T,Hkv] (cache state)."""
        tau = F.softplus(router_logits_fp32(self.rate, x))  # fp32
        inclusive = tau.cumsum(1)
        if t0 is not None:
            inclusive = inclusive + t0[:, None]
        return inclusive - tau, inclusive

    def rotate(self, x, times):  # x [B,h,T,dh]; times [B,h,T]
        # Frequencies are recomputed rather than buffered: nothing here can be left
        # uninitialized by meta-device / no_init_weights loading.
        inv = 1.0 / self.base ** (
            torch.arange(self.half, device=x.device, dtype=torch.float32) / self.half
        )
        ang = times[..., None] * inv
        cos, sin = ang.cos(), ang.sin()
        xf = x.float()
        x1, x2 = xf[..., 0::2], xf[..., 1::2]
        return (
            torch.stack([x1 * cos - x2 * sin, x1 * sin + x2 * cos], -1)
            .flatten(-2)
            .to(x.dtype)
        )


class BlackHoleRoPE(nn.Module):
    def __init__(
        self,
        dim,
        max_seq_len=2048,
        base_vortex_strength=1.0,
        energy_amplification=0.1,
        theta_base=10_000.0,
        adaptive_vortex=True,
        discrepancy_modulation=True,
    ):
        super().__init__()
        if dim <= 0 or dim % 2 or max_seq_len < 1:
            raise ValueError(
                "BlackHoleRoPE requires an even dimension and positive max_seq_len"
            )
        self.dim = int(dim)
        self.half_dim = dim // 2
        self.max_seq_len = int(max_seq_len)
        self.energy_amplification = float(energy_amplification)
        self.discrepancy_modulation = bool(discrepancy_modulation)
        freqs = 1.0 / (
            theta_base
            ** (torch.arange(0, self.half_dim, dtype=torch.float32) / self.half_dim)
        )
        self.register_buffer("base_freqs", freqs)
        self.vortex_strength = nn.Parameter(
            torch.full((max_seq_len,), base_vortex_strength),
            requires_grad=adaptive_vortex,
        )
        self.phase_shift = nn.Parameter(torch.randn(max_seq_len, self.half_dim) * 0.01)
        self.radial_warp = nn.Parameter(torch.ones(max_seq_len, self.half_dim))
        if discrepancy_modulation:
            self.discrepancy_projector = nn.Sequential(
                nn.Linear(dim, self.half_dim),
                nn.Tanh(),
                nn.Linear(self.half_dim, self.half_dim),
            )
        self.register_buffer("energy_clamp_min", torch.tensor(0.1))
        self.register_buffer("energy_clamp_max", torch.tensor(10.0))

    def _compute_positional_vortex(self, seq_len, device, dtype, start_pos=0):
        positions = torch.arange(
            start_pos, start_pos + seq_len, dtype=torch.float32, device=device
        )
        position_freqs = positions.unsqueeze(1) * self.base_freqs.unsqueeze(0)
        base_phase = position_freqs + self.phase_shift[start_pos : start_pos + seq_len]
        vortex_phase = self.vortex_strength[start_pos : start_pos + seq_len].unsqueeze(
            1
        ) * torch.sin(base_phase + position_freqs * 0.1)
        final_phase = base_phase + vortex_phase
        rotation = torch.complex(torch.cos(final_phase), torch.sin(final_phase))
        radial_factor = torch.sqrt(position_freqs + 1e-8)
        energy = 1.0 + self.energy_amplification * torch.sin(
            self.radial_warp[start_pos : start_pos + seq_len] * radial_factor
            + vortex_phase.abs()
        )
        energy = energy.clamp(self.energy_clamp_min, self.energy_clamp_max)
        return rotation, energy.to(dtype=dtype)

    def apply_discrepancy_modulation(self, x, symplectic_state):
        if not self.discrepancy_modulation or not symplectic_state:
            return None
        bsz, seq_len, _ = x.shape
        total = torch.zeros(bsz, seq_len, 1, device=x.device, dtype=x.dtype)
        count = 0
        for theta, p in symplectic_state.values():
            if theta is None or p is None:
                continue
            ev = theta.float().var(dim=-1, keepdim=True, correction=0) + p.float().var(
                dim=-1, keepdim=True, correction=0
            )
            ev = (
                ev.unsqueeze(1).expand(-1, seq_len, -1)
                if theta.dim() == 2
                else ev[:, :seq_len]
            )
            total = total + ev.to(dtype=x.dtype)
            count += 1
        if count == 0:
            return None
        lifted = F.pad(total / count, (0, self.dim - 1))
        return self.discrepancy_projector(lifted)

    def forward(self, x, symplectic_state=None, start_pos=0):
        bsz, seq_len, dim = x.shape
        if dim != self.dim or start_pos < 0 or start_pos + seq_len > self.max_seq_len:
            raise ValueError(
                "BlackHoleRoPE dimension/position exceeds configured bounds"
            )
        input_dtype = x.dtype
        xp = x.reshape(bsz, seq_len, self.half_dim, 2)
        xc = torch.complex(xp[..., 0].float(), xp[..., 1].float())
        rotation, energy = self._compute_positional_vortex(
            seq_len, x.device, torch.float32, start_pos
        )
        rotation = rotation.unsqueeze(0)
        energy = energy.unsqueeze(0)
        disc = self.apply_discrepancy_modulation(x, symplectic_state)
        if disc is not None:
            phase_scale = 1.0 + 0.1 * torch.tanh(disc.float())
            base_phase = torch.angle(rotation)
            rotation = torch.polar(
                torch.ones_like(base_phase), base_phase * phase_scale
            )
        amp = xc * rotation * energy.float()
        return (
            torch.stack([amp.real, amp.imag], dim=-1)
            .reshape(bsz, seq_len, dim)
            .to(input_dtype)
        )


class BlackHolePos(nn.Module):
    """Adapter: folds heads into batch for BlackHoleRoPE and repeats the symplectic
    state per head so discrepancy modulation keeps the right batch alignment."""

    def __init__(self, bh: nn.Module):
        super().__init__()
        self.bh = bh

    def forward(self, x, start_pos: int = 0, symplectic_state=None):
        B, H, T, dh = x.shape
        state = None
        if symplectic_state:
            state = {
                n: (th.repeat_interleave(H, 0), p.repeat_interleave(H, 0))
                for n, (th, p) in symplectic_state.items()
                if th is not None and p is not None
            }
        y = self.bh(
            x.reshape(B * H, T, dh), symplectic_state=state, start_pos=start_pos
        )
        return y.view(B, H, T, dh)


# FlexAttention compilation is lazy and shared by CUDA attention calls.
_COMPILED_FLEX = None


# =============================================================================
# Attention backends and visibility geometry
# =============================================================================


def _run_flex(q, k, v, block_mask):
    global _COMPILED_FLEX
    if q.is_cuda:
        if min(q.shape[-1], k.shape[-1], v.shape[-1]) < 16:
            raise ValueError(
                "CUDA FlexAttention requires head_dim >= 16; use a supported GPU model configuration"
            )
        if _COMPILED_FLEX is None:
            _COMPILED_FLEX = torch.compile(flex_attention, dynamic=False)
        return _COMPILED_FLEX(q, k, v, block_mask=block_mask, enable_gqa=True)
    return flex_attention(q, k, v, block_mask=block_mask, enable_gqa=True)


def geometry_allows(mech, q_idx, kv_idx, cfg: RoutedAttentionConfig, picked=None):
    """Boolean key-visibility for a given mechanism id. Pure tensor algebra so the
    same function serves FlexAttention's mask_mod and the dense reference mask.
    `picked` is True where kv_idx lies in a graph-selected tile of q_idx's tile."""
    d = q_idx - kv_idx
    causal = d >= 0
    local = d < cfg.local_window
    tile_gap = torch.div(q_idx, cfg.tile, rounding_mode="floor") - torch.div(
        kv_idx, cfg.tile, rounding_mode="floor"
    )
    strided = (d < cfg.strided_recent) | (
        torch.remainder(tile_gap, cfg.strided_every) == 0
    )
    allowed = (
        (mech == GLOBAL) | ((mech == LOCAL) & local) | ((mech == STRIDED) & strided)
    )
    if cfg.graph_top_r:
        graph = d < cfg.graph_recent
        if picked is not None:
            graph = graph | picked
        allowed = allowed | ((mech == GRAPH) & graph)
    return causal & allowed


def geometry_block_mask(mech, length, cfg, mask_mod, picked=None):
    """Exact block classification in O(B * H * (T/tile)^2), never O(B*H*T^2).

    A partial block uses the original elementwise predicate. Fully allowed blocks
    skip that predicate. No attention probabilities or causal rules are approximated.
    `picked` is the tile-level graph selection [B, H, n, n] (query tile, kv tile).
    """
    c = cfg.tile
    n = mech.shape[-1]
    starts = torch.arange(n, device=mech.device) * c
    ends = (starts + c - 1).clamp(max=length - 1)
    dmin = starts[:, None] - ends[None, :]
    dmax = ends[:, None] - starts[None, :]
    gap = (
        torch.arange(n, device=mech.device)[:, None]
        - torch.arange(n, device=mech.device)[None, :]
    )
    periodic = gap.remainder(cfg.strided_every) == 0
    mechanism = mech[..., None]
    any_geometry = (
        (mechanism == GLOBAL)
        | ((mechanism == LOCAL) & (dmin < cfg.local_window))
        | ((mechanism == STRIDED) & ((dmin < cfg.strided_recent) | periodic))
    )
    full_geometry = (
        (mechanism == GLOBAL)
        | ((mechanism == LOCAL) & (dmax < cfg.local_window))
        | ((mechanism == STRIDED) & ((dmax < cfg.strided_recent) | periodic))
    )
    if cfg.graph_top_r:
        g_any, g_full = dmin < cfg.graph_recent, dmax < cfg.graph_recent
        if picked is not None:
            # A picked tile is a strictly earlier, complete tile: every pair in the
            # block is allowed, so it is safe for the full-block path.
            g_any, g_full = g_any | picked, g_full | picked
        any_geometry = any_geometry | ((mechanism == GRAPH) & g_any)
        full_geometry = full_geometry | ((mechanism == GRAPH) & g_full)
    any_block = (dmax >= 0) & any_geometry
    # Out-of-range positions in tail tiles are masked. Such blocks must not
    # enter the full-block path, which skips mask_mod in the CUDA kernel.
    complete_tiles = starts + c <= length
    full = (
        (dmin >= 0) & full_geometry & complete_tiles[:, None] & complete_tiles[None, :]
    )
    partial = any_block & ~full

    def encode(mask):
        counts = mask.sum(-1).to(
            dtype=torch.int32, memory_format=torch.contiguous_format
        )
        # Router permutes tile/head axes; argsort preserves that nonstandard
        # layout. CUDA Flex flattens B/H using the H stride and requires
        # contiguous metadata (the CPU eager path does not enforce this).
        indices = (
            mask.to(torch.int32)
            .argsort(dim=-1, descending=True, stable=True)
            .to(dtype=torch.int32, memory_format=torch.contiguous_format)
        )
        return counts, indices

    pnum, pidx = encode(partial)
    fnum, fidx = encode(full)
    return BlockMask.from_kv_blocks(
        pnum,
        pidx,
        fnum,
        fidx,
        BLOCK_SIZE=c,
        mask_mod=mask_mod,
        seq_lengths=(length, length),
    )


# TAMELM routed sequence attention, v2.
#
# One shared GQA projection/cache; a tiny tile-level router picks, per (tile, q-head),
# top-k attention geometries from {global, local, strided, graph}; all geometries run
# through ONE attention call (FlexAttention block mask, or an equivalent dense mask).
#
# Invariants:
#   * K/V are projected, position-encoded and cached BEFORE routing.
#   * Tile routing is causal: tile c is routed, and its graph neighbours are chosen,
#     from its first token only, so training == chunked prefill == token-by-token decode.
#   * Top-k outputs are weighted by the ORIGINAL softmax probabilities (no
#     renormalization), so top-1 still trains the router.
#   * Graph geometry: a query tile sees its recent window plus the top-r COMPLETED
#     past tiles ranked by (first-token query) . (tile-mean key). Ranking uses
#     un-rotated q/k, so it is content similarity rather than position. The tile choice
#     is hard (no gradient); the router weight on 'graph' is what trains. This is the
#     causal, tile-level repair of the learned k-NN adjacency from the graph router.
#
# Positional transform is pluggable: RotaryPos (standard RoPE), WarpedRoPE (RoPE on a
# content-defined clock; relative in warped time), or BlackHolePos (legacy adapter;
# note it depends on absolute position through per-position phase/vortex/energy).


# =============================================================================
# Routed sequence attention
# =============================================================================


class RoutedSequenceAttention(nn.Module):

    def __init__(self, cfg: RoutedAttentionConfig, pos: Optional[nn.Module] = None):
        super().__init__()
        H, Hkv, K = cfg.n_q_heads, cfg.n_kv_heads, cfg.top_k
        dh = cfg.head_dim or cfg.d_model // H
        if cfg.graph_top_r < 0:
            raise ValueError("graph_top_r must be nonnegative")
        self.M = 4 if cfg.graph_top_r else 3  # number of mechanisms
        if (
            min(
                H,
                Hkv,
                dh,
                cfg.tile,
                cfg.local_window,
                cfg.strided_recent,
                cfg.strided_every,
                cfg.graph_recent,
                cfg.max_seq_len,
            )
            <= 0
        ):
            raise ValueError("Attention dimensions/windows must be positive")
        if H % Hkv or dh % 2 or not 1 <= K <= self.M:
            raise ValueError("Invalid GQA heads, odd rotary dimension, or top_k")
        if cfg.backend not in {"flex", "dense"}:
            raise ValueError("backend must be flex or dense")
        self.cfg, self.H, self.Hkv, self.K, self.dh = cfg, H, Hkv, K, dh
        self.graph = bool(cfg.graph_top_r)
        self.n_max = -(-cfg.max_seq_len // cfg.tile)  # tiles in a full window
        self._force_flex = False  # test hook: Flex on CPU (eager)

        self.q_proj = nn.Linear(cfg.d_model, H * dh, bias=False)
        self.k_proj = nn.Linear(cfg.d_model, Hkv * dh, bias=False)
        self.v_proj = nn.Linear(cfg.d_model, Hkv * dh, bias=False)
        self.o_proj = nn.Linear(H * dh, cfg.d_model, bias=False)

        self.router = nn.Linear(cfg.d_model, H * self.M, bias=False)
        nn.init.normal_(self.router.weight, std=cfg.router_init_std)
        # [H, M] mask: this revision has no mandatory anchor heads.
        # The run contract records actual constraints; anchored variants are separate experiments.
        self.register_buffer("allowed", torch.ones(H, self.M, dtype=torch.bool))

        self.pos = pos if pos is not None else RotaryPos(dh)

    # --------------------------------------------------------------------- routing
    def _route(self, x_starts):  # [B, n, d]
        B, n, _ = x_starts.shape
        logits = router_logits_fp32(self.router, x_starts).view(B, n, self.H, self.M)
        logits = logits.masked_fill(~self.allowed, float("-inf"))
        P = logits.softmax(-1)
        w, idx = P.topk(self.K, dim=-1)  # un-renormalized weights
        return P, w, idx

    def _balance(self, P, idx):
        M = self.M
        f = F.one_hot(idx, M).float().sum(-2).reshape(-1, M).mean(0) / self.K
        Pbar = P.reshape(-1, M).mean(0)
        loss = self.cfg.balance_coef * M * (f * Pbar).sum()
        ent = -(P.clamp_min(1e-9).log() * P).sum(-1).mean()
        return loss, f.detach(), ent.detach()

    @torch.no_grad()
    def _select_tiles(self, q_first, k_raw, tiles):
        """q_first [B,H,n_new,dh]: un-rotated query of each new tile's first token.
        k_raw [B,Hkv,S,dh]: every un-rotated key known so far. tiles [n_new].
        Returns [B, n_new, H, n_max] bool: past complete tiles picked per query tile."""
        C, r = self.cfg.tile, self.cfg.graph_top_r
        B, H, n_new, dh = q_first.shape
        sel = torch.zeros(
            B, n_new, H, self.n_max, dtype=torch.bool, device=q_first.device
        )
        n_full = min(k_raw.shape[2] // C, self.n_max)
        if n_full == 0 or n_new == 0:
            return sel
        with torch.autocast(device_type=q_first.device.type, enabled=False):
            kbar = k_raw[:, :, : n_full * C].float().unflatten(2, (n_full, C)).mean(3)
            kbar = kbar.repeat_interleave(H // self.Hkv, dim=1)  # [B,H,n_full,dh]
            scores = torch.einsum("bhnd,bhtd->bnht", q_first.float(), kbar)
        # Tile t is a candidate for query tile c iff t < c (strictly earlier, hence
        # complete by the time tile c's first token exists).
        past = torch.arange(n_full, device=sel.device)[None, :] < tiles[:, None]
        scores = scores.masked_fill(~past[None, :, None, :], float("-inf"))
        vals, idx = scores.topk(min(r, n_full), dim=-1)
        part = torch.zeros(B, n_new, H, n_full, dtype=torch.bool, device=sel.device)
        part.scatter_(-1, idx, torch.isfinite(vals))
        sel[..., :n_full] = part
        return sel

    # --------------------------------------------------------------------- forward
    def forward(
        self, x, cache: Optional[Dict] = None, start_pos: int = 0, symplectic_state=None
    ):
        """x: [B, T, d]. With `cache` (a dict, mutated in place) supports chunked
        prefill and decode; `start_pos` is the absolute position of x[:, 0]."""
        cfg, H, Hkv, K, dh, C = (
            self.cfg,
            self.H,
            self.Hkv,
            self.K,
            self.dh,
            self.cfg.tile,
        )
        B, T, _ = x.shape
        if T < 1 or start_pos < 0:
            raise ValueError("Nonempty input and nonnegative start_pos required")
        if cache is not None and torch.is_grad_enabled():
            raise ValueError("KV caching is inference-only; use torch.no_grad()")
        if cache is None and start_pos != 0:
            raise ValueError("A nonzero start_pos requires the prior KV cache")
        if cache is not None and "k" not in cache and start_pos != 0:
            raise ValueError("An empty cache must begin at position zero")

        q_raw = self.q_proj(x).view(B, T, H, dh).transpose(1, 2)
        k_raw = self.k_proj(x).view(B, T, Hkv, dh).transpose(1, 2)
        v = self.v_proj(x).view(B, T, Hkv, dh).transpose(1, 2)
        if isinstance(self.pos, WarpedRoPE):
            t0 = (
                cache["clock"][:, -1]
                if (cache is not None and "clock" in cache)
                else None
            )
            times, clock = self.pos.clock(x, t0)  # [B, T, Hkv]
            if cache is not None:
                cache["clock"] = (
                    torch.cat([cache["clock"], clock], 1) if "clock" in cache else clock
                )
            times = times.transpose(1, 2)  # [B, Hkv, T]
            q = self.pos.rotate(q_raw, times.repeat_interleave(H // Hkv, dim=1))
            k = self.pos.rotate(k_raw, times)
        else:
            # Compute the positional transform once for the shared Q/K position range.
            qk = self.pos(
                torch.cat((q_raw, k_raw), dim=1),
                start_pos=start_pos,
                symplectic_state=symplectic_state,
            )
            q, k = qk.split((H, Hkv), dim=1)

        # 1) write sequence memory before any routing decision
        k_raw_all = k_raw
        if cache is not None:
            if "k" in cache:
                assert (
                    cache["k"].shape[2] == start_pos
                ), "cache length must equal start_pos"
                k = torch.cat([cache["k"], k], 2)
                v = torch.cat([cache["v"], v], 2)
                if self.graph:
                    k_raw_all = torch.cat([cache["k_raw"], k_raw], 2)
            cache["k"], cache["v"] = k, v
            if self.graph:
                cache["k_raw"] = k_raw_all

        # 2) causal tile routing: route every tile whose FIRST token lies in this chunk
        first_tile, last_tile = start_pos // C, (start_pos + T - 1) // C
        n_known = cache["w"].shape[1] if (cache is not None and "w" in cache) else 0
        new_tiles = torch.arange(n_known, last_tile + 1, device=x.device)
        offsets = new_tiles * C - start_pos
        if n_known <= last_tile and n_known * C < start_pos:
            raise ValueError("A tile first token was skipped; routes out of sync")
        aux = {}
        sel_new = None
        if len(new_tiles):
            P_new, w_new, idx_new = self._route(x[:, offsets])
            loss, f, ent = self._balance(P_new, idx_new)
            aux = {"balance_loss": loss, "mech_freq": f, "router_entropy": ent}
            with torch.no_grad():
                selected = F.one_hot(idx_new, self.M).float()
                selected_weight = torch.zeros_like(P_new).scatter_add_(
                    -1, idx_new, w_new
                )
                aux.update(
                    query_tile_indices=new_tiles.detach(),
                    mech_frequency_by_query_tile=selected.mean(dim=(0, 2, 3)),
                    mech_probability_by_query_tile=P_new.detach().mean(dim=(0, 2)),
                    mech_selected_weight_by_query_tile=selected_weight.mean(dim=(0, 2)),
                )
            if self.graph:
                sel_new = self._select_tiles(q_raw[:, :, offsets], k_raw_all, new_tiles)
        else:
            w_new = x.new_zeros(B, 0, H, K, dtype=torch.float32)
            idx_new = torch.zeros(B, 0, H, K, dtype=torch.long, device=x.device)
        if self.graph and sel_new is None:
            sel_new = torch.zeros(
                B, 0, H, self.n_max, dtype=torch.bool, device=x.device
            )
        if cache is not None and n_known:
            w_all = torch.cat([cache["w"], w_new], 1)
            idx_all = torch.cat([cache["idx"], idx_new], 1)
            sel_all = torch.cat([cache["sel"], sel_new], 1) if self.graph else None
        else:
            w_all, idx_all, sel_all = w_new, idx_new, sel_new
        if cache is not None:
            cache["w"], cache["idx"] = w_all.detach(), idx_all
            if self.graph:
                cache["sel"] = sel_all

        # slot s = h*K + j  (maps to kv head s // (H*K/Hkv) == h // (H/Hkv))
        w_t = w_all[:, first_tile : last_tile + 1]  # [B, n, H, K]
        i_t = idx_all[:, first_tile : last_tile + 1]
        n = w_t.shape[1]
        mech = i_t.permute(0, 2, 3, 1).reshape(B, H * K, n)  # [B, HK, n]
        wslot = w_t.permute(0, 2, 3, 1).reshape(B, H * K, n)
        picked_slots = None
        if self.graph:  # [B, HK, n, n_max]
            picked_slots = (
                sel_all[:, first_tile : last_tile + 1]
                .permute(0, 2, 1, 3)
                .repeat_interleave(K, dim=1)
            )

        q_slots = q.repeat_interleave(K, dim=1)  # [B, HK, T, dh]
        q_pos = torch.arange(start_pos, start_pos + T, device=x.device)
        q_tile = q_pos // C - first_tile  # [T]

        if cfg.backend == "flex" and not HAS_FLEX:
            raise RuntimeError(
                "FlexAttention unavailable; install compatible torch or explicitly select dense"
            )
        use_flex = (
            cfg.backend == "flex"
            and (x.is_cuda or self._force_flex)
            and cache is None
            and start_pos == 0
        )
        if use_flex:
            n_max = self.n_max
            if picked_slots is not None:

                def mask_mod(b, h, q_idx, kv_idx):
                    qt = (q_idx // C).clamp(max=n - 1)
                    picked = picked_slots[b, h, qt, (kv_idx // C).clamp(max=n_max - 1)]
                    return (
                        (q_idx < T)
                        & (kv_idx < T)
                        & geometry_allows(mech[b, h, qt], q_idx, kv_idx, cfg, picked)
                    )

            else:

                def mask_mod(b, h, q_idx, kv_idx):
                    return (
                        (q_idx < T)
                        & (kv_idx < T)
                        & geometry_allows(
                            mech[b, h, (q_idx // C).clamp(max=n - 1)],
                            q_idx,
                            kv_idx,
                            cfg,
                        )
                    )

            if C == 128:
                bm = geometry_block_mask(
                    mech,
                    T,
                    cfg,
                    mask_mod,
                    None if picked_slots is None else picked_slots[..., :n],
                )
            else:
                bm = create_block_mask(
                    mask_mod,
                    B,
                    H * K,
                    T,
                    T,
                    device=x.device,
                    BLOCK_SIZE=C,
                    _compile=x.is_cuda,
                )
            out = _run_flex(q_slots, k, v, bm)
        else:
            S = k.shape[2]
            kv_pos = torch.arange(S, device=x.device)
            m = mech[:, :, q_tile]  # [B, HK, T]
            picked = None
            if picked_slots is not None:
                picked = picked_slots[:, :, q_tile][..., kv_pos // C]  # [B, HK, T, S]
            allow = geometry_allows(
                m[..., None],
                q_pos[None, None, :, None],
                kv_pos[None, None, None, :],
                cfg,
                picked,
            )  # [B, HK, T, S]
            out = F.scaled_dot_product_attention(
                q_slots, k, v, attn_mask=allow, enable_gqa=True
            )

        # 3) merge: o_h = sum_j P_{h,m_j} * A_{m_j}
        out = out * wslot[:, :, q_tile, None].to(out.dtype)
        out = out.view(B, H, K, T, dh).sum(2).transpose(1, 2).reshape(B, T, H * dh)
        return self.o_proj(out), aux


# =============================================================================
# Grouped GEMM acceleration
# =============================================================================

# Ampere-compatible dropless grouped GEMM with explicit autograd.
#
# No Hopper TMA instructions, token dropping, padding capacity, or CPU count reads.
# A is [sum(tokens), K], W is [experts,K,N], offsets are cumulative token counts.
# Persistent forward/input-gradient kernels use tensor-core dot products; each
# weight-gradient tile has a unique writer and accumulates in FP32.

if HAS_TRITON:

    @triton.jit
    def _grouped_forward(
        A,
        W,
        O,
        Y,
        K: tl.constexpr,
        N: tl.constexpr,
        AS0: tl.constexpr,
        AS1: tl.constexpr,
        WS0: tl.constexpr,
        WS1: tl.constexpr,
        WS2: tl.constexpr,
        E: tl.constexpr,
        GRID: tl.constexpr,
        BM: tl.constexpr = 32,
        BN: tl.constexpr = 64,
        BK: tl.constexpr = 32,
    ):
        tile_id = tl.program_id(0)
        prior_tiles = 0
        start = 0
        for expert in range(E):
            end = tl.load(O + expert)
            count = end - start
            nt = tl.cdiv(N, BN)
            tiles = tl.cdiv(count, BM) * nt
            while tile_id >= prior_tiles and tile_id < prior_tiles + tiles:
                local = tile_id - prior_tiles
                mi = (local // nt) * BM + tl.arange(0, BM)
                ni = (local % nt) * BN + tl.arange(0, BN)
                ki = tl.arange(0, BK)
                acc = tl.zeros((BM, BN), tl.float32)
                for kk in range(tl.cdiv(K, BK)):
                    kval = kk * BK + ki
                    av = tl.load(
                        A + (start + mi[:, None]) * AS0 + kval[None, :] * AS1,
                        (mi[:, None] < count) & (kval[None, :] < K),
                        other=0,
                    )
                    wv = tl.load(
                        W + expert * WS0 + kval[:, None] * WS1 + ni[None, :] * WS2,
                        (kval[:, None] < K) & (ni[None, :] < N),
                        other=0,
                    )
                    acc += tl.dot(av, wv)
                tl.store(
                    Y + (start + mi[:, None]) * N + ni[None, :],
                    acc,
                    (mi[:, None] < count) & (ni[None, :] < N),
                )
                tile_id += GRID
            prior_tiles += tiles
            start = end

    @triton.jit
    def _grouped_weight_grad(
        A,
        DY,
        O,
        DW,
        K: tl.constexpr,
        N: tl.constexpr,
        AS0: tl.constexpr,
        AS1: tl.constexpr,
        DS0: tl.constexpr,
        DS1: tl.constexpr,
        BM: tl.constexpr = 32,
        BN: tl.constexpr = 64,
        BT: tl.constexpr = 32,
    ):
        expert = tl.program_id(0)
        ki = tl.program_id(1) * BM + tl.arange(0, BM)
        ni = tl.program_id(2) * BN + tl.arange(0, BN)
        end = tl.load(O + expert)
        start = 0
        if expert > 0:
            start = tl.load(O + expert - 1)
        ti = tl.arange(0, BT)
        acc = tl.zeros((BM, BN), tl.float32)
        for block in range(tl.cdiv(end - start, BT)):
            token = start + block * BT + ti
            av = tl.load(
                A + token[None, :] * AS0 + ki[:, None] * AS1,
                (token[None, :] < end) & (ki[:, None] < K),
                other=0,
            )
            dy = tl.load(
                DY + token[:, None] * DS0 + ni[None, :] * DS1,
                (token[:, None] < end) & (ni[None, :] < N),
                other=0,
            )
            acc += tl.dot(av, dy)
        tl.store(
            DW + expert * K * N + ki[:, None] * N + ni[None, :],
            acc,
            (ki[:, None] < K) & (ni[None, :] < N),
        )


def _forward(a, w, offsets):
    if not HAS_TRITON or not a.is_cuda:
        raise RuntimeError("Ampere grouped GEMM requires CUDA and Triton")
    if a.dtype not in (torch.bfloat16, torch.float16) or w.dtype != a.dtype:
        raise ValueError("Grouped GEMM expects matching BF16/FP16 operands")
    if (
        a.ndim != 2
        or w.ndim != 3
        or a.shape[1] != w.shape[1]
        or offsets.numel() != w.shape[0]
    ):
        raise ValueError("Grouped GEMM shape mismatch")
    out = torch.empty((a.shape[0], w.shape[2]), device=a.device, dtype=a.dtype)
    grid = torch.cuda.get_device_properties(a.device).multi_processor_count
    with torch.cuda.device(a.device):
        _grouped_forward[(grid,)](
            a,
            w,
            offsets,
            out,
            a.shape[1],
            w.shape[2],
            *a.stride(),
            *w.stride(),
            w.shape[0],
            grid,
            num_warps=4,
            num_stages=3,
        )
    return out


class _GroupedGEMM(torch.autograd.Function):
    @staticmethod
    def forward(ctx, a, w, offsets):
        ctx.save_for_backward(a, w, offsets)
        return _forward(a, w, offsets)

    @staticmethod
    def backward(ctx, grad):
        a, w, offsets = ctx.saved_tensors
        da = (
            _forward(grad, w.transpose(-1, -2), offsets)
            if ctx.needs_input_grad[0]
            else None
        )
        dw = None
        if ctx.needs_input_grad[1]:
            dw = torch.empty(w.shape, device=w.device, dtype=w.dtype)
            grid = (
                w.shape[0],
                triton.cdiv(w.shape[1], 32),
                triton.cdiv(w.shape[2], 64),
            )
            with torch.cuda.device(a.device):
                _grouped_weight_grad[grid](
                    a,
                    grad,
                    offsets,
                    dw,
                    w.shape[1],
                    w.shape[2],
                    *a.stride(),
                    *grad.stride(),
                    num_warps=4,
                    num_stages=3,
                )
        return da, dw, None


def ampere_grouped_mm(a, w, offsets):
    return _GroupedGEMM.apply(a, w, offsets)


def grouped_mm(a, b, offsets):
    return ampere_grouped_mm(a, b, offsets)


# =============================================================================
# Model building blocks
# =============================================================================


class RMSNorm(nn.Module):
    def __init__(self, dim, eps=1e-6):
        super().__init__()
        self.eps = eps
        self.weight = nn.Parameter(torch.ones(dim))

    def forward(self, x):
        return (
            x.float() * torch.rsqrt(x.float().square().mean(-1, keepdim=True) + self.eps)
        ).to(x.dtype) * self.weight


class DepthAttention(nn.Module):
    """Bank entries [B,T,H,d] preserve token index exactly; never reduce over T.

    Keep the input embedding plus the most recent layer states. Keys and values
    are projected once when written, with autograd retained through the bank.
    """

    def __init__(self, cfg):
        super().__init__()
        self.heads = cfg.depth_heads
        self.dim = cfg.depth_dim
        self.q_proj = nn.Linear(cfg.embed_dim, cfg.depth_dim, bias=False)
        self.out_proj = nn.Linear(cfg.depth_dim, cfg.embed_dim, bias=False)
        self.gate = nn.Parameter(torch.tensor(cfg.depth_gate_init))

    def forward(self, x, bank):
        b, t, _ = x.shape
        q = self.q_proj(x).view(b, t, self.heads, self.dim // self.heads)
        keys = torch.stack([kv[0] for kv in bank], dim=-2)
        vals = torch.stack([kv[1] for kv in bank], dim=-2)
        scores = (q.unsqueeze(-2).float() * keys.float()).sum(-1) / math.sqrt(
            q.shape[-1]
        )
        weights = scores.softmax(-1).to(vals.dtype)
        y = (weights.unsqueeze(-1) * vals).sum(-2).reshape(b, t, self.dim)
        return self.gate.tanh() * self.out_proj(y)


class DroplessMoE(nn.Module):
    def __init__(self, cfg):
        super().__init__()
        self.cfg = cfg
        self.router = nn.Linear(cfg.embed_dim, cfg.num_experts, bias=False)
        self.up = nn.Parameter(
            torch.empty(cfg.num_experts, cfg.embed_dim, cfg.expert_hidden_dim * 2)
        )
        self.down = nn.Parameter(
            torch.empty(cfg.num_experts, cfg.expert_hidden_dim, cfg.embed_dim)
        )
        nn.init.normal_(self.up, std=0.02)
        nn.init.normal_(self.down, std=0.02 / math.sqrt(2 * cfg.num_layers))

    def forward(self, x):
        cfg = self.cfg
        flat = x.reshape(-1, x.shape[-1])
        prob = router_logits_fp32(self.router, flat).softmax(-1)
        weights, experts = prob.topk(cfg.expert_top_k, -1)
        weights = weights / weights.sum(-1, keepdim=True)
        order = experts.flatten().argsort(stable=True)
        token_ids = order // cfg.expert_top_k
        dispatched = flat[token_ids]
        counts = torch.zeros(cfg.num_experts, device=x.device, dtype=torch.int32)
        counts.scatter_add_(
            0, experts.flatten(), torch.ones_like(experts.flatten(), dtype=torch.int32)
        )
        if cfg.expert_backend == "grouped" and x.is_cuda:
            # Explicit casts are differentiable and avoid ambiguous autocast support.
            offsets = counts.cumsum(0, dtype=torch.int32)
            h = grouped_mm(
                dispatched.to(torch.bfloat16), self.up.to(torch.bfloat16), offsets
            )
            gate, value = h.chunk(2, -1)
            h = (F.silu(gate) * value).contiguous()
            result = grouped_mm(h, self.down.to(torch.bfloat16), offsets)
        else:
            # One synchronization for all counts, instead of nonzero()+sync per expert.
            sizes = counts.cpu().tolist()
            chunks = dispatched.split(sizes, 0)
            result = torch.cat(
                [self._expert(chunk, i) for i, chunk in enumerate(chunks)], 0
            )
        # Autocast produces BF16 expert outputs while the residual may be FP32.
        # Both backends must mix in the residual dtype; index_add does not promote.
        result = result.to(flat.dtype)
        weighted = result * weights.flatten()[order, None].to(result.dtype)
        mixed = torch.zeros_like(flat).index_add(0, token_ids, weighted)
        frequency = counts.float() / experts.numel()
        balance = cfg.balance_coef * cfg.num_experts * (frequency * prob.mean(0)).sum()
        return mixed.view_as(x), balance, frequency.detach()

    def _expert(self, x, i):
        gate, value = (x @ self.up[i]).chunk(2, -1)
        return (F.silu(gate) * value) @ self.down[i]


class AttentionBlock(nn.Module):
    def __init__(self, cfg):
        super().__init__()
        self.hnorm, self.dnorm, self.mnorm = (RMSNorm(cfg.embed_dim, cfg.rms_norm_eps) for _ in range(3))
        ac = RoutedAttentionConfig(
            d_model=cfg.embed_dim,
            n_q_heads=cfg.n_q_heads,
            n_kv_heads=cfg.n_kv_heads,
            top_k=cfg.attention_top_k,
            tile=cfg.tile,
            local_window=cfg.local_window,
            strided_recent=cfg.strided_recent,
            strided_every=cfg.strided_every,
            graph_top_r=cfg.graph_top_r,
            graph_recent=cfg.graph_recent,
            max_seq_len=cfg.max_seq_len,
            router_init_std=cfg.router_init_std,
            balance_coef=cfg.balance_coef,
            backend=cfg.attention_backend,
        )
        head_dim = cfg.embed_dim // cfg.n_q_heads
        if cfg.rope_type == "warped":
            pos = WarpedRoPE(cfg.embed_dim, head_dim, cfg.n_q_heads, cfg.n_kv_heads)
        elif cfg.rope_type == "blackhole":
            pos = BlackHolePos(
                BlackHoleRoPE(head_dim, cfg.max_seq_len, discrepancy_modulation=False)
            )
        else:
            pos = None
        self.horizontal = RoutedSequenceAttention(ac, pos)
        self.vertical = DepthAttention(cfg)
        self.moe = DroplessMoE(cfg)
        self.depth_mode = cfg.depth_mode

    def forward(self, x, bank, cache=None, start_pos=0):
        h, aux = self.horizontal(self.hnorm(x), cache=cache, start_pos=start_pos)
        x = x + h
        if self.depth_mode == "attention":
            x = x + self.vertical(self.dnorm(x), bank)
        m, mloss, freq = self.moe(self.mnorm(x))
        aux["expert_frequency"] = freq
        return x + m, aux.get("balance_loss", x.new_zeros(())) + mloss, aux


# =============================================================================
# Hugging Face cache and output interfaces
# =============================================================================


class TAMECache(Cache):
    """Per-sequence KV and tile-route state with HF beam and rollback operations.

    Physical token count includes padding; each row's rotary position counts only
    real tokens. Depth attention is same-token across layers, so its bank is
    reconstructed for new tokens and needs no cross-token cache.
    """

    def __init__(self, num_layers, tile):
        super().__init__(layers=[])
        self.num_layers, self.tile = num_layers, tile
        self.rows = []
        self.mask = None

    def get_seq_length(self, layer_idx=0):
        return 0 if self.mask is None else self.mask.shape[1]

    def get_max_cache_shape(self, layer_idx=0):
        return -1

    def get_mask_sizes(self, query_length, layer_idx=0):
        return self.get_seq_length() + query_length, 0

    @property
    def is_compileable(self):
        return False

    @property
    def is_initialized(self):
        return bool(self.rows)

    @property
    def is_sliding(self):
        return [False] * self.num_layers

    @property
    def max_batch_size(self):
        return len(self.rows)

    def reset(self):
        self.rows = []
        self.mask = None

    def row_length(self, index):
        row = self.rows[index]
        return row[0]["k"].shape[2] if row and "k" in row[0] else 0

    def reorder_cache(self, beam_idx):
        indices = beam_idx.tolist()
        self.rows = [[dict(layer) for layer in self.rows[i]] for i in indices]
        if self.mask is not None:
            self.mask = self.mask.index_select(0, beam_idx.to(self.mask.device))

    def batch_select_indices(self, indices):
        self.reorder_cache(indices)

    def batch_repeat_interleave(self, repeats):
        if self.mask is not None:
            indices = torch.arange(
                len(self.rows), device=self.mask.device
            ).repeat_interleave(repeats)
            self.reorder_cache(indices)

    def crop(self, max_length):
        seen = self.get_seq_length()
        end = max(0, seen + max_length) if max_length < 0 else min(seen, max_length)
        if self.mask is None:
            return
        self.mask = self.mask[:, :end]
        for i, row in enumerate(self.rows):
            length = int(self.mask[i].sum())
            tiles = (length + self.tile - 1) // self.tile
            for layer in row:
                for key in ("k", "v", "k_raw"):  # [B, Hkv, S, dh]
                    if key in layer:
                        layer[key] = layer[key][:, :, :length]
                if "clock" in layer:  # [B, S, Hkv] warped time
                    layer["clock"] = layer["clock"][:, :length]
                for key in ("w", "idx", "sel"):  # [B, tiles, ...]
                    if key in layer:
                        layer[key] = layer[key][:, :tiles]


@dataclass
class TAMEOutput(ModelOutput):
    loss: torch.Tensor | None = None
    logits: torch.Tensor | None = None
    past_key_values: TAMECache | list | None = None
    aux_losses: dict | None = None
    attention_stats: list | None = None


# =============================================================================
# Complete language model configuration
# =============================================================================

class TAMEConfig(PretrainedConfig):
    model_type = "tamelm_two_axis"
    attribute_map = {
        "hidden_size": "embed_dim",
        "num_hidden_layers": "num_layers",
        "num_attention_heads": "n_q_heads",
        "num_key_value_heads": "n_kv_heads",
        "max_position_embeddings": "max_seq_len",
    }
    # Keys introduced after the alpha checkpoint, with the value that reproduces
    # the older architecture when a saved config predates them.
    _LEGACY_DEFAULTS = {"graph_top_r": 0, "init_scheme": "pilot"}

    def __init__(
        # Configuration instance; supplied automatically by Python.
        self,
        # Tokenizer vocabulary size; number of token IDs to embed/predict.
        vocab_size=44469,
        # Main token representation width.
        embed_dim=1024,
        # Number of stacked attention/depth/expert blocks.
        num_layers=14,
        # Available feed-forward experts per layer.
        num_experts=8,
        # Experts selected per token; their outputs are weighted together.
        expert_top_k=2,
        # Width of each expert gate/value branch before projecting back.
        expert_hidden_dim=4096,
        # Query heads; head width is embed_dim / n_q_heads (64 here).
        n_q_heads=16,
        # Shared key/value heads; eight query heads share each KV head here.
        n_kv_heads=2,
        # Attention geometries selected per query head and routing tile.
        attention_top_k=2,
        # Tokens per routing tile; first token sets routes. Also mask block size.
        tile=128,
        # Local visibility: current token plus up to 255 prior tokens here.
        local_window=256,
        # Dense recent window for strided attention, including current token.
        strided_recent=128,
        # Also expose past tiles at tile-distance multiples of this value.
        strided_every=4,
        # Content-selected completed past tiles; 0 disables graph geometry.
        graph_top_r=2,
        # Dense recent window added to graph-selected tiles.
        graph_recent=128,
        # "flex" or "dense"; cached decoding uses the dense path.
        attention_backend="flex",
        # "grouped" uses CUDA/Triton; "reference" is PyTorch. CPU uses reference.
        expert_backend="grouped",
        # Maximum real tokens per sequence, including generated continuation.
        max_seq_len=2048,
        # "attention" reads same-token earlier-layer states; "baseline" skips it.
        depth_mode="attention",
        # Depth bank capacity; here embedding entry plus recent layer entries.
        depth_slots=4,
        # Width of projected depth-attention representations.
        depth_dim=384,
        # Depth-attention heads; each has depth_dim / depth_heads dimensions.
        depth_heads=4,
        # Initial learned gate; tanh(gate) scales the depth contribution.
        depth_gate_init=0.01,
        # Auxiliary loss strength for balancing attention routes and experts.
        balance_coef=0.01,
        # "warped" learns token clock rates; alternatives: "standard", "blackhole".
        rope_type="warped",
        # Attention-router weight std under "v1"; not expert-router scale.
        router_init_std=0.01,
        # "v1" restores router/output scales; "pilot" retains older initialization.
        init_scheme="v1",
        # Token positions per vocabulary projection/CE chunk; saves memory.
        loss_chunk_size=256,
        # Tokenizer ID for padding; use attention_mask to mark padded positions.
        pad_token_id=0,
        # Tokenizer ID for beginning of sequence.
        bos_token_id=1,
        # Tokenizer ID for end of sequence; can stop generation.
        eos_token_id=2,
        # Generation cache default; direct forward() still defaults to False.
        use_cache=True,
        # RMS normalization stability epsilon; applied to every RMSNorm.
        rms_norm_eps=1e-6,
        # Additional Hugging Face configuration fields.
        **kwargs,
    ):
        kwargs.setdefault("tie_word_embeddings", True)
        super().__init__(
            pad_token_id=pad_token_id,
            bos_token_id=bos_token_id,
            eos_token_id=eos_token_id,
            **kwargs,
        )
        values = locals().copy()
        for key, value in values.items():
            if key not in {"self", "kwargs", "__class__", "values"}:
                setattr(self, key, value)
        if not math.isfinite(rms_norm_eps) or rms_norm_eps <= 0:
            raise ValueError("rms_norm_eps must be finite and positive")
        if embed_dim % n_q_heads or (embed_dim // n_q_heads) % 2:
            raise ValueError(
                "Width must divide query heads with even rotary head dimension"
            )
        if depth_dim % depth_heads or depth_mode not in {"attention", "baseline"}:
            raise ValueError("Invalid depth dimensions/mode")
        if (
            not 1 <= expert_top_k <= num_experts
            or min(num_layers, depth_slots, loss_chunk_size) < 1
        ):
            raise ValueError("Invalid expert/depth/loss dimensions")
        if expert_backend not in {"reference", "grouped"} or rope_type not in {
            "blackhole",
            "standard",
            "warped",
        }:
            raise ValueError("Unknown expert backend or position transform")
        if graph_top_r < 0 or init_scheme not in {"v1", "pilot"} or router_init_std < 0:
            raise ValueError("Invalid graph_top_r, init_scheme or router_init_std")

    @classmethod
    def from_dict(cls, config_dict, **kwargs):
        config_dict = dict(config_dict)
        for key, legacy in cls._LEGACY_DEFAULTS.items():
            config_dict.setdefault(key, legacy)
        return super().from_dict(config_dict, **kwargs)


# =============================================================================
# Complete language model
# =============================================================================


class TAMELM(PreTrainedModel, GenerationMixin):
    config_class = TAMEConfig
    base_model_prefix = "tamelm"
    _tied_weights_keys = {"lm_head.weight": "token_embedding.weight"}

    # Construction and checkpoint compatibility

    def _init_weights(self, module):
        # Initialization below is the existing TAMELM scheme. HF post_init
        # records model metadata but must not draw extra random numbers or
        # change the seeded backbone used by the pilot and matched controls.
        return

    def __init__(self, config):
        super().__init__(config)
        self.token_embedding = nn.Embedding(config.vocab_size, config.embed_dim)
        self.blocks = nn.ModuleList(
            [AttentionBlock(config) for _ in range(config.num_layers)]
        )
        self.final_norm = RMSNorm(config.embed_dim, config.rms_norm_eps)
        self.depth_norm = RMSNorm(config.embed_dim, config.rms_norm_eps)
        self.depth_kv = nn.Linear(config.embed_dim, 2 * config.depth_dim, bias=False)
        self.lm_head = nn.Linear(config.embed_dim, config.vocab_size, bias=False)
        self.lm_head.weight = self.token_embedding.weight
        self.post_init()
        nn.init.normal_(self.token_embedding.weight, std=0.02)
        for module in self.modules():
            if isinstance(module, nn.Linear) and module is not self.lm_head:
                nn.init.normal_(module.weight, std=0.02)
        # HF loads under no_init_weights(); torch.ones there may leave this
        # nonpersistent routing mask uninitialized. It is fixed config state.
        for block in self.blocks:
            block.horizontal.allowed.fill_(True)
        if config.init_scheme == "v1":
            # The generic Linear loop above overwrites module-specific schemes.
            # Restore them so the run config describes the network that trains.
            out_std = 0.02 / math.sqrt(2 * config.num_layers)
            for block in self.blocks:
                nn.init.normal_(
                    block.horizontal.router.weight, std=config.router_init_std
                )
                nn.init.normal_(block.horizontal.o_proj.weight, std=out_std)
                nn.init.normal_(block.vertical.out_proj.weight, std=out_std)
        for block in self.blocks:
            if isinstance(block.horizontal.pos, WarpedRoPE):
                block.horizontal.pos.reset_parameters()  # exactly standard RoPE at step 0
        self.tokenizer = None

    # Embedding and tokenizer interfaces

    def get_input_embeddings(self):
        return self.token_embedding

    def load_state_dict(self, state_dict, strict=True, assign=False):
        # Pilot checkpoints predate serialization of this fixed routing mask.
        # Fill only missing masks; leave every learned tensor untouched.
        missing = [
            f"blocks.{i}.horizontal.allowed"
            for i in range(len(self.blocks))
            if f"blocks.{i}.horizontal.allowed" not in state_dict
        ]
        if missing:
            state_dict = dict(state_dict)
            for key in missing:
                state_dict[key] = torch.ones_like(self.get_buffer(key))
        return super().load_state_dict(state_dict, strict=strict, assign=assign)

    def set_input_embeddings(self, value):
        self.token_embedding = value
        self.lm_head.weight = value.weight

    def get_output_embeddings(self):
        return self.lm_head

    def set_output_embeddings(self, value):
        self.lm_head = value
        self.token_embedding.weight = value.weight

    def attach_tokenizer(self, tokenizer):
        import hashlib
        import json

        if len(tokenizer) != self.config.vocab_size:
            raise ValueError("Tokenizer vocabulary does not match model")
        for name in ("pad", "bos", "eos"):
            if getattr(tokenizer, name + "_token_id") != getattr(
                self.config, name + "_token_id"
            ):
                raise ValueError(f"Tokenizer {name} ID does not match model")
        backend = json.loads(tokenizer.backend_tokenizer.to_str())
        backend.pop("truncation", None)
        backend.pop("padding", None)
        identity = {
            "backend": backend,
            "special_ids": {
                name: getattr(tokenizer, name + "_token_id")
                for name in ("pad", "bos", "eos")
            },
        }
        digest = hashlib.sha256(
            json.dumps(identity, sort_keys=True, separators=(",", ":")).encode()
        ).hexdigest()
        if getattr(self.config, "tokenizer_sha256", digest) != digest:
            raise ValueError(
                "Tokenizer identity differs from the checkpoint vocabulary/policy"
            )
        self.config.tokenizer_sha256 = digest
        self.tokenizer = tokenizer

    # Depth state and packed forward path

    def _write_depth(self, x):
        b, t, _ = x.shape
        k, v = self.depth_kv(self.depth_norm(x)).chunk(2, -1)
        shape = (
            b,
            t,
            self.config.depth_heads,
            self.config.depth_dim // self.config.depth_heads,
        )
        return k.reshape(shape), v.reshape(shape)

    def _forward_unpadded(
        self,
        input_ids,
        labels=None,
        attention_mask=None,
        use_cache=False,
        past_key_values=None,
        start_pos=0,
        return_logits=None,
        **kwargs,
    ):
        if (
            input_ids.ndim != 2
            or not 0 < input_ids.shape[1] <= self.config.max_seq_len - start_pos
        ):
            raise ValueError("Input must be [batch, tokens] within max_seq_len")
        # Packed training has no padding. Do not accept a mask and silently ignore it.
        if attention_mask is not None:
            raise ValueError(
                "Use packed unpadded input_ids; padding masks are not supported"
            )
        if (use_cache or past_key_values is not None) and (
            labels is not None or torch.is_grad_enabled()
        ):
            raise ValueError(
                "KV cache requires inference without labels under torch.no_grad()"
            )
        caches = (
            past_key_values
            if past_key_values is not None
            else ([{} for _ in self.blocks] if use_cache else None)
        )
        if caches is not None and len(caches) != len(self.blocks):
            raise ValueError("One cache per layer required")
        x = self.token_embedding(input_ids)
        bank = [self._write_depth(x)] if self.config.depth_mode == "attention" else []
        losses, stats = [], []
        for i, block in enumerate(self.blocks):
            x, loss, info = block(
                x, bank, None if caches is None else caches[i], start_pos
            )
            losses.append(loss)
            stats.append({k: v.detach() for k, v in info.items()})
            if self.config.depth_mode == "attention" and i + 1 < len(self.blocks):
                bank.append(self._write_depth(x))
                if len(bank) > self.config.depth_slots:
                    bank = (
                        [bank[0]] + bank[-(self.config.depth_slots - 1) :]
                        if self.config.depth_slots > 1
                        else bank[-1:]
                    )
        x = self.final_norm(x)
        aux = torch.stack(losses).mean()
        primary, total = None, None
        if labels is not None:
            # Checkpoint the vocabulary projection/CE chunks: no retained [B,T,V]
            # logits. Recompute only this head in backward, not the full model.
            from torch.utils.checkpoint import checkpoint

            states, targets = x[:, :-1].reshape(-1, x.shape[-1]), labels[:, 1:].reshape(
                -1
            )
            if targets.numel() == 0:
                raise ValueError("Language-model loss requires at least two tokens")
            parts = []

            def chunk_loss(h, y):
                return F.cross_entropy(
                    F.linear(h, self.lm_head.weight).float(),
                    y,
                    reduction="sum",
                    ignore_index=-100,
                )

            for offset in range(0, targets.numel(), self.config.loss_chunk_size):
                h, y = (
                    states[offset : offset + self.config.loss_chunk_size],
                    targets[offset : offset + self.config.loss_chunk_size],
                )
                parts.append(
                    checkpoint(chunk_loss, h, y, use_reentrant=False)
                    if torch.is_grad_enabled()
                    else chunk_loss(h, y)
                )
            primary = torch.stack(parts).sum() / (targets != -100).sum().clamp_min(1)
            total = primary + aux
        if return_logits is None:
            return_logits = labels is None
        logits = self.lm_head(x) if return_logits else None
        return TAMEOutput(
            loss=total,
            logits=logits,
            past_key_values=caches,
            aux_losses={
                "primary": None if primary is None else primary.detach(),
                "router_balance": aux.detach(),
            },
            attention_stats=stats,
        )

    # Generation integration

    @classmethod
    def _supports_default_dynamic_cache(cls):
        # A plain DynamicCache has no tile-routing state.
        return False

    def _prepare_cache_for_generation(
        self, generation_config, model_kwargs, *args, **kwargs
    ):
        implementation = generation_config.cache_implementation
        if implementation not in (None, "dynamic"):
            raise ValueError(
                "TAMELM supports its dynamic KV/routing cache; static, offloaded and quantized caches are unsupported"
            )
        cache = model_kwargs.get("past_key_values")
        if cache is not None and not isinstance(cache, TAMECache):
            raise TypeError("TAMELM requires TAMECache to preserve tile-routing state")

    def _validate_generated_length(
        self, generation_config, input_ids_length, has_default_max_length
    ):
        super()._validate_generated_length(
            generation_config, input_ids_length, has_default_max_length
        )
        if generation_config.max_length > self.config.max_seq_len:
            raise ValueError(
                f"Requested generation exceeds max_seq_len={self.config.max_seq_len}; shorten the prompt or max_new_tokens"
            )

    def prepare_inputs_for_generation(
        self,
        input_ids,
        past_key_values=None,
        attention_mask=None,
        use_cache=True,
        next_sequence_length=None,
        position_ids=None,
        **kwargs,
    ):
        seen = past_key_values.get_seq_length() if past_key_values is not None else 0
        if seen:
            if input_ids.shape[1] > seen:
                input_ids = input_ids[:, seen:]
            elif next_sequence_length is not None:
                input_ids = input_ids[:, -next_sequence_length:]
        if position_ids is not None:
            position_ids = position_ids[:, -input_ids.shape[1] :]
        return {
            "input_ids": input_ids.contiguous(),
            "past_key_values": past_key_values,
            "attention_mask": attention_mask,
            "position_ids": position_ids,
            "use_cache": use_cache,
            "logits_to_keep": 1,
        }

    # Public forward path with padding and cache handling

    def forward(
        self,
        input_ids,
        labels=None,
        attention_mask=None,
        use_cache=False,
        past_key_values=None,
        start_pos=None,
        return_logits=None,
        return_dict=None,
        position_ids=None,
        logits_to_keep=0,
        token_type_ids=None,
        **kwargs,
    ):
        if input_ids.ndim != 2 or input_ids.shape[1] < 1:
            raise ValueError("input_ids must be a nonempty [batch, tokens] tensor")
        if token_type_ids is not None and bool(token_type_ids.any()):
            raise ValueError("TAMELM does not use token type embeddings")
        if kwargs.get("output_attentions") or kwargs.get("output_hidden_states"):
            raise NotImplementedError(
                "Attention/hidden-state collection is not implemented"
            )
        if (use_cache or past_key_values is not None) and (
            labels is not None or torch.is_grad_enabled()
        ):
            raise ValueError(
                "KV cache requires inference without labels under torch.no_grad()"
            )
        if past_key_values is not None and not isinstance(past_key_values, TAMECache):
            raise TypeError("past_key_values must be a TAMECache")
        if past_key_values is not None and not use_cache:
            raise ValueError("Set use_cache=True when providing past_key_values")
        cache = past_key_values
        if use_cache and cache is None:
            cache = TAMECache(len(self.blocks), self.config.tile)
        seen = cache.get_seq_length() if cache is not None else 0
        if start_pos is not None and start_pos != seen:
            raise ValueError("start_pos disagrees with the cache token count")
        batch, length = input_ids.shape
        if labels is not None and labels.shape != input_ids.shape:
            raise ValueError("labels must match input_ids")
        if attention_mask is None:
            current_mask = torch.ones_like(input_ids, dtype=torch.bool)
            mask = (
                torch.cat((cache.mask, current_mask), dim=1) if seen else current_mask
            )
        else:
            if attention_mask.shape != (batch, seen + length):
                raise ValueError("attention_mask must cover cached and new tokens")
            if not bool(((attention_mask == 0) | (attention_mask == 1)).all()):
                raise ValueError("attention_mask must contain only zeros and ones")
            mask = attention_mask.to(device=input_ids.device, dtype=torch.bool)
            if seen and not torch.equal(mask[:, :seen], cache.mask):
                raise ValueError("attention_mask changes already cached tokens")
            current_mask = mask[:, seen:]
        if cache is not None:
            if not cache.rows:
                cache.rows = [[{} for _ in self.blocks] for _ in range(batch)]
            if len(cache.rows) != batch:
                raise ValueError(
                    "Cache batch size differs; reorder or expand it explicitly"
                )
        if (
            attention_mask is None
            and cache is None
            and length > self.config.max_seq_len
        ) or (
            (attention_mask is not None or cache is not None)
            and bool((mask.sum(1) > self.config.max_seq_len).any())
        ):
            raise ValueError("Real token count exceeds max_seq_len")
        if position_ids is not None:
            expected = mask.long().cumsum(-1)[:, seen:] - 1
            if position_ids.shape != input_ids.shape or not torch.equal(
                position_ids[current_mask], expected[current_mask]
            ):
                raise ValueError("position_ids must follow unpadded token positions")
        # Packed training and unpadded full forwards retain the vectorized path.
        if cache is None and (attention_mask is None or bool(current_mask.all())):
            out = self._forward_unpadded(
                input_ids, labels=labels, return_logits=return_logits
            )
        else:
            if return_logits is None:
                return_logits = labels is None
            logits = None
            weighted_loss, weighted_primary, weighted_aux, counts = [], [], [], []
            stats = []
            for i in range(batch):
                positions = current_mask[i].nonzero(as_tuple=True)[0]
                if not positions.numel():
                    raise ValueError(
                        "Each sequence must provide at least one real token"
                    )
                ids = input_ids[i : i + 1].index_select(1, positions)
                target = (
                    labels[i : i + 1].index_select(1, positions)
                    if labels is not None
                    else None
                )
                offset = cache.row_length(i) if cache is not None else 0
                native = self._forward_unpadded(
                    ids,
                    labels=target,
                    use_cache=cache is not None,
                    past_key_values=cache.rows[i] if cache is not None else None,
                    start_pos=offset,
                    return_logits=return_logits,
                )
                if native.logits is not None:
                    if logits is None:
                        logits = native.logits.new_zeros(
                            batch, length, self.config.vocab_size
                        )
                    logits[i, positions] = native.logits[0]
                if labels is not None:
                    count = (target[:, 1:] != -100).sum()
                    counts.append(count)
                    weighted_loss.append(native.loss * count)
                    weighted_primary.append(native.aux_losses["primary"] * count)
                    weighted_aux.append(native.aux_losses["router_balance"] * count)
                stats = native.attention_stats
            if cache is not None:
                cache.mask = mask.detach().clone()
            denom = torch.stack(counts).sum().clamp_min(1) if counts else None
            out = TAMEOutput(
                loss=torch.stack(weighted_loss).sum() / denom if counts else None,
                logits=logits,
                past_key_values=cache,
                aux_losses={
                    "primary": (
                        torch.stack(weighted_primary).sum() / denom if counts else None
                    ),
                    "router_balance": (
                        torch.stack(weighted_aux).sum() / denom
                        if counts
                        else input_ids.new_zeros((), dtype=torch.float32)
                    ),
                },
                attention_stats=None,
            )  # per-layer routing summaries are defined for packed batches
        if logits_to_keep and out.logits is not None:
            out.logits = out.logits[:, -logits_to_keep:]
        return out if return_dict is not False else out.to_tuple()

    # Portable Hugging Face export

    def export_hf_code(self, directory):
        from pathlib import Path
        import inspect
        import sys

        directory = Path(directory)
        directory.mkdir(parents=True, exist_ok=True)
        source = _NOTEBOOK_SOURCES.get("model")
        if source is None:
            source = inspect.getsource(sys.modules[type(self).__module__])
        # Registration is notebook-local; portable loading uses auto_map.
        source = source.split("\nAutoConfig.register(TAMEConfig.model_type")[0]
        source = source.split("\n# Notebook-only source capture")[0]
        (directory / "modeling_tamelm.py").write_text(source, encoding="utf-8")
        self.config.auto_map = {
            "AutoConfig": "modeling_tamelm.TAMEConfig",
            "AutoModelForCausalLM": "modeling_tamelm.TAMELM",
        }
        self.config.architectures = ["TAMELM"]
        self.config.save_pretrained(directory)
        self.generation_config.save_pretrained(directory)

    def save_pretrained(self, save_directory, *args, **kwargs):
        self.export_hf_code(save_directory)
        result = super().save_pretrained(save_directory, *args, **kwargs)
        if self.tokenizer is not None:
            self.tokenizer.save_pretrained(save_directory)
        return result