File size: 8,930 Bytes
7d421db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab23a40
 
7d421db
 
 
 
ab23a40
 
 
 
 
54c67a0
ab23a40
 
 
 
 
 
 
 
7d421db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54c67a0
7d421db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b3defc
7d421db
 
 
54c67a0
7d421db
 
 
 
 
 
ab23a40
 
7d421db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import warnings
from typing import Optional

import torch
from diffusers import DDIMScheduler, TextToVideoSDPipeline
from einops import rearrange
from torch import Tensor
from torch.nn.functional import interpolate
from tqdm import trange
import random

from MotionDirector_train import export_to_video, handle_memory_attention, load_primary_models, unet_and_text_g_c, freeze_models
from utils.lora_handler import LoraHandler
from utils.ddim_utils import ddim_inversion
from utils.lora import extract_lora_child_module
import imageio


def initialize_pipeline(
    model: str,
    device: str = "cuda",
    xformers: bool = True,
    sdp: bool = True,
    lora_path: str = "",
    lora_rank: int = 32,
    lora_scale: float = 1.0,
):
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")

        scheduler, tokenizer, text_encoder, vae, unet = load_primary_models(model)

    # Freeze any necessary models
    freeze_models([vae, text_encoder, unet])

    # Enable xformers if available
    handle_memory_attention(xformers, sdp, unet)

    lora_manager_temporal = LoraHandler(
        version="cloneofsimo",
        use_unet_lora=True,
        use_text_lora=False,
        save_for_webui=False,
        only_for_webui=False,
        unet_replace_modules=["TransformerTemporalModel"],
        text_encoder_replace_modules=None,
        lora_bias=None
    )

    unet_lora_params, unet_negation = lora_manager_temporal.add_lora_to_model(
        True, unet, lora_manager_temporal.unet_replace_modules, 0, lora_path, r=lora_rank, scale=lora_scale)

    unet.eval()
    text_encoder.eval()
    unet_and_text_g_c(unet, text_encoder, False, False)

    pipe = TextToVideoSDPipeline.from_pretrained(
        pretrained_model_name_or_path=model,
        scheduler=scheduler,
        tokenizer=tokenizer,
        text_encoder=text_encoder.to(device=device, dtype=torch.half),
        vae=vae.to(device=device, dtype=torch.half),
        unet=unet.to(device=device, dtype=torch.half),
    )
    pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)

    return pipe


def inverse_video(pipe, latents, num_steps):
    ddim_inv_scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
    ddim_inv_scheduler.set_timesteps(num_steps)

    ddim_inv_latent = ddim_inversion(
        pipe, ddim_inv_scheduler, video_latent=latents.to(pipe.device),
        num_inv_steps=num_steps, prompt="")[-1]
    return ddim_inv_latent


def prepare_input_latents(
    pipe: TextToVideoSDPipeline,
    batch_size: int,
    num_frames: int,
    height: int,
    width: int,
    latents_path:str,
    model_select: str,
    random_seed: int,
):
    # initialize with random gaussian noise
    scale = pipe.vae_scale_factor
    shape = (batch_size, pipe.unet.config.in_channels, num_frames, height // scale, width // scale)
    if random_seed > 1000:
        torch.manual_seed(random_seed)
    else:
        random_seed = random.randint(100, 10000000)
        torch.manual_seed(random_seed)
        print(f"random_seed: {random_seed}")
    if '1-' in model_select:
        noise_prior = 0.3
    elif '2-' in model_select:
        noise_prior = 0.5
    elif '3-' in model_select:
        noise_prior = 0.
    else:
        noise_prior = 0.
    if noise_prior > 0.:
        cached_latents = torch.load(latents_path)
        if 'inversion_noise' not in cached_latents:
            latents = inverse_video(pipe, cached_latents['latents'].unsqueeze(0), 50).squeeze(0)
        else:
            latents = torch.load(latents_path)['inversion_noise'].unsqueeze(0)
        if latents.shape[0] != batch_size:
            latents = latents.repeat(batch_size, 1, 1, 1, 1)
        if latents.shape != shape:
            latents = interpolate(rearrange(latents, "b c f h w -> (b f) c h w", b=batch_size), (height // scale, width // scale), mode='bilinear')
            latents = rearrange(latents, "(b f) c h w -> b c f h w", b=batch_size)
        noise = torch.randn_like(latents, dtype=torch.half)
        latents_base = noise
        latents = (noise_prior) ** 0.5 * latents + (1 - noise_prior) ** 0.5 * noise
    else:
        latents = torch.randn(shape, dtype=torch.half)
        latents_base = latents

    return latents, latents_base, random_seed


class MotionDirector():
    def __init__(self):
        self.version = "0.0.0"
        self.foundation_model_path = "./zeroscope_v2_576w/"
        self.lora_path = "./MotionDirector_pretrained/dolly_zoom_(hitchcockian_zoom)/checkpoint-default/temporal/lora"
        with torch.autocast("cuda", dtype=torch.half):
            self.pipe = initialize_pipeline(model=self.foundation_model_path, lora_path=self.lora_path, lora_scale=1)

    def reload_lora(self, lora_path):
        if lora_path != self.lora_path:
            self.lora_path = lora_path
            with torch.autocast("cuda", dtype=torch.half):
                self.pipe = initialize_pipeline(model=self.foundation_model_path, lora_path=self.lora_path)

    def __call__(self, model_select, text_pormpt, neg_text_pormpt, random_seed, steps, guidance_scale, baseline_select):
        model_select = str(model_select)
        out_name = f"./outputs/inference"
        out_name += f"{text_pormpt}".replace(' ', '_').replace(',', '').replace('.', '')

        model_select_type = model_select.split('--')[1].strip()
        model_select_type = model_select_type.lower().replace(' ', '_')

        lora_path = f"./MotionDirector_pretrained/{model_select_type}/checkpoint-default/temporal/lora"
        self.reload_lora(lora_path)
        latents_folder = f"./MotionDirector_pretrained/{model_select_type}/cached_latents"
        latents_path = f"{latents_folder}/{random.choice(os.listdir(latents_folder))}"
        assert os.path.exists(lora_path)
        device = "cuda"
        self.pipe.enable_vae_slicing()
        with torch.autocast(device, dtype=torch.half):
            # prepare input latents
            with torch.no_grad():
                init_latents, init_latents_base, random_seed = prepare_input_latents(
                    pipe=self.pipe,
                    batch_size=1,
                    num_frames=16,
                    height=384,
                    width=384,
                    latents_path=latents_path,
                    model_select=model_select,
                    random_seed=random_seed
                )
                video_frames = self.pipe(
                    prompt=text_pormpt,
                    negative_prompt=neg_text_pormpt,
                    width=384,
                    height=384,
                    num_frames=16,
                    num_inference_steps=steps,
                    guidance_scale=guidance_scale,
                    latents=init_latents
                ).frames

                out_file = f"{out_name}_{random_seed}.mp4"
                os.makedirs(os.path.dirname(out_file), exist_ok=True)
                export_to_video(video_frames, out_file, 8)

                if baseline_select:
                    with torch.autocast("cuda", dtype=torch.half):

                        loras = extract_lora_child_module(self.pipe.unet, target_replace_module=["TransformerTemporalModel"])
                        for lora_i in loras:
                            lora_i.scale = 0.

                        # self.pipe = initialize_pipeline(model=self.foundation_model_path, lora_path=self.lora_path,
                        #                                 lora_scale=0.)
                        with torch.no_grad():
                            video_frames = self.pipe(
                                prompt=text_pormpt,
                                negative_prompt=neg_text_pormpt,
                                width=384,
                                height=384,
                                num_frames=16,
                                num_inference_steps=steps,
                                guidance_scale=guidance_scale,
                                latents=init_latents_base,
                            ).frames

                            out_file_baseline = f"{out_name}_{random_seed}_baseline.mp4"
                            os.makedirs(os.path.dirname(out_file_baseline), exist_ok=True)
                            export_to_video(video_frames, out_file_baseline, 8)
                    # with torch.autocast("cuda", dtype=torch.half):
                    #     self.pipe = initialize_pipeline(model=self.foundation_model_path, lora_path=self.lora_path,
                    #                                     lora_scale=1.)
                    loras = extract_lora_child_module(self.pipe.unet,
                                                      target_replace_module=["TransformerTemporalModel"])
                    for lora_i in loras:
                        lora_i.scale = 1.

                else:
                    out_file_baseline = None

        return [out_file, out_file_baseline]