wan_runner.py 9.83 KB
Newer Older
helloyongyang's avatar
helloyongyang committed
1
import os
2
import gc
helloyongyang's avatar
helloyongyang committed
3
4
5
6
7
8
9
import numpy as np
import torch
import torchvision.transforms.functional as TF
from PIL import Image
from lightx2v.utils.registry_factory import RUNNER_REGISTER
from lightx2v.models.runners.default_runner import DefaultRunner
from lightx2v.models.schedulers.wan.scheduler import WanScheduler
10
11
from lightx2v.models.schedulers.wan.feature_caching.scheduler import (
    WanSchedulerTeaCaching,
12
13
14
    WanSchedulerTaylorCaching,
    WanSchedulerAdaCaching,
    WanSchedulerCustomCaching,
15
)
helloyongyang's avatar
helloyongyang committed
16
17
18
19
20
21
from lightx2v.utils.profiler import ProfilingContext
from lightx2v.models.input_encoders.hf.t5.model import T5EncoderModel
from lightx2v.models.input_encoders.hf.xlm_roberta.model import CLIPModel
from lightx2v.models.networks.wan.model import WanModel
from lightx2v.models.networks.wan.lora_adapter import WanLoraWrapper
from lightx2v.models.video_encoders.hf.wan.vae import WanVAE
22
from lightx2v.models.video_encoders.hf.wan.vae_tiny import WanVAE_tiny
23
from lightx2v.utils.utils import cache_video
root's avatar
root committed
24
from loguru import logger
helloyongyang's avatar
helloyongyang committed
25
26
27
28
29
30
31


@RUNNER_REGISTER("wan2.1")
class WanRunner(DefaultRunner):
    def __init__(self, config):
        super().__init__(config)

32
33
34
35
36
37
    def load_transformer(self):
        model = WanModel(
            self.config.model_path,
            self.config,
            self.init_device,
        )
38
        if self.config.lora_path:
39
            assert not self.config.get("dit_quantized", False) or self.config.mm_config.get("weight_auto_quant", False)
40
            lora_wrapper = WanLoraWrapper(model)
GoatWu's avatar
GoatWu committed
41
42
43
44
            for lora_path in self.config.lora_path:
                lora_name = lora_wrapper.load_lora(lora_path)
                lora_wrapper.apply_lora(lora_name, self.config.strength_model)
                logger.info(f"Loaded LoRA: {lora_name}")
45
46
        return model

47
    def load_image_encoder(self):
helloyongyang's avatar
helloyongyang committed
48
        image_encoder = None
49
50
51
        if self.config.task == "i2v":
            image_encoder = CLIPModel(
                dtype=torch.float16,
52
                device=self.init_device,
53
54
55
56
                checkpoint_path=os.path.join(
                    self.config.model_path,
                    "models_clip_open-clip-xlm-roberta-large-vit-huge-14.pth",
                ),
57
58
59
                clip_quantized=self.config.get("clip_quantized", False),
                clip_quantized_ckpt=self.config.get("clip_quantized_ckpt", None),
                quant_scheme=self.config.get("clip_quant_scheme", None),
60
61
            )
        return image_encoder
helloyongyang's avatar
helloyongyang committed
62

63
    def load_text_encoder(self):
helloyongyang's avatar
helloyongyang committed
64
65
66
        text_encoder = T5EncoderModel(
            text_len=self.config["text_len"],
            dtype=torch.bfloat16,
67
            device=self.init_device,
helloyongyang's avatar
helloyongyang committed
68
69
70
            checkpoint_path=os.path.join(self.config.model_path, "models_t5_umt5-xxl-enc-bf16.pth"),
            tokenizer_path=os.path.join(self.config.model_path, "google/umt5-xxl"),
            shard_fn=None,
71
            cpu_offload=self.config.cpu_offload,
72
73
74
75
            offload_granularity=self.config.get("t5_offload_granularity", "model"),
            t5_quantized=self.config.get("t5_quantized", False),
            t5_quantized_ckpt=self.config.get("t5_quantized_ckpt", None),
            quant_scheme=self.config.get("t5_quant_scheme", None),
helloyongyang's avatar
helloyongyang committed
76
77
        )
        text_encoders = [text_encoder]
78
        return text_encoders
helloyongyang's avatar
helloyongyang committed
79

80
    def load_vae_encoder(self):
81
82
        vae_config = {
            "vae_pth": os.path.join(self.config.model_path, "Wan2.1_VAE.pth"),
83
            "device": self.init_device,
84
85
86
            "parallel": self.config.parallel_vae,
            "use_tiling": self.config.get("use_tiling_vae", False),
        }
87
88
89
90
91
92
93
94
95
96
97
98
99
        if self.config.task != "i2v":
            return None
        else:
            return WanVAE(**vae_config)

    def load_vae_decoder(self):
        vae_config = {
            "vae_pth": os.path.join(self.config.model_path, "Wan2.1_VAE.pth"),
            "device": self.init_device,
            "parallel": self.config.parallel_vae,
            "use_tiling": self.config.get("use_tiling_vae", False),
        }
        if self.config.get("tiny_vae", False):
100
            vae_decoder = WanVAE_tiny(
101
                vae_pth=self.config.tiny_vae_path,
102
                device=self.init_device,
103
            ).to("cuda")
104
        else:
105
            vae_decoder = WanVAE(**vae_config)
106
        return vae_decoder
helloyongyang's avatar
helloyongyang committed
107

108
    def load_vae(self):
gushiqiao's avatar
gushiqiao committed
109
110
111
112
113
114
        vae_encoder = self.load_vae_encoder()
        if vae_encoder is None or self.config.get("tiny_vae", False):
            vae_decoder = self.load_vae_decoder()
        else:
            vae_decoder = vae_encoder
        return vae_encoder, vae_decoder
helloyongyang's avatar
helloyongyang committed
115
116
117
118
119
120

    def init_scheduler(self):
        if self.config.feature_caching == "NoCaching":
            scheduler = WanScheduler(self.config)
        elif self.config.feature_caching == "Tea":
            scheduler = WanSchedulerTeaCaching(self.config)
Yang Yong(雍洋)'s avatar
Yang Yong(雍洋) committed
121
        elif self.config.feature_caching == "TaylorSeer":
122
123
124
125
126
            scheduler = WanSchedulerTaylorCaching(self.config)
        elif self.config.feature_caching == "Ada":
            scheduler = WanSchedulerAdaCaching(self.config)
        elif self.config.feature_caching == "Custom":
            scheduler = WanSchedulerCustomCaching(self.config)
helloyongyang's avatar
helloyongyang committed
127
128
129
130
        else:
            raise NotImplementedError(f"Unsupported feature_caching type: {self.config.feature_caching}")
        self.model.set_scheduler(scheduler)

131
    def run_text_encoder(self, text, img):
132
133
        if self.config.get("lazy_load", False):
            self.text_encoders = self.load_text_encoder()
helloyongyang's avatar
helloyongyang committed
134
        text_encoder_output = {}
135
136
137
        n_prompt = self.config.get("negative_prompt", "")
        context = self.text_encoders[0].infer([text])
        context_null = self.text_encoders[0].infer([n_prompt if n_prompt else ""])
138
139
140
141
        if self.config.get("lazy_load", False):
            del self.text_encoders[0]
            torch.cuda.empty_cache()
            gc.collect()
helloyongyang's avatar
helloyongyang committed
142
143
144
145
        text_encoder_output["context"] = context
        text_encoder_output["context_null"] = context_null
        return text_encoder_output

146
    def run_image_encoder(self, img):
147
148
        if self.config.get("lazy_load", False):
            self.image_encoder = self.load_image_encoder()
149
150
        img = TF.to_tensor(img).sub_(0.5).div_(0.5).cuda()
        clip_encoder_out = self.image_encoder.visual([img[:, None, :, :]], self.config).squeeze(0).to(torch.bfloat16)
151
152
153
154
        if self.config.get("lazy_load", False):
            del self.image_encoder
            torch.cuda.empty_cache()
            gc.collect()
155
156
157
158
        return clip_encoder_out

    def run_vae_encoder(self, img):
        kwargs = {}
helloyongyang's avatar
helloyongyang committed
159
160
161
        img = TF.to_tensor(img).sub_(0.5).div_(0.5).cuda()
        h, w = img.shape[1:]
        aspect_ratio = h / w
162
163
164
165
166
        max_area = self.config.target_height * self.config.target_width
        lat_h = round(np.sqrt(max_area * aspect_ratio) // self.config.vae_stride[1] // self.config.patch_size[1] * self.config.patch_size[1])
        lat_w = round(np.sqrt(max_area / aspect_ratio) // self.config.vae_stride[2] // self.config.patch_size[2] * self.config.patch_size[2])
        h = lat_h * self.config.vae_stride[1]
        w = lat_w * self.config.vae_stride[2]
helloyongyang's avatar
helloyongyang committed
167

168
169
        self.config.lat_h, kwargs["lat_h"] = lat_h, lat_h
        self.config.lat_w, kwargs["lat_w"] = lat_w, lat_w
helloyongyang's avatar
helloyongyang committed
170

171
172
173
174
175
176
177
        msk = torch.ones(
            1,
            self.config.target_video_length,
            lat_h,
            lat_w,
            device=torch.device("cuda"),
        )
helloyongyang's avatar
helloyongyang committed
178
179
180
181
        msk[:, 1:] = 0
        msk = torch.concat([torch.repeat_interleave(msk[:, 0:1], repeats=4, dim=1), msk[:, 1:]], dim=1)
        msk = msk.view(1, msk.shape[1] // 4, 4, lat_h, lat_w)
        msk = msk.transpose(1, 2)[0]
182
183
        if self.config.get("lazy_load", False):
            self.vae_encoder = self.load_vae_encoder()
184
        vae_encode_out = self.vae_encoder.encode(
185
186
187
188
            [
                torch.concat(
                    [
                        torch.nn.functional.interpolate(img[None].cpu(), size=(h, w), mode="bicubic").transpose(0, 1),
189
                        torch.zeros(3, self.config.target_video_length - 1, h, w),
190
191
192
193
                    ],
                    dim=1,
                ).cuda()
            ],
194
            self.config,
helloyongyang's avatar
helloyongyang committed
195
        )[0]
196
197
198
199
        if self.config.get("lazy_load", False):
            del self.vae_encoder
            torch.cuda.empty_cache()
            gc.collect()
helloyongyang's avatar
helloyongyang committed
200
        vae_encode_out = torch.concat([msk, vae_encode_out]).to(torch.bfloat16)
201
202
203
        return vae_encode_out, kwargs

    def get_encoder_output_i2v(self, clip_encoder_out, vae_encode_out, text_encoder_output, img):
204
205
206
207
208
209
210
211
        image_encoder_output = {
            "clip_encoder_out": clip_encoder_out,
            "vae_encode_out": vae_encode_out,
        }
        return {
            "text_encoder_output": text_encoder_output,
            "image_encoder_output": image_encoder_output,
        }
helloyongyang's avatar
helloyongyang committed
212
213

    def set_target_shape(self):
214
        ret = {}
215
        num_channels_latents = self.config.get("num_channels_latents", 16)
helloyongyang's avatar
helloyongyang committed
216
        if self.config.task == "i2v":
217
218
            self.config.target_shape = (
                num_channels_latents,
219
                (self.config.target_video_length - 1) // self.config.vae_stride[0] + 1,
220
221
222
                self.config.lat_h,
                self.config.lat_w,
            )
223
224
            ret["lat_h"] = self.config.lat_h
            ret["lat_w"] = self.config.lat_w
helloyongyang's avatar
helloyongyang committed
225
226
        elif self.config.task == "t2v":
            self.config.target_shape = (
227
                num_channels_latents,
228
                (self.config.target_video_length - 1) // self.config.vae_stride[0] + 1,
helloyongyang's avatar
helloyongyang committed
229
230
231
                int(self.config.target_height) // self.config.vae_stride[1],
                int(self.config.target_width) // self.config.vae_stride[2],
            )
232
233
234
235
        ret["target_shape"] = self.config.target_shape
        return ret

    def save_video_func(self, images):
236
237
238
239
240
241
242
243
        cache_video(
            tensor=images,
            save_file=self.config.save_video_path,
            fps=self.config.get("fps", 16),
            nrow=1,
            normalize=True,
            value_range=(-1, 1),
        )