File size: 46,860 Bytes
4f07533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
DiT (Diffusion Transformer) based flow matching action head for PRTS.

Replaces the Qwen3VLTextModel-based fm_action_expert with a lightweight DiT
that uses explicit cross-attention to VLM hidden states, following the architecture
from GR00T / pi05.

Architecture:
    ActionEncoder(noisy_actions + dof_mask, timestep)
    → action_features
    → DiT(cross-attn to VLM hidden states, ada-norm timestep conditioning)
    → ActionDecoder → predicted velocity
"""

import math

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Beta
from typing import Optional

from transformers.cache_utils import Cache
from transformers.modeling_flash_attention_utils import _flash_attention_forward


# DIT_PRESETS = {
#     "DiT-B": {"num_attention_heads": 12, "attention_head_dim": 64, "output_dim": 768},
#     "DiT-L": {"num_attention_heads": 32, "attention_head_dim": 48, "output_dim": 1536},
# }


class SinusoidalPositionalEncoding(nn.Module):
    """Sinusoidal positional encoding for sequence positions or timesteps."""

    def __init__(self, embedding_dim: int):
        super().__init__()
        self.embedding_dim = embedding_dim

    def forward(self, timesteps: torch.Tensor) -> torch.Tensor:
        timesteps = timesteps.float()
        squeeze = False
        if timesteps.dim() == 1:
            timesteps = timesteps.unsqueeze(1)
            squeeze = True

        half_dim = self.embedding_dim // 2
        exponent = -torch.arange(half_dim, dtype=torch.float, device=timesteps.device) * (
            math.log(10000.0) / half_dim
        )
        freqs = timesteps.unsqueeze(-1) * exponent.exp()
        enc = torch.cat([torch.sin(freqs), torch.cos(freqs)], dim=-1)

        if squeeze:
            enc = enc.squeeze(1)
        return enc


class TimestepEncoder(nn.Module):
    """Projects scalar timesteps to embedding space via sinusoidal encoding + MLP."""

    def __init__(self, embedding_dim: int):
        super().__init__()
        self.sinusoidal = SinusoidalPositionalEncoding(256)
        self.linear_1 = nn.Linear(256, embedding_dim)
        self.act = nn.SiLU()
        self.linear_2 = nn.Linear(embedding_dim, embedding_dim)

    def forward(self, timesteps: torch.Tensor) -> torch.Tensor:
        t_emb = self.sinusoidal(timesteps)
        t_emb = self.linear_1(t_emb.to(dtype=self.linear_1.weight.dtype))
        t_emb = self.act(t_emb)
        t_emb = self.linear_2(t_emb)
        return t_emb


class AdaLayerNorm(nn.Module):
    """Adaptive Layer Normalization conditioned on timestep embeddings.

    Applies scale-shift modulation: out = norm(x) * (1 + scale) + shift,
    where (scale, shift) are linearly projected from the timestep embedding.
    """

    def __init__(self, embedding_dim: int, eps: float = 1e-5):
        super().__init__()
        self.silu = nn.SiLU()
        self.linear = nn.Linear(embedding_dim, embedding_dim * 2)
        self.norm = nn.LayerNorm(embedding_dim, eps=eps, elementwise_affine=False)

    def forward(self, x: torch.Tensor, temb: torch.Tensor) -> torch.Tensor:
        temb = self.linear(self.silu(temb))
        scale, shift = temb.chunk(2, dim=-1)
        x = self.norm(x) * (1 + scale[:, None]) + shift[:, None]
        return x


class DiTAttention(nn.Module):
    """Multi-head attention supporting both self-attention and cross-attention.

    Supports two backends selected via ``attn_implementation``:

    * ``"sdpa"`` (default) – uses :func:`F.scaled_dot_product_attention`, which
      dispatches automatically to FlashAttention / memory-efficient attention
      depending on the installed PyTorch build.  The encoder padding mask is
      expanded to ``(B, 1, 1, S)`` and passed as ``attn_mask``.

    * ``"flash_attention_2"`` – calls the ``flash_attn`` package directly for
      lower memory usage and higher throughput.  For cross-attention with an
      encoder padding mask the k/v tensors are unpadded and
      :func:`flash_attn_varlen_func` is used so that padding tokens are never
      processed.  For self-attention (no mask) the simpler
      :func:`flash_attn_func` is used.
    """

    def __init__(
        self,
        query_dim: int,
        num_heads: int,
        head_dim: int,
        cross_attention_dim: Optional[int] = None,
        dropout: float = 0.0,
        bias: bool = True,
        attn_implementation: str = "sdpa",
    ):
        super().__init__()
        self.num_heads = num_heads
        self.head_dim = head_dim
        self.attn_implementation = attn_implementation
        inner_dim = num_heads * head_dim

        self.to_q = nn.Linear(query_dim, inner_dim, bias=bias)
        kv_dim = cross_attention_dim if cross_attention_dim is not None else query_dim
        self.to_k = nn.Linear(kv_dim, inner_dim, bias=bias)
        self.to_v = nn.Linear(kv_dim, inner_dim, bias=bias)
        self.to_out = nn.Sequential(
            nn.Linear(inner_dim, query_dim, bias=bias),
            nn.Dropout(dropout),
        )

    # ------------------------------------------------------------------
    # Flash-Attention backend
    # ------------------------------------------------------------------

    def _flash_attn_forward(
        self,
        q: torch.Tensor,
        k: torch.Tensor,
        v: torch.Tensor,
        attention_mask: Optional[torch.Tensor],
    ) -> torch.Tensor:
        """Run Flash Attention via HuggingFace's ``_flash_attention_forward``.

        Args:
            q: ``(B, T_q, H, D)``
            k: ``(B, T_k, H, D)``
            v: ``(B, T_k, H, D)``
            attention_mask: ``(B, T_k)`` bool, True = valid token.

        Returns:
            ``(B, T_q, H*D)``
        """

        B, T_q, H, D = q.shape
        # _flash_attention_forward returns (B, T_q, H, D); handles unpad/varlen internally.
        out = _flash_attention_forward(
            q, k, v,
            attention_mask=attention_mask,
            query_length=T_q,
            is_causal=False,
            dropout=0.0,
        )
        return out.reshape(B, T_q, H * D)

    # ------------------------------------------------------------------
    # Forward
    # ------------------------------------------------------------------

    def forward(
        self,
        hidden_states: torch.Tensor,
        encoder_hidden_states: Optional[torch.Tensor] = None,
        attention_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        B, T, _ = hidden_states.shape

        q = self.to_q(hidden_states)
        kv_input = encoder_hidden_states if encoder_hidden_states is not None else hidden_states
        k = self.to_k(kv_input)
        v = self.to_v(kv_input)

        if self.attn_implementation == "flash_attention_2":
            # Flash Attention expects (B, S, H, D)
            q = q.view(B, T, self.num_heads, self.head_dim)
            k = k.view(B, -1, self.num_heads, self.head_dim)
            v = v.view(B, -1, self.num_heads, self.head_dim)
            attn_output = self._flash_attn_forward(q, k, v, attention_mask)
        else:
            # SDPA expects (B, H, S, D)
            q = q.view(B, T, self.num_heads, self.head_dim).transpose(1, 2)
            k = k.view(B, -1, self.num_heads, self.head_dim).transpose(1, 2)
            v = v.view(B, -1, self.num_heads, self.head_dim).transpose(1, 2)

            # Expand (B, S) bool mask → (B, 1, 1, S) for broadcasting.
            sdpa_mask = None
            if attention_mask is not None:
                if attention_mask.dim() == 2:
                    sdpa_mask = attention_mask[:, None, None, :]
                else:
                    sdpa_mask = attention_mask

            attn_output = F.scaled_dot_product_attention(
                q, k, v, attn_mask=sdpa_mask, dropout_p=0.0
            )
            attn_output = attn_output.transpose(1, 2).contiguous().view(B, T, -1)

        return self.to_out(attn_output)


class FeedForward(nn.Module):
    """Feed-forward network with GELU activation."""

    def __init__(self, dim: int, dropout: float = 0.0, mult: int = 4):
        super().__init__()
        inner_dim = dim * mult
        self.net = nn.Sequential(
            nn.Linear(dim, inner_dim),
            nn.GELU(approximate="tanh"),
            nn.Dropout(dropout),
            nn.Linear(inner_dim, dim),
            nn.Dropout(dropout),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.net(x)


class BasicTransformerBlock(nn.Module):
    """Transformer block with self/cross-attention, optional AdaLayerNorm, and feed-forward.

    When cross_attention_dim is set, the attention block performs cross-attention
    to encoder_hidden_states. Otherwise, it performs self-attention.
    """

    def __init__(
        self,
        dim: int,
        num_attention_heads: int,
        attention_head_dim: int,
        dropout: float = 0.0,
        cross_attention_dim: Optional[int] = None,
        norm_type: str = "ada_norm",
        final_dropout: bool = False,
        attn_implementation: str = "sdpa",
    ):
        super().__init__()
        self.norm_type = norm_type

        if norm_type == "ada_norm":
            self.norm1 = AdaLayerNorm(dim)
        else:
            self.norm1 = nn.LayerNorm(dim)

        self.attn1 = DiTAttention(
            query_dim=dim,
            num_heads=num_attention_heads,
            head_dim=attention_head_dim,
            cross_attention_dim=cross_attention_dim,
            dropout=dropout,
            attn_implementation=attn_implementation,
        )

        self.norm3 = nn.LayerNorm(dim)
        self.ff = FeedForward(dim, dropout=dropout)
        self.final_dropout = nn.Dropout(dropout) if final_dropout else None

    def forward(
        self,
        hidden_states: torch.Tensor,
        encoder_hidden_states: Optional[torch.Tensor] = None,
        encoder_attention_mask: Optional[torch.Tensor] = None,
        temb: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        if self.norm_type == "ada_norm":
            norm_hidden_states = self.norm1(hidden_states, temb)
        else:
            norm_hidden_states = self.norm1(hidden_states)

        attn_output = self.attn1(
            norm_hidden_states,
            encoder_hidden_states=encoder_hidden_states,
            attention_mask=encoder_attention_mask,
        )

        if self.final_dropout is not None:
            attn_output = self.final_dropout(attn_output)

        hidden_states = attn_output + hidden_states

        norm_hidden_states = self.norm3(hidden_states)
        ff_output = self.ff(norm_hidden_states)
        hidden_states = ff_output + hidden_states

        return hidden_states


class DiT(nn.Module):
    """Diffusion Transformer with cross-attention to VLM context features.

    Interleaves cross-attention blocks (attending to encoder_hidden_states)
    with self-attention blocks when interleave_self_attention=True.
    Uses AdaLayerNorm for timestep conditioning throughout.

    Output block applies timestep-conditioned scale-shift before final projection.
    """

    def __init__(
        self,
        num_attention_heads: int = 12,
        attention_head_dim: int = 64,
        output_dim: int = 768,
        num_layers: int = 12,
        dropout: float = 0.1,
        norm_type: str = "ada_norm",
        final_dropout: bool = True,
        interleave_self_attention: bool = False,
        cross_attention_dim: Optional[int] = None,
        attn_implementation: str = "sdpa",
    ):
        super().__init__()
        self.inner_dim = num_attention_heads * attention_head_dim
        self.output_dim = output_dim
        self.num_layers = num_layers
        self.interleave_self_attention = interleave_self_attention

        self.timestep_encoder = TimestepEncoder(self.inner_dim)

        all_blocks = []
        for idx in range(num_layers):
            use_self_attn = idx % 2 == 1 and interleave_self_attention
            curr_cross_attention_dim = cross_attention_dim if not use_self_attn else None

            all_blocks.append(
                BasicTransformerBlock(
                    dim=self.inner_dim,
                    num_attention_heads=num_attention_heads,
                    attention_head_dim=attention_head_dim,
                    dropout=dropout,
                    cross_attention_dim=curr_cross_attention_dim,
                    norm_type=norm_type,
                    final_dropout=final_dropout,
                    attn_implementation=attn_implementation,
                )
            )
        self.transformer_blocks = nn.ModuleList(all_blocks)

        self.norm_out = nn.LayerNorm(self.inner_dim, elementwise_affine=False, eps=1e-6)
        self.proj_out_1 = nn.Linear(self.inner_dim, 2 * self.inner_dim)
        self.proj_out_2 = nn.Linear(self.inner_dim, output_dim)

    def forward(
        self,
        hidden_states: torch.Tensor,
        encoder_hidden_states: torch.Tensor,
        timestep: torch.LongTensor,
        encoder_attention_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        temb = self.timestep_encoder(timestep)

        hidden_states = hidden_states.contiguous()
        encoder_hidden_states = encoder_hidden_states.contiguous()

        for idx, block in enumerate(self.transformer_blocks):
            if idx % 2 == 1 and self.interleave_self_attention:
                hidden_states = block(
                    hidden_states,
                    encoder_hidden_states=None,
                    encoder_attention_mask=None,
                    temb=temb,
                )
            else:
                hidden_states = block(
                    hidden_states,
                    encoder_hidden_states=encoder_hidden_states,
                    encoder_attention_mask=encoder_attention_mask,
                    temb=temb,
                )

        conditioning = temb
        shift, scale = self.proj_out_1(F.silu(conditioning)).chunk(2, dim=-1)
        hidden_states = self.norm_out(hidden_states) * (1 + scale[:, None]) + shift[:, None]
        return self.proj_out_2(hidden_states)


class AlternateVLDiT(DiT):
    """DiT variant that separates visual and text tokens during cross-attention.

    Mirrors GR00T's AlternateVLDiT: even-indexed blocks do cross-attention,
    alternating every ``attend_text_every_n_blocks`` between text tokens and
    visual tokens.  Odd-indexed blocks do self-attention (requires
    ``interleave_self_attention=True``).

    When no visual tokens are present (``image_mask`` is None or all-False),
    all valid tokens are treated as text.
    """

    def __init__(self, *args, attend_text_every_n_blocks: int = 2, **kwargs):
        super().__init__(*args, **kwargs)
        assert self.interleave_self_attention, (
            "AlternateVLDiT requires interleave_self_attention=True"
        )
        self.attend_text_every_n_blocks = attend_text_every_n_blocks

    def forward(
        self,
        hidden_states: torch.Tensor,
        encoder_hidden_states: torch.Tensor,
        timestep: torch.LongTensor,
        encoder_attention_mask: Optional[torch.Tensor] = None,
        image_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        """
        Args:
            encoder_attention_mask: (B, S) bool – True = valid VLM token.
            image_mask: (B, S) bool – True = visual token position.
                If None, all valid tokens are treated as text.
        """
        temb = self.timestep_encoder(timestep)
        hidden_states = hidden_states.contiguous()
        encoder_hidden_states = encoder_hidden_states.contiguous()

        B, S, _ = encoder_hidden_states.shape
        backbone_mask = (
            encoder_attention_mask.bool()
            if encoder_attention_mask is not None
            else torch.ones(B, S, dtype=torch.bool, device=hidden_states.device)
        )

        if image_mask is not None and image_mask.any():
            vis_mask  = image_mask.bool() & backbone_mask   # visual tokens
            text_mask = (~image_mask.bool()) & backbone_mask # text tokens
        else:
            # No visual tokens – treat everything as text.
            vis_mask  = torch.zeros_like(backbone_mask)
            text_mask = backbone_mask

        for idx, block in enumerate(self.transformer_blocks):
            if idx % 2 == 1:
                # Self-attention block.
                hidden_states = block(
                    hidden_states,
                    encoder_hidden_states=None,
                    encoder_attention_mask=None,
                    temb=temb,
                )
            else:
                # Cross-attention block: alternate text / visual every N blocks.
                if idx % (2 * self.attend_text_every_n_blocks) == 0:
                    curr_mask = text_mask
                else:
                    curr_mask = vis_mask
                hidden_states = block(
                    hidden_states,
                    encoder_hidden_states=encoder_hidden_states,
                    encoder_attention_mask=curr_mask,
                    temb=temb,
                )

        conditioning = temb
        shift, scale = self.proj_out_1(F.silu(conditioning)).chunk(2, dim=-1)
        hidden_states = self.norm_out(hidden_states) * (1 + scale[:, None]) + shift[:, None]
        return self.proj_out_2(hidden_states)


class ActionEncoder(nn.Module):
    """Encodes noisy actions (optionally concatenated with DOF mask) and timestep
    into hidden features via MLP + sinusoidal time encoding.

    Architecture: Linear → concat(action_emb, time_emb) → SiLU + Linear → Linear
    """

    def __init__(self, action_input_dim: int, hidden_size: int):
        super().__init__()
        self.hidden_size = hidden_size
        self.layer1 = nn.Linear(action_input_dim, hidden_size)
        self.layer2 = nn.Linear(2 * hidden_size, hidden_size)
        self.layer3 = nn.Linear(hidden_size, hidden_size)
        self.pos_encoding = SinusoidalPositionalEncoding(hidden_size)

    def forward(self, actions: torch.Tensor, timesteps: torch.Tensor) -> torch.Tensor:
        """
        Args:
            actions: (B, T, action_input_dim) noisy actions (+ DOF mask)
            timesteps: (B,) discretized timesteps
        """
        B, T, _ = actions.shape
        timesteps_expanded = timesteps.unsqueeze(1).expand(-1, T)

        a_emb = self.layer1(actions)
        tau_emb = self.pos_encoding(timesteps_expanded).to(dtype=a_emb.dtype)

        x = torch.cat([a_emb, tau_emb], dim=-1)
        x = F.silu(self.layer2(x))
        x = self.layer3(x)
        return x


class ActionDecoder(nn.Module):
    """2-layer MLP that decodes DiT output to action-space velocity."""

    def __init__(self, input_dim: int, hidden_dim: int, output_dim: int):
        super().__init__()
        self.layer1 = nn.Linear(input_dim, hidden_dim)
        self.layer2 = nn.Linear(hidden_dim, output_dim)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.layer2(F.relu(self.layer1(x)))


class FlowMatchingDiTHead(nn.Module):
    """Flow matching action head using DiT (Diffusion Transformer).

    Replaces the fm_action_expert (Qwen3VLTextModel-based) with a DiT that uses
    explicit cross-attention to VLM hidden states instead of KV cache continuation.

    Training:
        1. Sample noise and timestep from Beta distribution
        2. Compute noisy trajectory: x_t = (1-t)*noise + t*actions
        3. Compute velocity target: v = actions - noise
        4. Encode noisy actions + DOF mask + timestep → action features
        5. Prepend learned future query tokens
        6. Run DiT with cross-attention to VLM hidden states
        7. Decode to action-space velocity prediction

    Inference:
        Euler integration from pure noise (t=0) to clean actions (t=1)
        over num_inference_timesteps steps.
    """

    def __init__(
        self,
        action_dim: int,
        action_chunk_size: int,
        cross_attention_dim: int,
        num_inference_timesteps: int = 4,
        config: Optional[dict] = None,
    ):
        super().__init__()
        cfg = {
            "num_layers": 16,
            "num_attention_heads": 12,
            "attention_head_dim": 64,
            "output_dim": 1024,
            "dropout": 0.2,
            "interleave_self_attention": True,
            "norm_type": "ada_norm",
            "final_dropout": True,
            "add_pos_embed": True,
            "noise_beta_alpha": 1.5,
            "noise_beta_beta": 1.0,
            "noise_s": 0.999,
            "num_timestep_buckets": 1000,
            "attn_implementation": "sdpa",
            "use_alternate_vl_dit": False,
            "attend_text_every_n_blocks": 2,
        }
        if config is not None:
            cfg.update(config)
            # dit_model_type = config.get("dit_model_type")
            # if dit_model_type and dit_model_type in DIT_PRESETS:
            #     cfg.update(DIT_PRESETS[dit_model_type])
        # cfg.pop("dit_model_type", None)

        self.action_dim = action_dim
        self.action_chunk_size = action_chunk_size
        self.num_inference_timesteps = num_inference_timesteps
        self.num_timestep_buckets = cfg["num_timestep_buckets"]
        self.noise_s = cfg["noise_s"]
        self.use_alternate_vl_dit = cfg["use_alternate_vl_dit"]
        self.add_pos_embed = cfg["add_pos_embed"]

        num_attention_heads = cfg["num_attention_heads"]
        attention_head_dim = cfg["attention_head_dim"]
        output_dim = cfg["output_dim"]
        inner_dim = num_attention_heads * attention_head_dim

        dit_kwargs = dict(
            num_attention_heads=num_attention_heads,
            attention_head_dim=attention_head_dim,
            output_dim=output_dim,
            num_layers=cfg["num_layers"],
            dropout=cfg["dropout"],
            norm_type=cfg["norm_type"],
            final_dropout=cfg["final_dropout"],
            interleave_self_attention=cfg["interleave_self_attention"],
            cross_attention_dim=cross_attention_dim,
            attn_implementation=cfg["attn_implementation"],
        )
        if self.use_alternate_vl_dit:
            self.dit = AlternateVLDiT(
                **dit_kwargs,
                attend_text_every_n_blocks=cfg["attend_text_every_n_blocks"],
            )
        else:
            self.dit = DiT(**dit_kwargs)

        # action_dim * 2: noisy action + DOF mask concatenated
        self.action_encoder = ActionEncoder(action_dim * 2, inner_dim)
        self.action_decoder = ActionDecoder(output_dim, inner_dim, action_dim)

        if self.add_pos_embed:
            max_seq_len = max(action_chunk_size, 256)
            self.position_embedding = nn.Embedding(max_seq_len, inner_dim)
            nn.init.normal_(self.position_embedding.weight, mean=0.0, std=0.02)

        # self.beta_dist = Beta(cfg["noise_beta_alpha"], cfg["noise_beta_beta"])
        self._beta_alpha = cfg["noise_beta_alpha"]
        self._beta_beta  = cfg["noise_beta_beta"]

    def reset_parameters(self):
        """Re-apply proper initialization.

        HuggingFace from_pretrained calls _init_weights on modules whose
        parameters are absent from the checkpoint, overwriting any custom
        init done in __init__.  Call this after from_pretrained when loading
        from a base VLM checkpoint that does not contain DiT weights.
        """
        if self.add_pos_embed:
            nn.init.normal_(self.position_embedding.weight, mean=0.0, std=0.02)
        for module in self.modules():
            if isinstance(module, nn.Linear):
                nn.init.kaiming_uniform_(module.weight, a=math.sqrt(5))
                if module.bias is not None:
                    fan_in, _ = nn.init._calculate_fan_in_and_fan_out(module.weight)
                    bound = 1 / math.sqrt(fan_in) if fan_in > 0 else 0
                    nn.init.uniform_(module.bias, -bound, bound)
            elif isinstance(module, nn.LayerNorm):
                if module.elementwise_affine:
                    nn.init.ones_(module.weight)
                    nn.init.zeros_(module.bias)

    def sample_time(self, batch_size: int, device, dtype) -> torch.Tensor:
        beta_dist = Beta(self._beta_alpha, self._beta_beta)
        sample = beta_dist.sample([batch_size]).to(device, dtype=dtype).clamp(max=self.noise_s)
        return (self.noise_s - sample) / self.noise_s

    def _encode_actions(
        self,
        noisy_actions: torch.Tensor,
        t_discretized: torch.Tensor,
        action_dof_mask: Optional[torch.Tensor],
        device,
    ) -> torch.Tensor:
        """Encode noisy actions with DOF mask and timestep, add position embeddings."""
        if action_dof_mask is not None:
            encoder_input = torch.cat(
                [noisy_actions, action_dof_mask.to(noisy_actions.dtype)], dim=-1
            )
        else:
            encoder_input = torch.cat(
                [noisy_actions, torch.ones_like(noisy_actions)], dim=-1
            )

        action_features = self.action_encoder(encoder_input, t_discretized)

        if self.add_pos_embed:
            pos_ids = torch.arange(action_features.shape[1], dtype=torch.long, device=device)
            pos_embs = self.position_embedding(pos_ids).unsqueeze(0)
            action_features = action_features + pos_embs

        return action_features

    def _dit_forward(
        self,
        sa_embs: torch.Tensor,
        vl_embs: torch.Tensor,
        t_discretized: torch.LongTensor,
        encoder_attention_mask: Optional[torch.Tensor],
        image_mask: Optional[torch.Tensor],
    ) -> torch.Tensor:
        if self.use_alternate_vl_dit:
            return self.dit(
                hidden_states=sa_embs,
                encoder_hidden_states=vl_embs,
                timestep=t_discretized,
                encoder_attention_mask=encoder_attention_mask,
                image_mask=image_mask,
            )
        return self.dit(
            hidden_states=sa_embs,
            encoder_hidden_states=vl_embs,
            timestep=t_discretized,
            encoder_attention_mask=encoder_attention_mask,
        )

    def forward(
        self,
        vl_embs: torch.Tensor,
        actions: torch.Tensor,
        action_dof_mask: Optional[torch.Tensor] = None,
        encoder_attention_mask: Optional[torch.Tensor] = None,
        image_mask: Optional[torch.Tensor] = None,
    ) -> tuple:
        """Training forward pass.

        Args:
            vl_embs: (B, S, D) VLM hidden states for cross-attention
            actions: (B, T, action_dim) ground truth action trajectories
            action_dof_mask: (B, T, action_dim) DOF validity mask
            encoder_attention_mask: (B, S) bool – True = valid VLM token
            image_mask: (B, S) bool – True = visual token (used by AlternateVLDiT)

        Returns:
            (pred_v, velocity): predicted velocity and target velocity, both (B, T, action_dim)
        """
        device = vl_embs.device
        B = actions.shape[0]

        noise = torch.randn(actions.shape, device=device, dtype=actions.dtype)
        t = self.sample_time(B, device=device, dtype=actions.dtype)
        t_expanded = t[:, None, None]

        noisy_trajectory = (1 - t_expanded) * noise + t_expanded * actions
        velocity = actions - noise

        t_discretized = (t * self.num_timestep_buckets).long()

        action_features = self._encode_actions(noisy_trajectory, t_discretized, action_dof_mask, device)

        model_output = self._dit_forward(
            action_features, vl_embs, t_discretized, encoder_attention_mask, image_mask
        )

        pred = self.action_decoder(model_output)
        pred_v = pred[:, :actions.shape[1]]

        return pred_v, velocity

    @torch.no_grad()
    def predict_action(
        self,
        vl_embs: torch.Tensor,
        action_dof_mask: Optional[torch.Tensor] = None,
        encoder_attention_mask: Optional[torch.Tensor] = None,
        image_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        """Inference: denoise actions from noise using Euler integration.

        Args:
            vl_embs: (B, S, D) VLM hidden states
            action_dof_mask: optional (B, T, action_dim) or (1, T, action_dim) DOF mask
            encoder_attention_mask: (B, S) bool – True = valid VLM token
            image_mask: (B, S) bool – True = visual token (used by AlternateVLDiT)

        Returns:
            (B, T, action_dim) denoised action trajectories
        """
        B = vl_embs.shape[0]
        device = vl_embs.device
        dtype = vl_embs.dtype

        actions = torch.randn(
            (B, self.action_chunk_size, self.action_dim),
            device=device, dtype=dtype,
        )

        dt = 1.0 / self.num_inference_timesteps

        for step in range(self.num_inference_timesteps):
            t_cont = step / float(self.num_inference_timesteps)
            t_discretized_val = int(t_cont * self.num_timestep_buckets)
            timesteps_tensor = torch.full((B,), t_discretized_val, device=device, dtype=torch.long)

            action_features = self._encode_actions(actions, timesteps_tensor, action_dof_mask, device)

            model_output = self._dit_forward(
                action_features, vl_embs, timesteps_tensor, encoder_attention_mask, image_mask
            )

            pred = self.action_decoder(model_output)
            pred_velocity = pred[:, :self.action_chunk_size]

            actions = actions + dt * pred_velocity

        return actions


# ============================================================================
# Pi0.5-style KV-cache action expert (VLM K/V concat + GQA + SwiGLU FFN)
# ============================================================================
class AdaRMSNorm(nn.Module):
    """Adaptive RMS normalization: (scale, shift, gate) from cond; zero-init."""

    def __init__(self, dim: int, eps: float = 1e-6):
        super().__init__()
        self.eps = eps
        self.modulation = nn.Linear(dim, dim * 3)
        nn.init.zeros_(self.modulation.weight)
        nn.init.zeros_(self.modulation.bias)

    def forward(self, x: torch.Tensor, cond: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
        var = x.float().pow(2).mean(-1, keepdim=True)
        normed = (x * torch.rsqrt(var + self.eps)).to(x.dtype)
        scale, shift, gate = self.modulation(cond).chunk(3, dim=-1)
        normed = normed * (1 + scale[:, None]) + shift[:, None]
        return normed, gate[:, None]


class SwiGLUFeedForward(nn.Module):
    """SiLU(gate_proj(x)) * up_proj(x) → down_proj."""

    def __init__(self, dim: int, hidden_dim: int, dropout: float = 0.0, bias: bool = True):
        super().__init__()
        self.gate_proj = nn.Linear(dim, hidden_dim, bias=bias)
        self.up_proj = nn.Linear(dim, hidden_dim, bias=bias)
        self.down_proj = nn.Linear(hidden_dim, dim, bias=bias)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.down_proj(self.dropout(F.silu(self.gate_proj(x)) * self.up_proj(x)))


class MoTAttention(nn.Module):
    """Action Q attends to concatenated [VLM KV cache ; action KV]; GQA expand for SDPA."""

    def __init__(
        self,
        hidden_size: int,
        num_attention_heads: int,
        num_kv_heads: int,
        head_dim: int,
        dropout: float = 0.0,
        bias: bool = True,
    ):
        super().__init__()
        if num_attention_heads % num_kv_heads != 0:
            raise ValueError(
                f"num_attention_heads ({num_attention_heads}) must be divisible by "
                f"num_kv_heads ({num_kv_heads})"
            )
        self.num_attention_heads = num_attention_heads
        self.num_kv_heads = num_kv_heads
        self.head_dim = head_dim
        q_dim = num_attention_heads * head_dim
        kv_dim = num_kv_heads * head_dim
        self.q_proj = nn.Linear(hidden_size, q_dim, bias=bias)
        self.k_proj = nn.Linear(hidden_size, kv_dim, bias=bias)
        self.v_proj = nn.Linear(hidden_size, kv_dim, bias=bias)
        self.o_proj = nn.Linear(q_dim, hidden_size, bias=bias)
        self.dropout = nn.Dropout(dropout)

    def forward(
        self,
        action_hidden: torch.Tensor,
        vlm_cached_k: torch.Tensor,
        vlm_cached_v: torch.Tensor,
        vlm_attention_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        B, T_a, _ = action_hidden.shape

        q = self.q_proj(action_hidden)
        act_k = self.k_proj(action_hidden)
        act_v = self.v_proj(action_hidden)

        q = q.view(B, T_a, self.num_attention_heads, self.head_dim).transpose(1, 2)
        act_k = act_k.view(B, T_a, self.num_kv_heads, self.head_dim).transpose(1, 2)
        act_v = act_v.view(B, T_a, self.num_kv_heads, self.head_dim).transpose(1, 2)

        k = torch.cat([vlm_cached_k, act_k], dim=2)
        v = torch.cat([vlm_cached_v, act_v], dim=2)

        repeat_factor = self.num_attention_heads // self.num_kv_heads
        k = k.repeat_interleave(repeat_factor, dim=1)
        v = v.repeat_interleave(repeat_factor, dim=1)

        sdpa_mask = None
        if vlm_attention_mask is not None:
            action_mask = vlm_attention_mask.new_ones(B, T_a)
            combined_mask = torch.cat([vlm_attention_mask, action_mask], dim=1)
            sdpa_mask = combined_mask[:, None, None, :]

        attn_out = F.scaled_dot_product_attention(
            q, k, v, attn_mask=sdpa_mask, dropout_p=0.0,
        )
        attn_out = attn_out.transpose(1, 2).contiguous().view(B, T_a, -1)
        return self.dropout(self.o_proj(attn_out))


class MoTBlock(nn.Module):
    """AdaRMSNorm → attention → gated residual → AdaRMSNorm → SwiGLU FFN → gated residual."""

    def __init__(
        self,
        hidden_size: int,
        num_attention_heads: int,
        num_kv_heads: int,
        head_dim: int,
        intermediate_size: int,
        dropout: float = 0.0,
    ):
        super().__init__()
        self.pre_attn_norm = AdaRMSNorm(hidden_size)
        self.attn = MoTAttention(
            hidden_size=hidden_size,
            num_attention_heads=num_attention_heads,
            num_kv_heads=num_kv_heads,
            head_dim=head_dim,
            dropout=dropout,
        )
        self.pre_ffn_norm = AdaRMSNorm(hidden_size)
        self.ffn = SwiGLUFeedForward(hidden_size, intermediate_size, dropout=dropout)

    def forward(
        self,
        action_hidden: torch.Tensor,
        vlm_cached_k: torch.Tensor,
        vlm_cached_v: torch.Tensor,
        adarms_cond: torch.Tensor,
        vlm_attention_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        normed, gate1 = self.pre_attn_norm(action_hidden, adarms_cond)
        attn_out = self.attn(normed, vlm_cached_k, vlm_cached_v, vlm_attention_mask)
        action_hidden = action_hidden + attn_out * gate1

        normed2, gate2 = self.pre_ffn_norm(action_hidden, adarms_cond)
        action_hidden = action_hidden + self.ffn(normed2) * gate2
        return action_hidden


class MoTDiT(nn.Module):
    """Stack of ActionBlocks; each block uses one VLM layer's KV pair."""

    def __init__(
        self,
        hidden_size: int,
        num_attention_heads: int,
        num_kv_heads: int,
        head_dim: int,
        intermediate_size: int,
        num_layers: int,
        dropout: float = 0.2,
    ):
        super().__init__()
        self.num_layers = num_layers
        self.blocks = nn.ModuleList([
            MoTBlock(
                hidden_size=hidden_size,
                num_attention_heads=num_attention_heads,
                num_kv_heads=num_kv_heads,
                head_dim=head_dim,
                intermediate_size=intermediate_size,
                dropout=dropout,
            )
            for _ in range(num_layers)
        ])
        self.final_norm = AdaRMSNorm(hidden_size)

    def forward(
        self,
        action_hidden: torch.Tensor,
        vlm_kv_cache: list,
        adarms_cond: torch.Tensor,
        vlm_attention_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        for idx, block in enumerate(self.blocks):
            cached_k, cached_v = vlm_kv_cache[idx]
            action_hidden = block(
                action_hidden, cached_k, cached_v, adarms_cond, vlm_attention_mask,
            )
        action_hidden, _ = self.final_norm(action_hidden, adarms_cond)
        return action_hidden


def _kv_pairs_from_past_key_values(past_key_values: Cache) -> list[tuple[torch.Tensor, torch.Tensor]]:
    """Per-layer (K, V) from a HuggingFace decoder KV cache (order matches transformer layers)."""
    return [
        (past_key_values[i][0], past_key_values[i][1])
        for i in range(len(past_key_values))
    ]


class MoTFlowMatchingHead(nn.Module):
    """Flow matching head: MoT-style action expert over VLM KV cache (concat + GQA)."""

    def __init__(
        self,
        action_dim: int,
        action_chunk_size: int,
        vlm_config,
        num_inference_timesteps: int = 10,
        config: Optional[dict] = None,
    ):
        super().__init__()

        _vlm_num_q_heads  = 8 # vlm_config.num_attention_heads // 2 # optional: 8
        _vlm_num_kv_heads = vlm_config.num_key_value_heads   # 8
        _vlm_head_dim     = getattr(
            vlm_config, "head_dim", vlm_config.hidden_size // vlm_config.num_attention_heads
        )  # 128

        cfg = {
            "hidden_size": 1024, # vlm_config.hidden_size // 2,
            # "hidden_size": vlm_config.hidden_size // 2,
            "intermediate_size": vlm_config.intermediate_size // 4,
            "expert_num_layers": vlm_config.num_hidden_layers,
            # Attention dims default to VLM values (required for KV cache compat)
            "num_attention_heads": _vlm_num_q_heads,
            "num_kv_heads": _vlm_num_kv_heads,
            "head_dim": _vlm_head_dim,
            # Noise schedule
            "dropout": 0.2,
            "add_pos_embed": True,
            "noise_beta_alpha": 1.5,
            "noise_beta_beta": 1.0,
            "noise_s": 0.999,
            "num_timestep_buckets": 1000,
        }
        if config is not None:
            config = cfg.copy()

        num_attention_heads = cfg["num_attention_heads"]
        num_kv_heads        = cfg["num_kv_heads"]
        head_dim            = cfg["head_dim"]
        hidden_size         = cfg["hidden_size"]
        intermediate_size   = cfg["intermediate_size"]
        num_layers          = cfg["expert_num_layers"]

        self.action_dim = action_dim
        self.action_chunk_size = action_chunk_size
        self.num_inference_timesteps = num_inference_timesteps
        self.num_timestep_buckets = cfg["num_timestep_buckets"]
        self.noise_s = cfg["noise_s"]
        self.add_pos_embed = cfg["add_pos_embed"]

        self.action_in_proj = nn.Linear(action_dim * 2, hidden_size)
        self.action_out_proj = nn.Linear(hidden_size, action_dim)

        self.time_sinusoidal = SinusoidalPositionalEncoding(hidden_size)
        self.time_mlp_1 = nn.Linear(hidden_size, hidden_size)
        self.time_mlp_2 = nn.Linear(hidden_size, hidden_size)

        if self.add_pos_embed:
            max_seq = max(action_chunk_size, 256)
            self.position_embedding = nn.Embedding(max_seq, hidden_size)
            nn.init.normal_(self.position_embedding.weight, mean=0.0, std=0.02)

        self.dit = MoTDiT(
            hidden_size=hidden_size,
            num_attention_heads=num_attention_heads,
            num_kv_heads=num_kv_heads,
            head_dim=head_dim,
            intermediate_size=intermediate_size,
            num_layers=num_layers,
            dropout=cfg["dropout"],
        )

        self._beta_alpha = cfg["noise_beta_alpha"]
        self._beta_beta = cfg["noise_beta_beta"]

    @property
    def num_dit_layers(self) -> int:
        """Number of expert blocks; must match ``len(past_key_values.key_cache)``."""
        return self.dit.num_layers

    def _vlm_kv_list_from_past(self, past_key_values: Cache) -> list[tuple[torch.Tensor, torch.Tensor]]:
        n = len(past_key_values)
        if n != self.num_dit_layers:
            raise ValueError(
                f"MoT expert has {self.num_dit_layers} blocks but `past_key_values` has {n} "
                "layers. Set `dit_action_head_config['expert_num_layers']` to match "
                "`text_config.num_hidden_layers`."
            )
        return _kv_pairs_from_past_key_values(past_key_values)

    def reset_parameters(self):
        """Re-apply proper initialization after from_pretrained."""
        if self.add_pos_embed:
            nn.init.normal_(self.position_embedding.weight, mean=0.0, std=0.02)
        for module in self.modules():
            if isinstance(module, AdaRMSNorm):
                nn.init.zeros_(module.modulation.weight)
                nn.init.zeros_(module.modulation.bias)
            elif isinstance(module, nn.Linear):
                nn.init.kaiming_uniform_(module.weight, a=math.sqrt(5))
                if module.bias is not None:
                    fan_in, _ = nn.init._calculate_fan_in_and_fan_out(module.weight)
                    bound = 1 / math.sqrt(fan_in) if fan_in > 0 else 0
                    nn.init.uniform_(module.bias, -bound, bound)

    def _compute_adarms_cond(self, t_discretized: torch.Tensor) -> torch.Tensor:
        t_emb = self.time_sinusoidal(t_discretized.float())
        t_emb = t_emb.to(dtype=self.time_mlp_1.weight.dtype)
        t_emb = F.silu(self.time_mlp_1(t_emb))
        t_emb = F.silu(self.time_mlp_2(t_emb))
        return t_emb

    def sample_time(self, batch_size: int, device, dtype) -> torch.Tensor:
        beta_dist = Beta(self._beta_alpha, self._beta_beta)
        sample = beta_dist.sample([batch_size]).to(device, dtype=dtype).clamp(max=self.noise_s)
        return (self.noise_s - sample) / self.noise_s

    def _prepare_action_embeds(
        self,
        noisy_actions: torch.Tensor,
        action_dof_mask: Optional[torch.Tensor],
    ) -> torch.Tensor:
        if action_dof_mask is not None:
            x = torch.cat(
                [noisy_actions, action_dof_mask.to(noisy_actions.dtype)], dim=-1,
            )
        else:
            x = torch.cat([noisy_actions, torch.ones_like(noisy_actions)], dim=-1)

        tokens = self.action_in_proj(x)

        if self.add_pos_embed:
            pos_ids = torch.arange(tokens.shape[1], dtype=torch.long, device=noisy_actions.device)
            tokens = tokens + self.position_embedding(pos_ids).unsqueeze(0)

        return tokens

    def forward(
        self,
        past_key_values: Cache,
        actions: torch.Tensor,
        action_dof_mask: Optional[torch.Tensor] = None,
        encoder_attention_mask: Optional[torch.Tensor] = None,
    ) -> tuple:
        """Training: returns (pred_velocity, target_velocity).

        Args:
            past_key_values: VLM decoder KV cache; layer count must equal ``num_dit_layers``.
        """
        vlm_kv_cache = self._vlm_kv_list_from_past(past_key_values)
        device = actions.device
        B = actions.shape[0]

        noise = torch.randn(actions.shape, device=device, dtype=actions.dtype)
        t = self.sample_time(B, device=device, dtype=actions.dtype)
        t_expanded = t[:, None, None]

        noisy_trajectory = (1 - t_expanded) * noise + t_expanded * actions
        velocity = actions - noise

        t_discretized = (t * self.num_timestep_buckets).long()
        adarms_cond = self._compute_adarms_cond(t_discretized)

        action_tokens = self._prepare_action_embeds(noisy_trajectory, action_dof_mask)

        output = self.dit(
            action_tokens, vlm_kv_cache, adarms_cond, encoder_attention_mask,
        )

        pred = self.action_out_proj(output)
        pred_v = pred[:, :actions.shape[1]]
        return pred_v, velocity
    
    def compute_velocity(
        self,
        past_key_values: Cache,
        actions: torch.Tensor,
        noise: torch.Tensor,
        t: torch.Tensor,
        action_dof_mask: Optional[torch.Tensor] = None,
        encoder_attention_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        """Compute velocity prediction for pre-sampled noise and timestep.

        Used by DiffusionNFT where noise and timestep must be shared between
        the current policy (v_θ) and the reference policy (v_old).

        Args:
            past_key_values: VLM decoder KV cache
            actions: (B, T, action_dim) ground truth actions (x_0)
            noise: (B, T, action_dim) pre-sampled noise (ε)
            t: (B,) continuous timesteps in [0, 1)
            action_dof_mask, encoder_attention_mask,

        Returns:
            pred_v: (B, T, action_dim) predicted velocity
        """
        vlm_kv_cache = self._vlm_kv_list_from_past(past_key_values)
        device = actions.device
        t_expanded = t[:, None, None]

        noisy_trajectory = (1 - t_expanded) * noise + t_expanded * actions
        t_discretized = (t * self.num_timestep_buckets).long()
        adarms_cond = self._compute_adarms_cond(t_discretized)
        action_tokens = self._prepare_action_embeds(noisy_trajectory, action_dof_mask)
        output = self.dit(
            action_tokens, vlm_kv_cache, adarms_cond, encoder_attention_mask,
        )
        pred = self.action_out_proj(output)
        return pred[:, :actions.shape[1]]


    @torch.no_grad()
    def predict_action(
        self,
        past_key_values: Cache,
        action_dof_mask: Optional[torch.Tensor] = None,
        encoder_attention_mask: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        """Inference: Euler integration, returns (B, chunk_size, action_dim)."""
        k0 = past_key_values[0][0]
        B = k0.shape[0]
        device = k0.device
        dtype = k0.dtype
        vlm_kv_cache = self._vlm_kv_list_from_past(past_key_values)

        actions = torch.randn(
            (B, self.action_chunk_size, self.action_dim),
            device=device, dtype=dtype,
        )
        dt = 1.0 / self.num_inference_timesteps

        for step in range(self.num_inference_timesteps):
            t_cont = step / float(self.num_inference_timesteps)
            t_disc_val = int(t_cont * self.num_timestep_buckets)
            t_tensor = torch.full((B,), t_disc_val, device=device, dtype=torch.long)

            adarms_cond = self._compute_adarms_cond(t_tensor)
            action_tokens = self._prepare_action_embeds(actions, action_dof_mask)

            output = self.dit(
                action_tokens, vlm_kv_cache, adarms_cond, encoder_attention_mask,
            )
            pred_velocity = self.action_out_proj(output)[:, :self.action_chunk_size]
            actions = actions + dt * pred_velocity

        return actions