scheduler.py 3.15 KB
Newer Older
1
import math
PengGao's avatar
PengGao committed
2
from typing import Union
PengGao's avatar
PengGao committed
3

4
import torch
PengGao's avatar
PengGao committed
5

6
7
8
from lightx2v.models.schedulers.wan.scheduler import WanScheduler


9
class WanStepDistillScheduler(WanScheduler):
10
11
    def __init__(self, config):
        super().__init__(config)
12
        self.denoising_step_list = config["denoising_step_list"]
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
13
        self.infer_steps = len(self.denoising_step_list)
14
        self.sample_shift = self.config["sample_shift"]
15

16
17
18
19
        self.num_train_timesteps = 1000
        self.sigma_max = 1.0
        self.sigma_min = 0.0

20
21
    def prepare(self, seed, latent_shape, image_encoder_output=None):
        self.prepare_latents(seed, latent_shape, dtype=torch.float32)
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
22
        self.set_denoising_timesteps(device=self.device)
23
24

    def set_denoising_timesteps(self, device: Union[str, torch.device] = None):
25
26
27
28
        sigma_start = self.sigma_min + (self.sigma_max - self.sigma_min)
        self.sigmas = torch.linspace(sigma_start, self.sigma_min, self.num_train_timesteps + 1)[:-1]
        self.sigmas = self.sample_shift * self.sigmas / (1 + (self.sample_shift - 1) * self.sigmas)
        self.timesteps = self.sigmas * self.num_train_timesteps
29

30
31
32
        self.denoising_step_index = [self.num_train_timesteps - x for x in self.denoising_step_list]
        self.timesteps = self.timesteps[self.denoising_step_index].to(device)
        self.sigmas = self.sigmas[self.denoising_step_index].to("cpu")
33

34
35
    def reset(self, seed, latent_shape, step_index=None):
        self.prepare_latents(seed, latent_shape, dtype=torch.float32)
36
37
38
39
40
41
42
43
44
45

    def add_noise(self, original_samples, noise, sigma):
        sample = (1 - sigma) * original_samples + sigma * noise
        return sample.type_as(noise)

    def step_post(self):
        flow_pred = self.noise_pred.to(torch.float32)
        sigma = self.sigmas[self.step_index].item()
        noisy_image_or_video = self.latents.to(torch.float32) - sigma * flow_pred
        if self.step_index < self.infer_steps - 1:
Zhuguanyu Wu's avatar
Zhuguanyu Wu committed
46
47
            sigma_n = self.sigmas[self.step_index + 1].item()
            noisy_image_or_video = noisy_image_or_video + flow_pred * sigma_n
48
        self.latents = noisy_image_or_video.to(self.latents.dtype)
49
50
51
52
53


class Wan22StepDistillScheduler(WanStepDistillScheduler):
    def __init__(self, config):
        super().__init__(config)
54
        self.boundary_step_index = config["boundary_step_index"]
55
56
57

    def set_denoising_timesteps(self, device: Union[str, torch.device] = None):
        super().set_denoising_timesteps(device)
58
59
60
61
62
63
        self.sigma_bound = self.sigmas[self.boundary_step_index].item()

    def calculate_alpha_beta_high(self, sigma):
        alpha = (1 - sigma) / (1 - self.sigma_bound)
        beta = math.sqrt(sigma**2 - (alpha * self.sigma_bound) ** 2)
        return alpha, beta
64
65
66
67

    def step_post(self):
        flow_pred = self.noise_pred.to(torch.float32)
        sigma = self.sigmas[self.step_index].item()
gushiqiao's avatar
gushiqiao committed
68
        noisy_image_or_video = self.latents.to(torch.float32) - flow_pred * sigma
69
        # self.latent: x_t
gushiqiao's avatar
gushiqiao committed
70
71
72
73
        if self.step_index < self.infer_steps - 1:
            sigma_n = self.sigmas[self.step_index + 1].item()
            noisy_image_or_video = noisy_image_or_video + flow_pred * sigma_n

74
        self.latents = noisy_image_or_video.to(self.latents.dtype)