File size: 27,528 Bytes
5024339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import types
import json
from typing import List, Optional
import torch
from torch import nn
import os
import numpy as np
import gc

from safetensors.torch import load_file

from .utils import SchedulerInterface, FlowMatchScheduler
from .wan.modules.tokenizers import HuggingfaceTokenizer
from .wan.modules.model import WanModel
from .wan.modules.vae import _video_vae
from .wan.modules.t5 import umt5_xxl
from .wan.modules.clip import CLIPModel
from .utils import GeneralLoRALoader

class WanTextEncoder(torch.nn.Module):
    def __init__(self, model_name: str) -> None:
        super().__init__()
        self.model_name = model_name

        self.text_encoder = umt5_xxl(
            encoder_only=True,
            return_tokenizer=False,
            dtype=torch.float32,
            device=torch.device('cpu')
        ).eval().requires_grad_(False)

        # Dynamically construct path based on model_name
        t5_encoder_path = os.path.join(model_name, "models_t5_umt5-xxl-enc-bf16.pth")
        self.text_encoder.load_state_dict(
            torch.load(t5_encoder_path, map_location='cpu', weights_only=False)
        )

        # Dynamically construct tokenizer path
        tokenizer_path = os.path.join(model_name, "google/umt5-xxl/")
        self.tokenizer = HuggingfaceTokenizer(
            name=tokenizer_path, seq_len=512, clean='whitespace')

    @property
    def device(self):
        # Return actual device of text_encoder parameters
        return next(self.text_encoder.parameters()).device

    def forward(self, text_prompts: List[str]) -> dict:
        ids, mask = self.tokenizer(
            text_prompts, return_mask=True, add_special_tokens=True)
        ids = ids.to(self.device)
        mask = mask.to(self.device)
        seq_lens = mask.gt(0).sum(dim=1).long()
        context = self.text_encoder(ids, mask)

        for u, v in zip(context, seq_lens):
            u[v:] = 0.0  # set padding to 0.0

        return {
            "prompt_embeds": context
        }


class WanVAEWrapper(torch.nn.Module):
    def __init__(self, model_name="/home/zhangzhilu/anaconda3/pretrained_models/Wan2.1-T2V-1.3B"):
        super().__init__()
        self.model_name = model_name
        mean = [
            -0.7571, -0.7089, -0.9113, 0.1075, -0.1745, 0.9653, -0.1517, 1.5508,
            0.4134, -0.0715, 0.5517, -0.3632, -0.1922, -0.9497, 0.2503, -0.2921
        ]
        std = [
            2.8184, 1.4541, 2.3275, 2.6558, 1.2196, 1.7708, 2.6052, 2.0743,
            3.2687, 2.1526, 2.8652, 1.5579, 1.6382, 1.1253, 2.8251, 1.9160
        ]
        self.mean = torch.tensor(mean, dtype=torch.float32)
        self.std = torch.tensor(std, dtype=torch.float32)

        # init model
        self.model = _video_vae(
            pretrained_path=f"{self.model_name}/Wan2.1_VAE.pth",
            z_dim=16,
        ).eval().requires_grad_(False)

        self.dtype = torch.bfloat16

        self.vae_stride = (4, 8, 8)
        self.target_video_length = 81

    @torch.no_grad()
    def encode(self, pixel):
        device, dtype = pixel[0].device, self.dtype
        scale = [self.mean.to(device=device, dtype=dtype),
                 1.0 / self.std.to(device=device, dtype=dtype)]
        with torch.autocast(device_type='cuda', dtype=self.dtype):
            output = [
                self.model.encode(u.to(self.dtype).unsqueeze(0), scale).squeeze(0)
                for u in pixel
            ]
        return output

    def run_vae_encoder(self, img, new_target_video_length=None, add_first_to_kvcache=False):
        if add_first_to_kvcache == None:
            add_first_to_kvcache = False

        if new_target_video_length is not None:
            self.target_video_length = new_target_video_length
        # 将输入直接转换到目标 dtype/device,避免频繁 cpu<->cuda 拷贝触发大块缓存
        img = img.to(device="cuda", dtype=torch.bfloat16)
        h, w = img.shape[1:]
        lat_h = h // self.vae_stride[1]
        lat_w = w // self.vae_stride[2]

        msk = torch.ones(
            1,
            self.target_video_length,
            lat_h,
            lat_w,
            device=img.device,
            dtype=torch.bfloat16,
        )
        
        #! 只有普通I2V,才不用生成第一帧
        msk[:, 1:] = 0
        
        #! 其他所有情况,就算是把第一帧加到KVCache里,也是denoise的一部分。
        if add_first_to_kvcache:
            msk[:, 0:] = 0

        msk = torch.concat([torch.repeat_interleave(msk[:, 0:1], repeats=4, dim=1), msk[:, 1:]], dim=1) # 首帧是1,后续20都是0
        msk = msk.view(1, msk.shape[1] // 4, 4, lat_h, lat_w)
        msk = msk.transpose(1, 2)[0]
        #! 如果是普通I2V
        if not add_first_to_kvcache:
            vae_encode_out = self.encode(
                [
                    torch.concat(
                        [
                            torch.nn.functional.interpolate(img[None], size=(h, w),mode="bicubic",).transpose(0, 1),  # [3, 1, H, W]
                            torch.zeros(3, self.target_video_length - 1, h, w, device=img.device, dtype=torch.bfloat16,),  # [3, F-1, H, W]
                        ],
                        dim=1,  # [3, F, H, W]
                    )
                ],
            )[0]  # [1帧img, 其余全 0]
            
        else:
            vae_encode_out = self.encode(
                [
                    torch.concat(
                        [
                            torch.zeros(
                                3,
                                self.target_video_length,
                                h,
                                w,
                                device=img.device,
                                dtype=torch.bfloat16,
                            ),
                        ],
                        dim=1,
                    )
                ],
            )[0]
        
        # msk: [1帧1,后面都是0],维度4

        vae_encode_out = torch.concat([msk, vae_encode_out]).to(torch.bfloat16)
        return vae_encode_out # [20, 21, H, W]

    def encode_to_latent(self, pixel: torch.Tensor) -> torch.Tensor:
        # pixel: [batch_size, num_channels, num_frames, height, width]
        device, dtype = pixel.device, pixel.dtype
        scale = [self.mean.to(device=device, dtype=dtype),
                 1.0 / self.std.to(device=device, dtype=dtype)]

        with torch.autocast(device_type='cuda', dtype=self.dtype):
            output = [
                self.model.encode(u.unsqueeze(0), scale).squeeze(0)
                for u in pixel
            ]
        output = torch.stack(output, dim=0)
        # from [batch_size, num_channels, num_frames, height, width]
        # to [batch_size, num_frames, num_channels, height, width]
        output = output.permute(0, 2, 1, 3, 4)
        return output

    def decode_to_pixel(self, latent: torch.Tensor, use_cache: bool = False) -> torch.Tensor:
        # from [batch_size, num_frames, num_channels, height, width]
        # to [batch_size, num_channels, num_frames, height, width]
        zs = latent.permute(0, 2, 1, 3, 4)
        if use_cache:
            assert latent.shape[0] == 1, "Batch size must be 1 when using cache"

        device, dtype = latent.device, latent.dtype
        scale = [self.mean.to(device=device, dtype=dtype),
                 1.0 / self.std.to(device=device, dtype=dtype)]

        if use_cache:
            decode_function = self.model.cached_decode
        else:
            decode_function = self.model.decode

        output = []
        for u in zs:
            output.append(decode_function(u.unsqueeze(0), scale).float().clamp_(-1, 1).squeeze(0))
        output = torch.stack(output, dim=0)
        # from [batch_size, num_channels, num_frames, height, width]
        # to [batch_size, num_frames, num_channels, height, width]
        output = output.permute(0, 2, 1, 3, 4)
        return output


class WanCLIPEncoder(torch.nn.Module):
    def __init__(self, model_name="/home/zhangzhilu/anaconda3/pretrained_models/Wan2.1-T2V-1.3B"):
        super().__init__()
        self.model_name = model_name
        self.image_encoder = CLIPModel(
            dtype=torch.bfloat16,
            device=torch.device('cpu'),
            checkpoint_path=os.path.join(
                f"{self.model_name}/",
                "models_clip_open-clip-xlm-roberta-large-vit-huge-14.pth",
            )
        )

    @property
    def device(self):
        # Return actual device of image_encoder parameters
        try:
            return next(self.image_encoder.model.parameters()).device
        except StopIteration:
            return torch.device('cpu')

    def forward(self, img):
        # img = TF.to_tensor(img).sub_(0.5).div_(0.5).cuda()
        target_device = self.device
        if img.ndim == 3:
            img = img[:, None, :, :].to(target_device)
        elif img.ndim == 4:
            img = img.transpose(0,1).to(target_device)

        clip_encoder_out = self.image_encoder.visual([img]).squeeze(0)
        return clip_encoder_out


class WanWrapperBase(torch.nn.Module):
    """
    Wan Wrapper Base Class - simplified model loading:

    Stage 1: Random initialization of base model + adapters/controllers
    Stage 2: Load official Wan pretrained weights (wan_model_name) - backbone only
    Stage 3: Load generator_backbone_ckpt_path as backbone weights (only if use_lora=False)

    NOTE:
    - LoRA is handled entirely in Task layer (using generator_lora_ckpt_path)
    - Submodule weights (control_adapter, sp_blocks) loaded in Task layer
    """

    def __init__(
            self,
            role="generator",
            config=None,
            **kwargs
    ):
        super().__init__()
        self.config = config
        self.role = role

        # Extract parameters from config
        wan_model_name = getattr(config, 'wan_model_name', None)
        generator_backbone_ckpt_path = getattr(config, 'generator_backbone_ckpt_path', None)
        generator_lora_ckpt_path = getattr(config, 'generator_lora_ckpt_path', None)
        timestep_shift = getattr(config, 'timestep_shift', 8.0)
        num_latent_frames = getattr(config, 'num_latent_frames', config.image_or_video_shape[1])
        use_lora = getattr(config, 'use_lora', False)
        # Default: if we already have a backbone checkpoint (non-LoRA), skip the official weights to avoid double loading a 14B model.
        default_load_official_backbone = use_lora or generator_backbone_ckpt_path is None
        load_official_backbone = getattr(config, 'load_official_backbone', default_load_official_backbone)
        print(f"[Wrapper] load_official_backbone={load_official_backbone}, use_lora={use_lora}, generator_backbone_ckpt_path_set={generator_backbone_ckpt_path is not None}")

        # ====== Stage 1: Random initialization ======
        self._init_base_model(**config)

        # Initialize scheduler
        self.scheduler = FlowMatchScheduler(shift=timestep_shift, sigma_min=0.0, extra_one_step=True)
        self.scheduler.set_timesteps(1000, training=True)
        self.post_init()

        self.seq_len = num_latent_frames * np.prod(self.config.image_or_video_shape[-2:]).item() // 4
        self.local_attn_size = num_latent_frames

        # ====== Stage 2: Load official Wan pretrained weights ======
        if wan_model_name is not None and load_official_backbone:
            self._load_official_weights(wan_model_name)
        elif wan_model_name is not None:
            print("[Wrapper] Skipping official Wan weights load (load_official_backbone=False)")

        # ====== Stage 3: Load backbone checkpoint ======
        if role == "generator" and generator_backbone_ckpt_path:
            self._load_backbone_checkpoint(generator_backbone_ckpt_path)

        # Freeze all parameters by default
        self.requires_grad_(False)
        torch.cuda.empty_cache()

    def _init_base_model(self, **kwargs):
        """
        Stage 1: Initialize base model with random weights.
        All sub-modules (adapters, controllers) are also randomly initialized here.
        Subclasses should override this to call load_caus_or_bir_basemodel.
        """
        self.load_caus_or_bir_basemodel(**kwargs)

    def _load_official_weights(self, wan_model_name: str):
        """
        Stage 2: Load official Wan pretrained weights.
        Only loads weights for the base model backbone.
        Sub-modules (adapters, controllers) remain randomly initialized.
        """
        print(f"[Wrapper] Loading official Wan weights from: {wan_model_name}")
        wan_state_dict = self._load_state_dict_from_pretrained(wan_model_name)

        # Filter out adapter/controller weights to keep them randomly initialized
        backbone_state_dict = {
            k: v for k, v in wan_state_dict.items()
            if not self._is_submodule_key(k)
        }

        missing, unexpected = self.model.load_state_dict(backbone_state_dict, strict=False)
        
        # Log missing keys (expected for adapters/controllers)
        if missing:
            submodule_missing = [k for k in missing if self._is_submodule_key(k)]
            backbone_missing = [k for k in missing if not self._is_submodule_key(k)]
            if backbone_missing:
                print(f"[Wrapper] WARNING: Missing backbone keys: {backbone_missing[:5]}...")
            if submodule_missing:
                print(f"[Wrapper] INFO: Submodule keys not loaded (expected): {len(submodule_missing)} keys")

        assert len(unexpected) == 0, f"Official weights load failed! Unexpected keys: {unexpected}"

        del wan_state_dict, backbone_state_dict
        gc.collect()
        torch.cuda.empty_cache()
        print(f"[Wrapper] Official weights loaded successfully")

    def _is_submodule_key(self, key: str) -> bool:
        """
        Check if a key belongs to a submodule (adapter/controller).
        These should be loaded separately, not from official weights.
        """
        submodule_prefixes = [
            'control_adapter',
            'sp_blocks',
            'sp_patch_embedding',
        ]
        return any(prefix in key for prefix in submodule_prefixes)

    def _load_backbone_checkpoint(self, generator_ckpt_path: str):
        """
        Load full backbone weights from checkpoint (non-LoRA case).
        """
        print(f"[Wrapper] Loading backbone checkpoint from: {generator_ckpt_path}")

        state_dict = self._load_checkpoint_file(generator_ckpt_path)

        # Load full model weights (excluding submodule keys)
        backbone_state_dict = {
            k: v for k, v in state_dict.items()
            if not self._is_submodule_key(k)
        }
        missing, unexpected = self.model.load_state_dict(backbone_state_dict, strict=False)

        if unexpected:
            # Filter out LoRA keys which are expected to be missing when not using LoRA
            non_lora_unexpected = [k for k in unexpected if 'lora_' not in k]
            assert len(non_lora_unexpected) == 0, f"Backbone checkpoint load failed! Unexpected keys: {non_lora_unexpected}"

        print(f"[Wrapper] Backbone weights loaded successfully")
        del state_dict, backbone_state_dict
        gc.collect()
        torch.cuda.empty_cache()

    def _load_checkpoint_file(self, ckpt_path: str) -> dict:
        """
        Load checkpoint file and handle different formats.

        Respects config.load_ema setting:
        - load_ema=True (default): prefer generator_ema if available
        - load_ema=False: always use generator, ignore generator_ema
        """
        if ckpt_path.endswith('.safetensors'):
            state_dict = load_file(ckpt_path, device="cpu")
        else:
            # Use mmap for large checkpoint files to avoid OOM during loading
            state_dict = torch.load(ckpt_path, map_location="cpu", weights_only=False, mmap=True)

        # Check load_ema config (default True for backward compatibility)
        load_ema = getattr(self.config, 'load_ema', True)

        # Handle different checkpoint formats
        if "generator" in state_dict or "generator_ema" in state_dict:
            if load_ema and "generator_ema" in state_dict:
                # Prefer EMA weights
                print(f"[Wrapper] Loading generator_ema weights (load_ema=True)")
                state_dict = state_dict["generator_ema"]
            elif "generator" in state_dict:
                # Use non-EMA weights
                print(f"[Wrapper] Loading generator weights (load_ema={load_ema})")
                state_dict = state_dict["generator"]
            else:
                # Only EMA available but load_ema=False, use EMA anyway with warning
                print(f"[Wrapper] WARNING: load_ema=False but only generator_ema found, using it anyway")
                state_dict = state_dict["generator_ema"]

            state_dict = {k.replace("model.", ""): v for k, v in state_dict.items()}

        return state_dict

    def load_caus_or_bir_basemodel(self, **kwargs):
        raise NotImplementedError

    @staticmethod
    def _load_state_dict_from_pretrained(model_path: str) -> dict:
        """
        Load state_dict directly from pretrained model path without instantiating the model class.
        This avoids memory issues when the pretrained model uses a different class (e.g., WanTransformer3DModel vs WanModel).

        Supports both single safetensors file and sharded safetensors files.
        """
        # Check for single safetensors file
        single_file = os.path.join(model_path, "diffusion_pytorch_model.safetensors")
        if os.path.exists(single_file):
            print(f"Loading state_dict from single file: {single_file}")
            return load_file(single_file, device='cpu')

        # Check for sharded safetensors files
        index_file = os.path.join(model_path, "diffusion_pytorch_model.safetensors.index.json")
        if os.path.exists(index_file):
            print(f"Loading state_dict from sharded files: {index_file}")
            with open(index_file, 'r') as f:
                index = json.load(f)

            # Get unique shard files
            shard_files = set(index["weight_map"].values())

            # Load and merge all shards
            state_dict = {}
            for shard_file in sorted(shard_files):
                shard_path = os.path.join(model_path, shard_file)
                print(f"  Loading shard: {shard_file}")
                shard_dict = load_file(shard_path, device='cpu')
                state_dict.update(shard_dict)

            return state_dict

        # Fallback to .bin format
        bin_file = os.path.join(model_path, "diffusion_pytorch_model.bin")
        if os.path.exists(bin_file):
            print(f"Loading state_dict from bin file: {bin_file}")
            return torch.load(bin_file, map_location='cpu', weights_only=False)

        raise FileNotFoundError(
            f"No model weights found in {model_path}. "
            f"Expected one of: diffusion_pytorch_model.safetensors, "
            f"diffusion_pytorch_model.safetensors.index.json, or diffusion_pytorch_model.bin"
        )
    
    
    def enable_gradient_checkpointing(self) -> None:
        self.model.enable_gradient_checkpointing()

    
    def _convert_flow_pred_to_x0(self, flow_pred: torch.Tensor, xt: torch.Tensor, timestep: torch.Tensor) -> torch.Tensor:
        """
        Convert flow matching's prediction to x0 prediction.
        flow_pred: the prediction with shape [B, C, H, W]
        xt: the input noisy data with shape [B, C, H, W]
        timestep: the timestep with shape [B]

        pred = noise - x0
        x_t = (1-sigma_t) * x0 + sigma_t * noise
        we have x0 = x_t - sigma_t * pred
        see derivations https://chatgpt.com/share/67bf8589-3d04-8008-bc6e-4cf1a24e2d0e
        """
        # use higher precision for calculations
        original_dtype = flow_pred.dtype
        flow_pred, xt, sigmas, timesteps = map(
            lambda x: x.double().to(flow_pred.device), [flow_pred, xt,
                                                        self.scheduler.sigmas,
                                                        self.scheduler.timesteps]
        )

        timestep_id = torch.argmin(
            (timesteps.unsqueeze(0) - timestep.unsqueeze(1)).abs(), dim=1)
        sigma_t = sigmas[timestep_id].reshape(-1, 1, 1, 1)
        x0_pred = xt - sigma_t * flow_pred
        return x0_pred.to(original_dtype)

    
    @staticmethod
    def forward(
        self,
        noisy_image_or_video,
        timestep: torch.Tensor,
        context,
        clip_fea=None,
        y=None,
        kv_cache: Optional[List[dict]] = None,
        crossattn_cache: Optional[List[dict]] = None,
        kv_start: Optional[int] = None,
        kv_end: Optional[int] = None,
        rope_start: Optional[int] = None,
        y_camera=None,
        **kwargs
    ) -> torch.Tensor:
        # [B, F] -> [B]
        if self.uniform_timestep:
            input_timestep = timestep[:, 0]
        else:
            input_timestep = timestep

        if kv_cache is not None:
            # dzc: For training Self-Forcing; for infering DMD, Self-Forcing
            flow_pred = self.model(
                x=noisy_image_or_video.permute(0, 2, 1, 3, 4),
                t=input_timestep,
                context=context,
                clip_fea=clip_fea,
                y=list(y) if y is not None else None,  # y is [B, C, F, H, W], model expects list of [C, F, H, W]
                seq_len=self.seq_len,
                kv_cache=kv_cache,
                crossattn_cache=crossattn_cache,
                kv_start=kv_start,
                kv_end=kv_end,
                rope_start=rope_start,
                **kwargs
            ).permute(0, 2, 1, 3, 4)
        else:
            flow_pred = self.model(
                x=noisy_image_or_video.permute(0, 2, 1, 3, 4),
                t=input_timestep,
                context=context,
                clip_fea=clip_fea,
                y=list(y) if y is not None else None,  # y is [B, C, F, H, W], model expects list of [C, F, H, W]
                seq_len=self.seq_len,
                y_camera=y_camera,
                **kwargs
            ).permute(0, 2, 1, 3, 4)
        
        pred_x0 = self._convert_flow_pred_to_x0(
            flow_pred=flow_pred.flatten(0, 1),
            xt=noisy_image_or_video.flatten(0, 1),
            timestep=timestep.flatten(0, 1)
        ).unflatten(0, flow_pred.shape[:2])

        return flow_pred, pred_x0

    
    def get_scheduler(self) -> SchedulerInterface:
        """
        Update the current scheduler with the interface's static method
        """
        scheduler = self.scheduler
        scheduler.convert_x0_to_noise = types.MethodType(
            SchedulerInterface.convert_x0_to_noise, scheduler)
        scheduler.convert_noise_to_x0 = types.MethodType(
            SchedulerInterface.convert_noise_to_x0, scheduler)
        scheduler.convert_velocity_to_x0 = types.MethodType(
            SchedulerInterface.convert_velocity_to_x0, scheduler)
        self.scheduler = scheduler
        return scheduler

    def post_init(self):
        """
        A few custom initialization steps that should be called after the object is created.
        Currently, the only one we have is to bind a few methods to scheduler.
        We can gradually add more methods here if needed.
        """
        self.get_scheduler()



# --- Additional import for BidirectionalWanWrapperSP ---
from .wan.modules.model_state_adapter import StateAdapterWanModel

from typing import Optional, List
import torch


class BidirectionalWanWrapperSP(WanWrapperBase):
    """
    Wrapper for LiveWorld: Camera-controlled video generation with State Adapter.

    Uses StateAdapterWanModel which is optimized for:
    - Per-frame timestep (R+P frames get t=0, T frames get sampled t)
    - State Adapter processes P+T scene projections while main model has R+P+T frames

    Note: State Adapter weights are loaded in the Task layer (_load_sp_weights),
    not in the wrapper, following the same pattern as camera adapter loading.
    """

    def __init__(
            self,
            role="generator",
            config=None,
            **kwargs
    ):
        super().__init__(
            role=role,
            config=config,
            **kwargs
        )
        # LiveWorld always uses per-frame timestep
        self.uniform_timestep = False

    def load_caus_or_bir_basemodel(self, **kwargs):
        wan_config_dict = kwargs["wan_config_dict"]
        self.model = StateAdapterWanModel(**wan_config_dict, **kwargs)
        self.model.eval()


    def forward(
        self,
        noisy_image_or_video,
        timestep: torch.Tensor,
        context,
        clip_fea=None,
        y=None,
        sp_context=None,
        sp_context_scale=1.0,
        sp_hint_offset=0,
        kv_cache: Optional[List[dict]] = None,
        crossattn_cache: Optional[List[dict]] = None,
        kv_start: Optional[int] = None,
        kv_end: Optional[int] = None,
        rope_start: Optional[int] = None,
        y_camera=None,
        **kwargs
    ) -> torch.Tensor:
        # [B, F] -> [B]
        if self.uniform_timestep:
            input_timestep = timestep[:, 0]
        else:
            input_timestep = timestep

        # Calculate seq_len based on actual input shape (may include R+P+T frames for LiveWorld)
        # noisy_image_or_video: [B, F, C, H, W]
        actual_frames = noisy_image_or_video.shape[1]
        H, W = noisy_image_or_video.shape[-2:]
        actual_seq_len = actual_frames * (H // 2) * (W // 2)  # after patch embedding with stride (1,2,2)

        # sp_context is already [C, T, H, W] format from task layer
        if kv_cache is not None:
            # dzc: For training Self-Forcing; for infering DMD, Self-Forcing
            flow_pred = self.model(
                x=noisy_image_or_video.permute(0, 2, 1, 3, 4),
                t=input_timestep,
                context=context,
                clip_fea=clip_fea,
                y=list(y) if y is not None else None,  # y is [B, C, F, H, W], model expects list of [C, F, H, W]
                sp_context=sp_context,
                sp_context_scale=sp_context_scale,
                sp_hint_offset=sp_hint_offset,
                seq_len=actual_seq_len,
                kv_cache=kv_cache,
                crossattn_cache=crossattn_cache,
                kv_start=kv_start,
                kv_end=kv_end,
                rope_start=rope_start,
                **kwargs
            ).permute(0, 2, 1, 3, 4)
        else:
            flow_pred = self.model(
                x=noisy_image_or_video.permute(0, 2, 1, 3, 4),
                t=input_timestep,
                context=context,
                clip_fea=clip_fea,
                y=list(y) if y is not None else None,  # y is [B, C, F, H, W], model expects list of [C, F, H, W]
                sp_context=sp_context,
                sp_context_scale=sp_context_scale,
                sp_hint_offset=sp_hint_offset,
                seq_len=actual_seq_len,
                y_camera=y_camera,
                **kwargs
            ).permute(0, 2, 1, 3, 4)

        pred_x0 = self._convert_flow_pred_to_x0(
            flow_pred=flow_pred.flatten(0, 1),
            xt=noisy_image_or_video.flatten(0, 1),
            timestep=timestep.flatten(0, 1)
        ).unflatten(0, flow_pred.shape[:2])

        return flow_pred, pred_x0