post_infer.py 865 Bytes
Newer Older
helloyongyang's avatar
helloyongyang committed
1
import math
PengGao's avatar
PengGao committed
2

helloyongyang's avatar
helloyongyang committed
3
import torch
PengGao's avatar
PengGao committed
4

gushiqiao's avatar
gushiqiao committed
5
from lightx2v.utils.envs import *
helloyongyang's avatar
helloyongyang committed
6
7
8
9
10
11


class WanPostInfer:
    def __init__(self, config):
        self.out_dim = config["out_dim"]
        self.patch_size = (1, 2, 2)
gushiqiao's avatar
gushiqiao committed
12
        self.clean_cuda_cache = config.get("clean_cuda_cache", False)
helloyongyang's avatar
helloyongyang committed
13

14
15
16
    def set_scheduler(self, scheduler):
        self.scheduler = scheduler

Yang Yong(雍洋)'s avatar
Yang Yong(雍洋) committed
17
    @torch.no_grad()
gushiqiao's avatar
gushiqiao committed
18
    def infer(self, x, pre_infer_out):
19
        x = self.unpatchify(x, pre_infer_out.grid_sizes.tuple)
gushiqiao's avatar
gushiqiao committed
20
21
22
23

        if self.clean_cuda_cache:
            torch.cuda.empty_cache()

helloyongyang's avatar
helloyongyang committed
24
25
26
27
        return [u.float() for u in x]

    def unpatchify(self, x, grid_sizes):
        c = self.out_dim
28
29
30
31
        x = x[: math.prod(grid_sizes)].view(*grid_sizes, *self.patch_size, c)
        x = torch.einsum("fhwpqrc->cfphqwr", x)
        x = x.reshape(c, *[i * j for i, j in zip(grid_sizes, self.patch_size)])
        return [x]