File size: 119,720 Bytes
9a25493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
"""Position-conditioned diffusion-forcing models with an isolated engine.



The original models.diffusion_forcing_wan module is intentionally not modified.

This module carries its own position-capable engine so direct per-frame root

motion and WAN-packed root conditioning can share identical offline/stream

logic without changing the base DiffForcingWanModel behavior.  Root motion means

only XZ difference plus angular difference; pelvis/root pose remains in the

diffused pose state.

"""

import math
from fractions import Fraction
from pathlib import Path

import numpy as np
import torch
import torch.nn as nn

from .tools.t5 import T5EncoderModel
from .tools.wan_model import WanModel
from .normalization import load_statistics
from .quadratic_loss import load_quadratic_loss_matrix, quadratic_error_sum

EPSILON = 0.05
# Most-negative rope frame index supported by rope_params_with_negative(1024, ...).
# Clamping at this value keeps trim_stream's shifted ids inside the cached range.
ROPE_MIN_INDEX = -1024


class TriangularTimeScheduler:
    def __init__(self, config):
        self.steps = config["steps"]
        self.chunk_size = config["chunk_size"]
        self.random_epsilon = config.get("random_epsilon", 0.00)  # schedule jittering
        self.noise_type = config.get("noise_type", "linear")
        self.sigma_type = config.get("sigma_type", "zero")  # "zero", "memoryless"

        if self.noise_type == "exponential" or self.noise_type == "exponential_rev":
            self.exp_max = config.get("exp_max", 5.0)
        elif self.noise_type == "diffusion":
            self.T = config.get("T", 1000)
            self.beta_start = config.get("beta_start", 0.0001)
            self.beta_end = config.get("beta_end", 0.02)

        if self.sigma_type == "memoryless":
            self.sigma_scale = config.get("sigma_scale", 1.0)
        self.content_len = config.get("content_len", None)
        # True (default): t_max covers the tail ramp-down in place (pairs
        # with extra_len=0). False: original L/c; cover the tail with
        # extra_len padding (typically chunk_size-1) at generate time.
        self.t_max_tail = bool(config.get("t_max_tail", True))
        # For simplicity we require steps to be divisible by chunk_size, so that time windows align well.

    def _t_max_exact(self, valid_len):
        """Exact rational t upper bound (single source of truth).

        t_max_tail: 1 + (L-1)/c so the last frame's band is covered in

        place; otherwise the original L/c (tail covered via extra_len)."""
        if self.t_max_tail:
            return Fraction(valid_len + self.chunk_size - 1, self.chunk_size)
        return Fraction(valid_len, self.chunk_size)

    def get_total_steps(self, seq_len):
        # Exact arithmetic: deriving via float double-rounds and can lose the
        # final step (e.g. steps=30, c=30, L=94).
        total = self.steps * self._t_max_exact(seq_len)
        assert total.denominator == 1, (
            f"inference grid does not close: steps={self.steps} * t_max"
            f"({seq_len}+{self.chunk_size}-1)/{self.chunk_size} is not an "
            f"integer; use steps divisible by chunk_size"
        )
        return int(total)

    def get_t_max(self, valid_len):
        """Float view of _t_max_exact (for continuous t sampling)."""
        return float(self._t_max_exact(valid_len))

    def get_time_steps(self, device, valid_len, current_step):
        """current_step (int) → one t scalar shared by the whole batch,

        returned as a per-sample List. Inference-only (generate / stream);

        training's stratified sampling bypasses this and calls the

        lower-level helpers directly."""
        t = current_step * (1 / self.steps)
        return [torch.tensor(t, device=device) for _ in range(len(valid_len))]

    def get_time_schedules(self, device, valid_len, time_steps, training=False):
        time_schedules = []
        time_schedules_derivative = []
        for i in range(len(valid_len)):
            t = time_steps[i].item()
            current_time_schedules = torch.clamp(
                -torch.arange(valid_len[i], device=device) / self.chunk_size + t,
                min=0.0,
                max=1.0,
            )
            current_time_schedules_derivative = torch.ones_like(
                current_time_schedules
            ) * (1 / self.steps)
            if training:
                current_time_schedules = torch.clamp(
                    current_time_schedules
                    + torch.randn_like(current_time_schedules) * self.random_epsilon,
                    min=0.0,
                    max=1.0,
                )
            time_schedules.append(current_time_schedules)
            time_schedules_derivative.append(current_time_schedules_derivative)
        return time_schedules, time_schedules_derivative

    def get_windows(self, valid_len, time_steps, training=False):
        # Grid t (inference) lands exactly on band boundaries; +eps makes the
        # half-open [0, 1) level convention robust to float rounding there.
        # Continuous t (training) never hits boundaries; eps would only shift
        # the band by eps/chunk_size, so it is skipped.
        eps = 0.0 if training else 0.5 * (1 / (self.steps * self.chunk_size))
        input_start, input_end, output_start, output_end = [], [], [], []
        for i in range(len(time_steps)):
            t = time_steps[i].item()
            start_index = max(
                0,
                math.floor((t - 1) * self.chunk_size + eps) + 1,
            )
            end_index = min(
                valid_len[i],
                math.floor(t * self.chunk_size + eps) + 1,
            )
            # An exact boundary hit at the sequence end gives start == end;
            # the last frame is still in the band, so fall back to [end-1, end).
            start_index = min(start_index, max(end_index - 1, 0))

            if self.content_len is not None:
                input_start.append(max(0, end_index - self.content_len))
            else:
                input_start.append(0)
            input_end.append(end_index)
            output_start.append(start_index)
            output_end.append(end_index)
        return input_start, input_end, output_start, output_end

    def get_noise_levels(self, device, valid_len, time_schedules):
        alpha = []
        dalpha = []
        dlog_alpha = []
        beta = []
        dbeta = []
        dlog_beta = []
        sigma = []
        for i in range(len(valid_len)):
            t = time_schedules[i]
            if self.noise_type == "linear":
                alpha_i = t
                dalpha_i = torch.ones_like(alpha_i)
                dlog_alpha_i = dalpha_i / torch.clamp(alpha_i, min=EPSILON)
                beta_i = 1 - t
                dbeta_i = -torch.ones_like(beta_i)
                dlog_beta_i = dbeta_i / torch.clamp(beta_i, min=EPSILON)
            elif self.noise_type == "exponential":
                # "eps" prediction
                k = self.exp_max
                alpha_i = torch.exp(-k * (1 - t))
                dalpha_i = k * alpha_i
                dlog_alpha_i = k * torch.ones_like(alpha_i)
                beta_i = 1 - alpha_i
                dbeta_i = -dalpha_i
                dlog_beta_i = dbeta_i / torch.clamp(beta_i, min=EPSILON)
            elif self.noise_type == "exponential_rev":
                # "x0" prediction
                k = self.exp_max
                beta_i = torch.exp(-k * t)
                dbeta_i = -k * beta_i
                dlog_beta_i = -k * torch.ones_like(beta_i)
                alpha_i = 1 - beta_i
                dalpha_i = -dbeta_i
                dlog_alpha_i = dalpha_i / torch.clamp(alpha_i, min=EPSILON)
            elif self.noise_type == "diffusion":
                t_rev = 1.0 - t
                beta_rate = (
                    self.beta_start + t_rev * (self.beta_end - self.beta_start)
                ) * self.T
                Gamma = (
                    self.beta_start * t_rev
                    + 0.5 * (self.beta_end - self.beta_start) * t_rev * t_rev
                ) * self.T
                alpha_i = torch.exp(-0.5 * Gamma)
                dalpha_i = 0.5 * beta_rate * alpha_i
                dlog_alpha_i = 0.5 * beta_rate
                beta_i = torch.sqrt(torch.clamp(1 - torch.exp(-Gamma), min=0.0))
                dbeta_i = (
                    -0.5
                    * torch.exp(-Gamma)
                    * beta_rate
                    / torch.clamp(beta_i, min=EPSILON)
                )
                dlog_beta_i = dbeta_i / torch.clamp(beta_i, min=EPSILON)
            else:
                raise ValueError(f"Unknown noise type: {self.noise_type}")
            alpha.append(torch.clamp(alpha_i, min=0.0, max=1.0))
            dalpha.append(dalpha_i)
            dlog_alpha.append(dlog_alpha_i)
            beta.append(torch.clamp(beta_i, min=0.0, max=1.0))
            dbeta.append(dbeta_i)
            dlog_beta.append(dlog_beta_i)
            if self.sigma_type == "zero":
                sigma_i = torch.zeros_like(t)
            elif self.sigma_type == "memoryless":
                if (
                    self.noise_type == "linear"
                    or self.noise_type == "exponential"
                    or self.noise_type == "exponential_rev"
                ):
                    sigma_i = self.sigma_scale * torch.sqrt(
                        torch.clamp(2 * dlog_alpha_i * beta_i, min=0.0)
                    )
                elif self.noise_type == "diffusion":
                    sigma_i = self.sigma_scale * torch.sqrt(
                        torch.clamp(2 * dlog_alpha_i, min=0.0)
                    )
                else:
                    sigma_i = self.sigma_scale * torch.sqrt(
                        torch.clamp(
                            2 * beta_i * (dlog_alpha_i * beta_i - dbeta_i), min=0.0
                        )
                    )
            sigma.append(sigma_i)
        return alpha, dalpha, beta, dbeta, sigma, dlog_alpha, dlog_beta

    def slice_input(self, x, input_start, input_end):
        """Inference-side: slice each sample's current state to the input window."""
        return [x[i][:, input_start[i] : input_end[i], ...] for i in range(len(x))]

    def prepare(self, x, device, valid_len, current_step):
        """Inference-side helper: current_step (int) → all schedule/noise/window

        coefficients sliced to their input/output windows, plus xt sliced to

        input window. Training-side stratified sampling does not go through

        this helper; it calls the lower-level scheduler methods directly.



        Returns dict with keys:

            time_schedules, time_schedules_derivative,

            alpha, dalpha, beta, dbeta, sigma, dlog_alpha, dlog_beta,

            input_start, input_end, output_start, output_end, xt

        """
        time_steps = self.get_time_steps(device, valid_len, current_step)
        time_schedules, time_schedules_derivative = self.get_time_schedules(
            device, valid_len, time_steps,
        )
        alpha, dalpha, beta, dbeta, sigma, dlog_alpha, dlog_beta = \
            self.get_noise_levels(device, valid_len, time_schedules)
        input_start, input_end, output_start, output_end = \
            self.get_windows(valid_len, time_steps)
        xt = self.slice_input(x, input_start, input_end)

        # Slice all coefficients to their respective windows
        batch_size = len(valid_len)
        time_schedules = [time_schedules[i][input_start[i]:input_end[i]] for i in range(batch_size)]
        time_schedules_derivative = [time_schedules_derivative[i][output_start[i]:output_end[i]] for i in range(batch_size)]
        alpha = [alpha[i][output_start[i]:output_end[i]] for i in range(batch_size)]
        dalpha = [dalpha[i][output_start[i]:output_end[i]] for i in range(batch_size)]
        beta = [beta[i][output_start[i]:output_end[i]] for i in range(batch_size)]
        dbeta = [dbeta[i][output_start[i]:output_end[i]] for i in range(batch_size)]
        sigma = [sigma[i][output_start[i]:output_end[i]] for i in range(batch_size)]
        dlog_alpha = [dlog_alpha[i][output_start[i]:output_end[i]] for i in range(batch_size)]
        dlog_beta = [dlog_beta[i][output_start[i]:output_end[i]] for i in range(batch_size)]

        return {
            "time_schedules": time_schedules,
            "time_schedules_derivative": time_schedules_derivative,
            "input_start": input_start,
            "input_end": input_end,
            "output_start": output_start,
            "output_end": output_end,
            "alpha": alpha,
            "dalpha": dalpha,
            "beta": beta,
            "dbeta": dbeta,
            "sigma": sigma,
            "dlog_alpha": dlog_alpha,
            "dlog_beta": dlog_beta,
            "xt": xt,
        }

    # --- Streaming support ---

    def get_committable(self, total_frames):
        """Given total accumulated conditions, return how many frames can be committed.

        Currently, we suppose steps % chunk_size == 0 for simplicity."""
        committable_length = max(0, total_frames - self.chunk_size + 1)
        committable_steps = total_frames * (self.steps // self.chunk_size)
        return committable_length, committable_steps

    def get_step_rollback(self, seq_len):
        """Get the step count to subtract when wrapping the buffer by seq_len.

        Corresponds to how many steps were consumed by seq_len frames."""
        steps = seq_len * (self.steps // self.chunk_size)
        return steps


class T5TextCrossModule(nn.Module):
    """Cross-attention module for T5 text conditioning."""

    def __init__(

        self,

        len=512,

        dim=4096,

        t5_size="xxl",

        checkpoint_path=None,

        tokenizer_path=None,

        drop_out=0.1,

        cross_rope=False,

        input_keys={

            "text": "text",

            "text_end": "text_end",

        },

        cache_encoded=True,

    ):
        assert checkpoint_path is not None and tokenizer_path is not None, (
            "T5 checkpoint and tokenizer paths must be provided."
        )
        super().__init__()
        self.len = len
        self.dim = dim
        self.cross_attn_norm = True
        self.cross_rope = cross_rope
        self.drop_out = drop_out
        self.input_keys = input_keys

        self.text_encoder = T5EncoderModel(
            text_len=len,
            dtype=torch.bfloat16,
            device=torch.device("cpu"),
            checkpoint_path=checkpoint_path,
            tokenizer_path=tokenizer_path,
            shard_fn=None,
            t5_size=t5_size,
        )
        # When False, text_cache is cleared after each encode/get_context call
        # so it doesn't accumulate across training steps (~190KB per caption,
        # OOM on 1M+ unique captions).
        self.cache_encoded = bool(cache_encoded)
        self.text_cache = {}

    def encode(self, text_list, device):
        """Encode text list with cache. Returns List[Tensor]."""
        # Deduplicate uncached texts
        texts_to_encode = []
        for text in text_list:
            if text not in self.text_cache and text not in texts_to_encode:
                texts_to_encode.append(text)

        # Batch encode deduplicated texts
        if texts_to_encode:
            self.text_encoder.model.to(device)
            encoded = self.text_encoder(texts_to_encode, device)
            for text, feature in zip(texts_to_encode, encoded):
                self.text_cache[text] = feature.cpu()

        # Collect from cache
        out = [self.text_cache[text].to(device) for text in text_list]
        if not self.cache_encoded:
            self.text_cache.clear()
        return out

    def get_context(self, x, valid_len, device, param_dtype, training=False):
        """Mask-based unified context.



        Both uniform (text=str) and multi-segment (text=List[str] + text_end)

        inputs are funneled into a single representation: for each sample,

        concatenate all (unique) segments along the K dimension and produce a

        (T_i, S_i) cross-attn mask telling which tokens each query frame may see.



        Returns:

            context: List[Tensor (S_i, D_kv)]   (S_i = num_seg_i * L_text)

            metadata: dict with

              'full_text': List[str] (logging only)

              'cross_attn_mask': List[BoolTensor (T_i, S_i)]

              'text_k_rope_ids' (cross_rope=True): List[Tuple]; f_ids

                  has shape (S_i,) and each segment's L_text tokens share the

                  segment-start-frame value. Null samples carry f_ids=0

                  throughout (single segment, L_text tokens).

              'text_q_rope_ids' (cross_rope=True): List[Tuple|None]

                  per-sample Q rope override; non-None only for null samples

                  (zeros over T_i) so cross-attn phase = 0 (matches K=0).

        """
        text_key = self.input_keys.get("text", "text")
        text_end_key = self.input_keys.get("text_end", "text_end")
        metadata = {}

        text_input = x[text_key] if text_key in x else ["" for _ in range(len(valid_len))]
        text_end_input = x.get(text_end_key, None)

        # Normalize each sample to (text_list, text_end_list, is_null).
        # text_end_list always starts with 0 and ends with valid_len[i].
        norm = []
        for i in range(len(valid_len)):
            is_null = training and np.random.rand() <= self.drop_out
            if is_null:
                norm.append(([""], [0, valid_len[i]], True))
                continue
            if isinstance(text_input[i], list):
                tl = text_input[i]
                assert text_end_input is not None, (
                    "text_end required when text is List[str] (multi-segment)"
                )
                te = [0] + [min(int(t), valid_len[i]) for t in text_end_input[i]]
            else:
                tl = [text_input[i]]
                te = [0, valid_len[i]]
            # Force last segment to extend to valid_len so batch-padding
            # frames (generate passes the batch-max seq_len as every sample's
            # generated_len) get text K coverage instead of an all-False mask.
            # Mirrors PositionCrossModule. No-op for the single-segment
            # branch where te[-1] is already valid_len[i].
            te[-1] = valid_len[i]
            norm.append((tl, te, False))

        # Encode all unique texts (cache-aware dedup)
        texts_to_encode = []
        for tl, _, _ in norm:
            for t in tl:
                if t not in self.text_cache and t not in texts_to_encode:
                    texts_to_encode.append(t)
        if texts_to_encode:
            self.text_encoder.model.to(device)
            encoded = self.text_encoder(texts_to_encode, device)
            for t, f in zip(texts_to_encode, encoded):
                self.text_cache[t] = f.cpu()

        all_context = []
        all_mask = []
        all_text_k_rope_ids = [] if self.cross_rope else None
        text_q_rope_override = [] if self.cross_rope else None
        full_text = []

        for i, (tl, te, is_null) in enumerate(norm):
            seg_tensors = [self.text_cache[t].to(device).to(param_dtype) for t in tl]
            # T5 returns variable-length tokens per caption (trimmed to its
            # actual mask), so segments may have different K-dim lengths.
            # Use cumulative offsets, NOT k * L_text (would mis-stride and
            # leak/lose K tokens whenever segments have different lengths).
            seg_K_lens = [t.size(0) for t in seg_tensors]
            seg_K_offsets = [0]
            for L in seg_K_lens:
                seg_K_offsets.append(seg_K_offsets[-1] + L)
            seg_lens = [t - b for t, b in zip(te[1:], te[:-1])]

            context_i = torch.cat(seg_tensors, dim=0)  # (sum(seg_K_lens), D_kv)
            S_i = context_i.size(0)
            all_context.append(context_i)

            full_text.append(
                " ////////// ".join([f"{u} //dur:{d}" for u, d in zip(tl, seg_lens)])
            )

            # Frame f belongs to segment k where te[k] <= f < te[k+1];
            # mark mask[f, seg_K_offsets[k]:seg_K_offsets[k+1]] = True.
            T_i = valid_len[i]
            mask = torch.zeros(T_i, S_i, dtype=torch.bool, device=device)
            for k, (s_f, e_f) in enumerate(zip(te[:-1], te[1:])):
                s_f = max(0, s_f)
                e_f = min(T_i, e_f)
                if s_f >= e_f:
                    continue
                mask[s_f:e_f, seg_K_offsets[k]:seg_K_offsets[k+1]] = True
            all_mask.append(mask)

            if self.cross_rope:
                # K rope: per-token segment-start-frame; null samples → 0.
                f_ids = torch.empty(S_i, dtype=torch.long, device=device)
                if is_null:
                    f_ids.zero_()
                else:
                    for k, s_f in enumerate(te[:-1]):
                        f_ids[seg_K_offsets[k]:seg_K_offsets[k+1]] = s_f
                all_text_k_rope_ids.append((f_ids, None, None))

                # Q rope override: real → None (use self-attn default arange);
                # null → zeros so phase = Q(0) - K(0) = 0 (rope-off equivalent).
                if is_null:
                    text_q_rope_override.append(
                        (torch.zeros(T_i, dtype=torch.long, device=device), None, None)
                    )
                else:
                    text_q_rope_override.append(None)

        metadata["full_text"] = full_text
        metadata["cross_attn_mask"] = all_mask
        if self.cross_rope:
            metadata["text_k_rope_ids"] = all_text_k_rope_ids
            metadata["text_q_rope_ids"] = text_q_rope_override
        if not self.cache_encoded:
            self.text_cache.clear()
        return all_context, metadata

    def get_null_context(self, real_context, device, param_dtype, valid_len):
        """Build null context with the mask-path convention.



        Each sample gets a single null segment of L_text tokens (encoded("")),

        an all-True (T_i, L_text) mask, K rope = 0, Q rope override = 0.

        This matches the original uniform-null math (phase = 0 = rope-off

        equivalent) and is the only configuration consistent with the mask

        path's unified shape.



        Args:

            real_context: List[Tensor], used to read batch size.

            valid_len: List[int], per-sample query frame count (T_i).



        Returns:

            null_context: List[Tensor (L_text, D_kv)]

            metadata: dict with cross_attn_mask + (when self.cross_rope=True)

                text_k_rope_ids and text_q_rope_ids.

        """
        batch_size = len(real_context)
        encoded_null = self.encode([""], device)[0].to(param_dtype)
        L_text = encoded_null.size(0)
        metadata = {}

        null_context = [encoded_null for _ in range(batch_size)]
        metadata["cross_attn_mask"] = [
            torch.ones(valid_len[i], L_text, dtype=torch.bool, device=device)
            for i in range(batch_size)
        ]

        if self.cross_rope:
            metadata["text_k_rope_ids"] = [
                (torch.zeros(L_text, dtype=torch.long, device=device), None, None)
                for _ in range(batch_size)
            ]
            metadata["text_q_rope_ids"] = [
                (torch.zeros(valid_len[i], dtype=torch.long, device=device), None, None)
                for i in range(batch_size)
            ]
        return null_context, metadata

    # --- Streaming state management (mask path) ---

    def init_stream(self, batch_size):
        # Per-sample list of segments; each entry is (text, start_frame_in_buffer).
        # End frame of segment k is start of segment k+1 (or stream_frames[i]).
        self.stream_segments = [[] for _ in range(batch_size)]
        # Total frames pushed via update_stream (one per call).
        self.stream_frames = [0 for _ in range(batch_size)]

    def update_stream(self, x, device, param_dtype):
        """Add one frame of conditioning. If text matches the latest segment's

        text, extend (implicit via stream_frames++); otherwise start a new

        segment whose start_frame = current frame index.



        Pre-encodes any uncached texts so get_stream_context is cache-only.

        """
        text_key = self.input_keys.get("text", "text")
        text_input = x[text_key]

        # Cache uncached texts (incl. "" so null path can pull from cache too).
        to_encode = []
        for t in list(text_input) + [""]:
            if t not in self.text_cache and t not in to_encode:
                to_encode.append(t)
        if to_encode:
            self.text_encoder.model.to(device)
            encoded = self.text_encoder(to_encode, device)
            for t, f in zip(to_encode, encoded):
                self.text_cache[t] = f.cpu()

        for i in range(len(self.stream_segments)):
            cur_text = text_input[i] if i < len(text_input) else ""
            cur_frame = self.stream_frames[i]
            if (
                len(self.stream_segments[i]) > 0
                and self.stream_segments[i][-1][0] == cur_text
            ):
                # Same as latest segment → frame counter handles extension.
                pass
            else:
                self.stream_segments[i].append((cur_text, cur_frame))
            self.stream_frames[i] += 1

    def get_stream_context(self, start_index, end_index, device, param_dtype):
        """Return mask-form context for buffer window [start_index, end_index).



        Returns:

            contexts: List[Tensor (S_i, D_kv)]

            metadata: {

                'cross_attn_mask': List[BoolTensor (window_len, S_i)]

                'text_k_rope_ids' (cross_rope=True): List[Tuple]

                    f_ids = segment_start_frame (buffer-absolute), repeated L_text

                    times within each segment.

                'text_q_rope_ids' (cross_rope=True): List[None] (real path).

            }

        """
        window_len = end_index - start_index
        contexts = []
        masks = []
        rope_ids = [] if self.cross_rope else None
        text_q_rope_overrides = [] if self.cross_rope else None

        for i in range(len(self.stream_segments)):
            segs = self.stream_segments[i]
            if len(segs) == 0:
                raise RuntimeError(
                    f"sample {i}: stream not initialized (call update_stream first)"
                )
            seg_starts = [s for _, s in segs]
            seg_ends = seg_starts[1:] + [self.stream_frames[i]]

            # Active = segments overlapping [start_index, end_index).
            active = []
            for k, (text, s_start) in enumerate(segs):
                s_end = seg_ends[k]
                if s_end <= start_index or s_start >= end_index:
                    continue
                active.append((text, s_start, s_end))
            if len(active) == 0:
                # Window entirely outside any segment — fall back to null seg
                # covering the whole window so attention still has valid K.
                active = [("", start_index, end_index)]

            seg_tensors = [
                self.text_cache[text].to(device).to(param_dtype)
                for (text, _, _) in active
            ]
            # T5 returns variable-length per-caption tokens; segments may
            # have different K-dim lengths. Use cumulative offsets, not
            # k * L_text (would mis-stride and leak/lose K tokens).
            seg_K_lens = [t.size(0) for t in seg_tensors]
            seg_K_offsets = [0]
            for L in seg_K_lens:
                seg_K_offsets.append(seg_K_offsets[-1] + L)
            context_i = torch.cat(seg_tensors, dim=0)
            S_i = context_i.size(0)
            contexts.append(context_i)

            mask = torch.zeros(window_len, S_i, dtype=torch.bool, device=device)
            for k, (_, s_start, s_end) in enumerate(active):
                local_start = max(0, s_start - start_index)
                local_end = min(window_len, s_end - start_index)
                if local_start < local_end:
                    mask[local_start:local_end, seg_K_offsets[k]:seg_K_offsets[k+1]] = True
            masks.append(mask)

            if self.cross_rope:
                f_ids = torch.empty(S_i, dtype=torch.long, device=device)
                for k, (_, s_start, _) in enumerate(active):
                    f_ids[seg_K_offsets[k]:seg_K_offsets[k+1]] = s_start
                rope_ids.append((f_ids, None, None))
                text_q_rope_overrides.append(None)

        metadata = {"cross_attn_mask": masks}
        if self.cross_rope:
            metadata["text_k_rope_ids"] = rope_ids
            metadata["text_q_rope_ids"] = text_q_rope_overrides
        return contexts, metadata

    def trim_stream(self, trim_len):
        """Shift segment starts back by trim_len; drop segments fully out of buffer.



        Negative starts are kept (clamped to ROPE_MIN_INDEX) so K rope can still

        encode pre-buffer positions via rope_params_with_negative.

        """
        for i in range(len(self.stream_segments)):
            segs = self.stream_segments[i]
            if len(segs) == 0:
                self.stream_frames[i] -= trim_len
                continue
            seg_starts = [s for _, s in segs]
            seg_ends = seg_starts[1:] + [self.stream_frames[i]]
            new_segs = []
            for k, (text, s_start) in enumerate(segs):
                s_end = seg_ends[k]
                new_start = s_start - trim_len
                new_end = s_end - trim_len
                if new_end <= 0:
                    continue  # fully before buffer
                new_segs.append((text, max(new_start, ROPE_MIN_INDEX)))
            self.stream_segments[i] = new_segs
            self.stream_frames[i] -= trim_len


class DiffForcingWanModel(nn.Module):
    def __init__(

        self,

        input_dim=256,

        mean_path=None,

        std_path=None,

        loss_W=None,

        loss_w_coefficient=1.0,

        hidden_dim=1024,

        ffn_dim=2048,

        freq_dim=256,

        num_heads=8,

        num_layers=8,

        time_embedding_scale=1.0,

        attn_type="full",     # "causal" | "full" | "partial"

        rope_channel_split=[1, 0, 0],

        spatial_shape=(1, 1),

        prediction_type="vel",  # "vel", "x0", "eps"

        text_config={

            "len": 512,

            "dim": 4096,

            "cross_rope": False,

        },

        schedule_config={

            "noise_type": "linear",

            "chunk_size": 5,

            "steps": 10,

            "random_epsilon": 0.00,

        },

        cfg_config={

            "text_scale": 5.0,

            "null_scale": -4.0,

        },

        input_keys={

            "feature": "feature",

            "feature_length": "feature_length",

            "text": "text",

            "text_end": "text_end",

        },

        model_input_dim=None,

    ):
        super().__init__()
        self.input_keys = input_keys

        self.mean_path = mean_path
        self.std_path = std_path
        self.input_dim = input_dim
        loss_matrix = load_quadratic_loss_matrix(
            loss_W, loss_w_coefficient, self.input_dim
        )
        if loss_matrix is None:
            self.loss_matrix = None
        else:
            self.register_buffer("loss_matrix", loss_matrix)
        # The diffusion state/output dimensionality remains ``input_dim``.
        # Conditioned variants may expose extra clean channels only to the
        # denoiser (packed root + latent), without diffusing or
        # scoring those channels.
        self.model_input_dim = (
            self.input_dim if model_input_dim is None else int(model_input_dim)
        )
        self.spatial_shape = tuple(spatial_shape)
        self.spatial_size = math.prod(self.spatial_shape)
        self.hidden_dim = hidden_dim
        self.ffn_dim = ffn_dim
        self.freq_dim = freq_dim
        self.num_heads = num_heads
        self.num_layers = num_layers
        self.time_embedding_scale = time_embedding_scale
        assert attn_type in ("causal", "full", "partial"), (
            f"attn_type must be one of 'causal'/'full'/'partial', got {attn_type!r}"
        )
        self.attn_type = attn_type
        self.rope_channel_split = rope_channel_split
        self.prediction_type = prediction_type
        self.cfg_config = cfg_config
        self.schedule_config = schedule_config
        # The multi-window mask is only sound for attn_type='partial': it
        # forces clean x clean to be causal and W_k x clean to be a strict
        # prefix, which contradicts full's "no restriction" and causal's
        # "global sequence causal". Force n_windows=1 under these attn_types
        # so the packed sequence degenerates to a single window and the
        # multi-window mask is bypassed in forward.
        if self.attn_type in ("full", "causal"):
            cfg_n = int(self.schedule_config.get("train_n_windows", 10))
            if cfg_n != 1:
                import warnings
                warnings.warn(
                    f"attn_type={self.attn_type!r} is incompatible with "
                    f"multi-window training (multi-window mask differs from "
                    f"full/causal inference mask). "
                    f"Forcing train_n_windows from {cfg_n} to 1.",
                    stacklevel=2,
                )
                self.schedule_config["train_n_windows"] = 1
        self.time_scheduler = TriangularTimeScheduler(schedule_config)
        # Cross-attention module (text)
        self.text_module = T5TextCrossModule(**text_config)

        mean, std = load_statistics(self.mean_path, self.std_path, input_dim)
        self.register_buffer("mean", mean)
        self.register_buffer("std", std)

        self.model = WanModel(
            patch_size=(1, 1, 1),
            text_len=self.text_module.len,
            text_dim=self.text_module.dim,
            cross_attn_norm=self.text_module.cross_attn_norm,
            cross_rope=self.text_module.cross_rope,
            in_dim=self.model_input_dim,
            dim=self.hidden_dim,
            ffn_dim=self.ffn_dim,
            freq_dim=self.freq_dim,
            out_dim=self.input_dim,
            num_heads=self.num_heads,
            num_layers=self.num_layers,
            window_size=(-1, -1),
            qk_norm=True,
            eps=1e-6,
            causal=(self.attn_type == "causal"),
            rope_channel_split=self.rope_channel_split,
        )
        self.param_dtype = torch.float32

    # ------------------------------------------------------------------
    # Conditioning extension points
    # ------------------------------------------------------------------
    # The base model deliberately keeps these hooks as no-ops.  Position
    # conditioning lives in models/diffusion_forcing_position_wan.py while
    # this class remains bit-for-bit compatible for existing configs.

    def _sample_training_noise(self, x0, inputs, batch_index):
        return torch.randn_like(x0)

    def _get_diffusion_state(self, inputs):
        return inputs["feature"]

    def _prepare_training_condition(

        self, inputs, normalized_feature, valid_len, device

    ):
        return None

    def _condition_training_input(

        self, noisy, clean, condition, batch_index, start, end

    ):
        return noisy

    def _prepare_generation_condition(self, inputs, valid_len, seq_len, device):
        return None

    def _condition_generation_input(self, noisy, condition, starts, ends):
        return noisy

    def _apply_generation_state_condition(self, generated, condition):
        return generated

    def _finalize_generation_output(self, generated, condition, valid_len):
        return generated

    def _generation_output_metadata(self, condition, valid_len):
        return {}

    def _init_stream_condition(

        self, batch_size, buf_len, device, history_position=None,

        history_latent=None,

    ):
        return None

    def _update_stream_condition(self, inputs, device):
        return None

    def _commit_stream_condition(self, index):
        return None

    def _rollback_stream_condition(self, trim_len):
        return None

    def _condition_stream_input(self, noisy, starts, ends):
        return noisy

    def _apply_stream_state_condition(self):
        return None

    def _finalize_stream_output(self, generated, start, end):
        return generated

    def _stream_output_metadata(self, start, end):
        return {}

    def _extract_inputs(self, x):
        """Extract inputs from x using input_keys mapping."""
        inputs = {}
        for internal_key, external_key in self.input_keys.items():
            if external_key in x:
                inputs[internal_key] = x[external_key]
        return inputs

    def preprocess(self, x):
        """Convert last-channel format to channel-first, padding to 4D (C, T, H, W).

        (T, C) -> (C, T, 1, 1)

        (T, H, C) -> (C, T, H, 1)

        (T, H, W, C) -> (C, T, H, W)

        """
        for i in range(len(x)):
            ndim = x[i].ndim
            if ndim == 2:  # (T, C)
                x[i] = x[i].permute(1, 0)[:, :, None, None]
            elif ndim == 3:  # (T, H, C)
                x[i] = x[i].permute(2, 0, 1)[:, :, :, None]
            elif ndim == 4:  # (T, H, W, C)
                x[i] = x[i].permute(3, 0, 1, 2)
        return x

    def postprocess(self, x):
        """Reverse of preprocess: channel-first 4D back to last-channel, stripping padding dims.

        (C, T, 1, 1) -> (T, C)

        (C, T, H, 1) -> (T, H, C)

        (C, T, H, W) -> (T, H, W, C)

        """
        for i in range(len(x)):
            shape = x[i].shape  # (C, T, H, W)
            if shape[2] == 1 and shape[3] == 1:  # (C, T, 1, 1) -> (T, C)
                x[i] = x[i][:, :, 0, 0].permute(1, 0)
            elif shape[3] == 1:  # (C, T, H, 1) -> (T, H, C)
                x[i] = x[i][:, :, :, 0].permute(1, 2, 0)
            else:  # (C, T, H, W) -> (T, H, W, C)
                x[i] = x[i].permute(1, 2, 3, 0)
        return x

    @staticmethod
    def _shift_rope_ids(rope_ids, starts):
        """Subtract starts[i] from f_ids of each tuple, making K rope window-relative

        so cross-attn phase matches Q's default-arange convention even when input

        window doesn't start at frame 0 (content_len truncation).

        Handles per-frame (List[List[Tuple]]), uniform (List[Tuple]), or per-sample

        optional (List[Tuple|None]). None per-sample entries pass through.

        Tuples with f_ids=None (null/CFG placeholders) are also passed through.

        """
        if rope_ids is None:
            return None

        def shift_tuple(tup, s):
            f, h, w = tup
            return ((f - s) if f is not None else None, h, w)

        result = []
        for sample_ids, s in zip(rope_ids, starts):
            if sample_ids is None:
                result.append(None)
            elif isinstance(sample_ids, list):
                result.append([shift_tuple(t, s) for t in sample_ids])
            else:
                result.append(shift_tuple(sample_ids, s))
        return result

    def _patch_seq_len(self, frame_seq_len):
        """Frame-count seq_len → patch-count seq_len."""
        return frame_seq_len * self.spatial_size

    def _expand_schedule_to_patch(self, time_schedules_input):
        """Per-frame schedule → per-patch by repeating each frame value spatial_size times.

        No-op when spatial_size == 1.

        """
        if self.spatial_size == 1:
            return time_schedules_input
        return [s.repeat_interleave(self.spatial_size) for s in time_schedules_input]

    def _expand_context_to_patch(self, context):
        """Per-frame nested context → per-patch by repeating each frame's context spatial_size times.

        Uniform per-sample context (List[Tensor]) is returned unchanged.

        No-op when spatial_size == 1.

        """
        if self.spatial_size == 1:
            return context
        if len(context) > 0 and isinstance(context[0], (list, tuple)):
            return [
                [u for u in sample_ctx for _ in range(self.spatial_size)]
                for sample_ctx in context
            ]
        return context

    def _expand_mask_to_patch(self, masks):
        """Cross-attn mask Q dim (frames) → patch dim by repeating each row

        spatial_size times. K dim untouched. No-op when spatial_size == 1

        or masks is None.

        """
        if masks is None or self.spatial_size == 1:
            return masks
        return [m.repeat_interleave(self.spatial_size, dim=0) for m in masks]

    def _build_partial_mask(self, input_start, input_end, output_start, output_end, device):
        """Per-sample partial-causal attention mask (used when attn_type='partial').



        Layout per sample:

          [input_start, output_start) → history: causal sliding window of size history_len

          [output_start, output_end)  → target : bidirectional + sees all history

        When spatial > 1, repeat-interleave to patch level (F·H·W, F·H·W).

        """
        spatial = self.spatial_size
        masks = []
        for is_, ie_, os_, _oe in zip(input_start, input_end, output_start, output_end):
            L_frame = ie_ - is_
            history_len = os_ - is_
            sliding_window = history_len
            m = torch.zeros(L_frame, L_frame, dtype=torch.bool, device=device)
            for j in range(history_len):
                m[j, max(0, j - sliding_window + 1):j + 1] = True
            m[history_len:, :history_len] = True   # target -> history (fully visible)
            m[history_len:, history_len:] = True   # target x target (bidirectional)
            if spatial > 1:
                m = m.repeat_interleave(spatial, dim=0).repeat_interleave(spatial, dim=1)
            masks.append(m)
        return masks

    def _build_multi_window_attn_mask(self, clean_len, windows, clean_frame_start, device):
        """Self-attn mask for multi-window packed training.



        Packed sequence layout per sample: [clean (clean_len tokens)] + concat over k

        of W_k tokens, where W_k = frames [windows[k][0], windows[k][1]).

        The clean section corresponds to frames

        [clean_frame_start, clean_frame_start + clean_len).

        - clean × clean: causal (clean[i] sees clean[0..i]).

        - clean × W: blocked (clean doesn't depend on noised tokens).

        - W_k × clean[0:s_k - clean_frame_start]: visible — W_k sees every

          clean token in the packed sequence whose absolute frame index is

          less than s_k (strict causal prefix in frame coords).

        - W_k × W_k: bidirectional (denoising chunk full attention).

        - W_k × W_j, j != k: blocked (independent denoising instances).

        When spatial > 1, repeat-interleave to patch level

        (L_frame·spatial, L_frame·spatial).

        """
        sizes = [e - s for s, e in windows]
        total_len = clean_len + sum(sizes)
        mask = torch.zeros(total_len, total_len, dtype=torch.bool, device=device)
        mask[:clean_len, :clean_len] = torch.tril(
            torch.ones(clean_len, clean_len, dtype=torch.bool, device=device)
        )
        cursor = clean_len
        for s, e in windows:
            L = e - s
            s_packed = s - clean_frame_start    # packed col for frame s
            if s_packed > 0:
                mask[cursor:cursor + L, :s_packed] = True
            mask[cursor:cursor + L, cursor:cursor + L] = True
            cursor += L
        if self.spatial_size > 1:
            mask = mask.repeat_interleave(
                self.spatial_size, dim=0
            ).repeat_interleave(self.spatial_size, dim=1)
        return mask

    @staticmethod
    def _gather_for_packed(per_frame_tensor, clean_frame_start, clean_len, windows):
        """Gather rows from a per-frame tensor (T_i, ...) into the packed

        layout [clean section | W_0 | W_1 | ...]. The clean section uses

        rows [clean_frame_start, clean_frame_start + clean_len); each W_k

        uses rows [s_k, e_k). Returns a tensor of shape (packed_len, ...)

        with packed_len = clean_len + sum(L_k).

        """
        parts = []
        if clean_len > 0:
            parts.append(per_frame_tensor[clean_frame_start:clean_frame_start + clean_len])
        for s, e in windows:
            parts.append(per_frame_tensor[s:e])
        return torch.cat(parts, dim=0)

    def _gather_text_q_rope_packed(self, q_rope_ids_full,

                                   clean_frame_starts, clean_lens, all_windows):
        """Gather per-sample text_q_rope_ids tuples (per-sample List[Tuple|None])

        to the packed layout. Per-sample None entries pass through; non-None

        tuples have their f_ids (length T_i) gathered to packed_len following

        the packed layout. h_ids / w_ids are forwarded unchanged.

        """
        if q_rope_ids_full is None:
            return None
        result = []
        for i, item in enumerate(q_rope_ids_full):
            if item is None:
                result.append(None)
                continue
            f_ids, h_ids, w_ids = item
            if f_ids is None:
                result.append(item)
                continue
            gathered = self._gather_for_packed(
                f_ids, clean_frame_starts[i], clean_lens[i], all_windows[i],
            )
            result.append((gathered, h_ids, w_ids))
        return result

    def _build_multi_window_rope_ids(self, clean_len, windows, clean_frame_start, device):
        """Self-attn rope f_ids: per-token position **relative to clean_frame_start**.



        Clean: arange(0, clean_len)。

        W_k: arange(s_k - clean_frame_start, e_k - clean_frame_start)。

        Using positions relative to clean_frame_start matches the inference

        path's default arange(0, e) convention. The Q-K rope phase is still

        the frame difference (the constant offset cancels out after

        subtraction), so the semantics are unchanged.

        """
        parts = [torch.arange(clean_len, device=device, dtype=torch.long)]
        for s, e in windows:
            parts.append(torch.arange(
                s - clean_frame_start, e - clean_frame_start,
                device=device, dtype=torch.long,
            ))
        return (torch.cat(parts), None, None)

    def forward(self, x):
        """Multi-window packed training.



        Per sample (valid_len = T_i), divide t-range [0, (T_i+c-1)/c) into

        `train_n_windows` equal buckets (default 10, configurable via

        schedule_config); each bucket yields one uniform t_k. Windows are

        derived from t_k via `time_scheduler.get_windows`, so the choice of

        n_windows stays consistent with the inference path's formula and

        training cost is independent of sequence length.

        The packed self-attn sequence is

            [clean feature | W_0 | W_1 | ... | W_{N-1}]

        wired together by `_build_multi_window_attn_mask` and per-token rope

        ids from `_build_multi_window_rope_ids`. Cross-attn is uniform (full

        True mask, no rope override, no cross-rope). Loss is per-token MSE

        averaged across all window tokens in the batch.



        When spatial > 1, the metadata (time / attn_mask / cross_attn_mask /

        seq_len) is expanded to patch granularity; rope_ids' f_ids stays at

        frame level since rope_apply broadcasts across H/W internally.

        """
        x = self._extract_inputs(x)
        feature_original = self._get_diffusion_state(x)
        feature_length = x["feature_length"]
        feature_original = (feature_original - self.mean) / self.std
        batch_size = feature_original.shape[0]
        pad_seq_len = feature_original.shape[1]
        device = feature_original.device
        n_windows = int(self.schedule_config.get("train_n_windows", 10))

        valid_len = []
        feature = []
        for i in range(batch_size):
            length = min(feature_length[i].item(), pad_seq_len)
            valid_len.append(length)
            feature.append(feature_original[i, :length, ...])
        feature = self.preprocess(feature)  # list of (C, T_i, 1, 1)
        training_condition = self._prepare_training_condition(
            x, feature_original, valid_len, device,
        )

        # Text context + metadata. cross_attn_mask handles multi-segment Q->K
        # visibility; text_k_rope_ids / text_q_rope_ids carry per-segment rope
        # info when cross_rope=True (None otherwise).
        context, ctx_meta = self.text_module.get_context(
            x, valid_len, device, self.param_dtype, training=True,
        )
        cross_attn_mask_full = ctx_meta["cross_attn_mask"]
        text_k_rope_ids_full = ctx_meta.get("text_k_rope_ids")
        text_q_rope_ids_full = ctx_meta.get("text_q_rope_ids")

        all_xt = []
        all_t = []
        all_rope = []
        all_attn_mask = []
        all_cross_attn_mask = []
        all_targets = []  # per-sample list of (target_tensor, seq_start, seq_end)
        # Per-sample packed layout (used after the loop to gather Q-side text rope
        # and to shift K-side rope into the packed coordinate frame).
        all_clean_frame_starts = []
        all_clean_lens = []
        all_packed_windows = []

        for i in range(batch_size):
            T_i = valid_len[i]
            x0_i = feature[i]
            eps_i = self._sample_training_noise(x0_i, x, i)

            # Split t-range [0, t_max) into n_windows equal buckets; one
            # uniform sample per bucket. Bucket size scales with t_max so the
            # window count stays constant across sequence lengths.
            t_max = self.time_scheduler.get_t_max(T_i)
            bucket = t_max / n_windows
            t_values = [
                torch.tensor(
                    np.random.uniform(k * bucket, (k + 1) * bucket),
                    device=device,
                )
                for k in range(n_windows)
            ]
            input_start_all, _, output_start, output_end = self.time_scheduler.get_windows(
                [T_i] * n_windows, t_values, training=True,
            )
            windows = list(zip(output_start, output_end))

            # t_values is non-decreasing => window starts are
            # monotonically non-decreasing => clean section bounds:
            #   - clean_frame_start = min(input_start): the earliest window's
            #     history start
            #   - clean_frame_end   = max(output_start) = windows[-1][0]
            # Frames beyond clean_frame_end are not read by any W_k, and
            # frames before clean_frame_start are never visible to any W_k
            # (mask gates by frame index), so trimming the buffer to this
            # span is sufficient.
            clean_frame_start = min(input_start_all)
            clean_frame_end = windows[-1][0]
            clean_len = clean_frame_end - clean_frame_start

            # N schedules over T_i frames, one per t_k. Reuses existing
            # scheduler so noise_type / random_epsilon stay consistent with
            # generate.
            schedules, _ = self.time_scheduler.get_time_schedules(
                device, [T_i] * n_windows, t_values, training=True,
            )
            alphas, dalphas, betas, dbetas, _sg, _dla, _dlb = (
                self.time_scheduler.get_noise_levels(
                    device, [T_i] * n_windows, schedules,
                )
            )
            # Clean section: same α·x0 + β·noise noising path as every window,
            # so the latent agrees with the (possibly random_epsilon-jittered)
            # schedule that's also handed to the model as time embedding.
            clean_schedule, _ = self.time_scheduler.get_time_schedules(
                device, [T_i], [torch.tensor(t_max, device=device)],
                training=True,
            )
            clean_alphas, _, clean_betas, _, _, _, _ = (
                self.time_scheduler.get_noise_levels(
                    device, [T_i], clean_schedule,
                )
            )

            # Assemble packed sequence [clean | W_0 | ... | W_{N-1}].
            ca = clean_alphas[0][clean_frame_start:clean_frame_end]
            cb = clean_betas[0][clean_frame_start:clean_frame_end]
            x0_c = x0_i[:, clean_frame_start:clean_frame_end, ...]
            eps_c = eps_i[:, clean_frame_start:clean_frame_end, ...]
            xt_c = (
                x0_c * ca[None, :, None, None]
                + eps_c * cb[None, :, None, None]
            )
            xt_c = self._condition_training_input(
                xt_c,
                x0_c,
                training_condition,
                i,
                clean_frame_start,
                clean_frame_end,
            )
            parts_xt = [xt_c]
            parts_time = [clean_schedule[0][clean_frame_start:clean_frame_end]]
            targets = []
            cursor = clean_len
            for k in range(n_windows):
                s, e = windows[k]
                L = e - s
                a = alphas[k][s:e]
                b = betas[k][s:e]
                x0_w = x0_i[:, s:e, ...]
                eps_w = eps_i[:, s:e, ...]
                xt_w = (
                    x0_w * a[None, :, None, None]
                    + eps_w * b[None, :, None, None]
                )
                xt_w = self._condition_training_input(
                    xt_w, x0_w, training_condition, i, s, e,
                )
                parts_xt.append(xt_w)
                parts_time.append(schedules[k][s:e])

                if self.prediction_type == "vel":
                    da = dalphas[k][s:e]
                    db = dbetas[k][s:e]
                    tgt = (
                        x0_w * da[None, :, None, None]
                        + eps_w * db[None, :, None, None]
                    )
                elif self.prediction_type == "x0":
                    tgt = x0_w
                elif self.prediction_type == "eps":
                    tgt = eps_w
                else:
                    raise ValueError(
                        f"Unknown prediction_type: {self.prediction_type!r}"
                    )
                targets.append((tgt, cursor, cursor + L))
                cursor += L

            xt_i = torch.cat(parts_xt, dim=1)
            assert xt_i.size(0) == self.model_input_dim, (
                f"conditioned denoiser input has {xt_i.size(0)} channels; "
                f"expected model_input_dim={self.model_input_dim}"
            )
            time_i = torch.cat(parts_time) * self.time_embedding_scale

            all_xt.append(xt_i)
            all_t.append(time_i)
            all_rope.append(
                self._build_multi_window_rope_ids(
                    clean_len, windows, clean_frame_start, device,
                )
            )
            all_attn_mask.append(
                self._build_multi_window_attn_mask(
                    clean_len, windows, clean_frame_start, device,
                )
            )
            # Cross-attn mask: gather per-frame rows from cross_attn_mask_full
            # to follow the packed [clean | W_k] layout. Frame-level here;
            # spatial expansion happens after the loop.
            all_cross_attn_mask.append(
                self._gather_for_packed(
                    cross_attn_mask_full[i], clean_frame_start, clean_len, windows,
                )
            )
            all_targets.append(targets)
            all_clean_frame_starts.append(clean_frame_start)
            all_clean_lens.append(clean_len)
            all_packed_windows.append(windows)

        # Expand frame-level time / cross_attn_mask to patch-level
        # (no-ops when spatial_size == 1).
        all_t = self._expand_schedule_to_patch(all_t)
        all_cross_attn_mask = self._expand_mask_to_patch(all_cross_attn_mask)
        # seq_len = max(pad_seq_len, max_packed). In n=1 mode
        # packed_length <= T_i <= pad_seq_len so pad_seq_len dominates and
        # the model sees the same padded geometry as the inference path.
        # With n>1 (partial only) packed can exceed pad_seq_len; the max
        # keeps the upper bound correct.
        seq_len_for_model = self._patch_seq_len(
            max(pad_seq_len, max(xt.size(1) for xt in all_xt))
        )
        text_seq_len = max(int(ctx.size(0)) for ctx in context)

        # Cross-attn rope (only active when cross_rope=True): K rope encodes
        # per-token segment-start-frame in absolute coords, Q rope override
        # exists for null samples (zeros over T_i). Both must be expressed in
        # the same packed coordinate frame as self-attn rope_ids (which is
        # relative to clean_frame_start), so:
        #   - K rope: shift by clean_frame_start (subtract) per sample
        #   - Q rope: gather per-frame f_ids to packed layout, then shift
        text_k_rope_ids = self._shift_rope_ids(
            text_k_rope_ids_full, all_clean_frame_starts,
        )
        text_q_rope_ids = self._gather_text_q_rope_packed(
            text_q_rope_ids_full, all_clean_frame_starts, all_clean_lens,
            all_packed_windows,
        )
        text_q_rope_ids = self._shift_rope_ids(
            text_q_rope_ids, all_clean_frame_starts,
        )

        # The multi-window mask is only meaningful for attn_type='partial'.
        # Under full/causal (where __init__ already forced n_windows=1), the
        # packed sequence is a single window, so we let WanModel apply
        # default full attention or its internal causal mask.
        final_attn_mask = all_attn_mask if self.attn_type == "partial" else None
        predicted_result = self.model(
            all_xt,
            all_t,
            context,
            seq_len_for_model,
            attn_mask=final_attn_mask,
            text_k_rope_ids=text_k_rope_ids,
            text_q_rope_ids=text_q_rope_ids,
            cross_attn_mask=all_cross_attn_mask,
            text_seq_len=text_seq_len,
            y=None,
            rope_ids=all_rope,
        )

        # Per-sample mean -> batch mean. Within each sample sum SE across
        # all W_k tokens and divide by the token count to get per-sample
        # MSE, then average across the batch.
        loss = 0.0
        for i in range(batch_size):
            pred_i = predicted_result[i]
            sample_se_sum = pred_i.new_zeros(())
            sample_n = 0
            for tgt, s, e in all_targets[i]:
                error = pred_i[:, s:e, ...] - tgt
                if self.loss_matrix is None:
                    sample_se_sum = sample_se_sum + (error ** 2).sum()
                else:
                    sample_se_sum = sample_se_sum + quadratic_error_sum(
                        error, self.loss_matrix
                    )
                sample_n += error.numel()
            loss = loss + sample_se_sum / max(sample_n, 1)
        loss = loss / batch_size
        return {"total": loss, "mse": loss}

    def generate(self, x):
        """

        Generation - Diffusion Forcing inference

        Uses triangular noise schedule, progressively generating from left to right



        Generation process:

        1. Start from t=0, gradually increase t

        2. Each t corresponds to a noise schedule: clean on left, noisy on right, gradient in middle

        3. After each denoising step, t increases slightly and continues

        """
        x = self._extract_inputs(x)
        feature_length = x["feature_length"]  # (B,)
        batch_size = len(feature_length)
        # extra_len pads the rollout with disposable frames so real frames
        # finish under full-width windows; 0 relies on the tail ramp-down.
        extra_len = self.schedule_config.get("extra_len", 0)
        seq_len = max(feature_length).item() + extra_len
        if (not getattr(self.time_scheduler, "t_max_tail", True)
                and extra_len < self.time_scheduler.chunk_size - 1):
            import warnings
            warnings.warn(
                f"t_max_tail=False with extra_len={extra_len} < chunk_size-1="
                f"{self.time_scheduler.chunk_size - 1}: the last frames of "
                f"every generated sequence will not finish denoising",
                stacklevel=2,
            )
        device = next(self.parameters()).device
        valid_len = []
        for i in range(batch_size):
            length = min(feature_length[i].item(), seq_len)
            valid_len.append(length)
        generated_len = [seq_len for _ in range(batch_size)]
        generation_condition = self._prepare_generation_condition(
            x, valid_len, seq_len, device,
        )

        # Initialize entire sequence as pure noise
        generated = torch.randn(
            batch_size, seq_len, *self.spatial_shape, self.input_dim, device=device
        )
        generated = [generated[i] for i in range(batch_size)]
        generated = self.preprocess(generated)
        self._apply_generation_state_condition(generated, generation_condition)

        # Precompute real text context (null built per-step from window-sliced real).
        text_context, metadata = self.text_module.get_context(
            x,
            generated_len,
            device,
            self.param_dtype,
            training=False,
        )
        full_text = metadata["full_text"]
        cross_attn_mask_full = metadata["cross_attn_mask"]
        text_k_rope_ids_full = metadata.get("text_k_rope_ids")

        total_steps = self.time_scheduler.get_total_steps(seq_len)
        # Progressively advance from t=0 to t=max_t
        for step in range(total_steps):
            s = self.time_scheduler.prepare(
                generated, device, generated_len, current_step=step,
            )
            time_schedules = s["time_schedules"]
            time_schedules_derivative = s["time_schedules_derivative"]
            alpha = s["alpha"]
            dalpha = s["dalpha"]
            beta = s["beta"]
            dbeta = s["dbeta"]
            sigma = s["sigma"]
            dlog_alpha = s["dlog_alpha"]
            dlog_beta = s["dlog_beta"]
            input_start_index = s["input_start"]
            input_end_index = s["input_end"]
            output_start_index = s["output_start"]
            output_end_index = s["output_end"]
            xt = s["xt"]
            xt = self._condition_generation_input(
                xt, generation_condition, input_start_index, input_end_index,
            )

            # time_schedules already sliced to input window by prepare()
            time_schedules_input = [
                time_schedules[i] * self.time_embedding_scale
                for i in range(batch_size)
            ]

            # Slice cross_attn_mask Q dim to input window (K untouched).
            window_cross_attn_mask = [
                cross_attn_mask_full[i][input_start_index[i]:input_end_index[i], :]
                for i in range(batch_size)
            ]
            # Shift K rope to window-relative coords (lockstep with forward path).
            window_text_k_rope = self._shift_rope_ids(text_k_rope_ids_full, input_start_index)

            # CFG: text_scale * pred_text + null_scale * pred_null
            ts_patch = self._expand_schedule_to_patch(time_schedules_input)
            patch_seq_len = self._patch_seq_len(seq_len)
            attn_mask = (
                self._build_partial_mask(input_start_index, input_end_index,
                                         output_start_index, output_end_index, device)
                if self.attn_type == "partial" else None
            )
            # Build null context matching window-sliced real shape.
            window_lens = [
                input_end_index[i] - input_start_index[i] for i in range(batch_size)
            ]
            window_null_context, window_null_meta = self.text_module.get_null_context(
                text_context, device, self.param_dtype, valid_len=window_lens,
            )
            window_null_mask = window_null_meta["cross_attn_mask"]
            window_null_text_k_rope = window_null_meta.get("text_k_rope_ids")
            window_null_text_q_rope = window_null_meta.get("text_q_rope_ids")

            # Doubled batch: [text..., null...]. One model.forward, split outputs.
            combined_ctx = self._expand_context_to_patch(text_context + window_null_context)
            combined_cross_attn_mask = self._expand_mask_to_patch(
                window_cross_attn_mask + window_null_mask
            )
            if window_text_k_rope is not None and window_null_text_k_rope is not None:
                combined_text_k_rope = window_text_k_rope + window_null_text_k_rope
            else:
                combined_text_k_rope = None
            # Real branch has no Q-override (cross falls back to self_attn rope);
            # null branch emits one when cross_rope=True. Concat [None]*B for real.
            if window_null_text_q_rope is not None:
                combined_text_q_rope = [None] * batch_size + list(window_null_text_q_rope)
            else:
                combined_text_q_rope = None
            combined_mask = attn_mask + attn_mask if attn_mask is not None else None

            combined_text_seq_len = max(
                int(c.size(0)) for c in (text_context + window_null_context)
            )
            pred_all = self.model(
                xt + xt,
                ts_patch + ts_patch,
                combined_ctx,
                patch_seq_len,
                attn_mask=combined_mask,
                text_k_rope_ids=combined_text_k_rope,
                text_q_rope_ids=combined_text_q_rope,
                cross_attn_mask=combined_cross_attn_mask,
                text_seq_len=combined_text_seq_len,
                y=None,
            )
            pred_text = pred_all[:batch_size]
            pred_null = pred_all[batch_size:]
            predicted_result = [
                self.cfg_config["text_scale"] * pt + self.cfg_config["null_scale"] * pn
                for pt, pn in zip(pred_text, pred_null)
            ]

            # All noise coefficients already sliced to output window by prepare()
            for i in range(batch_size):
                os, oe = output_start_index[i], output_end_index[i]
                pred_os = os - input_start_index[i]
                pred_oe = oe - input_start_index[i]
                predicted_result_i = predicted_result[i][:, pred_os:pred_oe, ...]
                generated_i = generated[i][:, os:oe, ...]
                dt = time_schedules_derivative[i][None, :, None, None]
                alpha_i = alpha[i][None, :, None, None]
                dalpha_i = dalpha[i][None, :, None, None]
                beta_i = beta[i][None, :, None, None]
                dbeta_i = dbeta[i][None, :, None, None]
                sigma_i = sigma[i][None, :, None, None]
                dlog_alpha_i = dlog_alpha[i][None, :, None, None]
                dlog_beta_i = dlog_beta[i][None, :, None, None]
                if self.prediction_type == "vel":
                    vel = predicted_result_i
                elif self.prediction_type == "x0":
                    vel = (
                        predicted_result_i * (-dlog_beta_i * alpha_i + dalpha_i)
                        + generated_i * dlog_beta_i
                    )
                elif self.prediction_type == "eps":
                    vel = (
                        predicted_result_i * (-dlog_alpha_i * beta_i + dbeta_i)
                        + generated_i * dlog_alpha_i
                    )
                st = (vel - generated_i * dlog_alpha_i) / (
                    (beta_i * dlog_alpha_i - dbeta_i) * beta_i
                )
                generated[i][:, os:oe, ...] += (
                    vel * dt
                    + st * 0.5 * sigma_i**2 * dt
                    + sigma_i * torch.sqrt(dt) * torch.randn_like(generated_i)
                )
            self._apply_generation_state_condition(generated, generation_condition)

        generated = self.postprocess(generated)  # list of (T, C)
        y_hat_out = []
        for i in range(batch_size):
            single_generated = generated[i][: valid_len[i], :] * self.std + self.mean
            y_hat_out.append(single_generated)
        y_hat_out = self._finalize_generation_output(
            y_hat_out, generation_condition, valid_len,
        )
        out = {}
        out["generated"] = y_hat_out
        out["text"] = full_text
        out.update(self._generation_output_metadata(generation_condition, valid_len))

        return out

    def init_generated(

        self,

        seq_len,

        batch_size=1,

        schedule_config={},

        history_latent=None,

        history_position=None,

    ):
        """Initialize streaming generation state.



        Args:

            seq_len: Model window size (how many frames WanModel processes per step).

            schedule_config: Optional schedule config overrides.

            history_latent: optional list of (h, ...) un-normalized latent tensors per sample

                            (typically vae.encode(motion) output). If provided:

                              - normalize via self.mean/self.std

                              - apply triangular noise matching the schedule at t=1

                                (ts[i] = clamp(1 - i/chunk_size, 0, 1) for i in [0, h))

                              - inject into buffer[:, :h, ...]

                              - set current_step = steps so wavefront starts just past history

                            If None: cold start (buffer all noise, current_step=0).



        Buffer is 2*seq_len. Model window is always buffer[0:seq_len].

        When conditions overflow seq_len, shift buffer by seq_len and restart.

        """
        self.schedule_config.update(schedule_config)
        content_len = self.schedule_config.get("content_len", None)
        if content_len is None:
            self.schedule_config["content_len"] = seq_len
        else:
            self.schedule_config["content_len"] = min(seq_len, content_len)
        self.time_scheduler = TriangularTimeScheduler(self.schedule_config)

        self.batch_size = batch_size
        self.seq_len = seq_len
        self.buf_len = seq_len * 2
        self.current_step = 0
        self.current_commit = 0
        self.condition_frames = 0

        device = next(self.parameters()).device
        # Initialize entire buffer as pure noise
        generated = torch.randn(
            batch_size, self.buf_len, *self.spatial_shape, self.input_dim, device=device
        )
        generated = [generated[i] for i in range(batch_size)]
        self.generated = self.preprocess(generated)

        # Initialize streaming state for cross module
        self.text_module.init_stream(self.batch_size)
        self._init_stream_condition(
            batch_size,
            self.buf_len,
            device,
            history_position=history_position,
            history_latent=history_latent,
        )

        # Optional: inject GT history with triangular re-noising matching schedule at t=1
        if history_latent is not None:
            h = history_latent[0].shape[0]
            chunk_size = self.time_scheduler.chunk_size
            # Triangular ts: ts[0]=1 (clean), ts[chunk_size-1]=1/chunk_size (most noisy),
            # ts[i>=chunk_size]=0 (full noise — beyond effective transition zone)
            ts_history = torch.clamp(
                1 - torch.arange(h, device=device, dtype=torch.float32) / chunk_size,
                min=0.0, max=1.0,
            )
            alpha_h, _, beta_h, _, _, _, _ = self.time_scheduler.get_noise_levels(
                device, [h], [ts_history],
            )
            alpha_b = alpha_h[0][None, :, None, None]
            beta_b = beta_h[0][None, :, None, None]
            for i in range(batch_size):
                z = (history_latent[i].to(device) - self.mean) / self.std
                z = self.preprocess([z])[0]
                noise = torch.randn_like(z)
                z_noised = z * alpha_b + noise * beta_b
                self.generated[i][:, :h, ...] = z_noised
            # Conditioned subclasses may need to restore channels that must
            # remain clean after the base model's triangular history re-noising.
            self._apply_stream_state_condition()
            # t starts from 1 → wavefront just past the history transition zone
            self.current_step = self.time_scheduler.steps

    def _rollback(self):
        """Shift buffer by seq_len when conditions overflow the window."""
        for i in range(self.batch_size):
            self.generated[i][:, : self.seq_len, ...] = self.generated[i][
                :, self.seq_len :, ...
            ].clone()
            self.generated[i][:, self.seq_len :, ...] = torch.randn_like(
                self.generated[i][:, self.seq_len :, ...]
            )
        self.current_step -= self.time_scheduler.get_step_rollback(self.seq_len)
        self.condition_frames -= self.seq_len
        self.current_commit -= self.seq_len
        self.text_module.trim_stream(self.seq_len)
        self._rollback_stream_condition(self.seq_len)

    @torch.no_grad()
    def stream_generate_step(self, x):
        """

        Streaming generation step. Each call provides 1 frame of conditions.

        The scheduler determines committable frames from accumulated conditions.



        Returns:

            dict with "generated": list of one (N, C) tensor, or [] if nothing to commit.

        """
        x = self._extract_inputs(x)
        device = next(self.parameters()).device
        self.generated = [g.to(device) for g in self.generated]

        # 1. Update conditions (1 frame per call)
        self._update_stream_condition(x, device)
        self.text_module.update_stream(x, device, self.param_dtype)
        self.condition_frames += 1

        # 2. Rollback if conditions overflow the window
        if self.condition_frames > self.buf_len:
            self._rollback()
        self._commit_stream_condition(self.condition_frames - 1)

        # 3. Determine how many frames can be committed
        committable_length, committable_steps = self.time_scheduler.get_committable(
            self.condition_frames
        )
        while self.current_step < committable_steps:
            s = self.time_scheduler.prepare(
                self.generated, device, [self.buf_len] * self.batch_size,
                current_step=self.current_step,
            )
            time_schedules = s["time_schedules"]
            time_schedules_derivative = s["time_schedules_derivative"]
            alpha = s["alpha"]
            dalpha = s["dalpha"]
            beta = s["beta"]
            dbeta = s["dbeta"]
            sigma = s["sigma"]
            dlog_alpha = s["dlog_alpha"]
            dlog_beta = s["dlog_beta"]
            is_ = s["input_start"]
            ie_ = s["input_end"]
            os_ = s["output_start"]
            oe_ = s["output_end"]
            xt = s["xt"]
            xt = self._condition_stream_input(xt, is_, ie_)

            # time_schedules already sliced to input window by prepare()
            time_schedules_input = [
                time_schedules[0] * self.time_embedding_scale
            ] * self.batch_size

            # CFG: batch text + null in one forward pass
            text_context, text_meta = self.text_module.get_stream_context(
                is_[0], ie_[0], device, self.param_dtype,
            )
            text_mask = text_meta["cross_attn_mask"]
            text_k_rope = text_meta.get("text_k_rope_ids")
            # Q-rope override (per-sample List[Tuple|None]): real-text stream
            # entries are None and pass through _shift unchanged.
            text_q_rope = text_meta.get("text_q_rope_ids")

            window_len = ie_[0] - is_[0]
            null_context, null_meta = self.text_module.get_null_context(
                text_context, device, self.param_dtype,
                valid_len=[window_len] * self.batch_size,
            )
            null_mask = null_meta["cross_attn_mask"]
            null_text_k_rope = null_meta.get("text_k_rope_ids")
            null_text_q_rope = null_meta.get("text_q_rope_ids")

            ts_patch = self._expand_schedule_to_patch(time_schedules_input)
            ctx_patch = self._expand_context_to_patch(text_context + null_context)
            cross_mask_patch = self._expand_mask_to_patch(text_mask + null_mask)
            # K-rope ids: real is buffer-absolute → shift to window-relative; null
            # comes from get_null_context already window-relative (zeros).
            text_k_rope = self._shift_rope_ids(
                text_k_rope, [is_[0]] * self.batch_size,
            )
            text_q_rope = self._shift_rope_ids(
                text_q_rope, [is_[0]] * self.batch_size,
            )
            if text_k_rope is not None and null_text_k_rope is not None:
                text_k_rope_patch = text_k_rope + null_text_k_rope
            else:
                text_k_rope_patch = None
            # Q-override: real samples carry None (cross uses self_attn rope),
            # null samples carry zeros tuple (phase=0 vs K=0).
            if text_q_rope is not None or null_text_q_rope is not None:
                text_q_rope_patch = (
                    (text_q_rope if text_q_rope is not None
                     else [None] * self.batch_size)
                    + (null_text_q_rope if null_text_q_rope is not None
                       else [None] * self.batch_size)
                )
            else:
                text_q_rope_patch = None

            if self.attn_type == "partial":
                masks = self._build_partial_mask(is_, ie_, os_, oe_, device)
                attn_mask = masks + masks   # CFG doubled batch (text + null), same mask
            else:
                attn_mask = None
            stream_text_seq_len = max(
                int(c.size(0)) for c in (text_context + null_context)
            )
            pred_all = self.model(
                xt + xt,
                ts_patch + ts_patch,
                ctx_patch,
                self._patch_seq_len(self.seq_len),
                attn_mask=attn_mask,
                text_k_rope_ids=text_k_rope_patch,
                text_q_rope_ids=text_q_rope_patch,
                cross_attn_mask=cross_mask_patch,
                text_seq_len=stream_text_seq_len,
                y=None,
            )
            pred_text = pred_all[: self.batch_size]
            pred_null = pred_all[self.batch_size :]
            predicted_result = [
                self.cfg_config["text_scale"] * pt + self.cfg_config["null_scale"] * pn
                for pt, pn in zip(pred_text, pred_null)
            ]

            # All noise coefficients already sliced to output window by prepare()
            os_idx, oe_idx = os_[0], oe_[0]
            pred_os_idx = os_idx - is_[0]
            pred_oe_idx = oe_idx - is_[0]
            dt = time_schedules_derivative[0][None, :, None, None]
            alpha_i = alpha[0][None, :, None, None]
            dalpha_i = dalpha[0][None, :, None, None]
            beta_i = beta[0][None, :, None, None]
            dbeta_i = dbeta[0][None, :, None, None]
            sigma_i = sigma[0][None, :, None, None]
            dlog_alpha_i = dlog_alpha[0][None, :, None, None]
            dlog_beta_i = dlog_beta[0][None, :, None, None]
            for i in range(self.batch_size):
                predicted_result_i = predicted_result[i][:, pred_os_idx:pred_oe_idx, ...]
                generated_i = self.generated[i][:, os_idx:oe_idx, ...]
                if self.prediction_type == "vel":
                    vel = predicted_result_i
                elif self.prediction_type == "x0":
                    vel = (
                        predicted_result_i * (-dlog_beta_i * alpha_i + dalpha_i)
                        + generated_i * dlog_beta_i
                    )
                elif self.prediction_type == "eps":
                    vel = (
                        predicted_result_i * (-dlog_alpha_i * beta_i + dbeta_i)
                        + generated_i * dlog_alpha_i
                    )
                st = (vel - generated_i * dlog_alpha_i) / (
                    (beta_i * dlog_alpha_i - dbeta_i) * beta_i
                )
                self.generated[i][:, os_idx:oe_idx, ...] += (
                    vel * dt
                    + st * 0.5 * sigma_i**2 * dt
                    + sigma_i * torch.sqrt(dt) * torch.randn_like(generated_i)
                )
            self._apply_stream_state_condition()
            self.current_step += 1

        # 5. Extract newly committed frames
        if self.current_commit < committable_length:
            commit_start = self.current_commit
            output = [
                self.generated[i][:, commit_start:committable_length, ...]
                for i in range(self.batch_size)
            ]
            output = self.postprocess(output)
            output = [o * self.std + self.mean for o in output]
            output = self._finalize_stream_output(
                output, commit_start, committable_length,
            )
            self.current_commit = committable_length
            result = {"generated": output}
            result.update(
                self._stream_output_metadata(commit_start, committable_length)
            )
            return result
        else:
            empty = [
                torch.zeros(self.input_dim, 0, *self.spatial_shape, device=device)
                for _ in range(self.batch_size)
            ]
            empty = self.postprocess(empty)
            empty = self._finalize_stream_output(
                empty, self.current_commit, self.current_commit,
            )
            result = {"generated": empty}
            result.update(
                self._stream_output_metadata(
                    self.current_commit, self.current_commit,
                )
            )
            return result

# Position-specific root packing and model contracts.
from .vae_wan_position import (
    POSITION_ROOT_DIMS,
    WAN_ROOT_GROUP_SIZE,
    pack_root_condition,
    resolve_position_representation,
)

# Channels that are identically zero in the ground truth have std ~0.  Dividing
# by that std turns float noise into an O(1) training target, so below this
# floor the channel is centred but not scaled.  The floor sits in the measured
# gap observed in the supported motion statistics.
STD_NORMALIZATION_FLOOR = 1e-3


def guard_std(std, floor=STD_NORMALIZATION_FLOOR):
    """Replace near-zero standard deviations with 1 (i.e. do not divide)."""
    std = torch.as_tensor(std).float()
    return torch.where(std < floor, torch.ones_like(std), std)


def _to_batched_tensor(value, device, dtype=torch.float32):
    if isinstance(value, (list, tuple)):
        tensors = [torch.as_tensor(v, device=device, dtype=dtype) for v in value]
        if not tensors:
            raise ValueError("position condition cannot be empty")
        if tensors[0].ndim == 0:
            if any(tensor.ndim != 0 for tensor in tensors):
                raise ValueError("position list mixes scalar and non-scalar values")
            return torch.stack(tensors, dim=0)
        if tensors[0].ndim == 1:
            return torch.stack(tensors, dim=0)
        lengths = {int(t.size(0)) for t in tensors}
        if len(lengths) != 1:
            raise ValueError(
                "variable-length position lists must be padded/collated before use"
            )
        return torch.stack(tensors, dim=0)
    return torch.as_tensor(value, device=device, dtype=dtype)


class _PositionStreamBufferMixin:
    """Fixed-size, rollback-safe raw condition buffer for stream generation."""

    stream_condition_dim = 0

    def _assert_stream_schedule(self):
        """Subclass hook: validated again after init_generated rebuilds it."""
        return None

    def init_generated(self, seq_len, *args, **kwargs):
        # The base rebuilds time_scheduler from a caller-supplied
        # schedule_config, so schedule invariants must be re-checked.
        result = super().init_generated(seq_len, *args, **kwargs)
        self._assert_stream_schedule()
        return result

    def _init_stream_condition(

        self,

        batch_size,

        buf_len,

        device,

        history_position=None,

        history_latent=None,

    ):
        self._stream_position = torch.zeros(
            batch_size,
            buf_len,
            self.stream_condition_dim,
            device=device,
            dtype=torch.float32,
        )
        self._stream_position_valid = torch.zeros(
            batch_size, buf_len, device=device, dtype=torch.bool
        )
        self._pending_stream_position = None

        if history_position is not None and history_latent is None:
            raise ValueError("history_position requires matching history_latent")
        history_length = None
        if history_latent is not None:
            if len(history_latent) != batch_size:
                raise ValueError(
                    f"history latent batch={len(history_latent)}, expected {batch_size}"
                )
            history_lengths = [int(torch.as_tensor(v).size(0)) for v in history_latent]
            if len(set(history_lengths)) != 1:
                raise ValueError(
                    f"all history latent lengths must match, got {history_lengths}"
                )
            history_length = history_lengths[0]
            if history_length > buf_len:
                raise ValueError(
                    f"history latent length={history_length} exceeds buffer={buf_len}"
                )

        if history_position is None and history_latent is not None:
            history_position = self._position_from_history(history_latent)
        if history_position is not None:
            history = self._coerce_stream_history(history_position, device)
            if history.size(0) != batch_size:
                raise ValueError(
                    f"history position batch={history.size(0)}, expected {batch_size}"
                )
            if history.size(1) > buf_len:
                raise ValueError(
                    f"history position length={history.size(1)} exceeds buffer={buf_len}"
                )
            if history_length is not None and history.size(1) != history_length:
                raise ValueError(
                    f"history position length={history.size(1)} does not match "
                    f"history latent length={history_length}"
                )
            self._stream_position[:, : history.size(1)] = history
            self._stream_position_valid[:, : history.size(1)] = True

    def _position_from_history(self, history_latent):
        return None

    def _coerce_stream_history(self, position, device):
        value = _to_batched_tensor(position, device)
        if value.ndim == 2:
            value = value.unsqueeze(0)
        if value.ndim != 3 or value.size(-1) != self.stream_condition_dim:
            raise ValueError(
                f"history position must be (B,T,{self.stream_condition_dim}), "
                f"got {tuple(value.shape)}"
            )
        if not torch.isfinite(value).all():
            raise ValueError("history position contains NaN or Inf")
        return value.float()

    def _update_stream_condition(self, inputs, device):
        # Validation happens before text/counters are mutated in the base class.
        value = self._parse_stream_position(inputs, device)
        if value.shape != (self.batch_size, self.stream_condition_dim):
            raise ValueError(
                f"stream position must resolve to "
                f"({self.batch_size},{self.stream_condition_dim}), "
                f"got {tuple(value.shape)}"
            )
        if not torch.isfinite(value).all():
            raise ValueError("stream position contains NaN or Inf")
        self._pending_stream_position = value.float()

    def _commit_stream_condition(self, index):
        if self._pending_stream_position is None:
            raise RuntimeError("stream position validation/commit transaction is missing")
        if not 0 <= index < self.buf_len:
            raise IndexError(
                f"stream position index {index} outside buffer [0,{self.buf_len})"
            )
        self._stream_position[:, index] = self._pending_stream_position
        self._stream_position_valid[:, index] = True
        self._pending_stream_position = None

    def _rollback_stream_condition(self, trim_len):
        keep = self.buf_len - trim_len
        self._stream_position[:, :keep] = self._stream_position[
            :, trim_len:
        ].clone()
        self._stream_position_valid[:, :keep] = self._stream_position_valid[
            :, trim_len:
        ].clone()
        self._stream_position[:, keep:] = 0
        self._stream_position_valid[:, keep:] = False

    def _stream_position_slice(self, start, end):
        valid = self._stream_position_valid[:, start:end]
        if valid.numel() and not bool(valid.all()):
            missing = (~valid).nonzero(as_tuple=False)[0].tolist()
            raise RuntimeError(
                f"stream root condition missing inside active window "
                f"[{start},{end}); first missing batch/local-index={missing}"
            )
        return self._stream_position[:, start:end]


class _RootConditionedDiffusion(
    _PositionStreamBufferMixin, DiffForcingWanModel
):
    """Diffusion whose root channels are pure conditioning.



    The root is never noised, never predicted and never scored: the diffused

    state holds only the non-root channels, the root is concatenated in front

    of the denoiser input at every training window and every denoise step, and

    the emitted output is ``[supplied root, network state]``.  Root output is

    therefore bit-exact with the supplied root.



    Subclasses define how a batch's raw root maps onto one condition vector per

    state unit (``condition_dim``): the direct model uses one root vector per

    frame, while the latent model packs four root vectors per latent token.

    """

    condition_dim = 0

    def _split_statistics(self, mean_path, std_path, expected_width):
        """Split one statistics pair into (condition, state) halves.



        Both halves come from the same file, so the condition is normalized

        with per-dimension statistics of the exact values it carries.

        """
        if (mean_path is None) != (std_path is None):
            raise ValueError("mean_path and std_path must be provided together")
        mean, std = load_statistics(mean_path, std_path, expected_width)
        mean, std = mean.reshape(-1), std.reshape(-1)
        if mean.numel() != expected_width or std.numel() != expected_width:
            raise ValueError(
                f"statistics must have {expected_width} values, got "
                f"{mean.numel()}/{std.numel()}"
            )
        std = guard_std(std)
        return (
            mean[: self.condition_dim].clone(),
            std[: self.condition_dim].clone(),
            mean[self.condition_dim :].clone(),
            std[self.condition_dim :].clone(),
        )

    def _register_statistics(self, mean_path, std_path, expected_width):
        cond_mean, cond_std, state_mean, state_std = self._split_statistics(
            mean_path, std_path, expected_width
        )
        self.mean = state_mean
        self.std = state_std
        self.register_buffer("position_mean", cond_mean)
        self.register_buffer("position_std", cond_std)

    def _assert_stream_schedule(self):
        """Reject schedules that commit frames without ever denoising them.



        ``get_committable`` yields zero committable steps when

        ``steps < chunk_size``, so the loop body never runs while frames are

        still emitted.

        """
        steps = int(self.time_scheduler.steps)
        chunk_size = int(self.time_scheduler.chunk_size)
        if steps < chunk_size or steps % chunk_size != 0:
            raise ValueError(
                "streaming requires steps to be a positive multiple of "
                f"chunk_size, got steps={steps}, chunk_size={chunk_size}"
            )

    # ------------------------------------------------------------------
    # Condition extraction (subclass hooks)
    # ------------------------------------------------------------------
    def _split_feature(self, feature):
        """Return (condition_part, state_part) of a full-width feature."""
        raise NotImplementedError

    def _pack_position_batch(self, position, valid_len, seq_len, device):
        """Return (B, seq_len, condition_dim) raw conditions."""
        raise NotImplementedError

    def _extract_inputs(self, x):
        inputs = super()._extract_inputs(x)
        # Position-only and streaming callers commonly use the literal key
        # without adding it to input_keys.  Keep custom mappings authoritative,
        # then fall back to the public ``position`` key.
        if "position" not in inputs:
            position_key = self.input_keys.get("position", "position")
            if position_key in x:
                inputs["position"] = x[position_key]
            elif "position" in x:
                inputs["position"] = x["position"]
        feature = inputs.get("feature")
        if feature is not None:
            width = feature.size(-1)
            if width == self.condition_dim + self.input_dim:
                condition, state = self._split_feature(feature)
                inputs["feature"] = state
                inputs.setdefault("position", condition)
            elif width != self.input_dim:
                raise ValueError(
                    f"feature must have {self.condition_dim + self.input_dim} "
                    f"(root+state) or {self.input_dim} (state) channels, got {width}"
                )
        return inputs

    def _get_raw_position(self, inputs):
        position = inputs.get("position")
        if position is None:
            raise KeyError(
                "root conditioning is required: supply input_keys.position or a "
                "full-width feature whose leading channels carry the root"
            )
        return position

    # ------------------------------------------------------------------
    # Training
    # ------------------------------------------------------------------
    def _prepare_training_condition(

        self, inputs, normalized_feature, valid_len, device

    ):
        packed = self._pack_position_batch(
            self._get_raw_position(inputs),
            valid_len,
            normalized_feature.size(1),
            device,
        )
        normalized = (packed - self.position_mean) / self.position_std
        return [
            normalized[i].transpose(0, 1)[:, :, None, None]
            for i in range(normalized.size(0))
        ]

    def _condition_training_input(

        self, noisy, clean, condition, batch_index, start, end

    ):
        root = condition[batch_index][:, start:end]
        return torch.cat([root.to(noisy), noisy], dim=0)

    # ------------------------------------------------------------------
    # Offline generation
    # ------------------------------------------------------------------
    def _prepare_generation_condition(self, inputs, valid_len, seq_len, device):
        packed = self._pack_position_batch(
            self._get_raw_position(inputs), valid_len, seq_len, device
        )
        normalized = (packed - self.position_mean) / self.position_std
        return {
            "raw": packed,
            "normalized": [
                normalized[i].transpose(0, 1)[:, :, None, None]
                for i in range(normalized.size(0))
            ],
        }

    def _condition_generation_input(self, noisy, condition, starts, ends):
        return [
            torch.cat(
                [
                    condition["normalized"][i][:, starts[i] : ends[i]].to(noisy[i]),
                    noisy[i],
                ],
                dim=0,
            )
            for i in range(len(noisy))
        ]

    def _finalize_generation_output(self, generated, condition, valid_len):
        return [
            torch.cat(
                [condition["raw"][i, : valid_len[i]].to(generated[i]), generated[i]],
                dim=-1,
            )
            for i in range(len(generated))
        ]

    def _generation_output_metadata(self, condition, valid_len):
        return {
            "position": [
                condition["raw"][i, : valid_len[i]].clone()
                for i in range(len(valid_len))
            ]
        }

    # ------------------------------------------------------------------
    # Streaming
    # ------------------------------------------------------------------
    def _condition_stream_input(self, noisy, starts, ends):
        result = []
        for i, item in enumerate(noisy):
            raw = self._stream_position_slice(starts[i], ends[i])[i]
            norm = (raw - self.position_mean) / self.position_std
            root = norm.transpose(0, 1)[:, :, None, None].to(item)
            result.append(torch.cat([root, item], dim=0))
        return result

    def _finalize_stream_output(self, generated, start, end):
        raw = self._stream_position_slice(start, end)
        return [
            torch.cat([raw[i].to(generated[i]), generated[i]], dim=-1)
            for i in range(len(generated))
        ]

    def _stream_output_metadata(self, start, end):
        # Clone: the slice is a view of the rollback-mutated ring buffer.
        raw = self._stream_position_slice(start, end)
        return {"position": [raw[i].clone() for i in range(self.batch_size)]}


class DiffForcingPositionWanModel(_RootConditionedDiffusion):
    """Direct full-feature diffusion conditioned on clean root motion.



    ``input_dim`` keeps its config meaning (the complete feature width); the

    diffused state contains every non-root channel.  The denoiser reads the

    clean root plus noisy pose state and predicts only the pose state, so the

    loss never covers root motion and emitted root channels are exactly the

    supplied values.

    """

    def __init__(

        self,

        *args,

        input_dim=138,

        representation=None,

        root_dim=None,

        mean_path=None,

        std_path=None,

        root_mean_path=None,

        root_std_path=None,

        **kwargs,

    ):
        self.feature_dim = int(input_dim)
        self.representation, self.root_dim = resolve_position_representation(
            representation,
            feature_dim=self.feature_dim,
            root_dim=root_dim,
        )
        self.condition_dim = self.root_dim
        if not 0 < self.root_dim < self.feature_dim:
            raise ValueError(
                f"root_dim must be in (0,{self.feature_dim}), got {self.root_dim}"
            )
        kwargs.pop("model_input_dim", None)
        super().__init__(
            *args,
            input_dim=self.feature_dim - self.root_dim,
            model_input_dim=self.feature_dim,
            mean_path=None,
            std_path=None,
            **kwargs,
        )
        self._assert_stream_schedule()
        self.stream_condition_dim = self.root_dim
        self._register_statistics(mean_path, std_path, self.feature_dim)
        if root_mean_path is not None or root_std_path is not None:
            # Optional per-dimension root statistics can override the leading
            # values from the full-feature statistics.
            if root_mean_path is None or root_std_path is None:
                raise ValueError(
                    "root_mean_path and root_std_path must be provided together"
                )
            root_mean, root_std = load_statistics(
                root_mean_path, root_std_path, self.root_dim
            )
            root_mean, root_std = root_mean.reshape(-1), root_std.reshape(-1)
            if root_mean.numel() < self.root_dim or root_std.numel() < self.root_dim:
                raise ValueError(
                    f"root statistics must have at least {self.root_dim} values"
                )
            self.position_mean = root_mean[: self.root_dim].clone()
            self.position_std = guard_std(root_std[: self.root_dim]).clone()

    def _split_feature(self, feature):
        return feature[..., : self.root_dim], feature[..., self.root_dim :]

    def _pack_position_batch(self, position, valid_len, seq_len, device):
        position = _to_batched_tensor(position, device)
        if position.ndim == 2:
            position = position.unsqueeze(0)
        if position.ndim != 3 or position.size(-1) < self.root_dim:
            raise ValueError(
                f"direct root must be (B,T,>={self.root_dim}), got "
                f"{tuple(position.shape)}"
            )
        if position.size(0) != len(valid_len):
            raise ValueError(
                f"root batch={position.size(0)}, expected {len(valid_len)}"
            )
        raw = position[..., : self.root_dim].float()
        padded = []
        for i, length in enumerate(valid_len):
            if raw.size(1) < length:
                raise ValueError(
                    f"sample {i} has {raw.size(1)} root frames but needs {length}"
                )
            item = raw[i, :length]
            if seq_len > length:
                if length == 0:
                    raise ValueError("cannot pad an empty valid root sequence")
                item = torch.cat([item, item[-1:].expand(seq_len - length, -1)], dim=0)
            padded.append(item)
        packed = torch.stack(padded)
        if not torch.isfinite(packed).all():
            raise ValueError("root condition contains NaN or Inf")
        return packed

    def _position_from_history(self, history_latent):
        tensors = [torch.as_tensor(v) for v in history_latent]
        if not tensors:
            return None
        width = int(tensors[0].size(-1))
        if width == self.feature_dim:
            return torch.stack([v[..., : self.root_dim] for v in tensors])
        return None

    def init_generated(

        self,

        seq_len,

        batch_size=1,

        schedule_config={},

        history_latent=None,

        history_position=None,

    ):
        if history_latent is not None:
            # A full-width history carries its own root, which must be pulled
            # out here: the diffused state has no root channels for the base
            # class to recover it from.
            split_state = []
            inferred_position = []
            for item in history_latent:
                item = torch.as_tensor(item)
                width = int(item.size(-1))
                if width == self.feature_dim:
                    inferred_position.append(item[..., : self.root_dim])
                    split_state.append(item[..., self.root_dim :])
                elif width == self.input_dim:
                    split_state.append(item)
                else:
                    raise ValueError(
                        f"history must be {self.feature_dim}D (root+state) or "
                        f"{self.input_dim}D (state), got {width}D"
                    )
            if inferred_position:
                if len(inferred_position) != len(split_state):
                    raise ValueError("cannot mix full-width and state-only history")
                if history_position is not None:
                    raise ValueError(
                        "history_position must be omitted for a full-width history"
                    )
                history_position = inferred_position
            elif history_position is None:
                raise ValueError("a state-only history requires history_position")
            history_latent = split_state
        return super().init_generated(
            seq_len,
            batch_size=batch_size,
            schedule_config=schedule_config,
            history_latent=history_latent,
            history_position=history_position,
        )

    def _parse_stream_position(self, inputs, device):
        position = _to_batched_tensor(self._get_raw_position(inputs), device)
        if position.ndim == 3 and position.size(1) == 1:
            position = position[:, 0]
        if position.ndim == 1:
            position = position.unsqueeze(0)
        if position.ndim != 2 or position.size(-1) < self.root_dim:
            raise ValueError(
                f"each direct stream step needs (B,{self.root_dim}) root, "
                f"got {tuple(position.shape)}"
            )
        return position[..., : self.root_dim]


class LatentDiffForcingPositionWanModel(_RootConditionedDiffusion):
    """Latent diffusion conditioned on the WAN-causal packed root.



    ``input_dim`` keeps its config meaning (the VAE latent width); the diffused

    state is exactly that latent.  The denoiser reads ``position_dim + latent``

    channels and predicts only the latent, so the emitted token is

    ``[supplied packed root, network latent]`` and its root is bit-exact.



    The packed root width is inferred, in order, from ``representation``, an

    explicit ``root_dim``/``position_dim``, or the token statistics width.  The

    last path keeps existing configs working when they do not forward the

    top-level representation into model parameters.

    """

    def __init__(

        self,

        *args,

        input_dim=16,

        representation=None,

        position_dim=None,

        root_dim=None,

        root_group_size=WAN_ROOT_GROUP_SIZE,

        mean_path=None,

        std_path=None,

        **kwargs,

    ):
        self.root_group_size = int(root_group_size)
        if self.root_group_size != WAN_ROOT_GROUP_SIZE:
            raise ValueError(
                "the WAN position ABI groups exactly "
                f"{WAN_ROOT_GROUP_SIZE} root frames per latent, got "
                f"root_group_size={self.root_group_size}"
            )
        self.latent_dim = int(input_dim)
        if self.latent_dim <= 0:
            raise ValueError(f"input_dim must be positive, got {self.latent_dim}")

        resolved_root_dim = None if root_dim is None else int(root_dim)
        self.representation = None
        if representation is not None:
            self.representation, resolved_root_dim = (
                resolve_position_representation(
                    representation,
                    root_dim=resolved_root_dim,
                )
            )

        resolved_position_dim = (
            None if position_dim is None else int(position_dim)
        )
        if resolved_position_dim is not None:
            if resolved_position_dim <= 0:
                raise ValueError(
                    f"position_dim must be positive, got {resolved_position_dim}"
                )
            if resolved_position_dim % self.root_group_size != 0:
                raise ValueError(
                    "position_dim must be divisible by root_group_size "
                    f"({self.root_group_size}), got {resolved_position_dim}"
                )
            position_root_dim = resolved_position_dim // self.root_group_size
            if (
                resolved_root_dim is not None
                and resolved_root_dim != position_root_dim
            ):
                raise ValueError(
                    "position_dim and root_dim disagree: "
                    f"{resolved_position_dim} != {resolved_root_dim} * "
                    f"{self.root_group_size}"
                )
            resolved_root_dim = position_root_dim
        elif resolved_root_dim is not None:
            resolved_position_dim = resolved_root_dim * self.root_group_size
        elif (
            mean_path is not None and std_path is not None
            and Path(mean_path).is_file() and Path(std_path).is_file()
        ):
            mean_width = int(np.asarray(np.load(mean_path)).size)
            std_width = int(np.asarray(np.load(std_path)).size)
            if mean_width != std_width:
                raise ValueError(
                    "token statistics widths must match, got "
                    f"{mean_width}/{std_width}"
                )
            resolved_position_dim = mean_width - self.latent_dim
            if (
                resolved_position_dim <= 0
                or resolved_position_dim % self.root_group_size != 0
            ):
                raise ValueError(
                    f"cannot infer packed root width from {mean_width}D token "
                    f"statistics, latent_dim={self.latent_dim}, and "
                    f"root_group_size={self.root_group_size}"
                )
            resolved_root_dim = resolved_position_dim // self.root_group_size
            if resolved_root_dim not in set(POSITION_ROOT_DIMS.values()):
                raise ValueError(
                    f"cannot infer a supported root width from {mean_width}D "
                    f"token statistics: inferred root_dim={resolved_root_dim}, "
                    f"supported root widths are "
                    f"{sorted(set(POSITION_ROOT_DIMS.values()))}; pass "
                    "root_dim/position_dim explicitly for a custom schema"
                )
        else:
            # Historical root3/root12 default for configs without token stats.
            _, resolved_root_dim = resolve_position_representation()
            resolved_position_dim = resolved_root_dim * self.root_group_size

        # If a representation supplied the root width, also validate any width
        # inferred from position_dim or token statistics.
        self.representation, resolved_root_dim = resolve_position_representation(
            self.representation,
            root_dim=resolved_root_dim,
        )
        self.root_dim = resolved_root_dim
        self.position_dim = resolved_position_dim
        self.condition_dim = self.position_dim
        if self.position_dim != self.root_dim * self.root_group_size:
            raise ValueError(
                "position_dim must equal root_dim * root_group_size "
                f"({self.root_dim}*{self.root_group_size})"
            )
        kwargs.pop("model_input_dim", None)
        super().__init__(
            *args,
            input_dim=self.latent_dim,
            model_input_dim=self.position_dim + self.latent_dim,
            mean_path=None,
            std_path=None,
            **kwargs,
        )
        self._assert_stream_schedule()
        self.stream_condition_dim = self.position_dim
        self._register_statistics(
            mean_path, std_path, self.position_dim + self.latent_dim
        )

    def _split_feature(self, feature):
        return feature[..., : self.position_dim], feature[..., self.position_dim :]

    def _pack_position_batch(self, position, valid_len, seq_len, device):
        position = _to_batched_tensor(position, device)
        if position.ndim == 2:
            position = position.unsqueeze(0)
        if position.ndim != 3:
            raise ValueError(
                f"latent root must be (B,L,{self.position_dim}) or (B,T,>="
                f"{self.root_dim}), got {tuple(position.shape)}"
            )
        if position.size(0) != len(valid_len):
            raise ValueError(
                f"root batch={position.size(0)}, expected {len(valid_len)}"
            )
        packed_items = []
        for i, length in enumerate(valid_len):
            item = position[i : i + 1]
            if item.size(-1) == self.position_dim:
                if item.size(1) < length:
                    raise ValueError(
                        f"sample {i} has {item.size(1)} packed roots but needs {length}"
                    )
                packed = item[:, :length]
            else:
                required_frames = (
                    0 if length == 0 else 1 + self.root_group_size * (length - 1)
                )
                if item.size(-1) < self.root_dim or item.size(1) < required_frames:
                    raise ValueError(
                        f"sample {i} needs {required_frames} raw root frames for "
                        f"{length} latents, got {item.size(1)}"
                    )
                # Trim to this sample's own frames: in a collated batch the
                # tensor is padded to the batch maximum, and pack_root_condition
                # clamps its taps to the frame count it is given, so an untrimmed
                # item would let a short sample's tail taps land on pad zeros.
                item = item[:, :required_frames]
                packed = pack_root_condition(
                    item[..., : self.root_dim],
                    latent_length=length,
                    root_dim=self.root_dim,
                    group_size=self.root_group_size,
                    first_chunk=True,
                )
            packed = packed[0]
            if seq_len > length:
                if length == 0:
                    raise ValueError("cannot pad an empty valid root sequence")
                packed = torch.cat(
                    [packed, packed[-1:].expand(seq_len - length, -1)], dim=0
                )
            packed_items.append(packed)
        packed = torch.stack(packed_items).float()
        if not torch.isfinite(packed).all():
            raise ValueError("latent root condition contains NaN or Inf")
        return packed

    def _position_from_history(self, history_latent):
        tensors = [torch.as_tensor(v) for v in history_latent]
        if not tensors:
            return None
        width = int(tensors[0].size(-1))
        if width == self.position_dim + self.latent_dim:
            return torch.stack([v[..., : self.position_dim] for v in tensors])
        return None

    def init_generated(

        self,

        seq_len,

        batch_size=1,

        schedule_config={},

        history_latent=None,

        history_position=None,

    ):
        if history_latent is not None:
            token_dim = self.position_dim + self.latent_dim
            split_state = []
            inferred_position = []
            for item in history_latent:
                item = torch.as_tensor(item)
                width = int(item.size(-1))
                if width == token_dim:
                    inferred_position.append(item[..., : self.position_dim])
                    split_state.append(item[..., self.position_dim :])
                elif width == self.latent_dim:
                    split_state.append(item)
                else:
                    raise ValueError(
                        f"latent history must be {token_dim}D (root+latent) or "
                        f"{self.latent_dim}D (latent), got {width}D"
                    )
            if inferred_position:
                if len(inferred_position) != len(split_state):
                    raise ValueError("cannot mix token-width and latent-only history")
                if history_position is not None:
                    raise ValueError(
                        "history_position must be omitted for a token-width history"
                    )
                history_position = inferred_position
            elif history_position is None:
                raise ValueError("a latent-only history requires history_position")
            history_latent = split_state
        return super().init_generated(
            seq_len,
            batch_size=batch_size,
            schedule_config=schedule_config,
            history_latent=history_latent,
            history_position=history_position,
        )

    def _parse_stream_position(self, inputs, device):
        position = _to_batched_tensor(self._get_raw_position(inputs), device)
        if position.ndim == 3 and position.shape[-2:] == (
            self.root_group_size,
            self.root_dim,
        ):
            position = position.flatten(-2)
        elif position.ndim == 3 and position.size(1) == 1:
            position = position[:, 0]
        if position.ndim == 1:
            position = position.unsqueeze(0)
        if position.ndim != 2 or position.size(-1) != self.position_dim:
            raise ValueError(
                f"each latent stream step needs (B,{self.position_dim}) or "
                f"(B,{self.root_group_size},{self.root_dim}) root, got "
                f"{tuple(position.shape)}"
            )
        return position


# Concise aliases for configs and downstream imports.
DiffForcingWanPositionModel = DiffForcingPositionWanModel
LatentDiffForcingWanPositionModel = LatentDiffForcingPositionWanModel