default_runner.py 17.2 KB
Newer Older
helloyongyang's avatar
helloyongyang committed
1
import gc
PengGao's avatar
PengGao committed
2

3
import requests
helloyongyang's avatar
helloyongyang committed
4
5
import torch
import torch.distributed as dist
helloyongyang's avatar
helloyongyang committed
6
import torchvision.transforms.functional as TF
PengGao's avatar
PengGao committed
7
8
9
from PIL import Image
from loguru import logger
from requests.exceptions import RequestException
PengGao's avatar
PengGao committed
10

yihuiwen's avatar
yihuiwen committed
11
from lightx2v.server.metrics import monitor_cli
helloyongyang's avatar
helloyongyang committed
12
from lightx2v.utils.envs import *
PengGao's avatar
PengGao committed
13
from lightx2v.utils.generate_task_id import generate_task_id
14
from lightx2v.utils.global_paras import CALIB
15
from lightx2v.utils.memory_profiler import peak_memory_decorator
16
from lightx2v.utils.profiler import *
helloyongyang's avatar
helloyongyang committed
17
from lightx2v.utils.utils import save_to_video, vae_to_comfyui_image
PengGao's avatar
PengGao committed
18

PengGao's avatar
PengGao committed
19
from .base_runner import BaseRunner
20
21


PengGao's avatar
PengGao committed
22
class DefaultRunner(BaseRunner):
helloyongyang's avatar
helloyongyang committed
23
    def __init__(self, config):
PengGao's avatar
PengGao committed
24
        super().__init__(config)
25
        self.has_prompt_enhancer = False
PengGao's avatar
PengGao committed
26
        self.progress_callback = None
27
        if self.config["task"] == "t2v" and self.config.get("sub_servers", {}).get("prompt_enhancer") is not None:
28
29
30
31
            self.has_prompt_enhancer = True
            if not self.check_sub_servers("prompt_enhancer"):
                self.has_prompt_enhancer = False
                logger.warning("No prompt enhancer server available, disable prompt enhancer.")
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
32
        if not self.has_prompt_enhancer:
33
            self.config["use_prompt_enhancer"] = False
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
34
        self.set_init_device()
Yang Yong(雍洋)'s avatar
Yang Yong(雍洋) committed
35
        self.init_scheduler()
36

37
    def init_modules(self):
gushiqiao's avatar
gushiqiao committed
38
        logger.info("Initializing runner modules...")
39
40
        if not self.config.get("lazy_load", False) and not self.config.get("unload_modules", False):
            self.load_model()
41
42
        elif self.config.get("lazy_load", False):
            assert self.config.get("cpu_offload", False)
Yang Yong(雍洋)'s avatar
Yang Yong(雍洋) committed
43
        self.model.set_scheduler(self.scheduler)  # set scheduler to model
44
45
        if self.config["task"] == "i2v":
            self.run_input_encoder = self._run_input_encoder_local_i2v
gushiqiao's avatar
gushiqiao committed
46
47
48
        elif self.config["task"] == "flf2v":
            self.run_input_encoder = self._run_input_encoder_local_flf2v
        elif self.config["task"] == "t2v":
49
            self.run_input_encoder = self._run_input_encoder_local_t2v
gushiqiao's avatar
gushiqiao committed
50
51
        elif self.config["task"] == "vace":
            self.run_input_encoder = self._run_input_encoder_local_vace
52
53
        elif self.config["task"] == "animate":
            self.run_input_encoder = self._run_input_encoder_local_animate
54
55
56
        elif self.config["task"] == "s2v":
            self.run_input_encoder = self._run_input_encoder_local_s2v
        self.config.lock()  # lock config to avoid modification
Yang Yong(雍洋)'s avatar
Yang Yong(雍洋) committed
57
58
59
        if self.config.get("compile", False):
            logger.info(f"[Compile] Compile all shapes: {self.config.get('compile_shapes', [])}")
            self.model.compile(self.config.get("compile_shapes", []))
60

61
    def set_init_device(self):
62
        if self.config["cpu_offload"]:
63
            self.init_device = torch.device("cpu")
64
        else:
Kane's avatar
Kane committed
65
            self.init_device = torch.device(self.config.get("run_device", "cuda"))
66

PengGao's avatar
PengGao committed
67
68
69
70
71
72
73
    def load_vfi_model(self):
        if self.config["video_frame_interpolation"].get("algo", None) == "rife":
            from lightx2v.models.vfi.rife.rife_comfyui_wrapper import RIFEWrapper

            logger.info("Loading RIFE model...")
            return RIFEWrapper(self.config["video_frame_interpolation"]["model_path"])
        else:
74
            raise ValueError(f"Unsupported VFI model: {self.config['video_frame_interpolation']['algo']}")
PengGao's avatar
PengGao committed
75

76
77
78
79
80
81
82
83
84
    def load_vsr_model(self):
        if "video_super_resolution" in self.config:
            from lightx2v.models.runners.vsr.vsr_wrapper import VSRWrapper

            logger.info("Loading VSR model...")
            return VSRWrapper(self.config["video_super_resolution"]["model_path"])
        else:
            return None

85
    @ProfilingContext4DebugL2("Load models")
86
    def load_model(self):
87
88
89
90
        self.model = self.load_transformer()
        self.text_encoders = self.load_text_encoder()
        self.image_encoder = self.load_image_encoder()
        self.vae_encoder, self.vae_decoder = self.load_vae()
PengGao's avatar
PengGao committed
91
        self.vfi_model = self.load_vfi_model() if "video_frame_interpolation" in self.config else None
92
        self.vsr_model = self.load_vsr_model() if "video_super_resolution" in self.config else None
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
93

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    def check_sub_servers(self, task_type):
        urls = self.config.get("sub_servers", {}).get(task_type, [])
        available_servers = []
        for url in urls:
            try:
                status_url = f"{url}/v1/local/{task_type}/generate/service_status"
                response = requests.get(status_url, timeout=2)
                if response.status_code == 200:
                    available_servers.append(url)
                else:
                    logger.warning(f"Service {url} returned status code {response.status_code}")

            except RequestException as e:
                logger.warning(f"Failed to connect to {url}: {str(e)}")
                continue
        logger.info(f"{task_type} available servers: {available_servers}")
        self.config["sub_servers"][task_type] = available_servers
        return len(available_servers) > 0

helloyongyang's avatar
helloyongyang committed
113
    def set_inputs(self, inputs):
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        self.input_info.seed = inputs.get("seed", 42)
        self.input_info.prompt = inputs.get("prompt", "")
        if self.config["use_prompt_enhancer"]:
            self.input_info.prompt_enhanced = inputs.get("prompt_enhanced", "")
        self.input_info.negative_prompt = inputs.get("negative_prompt", "")
        if "image_path" in self.input_info.__dataclass_fields__:
            self.input_info.image_path = inputs.get("image_path", "")
        if "audio_path" in self.input_info.__dataclass_fields__:
            self.input_info.audio_path = inputs.get("audio_path", "")
        if "video_path" in self.input_info.__dataclass_fields__:
            self.input_info.video_path = inputs.get("video_path", "")
        self.input_info.save_result_path = inputs.get("save_result_path", "")

    def set_config(self, config_modify):
        logger.info(f"modify config: {config_modify}")
        with self.config.temporarily_unlocked():
            self.config.update(config_modify)
helloyongyang's avatar
helloyongyang committed
131

PengGao's avatar
PengGao committed
132
133
134
    def set_progress_callback(self, callback):
        self.progress_callback = callback

135
    @peak_memory_decorator
helloyongyang's avatar
helloyongyang committed
136
    def run_segment(self, total_steps=None):
helloyongyang's avatar
helloyongyang committed
137
138
        if total_steps is None:
            total_steps = self.model.scheduler.infer_steps
PengGao's avatar
PengGao committed
139
        for step_index in range(total_steps):
LiangLiu's avatar
LiangLiu committed
140
            # only for single segment, check stop signal every step
yihuiwen's avatar
yihuiwen committed
141
142
143
144
145
146
147
148
149
            with ProfilingContext4DebugL1(
                f"Run Dit every step",
                recorder_mode=GET_RECORDER_MODE(),
                metrics_func=monitor_cli.lightx2v_run_per_step_dit_duration,
                metrics_labels=[step_index + 1, total_steps],
            ):
                if self.video_segment_num == 1:
                    self.check_stop()
                logger.info(f"==> step_index: {step_index + 1} / {total_steps}")
150

yihuiwen's avatar
yihuiwen committed
151
152
                with ProfilingContext4DebugL1("step_pre"):
                    self.model.scheduler.step_pre(step_index=step_index)
153

yihuiwen's avatar
yihuiwen committed
154
155
                with ProfilingContext4DebugL1("🚀 infer_main"):
                    self.model.infer(self.inputs)
156

yihuiwen's avatar
yihuiwen committed
157
158
                with ProfilingContext4DebugL1("step_post"):
                    self.model.scheduler.step_post()
159

yihuiwen's avatar
yihuiwen committed
160
161
                if self.progress_callback:
                    self.progress_callback(((step_index + 1) / total_steps) * 100, 100)
PengGao's avatar
PengGao committed
162

helloyongyang's avatar
helloyongyang committed
163
        return self.model.scheduler.latents
164

helloyongyang's avatar
helloyongyang committed
165
    def run_step(self):
166
        self.inputs = self.run_input_encoder()
helloyongyang's avatar
helloyongyang committed
167
        self.run_main(total_steps=1)
helloyongyang's avatar
helloyongyang committed
168
169

    def end_run(self):
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
170
        self.model.scheduler.clear()
LiangLiu's avatar
LiangLiu committed
171
        del self.inputs
172
        self.input_info = None
gushiqiao's avatar
gushiqiao committed
173
174
175
176
177
178
        if self.config.get("lazy_load", False) or self.config.get("unload_modules", False):
            if hasattr(self.model.transformer_infer, "weights_stream_mgr"):
                self.model.transformer_infer.weights_stream_mgr.clear()
            if hasattr(self.model.transformer_weights, "clear"):
                self.model.transformer_weights.clear()
            self.model.pre_weight.clear()
179
            del self.model
180
181
182
183
        if self.config.get("do_mm_calib", False):
            calib_path = os.path.join(os.getcwd(), "calib.pt")
            torch.save(CALIB, calib_path)
            logger.info(f"[CALIB] Saved calibration data successfully to: {calib_path}")
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
184
        torch.cuda.empty_cache()
185
        gc.collect()
helloyongyang's avatar
helloyongyang committed
186

helloyongyang's avatar
helloyongyang committed
187
    def read_image_input(self, img_path):
LiangLiu's avatar
LiangLiu committed
188
189
190
191
        if isinstance(img_path, Image.Image):
            img_ori = img_path
        else:
            img_ori = Image.open(img_path).convert("RGB")
yihuiwen's avatar
yihuiwen committed
192
193
        if GET_RECORDER_MODE():
            width, height = img_ori.size
yihuiwen's avatar
yihuiwen committed
194
            monitor_cli.lightx2v_input_image_len.observe(width * height)
195
        img = TF.to_tensor(img_ori).sub_(0.5).div_(0.5).unsqueeze(0).cuda()
196
        self.input_info.original_size = img_ori.size
197
        return img, img_ori
helloyongyang's avatar
helloyongyang committed
198

199
    @ProfilingContext4DebugL2("Run Encoders")
PengGao's avatar
PengGao committed
200
    def _run_input_encoder_local_i2v(self):
201
        img, img_ori = self.read_image_input(self.input_info.image_path)
helloyongyang's avatar
helloyongyang committed
202
        clip_encoder_out = self.run_image_encoder(img) if self.config.get("use_image_encoder", True) else None
203
204
205
        vae_encode_out, latent_shape = self.run_vae_encoder(img_ori if self.vae_encoder_need_img_original else img)
        self.input_info.latent_shape = latent_shape  # Important: set latent_shape in input_info
        text_encoder_output = self.run_text_encoder(self.input_info)
206
207
        torch.cuda.empty_cache()
        gc.collect()
208
209
        return self.get_encoder_output_i2v(clip_encoder_out, vae_encode_out, text_encoder_output, img)

210
    @ProfilingContext4DebugL2("Run Encoders")
PengGao's avatar
PengGao committed
211
    def _run_input_encoder_local_t2v(self):
212
213
        self.input_info.latent_shape = self.get_latent_shape_with_target_hw(self.config["target_height"], self.config["target_width"])  # Important: set latent_shape in input_info
        text_encoder_output = self.run_text_encoder(self.input_info)
214
215
        torch.cuda.empty_cache()
        gc.collect()
216
217
218
219
        return {
            "text_encoder_output": text_encoder_output,
            "image_encoder_output": None,
        }
220

221
    @ProfilingContext4DebugL2("Run Encoders")
gushiqiao's avatar
gushiqiao committed
222
    def _run_input_encoder_local_flf2v(self):
223
224
        first_frame, _ = self.read_image_input(self.input_info.image_path)
        last_frame, _ = self.read_image_input(self.input_info.last_frame_path)
gushiqiao's avatar
gushiqiao committed
225
        clip_encoder_out = self.run_image_encoder(first_frame, last_frame) if self.config.get("use_image_encoder", True) else None
226
227
228
        vae_encode_out, latent_shape = self.run_vae_encoder(first_frame, last_frame)
        self.input_info.latent_shape = latent_shape  # Important: set latent_shape in input_info
        text_encoder_output = self.run_text_encoder(self.input_info)
gushiqiao's avatar
gushiqiao committed
229
230
231
232
        torch.cuda.empty_cache()
        gc.collect()
        return self.get_encoder_output_i2v(clip_encoder_out, vae_encode_out, text_encoder_output)

233
    @ProfilingContext4DebugL2("Run Encoders")
gushiqiao's avatar
gushiqiao committed
234
    def _run_input_encoder_local_vace(self):
235
236
237
        src_video = self.input_info.src_video
        src_mask = self.input_info.src_mask
        src_ref_images = self.input_info.src_ref_images
gushiqiao's avatar
gushiqiao committed
238
239
240
241
        src_video, src_mask, src_ref_images = self.prepare_source(
            [src_video],
            [src_mask],
            [None if src_ref_images is None else src_ref_images.split(",")],
242
            (self.config["target_width"], self.config["target_height"]),
gushiqiao's avatar
gushiqiao committed
243
244
245
        )
        self.src_ref_images = src_ref_images

246
247
248
        vae_encoder_out, latent_shape = self.run_vae_encoder(src_video, src_ref_images, src_mask)
        self.input_info.latent_shape = latent_shape  # Important: set latent_shape in input_info
        text_encoder_output = self.run_text_encoder(self.input_info)
gushiqiao's avatar
gushiqiao committed
249
250
251
252
        torch.cuda.empty_cache()
        gc.collect()
        return self.get_encoder_output_i2v(None, vae_encoder_out, text_encoder_output)

253
254
    @ProfilingContext4DebugL2("Run Text Encoder")
    def _run_input_encoder_local_animate(self):
255
        text_encoder_output = self.run_text_encoder(self.input_info)
256
257
258
259
        torch.cuda.empty_cache()
        gc.collect()
        return self.get_encoder_output_i2v(None, None, text_encoder_output, None)

260
261
262
    def _run_input_encoder_local_s2v(self):
        pass

helloyongyang's avatar
helloyongyang committed
263
    def init_run(self):
264
        self.gen_video_final = None
helloyongyang's avatar
helloyongyang committed
265
        self.get_video_segment_num()
266

gushiqiao's avatar
gushiqiao committed
267
        if self.config.get("lazy_load", False) or self.config.get("unload_modules", False):
268
            self.model = self.load_transformer()
269
270
271

        self.model.scheduler.prepare(seed=self.input_info.seed, latent_shape=self.input_info.latent_shape, image_encoder_output=self.inputs["image_encoder_output"])
        if self.config.get("model_cls") == "wan2.2" and self.config["task"] in ["i2v", "s2v"]:
272
            self.inputs["image_encoder_output"]["vae_encoder_out"] = None
helloyongyang's avatar
helloyongyang committed
273

274
    @ProfilingContext4DebugL2("Run DiT")
helloyongyang's avatar
helloyongyang committed
275
276
    def run_main(self, total_steps=None):
        self.init_run()
Yang Yong(雍洋)'s avatar
Yang Yong(雍洋) committed
277
        if self.config.get("compile", False):
278
            self.model.select_graph_for_compile(self.input_info)
helloyongyang's avatar
helloyongyang committed
279
        for segment_idx in range(self.video_segment_num):
280
            logger.info(f"🔄 start segment {segment_idx + 1}/{self.video_segment_num}")
yihuiwen's avatar
yihuiwen committed
281
282
283
            with ProfilingContext4DebugL1(
                f"segment end2end {segment_idx + 1}/{self.video_segment_num}",
                recorder_mode=GET_RECORDER_MODE(),
yihuiwen's avatar
yihuiwen committed
284
285
                metrics_func=monitor_cli.lightx2v_run_segments_end2end_duration,
                metrics_labels=["DefaultRunner"],
yihuiwen's avatar
yihuiwen committed
286
            ):
LiangLiu's avatar
LiangLiu committed
287
                self.check_stop()
Yang Yong(雍洋)'s avatar
Yang Yong(雍洋) committed
288
289
290
                # 1. default do nothing
                self.init_run_segment(segment_idx)
                # 2. main inference loop
helloyongyang's avatar
helloyongyang committed
291
                latents = self.run_segment(total_steps=total_steps)
Yang Yong(雍洋)'s avatar
Yang Yong(雍洋) committed
292
293
294
                # 3. vae decoder
                self.gen_video = self.run_vae_decoder(latents)
                # 4. default do nothing
295
                self.end_run_segment(segment_idx)
296
        gen_video_final = self.process_images_after_vae_decoder()
297
        self.end_run()
298
        return gen_video_final
299

yihuiwen's avatar
yihuiwen committed
300
    @ProfilingContext4DebugL1("Run VAE Decoder", recorder_mode=GET_RECORDER_MODE(), metrics_func=monitor_cli.lightx2v_run_vae_decode_duration, metrics_labels=["DefaultRunner"])
gushiqiao's avatar
gushiqiao committed
301
    def run_vae_decoder(self, latents):
gushiqiao's avatar
gushiqiao committed
302
        if self.config.get("lazy_load", False) or self.config.get("unload_modules", False):
303
            self.vae_decoder = self.load_vae_decoder()
304
        images = self.vae_decoder.decode(latents.to(GET_DTYPE()))
gushiqiao's avatar
gushiqiao committed
305
        if self.config.get("lazy_load", False) or self.config.get("unload_modules", False):
gushiqiao's avatar
gushiqiao committed
306
            del self.vae_decoder
307
308
            torch.cuda.empty_cache()
            gc.collect()
helloyongyang's avatar
helloyongyang committed
309
310
        return images

311
312
313
314
315
    def post_prompt_enhancer(self):
        while True:
            for url in self.config["sub_servers"]["prompt_enhancer"]:
                response = requests.get(f"{url}/v1/local/prompt_enhancer/generate/service_status").json()
                if response["service_status"] == "idle":
316
317
318
319
320
321
322
                    response = requests.post(
                        f"{url}/v1/local/prompt_enhancer/generate",
                        json={
                            "task_id": generate_task_id(),
                            "prompt": self.config["prompt"],
                        },
                    )
323
324
325
326
                    enhanced_prompt = response.json()["output"]
                    logger.info(f"Enhanced prompt: {enhanced_prompt}")
                    return enhanced_prompt

327
328
    def process_images_after_vae_decoder(self):
        self.gen_video_final = vae_to_comfyui_image(self.gen_video_final)
PengGao's avatar
PengGao committed
329
330
331
332
333

        if "video_frame_interpolation" in self.config:
            assert self.vfi_model is not None and self.config["video_frame_interpolation"].get("target_fps", None) is not None
            target_fps = self.config["video_frame_interpolation"]["target_fps"]
            logger.info(f"Interpolating frames from {self.config.get('fps', 16)} to {target_fps}")
334
335
            self.gen_video_final = self.vfi_model.interpolate_frames(
                self.gen_video_final,
PengGao's avatar
PengGao committed
336
337
338
                source_fps=self.config.get("fps", 16),
                target_fps=target_fps,
            )
PengGao's avatar
PengGao committed
339

340
341
342
        if self.input_info.return_result_tensor:
            return {"video": self.gen_video_final}
        elif self.input_info.save_result_path is not None:
PengGao's avatar
PengGao committed
343
344
345
346
            if "video_frame_interpolation" in self.config and self.config["video_frame_interpolation"].get("target_fps"):
                fps = self.config["video_frame_interpolation"]["target_fps"]
            else:
                fps = self.config.get("fps", 16)
helloyongyang's avatar
helloyongyang committed
347

348
            if not dist.is_initialized() or dist.get_rank() == 0:
helloyongyang's avatar
helloyongyang committed
349
                logger.info(f"🎬 Start to save video 🎬")
350

351
352
353
354
                save_to_video(self.gen_video_final, self.input_info.save_result_path, fps=fps, method="ffmpeg")
                logger.info(f"✅ Video saved successfully to: {self.input_info.save_result_path} ✅")
            return {"video": None}

yihuiwen's avatar
yihuiwen committed
355
    @ProfilingContext4DebugL1("RUN pipeline", recorder_mode=GET_RECORDER_MODE(), metrics_func=monitor_cli.lightx2v_worker_request_duration, metrics_labels=["DefaultRunner"])
356
    def run_pipeline(self, input_info):
yihuiwen's avatar
yihuiwen committed
357
358
        if GET_RECORDER_MODE():
            monitor_cli.lightx2v_worker_request_count.inc()
359
        self.input_info = input_info
PengGao's avatar
PengGao committed
360

helloyongyang's avatar
helloyongyang committed
361
        if self.config["use_prompt_enhancer"]:
362
            self.input_info.prompt_enhanced = self.post_prompt_enhancer()
helloyongyang's avatar
helloyongyang committed
363
364
365

        self.inputs = self.run_input_encoder()

366
        gen_video_final = self.run_main()
PengGao's avatar
PengGao committed
367

yihuiwen's avatar
yihuiwen committed
368
369
        if GET_RECORDER_MODE():
            monitor_cli.lightx2v_worker_request_success.inc()
370
        return gen_video_final