File size: 36,960 Bytes
373af33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import torch
from torch import nn
import random
from typing import Optional, List, Dict

from mogen.models.utils.misc import zero_module
from ..builder import SUBMODULES, build_attention
from ..utils.stylization_block import StylizationBlock
from .motion_transformer import MotionTransformer
from mogen.models.utils.position_encoding import timestep_embedding
from scipy.ndimage import gaussian_filter


def get_tomato_slice(idx: int) -> List[int]:
    """Return specific slices for the tomato dataset."""
    if idx == 0:
        result = [0, 1, 2, 3, 463, 464, 465]
    else:
        result = [
            4 + (idx - 1) * 3,
            4 + (idx - 1) * 3 + 1,
            4 + (idx - 1) * 3 + 2,
            157 + (idx - 1) * 6,
            157 + (idx - 1) * 6 + 1,
            157 + (idx - 1) * 6 + 2,
            157 + (idx - 1) * 6 + 3,
            157 + (idx - 1) * 6 + 4,
            157 + (idx - 1) * 6 + 5,
            463 + idx * 3,
            463 + idx * 3 + 1,
            463 + idx * 3 + 2,
        ]
    return result


def get_part_slice(idx_list: List[int], func) -> List[int]:
    """Return a list of slices by applying the provided function."""
    result = []
    for idx in idx_list:
        result.extend(func(idx))
    return result


class SinglePoseEncoder(nn.Module):
    """Encoder module for individual pose, separating different body parts."""

    def __init__(self, latent_dim: int = 64):
        super().__init__()
        func = get_tomato_slice
        self.root_slice = get_part_slice([0], func)
        self.head_slice = get_part_slice([12, 15], func)
        self.stem_slice = get_part_slice([3, 6, 9], func)
        self.larm_slice = get_part_slice([14, 17, 19, 21], func)
        self.rarm_slice = get_part_slice([13, 16, 18, 20], func)
        self.lleg_slice = get_part_slice([2, 5, 8, 11], func)
        self.rleg_slice = get_part_slice([1, 4, 7, 10], func)
        self.lhnd_slice = get_part_slice(range(22, 37), func)
        self.rhnd_slice = get_part_slice(range(37, 52), func)
        self.face_slice = range(619, 669)

        # Initialize linear layers for each body part embedding
        self.root_embed = nn.Linear(len(self.root_slice), latent_dim)
        self.head_embed = nn.Linear(len(self.head_slice), latent_dim)
        self.stem_embed = nn.Linear(len(self.stem_slice), latent_dim)
        self.larm_embed = nn.Linear(len(self.larm_slice), latent_dim)
        self.rarm_embed = nn.Linear(len(self.rarm_slice), latent_dim)
        self.lleg_embed = nn.Linear(len(self.lleg_slice), latent_dim)
        self.rleg_embed = nn.Linear(len(self.rleg_slice), latent_dim)
        self.lhnd_embed = nn.Linear(len(self.lhnd_slice), latent_dim)
        self.rhnd_embed = nn.Linear(len(self.rhnd_slice), latent_dim)
        self.face_embed = nn.Linear(len(self.face_slice), latent_dim)

    def forward(self, motion: torch.Tensor) -> torch.Tensor:
        """Forward pass to embed different parts of the motion tensor."""
        root_feat = self.root_embed(motion[:, :, self.root_slice].contiguous())
        head_feat = self.head_embed(motion[:, :, self.head_slice].contiguous())
        stem_feat = self.stem_embed(motion[:, :, self.stem_slice].contiguous())
        larm_feat = self.larm_embed(motion[:, :, self.larm_slice].contiguous())
        rarm_feat = self.rarm_embed(motion[:, :, self.rarm_slice].contiguous())
        lleg_feat = self.lleg_embed(motion[:, :, self.lleg_slice].contiguous())
        rleg_feat = self.rleg_embed(motion[:, :, self.rleg_slice].contiguous())
        lhnd_feat = self.lhnd_embed(motion[:, :, self.lhnd_slice].contiguous())
        rhnd_feat = self.rhnd_embed(motion[:, :, self.rhnd_slice].contiguous())
        face_feat = self.face_embed(motion[:, :, self.face_slice].contiguous())

        # Concatenate all embeddings
        feat = torch.cat((root_feat, head_feat, stem_feat,
                          larm_feat, rarm_feat, lleg_feat, rleg_feat,
                          lhnd_feat, rhnd_feat, face_feat), dim=-1)
        return feat


class PoseEncoder(nn.Module):
    """Encoder for multi-dataset scenarios, handling different datasets."""

    def __init__(self, latent_dim: int, num_datasets: int):
        super().__init__()
        self.models = nn.ModuleList()
        self.num_datasets = num_datasets
        self.latent_dim = latent_dim

        # Initialize single pose encoders for each dataset
        for _ in range(num_datasets):
            self.models.append(SinglePoseEncoder(latent_dim=latent_dim))

    def forward(self, motion: torch.Tensor, dataset_idx: torch.Tensor) -> torch.Tensor:
        """Forward pass for multi-dataset encoding."""
        B, T = motion.shape[:2]
        output = torch.zeros(B, T, 10 * self.latent_dim).type_as(motion)
        num_finish = 0

        # Process each dataset's motion separately
        for i in range(self.num_datasets):
            batch_motion = motion[dataset_idx == i]
            if len(batch_motion) == 0:
                continue
            num_finish += len(batch_motion)
            batch_motion = self.models[i](batch_motion)
            output[dataset_idx == i] = batch_motion
        assert num_finish == B
        return output


class SinglePoseDecoder(nn.Module):
    """Decoder module for individual pose, reconstructing body parts."""

    def __init__(self, latent_dim: int = 64, output_dim: int = 669):
        super().__init__()
        self.latent_dim = latent_dim
        self.output_dim = output_dim
        func = get_tomato_slice
        self.root_slice = get_part_slice([0], func)
        self.head_slice = get_part_slice([12, 15], func)
        self.stem_slice = get_part_slice([3, 6, 9], func)
        self.larm_slice = get_part_slice([14, 17, 19, 21], func)
        self.rarm_slice = get_part_slice([13, 16, 18, 20], func)
        self.lleg_slice = get_part_slice([2, 5, 8, 11], func)
        self.rleg_slice = get_part_slice([1, 4, 7, 10], func)
        self.lhnd_slice = get_part_slice(range(22, 37), func)
        self.rhnd_slice = get_part_slice(range(37, 52), func)
        self.face_slice = range(619, 669)

        # Initialize linear layers for each body part output
        self.root_out = nn.Linear(latent_dim, len(self.root_slice))
        self.head_out = nn.Linear(latent_dim, len(self.head_slice))
        self.stem_out = nn.Linear(latent_dim, len(self.stem_slice))
        self.larm_out = nn.Linear(latent_dim, len(self.larm_slice))
        self.rarm_out = nn.Linear(latent_dim, len(self.rarm_slice))
        self.lleg_out = nn.Linear(latent_dim, len(self.lleg_slice))
        self.rleg_out = nn.Linear(latent_dim, len(self.rleg_slice))
        self.lhnd_out = nn.Linear(latent_dim, len(self.lhnd_slice))
        self.rhnd_out = nn.Linear(latent_dim, len(self.rhnd_slice))
        self.face_out = nn.Linear(latent_dim, len(self.face_slice))
        

    def forward(self, motion: torch.Tensor) -> torch.Tensor:
        """Forward pass to decode body parts from latent representation."""
        B, T = motion.shape[:2]
        D = self.latent_dim

        # Decode each part using corresponding linear layer
        root_feat = self.root_out(motion[:, :, :D].contiguous())
        head_feat = self.head_out(motion[:, :, D: 2 * D].contiguous())
        stem_feat = self.stem_out(motion[:, :, 2 * D: 3 * D].contiguous())
        larm_feat = self.larm_out(motion[:, :, 3 * D: 4 * D].contiguous())
        rarm_feat = self.rarm_out(motion[:, :, 4 * D: 5 * D].contiguous())
        lleg_feat = self.lleg_out(motion[:, :, 5 * D: 6 * D].contiguous())
        rleg_feat = self.rleg_out(motion[:, :, 6 * D: 7 * D].contiguous())
        lhnd_feat = self.lhnd_out(motion[:, :, 7 * D: 8 * D].contiguous())
        rhnd_feat = self.rhnd_out(motion[:, :, 8 * D: 9 * D].contiguous())
        face_feat = self.face_out(motion[:, :, 9 * D:].contiguous())

        # Combine outputs into final tensor
        output = torch.zeros(B, T, self.output_dim).type_as(motion)
        output[:, :, self.root_slice] = root_feat
        output[:, :, self.head_slice] = head_feat
        output[:, :, self.stem_slice] = stem_feat
        output[:, :, self.larm_slice] = larm_feat
        output[:, :, self.rarm_slice] = rarm_feat
        output[:, :, self.lleg_slice] = lleg_feat
        output[:, :, self.rleg_slice] = rleg_feat
        output[:, :, self.lhnd_slice] = lhnd_feat
        output[:, :, self.rhnd_slice] = rhnd_feat
        output[:, :, self.face_slice] = face_feat

        return output


class PoseDecoder(nn.Module):
    """Decoder for multi-dataset scenarios, handling different datasets."""

    def __init__(self, latent_dim: int, output_dim: int, num_datasets: int):
        super().__init__()
        self.models = nn.ModuleList()
        self.num_datasets = num_datasets
        self.latent_dim = latent_dim
        self.output_dim = output_dim

        # Initialize single pose decoders for each dataset
        for _ in range(num_datasets):
            self.models.append(
                SinglePoseDecoder(latent_dim=latent_dim, output_dim=output_dim)
            )

    def forward(self, motion: torch.Tensor, dataset_idx: torch.Tensor) -> torch.Tensor:
        """Forward pass for multi-dataset decoding."""
        B, T = motion.shape[:2]
        output = torch.zeros(B, T, self.output_dim).type_as(motion)
        num_finish = 0

        # Process each dataset's motion separately
        for i in range(self.num_datasets):
            batch_motion = motion[dataset_idx == i]
            if len(batch_motion) == 0:
                continue
            num_finish += len(batch_motion)
            batch_motion = self.models[i](batch_motion)
            output[dataset_idx == i] = batch_motion
        assert num_finish == B
        return output


class SFFN(nn.Module):
    """SFFN module with multiple linear layers, acting on different parts of the input."""

    def __init__(self,
                 latent_dim: int,
                 ffn_dim: int,
                 dropout: float,
                 time_embed_dim: int,
                 activation: str = "GELU"):
        super().__init__()
        self.linear1_list = nn.ModuleList()
        self.linear2_list = nn.ModuleList()

        if activation == "GELU":
            self.activation = nn.GELU()
        self.linear1 = nn.Linear(latent_dim * 10, ffn_dim * 10)
        self.linear2 = nn.Linear(ffn_dim * 10, latent_dim * 10)

        self.dropout = nn.Dropout(dropout)
        self.proj_out = StylizationBlock(latent_dim * 10, time_embed_dim, dropout)

    def forward(self, x: torch.Tensor, emb: torch.Tensor, **kwargs) -> torch.Tensor:
        """Forward pass for SFFN, applying stylization block."""
        B, T, D = x.shape
        y = self.linear2(self.dropout(self.activation(self.linear1(x))))
        y = x.reshape(B, T, D) + self.proj_out(y, emb)

        return y


class FFN(nn.Module):
    """Feed-forward network with GELU activation and dropout."""

    def __init__(self, latent_dim: int, ffn_dim: int, dropout: float):
        super().__init__()
        self.linear1 = nn.Linear(latent_dim, ffn_dim)
        self.linear2 = nn.Linear(ffn_dim, latent_dim)
        self.activation = nn.GELU()
        self.dropout = nn.Dropout(dropout)

    def forward(self, x: torch.Tensor, **kwargs) -> torch.Tensor:
        """Forward pass with normalization and residual connection."""
        y = self.linear2(self.dropout(self.activation(self.linear1(x))))
        y = x + y
        return y


class DecoderLayer(nn.Module):
    """Decoder layer consisting of conditional attention block and SFFN."""

    def __init__(self, ca_block_cfg: Optional[Dict] = None, ffn_cfg: Optional[Dict] = None):
        super().__init__()
        self.ca_block = build_attention(ca_block_cfg) if ca_block_cfg else None
        self.ffn = SFFN(**ffn_cfg) if ffn_cfg else None

    def forward(self, **kwargs) -> torch.Tensor:
        """Forward pass for the decoder layer."""
        if self.ca_block is not None:
            x = self.ca_block(**kwargs)
            kwargs.update({'x': x})
        if self.ffn is not None:
            x = self.ffn(**kwargs)
        return x


class EncoderLayer(nn.Module):
    """Encoder layer consisting of self-attention block and FFN."""

    def __init__(self, sa_block_cfg: Optional[Dict] = None, ffn_cfg: Optional[Dict] = None):
        super().__init__()
        self.sa_block = build_attention(sa_block_cfg) if sa_block_cfg else None
        self.ffn = FFN(**ffn_cfg) if ffn_cfg else None

    def forward(self, **kwargs) -> torch.Tensor:
        """Forward pass for the encoder layer."""
        if self.sa_block is not None:
            x = self.sa_block(**kwargs)
            kwargs.update({'x': x})
        if self.ffn is not None:
            x = self.ffn(**kwargs)
        return x

class Transformer(nn.Module):
    """Transformer model with self-attention and feed-forward network layers."""

    def __init__(self,
                 input_dim: int = 1024,
                 latent_dim: int = 1024,
                 num_heads: int = 10,
                 num_layers: int = 4,
                 max_seq_len: int = 300,
                 stride: int = 1,
                 dropout: float = 0):
        super().__init__()
        self.blocks = nn.ModuleList()
        self.proj_in = nn.Linear(input_dim, latent_dim)
        self.embedding = nn.Parameter(torch.randn(1, max_seq_len, latent_dim))
        self.latent_dim = latent_dim
        self.stride = stride
        self.num_heads = num_heads
        self.dropout = dropout
        
        sa_block_cfg = dict(
            type='EfficientSelfAttention',
            latent_dim=latent_dim,
            num_heads=num_heads,
            dropout=dropout
        )
        
        ffn_cfg = dict(
            latent_dim=latent_dim,
            ffn_dim=latent_dim * 4,
            dropout=dropout
        )
        for _ in range(num_layers):
            self.blocks.append(
                EncoderLayer(sa_block_cfg=sa_block_cfg, ffn_cfg=ffn_cfg)
            )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Forward pass through transformer layers."""
        x = x[:, ::self.stride, :]
        x = self.proj_in(x)
        T = x.shape[1]
        x = x + self.embedding[:, :T, :]
        # Apply each encoder layer
        for block in self.blocks:
            x = block(x=x)

        return x


@SUBMODULES.register_module()
class LargeMotionModel(MotionTransformer):
    """Large motion model with optional multi-modal conditioning (text, music, video, etc.)."""

    def __init__(self,
                 num_parts: int = 10,
                 latent_part_dim: int = 64,
                 num_cond_layers: int = 2,
                 num_datasets: int = 27,
                 guidance_cfg: Optional[Dict] = None,
                 moe_route_loss_weight: float = 1.0,
                 template_kl_loss_weight: float = 0.0001,
                 dataset_names: Optional[List[str]] = None,
                 text_input_dim: Optional[int] = None,
                 music_input_dim: Optional[int] = None,
                 speech_input_dim: Optional[int] = None,
                 video_input_dim: Optional[int] = None,
                 music_input_stride: Optional[int] = 3,
                 speech_input_stride: Optional[int] = 3,
                 cond_drop_rate: float = 0,
                 random_mask: float = 0,
                 dropout: float = 0,
                 **kwargs):
        kwargs['latent_dim'] = latent_part_dim * num_parts
        self.num_parts = num_parts
        self.latent_part_dim = latent_part_dim
        self.num_datasets = num_datasets
        self.dropout = dropout
        
        super().__init__(**kwargs)
        self.guidance_cfg = guidance_cfg

        self.joint_embed = PoseEncoder(
            latent_dim=self.latent_part_dim,
            num_datasets=self.num_datasets)
        self.out = zero_module(PoseDecoder(
            latent_dim=self.latent_part_dim,
            output_dim=self.input_feats,
            num_datasets=self.num_datasets))

        self.dataset_proj = {name: i for i, name in enumerate(dataset_names or [])}
        self.rotation_proj = {'h3d_rot': 0, 'smpl_rot': 1, 'bvh_rot': 2}

        self.moe_route_loss_weight = moe_route_loss_weight
        self.template_kl_loss_weight = template_kl_loss_weight
        self.cond_drop_rate = cond_drop_rate

        # Conditional transformers for multi-modal inputs
        self.text_cond = text_input_dim is not None
        self.music_cond = music_input_dim is not None
        self.speech_cond = speech_input_dim is not None
        self.video_cond = video_input_dim is not None

        if self.text_cond:
            self.text_transformer = Transformer(
                input_dim=text_input_dim,
                latent_dim=self.latent_dim,
                num_heads=self.num_parts,
                num_layers=num_cond_layers,
                dropout=self.dropout)
        if self.music_cond:
            self.music_transformer = Transformer(
                input_dim=music_input_dim,
                latent_dim=self.latent_dim,
                num_heads=self.num_parts,
                num_layers=num_cond_layers,
                dropout=self.dropout,
                stride=music_input_stride)
        if self.speech_cond:
            self.speech_transformer = Transformer(
                input_dim=speech_input_dim,
                latent_dim=self.latent_dim,
                num_heads=self.num_parts,
                num_layers=num_cond_layers,
                dropout=self.dropout,
                stride=speech_input_stride)
        if self.video_cond:
            self.video_transformer = Transformer(
                input_dim=video_input_dim,
                latent_dim=self.latent_dim,
                num_heads=self.num_parts,
                num_layers=num_cond_layers,
                dropout=self.dropout)

        self.mask_token = nn.Parameter(torch.randn(self.num_parts, self.latent_part_dim))
        self.clean_token = nn.Parameter(torch.randn(self.num_parts, self.latent_part_dim))
        self.random_mask = random_mask

    def build_temporal_blocks(self,
                              sa_block_cfg: Optional[Dict] = None, 
                              ca_block_cfg: Optional[Dict] = None,
                              ffn_cfg: Optional[Dict] = None):
        """Build temporal decoder blocks with attention and feed-forward networks."""
        self.temporal_decoder_blocks = nn.ModuleList()
        ca_block_cfg['latent_dim'] = self.latent_part_dim
        ca_block_cfg['num_heads'] = self.num_parts
        ca_block_cfg['ffn_dim'] = self.latent_part_dim * 4
        ca_block_cfg['time_embed_dim'] = self.time_embed_dim
        ca_block_cfg['max_seq_len'] = self.max_seq_len
        ca_block_cfg['dropout'] = self.dropout
        for _ in range(self.num_layers):
            ffn_cfg_block = dict(
                latent_dim=self.latent_part_dim,
                ffn_dim=self.latent_part_dim * 4,
                dropout=self.dropout,
                time_embed_dim=self.time_embed_dim
            )
            self.temporal_decoder_blocks.append(
                DecoderLayer(ca_block_cfg=ca_block_cfg, ffn_cfg=ffn_cfg_block)
            )

    def scale_func(self, timestep: torch.Tensor, dataset_name: str) -> torch.Tensor:
        """Scale function for diffusion, adjusting weights based on timestep."""
        guidance_cfg = self.guidance_cfg[dataset_name]
        if guidance_cfg['type'] == 'constant':
            w = torch.ones_like(timestep).float() * guidance_cfg['scale']
        elif guidance_cfg['type'] == 'linear':
            scale = guidance_cfg['scale']
            w = (1 - (1000 - timestep) / 1000) * scale + 1
        else:
            raise NotImplementedError()
        return w

    def aux_loss(self) -> Dict[str, torch.Tensor]:
        """Compute auxiliary and KL losses for multi-modal routing."""
        aux_loss = 0
        kl_loss = 0
        for module in self.temporal_decoder_blocks:
            if hasattr(module.ca_block, 'aux_loss'):
                aux_loss += module.ca_block.aux_loss
            if hasattr(module.ca_block, 'kl_loss'):
                kl_loss += module.ca_block.kl_loss
        losses = {}
        if aux_loss > 0:
            losses['moe_route_loss'] = aux_loss * self.moe_route_loss_weight
        if kl_loss > 0:
            losses['template_kl_loss'] = kl_loss * self.template_kl_loss_weight
        return losses

    def get_precompute_condition(self,
                                 text_word_feat: Optional[torch.Tensor] = None,
                                 text_word_out: Optional[torch.Tensor] = None,
                                 text_cond: Optional[torch.Tensor] = None,
                                 music_word_feat: Optional[torch.Tensor] = None,
                                 music_word_out: Optional[torch.Tensor] = None,
                                 music_cond: Optional[torch.Tensor] = None,
                                 speech_word_feat: Optional[torch.Tensor] = None,
                                 speech_word_out: Optional[torch.Tensor] = None,
                                 speech_cond: Optional[torch.Tensor] = None,
                                 video_word_feat: Optional[torch.Tensor] = None,
                                 video_word_out: Optional[torch.Tensor] = None,
                                 video_cond: Optional[torch.Tensor] = None,
                                 **kwargs) -> Dict[str, torch.Tensor]:
        """Precompute conditions for various modalities (text, music, speech, video)."""
        output = {}
        if self.text_cond and text_word_feat is not None:
            text_word_feat = text_word_feat.float()
            if text_word_out is None:
                if text_cond is None or torch.sum(text_cond) == 0:
                    latent_dim = self.text_transformer.latent_dim
                    B, N = text_word_feat.shape[:2]
                    text_word_out = torch.zeros(B, N, latent_dim).type_as(text_word_feat)
                else:
                    text_word_out = self.text_transformer(text_word_feat)
            output['text_word_out'] = text_word_out
        if self.music_cond and music_word_feat is not None:
            music_word_feat = music_word_feat.float()
            if music_word_out is None:
                if music_cond is None or torch.sum(music_cond) == 0:
                    latent_dim = self.music_transformer.latent_dim
                    B, N = music_word_feat.shape[:2]
                    music_word_out = torch.zeros(B, N, latent_dim).type_as(music_word_feat)
                else:
                    music_word_out = self.music_transformer(music_word_feat)
            output['music_word_out'] = music_word_out
        if self.speech_cond and speech_word_feat is not None:
            speech_word_feat = speech_word_feat.float()
            if speech_word_out is None:
                if speech_cond is None or torch.sum(speech_cond) == 0:
                    latent_dim = self.speech_transformer.latent_dim
                    B, N = speech_word_feat.shape[:2]
                    speech_word_out = torch.zeros(B, N, latent_dim).type_as(speech_word_feat)
                else:
                    speech_word_out = self.speech_transformer(speech_word_feat)
            output['speech_word_out'] = speech_word_out
        if self.video_cond and video_word_feat is not None:
            video_word_feat = video_word_feat.float()
            if video_word_out is None:
                if video_cond is None or torch.sum(video_cond) == 0:
                    latent_dim = self.video_transformer.latent_dim
                    B, N = video_word_feat.shape[:2]
                    video_word_out = torch.zeros(B, N, latent_dim).type_as(video_word_feat)
                else:
                    video_word_out = self.video_transformer(video_word_feat)
            output['video_word_out'] = video_word_out
        return output

    def post_process(self, motion: torch.Tensor) -> torch.Tensor:
        """Post-process motion data (e.g., unnormalization)."""
        if self.post_process_cfg is not None and self.post_process_cfg.get("unnormalized_infer", False):
            mean = torch.from_numpy(np.load(self.post_process_cfg['mean_path'])).type_as(motion)
            std = torch.from_numpy(np.load(self.post_process_cfg['std_path'])).type_as(motion)
            motion = motion * std + mean
        return motion

    def forward_train(self,
                      h: torch.Tensor,
                      src_mask: torch.Tensor,
                      emb: torch.Tensor,
                      timesteps: torch.Tensor,
                      motion_length: Optional[torch.Tensor] = None,
                      text_word_out: Optional[torch.Tensor] = None,
                      text_cond: Optional[torch.Tensor] = None,
                      music_word_out: Optional[torch.Tensor] = None,
                      music_cond: Optional[torch.Tensor] = None,
                      speech_word_out: Optional[torch.Tensor] = None,
                      speech_cond: Optional[torch.Tensor] = None,
                      video_word_out: Optional[torch.Tensor] = None,
                      video_cond: Optional[torch.Tensor] = None,
                      num_intervals: int = 1,
                      duration: Optional[torch.Tensor] = None,
                      dataset_idx: Optional[torch.Tensor] = None,
                      rotation_idx: Optional[torch.Tensor] = None,
                      **kwargs) -> torch.Tensor:
        """Forward pass for training, applying multi-modal conditions."""
        B, T = h.shape[:2]
        # Apply conditional masking if needed
        if self.text_cond and text_cond is not None:
            text_cond_mask = torch.rand(B).type_as(h)
            text_cond[text_cond_mask < self.cond_drop_rate] = 0
        if self.music_cond and music_cond is not None:
            music_cond_mask = torch.rand(B).type_as(h)
            music_cond[music_cond_mask < self.cond_drop_rate] = 0
        if self.speech_cond and speech_cond is not None:
            speech_cond_mask = torch.rand(B).type_as(h)
            speech_cond[speech_cond_mask < self.cond_drop_rate] = 0
        if self.video_cond and video_cond is not None:
            video_cond_mask = torch.rand(B).type_as(h)
            video_cond[video_cond_mask < self.cond_drop_rate] = 0

        # Apply each temporal decoder block
        for module in self.temporal_decoder_blocks:
            h = module(x=h,
                       emb=emb,
                       src_mask=src_mask,
                       motion_length=motion_length,
                       text_cond=text_cond,
                       text_word_out=text_word_out,
                       music_cond=music_cond,
                       music_word_out=music_word_out,
                       speech_cond=speech_cond,
                       speech_word_out=speech_word_out,
                       video_cond=video_cond,
                       video_word_out=video_word_out,
                       num_intervals=num_intervals,
                       duration=duration,
                       dataset_idx=dataset_idx,
                       rotation_idx=rotation_idx)

        # Output layer
        output = self.out(h, dataset_idx).view(B, T, -1).contiguous()
        return output

    def forward_test(self,
                     h: torch.Tensor,
                     src_mask: torch.Tensor,
                     emb: torch.Tensor,
                     timesteps: torch.Tensor,
                     motion_length: torch.Tensor,
                     text_word_out: Optional[torch.Tensor] = None,
                     text_cond: Optional[torch.Tensor] = None,
                     music_word_out: Optional[torch.Tensor] = None,
                     music_cond: Optional[torch.Tensor] = None,
                     speech_word_out: Optional[torch.Tensor] = None,
                     speech_cond: Optional[torch.Tensor] = None,
                     video_word_out: Optional[torch.Tensor] = None,
                     video_cond: Optional[torch.Tensor] = None,
                     num_intervals: int = 1,
                     duration: Optional[torch.Tensor] = None,
                     dataset_idx: Optional[torch.Tensor] = None,
                     rotation_idx: Optional[torch.Tensor] = None,
                     dataset_name: Optional[str] = 'humanml3d_t2m',
                     **kwargs) -> torch.Tensor:
        """Forward pass for testing, including scaling and conditional fusion."""
        B, T = h.shape[:2]
        # Duplicate tensors for conditional and non-conditional cases
        h = h.repeat(2, 1, 1)
        emb = emb.repeat(2, 1)
        src_mask = src_mask.repeat(2, 1, 1, 1)
        motion_length = motion_length.repeat(2, 1)
        duration = duration.repeat(2)
        
        # dataset_idx_att = [self.dataset_proj['all'] for i in range(B)]
        # dataset_idx_att = torch.tensor(dataset_idx_att, dtype=torch.long).to(h.device)
        # dataset_idx_att = torch.cat((dataset_idx, dataset_idx_att))
        dataset_idx = dataset_idx.repeat(2)
        rotation_idx = rotation_idx.repeat(2)
        
        if self.text_cond and text_cond is not None and text_word_out is not None:
            text_cond = text_cond.repeat(2, 1)
            text_cond[B:] = 0
            text_word_out = text_word_out.repeat(2, 1, 1)
        if self.music_cond and music_cond is not None and music_word_out is not None:
            music_cond = music_cond.repeat(2, 1)
            music_cond[B:] = 0
            music_word_out = music_word_out.repeat(2, 1, 1)
        if self.speech_cond and speech_cond is not None and speech_word_out is not None:
            speech_cond = speech_cond.repeat(2, 1)
            speech_cond[B:] = 0
            speech_word_out = speech_word_out.repeat(2, 1, 1)
        if self.video_cond and video_cond is not None and video_word_out is not None:
            video_cond = video_cond.repeat(2, 1)
            video_cond[B:] = 0
            video_word_out = video_word_out.repeat(2, 1, 1)

        # Apply each temporal decoder block
        for module in self.temporal_decoder_blocks:
            h = module(x=h,
                       emb=emb,
                       src_mask=src_mask,
                       motion_length=motion_length,
                       text_cond=text_cond,
                       text_word_out=text_word_out,
                       music_cond=music_cond,
                       music_word_out=music_word_out,
                       speech_cond=speech_cond,
                       speech_word_out=speech_word_out,
                       video_cond=video_cond,
                       video_word_out=video_word_out,
                       num_intervals=num_intervals,
                       duration=duration,
                       dataset_idx=dataset_idx,
                       rotation_idx=rotation_idx)

        # Process the output from conditional and non-conditional branches
        output = self.out(h, dataset_idx).view(2 * B, T, -1).contiguous()
        scale = self.scale_func(timesteps, dataset_name).view(-1, 1, 1)
        output_cond = output[:B].contiguous()
        output_none = output[B:].contiguous()

        # Fuse conditional and non-conditional outputs
        output = output_cond * scale + output_none * (1 - scale)
        return output

    def create_mask_from_length(self, T: int, motion_length: torch.Tensor) -> torch.Tensor:
        """Create a binary mask based on motion length."""
        B = motion_length.shape[0]
        src_mask = torch.zeros(B, T)
        for bix in range(B):
            src_mask[bix, :int(motion_length[bix])] = 1
        return src_mask

    def forward(self,
                motion: torch.Tensor,
                timesteps: torch.Tensor,
                motion_mask: Optional[torch.Tensor] = None,
                motion_length: Optional[torch.Tensor] = None,
                num_intervals: int = 1,
                motion_metas: Optional[List[Dict]] = None,
                text_seq_feat: Optional[torch.Tensor] = None,
                text_word_feat: Optional[torch.Tensor] = None,
                text_cond: Optional[torch.Tensor] = None,
                music_seq_feat: Optional[torch.Tensor] = None,
                music_word_feat: Optional[torch.Tensor] = None,
                music_cond: Optional[torch.Tensor] = None,
                speech_seq_feat: Optional[torch.Tensor] = None,
                speech_word_feat: Optional[torch.Tensor] = None,
                speech_cond: Optional[torch.Tensor] = None,
                video_seq_feat: Optional[torch.Tensor] = None,
                video_word_feat: Optional[torch.Tensor] = None,
                video_cond: Optional[torch.Tensor] = None,
                **kwargs) -> torch.Tensor:
        """Unified forward pass for both training and testing."""
        B, T = motion.shape[:2]
        # Precompute conditioning features
        conditions = self.get_precompute_condition(
            motion_length=motion_length,
            text_seq_feat=text_seq_feat,
            text_word_feat=text_word_feat,
            text_cond=text_cond,
            music_seq_feat=music_seq_feat,
            music_word_feat=music_word_feat,
            music_cond=music_cond,
            speech_seq_feat=speech_seq_feat,
            speech_word_feat=speech_word_feat,
            speech_cond=speech_cond,
            video_seq_feat=video_seq_feat,
            video_word_feat=video_word_feat,
            video_cond=video_cond,
            device=motion.device,
            **kwargs
        )
        if self.training:
            new_motion_mask = motion_mask.clone()
            rand_mask = torch.rand_like(motion_mask)
            threshold = torch.rand(B).type_as(rand_mask)
            threshold = threshold.view(B, 1, 1).repeat(1, T, self.num_parts)
            new_motion_mask[rand_mask < threshold] = 0
            motion_mask = new_motion_mask
        else:
            t = int(timesteps[0])

        motion_mask = motion_mask.view(B, T, 10, 1)
        
        # Temporal embedding
        emb = self.time_embed(timestep_embedding(timesteps, self.latent_dim))
        
        # Prepare duration and framerate embeddings
        duration = []
        for meta in motion_metas:
            framerate = meta['meta_data']['framerate']
            duration.append(1.0 / framerate)

        duration = torch.tensor(duration, dtype=motion.dtype).to(motion.device)

        # Dataset index embedding
        dataset_idx = []
        for i in range(B):
            dataset_name = motion_metas[i]['meta_data']['dataset_name']
            if torch.rand(1).item() < 0.1 and self.training:
                dataset_name = 'all'
            idx = self.dataset_proj[dataset_name]
            dataset_idx.append(idx)
        dataset_idx = torch.tensor(dataset_idx, dtype=torch.long).to(motion.device)
        self.dataset_idx = dataset_idx.clone().detach()
        
        # Rotation index embedding
        rotation_idx = [self.rotation_proj[meta['meta_data']['rotation_type']] for meta in motion_metas]
        rotation_idx = torch.tensor(rotation_idx, dtype=torch.long).to(motion.device)

        # Embed motion with pose encoder
        h = self.joint_embed(motion, dataset_idx)
        h = h.view(B, T, 10, -1) * motion_mask + (1 - motion_mask) * self.mask_token
        h = h.view(B, T, -1)

        # Source mask based on motion length
        src_mask = self.create_mask_from_length(T, motion_length).to(motion.device)
        src_mask = src_mask.view(B, T, 1, 1).repeat(1, 1, 10, 1)

        # Training or testing forward
        if self.training:
            output = self.forward_train(
                h=h,
                emb=emb,
                src_mask=src_mask,
                timesteps=timesteps,
                motion_length=motion_length,
                text_cond=text_cond,
                music_cond=music_cond,
                speech_cond=speech_cond,
                video_cond=video_cond,
                num_intervals=num_intervals,
                duration=duration,
                dataset_idx=dataset_idx,
                rotation_idx=rotation_idx,
                **conditions
            )
        else:
            output = self.forward_test(
                h=h,
                emb=emb,
                src_mask=src_mask,
                timesteps=timesteps,
                motion_length=motion_length,
                text_cond=text_cond,
                music_cond=music_cond,
                speech_cond=speech_cond,
                video_cond=video_cond,
                num_intervals=num_intervals,
                duration=duration,
                dataset_idx=dataset_idx,
                rotation_idx=rotation_idx,
                dataset_name=dataset_name,
                **conditions
            )

        return output