wan_causvid_runner.py 5.69 KB
Newer Older
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
1
import gc
PengGao's avatar
PengGao committed
2

3
import torch
PengGao's avatar
PengGao committed
4
5
from loguru import logger

Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
6
from lightx2v.models.networks.wan.causvid_model import WanCausVidModel
7
from lightx2v.models.networks.wan.lora_adapter import WanLoraWrapper
PengGao's avatar
PengGao committed
8
9
10
from lightx2v.models.networks.wan.model import WanModel
from lightx2v.models.runners.wan.wan_runner import WanRunner
from lightx2v.models.schedulers.wan.step_distill.scheduler import WanStepDistillScheduler
11
from lightx2v.utils.envs import *
PengGao's avatar
PengGao committed
12
from lightx2v.utils.profiler import ProfilingContext4Debug
PengGao's avatar
PengGao committed
13
from lightx2v.utils.registry_factory import RUNNER_REGISTER
14
15


Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
16
17
@RUNNER_REGISTER("wan2.1_causvid")
class WanCausVidRunner(WanRunner):
18
19
    def __init__(self, config):
        super().__init__(config)
GoatWu's avatar
GoatWu committed
20
21
22
23
24
        self.num_frame_per_block = self.config.num_frame_per_block
        self.num_frames = self.config.num_frames
        self.frame_seq_length = self.config.frame_seq_length
        self.infer_blocks = self.config.num_blocks
        self.num_fragments = self.config.num_fragments
25

Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
26
    def load_transformer(self):
27
        if self.config.get("lora_configs") and self.config.lora_configs:
GoatWu's avatar
GoatWu committed
28
29
30
31
32
33
            model = WanModel(
                self.config.model_path,
                self.config,
                self.init_device,
            )
            lora_wrapper = WanLoraWrapper(model)
34
35
36
            for lora_config in self.config.lora_configs:
                lora_path = lora_config["path"]
                strength = lora_config.get("strength", 1.0)
GoatWu's avatar
GoatWu committed
37
                lora_name = lora_wrapper.load_lora(lora_path)
38
39
                lora_wrapper.apply_lora(lora_name, strength)
                logger.info(f"Loaded LoRA: {lora_name} with strength: {strength}")
GoatWu's avatar
GoatWu committed
40
41
42
        else:
            model = WanCausVidModel(self.config.model_path, self.config, self.init_device)
        return model
43

Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
44
45
46
47
48
    def set_inputs(self, inputs):
        super().set_inputs(inputs)
        self.config["num_fragments"] = inputs.get("num_fragments", 1)
        self.num_fragments = self.config["num_fragments"]

49
    def init_scheduler(self):
50
        scheduler = WanStepDistillScheduler(self.config)
51
52
53
54
        self.model.set_scheduler(scheduler)

    def set_target_shape(self):
        if self.config.task == "i2v":
wangshankun's avatar
wangshankun committed
55
56
57
58
59
            self.config.target_shape = (16, self.config.num_frame_per_block, self.config.lat_h, self.config.lat_w)
            # i2v需根据input shape重置frame_seq_length
            frame_seq_length = (self.config.lat_h // 2) * (self.config.lat_w // 2)
            self.model.transformer_infer.frame_seq_length = frame_seq_length
            self.frame_seq_length = frame_seq_length
60
61
62
63
64
65
66
67
68
        elif self.config.task == "t2v":
            self.config.target_shape = (
                16,
                self.config.num_frame_per_block,
                int(self.config.target_height) // self.config.vae_stride[1],
                int(self.config.target_width) // self.config.vae_stride[2],
            )

    def run(self):
69
70
        self.model.transformer_infer._init_kv_cache(dtype=GET_DTYPE(), device="cuda")
        self.model.transformer_infer._init_crossattn_cache(dtype=GET_DTYPE(), device="cuda")
71
72
73
74

        output_latents = torch.zeros(
            (self.model.config.target_shape[0], self.num_frames + (self.num_fragments - 1) * (self.num_frames - self.num_frame_per_block), *self.model.config.target_shape[2:]),
            device="cuda",
75
            dtype=GET_DTYPE(),
76
77
78
79
80
        )

        start_block_idx = 0

        for fragment_idx in range(self.num_fragments):
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
81
            logger.info(f"========> fragment_idx: {fragment_idx + 1} / {self.num_fragments}")
82
83
84
85
86

            kv_start = 0
            kv_end = kv_start + self.num_frame_per_block * self.frame_seq_length

            if fragment_idx > 0:
root's avatar
root committed
87
                logger.info("recompute the kv_cache ...")
88
89
90
91
                with ProfilingContext4Debug("step_pre"):
                    self.model.scheduler.latents = self.model.scheduler.last_sample
                    self.model.scheduler.step_pre(step_index=self.model.scheduler.infer_steps - 1)

helloyongyang's avatar
helloyongyang committed
92
                with ProfilingContext4Debug("🚀 infer_main"):
93
94
95
96
97
98
99
100
                    self.model.infer(self.inputs, kv_start, kv_end)

                kv_start += self.num_frame_per_block * self.frame_seq_length
                kv_end += self.num_frame_per_block * self.frame_seq_length

            infer_blocks = self.infer_blocks - (fragment_idx > 0)

            for block_idx in range(infer_blocks):
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
101
102
                logger.info(f"=====> block_idx: {block_idx + 1} / {infer_blocks}")
                logger.info(f"=====> kv_start: {kv_start}, kv_end: {kv_end}")
103
104
105
                self.model.scheduler.reset()

                for step_index in range(self.model.scheduler.infer_steps):
root's avatar
root committed
106
                    logger.info(f"==> step_index: {step_index + 1} / {self.model.scheduler.infer_steps}")
107
108
109
110

                    with ProfilingContext4Debug("step_pre"):
                        self.model.scheduler.step_pre(step_index=step_index)

helloyongyang's avatar
helloyongyang committed
111
                    with ProfilingContext4Debug("🚀 infer_main"):
112
113
114
115
116
117
118
119
120
121
122
123
                        self.model.infer(self.inputs, kv_start, kv_end)

                    with ProfilingContext4Debug("step_post"):
                        self.model.scheduler.step_post()

                kv_start += self.num_frame_per_block * self.frame_seq_length
                kv_end += self.num_frame_per_block * self.frame_seq_length

                output_latents[:, start_block_idx * self.num_frame_per_block : (start_block_idx + 1) * self.num_frame_per_block] = self.model.scheduler.latents
                start_block_idx += 1

        return output_latents, self.model.scheduler.generator
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
124
125
126
127
128
129

    def end_run(self):
        self.model.scheduler.clear()
        del self.inputs, self.model.scheduler, self.model.transformer_infer.kv_cache, self.model.transformer_infer.crossattn_cache
        gc.collect()
        torch.cuda.empty_cache()