File size: 147,291 Bytes
c4f8e4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
import os
import json
import copy
import contextlib
from tqdm import tqdm
from typing import List, Optional, Tuple, Union, Dict, Any
from typing import Any, Dict, List, Optional, Tuple, Union
from typing_extensions import Self
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from PIL import Image
from dataclasses import dataclass
from diffusers.utils.outputs import BaseOutput
from diffusers.configuration_utils import ConfigMixin, FrozenDict, register_to_config
# from diffusers.utils.torch_utils import maybe_allow_in_graph
from diffusers.models.modeling_utils import ModelMixin
# from diffusers.models.transformers.transformer_sd3 import SD3Transformer2DModel
from diffusers.models.embeddings import CombinedTimestepTextProjEmbeddings
# from diffusers.models.normalization import AdaLayerNorm, AdaLayerNormZero, AdaLayerNormContinuous
from diffusers.models.embeddings import CombinedTimestepLabelEmbeddings
from diffusers.models.normalization import FP32LayerNorm
from diffusers.models.attention_processor import Attention, JointAttnProcessor2_0
from diffusers.models.attention import FeedForward, _chunked_feed_forward
# from dit_policy.data4robotics.models.diffusion import _TimeNetwork
from psi.config.config import LaunchConfig
from transformers import (
    Qwen3VLForConditionalGeneration,
    Qwen2_5OmniThinkerForConditionalGeneration,
    Qwen2_5OmniProcessor,
    AutoConfig,
    AutoProcessor,
    Qwen2TokenizerFast,
    Qwen3VLProcessor,
)
from qwen_vl_utils import process_vision_info

from psi.utils import initialize_overwatch, count_parameters

# from InternVLA.model.modules.action_model.DiT_modules.models import DiT
# from InternVLA.model.modules.projector.QFormer import CrossAttentionBlock
overwatch = initialize_overwatch(__name__)

# from psi.config.model import ModelConfig, InternVLA_M1_ModelConfig
# from psi.learn.vlms import _QWen_VL_Interface
from transformers.models.siglip import SiglipModel
from transformers.models.dinov2 import Dinov2Model
# from vlt.transformers.vla import ActionTransformerModel

from diffusers.utils import logging
logger = logging.get_logger(__name__)  # pylint: disable=invalid-name  FIXME why this not works any more?

QWEN3VL_VARIANT = "Qwen/Qwen3-VL-2B-Instruct"
OMNI_DEFAULT_SYSTEM_PROMPT = (
    "You are Qwen, a virtual human developed by the Qwen Team, Alibaba Group, "
    "capable of perceiving auditory and visual inputs, as well as generating text and speech."
)

@dataclass
class HumanFoundationModelOutput(BaseOutput):
    action: "torch.Tensor"  # noqa: F821

    def to_tuple(self):
        return (None, self.action)

@dataclass
class ActionTransformerModelOutput(BaseOutput):
    action: "torch.Tensor"  # noqa: F821

    def to_tuple(self):
        return (None, self.action)

class _TimeNetwork(nn.Module):
    def __init__(self, time_dim, out_dim, learnable_w=False):
        assert time_dim % 2 == 0, "time_dim must be even!"
        half_dim = int(time_dim // 2)
        super().__init__()

        w = np.log(10000) / (half_dim - 1)
        w = torch.exp(torch.arange(half_dim) * -w).float()
        # self.register_parameter("w", nn.Parameter(w, requires_grad=learnable_w))
        self.w = nn.Parameter(w, requires_grad=learnable_w)

        self.out_net = nn.Sequential(
            nn.Linear(time_dim, out_dim), nn.SiLU(), nn.Linear(out_dim, out_dim)
        )

    def forward(self, x):
        # assert len(x.shape) == 1, "assumes 1d input timestep array"
        # RTC: support 2D timesteps (B,Tp)
        x = x[..., None] * self.w 
        x = torch.cat((torch.cos(x), torch.sin(x)), dim=-1) 
        return self.out_net(x) 

class RMSNorm(nn.Module):
    def __init__(self, dim, eps: float, elementwise_affine: bool = True):
        super().__init__()

        self.eps = eps

        if isinstance(dim, int):
            dim = (dim,)

        self.dim = torch.Size(dim)

        if elementwise_affine:
            self.weight = nn.Parameter(torch.ones(dim))
        else:
            self.weight = None

    def forward(self, hidden_states):
        input_dtype = hidden_states.dtype
        variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True)
        hidden_states = hidden_states * torch.rsqrt(variance + self.eps)

        if self.weight is not None:
            # convert into half-precision if necessary
            if self.weight.dtype in [torch.float16, torch.bfloat16]:
                hidden_states = hidden_states.to(self.weight.dtype)
            hidden_states = hidden_states * self.weight
        else:
            hidden_states = hidden_states.to(input_dtype)

        return hidden_states


if torch.__version__ >= "2.1.0":
    LayerNorm = nn.LayerNorm # type:ignore
else:
    # Has optional bias parameter compared to torch layer norm
    # TODO: replace with torch layernorm once min required torch version >= 2.1
    class LayerNorm(nn.Module):
        def __init__(self, dim, eps: float = 1e-5, elementwise_affine: bool = True, bias: bool = True):
            super().__init__()

            self.eps = eps

            if isinstance(dim, int):
                dim = (dim,)

            self.dim = torch.Size(dim)

            if elementwise_affine:
                self.weight = nn.Parameter(torch.ones(dim))
                self.bias = nn.Parameter(torch.zeros(dim)) if bias else None
            else:
                self.weight = None
                self.bias = None

        def forward(self, input):
            return F.layer_norm(input, self.dim, self.weight, self.bias, self.eps)

class AdaLayerNormContinuous(nn.Module):
    def __init__(
        self,
        embedding_dim: int,
        conditioning_embedding_dim: int,
        # NOTE: It is a bit weird that the norm layer can be configured to have scale and shift parameters
        # because the output is immediately scaled and shifted by the projected conditioning embeddings.
        # Note that AdaLayerNorm does not let the norm layer have scale and shift parameters.
        # However, this is how it was implemented in the original code, and it's rather likely you should
        # set `elementwise_affine` to False.
        elementwise_affine=True,
        eps=1e-5,
        bias=True,
        norm_type="layer_norm",
    ):
        super().__init__()
        self.silu = nn.SiLU()
        self.linear = nn.Linear(conditioning_embedding_dim, embedding_dim * 2, bias=bias)
        if norm_type == "layer_norm":
            self.norm = LayerNorm(embedding_dim, eps, elementwise_affine, bias)
        elif norm_type == "rms_norm":
            self.norm = RMSNorm(embedding_dim, eps, elementwise_affine)
        else:
            raise ValueError(f"unknown norm_type {norm_type}")

    def forward(self, x: torch.Tensor, conditioning_embedding: torch.Tensor) -> torch.Tensor:
        # convert back to the original dtype in case `conditioning_embedding`` is upcasted to float32 (needed for hunyuanDiT)
        if len(conditioning_embedding.shape) == 2: # (B, D)
            conditioning_embedding = conditioning_embedding.unsqueeze(1) # (B, 1, D)
        elif len(conditioning_embedding.shape) == 3: # (B, T, D)
            pass
        else:
            raise ValueError(f"Invalid shape of conditioning_embedding: {conditioning_embedding.shape}")
        emb = self.linear(self.silu(conditioning_embedding).to(x.dtype))
        scale, shift = torch.chunk(emb, 2, dim=-1)
        x = self.norm(x) * (1 + scale) + shift
        return x

class AdaLayerNormZero(nn.Module):
    r"""
    Norm layer adaptive layer norm zero (adaLN-Zero).

    Parameters:
        embedding_dim (`int`): The size of each embedding vector.
        num_embeddings (`int`): The size of the embeddings dictionary.
    """

    def __init__(self, embedding_dim: int, num_embeddings: Optional[int] = None, norm_type="layer_norm", bias=True):
        super().__init__()
        if num_embeddings is not None:
            self.emb = CombinedTimestepLabelEmbeddings(num_embeddings, embedding_dim)
        else:
            self.emb = None

        self.silu = nn.SiLU()
        self.linear = nn.Linear(embedding_dim, 6 * embedding_dim, bias=bias)
        if norm_type == "layer_norm":
            self.norm = nn.LayerNorm(embedding_dim, elementwise_affine=False, eps=1e-6)
        elif norm_type == "fp32_layer_norm":
            self.norm = FP32LayerNorm(embedding_dim, elementwise_affine=False, bias=False)
        else:
            raise ValueError(
                f"Unsupported `norm_type` ({norm_type}) provided. Supported ones are: 'layer_norm', 'fp32_layer_norm'."
            )

    def forward(
        self,
        x: torch.Tensor,
        timestep: Optional[torch.Tensor] = None,
        class_labels: Optional[torch.LongTensor] = None,
        hidden_dtype: Optional[torch.dtype] = None,
        emb: Optional[torch.Tensor] = None,
    ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
        if self.emb is not None:
            emb = self.emb(timestep, class_labels, hidden_dtype=hidden_dtype)
            assert False, "Not implemented with emb shape == 3, (B, T, D)"
        
        if len(emb.shape) == 2: # (B, D)
            emb = emb.unsqueeze(1) # (B, 1, D)
        elif len(emb.shape) == 3: # (B, T, D)
            pass
        else:
            raise ValueError(f"Invalid shape of emb: {emb.shape}")
        emb = self.linear(self.silu(emb))
        shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = emb.chunk(6, dim=-1)
        x = self.norm(x) * (1 + scale_msa) + shift_msa
        return x, gate_msa, shift_mlp, scale_mlp, gate_mlp

class _PositionalEncoding(nn.Module):
    def __init__(self, d_model, max_len=5000):
        super().__init__()
        # Compute the positional encodings once in log space
        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(
            torch.arange(0, d_model, 2, dtype=torch.float)
            * -(np.log(10000.0) / d_model)
        )
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        pe = pe.unsqueeze(0).transpose(0, 1)
        # self.register_buffer("pe", torch.clone(pe)) # <-- SONGLIN: this will cause problems when accelerate.prepare
        # self.register_parameter("pe", nn.Parameter(pe, requires_grad=False))
        self.pe = nn.Parameter(pe, requires_grad=False)

    def forward(self, x):
        """
        Args:
            x: Tensor of shape (seq_len, batch_size, d_model)

        Returns:
            Tensor of shape (seq_len, batch_size, d_model) with positional encodings added
        """
        pe = self.pe[: x.shape[0]]
        pe = pe.repeat((1, x.shape[1], 1))
        return pe.detach().clone()

class FilmConditioning(nn.Module):
    """Layer that adds FiLM conditioning.

    This is intended to be applied after a convolutional layer. It will learn a
    multiplicative and an additive factor to be applied to each channel of the
    convolution's output.

    Conv layer can be rank 2 or 4.

    For further details, see: https://arxiv.org/abs/1709.07871
    """
    
    def __init__(self,
                 in_dim: int,
                 num_channels: int):
        """Constructs a FiLM conditioning layer.

        Args:
            num_channels: Number of filter channels to expect in the input.
        """
        super(FilmConditioning, self).__init__() 
        # Note that we initialize with zeros because empirically we have found
        # this works better than initializing with glorot.
        self._projection_add = nn.Linear(in_dim, num_channels)
        self._projection_mult = nn.Linear(in_dim, num_channels)
        for m in self.modules():
            if isinstance(m, nn.Linear):
                nn.init.zeros_(m.weight)
                nn.init.zeros_(m.bias)
    
    def forward(self, conv_filters, conditioning):
        assert len(conditioning.shape) == 2
        projected_cond_add = self._projection_add(conditioning)
        projected_cond_mult = self._projection_mult(conditioning)

        if len(conv_filters.shape) == 4:
            # [B, D] -> [B, D, 1, 1]
            projected_cond_add = projected_cond_add.unsqueeze(-1).unsqueeze(-1)
            projected_cond_mult = projected_cond_mult.unsqueeze(-1).unsqueeze(-1)
        elif len(conv_filters.shape) == 3:
            # [B, D] -> [B, 1, D]
            projected_cond_add = projected_cond_add.unsqueeze(1)
            projected_cond_mult = projected_cond_mult.unsqueeze(1)
        else:
            assert len(conv_filters.shape) == 2
        
        # Original FiLM paper argues that 1 + gamma centers the initialization at
        # identity transform.
        result = (1 + projected_cond_mult) * conv_filters + projected_cond_add
        return result

class JointVLAAttnProcessor: #(nn.Module):
    """Attention processor used typically in processing the SD3-like self-attention projections."""

    def __init__(
        self,
        query_dim: int,
        heads: int = 8,
        kv_heads: Optional[int] = None,
        dim_head: int = 64,
        out_dim: Optional[int] = None,
        dropout: float = 0.0,
        bias: bool = False,
        out_bias: bool = True,
        context_pre_only=None,
    ):
        super().__init__()

        if not hasattr(F, "scaled_dot_product_attention"):
            raise ImportError("JointAttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.")

        # self.inner_dim = out_dim if out_dim is not None else dim_head * heads
        # self.inner_kv_dim = self.inner_dim if kv_heads is None else dim_head * kv_heads
        # self.out_dim = out_dim if out_dim is not None else query_dim

        # self.to_q_lang = nn.Linear(query_dim, self.inner_dim, bias=bias)
        # self.to_k_lang = nn.Linear(query_dim, self.inner_kv_dim, bias=bias)
        # self.to_v_lang = nn.Linear(query_dim, self.inner_kv_dim, bias=bias)

    def __call__(
        self,
        attn: Attention,
        hidden_states: torch.Tensor,
        encoder_hidden_states: Optional[torch.Tensor] = None,
        attention_mask: Optional[torch.Tensor] = None,
        *args,
        **kwargs,
    ) -> tuple[torch.Tensor, Optional[torch.Tensor]]:
        residual = hidden_states

        batch_size = hidden_states.shape[0]

        # `sample` projections.
        query = attn.to_q(hidden_states)

        assert attn.to_k is not None
        key = attn.to_k(hidden_states)

        assert attn.to_v is not None
        value = attn.to_v(hidden_states)

        inner_dim = key.shape[-1]
        head_dim = inner_dim // attn.heads

        query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
        key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
        value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)

        if attn.norm_q is not None:
            query = attn.norm_q(query)
        if attn.norm_k is not None:
            key = attn.norm_k(key)

        # `context` projections.
        if encoder_hidden_states is not None:
            encoder_hidden_states_query_proj = attn.add_q_proj(encoder_hidden_states)
            encoder_hidden_states_key_proj = attn.add_k_proj(encoder_hidden_states)
            encoder_hidden_states_value_proj = attn.add_v_proj(encoder_hidden_states)

            encoder_hidden_states_query_proj = encoder_hidden_states_query_proj.view(
                batch_size, -1, attn.heads, head_dim
            ).transpose(1, 2)
            encoder_hidden_states_key_proj = encoder_hidden_states_key_proj.view(
                batch_size, -1, attn.heads, head_dim
            ).transpose(1, 2)
            encoder_hidden_states_value_proj = encoder_hidden_states_value_proj.view(
                batch_size, -1, attn.heads, head_dim
            ).transpose(1, 2)

            if attn.norm_added_q is not None:
                encoder_hidden_states_query_proj = attn.norm_added_q(encoder_hidden_states_query_proj)
            if attn.norm_added_k is not None:
                encoder_hidden_states_key_proj = attn.norm_added_k(encoder_hidden_states_key_proj)

            query = torch.cat([query, encoder_hidden_states_query_proj], dim=2)
            key = torch.cat([key, encoder_hidden_states_key_proj], dim=2)
            value = torch.cat([value, encoder_hidden_states_value_proj], dim=2)
            if attention_mask is not None:
                assert attention_mask.dtype == torch.float32
                attn_mask = torch.cat([
                    torch.ones(hidden_states.shape[0], 1, 1, hidden_states.shape[1], device=attention_mask.device).to(torch.bool),  # (B, 1, 1, S1)
                    (attention_mask == 1)[:, None, None, :]  # (B, 1, 1, S2)
                ], dim=-1)
            else:
                attn_mask = None

        hidden_states = F.scaled_dot_product_attention(query, key, value, dropout_p=0.0, is_causal=False, attn_mask=attn_mask) # type: ignore
        hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim) # type: ignore
        hidden_states = hidden_states.to(query.dtype) # type: ignore
 
        # Split the attention outputs.
        hidden_states, encoder_hidden_states = (
            hidden_states[:, : residual.shape[1]],
            hidden_states[:, residual.shape[1] :],
        ) # type: ignore
        if not attn.context_pre_only:
            encoder_hidden_states = attn.to_add_out(encoder_hidden_states)

        # linear proj
        hidden_states = attn.to_out[0](hidden_states)
        # dropout
        hidden_states = attn.to_out[1](hidden_states)

        return hidden_states, encoder_hidden_states
    
        
class ObservationProjection(nn.Module): # FIXME naming
    """ VLT observation projector

    Args:
        ...
    """

    def __init__(
        self,
        action_pred_horizon: int = 50, # Tp
        action_dim: int = 7, # in dim
        output_dim: int = 512, # out dim,
        hidden_dim: int = 1280,
        n_conditions: int = 1, # num image of contitions
        timestep_in_dim: int = 320,
        timestep_flip_sin_to_cos: bool = True,
        timestep_freq_shift: int = 0,
        token_fusion: str = "concat",
        resnet_store_path: str = "cache/visual_features/resnet18/IN_1M_resnet18.pth",
        odim: int = 32,
        view_feature_dim: int = 1920,  # VLM输出hidden_dim维度
        use_film: bool = False
    ):
        super().__init__()

        # TODO 1. compare
        
        # self.time_net = _TimeNetwork(time_dim=256, out_dim=512)
        
        # self.time_proj = Timesteps(timestep_in_dim, timestep_flip_sin_to_cos, timestep_freq_shift)
        # self.time_embedding = TimestepEmbedding(timestep_in_dim, output_dim, act_fn="silu")
        self.action_pred_horizon = action_pred_horizon
        self.action_dim = action_dim
        # self.ac_proj = nn.Sequential(
        #     nn.Linear(action_dim, action_dim),
        #     nn.GELU(approximate="tanh"),
        #     nn.Linear(action_dim, output_dim),
        # )

        # _DiTNoiseNet.__init__
        self.enc_pos = _PositionalEncoding(d_model=output_dim)

        

        # agent.__init__
        n_cams = 0 # 1
        
        imgs_per_cam = 1
        self.camera_indices = [] # [0]
        self._n_cams = n_cams
        self._share_cam_features = False # model_cfg.share_cam_features
        early_fusion = False
        text_dim = 768
        # odim = 15
        self.token_fusion = token_fusion

        if self._share_cam_features:
            ...
        else:
            if n_conditions > 0:
                from dit_policy.data4robotics.models.resnet import ResNet
                features = ResNet(
                    size=18,
                    weights="IMAGENET1K_V1",
                    restore_path=resnet_store_path,
                    avg_pool=False,
                    conv_repeat=0,
                    norm_cfg={
                        "name": "group_norm",
                        "num_groups": 16,
                    }
                )
            else:
                features = None
            feat_list = [copy.deepcopy(features) for _ in range(0, n_conditions)] # + [copy.deepcopy(features) for _ in range(1, n_cams)] + [copy.deepcopy(features) for _ in range(0, n_conditions)]
            self.visual_features = nn.ModuleList(feat_list)
            self.views_proj = nn.Linear(view_feature_dim, output_dim, bias=True) # TODO configure 1920 # boqian_fix

            # TODO abl: more layers # view proj
            # self.fc1 = nn.Linear(self.vision_dim, initial_projection_dim, bias=True)
            # self.fc2 = nn.Linear(initial_projection_dim, self.llm_dim, bias=True)
            # self.fc3 = nn.Linear(self.llm_dim, self.llm_dim, bias=True)
            # self.act_fn1 = nn.GELU()
            # self.act_fn2 = nn.GELU()

            # self.views_proj = nn.Sequential(
            #     nn.Conv2d(16, output_dim, kernel_size=(2, 2), stride=2, bias=True),
            #     nn.ReLU(),
            #     nn.Conv2d(output_dim, output_dim, kernel_size=(2, 2), stride=2, bias=True)
            # )
            # self.traj2d_proj = nn.ModuleList([
            #     nn.Conv2d(16, 512, kernel_size=(2, 2), stride=2, bias=True),
            #     nn.ReLU(),
            #     nn.Conv2d(512, output_dim, kernel_size=(2, 2), stride=2, bias=True),
            #     nn.ReLU(),
            #     nn.Linear(output_dim, output_dim, bias=True),
            #     nn.LayerNorm(output_dim)
            # ])
        
        self.early_fusion = early_fusion
        self.imgs_per_cam = imgs_per_cam
        imgs_per_cam = 1 if early_fusion else imgs_per_cam
        self._n_conditions = n_conditions

        self.embed_proj = nn.Identity()
        if self.token_fusion != "perceiver" and self._n_conditions > 0:
            assert features is not None
            self._token_dim = features.embed_dim
            if output_dim != self._token_dim:
                self.embed_proj = nn.Linear(self._token_dim, output_dim, bias=False)
        else:
            self._token_dim = 0

        self._n_tokens = imgs_per_cam * n_cams * self._token_dim + n_conditions * self._token_dim
        self.traj_n_token = self._token_dim
        
        self.feat_norm = nn.LayerNorm(output_dim, elementwise_affine=False, eps=1e-6) # feat_norm = None
        self.feat_traj_norm = nn.LayerNorm(output_dim, elementwise_affine=False, eps=1e-6) # feat_norm = None
        # self.traj2d_proj = nn.ModuleList([
        #     nn.Linear(output_dim, output_dim) for _ in range(2)
        # ])
        # self.traj2d_proj = nn.Conv2d(
        #     16, output_dim, kernel_size=(2, 2), stride=2, bias=True
        # )

        if self.token_fusion == "cross":
            
            self.attn = Attention(
                query_dim=self._token_dim,
                cross_attention_dim=1920,
                dim_head=64,
                out_dim=output_dim,
                heads=output_dim//64,
                bias=True,
                # pre_only=True,
                eps=1e-6
            )
        elif self.token_fusion == "perceiver":
            num_queries = 256
            self.latents1 = nn.Parameter(torch.randn(1, num_queries, hidden_dim) / hidden_dim**0.5)
            # self.proj_in1 = nn.Linear(1920, hidden_dim)  -> views_proj (see above)
            self.cross_attn1 = Attention(
                query_dim=hidden_dim,
                cross_attention_dim=hidden_dim,
                dim_head=64,
                out_dim=output_dim,
                heads=output_dim//64,
                bias=True,
                # pre_only=True,
                eps=1e-6
            )
            self.latents2 = nn.Parameter(torch.randn(1, num_queries, hidden_dim) / hidden_dim**0.5)
            self.proj_in2 = nn.Linear(self._token_dim, hidden_dim)
            self.cross_attn2 = Attention(
                query_dim=hidden_dim,
                cross_attention_dim=hidden_dim,
                dim_head=64,
                out_dim=output_dim,
                heads=output_dim//64,
                bias=True,
                # pre_only=True,
                eps=1e-6
            )

        self.film = FilmConditioning(2048, output_dim) if use_film else None

        self._obs_strat = "add_token"
        self._n_tokens += 1

        self._obs_proc = nn.Sequential(
            nn.Dropout(p=0.2), nn.Linear(odim, output_dim)
        )
        dropout = 0.1 # @see model_cfg.dropout
        linear_proj = nn.Identity() # build (optional) token feature projection layer 
        norm = nn.Identity() # feat_norm = None
        self.post_proc = nn.Sequential(linear_proj, norm, nn.Dropout(dropout))

    def forward(self, 
                # noisy_actions: torch.Tensor, 
                # timestep: torch.Tensor, 
                # temp
                views, 
                obs, 
                traj2ds=None, 
                # ac_flat = None, 
                # mask_flat = None, 
                text_embeddings = None, 
                vlm_attn_mask=None
                ) -> Tuple[torch.Tensor, torch.Tensor]:
        """Forward pass.

        Args:
            noisy_actions (`torch.Tensor`):
                Image features.
            timestep (`torch.Tensor`):
                Timestep in denoising process.
        Returns:
            `Tuple`[`torch.Tensor`, `torch.Tensor`]: The pair (latents, timestep_emb).
        """
        # DiffusionTransformerAgent.forward
        s_t, vlm_attn_mask = self.tokenize_obs(views, obs, traj2ds, text_embeddings=text_embeddings, vlm_attn_mask=vlm_attn_mask) # (B, S, d_model)
        # _DiTNoiseNet.forward_enc
        s_t = s_t.transpose(0, 1) # (S, B, d_model)
        pos = self.enc_pos(s_t) # (S, B, d_model)
        return (s_t + pos).transpose(0,1), vlm_attn_mask # (B,S,d_model)

    def tokenize_obs(self, views, obs, traj2ds = None, flatten=False, text_embeddings=None, vlm_attn_mask=None):
        
        view_tokens = self.views_proj(views)
        # V:views,相机、视角数量
        # S:每个view的token数(VLM长度)
        # D:token维度
        B,V,S,D = view_tokens.shape
        
        if traj2ds is not None:
            traj_tokens = self.embed({}, {f"cond{i}": traj2ds[:, i] for i in range(traj2ds.shape[1])})

            if self.token_fusion == "concat":
                traj_tokens = self.embed_proj(traj_tokens) #(B,S,d_model)
                tokens = torch.cat([view_tokens.view(B,V*S,D), traj_tokens], dim=1) # (B, tok1+tok2, 1536)
            elif self.token_fusion == "cross":  
                traj_tokens_list, view_tokens_list = [], []
                for v_idx in range(traj2ds.shape[1]):
                    # traj2d --> cross attend --> view features
                    view_tokens = self.attn(
                        hidden_states=traj_tokens[:, v_idx*self.traj_n_token, (v_idx+1)*self.traj_n_token],
                        encoder_hidden_states=views[:, v_idx],
                    )
                    traj_tokens_list.append(traj_tokens)
                    view_tokens_list.append(view_tokens)

                traj_tokens = torch.cat(traj_tokens_list, dim=1)
                traj_tokens = self.embed_proj(traj_tokens)
                view_tokens = torch.cat(view_tokens_list, dim=1)
                tokens = torch.cat([view_tokens, traj_tokens], dim=1) # (B, tok1+tok2, 1536)
            elif self.token_fusion == "perceiver":
                latents1 = self.latents1.repeat(B, 1, 1)
                # views = self.proj_in1(views)
                # views = views + text_embeddings[:, None]
                tokens1 = self.cross_attn1(
                    hidden_states=latents1,
                    encoder_hidden_states=view_tokens.view(B, V*S, D),
                )
                latents2 = self.latents2.repeat(B, 1, 1)
                traj_tokens = self.proj_in2(traj_tokens)
                # traj2ds = traj2ds + text_embeddings[:, None]
                tokens2 = self.cross_attn2(
                    hidden_states=latents2,
                    encoder_hidden_states=traj_tokens,
                )
                tokens = torch.cat([tokens1, tokens2], dim=1) # (B, tok1+tok2, 1536)
            else:
                raise ValueError
        else:
            assert self._n_conditions == 0, "inconsistent confg"
            tokens = view_tokens.view(B,V*S,D)

        # 用FiLM把text_embeddings融进token
        if (self.film is not None) and (text_embeddings is not None):
            tokens = self.film(tokens, text_embeddings)

        if self._obs_strat == "add_token":
            # <modify_states> 多帧state时,每一帧都作为一个proprio token参与注意力。
            obs_token = self._obs_proc(obs)#[:, None]
            tokens = torch.cat((tokens, obs_token), 1)
            # 原逻辑:
            # vlm_attn_mask = torch.cat([vlm_attn_mask, torch.ones((vlm_attn_mask.shape[0], 1), device=vlm_attn_mask.device)], 1) if vlm_attn_mask is not None else None
            if vlm_attn_mask is not None:
                obs_token_count = obs_token.shape[1] if len(obs_token.shape) == 3 else 1
                # <modify_states> 保持与原始训练代码一致:新增obs token mask继续使用默认float32,
                # 这样拼接后整体attention mask仍会提升成float32,满足后续断言。
                obs_token_mask = torch.ones(
                    (vlm_attn_mask.shape[0], obs_token_count),
                    device=vlm_attn_mask.device,
                )
                vlm_attn_mask = torch.cat([vlm_attn_mask, obs_token_mask], 1)
        elif self._obs_strat == "pad_img_tokens":
            obs = self._obs_proc(obs)
            obs = obs[:, None].repeat((1, tokens.shape[1], 1))
            tokens = torch.cat((obs, tokens), 2)
            vlm_attn_mask = None
        else:
            assert self._obs_strat is None

        tokens = self.post_proc(tokens)
        if flatten:
            return tokens.reshape((tokens.shape[0], -1)), vlm_attn_mask
        return tokens, vlm_attn_mask
    
    def embed(self, imgs, conditions=None):
        """ SONGLIN: DOWNSCALE == 2**5 = 32, total 5 conv2d with stride(2) """
        def embed_helper(net, im):
            # with torch.autocast(device_type='cuda', dtype=torch.bfloat16): # TODO check if this is needed
            if self.early_fusion and len(im.shape) == 5:
                T = im.shape[1]
                im = torch.cat([im[:, t] for t in range(T)], 1)
                embeds = net(im)
            elif len(im.shape) == 5:
                B, T, C, H, W = im.shape
                embeds = net(im.reshape((B * T, C, H, W)))
                embeds = embeds.reshape((B, -1, net.embed_dim))
            else:
                assert len(im.shape) == 4
                embeds = net(im)
            return embeds

        if self._share_cam_features:
            embeds = [
                embed_helper(self.visual_features, imgs[f"cam{i}"])
                for i in self.camera_indices
            ]
            if conditions is not None:
                assert self._n_conditions > 0
                embeds += [
                    embed_helper(self.visual_features, conditions[f"cond{i}"]) 
                    for i in range(self._n_conditions)
                ]
        else:
            assert conditions is not None
            # img_encoders = self.visual_features[:self._n_cams] # sum([v.sum().item() for k, v in img_encoders.named_parameters()])
            # cond_encoders = self.visual_features[self._n_cams:]
            cond_encoders = self.visual_features
            # [
            #     embed_helper(net, imgs[f"cam{i}"])
            #     for i, net in zip(self.camera_indices, img_encoders)
            # ] +
            embeds = [
                embed_helper(net, conditions[f"cond{0}"])
                for i, net in enumerate(cond_encoders)
            ]

        """ if self.training: # FIXME better handle this thing
            return torch.cat(embeds, dim=1) """
        assert conditions is not None
        in_dtype = next(iter(imgs.values())).dtype if len(imgs) > 0 else next(iter(conditions.values())).dtype
        embeds = torch.cat(embeds, dim=1).to(dtype=in_dtype) # (B, S, d_model)
        return embeds

class ActionProjectionIn(nn.Module):
    def __init__(
        self,
        action_pred_horizon,
        action_dim,
        output_dim,
    ):  
        super().__init__()
        self.action_pred_horizon = action_pred_horizon
        self.action_dim = action_dim

        self.ac_proj = nn.Sequential(
            nn.Linear(action_dim, action_dim),
            nn.GELU(approximate="tanh"),
            nn.Linear(action_dim, output_dim),
        )
        # self.time_net = _TimeNetwork(time_dim=256, out_dim=output_dim)

        # self.register_parameter(
        #     "dec_pos",
        #     nn.Parameter(torch.empty(action_pred_horizon, output_dim), requires_grad=True),
        # ) # (1, Tp, hidden_dim)
        self.dec_pos = nn.Parameter(torch.empty(action_pred_horizon, output_dim), requires_grad=True)
        nn.init.xavier_uniform_(self.dec_pos.data)

    def forward(self, noisy_actions: torch.Tensor, 
        # timestep
        ) -> torch.Tensor:
        B = noisy_actions.shape[0]
        # action_dim = self._dit_obs_proj.action_dim
        # time_enc = self.time_net(timestep)
        noise_acs = noisy_actions.reshape(B, -1, self.action_dim) # DiffusionTransformerAgent.forward
        ac_tokens = self.ac_proj(noise_acs) # noisy_actions, _DiTNoiseNet.forward
        # ac_tokens = ac_tokens.transpose(0, 1) # (B, L, d_model) _DiTNoiseNet.forward_dec
        action_hidden_states = ac_tokens + self.dec_pos.unsqueeze(0)
        return action_hidden_states#, time_enc
    

class ActionProjectionOut(nn.Module): # _FinalLayer
    def __init__(self, hidden_size, action_dim):
        super().__init__()
        self.norm_final = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6)
        self.linear = nn.Linear(hidden_size, action_dim, bias=True)
        self.adaLN_modulation = nn.Sequential(
            nn.SiLU(), nn.Linear(hidden_size, 2 * hidden_size, bias=True)
        )
        # self.reset_parameters()

    def forward(self, x, t, cond=None):
        # process the conditioning vector first
        # cond = torch.mean(cond, dim=1)
        # cond = cond + t

        if len(t.shape) == 2: # (B, D)
            t = t.unsqueeze(1) # (B, 1, D)
        elif len(t.shape) == 3: # (B, T, D)
            pass
        else:
            raise ValueError(f"Invalid shape of t: {t.shape}")
        shift, scale = self.adaLN_modulation(t).chunk(2, dim=-1) # cond -> t
        # x = self.norm_final(x) * (1+scale[None]) + shift[None] # TODO be careful!
        # x = x * scale + shift
        x = self.norm_final(x) * (1 + scale) + shift
        x = self.linear(x)
        # return x.transpose(0, 1)
        return x
    

# @maybe_allow_in_graph
class VLATransformerBlock(nn.Module):

    def __init__(
        self,
        dim: int,
        num_attention_heads: int,
        attention_head_dim: int,
        context_pre_only: bool = False,
        qk_norm: Optional[str] = None,
        use_dual_attention: bool = False,
        training_phase: str = "joint",  # "joint", "action", "traj2d", "action_mini"
    ):
        super().__init__()

        # self.training_phase = training_phase

        # from models.dit_policy.data4robotics.models.diffusion import  _SelfAttnEncoder #_TransformerEncoder,
        # self.encoder_module = _SelfAttnEncoder(
        #     d_model=dim,
        #     nhead=num_attention_heads,
        #     dim_feedforward=2048,
        #     dropout=0.1,
        #     activation="gelu",
        # )
        # self.encoder_module.reset_parameters() 

        # # decoder blocks
        # from models.dit_policy.data4robotics.models.diffusion import _DiTDecoder #_TransformerDecoder, 
        # self.decoder_module = _DiTDecoder(
        #     d_model=dim,
        #     nhead=num_attention_heads,
        #     dim_feedforward=2048,
        #     dropout=0.1,
        #     activation="gelu",
        # )
        # self.decoder_module.reset_parameters() # _TransformerDecoder.__init__

        self.context_pre_only = context_pre_only
        context_norm_type = "ada_norm_continous" if context_pre_only else "ada_norm_zero"

        # self.norm1 = AdaLayerNormZero(dim)
        self.norm1_act = AdaLayerNormZero(dim)
        logger.debug(f"AdaLN act parameters: {count_parameters(self.norm1_act):,}")
        if context_norm_type == "ada_norm_continous":
            """ self.norm1_lang = AdaLayerNormContinuous(dim, dim, elementwise_affine=False, eps=1e-6, bias=True, norm_type="layer_norm") """
            self.norm1_obs = AdaLayerNormContinuous(dim, dim, elementwise_affine=False, eps=1e-6, bias=True, norm_type="layer_norm")
            logger.debug(f"AdaLN obs parameters: {count_parameters(self.norm1_obs):,}")
        elif context_norm_type == "ada_norm_zero":
            """ self.norm1_lang = AdaLayerNormZero(dim) """
            self.norm1_obs = AdaLayerNormZero(dim)
            logger.debug(f"AdaLN obs parameters: {count_parameters(self.norm1_obs):,}")
        else:
            raise ValueError(
                f"Unknown context_norm_type: {context_norm_type}, currently only support `ada_norm_continous`, `ada_norm_zero`"
            )
        
        if hasattr(F, "scaled_dot_product_attention"):
            processor = JointVLAAttnProcessor(
                query_dim=dim,
                dim_head=attention_head_dim,
                heads=num_attention_heads,
                out_dim=dim,
                bias=True,
                context_pre_only=context_pre_only,
            )
        else:
            raise ValueError(
                "The current PyTorch version does not support the `scaled_dot_product_attention` function."
            )

        self.attn = Attention(
            query_dim=dim,
            cross_attention_dim=None,
            added_kv_proj_dim=dim,
            dim_head=attention_head_dim,
            heads=num_attention_heads,
            out_dim=dim,
            context_pre_only=context_pre_only,
            bias=True,
            processor=processor, # type: ignore
            qk_norm=qk_norm,
            eps=1e-6,
        )
        logger.debug(f"Joint Attention parameters: {count_parameters(self.attn):,}")

        self.norm2_act = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
        self.ff_act = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate")
        logger.debug(f"FF act parameters: {count_parameters(self.ff_act):,}")

        if not context_pre_only:
            """ self.norm2_lang = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
            self.ff_lang = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate") """

            self.norm2_obs = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
            self.ff_obs = FeedForward(dim=dim, dim_out=dim, activation_fn="gelu-approximate")
            logger.debug(f"FF obs parameters: {count_parameters(self.ff_obs):,}")
        else:
            """ self.norm2_lang = None
            self.ff_lang = None """

            self.norm2_obs= None
            self.ff_obs = None

        # let chunk size default to None
        self._chunk_size = None
        self._chunk_dim = 0

    def forward(
        self, 
        # hidden_states: torch.FloatTensor,  #  V
        # lang_hidden_states: torch.FloatTensor,  # L
        action_hidden_states: torch.Tensor, # A
        obs_hidden_states: torch.Tensor, # O
        temb: torch.Tensor,
        obs_token_mask: Optional[torch.Tensor] = None,
        # obs_pos_emb: Optional[torch.FloatTensor] = None,
        # time_enc: Optional[torch.FloatTensor] = None,
        joint_attention_kwargs=None,
    ):
        # obs_hidden_states = self.encoder_module(src=obs_hidden_states, pos=obs_pos_emb)
        # action_hidden_states = self.decoder_module(x=action_hidden_states, t=time_enc, cond=obs_hidden_states) # recursive: _TransformerDecoder.forward
        
        norm_action_hidden_states, gate_msa_act, shift_mlp_act, scale_mlp_act, gate_mlp_act = self.norm1_act(action_hidden_states, emb=temb)

        if self.context_pre_only:
            """ norm_lang_hidden_states = self.norm1_lang(lang_hidden_states, temb) """
            norm_obs_hidden_states = self.norm1_obs(obs_hidden_states, temb[:,-1] if len(temb.shape) > 2 else temb)

            gate_msa_obs, shift_mlp_obs, scale_mlp_obs, gate_mlp_obs = None, None, None, None
        else:
            """ norm_lang_hidden_states, c_gate_msa, c_shift_mlp, c_scale_mlp, c_gate_mlp = self.norm1_lang(
                lang_hidden_states, emb=temb
            ) """
            norm_obs_hidden_states, gate_msa_obs, shift_mlp_obs, scale_mlp_obs, gate_mlp_obs = self.norm1_obs(obs_hidden_states, emb=temb[:,-1] if len(temb.shape) > 2 else temb)

        act_attn_output, obs_attn_output = self.attn( ## indirectly calls JointAttnProcessor2_0.__call__, VLAAttnProcessor.__call__
            # hidden_states=None,
            # lang_hidden_states=norm_lang_hidden_states,
            hidden_states=norm_action_hidden_states,
            encoder_hidden_states=norm_obs_hidden_states,
            attention_mask=obs_token_mask
            # **joint_attention_kwargs,
        )

        # Action feed forward
        act_attn_output = gate_msa_act * act_attn_output
        action_hidden_states = action_hidden_states + act_attn_output

        norm_action_hidden_states = self.norm2_act(action_hidden_states)
        norm_action_hidden_states = norm_action_hidden_states * (1 + scale_mlp_act) + shift_mlp_act
        ff_action_output = self.ff_act(norm_action_hidden_states)

        ff_action_output = gate_mlp_act * ff_action_output
        action_hidden_states = action_hidden_states + ff_action_output

        # attention outputs for the `obs_hidden_states`.
        if self.context_pre_only:
            # encoder_hidden_states = None
            obs_hidden_states = None # type:ignore
        else:
            assert gate_msa_obs is not None and \
                shift_mlp_obs is not None and \
                    scale_mlp_obs is not None and \
                        gate_mlp_obs is not None
            """ # lang forward
            lang_attn_output = c_gate_msa.unsqueeze(1) * lang_attn_output
            lang_hidden_states = lang_hidden_states + lang_attn_output
            norm_lang_hidden_states = self.norm2_lang(lang_hidden_states)
            norm_lang_hidden_states = norm_lang_hidden_states * (1 + c_scale_mlp[:, None]) + c_shift_mlp[:, None]
            if self._chunk_size is not None:
                # "feed_forward_chunk_size" can be used to save memory
                context_ff_output = _chunked_feed_forward(
                    self.ff_lang, norm_lang_hidden_states, self._chunk_dim, self._chunk_size
                )
            else:
                context_ff_output = self.ff_lang(norm_lang_hidden_states)
            lang_hidden_states = lang_hidden_states + c_gate_mlp.unsqueeze(1) * context_ff_output """
            
            # obs feedforward
            assert self.norm2_obs is not None

            obs_attn_output = gate_msa_obs * obs_attn_output
            obs_hidden_states = obs_hidden_states + obs_attn_output

            norm_obs_hidden_states = self.norm2_obs(obs_hidden_states)
            norm_obs_hidden_states = norm_obs_hidden_states * (1 + scale_mlp_obs) + shift_mlp_obs

            assert self.ff_obs is not None
            ff_obs_output = self.ff_obs(norm_obs_hidden_states)
            ff_obs_output = gate_mlp_obs * ff_obs_output
            obs_hidden_states = obs_hidden_states + ff_obs_output

        return action_hidden_states, obs_hidden_states
    
class ActionTransformerModel(
    ModelMixin, ConfigMixin #, VLTTransformerActionMixin
    # PeftAdapterMixin, FromOriginalModelMixin, ReprodMixin, SD3Transformer2DLoadersMixin, 
):  
    """  An action transformer model """

    _supports_gradient_checkpointing = True
    _no_split_modules = ["ActionTransformerBlock"]
    # _skip_layerwise_casting_patterns = ["pos_embed", "norm"]

    @classmethod
    def from_pretrained(cls,  pretrained_model_name_or_path: Optional[Union[str, os.PathLike]], **kwargs):
        # model = cls.from_config(config=None, **kwargs)
        model = ActionTransformerModel()
        torch_dtype = kwargs.pop("torch_dtype", torch.float32)
        if pretrained_model_name_or_path is not None and "checkpoints" in str(pretrained_model_name_or_path):
            if os.path.exists(pretrained_model_name_or_path):
                 # NOTE use accelerator.checkpointing 
                from safetensors.torch import load_file
                file_path = f"{pretrained_model_name_or_path}/model.safetensors"
                assert os.path.exists(file_path), f"Checkpoint {file_path} does not exist."
                model_state_dict = load_file(file_path)
                missing_keys, unexpected_keys = model.load_state_dict(model_state_dict, strict=False)
                
                """ # load all checkpoint data, including weights, optimizer, etc.
                load_dict = torch.load(pretrained_model_name_or_path, weights_only=False) 
                from torch.nn.parallel import DistributedDataParallel as DDP
                model = model.module if isinstance(model, DDP) else model
                missing_keys, unexpected_keys = model.load_state_dict(load_dict["model"], strict=False)  """

                if len(unexpected_keys) > 0:
                    logger.info(f"Unexpected keys in the checkpoint: {unexpected_keys}. ")
                if len(missing_keys) > 0:
                    logger.info(f"Missing keys in the checkpoint: {missing_keys}.")

                logger.info(f"Loaded model from {pretrained_model_name_or_path}")
            else:
                raise ValueError(f"Checkpoint {pretrained_model_name_or_path} does not exist. ")
        
        model.to(torch_dtype)
        return model

    @register_to_config
    def __init__(
        self,
        # sample_size: int = 128,
        # patch_size: int = 2,
        # in_channels: int = 16,
        # num_layers: int = 24,
        attention_head_dim: int = 64,
        num_attention_heads: int = 24,
        # joint_attention_dim: int = 4096,
        caption_projection_dim: int = 1536,
        pooled_projection_dim: int = 2048,
        # out_channels: int = 16,
        pos_embed_max_size: int = 192,
        dual_attention_layers: Tuple[
            int, ...
        ] = (),  # () for sd3.0; (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) for sd3.5
        qk_norm: Optional[str] = None,
        # VLA parameters
        training_phase: str = "joint",  # "joint", "action", "traj2d", "action_mini"
        # action head parameters
        action_num_blocks:int = 6,
        action_pred_horizon: int = 6,
        action_dim: int = 7,
        action_hidden_dim: int = 1536,
        action_nheads: int = 24,
        # action_attention_head_dim: Optional[int] = 64,
        n_conditions: int = 0,
        token_fusion: str = "concat",  # "concat", "cross"
        resnet_store_path: str = "cache/visual_features/resnet18/IN_1M_resnet18.pth",
        odim: int = 32,
        view_feature_dim: int = 1920,
        use_film: bool = False,
        combined_temb: bool = False,
        use_future_progress_embedding: bool = False,
        future_progress_dim: int = 5,
    ):
        super().__init__()
        # self.inner_dim = num_attention_heads * attention_head_dim
        self.inner_dim = num_attention_heads * attention_head_dim 

        # TODO-yth: 扩散时间步embedding是否融合文本语义
        self.combined_temb = combined_temb
        if self.combined_temb:
            self.time_ins_embed = CombinedTimestepTextProjEmbeddings(
                embedding_dim=self.inner_dim, pooled_projection_dim=pooled_projection_dim
            ) # used
        else:
            self.time_ins_embed = _TimeNetwork(time_dim=256, out_dim=action_hidden_dim)
        """ self.lang_embedder = nn.Linear(joint_attention_dim, caption_projection_dim) """

        # Set the observation projection
        self.obs_proj = ObservationProjection(
            action_pred_horizon=action_pred_horizon, 
            action_dim=action_dim, 
            hidden_dim=self.inner_dim, # not used
            output_dim=action_hidden_dim,
            n_conditions=n_conditions,
            token_fusion=token_fusion,
            resnet_store_path=resnet_store_path,
            odim=odim,
            view_feature_dim=view_feature_dim,
            use_film=use_film
        )

        total_params = sum(p.numel() for p in self.obs_proj.parameters() if p.requires_grad)
        logger.debug(f"ObservationEncoder parameters: {total_params:,}")
        # Set the action projection
        self.action_proj_in = ActionProjectionIn(
            action_pred_horizon=action_pred_horizon, 
            action_dim=action_dim, 
            output_dim=action_hidden_dim,
        )
        self.use_future_progress_embedding = bool(use_future_progress_embedding)
        self.future_progress_dim = int(future_progress_dim)
        if self.use_future_progress_embedding:
            self.future_progress_mlp = nn.Sequential(
                nn.Linear(self.future_progress_dim, action_hidden_dim),
                nn.SiLU(),
                nn.Linear(action_hidden_dim, action_hidden_dim),
            )
        else:
            self.future_progress_mlp = None
        total_params = sum(p.numel() for p in self.action_proj_in.parameters() if p.requires_grad)
        logger.debug(f"ActionProjectionIn parameters: {total_params:,}")

        self.transformer_blocks = nn.ModuleList(
            [
                # IdentityBlock()
                VLATransformerBlock( # JointTransformerBlock
                    dim=action_hidden_dim,
                    num_attention_heads=action_nheads,
                    attention_head_dim=attention_head_dim,
                    context_pre_only=i == action_num_blocks - 1,
                    qk_norm=qk_norm,
                    # use_dual_attention=True if i in dual_attention_layers else False,
                    training_phase=training_phase,  # "joint", "action", "traj2d", "action_mini"
                )
                for i in range(action_num_blocks)
            ]
        )
        total_params = sum(p.numel() for p in self.transformer_blocks[0].parameters() if p.requires_grad)
        logger.debug(f"ActionTransformerBlock parameters: {total_params:,}")

        self.action_proj_out = ActionProjectionOut(
            hidden_size=action_hidden_dim, 
            action_dim=action_dim
        )
        total_params = sum(p.numel() for p in self.action_proj_out.parameters())
        logger.debug(f"ActionProjectionOut parameters: {total_params:,}")

    def forward(
        self,
        hidden_states: torch.Tensor,
        encoder_hidden_states: Optional[torch.Tensor] = None,
        pooled_projections: Optional[torch.Tensor] = None,
        timestep: Optional[torch.LongTensor] = None,
        block_controlnet_hidden_states: Optional[List] = None,
        joint_attention_kwargs: Optional[Dict[str, Any]] = None,
        vlm_attn_mask: Optional[torch.Tensor] = None,
        return_dict: bool = True,
        skip_layers: Optional[List[int]] = None,
    ) -> Union[List[torch.Tensor], ActionTransformerModelOutput]: 
        assert joint_attention_kwargs is not None

        if self.config.n_conditions > 0: # type: ignore
            assert joint_attention_kwargs["traj2ds"].shape[1] == self.config.n_conditions, "model n_conditions does not match" # type: ignore

        # L
        if self.combined_temb:
            temb = self.time_ins_embed(timestep, pooled_projections)
        else: 
            temb = self.time_ins_embed(timestep)

        """ lang_hidden_states = self.lang_embedder(encoder_hidden_states) """

        # A
        noisy_action = joint_attention_kwargs.pop("action_hidden_embeds")
        action_hidden_states = self.action_proj_in(noisy_action)
        if self.use_future_progress_embedding:
            future_progress = joint_attention_kwargs.get("future_progress", None)
            if future_progress is None:
                raise ValueError("use_future_progress_embedding=True requires future_progress tensor")
            assert self.future_progress_mlp is not None
            future_progress = future_progress.to(
                device=action_hidden_states.device,
                dtype=action_hidden_states.dtype,
            )
            if future_progress.shape[:2] != action_hidden_states.shape[:2]:
                raise ValueError(
                    f"future_progress shape {future_progress.shape} does not match action tokens "
                    f"{action_hidden_states.shape[:2]}"
                )
            action_hidden_states = action_hidden_states + self.future_progress_mlp(future_progress)

        # Obs: V+Proprio
        obs_hidden_states, obs_token_mask = self.obs_proj(
            views=joint_attention_kwargs["views"],
            obs=joint_attention_kwargs["obs"],
            traj2ds=joint_attention_kwargs["traj2ds"],
            text_embeddings=pooled_projections,
            vlm_attn_mask=vlm_attn_mask
        ) # S, B, d_model
        
        for index_block, block in enumerate(self.transformer_blocks):
            is_skip = True if skip_layers is not None and index_block in skip_layers else False
            if is_skip:
                continue

            action_hidden_states, obs_hidden_states = block(
                # hidden_states=hidden_states, # V
                # lang_hidden_states=lang_hidden_states, # L
                action_hidden_states=action_hidden_states, # A
                obs_hidden_states=obs_hidden_states, # O
                temb=temb,
                obs_token_mask=obs_token_mask
                # time_enc=time_enc,
                # obs_pos_emb=obs_pos_emb,
                # joint_attention_kwargs=joint_attention_kwargs,
            )

        action_output = self.action_proj_out(
            x=action_hidden_states,
            t=temb,
            # cond=obs_hidden_states,
        )
        return ActionTransformerModelOutput(action=action_output)


class DiTActionTransformerModel(
    ModelMixin, ConfigMixin #, VLTTransformerActionMixin
    # PeftAdapterMixin, FromOriginalModelMixin, ReprodMixin, SD3Transformer2DLoadersMixin, 
):  
    """  An action transformer model """

    _supports_gradient_checkpointing = True
    # _no_split_modules = ["ActionTransformerBlock"]
    # _skip_layerwise_casting_patterns = ["pos_embed", "norm"]

    @register_to_config
    def __init__(
        self,
        # sample_size: int = 128,
        # patch_size: int = 2,
        # in_channels: int = 16,
        # num_layers: int = 24,
        attention_head_dim: int = 64,
        num_attention_heads: int = 24,
        # joint_attention_dim: int = 4096,
        caption_projection_dim: int = 1536,
        pooled_projection_dim: int = 2048,
        # out_channels: int = 16,
        pos_embed_max_size: int = 192,
        dual_attention_layers: Tuple[
            int, ...
        ] = (),  # () for sd3.0; (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) for sd3.5
        qk_norm: Optional[str] = None,
        # VLA parameters
        training_phase: str = "joint",  # "joint", "action", "traj2d", "action_mini"
        # action head parameters
        action_num_blocks:int = 6,
        action_pred_horizon: int = 6,
        action_dim: int = 7,
        action_hidden_dim: int = 1536, #this
        action_nheads: int = 24,
        # action_attention_head_dim: Optional[int] = 64,
        n_conditions: int = 0,
        token_fusion: str = "concat",  # "concat", "cross"
        resnet_store_path: str = "cache/visual_features/resnet18/IN_1M_resnet18.pth",
        odim: int = 32,
        view_feature_dim: int = 1920,
        use_film: bool = False,
        combined_temb: bool = False,
        use_future_progress_embedding: bool = False,
        future_progress_dim: int = 5,
    ):
        super().__init__()
        # self.inner_dim = num_attention_heads * attention_head_dim
        self.inner_dim = num_attention_heads * attention_head_dim
        if use_future_progress_embedding:
            raise ValueError("DiTActionTransformerModel does not support future progress embedding")

        # Set the observation projection
        self.obs_proj = ObservationProjection(
            action_pred_horizon=action_pred_horizon, 
            action_dim=action_dim, 
            hidden_dim=self.inner_dim, # not used
            output_dim=action_hidden_dim,
            n_conditions=n_conditions,
            token_fusion="concat",
            resnet_store_path=resnet_store_path,
            odim=odim,
            view_feature_dim=view_feature_dim,
            use_film=use_film
        )

        total_params = sum(p.numel() for p in self.obs_proj.parameters() if p.requires_grad)
        logger.debug(f"ObservationEncoder parameters: {total_params:,}")


        # q_former:
        # TODO: boqian: hardcode here
        num_query_tokens = 256
        num_heads_ = 16
        self.query_tokens = nn.Parameter(torch.randn(num_query_tokens, action_hidden_dim))
        self.cross_attention = CrossAttentionBlock(action_hidden_dim, num_heads_)


        # assert action_hidden_dim == 1024
        self.DiT = DiT(
            in_channels=action_dim,
            depth=24,
            num_heads=16,
            token_size=action_hidden_dim,
            learn_sigma=False,
            class_dropout_prob=0,
            future_action_window_size=action_pred_horizon-1,
            past_action_window_size=0,
            n_conditon_token=num_query_tokens
        )
        total_params = sum(p.numel() for p in self.DiT.parameters() if p.requires_grad)
        logger.debug(f"DiT parameters: {total_params:,}")


    def forward(
        self,
        hidden_states: torch.Tensor,
        encoder_hidden_states: Optional[torch.Tensor] = None,
        pooled_projections: Optional[torch.Tensor] = None,
        timestep: Optional[torch.LongTensor] = None,
        block_controlnet_hidden_states: Optional[List] = None,
        joint_attention_kwargs: Optional[Dict[str, Any]] = None,
        vlm_attn_mask: Optional[torch.Tensor] = None,
        return_dict: bool = True,
        skip_layers: Optional[List[int]] = None,
    ) -> Union[List[torch.Tensor], ActionTransformerModelOutput]: 
        assert joint_attention_kwargs is not None

        if self.config.n_conditions > 0: # type: ignore
            assert joint_attention_kwargs["traj2ds"].shape[1] == self.config.n_conditions, "model n_conditions does not match" # type: ignore


        """ lang_hidden_states = self.lang_embedder(encoder_hidden_states) """

        # A
        noisy_action = joint_attention_kwargs.pop("action_hidden_embeds")

        # Obs: V+Proprio
        condition_tokens, vlm_attn_mask = self.obs_proj(
            views=joint_attention_kwargs["views"],
            obs=joint_attention_kwargs["obs"],
            traj2ds=joint_attention_kwargs["traj2ds"],
            text_embeddings=pooled_projections,
            vlm_attn_mask=vlm_attn_mask
        ) # B, S, d_model

        # print("condition_tokens.shape:", condition_tokens.shape) # [B, 106, 1024])


        # q_former
        # TODO: forward vlm_attn_mask here 
        query = self.query_tokens.unsqueeze(0).expand(condition_tokens.shape[0], -1, -1) # (B, Q, D)
        queried = self.cross_attention(query, condition_tokens) # (B, Q, D)


        # DiT
        action_output = self.DiT(noisy_action, timestep, queried)
        

        return ActionTransformerModelOutput(action=action_output)


class HumanFoundationModel(nn.Module):

    def __init__(self, model_cfg, feature_extractors: dict[str, str]):
        super().__init__()
        
        # self.qwen = _QWen_VL_Interface(config=model_cfg)
        # self.layer_qformer = get_layerwise_qformer(config=self.config)
        # self.action_model = get_action_model(config=self.config)

        action_nheads = int(model_cfg.nhead)
        if model_cfg.hidden_dim % action_nheads != 0:
            raise ValueError(
                f"Action hidden dim {model_cfg.hidden_dim} must be divisible by nhead {action_nheads}"
            )
        action_head_dim = model_cfg.hidden_dim // action_nheads
        self.action_header = ActionTransformerModel(
            attention_head_dim=action_head_dim,
            num_attention_heads=action_nheads,
            action_num_blocks=model_cfg.num_blocks,
            action_nheads=action_nheads,
            resnet_store_path=model_cfg.resnet_store_path,
            odim=model_cfg.odim,
            action_dim=model_cfg.action_dim,
            action_pred_horizon=model_cfg.action_chunk_size,
            view_feature_dim=model_cfg.view_feature_dim,
            use_film=model_cfg.use_film,
            combined_temb=model_cfg.combined_temb,
            action_hidden_dim=model_cfg.hidden_dim,
            use_future_progress_embedding=getattr(model_cfg, "use_future_progress_embedding", False),
            future_progress_dim=getattr(model_cfg, "future_progress_dim", 5),
            )

        total_params = sum(p.numel() for p in self.action_header.parameters())
        overwatch.info(f"Total ActionTransformerModel parameters: {total_params:,}")

        vision_backbones = {}
        for backbone, model_path in feature_extractors.items():
            if backbone == "siglip":
                siglip = SiglipModel.from_pretrained(
                    model_path
                )  # , resume_download=None, local_files_only=True
                vision_backbones[backbone] = siglip.vision_model
                vision_backbones[backbone].head = nn.Identity() # remove classification head
                del siglip
            elif backbone == "dinov2":
                vision_backbones[backbone] = Dinov2Model.from_pretrained(
                    model_path
                )  # , resume_download=None, local_files_only=True
                vision_backbones[backbone].embeddings.mask_token = None
            else:
                raise ValueError(f"Unsupported vision backbone: {backbone}")
            
        self.vision_backbones = nn.ModuleDict(vision_backbones)

        for name, backbone in self.vision_backbones.items():
            num_parameters = sum(p.numel() for p in backbone.parameters() if p.requires_grad)
            overwatch.info(f"Vision backbone {name} has {num_parameters*1e-6:,}M parameters")
            
    def tune_vision_backbones(self, backbone_names: List[str]):
        for name, backbone in self.vision_backbones.items():
            if name in backbone_names:
                overwatch.info(f"Tuning vision backbone: {name}")
                for param in backbone.parameters():
                    param.requires_grad = True
            else:
                overwatch.info(f"Freezing vision backbone: {name}")
                for param in backbone.parameters():
                    param.requires_grad = False
    
    def extract_vision_features(self, batch):
        features = []
        for vision_backbone, model in self.vision_backbones.items():
            assert vision_backbone in batch
            B, V, C, H, W = batch[vision_backbone].shape
            if vision_backbone == "siglip":
                vision_outputs = model(
                    pixel_values=batch[vision_backbone].reshape(B * V, C, H, W),
                    return_dict=True,
                )
                siglip_features = vision_outputs.last_hidden_state
                features.append(siglip_features.view(B, V, *siglip_features.shape[1:]))
            elif vision_backbone == "dinov2":
                dinov2_outputs = model(
                    pixel_values=batch[vision_backbone].view(B * V, C, H, W)
                )
                dinov2_features = dinov2_outputs.last_hidden_state[:, 1:, :]
                features.append(dinov2_features.view(B, V, *dinov2_features.shape[1:]))
            else:
                raise ValueError(f"Unsupported vision backbone: {vision_backbone}")
        return torch.cat(features, dim=3)  # (B,V,N,D)
    
    def forward(
        self,
        action_samples: torch.Tensor,
        views: dict[str, torch.Tensor],
        states: torch.Tensor,
        # hidden_states: torch.Tensor,
        lang_hidden_states: torch.Tensor,
        pooled_projections: torch.Tensor,
        timestep: torch.LongTensor,
        # block_controlnet_hidden_states: List,
        # joint_attention_kwargs: Optional[Dict[str, Any]],
        traj2ds: Optional[torch.Tensor] = None,
        return_dict: bool = True,
        skip_layers: Optional[List[int]] = None,
    ) -> Union[List[torch.Tensor], HumanFoundationModelOutput]: 

        view_features = self.extract_vision_features(views)

        model_output = self.action_header(
            hidden_states=None,
            encoder_hidden_states=lang_hidden_states, # (B, 333, 4096)
            timestep=timestep,
            pooled_projections=pooled_projections, # (B, 2048)
            joint_attention_kwargs=dict(
                action_hidden_embeds=action_samples, # (B,Tp,Da)
                views=view_features,  # (B,V,N,D)
                obs=states,  # (B,1,M)
                traj2ds=traj2ds,  # (B, C, 3, H, W)
                # text_embeddings=text_embeddings
            ),
            return_dict=return_dict,
        )
        return HumanFoundationModelOutput(action=model_output.action)

    @torch.inference_mode()
    def predict_action(
        self,
        observations: List[List[Image.Image]],  # B * List of PIL Image as [view1, view2]
        states: List[Any],
        instructions: List[str],
        **kwargs: str
    ) -> np.ndarray:
        # with torch.autocast("cuda", dtype=torch.float32):
        ...

    @torch.inference_mode()
    def chat(self):
        ...
    


class Psi0Model(nn.Module):

    # 这里改不改2.5无所谓,外部会传参(来自posttrain.py的init_model)
    def __init__(self, model_cfg, vlm_model: Qwen3VLForConditionalGeneration):
        super().__init__()
        
        if model_cfg.use_dit:
            self.action_header = DiTActionTransformerModel(
                resnet_store_path=model_cfg.resnet_store_path,
                odim=model_cfg.odim,
                action_dim=model_cfg.action_dim,
                action_pred_horizon=model_cfg.action_chunk_size,
                view_feature_dim=model_cfg.view_feature_dim,
                use_film=model_cfg.use_film,
                combined_temb=model_cfg.combined_temb,
                action_hidden_dim=model_cfg.hidden_dim,
                use_future_progress_embedding=getattr(model_cfg, "use_future_progress_embedding", False),
                future_progress_dim=getattr(model_cfg, "future_progress_dim", 5),
            )
        else:
            action_nheads = int(model_cfg.nhead)
            if model_cfg.hidden_dim % action_nheads != 0:
                raise ValueError(
                    f"Action hidden dim {model_cfg.hidden_dim} must be divisible by nhead {action_nheads}"
                )
            action_head_dim = model_cfg.hidden_dim // action_nheads
            self.action_header = ActionTransformerModel(
                attention_head_dim=action_head_dim,
                num_attention_heads=action_nheads,
                action_num_blocks=model_cfg.num_blocks,
                action_nheads=action_nheads,
                resnet_store_path=model_cfg.resnet_store_path,
                odim=model_cfg.odim,
                action_dim=model_cfg.action_dim,
                action_pred_horizon=model_cfg.action_chunk_size,
                view_feature_dim=model_cfg.view_feature_dim,
                use_film=model_cfg.use_film,
                combined_temb=model_cfg.combined_temb,
                action_hidden_dim=model_cfg.hidden_dim,
                use_future_progress_embedding=getattr(model_cfg, "use_future_progress_embedding", False),
                future_progress_dim=getattr(model_cfg, "future_progress_dim", 5),
            )


        total_params = sum(p.numel() for p in self.action_header.parameters())
        overwatch.info(f"Total ActionTransformerModel parameters: {total_params:,}")

        self.vlm_model = vlm_model
        total_params = sum(p.numel() for p in self.vlm_model.parameters())
        overwatch.info(f"Total VLM Backbone parameters: {total_params:,}")

    # load pretrained vlm+action head, and all the modules needed for predict_action
    # TODO-yth: 暂时没看怎么改加载预训练
    @classmethod
    def from_pretrained(cls, run_dir, ckpt_step, launch_config:LaunchConfig, device):
        if not os.path.exists(run_dir):
            raise ValueError(f"Pretrained model path {run_dir} does not exist.")

        from safetensors.torch import load_file
        # <modify> 支持infer脚本传入best_ckpt目录名,用于读取checkpoints/best_ckpt。
        # 原逻辑:
        # ckpt_dir = f"{run_dir}/checkpoints/ckpt_{ckpt_step}"
        if str(ckpt_step) in {"best", "best_ckpt"}:
            ckpt_dir = f"{run_dir}/checkpoints/best_ckpt"
        else:
            ckpt_dir = f"{run_dir}/checkpoints/ckpt_{ckpt_step}"
        # <modify> 兼容训练中间保存到run根目录的ckpt_xxx,例如用户当前的.../2604192034/ckpt_8000。
        # 原逻辑只读取run_dir/checkpoints/ckpt_xxx。
        if not os.path.exists(ckpt_dir):
            root_ckpt_dir = f"{run_dir}/ckpt_{ckpt_step}"
            if os.path.exists(root_ckpt_dir):
                ckpt_dir = root_ckpt_dir
        # ckpt_dir = f"{run_dir}/checkpoints/ckpt_800"
        file_path = f"{ckpt_dir}/model.safetensors"
        index_path = f"{ckpt_dir}/model.safetensors.index.json"

        if os.path.exists(file_path):
            state_dict = load_file(file_path, device="cpu")
        elif os.path.exists(index_path):
            with open(index_path, "r") as f:
                index_obj = json.load(f)
            weight_map = index_obj.get("weight_map", {})
            shard_files = sorted(set(weight_map.values()))
            state_dict = {}
            for shard_file in shard_files:
                shard_path = os.path.join(ckpt_dir, shard_file)
                if not os.path.exists(shard_path):
                    raise ValueError(f"Checkpoint shard {shard_path} does not exist.")
                state_dict.update(load_file(shard_path, device="cpu"))
        else:
            raise ValueError(f"Checkpoint file {file_path} does not exist.")

        model_name_or_path = launch_config.model.model_name_or_path
        is_omni = "omni" in model_name_or_path.lower()
        # <modify_float> 增加float32推理开关;开启后避免FlashAttention2(fp16/bf16-only)报错。
        # 原逻辑:
        # attn_impl = "flash_attention_2" if str(device).startswith("cuda") else "sdpa"
        force_float32_infer = os.getenv("PSI_INFER_FLOAT32", "0") == "1"
        if force_float32_infer:
            attn_impl = "sdpa"
            vlm_dtype = torch.float32
        else:
            attn_impl = "flash_attention_2" if str(device).startswith("cuda") else "sdpa"
            vlm_dtype = torch.bfloat16

        if is_omni:
            vlm_model = Qwen2_5OmniThinkerForConditionalGeneration.from_pretrained(
                model_name_or_path,
                attn_implementation=attn_impl,
                dtype=vlm_dtype,
            )
        else:
            # init empty vlm backbone from config only (skip loading base pretrained weights)
            vlm_config = AutoConfig.from_pretrained(model_name_or_path)
            vlm_config._attn_implementation = attn_impl
            vlm_model = Qwen3VLForConditionalGeneration(vlm_config)
        # <modify_float> float32开关打开时,VLM保持float32;默认保持bf16。
        # 原逻辑:
        # vlm_model = vlm_model.to(dtype=torch.bfloat16) # type: ignore
        vlm_model = vlm_model.to(dtype=vlm_dtype) # type: ignore

        vlm_state_dict = {}
        for k, v in state_dict.items():
            if k.startswith("action_header."):
                continue
            elif k.startswith('vlm_model.'):
                vlm_state_dict[k.replace("vlm_model.", "")] = v
            elif k.startswith("motion_encoder."):
                continue
            else:
                assert False, "check here"

        if "lm_head.weight" not in vlm_state_dict:
            if "model.language_model.embed_tokens.weight" in vlm_state_dict:
                vlm_state_dict["lm_head.weight"] = vlm_state_dict["model.language_model.embed_tokens.weight"]
            elif "model.embed_tokens.weight" in vlm_state_dict:
                vlm_state_dict["lm_head.weight"] = vlm_state_dict["model.embed_tokens.weight"]

        if vlm_state_dict["lm_head.weight"].shape[0] != vlm_model.lm_head.weight.shape[0]:
            vlm_model.resize_token_embeddings(
                vlm_state_dict["lm_head.weight"].shape[0], 
                pad_to_multiple_of = 192,
                mean_resizing = True
            )
            overwatch.info(f"Resized model token embeddings to {vlm_model.lm_head.weight.shape[0]}")

        vlm_model.load_state_dict(vlm_state_dict, strict=True)
        overwatch.info("loaded vlm_backbone checkpoint successfully.")

        # init hfm-together model with vlm backbone
        model = cls(
            model_cfg=launch_config.model,
            vlm_model = vlm_model,
        )

        # load action head
        action_head_state_dict = {}
        for k, v in state_dict.items():
            if k.startswith("vlm_model."):
                continue
            elif k.startswith('action_header.'):
                action_head_state_dict[k.replace("action_header.", "")] = v
            elif k.startswith("motion_encoder."):
                continue
            else:
                assert False, "check here"
        model.action_header.load_state_dict(action_head_state_dict, strict=True)
        overwatch.info("loaded action head checkpoint successfully.")

        if hasattr(model, "motion_encoder"):
            motion_encoder_state_dict = {
                k.replace("motion_encoder.", ""): v
                for k, v in state_dict.items()
                if k.startswith("motion_encoder.")
            }
            if motion_encoder_state_dict:
                model.motion_encoder.load_state_dict(motion_encoder_state_dict, strict=True)  # type: ignore[attr-defined]
                overwatch.info("loaded motion encoder checkpoint successfully.")

        # load necessary modules
        if is_omni:
            model.vlm_processor = Qwen2_5OmniProcessor.from_pretrained(model_name_or_path)
        else:
            model.vlm_processor = AutoProcessor.from_pretrained(model_name_or_path)
        # model.tokenizer = model.vlm_processor.tokenizer

        if launch_config.model.noise_scheduler == "ddpm":
                from diffusers.schedulers.scheduling_ddim import DDIMScheduler
                scheduler = DDIMScheduler(
                    num_train_timesteps=launch_config.model.train_diffusion_steps,
                    beta_start=0.0001,
                    beta_end=0.02,
                    beta_schedule="squaredcos_cap_v2",
                    clip_sample=True,   
                    set_alpha_to_one=True,
                    steps_offset=0,
                    prediction_type="epsilon",
                )
        elif launch_config.model.noise_scheduler == "flow":
            from diffusers.schedulers.scheduling_flow_match_euler_discrete import (FlowMatchEulerDiscreteScheduler)
            scheduler= FlowMatchEulerDiscreteScheduler(
                num_train_timesteps=launch_config.model.train_diffusion_steps, # MUST be 1000 as per pretrained SD3
            )
        else:
            raise ValueError(f"Unsupported noise scheduler: {launch_config.model.noise_scheduler}")

        model.noise_scheduler = scheduler
        model.action_horizon = launch_config.model.action_chunk_size
        model.action_dim = launch_config.model.action_dim
        model.text_only = bool(getattr(launch_config.data.transform.model, "text_only", False))
        model.audio_only = bool(getattr(launch_config.data.transform.model, "audio_only", False))
        model.video_only = bool(getattr(launch_config.data.transform.model, "video_only", False))
        model.video_size_pixels = int(getattr(launch_config.data.transform.model, "video_size_pixels", 50176))
        model.use_delta_actions = bool(getattr(launch_config.data.transform.repack, "use_delta_actions", False))
        # <modify_jerk> 记录hybrid action模式,方便infer/调试区分root-delta checkpoint。
        model.root_delta_actions = bool(getattr(launch_config.data.transform.repack, "root_delta_actions", False))
        # 记录TextOp动作checkpoint,推理脚本据此走一致的decode路径。
        model.textop_actions = bool(getattr(launch_config.data.transform.repack, "textop_actions", False))
        # <modify_float> 记录推理精度配置,供predict_action构造autocast上下文。
        model.infer_autocast_dtype = vlm_dtype
        model.device = device
        return model

    def _action_condition_hidden_states(
        self,
        vlm_hidden_states: torch.Tensor,
        input_ids: Optional[torch.Tensor] = None,
        attention_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        del input_ids, attention_mask
        return vlm_hidden_states.unsqueeze(1)

    def _omni_hidden_states_without_lm_head(self, vlm_kwargs: dict[str, Any]) -> torch.Tensor:
        """Run Qwen2.5-Omni thinker up to hidden states without materializing logits."""
        input_ids = vlm_kwargs.get("input_ids", None)
        attention_mask = vlm_kwargs.get("attention_mask", None)
        input_features = vlm_kwargs.get("input_features", None)
        pixel_values = vlm_kwargs.get("pixel_values", None)
        pixel_values_videos = vlm_kwargs.get("pixel_values_videos", None)
        image_grid_thw = vlm_kwargs.get("image_grid_thw", None)
        video_grid_thw = vlm_kwargs.get("video_grid_thw", None)
        feature_attention_mask = vlm_kwargs.get("feature_attention_mask", None)
        audio_feature_lengths = vlm_kwargs.get("audio_feature_lengths", None)
        video_second_per_grid = vlm_kwargs.get("video_second_per_grid", None)
        inputs_embeds = vlm_kwargs.get("inputs_embeds", None)
        position_ids = vlm_kwargs.get("position_ids", None)
        cache_position = vlm_kwargs.get("cache_position", None)
        use_audio_in_video = vlm_kwargs.get("use_audio_in_video", None)

        if inputs_embeds is None:
            inputs_embeds = self.vlm_model.get_input_embeddings()(input_ids)

        if input_features is not None:
            audio_features = self.vlm_model.get_audio_features(
                input_features,
                feature_attention_mask=feature_attention_mask,
                audio_feature_lengths=audio_feature_lengths,
            )
            audio_features = audio_features.to(inputs_embeds.device, inputs_embeds.dtype)
            _, _, audio_mask = self.vlm_model.get_placeholder_mask(input_ids, inputs_embeds=inputs_embeds)
            inputs_embeds = inputs_embeds.masked_scatter(audio_mask, audio_features)

        if pixel_values is not None:
            image_embeds = self.vlm_model.get_image_features(pixel_values, image_grid_thw)
            image_embeds = image_embeds.to(inputs_embeds.device, inputs_embeds.dtype)
            image_mask, _, _ = self.vlm_model.get_placeholder_mask(
                input_ids, inputs_embeds=inputs_embeds, image_features=image_embeds
            )
            inputs_embeds = inputs_embeds.masked_scatter(image_mask, image_embeds)

        if pixel_values_videos is not None:
            video_embeds = self.vlm_model.get_video_features(pixel_values_videos, video_grid_thw)
            video_embeds = video_embeds.to(inputs_embeds.device, inputs_embeds.dtype)
            _, video_mask, _ = self.vlm_model.get_placeholder_mask(
                input_ids, inputs_embeds=inputs_embeds, video_features=video_embeds
            )
            inputs_embeds = inputs_embeds.masked_scatter(video_mask, video_embeds)

        if feature_attention_mask is not None:
            audio_feature_lengths = torch.sum(feature_attention_mask, dim=1)
        else:
            audio_feature_lengths = None

        if attention_mask is not None and position_ids is None:
            if cache_position is None or (cache_position is not None and cache_position[0] == 0) or self.vlm_model.rope_deltas is None:
                delta0 = (1 - attention_mask).sum(dim=-1).unsqueeze(1)
                position_ids, rope_deltas = self.vlm_model.get_rope_index(
                    input_ids,
                    image_grid_thw,
                    video_grid_thw,
                    attention_mask,
                    use_audio_in_video,
                    audio_feature_lengths,
                    video_second_per_grid,
                )
                self.vlm_model.rope_deltas = rope_deltas - delta0
            else:
                batch_size, seq_length = input_ids.shape
                delta = cache_position[0] + self.vlm_model.rope_deltas if cache_position is not None else 0
                position_ids = torch.arange(seq_length, device=input_ids.device)
                position_ids = position_ids.view(1, -1).expand(batch_size, -1)
                position_ids = position_ids.add(delta)
                position_ids = position_ids.unsqueeze(0).expand(3, -1, -1)

        outputs = self.vlm_model.model(
            attention_mask=attention_mask,
            position_ids=position_ids,
            past_key_values=vlm_kwargs.get("past_key_values", None),
            inputs_embeds=inputs_embeds,
            use_cache=False,
            output_attentions=vlm_kwargs.get("output_attentions", None),
            output_hidden_states=True,
            return_dict=True,
            cache_position=cache_position,
        )
        return outputs.hidden_states[-1]

    def _extract_vlm_hidden_states(self, vlm_kwargs: dict[str, Any]) -> torch.Tensor:
        if self.vlm_model.__class__.__name__ == "Qwen2_5OmniThinkerForConditionalGeneration":
            return self._omni_hidden_states_without_lm_head(vlm_kwargs)
        return self.vlm_model(**vlm_kwargs).hidden_states[-1]

    # TODO-yth: forward入口,vlm(Qwen3VLForConditionalGeneration)输出hidden最后一层到action_head,得到action_pred和目标噪声算MSE
    def forward(
        self,
        input_ids,
        attention_mask,
        pixel_values,
        image_grid_thw,
        action_samples: torch.Tensor,
        states: torch.Tensor,
        timestep: torch.LongTensor,
        # <modify_audio> 支持Qwen2.5-Omni audio-only processor输出的音频特征。
        input_features=None,
        feature_attention_mask=None,
        audio_feature_lengths=None,
        pixel_values_videos=None,
        video_grid_thw=None,
        video_second_per_grid=None,
        traj2ds: Optional[torch.Tensor] = None,
        progress: Optional[torch.Tensor] = None,
        future_progress: Optional[torch.Tensor] = None,
        return_dict: bool = True,
        skip_layers: Optional[List[int]] = None,
    ) -> Union[List[torch.Tensor], HumanFoundationModelOutput]: 

        # <modify>
        # qwen2.5里面有一行delta0 = (1 - attention_mask),而qwen3这边接收的mask是bool,所以这里要转成long
        vlm_attention_mask = attention_mask
        if vlm_attention_mask is not None and vlm_attention_mask.dtype == torch.bool:
            vlm_attention_mask = vlm_attention_mask.to(dtype=torch.long)

        # <modify_audio> 原逻辑固定传pixel_values/image_grid_thw;现在按实际batch动态拼VLM输入,避免Qwen3VL收到音频空参数。
        # 原逻辑:
        # vlm_hidden_states = self.vlm_model(
        #     input_ids=input_ids,
        #     attention_mask=vlm_attention_mask,
        #     pixel_values=pixel_values,
        #     image_grid_thw=image_grid_thw,
        #     output_hidden_states=True,
        #     return_dict=True
        # ).hidden_states[-1]
        vlm_kwargs = {
            "input_ids": input_ids,
            "attention_mask": vlm_attention_mask,
            "output_hidden_states": True,
            "return_dict": True,
            "use_cache": False,
        }
        if pixel_values is not None:
            vlm_kwargs["pixel_values"] = pixel_values
        if image_grid_thw is not None:
            vlm_kwargs["image_grid_thw"] = image_grid_thw
        if input_features is not None:
            vlm_kwargs["input_features"] = input_features
        if feature_attention_mask is not None:
            vlm_kwargs["feature_attention_mask"] = feature_attention_mask
        if audio_feature_lengths is not None:
            vlm_kwargs["audio_feature_lengths"] = audio_feature_lengths
        if pixel_values_videos is not None:
            vlm_kwargs["pixel_values_videos"] = pixel_values_videos
        if video_grid_thw is not None:
            vlm_kwargs["video_grid_thw"] = video_grid_thw
        if video_second_per_grid is not None:
            vlm_kwargs["video_second_per_grid"] = video_second_per_grid

        # extract vision/audio + language features
        vlm_hidden_states = self._extract_vlm_hidden_states(vlm_kwargs)
        # print(vlm_hidden_states.shape)

        model_output = self.action_header(
            hidden_states=None,
            timestep=timestep,
            joint_attention_kwargs=dict(
                action_hidden_embeds=action_samples, # (B,Tp,Da)
                views=vlm_hidden_states.unsqueeze(1),  # (B,V,N,D)
                obs=states,  # (B,1,M)
                traj2ds=traj2ds,  # (B, C, 3, H, W)
                progress=progress,
                future_progress=future_progress,
            ),
            vlm_attn_mask=attention_mask, # (B, seq_len)
            return_dict=return_dict,
        )
        return HumanFoundationModelOutput(action=model_output.action)

    def _build_video_only_inputs(
        self,
        texts,
        video=None,
        video_embed=None,
        video_size_pixels: int = 50176,
        video_sample_fps: float = 1.0,
    ):
        if video_embed is not None:
            pixel_values_videos = torch.as_tensor(
                video_embed["pixel_values_videos"],
                dtype=torch.float32,
                device=self.device,
            )
            video_grid_thw = torch.as_tensor(
                video_embed["video_grid_thw"],
                dtype=torch.long,
                device=self.device,
            )
            if video_grid_thw.dim() == 1:
                video_grid_thw = video_grid_thw.unsqueeze(0)
            if "video_second_per_grid" in video_embed:
                video_second_per_grid = torch.as_tensor(
                    video_embed["video_second_per_grid"],
                    dtype=torch.float32,
                    device=self.device,
                ).reshape(-1)
            else:
                video_second_per_grid = torch.tensor(
                    [float(self.vlm_processor.video_processor.temporal_patch_size) / float(video_sample_fps)],
                    dtype=torch.float32,
                    device=self.device,
                )

            texts = self.vlm_processor.replace_multimodal_special_tokens(
                texts,
                iter([]),
                iter([]),
                iter([video_grid_thw[0].detach().cpu()]),
                iter([float(video_second_per_grid.reshape(-1)[0].item())]),
                False,
                25,
                2.0,
            )
            text_inputs = self.vlm_processor.tokenizer(
                texts,
                padding=True,
                return_tensors="pt",
            )
            inputs = {key: value.to(self.device) for key, value in text_inputs.items()}
            inputs["pixel_values_videos"] = pixel_values_videos
            inputs["video_grid_thw"] = video_grid_thw
            inputs["video_second_per_grid"] = video_second_per_grid
            return inputs

        return self.vlm_processor(
            text=texts,
            videos=[np.asarray(video, dtype=np.uint8)],
            padding=True,
            return_tensors="pt",
            use_audio_in_video=False,
            videos_kwargs={
                "size": {
                    "shortest_edge": int(video_size_pixels),
                    "longest_edge": int(video_size_pixels),
                },
                "fps": float(video_sample_fps),
            },
        ).to(self.device)

    @torch.inference_mode()
    def predict_action(
        self,
        observations: Optional[List[List[Image.Image]]],  # B * List of PIL Image as [view1, view2]
        states: torch.Tensor, # (B, Ts, Ds)
        instructions: List[str], # (B,)
        num_inference_steps: int,
        traj2ds, 
        **kwargs: str
    ) -> torch.Tensor:

        bsz = states.shape[0]
        progress = kwargs.get("progress", None)
        future_progress = kwargs.get("future_progress", None)
        text_only = bool(getattr(self, "text_only", False))
        audio_only = bool(getattr(self, "audio_only", False))
        video_only = bool(getattr(self, "video_only", False))
        batch_input_ids = []
        batch_attention_mask = []
        batch_pixel_values = [] if (not text_only and not audio_only and not video_only) else None
        batch_image_grid_thw = [] if (not text_only and not audio_only and not video_only) else None
        # <modify_audio> audio推理时从kwargs接收每个batch样本的waveform片段,可同时使用文本指令。
        batch_input_features = [] if audio_only else None
        batch_feature_attention_mask = [] if audio_only else None
        batch_pixel_values_videos = [] if video_only else None
        batch_video_grid_thw = [] if video_only else None
        batch_video_second_per_grid = [] if video_only else None

        if audio_only:
            audios = kwargs.get("audios", None)
            assert audios is not None, "audios must be provided for audio-only inference"
            audio_items = list(audios)
            audio_instructions = list(instructions or [""] * len(audio_items))
            assert len(audio_instructions) == len(audio_items), "instructions must match audios for audio-only inference"
            items = list(zip(audio_items, audio_instructions))
        elif video_only:
            video_embeds = kwargs.get("video_embeds", None)
            if video_embeds is not None:
                video_paths = kwargs.get("video_paths", ["video.mp4"] * len(video_embeds))
                items = list(zip(video_embeds, video_paths, instructions))
            else:
                videos = kwargs.get("videos", None)
                assert videos is not None, "videos must be provided for video-only inference"
                video_paths = kwargs.get("video_paths", ["video.mp4"] * len(videos))
                items = list(zip(videos, video_paths, instructions))
        elif text_only:
            items = [(None, instruction) for instruction in instructions]
        else:
            assert observations is not None, "observations must be provided for non-text-only inference"
            items = list(zip(observations, instructions))

        for item in items:
            if audio_only:
                audio, instruction = item
                content = [{"type": "audio", "audio": "audio.wav"}]
                if str(instruction).strip():
                    content.append({"type": "text", "text": str(instruction).strip()})
                messages = [[
                    {"role": "system", "content": [{"type": "text", "text": OMNI_DEFAULT_SYSTEM_PROMPT}]},
                    {"role": "user", "content": content},
                ]]
            elif video_only:
                video_item, video_path, instruction = item
                content = [{"type": "video", "video": str(video_path or "video.mp4")}]
                if str(instruction).strip():
                    content.append({"type": "text", "text": str(instruction).strip()})
                messages = [[
                    {"role": "system", "content": [{"type": "text", "text": OMNI_DEFAULT_SYSTEM_PROMPT}]},
                    {"role": "user", "content": content},
                ]]
            elif text_only:
                _, instruction = item
                messages = [[
                    {"role": "system", "content": [{"type": "text", "text": OMNI_DEFAULT_SYSTEM_PROMPT}]},
                    {"role": "user", "content": [{"type": "text", "text": instruction}]},
                ]]
            else:
                observation, instruction = item
                assert observation is not None
                messages = []
                content = [{"type": "image", "image": img} for img in observation]
                content.append({"type": "text", "text": instruction})
                user_msg = {"role": "user", "content": content}
                messages.append([user_msg])
            texts = [
                self.vlm_processor.apply_chat_template(
                    m, tokenize=False, add_generation_prompt=True
                )
                for m in messages
            ]
            if audio_only:
                inputs = self.vlm_processor(
                    text=texts,
                    audio=[np.asarray(audio, dtype=np.float32)],
                    padding=True,
                    return_tensors="pt",
                ).to(self.device)
            elif video_only:
                video_size_pixels = int(kwargs.get("video_size_pixels", getattr(self, "video_size_pixels", 50176)))
                video_sample_fps = float(kwargs.get("video_sample_fps", 1.0))
                inputs = self._build_video_only_inputs(
                    texts,
                    video=None if isinstance(video_item, dict) else video_item,
                    video_embed=video_item if isinstance(video_item, dict) else None,
                    video_size_pixels=video_size_pixels,
                    video_sample_fps=video_sample_fps,
                )
            elif text_only:
                inputs = self.vlm_processor(
                    text=texts,
                    padding=True,
                    return_tensors="pt",
                ).to(self.device)
            else:
                image_inputs, video_inputs = process_vision_info(messages, image_patch_size=16)
                inputs = self.vlm_processor(
                    text=texts,
                    images=image_inputs,
                    videos=video_inputs,
                    padding=True,
                    return_tensors="pt",
                ).to(self.device)
            batch_input_ids.append(inputs['input_ids'].squeeze(0))
            batch_attention_mask.append(inputs['attention_mask'].squeeze(0))
            if audio_only:
                assert batch_input_features is not None and batch_feature_attention_mask is not None
                batch_input_features.append(inputs["input_features"].squeeze(0))
                batch_feature_attention_mask.append(inputs["feature_attention_mask"].squeeze(0))
            if video_only:
                assert batch_pixel_values_videos is not None and batch_video_grid_thw is not None
                assert batch_video_second_per_grid is not None
                batch_pixel_values_videos.append(inputs["pixel_values_videos"].squeeze(0))
                batch_video_grid_thw.append(inputs["video_grid_thw"].squeeze(0))
                batch_video_second_per_grid.append(inputs["video_second_per_grid"].reshape(-1))
            if not text_only and not audio_only and not video_only:
                assert batch_pixel_values is not None and batch_image_grid_thw is not None
                batch_pixel_values.append(inputs['pixel_values'])
                batch_image_grid_thw.append(inputs['image_grid_thw'].squeeze(0))

        pad_token_id = int(getattr(self.vlm_processor.tokenizer, "pad_token_id", 0))
        batch_input_ids = torch.nn.utils.rnn.pad_sequence(
            batch_input_ids, batch_first=True, padding_value=pad_token_id
        )
        batch_attention_mask = torch.nn.utils.rnn.pad_sequence(
            batch_attention_mask, batch_first=True, padding_value=0
        )
        if audio_only:
            assert batch_input_features is not None and batch_feature_attention_mask is not None
            # <modify_full_audio> 整段音频推理时不同样本audio feature长度可能不同,按batch最长补零。
            max_audio_len = max(int(feature.shape[-1]) for feature in batch_input_features)
            padded_input_features = []
            padded_feature_attention_mask = []
            for feature, feature_mask in zip(batch_input_features, batch_feature_attention_mask):
                pad_len = max_audio_len - int(feature.shape[-1])
                if pad_len > 0:
                    feature_pad_shape = list(feature.shape)
                    feature_pad_shape[-1] = pad_len
                    feature = torch.cat(
                        [feature, torch.zeros(feature_pad_shape, dtype=feature.dtype, device=feature.device)],
                        dim=-1,
                    )
                    mask_pad_shape = list(feature_mask.shape)
                    mask_pad_shape[-1] = pad_len
                    feature_mask = torch.cat(
                        [feature_mask, torch.zeros(mask_pad_shape, dtype=feature_mask.dtype, device=feature_mask.device)],
                        dim=-1,
                    )
                padded_input_features.append(feature)
                padded_feature_attention_mask.append(feature_mask)
            batch_input_features = torch.stack(padded_input_features)
            batch_feature_attention_mask = torch.stack(padded_feature_attention_mask)
        if not text_only and not audio_only and not video_only:
            assert batch_pixel_values is not None and batch_image_grid_thw is not None
            batch_pixel_values = torch.stack(batch_pixel_values) # (B, 256, 1536)
            batch_image_grid_thw = torch.stack(batch_image_grid_thw) # (B, 3)
        if video_only:
            assert batch_pixel_values_videos is not None and batch_video_grid_thw is not None
            assert batch_video_second_per_grid is not None
            batch_pixel_values_videos = torch.cat(batch_pixel_values_videos, dim=0)
            batch_video_grid_thw = torch.stack(batch_video_grid_thw)
            batch_video_second_per_grid = torch.cat(batch_video_second_per_grid, dim=0)

        # <modify_float> 推理时按infer_autocast_dtype选择精度;float32时不启用autocast。
        # 原逻辑:
        # autocast_ctx = (
        #     torch.autocast("cuda", dtype=torch.bfloat16)
        #     if str(self.device).startswith("cuda")
        #     else contextlib.nullcontext()
        # )
        infer_dtype = getattr(self, "infer_autocast_dtype", torch.bfloat16)
        if str(self.device).startswith("cuda") and infer_dtype in (torch.float16, torch.bfloat16):
            autocast_ctx = torch.autocast("cuda", dtype=infer_dtype)
        else:
            autocast_ctx = contextlib.nullcontext()
        with autocast_ctx:
            # extract vision + language features
            if audio_only:
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    input_features=batch_input_features,  # type: ignore
                    feature_attention_mask=batch_feature_attention_mask,  # type: ignore
                    output_hidden_states=True,
                    return_dict=True
                )
            elif video_only:
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    pixel_values_videos=batch_pixel_values_videos,  # type: ignore
                    video_grid_thw=batch_video_grid_thw,  # type: ignore
                    video_second_per_grid=batch_video_second_per_grid,  # type: ignore
                    output_hidden_states=True,
                    return_dict=True,
                )
            elif text_only:
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    output_hidden_states=True,
                    return_dict=True
                )
            else:
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    pixel_values=batch_pixel_values, # type: ignore
                    image_grid_thw=batch_image_grid_thw, # type: ignore
                    output_hidden_states=True,
                    return_dict=True
                )
            vlm_hidden_states_ = output.hidden_states # len(vlm_hidden_states_) == 29
            

            # use hidden states from the last layer
            vlm_hidden_states = vlm_hidden_states_[-1] # shape (B, seq_len, D_h)  shape(16, 80, 2048)
            vlm_hidden_states = self._action_condition_hidden_states(
                vlm_hidden_states.to(dtype=states.dtype),
                input_ids=batch_input_ids,
                attention_mask=batch_attention_mask,
            ) # shape (B, 1, seq_len, D_h) or subclass-specific latent tokens

            # generate action from noise
            action_samples = torch.randn(
                bsz, self.action_horizon, self.action_dim, device=self.device
            )
            self.noise_scheduler.set_timesteps(num_inference_steps)

            for timestep in self.noise_scheduler.timesteps:
                batched_timestep = timestep.expand(bsz).to(self.device)
                model_pred = self.action_header(
                    hidden_states=None,
                    timestep=batched_timestep,
                    joint_attention_kwargs=dict(
                        action_hidden_embeds=action_samples, # (B,Tp,Da)
                        views=vlm_hidden_states,  # (B,V,N,D)
                        obs=states,  # (B,1,M)
                        traj2ds=traj2ds,  # (B, C, 3, H, W)
                        progress=progress,
                        future_progress=future_progress,
                    ),
                    return_dict=True,
                ).action
                action_samples = self.noise_scheduler.step(
                    model_output=model_pred, timestep=timestep, sample=action_samples # type: ignore
                ).prev_sample

        return action_samples.float()

    @torch.inference_mode()
    def predict_action_with_training_rtc_flow(
        self,
        observations: Optional[List[List[Image.Image]]],  # B * List of PIL Image as [view1, view2]
        states: torch.Tensor, # (B, Ts, Ds)
        instructions: List[str], # (B,)
        num_inference_steps: int,
        traj2ds, 
        prev_actions: torch.Tensor = None, # (1, H, D)
        inference_delay: int = 0,
        max_delay: int = 0,
        **kwargs: str
    ) -> torch.Tensor:

        ## RTC related ##
        H = self.action_horizon
        assert prev_actions is not None and inference_delay > 0 and max_delay > 0, "prev_actions, inference_delay and max_delay must be provided"
        assert prev_actions.shape[0] == 1
        prev_actions = prev_actions.to(device=self.device, dtype=torch.float32)

        # Create soft mask for inpainting
        d = inference_delay

        # Validate constraint from paper: d ≤ s ≤ H - d
        assert d < H, f"Constraint violated: d={d}, H={H}. Need d < H"
        assert d < max_delay, f"Constraint violated: d={d}, max_delay={max_delay}. Need d < max_delay"

        prefix_mask = torch.arange(H, device=self.device)[None, :] < d # shape (1, H)
        ##              ##


        bsz = states.shape[0]
        progress = kwargs.get("progress", None)
        future_progress = kwargs.get("future_progress", None)
        text_only = bool(getattr(self, "text_only", False))
        audio_only = bool(getattr(self, "audio_only", False))
        video_only = bool(getattr(self, "video_only", False))
        batch_input_ids = []
        batch_attention_mask = []
        batch_pixel_values = [] if (not text_only and not audio_only and not video_only) else None
        batch_image_grid_thw = [] if (not text_only and not audio_only and not video_only) else None
        # <modify_audio> audio RTC推理同样从kwargs接收waveform片段,可同时使用文本指令。
        batch_input_features = [] if audio_only else None
        batch_feature_attention_mask = [] if audio_only else None
        batch_pixel_values_videos = [] if video_only else None
        batch_video_grid_thw = [] if video_only else None
        batch_video_second_per_grid = [] if video_only else None

        if audio_only:
            audios = kwargs.get("audios", None)
            assert audios is not None, "audios must be provided for audio-only inference"
            audio_items = list(audios)
            audio_instructions = list(instructions or [""] * len(audio_items))
            assert len(audio_instructions) == len(audio_items), "instructions must match audios for audio-only inference"
            items = list(zip(audio_items, audio_instructions))
        elif video_only:
            video_embeds = kwargs.get("video_embeds", None)
            if video_embeds is not None:
                video_paths = kwargs.get("video_paths", ["video.mp4"] * len(video_embeds))
                items = list(zip(video_embeds, video_paths, instructions))
            else:
                videos = kwargs.get("videos", None)
                assert videos is not None, "videos must be provided for video-only inference"
                video_paths = kwargs.get("video_paths", ["video.mp4"] * len(videos))
                items = list(zip(videos, video_paths, instructions))
        elif text_only:
            items = [(None, instruction) for instruction in instructions]
        else:
            assert observations is not None, "observations must be provided for non-text-only inference"
            items = list(zip(observations, instructions))

        for item in items:
            if audio_only:
                audio, instruction = item
                content = [{"type": "audio", "audio": "audio.wav"}]
                if str(instruction).strip():
                    content.append({"type": "text", "text": str(instruction).strip()})
                messages = [[
                    {"role": "system", "content": [{"type": "text", "text": OMNI_DEFAULT_SYSTEM_PROMPT}]},
                    {"role": "user", "content": content},
                ]]
            elif video_only:
                video_item, video_path, instruction = item
                content = [{"type": "video", "video": str(video_path or "video.mp4")}]
                if str(instruction).strip():
                    content.append({"type": "text", "text": str(instruction).strip()})
                messages = [[
                    {"role": "system", "content": [{"type": "text", "text": OMNI_DEFAULT_SYSTEM_PROMPT}]},
                    {"role": "user", "content": content},
                ]]
            elif text_only:
                _, instruction = item
                messages = [[
                    {"role": "system", "content": [{"type": "text", "text": OMNI_DEFAULT_SYSTEM_PROMPT}]},
                    {"role": "user", "content": [{"type": "text", "text": instruction}]},
                ]]
            else:
                observation, instruction = item
                assert observation is not None
                messages = []
                content = [{"type": "image", "image": img} for img in observation]
                content.append({"type": "text", "text": instruction})
                user_msg = {"role": "user", "content": content}
                messages.append([user_msg])
            texts = [
                self.vlm_processor.apply_chat_template(
                    m, tokenize=False, add_generation_prompt=True
                )
                for m in messages
            ]
            if audio_only:
                inputs = self.vlm_processor(
                    text=texts,
                    audio=[np.asarray(audio, dtype=np.float32)],
                    padding=True,
                    return_tensors="pt",
                ).to(self.device)
            elif video_only:
                video_size_pixels = int(kwargs.get("video_size_pixels", getattr(self, "video_size_pixels", 50176)))
                video_sample_fps = float(kwargs.get("video_sample_fps", 1.0))
                inputs = self._build_video_only_inputs(
                    texts,
                    video=None if isinstance(video_item, dict) else video_item,
                    video_embed=video_item if isinstance(video_item, dict) else None,
                    video_size_pixels=video_size_pixels,
                    video_sample_fps=video_sample_fps,
                )
            elif text_only:
                inputs = self.vlm_processor(
                    text=texts,
                    padding=True,
                    return_tensors="pt",
                ).to(self.device)
            else:
                image_inputs, video_inputs = process_vision_info(messages, image_patch_size=16)
                inputs = self.vlm_processor(
                    text=texts,
                    images=image_inputs,
                    videos=video_inputs,
                    padding=True,
                    return_tensors="pt",
                ).to(self.device)
            batch_input_ids.append(inputs['input_ids'].squeeze(0))
            batch_attention_mask.append(inputs['attention_mask'].squeeze(0))
            if audio_only:
                assert batch_input_features is not None and batch_feature_attention_mask is not None
                batch_input_features.append(inputs["input_features"].squeeze(0))
                batch_feature_attention_mask.append(inputs["feature_attention_mask"].squeeze(0))
            if video_only:
                assert batch_pixel_values_videos is not None and batch_video_grid_thw is not None
                assert batch_video_second_per_grid is not None
                batch_pixel_values_videos.append(inputs["pixel_values_videos"].squeeze(0))
                batch_video_grid_thw.append(inputs["video_grid_thw"].squeeze(0))
                batch_video_second_per_grid.append(inputs["video_second_per_grid"].reshape(-1))
            if not text_only and not audio_only and not video_only:
                assert batch_pixel_values is not None and batch_image_grid_thw is not None
                batch_pixel_values.append(inputs['pixel_values'])
                batch_image_grid_thw.append(inputs['image_grid_thw'].squeeze(0))

        pad_token_id = int(getattr(self.vlm_processor.tokenizer, "pad_token_id", 0))
        batch_input_ids = torch.nn.utils.rnn.pad_sequence(
            batch_input_ids, batch_first=True, padding_value=pad_token_id
        )
        batch_attention_mask = torch.nn.utils.rnn.pad_sequence(
            batch_attention_mask, batch_first=True, padding_value=0
        )
        if audio_only:
            assert batch_input_features is not None and batch_feature_attention_mask is not None
            # <modify_full_audio> 整段音频RTC推理同样补齐batch内audio feature长度。
            max_audio_len = max(int(feature.shape[-1]) for feature in batch_input_features)
            padded_input_features = []
            padded_feature_attention_mask = []
            for feature, feature_mask in zip(batch_input_features, batch_feature_attention_mask):
                pad_len = max_audio_len - int(feature.shape[-1])
                if pad_len > 0:
                    feature_pad_shape = list(feature.shape)
                    feature_pad_shape[-1] = pad_len
                    feature = torch.cat(
                        [feature, torch.zeros(feature_pad_shape, dtype=feature.dtype, device=feature.device)],
                        dim=-1,
                    )
                    mask_pad_shape = list(feature_mask.shape)
                    mask_pad_shape[-1] = pad_len
                    feature_mask = torch.cat(
                        [feature_mask, torch.zeros(mask_pad_shape, dtype=feature_mask.dtype, device=feature_mask.device)],
                        dim=-1,
                    )
                padded_input_features.append(feature)
                padded_feature_attention_mask.append(feature_mask)
            batch_input_features = torch.stack(padded_input_features)
            batch_feature_attention_mask = torch.stack(padded_feature_attention_mask)
        if not text_only and not audio_only and not video_only:
            assert batch_pixel_values is not None and batch_image_grid_thw is not None
            batch_pixel_values = torch.stack(batch_pixel_values) # (B, 256, 1536)
            batch_image_grid_thw = torch.stack(batch_image_grid_thw) # (B, 3)
        if video_only:
            assert batch_pixel_values_videos is not None and batch_video_grid_thw is not None
            assert batch_video_second_per_grid is not None
            batch_pixel_values_videos = torch.cat(batch_pixel_values_videos, dim=0)
            batch_video_grid_thw = torch.stack(batch_video_grid_thw)
            batch_video_second_per_grid = torch.cat(batch_video_second_per_grid, dim=0)

        # <modify_float> RTC推理同样按infer_autocast_dtype选择精度;float32时不启用autocast。
        # 原逻辑:
        # autocast_ctx = (
        #     torch.autocast("cuda", dtype=torch.bfloat16)
        #     if str(self.device).startswith("cuda")
        #     else contextlib.nullcontext()
        # )
        infer_dtype = getattr(self, "infer_autocast_dtype", torch.bfloat16)
        if str(self.device).startswith("cuda") and infer_dtype in (torch.float16, torch.bfloat16):
            autocast_ctx = torch.autocast("cuda", dtype=infer_dtype)
        else:
            autocast_ctx = contextlib.nullcontext()
        with autocast_ctx:
            # extract vision + language features
            if audio_only:
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    input_features=batch_input_features,  # type: ignore
                    feature_attention_mask=batch_feature_attention_mask,  # type: ignore
                    output_hidden_states=True,
                    return_dict=True
                )
            elif video_only:
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    pixel_values_videos=batch_pixel_values_videos,  # type: ignore
                    video_grid_thw=batch_video_grid_thw,  # type: ignore
                    video_second_per_grid=batch_video_second_per_grid,  # type: ignore
                    output_hidden_states=True,
                    return_dict=True,
                )
            elif text_only:
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    output_hidden_states=True,
                    return_dict=True
                )
            else:
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    pixel_values=batch_pixel_values, # type: ignore
                    image_grid_thw=batch_image_grid_thw, # type: ignore
                    output_hidden_states=True,
                    return_dict=True
                )
            vlm_hidden_states_ = output.hidden_states # len(vlm_hidden_states_) == 29
            

            # use hidden states from the last layer
            vlm_hidden_states = vlm_hidden_states_[-1] # shape (B, seq_len, D_h)  shape(16, 80, 2048)
            vlm_hidden_states = self._action_condition_hidden_states(
                vlm_hidden_states.to(dtype=states.dtype),
                input_ids=batch_input_ids,
                attention_mask=batch_attention_mask,
            ) # shape (B, 1, seq_len, D_h) or subclass-specific latent tokens

            # generate action from noise
            action_samples = torch.randn(
                bsz, self.action_horizon, self.action_dim, device=self.device
            )
            self.noise_scheduler.set_timesteps(num_inference_steps)

            # self.noise_scheduler.timesteps: tensor([1000.,  889.,  778.,  667.,  556.,  445.,  334.,  223.,  112.,    1.])
            # self.noise_scheduler.sigmas: tensor([1.0000, 0.8890, 0.7780, 0.6670, 0.5560, 0.4450, 0.3340, 0.2230, 0.1120, 0.0010, 0.0000])

            for i, timestep in enumerate(self.noise_scheduler.timesteps):
                # batched_timestep = timestep.expand(bsz).to(self.device).detach()

                batched_timestep_masked = torch.where(prefix_mask, 0, timestep).to(self.device) # shape (B, H)

                # replace action_samples with clean prev_actions when prefix_mask == True
                action_samples = torch.where(prefix_mask[:, :, None], prev_actions, action_samples)

                model_pred = self.action_header(
                    hidden_states=None,
                    timestep=batched_timestep_masked,
                    joint_attention_kwargs=dict(
                        action_hidden_embeds=action_samples, # (B,Tp,Da)
                        views=vlm_hidden_states,  # (B,V,N,D)
                        obs=states,  # (B,1,M)
                        traj2ds=traj2ds,  # (B, C, 3, H, W)
                        progress=progress,
                        future_progress=future_progress,
                    ),
                    return_dict=True,
                ).action

                action_samples = self.noise_scheduler.step(
                    model_output=model_pred, timestep=timestep, sample=action_samples # type: ignore
                ).prev_sample

                # if i == len(self.noise_scheduler.timesteps) - 1:
                #     action_samples = torch.where(prefix_mask[:, :, None], prev_actions, action_samples)

        return action_samples.float()

    def predict_action_with_rtc_flow(
        self,
        observations: List[List[Image.Image]],  # B * List of PIL Image as [view1, view2]
        states: torch.Tensor, # (B, Ts, Ds)
        instructions: List[str], # (B,)
        num_inference_steps: int,
        traj2ds, 
        prev_actions: torch.Tensor = None, # (1, H, D)
        inference_delay: int = 0,
        execution_horizon: int = 0,
        mask_schedule: str = "exponential",
        guidance_weight: float = 5.0,
        **kwargs: str
    ) -> np.ndarray:
        with torch.no_grad():

            ## RTC related ##
            H = self.action_horizon
            assert prev_actions is not None and inference_delay > 0 and execution_horizon > 0, "prev_actions, inference_delay and execution_horizon must be provided"
            assert prev_actions.shape[0] == 1
            prev_actions = prev_actions.to(device=self.device, dtype=torch.float32)

            # Create soft mask for inpainting
            d = inference_delay
            s = execution_horizon

            # Validate constraint from paper: d ≤ s ≤ H - d
            assert d <= s and s <= H - d, f"Constraint violated: d={d}, s={s}, H={H}. Need d ≤ s ≤ H-d"

            # Create soft mask [H] according to Equation 5
            mask = self._create_soft_mask(
                H, d, s, schedule=mask_schedule, device=self.device
            )
            mask_expanded = mask.view(1, -1, 1).expand(1, self.action_horizon, self.action_dim)
            ##              ##


            bsz = states.shape[0]
            progress = kwargs.get("progress", None)
            future_progress = kwargs.get("future_progress", None)
            batch_input_ids = []
            batch_attention_mask = []
            batch_pixel_values = []
            batch_image_grid_thw = []


            for observation, instruction in zip(observations, instructions):
                messages = []
                content = [{"type": "image", "image": img} for img in observation]
                content.append({"type": "text", "text": instruction})
                user_msg = {"role": "user", "content": content}
                messages.append([user_msg])
                texts = [
                    self.vlm_processor.apply_chat_template(
                        m, tokenize=False, add_generation_prompt=True
                    )
                    for m in messages
                ]
                image_inputs, video_inputs = process_vision_info(messages, image_patch_size=16)
                inputs = self.vlm_processor(
                    text=texts,
                    images=image_inputs,
                    videos=video_inputs,
                    padding=True,
                    return_tensors="pt",
                ).to(self.device)
                # input_ids torch.Size([1, 80])
                # attention_mask torch.Size([1, 80])
                # pixel_values torch.Size([256, 1536])
                # image_grid_thw torch.Size([1, 3])
                batch_input_ids.append(inputs['input_ids'].squeeze(0))
                batch_attention_mask.append(inputs['attention_mask'].squeeze(0))
                batch_pixel_values.append(inputs['pixel_values'])
                batch_image_grid_thw.append(inputs['image_grid_thw'].squeeze(0))

            batch_input_ids = torch.stack(batch_input_ids) # (B, 80)
            batch_attention_mask = torch.stack(batch_attention_mask) # (B, 80)
            batch_pixel_values = torch.stack(batch_pixel_values) # (B, 256, 1536)
            batch_image_grid_thw = torch.stack(batch_image_grid_thw) # (B, 3)

            with torch.autocast("cuda", dtype=torch.bfloat16):
                # extract vision + language features
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    pixel_values=batch_pixel_values,
                    image_grid_thw=batch_image_grid_thw,
                    output_hidden_states=True,
                    return_dict=True
                )
                vlm_hidden_states_ = output.hidden_states # len(vlm_hidden_states_) == 29
                

                # use hidden states from the last layer
                vlm_hidden_states = vlm_hidden_states_[-1] # shape (B, seq_len, D_h)  shape(16, 80, 2048)
                vlm_hidden_states = vlm_hidden_states.unsqueeze(1) # shape (B, 1, seq_len, D_h) (16, 1, 80, 2048)

                # generate action from noise
                action_samples = torch.randn(
                    bsz, self.action_horizon, self.action_dim, device=self.device
                )
                # target_noise = action_samples.clone().detach()
                self.noise_scheduler.set_timesteps(num_inference_steps)

                # self.noise_scheduler.timesteps: tensor([1000.,  889.,  778.,  667.,  556.,  445.,  334.,  223.,  112.,    1.])
                # self.noise_scheduler.sigmas: tensor([1.0000, 0.8890, 0.7780, 0.6670, 0.5560, 0.4450, 0.3340, 0.2230, 0.1120, 0.0010, 0.0000])

                # self.noise_scheduler.timesteps: tensor([1000.,  889.,  778.,  667.,  556.,  445.,  334.,  223.,  112.,    1.])
                # self.noise_scheduler.sigmas: tensor([1.0000, 0.8890, 0.7780, 0.6670, 0.5560, 0.4450, 0.3340, 0.2230, 0.1120, 0.0010, 0.0000])

                for timestep in self.noise_scheduler.timesteps:
                    batched_timestep = timestep.expand(bsz).to(self.device).detach()

                    ### pseudo inverse (with gradient):
                    # Enable gradient only for this step
                    with torch.enable_grad():
                        # Detach and enable gradient only for sample_actions in this step
                        action_samples_grad = action_samples.detach().requires_grad_(True)

                        model_pred = self.action_header(
                            hidden_states=None,
                            timestep=batched_timestep,
                            joint_attention_kwargs=dict(
                                action_hidden_embeds=action_samples_grad, # (B,Tp,Da)
                                views=vlm_hidden_states,  # (B,V,N,D)
                                obs=states,  # (B,1,M)
                                traj2ds=traj2ds,  # (B, C, 3, H, W)
                                progress=progress,
                                future_progress=future_progress,
                            ),
                            return_dict=True,
                        ).action

                        # 1. 外推干净data X_0
                        tau = self.noise_scheduler.sigmas[self.noise_scheduler.index_for_timestep(timestep)].detach()

                        # DEBUG test inpainting
                        # noisy_prev_actions = (1 - tau) * prev_actions + tau * target_noise
                        # action_samples[:, :d, 14:28] = noisy_prev_actions[:, :d, 14:28]

                        # print("tau:", tau.item())
                        # print("timestep:", timestep)
                        # print("self.scheduler.sigmas:", self.scheduler.sigmas)
                        # print("self.scheduler.index_for_timestep(timestep):", self.scheduler.index_for_timestep(timestep))
                        pred_x0 = action_samples_grad - tau * model_pred
                        # pred_x0 = epsilon - model_pred
                        # print("pred_x0:", pred_x0)
                        

                        # 2. 计算(Y - pred_X_0) * weights
                        error = (prev_actions - pred_x0.detach()) * mask_expanded
                        # print("error:", error)
                        # print("error.abs().mean():", error.abs().mean())

                        # 3. vjp
                        pinv_correction = torch.autograd.grad(
                            outputs=pred_x0,  # [1,T,D]
                            inputs=action_samples_grad,        # [1,T,D]
                            grad_outputs=error,         # 匹配形状 [1,T,D]
                            retain_graph=False,
                            create_graph=False
                        )[0]  # 输出 [1,T,D]
                        # print("pinv_correction:", pinv_correction)

                    # 4. 计算tau相关参数,得到修正项,修正v
                    inv_r2 = (tau**2 + (1 - tau) ** 2) / (tau**2)
                    c = torch.nan_to_num(tau / (1 - tau), posinf=guidance_weight)
                    g = torch.clamp(c * inv_r2, max=guidance_weight)
                    # print("c * inv_r2:", c * inv_r2)
                    # print("model_pred_before_correction:", model_pred)

                    model_pred = model_pred - g * pinv_correction
                    # print("pinv_correction.abs().mean():", pinv_correction.abs().mean())
                    ###

                    action_samples = self.noise_scheduler.step(
                        model_output=model_pred, timestep=timestep, sample=action_samples # type: ignore
                    ).prev_sample

                    # DEBUG test inpainting
                    # if i == len(self.noise_scheduler.timesteps) - 1:
                    #     action_samples[:, :d, 14:28] = prev_actions[:, :d, 14:28]

            return action_samples


    def predict_action_with_rtc_flow_naive_inpaint(
        self,
        observations: List[List[Image.Image]],  # B * List of PIL Image as [view1, view2]
        states: torch.Tensor, # (B, Ts, Ds)
        instructions: List[str], # (B,)
        num_inference_steps: int,
        traj2ds, 
        prev_actions: torch.Tensor = None, # (1, H, D)
        inference_delay: int = 0,
        execution_horizon: int = 0,
        mask_schedule: str = "exponential",
        guidance_weight: float = 5.0,
        **kwargs: str
    ) -> np.ndarray:
        with torch.no_grad():

            ## RTC related ##
            H = self.action_horizon
            assert prev_actions is not None and inference_delay > 0 and execution_horizon > 0, "prev_actions, inference_delay and execution_horizon must be provided"
            assert prev_actions.shape[0] == 1
            prev_actions = prev_actions.to(device=self.device, dtype=torch.float32)

            # Create soft mask for inpainting
            d = inference_delay
            s = execution_horizon

            # Validate constraint from paper: d ≤ s ≤ H - d
            assert d <= s and s <= H - d, f"Constraint violated: d={d}, s={s}, H={H}. Need d ≤ s ≤ H-d"

            # Create soft mask [H] according to Equation 5
            mask = self._create_soft_mask(
                H, d, s, schedule=mask_schedule, device=self.device
            )
            mask_expanded = mask.view(1, -1, 1).expand(1, self.action_horizon, self.action_dim)
            ##              ##


            bsz = states.shape[0]
            progress = kwargs.get("progress", None)
            future_progress = kwargs.get("future_progress", None)
            batch_input_ids = []
            batch_attention_mask = []
            batch_pixel_values = []
            batch_image_grid_thw = []


            for observation, instruction in zip(observations, instructions):
                messages = []
                content = [{"type": "image", "image": img} for img in observation]
                content.append({"type": "text", "text": instruction})
                user_msg = {"role": "user", "content": content}
                messages.append([user_msg])
                texts = [
                    self.vlm_processor.apply_chat_template(
                        m, tokenize=False, add_generation_prompt=True
                    )
                    for m in messages
                ]
                image_inputs, video_inputs = process_vision_info(messages, image_patch_size=16)
                inputs = self.vlm_processor(
                    text=texts,
                    images=image_inputs,
                    videos=video_inputs,
                    padding=True,
                    return_tensors="pt",
                ).to(self.device)
                # input_ids torch.Size([1, 80])
                # attention_mask torch.Size([1, 80])
                # pixel_values torch.Size([256, 1536])
                # image_grid_thw torch.Size([1, 3])
                batch_input_ids.append(inputs['input_ids'].squeeze(0))
                batch_attention_mask.append(inputs['attention_mask'].squeeze(0))
                batch_pixel_values.append(inputs['pixel_values'])
                batch_image_grid_thw.append(inputs['image_grid_thw'].squeeze(0))

            batch_input_ids = torch.stack(batch_input_ids) # (B, 80)
            batch_attention_mask = torch.stack(batch_attention_mask) # (B, 80)
            batch_pixel_values = torch.stack(batch_pixel_values) # (B, 256, 1536)
            batch_image_grid_thw = torch.stack(batch_image_grid_thw) # (B, 3)

            with torch.autocast("cuda", dtype=torch.bfloat16):
                # extract vision + language features
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    pixel_values=batch_pixel_values,
                    image_grid_thw=batch_image_grid_thw,
                    output_hidden_states=True,
                    return_dict=True
                )
                vlm_hidden_states_ = output.hidden_states # len(vlm_hidden_states_) == 29
                

                # use hidden states from the last layer
                vlm_hidden_states = vlm_hidden_states_[-1] # shape (B, seq_len, D_h)  shape(16, 80, 2048)
                vlm_hidden_states = vlm_hidden_states.unsqueeze(1) # shape (B, 1, seq_len, D_h) (16, 1, 80, 2048)

                # generate action from noise
                action_samples = torch.randn(
                    bsz, self.action_horizon, self.action_dim, device=self.device
                )
                self.noise_scheduler.set_timesteps(num_inference_steps)

                # Fixed noise for consistency in inpainting
                # Use the same noise as action_samples to ensure the whole sequence starts from the same latent x_1
                target_noise = action_samples.clone()

                for i, timestep in enumerate(self.noise_scheduler.timesteps):
                    batched_timestep = timestep.expand(bsz).to(self.device).detach()

                    ### Naive inpainting (Repaint method):
                    # Step 1: Add noise to prev_actions matching current timestep's noise level
                    # This ensures consistency in noise levels across the entire action sequence
                    tau = self.noise_scheduler.sigmas[self.noise_scheduler.index_for_timestep(timestep)]
                    noisy_prev_actions = (1 - tau) * prev_actions + tau * target_noise
                    
                    # Step 2: Replace first d positions with noisy prev_actions (hard mask)
                    action_samples[:, :d, :] = noisy_prev_actions[:, :d, :]

                    # Step 3: Model prediction
                    model_pred = self.action_header(
                        hidden_states=None,
                        timestep=batched_timestep,
                        joint_attention_kwargs=dict(
                            action_hidden_embeds=action_samples, # (B,Tp,Da)
                            views=vlm_hidden_states,  # (B,V,N,D)
                            obs=states,  # (B,1,M)
                            traj2ds=traj2ds,  # (B, C, 3, H, W)
                            progress=progress,
                            future_progress=future_progress,
                        ),
                        return_dict=True,
                    ).action

                    # Step 4: Denoise one step (updates all positions including first d)
                    action_samples = self.noise_scheduler.step(
                        model_output=model_pred, timestep=timestep, sample=action_samples # type: ignore
                    ).prev_sample
                    
                    # Step 5: Re-inpaint with noise level matching the denoised state
                    # TODO: do this or not??? boqian
                    if i == len(self.noise_scheduler.timesteps) - 1:
                        # Last step: use clean prev_actions (tau = 0)
                        action_samples[:, :d, :] = prev_actions[:, :d, :]

            return action_samples


# TODO-yth: 重要
    def predict_action_with_rtc_flow_based_gradient(
        self,
        observations: List[List[Image.Image]],  # B * List of PIL Image as [view1, view2]
        states: torch.Tensor, # (B, Ts, Ds)
        instructions: List[str], # (B,)
        num_inference_steps: int,
        traj2ds, 
        prev_actions: torch.Tensor | None = None, # (1, H, D)
        inference_delay: int = 0,
        execution_horizon: int = 0,
        mask_schedule: str = "exponential",
        guidance_weight: float = 5.0,
        **kwargs: str
    ) -> np.ndarray:
        with torch.no_grad():

            ## RTC related ##
            H = self.action_horizon
            assert prev_actions is not None and inference_delay > 0 and execution_horizon > 0, "prev_actions, inference_delay and execution_horizon must be provided"
            assert prev_actions.shape[0] == 1
            prev_actions = prev_actions.to(device=self.device, dtype=torch.float32)

            # Create soft mask for inpainting
            d = inference_delay
            s = execution_horizon

            # Validate constraint from paper: d ≤ s ≤ H - d
            assert d <= s and s <= H - d, f"Constraint violated: d={d}, s={s}, H={H}. Need d ≤ s ≤ H-d"

            # Create soft mask [H] according to Equation 5
            mask = self._create_soft_mask(
                H, d, s, schedule=mask_schedule, device=self.device
            )
            mask_expanded = mask.view(1, -1, 1).expand(1, self.action_horizon, self.action_dim)
            ##              ##


            bsz = states.shape[0]
            progress = kwargs.get("progress", None)
            future_progress = kwargs.get("future_progress", None)
            batch_input_ids = []
            batch_attention_mask = []
            batch_pixel_values = []
            batch_image_grid_thw = []


            for observation, instruction in zip(observations, instructions):
                messages = []
                content = [{"type": "image", "image": img} for img in observation]
                content.append({"type": "text", "text": instruction})
                user_msg = {"role": "user", "content": content}
                messages.append([user_msg])
                texts = [
                    self.vlm_processor.apply_chat_template(
                        m, tokenize=False, add_generation_prompt=True
                    )
                    for m in messages
                ]
                image_inputs, video_inputs = process_vision_info(messages, image_patch_size=16)
                inputs = self.vlm_processor(
                    text=texts,
                    images=image_inputs,
                    videos=video_inputs,
                    padding=True,
                    return_tensors="pt",
                ).to(self.device)
                # input_ids torch.Size([1, 80])
                # attention_mask torch.Size([1, 80])
                # pixel_values torch.Size([256, 1536])
                # image_grid_thw torch.Size([1, 3])
                batch_input_ids.append(inputs['input_ids'].squeeze(0))
                batch_attention_mask.append(inputs['attention_mask'].squeeze(0))
                batch_pixel_values.append(inputs['pixel_values'])
                batch_image_grid_thw.append(inputs['image_grid_thw'].squeeze(0))

            batch_input_ids = torch.stack(batch_input_ids) # (B, 80)
            batch_attention_mask = torch.stack(batch_attention_mask) # (B, 80)
            batch_pixel_values = torch.stack(batch_pixel_values) # (B, 256, 1536)
            batch_image_grid_thw = torch.stack(batch_image_grid_thw) # (B, 3)

            with torch.autocast("cuda", dtype=torch.bfloat16):
                # extract vision + language features
                output = self.vlm_model(
                    input_ids=batch_input_ids,
                    attention_mask=batch_attention_mask,
                    pixel_values=batch_pixel_values,
                    image_grid_thw=batch_image_grid_thw,
                    output_hidden_states=True,
                    return_dict=True
                )
                vlm_hidden_states_ = output.hidden_states # len(vlm_hidden_states_) == 29
                

                # use hidden states from the last layer
                vlm_hidden_states = vlm_hidden_states_[-1] # shape (B, seq_len, D_h)  shape(16, 80, 2048)
                vlm_hidden_states = vlm_hidden_states.unsqueeze(1) # shape (B, 1, seq_len, D_h) (16, 1, 80, 2048)

                # 初始化 action_samples
                action_samples = torch.randn(
                    bsz, self.action_horizon, self.action_dim, device=self.device
                )
                
                # 使用同样的 noise 构建目标,保证一致性
                target_noise = action_samples.clone()
                
                self.noise_scheduler.set_timesteps(num_inference_steps)

                for i, timestep in enumerate(self.noise_scheduler.timesteps):
                    # 1. 计算当前步的目标 (Noisy Target)
                    # ---------------------------------------------------------
                    tau = self.noise_scheduler.sigmas[self.noise_scheduler.index_for_timestep(timestep)]
                    
                    # 这是一个移动的目标:随着 tau 变小,它越来越接近真实的 prev_actions
                    noisy_prev_actions = (1 - tau) * prev_actions + tau * target_noise
                    
                    # 2. 基于梯度的 Guidance (核心部分)
                    # ---------------------------------------------------------
                    # 我们希望 action_samples 在 mask 区域尽可能接近 noisy_prev_actions
                    # 通过梯度下降来修改 action_samples
                    
                    # 定义 Guidance 的强度衰减:
                    # 早期(噪声大)需要强引导来定型,晚期(噪声小)减弱引导以允许模型平滑过渡
                    current_scale = guidance_weight * (tau ** 0.5) # 这里的衰减策略可以调整,例如用 linear

                    # 临时开启梯度计算
                    with torch.enable_grad():
                        # Detach 并开启梯度,创建一个新的叶子节点
                        x_in = action_samples.detach().requires_grad_(True)
                        
                        # 计算 Loss: 只在 mask 有值的地方计算 MSE
                        # 这里的 mask_expanded 充当了权重的角色
                        # 重点:只计算前 s 步的差异 (inference_delay + execution_horizon)
                        diff = (x_in - noisy_prev_actions)
                        
                        # 使用 mask 加权平方误差
                        # 形状: (B, H, D) * (1, H, D)
                        loss = (diff ** 2 * mask_expanded).sum() 
                        
                        # 计算梯度
                        grad = torch.autograd.grad(loss, x_in)[0]

                    # 更新 action_samples (朝着 loss 减小的方向)
                    # 注意:这里我们是在 Latent 空间进行微调
                    print("grad[:, :, 0:3].abs().mean():", grad[:, :, 0:3].abs().mean())
                    print("grad[:, :, 15:17].abs().mean():", grad[:, :, 15:17].abs().mean())
                    

                    action_samples = action_samples - current_scale * grad

                    # 3. 常规的 Flow Matching / Diffusion Step
                    # ---------------------------------------------------------
                    batched_timestep = timestep.expand(bsz).to(self.device).detach()
                    
                    model_pred = self.action_header(
                        hidden_states=None,
                        timestep=batched_timestep,
                        joint_attention_kwargs=dict(
                            action_hidden_embeds=action_samples, 
                            views=vlm_hidden_states,
                            obs=states,
                            traj2ds=traj2ds,
                            progress=progress,
                            future_progress=future_progress,
                        ),
                        return_dict=True,
                    ).action

                    # 4. Denoise one step
                    action_samples = self.noise_scheduler.step(
                        model_output=model_pred, timestep=timestep, sample=action_samples
                    ).prev_sample

                    # 5. (可选) Soft Blending 作为最后的保险
                    # 如果单纯靠梯度还是压不住,可以在最后一步稍微融合一下
                    # 但如果 guidance_weight 足够,通常不需要这步
                    # if i == len(self.noise_scheduler.timesteps) - 1:
                    #         # 最后一步不再有噪声,直接做一次软混合确保开头完全吻合
                    #         action_samples = prev_actions * mask_expanded + action_samples * (1 - mask_expanded)

            return action_samples




    def _create_soft_mask(self, H, d, s, schedule="exponential", device="cpu"):
        """
        Create soft mask for RTC inpainting (Equation 5 in paper).
        
        Paper: "Real-Time Execution of Action Chunking Flow Policies"
        Figure 3 and Equation 5:
        
        W_i = { 1                           if i < d (frozen region)
              { c_i * (e^(c_i) - 1)/(e - 1) if d ≤ i < H - s (intermediate region)
              { 0                           if i ≥ H - s (free region)
        
        where c_i = (H - s - i)/(H - s - d + 1)
        
        Args:
            H: Prediction horizon (total sequence length)
            d: Inference delay (number of frozen steps, already executed)
            s: Execution horizon (non-overlapping steps at end). 
            schedule: 'exponential' (paper default), 'linear', 'hard', or 'simple'
        
        Returns:
            mask: [H] tensor with values in [0, 1]
                  Guidance weights for each timestep
        """
        mask = torch.zeros(H, device=device)
        
        if schedule == "hard":
            # Hard mask: 1 for frozen, 0 for rest
            mask[:d] = 1.0
        
        elif schedule == "linear":
            # Linear decay
            mask[:d] = 1.0
            # Three-region version (full paper)
            overlap_end = H - s
            if d < overlap_end:
                # Linear decay from 1 to 0 in intermediate region
                indices = torch.arange(d, overlap_end, device=device).float()
                mask[d:overlap_end] = 1.0 - (indices - d) / (overlap_end - d)
            # mask[overlap_end:] remains 0
        
        elif schedule == "exponential":
            # Exponential decay (paper default, Equation 5)
            mask[:d] = 1.0
            
            # Three-region version (full paper implementation)
            overlap_end = H - s
            if d < overlap_end:
                indices = torch.arange(d, overlap_end, device=device).float()
                # c_i = (H - s - i) / (H - s - d + 1)
                c_i = (overlap_end - indices) / (overlap_end - d + 1)
                # W_i = c_i * (e^(c_i) - 1) / (e - 1)
                e = torch.tensor(torch.e, device=device)
                mask[d:overlap_end] = c_i * (torch.exp(c_i) - 1) / (e - 1)
            # mask[overlap_end:] remains 0
        
        elif schedule == "simple":
            # Simplified exponential (for backward compatibility)
            mask[:d] = 1.0
            if d < H:
                indices = torch.arange(d, H, device=device).float()
                mask[d:] = torch.exp(-5.0 * (indices - d) / (H - d))
        
        else:
            raise ValueError(f"Unknown mask schedule: {schedule}")
        
        return mask
    
    @torch.inference_mode()
    def chat(self):
        ...