File size: 30,907 Bytes
7d124ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# UnisonBackbone — MM-DiT backbone for UNISON.
#
# Task identity and source audio are encoded entirely through the input tensor:
#   model input shape: [B, 2*C+1, T]
#     [:, :C, :]   — noisy target latent
#     [:, C:2C, :] — source/reference latent (zeros for pure generation)
#     [:, 2C, :]   — task mask (0=generate, 1=edit, 2=zero-shot-TTS reference region)
#
# The UniversalPatchEmbed layer (concat_condition=True) handles the 2C+1 → hidden_size
# projection; C is set by in_channels in the model config and matches the VAE latent dim.
#
# Deep LLM fusion: each MM-DiT block receives the hidden state from the corresponding
# layer of a frozen MLLM (Qwen2.5-Omni or Qwen3) via a learned linear projection,
# providing depth-matched semantic conditioning.

from typing import Any, List, Tuple, Optional, Union, Dict

import torch
import torch.nn as nn
from einops import rearrange
from loguru import logger

from diffusers.models import ModelMixin
from diffusers.configuration_utils import ConfigMixin, register_to_config

from .modules.activation_layers import get_activation_layer
from .modules.norm_layers import get_norm_layer
from .modules.embed_layers import (
    TimestepEmbedder,
    PatchEmbed,
    VisionProjection,
    UniversalPatchEmbed,
    DurationEmbedder
)
from .modules.attention import parallel_attention
from .modules.posemb_layers import apply_rotary_emb, get_nd_rotary_pos_embed, get_audio_rotary_pos_embed
from .modules.mlp_layers import MLP, MLPEmbedder, FinalLayer, LinearWarpforSingle
from .modules.modulate_layers import ModulateDiT, modulate, apply_gate
from .modules.token_refiner import SingleTokenRefiner

from unison.utils.communications import all_gather
from unison.utils.infer_utils import torch_compile_wrapper

from unison.commons.parallel_states import get_parallel_state

class MMDoubleStreamBlock(nn.Module):

    def __init__(
        self,
        hidden_size: int,
        heads_num: int,
        mlp_width_ratio: float,
        mlp_act_type: str = "gelu_tanh",
        attn_mode: str = None,
        qk_norm: bool = True,
        qk_norm_type: str = "rms",
        qkv_bias: bool = False,
        has_text_ffn: bool = True,
        dtype: Optional[torch.dtype] = None,
        device: Optional[torch.device] = None,
    ):
        factory_kwargs = {"device": device, "dtype": dtype}
        super().__init__()

        self.deterministic = False
        self.heads_num = heads_num
        self.attn_mode = attn_mode
        self.has_text_ffn = has_text_ffn

        head_dim = hidden_size // heads_num
        mlp_hidden_dim = int(hidden_size * mlp_width_ratio)

        self.img_mod = ModulateDiT(
            hidden_size, factor=6, act_layer=get_activation_layer("silu"), **factory_kwargs
        )
        self.img_norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6, **factory_kwargs)
        self.img_attn_q = nn.Linear(hidden_size, hidden_size, bias=qkv_bias, **factory_kwargs)
        self.img_attn_k = nn.Linear(hidden_size, hidden_size, bias=qkv_bias, **factory_kwargs)
        self.img_attn_v = nn.Linear(hidden_size, hidden_size, bias=qkv_bias, **factory_kwargs)

        qk_norm_layer = get_norm_layer(qk_norm_type)
        self.img_attn_q_norm = (
            qk_norm_layer(head_dim, elementwise_affine=True, eps=1e-6, **factory_kwargs) if qk_norm else nn.Identity()
        )
        self.img_attn_k_norm = (
            qk_norm_layer(head_dim, elementwise_affine=True, eps=1e-6, **factory_kwargs) if qk_norm else nn.Identity()
        )
        self.img_attn_proj = nn.Linear(hidden_size, hidden_size, bias=qkv_bias, **factory_kwargs)

        self.img_norm2 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6, **factory_kwargs)
        self.img_mlp = MLP(hidden_size, mlp_hidden_dim, act_layer=get_activation_layer(mlp_act_type), bias=True, **factory_kwargs)

        txt_mod_factor = 6 if has_text_ffn else 2
        self.txt_mod = ModulateDiT(
            hidden_size, factor=txt_mod_factor, act_layer=get_activation_layer("silu"), **factory_kwargs
        )
        self.txt_norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6, **factory_kwargs)

        self.txt_attn_q = nn.Linear(hidden_size, hidden_size, bias=qkv_bias, **factory_kwargs)
        self.txt_attn_k = nn.Linear(hidden_size, hidden_size, bias=qkv_bias, **factory_kwargs)
        self.txt_attn_v = nn.Linear(hidden_size, hidden_size, bias=qkv_bias, **factory_kwargs)

        self.txt_attn_q_norm = (
            qk_norm_layer(head_dim, elementwise_affine=True, eps=1e-6, **factory_kwargs) if qk_norm else nn.Identity()
        )
        self.txt_attn_k_norm = (
            qk_norm_layer(head_dim, elementwise_affine=True, eps=1e-6, **factory_kwargs) if qk_norm else nn.Identity()
        )

        if has_text_ffn:
            self.txt_attn_proj = nn.Linear(hidden_size, hidden_size, bias=qkv_bias, **factory_kwargs)
            self.txt_norm2 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6, **factory_kwargs)
            self.txt_mlp = MLP(hidden_size, mlp_hidden_dim, act_layer=get_activation_layer(mlp_act_type), bias=True, **factory_kwargs)

        self.hybrid_seq_parallel_attn = None

    def enable_deterministic(self):
        self.deterministic = True

    def disable_deterministic(self):
        self.deterministic = False

    @torch_compile_wrapper()
    def forward(
        self,
        img: torch.Tensor,
        txt: torch.Tensor,
        vec: torch.Tensor,
        freqs_cis: tuple = None,
        text_mask=None,
        attn_param=None,
        is_flash=False,
        block_idx=None,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        (
            img_mod1_shift,
            img_mod1_scale,
            img_mod1_gate,
            img_mod2_shift,
            img_mod2_scale,
            img_mod2_gate,
        ) = self.img_mod(vec).chunk(6, dim=-1)

        if self.has_text_ffn:
            (
                txt_mod1_shift,
                txt_mod1_scale,
                txt_mod1_gate,
                txt_mod2_shift,
                txt_mod2_scale,
                txt_mod2_gate,
            ) = self.txt_mod(vec).chunk(6, dim=-1)
        else:
            txt_mod1_shift, txt_mod1_scale = self.txt_mod(vec).chunk(2, dim=-1)

        img_modulated = self.img_norm1(img)
        img_modulated = modulate(img_modulated, shift=img_mod1_shift, scale=img_mod1_scale)

        img_q = self.img_attn_q(img_modulated)
        img_k = self.img_attn_k(img_modulated)
        img_v = self.img_attn_v(img_modulated)
        img_q = rearrange(img_q, "B L (H D) -> B L H D", H=self.heads_num)
        img_k = rearrange(img_k, "B L (H D) -> B L H D", H=self.heads_num)
        img_v = rearrange(img_v, "B L (H D) -> B L H D", H=self.heads_num)
        img_q = self.img_attn_q_norm(img_q).to(img_v)
        img_k = self.img_attn_k_norm(img_k).to(img_v)

        if freqs_cis is not None:
            img_qq, img_kk = apply_rotary_emb(img_q, img_k, freqs_cis, head_first=False)
            assert (
                img_qq.shape == img_q.shape and img_kk.shape == img_k.shape
            ), f"img_kk: {img_qq.shape}, img_q: {img_q.shape}, img_kk: {img_kk.shape}, img_k: {img_k.shape}"
            img_q, img_k = img_qq, img_kk

        txt_modulated = self.txt_norm1(txt)
        txt_modulated = modulate(txt_modulated, shift=txt_mod1_shift, scale=txt_mod1_scale)
        txt_q = self.txt_attn_q(txt_modulated)
        txt_k = self.txt_attn_k(txt_modulated)
        txt_v = self.txt_attn_v(txt_modulated)
        txt_q = rearrange(txt_q, "B L (H D) -> B L H D", H=self.heads_num)
        txt_k = rearrange(txt_k, "B L (H D) -> B L H D", H=self.heads_num)
        txt_v = rearrange(txt_v, "B L (H D) -> B L H D", H=self.heads_num)
        txt_q = self.txt_attn_q_norm(txt_q).to(txt_v)
        txt_k = self.txt_attn_k_norm(txt_k).to(txt_v)

        attn_mode = 'flash' if is_flash else self.attn_mode
        attn = parallel_attention(
            (img_q, txt_q),
            (img_k, txt_k),
            (img_v, txt_v),
            img_q_len=img_q.shape[1],
            img_kv_len=img_k.shape[1],
            text_mask=text_mask,
            attn_mode=attn_mode,
            attn_param=attn_param,
            block_idx=block_idx,
        )

        img_attn, txt_attn = attn[:, :img_q.shape[1]].contiguous(), attn[:, img_q.shape[1]:].contiguous()

        img = img + apply_gate(self.img_attn_proj(img_attn), gate=img_mod1_gate)
        img = img + apply_gate(
            self.img_mlp(
                modulate(self.img_norm2(img), shift=img_mod2_shift, scale=img_mod2_scale)
            ),
            gate=img_mod2_gate,
        )

        if self.has_text_ffn:
            txt = txt + apply_gate(self.txt_attn_proj(txt_attn), gate=txt_mod1_gate)
            txt = txt + apply_gate(
                self.txt_mlp(modulate(self.txt_norm2(txt), shift=txt_mod2_shift, scale=txt_mod2_scale)),
                gate=txt_mod2_gate,
            )

        return img, txt


class MMSingleStreamBlock(nn.Module):

    def __init__(
        self,
        hidden_size: int,
        heads_num: int,
        mlp_width_ratio: float = 4.0,
        mlp_act_type: str = "gelu_tanh",
        attn_mode: str = None,
        qk_norm: bool = True,
        qk_norm_type: str = "rms",
        qk_scale: float = None,
        dtype: Optional[torch.dtype] = None,
        device: Optional[torch.device] = None,
    ):
        factory_kwargs = {"device": device, "dtype": dtype}
        super().__init__()

        self.deterministic = False
        self.attn_mode = attn_mode

        self.hidden_size = hidden_size
        self.heads_num = heads_num
        head_dim = hidden_size // heads_num
        mlp_hidden_dim = int(hidden_size * mlp_width_ratio)
        self.mlp_hidden_dim = mlp_hidden_dim
        self.scale = qk_scale or head_dim ** -0.5

        self.linear1_q = nn.Linear(hidden_size, hidden_size, **factory_kwargs)
        self.linear1_k = nn.Linear(hidden_size, hidden_size, **factory_kwargs)
        self.linear1_v = nn.Linear(hidden_size, hidden_size, **factory_kwargs)
        self.linear1_mlp = nn.Linear(hidden_size, mlp_hidden_dim, **factory_kwargs)
        self.linear2 = LinearWarpforSingle(hidden_size + mlp_hidden_dim, hidden_size, bias=True, **factory_kwargs)
        self.mlp_act = get_activation_layer(mlp_act_type)()

        qk_norm_layer = get_norm_layer(qk_norm_type)
        self.q_norm = (
            qk_norm_layer(head_dim, elementwise_affine=True, eps=1e-6, **factory_kwargs) if qk_norm else nn.Identity()
        )
        self.k_norm = (
            qk_norm_layer(head_dim, elementwise_affine=True, eps=1e-6, **factory_kwargs) if qk_norm else nn.Identity()
        )

        self.pre_norm = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6, **factory_kwargs)
        self.modulation = ModulateDiT(hidden_size, factor=3, act_layer=get_activation_layer("silu"), **factory_kwargs)
        self.hybrid_seq_parallel_attn = None

    def enable_deterministic(self):
        self.deterministic = True

    def disable_deterministic(self):
        self.deterministic = False

    def forward(
        self,
        x: torch.Tensor,
        vec: torch.Tensor,
        txt_len: int,
        freqs_cis: Tuple[torch.Tensor, torch.Tensor] = None,
        text_mask=None,
        attn_param=None,
        is_flash=False,
    ) -> torch.Tensor:
        mod_shift, mod_scale, mod_gate = self.modulation(vec).chunk(3, dim=-1)
        x_mod = modulate(self.pre_norm(x), shift=mod_shift, scale=mod_scale)

        q = self.linear1_q(x_mod)
        k = self.linear1_k(x_mod)
        v = self.linear1_v(x_mod)

        q = rearrange(q, "B L (H D) -> B L H D", H=self.heads_num)
        k = rearrange(k, "B L (H D) -> B L H D", H=self.heads_num)
        v = rearrange(v, "B L (H D) -> B L H D", H=self.heads_num)

        mlp = self.linear1_mlp(x_mod)

        q = self.q_norm(q).to(v)
        k = self.k_norm(k).to(v)

        img_q, txt_q = q[:, :-txt_len, :, :], q[:, -txt_len:, :, :]
        img_k, txt_k = k[:, :-txt_len, :, :], k[:, -txt_len:, :, :]
        img_v, txt_v = v[:, :-txt_len, :, :], v[:, -txt_len:, :, :]
        img_qq, img_kk = apply_rotary_emb(img_q, img_k, freqs_cis, head_first=False)
        assert (
            img_qq.shape == img_q.shape and img_kk.shape == img_k.shape
        ), f"img_kk: {img_qq.shape}, img_q: {img_q.shape}, img_kk: {img_kk.shape}, img_k: {img_k.shape}"
        img_q, img_k = img_qq, img_kk

        if is_flash:
            attn_mode = 'flash'
        else:
            attn_mode = self.attn_mode
        attn = parallel_attention(
            (img_q, txt_q),
            (img_k, txt_k),
            (img_v, txt_v),
            img_q_len=img_q.shape[1],
            img_kv_len=img_k.shape[1],
            text_mask=text_mask,
            attn_mode=attn_mode,
            attn_param=attn_param,
        )
        output = self.linear2(attn, self.mlp_act(mlp))

        return x + apply_gate(output, gate=mod_gate)


class UnisonBackbone(ModelMixin, ConfigMixin):
    """Channel-cat backbone: no ref_latents, no ref-isolated AdaLN, continuous RoPE."""

    @register_to_config
    def __init__(
        self,
        in_channels: int = 48,
        out_channels: int = None,
        patch_size: list = [1, 2, 2],
        hidden_size: int = 1024,
        heads_num: int = 8,
        concat_condition: bool = False,
        mlp_act_type: str = "gelu_tanh",
        mlp_width_ratio: float = 4.0,
        mm_double_blocks_depth: int = 18,
        mm_single_blocks_depth: int = 0,
        qkv_bias: bool = True,
        qk_norm: bool = True,
        qk_norm_type: str = "rms",
        attn_mode: str = "flash",
        attn_param: dict = None,
        rope_dim_list: list = [16, 56, 56],
        rope_theta: int = 10000,
        omni_dim: int = 1280,
        is_reshape_temporal_channels: bool = False,
        use_duration_embedding: bool = False,
        is_audio_type: bool = False,
        use_omni_embedding: bool = False,
        use_omni_last_embedding: bool = False,
        projector_type: str = "linear",
        guidance_embed: bool = False,
        temporal_rope_scaling_factor: float = 1.0,
        repa_z_dim: int = None,
        repa_layer_num: int = None,
        audio_patch_type: str = "conv_mlp",
    ):
        super().__init__()

        self.hidden_size = hidden_size
        self.heads_num = heads_num
        self.is_audio_type = is_audio_type
        self.concat_condition = concat_condition
        self.out_channels = out_channels or in_channels
        self.patch_size = patch_size
        self.rope_dim_list = rope_dim_list
        self.rope_theta = rope_theta
        self.temporal_rope_scaling_factor = temporal_rope_scaling_factor
        self.mm_double_blocks_depth = mm_double_blocks_depth
        self.mm_single_blocks_depth = mm_single_blocks_depth
        self.total_blocks_depth = mm_double_blocks_depth + mm_single_blocks_depth
        self.guidance_embed = guidance_embed
        self.use_duration_embedding = use_duration_embedding
        self.use_omni_embedding = use_omni_embedding
        self.projector_type = projector_type
        self.repa_z_dim = repa_z_dim
        self.repa_layer_num = repa_layer_num
        self.use_omni_last_embedding = use_omni_last_embedding
        self.gradient_checkpointing = False

        self.img_in = UniversalPatchEmbed(
            patch_size=patch_size,
            in_chans=in_channels,
            embed_dim=hidden_size,
            is_audio=is_audio_type,
            is_reshape_temporal_channels=is_reshape_temporal_channels,
            audio_kernel_size=7,
            audio_padding=3,
            concat_condition=concat_condition,
            audio_patch_type=audio_patch_type,
        )

        self.time_in = TimestepEmbedder(hidden_size, get_activation_layer("silu"))
        self.guidance_in = TimestepEmbedder(hidden_size, get_activation_layer("silu")) if guidance_embed else None

        if use_duration_embedding:
            self.duration_embedder = DurationEmbedder(hidden_size, min_value=0, max_value=30)
        else:
            self.duration_embedder = None

        if use_omni_embedding:
            if self.projector_type == "linear":
                self.deep_fusion_projs = nn.ModuleList([
                    nn.Linear(omni_dim, hidden_size) for _ in range(self.total_blocks_depth)
                ])
            elif self.projector_type == "mlp":
                self.deep_fusion_projs = nn.ModuleList([
                    nn.Sequential(
                        nn.Linear(omni_dim, hidden_size),
                        nn.SiLU(),
                        nn.Linear(hidden_size, hidden_size)
                    ) for _ in range(self.total_blocks_depth)
                ])
            else:
                raise ValueError(f"Invalid projector type: {self.projector_type}")
        else:
            self.deep_fusion_projs = None

        if self.use_omni_last_embedding:
            if self.projector_type == "linear":
                self.omni_last_proj = nn.Linear(omni_dim, hidden_size)
            elif self.projector_type == "mlp":
                self.omni_last_proj = nn.Sequential(
                    nn.Linear(omni_dim, hidden_size),
                    nn.SiLU(),
                    nn.Linear(hidden_size, hidden_size)
                )
            else:
                raise ValueError(f"Invalid projector type: {self.projector_type}")
        else:
            self.omni_last_proj = None

        need_text_ffn = self.use_omni_last_embedding or (self.duration_embedder is not None)

        self.double_blocks = nn.ModuleList([
            MMDoubleStreamBlock(
                hidden_size=hidden_size,
                heads_num=heads_num,
                mlp_width_ratio=mlp_width_ratio,
                mlp_act_type=mlp_act_type,
                attn_mode="flash",
                qk_norm=qk_norm,
                qk_norm_type=qk_norm_type,
                qkv_bias=qkv_bias,
                has_text_ffn=need_text_ffn,
            ) for _ in range(mm_double_blocks_depth)
        ])
        self.single_blocks = nn.ModuleList([
            MMSingleStreamBlock(
                hidden_size=hidden_size,
                heads_num=heads_num,
                mlp_width_ratio=mlp_width_ratio,
                mlp_act_type=mlp_act_type,
                attn_mode="flash",
                qk_norm=qk_norm,
                qk_norm_type=qk_norm_type,
            ) for _ in range(mm_single_blocks_depth)
        ])

        if self.is_audio_type:
            final_patch_size = (1, 1, 1)
        else:
            final_patch_size = patch_size

        self.final_layer = FinalLayer(hidden_size, final_patch_size, self.out_channels, get_activation_layer("silu"))

        if self.repa_z_dim is not None and self.repa_layer_num is not None:
            self.repa_proj = nn.Linear(hidden_size, repa_z_dim)
        else:
            self.repa_proj = None

    def enable_gradient_checkpointing(self):
        self.gradient_checkpointing = True

    def disable_gradient_checkpointing(self):
        self.gradient_checkpointing = False

    def get_rotary_pos_embed(self, grid_sizes):
        if self.is_audio_type:
            return get_audio_rotary_pos_embed(
                rope_dim_list=self.rope_dim_list,
                length=grid_sizes[0],
                theta=self.rope_theta,
                freqs_scaling=self.temporal_rope_scaling_factor,
                use_real=True
            )
        else:
            return get_nd_rotary_pos_embed(
                self.rope_dim_list,
                grid_sizes,
                theta=self.rope_theta,
                use_real=True,
                theta_rescale_factor=1
            )

    def count_parameters(self, verbose=True):
        total_params = 0
        trainable_params = 0

        module_params = {
            "Patch Embed (img_in)": 0,
            "Timestep Embed (time_in)": 0,
            "Guidance Embed": 0,
            "Duration Embed": 0,
            "Omni Last Projection": 0,
            "Deep Fusion Projections": 0,
            "Double Blocks": 0,
            "Single Blocks": 0,
            "Final Layer": 0,
            "RepA Projection": 0,
            "Others": 0,
        }

        for name, param in self.named_parameters():
            num_params = param.numel()
            total_params += num_params
            if param.requires_grad:
                trainable_params += num_params

            if "img_in" in name:
                module_params["Patch Embed (img_in)"] += num_params
            elif "time_in" in name:
                module_params["Timestep Embed (time_in)"] += num_params
            elif "guidance_in" in name:
                module_params["Guidance Embed"] += num_params
            elif "duration_embedder" in name:
                module_params["Duration Embed"] += num_params
            elif "omni_last_proj" in name:
                module_params["Omni Last Projection"] += num_params
            elif "deep_fusion_projs" in name:
                module_params["Deep Fusion Projections"] += num_params
            elif "double_blocks" in name:
                module_params["Double Blocks"] += num_params
            elif "single_blocks" in name:
                module_params["Single Blocks"] += num_params
            elif "final_layer" in name:
                module_params["Final Layer"] += num_params
            elif "repa_proj" in name:
                module_params["RepA Projection"] += num_params
            else:
                module_params["Others"] += num_params

        if verbose:
            print(f"\n{'='*50}")
            print(f"  Model Parameter Statistics")
            print(f"{'='*50}")
            for category, count in module_params.items():
                if count > 0:
                    print(f"  {category:<30}: {count / 1e6:>8.2f} M")
            print(f"  {'-'*48}")
            print(f"  {'Total Parameters':<30}: {total_params / 1e6:>8.2f} M ({total_params / 1e9:.3f} B)")
            print(f"  {'Trainable Parameters':<30}: {trainable_params / 1e6:>8.2f} M ({trainable_params / 1e9:.3f} B)")
            print(f"{'='*50}\n")

        return total_params

    def prepare_packed_indices(self, mask_list):
        total_mask = torch.cat(mask_list, dim=1).bool()
        sorted_mask, sort_indices = torch.sort(
            total_mask.int(),
            dim=1,
            descending=True,
            stable=True
        )
        gather_indices = sort_indices.unsqueeze(-1).expand(-1, -1, self.hidden_size)
        return sorted_mask.bool(), gather_indices

    def apply_packing(self, embedding_list, gather_indices, sorted_mask):
        raw_concat = torch.cat(embedding_list, dim=1)
        packed_embedding = torch.gather(raw_concat, dim=1, index=gather_indices)
        mask_broadcaster = sorted_mask.unsqueeze(-1).to(dtype=packed_embedding.dtype)
        packed_embedding = packed_embedding * mask_broadcaster
        return packed_embedding

    def forward(
        self,
        x: torch.Tensor,
        t: torch.Tensor,
        duration: torch.Tensor = None,
        omni_emb_list: List[torch.Tensor] = None,
        omni_last_emb: torch.Tensor = None,
        omni_mask: torch.Tensor = None,
        guidance: torch.Tensor = None,
    ):
        assert self.use_omni_embedding or self.use_omni_last_embedding, \
            "At least one of use_omni_embedding / use_omni_last_embedding must be True"
        assert omni_mask is not None, "omni_mask is required"
        if self.use_omni_embedding:
            assert omni_emb_list is not None and len(omni_emb_list) >= self.total_blocks_depth, \
                f"omni_emb_list required. Expected len >= {self.total_blocks_depth}, got {len(omni_emb_list) if omni_emb_list else 0}"
        if self.use_omni_last_embedding:
            if omni_last_emb is None:
                assert omni_emb_list is not None and len(omni_emb_list) > 0
                omni_last_emb = omni_emb_list[-1]
        if omni_mask.dim() == 3:
            omni_mask = omni_mask.squeeze(1)

        # --- 1. Patching & Embeddings ---
        img = self.img_in(x)
        vec = self.time_in(t)
        repa_feat = None
        if self.guidance_embed and guidance is not None:
            vec = vec + self.guidance_in(guidance)

        # --- 2. RoPE (always continuous, no offset) ---
        grid_sizes_target = None
        if self.is_audio_type:
            freqs_cos, freqs_sin = get_audio_rotary_pos_embed(
                rope_dim_list=self.rope_dim_list,
                length=img.shape[1],
                theta=self.rope_theta,
                freqs_scaling=self.temporal_rope_scaling_factor,
                use_real=True,
            )
            freqs_cis = (freqs_cos.to(img.device), freqs_sin.to(img.device))
        else:
            _, _, ot, oh, ow = x.shape
            pt, ph, pw = self.patch_size
            grid_sizes_target = (ot // pt, oh // ph, ow // pw)
            freqs_cos, freqs_sin = self.get_rotary_pos_embed(grid_sizes_target)
            freqs_cis = (freqs_cos.to(img.device), freqs_sin.to(img.device))

        # --- 3. Build base text tokens ---
        txt_base = None
        txt_mask = None

        if self.use_omni_last_embedding:
            txt_base = self.omni_last_proj(omni_last_emb)
            txt_mask = omni_mask

        if self.duration_embedder is not None and duration is not None:
            dur_emb = self.duration_embedder(duration)
            dur_mask = torch.ones(omni_mask.shape[0], 1, device=omni_mask.device, dtype=omni_mask.dtype)
            if txt_base is not None:
                txt_base = torch.cat([dur_emb, txt_base], dim=1)
                txt_mask = torch.cat([dur_mask, txt_mask], dim=1)
            else:
                txt_base = dur_emb
                txt_mask = dur_mask

        has_base = txt_base is not None
        txt_base_len = txt_base.shape[1] if has_base else 0

        if self.use_omni_embedding and has_base:
            txt_mask_sorted, gather_indices = self.prepare_packed_indices([txt_mask, omni_mask])
        elif self.use_omni_embedding and not has_base:
            txt_mask_sorted = omni_mask
            gather_indices = None
        else:
            txt_mask_sorted = txt_mask
            gather_indices = None

        # --- 4. Double Stream Loop ---
        for i, block in enumerate(self.double_blocks):
            if self.use_omni_embedding:
                omni_curr = self.deep_fusion_projs[i](omni_emb_list[i])
                if has_base:
                    txt_packed = self.apply_packing([txt_base, omni_curr], gather_indices, txt_mask_sorted)
                else:
                    txt_packed = omni_curr
            else:
                txt_packed = txt_base

            if self.training and self.gradient_checkpointing:
                def create_custom_forward(module):
                    def custom_forward(*inputs):
                        return module(*inputs, freqs_cis=freqs_cis, text_mask=txt_mask_sorted, is_flash=True)
                    return custom_forward

                img, txt_out_packed = torch.utils.checkpoint.checkpoint(
                    create_custom_forward(block),
                    img, txt_packed, vec,
                    use_reentrant=False
                )
            else:
                img, txt_out_packed = block(
                    img=img, txt=txt_packed, vec=vec,
                    freqs_cis=freqs_cis, text_mask=txt_mask_sorted,
                    is_flash=True
                )

            if self.repa_proj is not None and i == self.repa_layer_num:
                repa_feat = self.repa_proj(img)

            if self.use_omni_embedding and has_base:
                B, Total_Len, D = txt_packed.shape
                restored_txt = torch.zeros(B, Total_Len, D, device=img.device, dtype=img.dtype)
                restored_txt.scatter_(dim=1, index=gather_indices, src=txt_out_packed)
                txt_base = restored_txt[:, :txt_base_len, :]
            elif not self.use_omni_embedding:
                txt_base = txt_out_packed

        # --- 5. Single Stream Loop ---
        img_len = img.shape[1]
        block_idx_offset = self.mm_double_blocks_depth

        for i, block in enumerate(self.single_blocks):
            if self.use_omni_embedding:
                omni_curr = self.deep_fusion_projs[block_idx_offset + i](omni_emb_list[block_idx_offset + i])
                if has_base:
                    txt_packed = self.apply_packing([txt_base, omni_curr], gather_indices, txt_mask_sorted)
                else:
                    txt_packed = omni_curr
            else:
                txt_packed = txt_base

            x_in = torch.cat([img, txt_packed], dim=1)
            txt_len_packed = txt_packed.shape[1]

            if self.training and self.gradient_checkpointing:
                def create_custom_forward(module):
                    def custom_forward(*inputs):
                        return module(*inputs, txt_len=txt_len_packed, freqs_cis=freqs_cis, text_mask=txt_mask_sorted, is_flash=True)
                    return custom_forward

                x_out = torch.utils.checkpoint.checkpoint(
                    create_custom_forward(block),
                    x_in, vec,
                    use_reentrant=False
                )
            else:
                x_out = block(
                    x=x_in, vec=vec, txt_len=txt_len_packed,
                    freqs_cis=freqs_cis, text_mask=txt_mask_sorted,
                    is_flash=True
                )

            img = x_out[:, :img_len, :]

            if self.repa_proj is not None and i + block_idx_offset == self.repa_layer_num:
                repa_feat = self.repa_proj(img)

            txt_out_packed = x_out[:, img_len:, :]

            if self.use_omni_embedding and has_base:
                B, Total_Len, D = txt_packed.shape
                restored_txt = torch.zeros(B, Total_Len, D, device=img.device, dtype=img.dtype)
                restored_txt.scatter_(dim=1, index=gather_indices, src=txt_out_packed)
                txt_base = restored_txt[:, :txt_base_len, :]
            elif not self.use_omni_embedding:
                txt_base = txt_out_packed

        # --- 6. Final Layer ---
        img_out = self.final_layer(img, vec)
        out = self.unpatchify(img_out, shape_info=None if self.is_audio_type else grid_sizes_target)

        return out, 0, repa_feat

    def unpatchify(self, x, shape_info):
        if self.is_audio_type:
            return x.transpose(1, 2).contiguous()
        else:
            t, h, w = shape_info
            c = self.out_channels
            pt, ph, pw = self.patch_size

            x = x.reshape(x.shape[0], t, h, w, c, pt, ph, pw)
            x = torch.einsum("nthwcopq->nctohpwq", x)
            x = x.reshape(x.shape[0], c, t * pt, h * ph, w * pw)
            return x