File size: 117,481 Bytes
6e802a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
# train.py: Training for every stage (DDP, losses, optimiser, schedule, checkpoints) and the checkpoint loader
# the inference code shares; it reads training .pt files and released .safetensors alike.

import os
import sys
import json
import math
import random
import inspect
import logging
import argparse
from typing import Optional, List, Dict, Any
from dataclasses import dataclass

if "dill" not in sys.modules:
    import types as _types
    import importlib.machinery as _im
    _dill_shim = _types.ModuleType("dill")
    _dill_shim.extend = lambda *a, **k: None
    _dill_shim.__version__ = "0.0.0-shim"
    _dill_shim.__spec__ = _im.ModuleSpec("dill", loader=None)
    sys.modules["dill"] = _dill_shim

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributed as dist
from torch.utils.data import DataLoader, DistributedSampler, Dataset
from torch.nn.parallel import DistributedDataParallel as DDP

import config
from model import MambaCoTModel
from dataset import MambaCoTTTSDataset, MambaCoTDataCollator
from tokenizer import TokenRegistry

logger = logging.getLogger(__name__)

STAGE_DEFAULTS = {
    1: {
        "max_steps": 50_000,
        "lr": 3e-4,
        "lr_lora": 1e-4,
        "lr_min": 1e-5,
        "warmup_steps": 1_000,
        "batch_size": 4,
        "grad_accum": 4,
        "use_weighted_loss": False,
        "dataset_stage": 1,
    },
    2: {
        "max_steps": 100_000,
        "lr": 5e-5,
        "lr_lora": None,
        "lr_min": 1e-6,
        "warmup_steps": 2_000,
        "batch_size": 4,
        "grad_accum": 4,
        "use_weighted_loss": True,
        "dataset_stage": 2,
    },
    3: {
        "max_steps": 50_000,
        "lr": 1e-5,
        "lr_lora": None,
        "lr_min": 1e-6,
        "warmup_steps": 500,
        "batch_size": 4,
        "grad_accum": 4,
        "use_weighted_loss": True,
        "dataset_stage": 2,
    },
    4: {
        "max_steps": 10_000,
        "lr": 5e-6,
        "lr_lora": None,
        "lr_min": 1e-7,
        "warmup_steps": 200,
        "batch_size": 2,
        "grad_accum": 4,
        "use_weighted_loss": False,
        "dataset_stage": 2,
    },
    5: {
        "max_steps": 5_000,
        "lr": 1e-4,
        "lr_lora": None,
        "lr_min": 1e-6,
        "warmup_steps": 100,
        "batch_size": 4,
        "grad_accum": 4,
        "use_weighted_loss": False,
        "dataset_stage": 2,
    },
}


class LoRALinear(nn.Module):
    def __init__(self, original: nn.Linear, rank: int = 64,
                 alpha: int = 64, dropout: float = 0.05):
        super().__init__()
        self.original = original
        self.rank = rank
        self.alpha = alpha
        self.scaling = alpha / rank

        in_features = original.in_features
        out_features = original.out_features

        original.weight.requires_grad = False
        if original.bias is not None:
            original.bias.requires_grad = False

        self.lora_A = nn.Parameter(
            torch.randn(rank, in_features, device=original.weight.device,
                        dtype=original.weight.dtype) * 0.01
        )
        self.lora_B = nn.Parameter(
            torch.zeros(out_features, rank, device=original.weight.device,
                        dtype=original.weight.dtype)
        )
        self.lora_dropout = nn.Dropout(dropout) if dropout > 0 else nn.Identity()

    def __getattr__(self, name):
        try:
            return super().__getattr__(name)
        except AttributeError:
            return getattr(self.original, name)

    def forward(self, x):
        result = self.original(x)
        lora_out = F.linear(self.lora_dropout(x), self.lora_A)
        lora_out = F.linear(lora_out, self.lora_B)
        return result + lora_out * self.scaling

    @property
    def weight(self):
        return self.original.weight + (self.lora_B @ self.lora_A) * self.scaling

    def extra_repr(self):
        return (f"in={self.original.in_features}, out={self.original.out_features}, "
                f"rank={self.rank}, alpha={self.alpha}")


def apply_lora(model: MambaCoTModel, rank: int = 64, alpha: int = 64,
               target_modules: List[str] = None, dropout: float = 0.05):
    if target_modules is None:
        target_modules = ["in_proj", "out_proj"]

    for param in model.backbone.parameters():
        param.requires_grad = False

    model.backbone.backbone.embedding.weight.requires_grad = True
    model.backbone.lm_head.weight.requires_grad = True

    lora_count = 0
    for name, module in model.backbone.named_modules():
        for target in target_modules:
            if name.endswith(target) and isinstance(module, nn.Linear):
                parts = name.rsplit(".", 1)
                if len(parts) == 2:
                    parent_name, attr_name = parts
                    parent = dict(model.backbone.named_modules())[parent_name]
                else:
                    parent = model.backbone
                    attr_name = parts[0]

                lora_module = LoRALinear(module, rank=rank, alpha=alpha, dropout=dropout)
                setattr(parent, attr_name, lora_module)
                lora_count += 1
                logger.info(f"Applied LoRA to {name} (rank={rank})")
                break

    for param in model.mtp_module.parameters():
        param.requires_grad = True

    logger.info(f"Applied {lora_count} LoRA adapters. "
                f"Backbone frozen, embeddings/head/MTP trainable.")

    return lora_count


def count_parameters(model: nn.Module):
    total = sum(p.numel() for p in model.parameters())
    trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
    return total, trainable

PLAN_TIERS = ("none", "plan", "tags+plan", "full")

PLAN_COT_MODES = ("plan", "tags+plan", "full")
LEGACY_COT_MODES = ("prose", "tags", "none")

PLAN_DATASET_DEFAULTS = {
    "plan_weight": 1.0,
    "tag_weight": None,
    "plan_schedule_from": "gt",
    "min_plan_coverage": 0.0,
    "style_tier": "none",
    "style_dropout": 0.0,
}

PLAN_BIN_ORDER = ("pitch", "duration", "energy")
PLAN_BIN_ABSENT = -1

PLAN_DRY_STEPS = 200


def _is_plan_param(name: str) -> bool:
    return any(part.startswith("plan_") for part in name.split("."))


def _plan_kwargs_for(fn, kwargs: Dict[str, Any], what: str,
                     defaults: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
    params = inspect.signature(fn).parameters
    if any(p.kind is inspect.Parameter.VAR_KEYWORD for p in params.values()):
        return dict(kwargs)
    accepted = {k: v for k, v in kwargs.items() if k in params}
    lost = [k for k in kwargs
            if k not in params
            and (defaults is None or kwargs[k] != defaults.get(k))]
    if lost:
        raise RuntimeError(
            f"{what} does not accept {lost} — those P²-CoT settings were "
            f"requested on the command line and would be SILENTLY IGNORED. "
            f"Its signature is ({', '.join(params)}). Either the flag names or "
            f"the {what} signature drifted; fix the seam, do not drop the flag."
        )
    return accepted


def plan_dataset_kwargs(args) -> Dict[str, Any]:
    tier = getattr(args, "plan_tier", "none")
    style = getattr(args, "style_tier", "none")
    if tier == "none" and style == "none":
        return {}
    tagw = getattr(args, "plan_tag_weight", None)
    kw: Dict[str, Any] = {
        "plan_weight": float(getattr(args, "plan_loss_weight", 1.0)),
        "tag_weight": None if tagw is None else float(tagw),
        "plan_schedule_from": getattr(args, "plan_schedule_from", "gt"),
        "min_plan_coverage": float(getattr(args, "min_plan_coverage", 0.0)),
    }
    if style != "none":
        kw["style_tier"] = style
        kw["style_dropout"] = float(getattr(args, "style_dropout", 0.0))
    return kw


def plan_bins_from_slots(input_ids, plan_slots, registry):
    families = (registry.pitch_ids, registry.duration_ids, registry.energy_ids)
    bases = []
    for name, ids in zip(PLAN_BIN_ORDER, families):
        ids = list(ids or [])
        if not ids:
            raise RuntimeError(
                f"plan_bins_from_slots: the tokenizer has no {name} bin ids "
                f"(MVC_ENABLE_PLAN_TOKENS off for this process?) — the batch "
                f"carries a plan the vocabulary cannot express.")
        if ids != list(range(ids[0], ids[0] + len(ids))):
            raise RuntimeError(
                f"plan_bins_from_slots: {name} ids are not contiguous ({ids[:4]}"
                f"...), so id-minus-base would mis-decode every bin.")
        bases.append((ids[0], len(ids)))

    slots = plan_slots.to(input_ids.device)
    if slots.dim() != 3 or slots.shape[-1] != len(PLAN_BIN_ORDER):
        raise ValueError(f"plan_slots must be [B, W, {len(PLAN_BIN_ORDER)}], "
                         f"got {tuple(slots.shape)}")
    B, W, C = slots.shape
    valid = slots >= 0
    tok = input_ids.gather(1, slots.clamp(min=0).reshape(B, W * C)).reshape(B, W, C)

    bins = torch.full_like(slots, PLAN_BIN_ABSENT)
    for c, (lo, n) in enumerate(bases):
        b = tok[..., c] - lo
        bad = valid[..., c] & ((b < 0) | (b >= n))
        if bool(bad.any()):
            i, w = (bad.nonzero()[0]).tolist()
            raise RuntimeError(
                f"plan_slots[{i}][{w}][{c}] points at position "
                f"{int(slots[i, w, c])} holding token id {int(tok[i, w, c])}, "
                f"which is not a {PLAN_BIN_ORDER[c]} bin token "
                f"(ids {lo}..{lo + n - 1}). The collator's plan layout and the "
                f"tokenizer's plan block disagree.")
        bins[..., c] = torch.where(valid[..., c], b,
                                   torch.full_like(b, PLAN_BIN_ABSENT))
    return bins


def plan_forward_params(model) -> set:
    m = model.module if isinstance(model, DDP) else model
    return {p for p in inspect.signature(m.forward).parameters
            if p.startswith("plan_")}


def plan_forward_kwargs(batch, accepted: set, registry, device):
    plan_keys = {k: v for k, v in batch.items()
                 if k.startswith("plan_") and v is not None}
    if not plan_keys:
        return {}, []

    consumed = set()
    if ("plan_bins" in accepted and "plan_bins" not in plan_keys
            and "plan_slots" in plan_keys):
        plan_keys["plan_bins"] = plan_bins_from_slots(
            batch["input_ids"].to(device), plan_keys["plan_slots"], registry)
        consumed.add("plan_slots")
    if "plan_bins" in accepted and "plan_bins" not in plan_keys:
        raise RuntimeError(
            f"the model's forward expects plan_bins but this batch carries "
            f"neither plan_bins nor plan_slots (keys: {sorted(plan_keys)}) — "
            f"the injection would receive nothing and the plan tier would be "
            f"silently inert.")

    out = {k: (v.to(device) if torch.is_tensor(v) else v)
           for k, v in plan_keys.items() if k in accepted}
    unused = sorted(k for k in plan_keys
                    if k not in accepted and k not in consumed)
    return out, unused


def absent_plan_kwargs(input_ids, accepted: set, device) -> Dict[str, Any]:
    if "plan_bins" not in accepted or "plan_schedule" not in accepted:
        return {}
    B, S = input_ids.shape
    return {
        "plan_bins": torch.full((B, 1, len(PLAN_BIN_ORDER)), PLAN_BIN_ABSENT,
                                dtype=torch.long, device=device),
        "plan_schedule": torch.full((B, S), PLAN_BIN_ABSENT, dtype=torch.long,
                                    device=device),
    }


def plan_corpus_stats(dataset, registry, sample: int = 512,
                      seed: int = 20260817, max_seq_len: int = 0) -> dict:
    n = len(dataset)
    if n == 0:
        raise RuntimeError("plan_corpus_stats: the dataset is empty")
    k = min(sample, n)
    idx = (list(range(n)) if k == n
           else random.Random(seed).sample(range(n), k))

    n_with, plan_lens, max_len, n_over = 0, [], 0, 0
    speech_fracs = []
    with torch.random.fork_rng(devices=[]):
        for i in idx:
            item = dataset[i]
            ids = item["input_ids"]
            ids = ids.tolist() if hasattr(ids, "tolist") else list(ids)
            n_plan = sum(1 for t in ids if registry.is_plan_token(int(t)))
            if n_plan:
                n_with += 1
                plan_lens.append(n_plan)
            max_len = max(max_len, len(ids))
            if max_seq_len and len(ids) > max_seq_len:
                n_over += 1
            durs = item.get("plan_durations")
            if durs is not None:
                sigma = float(sum(durs.tolist() if hasattr(durs, "tolist")
                                  else durs))
                frames = int(item["audio_end"]) - int(item["audio_start"])
                if sigma > 0 and frames > 0:
                    speech_fracs.append(sigma / frames)
    speech_fracs.sort()
    return {
        "n_rows": n,
        "n_sampled": k,
        "n_with_plan": n_with,
        "n_without_plan": k - n_with,
        "frac_with_plan": n_with / k,
        "mean_plan_tokens": (sum(plan_lens) / len(plan_lens)) if plan_lens else 0.0,
        "max_plan_tokens": max(plan_lens) if plan_lens else 0,
        "max_seq_len_seen": max_len,
        "n_over_window": n_over,
        "speech_frac_median": (speech_fracs[len(speech_fracs) // 2]
                               if speech_fracs else None),
        "n_speech_frac": len(speech_fracs),
    }


def log_plan_stats(dataset, registry, args, sample: int = 512, rank: int = 0,
                   is_ddp: bool = False) -> dict:
    stats = plan_corpus_stats(dataset, registry, sample=sample,
                              max_seq_len=getattr(args, "max_seq_len", 0))
    if rank == 0:
        logger.info(
            "PLAN TIER: --plan_tier %s | injection=%s | schedule_from=%s | "
            "loss_weight=%s | scaffold_dropout=%s",
            getattr(args, "plan_tier", "none"),
            bool(getattr(args, "plan_injection", False)),
            getattr(args, "plan_schedule_from", "gt"),
            getattr(args, "plan_loss_weight", 1.0),
            getattr(args, "plan_scaffold_dropout", 0.0))
        logger.info(
            "PLAN TIER: %d/%d sampled rows carry a plan (%.1f%% of %d rows); "
            "mean %.1f plan tokens/row (max %d)",
            stats["n_with_plan"], stats["n_sampled"],
            100.0 * stats["frac_with_plan"], stats["n_rows"],
            stats["mean_plan_tokens"], stats["max_plan_tokens"])
        logger.info(
            "PLAN TIER: longest assembled sequence %d tokens "
            "(--max_seq_len %d); %d sampled row(s) over the window",
            stats["max_seq_len_seen"], getattr(args, "max_seq_len", 0),
            stats["n_over_window"])
        if stats["speech_frac_median"] is not None:
            logger.info(
                "PLAN TIER: rho = SumD/T median %.3f over %d plan row(s) — pass "
                "`--plan_speech_ratio %.3f` to scripts/gen_fish_cot_samples.py "
                "when decoding with --plan_schedule stretch, or the program is "
                "compressed into the first %.0f%% of the utterance and the last "
                "word is held for the rest (a schedule the model never saw).",
                stats["speech_frac_median"], stats["n_speech_frac"],
                stats["speech_frac_median"], 100.0 * stats["speech_frac_median"])

    if stats["n_with_plan"] == 0:
        raise RuntimeError(
            f"--plan_tier {getattr(args, 'plan_tier', 'none')} but NONE of "
            f"{stats['n_sampled']} sampled rows contains a single plan token. "
            f"This run would train a plan arm on a plan-free corpus and report "
            f"it as one. Check that the corpus has plan sidecars, that "
            f"MVC_ENABLE_PLAN_TOKENS=1 was exported for THIS process, and that "
            f"the dataset accepted plan_tier.")
    if stats["frac_with_plan"] < 0.5:
        logger.warning(
            "PLAN TIER: only %.1f%% of sampled rows carry a plan — the arm is "
            "mostly plan-free and its contrast with the control is diluted "
            "accordingly. Intended only if the corpus is deliberately mixed.",
            100.0 * stats["frac_with_plan"])
    if (is_ddp and getattr(args, "plan_injection", False)
            and stats["frac_with_plan"] < 1.0):
        raise RuntimeError(
            f"--plan_injection under DDP needs EVERY batch to carry the plan "
            f"channel, but only {100.0 * stats['frac_with_plan']:.1f}% of "
            f"sampled rows have a plan. A plan-free batch drops the injector "
            f"out of the graph and DDP kills all ranks ('parameters that were "
            f"not used in producing loss'). Use a fully-planned corpus, or "
            f"make the collator emit an all-absent plan for every batch.")
    if stats["n_over_window"]:
        logger.warning(
            "PLAN TIER: %d/%d sampled rows exceed --max_seq_len %d — the plan "
            "block pushes them past the window and their audio is TRUNCATED "
            "(master-plan risk R2). Shorten Tier-A text or raise the window.",
            stats["n_over_window"], stats["n_sampled"],
            getattr(args, "max_seq_len", 0))
    return stats


class DPODataset(Dataset):
    def __init__(self, data_path: str, tokenizer, registry: TokenRegistry,
                 max_seq_len: int = 2048):
        self.tokenizer = tokenizer
        self.registry = registry
        self.max_seq_len = max_seq_len

        self.data = []
        with open(data_path, "r") as f:
            for line in f:
                line = line.strip()
                if line:
                    self.data.append(json.loads(line))

        self.bos_id = registry.bos_id
        self.eos_id = registry.eos_id
        self.user_prompt_id = registry.user_prompt_id
        self.think_start_id = registry.think_start_id
        self.think_end_id = registry.think_end_id
        self.audio_start_id = registry.audio_start_id
        self.audio_end_id = registry.audio_end_id

        logger.info(f"Loaded {len(self.data)} DPO preference pairs from {data_path}")

    def __len__(self):
        return len(self.data)

    def _build_sequence(self, item: dict, speech_tokens_key: str):
        prompt_text = item["prompt"]
        speech_values = item[speech_tokens_key]

        speech_ids = [self.registry.speech_value_to_id(v) for v in speech_values]

        prompt_ids = self.tokenizer.encode(prompt_text, add_special_tokens=False)

        reasoning_ids = []
        reasoning_text = item.get("reasoning", "")
        if reasoning_text:
            clean = reasoning_text.strip()
            if clean.startswith("<THINK>"):
                clean = clean[len("<THINK>"):].strip()
            if clean.endswith("</THINK>"):
                clean = clean[:-len("</THINK>")].strip()
            if clean:
                reasoning_ids = self.tokenizer.encode(clean, add_special_tokens=False)

        if reasoning_ids:
            sequence = (
                [self.bos_id, self.user_prompt_id]
                + prompt_ids
                + [self.think_start_id]
                + reasoning_ids
                + [self.think_end_id]
                + [self.audio_start_id]
                + speech_ids
                + [self.audio_end_id, self.eos_id]
            )
            prompt_end = 2 + len(prompt_ids)
        else:
            sequence = (
                [self.bos_id, self.user_prompt_id]
                + prompt_ids
                + [self.audio_start_id]
                + speech_ids
                + [self.audio_end_id, self.eos_id]
            )
            prompt_end = 2 + len(prompt_ids)

        if len(sequence) > self.max_seq_len:
            sequence = sequence[:self.max_seq_len]

        input_ids = torch.tensor(sequence, dtype=torch.long)

        labels = input_ids.clone()
        labels[:prompt_end] = -100

        return input_ids, labels

    def __getitem__(self, idx):
        item = self.data[idx]
        chosen_ids, chosen_labels = self._build_sequence(item, "chosen_speech_tokens")
        rejected_ids, rejected_labels = self._build_sequence(item, "rejected_speech_tokens")

        return {
            "input_ids_w": chosen_ids,
            "labels_w": chosen_labels,
            "input_ids_l": rejected_ids,
            "labels_l": rejected_labels,
        }


@dataclass
class DPODataCollator:
    pad_token_id: int

    def __call__(self, batch: List[Dict[str, Any]]) -> Dict[str, torch.Tensor]:
        result = {}
        for key in ["input_ids_w", "labels_w", "input_ids_l", "labels_l"]:
            tensors = [item[key] for item in batch]
            pad_val = self.pad_token_id if "input" in key else -100
            padded = torch.nn.utils.rnn.pad_sequence(
                tensors, batch_first=True, padding_value=pad_val
            )
            result[key] = padded
        return result


def get_sequence_logprobs(model: MambaCoTModel, input_ids: torch.Tensor,
                          labels: torch.Tensor) -> torch.Tensor:
    outputs = model(input_ids=input_ids)
    logits = outputs["logits"]

    shift_logits = logits[..., :-1, :].contiguous()
    shift_labels = labels[..., 1:].contiguous()

    log_probs = F.log_softmax(shift_logits, dim=-1)
    gather_labels = shift_labels.clone()
    gather_labels[gather_labels == -100] = 0
    per_token = log_probs.gather(-1, gather_labels.unsqueeze(-1)).squeeze(-1)

    mask = (shift_labels != -100).float()
    return (per_token * mask).sum(dim=-1) / mask.sum(dim=-1).clamp(min=1)


def compute_dpo_loss(model: MambaCoTModel, ref_model: MambaCoTModel,
                     batch: Dict[str, torch.Tensor],
                     beta: float = 0.5, alpha: float = 0.01,
                     kl_ceiling: float = 5.0) -> tuple:
    logprobs_w = get_sequence_logprobs(model, batch["input_ids_w"], batch["labels_w"])
    logprobs_l = get_sequence_logprobs(model, batch["input_ids_l"], batch["labels_l"])

    with torch.no_grad():
        ref_logprobs_w = get_sequence_logprobs(ref_model, batch["input_ids_w"], batch["labels_w"])
        ref_logprobs_l = get_sequence_logprobs(ref_model, batch["input_ids_l"], batch["labels_l"])

    log_ratio_w = logprobs_w - ref_logprobs_w
    log_ratio_l = logprobs_l - ref_logprobs_l
    dpo_loss = -F.logsigmoid(beta * (log_ratio_w - log_ratio_l)).mean()

    entropy_reg = alpha * (logprobs_w.mean() + logprobs_l.mean()) / 2.0

    kl_w = (logprobs_w - ref_logprobs_w).mean()
    kl_l = (logprobs_l - ref_logprobs_l).mean()
    kl_mean = (kl_w + kl_l) / 2.0
    kl_penalty = F.relu(kl_mean - kl_ceiling)

    total_loss = dpo_loss + entropy_reg + kl_penalty

    metrics = {
        "dpo_loss": dpo_loss.item(),
        "entropy_reg": entropy_reg.item(),
        "kl_mean": kl_mean.item(),
        "kl_penalty": kl_penalty.item(),
        "logprobs_w": logprobs_w.mean().item(),
        "logprobs_l": logprobs_l.mean().item(),
    }

    return total_loss, metrics


def _cot_embedding_rows(data_path, model, args) -> set:
    reg = model.token_registry
    tok = model.tokenizer
    mode = getattr(args, "cot_mode", None)
    if mode != "tags":
        raise RuntimeError(
            f"--train_cot_embeddings_only expects --cot_mode tags (got "
            f"{mode!r}): prose reasoning would whitelist most of the "
            f"vocabulary, which is not the frozen-backbone floor this arm is "
            f"meant to measure.")
    keep = {reg.think_start_id, reg.think_end_id}
    plan_structural = set(getattr(reg, "plan_token_ids", ()) or ())
    keep |= plan_structural
    prompt_tokens = set()
    seen, n_cot = set(), 0
    with open(data_path, encoding="utf-8") as fh:
        for line in fh:
            if not line.strip():
                continue
            try:
                d = json.loads(line)
            except Exception:
                continue
            p = str(d.get("prompt") or "")
            if p:
                prompt_tokens.update(tok.encode(p, add_special_tokens=False))
            if not str(d.get("reasoning") or "").strip():
                continue
            n_cot += 1
            txt = (f"EMO={d.get('emotion_label') or 'neutral'} "
                   f"PACE={d.get('pace') or 'normal'} "
                   f"PITCH={d.get('pitch') or 'normal'}")
            if txt not in seen:
                seen.add(txt)
                keep.update(tok.encode(txt, add_special_tokens=False))

    for extra in (getattr(args, "f1_exclude_prompts_from", None) or []):
        if not os.path.exists(extra):
            continue
        with open(extra, encoding="utf-8") as fh:
            for line in fh:
                if not line.strip():
                    continue
                try:
                    d = json.loads(line)
                except Exception:
                    continue
                p = str(d.get("prompt") or "")
                if p:
                    prompt_tokens.update(
                        tok.encode(p, add_special_tokens=False))

    structural = {reg.think_start_id, reg.think_end_id} | plan_structural
    dropped = (keep - structural) & prompt_tokens
    keep = (keep - prompt_tokens) | structural
    if len(keep) <= len(structural) or not n_cot:
        raise RuntimeError(
            f"no trainable reasoning tokens in {data_path} ({n_cot} CoT rows, "
            f"{len(dropped)} tag tokens dropped for also appearing in prompts) "
            f"— this arm would train nothing beyond the structural tokens.")
    logger.info(
        f"F1 whitelist: {len(keep)} rows ({len(dropped)} tag tokens dropped "
        f"because they also occur in prompt text, which is what keeps the "
        f"base-domain forward bitwise identical)")
    return keep


def build_optimizer(model: MambaCoTModel, args) -> torch.optim.Optimizer:
    if args.stage == 1 and getattr(args, "full_finetune", False):
        hybrid_prefixes = tuple(
            f"backbone.backbone.layers.{i}.mixer"
            for i in getattr(model, "hybrid_replaced_layers", []))

        def _is_fresh(name: str) -> bool:
            return (
                name.startswith("backbone.backbone.embedding")
                or name.startswith("backbone.lm_head")
                or name.startswith("mtp_module")
                or name.startswith("depth_module")
                or name.startswith("flow_head")
                or _is_plan_param(name)
                or (bool(hybrid_prefixes)
                    and name.startswith(hybrid_prefixes))
            )

        backbone_params = []
        fresh_params = []
        for name, param in model.named_parameters():
            if not param.requires_grad:
                continue
            if _is_fresh(name):
                fresh_params.append(param)
            else:
                backbone_params.append(param)

        param_groups = [
            {"params": backbone_params, "lr": args.lr_backbone},
            {"params": fresh_params, "lr": args.lr_new_params},
        ]
        logger.info(
            f"Stage 1 full-FT optimizer: {len(backbone_params)} backbone params "
            f"@ {args.lr_backbone}, {len(fresh_params)} fresh "
            f"(embed/lm_head/MTP) params @ {args.lr_new_params}"
        )
    elif args.stage == 1:
        lora_params = []
        new_params = []
        for name, param in model.named_parameters():
            if not param.requires_grad:
                continue
            if "lora_A" in name or "lora_B" in name:
                lora_params.append(param)
            else:
                new_params.append(param)

        param_groups = [
            {"params": new_params, "lr": args.lr_new_params},
            {"params": lora_params, "lr": args.lr_lora},
        ]
        logger.info(f"Stage 1 optimizer: {len(new_params)} new params @ {args.lr_new_params}, "
                     f"{len(lora_params)} LoRA params @ {args.lr_lora}")
    else:
        lr = args.lr or STAGE_DEFAULTS[args.stage]["lr"]
        spk, rest = [], []
        for name, param in model.named_parameters():
            if not param.requires_grad:
                continue
            (spk if "speaker_encoder" in name else rest).append(param)
        lr_speaker = getattr(args, "lr_speaker", 0.0)
        if lr_speaker and spk:
            param_groups = [{"params": rest, "lr": lr},
                            {"params": spk, "lr": lr_speaker}]
            logger.info(f"Stage {args.stage} optimizer: {len(rest)} params @ {lr}, "
                        f"{len(spk)} SPEAKER params @ {lr_speaker}")
        else:
            trainable = rest + spk
            param_groups = [{"params": trainable, "lr": lr}]
            logger.info(f"Stage {args.stage} optimizer: {len(trainable)} params @ {lr}")

    optimizer = torch.optim.AdamW(
        param_groups, betas=(0.9, 0.95),
        weight_decay=getattr(args, "weight_decay", 0.1),
        eps=getattr(args, "adam_eps", 1e-8),
    )
    return optimizer


def build_scheduler(optimizer: torch.optim.Optimizer, args):
    lr_min_ratio = args.lr_min / max(args.lr or STAGE_DEFAULTS[args.stage]["lr"], 1e-10)
    grad_accum = max(1, getattr(args, "grad_accum", 1) or 1)
    total_updates = max(1, args.max_steps // grad_accum)
    warmup_updates = max(1, args.warmup_steps // grad_accum)

    def lr_lambda(update):
        if update < warmup_updates:
            return update / max(1, warmup_updates)
        progress = (update - warmup_updates) / max(1, total_updates - warmup_updates)
        return max(lr_min_ratio, 0.5 * (1.0 + math.cos(math.pi * progress)))

    return torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda)


def next_batch(data_iter, loader):
    try:
        return next(data_iter), data_iter
    except StopIteration:
        data_iter = iter(loader)
        return next(data_iter), data_iter


def train_loop(model, loader, optimizer, scheduler, device, args, rank, start_step=0):
    model.train()
    use_weighted_loss = (
        STAGE_DEFAULTS[args.stage]["use_weighted_loss"]
        or getattr(args, "semantic_weight", 1.0) != 1.0
        or getattr(args, "eos_weight", 1.0) != 1.0
    )

    if start_step > 0 and isinstance(getattr(loader, "sampler", None), DistributedSampler):
        loader.sampler.set_epoch(max(1, start_step // max(1, len(loader))))
    data_iter = iter(loader)

    optimizer.zero_grad()
    running_loss = 0.0
    running_main = 0.0
    running_mtp = 0.0
    running_flow = 0.0
    flow_codec = getattr(args, "_flow_codec", None)

    plan_accepted = plan_forward_params(model)
    plan_registry = getattr(model.module if isinstance(model, DDP) else model,
                            "token_registry", None)
    plan_seen = False

    for step in range(start_step + 1, args.max_steps + 1):
        batch, data_iter = next_batch(data_iter, loader)

        input_ids = batch["input_ids"].to(device)
        labels = batch["labels"].to(device)
        speech_mask = (batch["speech_mask"].to(device)
                       if "speech_mask" in batch else None)
        loss_weights = (batch["loss_weights"].to(device)
                        if use_weighted_loss and "loss_weights" in batch else None)
        residual_codes = batch.get("residual_codes")
        if residual_codes is not None:
            residual_codes = residual_codes.to(device)
        speaker_ids = batch.get("speaker_ids")
        if speaker_ids is not None:
            speaker_ids = speaker_ids.to(device)
            mx = int(speaker_ids.max().item())
            if mx >= args.num_speakers:
                raise RuntimeError(
                    f"speaker_id {mx} >= --num_speakers {args.num_speakers}: "
                    f"the corpus carries ids outside the embedding table — "
                    f"re-map speakers or expand the table "
                    f"(scripts/expand_speaker_table.py)")
        prosody_vecs = batch.get("prosody_vecs")
        if prosody_vecs is not None:
            prosody_vecs = prosody_vecs.to(device)

        plan_batch, plan_unused = plan_forward_kwargs(
            batch, plan_accepted, plan_registry, device)
        if plan_batch and not plan_seen:
            plan_seen = True
            if plan_unused and rank == 0:
                logger.warning(
                    "PLAN TIER: collator keys %s are not parameters of "
                    "model.forward and are unused during training (expected for "
                    "the decode-window channels; investigate anything else).",
                    plan_unused)
        elif not plan_seen and getattr(args, "plan_injection", False):
            raw = sorted(k for k in batch if k.startswith("plan_"))
            if raw:
                raise RuntimeError(
                    f"--plan_injection is on and the collator emitted {raw}, but "
                    f"none of it reached model.forward, which accepts "
                    f"{sorted(plan_accepted) or 'no plan parameter'}. The "
                    f"injection would train on an all-zero channel and the run "
                    f"would look like a plan arm that merely did not work.")
            if step - start_step >= PLAN_DRY_STEPS:
                raise RuntimeError(
                    f"--plan_injection is on but NO batch in the first "
                    f"{PLAN_DRY_STEPS} steps carried a plan channel. The corpus "
                    f"has no usable plans (the startup audit samples rows; this "
                    f"sees every batch), so the injection is training on "
                    f"nothing.")
        if not plan_batch and getattr(args, "plan_injection", False):
            plan_batch = absent_plan_kwargs(batch["input_ids"], plan_accepted,
                                            device)

        target_latents = None
        if flow_codec is not None and "codes" in batch:
            with torch.no_grad():
                cg = batch["codes"].to(device).clamp(min=0).transpose(1, 2)
                target_latents = flow_codec.codes_to_latents(cg).float()

        with torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16):
            if "codes" in batch:
                outputs = model(
                    input_ids=input_ids,
                    labels=labels,
                    speaker_ids=speaker_ids,
                    codes=batch["codes"].to(device),
                    prosody_vecs=prosody_vecs,
                    semantic_weight=getattr(args, "semantic_weight", 1.0),
                    eos_weight=getattr(args, "eos_weight", 1.0),
                    pause_exit_weight=getattr(args, "pause_exit_weight", 1.0),
                    pause_codes=getattr(args, "pause_codes", None),
                    target_latents=target_latents,
                    loss_weights=(batch["loss_weights"].to(device)
                                  if "loss_weights" in batch else None),
                    **plan_batch,
                )
            else:
                outputs = model(
                    input_ids=input_ids,
                    labels=labels,
                    speech_mask=speech_mask,
                    loss_weights=loss_weights,
                    residual_codes=residual_codes,
                    speaker_ids=speaker_ids,
                    **plan_batch,
                )
            l2sp_w = getattr(args, "l2sp_weight", 0.0)
            l2sp_term = None

            loss = outputs["loss"] / args.grad_accum

        loss.backward()

        if step % args.grad_accum == 0:
            if l2sp_w > 0:
                _um = model.module if isinstance(model, DDP) else model
                _ref = getattr(_um, "_l2sp_ref", None)
                if _ref is not None:
                    _acc = 0.0
                    with torch.no_grad():
                        for _n, _p in _um.backbone.named_parameters():
                            _r = _ref.get(_n)
                            if _r is None or _p.grad is None:
                                continue
                            _d = _p.detach() - _r.to(_p.device).to(_p.dtype)
                            _p.grad.add_(_d, alpha=2.0 * l2sp_w)
                            _acc += float(_d.pow(2).sum())
                    l2sp_term = torch.tensor(l2sp_w * _acc)
            torch.nn.utils.clip_grad_norm_(
                [p for p in model.parameters() if p.requires_grad],
                max_norm=1.0,
            )
            optimizer.step()
            optimizer.zero_grad()
            scheduler.step()

        running_loss += outputs["loss"].item()
        running_main += outputs["main_loss"].item()
        running_mtp += outputs["mtp_loss"].item()
        _fl = outputs.get("flow_loss")
        running_flow += _fl.item() if _fl is not None else 0.0

        if rank == 0 and step % args.log_every == 0:
            avg_loss = running_loss / args.log_every
            avg_main = running_main / args.log_every
            avg_mtp = running_mtp / args.log_every
            avg_flow = running_flow / args.log_every
            lr_current = optimizer.param_groups[0]["lr"]

            l2sp_str = (f" | l2sp: {l2sp_term.item():.4f}"
                        if l2sp_term is not None else "")
            flow_str = f" | flow: {avg_flow:.4f}" if flow_codec is not None else ""
            logger.info(
                f"[Stage {args.stage}] Step {step}/{args.max_steps} | "
                f"Loss: {avg_loss:.4f} (main: {avg_main:.4f}, mtp: {avg_mtp:.4f}) | "
                f"LR: {lr_current:.2e}{l2sp_str}{flow_str}"
            )

            running_loss = 0.0
            running_main = 0.0
            running_mtp = 0.0
            running_flow = 0.0

        if rank == 0 and step % args.save_every == 0:
            save_checkpoint(model, optimizer, scheduler, step, args)

        if args.val_data_path and rank == 0 and step % args.val_every == 0:
            val_loss = validate(model, args, device)
            logger.info(f"[Stage {args.stage}] Step {step} | Val Loss: {val_loss:.4f}")
            model.train()

    if rank == 0:
        save_checkpoint(model, optimizer, scheduler, args.max_steps, args)

    logger.info(f"[Stage {args.stage}] Training complete ({args.max_steps} steps).")


def dpo_train_loop(model, ref_model, loader, optimizer, scheduler, device, args, rank):
    model.train()
    ref_model.eval()
    data_iter = iter(loader)

    optimizer.zero_grad()
    running_loss = 0.0

    for step in range(1, args.max_steps + 1):
        batch, data_iter = next_batch(data_iter, loader)

        batch = {k: v.to(device) for k, v in batch.items()}

        with torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16):
            total_loss, metrics = compute_dpo_loss(
                model=model,
                ref_model=ref_model,
                batch=batch,
                beta=args.dpo_beta,
                alpha=args.dpo_alpha,
                kl_ceiling=args.dpo_kl_ceiling,
            )
            scaled_loss = total_loss / args.grad_accum

        scaled_loss.backward()

        if step % args.grad_accum == 0:
            torch.nn.utils.clip_grad_norm_(
                [p for p in model.parameters() if p.requires_grad],
                max_norm=1.0,
            )
            optimizer.step()
            optimizer.zero_grad()
            scheduler.step()

        running_loss += total_loss.item()

        if rank == 0 and step % args.log_every == 0:
            avg_loss = running_loss / args.log_every
            lr_current = optimizer.param_groups[0]["lr"]

            logger.info(
                f"[Stage 4 DPO] Step {step}/{args.max_steps} | "
                f"Loss: {avg_loss:.4f} | "
                f"DPO: {metrics['dpo_loss']:.4f} | "
                f"Entropy: {metrics['entropy_reg']:.4f} | "
                f"KL: {metrics['kl_mean']:.4f} | "
                f"LR: {lr_current:.2e}"
            )
            running_loss = 0.0

        if rank == 0 and step % args.save_every == 0:
            save_checkpoint(model, optimizer, scheduler, step, args)

    if rank == 0:
        save_checkpoint(model, optimizer, scheduler, args.max_steps, args)

    logger.info(f"[Stage 4 DPO] Training complete ({args.max_steps} steps).")


def save_checkpoint(model, optimizer, scheduler, step, args):
    os.makedirs(args.output_dir, exist_ok=True)
    path = os.path.join(args.output_dir, f"checkpoint_step_{step}.pt")

    model_to_save = model.module if isinstance(model, DDP) else model

    checkpoint = {
        "step": step,
        "stage": args.stage,
        "model_state_dict": model_to_save.state_dict(),
        "optimizer_state_dict": optimizer.state_dict(),
        "scheduler_state_dict": scheduler.state_dict(),
        "args": vars(args),
    }

    torch.save(checkpoint, path)
    logger.info(f"Saved checkpoint to {path}")

_CRITICAL_PROJ = (
    "in_proj.weight", "out_proj.weight",
    "query_key_value.weight", "attention.dense.weight",
)


def _remap_plain_keys_for_lora(sd, model_sd):
    out = {}
    n_remapped = 0
    for k, v in sd.items():
        if k not in model_sd:
            head, _, leaf = k.rpartition(".")
            candidate = f"{head}.original.{leaf}" if head else k
            if candidate in model_sd:
                out[candidate] = v
                n_remapped += 1
                continue
        out[k] = v
    return out, n_remapped

_VOCAB_PAD_SLACK = 256


def _vocab_row_keys(model, model_sd) -> set:
    reg = getattr(model, "token_registry", None)
    tk = getattr(reg, "tokenizer", None)
    if tk is None:
        return set()
    try:
        v_model = int(len(tk))
    except TypeError:
        return set()
    return {k for k, t in model_sd.items()
            if getattr(t, "ndim", 0) == 2
            and v_model <= int(t.shape[0]) < v_model + _VOCAB_PAD_SLACK}

_SPEAKER_TABLE_SUFFIX = "speaker_encoder.embedding.weight"


def _is_safetensors(path) -> bool:
    return str(path).endswith(".safetensors")


def read_checkpoint(path, map_location="cpu", mmap=False):
    if not _is_safetensors(path):
        return torch.load(path, map_location=map_location, weights_only=False, mmap=mmap)
    from safetensors import safe_open
    from safetensors.torch import load_file
    with safe_open(str(path), framework="pt") as f:
        meta = f.metadata() or {}
    out = {"model_state_dict": load_file(str(path), device=str(map_location or "cpu"))}
    if str(meta.get("step", "")).isdigit():
        out["step"] = int(meta["step"])
    return out


def checkpoint_tensor_shapes(path):
    if _is_safetensors(path):
        from safetensors import safe_open
        with safe_open(str(path), framework="pt") as f:
            return {k: tuple(f.get_slice(k).get_shape()) for k in f.keys()}
    peek = torch.load(path, map_location="cpu", weights_only=False, mmap=True)
    sd = peek.get("model_state_dict", peek) if isinstance(peek, dict) else {}
    return {k: tuple(getattr(v, "shape", ())) for k, v in sd.items()}


def checkpoint_speaker_rows(path):
    if _is_safetensors(path):
        try:
            shapes = checkpoint_tensor_shapes(path)
        except Exception:
            return None
        return next((int(s[0]) for k, s in shapes.items()
                     if k.endswith(_SPEAKER_TABLE_SUFFIX) and len(s) == 2), None)
    try:
        peek = torch.load(path, map_location="cpu", weights_only=False, mmap=True)
    except Exception:
        return None
    sd = peek.get("model_state_dict", peek) if isinstance(peek, dict) else {}
    for k, v in sd.items():
        if k.endswith(_SPEAKER_TABLE_SUFFIX) and getattr(v, "ndim", 0) == 2:
            return int(v.shape[0])
    return None


def _splice_plan_vocab_rows(old, new_like, plan_lo: int, n_plan: int,
                            v_old_real: int):
    v_old_pad, v_new_pad = int(old.shape[0]), int(new_like.shape[0])
    v_old_real = int(v_old_real)
    if tuple(old.shape[1:]) != tuple(new_like.shape[1:]):
        raise RuntimeError(
            f"vocab splice: trailing dims differ ({tuple(old.shape)} vs "
            f"{tuple(new_like.shape)}) — this is not a pure vocabulary resize.")
    if not 0 <= plan_lo <= v_old_real:
        raise RuntimeError(f"vocab splice: insertion point {plan_lo} outside "
                           f"[0, {v_old_real}]")
    if v_old_real > v_old_pad:
        raise RuntimeError(
            f"vocab splice: checkpoint tensor has {v_old_pad} rows but the "
            f"pre-plan vocabulary is {v_old_real} tokens — this checkpoint was "
            f"not produced by the vocabulary this model was built with "
            f"(MVC_NUM_SPEECH_TOKENS / MVC_NUM_STYLE_TOKENS mismatch?).")
    tail = v_old_real - plan_lo
    if plan_lo + n_plan + tail > v_new_pad:
        raise RuntimeError(
            f"vocab splice does not fit: {plan_lo} kept + {n_plan} plan + "
            f"{tail} shifted = {plan_lo + n_plan + tail} rows > the model's "
            f"{v_new_pad}.")
    out = new_like.clone()
    src = old.to(device=out.device, dtype=out.dtype)
    out[:plan_lo] = src[:plan_lo]
    out[plan_lo + n_plan:plan_lo + n_plan + tail] = src[plan_lo:v_old_real]
    return out


def load_checkpoint(path, model, optimizer=None, scheduler=None, device="cuda",
                    splice_plan_vocab: bool = False,
                    allow_speaker_table_reinit: bool = False):
    checkpoint = read_checkpoint(path, map_location=device)

    sd = checkpoint["model_state_dict"]
    model_sd = model.state_dict()

    ckpt_has_lora = any("lora_A" in k or "lora_B" in k for k in sd)
    model_has_lora = any("lora_A" in k or "lora_B" in k for k in model_sd)
    if ckpt_has_lora and not model_has_lora:
        raise RuntimeError(
            f"Checkpoint {path} contains LoRA adapters but the model has no "
            f"LoRA modules — loading would silently drop all adapter weights. "
            f"Either apply LoRA before loading (--stage 1 does this), or merge "
            f"the adapters first: scripts/merge_lora.py --checkpoint {path}"
        )

    ckpt_plan_keys = [k for k in sd if _is_plan_param(k)]
    model_plan_keys = [k for k in model_sd if _is_plan_param(k)]
    if ckpt_plan_keys and not model_plan_keys:
        raise RuntimeError(
            f"Checkpoint {path} carries {len(ckpt_plan_keys)} P²-CoT plan-tier "
            f"tensor(s) (e.g. {ckpt_plan_keys[0]}) but the model has none — "
            f"loading would silently drop the whole plan injection and train/"
            f"decode as an unplanned arm. Rebuild the model with "
            f"--plan_injection (and export MVC_ENABLE_PLAN_TOKENS=1 so the "
            f"vocabulary matches), or load a pre-plan checkpoint."
        )

    if model_has_lora and not ckpt_has_lora:
        sd, n_remapped = _remap_plain_keys_for_lora(sd, model_sd)
        if n_remapped:
            logger.info("Remapped %d plain checkpoint key(s) onto LoRA "
                        "'.original.' targets (full-FT/merged ckpt -> "
                        "LoRA-wrapped model)", n_remapped)

    replaced = getattr(model, "hybrid_replaced_layers", None) or []
    if replaced:
        pref = tuple(f"backbone.backbone.layers.{i}.mixer." for i in replaced)
        ckpt_mixer_is_mamba = any(k.startswith(pref) and "A_log" in k for k in sd)
        if ckpt_mixer_is_mamba:
            hyb_dropped = [k for k in sd if k.startswith(pref)]
            sd = {k: v for k, v in sd.items() if k not in hyb_dropped}
            logger.info("Hybrid warm-start: dropped %d Mamba mixer key(s) for "
                        "replaced layers %s (attention mixers keep fresh init)",
                        len(hyb_dropped), replaced)

    dropped = [k for k, v in sd.items()
               if k in model_sd and tuple(model_sd[k].shape) != tuple(v.shape)]

    vocab_keys = _vocab_row_keys(model, model_sd)
    vocab_dropped = [k for k in dropped if k in vocab_keys]
    if vocab_dropped:
        reg = getattr(model, "token_registry", None)
        plan_lo = getattr(reg, "plan_token_id_min", None)
        n_plan = len(getattr(reg, "plan_token_ids", ()) or ())
        v_new_real = len(reg.tokenizer) if getattr(reg, "tokenizer", None) else 0
        v_old_real = v_new_real - n_plan
        can_splice = (plan_lo is not None and n_plan > 0 and v_new_real > 0
                      and all(int(sd[k].shape[0]) >= v_old_real
                              and int(model_sd[k].shape[0]) >= v_new_real
                              and int(model_sd[k].shape[0]) > int(sd[k].shape[0])
                              for k in vocab_dropped))
        if splice_plan_vocab and can_splice:
            for k in vocab_dropped:
                sd[k] = _splice_plan_vocab_rows(sd[k], model_sd[k], plan_lo,
                                                n_plan, v_old_real)
            dropped = [k for k in dropped if k not in vocab_dropped]
            logger.warning(
                "PLAN TIER: spliced %d vocab-sized tensor(s) from %s — ids "
                "[0,%d) unchanged, ids [%d,%d) shifted up by %d, the %d plan "
                "rows and any padding keep the model's fresh init: %s",
                len(vocab_dropped), path, plan_lo, plan_lo, v_old_real, n_plan,
                n_plan, ", ".join(sorted(vocab_dropped)))
        else:
            shapes = ", ".join(
                f"{k}: ckpt{tuple(sd[k].shape)} vs model{tuple(model_sd[k].shape)}"
                for k in sorted(vocab_dropped)[:4])
            raise RuntimeError(
                f"{len(vocab_dropped)} VOCABULARY-sized tensor(s) in checkpoint "
                f"{path} have a different row count than the model ({shapes}). "
                f"Dropping them would silently re-initialise the token embedding "
                f"and/or the LM head — a cold start wearing a warm start's name. "
                f"This is what MVC_ENABLE_PLAN_TOKENS=1 does to a pre-P²-CoT "
                f"checkpoint (the plan block adds {n_plan or 'N'} rows below the "
                f"speech block). Fix by either: (a) passing "
                f"splice_plan_vocab=True / --splice_plan_vocab to remap the rows "
                f"exactly" + ("" if can_splice else
                              " (NOT applicable here — the row delta is not the "
                              "plan-block size)") +
                f", or (b) matching the gate/MVC_NUM_SPEECH_TOKENS this "
                f"checkpoint was trained under.")

    if dropped:
        arch = [k for k in dropped if k.endswith(_CRITICAL_PROJ)]
        if arch:
            raise RuntimeError(
                f"{len(arch)} projection weight(s) in checkpoint {path} have a "
                f"different SHAPE than the model (e.g. {arch[0]}) — the model was "
                f"built with a different architecture than the one that produced "
                f"this checkpoint. Check --hybrid_attention_top_k / --backbone."
            )
        spk = [k for k in dropped if k.endswith(_SPEAKER_TABLE_SUFFIX)]
        if spk and not allow_speaker_table_reinit:
            k = spk[0]
            raise RuntimeError(
                f"Speaker table {k} in checkpoint {path} is "
                f"{tuple(sd[k].shape)} but the model was built as "
                f"{tuple(model_sd[k].shape)} (--num_speakers). Dropping it "
                f"re-initialises EVERY voice from scratch, and with input "
                f"injection that random vector enters the backbone at every "
                f"step — the 2026-09-21 WER battery ran 32768-voice checkpoints "
                f"through 8192-row tables this way. Pass --num_speakers "
                f"{int(sd[k].shape[0])} (inference); for a warm start that grows "
                f"the table run scripts/expand_speaker_table.py first; "
                f"--allow_speaker_table_reinit keeps the old silent drop on "
                f"purpose."
            )
        logger.warning("Dropping %d shape-mismatched checkpoint key(s) (re-init "
                       "from scratch): %s", len(dropped),
                       ", ".join(dropped[:6]) + (" ..." if len(dropped) > 6 else ""))
        sd = {k: v for k, v in sd.items() if k not in dropped}

    incompat = model.load_state_dict(sd, strict=False)

    unexpected = list(incompat.unexpected_keys)
    critical = [k for k in unexpected if k.endswith(_CRITICAL_PROJ)]
    if critical:
        raise RuntimeError(
            f"{len(critical)} projection weight(s) in checkpoint {path} match no "
            f"model key and would be silently dropped (e.g. {critical[0]}) — this "
            f"is the full-FT-ckpt/LoRA-model mismatch that produces base-weight "
            f"babble. Load with the matching LoRA rank (0 for full-FT checkpoints)."
        )
    if unexpected:
        logger.warning("load_state_dict ignored %d unexpected checkpoint key(s): %s",
                       len(unexpected),
                       ", ".join(unexpected[:6]) + (" ..." if len(unexpected) > 6 else ""))
    missing = [k for k in incompat.missing_keys
               if not (("lora_A" in k or "lora_B" in k) and not ckpt_has_lora)]

    plan_missing = [k for k in incompat.missing_keys if _is_plan_param(k)]
    if plan_missing:
        logger.warning(
            "PLAN TIER: %d plan tensor(s) received NO value from %s and keep "
            "their fresh init%s: %s", len(plan_missing), path,
            " (expected on a warm start from a pre-plan checkpoint)"
            if not ckpt_plan_keys else " — the checkpoint HAS plan tensors, so "
            "this is a real mismatch, not a graft",
            ", ".join(plan_missing))

    if missing:
        logger.warning("%d model key(s) received no checkpoint value (kept init): %s",
                       len(missing),
                       ", ".join(missing[:6]) + (" ..." if len(missing) > 6 else ""))

    graft_pref = tuple(f"backbone.backbone.layers.{i}.mixer." for i in replaced)

    def _depth_stack(keys):
        return {k for k in keys
                if ".depth_tf." in k or ".gru." in k or ".depth_ssm." in k}
    ck_stack, md_stack = _depth_stack(sd), _depth_stack(model_sd)
    depth_swap = bool(ck_stack) and bool(md_stack) and not (ck_stack & md_stack)
    if depth_swap:
        logger.warning("Depth head ARCHITECTURE SWAP detected (checkpoint and "
                       "model use different depth stacks) — the depth module "
                       "starts from fresh init by design; the backbone still "
                       "loads and is still guarded.")

    plan_graft = bool(model_plan_keys) and not ckpt_plan_keys

    critical_missing = [k for k in missing
                        if k.endswith(_CRITICAL_PROJ)
                        and k not in dropped
                        and not (graft_pref and k.startswith(graft_pref))
                        and not (depth_swap and k.startswith("depth_module."))
                        and not (plan_graft and _is_plan_param(k))]
    if critical_missing:
        raise RuntimeError(
            f"{len(critical_missing)} projection weight(s) received NO value from "
            f"checkpoint {path} (e.g. {critical_missing[0]}) and would train from "
            f"fresh init. Check that --backbone / --model_name / LoRA rank match "
            f"the checkpoint that produced it."
        )

    logger.info(f"Loaded model from {path} (step {checkpoint.get('step', '?')})")

    if optimizer and "optimizer_state_dict" in checkpoint:
        optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
    if scheduler and "scheduler_state_dict" in checkpoint:
        scheduler.load_state_dict(checkpoint["scheduler_state_dict"])

    return checkpoint.get("step", 0)


def validate(model, args, device):
    model.eval()

    unwrapped = model.module if isinstance(model, DDP) else model
    plan_kw = plan_dataset_kwargs(args)
    if getattr(args, "delay_pattern", False) or getattr(args, "depth_module", False):
        from delay_dataset import DelayMimiDataset, DelayCollator
        val_dataset = DelayMimiDataset(
            data_path=args.val_data_path,
            tokenizer=unwrapped.tokenizer,
            registry=unwrapped.token_registry,
            max_seq_len=args.max_seq_len,
            aligned=getattr(args, "depth_module", False),
            stage=args.dataset_stage,
            cot_mode=getattr(args, "cot_mode", "prose"),
            reasoning_weight=getattr(args, "reasoning_weight", 0.3),
            audio_continue_weight=getattr(args, "audio_continue_weight", 0.0),
            default_speaker_id=getattr(args, "default_speaker_id", None),
            **_plan_kwargs_for(DelayMimiDataset.__init__, plan_kw,
                               "DelayMimiDataset", PLAN_DATASET_DEFAULTS),
        )
        val_collator = DelayCollator(pad_token_id=unwrapped.token_registry.pad_id)
    else:
        val_dataset = MambaCoTTTSDataset(
            data_path=args.val_data_path,
            tokenizer=unwrapped.tokenizer,
            registry=unwrapped.token_registry,
            stage=args.dataset_stage,
            cot_mode=getattr(args, "cot_mode", "prose"),
            max_seq_len=args.max_seq_len,
            **_plan_kwargs_for(MambaCoTTTSDataset.__init__, plan_kw,
                               "MambaCoTTTSDataset", PLAN_DATASET_DEFAULTS),
        )
        val_collator = MambaCoTDataCollator(
            pad_token_id=unwrapped.token_registry.pad_id,
            registry=unwrapped.token_registry,
            stage=args.dataset_stage,
        )
    use_weighted_loss = STAGE_DEFAULTS[args.stage]["use_weighted_loss"]
    plan_accepted = plan_forward_params(unwrapped)
    val_loader = DataLoader(
        val_dataset, batch_size=args.batch_size, shuffle=False,
        collate_fn=val_collator, num_workers=2,
    )

    total_loss = 0.0
    num_batches = 0

    with torch.no_grad():
        for batch in val_loader:
            input_ids = batch["input_ids"].to(device)
            labels = batch["labels"].to(device)

            plan_batch, _ = plan_forward_kwargs(
                batch, plan_accepted, unwrapped.token_registry, device)
            if "codes" in batch:
                speaker_ids = batch.get("speaker_ids")
                prosody_vecs = batch.get("prosody_vecs")
                outputs = unwrapped(
                    input_ids=input_ids, labels=labels,
                    speaker_ids=speaker_ids.to(device) if speaker_ids is not None else None,
                    codes=batch["codes"].to(device),
                    prosody_vecs=prosody_vecs.to(device) if prosody_vecs is not None else None,
                    semantic_weight=getattr(args, "semantic_weight", 1.0),
                    eos_weight=getattr(args, "eos_weight", 1.0),
                    pause_exit_weight=getattr(args, "pause_exit_weight", 1.0),
                    pause_codes=getattr(args, "pause_codes", None),
                    loss_weights=(batch["loss_weights"].to(device)
                                  if "loss_weights" in batch else None),
                    **plan_batch,
                )
            else:
                speech_mask = batch["speech_mask"].to(device)
                loss_weights = (batch["loss_weights"].to(device)
                                if use_weighted_loss else None)
                outputs = unwrapped(
                    input_ids=input_ids, labels=labels,
                    speech_mask=speech_mask, loss_weights=loss_weights,
                    **plan_batch,
                )
            total_loss += outputs["loss"].item()
            num_batches += 1

    return total_loss / max(num_batches, 1)


def decode_speech_tokens(token_ids: list, registry: TokenRegistry) -> list:
    codec_values = []
    for tid in token_ids:
        if registry.is_speech_token(tid):
            codec_values.append(registry.speech_id_to_value(tid))
    return codec_values


def freeze_for_mtp_healing(model):
    for param in model.parameters():
        param.requires_grad = False

    trainable_count = 0
    for name, param in model.mtp_module.named_parameters():
        param.requires_grad = True
        trainable_count += param.numel()

    total = sum(p.numel() for p in model.parameters())
    logger.info(
        f"MTP Healing: frozen all params, unfrozen MTP heads. "
        f"Trainable: {trainable_count:,} / {total:,} "
        f"({100 * trainable_count / total:.3f}%)"
    )
    return trainable_count


def mtp_healing_loop(model, loader, optimizer, scheduler, device, args, rank):
    model.train()
    data_iter = iter(loader)

    optimizer.zero_grad()
    running_mtp_loss = 0.0

    for step in range(1, args.max_steps + 1):
        try:
            batch = next(data_iter)
        except StopIteration:
            data_iter = iter(loader)
            batch = next(data_iter)

        input_ids = batch["input_ids"].to(device)
        labels = batch["labels"].to(device)
        speech_mask = batch["speech_mask"].to(device)
        residual_codes = batch.get("residual_codes")
        if residual_codes is not None:
            residual_codes = residual_codes.to(device)

        with torch.amp.autocast(device_type="cuda", dtype=torch.bfloat16):
            outputs = model(
                input_ids=input_ids,
                labels=labels,
                speech_mask=speech_mask,
                loss_weights=None,
                residual_codes=residual_codes,
            )

            mtp_loss = outputs["mtp_loss"]
            if mtp_loss is None or mtp_loss.item() == 0.0:
                continue

            scaled_loss = mtp_loss / args.grad_accum

        scaled_loss.backward()

        if step % args.grad_accum == 0:
            torch.nn.utils.clip_grad_norm_(
                [p for p in model.parameters() if p.requires_grad],
                max_norm=1.0,
            )
            optimizer.step()
            optimizer.zero_grad()
            scheduler.step()

        running_mtp_loss += mtp_loss.item()

        if rank == 0 and step % args.log_every == 0:
            avg_mtp = running_mtp_loss / args.log_every
            lr_current = optimizer.param_groups[0]["lr"]

            logger.info(
                f"[Stage 5 MTP Healing] Step {step}/{args.max_steps} | "
                f"MTP Loss: {avg_mtp:.4f} | LR: {lr_current:.2e}"
            )
            running_mtp_loss = 0.0

        if rank == 0 and step % args.save_every == 0:
            save_checkpoint(model, optimizer, scheduler, step, args)

    if rank == 0:
        save_checkpoint(model, optimizer, scheduler, args.max_steps, args)

    logger.info(f"[Stage 5 MTP Healing] Complete ({args.max_steps} steps).")


def setup_distributed():
    if "RANK" in os.environ:
        dist.init_process_group(backend="nccl")
        rank = int(os.environ["RANK"])
        local_rank = int(os.environ["LOCAL_RANK"])
        world_size = int(os.environ["WORLD_SIZE"])
        torch.cuda.set_device(local_rank)
        device = torch.device(f"cuda:{local_rank}")
        return rank, local_rank, world_size, device, True
    else:
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        return 0, 0, 1, device, False


def cleanup_distributed():
    if dist.is_initialized():
        dist.destroy_process_group()


def parse_args():
    parser = argparse.ArgumentParser(
        description="Mamba-CoT-TTS Multi-Stage Training Pipeline"
    )

    parser.add_argument("--stage", type=int, required=True, choices=[1, 2, 3, 4, 5],
                        help="Training stage (1=codec align, 2=CoT pretrain, 3=SFT, 4=DPO, 5=MTP healing)")
    parser.add_argument("--data_path", type=str, required=True,
                        help="Path to training JSONL data")

    parser.add_argument("--backbone", type=str, default="mamba",
                        choices=["mamba", "transformer"],
                        help="Architecture arm for the thesis comparison: "
                             "'mamba' (0%% attention, state-spaces/mamba2-1.3b) "
                             "or 'transformer' (100%%, EleutherAI/pythia-1.4b — "
                             "matched Pile corpus / GPT-NeoX tokenizer / d_model "
                             "2048). Pair with --model_name. See backbones.py.")
    parser.add_argument("--model_name", type=str, default="state-spaces/mamba2-1.3b",
                        help="HuggingFace model ID for Mamba-2 backbone")
    parser.add_argument("--output_dir", type=str, default="./checkpoints",
                        help="Directory for checkpoints")
    parser.add_argument("--resume_from", type=str, default=None,
                        help="Path to checkpoint to resume from")
    parser.add_argument("--lr_speaker", type=float, default=0.0,
                        help="separate LR for the speaker embedding at stages "
                             "2-4 (0 = share the main LR). A row that is FRESH "
                             "at Stage 2 must be LEARNED, not refined, and the "
                             "fine-tuning LR cannot do that in one epoch - see "
                             "ledger S120.")
    parser.add_argument("--f1_exclude_prompts_from", nargs="*", default=None,
                        help="extra JSONL files whose `prompt` tokens are "
                             "excluded from the F1 whitelist. Pass the eval "
                             "prompt sets: a tag token that never appears in a "
                             "TRAINING prompt can still appear in an eval one, "
                             "and training it would perturb exactly the rows "
                             "the base-preservation gate is measured on.")
    parser.add_argument("--train_cot_embeddings_only", action="store_true",
                        help="WORKSTREAM F1 (the frozen-backbone floor): train "
                             "ONLY the token-embedding rows the THINK region "
                             "uses; every other parameter is frozen. Base-"
                             "domain generation is then preserved by "
                             "construction, not by passing a gate — unlike "
                             "LoRA, which could still rewrite every projection "
                             "and failed worst of all Track-A arms (S138). "
                             "Requires --cot_mode tags and --weight_decay 0.")
    parser.add_argument("--freeze_speaker", action="store_true",
                        help="Pin the speaker table (requires_grad=False) after "
                             "any --speaker_init_from copy. NOT the same as "
                             "--lr_speaker 0, which is falsy and silently puts "
                             "the table back on the main LR.")
    parser.add_argument("--speaker_init_from", type=str, default=None,
                        help="copy TRAINED speaker rows onto fresh ids before "
                             "training, as 'new:src,new:src' (e.g. "
                             "'4200:1,4201:2'). A random row is off-manifold for "
                             "a backbone trained to expect real speaker vectors; "
                             "starting from a trained voice makes the fine-tune "
                             "a refinement instead of a search.")
    parser.add_argument("--init_from", type=str, default=None,
                        help="Initialize MODEL WEIGHTS ONLY from this checkpoint "
                             "(fresh optimizer/scheduler, step 0). Use when the "
                             "module set or loss objective changed (e.g. adding "
                             "speaker input-injection) so the old optimizer state "
                             "no longer matches. New zero-init params stay zero.")

    parser.add_argument("--batch_size", type=int, default=None,
                        help="Per-GPU batch size (default: stage-specific)")
    parser.add_argument("--grad_accum", type=int, default=None,
                        help="Gradient accumulation steps (default: stage-specific)")
    parser.add_argument("--max_steps", type=int, default=None,
                        help="Total training steps (default: stage-specific)")
    parser.add_argument("--seed", type=int, default=None,
                        help="Training seed for a seed replicate (2026-09-27): seeds python / numpy / torch "
                             "(offset by rank) AND the DistributedSampler, so the data order changes too. "
                             "Default None = the historical behaviour (no explicit seeding, sampler seed 0), "
                             "so every earlier run's command reproduces unchanged.")
    parser.add_argument("--warmup_steps", type=int, default=None,
                        help="LR warmup steps (default: stage-specific)")
    parser.add_argument("--max_seq_len", type=int, default=2048,
                        help="Maximum sequence length")
    parser.add_argument("--log_every", type=int, default=10,
                        help="Log metrics every N steps")
    parser.add_argument("--save_every", type=int, default=5000,
                        help="Save checkpoint every N steps")
    parser.add_argument("--val_every", type=int, default=1000,
                        help="Validate every N steps")

    parser.add_argument("--lr", type=float, default=None,
                        help="Learning rate (default: stage-specific)")
    parser.add_argument("--lr_new_params", type=float, default=3e-4,
                        help="Stage 1: LR for new embeddings/head/MTP "
                             "(also used for the fresh embedding/lm_head/MTP "
                             "group under --full_finetune)")
    parser.add_argument("--lr_lora", type=float, default=1e-4,
                        help="Stage 1: LR for LoRA adapters")
    parser.add_argument("--lr_backbone", type=float, default=5e-5,
                        help="Stage 1 --full_finetune: LR for the PRETRAINED "
                             "Mamba-2 SSM backbone (layers + norm_f). Kept well "
                             "below lr_new_params because full fine-tuning a "
                             "pretrained backbone at the new-param LR (3e-4) "
                             "diverges. Recommended 5e-5 (ceiling ~1e-4).")
    parser.add_argument("--lr_min", type=float, default=None,
                        help="Minimum LR for cosine decay (default: stage-specific)")

    parser.add_argument("--full_finetune", action="store_true",
                        help="Stage 1: skip LoRA and full fine-tune the whole "
                             "model (unfreeze ALL params). Pretrained SSM "
                             "backbone trains @ --lr_backbone (5e-5), the fresh "
                             "embedding/lm_head/MTP @ --lr_new_params (3e-4). "
                             "When absent, behavior is byte-identical to the "
                             "LoRA path. (Track B: test whether limited adapter "
                             "capacity, not the codec, was the Stage-1 bottleneck.)")
    parser.add_argument("--fullft_bf16_optim", action="store_true",
                        help="--full_finetune only: keep params/optimizer in "
                             "bf16 instead of upcasting to fp32 master weights. "
                             "Lower memory, but bf16 second-moment accumulation "
                             "over 1.7B params can confound convergence. Default "
                             "(off) = fp32 master weights for stability.")
    parser.add_argument("--use_lora", action="store_true",
                        help="Force LoRA in stages >1 (default: only stage 1 uses "
                             "LoRA; stages 2+ full-finetune). Phase A': LoRA in "
                             "stage 2 freezes the backbone so the Stage-1 cb0 "
                             "articulation survives the small-corpus CoT fine-tune "
                             "(full-FT on ~2.9k utts destroyed cb0 — job 14733469).")
    parser.add_argument("--lora_rank", type=int, default=64,
                        help="LoRA rank")
    parser.add_argument("--lora_alpha", type=int, default=64,
                        help="LoRA alpha (scaling = alpha/rank)")
    parser.add_argument("--lora_target_modules", nargs="+",
                        default=["in_proj", "out_proj"],
                        help="Module names to apply LoRA (Mamba-2 SSM projections)")
    parser.add_argument("--num_crh_heads", type=int, default=0,
                        help="Codebook-residual heads. 0 = X-Codec2 prototype "
                             "(disabled). Set to (codec.num_codebooks - 1) "
                             "for multi-codebook codecs: e.g. 7 for Mimi-8.")
    parser.add_argument("--crh_codebook_size", type=int, default=2048,
                        help="Per-codebook size for CRH targets. Mimi: 2048.")
    parser.add_argument("--speaker_conditioning", action="store_true",
                        help="Enable AdaLN speaker conditioning (v1, post-backbone; "
                             "zero-init so it starts as identity). Requires "
                             "speaker_id in the data (scripts/build_speaker_map.py).")
    parser.add_argument("--num_speakers", type=int, default=4000,
                        help="SpeakerEncoder table size; must exceed the highest "
                             "speaker_id in the data (LibriTTS-R clean+other ~2.5K)")
    parser.add_argument("--allow_speaker_table_reinit", action="store_true",
                        help="Let a weights-only warm start DROP a shape-mismatched "
                             "speaker table (every voice re-initialised). Off by "
                             "default since 2026-09-21; the sanctioned way to grow "
                             "the table is scripts/expand_speaker_table.py.")
    parser.add_argument("--speaker_dim", type=int, default=256)
    parser.add_argument("--prosody_vec_injection", action="store_true",
                        help="PB-12: condition the delay-pattern renderer on a "
                             "6-dim mechanical prosody vector (rows' "
                             "prosody_vec field from scripts/"
                             "prep_prosody_vectors.py), injected zero-init "
                             "through the SSM input channel.")
    parser.add_argument("--speaker_input_injection", action="store_true",
                        help="Also inject the speaker embedding into the backbone "
                             "INPUT (carried through the SSM recurrence) for "
                             "persistent frame-to-frame timbre; fixes the post-"
                             "backbone AdaLN's within-utterance voice drift. "
                             "Zero-init -> exact resume from a non-injection ckpt.")
    parser.add_argument("--semantic_weight", type=float, default=1.0,
                        help="Loss upweight on level-0 (semantic) positions of a "
                             "flattened multi-codebook stream; needs --level_cycle")
    parser.add_argument("--weight_decay", type=float, default=0.1,
                        help="AdamW weight decay. fish-speech uses 0.0; 0.1 is "
                             "this project's historical default.")
    parser.add_argument("--adam_eps", type=float, default=1e-8,
                        help="AdamW eps. fish-speech uses 1e-5.")
    parser.add_argument("--depth_arch", choices=["gru", "mamba2", "transformer"],
                        default="gru",
                        help="depth-head architecture over the codebook axis. "
                             "'transformer' matches the reference "
                             "(fish-speech DualAR uses n_fast_layer=4 "
                             "TRANSFORMER layers); 'gru' is the original.")
    parser.add_argument("--depth_level_decay", type=float, default=1.0,
                        help="geometric decay of the per-codebook depth loss "
                             "weight, w(k)=decay^(k-1) for k>=1 (Fish S2 eq.3 "
                             "puts capacity on the coarse acoustic codebooks). "
                             "1.0 = the old flat weighting.")
    parser.add_argument("--depth_cond", choices=["add", "prefix"], default="add",
                        help="how the backbone hidden state conditions the depth "
                             "stack. 'prefix' = Fish S2 §2.2 (h_slow as a TOKEN "
                             "at depth position 0, one SHARED codebook embedding "
                             "table, explicit level identity). 'add' = ours "
                             "(h broadcast onto every step, per-level tables).")
    parser.add_argument("--depth_heads", type=int, default=8,
                        help="attention heads in the transformer depth head.")
    parser.add_argument("--audio_continue_weight", type=float, default=0.0,
                        help="Supervise the LM head across the AUDIO span at this "
                             "weight instead of IGNORE_INDEX (ledger S79). The "
                             "stop head is queried every audio frame at inference "
                             "but, at 0.0, is trained at none of them — p(stop) "
                             "then drifts to 15-59x its base rate and generation "
                             "halts at the first comma. ~0.1 supplies the missing "
                             "'continue' evidence without swamping the text CE. "
                             "NB: non-zero SHIFTS absolute val loss, so it "
                             "re-baselines any cross-run comparison.")
    parser.add_argument("--pause_exit_weight", type=float, default=1.0,
                        help="S200 remedy (backtrack experiment): multiply the cb0 "
                             "depth-CE on frames whose target is a SPEECH code and "
                             "whose predecessor was a PAUSE code - the pause->speech "
                             "transition the collapsed head never makes. Weighted "
                             "mean normalised by the weights (eos_weight convention); "
                             "1.0 = off, bit-identical. Needs --pause_codes_file.")
    parser.add_argument("--pause_codes_file", default="",
                        help="JSON with a `codes` list (or a bare list) of cb0 pause "
                             "codes, e.g. data/silence_codes_fish_s2.json.")
    parser.add_argument("--eos_weight", type=float, default=1.0,
                        help="Loss upweight on </AUDIO>/<EOS> positions "
                             "(stopping/alignment supervision)")
    parser.add_argument("--level_cycle", type=int, default=0,
                        help="Codebook levels per frame in the flattened stream "
                             "(8 for mimi8); used by --semantic_weight")
    parser.add_argument("--cfg_dropout", type=float, default=0.0,
                        help="Classifier-free-guidance prompt dropout probability "
                             "(e.g. 0.1): drop the text condition so an "
                             "unconditional stream is learned for guided decoding")
    parser.add_argument("--delay_pattern", action="store_true",
                        help="Arm B: MusicGen-style delay multi-codebook path — "
                             "one position per frame, K summed level embeddings "
                             "+ K heads. Consumes *_mimi (multi-codebook) JSONLs "
                             "directly; --semantic_weight applies to level 0.")
    parser.add_argument("--depth_module", action="store_true",
                        help="PB-14 Lever 3: RQ-style recurrent depth module — "
                             "frame-aligned grid (no stagger); a GRU generates "
                             "cb0..7 sequentially per frame from the backbone "
                             "hidden state (explicit intra-frame conditioning). "
                             "Consumes the same *_mimi JSONLs as --delay_pattern.")
    parser.add_argument("--depth_dim", type=int, default=1024,
                        help="depth module width")
    parser.add_argument("--depth_layers", type=int, default=2,
                        help="depth module GRU layers")
    parser.add_argument("--depth_feedback", choices=["all", "semantic"],
                        default="all",
                        help="what the TEMPORAL stream sees of each frame: all "
                             "8 levels (Moshi-style) or cb0 only (decode-time "
                             "acoustic sampling cannot contaminate the "
                             "recurrence)")
    parser.add_argument("--depth_cross_frame", type=int, default=0,
                        help="S30 fan fix: number of PREVIOUS-frame coarse "
                             "acoustic levels (cb1..cbN) whose codes condition "
                             "this frame's depth unroll — the cross-FRAME "
                             "channel the per-frame GRU reset lacks. The fan is "
                             "carried by cb1..3 (jitter probe), so 3 is the "
                             "recommended value. 0 = legacy per-frame behavior.")
    parser.add_argument("--codec_num_levels", type=int, default=8,
                        help="number of codec levels for the delay/depth paths "
                             "(Mimi: 8; Fish S2: 10)")
    parser.add_argument("--codebook_sizes", type=str, default=None,
                        help="comma-separated per-level vocab sizes for "
                             "heterogeneous codecs (Fish S2: "
                             "'4096,1024,1024,1024,1024,1024,1024,1024,1024,1024'). "
                             "Default None = uniform 2048 (Mimi)")
    parser.add_argument("--freeze_depth_module", action="store_true",
                        help="S25 fan fix: freeze the depth module during "
                             "Stage-2 fine-tuning so the init checkpoint's "
                             "acoustic rendering (e.g. dial-108's clean "
                             "flatmod) survives unchanged — the CoT/tags "
                             "conditioning trains into the backbone only")
    parser.add_argument("--flow_head", action="store_true",
                        help="PB-14 option A: attach a continuous flow-matching "
                             "head that predicts the Mimi RVQ-recon latent "
                             "(dim 512) per frame — decoded through the FROZEN "
                             "Mimi vocoder, no argmax. Additive to --depth_module "
                             "(discrete cb0 kept for WER/stop/CoT); loads the "
                             "codec to compute fp32 latent targets on the fly.")
    parser.add_argument("--flow_hidden", type=int, default=1024,
                        help="flow head MLP width")
    parser.add_argument("--flow_layers", type=int, default=4,
                        help="flow head residual MLP blocks")
    parser.add_argument("--flow_weight", type=float, default=1.0,
                        help="weight of the flow loss in the total")
    parser.add_argument("--hybrid_attention_top_k", type=int, default=0,
                        help="Phase 8: replace the top-K Mamba blocks' mixers "
                             "with causal self-attention + RoPE (0 = pure Mamba). "
                             "Fresh-init; on a pure-ckpt warm start the replaced "
                             "layers' Mamba weights are dropped. The co-equal "
                             "comparison arm uses K=4.")
    parser.add_argument("--flow_only", action="store_true",
                        help="freeze everything except the flow head — the cheap, "
                             "safe A-frozen first pass (discrete cb0/WER untouched; "
                             "only ~15M flow params train). Drop it for a full-FT "
                             "run where the backbone can also adapt to the head.")
    parser.add_argument("--mtp_num_heads", type=int, default=3,
                        help="Shared-weight speculative MTP head offsets trained "
                             "as an auxiliary loss. Default 3 (backward compat). "
                             "Set 0 to disable MTP entirely — recommended for "
                             "Stage-1 codec alignment so all LoRA capacity goes to "
                             "the next-token (main) objective that generation uses. "
                             "MTP is restored/trained in Stage-5 healing.")
    parser.add_argument("--lora_dropout", type=float, default=0.05,
                        help="LoRA dropout rate")

    parser.add_argument("--dpo_beta", type=float, default=0.5,
                        help="DPO temperature coefficient")
    parser.add_argument("--dpo_alpha", type=float, default=0.01,
                        help="DPO entropy regularization weight")
    parser.add_argument("--dpo_kl_ceiling", type=float, default=5.0,
                        help="DPO KL divergence ceiling")
    parser.add_argument("--ref_model_path", type=str, default=None,
                        help="Stage 4: path to reference model checkpoint")

    parser.add_argument("--l2sp_weight", type=float, default=0.0,
                        help="PB-04 (c): L2-SP anchor weight — quadratic pull "
                             "of backbone params toward their --init_from "
                             "snapshot (cb0-articulation preservation). "
                             "Typical 1e-4..1e-3; 0 = off.")
    parser.add_argument("--cot_mode", default=None,
                        choices=list(LEGACY_COT_MODES) + list(PLAN_COT_MODES),
                        help="form of the <THINK> block: the full delivery "
                             "prose, EMO/PACE/PITCH tags only, or no reasoning "
                             "(default: prose); plus the P²-CoT plan-bearing "
                             "forms plan / tags+plan / full, which are the same "
                             "thing --plan_tier selects. All read the SAME "
                             "corpus file, so the arms differ only in this.")

    parser.add_argument("--plan_tier", default="none", choices=list(PLAN_TIERS),
                        help="which CoT tiers the <THINK> block carries — the "
                             "C0-C3 factorial as one flag: none (C0, today's "
                             "behaviour), plan (C1, prosodic program only), "
                             "tags+plan (C2), full (C3, prose+tags+plan). "
                             "Sets --cot_mode to the same value; anything but "
                             "'none' needs MVC_ENABLE_PLAN_TOKENS=1.")
    parser.add_argument("--plan_injection", action="store_true",
                        help="consume the plan during the AUDIO block via "
                             "duration-scheduled input injection (§3.2) — the "
                             "attention-free analogue of RALL-E's "
                             "duration-guided masking. Adds a zero-init "
                             "projection, so enabling it on a fresh model is an "
                             "exact no-op until trained.")
    parser.add_argument("--plan_scaffold_dropout", type=float, default=0.3,
                        help="probability of dropping the injection for a whole "
                             "utterance during training (§3.2). Keeps a "
                             "prefix-only consumption path alive, so "
                             "injection-OFF stays a valid MEASUREMENT condition "
                             "(risk R3) instead of the scaffold being the only "
                             "way the plan is read. Inert without "
                             "--plan_injection.")
    parser.add_argument("--splice_plan_vocab", action="store_true",
                        help="on --init_from ONLY: remap a pre-P²-CoT "
                             "checkpoint's embedding/lm_head rows into the "
                             "plan-enabled vocabulary (rows below the plan "
                             "block keep their id, rows above shift up by "
                             "|plan block|, the plan rows keep their fresh "
                             "init). REQUIRED for any warm start under "
                             "MVC_ENABLE_PLAN_TOKENS=1; without it the "
                             "vocab-sized mismatch is a hard error rather than "
                             "a silent re-init of the whole embedding.")
    parser.add_argument("--plan_loss_weight", type=float, default=1.0,
                        help="text-CE weight (λ) on the <PLAN> region "
                             "(the dataset's `plan_weight`). 1.0 = the speech "
                             "weight: the model must AUTHOR the program at "
                             "inference, unlike Tier-A prose at "
                             "--reasoning_weight 0.3.")
    parser.add_argument("--plan_tag_weight", type=float, default=None,
                        help="text-CE weight (λ) on the Tier-B1 EMO/PACE/PITCH "
                             "tags (the dataset's `tag_weight`). Unset = "
                             "inherit --reasoning_weight, which is exactly what "
                             "the 9-arm `tags` runs did, so legacy arms are "
                             "unchanged. The master plan §3.1 specifies 1.0 for "
                             "the tag-bearing plan tiers (C2 tags+plan, C3 "
                             "full) — pass it explicitly there, or those tags "
                             "train at 0.3 with nothing in the log to say so.")
    parser.add_argument("--min_plan_coverage", type=float, default=0.0,
                        help="minimum fraction of corpus rows that must carry a "
                             "usable plan, enforced by the dataset's "
                             "construction-time audit. 0 = report only (today's "
                             "behaviour); the master plan's data gate is 0.95.")
    parser.add_argument("--plan_schedule_from", default="gt",
                        choices=["gt", "self"],
                        help="whose durations schedule the injection during "
                             "training: 'gt' teacher-forces the aligned corpus "
                             "plan (Stage A); 'self' anneals onto the model's "
                             "own emitted plan (Stage-B scheduled sampling "
                             "against exposure bias).")
    parser.add_argument("--style_tier", default="none", choices=["none", "gt"],
                        help="utterance-level style tokens (§3.3): 'gt' puts "
                             "the row's own VQ style tokens in the prefix (the "
                             "GST trick). Requires MVC_NUM_STYLE_TOKENS>0.")
    parser.add_argument("--style_dropout", type=float, default=0.1,
                        help="conditioning-dropout probability on the style "
                             "tokens, so the 'none' inference mode (CoT does "
                             "all the work) stays trained. Inert while "
                             "--style_tier is none.")
    parser.add_argument("--dataset_stage",
                        type=lambda s: s if s == "auto" else int(s),
                        default=None, choices=[1, 2, "auto"],
                        help="Override the sequence-assembly format independent of "
                             "the training stage. E.g. '--stage 2 --dataset_stage 1' "
                             "trains with Stage-2 hyperparameters on reasoning-free "
                             "(Stage-1 format) sequences — the no-CoT control arm, "
                             "differing from the CoT arm ONLY in data.")
    parser.add_argument("--reasoning_weight", type=float, default=0.3,
                        help="Depth Stage-2 CoT: text-CE weight (λ_r) on the "
                             "<THINK> reasoning region (1.0 = speech).")
    parser.add_argument("--default_speaker_id", type=int, default=None,
                        help="Speaker id for rows lacking one (the CoT corpus) — "
                             "holds the voice constant across CoT/no-CoT arms.")
    parser.add_argument("--val_data_path", type=str, default=None,
                        help="Path to validation JSONL data")

    parser.add_argument("--log_level", type=str, default="INFO",
                        choices=["DEBUG", "INFO", "WARNING", "ERROR"],
                        help="Logging level")

    args = parser.parse_args()
    args.pause_codes = None
    if getattr(args, "pause_exit_weight", 1.0) != 1.0:
        if not getattr(args, "pause_codes_file", ""):
            raise SystemExit("--pause_exit_weight != 1.0 needs --pause_codes_file "
                             "(a weight with no code set would be a silent no-op)")
        with open(args.pause_codes_file, encoding="utf-8") as _pf:
            _pc = json.load(_pf)
        _pc = _pc["codes"] if isinstance(_pc, dict) else _pc
        args.pause_codes = torch.tensor(sorted(set(int(c) for c in _pc)), dtype=torch.long)
        print(f"[pause-exit] weight {args.pause_exit_weight} on pause->speech cb0 "
              f"transitions; {args.pause_codes.numel()} pause codes from "
              f"{args.pause_codes_file}", flush=True)

    defaults = STAGE_DEFAULTS[args.stage]
    if args.batch_size is None:
        args.batch_size = defaults["batch_size"]
    if args.grad_accum is None:
        args.grad_accum = defaults["grad_accum"]
    if args.max_steps is None:
        args.max_steps = defaults["max_steps"]
    if args.warmup_steps is None:
        args.warmup_steps = defaults["warmup_steps"]
    if args.lr is None:
        args.lr = defaults["lr"]
    if args.lr_min is None:
        args.lr_min = defaults["lr_min"]
    if args.dataset_stage is None:
        args.dataset_stage = defaults["dataset_stage"]

    _resolve_plan_args(args)
    return args


def _resolve_plan_args(args):
    tier = getattr(args, "plan_tier", "none")

    if tier != "none":
        if args.cot_mode is not None and args.cot_mode != tier:
            raise SystemExit(
                f"--plan_tier {tier} and --cot_mode {args.cot_mode} name "
                f"different conditions. They select the same thing: pass one, "
                f"or pass both with the same value.")
        args.cot_mode = tier
    elif args.cot_mode in PLAN_COT_MODES:
        args.plan_tier = tier = args.cot_mode
    if args.cot_mode is None:
        args.cot_mode = "prose"

    plan_on = tier != "none" or getattr(args, "plan_injection", False)
    if plan_on and not config.ENABLE_PLAN_TOKENS:
        raise SystemExit(
            f"--plan_tier {tier}"
            f"{' --plan_injection' if getattr(args, 'plan_injection', False) else ''} "
            f"needs the plan vocabulary, but MVC_ENABLE_PLAN_TOKENS is off. "
            f"Export MVC_ENABLE_PLAN_TOKENS=1 for this process (and the same "
            f"MVC_NUM_STYLE_TOKENS as the checkpoint) before launching.")
    if getattr(args, "plan_injection", False) and tier == "none":
        raise SystemExit(
            "--plan_injection with --plan_tier none: there is no plan in the "
            "sequence to inject, so the module would train on nothing. Pass a "
            "tier (plan / tags+plan / full).")
    if plan_on and getattr(args, "stage", 1) in (4, 5):
        raise SystemExit(
            f"the plan tier is not wired into the stage-{args.stage} loop "
            f"({'DPO' if args.stage == 4 else 'MTP healing'} forwards no plan "
            f"channel), so it would be silently absent from training. Plan arms "
            f"run in stages 1-3.")
    d = float(getattr(args, "plan_scaffold_dropout", 0.0))
    if not 0.0 <= d < 1.0:
        raise SystemExit(f"--plan_scaffold_dropout must be in [0, 1), got {d} "
                         f"(1.0 would drop the injection on every utterance)")
    if getattr(args, "style_tier", "none") != "none" and config.NUM_STYLE_TOKENS <= 0:
        raise SystemExit(
            f"--style_tier {args.style_tier} needs MVC_NUM_STYLE_TOKENS>0 "
            f"(currently {config.NUM_STYLE_TOKENS}) — there is no style "
            f"vocabulary to emit.")
    s = float(getattr(args, "style_dropout", 0.0))
    if not 0.0 <= s < 1.0:
        raise SystemExit(f"--style_dropout must be in [0, 1), got {s}")
    return args


def main():
    args = parse_args()

    logging.basicConfig(
        level=getattr(logging, args.log_level),
        format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
    )

    rank, local_rank, world_size, device, is_ddp = setup_distributed()

    if args.seed is not None:
        import numpy as _np
        random.seed(args.seed + rank)
        _np.random.seed((args.seed + rank) % (2 ** 32))
        torch.manual_seed(args.seed + rank)
        torch.cuda.manual_seed_all(args.seed + rank)
        if rank == 0:
            logger.info(f"Seed: {args.seed} (python/numpy/torch offset by rank; sampler seed {args.seed})")

    if rank == 0:
        logger.info(f"=== Mamba-CoT-TTS Training Stage {args.stage} ===")
        logger.info(f"Device: {device} | World size: {world_size}")
        logger.info(f"Data: {args.data_path}")
        logger.info(f"Steps: {args.max_steps} | Batch: {args.batch_size} | "
                     f"Grad accum: {args.grad_accum}")
        os.makedirs(args.output_dir, exist_ok=True)

    logger.info("Loading model...")
    model = MambaCoTModel(
        model_name=args.model_name,
        device=str(device),
        dtype=torch.bfloat16,
        mtp_num_heads=args.mtp_num_heads,
        num_crh_heads=args.num_crh_heads,
        crh_codebook_size=args.crh_codebook_size,
        backbone_kind=args.backbone,
    )

    if args.speaker_conditioning:
        model.enable_speaker_conditioning(
            speaker_dim=args.speaker_dim, num_speakers=args.num_speakers,
            input_injection=args.speaker_input_injection,
        )
        if rank == 0:
            logger.info(f"Speaker conditioning enabled (AdaLN v1"
                        f"{'+input-injection' if args.speaker_input_injection else ''}, "
                        f"num_speakers={args.num_speakers}, dim={args.speaker_dim})")

    if getattr(args, "prosody_vec_injection", False):
        model.enable_prosody_conditioning()
        if rank == 0:
            logger.info("Prosody-vector input injection enabled (PB-12; "
                        "zero-init — exact no-op until trained; rows without "
                        "prosody_vec condition on the corpus mean)")

    if (args.stage == 1 or args.use_lora) and not args.full_finetune:
        lora_count = apply_lora(
            model, rank=args.lora_rank, alpha=args.lora_alpha,
            target_modules=args.lora_target_modules, dropout=args.lora_dropout,
        )
        if rank == 0:
            total, trainable = count_parameters(model)
            logger.info(f"LoRA applied ({lora_count} modules). "
                        f"Trainable: {trainable:,} / {total:,} "
                        f"({100*trainable/total:.1f}%)")
    elif args.stage == 1 and args.full_finetune:
        for param in model.parameters():
            param.requires_grad = True

        if not args.fullft_bf16_optim:
            for param in model.parameters():
                if param.dtype == torch.bfloat16:
                    param.data = param.data.float()
            if rank == 0:
                logger.info("Full fine-tune: upcast params to fp32 "
                            "(fp32 master weights; bf16 compute via autocast).")

        total, trainable = count_parameters(model)
        if rank == 0:
            logger.info(f"Full fine-tune: ALL params unfrozen. "
                        f"Trainable: {trainable:,} / {total:,} "
                        f"({100*trainable/total:.1f}%)")
        assert trainable == total, (
            f"--full_finetune expected all params trainable, "
            f"got {trainable:,}/{total:,}"
        )
    elif args.stage >= 2 and not args.use_lora:
        for param in model.parameters():
            param.requires_grad = True
        if not args.fullft_bf16_optim:
            for param in model.parameters():
                if param.dtype == torch.bfloat16:
                    param.data = param.data.float()
            if rank == 0:
                logger.info("Stage>=2 full-param: upcast params to fp32 "
                            "(fp32 master weights + fp32 Adam states; "
                            "bf16 compute via autocast).")
        total, trainable = count_parameters(model)
        assert trainable == total, (
            f"stage>=2 full-param expected all params trainable, "
            f"got {trainable:,}/{total:,}"
        )
        if rank == 0:
            logger.info(f"Stage>=2 full-param: Trainable: {trainable:,} / "
                        f"{total:,} (100.0%)")

    if args.delay_pattern and args.depth_module:
        raise SystemExit("--delay_pattern and --depth_module are mutually exclusive")
    _cb_sizes = ([int(s) for s in args.codebook_sizes.split(",")]
                 if args.codebook_sizes else None)
    if _cb_sizes is not None and len(_cb_sizes) != args.codec_num_levels:
        raise SystemExit(f"--codebook_sizes has {len(_cb_sizes)} entries but "
                         f"--codec_num_levels={args.codec_num_levels}")
    if args.delay_pattern:
        model.enable_delay_pattern(num_levels=args.codec_num_levels,
                                   codebook_size=2048, codebook_sizes=_cb_sizes)
        if rank == 0:
            n = sum(p.numel() for p in model.delay_heads.parameters())
            n += sum(p.numel()
                     for p in model.backbone.backbone.embedding.level_embeds.parameters())
            logger.info(f"Delay pattern enabled (8 levels; +{n:,} new params)")
    if args.depth_module:
        model.enable_depth_module(num_levels=args.codec_num_levels,
                                  codebook_size=2048,
                                  d_depth=args.depth_dim,
                                  depth_layers=args.depth_layers,
                                  depth_feedback=args.depth_feedback,
                depth_arch=args.depth_arch, depth_heads=args.depth_heads,
                level_decay=args.depth_level_decay,
                depth_cond=args.depth_cond,
                                  cross_frame=args.depth_cross_frame,
                                  codebook_sizes=_cb_sizes)
        if rank == 0:
            n = sum(p.numel() for p in model.depth_module.parameters())
            n += sum(p.numel()
                     for p in model.backbone.backbone.embedding.level_embeds.parameters())
            _arch = getattr(args, "depth_arch", "gru").upper()
            logger.info(f"Depth module enabled (d={args.depth_dim} x "
                        f"{args.depth_layers} {_arch}, "
                        f"feedback={args.depth_feedback}, "
                        f"cross_frame={args.depth_cross_frame}; "
                        f"+{n:,} new params)")

    if getattr(args, "plan_injection", False):
        enable = getattr(model, "enable_plan_injection", None)
        if enable is None:
            raise SystemExit(
                "--plan_injection needs model.enable_plan_injection "
                "(P²-CoT B1). This build of model.py does not have it, so the "
                "flag would be accepted and do nothing.")
        enable(**_plan_kwargs_for(
            enable,
            {"scaffold_dropout": float(args.plan_scaffold_dropout),
             "schedule_from": args.plan_schedule_from},
            "model.enable_plan_injection",
            defaults={"schedule_from": "gt"}))

        plan_params = [(n, p) for n, p in model.named_parameters()
                       if _is_plan_param(n)]
        if not plan_params:
            raise RuntimeError(
                "model.enable_plan_injection created no parameter whose name "
                "starts with 'plan_'. That prefix is the wiring contract: the "
                "optimizer's fresh-params group, the checkpoint guards and this "
                "assertion all key off it, so a differently-named module would "
                "silently sit outside every one of them. Rename the module "
                "attribute (e.g. self.plan_input_proj).")
        frozen = [n for n, p in plan_params if not p.requires_grad]
        if frozen and not getattr(args, "flow_only", False):
            raise RuntimeError(
                f"{len(frozen)} plan tensor(s) are frozen right after enabling "
                f"(e.g. {frozen[0]}) — they would never train. This is the "
                f"apply_lora ordering trap: the module must be created AFTER "
                f"the backbone freeze.")
        if rank == 0:
            npl = sum(p.numel() for _, p in plan_params)
            logger.info(
                f"Plan injection enabled (duration-scheduled input injection; "
                f"scaffold_dropout={args.plan_scaffold_dropout}, "
                f"schedule_from={args.plan_schedule_from}; "
                f"+{npl:,} params in {len(plan_params)} tensors, zero-init "
                f"=> exact no-op until trained)")

    if getattr(args, "flow_head", False):
        if not args.depth_module:
            raise SystemExit("--flow_head requires --depth_module")
        from codec import load_codec
        args._flow_codec = load_codec("mimi", device=device)
        model.enable_flow_head(latent_dim=512, cb0_size=2048,
                               d_hidden=args.flow_hidden,
                               n_layers=args.flow_layers,
                               flow_weight=args.flow_weight)
        if rank == 0:
            nf = sum(p.numel() for p in model.flow_head.parameters())
            logger.info(f"Flow head enabled (dim=512, hidden={args.flow_hidden} x "
                        f"{args.flow_layers}, weight={args.flow_weight}; "
                        f"+{nf:,} new params; Mimi decoder FROZEN)")
        if getattr(args, "flow_only", False):
            for n, p in model.named_parameters():
                p.requires_grad = n.startswith("flow_head")
            if rank == 0:
                nt = sum(p.numel() for p in model.parameters() if p.requires_grad)
                logger.info(f"flow_only: froze all but flow_head ({nt:,} trainable)")

    if (getattr(args, "freeze_depth_module", False)
            and getattr(model, "depth_module", None) is not None):
        for p in model.depth_module.parameters():
            p.requires_grad = False
        if rank == 0:
            nf = sum(p.numel() for p in model.depth_module.parameters())
            logger.info(f"freeze_depth_module: {nf:,} depth params FROZEN "
                        f"(acoustic rendering pinned to the init checkpoint)")

    if getattr(args, "hybrid_attention_top_k", 0) > 0:
        replaced = model.enable_hybrid_attention(
            top_k=args.hybrid_attention_top_k)
        if rank == 0:
            na = sum(p.numel()
                     for i in replaced
                     for p in model.backbone.backbone.layers[i].mixer.parameters())
            logger.info(f"Hybrid attention enabled: layers {replaced} -> causal "
                        f"MHA+RoPE ({na:,} fresh params)")

    if args.stage == 5:
        unwrapped_model = model.module if hasattr(model, 'module') else model
        freeze_for_mtp_healing(unwrapped_model)

    ref_model = None
    if args.stage == 4:
        if args.ref_model_path is None:
            logger.error("Stage 4 requires --ref_model_path for reference policy")
            sys.exit(1)
        logger.info(f"Loading reference model from {args.ref_model_path}...")
        ref_model = MambaCoTModel(model_name=args.model_name, device=str(device), dtype=torch.bfloat16)
        load_checkpoint(args.ref_model_path, ref_model, device=str(device))
        ref_model.eval()
        for param in ref_model.parameters():
            param.requires_grad = False

    if rank == 0:
        logger.info("CoT CONDITION: cot_mode=%s | plan_tier=%s | dataset_stage=%s "
                    "— the <THINK> block carries what cot_mode says, not what "
                    "plan_tier says",
                    getattr(args, "cot_mode", None),
                    getattr(args, "plan_tier", "none"),
                    getattr(args, "dataset_stage", None))

    if rank == 0:
        total, trainable = count_parameters(model)
        logger.info(f"Parameters — Total: {total:,} | Trainable: {trainable:,}")

    dataset_stage = args.dataset_stage

    if args.stage == 4:
        dataset = DPODataset(
            data_path=args.data_path,
            tokenizer=model.tokenizer,
            registry=model.token_registry,
            max_seq_len=args.max_seq_len,
        )
        collator = DPODataCollator(pad_token_id=model.token_registry.pad_id)
    else:
        plan_kw = plan_dataset_kwargs(args)
        if args.delay_pattern or args.depth_module:
            from delay_dataset import DelayMimiDataset, DelayCollator
            dataset = DelayMimiDataset(
                data_path=args.data_path,
                cot_mode=getattr(args, "cot_mode", "prose"),
                tokenizer=model.tokenizer,
                registry=model.token_registry,
                max_seq_len=args.max_seq_len,
                cfg_dropout=args.cfg_dropout,
                aligned=args.depth_module,
                stage=dataset_stage,
                reasoning_weight=getattr(args, "reasoning_weight", 0.3),
                audio_continue_weight=getattr(args, "audio_continue_weight", 0.0),
                default_speaker_id=getattr(args, "default_speaker_id", None),
                **_plan_kwargs_for(DelayMimiDataset.__init__, plan_kw,
                                   "DelayMimiDataset", PLAN_DATASET_DEFAULTS),
            )
            collator = DelayCollator(pad_token_id=model.token_registry.pad_id)
        else:
            dataset = MambaCoTTTSDataset(
                data_path=args.data_path,
                tokenizer=model.tokenizer,
                registry=model.token_registry,
                stage=dataset_stage,
                cot_mode=getattr(args, "cot_mode", "prose"),
                max_seq_len=args.max_seq_len,
                cfg_dropout=args.cfg_dropout,
                **_plan_kwargs_for(MambaCoTTTSDataset.__init__, plan_kw,
                                   "MambaCoTTTSDataset", PLAN_DATASET_DEFAULTS),
            )
            collator = MambaCoTDataCollator(
                pad_token_id=model.token_registry.pad_id,
                registry=model.token_registry,
                stage=dataset_stage,
                semantic_weight=args.semantic_weight,
                eos_weight=args.eos_weight,
                level_cycle=args.level_cycle,
            )

    sampler = (DistributedSampler(dataset, seed=args.seed) if args.seed is not None
               else DistributedSampler(dataset)) if is_ddp else None
    loader = DataLoader(
        dataset,
        batch_size=args.batch_size,
        shuffle=(sampler is None),
        sampler=sampler,
        collate_fn=collator,
        num_workers=4,
        pin_memory=True,
        drop_last=True,
    )

    if rank == 0:
        logger.info(f"Dataset: {len(dataset)} samples | "
                     f"Loader: {len(loader)} batches/epoch")

    if getattr(args, "plan_tier", "none") != "none":
        log_plan_stats(dataset, model.token_registry, args, rank=rank,
                       is_ddp=is_ddp)

    if getattr(args, "freeze_speaker", False):
        enc = getattr(model, "speaker_encoder", None)
        if enc is None:
            raise RuntimeError("--freeze_speaker needs --speaker_conditioning")
        for p in enc.parameters():
            p.requires_grad = False

    if getattr(args, "train_cot_embeddings_only", False):
        emb = model.backbone.backbone.embedding
        W = emb.weight
        if float(getattr(args, "weight_decay", 0.0)) != 0.0:
            raise RuntimeError(
                f"--train_cot_embeddings_only requires --weight_decay 0 "
                f"(got {args.weight_decay}): AdamW decays every element "
                f"regardless of gradient, which would move the frozen rows.")
        keep = _cot_embedding_rows(args.data_path, model, args)
        for p in model.parameters():
            p.requires_grad = False
        W.requires_grad = True
        mask = torch.zeros(W.shape[0], 1, dtype=W.dtype, device=W.device)
        mask[sorted(keep)] = 1.0
        W.register_hook(lambda g, _m=mask: g * _m)
        model._f1_rows = sorted(keep)
        if rank == 0:
            logger.info(
                f"F1: backbone FROZEN; training {len(keep)} embedding rows of "
                f"{W.shape[0]} ({100.0 * len(keep) / W.shape[0]:.2f}%) — the "
                f"tokens the THINK region uses")

    if is_ddp:
        model = DDP(model, device_ids=[local_rank])

    unwrapped = model.module if is_ddp else model
    optimizer = build_optimizer(unwrapped, args)
    scheduler = build_scheduler(optimizer, args)

    start_step = 0
    if args.resume_from:
        start_step = load_checkpoint(
            args.resume_from, unwrapped, optimizer, scheduler, device=str(device)
        )
        if rank == 0:
            logger.info(
                f"Resumed from {args.resume_from} at step {start_step} "
                f"(optimizer + scheduler state restored)"
            )
    elif args.init_from:
        load_checkpoint(args.init_from, unwrapped, device=str(device),
                        splice_plan_vocab=bool(getattr(args, "splice_plan_vocab",
                                                       False)),
                        allow_speaker_table_reinit=bool(
                            getattr(args, "allow_speaker_table_reinit", False)))
        if rank == 0:
            logger.info(f"Initialized model weights from {args.init_from} "
                        f"(fresh optimizer/scheduler, step 0)")

    if args.speaker_init_from:
        enc = getattr(unwrapped, "speaker_encoder", None)
        if enc is None:
            raise RuntimeError("--speaker_init_from needs --speaker_conditioning")
        W = enc.embedding.weight
        with torch.no_grad():
            for pair in args.speaker_init_from.split(","):
                dst, src = (int(x) for x in pair.strip().split(":"))
                if not (0 <= dst < W.shape[0] and 0 <= src < W.shape[0]):
                    raise ValueError(f"speaker row out of range in '{pair}' "
                                     f"(table is {W.shape[0]})")
                before = float(W[dst].norm())
                W[dst].copy_(W[src])
                if rank == 0:
                    logger.info(f"speaker warm-start: row {dst} <- row {src} "
                                f"(norm {before:.4f} -> {float(W[dst].norm()):.4f})")

    if getattr(args, "train_cot_embeddings_only", False):
        W = unwrapped.backbone.backbone.embedding.weight
        live = [n for n, p in unwrapped.named_parameters()
                if p.requires_grad and p is not W]
        if live:
            raise RuntimeError(
                f"F1: {len(live)} params outside the embedding are still "
                f"trainable, e.g. {live[:5]}")
        in_opt = [p for g in optimizer.param_groups for p in g["params"]]
        if len(in_opt) != 1 or in_opt[0] is not W:
            raise RuntimeError(
                f"F1: the optimizer holds {len(in_opt)} tensors; it must hold "
                f"exactly the embedding weight")
        if rank == 0:
            logger.info(f"F1 verified: 1 tensor in the optimizer, "
                        f"{len(unwrapped._f1_rows)} rows unmasked")

    if getattr(args, "freeze_speaker", False):
        enc = unwrapped.speaker_encoder
        live = [n for n, p in enc.named_parameters() if p.requires_grad]
        if live:
            raise RuntimeError(f"--freeze_speaker did not take: {live}")
        in_opt = {id(p) for g in optimizer.param_groups for p in g["params"]}
        leaked = [n for n, p in enc.named_parameters() if id(p) in in_opt]
        if leaked:
            raise RuntimeError(f"frozen speaker params are in the optimizer: {leaked}")
        if rank == 0:
            logger.info(f"speaker table FROZEN: {sum(1 for _ in enc.parameters())} "
                        f"tensors, none in the optimizer")

    if getattr(args, "l2sp_weight", 0.0) > 0:
        unwrapped._l2sp_ref = {
            n: p.detach().to("cpu", copy=True).to(torch.bfloat16)
            for n, p in unwrapped.backbone.named_parameters()
        }
        if rank == 0:
            n_ref = len(unwrapped._l2sp_ref)
            logger.info(f"L2-SP anchor snapshot: {n_ref} backbone tensors "
                        f"(bf16 on CPU, ~{sum(t.numel() for t in unwrapped._l2sp_ref.values()) * 2 / 1e9:.1f} GB), "
                        f"weight={args.l2sp_weight}")

    if args.stage == 4:
        dpo_train_loop(model, ref_model, loader, optimizer, scheduler, device, args, rank)
    elif args.stage == 5:
        mtp_healing_loop(model, loader, optimizer, scheduler, device, args, rank)
    else:
        train_loop(model, loader, optimizer, scheduler, device, args, rank,
                   start_step=start_step)

    if is_ddp:
        cleanup_distributed()

    if rank == 0:
        logger.info("Training complete.")

if __name__ == "__main__":
    main()