Ji4chenLi commited on
Commit
9f200a2
1 Parent(s): b7b902b

initial test

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. app.py +225 -84
  2. checkpoints/unet_lora.pt +3 -0
  3. configs/inference_t2v_512_v2.0.yaml +77 -0
  4. lvdm/__pycache__/basics.cpython-311.pyc +0 -0
  5. lvdm/__pycache__/common.cpython-311.pyc +0 -0
  6. lvdm/__pycache__/distributions.cpython-311.pyc +0 -0
  7. lvdm/__pycache__/ema.cpython-311.pyc +0 -0
  8. lvdm/basics.py +102 -0
  9. lvdm/common.py +112 -0
  10. lvdm/distributions.py +103 -0
  11. lvdm/ema.py +84 -0
  12. lvdm/models/__pycache__/autoencoder.cpython-311.pyc +0 -0
  13. lvdm/models/__pycache__/ddpm3d.cpython-311.pyc +0 -0
  14. lvdm/models/__pycache__/utils_diffusion.cpython-311.pyc +0 -0
  15. lvdm/models/autoencoder.py +276 -0
  16. lvdm/models/ddpm3d.py +967 -0
  17. lvdm/models/samplers/__pycache__/ddim.cpython-311.pyc +0 -0
  18. lvdm/models/samplers/ddim.py +493 -0
  19. lvdm/models/utils_diffusion.py +130 -0
  20. lvdm/modules/__pycache__/attention.cpython-311.pyc +0 -0
  21. lvdm/modules/attention.py +584 -0
  22. lvdm/modules/encoders/__pycache__/condition.cpython-311.pyc +0 -0
  23. lvdm/modules/encoders/__pycache__/ip_resampler.cpython-311.pyc +0 -0
  24. lvdm/modules/encoders/condition.py +512 -0
  25. lvdm/modules/encoders/ip_resampler.py +148 -0
  26. lvdm/modules/networks/__pycache__/ae_modules.cpython-311.pyc +0 -0
  27. lvdm/modules/networks/__pycache__/openaimodel3d.cpython-311.pyc +0 -0
  28. lvdm/modules/networks/ae_modules.py +1025 -0
  29. lvdm/modules/networks/openaimodel3d.py +710 -0
  30. lvdm/modules/x_transformer.py +704 -0
  31. pipeline/__init__.py +0 -0
  32. pipeline/__pycache__/__init__.cpython-311.pyc +0 -0
  33. pipeline/__pycache__/model_scope_vlcm_pipeline.cpython-311.pyc +0 -0
  34. pipeline/__pycache__/t2v_turbo_ms_pipeline.cpython-311.pyc +0 -0
  35. pipeline/__pycache__/t2v_turbo_vc2_pipeline.cpython-311.pyc +0 -0
  36. pipeline/__pycache__/vlcm_pipeline.cpython-311.pyc +0 -0
  37. pipeline/t2v_turbo_ms_pipeline.py +221 -0
  38. pipeline/t2v_turbo_vc2_pipeline.py +214 -0
  39. requirements.txt +18 -6
  40. scheduler/__pycache__/t2v_turbo_scheduler.cpython-311.pyc +0 -0
  41. scheduler/__pycache__/vlcm_scheduler.cpython-311.pyc +0 -0
  42. scheduler/t2v_turbo_scheduler.py +518 -0
  43. style.css +16 -0
  44. utils/__init__.py +0 -0
  45. utils/__pycache__/__init__.cpython-311.pyc +0 -0
  46. utils/__pycache__/common_utils.cpython-311.pyc +0 -0
  47. utils/__pycache__/lora.cpython-311.pyc +0 -0
  48. utils/__pycache__/lora_handler.cpython-311.pyc +0 -0
  49. utils/__pycache__/utils.cpython-311.pyc +0 -0
  50. utils/common_utils.py +385 -0
app.py CHANGED
@@ -1,61 +1,190 @@
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- from diffusers import DiffusionPipeline
 
 
 
 
5
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
8
 
9
  if torch.cuda.is_available():
10
- torch.cuda.max_memory_allocated(device=device)
11
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
12
- pipe.enable_xformers_memory_efficient_attention()
13
- pipe = pipe.to(device)
14
- else:
15
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
16
- pipe = pipe.to(device)
17
 
18
- MAX_SEED = np.iinfo(np.int32).max
19
- MAX_IMAGE_SIZE = 1024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
22
 
 
23
  if randomize_seed:
24
  seed = random.randint(0, MAX_SEED)
25
-
26
- generator = torch.Generator().manual_seed(seed)
27
-
28
- image = pipe(
29
- prompt = prompt,
30
- negative_prompt = negative_prompt,
31
- guidance_scale = guidance_scale,
32
- num_inference_steps = num_inference_steps,
33
- width = width,
34
- height = height,
35
- generator = generator
36
- ).images[0]
37
-
38
- return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  examples = [
41
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
42
- "An astronaut riding a green horse",
43
- "A delicious ceviche cheesecake slice",
 
 
 
 
 
 
 
44
  ]
45
 
46
- css="""
47
- #col-container {
48
- margin: 0 auto;
49
- max-width: 520px;
50
- }
51
- """
52
 
53
  if torch.cuda.is_available():
54
  power_device = "GPU"
55
  else:
56
  power_device = "CPU"
57
 
58
- with gr.Blocks(css=css) as demo:
59
 
60
  with gr.Column(elem_id="col-container"):
61
  gr.Markdown(f"""
@@ -64,7 +193,6 @@ with gr.Blocks(css=css) as demo:
64
  """)
65
 
66
  with gr.Row():
67
-
68
  prompt = gr.Text(
69
  label="Prompt",
70
  show_label=False,
@@ -72,75 +200,88 @@ with gr.Blocks(css=css) as demo:
72
  placeholder="Enter your prompt",
73
  container=False,
74
  )
75
-
76
  run_button = gr.Button("Run", scale=0)
77
-
78
- result = gr.Image(label="Result", show_label=False)
 
79
 
80
  with gr.Accordion("Advanced Settings", open=False):
81
 
82
- negative_prompt = gr.Text(
83
- label="Negative prompt",
84
- max_lines=1,
85
- placeholder="Enter a negative prompt",
86
- visible=False,
87
- )
88
-
89
  seed = gr.Slider(
90
  label="Seed",
91
  minimum=0,
92
  maximum=MAX_SEED,
93
  step=1,
94
  value=0,
 
95
  )
96
-
97
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
-
99
- with gr.Row():
100
-
101
- width = gr.Slider(
102
- label="Width",
103
- minimum=256,
104
- maximum=MAX_IMAGE_SIZE,
105
- step=32,
106
- value=512,
107
- )
108
-
109
- height = gr.Slider(
110
- label="Height",
111
- minimum=256,
112
- maximum=MAX_IMAGE_SIZE,
113
- step=32,
114
- value=512,
115
- )
116
-
117
  with gr.Row():
118
-
119
  guidance_scale = gr.Slider(
120
- label="Guidance scale",
121
- minimum=0.0,
122
- maximum=10.0,
123
  step=0.1,
124
- value=0.0,
125
  )
126
-
127
  num_inference_steps = gr.Slider(
128
- label="Number of inference steps",
129
  minimum=1,
130
- maximum=12,
131
  step=1,
132
- value=2,
133
  )
134
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  gr.Examples(
136
- examples = examples,
137
- inputs = [prompt]
 
 
 
138
  )
139
 
140
- run_button.click(
141
- fn = infer,
142
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
143
- outputs = [result]
144
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  demo.queue().launch()
 
1
+ import os
2
+ import uuid
3
  import gradio as gr
4
  import numpy as np
5
  import random
6
+ import time
7
+ from omegaconf import OmegaConf
8
+
9
+ import spaces
10
+
11
  import torch
12
+ import torchvision
13
+
14
+ from concurrent.futures import ThreadPoolExecutor
15
+ import uuid
16
+
17
+ from utils.lora import collapse_lora, monkeypatch_remove_lora
18
+ from utils.lora_handler import LoraHandler
19
+ from utils.common_utils import load_model_checkpoint
20
+ from utils.utils import instantiate_from_config
21
+ from scheduler.t2v_turbo_scheduler import T2VTurboScheduler
22
+ from pipeline.t2v_turbo_vc2_pipeline import T2VTurboVC2Pipeline
23
+
24
 
25
  device = "cuda" if torch.cuda.is_available() else "cpu"
26
+ MAX_SEED = np.iinfo(np.int32).max
27
+ CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES") == "1"
28
+
29
 
30
  if torch.cuda.is_available():
31
+ config = OmegaConf.load("configs/inference_t2v_512_v2.0.yaml")
32
+ model_config = config.pop("model", OmegaConf.create())
33
+ pretrained_t2v = instantiate_from_config(model_config)
34
+ pretrained_t2v = load_model_checkpoint(pretrained_t2v, "checkpoints/vc2_model.ckpt")
 
 
 
35
 
36
+ unet_config = model_config["params"]["unet_config"]
37
+ unet_config["params"]["time_cond_proj_dim"] = 256
38
+ unet = instantiate_from_config(unet_config)
39
+
40
+ unet.load_state_dict(
41
+ pretrained_t2v.model.diffusion_model.state_dict(), strict=False
42
+ )
43
+
44
+ use_unet_lora = True
45
+ lora_manager = LoraHandler(
46
+ version="cloneofsimo",
47
+ use_unet_lora=use_unet_lora,
48
+ save_for_webui=True,
49
+ unet_replace_modules=["UNetModel"],
50
+ )
51
+ lora_manager.add_lora_to_model(
52
+ use_unet_lora,
53
+ unet,
54
+ lora_manager.unet_replace_modules,
55
+ lora_path="checkpoints/unet_lora.pt",
56
+ dropout=0.1,
57
+ r=64,
58
+ )
59
+ unet.eval()
60
+ collapse_lora(unet, lora_manager.unet_replace_modules)
61
+ monkeypatch_remove_lora(unet)
62
+
63
+ torch.save(unet.state_dict(), "checkpoints/merged_unet.pt")
64
+
65
+ pretrained_t2v.model.diffusion_model = unet
66
+ scheduler = T2VTurboScheduler(
67
+ linear_start=model_config["params"]["linear_start"],
68
+ linear_end=model_config["params"]["linear_end"],
69
+ )
70
+ pipeline = T2VTurboVC2Pipeline(pretrained_t2v, scheduler, model_config)
71
+
72
+ pipeline.to(device)
73
+ else:
74
+ assert False
75
 
 
76
 
77
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
78
  if randomize_seed:
79
  seed = random.randint(0, MAX_SEED)
80
+ return seed
81
+
82
+
83
+ def save_video(
84
+ vid_tensor, profile: gr.OAuthProfile | None, metadata: dict, root_path="./", fps=16
85
+ ):
86
+ unique_name = str(uuid.uuid4()) + ".mp4"
87
+ prefix = ""
88
+ for k, v in metadata.items():
89
+ prefix += f"{k}={v}_"
90
+ unique_name = prefix + unique_name
91
+ unique_name = os.path.join(root_path, unique_name)
92
+
93
+ video = vid_tensor.detach().cpu()
94
+ video = torch.clamp(video.float(), -1.0, 1.0)
95
+ video = video.permute(1, 0, 2, 3) # t,c,h,w
96
+ video = (video + 1.0) / 2.0
97
+ video = (video * 255).to(torch.uint8).permute(0, 2, 3, 1)
98
+
99
+ torchvision.io.write_video(
100
+ unique_name, video, fps=fps, video_codec="h264", options={"crf": "10"}
101
+ )
102
+ return unique_name
103
+
104
+
105
+ def save_videos(
106
+ video_array, profile: gr.OAuthProfile | None, metadata: dict, fps: int = 16
107
+ ):
108
+ paths = []
109
+ root_path = "./videos/"
110
+ os.makedirs(root_path, exist_ok=True)
111
+ with ThreadPoolExecutor() as executor:
112
+ paths = list(
113
+ executor.map(
114
+ save_video,
115
+ video_array,
116
+ [profile] * len(video_array),
117
+ [metadata] * len(video_array),
118
+ [root_path] * len(video_array),
119
+ [fps] * len(video_array),
120
+ )
121
+ )
122
+ return paths[0]
123
+
124
+
125
+ @spaces.GPU(duration=60)
126
+ def generate(
127
+ prompt: str,
128
+ seed: int = 0,
129
+ guidance_scale: float = 7.5,
130
+ num_inference_steps: int = 4,
131
+ num_frames: int = 16,
132
+ fps: int = 16,
133
+ randomize_seed: bool = False,
134
+ param_dtype="torch.float16",
135
+ progress=gr.Progress(track_tqdm=True),
136
+ profile: gr.OAuthProfile | None = None,
137
+ ):
138
+ seed = randomize_seed_fn(seed, randomize_seed)
139
+ torch.manual_seed(seed)
140
+ pipeline.to(
141
+ torch_device=device,
142
+ torch_dtype=torch.float16 if param_dtype == "torch.float16" else torch.float32,
143
+ )
144
+ start_time = time.time()
145
+
146
+ result = pipeline(
147
+ prompt=prompt,
148
+ frames=num_frames,
149
+ fps=fps,
150
+ guidance_scale=guidance_scale,
151
+ num_inference_steps=num_inference_steps,
152
+ num_videos_per_prompt=1,
153
+ )
154
+ paths = save_videos(
155
+ result,
156
+ profile,
157
+ metadata={
158
+ "prompt": prompt,
159
+ "seed": seed,
160
+ "guidance_scale": guidance_scale,
161
+ "num_inference_steps": num_inference_steps,
162
+ },
163
+ fps=fps,
164
+ )
165
+ print(time.time() - start_time)
166
+ return paths, seed
167
 
168
  examples = [
169
+ "An astronaut riding a horse.",
170
+ "Darth vader surfing in waves.",
171
+ "Robot dancing in times square.",
172
+ "Clown fish swimming through the coral reef.",
173
+ "Pikachu snowboarding.",
174
+ "With the style of van gogh, A young couple dances under the moonlight by the lake.",
175
+ "A young woman with glasses is jogging in the park wearing a pink headband.",
176
+ "Impressionist style, a yellow rubber duck floating on the wave on the sunset",
177
+ "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k",
178
+ "With the style of low-poly game art, A majestic, white horse gallops gracefully across a moonlit beach.",
179
  ]
180
 
 
 
 
 
 
 
181
 
182
  if torch.cuda.is_available():
183
  power_device = "GPU"
184
  else:
185
  power_device = "CPU"
186
 
187
+ with gr.Blocks(css="style.css") as demo:
188
 
189
  with gr.Column(elem_id="col-container"):
190
  gr.Markdown(f"""
 
193
  """)
194
 
195
  with gr.Row():
 
196
  prompt = gr.Text(
197
  label="Prompt",
198
  show_label=False,
 
200
  placeholder="Enter your prompt",
201
  container=False,
202
  )
 
203
  run_button = gr.Button("Run", scale=0)
204
+ result_video = gr.Video(
205
+ label="Generated Video", interactive=False, autoplay=True
206
+ )
207
 
208
  with gr.Accordion("Advanced Settings", open=False):
209
 
 
 
 
 
 
 
 
210
  seed = gr.Slider(
211
  label="Seed",
212
  minimum=0,
213
  maximum=MAX_SEED,
214
  step=1,
215
  value=0,
216
+ randomize=True,
217
  )
218
+ randomize_seed = gr.Checkbox(label="Randomize seed across runs", value=True)
219
+ dtype_choices = ["torch.float16", "torch.float32"]
220
+ param_dtype = gr.Radio(
221
+ dtype_choices,
222
+ label="torch.dtype",
223
+ value=dtype_choices[0],
224
+ interactive=True,
225
+ info="To save GPU memory, use torch.float16. For better quality, use torch.float32.",
226
+ )
227
+
 
 
 
 
 
 
 
 
 
 
 
228
  with gr.Row():
 
229
  guidance_scale = gr.Slider(
230
+ label="Guidance scale for base",
231
+ minimum=2,
232
+ maximum=14,
233
  step=0.1,
234
+ value=7.5,
235
  )
 
236
  num_inference_steps = gr.Slider(
237
+ label="Number of inference steps for base",
238
  minimum=1,
239
+ maximum=8,
240
  step=1,
241
+ value=4,
242
  )
243
+ with gr.Row():
244
+ num_frames = gr.Slider(
245
+ label="Number of Video Frames",
246
+ minimum=16,
247
+ maximum=48,
248
+ step=8,
249
+ value=16,
250
+ )
251
+ fps = gr.Slider(
252
+ label="FPS",
253
+ minimum=8,
254
+ maximum=32,
255
+ step=4,
256
+ value=16,
257
+ )
258
+
259
  gr.Examples(
260
+ examples=examples,
261
+ inputs=prompt,
262
+ outputs=result_video,
263
+ fn=generate,
264
+ cache_examples=CACHE_EXAMPLES,
265
  )
266
 
267
+ gr.on(
268
+ triggers=[
269
+ prompt.submit,
270
+ run_button.click,
271
+ ],
272
+ fn=generate,
273
+ inputs=[
274
+ prompt,
275
+ seed,
276
+ guidance_scale,
277
+ num_inference_steps,
278
+ num_frames,
279
+ fps,
280
+ randomize_seed,
281
+ param_dtype,
282
+ ],
283
+ outputs=[result_video, seed],
284
+ api_name="run",
285
+ )
286
 
287
  demo.queue().launch()
checkpoints/unet_lora.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f1b223501f3fbbd491a9797c37f224ba031520cb442a10c07e4613b70b203845
3
+ size 468885008
configs/inference_t2v_512_v2.0.yaml ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ model:
2
+ target: lvdm.models.ddpm3d.LatentDiffusion
3
+ params:
4
+ linear_start: 0.00085
5
+ linear_end: 0.012
6
+ num_timesteps_cond: 1
7
+ timesteps: 1000
8
+ first_stage_key: video
9
+ cond_stage_key: caption
10
+ cond_stage_trainable: false
11
+ conditioning_key: crossattn
12
+ image_size:
13
+ - 40
14
+ - 64
15
+ channels: 4
16
+ scale_by_std: false
17
+ scale_factor: 0.18215
18
+ use_ema: false
19
+ uncond_type: empty_seq
20
+ use_scale: true
21
+ scale_b: 0.7
22
+ unet_config:
23
+ target: lvdm.modules.networks.openaimodel3d.UNetModel
24
+ params:
25
+ in_channels: 4
26
+ out_channels: 4
27
+ model_channels: 320
28
+ attention_resolutions:
29
+ - 4
30
+ - 2
31
+ - 1
32
+ num_res_blocks: 2
33
+ channel_mult:
34
+ - 1
35
+ - 2
36
+ - 4
37
+ - 4
38
+ num_head_channels: 64
39
+ transformer_depth: 1
40
+ context_dim: 1024
41
+ use_linear: true
42
+ use_checkpoint: true
43
+ temporal_conv: true
44
+ temporal_attention: true
45
+ temporal_selfatt_only: true
46
+ use_relative_position: false
47
+ use_causal_attention: false
48
+ temporal_length: 16
49
+ addition_attention: true
50
+ fps_cond: true
51
+ first_stage_config:
52
+ target: lvdm.models.autoencoder.AutoencoderKL
53
+ params:
54
+ embed_dim: 4
55
+ monitor: val/rec_loss
56
+ ddconfig:
57
+ double_z: true
58
+ z_channels: 4
59
+ resolution: 512
60
+ in_channels: 3
61
+ out_ch: 3
62
+ ch: 128
63
+ ch_mult:
64
+ - 1
65
+ - 2
66
+ - 4
67
+ - 4
68
+ num_res_blocks: 2
69
+ attn_resolutions: []
70
+ dropout: 0.0
71
+ lossconfig:
72
+ target: torch.nn.Identity
73
+ cond_stage_config:
74
+ target: lvdm.modules.encoders.condition.FrozenOpenCLIPEmbedder
75
+ params:
76
+ freeze: true
77
+ layer: penultimate
lvdm/__pycache__/basics.cpython-311.pyc ADDED
Binary file (5.03 kB). View file
 
lvdm/__pycache__/common.cpython-311.pyc ADDED
Binary file (7.2 kB). View file
 
lvdm/__pycache__/distributions.cpython-311.pyc ADDED
Binary file (6.25 kB). View file
 
lvdm/__pycache__/ema.cpython-311.pyc ADDED
Binary file (5.49 kB). View file
 
lvdm/basics.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # adopted from
2
+ # https://github.com/openai/improved-diffusion/blob/main/improved_diffusion/gaussian_diffusion.py
3
+ # and
4
+ # https://github.com/lucidrains/denoising-diffusion-pytorch/blob/7706bdfc6f527f58d33f84b7b522e61e6e3164b3/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py
5
+ # and
6
+ # https://github.com/openai/guided-diffusion/blob/0ba878e517b276c45d1195eb29f6f5f72659a05b/guided_diffusion/nn.py
7
+ #
8
+ # thanks!
9
+
10
+ import torch.nn as nn
11
+ from utils.utils import instantiate_from_config
12
+
13
+
14
+ def disabled_train(self, mode=True):
15
+ """Overwrite model.train with this function to make sure train/eval mode
16
+ does not change anymore."""
17
+ return self
18
+
19
+
20
+ def zero_module(module):
21
+ """
22
+ Zero out the parameters of a module and return it.
23
+ """
24
+ for p in module.parameters():
25
+ p.detach().zero_()
26
+ return module
27
+
28
+
29
+ def scale_module(module, scale):
30
+ """
31
+ Scale the parameters of a module and return it.
32
+ """
33
+ for p in module.parameters():
34
+ p.detach().mul_(scale)
35
+ return module
36
+
37
+
38
+ def conv_nd(dims, *args, **kwargs):
39
+ """
40
+ Create a 1D, 2D, or 3D convolution module.
41
+ """
42
+ if dims == 1:
43
+ return nn.Conv1d(*args, **kwargs)
44
+ elif dims == 2:
45
+ return nn.Conv2d(*args, **kwargs)
46
+ elif dims == 3:
47
+ return nn.Conv3d(*args, **kwargs)
48
+ raise ValueError(f"unsupported dimensions: {dims}")
49
+
50
+
51
+ def linear(*args, **kwargs):
52
+ """
53
+ Create a linear module.
54
+ """
55
+ return nn.Linear(*args, **kwargs)
56
+
57
+
58
+ def avg_pool_nd(dims, *args, **kwargs):
59
+ """
60
+ Create a 1D, 2D, or 3D average pooling module.
61
+ """
62
+ if dims == 1:
63
+ return nn.AvgPool1d(*args, **kwargs)
64
+ elif dims == 2:
65
+ return nn.AvgPool2d(*args, **kwargs)
66
+ elif dims == 3:
67
+ return nn.AvgPool3d(*args, **kwargs)
68
+ raise ValueError(f"unsupported dimensions: {dims}")
69
+
70
+
71
+ def nonlinearity(type="silu"):
72
+ if type == "silu":
73
+ return nn.SiLU()
74
+ elif type == "leaky_relu":
75
+ return nn.LeakyReLU()
76
+
77
+
78
+ class GroupNormSpecific(nn.GroupNorm):
79
+ def forward(self, x):
80
+ return super().forward(x.float()).type(x.dtype)
81
+
82
+
83
+ def normalization(channels, num_groups=32):
84
+ """
85
+ Make a standard normalization layer.
86
+ :param channels: number of input channels.
87
+ :return: an nn.Module for normalization.
88
+ """
89
+ return GroupNormSpecific(num_groups, channels)
90
+
91
+
92
+ class HybridConditioner(nn.Module):
93
+
94
+ def __init__(self, c_concat_config, c_crossattn_config):
95
+ super().__init__()
96
+ self.concat_conditioner = instantiate_from_config(c_concat_config)
97
+ self.crossattn_conditioner = instantiate_from_config(c_crossattn_config)
98
+
99
+ def forward(self, c_concat, c_crossattn):
100
+ c_concat = self.concat_conditioner(c_concat)
101
+ c_crossattn = self.crossattn_conditioner(c_crossattn)
102
+ return {"c_concat": [c_concat], "c_crossattn": [c_crossattn]}
lvdm/common.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from inspect import isfunction
3
+ import torch
4
+ from torch import nn
5
+ import torch.distributed as dist
6
+
7
+
8
+ def gather_data(data, return_np=True):
9
+ """gather data from multiple processes to one list"""
10
+ data_list = [torch.zeros_like(data) for _ in range(dist.get_world_size())]
11
+ dist.all_gather(data_list, data) # gather not supported with NCCL
12
+ if return_np:
13
+ data_list = [data.cpu().numpy() for data in data_list]
14
+ return data_list
15
+
16
+
17
+ def autocast(f):
18
+ def do_autocast(*args, **kwargs):
19
+ with torch.cuda.amp.autocast(
20
+ enabled=True,
21
+ dtype=torch.get_autocast_gpu_dtype(),
22
+ cache_enabled=torch.is_autocast_cache_enabled(),
23
+ ):
24
+ return f(*args, **kwargs)
25
+
26
+ return do_autocast
27
+
28
+
29
+ def extract_into_tensor(a, t, x_shape):
30
+ b, *_ = t.shape
31
+ out = a.gather(-1, t)
32
+ return out.reshape(b, *((1,) * (len(x_shape) - 1)))
33
+
34
+
35
+ def noise_like(shape, device, repeat=False):
36
+ repeat_noise = lambda: torch.randn((1, *shape[1:]), device=device).repeat(
37
+ shape[0], *((1,) * (len(shape) - 1))
38
+ )
39
+ noise = lambda: torch.randn(shape, device=device)
40
+ return repeat_noise() if repeat else noise()
41
+
42
+
43
+ def default(val, d):
44
+ if exists(val):
45
+ return val
46
+ return d() if isfunction(d) else d
47
+
48
+
49
+ def exists(val):
50
+ return val is not None
51
+
52
+
53
+ def identity(*args, **kwargs):
54
+ return nn.Identity()
55
+
56
+
57
+ def uniq(arr):
58
+ return {el: True for el in arr}.keys()
59
+
60
+
61
+ def mean_flat(tensor):
62
+ """
63
+ Take the mean over all non-batch dimensions.
64
+ """
65
+ return tensor.mean(dim=list(range(1, len(tensor.shape))))
66
+
67
+
68
+ def ismap(x):
69
+ if not isinstance(x, torch.Tensor):
70
+ return False
71
+ return (len(x.shape) == 4) and (x.shape[1] > 3)
72
+
73
+
74
+ def isimage(x):
75
+ if not isinstance(x, torch.Tensor):
76
+ return False
77
+ return (len(x.shape) == 4) and (x.shape[1] == 3 or x.shape[1] == 1)
78
+
79
+
80
+ def max_neg_value(t):
81
+ return -torch.finfo(t.dtype).max
82
+
83
+
84
+ def shape_to_str(x):
85
+ shape_str = "x".join([str(x) for x in x.shape])
86
+ return shape_str
87
+
88
+
89
+ def init_(tensor):
90
+ dim = tensor.shape[-1]
91
+ std = 1 / math.sqrt(dim)
92
+ tensor.uniform_(-std, std)
93
+ return tensor
94
+
95
+
96
+ ckpt = torch.utils.checkpoint.checkpoint
97
+
98
+
99
+ def checkpoint(func, inputs, params, flag):
100
+ """
101
+ Evaluate a function without caching intermediate activations, allowing for
102
+ reduced memory at the expense of extra compute in the backward pass.
103
+ :param func: the function to evaluate.
104
+ :param inputs: the argument sequence to pass to `func`.
105
+ :param params: a sequence of parameters `func` depends on but does not
106
+ explicitly take as arguments.
107
+ :param flag: if False, disable gradient checkpointing.
108
+ """
109
+ if flag:
110
+ return ckpt(func, *inputs)
111
+ else:
112
+ return func(*inputs)
lvdm/distributions.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+
4
+
5
+ class AbstractDistribution:
6
+ def sample(self):
7
+ raise NotImplementedError()
8
+
9
+ def mode(self):
10
+ raise NotImplementedError()
11
+
12
+
13
+ class DiracDistribution(AbstractDistribution):
14
+ def __init__(self, value):
15
+ self.value = value
16
+
17
+ def sample(self):
18
+ return self.value
19
+
20
+ def mode(self):
21
+ return self.value
22
+
23
+
24
+ class DiagonalGaussianDistribution(object):
25
+ def __init__(self, parameters, deterministic=False):
26
+ self.parameters = parameters
27
+ self.mean, self.logvar = torch.chunk(parameters, 2, dim=1)
28
+ self.logvar = torch.clamp(self.logvar, -30.0, 20.0)
29
+ self.deterministic = deterministic
30
+ self.std = torch.exp(0.5 * self.logvar)
31
+ self.var = torch.exp(self.logvar)
32
+ if self.deterministic:
33
+ self.var = self.std = torch.zeros_like(self.mean).to(
34
+ device=self.parameters.device
35
+ )
36
+
37
+ def sample(self, noise=None):
38
+ if noise is None:
39
+ noise = torch.randn(self.mean.shape)
40
+
41
+ x = self.mean + self.std * noise.to(device=self.parameters.device)
42
+ return x
43
+
44
+ def kl(self, other=None):
45
+ if self.deterministic:
46
+ return torch.Tensor([0.0])
47
+ else:
48
+ if other is None:
49
+ return 0.5 * torch.sum(
50
+ torch.pow(self.mean, 2) + self.var - 1.0 - self.logvar,
51
+ dim=[1, 2, 3],
52
+ )
53
+ else:
54
+ return 0.5 * torch.sum(
55
+ torch.pow(self.mean - other.mean, 2) / other.var
56
+ + self.var / other.var
57
+ - 1.0
58
+ - self.logvar
59
+ + other.logvar,
60
+ dim=[1, 2, 3],
61
+ )
62
+
63
+ def nll(self, sample, dims=[1, 2, 3]):
64
+ if self.deterministic:
65
+ return torch.Tensor([0.0])
66
+ logtwopi = np.log(2.0 * np.pi)
67
+ return 0.5 * torch.sum(
68
+ logtwopi + self.logvar + torch.pow(sample - self.mean, 2) / self.var,
69
+ dim=dims,
70
+ )
71
+
72
+ def mode(self):
73
+ return self.mean
74
+
75
+
76
+ def normal_kl(mean1, logvar1, mean2, logvar2):
77
+ """
78
+ source: https://github.com/openai/guided-diffusion/blob/27c20a8fab9cb472df5d6bdd6c8d11c8f430b924/guided_diffusion/losses.py#L12
79
+ Compute the KL divergence between two gaussians.
80
+ Shapes are automatically broadcasted, so batches can be compared to
81
+ scalars, among other use cases.
82
+ """
83
+ tensor = None
84
+ for obj in (mean1, logvar1, mean2, logvar2):
85
+ if isinstance(obj, torch.Tensor):
86
+ tensor = obj
87
+ break
88
+ assert tensor is not None, "at least one argument must be a Tensor"
89
+
90
+ # Force variances to be Tensors. Broadcasting helps convert scalars to
91
+ # Tensors, but it does not work for torch.exp().
92
+ logvar1, logvar2 = [
93
+ x if isinstance(x, torch.Tensor) else torch.tensor(x).to(tensor)
94
+ for x in (logvar1, logvar2)
95
+ ]
96
+
97
+ return 0.5 * (
98
+ -1.0
99
+ + logvar2
100
+ - logvar1
101
+ + torch.exp(logvar1 - logvar2)
102
+ + ((mean1 - mean2) ** 2) * torch.exp(-logvar2)
103
+ )
lvdm/ema.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch import nn
3
+
4
+
5
+ class LitEma(nn.Module):
6
+ def __init__(self, model, decay=0.9999, use_num_upates=True):
7
+ super().__init__()
8
+ if decay < 0.0 or decay > 1.0:
9
+ raise ValueError("Decay must be between 0 and 1")
10
+
11
+ self.m_name2s_name = {}
12
+ self.register_buffer("decay", torch.tensor(decay, dtype=torch.float32))
13
+ self.register_buffer(
14
+ "num_updates",
15
+ (
16
+ torch.tensor(0, dtype=torch.int)
17
+ if use_num_upates
18
+ else torch.tensor(-1, dtype=torch.int)
19
+ ),
20
+ )
21
+
22
+ for name, p in model.named_parameters():
23
+ if p.requires_grad:
24
+ # remove as '.'-character is not allowed in buffers
25
+ s_name = name.replace(".", "")
26
+ self.m_name2s_name.update({name: s_name})
27
+ self.register_buffer(s_name, p.clone().detach().data)
28
+
29
+ self.collected_params = []
30
+
31
+ def forward(self, model):
32
+ decay = self.decay
33
+
34
+ if self.num_updates >= 0:
35
+ self.num_updates += 1
36
+ decay = min(self.decay, (1 + self.num_updates) / (10 + self.num_updates))
37
+
38
+ one_minus_decay = 1.0 - decay
39
+
40
+ with torch.no_grad():
41
+ m_param = dict(model.named_parameters())
42
+ shadow_params = dict(self.named_buffers())
43
+
44
+ for key in m_param:
45
+ if m_param[key].requires_grad:
46
+ sname = self.m_name2s_name[key]
47
+ shadow_params[sname] = shadow_params[sname].type_as(m_param[key])
48
+ shadow_params[sname].sub_(
49
+ one_minus_decay * (shadow_params[sname] - m_param[key])
50
+ )
51
+ else:
52
+ assert not key in self.m_name2s_name
53
+
54
+ def copy_to(self, model):
55
+ m_param = dict(model.named_parameters())
56
+ shadow_params = dict(self.named_buffers())
57
+ for key in m_param:
58
+ if m_param[key].requires_grad:
59
+ m_param[key].data.copy_(shadow_params[self.m_name2s_name[key]].data)
60
+ else:
61
+ assert not key in self.m_name2s_name
62
+
63
+ def store(self, parameters):
64
+ """
65
+ Save the current parameters for restoring later.
66
+ Args:
67
+ parameters: Iterable of `torch.nn.Parameter`; the parameters to be
68
+ temporarily stored.
69
+ """
70
+ self.collected_params = [param.clone() for param in parameters]
71
+
72
+ def restore(self, parameters):
73
+ """
74
+ Restore the parameters stored with the `store` method.
75
+ Useful to validate the model with EMA parameters without affecting the
76
+ original optimization process. Store the parameters before the
77
+ `copy_to` method. After validation (or model saving), use this to
78
+ restore the former parameters.
79
+ Args:
80
+ parameters: Iterable of `torch.nn.Parameter`; the parameters to be
81
+ updated with the stored parameters.
82
+ """
83
+ for c_param, param in zip(self.collected_params, parameters):
84
+ param.data.copy_(c_param.data)
lvdm/models/__pycache__/autoencoder.cpython-311.pyc ADDED
Binary file (14.1 kB). View file
 
lvdm/models/__pycache__/ddpm3d.cpython-311.pyc ADDED
Binary file (45.5 kB). View file
 
lvdm/models/__pycache__/utils_diffusion.cpython-311.pyc ADDED
Binary file (6.64 kB). View file
 
lvdm/models/autoencoder.py ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from contextlib import contextmanager
3
+ import torch
4
+ import numpy as np
5
+ from einops import rearrange
6
+ import torch.nn.functional as F
7
+ import pytorch_lightning as pl
8
+ from lvdm.modules.networks.ae_modules import Encoder, Decoder
9
+ from lvdm.distributions import DiagonalGaussianDistribution
10
+ from utils.utils import instantiate_from_config
11
+
12
+
13
+ class AutoencoderKL(pl.LightningModule):
14
+ def __init__(
15
+ self,
16
+ ddconfig,
17
+ lossconfig,
18
+ embed_dim,
19
+ ckpt_path=None,
20
+ ignore_keys=[],
21
+ image_key="image",
22
+ colorize_nlabels=None,
23
+ monitor=None,
24
+ test=False,
25
+ logdir=None,
26
+ input_dim=4,
27
+ test_args=None,
28
+ ):
29
+ super().__init__()
30
+ self.image_key = image_key
31
+ self.encoder = Encoder(**ddconfig)
32
+ self.decoder = Decoder(**ddconfig)
33
+ self.loss = instantiate_from_config(lossconfig)
34
+ assert ddconfig["double_z"]
35
+ self.quant_conv = torch.nn.Conv2d(2 * ddconfig["z_channels"], 2 * embed_dim, 1)
36
+ self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1)
37
+ self.embed_dim = embed_dim
38
+ self.input_dim = input_dim
39
+ self.test = test
40
+ self.test_args = test_args
41
+ self.logdir = logdir
42
+ if colorize_nlabels is not None:
43
+ assert type(colorize_nlabels) == int
44
+ self.register_buffer("colorize", torch.randn(3, colorize_nlabels, 1, 1))
45
+ if monitor is not None:
46
+ self.monitor = monitor
47
+ if ckpt_path is not None:
48
+ self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys)
49
+ if self.test:
50
+ self.init_test()
51
+
52
+ def init_test(
53
+ self,
54
+ ):
55
+ self.test = True
56
+ save_dir = os.path.join(self.logdir, "test")
57
+ if "ckpt" in self.test_args:
58
+ ckpt_name = (
59
+ os.path.basename(self.test_args.ckpt).split(".ckpt")[0]
60
+ + f"_epoch{self._cur_epoch}"
61
+ )
62
+ self.root = os.path.join(save_dir, ckpt_name)
63
+ else:
64
+ self.root = save_dir
65
+ if "test_subdir" in self.test_args:
66
+ self.root = os.path.join(save_dir, self.test_args.test_subdir)
67
+
68
+ self.root_zs = os.path.join(self.root, "zs")
69
+ self.root_dec = os.path.join(self.root, "reconstructions")
70
+ self.root_inputs = os.path.join(self.root, "inputs")
71
+ os.makedirs(self.root, exist_ok=True)
72
+
73
+ if self.test_args.save_z:
74
+ os.makedirs(self.root_zs, exist_ok=True)
75
+ if self.test_args.save_reconstruction:
76
+ os.makedirs(self.root_dec, exist_ok=True)
77
+ if self.test_args.save_input:
78
+ os.makedirs(self.root_inputs, exist_ok=True)
79
+ assert self.test_args is not None
80
+ self.test_maximum = getattr(self.test_args, "test_maximum", None)
81
+ self.count = 0
82
+ self.eval_metrics = {}
83
+ self.decodes = []
84
+ self.save_decode_samples = 2048
85
+
86
+ def init_from_ckpt(self, path, ignore_keys=list()):
87
+ sd = torch.load(path, map_location="cpu")
88
+ try:
89
+ self._cur_epoch = sd["epoch"]
90
+ sd = sd["state_dict"]
91
+ except:
92
+ self._cur_epoch = "null"
93
+ keys = list(sd.keys())
94
+ for k in keys:
95
+ for ik in ignore_keys:
96
+ if k.startswith(ik):
97
+ print("Deleting key {} from state_dict.".format(k))
98
+ del sd[k]
99
+ self.load_state_dict(sd, strict=False)
100
+ # self.load_state_dict(sd, strict=True)
101
+ print(f"Restored from {path}")
102
+
103
+ def encode(self, x, **kwargs):
104
+
105
+ h = self.encoder(x)
106
+ moments = self.quant_conv(h)
107
+ posterior = DiagonalGaussianDistribution(moments)
108
+ return posterior
109
+
110
+ def decode(self, z, **kwargs):
111
+ z = self.post_quant_conv(z)
112
+ dec = self.decoder(z)
113
+ return dec
114
+
115
+ def forward(self, input, sample_posterior=True):
116
+ posterior = self.encode(input)
117
+ if sample_posterior:
118
+ z = posterior.sample()
119
+ else:
120
+ z = posterior.mode()
121
+ dec = self.decode(z)
122
+ return dec, posterior
123
+
124
+ def get_input(self, batch, k):
125
+ x = batch[k]
126
+ if x.dim() == 5 and self.input_dim == 4:
127
+ b, c, t, h, w = x.shape
128
+ self.b = b
129
+ self.t = t
130
+ x = rearrange(x, "b c t h w -> (b t) c h w")
131
+
132
+ return x
133
+
134
+ def training_step(self, batch, batch_idx, optimizer_idx):
135
+ inputs = self.get_input(batch, self.image_key)
136
+ reconstructions, posterior = self(inputs)
137
+
138
+ if optimizer_idx == 0:
139
+ # train encoder+decoder+logvar
140
+ aeloss, log_dict_ae = self.loss(
141
+ inputs,
142
+ reconstructions,
143
+ posterior,
144
+ optimizer_idx,
145
+ self.global_step,
146
+ last_layer=self.get_last_layer(),
147
+ split="train",
148
+ )
149
+ self.log(
150
+ "aeloss",
151
+ aeloss,
152
+ prog_bar=True,
153
+ logger=True,
154
+ on_step=True,
155
+ on_epoch=True,
156
+ )
157
+ self.log_dict(
158
+ log_dict_ae, prog_bar=False, logger=True, on_step=True, on_epoch=False
159
+ )
160
+ return aeloss
161
+
162
+ if optimizer_idx == 1:
163
+ # train the discriminator
164
+ discloss, log_dict_disc = self.loss(
165
+ inputs,
166
+ reconstructions,
167
+ posterior,
168
+ optimizer_idx,
169
+ self.global_step,
170
+ last_layer=self.get_last_layer(),
171
+ split="train",
172
+ )
173
+
174
+ self.log(
175
+ "discloss",
176
+ discloss,
177
+ prog_bar=True,
178
+ logger=True,
179
+ on_step=True,
180
+ on_epoch=True,
181
+ )
182
+ self.log_dict(
183
+ log_dict_disc, prog_bar=False, logger=True, on_step=True, on_epoch=False
184
+ )
185
+ return discloss
186
+
187
+ def validation_step(self, batch, batch_idx):
188
+ inputs = self.get_input(batch, self.image_key)
189
+ reconstructions, posterior = self(inputs)
190
+ aeloss, log_dict_ae = self.loss(
191
+ inputs,
192
+ reconstructions,
193
+ posterior,
194
+ 0,
195
+ self.global_step,
196
+ last_layer=self.get_last_layer(),
197
+ split="val",
198
+ )
199
+
200
+ discloss, log_dict_disc = self.loss(
201
+ inputs,
202
+ reconstructions,
203
+ posterior,
204
+ 1,
205
+ self.global_step,
206
+ last_layer=self.get_last_layer(),
207
+ split="val",
208
+ )
209
+
210
+ self.log("val/rec_loss", log_dict_ae["val/rec_loss"])
211
+ self.log_dict(log_dict_ae)
212
+ self.log_dict(log_dict_disc)
213
+ return self.log_dict
214
+
215
+ def configure_optimizers(self):
216
+ lr = self.learning_rate
217
+ opt_ae = torch.optim.Adam(
218
+ list(self.encoder.parameters())
219
+ + list(self.decoder.parameters())
220
+ + list(self.quant_conv.parameters())
221
+ + list(self.post_quant_conv.parameters()),
222
+ lr=lr,
223
+ betas=(0.5, 0.9),
224
+ )
225
+ opt_disc = torch.optim.Adam(
226
+ self.loss.discriminator.parameters(), lr=lr, betas=(0.5, 0.9)
227
+ )
228
+ return [opt_ae, opt_disc], []
229
+
230
+ def get_last_layer(self):
231
+ return self.decoder.conv_out.weight
232
+
233
+ @torch.no_grad()
234
+ def log_images(self, batch, only_inputs=False, **kwargs):
235
+ log = dict()
236
+ x = self.get_input(batch, self.image_key)
237
+ x = x.to(self.device)
238
+ if not only_inputs:
239
+ xrec, posterior = self(x)
240
+ if x.shape[1] > 3:
241
+ # colorize with random projection
242
+ assert xrec.shape[1] > 3
243
+ x = self.to_rgb(x)
244
+ xrec = self.to_rgb(xrec)
245
+ log["samples"] = self.decode(torch.randn_like(posterior.sample()))
246
+ log["reconstructions"] = xrec
247
+ log["inputs"] = x
248
+ return log
249
+
250
+ def to_rgb(self, x):
251
+ assert self.image_key == "segmentation"
252
+ if not hasattr(self, "colorize"):
253
+ self.register_buffer("colorize", torch.randn(3, x.shape[1], 1, 1).to(x))
254
+ x = F.conv2d(x, weight=self.colorize)
255
+ x = 2.0 * (x - x.min()) / (x.max() - x.min()) - 1.0
256
+ return x
257
+
258
+
259
+ class IdentityFirstStage(torch.nn.Module):
260
+ def __init__(self, *args, vq_interface=False, **kwargs):
261
+ self.vq_interface = vq_interface # TODO: Should be true by default but check to not break older stuff
262
+ super().__init__()
263
+
264
+ def encode(self, x, *args, **kwargs):
265
+ return x
266
+
267
+ def decode(self, x, *args, **kwargs):
268
+ return x
269
+
270
+ def quantize(self, x, *args, **kwargs):
271
+ if self.vq_interface:
272
+ return x, None, [None, None, None]
273
+ return x
274
+
275
+ def forward(self, x, *args, **kwargs):
276
+ return x
lvdm/models/ddpm3d.py ADDED
@@ -0,0 +1,967 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ wild mixture of
3
+ https://github.com/openai/improved-diffusion/blob/e94489283bb876ac1477d5dd7709bbbd2d9902ce/improved_diffusion/gaussian_diffusion.py
4
+ https://github.com/lucidrains/denoising-diffusion-pytorch/blob/7706bdfc6f527f58d33f84b7b522e61e6e3164b3/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py
5
+ https://github.com/CompVis/taming-transformers
6
+ -- merci
7
+ """
8
+
9
+ from functools import partial
10
+ from contextlib import contextmanager
11
+ import numpy as np
12
+ from tqdm import tqdm
13
+ from einops import rearrange, repeat
14
+ import logging
15
+
16
+ mainlogger = logging.getLogger("mainlogger")
17
+ import torch
18
+ import torch.nn as nn
19
+ from torchvision.utils import make_grid
20
+ import pytorch_lightning as pl
21
+ from utils.utils import instantiate_from_config
22
+ from lvdm.ema import LitEma
23
+ from lvdm.distributions import DiagonalGaussianDistribution
24
+ from lvdm.models.utils_diffusion import make_beta_schedule
25
+ from lvdm.modules.encoders.ip_resampler import ImageProjModel, Resampler
26
+ from lvdm.basics import disabled_train
27
+ from lvdm.common import extract_into_tensor, noise_like, exists, default
28
+
29
+
30
+ __conditioning_keys__ = {"concat": "c_concat", "crossattn": "c_crossattn", "adm": "y"}
31
+
32
+
33
+ class DDPM(pl.LightningModule):
34
+ # classic DDPM with Gaussian diffusion, in image space
35
+ def __init__(
36
+ self,
37
+ unet_config,
38
+ timesteps=1000,
39
+ beta_schedule="linear",
40
+ loss_type="l2",
41
+ ckpt_path=None,
42
+ ignore_keys=[],
43
+ load_only_unet=False,
44
+ monitor=None,
45
+ use_ema=True,
46
+ first_stage_key="image",
47
+ image_size=256,
48
+ channels=3,
49
+ log_every_t=100,
50
+ clip_denoised=True,
51
+ linear_start=1e-4,
52
+ linear_end=2e-2,
53
+ cosine_s=8e-3,
54
+ given_betas=None,
55
+ original_elbo_weight=0.0,
56
+ v_posterior=0.0, # weight for choosing posterior variance as sigma = (1-v) * beta_tilde + v * beta
57
+ l_simple_weight=1.0,
58
+ conditioning_key=None,
59
+ parameterization="eps", # all assuming fixed variance schedules
60
+ scheduler_config=None,
61
+ use_positional_encodings=False,
62
+ learn_logvar=False,
63
+ logvar_init=0.0,
64
+ ):
65
+ super().__init__()
66
+ assert parameterization in [
67
+ "eps",
68
+ "x0",
69
+ ], 'currently only supporting "eps" and "x0"'
70
+ self.parameterization = parameterization
71
+ mainlogger.info(
72
+ f"{self.__class__.__name__}: Running in {self.parameterization}-prediction mode"
73
+ )
74
+ self.cond_stage_model = None
75
+ self.clip_denoised = clip_denoised
76
+ self.log_every_t = log_every_t
77
+ self.first_stage_key = first_stage_key
78
+ self.channels = channels
79
+ self.temporal_length = unet_config.params.temporal_length
80
+ self.image_size = image_size
81
+ if isinstance(self.image_size, int):
82
+ self.image_size = [self.image_size, self.image_size]
83
+ self.use_positional_encodings = use_positional_encodings
84
+ self.model = DiffusionWrapper(unet_config, conditioning_key)
85
+ self.use_ema = use_ema
86
+ if self.use_ema:
87
+ self.model_ema = LitEma(self.model)
88
+ mainlogger.info(f"Keeping EMAs of {len(list(self.model_ema.buffers()))}.")
89
+
90
+ self.use_scheduler = scheduler_config is not None
91
+ if self.use_scheduler:
92
+ self.scheduler_config = scheduler_config
93
+
94
+ self.v_posterior = v_posterior
95
+ self.original_elbo_weight = original_elbo_weight
96
+ self.l_simple_weight = l_simple_weight
97
+
98
+ if monitor is not None:
99
+ self.monitor = monitor
100
+ if ckpt_path is not None:
101
+ self.init_from_ckpt(
102
+ ckpt_path, ignore_keys=ignore_keys, only_model=load_only_unet
103
+ )
104
+
105
+ self.register_schedule(
106
+ given_betas=given_betas,
107
+ beta_schedule=beta_schedule,
108
+ timesteps=timesteps,
109
+ linear_start=linear_start,
110
+ linear_end=linear_end,
111
+ cosine_s=cosine_s,
112
+ )
113
+
114
+ self.loss_type = loss_type
115
+
116
+ self.learn_logvar = learn_logvar
117
+ self.logvar = torch.full(fill_value=logvar_init, size=(self.num_timesteps,))
118
+ if self.learn_logvar:
119
+ self.logvar = nn.Parameter(self.logvar, requires_grad=True)
120
+
121
+ def register_schedule(
122
+ self,
123
+ given_betas=None,
124
+ beta_schedule="linear",
125
+ timesteps=1000,
126
+ linear_start=1e-4,
127
+ linear_end=2e-2,
128
+ cosine_s=8e-3,
129
+ ):
130
+ if exists(given_betas):
131
+ betas = given_betas
132
+ else:
133
+ betas = make_beta_schedule(
134
+ beta_schedule,
135
+ timesteps,
136
+ linear_start=linear_start,
137
+ linear_end=linear_end,
138
+ cosine_s=cosine_s,
139
+ )
140
+ alphas = 1.0 - betas
141
+ alphas_cumprod = np.cumprod(alphas, axis=0)
142
+ alphas_cumprod_prev = np.append(1.0, alphas_cumprod[:-1])
143
+
144
+ (timesteps,) = betas.shape
145
+ self.num_timesteps = int(timesteps)
146
+ self.linear_start = linear_start
147
+ self.linear_end = linear_end
148
+ assert (
149
+ alphas_cumprod.shape[0] == self.num_timesteps
150
+ ), "alphas have to be defined for each timestep"
151
+
152
+ to_torch = partial(torch.tensor, dtype=torch.float32)
153
+
154
+ self.register_buffer("betas", to_torch(betas))
155
+ self.register_buffer("alphas_cumprod", to_torch(alphas_cumprod))
156
+ self.register_buffer("alphas_cumprod_prev", to_torch(alphas_cumprod_prev))
157
+
158
+ # calculations for diffusion q(x_t | x_{t-1}) and others
159
+ self.register_buffer("sqrt_alphas_cumprod", to_torch(np.sqrt(alphas_cumprod)))
160
+ self.register_buffer(
161
+ "sqrt_one_minus_alphas_cumprod", to_torch(np.sqrt(1.0 - alphas_cumprod))
162
+ )
163
+ self.register_buffer(
164
+ "log_one_minus_alphas_cumprod", to_torch(np.log(1.0 - alphas_cumprod))
165
+ )
166
+ self.register_buffer(
167
+ "sqrt_recip_alphas_cumprod", to_torch(np.sqrt(1.0 / alphas_cumprod))
168
+ )
169
+ self.register_buffer(
170
+ "sqrt_recipm1_alphas_cumprod", to_torch(np.sqrt(1.0 / alphas_cumprod - 1))
171
+ )
172
+
173
+ # calculations for posterior q(x_{t-1} | x_t, x_0)
174
+ posterior_variance = (1 - self.v_posterior) * betas * (
175
+ 1.0 - alphas_cumprod_prev
176
+ ) / (1.0 - alphas_cumprod) + self.v_posterior * betas
177
+ # above: equal to 1. / (1. / (1. - alpha_cumprod_tm1) + alpha_t / beta_t)
178
+ self.register_buffer("posterior_variance", to_torch(posterior_variance))
179
+ # below: log calculation clipped because the posterior variance is 0 at the beginning of the diffusion chain
180
+ self.register_buffer(
181
+ "posterior_log_variance_clipped",
182
+ to_torch(np.log(np.maximum(posterior_variance, 1e-20))),
183
+ )
184
+ self.register_buffer(
185
+ "posterior_mean_coef1",
186
+ to_torch(betas * np.sqrt(alphas_cumprod_prev) / (1.0 - alphas_cumprod)),
187
+ )
188
+ self.register_buffer(
189
+ "posterior_mean_coef2",
190
+ to_torch(
191
+ (1.0 - alphas_cumprod_prev) * np.sqrt(alphas) / (1.0 - alphas_cumprod)
192
+ ),
193
+ )
194
+
195
+ if self.parameterization == "eps":
196
+ lvlb_weights = self.betas**2 / (
197
+ 2
198
+ * self.posterior_variance
199
+ * to_torch(alphas)
200
+ * (1 - self.alphas_cumprod)
201
+ )
202
+ elif self.parameterization == "x0":
203
+ lvlb_weights = (
204
+ 0.5
205
+ * np.sqrt(torch.Tensor(alphas_cumprod))
206
+ / (2.0 * 1 - torch.Tensor(alphas_cumprod))
207
+ )
208
+ else:
209
+ raise NotImplementedError("mu not supported")
210
+ # TODO how to choose this term
211
+ lvlb_weights[0] = lvlb_weights[1]
212
+ self.register_buffer("lvlb_weights", lvlb_weights, persistent=False)
213
+ assert not torch.isnan(self.lvlb_weights).all()
214
+
215
+ @contextmanager
216
+ def ema_scope(self, context=None):
217
+ if self.use_ema:
218
+ self.model_ema.store(self.model.parameters())
219
+ self.model_ema.copy_to(self.model)
220
+ if context is not None:
221
+ mainlogger.info(f"{context}: Switched to EMA weights")
222
+ try:
223
+ yield None
224
+ finally:
225
+ if self.use_ema:
226
+ self.model_ema.restore(self.model.parameters())
227
+ if context is not None:
228
+ mainlogger.info(f"{context}: Restored training weights")
229
+
230
+ def init_from_ckpt(self, path, ignore_keys=list(), only_model=False):
231
+ sd = torch.load(path, map_location="cpu")
232
+ if "state_dict" in list(sd.keys()):
233
+ sd = sd["state_dict"]
234
+ keys = list(sd.keys())
235
+ for k in keys:
236
+ for ik in ignore_keys:
237
+ if k.startswith(ik):
238
+ mainlogger.info("Deleting key {} from state_dict.".format(k))
239
+ del sd[k]
240
+ missing, unexpected = (
241
+ self.load_state_dict(sd, strict=False)
242
+ if not only_model
243
+ else self.model.load_state_dict(sd, strict=False)
244
+ )
245
+ mainlogger.info(
246
+ f"Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys"
247
+ )
248
+ if len(missing) > 0:
249
+ mainlogger.info(f"Missing Keys: {missing}")
250
+ if len(unexpected) > 0:
251
+ mainlogger.info(f"Unexpected Keys: {unexpected}")
252
+
253
+ def q_mean_variance(self, x_start, t):
254
+ """
255
+ Get the distribution q(x_t | x_0).
256
+ :param x_start: the [N x C x ...] tensor of noiseless inputs.
257
+ :param t: the number of diffusion steps (minus 1). Here, 0 means one step.
258
+ :return: A tuple (mean, variance, log_variance), all of x_start's shape.
259
+ """
260
+ mean = extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start
261
+ variance = extract_into_tensor(1.0 - self.alphas_cumprod, t, x_start.shape)
262
+ log_variance = extract_into_tensor(
263
+ self.log_one_minus_alphas_cumprod, t, x_start.shape
264
+ )
265
+ return mean, variance, log_variance
266
+
267
+ def predict_start_from_noise(self, x_t, t, noise):
268
+ return (
269
+ extract_into_tensor(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t
270
+ - extract_into_tensor(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape)
271
+ * noise
272
+ )
273
+
274
+ def q_posterior(self, x_start, x_t, t):
275
+ posterior_mean = (
276
+ extract_into_tensor(self.posterior_mean_coef1, t, x_t.shape) * x_start
277
+ + extract_into_tensor(self.posterior_mean_coef2, t, x_t.shape) * x_t
278
+ )
279
+ posterior_variance = extract_into_tensor(self.posterior_variance, t, x_t.shape)
280
+ posterior_log_variance_clipped = extract_into_tensor(
281
+ self.posterior_log_variance_clipped, t, x_t.shape
282
+ )
283
+ return posterior_mean, posterior_variance, posterior_log_variance_clipped
284
+
285
+ def p_mean_variance(self, x, t, clip_denoised: bool):
286
+ model_out = self.model(x, t)
287
+ if self.parameterization == "eps":
288
+ x_recon = self.predict_start_from_noise(x, t=t, noise=model_out)
289
+ elif self.parameterization == "x0":
290
+ x_recon = model_out
291
+ if clip_denoised:
292
+ x_recon.clamp_(-1.0, 1.0)
293
+
294
+ model_mean, posterior_variance, posterior_log_variance = self.q_posterior(
295
+ x_start=x_recon, x_t=x, t=t
296
+ )
297
+ return model_mean, posterior_variance, posterior_log_variance
298
+
299
+ @torch.no_grad()
300
+ def p_sample(self, x, t, clip_denoised=True, repeat_noise=False):
301
+ b, *_, device = *x.shape, x.device
302
+ model_mean, _, model_log_variance = self.p_mean_variance(
303
+ x=x, t=t, clip_denoised=clip_denoised
304
+ )
305
+ noise = noise_like(x.shape, device, repeat_noise)
306
+ # no noise when t == 0
307
+ nonzero_mask = (1 - (t == 0).float()).reshape(b, *((1,) * (len(x.shape) - 1)))
308
+ return model_mean + nonzero_mask * (0.5 * model_log_variance).exp() * noise
309
+
310
+ @torch.no_grad()
311
+ def p_sample_loop(self, shape, return_intermediates=False):
312
+ device = self.betas.device
313
+ b = shape[0]
314
+ img = torch.randn(shape, device=device)
315
+ intermediates = [img]
316
+ for i in tqdm(
317
+ reversed(range(0, self.num_timesteps)),
318
+ desc="Sampling t",
319
+ total=self.num_timesteps,
320
+ ):
321
+ img = self.p_sample(
322
+ img,
323
+ torch.full((b,), i, device=device, dtype=torch.long),
324
+ clip_denoised=self.clip_denoised,
325
+ )
326
+ if i % self.log_every_t == 0 or i == self.num_timesteps - 1:
327
+ intermediates.append(img)
328
+ if return_intermediates:
329
+ return img, intermediates
330
+ return img
331
+
332
+ @torch.no_grad()
333
+ def sample(self, batch_size=16, return_intermediates=False):
334
+ image_size = self.image_size
335
+ channels = self.channels
336
+ return self.p_sample_loop(
337
+ (batch_size, channels, image_size, image_size),
338
+ return_intermediates=return_intermediates,
339
+ )
340
+
341
+ def q_sample(self, x_start, t, noise=None):
342
+ noise = default(noise, lambda: torch.randn_like(x_start))
343
+ return (
344
+ extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape)
345
+ * x_start
346
+ * extract_into_tensor(self.scale_arr, t, x_start.shape)
347
+ + extract_into_tensor(self.sqrt_one_minus_alphas_cumprod, t, x_start.shape)
348
+ * noise
349
+ )
350
+
351
+ def get_input(self, batch, k):
352
+ x = batch[k]
353
+ x = x.to(memory_format=torch.contiguous_format).float()
354
+ return x
355
+
356
+ def _get_rows_from_list(self, samples):
357
+ n_imgs_per_row = len(samples)
358
+ denoise_grid = rearrange(samples, "n b c h w -> b n c h w")
359
+ denoise_grid = rearrange(denoise_grid, "b n c h w -> (b n) c h w")
360
+ denoise_grid = make_grid(denoise_grid, nrow=n_imgs_per_row)
361
+ return denoise_grid
362
+
363
+ @torch.no_grad()
364
+ def log_images(self, batch, N=8, n_row=2, sample=True, return_keys=None, **kwargs):
365
+ log = dict()
366
+ x = self.get_input(batch, self.first_stage_key)
367
+ N = min(x.shape[0], N)
368
+ n_row = min(x.shape[0], n_row)
369
+ x = x.to(self.device)[:N]
370
+ log["inputs"] = x
371
+
372
+ # get diffusion row
373
+ diffusion_row = list()
374
+ x_start = x[:n_row]
375
+
376
+ for t in range(self.num_timesteps):
377
+ if t % self.log_every_t == 0 or t == self.num_timesteps - 1:
378
+ t = repeat(torch.tensor([t]), "1 -> b", b=n_row)
379
+ t = t.to(self.device).long()
380
+ noise = torch.randn_like(x_start)
381
+ x_noisy = self.q_sample(x_start=x_start, t=t, noise=noise)
382
+ diffusion_row.append(x_noisy)
383
+
384
+ log["diffusion_row"] = self._get_rows_from_list(diffusion_row)
385
+
386
+ if sample:
387
+ # get denoise row
388
+ with self.ema_scope("Plotting"):
389
+ samples, denoise_row = self.sample(
390
+ batch_size=N, return_intermediates=True
391
+ )
392
+
393
+ log["samples"] = samples
394
+ log["denoise_row"] = self._get_rows_from_list(denoise_row)
395
+
396
+ if return_keys:
397
+ if np.intersect1d(list(log.keys()), return_keys).shape[0] == 0:
398
+ return log
399
+ else:
400
+ return {key: log[key] for key in return_keys}
401
+ return log
402
+
403
+
404
+ class LatentDiffusion(DDPM):
405
+ """main class"""
406
+
407
+ def __init__(
408
+ self,
409
+ first_stage_config,
410
+ cond_stage_config,
411
+ num_timesteps_cond=None,
412
+ cond_stage_key="caption",
413
+ cond_stage_trainable=False,
414
+ cond_stage_forward=None,
415
+ conditioning_key=None,
416
+ uncond_prob=0.2,
417
+ uncond_type="empty_seq",
418
+ scale_factor=1.0,
419
+ scale_by_std=False,
420
+ encoder_type="2d",
421
+ only_model=False,
422
+ use_scale=False,
423
+ scale_a=1,
424
+ scale_b=0.3,
425
+ mid_step=400,
426
+ fix_scale_bug=False,
427
+ *args,
428
+ **kwargs,
429
+ ):
430
+ self.num_timesteps_cond = default(num_timesteps_cond, 1)
431
+ self.scale_by_std = scale_by_std
432
+ assert self.num_timesteps_cond <= kwargs["timesteps"]
433
+ # for backwards compatibility after implementation of DiffusionWrapper
434
+ ckpt_path = kwargs.pop("ckpt_path", None)
435
+ ignore_keys = kwargs.pop("ignore_keys", [])
436
+ conditioning_key = default(conditioning_key, "crossattn")
437
+ super().__init__(conditioning_key=conditioning_key, *args, **kwargs)
438
+
439
+ self.cond_stage_trainable = cond_stage_trainable
440
+ self.cond_stage_key = cond_stage_key
441
+
442
+ # scale factor
443
+ self.use_scale = use_scale
444
+ if self.use_scale:
445
+ self.scale_a = scale_a
446
+ self.scale_b = scale_b
447
+ if fix_scale_bug:
448
+ scale_step = self.num_timesteps - mid_step
449
+ else: # bug
450
+ scale_step = self.num_timesteps
451
+
452
+ scale_arr1 = np.linspace(scale_a, scale_b, mid_step)
453
+ scale_arr2 = np.full(scale_step, scale_b)
454
+ scale_arr = np.concatenate((scale_arr1, scale_arr2))
455
+ scale_arr_prev = np.append(scale_a, scale_arr[:-1])
456
+ to_torch = partial(torch.tensor, dtype=torch.float32)
457
+ self.register_buffer("scale_arr", to_torch(scale_arr))
458
+
459
+ try:
460
+ self.num_downs = len(first_stage_config.params.ddconfig.ch_mult) - 1
461
+ except:
462
+ self.num_downs = 0
463
+ if not scale_by_std:
464
+ self.scale_factor = scale_factor
465
+ else:
466
+ self.register_buffer("scale_factor", torch.tensor(scale_factor))
467
+ self.instantiate_first_stage(first_stage_config)
468
+ self.instantiate_cond_stage(cond_stage_config)
469
+ self.first_stage_config = first_stage_config
470
+ self.cond_stage_config = cond_stage_config
471
+ self.clip_denoised = False
472
+
473
+ self.cond_stage_forward = cond_stage_forward
474
+ self.encoder_type = encoder_type
475
+ assert encoder_type in ["2d", "3d"]
476
+ self.uncond_prob = uncond_prob
477
+ self.classifier_free_guidance = True if uncond_prob > 0 else False
478
+ assert uncond_type in ["zero_embed", "empty_seq"]
479
+ self.uncond_type = uncond_type
480
+
481
+ self.restarted_from_ckpt = False
482
+ if ckpt_path is not None:
483
+ self.init_from_ckpt(ckpt_path, ignore_keys, only_model=only_model)
484
+ self.restarted_from_ckpt = True
485
+
486
+ def make_cond_schedule(
487
+ self,
488
+ ):
489
+ self.cond_ids = torch.full(
490
+ size=(self.num_timesteps,),
491
+ fill_value=self.num_timesteps - 1,
492
+ dtype=torch.long,
493
+ )
494
+ ids = torch.round(
495
+ torch.linspace(0, self.num_timesteps - 1, self.num_timesteps_cond)
496
+ ).long()
497
+ self.cond_ids[: self.num_timesteps_cond] = ids
498
+
499
+ def q_sample(self, x_start, t, noise=None):
500
+ noise = default(noise, lambda: torch.randn_like(x_start))
501
+ if self.use_scale:
502
+ return (
503
+ extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape)
504
+ * x_start
505
+ * extract_into_tensor(self.scale_arr, t, x_start.shape)
506
+ + extract_into_tensor(
507
+ self.sqrt_one_minus_alphas_cumprod, t, x_start.shape
508
+ )
509
+ * noise
510
+ )
511
+ else:
512
+ return (
513
+ extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape)
514
+ * x_start
515
+ + extract_into_tensor(
516
+ self.sqrt_one_minus_alphas_cumprod, t, x_start.shape
517
+ )
518
+ * noise
519
+ )
520
+
521
+ def _freeze_model(self):
522
+ for name, para in self.model.diffusion_model.named_parameters():
523
+ para.requires_grad = False
524
+
525
+ def instantiate_first_stage(self, config):
526
+ model = instantiate_from_config(config)
527
+ self.first_stage_model = model.eval()
528
+ self.first_stage_model.train = disabled_train
529
+ for param in self.first_stage_model.parameters():
530
+ param.requires_grad = False
531
+
532
+ def instantiate_cond_stage(self, config):
533
+ if not self.cond_stage_trainable:
534
+ model = instantiate_from_config(config)
535
+ self.cond_stage_model = model.eval()
536
+ self.cond_stage_model.train = disabled_train
537
+ for param in self.cond_stage_model.parameters():
538
+ param.requires_grad = False
539
+ else:
540
+ model = instantiate_from_config(config)
541
+ self.cond_stage_model = model
542
+
543
+ def get_learned_conditioning(self, c):
544
+ if self.cond_stage_forward is None:
545
+ if hasattr(self.cond_stage_model, "encode") and callable(
546
+ self.cond_stage_model.encode
547
+ ):
548
+ c = self.cond_stage_model.encode(c)
549
+ if isinstance(c, DiagonalGaussianDistribution):
550
+ c = c.mode()
551
+ else:
552
+ c = self.cond_stage_model(c)
553
+ else:
554
+ assert hasattr(self.cond_stage_model, self.cond_stage_forward)
555
+ c = getattr(self.cond_stage_model, self.cond_stage_forward)(c)
556
+ return c
557
+
558
+ def get_first_stage_encoding(self, encoder_posterior, noise=None):
559
+ if isinstance(encoder_posterior, DiagonalGaussianDistribution):
560
+ z = encoder_posterior.sample(noise=noise)
561
+ elif isinstance(encoder_posterior, torch.Tensor):
562
+ z = encoder_posterior
563
+ else:
564
+ raise NotImplementedError(
565
+ f"encoder_posterior of type '{type(encoder_posterior)}' not yet implemented"
566
+ )
567
+ return self.scale_factor * z
568
+
569
+ @torch.no_grad()
570
+ def encode_first_stage(self, x):
571
+ if self.encoder_type == "2d" and x.dim() == 5:
572
+ b, _, t, _, _ = x.shape
573
+ x = rearrange(x, "b c t h w -> (b t) c h w")
574
+ reshape_back = True
575
+ else:
576
+ reshape_back = False
577
+
578
+ encoder_posterior = self.first_stage_model.encode(x)
579
+ results = self.get_first_stage_encoding(encoder_posterior).detach()
580
+
581
+ if reshape_back:
582
+ results = rearrange(results, "(b t) c h w -> b c t h w", b=b, t=t)
583
+
584
+ return results
585
+
586
+ @torch.no_grad()
587
+ def encode_first_stage_2DAE(self, x):
588
+
589
+ b, _, t, _, _ = x.shape
590
+ results = torch.cat(
591
+ [
592
+ self.get_first_stage_encoding(self.first_stage_model.encode(x[:, :, i]))
593
+ .detach()
594
+ .unsqueeze(2)
595
+ for i in range(t)
596
+ ],
597
+ dim=2,
598
+ )
599
+
600
+ return results
601
+
602
+ def decode_core(self, z, **kwargs):
603
+ if self.encoder_type == "2d" and z.dim() == 5:
604
+ b, _, t, _, _ = z.shape
605
+ z = rearrange(z, "b c t h w -> (b t) c h w")
606
+ reshape_back = True
607
+ else:
608
+ reshape_back = False
609
+
610
+ z = 1.0 / self.scale_factor * z
611
+
612
+ results = self.first_stage_model.decode(z, **kwargs)
613
+
614
+ if reshape_back:
615
+ results = rearrange(results, "(b t) c h w -> b c t h w", b=b, t=t)
616
+ return results
617
+
618
+ @torch.no_grad()
619
+ def decode_first_stage(self, z, **kwargs):
620
+ return self.decode_core(z, **kwargs)
621
+
622
+ def apply_model(self, x_noisy, t, cond, **kwargs):
623
+ if isinstance(cond, dict):
624
+ # hybrid case, cond is exptected to be a dict
625
+ pass
626
+ else:
627
+ if not isinstance(cond, list):
628
+ cond = [cond]
629
+ key = (
630
+ "c_concat" if self.model.conditioning_key == "concat" else "c_crossattn"
631
+ )
632
+ cond = {key: cond}
633
+
634
+ x_recon = self.model(x_noisy, t, **cond, **kwargs)
635
+
636
+ if isinstance(x_recon, tuple):
637
+ return x_recon[0]
638
+ else:
639
+ return x_recon
640
+
641
+ def _get_denoise_row_from_list(self, samples, desc=""):
642
+ denoise_row = []
643
+ for zd in tqdm(samples, desc=desc):
644
+ denoise_row.append(self.decode_first_stage(zd.to(self.device)))
645
+ n_log_timesteps = len(denoise_row)
646
+
647
+ denoise_row = torch.stack(denoise_row) # n_log_timesteps, b, C, H, W
648
+
649
+ if denoise_row.dim() == 5:
650
+ # img, num_imgs= n_log_timesteps * bs, grid_size=[bs,n_log_timesteps]
651
+ denoise_grid = rearrange(denoise_row, "n b c h w -> b n c h w")
652
+ denoise_grid = rearrange(denoise_grid, "b n c h w -> (b n) c h w")
653
+ denoise_grid = make_grid(denoise_grid, nrow=n_log_timesteps)
654
+ elif denoise_row.dim() == 6:
655
+ # video, grid_size=[n_log_timesteps*bs, t]
656
+ video_length = denoise_row.shape[3]
657
+ denoise_grid = rearrange(denoise_row, "n b c t h w -> b n c t h w")
658
+ denoise_grid = rearrange(denoise_grid, "b n c t h w -> (b n) c t h w")
659
+ denoise_grid = rearrange(denoise_grid, "n c t h w -> (n t) c h w")
660
+ denoise_grid = make_grid(denoise_grid, nrow=video_length)
661
+ else:
662
+ raise ValueError
663
+
664
+ return denoise_grid
665
+
666
+ @torch.no_grad()
667
+ def decode_first_stage_2DAE(self, z, **kwargs):
668
+
669
+ b, _, t, _, _ = z.shape
670
+ z = 1.0 / self.scale_factor * z
671
+ results = torch.cat(
672
+ [
673
+ self.first_stage_model.decode(z[:, :, i], **kwargs).unsqueeze(2)
674
+ for i in range(t)
675
+ ],
676
+ dim=2,
677
+ )
678
+
679
+ return results
680
+
681
+ def p_mean_variance(
682
+ self,
683
+ x,
684
+ c,
685
+ t,
686
+ clip_denoised: bool,
687
+ return_x0=False,
688
+ score_corrector=None,
689
+ corrector_kwargs=None,
690
+ **kwargs,
691
+ ):
692
+ t_in = t
693
+ model_out = self.apply_model(x, t_in, c, **kwargs)
694
+
695
+ if score_corrector is not None:
696
+ assert self.parameterization == "eps"
697
+ model_out = score_corrector.modify_score(
698
+ self, model_out, x, t, c, **corrector_kwargs
699
+ )
700
+
701
+ if self.parameterization == "eps":
702
+ x_recon = self.predict_start_from_noise(x, t=t, noise=model_out)
703
+ elif self.parameterization == "x0":
704
+ x_recon = model_out
705
+ else:
706
+ raise NotImplementedError()
707
+
708
+ if clip_denoised:
709
+ x_recon.clamp_(-1.0, 1.0)
710
+
711
+ model_mean, posterior_variance, posterior_log_variance = self.q_posterior(
712
+ x_start=x_recon, x_t=x, t=t
713
+ )
714
+
715
+ if return_x0:
716
+ return model_mean, posterior_variance, posterior_log_variance, x_recon
717
+ else:
718
+ return model_mean, posterior_variance, posterior_log_variance
719
+
720
+ @torch.no_grad()
721
+ def p_sample(
722
+ self,
723
+ x,
724
+ c,
725
+ t,
726
+ clip_denoised=False,
727
+ repeat_noise=False,
728
+ return_x0=False,
729
+ temperature=1.0,
730
+ noise_dropout=0.0,
731
+ score_corrector=None,
732
+ corrector_kwargs=None,
733
+ **kwargs,
734
+ ):
735
+ b, *_, device = *x.shape, x.device
736
+ outputs = self.p_mean_variance(
737
+ x=x,
738
+ c=c,
739
+ t=t,
740
+ clip_denoised=clip_denoised,
741
+ return_x0=return_x0,
742
+ score_corrector=score_corrector,
743
+ corrector_kwargs=corrector_kwargs,
744
+ **kwargs,
745
+ )
746
+ if return_x0:
747
+ model_mean, _, model_log_variance, x0 = outputs
748
+ else:
749
+ model_mean, _, model_log_variance = outputs
750
+
751
+ noise = noise_like(x.shape, device, repeat_noise) * temperature
752
+ if noise_dropout > 0.0:
753
+ noise = torch.nn.functional.dropout(noise, p=noise_dropout)
754
+ # no noise when t == 0
755
+ nonzero_mask = (1 - (t == 0).float()).reshape(b, *((1,) * (len(x.shape) - 1)))
756
+
757
+ if return_x0:
758
+ return (
759
+ model_mean + nonzero_mask * (0.5 * model_log_variance).exp() * noise,
760
+ x0,
761
+ )
762
+ else:
763
+ return model_mean + nonzero_mask * (0.5 * model_log_variance).exp() * noise
764
+
765
+ @torch.no_grad()
766
+ def p_sample_loop(
767
+ self,
768
+ cond,
769
+ shape,
770
+ return_intermediates=False,
771
+ x_T=None,
772
+ verbose=True,
773
+ callback=None,
774
+ timesteps=None,
775
+ mask=None,
776
+ x0=None,
777
+ img_callback=None,
778
+ start_T=None,
779
+ log_every_t=None,
780
+ **kwargs,
781
+ ):
782
+
783
+ if not log_every_t:
784
+ log_every_t = self.log_every_t
785
+ device = self.betas.device
786
+ b = shape[0]
787
+ # sample an initial noise
788
+ if x_T is None:
789
+ img = torch.randn(shape, device=device)
790
+ else:
791
+ img = x_T
792
+
793
+ intermediates = [img]
794
+ if timesteps is None:
795
+ timesteps = self.num_timesteps
796
+ if start_T is not None:
797
+ timesteps = min(timesteps, start_T)
798
+
799
+ iterator = (
800
+ tqdm(reversed(range(0, timesteps)), desc="Sampling t", total=timesteps)
801
+ if verbose
802
+ else reversed(range(0, timesteps))
803
+ )
804
+
805
+ if mask is not None:
806
+ assert x0 is not None
807
+ assert x0.shape[2:3] == mask.shape[2:3] # spatial size has to match
808
+
809
+ for i in iterator:
810
+ ts = torch.full((b,), i, device=device, dtype=torch.long)
811
+ if self.shorten_cond_schedule:
812
+ assert self.model.conditioning_key != "hybrid"
813
+ tc = self.cond_ids[ts].to(cond.device)
814
+ cond = self.q_sample(x_start=cond, t=tc, noise=torch.randn_like(cond))
815
+
816
+ img = self.p_sample(
817
+ img, cond, ts, clip_denoised=self.clip_denoised, **kwargs
818
+ )
819
+ if mask is not None:
820
+ img_orig = self.q_sample(x0, ts)
821
+ img = img_orig * mask + (1.0 - mask) * img
822
+
823
+ if i % log_every_t == 0 or i == timesteps - 1:
824
+ intermediates.append(img)
825
+ if callback:
826
+ callback(i)
827
+ if img_callback:
828
+ img_callback(img, i)
829
+
830
+ if return_intermediates:
831
+ return img, intermediates
832
+ return img
833
+
834
+
835
+ class LatentVisualDiffusion(LatentDiffusion):
836
+ def __init__(
837
+ self, cond_img_config, finegrained=False, random_cond=False, *args, **kwargs
838
+ ):
839
+ super().__init__(*args, **kwargs)
840
+ self.random_cond = random_cond
841
+ self.instantiate_img_embedder(cond_img_config, freeze=True)
842
+ num_tokens = 16 if finegrained else 4
843
+ self.image_proj_model = self.init_projector(
844
+ use_finegrained=finegrained,
845
+ num_tokens=num_tokens,
846
+ input_dim=1024,
847
+ cross_attention_dim=1024,
848
+ dim=1280,
849
+ )
850
+
851
+ def instantiate_img_embedder(self, config, freeze=True):
852
+ embedder = instantiate_from_config(config)
853
+ if freeze:
854
+ self.embedder = embedder.eval()
855
+ self.embedder.train = disabled_train
856
+ for param in self.embedder.parameters():
857
+ param.requires_grad = False
858
+
859
+ def init_projector(
860
+ self, use_finegrained, num_tokens, input_dim, cross_attention_dim, dim
861
+ ):
862
+ if not use_finegrained:
863
+ image_proj_model = ImageProjModel(
864
+ clip_extra_context_tokens=num_tokens,
865
+ cross_attention_dim=cross_attention_dim,
866
+ clip_embeddings_dim=input_dim,
867
+ )
868
+ else:
869
+ image_proj_model = Resampler(
870
+ dim=input_dim,
871
+ depth=4,
872
+ dim_head=64,
873
+ heads=12,
874
+ num_queries=num_tokens,
875
+ embedding_dim=dim,
876
+ output_dim=cross_attention_dim,
877
+ ff_mult=4,
878
+ )
879
+ return image_proj_model
880
+
881
+ ## Never delete this func: it is used in log_images() and inference stage
882
+ def get_image_embeds(self, batch_imgs):
883
+ ## img: b c h w
884
+ img_token = self.embedder(batch_imgs)
885
+ img_emb = self.image_proj_model(img_token)
886
+ return img_emb
887
+
888
+
889
+ class DiffusionWrapper(pl.LightningModule):
890
+ def __init__(self, diff_model_config, conditioning_key):
891
+ super().__init__()
892
+ self.diffusion_model = instantiate_from_config(diff_model_config)
893
+ self.conditioning_key = conditioning_key
894
+
895
+ def forward(
896
+ self,
897
+ x,
898
+ t,
899
+ c_concat: list = None,
900
+ c_crossattn: list = None,
901
+ c_adm=None,
902
+ s=None,
903
+ mask=None,
904
+ **kwargs,
905
+ ):
906
+ # temporal_context = fps is foNone
907
+ if self.conditioning_key is None:
908
+ out = self.diffusion_model(x, t)
909
+ elif self.conditioning_key == "concat":
910
+ xc = torch.cat([x] + c_concat, dim=1)
911
+ out = self.diffusion_model(xc, t, **kwargs)
912
+ elif self.conditioning_key == "crossattn":
913
+ cc = torch.cat(c_crossattn, 1)
914
+ out = self.diffusion_model(x, t, context=cc, **kwargs)
915
+ elif self.conditioning_key == "hybrid":
916
+ ## it is just right [b,c,t,h,w]: concatenate in channel dim
917
+ xc = torch.cat([x] + c_concat, dim=1)
918
+ cc = torch.cat(c_crossattn, 1)
919
+ out = self.diffusion_model(xc, t, context=cc)
920
+ elif self.conditioning_key == "resblockcond":
921
+ cc = c_crossattn[0]
922
+ out = self.diffusion_model(x, t, context=cc)
923
+ elif self.conditioning_key == "adm":
924
+ cc = c_crossattn[0]
925
+ out = self.diffusion_model(x, t, y=cc)
926
+ elif self.conditioning_key == "hybrid-adm":
927
+ assert c_adm is not None
928
+ xc = torch.cat([x] + c_concat, dim=1)
929
+ cc = torch.cat(c_crossattn, 1)
930
+ out = self.diffusion_model(xc, t, context=cc, y=c_adm)
931
+ elif self.conditioning_key == "hybrid-time":
932
+ assert s is not None
933
+ xc = torch.cat([x] + c_concat, dim=1)
934
+ cc = torch.cat(c_crossattn, 1)
935
+ out = self.diffusion_model(xc, t, context=cc, s=s)
936
+ elif self.conditioning_key == "concat-time-mask":
937
+ # assert s is not None
938
+ # mainlogger.info('x & mask:',x.shape,c_concat[0].shape)
939
+ xc = torch.cat([x] + c_concat, dim=1)
940
+ out = self.diffusion_model(xc, t, context=None, s=s, mask=mask)
941
+ elif self.conditioning_key == "concat-adm-mask":
942
+ # assert s is not None
943
+ # mainlogger.info('x & mask:',x.shape,c_concat[0].shape)
944
+ if c_concat is not None:
945
+ xc = torch.cat([x] + c_concat, dim=1)
946
+ else:
947
+ xc = x
948
+ out = self.diffusion_model(xc, t, context=None, y=s, mask=mask)
949
+ elif self.conditioning_key == "hybrid-adm-mask":
950
+ cc = torch.cat(c_crossattn, 1)
951
+ if c_concat is not None:
952
+ xc = torch.cat([x] + c_concat, dim=1)
953
+ else:
954
+ xc = x
955
+ out = self.diffusion_model(xc, t, context=cc, y=s, mask=mask)
956
+ elif (
957
+ self.conditioning_key == "hybrid-time-adm"
958
+ ): # adm means y, e.g., class index
959
+ # assert s is not None
960
+ assert c_adm is not None
961
+ xc = torch.cat([x] + c_concat, dim=1)
962
+ cc = torch.cat(c_crossattn, 1)
963
+ out = self.diffusion_model(xc, t, context=cc, s=s, y=c_adm)
964
+ else:
965
+ raise NotImplementedError()
966
+
967
+ return out
lvdm/models/samplers/__pycache__/ddim.cpython-311.pyc ADDED
Binary file (18.5 kB). View file
 
lvdm/models/samplers/ddim.py ADDED
@@ -0,0 +1,493 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from tqdm import tqdm
3
+ import torch
4
+ from lvdm.models.utils_diffusion import (
5
+ make_ddim_sampling_parameters,
6
+ make_ddim_timesteps,
7
+ )
8
+ from lvdm.common import noise_like
9
+
10
+
11
+ class DDIMSampler(object):
12
+ def __init__(self, model, schedule="linear", **kwargs):
13
+ super().__init__()
14
+ self.model = model
15
+ self.ddpm_num_timesteps = model.num_timesteps
16
+ self.schedule = schedule
17
+ self.counter = 0
18
+
19
+ def register_buffer(self, name, attr):
20
+ if type(attr) == torch.Tensor:
21
+ if attr.device != torch.device("cuda"):
22
+ attr = attr.to(torch.device("cuda"))
23
+ setattr(self, name, attr)
24
+
25
+ def make_schedule(
26
+ self, ddim_num_steps, ddim_discretize="uniform", ddim_eta=0.0, verbose=True
27
+ ):
28
+ self.ddim_timesteps = make_ddim_timesteps(
29
+ ddim_discr_method=ddim_discretize,
30
+ num_ddim_timesteps=ddim_num_steps,
31
+ num_ddpm_timesteps=self.ddpm_num_timesteps,
32
+ verbose=verbose,
33
+ )
34
+ alphas_cumprod = self.model.alphas_cumprod
35
+ assert (
36
+ alphas_cumprod.shape[0] == self.ddpm_num_timesteps
37
+ ), "alphas have to be defined for each timestep"
38
+ to_torch = lambda x: x.clone().detach().to(torch.float32).to(self.model.device)
39
+
40
+ self.register_buffer("betas", to_torch(self.model.betas))
41
+ self.register_buffer("alphas_cumprod", to_torch(alphas_cumprod))
42
+ self.register_buffer(
43
+ "alphas_cumprod_prev", to_torch(self.model.alphas_cumprod_prev)
44
+ )
45
+ self.use_scale = self.model.use_scale
46
+ print("DDIM scale", self.use_scale)
47
+
48
+ if self.use_scale:
49
+ self.register_buffer("scale_arr", to_torch(self.model.scale_arr))
50
+ ddim_scale_arr = self.scale_arr.cpu()[self.ddim_timesteps]
51
+ self.register_buffer("ddim_scale_arr", ddim_scale_arr)
52
+ ddim_scale_arr = np.asarray(
53
+ [self.scale_arr.cpu()[0]]
54
+ + self.scale_arr.cpu()[self.ddim_timesteps[:-1]].tolist()
55
+ )
56
+ self.register_buffer("ddim_scale_arr_prev", ddim_scale_arr)
57
+
58
+ # calculations for diffusion q(x_t | x_{t-1}) and others
59
+ self.register_buffer(
60
+ "sqrt_alphas_cumprod", to_torch(np.sqrt(alphas_cumprod.cpu()))
61
+ )
62
+ self.register_buffer(
63
+ "sqrt_one_minus_alphas_cumprod",
64
+ to_torch(np.sqrt(1.0 - alphas_cumprod.cpu())),
65
+ )
66
+ self.register_buffer(
67
+ "log_one_minus_alphas_cumprod", to_torch(np.log(1.0 - alphas_cumprod.cpu()))
68
+ )
69
+ self.register_buffer(
70
+ "sqrt_recip_alphas_cumprod", to_torch(np.sqrt(1.0 / alphas_cumprod.cpu()))
71
+ )
72
+ self.register_buffer(
73
+ "sqrt_recipm1_alphas_cumprod",
74
+ to_torch(np.sqrt(1.0 / alphas_cumprod.cpu() - 1)),
75
+ )
76
+
77
+ # ddim sampling parameters
78
+ ddim_sigmas, ddim_alphas, ddim_alphas_prev = make_ddim_sampling_parameters(
79
+ alphacums=alphas_cumprod.cpu(),
80
+ ddim_timesteps=self.ddim_timesteps,
81
+ eta=ddim_eta,
82
+ verbose=verbose,
83
+ )
84
+ self.register_buffer("ddim_sigmas", ddim_sigmas)
85
+ self.register_buffer("ddim_alphas", ddim_alphas)
86
+ self.register_buffer("ddim_alphas_prev", ddim_alphas_prev)
87
+ self.register_buffer("ddim_sqrt_one_minus_alphas", np.sqrt(1.0 - ddim_alphas))
88
+ sigmas_for_original_sampling_steps = ddim_eta * torch.sqrt(
89
+ (1 - self.alphas_cumprod_prev)
90
+ / (1 - self.alphas_cumprod)
91
+ * (1 - self.alphas_cumprod / self.alphas_cumprod_prev)
92
+ )
93
+ self.register_buffer(
94
+ "ddim_sigmas_for_original_num_steps", sigmas_for_original_sampling_steps
95
+ )
96
+
97
+ @torch.no_grad()
98
+ def sample(
99
+ self,
100
+ S,
101
+ batch_size,
102
+ shape,
103
+ conditioning=None,
104
+ callback=None,
105
+ normals_sequence=None,
106
+ img_callback=None,
107
+ quantize_x0=False,
108
+ eta=0.0,
109
+ mask=None,
110
+ x0=None,
111
+ temperature=1.0,
112
+ noise_dropout=0.0,
113
+ score_corrector=None,
114
+ corrector_kwargs=None,
115
+ verbose=True,
116
+ schedule_verbose=False,
117
+ x_T=None,
118
+ log_every_t=100,
119
+ unconditional_guidance_scale=1.0,
120
+ unconditional_conditioning=None,
121
+ # this has to come in the same format as the conditioning, # e.g. as encoded tokens, ...
122
+ **kwargs,
123
+ ):
124
+
125
+ # check condition bs
126
+ if conditioning is not None:
127
+ if isinstance(conditioning, dict):
128
+ try:
129
+ cbs = conditioning[list(conditioning.keys())[0]].shape[0]
130
+ except:
131
+ cbs = conditioning[list(conditioning.keys())[0]][0].shape[0]
132
+
133
+ if cbs != batch_size:
134
+ print(
135
+ f"Warning: Got {cbs} conditionings but batch-size is {batch_size}"
136
+ )
137
+ else:
138
+ if conditioning.shape[0] != batch_size:
139
+ print(
140
+ f"Warning: Got {conditioning.shape[0]} conditionings but batch-size is {batch_size}"
141
+ )
142
+
143
+ self.make_schedule(ddim_num_steps=S, ddim_eta=eta, verbose=schedule_verbose)
144
+
145
+ # make shape
146
+ if len(shape) == 3:
147
+ C, H, W = shape
148
+ size = (batch_size, C, H, W)
149
+ elif len(shape) == 4:
150
+ C, T, H, W = shape
151
+ size = (batch_size, C, T, H, W)
152
+ # print(f'Data shape for DDIM sampling is {size}, eta {eta}')
153
+
154
+ samples, intermediates = self.ddim_sampling(
155
+ conditioning,
156
+ size,
157
+ callback=callback,
158
+ img_callback=img_callback,
159
+ quantize_denoised=quantize_x0,
160
+ mask=mask,
161
+ x0=x0,
162
+ ddim_use_original_steps=False,
163
+ noise_dropout=noise_dropout,
164
+ temperature=temperature,
165
+ score_corrector=score_corrector,
166
+ corrector_kwargs=corrector_kwargs,
167
+ x_T=x_T,
168
+ log_every_t=log_every_t,
169
+ unconditional_guidance_scale=unconditional_guidance_scale,
170
+ unconditional_conditioning=unconditional_conditioning,
171
+ verbose=verbose,
172
+ **kwargs,
173
+ )
174
+ return samples, intermediates
175
+
176
+ @torch.no_grad()
177
+ def ddim_sampling(
178
+ self,
179
+ cond,
180
+ shape,
181
+ x_T=None,
182
+ ddim_use_original_steps=False,
183
+ callback=None,
184
+ timesteps=None,
185
+ quantize_denoised=False,
186
+ mask=None,
187
+ x0=None,
188
+ img_callback=None,
189
+ log_every_t=100,
190
+ temperature=1.0,
191
+ noise_dropout=0.0,
192
+ score_corrector=None,
193
+ corrector_kwargs=None,
194
+ unconditional_guidance_scale=1.0,
195
+ unconditional_conditioning=None,
196
+ verbose=True,
197
+ cond_tau=1.0,
198
+ target_size=None,
199
+ start_timesteps=None,
200
+ **kwargs,
201
+ ):
202
+ device = self.model.betas.device
203
+ print("ddim device", device)
204
+ b = shape[0]
205
+ if x_T is None:
206
+ img = torch.randn(shape, device=device)
207
+ else:
208
+ img = x_T
209
+
210
+ if timesteps is None:
211
+ timesteps = (
212
+ self.ddpm_num_timesteps
213
+ if ddim_use_original_steps
214
+ else self.ddim_timesteps
215
+ )
216
+ elif timesteps is not None and not ddim_use_original_steps:
217
+ subset_end = (
218
+ int(
219
+ min(timesteps / self.ddim_timesteps.shape[0], 1)
220
+ * self.ddim_timesteps.shape[0]
221
+ )
222
+ - 1
223
+ )
224
+ timesteps = self.ddim_timesteps[:subset_end]
225
+
226
+ intermediates = {"x_inter": [img], "pred_x0": [img]}
227
+ time_range = (
228
+ reversed(range(0, timesteps))
229
+ if ddim_use_original_steps
230
+ else np.flip(timesteps)
231
+ )
232
+ total_steps = timesteps if ddim_use_original_steps else timesteps.shape[0]
233
+ if verbose:
234
+ iterator = tqdm(time_range, desc="DDIM Sampler", total=total_steps)
235
+ else:
236
+ iterator = time_range
237
+
238
+ init_x0 = False
239
+ clean_cond = kwargs.pop("clean_cond", False)
240
+ for i, step in enumerate(iterator):
241
+ index = total_steps - i - 1
242
+ ts = torch.full((b,), step, device=device, dtype=torch.long)
243
+ if start_timesteps is not None:
244
+ assert x0 is not None
245
+ if step > start_timesteps * time_range[0]:
246
+ continue
247
+ elif not init_x0:
248
+ img = self.model.q_sample(x0, ts)
249
+ init_x0 = True
250
+
251
+ # use mask to blend noised original latent (img_orig) & new sampled latent (img)
252
+ if mask is not None:
253
+ assert x0 is not None
254
+ if clean_cond:
255
+ img_orig = x0
256
+ else:
257
+ img_orig = self.model.q_sample(
258
+ x0, ts
259
+ ) # TODO: deterministic forward pass? <ddim inversion>
260
+ img = (
261
+ img_orig * mask + (1.0 - mask) * img
262
+ ) # keep original & modify use img
263
+
264
+ index_clip = int((1 - cond_tau) * total_steps)
265
+ if index <= index_clip and target_size is not None:
266
+ target_size_ = [
267
+ target_size[0],
268
+ target_size[1] // 8,
269
+ target_size[2] // 8,
270
+ ]
271
+ img = torch.nn.functional.interpolate(
272
+ img,
273
+ size=target_size_,
274
+ mode="nearest",
275
+ )
276
+ outs = self.p_sample_ddim(
277
+ img,
278
+ cond,
279
+ ts,
280
+ index=index,
281
+ use_original_steps=ddim_use_original_steps,
282
+ quantize_denoised=quantize_denoised,
283
+ temperature=temperature,
284
+ noise_dropout=noise_dropout,
285
+ score_corrector=score_corrector,
286
+ corrector_kwargs=corrector_kwargs,
287
+ unconditional_guidance_scale=unconditional_guidance_scale,
288
+ unconditional_conditioning=unconditional_conditioning,
289
+ x0=x0,
290
+ **kwargs,
291
+ )
292
+
293
+ img, pred_x0 = outs
294
+ if callback:
295
+ callback(i)
296
+ if img_callback:
297
+ img_callback(pred_x0, i)
298
+
299
+ if index % log_every_t == 0 or index == total_steps - 1:
300
+ intermediates["x_inter"].append(img)
301
+ intermediates["pred_x0"].append(pred_x0)
302
+
303
+ return img, intermediates
304
+
305
+ @torch.no_grad()
306
+ def p_sample_ddim(
307
+ self,
308
+ x,
309
+ c,
310
+ t,
311
+ index,
312
+ repeat_noise=False,
313
+ use_original_steps=False,
314
+ quantize_denoised=False,
315
+ temperature=1.0,
316
+ noise_dropout=0.0,
317
+ score_corrector=None,
318
+ corrector_kwargs=None,
319
+ unconditional_guidance_scale=1.0,
320
+ unconditional_conditioning=None,
321
+ uc_type=None,
322
+ conditional_guidance_scale_temporal=None,
323
+ **kwargs,
324
+ ):
325
+ b, *_, device = *x.shape, x.device
326
+ if x.dim() == 5:
327
+ is_video = True
328
+ else:
329
+ is_video = False
330
+ if unconditional_conditioning is None or unconditional_guidance_scale == 1.0:
331
+ e_t = self.model.apply_model(x, t, c, **kwargs) # unet denoiser
332
+ else:
333
+ # with unconditional condition
334
+ if isinstance(c, torch.Tensor):
335
+ e_t = self.model.apply_model(x, t, c, **kwargs)
336
+ e_t_uncond = self.model.apply_model(
337
+ x, t, unconditional_conditioning, **kwargs
338
+ )
339
+ elif isinstance(c, dict):
340
+ e_t = self.model.apply_model(x, t, c, **kwargs)
341
+ e_t_uncond = self.model.apply_model(
342
+ x, t, unconditional_conditioning, **kwargs
343
+ )
344
+ else:
345
+ raise NotImplementedError
346
+ # text cfg
347
+ if uc_type is None:
348
+ e_t = e_t_uncond + unconditional_guidance_scale * (e_t - e_t_uncond)
349
+ else:
350
+ if uc_type == "cfg_original":
351
+ e_t = e_t + unconditional_guidance_scale * (e_t - e_t_uncond)
352
+ elif uc_type == "cfg_ours":
353
+ e_t = e_t + unconditional_guidance_scale * (e_t_uncond - e_t)
354
+ else:
355
+ raise NotImplementedError
356
+ # temporal guidance
357
+ if conditional_guidance_scale_temporal is not None:
358
+ e_t_temporal = self.model.apply_model(x, t, c, **kwargs)
359
+ e_t_image = self.model.apply_model(
360
+ x, t, c, no_temporal_attn=True, **kwargs
361
+ )
362
+ e_t = e_t + conditional_guidance_scale_temporal * (
363
+ e_t_temporal - e_t_image
364
+ )
365
+
366
+ if score_corrector is not None:
367
+ assert self.model.parameterization == "eps"
368
+ e_t = score_corrector.modify_score(
369
+ self.model, e_t, x, t, c, **corrector_kwargs
370
+ )
371
+
372
+ alphas = self.model.alphas_cumprod if use_original_steps else self.ddim_alphas
373
+ alphas_prev = (
374
+ self.model.alphas_cumprod_prev
375
+ if use_original_steps
376
+ else self.ddim_alphas_prev
377
+ )
378
+ sqrt_one_minus_alphas = (
379
+ self.model.sqrt_one_minus_alphas_cumprod
380
+ if use_original_steps
381
+ else self.ddim_sqrt_one_minus_alphas
382
+ )
383
+ sigmas = (
384
+ self.model.ddim_sigmas_for_original_num_steps
385
+ if use_original_steps
386
+ else self.ddim_sigmas
387
+ )
388
+ # select parameters corresponding to the currently considered timestep
389
+
390
+ if is_video:
391
+ size = (b, 1, 1, 1, 1)
392
+ else:
393
+ size = (b, 1, 1, 1)
394
+ a_t = torch.full(size, alphas[index], device=device)
395
+ a_prev = torch.full(size, alphas_prev[index], device=device)
396
+ sigma_t = torch.full(size, sigmas[index], device=device)
397
+ sqrt_one_minus_at = torch.full(
398
+ size, sqrt_one_minus_alphas[index], device=device
399
+ )
400
+
401
+ # current prediction for x_0
402
+ pred_x0 = (x - sqrt_one_minus_at * e_t) / a_t.sqrt()
403
+ if quantize_denoised:
404
+ pred_x0, _, *_ = self.model.first_stage_model.quantize(pred_x0)
405
+ # direction pointing to x_t
406
+ dir_xt = (1.0 - a_prev - sigma_t**2).sqrt() * e_t
407
+
408
+ noise = sigma_t * noise_like(x.shape, device, repeat_noise) * temperature
409
+ if noise_dropout > 0.0:
410
+ noise = torch.nn.functional.dropout(noise, p=noise_dropout)
411
+
412
+ alphas = self.model.alphas_cumprod if use_original_steps else self.ddim_alphas
413
+ if self.use_scale:
414
+ scale_arr = (
415
+ self.model.scale_arr if use_original_steps else self.ddim_scale_arr
416
+ )
417
+ scale_t = torch.full(size, scale_arr[index], device=device)
418
+ scale_arr_prev = (
419
+ self.model.scale_arr_prev
420
+ if use_original_steps
421
+ else self.ddim_scale_arr_prev
422
+ )
423
+ scale_t_prev = torch.full(size, scale_arr_prev[index], device=device)
424
+ pred_x0 /= scale_t
425
+ x_prev = a_prev.sqrt() * scale_t_prev * pred_x0 + dir_xt + noise
426
+ else:
427
+ x_prev = a_prev.sqrt() * pred_x0 + dir_xt + noise
428
+
429
+ return x_prev, pred_x0
430
+
431
+ @torch.no_grad()
432
+ def stochastic_encode(self, x0, t, use_original_steps=False, noise=None):
433
+ # fast, but does not allow for exact reconstruction
434
+ # t serves as an index to gather the correct alphas
435
+ if use_original_steps:
436
+ sqrt_alphas_cumprod = self.sqrt_alphas_cumprod
437
+ sqrt_one_minus_alphas_cumprod = self.sqrt_one_minus_alphas_cumprod
438
+ else:
439
+ sqrt_alphas_cumprod = torch.sqrt(self.ddim_alphas)
440
+ sqrt_one_minus_alphas_cumprod = self.ddim_sqrt_one_minus_alphas
441
+
442
+ if noise is None:
443
+ noise = torch.randn_like(x0)
444
+
445
+ def extract_into_tensor(a, t, x_shape):
446
+ b, *_ = t.shape
447
+ out = a.gather(-1, t)
448
+ return out.reshape(b, *((1,) * (len(x_shape) - 1)))
449
+
450
+ return (
451
+ extract_into_tensor(sqrt_alphas_cumprod, t, x0.shape) * x0
452
+ + extract_into_tensor(sqrt_one_minus_alphas_cumprod, t, x0.shape) * noise
453
+ )
454
+
455
+ @torch.no_grad()
456
+ def decode(
457
+ self,
458
+ x_latent,
459
+ cond,
460
+ t_start,
461
+ unconditional_guidance_scale=1.0,
462
+ unconditional_conditioning=None,
463
+ use_original_steps=False,
464
+ ):
465
+
466
+ timesteps = (
467
+ np.arange(self.ddpm_num_timesteps)
468
+ if use_original_steps
469
+ else self.ddim_timesteps
470
+ )
471
+ timesteps = timesteps[:t_start]
472
+
473
+ time_range = np.flip(timesteps)
474
+ total_steps = timesteps.shape[0]
475
+ print(f"Running DDIM Sampling with {total_steps} timesteps")
476
+
477
+ iterator = tqdm(time_range, desc="Decoding image", total=total_steps)
478
+ x_dec = x_latent
479
+ for i, step in enumerate(iterator):
480
+ index = total_steps - i - 1
481
+ ts = torch.full(
482
+ (x_latent.shape[0],), step, device=x_latent.device, dtype=torch.long
483
+ )
484
+ x_dec, _ = self.p_sample_ddim(
485
+ x_dec,
486
+ cond,
487
+ ts,
488
+ index=index,
489
+ use_original_steps=use_original_steps,
490
+ unconditional_guidance_scale=unconditional_guidance_scale,
491
+ unconditional_conditioning=unconditional_conditioning,
492
+ )
493
+ return x_dec
lvdm/models/utils_diffusion.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import numpy as np
3
+ from einops import repeat
4
+ import torch
5
+ import torch.nn.functional as F
6
+
7
+
8
+ def timestep_embedding(timesteps, dim, max_period=10000, repeat_only=False):
9
+ """
10
+ Create sinusoidal timestep embeddings.
11
+ :param timesteps: a 1-D Tensor of N indices, one per batch element.
12
+ These may be fractional.
13
+ :param dim: the dimension of the output.
14
+ :param max_period: controls the minimum frequency of the embeddings.
15
+ :return: an [N x dim] Tensor of positional embeddings.
16
+ """
17
+ if not repeat_only:
18
+ half = dim // 2
19
+ freqs = torch.exp(
20
+ -math.log(max_period)
21
+ * torch.arange(start=0, end=half, dtype=torch.float32)
22
+ / half
23
+ ).to(device=timesteps.device)
24
+ args = timesteps[:, None].float() * freqs[None]
25
+ embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1)
26
+ if dim % 2:
27
+ embedding = torch.cat(
28
+ [embedding, torch.zeros_like(embedding[:, :1])], dim=-1
29
+ )
30
+ else:
31
+ embedding = repeat(timesteps, "b -> b d", d=dim)
32
+ return embedding
33
+
34
+
35
+ def make_beta_schedule(
36
+ schedule, n_timestep, linear_start=1e-4, linear_end=2e-2, cosine_s=8e-3
37
+ ):
38
+ if schedule == "linear":
39
+ betas = (
40
+ torch.linspace(
41
+ linear_start**0.5, linear_end**0.5, n_timestep, dtype=torch.float64
42
+ )
43
+ ** 2
44
+ )
45
+
46
+ elif schedule == "cosine":
47
+ timesteps = (
48
+ torch.arange(n_timestep + 1, dtype=torch.float64) / n_timestep + cosine_s
49
+ )
50
+ alphas = timesteps / (1 + cosine_s) * np.pi / 2
51
+ alphas = torch.cos(alphas).pow(2)
52
+ alphas = alphas / alphas[0]
53
+ betas = 1 - alphas[1:] / alphas[:-1]
54
+ betas = np.clip(betas, a_min=0, a_max=0.999)
55
+
56
+ elif schedule == "sqrt_linear":
57
+ betas = torch.linspace(
58
+ linear_start, linear_end, n_timestep, dtype=torch.float64
59
+ )
60
+ elif schedule == "sqrt":
61
+ betas = (
62
+ torch.linspace(linear_start, linear_end, n_timestep, dtype=torch.float64)
63
+ ** 0.5
64
+ )
65
+ else:
66
+ raise ValueError(f"schedule '{schedule}' unknown.")
67
+ return betas.numpy()
68
+
69
+
70
+ def make_ddim_timesteps(
71
+ ddim_discr_method, num_ddim_timesteps, num_ddpm_timesteps, verbose=True
72
+ ):
73
+ if ddim_discr_method == "uniform":
74
+ c = num_ddpm_timesteps // num_ddim_timesteps
75
+ ddim_timesteps = np.asarray(list(range(0, num_ddpm_timesteps, c)))
76
+ elif ddim_discr_method == "quad":
77
+ ddim_timesteps = (
78
+ (np.linspace(0, np.sqrt(num_ddpm_timesteps * 0.8), num_ddim_timesteps)) ** 2
79
+ ).astype(int)
80
+ else:
81
+ raise NotImplementedError(
82
+ f'There is no ddim discretization method called "{ddim_discr_method}"'
83
+ )
84
+
85
+ # assert ddim_timesteps.shape[0] == num_ddim_timesteps
86
+ # add one to get the final alpha values right (the ones from first scale to data during sampling)
87
+ steps_out = ddim_timesteps + 1
88
+ if verbose:
89
+ print(f"Selected timesteps for ddim sampler: {steps_out}")
90
+ return steps_out
91
+
92
+
93
+ def make_ddim_sampling_parameters(alphacums, ddim_timesteps, eta, verbose=True):
94
+ # select alphas for computing the variance schedule
95
+ # print(f'ddim_timesteps={ddim_timesteps}, len_alphacums={len(alphacums)}')
96
+ alphas = alphacums[ddim_timesteps]
97
+ alphas_prev = np.asarray([alphacums[0]] + alphacums[ddim_timesteps[:-1]].tolist())
98
+
99
+ # according the the formula provided in https://arxiv.org/abs/2010.02502
100
+ sigmas = eta * np.sqrt(
101
+ (1 - alphas_prev) / (1 - alphas) * (1 - alphas / alphas_prev)
102
+ )
103
+ if verbose:
104
+ print(
105
+ f"Selected alphas for ddim sampler: a_t: {alphas}; a_(t-1): {alphas_prev}"
106
+ )
107
+ print(
108
+ f"For the chosen value of eta, which is {eta}, "
109
+ f"this results in the following sigma_t schedule for ddim sampler {sigmas}"
110
+ )
111
+ return sigmas, alphas, alphas_prev
112
+
113
+
114
+ def betas_for_alpha_bar(num_diffusion_timesteps, alpha_bar, max_beta=0.999):
115
+ """
116
+ Create a beta schedule that discretizes the given alpha_t_bar function,
117
+ which defines the cumulative product of (1-beta) over time from t = [0,1].
118
+ :param num_diffusion_timesteps: the number of betas to produce.
119
+ :param alpha_bar: a lambda that takes an argument t from 0 to 1 and
120
+ produces the cumulative product of (1-beta) up to that
121
+ part of the diffusion process.
122
+ :param max_beta: the maximum beta to use; use values lower than 1 to
123
+ prevent singularities.
124
+ """
125
+ betas = []
126
+ for i in range(num_diffusion_timesteps):
127
+ t1 = i / num_diffusion_timesteps
128
+ t2 = (i + 1) / num_diffusion_timesteps
129
+ betas.append(min(1 - alpha_bar(t2) / alpha_bar(t1), max_beta))
130
+ return np.array(betas)
lvdm/modules/__pycache__/attention.cpython-311.pyc ADDED
Binary file (29.6 kB). View file
 
lvdm/modules/attention.py ADDED
@@ -0,0 +1,584 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from functools import partial
2
+ import torch
3
+ from torch import nn, einsum
4
+ import torch.nn.functional as F
5
+ from einops import rearrange, repeat
6
+
7
+ try:
8
+ import xformers
9
+ import xformers.ops
10
+
11
+ XFORMERS_IS_AVAILBLE = True
12
+ except:
13
+ XFORMERS_IS_AVAILBLE = False
14
+ from lvdm.common import (
15
+ checkpoint,
16
+ exists,
17
+ default,
18
+ )
19
+ from lvdm.basics import (
20
+ zero_module,
21
+ )
22
+
23
+
24
+ class RelativePosition(nn.Module):
25
+ """https://github.com/evelinehong/Transformer_Relative_Position_PyTorch/blob/master/relative_position.py"""
26
+
27
+ def __init__(self, num_units, max_relative_position):
28
+ super().__init__()
29
+ self.num_units = num_units
30
+ self.max_relative_position = max_relative_position
31
+ self.embeddings_table = nn.Parameter(
32
+ torch.Tensor(max_relative_position * 2 + 1, num_units)
33
+ )
34
+ nn.init.xavier_uniform_(self.embeddings_table)
35
+
36
+ def forward(self, length_q, length_k):
37
+ device = self.embeddings_table.device
38
+ range_vec_q = torch.arange(length_q, device=device)
39
+ range_vec_k = torch.arange(length_k, device=device)
40
+ distance_mat = range_vec_k[None, :] - range_vec_q[:, None]
41
+ distance_mat_clipped = torch.clamp(
42
+ distance_mat, -self.max_relative_position, self.max_relative_position
43
+ )
44
+ final_mat = distance_mat_clipped + self.max_relative_position
45
+ final_mat = final_mat.long()
46
+ embeddings = self.embeddings_table[final_mat]
47
+ return embeddings
48
+
49
+
50
+ class CrossAttention(nn.Module):
51
+
52
+ def __init__(
53
+ self,
54
+ query_dim,
55
+ context_dim=None,
56
+ heads=8,
57
+ dim_head=64,
58
+ dropout=0.0,
59
+ relative_position=False,
60
+ temporal_length=None,
61
+ img_cross_attention=False,
62
+ ):
63
+ super().__init__()
64
+ inner_dim = dim_head * heads
65
+ context_dim = default(context_dim, query_dim)
66
+
67
+ self.scale = dim_head**-0.5
68
+ self.heads = heads
69
+ self.dim_head = dim_head
70
+ self.to_q = nn.Linear(query_dim, inner_dim, bias=False)
71
+ self.to_k = nn.Linear(context_dim, inner_dim, bias=False)
72
+ self.to_v = nn.Linear(context_dim, inner_dim, bias=False)
73
+ self.to_out = nn.Sequential(
74
+ nn.Linear(inner_dim, query_dim), nn.Dropout(dropout)
75
+ )
76
+
77
+ self.image_cross_attention_scale = 1.0
78
+ self.text_context_len = 77
79
+ self.img_cross_attention = img_cross_attention
80
+ if self.img_cross_attention:
81
+ self.to_k_ip = nn.Linear(context_dim, inner_dim, bias=False)
82
+ self.to_v_ip = nn.Linear(context_dim, inner_dim, bias=False)
83
+
84
+ self.relative_position = relative_position
85
+ if self.relative_position:
86
+ assert temporal_length is not None
87
+ self.relative_position_k = RelativePosition(
88
+ num_units=dim_head, max_relative_position=temporal_length
89
+ )
90
+ self.relative_position_v = RelativePosition(
91
+ num_units=dim_head, max_relative_position=temporal_length
92
+ )
93
+ else:
94
+ ## only used for spatial attention, while NOT for temporal attention
95
+ if XFORMERS_IS_AVAILBLE and temporal_length is None:
96
+ self.forward = self.efficient_forward
97
+
98
+ def forward(self, x, context=None, mask=None):
99
+ h = self.heads
100
+
101
+ q = self.to_q(x)
102
+ context = default(context, x)
103
+ ## considering image token additionally
104
+ if context is not None and self.img_cross_attention:
105
+ context, context_img = (
106
+ context[:, : self.text_context_len, :],
107
+ context[:, self.text_context_len :, :],
108
+ )
109
+ k = self.to_k(context)
110
+ v = self.to_v(context)
111
+ k_ip = self.to_k_ip(context_img)
112
+ v_ip = self.to_v_ip(context_img)
113
+ else:
114
+ k = self.to_k(context)
115
+ v = self.to_v(context)
116
+
117
+ q, k, v = map(lambda t: rearrange(t, "b n (h d) -> (b h) n d", h=h), (q, k, v))
118
+ sim = torch.einsum("b i d, b j d -> b i j", q, k) * self.scale
119
+ if self.relative_position:
120
+ len_q, len_k, len_v = q.shape[1], k.shape[1], v.shape[1]
121
+ k2 = self.relative_position_k(len_q, len_k)
122
+ sim2 = einsum("b t d, t s d -> b t s", q, k2) * self.scale # TODO check
123
+ sim += sim2
124
+ del k
125
+
126
+ if exists(mask):
127
+ ## feasible for causal attention mask only
128
+ max_neg_value = -torch.finfo(sim.dtype).max
129
+ mask = repeat(mask, "b i j -> (b h) i j", h=h)
130
+ sim.masked_fill_(~(mask > 0.5), max_neg_value)
131
+
132
+ # attention, what we cannot get enough of
133
+ sim = sim.softmax(dim=-1)
134
+ out = torch.einsum("b i j, b j d -> b i d", sim, v)
135
+ if self.relative_position:
136
+ v2 = self.relative_position_v(len_q, len_v)
137
+ out2 = einsum("b t s, t s d -> b t d", sim, v2) # TODO check
138
+ out += out2
139
+ out = rearrange(out, "(b h) n d -> b n (h d)", h=h)
140
+
141
+ ## considering image token additionally
142
+ if context is not None and self.img_cross_attention:
143
+ k_ip, v_ip = map(
144
+ lambda t: rearrange(t, "b n (h d) -> (b h) n d", h=h), (k_ip, v_ip)
145
+ )
146
+ sim_ip = torch.einsum("b i d, b j d -> b i j", q, k_ip) * self.scale
147
+ del k_ip
148
+ sim_ip = sim_ip.softmax(dim=-1)
149
+ out_ip = torch.einsum("b i j, b j d -> b i d", sim_ip, v_ip)
150
+ out_ip = rearrange(out_ip, "(b h) n d -> b n (h d)", h=h)
151
+ out = out + self.image_cross_attention_scale * out_ip
152
+ del q
153
+
154
+ return self.to_out(out)
155
+
156
+ def efficient_forward(self, x, context=None, mask=None):
157
+ q = self.to_q(x)
158
+ context = default(context, x)
159
+
160
+ ## considering image token additionally
161
+ if context is not None and self.img_cross_attention:
162
+ context, context_img = (
163
+ context[:, : self.text_context_len, :],
164
+ context[:, self.text_context_len :, :],
165
+ )
166
+ k = self.to_k(context)
167
+ v = self.to_v(context)
168
+ k_ip = self.to_k_ip(context_img)
169
+ v_ip = self.to_v_ip(context_img)
170
+ else:
171
+ k = self.to_k(context)
172
+ v = self.to_v(context)
173
+
174
+ b, _, _ = q.shape
175
+ q, k, v = map(
176
+ lambda t: t.unsqueeze(3)
177
+ .reshape(b, t.shape[1], self.heads, self.dim_head)
178
+ .permute(0, 2, 1, 3)
179
+ .reshape(b * self.heads, t.shape[1], self.dim_head)
180
+ .contiguous(),
181
+ (q, k, v),
182
+ )
183
+ # actually compute the attention, what we cannot get enough of
184
+ out = xformers.ops.memory_efficient_attention(q, k, v, attn_bias=None, op=None)
185
+
186
+ ## considering image token additionally
187
+ if context is not None and self.img_cross_attention:
188
+ k_ip, v_ip = map(
189
+ lambda t: t.unsqueeze(3)
190
+ .reshape(b, t.shape[1], self.heads, self.dim_head)
191
+ .permute(0, 2, 1, 3)
192
+ .reshape(b * self.heads, t.shape[1], self.dim_head)
193
+ .contiguous(),
194
+ (k_ip, v_ip),
195
+ )
196
+ out_ip = xformers.ops.memory_efficient_attention(
197
+ q, k_ip, v_ip, attn_bias=None, op=None
198
+ )
199
+ out_ip = (
200
+ out_ip.unsqueeze(0)
201
+ .reshape(b, self.heads, out.shape[1], self.dim_head)
202
+ .permute(0, 2, 1, 3)
203
+ .reshape(b, out.shape[1], self.heads * self.dim_head)
204
+ )
205
+
206
+ if exists(mask):
207
+ raise NotImplementedError
208
+ out = (
209
+ out.unsqueeze(0)
210
+ .reshape(b, self.heads, out.shape[1], self.dim_head)
211
+ .permute(0, 2, 1, 3)
212
+ .reshape(b, out.shape[1], self.heads * self.dim_head)
213
+ )
214
+ if context is not None and self.img_cross_attention:
215
+ out = out + self.image_cross_attention_scale * out_ip
216
+ return self.to_out(out)
217
+
218
+
219
+ class BasicTransformerBlock(nn.Module):
220
+
221
+ def __init__(
222
+ self,
223
+ dim,
224
+ n_heads,
225
+ d_head,
226
+ dropout=0.0,
227
+ context_dim=None,
228
+ gated_ff=True,
229
+ checkpoint=True,
230
+ disable_self_attn=False,
231
+ attention_cls=None,
232
+ img_cross_attention=False,
233
+ ):
234
+ super().__init__()
235
+ attn_cls = CrossAttention if attention_cls is None else attention_cls
236
+ self.disable_self_attn = disable_self_attn
237
+ self.attn1 = attn_cls(
238
+ query_dim=dim,
239
+ heads=n_heads,
240
+ dim_head=d_head,
241
+ dropout=dropout,
242
+ context_dim=context_dim if self.disable_self_attn else None,
243
+ )
244
+ self.ff = FeedForward(dim, dropout=dropout, glu=gated_ff)
245
+ self.attn2 = attn_cls(
246
+ query_dim=dim,
247
+ context_dim=context_dim,
248
+ heads=n_heads,
249
+ dim_head=d_head,
250
+ dropout=dropout,
251
+ img_cross_attention=img_cross_attention,
252
+ )
253
+ self.norm1 = nn.LayerNorm(dim)
254
+ self.norm2 = nn.LayerNorm(dim)
255
+ self.norm3 = nn.LayerNorm(dim)
256
+ self.checkpoint = checkpoint
257
+
258
+ def forward(self, x, context=None, mask=None):
259
+ ## implementation tricks: because checkpointing doesn't support non-tensor (e.g. None or scalar) arguments
260
+ input_tuple = (
261
+ x,
262
+ ) ## should not be (x), otherwise *input_tuple will decouple x into multiple arguments
263
+ if context is not None:
264
+ input_tuple = (x, context)
265
+ if mask is not None:
266
+ forward_mask = partial(self._forward, mask=mask)
267
+ return checkpoint(forward_mask, (x,), self.parameters(), self.checkpoint)
268
+ if context is not None and mask is not None:
269
+ input_tuple = (x, context, mask)
270
+ return checkpoint(
271
+ self._forward, input_tuple, self.parameters(), self.checkpoint
272
+ )
273
+
274
+ def _forward(self, x, context=None, mask=None):
275
+ x = (
276
+ self.attn1(
277
+ self.norm1(x),
278
+ context=context if self.disable_self_attn else None,
279
+ mask=mask,
280
+ )
281
+ + x
282
+ )
283
+ x = self.attn2(self.norm2(x), context=context, mask=mask) + x
284
+ x = self.ff(self.norm3(x)) + x
285
+ return x
286
+
287
+
288
+ class SpatialTransformer(nn.Module):
289
+ """
290
+ Transformer block for image-like data in spatial axis.
291
+ First, project the input (aka embedding)
292
+ and reshape to b, t, d.
293
+ Then apply standard transformer action.
294
+ Finally, reshape to image
295
+ NEW: use_linear for more efficiency instead of the 1x1 convs
296
+ """
297
+
298
+ def __init__(
299
+ self,
300
+ in_channels,
301
+ n_heads,
302
+ d_head,
303
+ depth=1,
304
+ dropout=0.0,
305
+ context_dim=None,
306
+ use_checkpoint=True,
307
+ disable_self_attn=False,
308
+ use_linear=False,
309
+ img_cross_attention=False,
310
+ ):
311
+ super().__init__()
312
+ self.in_channels = in_channels
313
+ inner_dim = n_heads * d_head
314
+ self.norm = torch.nn.GroupNorm(
315
+ num_groups=32, num_channels=in_channels, eps=1e-6, affine=True
316
+ )
317
+ if not use_linear:
318
+ self.proj_in = nn.Conv2d(
319
+ in_channels, inner_dim, kernel_size=1, stride=1, padding=0
320
+ )
321
+ else:
322
+ self.proj_in = nn.Linear(in_channels, inner_dim)
323
+
324
+ self.transformer_blocks = nn.ModuleList(
325
+ [
326
+ BasicTransformerBlock(
327
+ inner_dim,
328
+ n_heads,
329
+ d_head,
330
+ dropout=dropout,
331
+ context_dim=context_dim,
332
+ img_cross_attention=img_cross_attention,
333
+ disable_self_attn=disable_self_attn,
334
+ checkpoint=use_checkpoint,
335
+ )
336
+ for d in range(depth)
337
+ ]
338
+ )
339
+ if not use_linear:
340
+ self.proj_out = zero_module(
341
+ nn.Conv2d(inner_dim, in_channels, kernel_size=1, stride=1, padding=0)
342
+ )
343
+ else:
344
+ self.proj_out = zero_module(nn.Linear(inner_dim, in_channels))
345
+ self.use_linear = use_linear
346
+
347
+ def forward(self, x, context=None):
348
+ b, c, h, w = x.shape
349
+ x_in = x
350
+ x = self.norm(x)
351
+ if not self.use_linear:
352
+ x = self.proj_in(x)
353
+ x = rearrange(x, "b c h w -> b (h w) c").contiguous()
354
+ if self.use_linear:
355
+ x = self.proj_in(x)
356
+ for i, block in enumerate(self.transformer_blocks):
357
+ x = block(x, context=context)
358
+ if self.use_linear:
359
+ x = self.proj_out(x)
360
+ x = rearrange(x, "b (h w) c -> b c h w", h=h, w=w).contiguous()
361
+ if not self.use_linear:
362
+ x = self.proj_out(x)
363
+ return x + x_in
364
+
365
+
366
+ class TemporalTransformer(nn.Module):
367
+ """
368
+ Transformer block for image-like data in temporal axis.
369
+ First, reshape to b, t, d.
370
+ Then apply standard transformer action.
371
+ Finally, reshape to image
372
+ """
373
+
374
+ def __init__(
375
+ self,
376
+ in_channels,
377
+ n_heads,
378
+ d_head,
379
+ depth=1,
380
+ dropout=0.0,
381
+ context_dim=None,
382
+ use_checkpoint=True,
383
+ use_linear=False,
384
+ only_self_att=True,
385
+ causal_attention=False,
386
+ relative_position=False,
387
+ temporal_length=None,
388
+ ):
389
+ super().__init__()
390
+ self.only_self_att = only_self_att
391
+ self.relative_position = relative_position
392
+ self.causal_attention = causal_attention
393
+ self.in_channels = in_channels
394
+ inner_dim = n_heads * d_head
395
+ self.norm = torch.nn.GroupNorm(
396
+ num_groups=32, num_channels=in_channels, eps=1e-6, affine=True
397
+ )
398
+ self.proj_in = nn.Conv1d(
399
+ in_channels, inner_dim, kernel_size=1, stride=1, padding=0
400
+ )
401
+ if not use_linear:
402
+ self.proj_in = nn.Conv1d(
403
+ in_channels, inner_dim, kernel_size=1, stride=1, padding=0
404
+ )
405
+ else:
406
+ self.proj_in = nn.Linear(in_channels, inner_dim)
407
+
408
+ if relative_position:
409
+ assert temporal_length is not None
410
+ attention_cls = partial(
411
+ CrossAttention, relative_position=True, temporal_length=temporal_length
412
+ )
413
+ else:
414
+ attention_cls = None
415
+ if self.causal_attention:
416
+ assert temporal_length is not None
417
+ self.mask = torch.tril(torch.ones([1, temporal_length, temporal_length]))
418
+
419
+ if self.only_self_att:
420
+ context_dim = None
421
+ self.transformer_blocks = nn.ModuleList(
422
+ [
423
+ BasicTransformerBlock(
424
+ inner_dim,
425
+ n_heads,
426
+ d_head,
427
+ dropout=dropout,
428
+ context_dim=context_dim,
429
+ attention_cls=attention_cls,
430
+ checkpoint=use_checkpoint,
431
+ )
432
+ for d in range(depth)
433
+ ]
434
+ )
435
+ if not use_linear:
436
+ self.proj_out = zero_module(
437
+ nn.Conv1d(inner_dim, in_channels, kernel_size=1, stride=1, padding=0)
438
+ )
439
+ else:
440
+ self.proj_out = zero_module(nn.Linear(inner_dim, in_channels))
441
+ self.use_linear = use_linear
442
+
443
+ def forward(self, x, context=None):
444
+ b, c, t, h, w = x.shape
445
+ x_in = x
446
+ x = self.norm(x)
447
+ x = rearrange(x, "b c t h w -> (b h w) c t").contiguous()
448
+ if not self.use_linear:
449
+ x = self.proj_in(x)
450
+ x = rearrange(x, "bhw c t -> bhw t c").contiguous()
451
+ if self.use_linear:
452
+ x = self.proj_in(x)
453
+
454
+ if self.causal_attention:
455
+ mask = self.mask.to(x.device)
456
+ mask = repeat(mask, "l i j -> (l bhw) i j", bhw=b * h * w)
457
+ else:
458
+ mask = None
459
+
460
+ if self.only_self_att:
461
+ ## note: if no context is given, cross-attention defaults to self-attention
462
+ for i, block in enumerate(self.transformer_blocks):
463
+ x = block(x, mask=mask)
464
+ x = rearrange(x, "(b hw) t c -> b hw t c", b=b).contiguous()
465
+ else:
466
+ x = rearrange(x, "(b hw) t c -> b hw t c", b=b).contiguous()
467
+ context = rearrange(context, "(b t) l con -> b t l con", t=t).contiguous()
468
+ for i, block in enumerate(self.transformer_blocks):
469
+ # calculate each batch one by one (since number in shape could not greater then 65,535 for some package)
470
+ for j in range(b):
471
+ context_j = repeat(
472
+ context[j], "t l con -> (t r) l con", r=(h * w) // t, t=t
473
+ ).contiguous()
474
+ ## note: causal mask will not applied in cross-attention case
475
+ x[j] = block(x[j], context=context_j)
476
+
477
+ if self.use_linear:
478
+ x = self.proj_out(x)
479
+ x = rearrange(x, "b (h w) t c -> b c t h w", h=h, w=w).contiguous()
480
+ if not self.use_linear:
481
+ x = rearrange(x, "b hw t c -> (b hw) c t").contiguous()
482
+ x = self.proj_out(x)
483
+ x = rearrange(x, "(b h w) c t -> b c t h w", b=b, h=h, w=w).contiguous()
484
+
485
+ return x + x_in
486
+
487
+
488
+ class GEGLU(nn.Module):
489
+ def __init__(self, dim_in, dim_out):
490
+ super().__init__()
491
+ self.proj = nn.Linear(dim_in, dim_out * 2)
492
+
493
+ def forward(self, x):
494
+ x, gate = self.proj(x).chunk(2, dim=-1)
495
+ return x * F.gelu(gate)
496
+
497
+
498
+ class FeedForward(nn.Module):
499
+ def __init__(self, dim, dim_out=None, mult=4, glu=False, dropout=0.0):
500
+ super().__init__()
501
+ inner_dim = int(dim * mult)
502
+ dim_out = default(dim_out, dim)
503
+ project_in = (
504
+ nn.Sequential(nn.Linear(dim, inner_dim), nn.GELU())
505
+ if not glu
506
+ else GEGLU(dim, inner_dim)
507
+ )
508
+
509
+ self.net = nn.Sequential(
510
+ project_in, nn.Dropout(dropout), nn.Linear(inner_dim, dim_out)
511
+ )
512
+
513
+ def forward(self, x):
514
+ return self.net(x)
515
+
516
+
517
+ class LinearAttention(nn.Module):
518
+ def __init__(self, dim, heads=4, dim_head=32):
519
+ super().__init__()
520
+ self.heads = heads
521
+ hidden_dim = dim_head * heads
522
+ self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias=False)
523
+ self.to_out = nn.Conv2d(hidden_dim, dim, 1)
524
+
525
+ def forward(self, x):
526
+ b, c, h, w = x.shape
527
+ qkv = self.to_qkv(x)
528
+ q, k, v = rearrange(
529
+ qkv, "b (qkv heads c) h w -> qkv b heads c (h w)", heads=self.heads, qkv=3
530
+ )
531
+ k = k.softmax(dim=-1)
532
+ context = torch.einsum("bhdn,bhen->bhde", k, v)
533
+ out = torch.einsum("bhde,bhdn->bhen", context, q)
534
+ out = rearrange(
535
+ out, "b heads c (h w) -> b (heads c) h w", heads=self.heads, h=h, w=w
536
+ )
537
+ return self.to_out(out)
538
+
539
+
540
+ class SpatialSelfAttention(nn.Module):
541
+ def __init__(self, in_channels):
542
+ super().__init__()
543
+ self.in_channels = in_channels
544
+
545
+ self.norm = torch.nn.GroupNorm(
546
+ num_groups=32, num_channels=in_channels, eps=1e-6, affine=True
547
+ )
548
+ self.q = torch.nn.Conv2d(
549
+ in_channels, in_channels, kernel_size=1, stride=1, padding=0
550
+ )
551
+ self.k = torch.nn.Conv2d(
552
+ in_channels, in_channels, kernel_size=1, stride=1, padding=0
553
+ )
554
+ self.v = torch.nn.Conv2d(
555
+ in_channels, in_channels, kernel_size=1, stride=1, padding=0
556
+ )
557
+ self.proj_out = torch.nn.Conv2d(
558
+ in_channels, in_channels, kernel_size=1, stride=1, padding=0
559
+ )
560
+
561
+ def forward(self, x):
562
+ h_ = x
563
+ h_ = self.norm(h_)
564
+ q = self.q(h_)
565
+ k = self.k(h_)
566
+ v = self.v(h_)
567
+
568
+ # compute attention
569
+ b, c, h, w = q.shape
570
+ q = rearrange(q, "b c h w -> b (h w) c")
571
+ k = rearrange(k, "b c h w -> b c (h w)")
572
+ w_ = torch.einsum("bij,bjk->bik", q, k)
573
+
574
+ w_ = w_ * (int(c) ** (-0.5))
575
+ w_ = torch.nn.functional.softmax(w_, dim=2)
576
+
577
+ # attend to values
578
+ v = rearrange(v, "b c h w -> b c (h w)")
579
+ w_ = rearrange(w_, "b i j -> b j i")
580
+ h_ = torch.einsum("bij,bjk->bik", v, w_)
581
+ h_ = rearrange(h_, "b c (h w) -> b c h w", h=h)
582
+ h_ = self.proj_out(h_)
583
+
584
+ return x + h_
lvdm/modules/encoders/__pycache__/condition.cpython-311.pyc ADDED
Binary file (25 kB). View file
 
lvdm/modules/encoders/__pycache__/ip_resampler.cpython-311.pyc ADDED
Binary file (7.94 kB). View file
 
lvdm/modules/encoders/condition.py ADDED
@@ -0,0 +1,512 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from torch.utils.checkpoint import checkpoint
4
+ import kornia
5
+ import open_clip
6
+ from transformers import T5Tokenizer, T5EncoderModel, CLIPTokenizer, CLIPTextModel
7
+ from lvdm.common import autocast
8
+ from utils.utils import count_params
9
+
10
+
11
+ class AbstractEncoder(nn.Module):
12
+ def __init__(self):
13
+ super().__init__()
14
+
15
+ def encode(self, *args, **kwargs):
16
+ raise NotImplementedError
17
+
18
+
19
+ class IdentityEncoder(AbstractEncoder):
20
+
21
+ def encode(self, x):
22
+ return x
23
+
24
+
25
+ class ClassEmbedder(nn.Module):
26
+ def __init__(self, embed_dim, n_classes=1000, key="class", ucg_rate=0.1):
27
+ super().__init__()
28
+ self.key = key
29
+ self.embedding = nn.Embedding(n_classes, embed_dim)
30
+ self.n_classes = n_classes
31
+ self.ucg_rate = ucg_rate
32
+
33
+ def forward(self, batch, key=None, disable_dropout=False):
34
+ if key is None:
35
+ key = self.key
36
+ # this is for use in crossattn
37
+ c = batch[key][:, None]
38
+ if self.ucg_rate > 0.0 and not disable_dropout:
39
+ mask = 1.0 - torch.bernoulli(torch.ones_like(c) * self.ucg_rate)
40
+ c = mask * c + (1 - mask) * torch.ones_like(c) * (self.n_classes - 1)
41
+ c = c.long()
42
+ c = self.embedding(c)
43
+ return c
44
+
45
+ def get_unconditional_conditioning(self, bs, device="cuda"):
46
+ uc_class = (
47
+ self.n_classes - 1
48
+ ) # 1000 classes --> 0 ... 999, one extra class for ucg (class 1000)
49
+ uc = torch.ones((bs,), device=device) * uc_class
50
+ uc = {self.key: uc}
51
+ return uc
52
+
53
+
54
+ def disabled_train(self, mode=True):
55
+ """Overwrite model.train with this function to make sure train/eval mode
56
+ does not change anymore."""
57
+ return self
58
+
59
+
60
+ class FrozenT5Embedder(AbstractEncoder):
61
+ """Uses the T5 transformer encoder for text"""
62
+
63
+ def __init__(
64
+ self, version="google/t5-v1_1-large", device="cuda", max_length=77, freeze=True
65
+ ): # others are google/t5-v1_1-xl and google/t5-v1_1-xxl
66
+ super().__init__()
67
+ self.tokenizer = T5Tokenizer.from_pretrained(version)
68
+ self.transformer = T5EncoderModel.from_pretrained(version)
69
+ self.device = device
70
+ self.max_length = max_length # TODO: typical value?
71
+ if freeze:
72
+ self.freeze()
73
+
74
+ def freeze(self):
75
+ self.transformer = self.transformer.eval()
76
+ # self.train = disabled_train
77
+ for param in self.parameters():
78
+ param.requires_grad = False
79
+
80
+ def forward(self, text):
81
+ batch_encoding = self.tokenizer(
82
+ text,
83
+ truncation=True,
84
+ max_length=self.max_length,
85
+ return_length=True,
86
+ return_overflowing_tokens=False,
87
+ padding="max_length",
88
+ return_tensors="pt",
89
+ )
90
+ tokens = batch_encoding["input_ids"].to(self.device)
91
+ outputs = self.transformer(input_ids=tokens)
92
+
93
+ z = outputs.last_hidden_state
94
+ return z
95
+
96
+ def encode(self, text):
97
+ return self(text)
98
+
99
+
100
+ class FrozenCLIPEmbedder(AbstractEncoder):
101
+ """Uses the CLIP transformer encoder for text (from huggingface)"""
102
+
103
+ LAYERS = ["last", "pooled", "hidden"]
104
+
105
+ def __init__(
106
+ self,
107
+ version="openai/clip-vit-large-patch14",
108
+ device="cuda",
109
+ max_length=77,
110
+ freeze=True,
111
+ layer="last",
112
+ layer_idx=None,
113
+ ): # clip-vit-base-patch32
114
+ super().__init__()
115
+ assert layer in self.LAYERS
116
+ self.tokenizer = CLIPTokenizer.from_pretrained(version)
117
+ self.transformer = CLIPTextModel.from_pretrained(version)
118
+ self.device = device
119
+ self.max_length = max_length
120
+ if freeze:
121
+ self.freeze()
122
+ self.layer = layer
123
+ self.layer_idx = layer_idx
124
+ if layer == "hidden":
125
+ assert layer_idx is not None
126
+ assert 0 <= abs(layer_idx) <= 12
127
+
128
+ def freeze(self):
129
+ self.transformer = self.transformer.eval()
130
+ # self.train = disabled_train
131
+ for param in self.parameters():
132
+ param.requires_grad = False
133
+
134
+ def forward(self, text):
135
+ batch_encoding = self.tokenizer(
136
+ text,
137
+ truncation=True,
138
+ max_length=self.max_length,
139
+ return_length=True,
140
+ return_overflowing_tokens=False,
141
+ padding="max_length",
142
+ return_tensors="pt",
143
+ )
144
+ tokens = batch_encoding["input_ids"].to(self.device)
145
+ outputs = self.transformer(
146
+ input_ids=tokens, output_hidden_states=self.layer == "hidden"
147
+ )
148
+ if self.layer == "last":
149
+ z = outputs.last_hidden_state
150
+ elif self.layer == "pooled":
151
+ z = outputs.pooler_output[:, None, :]
152
+ else:
153
+ z = outputs.hidden_states[self.layer_idx]
154
+ return z
155
+
156
+ def encode(self, text):
157
+ return self(text)
158
+
159
+
160
+ class ClipImageEmbedder(nn.Module):
161
+ def __init__(
162
+ self,
163
+ model,
164
+ jit=False,
165
+ device="cuda" if torch.cuda.is_available() else "cpu",
166
+ antialias=True,
167
+ ucg_rate=0.0,
168
+ ):
169
+ super().__init__()
170
+ from clip import load as load_clip
171
+
172
+ self.model, _ = load_clip(name=model, device=device, jit=jit)
173
+
174
+ self.antialias = antialias
175
+
176
+ self.register_buffer(
177
+ "mean", torch.Tensor([0.48145466, 0.4578275, 0.40821073]), persistent=False
178
+ )
179
+ self.register_buffer(
180
+ "std", torch.Tensor([0.26862954, 0.26130258, 0.27577711]), persistent=False
181
+ )
182
+ self.ucg_rate = ucg_rate
183
+
184
+ def preprocess(self, x):
185
+ # normalize to [0,1]
186
+ x = kornia.geometry.resize(
187
+ x,
188
+ (224, 224),
189
+ interpolation="bicubic",
190
+ align_corners=True,
191
+ antialias=self.antialias,
192
+ )
193
+ x = (x + 1.0) / 2.0
194
+ # re-normalize according to clip
195
+ x = kornia.enhance.normalize(x, self.mean, self.std)
196
+ return x
197
+
198
+ def forward(self, x, no_dropout=False):
199
+ # x is assumed to be in range [-1,1]
200
+ out = self.model.encode_image(self.preprocess(x))
201
+ out = out.to(x.dtype)
202
+ if self.ucg_rate > 0.0 and not no_dropout:
203
+ out = (
204
+ torch.bernoulli(
205
+ (1.0 - self.ucg_rate) * torch.ones(out.shape[0], device=out.device)
206
+ )[:, None]
207
+ * out
208
+ )
209
+ return out
210
+
211
+
212
+ class FrozenOpenCLIPEmbedder(AbstractEncoder):
213
+ """
214
+ Uses the OpenCLIP transformer encoder for text
215
+ """
216
+
217
+ LAYERS = [
218
+ # "pooled",
219
+ "last",
220
+ "penultimate",
221
+ ]
222
+
223
+ def __init__(
224
+ self,
225
+ arch="ViT-H-14",
226
+ version="laion2b_s32b_b79k",
227
+ device="cuda",
228
+ max_length=77,
229
+ freeze=True,
230
+ layer="last",
231
+ ):
232
+ super().__init__()
233
+ assert layer in self.LAYERS
234
+ model, _, _ = open_clip.create_model_and_transforms(
235
+ arch, device=torch.device("cpu")
236
+ )
237
+ del model.visual
238
+ self.model = model
239
+
240
+ self.device = device
241
+ self.max_length = max_length
242
+ if freeze:
243
+ self.freeze()
244
+ self.layer = layer
245
+ if self.layer == "last":
246
+ self.layer_idx = 0
247
+ elif self.layer == "penultimate":
248
+ self.layer_idx = 1
249
+ else:
250
+ raise NotImplementedError()
251
+
252
+ def freeze(self):
253
+ self.model = self.model.eval()
254
+ for param in self.parameters():
255
+ param.requires_grad = False
256
+
257
+ def forward(self, text):
258
+ self.device = self.model.positional_embedding.device
259
+ tokens = open_clip.tokenize(text)
260
+ z = self.encode_with_transformer(tokens.to(self.device))
261
+ return z
262
+
263
+ def encode_with_transformer(self, text):
264
+ x = self.model.token_embedding(text) # [batch_size, n_ctx, d_model]
265
+ x = x + self.model.positional_embedding
266
+ x = x.permute(1, 0, 2) # NLD -> LND
267
+ x = self.text_transformer_forward(x, attn_mask=self.model.attn_mask)
268
+ x = x.permute(1, 0, 2) # LND -> NLD
269
+ x = self.model.ln_final(x)
270
+ return x
271
+
272
+ def text_transformer_forward(self, x: torch.Tensor, attn_mask=None):
273
+ for i, r in enumerate(self.model.transformer.resblocks):
274
+ if i == len(self.model.transformer.resblocks) - self.layer_idx:
275
+ break
276
+ if (
277
+ self.model.transformer.grad_checkpointing
278
+ and not torch.jit.is_scripting()
279
+ ):
280
+ x = checkpoint(r, x, attn_mask)
281
+ else:
282
+ x = r(x, attn_mask=attn_mask)
283
+ return x
284
+
285
+ def encode(self, text):
286
+ return self(text)
287
+
288
+
289
+ class FrozenOpenCLIPImageEmbedder(AbstractEncoder):
290
+ """
291
+ Uses the OpenCLIP vision transformer encoder for images
292
+ """
293
+
294
+ def __init__(
295
+ self,
296
+ arch="ViT-H-14",
297
+ version="laion2b_s32b_b79k",
298
+ device="cuda",
299
+ max_length=77,
300
+ freeze=True,
301
+ layer="pooled",
302
+ antialias=True,
303
+ ucg_rate=0.0,
304
+ ):
305
+ super().__init__()
306
+ model, _, _ = open_clip.create_model_and_transforms(
307
+ arch,
308
+ device=torch.device("cpu"),
309
+ pretrained=version,
310
+ )
311
+ del model.transformer
312
+ self.model = model
313
+
314
+ self.device = device
315
+ self.max_length = max_length
316
+ if freeze:
317
+ self.freeze()
318
+ self.layer = layer
319
+ if self.layer == "penultimate":
320
+ raise NotImplementedError()
321
+ self.layer_idx = 1
322
+
323
+ self.antialias = antialias
324
+
325
+ self.register_buffer(
326
+ "mean", torch.Tensor([0.48145466, 0.4578275, 0.40821073]), persistent=False
327
+ )
328
+ self.register_buffer(
329
+ "std", torch.Tensor([0.26862954, 0.26130258, 0.27577711]), persistent=False
330
+ )
331
+ self.ucg_rate = ucg_rate
332
+
333
+ def preprocess(self, x):
334
+ # normalize to [0,1]
335
+ x = kornia.geometry.resize(
336
+ x,
337
+ (224, 224),
338
+ interpolation="bicubic",
339
+ align_corners=True,
340
+ antialias=self.antialias,
341
+ )
342
+ x = (x + 1.0) / 2.0
343
+ # renormalize according to clip
344
+ x = kornia.enhance.normalize(x, self.mean, self.std)
345
+ return x
346
+
347
+ def freeze(self):
348
+ self.model = self.model.eval()
349
+ for param in self.parameters():
350
+ param.requires_grad = False
351
+
352
+ @autocast
353
+ def forward(self, image, no_dropout=False):
354
+ z = self.encode_with_vision_transformer(image)
355
+ if self.ucg_rate > 0.0 and not no_dropout:
356
+ z = (
357
+ torch.bernoulli(
358
+ (1.0 - self.ucg_rate) * torch.ones(z.shape[0], device=z.device)
359
+ )[:, None]
360
+ * z
361
+ )
362
+ return z
363
+
364
+ def encode_with_vision_transformer(self, img):
365
+ img = self.preprocess(img)
366
+ x = self.model.visual(img)
367
+ return x
368
+
369
+ def encode(self, text):
370
+ return self(text)
371
+
372
+
373
+ class FrozenOpenCLIPImageEmbedderV2(AbstractEncoder):
374
+ """
375
+ Uses the OpenCLIP vision transformer encoder for images
376
+ """
377
+
378
+ def __init__(
379
+ self,
380
+ arch="ViT-H-14",
381
+ version="laion2b_s32b_b79k",
382
+ device="cuda",
383
+ freeze=True,
384
+ layer="pooled",
385
+ antialias=True,
386
+ ):
387
+ super().__init__()
388
+ model, _, _ = open_clip.create_model_and_transforms(
389
+ arch,
390
+ device=torch.device("cpu"),
391
+ pretrained=version,
392
+ )
393
+ del model.transformer
394
+ self.model = model
395
+ self.device = device
396
+
397
+ if freeze:
398
+ self.freeze()
399
+ self.layer = layer
400
+ if self.layer == "penultimate":
401
+ raise NotImplementedError()
402
+ self.layer_idx = 1
403
+
404
+ self.antialias = antialias
405
+ self.register_buffer(
406
+ "mean", torch.Tensor([0.48145466, 0.4578275, 0.40821073]), persistent=False
407
+ )
408
+ self.register_buffer(
409
+ "std", torch.Tensor([0.26862954, 0.26130258, 0.27577711]), persistent=False
410
+ )
411
+
412
+ def preprocess(self, x):
413
+ # normalize to [0,1]
414
+ x = kornia.geometry.resize(
415
+ x,
416
+ (224, 224),
417
+ interpolation="bicubic",
418
+ align_corners=True,
419
+ antialias=self.antialias,
420
+ )
421
+ x = (x + 1.0) / 2.0
422
+ # renormalize according to clip
423
+ x = kornia.enhance.normalize(x, self.mean, self.std)
424
+ return x
425
+
426
+ def freeze(self):
427
+ self.model = self.model.eval()
428
+ for param in self.model.parameters():
429
+ param.requires_grad = False
430
+
431
+ def forward(self, image, no_dropout=False):
432
+ ## image: b c h w
433
+ z = self.encode_with_vision_transformer(image)
434
+ return z
435
+
436
+ def encode_with_vision_transformer(self, x):
437
+ x = self.preprocess(x)
438
+
439
+ # to patches - whether to use dual patchnorm - https://arxiv.org/abs/2302.01327v1
440
+ if self.model.visual.input_patchnorm:
441
+ # einops - rearrange(x, 'b c (h p1) (w p2) -> b (h w) (c p1 p2)')
442
+ x = x.reshape(
443
+ x.shape[0],
444
+ x.shape[1],
445
+ self.model.visual.grid_size[0],
446
+ self.model.visual.patch_size[0],
447
+ self.model.visual.grid_size[1],
448
+ self.model.visual.patch_size[1],
449
+ )
450
+ x = x.permute(0, 2, 4, 1, 3, 5)
451
+ x = x.reshape(
452
+ x.shape[0],
453
+ self.model.visual.grid_size[0] * self.model.visual.grid_size[1],
454
+ -1,
455
+ )
456
+ x = self.model.visual.patchnorm_pre_ln(x)
457
+ x = self.model.visual.conv1(x)
458
+ else:
459
+ x = self.model.visual.conv1(x) # shape = [*, width, grid, grid]
460
+ x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2]
461
+ x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width]
462
+
463
+ # class embeddings and positional embeddings
464
+ x = torch.cat(
465
+ [
466
+ self.model.visual.class_embedding.to(x.dtype)
467
+ + torch.zeros(
468
+ x.shape[0], 1, x.shape[-1], dtype=x.dtype, device=x.device
469
+ ),
470
+ x,
471
+ ],
472
+ dim=1,
473
+ ) # shape = [*, grid ** 2 + 1, width]
474
+ x = x + self.model.visual.positional_embedding.to(x.dtype)
475
+
476
+ # a patch_dropout of 0. would mean it is disabled and this function would do nothing but return what was passed in
477
+ x = self.model.visual.patch_dropout(x)
478
+ x = self.model.visual.ln_pre(x)
479
+
480
+ x = x.permute(1, 0, 2) # NLD -> LND
481
+ x = self.model.visual.transformer(x)
482
+ x = x.permute(1, 0, 2) # LND -> NLD
483
+
484
+ return x
485
+
486
+
487
+ class FrozenCLIPT5Encoder(AbstractEncoder):
488
+ def __init__(
489
+ self,
490
+ clip_version="openai/clip-vit-large-patch14",
491
+ t5_version="google/t5-v1_1-xl",
492
+ device="cuda",
493
+ clip_max_length=77,
494
+ t5_max_length=77,
495
+ ):
496
+ super().__init__()
497
+ self.clip_encoder = FrozenCLIPEmbedder(
498
+ clip_version, device, max_length=clip_max_length
499
+ )
500
+ self.t5_encoder = FrozenT5Embedder(t5_version, device, max_length=t5_max_length)
501
+ print(
502
+ f"{self.clip_encoder.__class__.__name__} has {count_params(self.clip_encoder) * 1.e-6:.2f} M parameters, "
503
+ f"{self.t5_encoder.__class__.__name__} comes with {count_params(self.t5_encoder) * 1.e-6:.2f} M params."
504
+ )
505
+
506
+ def encode(self, text):
507
+ return self(text)
508
+
509
+ def forward(self, text):
510
+ clip_z = self.clip_encoder.encode(text)
511
+ t5_z = self.t5_encoder.encode(text)
512
+ return [clip_z, t5_z]
lvdm/modules/encoders/ip_resampler.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # modified from https://github.com/mlfoundations/open_flamingo/blob/main/open_flamingo/src/helpers.py
2
+ import math
3
+ import torch
4
+ import torch.nn as nn
5
+
6
+
7
+ class ImageProjModel(nn.Module):
8
+ """Projection Model"""
9
+
10
+ def __init__(
11
+ self,
12
+ cross_attention_dim=1024,
13
+ clip_embeddings_dim=1024,
14
+ clip_extra_context_tokens=4,
15
+ ):
16
+ super().__init__()
17
+ self.cross_attention_dim = cross_attention_dim
18
+ self.clip_extra_context_tokens = clip_extra_context_tokens
19
+ self.proj = nn.Linear(
20
+ clip_embeddings_dim, self.clip_extra_context_tokens * cross_attention_dim
21
+ )
22
+ self.norm = nn.LayerNorm(cross_attention_dim)
23
+
24
+ def forward(self, image_embeds):
25
+ # embeds = image_embeds
26
+ embeds = image_embeds.type(list(self.proj.parameters())[0].dtype)
27
+ clip_extra_context_tokens = self.proj(embeds).reshape(
28
+ -1, self.clip_extra_context_tokens, self.cross_attention_dim
29
+ )
30
+ clip_extra_context_tokens = self.norm(clip_extra_context_tokens)
31
+ return clip_extra_context_tokens
32
+
33
+
34
+ # FFN
35
+ def FeedForward(dim, mult=4):
36
+ inner_dim = int(dim * mult)
37
+ return nn.Sequential(
38
+ nn.LayerNorm(dim),
39
+ nn.Linear(dim, inner_dim, bias=False),
40
+ nn.GELU(),
41
+ nn.Linear(inner_dim, dim, bias=False),
42
+ )
43
+
44
+
45
+ def reshape_tensor(x, heads):
46
+ bs, length, width = x.shape
47
+ # (bs, length, width) --> (bs, length, n_heads, dim_per_head)
48
+ x = x.view(bs, length, heads, -1)
49
+ # (bs, length, n_heads, dim_per_head) --> (bs, n_heads, length, dim_per_head)
50
+ x = x.transpose(1, 2)
51
+ # (bs, n_heads, length, dim_per_head) --> (bs*n_heads, length, dim_per_head)
52
+ x = x.reshape(bs, heads, length, -1)
53
+ return x
54
+
55
+
56
+ class PerceiverAttention(nn.Module):
57
+ def __init__(self, *, dim, dim_head=64, heads=8):
58
+ super().__init__()
59
+ self.scale = dim_head**-0.5
60
+ self.dim_head = dim_head
61
+ self.heads = heads
62
+ inner_dim = dim_head * heads
63
+
64
+ self.norm1 = nn.LayerNorm(dim)
65
+ self.norm2 = nn.LayerNorm(dim)
66
+
67
+ self.to_q = nn.Linear(dim, inner_dim, bias=False)
68
+ self.to_kv = nn.Linear(dim, inner_dim * 2, bias=False)
69
+ self.to_out = nn.Linear(inner_dim, dim, bias=False)
70
+
71
+ def forward(self, x, latents):
72
+ """
73
+ Args:
74
+ x (torch.Tensor): image features
75
+ shape (b, n1, D)
76
+ latent (torch.Tensor): latent features
77
+ shape (b, n2, D)
78
+ """
79
+ x = self.norm1(x)
80
+ latents = self.norm2(latents)
81
+
82
+ b, l, _ = latents.shape
83
+
84
+ q = self.to_q(latents)
85
+ kv_input = torch.cat((x, latents), dim=-2)
86
+ k, v = self.to_kv(kv_input).chunk(2, dim=-1)
87
+
88
+ q = reshape_tensor(q, self.heads)
89
+ k = reshape_tensor(k, self.heads)
90
+ v = reshape_tensor(v, self.heads)
91
+
92
+ # attention
93
+ scale = 1 / math.sqrt(math.sqrt(self.dim_head))
94
+ weight = (q * scale) @ (k * scale).transpose(
95
+ -2, -1
96
+ ) # More stable with f16 than dividing afterwards
97
+ weight = torch.softmax(weight.float(), dim=-1).type(weight.dtype)
98
+ out = weight @ v
99
+
100
+ out = out.permute(0, 2, 1, 3).reshape(b, l, -1)
101
+
102
+ return self.to_out(out)
103
+
104
+
105
+ class Resampler(nn.Module):
106
+ def __init__(
107
+ self,
108
+ dim=1024,
109
+ depth=8,
110
+ dim_head=64,
111
+ heads=16,
112
+ num_queries=8,
113
+ embedding_dim=768,
114
+ output_dim=1024,
115
+ ff_mult=4,
116
+ ):
117
+ super().__init__()
118
+
119
+ self.latents = nn.Parameter(torch.randn(1, num_queries, dim) / dim**0.5)
120
+
121
+ self.proj_in = nn.Linear(embedding_dim, dim)
122
+
123
+ self.proj_out = nn.Linear(dim, output_dim)
124
+ self.norm_out = nn.LayerNorm(output_dim)
125
+
126
+ self.layers = nn.ModuleList([])
127
+ for _ in range(depth):
128
+ self.layers.append(
129
+ nn.ModuleList(
130
+ [
131
+ PerceiverAttention(dim=dim, dim_head=dim_head, heads=heads),
132
+ FeedForward(dim=dim, mult=ff_mult),
133
+ ]
134
+ )
135
+ )
136
+
137
+ def forward(self, x):
138
+
139
+ latents = self.latents.repeat(x.size(0), 1, 1)
140
+
141
+ x = self.proj_in(x)
142
+
143
+ for attn, ff in self.layers:
144
+ latents = attn(x, latents) + latents
145
+ latents = ff(latents) + latents
146
+
147
+ latents = self.proj_out(latents)
148
+ return self.norm_out(latents)
lvdm/modules/networks/__pycache__/ae_modules.cpython-311.pyc ADDED
Binary file (43.8 kB). View file
 
lvdm/modules/networks/__pycache__/openaimodel3d.cpython-311.pyc ADDED
Binary file (26.4 kB). View file
 
lvdm/modules/networks/ae_modules.py ADDED
@@ -0,0 +1,1025 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # pytorch_diffusion + derived encoder decoder
2
+ import math
3
+ import torch
4
+ import numpy as np
5
+ import torch.nn as nn
6
+ from einops import rearrange
7
+ from utils.utils import instantiate_from_config
8
+ from lvdm.modules.attention import LinearAttention
9
+
10
+
11
+ def nonlinearity(x):
12
+ # swish
13
+ return x * torch.sigmoid(x)
14
+
15
+
16
+ def Normalize(in_channels, num_groups=32):
17
+ return torch.nn.GroupNorm(
18
+ num_groups=num_groups, num_channels=in_channels, eps=1e-6, affine=True
19
+ )
20
+
21
+
22
+ class LinAttnBlock(LinearAttention):
23
+ """to match AttnBlock usage"""
24
+
25
+ def __init__(self, in_channels):
26
+ super().__init__(dim=in_channels, heads=1, dim_head=in_channels)
27
+
28
+
29
+ class AttnBlock(nn.Module):
30
+ def __init__(self, in_channels):
31
+ super().__init__()
32
+ self.in_channels = in_channels
33
+
34
+ self.norm = Normalize(in_channels)
35
+ self.q = torch.nn.Conv2d(
36
+ in_channels, in_channels, kernel_size=1, stride=1, padding=0
37
+ )
38
+ self.k = torch.nn.Conv2d(
39
+ in_channels, in_channels, kernel_size=1, stride=1, padding=0
40
+ )
41
+ self.v = torch.nn.Conv2d(
42
+ in_channels, in_channels, kernel_size=1, stride=1, padding=0
43
+ )
44
+ self.proj_out = torch.nn.Conv2d(
45
+ in_channels, in_channels, kernel_size=1, stride=1, padding=0
46
+ )
47
+
48
+ def forward(self, x):
49
+ h_ = x
50
+ h_ = self.norm(h_)
51
+ q = self.q(h_)
52
+ k = self.k(h_)
53
+ v = self.v(h_)
54
+
55
+ # compute attention
56
+ b, c, h, w = q.shape
57
+ q = q.reshape(b, c, h * w) # bcl
58
+ q = q.permute(0, 2, 1) # bcl -> blc l=hw
59
+ k = k.reshape(b, c, h * w) # bcl
60
+
61
+ w_ = torch.bmm(q, k) # b,hw,hw w[b,i,j]=sum_c q[b,i,c]k[b,c,j]
62
+ w_ = w_ * (int(c) ** (-0.5))
63
+ w_ = torch.nn.functional.softmax(w_, dim=2)
64
+
65
+ # attend to values
66
+ v = v.reshape(b, c, h * w)
67
+ w_ = w_.permute(0, 2, 1) # b,hw,hw (first hw of k, second of q)
68
+ h_ = torch.bmm(v, w_) # b, c,hw (hw of q) h_[b,c,j] = sum_i v[b,c,i] w_[b,i,j]
69
+ h_ = h_.reshape(b, c, h, w)
70
+
71
+ h_ = self.proj_out(h_)
72
+
73
+ return x + h_
74
+
75
+
76
+ def make_attn(in_channels, attn_type="vanilla"):
77
+ assert attn_type in ["vanilla", "linear", "none"], f"attn_type {attn_type} unknown"
78
+ # print(f"making attention of type '{attn_type}' with {in_channels} in_channels")
79
+ if attn_type == "vanilla":
80
+ return AttnBlock(in_channels)
81
+ elif attn_type == "none":
82
+ return nn.Identity(in_channels)
83
+ else:
84
+ return LinAttnBlock(in_channels)
85
+
86
+
87
+ class Downsample(nn.Module):
88
+ def __init__(self, in_channels, with_conv):
89
+ super().__init__()
90
+ self.with_conv = with_conv
91
+ self.in_channels = in_channels
92
+ if self.with_conv:
93
+ # no asymmetric padding in torch conv, must do it ourselves
94
+ self.conv = torch.nn.Conv2d(
95
+ in_channels, in_channels, kernel_size=3, stride=2, padding=0
96
+ )
97
+
98
+ def forward(self, x):
99
+ if self.with_conv:
100
+ pad = (0, 1, 0, 1)
101
+ x = torch.nn.functional.pad(x, pad, mode="constant", value=0)
102
+ x = self.conv(x)
103
+ else:
104
+ x = torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2)
105
+ return x
106
+
107
+
108
+ class Upsample(nn.Module):
109
+ def __init__(self, in_channels, with_conv):
110
+ super().__init__()
111
+ self.with_conv = with_conv
112
+ self.in_channels = in_channels
113
+ if self.with_conv:
114
+ self.conv = torch.nn.Conv2d(
115
+ in_channels, in_channels, kernel_size=3, stride=1, padding=1
116
+ )
117
+
118
+ def forward(self, x):
119
+ x = torch.nn.functional.interpolate(x, scale_factor=2.0, mode="nearest")
120
+ if self.with_conv:
121
+ x = self.conv(x)
122
+ return x
123
+
124
+
125
+ def get_timestep_embedding(timesteps, embedding_dim):
126
+ """
127
+ This matches the implementation in Denoising Diffusion Probabilistic Models:
128
+ From Fairseq.
129
+ Build sinusoidal embeddings.
130
+ This matches the implementation in tensor2tensor, but differs slightly
131
+ from the description in Section 3.5 of "Attention Is All You Need".
132
+ """
133
+ assert len(timesteps.shape) == 1
134
+
135
+ half_dim = embedding_dim // 2
136
+ emb = math.log(10000) / (half_dim - 1)
137
+ emb = torch.exp(torch.arange(half_dim, dtype=torch.float32) * -emb)
138
+ emb = emb.to(device=timesteps.device)
139
+ emb = timesteps.float()[:, None] * emb[None, :]
140
+ emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
141
+ if embedding_dim % 2 == 1: # zero pad
142
+ emb = torch.nn.functional.pad(emb, (0, 1, 0, 0))
143
+ return emb
144
+
145
+
146
+ class ResnetBlock(nn.Module):
147
+ def __init__(
148
+ self,
149
+ *,
150
+ in_channels,
151
+ out_channels=None,
152
+ conv_shortcut=False,
153
+ dropout,
154
+ temb_channels=512,
155
+ ):
156
+ super().__init__()
157
+ self.in_channels = in_channels
158
+ out_channels = in_channels if out_channels is None else out_channels
159
+ self.out_channels = out_channels
160
+ self.use_conv_shortcut = conv_shortcut
161
+
162
+ self.norm1 = Normalize(in_channels)
163
+ self.conv1 = torch.nn.Conv2d(
164
+ in_channels, out_channels, kernel_size=3, stride=1, padding=1
165
+ )
166
+ if temb_channels > 0:
167
+ self.temb_proj = torch.nn.Linear(temb_channels, out_channels)
168
+ self.norm2 = Normalize(out_channels)
169
+ self.dropout = torch.nn.Dropout(dropout)
170
+ self.conv2 = torch.nn.Conv2d(
171
+ out_channels, out_channels, kernel_size=3, stride=1, padding=1
172
+ )
173
+ if self.in_channels != self.out_channels:
174
+ if self.use_conv_shortcut:
175
+ self.conv_shortcut = torch.nn.Conv2d(
176
+ in_channels, out_channels, kernel_size=3, stride=1, padding=1
177
+ )
178
+ else:
179
+ self.nin_shortcut = torch.nn.Conv2d(
180
+ in_channels, out_channels, kernel_size=1, stride=1, padding=0
181
+ )
182
+
183
+ def forward(self, x, temb):
184
+ h = x
185
+ h = self.norm1(h)
186
+ h = nonlinearity(h)
187
+ h = self.conv1(h)
188
+
189
+ if temb is not None:
190
+ h = h + self.temb_proj(nonlinearity(temb))[:, :, None, None]
191
+
192
+ h = self.norm2(h)
193
+ h = nonlinearity(h)
194
+ h = self.dropout(h)
195
+ h = self.conv2(h)
196
+
197
+ if self.in_channels != self.out_channels:
198
+ if self.use_conv_shortcut:
199
+ x = self.conv_shortcut(x)
200
+ else:
201
+ x = self.nin_shortcut(x)
202
+
203
+ return x + h
204
+
205
+
206
+ class Model(nn.Module):
207
+ def __init__(
208
+ self,
209
+ *,
210
+ ch,
211
+ out_ch,
212
+ ch_mult=(1, 2, 4, 8),
213
+ num_res_blocks,
214
+ attn_resolutions,
215
+ dropout=0.0,
216
+ resamp_with_conv=True,
217
+ in_channels,
218
+ resolution,
219
+ use_timestep=True,
220
+ use_linear_attn=False,
221
+ attn_type="vanilla",
222
+ ):
223
+ super().__init__()
224
+ if use_linear_attn:
225
+ attn_type = "linear"
226
+ self.ch = ch
227
+ self.temb_ch = self.ch * 4
228
+ self.num_resolutions = len(ch_mult)
229
+ self.num_res_blocks = num_res_blocks
230
+ self.resolution = resolution
231
+ self.in_channels = in_channels
232
+
233
+ self.use_timestep = use_timestep
234
+ if self.use_timestep:
235
+ # timestep embedding
236
+ self.temb = nn.Module()
237
+ self.temb.dense = nn.ModuleList(
238
+ [
239
+ torch.nn.Linear(self.ch, self.temb_ch),
240
+ torch.nn.Linear(self.temb_ch, self.temb_ch),
241
+ ]
242
+ )
243
+
244
+ # downsampling
245
+ self.conv_in = torch.nn.Conv2d(
246
+ in_channels, self.ch, kernel_size=3, stride=1, padding=1
247
+ )
248
+
249
+ curr_res = resolution
250
+ in_ch_mult = (1,) + tuple(ch_mult)
251
+ self.down = nn.ModuleList()
252
+ for i_level in range(self.num_resolutions):
253
+ block = nn.ModuleList()
254
+ attn = nn.ModuleList()
255
+ block_in = ch * in_ch_mult[i_level]
256
+ block_out = ch * ch_mult[i_level]
257
+ for i_block in range(self.num_res_blocks):
258
+ block.append(
259
+ ResnetBlock(
260
+ in_channels=block_in,
261
+ out_channels=block_out,
262
+ temb_channels=self.temb_ch,
263
+ dropout=dropout,
264
+ )
265
+ )
266
+ block_in = block_out
267
+ if curr_res in attn_resolutions:
268
+ attn.append(make_attn(block_in, attn_type=attn_type))
269
+ down = nn.Module()
270
+ down.block = block
271
+ down.attn = attn
272
+ if i_level != self.num_resolutions - 1:
273
+ down.downsample = Downsample(block_in, resamp_with_conv)
274
+ curr_res = curr_res // 2
275
+ self.down.append(down)
276
+
277
+ # middle
278
+ self.mid = nn.Module()
279
+ self.mid.block_1 = ResnetBlock(
280
+ in_channels=block_in,
281
+ out_channels=block_in,
282
+ temb_channels=self.temb_ch,
283
+ dropout=dropout,
284
+ )
285
+ self.mid.attn_1 = make_attn(block_in, attn_type=attn_type)
286
+ self.mid.block_2 = ResnetBlock(
287
+ in_channels=block_in,
288
+ out_channels=block_in,
289
+ temb_channels=self.temb_ch,
290
+ dropout=dropout,
291
+ )
292
+
293
+ # upsampling
294
+ self.up = nn.ModuleList()
295
+ for i_level in reversed(range(self.num_resolutions)):
296
+ block = nn.ModuleList()
297
+ attn = nn.ModuleList()
298
+ block_out = ch * ch_mult[i_level]
299
+ skip_in = ch * ch_mult[i_level]
300
+ for i_block in range(self.num_res_blocks + 1):
301
+ if i_block == self.num_res_blocks:
302
+ skip_in = ch * in_ch_mult[i_level]
303
+ block.append(
304
+ ResnetBlock(
305
+ in_channels=block_in + skip_in,
306
+ out_channels=block_out,
307
+ temb_channels=self.temb_ch,
308
+ dropout=dropout,
309
+ )
310
+ )
311
+ block_in = block_out
312
+ if curr_res in attn_resolutions:
313
+ attn.append(make_attn(block_in, attn_type=attn_type))
314
+ up = nn.Module()
315
+ up.block = block
316
+ up.attn = attn
317
+ if i_level != 0:
318
+ up.upsample = Upsample(block_in, resamp_with_conv)
319
+ curr_res = curr_res * 2
320
+ self.up.insert(0, up) # prepend to get consistent order
321
+
322
+ # end
323
+ self.norm_out = Normalize(block_in)
324
+ self.conv_out = torch.nn.Conv2d(
325
+ block_in, out_ch, kernel_size=3, stride=1, padding=1
326
+ )
327
+
328
+ def forward(self, x, t=None, context=None):
329
+ # assert x.shape[2] == x.shape[3] == self.resolution
330
+ if context is not None:
331
+ # assume aligned context, cat along channel axis
332
+ x = torch.cat((x, context), dim=1)
333
+ if self.use_timestep:
334
+ # timestep embedding
335
+ assert t is not None
336
+ temb = get_timestep_embedding(t, self.ch)
337
+ temb = self.temb.dense[0](temb)
338
+ temb = nonlinearity(temb)
339
+ temb = self.temb.dense[1](temb)
340
+ else:
341
+ temb = None
342
+
343
+ # downsampling
344
+ hs = [self.conv_in(x)]
345
+ for i_level in range(self.num_resolutions):
346
+ for i_block in range(self.num_res_blocks):
347
+ h = self.down[i_level].block[i_block](hs[-1], temb)
348
+ if len(self.down[i_level].attn) > 0:
349
+ h = self.down[i_level].attn[i_block](h)
350
+ hs.append(h)
351
+ if i_level != self.num_resolutions - 1:
352
+ hs.append(self.down[i_level].downsample(hs[-1]))
353
+
354
+ # middle
355
+ h = hs[-1]
356
+ h = self.mid.block_1(h, temb)
357
+ h = self.mid.attn_1(h)
358
+ h = self.mid.block_2(h, temb)
359
+
360
+ # upsampling
361
+ for i_level in reversed(range(self.num_resolutions)):
362
+ for i_block in range(self.num_res_blocks + 1):
363
+ h = self.up[i_level].block[i_block](
364
+ torch.cat([h, hs.pop()], dim=1), temb
365
+ )
366
+ if len(self.up[i_level].attn) > 0:
367
+ h = self.up[i_level].attn[i_block](h)
368
+ if i_level != 0:
369
+ h = self.up[i_level].upsample(h)
370
+
371
+ # end
372
+ h = self.norm_out(h)
373
+ h = nonlinearity(h)
374
+ h = self.conv_out(h)
375
+ return h
376
+
377
+ def get_last_layer(self):
378
+ return self.conv_out.weight
379
+
380
+
381
+ class Encoder(nn.Module):
382
+ def __init__(
383
+ self,
384
+ *,
385
+ ch,
386
+ out_ch,
387
+ ch_mult=(1, 2, 4, 8),
388
+ num_res_blocks,
389
+ attn_resolutions,
390
+ dropout=0.0,
391
+ resamp_with_conv=True,
392
+ in_channels,
393
+ resolution,
394
+ z_channels,
395
+ double_z=True,
396
+ use_linear_attn=False,
397
+ attn_type="vanilla",
398
+ **ignore_kwargs,
399
+ ):
400
+ super().__init__()
401
+ if use_linear_attn:
402
+ attn_type = "linear"
403
+ self.ch = ch
404
+ self.temb_ch = 0
405
+ self.num_resolutions = len(ch_mult)
406
+ self.num_res_blocks = num_res_blocks
407
+ self.resolution = resolution
408
+ self.in_channels = in_channels
409
+
410
+ # downsampling
411
+ self.conv_in = torch.nn.Conv2d(
412
+ in_channels, self.ch, kernel_size=3, stride=1, padding=1
413
+ )
414
+
415
+ curr_res = resolution
416
+ in_ch_mult = (1,) + tuple(ch_mult)
417
+ self.in_ch_mult = in_ch_mult
418
+ self.down = nn.ModuleList()
419
+ for i_level in range(self.num_resolutions):
420
+ block = nn.ModuleList()
421
+ attn = nn.ModuleList()
422
+ block_in = ch * in_ch_mult[i_level]
423
+ block_out = ch * ch_mult[i_level]
424
+ for i_block in range(self.num_res_blocks):
425
+ block.append(
426
+ ResnetBlock(
427
+ in_channels=block_in,
428
+ out_channels=block_out,
429
+ temb_channels=self.temb_ch,
430
+ dropout=dropout,
431
+ )
432
+ )
433
+ block_in = block_out
434
+ if curr_res in attn_resolutions:
435
+ attn.append(make_attn(block_in, attn_type=attn_type))
436
+ down = nn.Module()
437
+ down.block = block
438
+ down.attn = attn
439
+ if i_level != self.num_resolutions - 1:
440
+ down.downsample = Downsample(block_in, resamp_with_conv)
441
+ curr_res = curr_res // 2
442
+ self.down.append(down)
443
+
444
+ # middle
445
+ self.mid = nn.Module()
446
+ self.mid.block_1 = ResnetBlock(
447
+ in_channels=block_in,
448
+ out_channels=block_in,
449
+ temb_channels=self.temb_ch,
450
+ dropout=dropout,
451
+ )
452
+ self.mid.attn_1 = make_attn(block_in, attn_type=attn_type)
453
+ self.mid.block_2 = ResnetBlock(
454
+ in_channels=block_in,
455
+ out_channels=block_in,
456
+ temb_channels=self.temb_ch,
457
+ dropout=dropout,
458
+ )
459
+
460
+ # end
461
+ self.norm_out = Normalize(block_in)
462
+ self.conv_out = torch.nn.Conv2d(
463
+ block_in,
464
+ 2 * z_channels if double_z else z_channels,
465
+ kernel_size=3,
466
+ stride=1,
467
+ padding=1,
468
+ )
469
+
470
+ def forward(self, x):
471
+ # timestep embedding
472
+ temb = None
473
+
474
+ # print(f'encoder-input={x.shape}')
475
+ # downsampling
476
+ hs = [self.conv_in(x)]
477
+ # print(f'encoder-conv in feat={hs[0].shape}')
478
+ for i_level in range(self.num_resolutions):
479
+ for i_block in range(self.num_res_blocks):
480
+ h = self.down[i_level].block[i_block](hs[-1], temb)
481
+ # print(f'encoder-down feat={h.shape}')
482
+ if len(self.down[i_level].attn) > 0:
483
+ h = self.down[i_level].attn[i_block](h)
484
+ hs.append(h)
485
+ if i_level != self.num_resolutions - 1:
486
+ # print(f'encoder-downsample (input)={hs[-1].shape}')
487
+ hs.append(self.down[i_level].downsample(hs[-1]))
488
+ # print(f'encoder-downsample (output)={hs[-1].shape}')
489
+
490
+ # middle
491
+ h = hs[-1]
492
+ h = self.mid.block_1(h, temb)
493
+ # print(f'encoder-mid1 feat={h.shape}')
494
+ h = self.mid.attn_1(h)
495
+ h = self.mid.block_2(h, temb)
496
+ # print(f'encoder-mid2 feat={h.shape}')
497
+
498
+ # end
499
+ h = self.norm_out(h)
500
+ h = nonlinearity(h)
501
+ h = self.conv_out(h)
502
+ # print(f'end feat={h.shape}')
503
+ return h
504
+
505
+
506
+ class Decoder(nn.Module):
507
+ def __init__(
508
+ self,
509
+ *,
510
+ ch,
511
+ out_ch,
512
+ ch_mult=(1, 2, 4, 8),
513
+ num_res_blocks,
514
+ attn_resolutions,
515
+ dropout=0.0,
516
+ resamp_with_conv=True,
517
+ in_channels,
518
+ resolution,
519
+ z_channels,
520
+ give_pre_end=False,
521
+ tanh_out=False,
522
+ use_linear_attn=False,
523
+ attn_type="vanilla",
524
+ **ignorekwargs,
525
+ ):
526
+ super().__init__()
527
+ if use_linear_attn:
528
+ attn_type = "linear"
529
+ self.ch = ch
530
+ self.temb_ch = 0
531
+ self.num_resolutions = len(ch_mult)
532
+ self.num_res_blocks = num_res_blocks
533
+ self.resolution = resolution
534
+ self.in_channels = in_channels
535
+ self.give_pre_end = give_pre_end
536
+ self.tanh_out = tanh_out
537
+
538
+ # compute in_ch_mult, block_in and curr_res at lowest res
539
+ in_ch_mult = (1,) + tuple(ch_mult)
540
+ block_in = ch * ch_mult[self.num_resolutions - 1]
541
+ curr_res = resolution // 2 ** (self.num_resolutions - 1)
542
+ self.z_shape = (1, z_channels, curr_res, curr_res)
543
+ print(
544
+ "AE working on z of shape {} = {} dimensions.".format(
545
+ self.z_shape, np.prod(self.z_shape)
546
+ )
547
+ )
548
+
549
+ # z to block_in
550
+ self.conv_in = torch.nn.Conv2d(
551
+ z_channels, block_in, kernel_size=3, stride=1, padding=1
552
+ )
553
+
554
+ # middle
555
+ self.mid = nn.Module()
556
+ self.mid.block_1 = ResnetBlock(
557
+ in_channels=block_in,
558
+ out_channels=block_in,
559
+ temb_channels=self.temb_ch,
560
+ dropout=dropout,
561
+ )
562
+ self.mid.attn_1 = make_attn(block_in, attn_type=attn_type)
563
+ self.mid.block_2 = ResnetBlock(
564
+ in_channels=block_in,
565
+ out_channels=block_in,
566
+ temb_channels=self.temb_ch,
567
+ dropout=dropout,
568
+ )
569
+
570
+ # upsampling
571
+ self.up = nn.ModuleList()
572
+ for i_level in reversed(range(self.num_resolutions)):
573
+ block = nn.ModuleList()
574
+ attn = nn.ModuleList()
575
+ block_out = ch * ch_mult[i_level]
576
+ for i_block in range(self.num_res_blocks + 1):
577
+ block.append(
578
+ ResnetBlock(
579
+ in_channels=block_in,
580
+ out_channels=block_out,
581
+ temb_channels=self.temb_ch,
582
+ dropout=dropout,
583
+ )
584
+ )
585
+ block_in = block_out
586
+ if curr_res in attn_resolutions:
587
+ attn.append(make_attn(block_in, attn_type=attn_type))
588
+ up = nn.Module()
589
+ up.block = block
590
+ up.attn = attn
591
+ if i_level != 0:
592
+ up.upsample = Upsample(block_in, resamp_with_conv)
593
+ curr_res = curr_res * 2
594
+ self.up.insert(0, up) # prepend to get consistent order
595
+
596
+ # end
597
+ self.norm_out = Normalize(block_in)
598
+ self.conv_out = torch.nn.Conv2d(
599
+ block_in, out_ch, kernel_size=3, stride=1, padding=1
600
+ )
601
+
602
+ def forward(self, z):
603
+ # assert z.shape[1:] == self.z_shape[1:]
604
+ self.last_z_shape = z.shape
605
+
606
+ # print(f'decoder-input={z.shape}')
607
+ # timestep embedding
608
+ temb = None
609
+
610
+ # z to block_in
611
+ h = self.conv_in(z)
612
+ # print(f'decoder-conv in feat={h.shape}')
613
+
614
+ # middle
615
+ h = self.mid.block_1(h, temb)
616
+ h = self.mid.attn_1(h)
617
+ h = self.mid.block_2(h, temb)
618
+ # print(f'decoder-mid feat={h.shape}')
619
+
620
+ # upsampling
621
+ for i_level in reversed(range(self.num_resolutions)):
622
+ for i_block in range(self.num_res_blocks + 1):
623
+ h = self.up[i_level].block[i_block](h, temb)
624
+ if len(self.up[i_level].attn) > 0:
625
+ h = self.up[i_level].attn[i_block](h)
626
+ # print(f'decoder-up feat={h.shape}')
627
+ if i_level != 0:
628
+ h = self.up[i_level].upsample(h)
629
+ # print(f'decoder-upsample feat={h.shape}')
630
+
631
+ # end
632
+ if self.give_pre_end:
633
+ return h
634
+
635
+ h = self.norm_out(h)
636
+ h = nonlinearity(h)
637
+ h = self.conv_out(h)
638
+ # print(f'decoder-conv_out feat={h.shape}')
639
+ if self.tanh_out:
640
+ h = torch.tanh(h)
641
+ return h
642
+
643
+
644
+ class SimpleDecoder(nn.Module):
645
+ def __init__(self, in_channels, out_channels, *args, **kwargs):
646
+ super().__init__()
647
+ self.model = nn.ModuleList(
648
+ [
649
+ nn.Conv2d(in_channels, in_channels, 1),
650
+ ResnetBlock(
651
+ in_channels=in_channels,
652
+ out_channels=2 * in_channels,
653
+ temb_channels=0,
654
+ dropout=0.0,
655
+ ),
656
+ ResnetBlock(
657
+ in_channels=2 * in_channels,
658
+ out_channels=4 * in_channels,
659
+ temb_channels=0,
660
+ dropout=0.0,
661
+ ),
662
+ ResnetBlock(
663
+ in_channels=4 * in_channels,
664
+ out_channels=2 * in_channels,
665
+ temb_channels=0,
666
+ dropout=0.0,
667
+ ),
668
+ nn.Conv2d(2 * in_channels, in_channels, 1),
669
+ Upsample(in_channels, with_conv=True),
670
+ ]
671
+ )
672
+ # end
673
+ self.norm_out = Normalize(in_channels)
674
+ self.conv_out = torch.nn.Conv2d(
675
+ in_channels, out_channels, kernel_size=3, stride=1, padding=1
676
+ )
677
+
678
+ def forward(self, x):
679
+ for i, layer in enumerate(self.model):
680
+ if i in [1, 2, 3]:
681
+ x = layer(x, None)
682
+ else:
683
+ x = layer(x)
684
+
685
+ h = self.norm_out(x)
686
+ h = nonlinearity(h)
687
+ x = self.conv_out(h)
688
+ return x
689
+
690
+
691
+ class UpsampleDecoder(nn.Module):
692
+ def __init__(
693
+ self,
694
+ in_channels,
695
+ out_channels,
696
+ ch,
697
+ num_res_blocks,
698
+ resolution,
699
+ ch_mult=(2, 2),
700
+ dropout=0.0,
701
+ ):
702
+ super().__init__()
703
+ # upsampling
704
+ self.temb_ch = 0
705
+ self.num_resolutions = len(ch_mult)
706
+ self.num_res_blocks = num_res_blocks
707
+ block_in = in_channels
708
+ curr_res = resolution // 2 ** (self.num_resolutions - 1)
709
+ self.res_blocks = nn.ModuleList()
710
+ self.upsample_blocks = nn.ModuleList()
711
+ for i_level in range(self.num_resolutions):
712
+ res_block = []
713
+ block_out = ch * ch_mult[i_level]
714
+ for i_block in range(self.num_res_blocks + 1):
715
+ res_block.append(
716
+ ResnetBlock(
717
+ in_channels=block_in,
718
+ out_channels=block_out,
719
+ temb_channels=self.temb_ch,
720
+ dropout=dropout,
721
+ )
722
+ )
723
+ block_in = block_out
724
+ self.res_blocks.append(nn.ModuleList(res_block))
725
+ if i_level != self.num_resolutions - 1:
726
+ self.upsample_blocks.append(Upsample(block_in, True))
727
+ curr_res = curr_res * 2
728
+
729
+ # end
730
+ self.norm_out = Normalize(block_in)
731
+ self.conv_out = torch.nn.Conv2d(
732
+ block_in, out_channels, kernel_size=3, stride=1, padding=1
733
+ )
734
+
735
+ def forward(self, x):
736
+ # upsampling
737
+ h = x
738
+ for k, i_level in enumerate(range(self.num_resolutions)):
739
+ for i_block in range(self.num_res_blocks + 1):
740
+ h = self.res_blocks[i_level][i_block](h, None)
741
+ if i_level != self.num_resolutions - 1:
742
+ h = self.upsample_blocks[k](h)
743
+ h = self.norm_out(h)
744
+ h = nonlinearity(h)
745
+ h = self.conv_out(h)
746
+ return h
747
+
748
+
749
+ class LatentRescaler(nn.Module):
750
+ def __init__(self, factor, in_channels, mid_channels, out_channels, depth=2):
751
+ super().__init__()
752
+ # residual block, interpolate, residual block
753
+ self.factor = factor
754
+ self.conv_in = nn.Conv2d(
755
+ in_channels, mid_channels, kernel_size=3, stride=1, padding=1
756
+ )
757
+ self.res_block1 = nn.ModuleList(
758
+ [
759
+ ResnetBlock(
760
+ in_channels=mid_channels,
761
+ out_channels=mid_channels,
762
+ temb_channels=0,
763
+ dropout=0.0,
764
+ )
765
+ for _ in range(depth)
766
+ ]
767
+ )
768
+ self.attn = AttnBlock(mid_channels)
769
+ self.res_block2 = nn.ModuleList(
770
+ [
771
+ ResnetBlock(
772
+ in_channels=mid_channels,
773
+ out_channels=mid_channels,
774
+ temb_channels=0,
775
+ dropout=0.0,
776
+ )
777
+ for _ in range(depth)
778
+ ]
779
+ )
780
+
781
+ self.conv_out = nn.Conv2d(
782
+ mid_channels,
783
+ out_channels,
784
+ kernel_size=1,
785
+ )
786
+
787
+ def forward(self, x):
788
+ x = self.conv_in(x)
789
+ for block in self.res_block1:
790
+ x = block(x, None)
791
+ x = torch.nn.functional.interpolate(
792
+ x,
793
+ size=(
794
+ int(round(x.shape[2] * self.factor)),
795
+ int(round(x.shape[3] * self.factor)),
796
+ ),
797
+ )
798
+ x = self.attn(x)
799
+ for block in self.res_block2:
800
+ x = block(x, None)
801
+ x = self.conv_out(x)
802
+ return x
803
+
804
+
805
+ class MergedRescaleEncoder(nn.Module):
806
+ def __init__(
807
+ self,
808
+ in_channels,
809
+ ch,
810
+ resolution,
811
+ out_ch,
812
+ num_res_blocks,
813
+ attn_resolutions,
814
+ dropout=0.0,
815
+ resamp_with_conv=True,
816
+ ch_mult=(1, 2, 4, 8),
817
+ rescale_factor=1.0,
818
+ rescale_module_depth=1,
819
+ ):
820
+ super().__init__()
821
+ intermediate_chn = ch * ch_mult[-1]
822
+ self.encoder = Encoder(
823
+ in_channels=in_channels,
824
+ num_res_blocks=num_res_blocks,
825
+ ch=ch,
826
+ ch_mult=ch_mult,
827
+ z_channels=intermediate_chn,
828
+ double_z=False,
829
+ resolution=resolution,
830
+ attn_resolutions=attn_resolutions,
831
+ dropout=dropout,
832
+ resamp_with_conv=resamp_with_conv,
833
+ out_ch=None,
834
+ )
835
+ self.rescaler = LatentRescaler(
836
+ factor=rescale_factor,
837
+ in_channels=intermediate_chn,
838
+ mid_channels=intermediate_chn,
839
+ out_channels=out_ch,
840
+ depth=rescale_module_depth,
841
+ )
842
+
843
+ def forward(self, x):
844
+ x = self.encoder(x)
845
+ x = self.rescaler(x)
846
+ return x
847
+
848
+
849
+ class MergedRescaleDecoder(nn.Module):
850
+ def __init__(
851
+ self,
852
+ z_channels,
853
+ out_ch,
854
+ resolution,
855
+ num_res_blocks,
856
+ attn_resolutions,
857
+ ch,
858
+ ch_mult=(1, 2, 4, 8),
859
+ dropout=0.0,
860
+ resamp_with_conv=True,
861
+ rescale_factor=1.0,
862
+ rescale_module_depth=1,
863
+ ):
864
+ super().__init__()
865
+ tmp_chn = z_channels * ch_mult[-1]
866
+ self.decoder = Decoder(
867
+ out_ch=out_ch,
868
+ z_channels=tmp_chn,
869
+ attn_resolutions=attn_resolutions,
870
+ dropout=dropout,
871
+ resamp_with_conv=resamp_with_conv,
872
+ in_channels=None,
873
+ num_res_blocks=num_res_blocks,
874
+ ch_mult=ch_mult,
875
+ resolution=resolution,
876
+ ch=ch,
877
+ )
878
+ self.rescaler = LatentRescaler(
879
+ factor=rescale_factor,
880
+ in_channels=z_channels,
881
+ mid_channels=tmp_chn,
882
+ out_channels=tmp_chn,
883
+ depth=rescale_module_depth,
884
+ )
885
+
886
+ def forward(self, x):
887
+ x = self.rescaler(x)
888
+ x = self.decoder(x)
889
+ return x
890
+
891
+
892
+ class Upsampler(nn.Module):
893
+ def __init__(self, in_size, out_size, in_channels, out_channels, ch_mult=2):
894
+ super().__init__()
895
+ assert out_size >= in_size
896
+ num_blocks = int(np.log2(out_size // in_size)) + 1
897
+ factor_up = 1.0 + (out_size % in_size)
898
+ print(
899
+ f"Building {self.__class__.__name__} with in_size: {in_size} --> out_size {out_size} and factor {factor_up}"
900
+ )
901
+ self.rescaler = LatentRescaler(
902
+ factor=factor_up,
903
+ in_channels=in_channels,
904
+ mid_channels=2 * in_channels,
905
+ out_channels=in_channels,
906
+ )
907
+ self.decoder = Decoder(
908
+ out_ch=out_channels,
909
+ resolution=out_size,
910
+ z_channels=in_channels,
911
+ num_res_blocks=2,
912
+ attn_resolutions=[],
913
+ in_channels=None,
914
+ ch=in_channels,
915
+ ch_mult=[ch_mult for _ in range(num_blocks)],
916
+ )
917
+
918
+ def forward(self, x):
919
+ x = self.rescaler(x)
920
+ x = self.decoder(x)
921
+ return x
922
+
923
+
924
+ class Resize(nn.Module):
925
+ def __init__(self, in_channels=None, learned=False, mode="bilinear"):
926
+ super().__init__()
927
+ self.with_conv = learned
928
+ self.mode = mode
929
+ if self.with_conv:
930
+ print(
931
+ f"Note: {self.__class__.__name} uses learned downsampling and will ignore the fixed {mode} mode"
932
+ )
933
+ raise NotImplementedError()
934
+ assert in_channels is not None
935
+ # no asymmetric padding in torch conv, must do it ourselves
936
+ self.conv = torch.nn.Conv2d(
937
+ in_channels, in_channels, kernel_size=4, stride=2, padding=1
938
+ )
939
+
940
+ def forward(self, x, scale_factor=1.0):
941
+ if scale_factor == 1.0:
942
+ return x
943
+ else:
944
+ x = torch.nn.functional.interpolate(
945
+ x, mode=self.mode, align_corners=False, scale_factor=scale_factor
946
+ )
947
+ return x
948
+
949
+
950
+ class FirstStagePostProcessor(nn.Module):
951
+
952
+ def __init__(
953
+ self,
954
+ ch_mult: list,
955
+ in_channels,
956
+ pretrained_model: nn.Module = None,
957
+ reshape=False,
958
+ n_channels=None,
959
+ dropout=0.0,
960
+ pretrained_config=None,
961
+ ):
962
+ super().__init__()
963
+ if pretrained_config is None:
964
+ assert (
965
+ pretrained_model is not None
966
+ ), 'Either "pretrained_model" or "pretrained_config" must not be None'
967
+ self.pretrained_model = pretrained_model
968
+ else:
969
+ assert (
970
+ pretrained_config is not None
971
+ ), 'Either "pretrained_model" or "pretrained_config" must not be None'
972
+ self.instantiate_pretrained(pretrained_config)
973
+
974
+ self.do_reshape = reshape
975
+
976
+ if n_channels is None:
977
+ n_channels = self.pretrained_model.encoder.ch
978
+
979
+ self.proj_norm = Normalize(in_channels, num_groups=in_channels // 2)
980
+ self.proj = nn.Conv2d(
981
+ in_channels, n_channels, kernel_size=3, stride=1, padding=1
982
+ )
983
+
984
+ blocks = []
985
+ downs = []
986
+ ch_in = n_channels
987
+ for m in ch_mult:
988
+ blocks.append(
989
+ ResnetBlock(
990
+ in_channels=ch_in, out_channels=m * n_channels, dropout=dropout
991
+ )
992
+ )
993
+ ch_in = m * n_channels
994
+ downs.append(Downsample(ch_in, with_conv=False))
995
+
996
+ self.model = nn.ModuleList(blocks)
997
+ self.downsampler = nn.ModuleList(downs)
998
+
999
+ def instantiate_pretrained(self, config):
1000
+ model = instantiate_from_config(config)
1001
+ self.pretrained_model = model.eval()
1002
+ # self.pretrained_model.train = False
1003
+ for param in self.pretrained_model.parameters():
1004
+ param.requires_grad = False
1005
+
1006
+ @torch.no_grad()
1007
+ def encode_with_pretrained(self, x):
1008
+ c = self.pretrained_model.encode(x)
1009
+ if isinstance(c, DiagonalGaussianDistribution):
1010
+ c = c.mode()
1011
+ return c
1012
+
1013
+ def forward(self, x):
1014
+ z_fs = self.encode_with_pretrained(x)
1015
+ z = self.proj_norm(z_fs)
1016
+ z = self.proj(z)
1017
+ z = nonlinearity(z)
1018
+
1019
+ for submodel, downmodel in zip(self.model, self.downsampler):
1020
+ z = submodel(z, temb=None)
1021
+ z = downmodel(z)
1022
+
1023
+ if self.do_reshape:
1024
+ z = rearrange(z, "b c h w -> b (h w) c")
1025
+ return z
lvdm/modules/networks/openaimodel3d.py ADDED
@@ -0,0 +1,710 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from functools import partial
2
+ from abc import abstractmethod
3
+ import torch
4
+ import torch.nn as nn
5
+ from einops import rearrange
6
+ import torch.nn.functional as F
7
+ from lvdm.models.utils_diffusion import timestep_embedding
8
+ from lvdm.common import checkpoint
9
+ from lvdm.basics import zero_module, conv_nd, linear, avg_pool_nd, normalization
10
+ from lvdm.modules.attention import SpatialTransformer, TemporalTransformer
11
+
12
+
13
+ class TimestepBlock(nn.Module):
14
+ """
15
+ Any module where forward() takes timestep embeddings as a second argument.
16
+ """
17
+
18
+ @abstractmethod
19
+ def forward(self, x, emb):
20
+ """
21
+ Apply the module to `x` given `emb` timestep embeddings.
22
+ """
23
+
24
+
25
+ class TimestepEmbedSequential(nn.Sequential, TimestepBlock):
26
+ """
27
+ A sequential module that passes timestep embeddings to the children that
28
+ support it as an extra input.
29
+ """
30
+
31
+ def forward(self, x, emb, context=None, batch_size=None):
32
+ for layer in self:
33
+ if isinstance(layer, TimestepBlock):
34
+ x = layer(x, emb, batch_size)
35
+ elif isinstance(layer, SpatialTransformer):
36
+ x = layer(x, context)
37
+ elif isinstance(layer, TemporalTransformer):
38
+ x = rearrange(x, "(b f) c h w -> b c f h w", b=batch_size)
39
+ x = layer(x, context)
40
+ x = rearrange(x, "b c f h w -> (b f) c h w")
41
+ else:
42
+ x = layer(
43
+ x,
44
+ )
45
+ return x
46
+
47
+
48
+ class Downsample(nn.Module):
49
+ """
50
+ A downsampling layer with an optional convolution.
51
+ :param channels: channels in the inputs and outputs.
52
+ :param use_conv: a bool determining if a convolution is applied.
53
+ :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then
54
+ downsampling occurs in the inner-two dimensions.
55
+ """
56
+
57
+ def __init__(self, channels, use_conv, dims=2, out_channels=None, padding=1):
58
+ super().__init__()
59
+ self.channels = channels
60
+ self.out_channels = out_channels or channels
61
+ self.use_conv = use_conv
62
+ self.dims = dims
63
+ stride = 2 if dims != 3 else (1, 2, 2)
64
+ if use_conv:
65
+ self.op = conv_nd(
66
+ dims,
67
+ self.channels,
68
+ self.out_channels,
69
+ 3,
70
+ stride=stride,
71
+ padding=padding,
72
+ )
73
+ else:
74
+ assert self.channels == self.out_channels
75
+ self.op = avg_pool_nd(dims, kernel_size=stride, stride=stride)
76
+
77
+ def forward(self, x):
78
+ assert x.shape[1] == self.channels
79
+ return self.op(x)
80
+
81
+
82
+ class Upsample(nn.Module):
83
+ """
84
+ An upsampling layer with an optional convolution.
85
+ :param channels: channels in the inputs and outputs.
86
+ :param use_conv: a bool determining if a convolution is applied.
87
+ :param dims: determines if the signal is 1D, 2D, or 3D. If 3D, then
88
+ upsampling occurs in the inner-two dimensions.
89
+ """
90
+
91
+ def __init__(self, channels, use_conv, dims=2, out_channels=None, padding=1):
92
+ super().__init__()
93
+ self.channels = channels
94
+ self.out_channels = out_channels or channels
95
+ self.use_conv = use_conv
96
+ self.dims = dims
97
+ if use_conv:
98
+ self.conv = conv_nd(
99
+ dims, self.channels, self.out_channels, 3, padding=padding
100
+ )
101
+
102
+ def forward(self, x):
103
+ assert x.shape[1] == self.channels
104
+ if self.dims == 3:
105
+ x = F.interpolate(
106
+ x, (x.shape[2], x.shape[3] * 2, x.shape[4] * 2), mode="nearest"
107
+ )
108
+ else:
109
+ x = F.interpolate(x, scale_factor=2, mode="nearest")
110
+ if self.use_conv:
111
+ x = self.conv(x)
112
+ return x
113
+
114
+
115
+ class ResBlock(TimestepBlock):
116
+ """
117
+ A residual block that can optionally change the number of channels.
118
+ :param channels: the number of input channels.
119
+ :param emb_channels: the number of timestep embedding channels.
120
+ :param dropout: the rate of dropout.
121
+ :param out_channels: if specified, the number of out channels.
122
+ :param use_conv: if True and out_channels is specified, use a spatial
123
+ convolution instead of a smaller 1x1 convolution to change the
124
+ channels in the skip connection.
125
+ :param dims: determines if the signal is 1D, 2D, or 3D.
126
+ :param up: if True, use this block for upsampling.
127
+ :param down: if True, use this block for downsampling.
128
+ """
129
+
130
+ def __init__(
131
+ self,
132
+ channels,
133
+ emb_channels,
134
+ dropout,
135
+ out_channels=None,
136
+ use_scale_shift_norm=False,
137
+ dims=2,
138
+ use_checkpoint=False,
139
+ use_conv=False,
140
+ up=False,
141
+ down=False,
142
+ use_temporal_conv=False,
143
+ tempspatial_aware=False,
144
+ ):
145
+ super().__init__()
146
+ self.channels = channels
147
+ self.emb_channels = emb_channels
148
+ self.dropout = dropout
149
+ self.out_channels = out_channels or channels
150
+ self.use_conv = use_conv
151
+ self.use_checkpoint = use_checkpoint
152
+ self.use_scale_shift_norm = use_scale_shift_norm
153
+ self.use_temporal_conv = use_temporal_conv
154
+
155
+ self.in_layers = nn.Sequential(
156
+ normalization(channels),
157
+ nn.SiLU(),
158
+ conv_nd(dims, channels, self.out_channels, 3, padding=1),
159
+ )
160
+
161
+ self.updown = up or down
162
+
163
+ if up:
164
+ self.h_upd = Upsample(channels, False, dims)
165
+ self.x_upd = Upsample(channels, False, dims)
166
+ elif down:
167
+ self.h_upd = Downsample(channels, False, dims)
168
+ self.x_upd = Downsample(channels, False, dims)
169
+ else:
170
+ self.h_upd = self.x_upd = nn.Identity()
171
+
172
+ self.emb_layers = nn.Sequential(
173
+ nn.SiLU(),
174
+ nn.Linear(
175
+ emb_channels,
176
+ 2 * self.out_channels if use_scale_shift_norm else self.out_channels,
177
+ ),
178
+ )
179
+ self.out_layers = nn.Sequential(
180
+ normalization(self.out_channels),
181
+ nn.SiLU(),
182
+ nn.Dropout(p=dropout),
183
+ zero_module(nn.Conv2d(self.out_channels, self.out_channels, 3, padding=1)),
184
+ )
185
+
186
+ if self.out_channels == channels:
187
+ self.skip_connection = nn.Identity()
188
+ elif use_conv:
189
+ self.skip_connection = conv_nd(
190
+ dims, channels, self.out_channels, 3, padding=1
191
+ )
192
+ else:
193
+ self.skip_connection = conv_nd(dims, channels, self.out_channels, 1)
194
+
195
+ if self.use_temporal_conv:
196
+ self.temopral_conv = TemporalConvBlock(
197
+ self.out_channels,
198
+ self.out_channels,
199
+ dropout=0.1,
200
+ spatial_aware=tempspatial_aware,
201
+ )
202
+
203
+ def forward(self, x, emb, batch_size=None):
204
+ """
205
+ Apply the block to a Tensor, conditioned on a timestep embedding.
206
+ :param x: an [N x C x ...] Tensor of features.
207
+ :param emb: an [N x emb_channels] Tensor of timestep embeddings.
208
+ :return: an [N x C x ...] Tensor of outputs.
209
+ """
210
+ input_tuple = (
211
+ x,
212
+ emb,
213
+ )
214
+ if batch_size:
215
+ forward_batchsize = partial(self._forward, batch_size=batch_size)
216
+ return checkpoint(
217
+ forward_batchsize, input_tuple, self.parameters(), self.use_checkpoint
218
+ )
219
+ return checkpoint(
220
+ self._forward, input_tuple, self.parameters(), self.use_checkpoint
221
+ )
222
+
223
+ def _forward(
224
+ self,
225
+ x,
226
+ emb,
227
+ batch_size=None,
228
+ ):
229
+ if self.updown:
230
+ in_rest, in_conv = self.in_layers[:-1], self.in_layers[-1]
231
+ h = in_rest(x)
232
+ h = self.h_upd(h)
233
+ x = self.x_upd(x)
234
+ h = in_conv(h)
235
+ else:
236
+ h = self.in_layers(x)
237
+ emb_out = self.emb_layers(emb).type(h.dtype)
238
+ while len(emb_out.shape) < len(h.shape):
239
+ emb_out = emb_out[..., None]
240
+ if self.use_scale_shift_norm:
241
+ out_norm, out_rest = self.out_layers[0], self.out_layers[1:]
242
+ scale, shift = torch.chunk(emb_out, 2, dim=1)
243
+ h = out_norm(h) * (1 + scale) + shift
244
+ h = out_rest(h)
245
+ else:
246
+ h = h + emb_out
247
+ h = self.out_layers(h)
248
+ h = self.skip_connection(x) + h
249
+
250
+ if self.use_temporal_conv and batch_size:
251
+ h = rearrange(h, "(b t) c h w -> b c t h w", b=batch_size)
252
+ h = self.temopral_conv(h)
253
+ h = rearrange(h, "b c t h w -> (b t) c h w")
254
+ return h
255
+
256
+
257
+ class TemporalConvBlock(nn.Module):
258
+ """
259
+ Adapted from modelscope: https://github.com/modelscope/modelscope/blob/master/modelscope/models/multi_modal/video_synthesis/unet_sd.py
260
+ """
261
+
262
+ def __init__(
263
+ self, in_channels, out_channels=None, dropout=0.0, spatial_aware=False
264
+ ):
265
+ super(TemporalConvBlock, self).__init__()
266
+ if out_channels is None:
267
+ out_channels = in_channels
268
+ self.in_channels = in_channels
269
+ self.out_channels = out_channels
270
+ kernel_shape = (3, 1, 1) if not spatial_aware else (3, 3, 3)
271
+ padding_shape = (1, 0, 0) if not spatial_aware else (1, 1, 1)
272
+
273
+ # conv layers
274
+ self.conv1 = nn.Sequential(
275
+ nn.GroupNorm(32, in_channels),
276
+ nn.SiLU(),
277
+ nn.Conv3d(in_channels, out_channels, kernel_shape, padding=padding_shape),
278
+ )
279
+ self.conv2 = nn.Sequential(
280
+ nn.GroupNorm(32, out_channels),
281
+ nn.SiLU(),
282
+ nn.Dropout(dropout),
283
+ nn.Conv3d(out_channels, in_channels, kernel_shape, padding=padding_shape),
284
+ )
285
+ self.conv3 = nn.Sequential(
286
+ nn.GroupNorm(32, out_channels),
287
+ nn.SiLU(),
288
+ nn.Dropout(dropout),
289
+ nn.Conv3d(out_channels, in_channels, (3, 1, 1), padding=(1, 0, 0)),
290
+ )
291
+ self.conv4 = nn.Sequential(
292
+ nn.GroupNorm(32, out_channels),
293
+ nn.SiLU(),
294
+ nn.Dropout(dropout),
295
+ nn.Conv3d(out_channels, in_channels, (3, 1, 1), padding=(1, 0, 0)),
296
+ )
297
+
298
+ # zero out the last layer params,so the conv block is identity
299
+ nn.init.zeros_(self.conv4[-1].weight)
300
+ nn.init.zeros_(self.conv4[-1].bias)
301
+
302
+ def forward(self, x):
303
+ identity = x
304
+ x = self.conv1(x)
305
+ x = self.conv2(x)
306
+ x = self.conv3(x)
307
+ x = self.conv4(x)
308
+
309
+ return x + identity
310
+
311
+
312
+ class UNetModel(nn.Module):
313
+ """
314
+ The full UNet model with attention and timestep embedding.
315
+ :param in_channels: in_channels in the input Tensor.
316
+ :param model_channels: base channel count for the model.
317
+ :param out_channels: channels in the output Tensor.
318
+ :param num_res_blocks: number of residual blocks per downsample.
319
+ :param attention_resolutions: a collection of downsample rates at which
320
+ attention will take place. May be a set, list, or tuple.
321
+ For example, if this contains 4, then at 4x downsampling, attention
322
+ will be used.
323
+ :param dropout: the dropout probability.
324
+ :param channel_mult: channel multiplier for each level of the UNet.
325
+ :param conv_resample: if True, use learned convolutions for upsampling and
326
+ downsampling.
327
+ :param dims: determines if the signal is 1D, 2D, or 3D.
328
+ :param num_classes: if specified (as an int), then this model will be
329
+ class-conditional with `num_classes` classes.
330
+ :param use_checkpoint: use gradient checkpointing to reduce memory usage.
331
+ :param num_heads: the number of attention heads in each attention layer.
332
+ :param num_heads_channels: if specified, ignore num_heads and instead use
333
+ a fixed channel width per attention head.
334
+ :param num_heads_upsample: works with num_heads to set a different number
335
+ of heads for upsampling. Deprecated.
336
+ :param use_scale_shift_norm: use a FiLM-like conditioning mechanism.
337
+ :param resblock_updown: use residual blocks for up/downsampling.
338
+ """
339
+
340
+ def __init__(
341
+ self,
342
+ in_channels,
343
+ model_channels,
344
+ out_channels,
345
+ num_res_blocks,
346
+ attention_resolutions,
347
+ dropout=0.0,
348
+ channel_mult=(1, 2, 4, 8),
349
+ conv_resample=True,
350
+ dims=2,
351
+ context_dim=None,
352
+ use_scale_shift_norm=False,
353
+ resblock_updown=False,
354
+ num_heads=-1,
355
+ num_head_channels=-1,
356
+ transformer_depth=1,
357
+ use_linear=False,
358
+ use_checkpoint=False,
359
+ temporal_conv=False,
360
+ tempspatial_aware=False,
361
+ temporal_attention=True,
362
+ temporal_selfatt_only=True,
363
+ use_relative_position=True,
364
+ use_causal_attention=False,
365
+ temporal_length=None,
366
+ use_fp16=False,
367
+ addition_attention=False,
368
+ use_image_attention=False,
369
+ temporal_transformer_depth=1,
370
+ fps_cond=False,
371
+ time_cond_proj_dim=None,
372
+ ):
373
+ super(UNetModel, self).__init__()
374
+ if num_heads == -1:
375
+ assert (
376
+ num_head_channels != -1
377
+ ), "Either num_heads or num_head_channels has to be set"
378
+ if num_head_channels == -1:
379
+ assert (
380
+ num_heads != -1
381
+ ), "Either num_heads or num_head_channels has to be set"
382
+
383
+ self.in_channels = in_channels
384
+ self.model_channels = model_channels
385
+ self.out_channels = out_channels
386
+ self.num_res_blocks = num_res_blocks
387
+ self.attention_resolutions = attention_resolutions
388
+ self.dropout = dropout
389
+ self.channel_mult = channel_mult
390
+ self.conv_resample = conv_resample
391
+ self.temporal_attention = temporal_attention
392
+ time_embed_dim = model_channels * 4
393
+ self.use_checkpoint = use_checkpoint
394
+ self.dtype = torch.float16 if use_fp16 else torch.float32
395
+ self.addition_attention = addition_attention
396
+ self.use_image_attention = use_image_attention
397
+ self.fps_cond = fps_cond
398
+ self.time_cond_proj_dim = time_cond_proj_dim
399
+
400
+ self.time_embed = nn.Sequential(
401
+ linear(model_channels, time_embed_dim),
402
+ nn.SiLU(),
403
+ linear(time_embed_dim, time_embed_dim),
404
+ )
405
+ if self.fps_cond:
406
+ self.fps_embedding = nn.Sequential(
407
+ linear(model_channels, time_embed_dim),
408
+ nn.SiLU(),
409
+ linear(time_embed_dim, time_embed_dim),
410
+ )
411
+ if time_cond_proj_dim is not None:
412
+ self.time_cond_proj = nn.Linear(
413
+ time_cond_proj_dim, model_channels, bias=False
414
+ )
415
+ else:
416
+ self.time_cond_proj = None
417
+
418
+ self.input_blocks = nn.ModuleList(
419
+ [
420
+ TimestepEmbedSequential(
421
+ conv_nd(dims, in_channels, model_channels, 3, padding=1)
422
+ )
423
+ ]
424
+ )
425
+ if self.addition_attention:
426
+ self.init_attn = TimestepEmbedSequential(
427
+ TemporalTransformer(
428
+ model_channels,
429
+ n_heads=8,
430
+ d_head=num_head_channels,
431
+ depth=transformer_depth,
432
+ context_dim=context_dim,
433
+ use_checkpoint=use_checkpoint,
434
+ only_self_att=temporal_selfatt_only,
435
+ causal_attention=use_causal_attention,
436
+ relative_position=use_relative_position,
437
+ temporal_length=temporal_length,
438
+ )
439
+ )
440
+
441
+ input_block_chans = [model_channels]
442
+ ch = model_channels
443
+ ds = 1
444
+ for level, mult in enumerate(channel_mult):
445
+ for _ in range(num_res_blocks):
446
+ layers = [
447
+ ResBlock(
448
+ ch,
449
+ time_embed_dim,
450
+ dropout,
451
+ out_channels=mult * model_channels,
452
+ dims=dims,
453
+ use_checkpoint=use_checkpoint,
454
+ use_scale_shift_norm=use_scale_shift_norm,
455
+ tempspatial_aware=tempspatial_aware,
456
+ use_temporal_conv=temporal_conv,
457
+ )
458
+ ]
459
+ ch = mult * model_channels
460
+ if ds in attention_resolutions:
461
+ if num_head_channels == -1:
462
+ dim_head = ch // num_heads
463
+ else:
464
+ num_heads = ch // num_head_channels
465
+ dim_head = num_head_channels
466
+ layers.append(
467
+ SpatialTransformer(
468
+ ch,
469
+ num_heads,
470
+ dim_head,
471
+ depth=transformer_depth,
472
+ context_dim=context_dim,
473
+ use_linear=use_linear,
474
+ use_checkpoint=use_checkpoint,
475
+ disable_self_attn=False,
476
+ img_cross_attention=self.use_image_attention,
477
+ )
478
+ )
479
+ if self.temporal_attention:
480
+ layers.append(
481
+ TemporalTransformer(
482
+ ch,
483
+ num_heads,
484
+ dim_head,
485
+ depth=temporal_transformer_depth,
486
+ context_dim=context_dim,
487
+ use_linear=use_linear,
488
+ use_checkpoint=use_checkpoint,
489
+ only_self_att=temporal_selfatt_only,
490
+ causal_attention=use_causal_attention,
491
+ relative_position=use_relative_position,
492
+ temporal_length=temporal_length,
493
+ )
494
+ )
495
+ self.input_blocks.append(TimestepEmbedSequential(*layers))
496
+ input_block_chans.append(ch)
497
+ if level != len(channel_mult) - 1:
498
+ out_ch = ch
499
+ self.input_blocks.append(
500
+ TimestepEmbedSequential(
501
+ ResBlock(
502
+ ch,
503
+ time_embed_dim,
504
+ dropout,
505
+ out_channels=out_ch,
506
+ dims=dims,
507
+ use_checkpoint=use_checkpoint,
508
+ use_scale_shift_norm=use_scale_shift_norm,
509
+ down=True,
510
+ )
511
+ if resblock_updown
512
+ else Downsample(
513
+ ch, conv_resample, dims=dims, out_channels=out_ch
514
+ )
515
+ )
516
+ )
517
+ ch = out_ch
518
+ input_block_chans.append(ch)
519
+ ds *= 2
520
+
521
+ if num_head_channels == -1:
522
+ dim_head = ch // num_heads
523
+ else:
524
+ num_heads = ch // num_head_channels
525
+ dim_head = num_head_channels
526
+ layers = [
527
+ ResBlock(
528
+ ch,
529
+ time_embed_dim,
530
+ dropout,
531
+ dims=dims,
532
+ use_checkpoint=use_checkpoint,
533
+ use_scale_shift_norm=use_scale_shift_norm,
534
+ tempspatial_aware=tempspatial_aware,
535
+ use_temporal_conv=temporal_conv,
536
+ ),
537
+ SpatialTransformer(
538
+ ch,
539
+ num_heads,
540
+ dim_head,
541
+ depth=transformer_depth,
542
+ context_dim=context_dim,
543
+ use_linear=use_linear,
544
+ use_checkpoint=use_checkpoint,
545
+ disable_self_attn=False,
546
+ img_cross_attention=self.use_image_attention,
547
+ ),
548
+ ]
549
+ if self.temporal_attention:
550
+ layers.append(
551
+ TemporalTransformer(
552
+ ch,
553
+ num_heads,
554
+ dim_head,
555
+ depth=temporal_transformer_depth,
556
+ context_dim=context_dim,
557
+ use_linear=use_linear,
558
+ use_checkpoint=use_checkpoint,
559
+ only_self_att=temporal_selfatt_only,
560
+ causal_attention=use_causal_attention,
561
+ relative_position=use_relative_position,
562
+ temporal_length=temporal_length,
563
+ )
564
+ )
565
+ layers.append(
566
+ ResBlock(
567
+ ch,
568
+ time_embed_dim,
569
+ dropout,
570
+ dims=dims,
571
+ use_checkpoint=use_checkpoint,
572
+ use_scale_shift_norm=use_scale_shift_norm,
573
+ tempspatial_aware=tempspatial_aware,
574
+ use_temporal_conv=temporal_conv,
575
+ )
576
+ )
577
+ self.middle_block = TimestepEmbedSequential(*layers)
578
+
579
+ self.output_blocks = nn.ModuleList([])
580
+ for level, mult in list(enumerate(channel_mult))[::-1]:
581
+ for i in range(num_res_blocks + 1):
582
+ ich = input_block_chans.pop()
583
+ layers = [
584
+ ResBlock(
585
+ ch + ich,
586
+ time_embed_dim,
587
+ dropout,
588
+ out_channels=mult * model_channels,
589
+ dims=dims,
590
+ use_checkpoint=use_checkpoint,
591
+ use_scale_shift_norm=use_scale_shift_norm,
592
+ tempspatial_aware=tempspatial_aware,
593
+ use_temporal_conv=temporal_conv,
594
+ )
595
+ ]
596
+ ch = model_channels * mult
597
+ if ds in attention_resolutions:
598
+ if num_head_channels == -1:
599
+ dim_head = ch // num_heads
600
+ else:
601
+ num_heads = ch // num_head_channels
602
+ dim_head = num_head_channels
603
+ layers.append(
604
+ SpatialTransformer(
605
+ ch,
606
+ num_heads,
607
+ dim_head,
608
+ depth=transformer_depth,
609
+ context_dim=context_dim,
610
+ use_linear=use_linear,
611
+ use_checkpoint=use_checkpoint,
612
+ disable_self_attn=False,
613
+ img_cross_attention=self.use_image_attention,
614
+ )
615
+ )
616
+ if self.temporal_attention:
617
+ layers.append(
618
+ TemporalTransformer(
619
+ ch,
620
+ num_heads,
621
+ dim_head,
622
+ depth=temporal_transformer_depth,
623
+ context_dim=context_dim,
624
+ use_linear=use_linear,
625
+ use_checkpoint=use_checkpoint,
626
+ only_self_att=temporal_selfatt_only,
627
+ causal_attention=use_causal_attention,
628
+ relative_position=use_relative_position,
629
+ temporal_length=temporal_length,
630
+ )
631
+ )
632
+ if level and i == num_res_blocks:
633
+ out_ch = ch
634
+ layers.append(
635
+ ResBlock(
636
+ ch,
637
+ time_embed_dim,
638
+ dropout,
639
+ out_channels=out_ch,
640
+ dims=dims,
641
+ use_checkpoint=use_checkpoint,
642
+ use_scale_shift_norm=use_scale_shift_norm,
643
+ up=True,
644
+ )
645
+ if resblock_updown
646
+ else Upsample(ch, conv_resample, dims=dims, out_channels=out_ch)
647
+ )
648
+ ds //= 2
649
+ self.output_blocks.append(TimestepEmbedSequential(*layers))
650
+
651
+ self.out = nn.Sequential(
652
+ normalization(ch),
653
+ nn.SiLU(),
654
+ zero_module(conv_nd(dims, model_channels, out_channels, 3, padding=1)),
655
+ )
656
+
657
+ def forward(
658
+ self,
659
+ x,
660
+ timesteps,
661
+ context=None,
662
+ features_adapter=None,
663
+ fps=16,
664
+ timestep_cond=None,
665
+ **kwargs
666
+ ):
667
+ t_emb = timestep_embedding(timesteps, self.model_channels, repeat_only=False)
668
+ if timestep_cond is not None:
669
+ t_emb = t_emb + self.time_cond_proj(timestep_cond)
670
+ emb = self.time_embed(t_emb)
671
+
672
+ if self.fps_cond:
673
+ if type(fps) == int:
674
+ fps = torch.full_like(timesteps, fps)
675
+ fps_emb = timestep_embedding(fps, self.model_channels, repeat_only=False)
676
+ emb += self.fps_embedding(fps_emb)
677
+
678
+ b, _, t, _, _ = x.shape
679
+ ## repeat t times for context [(b t) 77 768] & time embedding
680
+ context = context.repeat_interleave(repeats=t, dim=0)
681
+ emb = emb.repeat_interleave(repeats=t, dim=0)
682
+
683
+ ## always in shape (b t) c h w, except for temporal layer
684
+ x = rearrange(x, "b c t h w -> (b t) c h w")
685
+
686
+ h = x.type(self.dtype)
687
+ adapter_idx = 0
688
+ hs = []
689
+ for id, module in enumerate(self.input_blocks):
690
+ h = module(h, emb, context=context, batch_size=b)
691
+ if id == 0 and self.addition_attention:
692
+ h = self.init_attn(h, emb, context=context, batch_size=b)
693
+ ## plug-in adapter features
694
+ if ((id + 1) % 3 == 0) and features_adapter is not None:
695
+ h = h + features_adapter[adapter_idx]
696
+ adapter_idx += 1
697
+ hs.append(h)
698
+ if features_adapter is not None:
699
+ assert len(features_adapter) == adapter_idx, "Wrong features_adapter"
700
+
701
+ h = self.middle_block(h, emb, context=context, batch_size=b)
702
+ for module in self.output_blocks:
703
+ h = torch.cat([h, hs.pop()], dim=1)
704
+ h = module(h, emb, context=context, batch_size=b)
705
+ h = h.type(x.dtype)
706
+ y = self.out(h)
707
+
708
+ # reshape back to (b c t h w)
709
+ y = rearrange(y, "(b t) c h w -> b c t h w", b=b)
710
+ return y
lvdm/modules/x_transformer.py ADDED
@@ -0,0 +1,704 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """shout-out to https://github.com/lucidrains/x-transformers/tree/main/x_transformers"""
2
+
3
+ from functools import partial
4
+ from inspect import isfunction
5
+ from collections import namedtuple
6
+ from einops import rearrange, repeat
7
+ import torch
8
+ from torch import nn, einsum
9
+ import torch.nn.functional as F
10
+
11
+ # constants
12
+ DEFAULT_DIM_HEAD = 64
13
+
14
+ Intermediates = namedtuple("Intermediates", ["pre_softmax_attn", "post_softmax_attn"])
15
+
16
+ LayerIntermediates = namedtuple("Intermediates", ["hiddens", "attn_intermediates"])
17
+
18
+
19
+ class AbsolutePositionalEmbedding(nn.Module):
20
+ def __init__(self, dim, max_seq_len):
21
+ super().__init__()
22
+ self.emb = nn.Embedding(max_seq_len, dim)
23
+ self.init_()
24
+
25
+ def init_(self):
26
+ nn.init.normal_(self.emb.weight, std=0.02)
27
+
28
+ def forward(self, x):
29
+ n = torch.arange(x.shape[1], device=x.device)
30
+ return self.emb(n)[None, :, :]
31
+
32
+
33
+ class FixedPositionalEmbedding(nn.Module):
34
+ def __init__(self, dim):
35
+ super().__init__()
36
+ inv_freq = 1.0 / (10000 ** (torch.arange(0, dim, 2).float() / dim))
37
+ self.register_buffer("inv_freq", inv_freq)
38
+
39
+ def forward(self, x, seq_dim=1, offset=0):
40
+ t = (
41
+ torch.arange(x.shape[seq_dim], device=x.device).type_as(self.inv_freq)
42
+ + offset
43
+ )
44
+ sinusoid_inp = torch.einsum("i , j -> i j", t, self.inv_freq)
45
+ emb = torch.cat((sinusoid_inp.sin(), sinusoid_inp.cos()), dim=-1)
46
+ return emb[None, :, :]
47
+
48
+
49
+ # helpers
50
+
51
+
52
+ def exists(val):
53
+ return val is not None
54
+
55
+
56
+ def default(val, d):
57
+ if exists(val):
58
+ return val
59
+ return d() if isfunction(d) else d
60
+
61
+
62
+ def always(val):
63
+ def inner(*args, **kwargs):
64
+ return val
65
+
66
+ return inner
67
+
68
+
69
+ def not_equals(val):
70
+ def inner(x):
71
+ return x != val
72
+
73
+ return inner
74
+
75
+
76
+ def equals(val):
77
+ def inner(x):
78
+ return x == val
79
+
80
+ return inner
81
+
82
+
83
+ def max_neg_value(tensor):
84
+ return -torch.finfo(tensor.dtype).max
85
+
86
+
87
+ # keyword argument helpers
88
+
89
+
90
+ def pick_and_pop(keys, d):
91
+ values = list(map(lambda key: d.pop(key), keys))
92
+ return dict(zip(keys, values))
93
+
94
+
95
+ def group_dict_by_key(cond, d):
96
+ return_val = [dict(), dict()]
97
+ for key in d.keys():
98
+ match = bool(cond(key))
99
+ ind = int(not match)
100
+ return_val[ind][key] = d[key]
101
+ return (*return_val,)
102
+
103
+
104
+ def string_begins_with(prefix, str):
105
+ return str.startswith(prefix)
106
+
107
+
108
+ def group_by_key_prefix(prefix, d):
109
+ return group_dict_by_key(partial(string_begins_with, prefix), d)
110
+
111
+
112
+ def groupby_prefix_and_trim(prefix, d):
113
+ kwargs_with_prefix, kwargs = group_dict_by_key(
114
+ partial(string_begins_with, prefix), d
115
+ )
116
+ kwargs_without_prefix = dict(
117
+ map(lambda x: (x[0][len(prefix) :], x[1]), tuple(kwargs_with_prefix.items()))
118
+ )
119
+ return kwargs_without_prefix, kwargs
120
+
121
+
122
+ # classes
123
+ class Scale(nn.Module):
124
+ def __init__(self, value, fn):
125
+ super().__init__()
126
+ self.value = value
127
+ self.fn = fn
128
+
129
+ def forward(self, x, **kwargs):
130
+ x, *rest = self.fn(x, **kwargs)
131
+ return (x * self.value, *rest)
132
+
133
+
134
+ class Rezero(nn.Module):
135
+ def __init__(self, fn):
136
+ super().__init__()
137
+ self.fn = fn
138
+ self.g = nn.Parameter(torch.zeros(1))
139
+
140
+ def forward(self, x, **kwargs):
141
+ x, *rest = self.fn(x, **kwargs)
142
+ return (x * self.g, *rest)
143
+
144
+
145
+ class ScaleNorm(nn.Module):
146
+ def __init__(self, dim, eps=1e-5):
147
+ super().__init__()
148
+ self.scale = dim**-0.5
149
+ self.eps = eps
150
+ self.g = nn.Parameter(torch.ones(1))
151
+
152
+ def forward(self, x):
153
+ norm = torch.norm(x, dim=-1, keepdim=True) * self.scale
154
+ return x / norm.clamp(min=self.eps) * self.g
155
+
156
+
157
+ class RMSNorm(nn.Module):
158
+ def __init__(self, dim, eps=1e-8):
159
+ super().__init__()
160
+ self.scale = dim**-0.5
161
+ self.eps = eps
162
+ self.g = nn.Parameter(torch.ones(dim))
163
+
164
+ def forward(self, x):
165
+ norm = torch.norm(x, dim=-1, keepdim=True) * self.scale
166
+ return x / norm.clamp(min=self.eps) * self.g
167
+
168
+
169
+ class Residual(nn.Module):
170
+ def forward(self, x, residual):
171
+ return x + residual
172
+
173
+
174
+ class GRUGating(nn.Module):
175
+ def __init__(self, dim):
176
+ super().__init__()
177
+ self.gru = nn.GRUCell(dim, dim)
178
+
179
+ def forward(self, x, residual):
180
+ gated_output = self.gru(
181
+ rearrange(x, "b n d -> (b n) d"), rearrange(residual, "b n d -> (b n) d")
182
+ )
183
+
184
+ return gated_output.reshape_as(x)
185
+
186
+
187
+ # feedforward
188
+
189
+
190
+ class GEGLU(nn.Module):
191
+ def __init__(self, dim_in, dim_out):
192
+ super().__init__()
193
+ self.proj = nn.Linear(dim_in, dim_out * 2)
194
+
195
+ def forward(self, x):
196
+ x, gate = self.proj(x).chunk(2, dim=-1)
197
+ return x * F.gelu(gate)
198
+
199
+
200
+ class FeedForward(nn.Module):
201
+ def __init__(self, dim, dim_out=None, mult=4, glu=False, dropout=0.0):
202
+ super().__init__()
203
+ inner_dim = int(dim * mult)
204
+ dim_out = default(dim_out, dim)
205
+ project_in = (
206
+ nn.Sequential(nn.Linear(dim, inner_dim), nn.GELU())
207
+ if not glu
208
+ else GEGLU(dim, inner_dim)
209
+ )
210
+
211
+ self.net = nn.Sequential(
212
+ project_in, nn.Dropout(dropout), nn.Linear(inner_dim, dim_out)
213
+ )
214
+
215
+ def forward(self, x):
216
+ return self.net(x)
217
+
218
+
219
+ # attention.
220
+ class Attention(nn.Module):
221
+ def __init__(
222
+ self,
223
+ dim,
224
+ dim_head=DEFAULT_DIM_HEAD,
225
+ heads=8,
226
+ causal=False,
227
+ mask=None,
228
+ talking_heads=False,
229
+ sparse_topk=None,
230
+ use_entmax15=False,
231
+ num_mem_kv=0,
232
+ dropout=0.0,
233
+ on_attn=False,
234
+ ):
235
+ super().__init__()
236
+ if use_entmax15:
237
+ raise NotImplementedError(
238
+ "Check out entmax activation instead of softmax activation!"
239
+ )
240
+ self.scale = dim_head**-0.5
241
+ self.heads = heads
242
+ self.causal = causal
243
+ self.mask = mask
244
+
245
+ inner_dim = dim_head * heads
246
+
247
+ self.to_q = nn.Linear(dim, inner_dim, bias=False)
248
+ self.to_k = nn.Linear(dim, inner_dim, bias=False)
249
+ self.to_v = nn.Linear(dim, inner_dim, bias=False)
250
+ self.dropout = nn.Dropout(dropout)
251
+
252
+ # talking heads
253
+ self.talking_heads = talking_heads
254
+ if talking_heads:
255
+ self.pre_softmax_proj = nn.Parameter(torch.randn(heads, heads))
256
+ self.post_softmax_proj = nn.Parameter(torch.randn(heads, heads))
257
+
258
+ # explicit topk sparse attention
259
+ self.sparse_topk = sparse_topk
260
+
261
+ # entmax
262
+ # self.attn_fn = entmax15 if use_entmax15 else F.softmax
263
+ self.attn_fn = F.softmax
264
+
265
+ # add memory key / values
266
+ self.num_mem_kv = num_mem_kv
267
+ if num_mem_kv > 0:
268
+ self.mem_k = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head))
269
+ self.mem_v = nn.Parameter(torch.randn(heads, num_mem_kv, dim_head))
270
+
271
+ # attention on attention
272
+ self.attn_on_attn = on_attn
273
+ self.to_out = (
274
+ nn.Sequential(nn.Linear(inner_dim, dim * 2), nn.GLU())
275
+ if on_attn
276
+ else nn.Linear(inner_dim, dim)
277
+ )
278
+
279
+ def forward(
280
+ self,
281
+ x,
282
+ context=None,
283
+ mask=None,
284
+ context_mask=None,
285
+ rel_pos=None,
286
+ sinusoidal_emb=None,
287
+ prev_attn=None,
288
+ mem=None,
289
+ ):
290
+ b, n, _, h, talking_heads, device = (
291
+ *x.shape,
292
+ self.heads,
293
+ self.talking_heads,
294
+ x.device,
295
+ )
296
+ kv_input = default(context, x)
297
+
298
+ q_input = x
299
+ k_input = kv_input
300
+ v_input = kv_input
301
+
302
+ if exists(mem):
303
+ k_input = torch.cat((mem, k_input), dim=-2)
304
+ v_input = torch.cat((mem, v_input), dim=-2)
305
+
306
+ if exists(sinusoidal_emb):
307
+ # in shortformer, the query would start at a position offset depending on the past cached memory
308
+ offset = k_input.shape[-2] - q_input.shape[-2]
309
+ q_input = q_input + sinusoidal_emb(q_input, offset=offset)
310
+ k_input = k_input + sinusoidal_emb(k_input)
311
+
312
+ q = self.to_q(q_input)
313
+ k = self.to_k(k_input)
314
+ v = self.to_v(v_input)
315
+
316
+ q, k, v = map(lambda t: rearrange(t, "b n (h d) -> b h n d", h=h), (q, k, v))
317
+
318
+ input_mask = None
319
+ if any(map(exists, (mask, context_mask))):
320
+ q_mask = default(mask, lambda: torch.ones((b, n), device=device).bool())
321
+ k_mask = q_mask if not exists(context) else context_mask
322
+ k_mask = default(
323
+ k_mask, lambda: torch.ones((b, k.shape[-2]), device=device).bool()
324
+ )
325
+ q_mask = rearrange(q_mask, "b i -> b () i ()")
326
+ k_mask = rearrange(k_mask, "b j -> b () () j")
327
+ input_mask = q_mask * k_mask
328
+
329
+ if self.num_mem_kv > 0:
330
+ mem_k, mem_v = map(
331
+ lambda t: repeat(t, "h n d -> b h n d", b=b), (self.mem_k, self.mem_v)
332
+ )
333
+ k = torch.cat((mem_k, k), dim=-2)
334
+ v = torch.cat((mem_v, v), dim=-2)
335
+ if exists(input_mask):
336
+ input_mask = F.pad(input_mask, (self.num_mem_kv, 0), value=True)
337
+
338
+ dots = einsum("b h i d, b h j d -> b h i j", q, k) * self.scale
339
+ mask_value = max_neg_value(dots)
340
+
341
+ if exists(prev_attn):
342
+ dots = dots + prev_attn
343
+
344
+ pre_softmax_attn = dots
345
+
346
+ if talking_heads:
347
+ dots = einsum(
348
+ "b h i j, h k -> b k i j", dots, self.pre_softmax_proj
349
+ ).contiguous()
350
+
351
+ if exists(rel_pos):
352
+ dots = rel_pos(dots)
353
+
354
+ if exists(input_mask):
355
+ dots.masked_fill_(~input_mask, mask_value)
356
+ del input_mask
357
+
358
+ if self.causal:
359
+ i, j = dots.shape[-2:]
360
+ r = torch.arange(i, device=device)
361
+ mask = rearrange(r, "i -> () () i ()") < rearrange(r, "j -> () () () j")
362
+ mask = F.pad(mask, (j - i, 0), value=False)
363
+ dots.masked_fill_(mask, mask_value)
364
+ del mask
365
+
366
+ if exists(self.sparse_topk) and self.sparse_topk < dots.shape[-1]:
367
+ top, _ = dots.topk(self.sparse_topk, dim=-1)
368
+ vk = top[..., -1].unsqueeze(-1).expand_as(dots)
369
+ mask = dots < vk
370
+ dots.masked_fill_(mask, mask_value)
371
+ del mask
372
+
373
+ attn = self.attn_fn(dots, dim=-1)
374
+ post_softmax_attn = attn
375
+
376
+ attn = self.dropout(attn)
377
+
378
+ if talking_heads:
379
+ attn = einsum(
380
+ "b h i j, h k -> b k i j", attn, self.post_softmax_proj
381
+ ).contiguous()
382
+
383
+ out = einsum("b h i j, b h j d -> b h i d", attn, v)
384
+ out = rearrange(out, "b h n d -> b n (h d)")
385
+
386
+ intermediates = Intermediates(
387
+ pre_softmax_attn=pre_softmax_attn, post_softmax_attn=post_softmax_attn
388
+ )
389
+
390
+ return self.to_out(out), intermediates
391
+
392
+
393
+ class AttentionLayers(nn.Module):
394
+ def __init__(
395
+ self,
396
+ dim,
397
+ depth,
398
+ heads=8,
399
+ causal=False,
400
+ cross_attend=False,
401
+ only_cross=False,
402
+ use_scalenorm=False,
403
+ use_rmsnorm=False,
404
+ use_rezero=False,
405
+ rel_pos_num_buckets=32,
406
+ rel_pos_max_distance=128,
407
+ position_infused_attn=False,
408
+ custom_layers=None,
409
+ sandwich_coef=None,
410
+ par_ratio=None,
411
+ residual_attn=False,
412
+ cross_residual_attn=False,
413
+ macaron=False,
414
+ pre_norm=True,
415
+ gate_residual=False,
416
+ **kwargs,
417
+ ):
418
+ super().__init__()
419
+ ff_kwargs, kwargs = groupby_prefix_and_trim("ff_", kwargs)
420
+ attn_kwargs, _ = groupby_prefix_and_trim("attn_", kwargs)
421
+
422
+ dim_head = attn_kwargs.get("dim_head", DEFAULT_DIM_HEAD)
423
+
424
+ self.dim = dim
425
+ self.depth = depth
426
+ self.layers = nn.ModuleList([])
427
+
428
+ self.has_pos_emb = position_infused_attn
429
+ self.pia_pos_emb = (
430
+ FixedPositionalEmbedding(dim) if position_infused_attn else None
431
+ )
432
+ self.rotary_pos_emb = always(None)
433
+
434
+ assert (
435
+ rel_pos_num_buckets <= rel_pos_max_distance
436
+ ), "number of relative position buckets must be less than the relative position max distance"
437
+ self.rel_pos = None
438
+
439
+ self.pre_norm = pre_norm
440
+
441
+ self.residual_attn = residual_attn
442
+ self.cross_residual_attn = cross_residual_attn
443
+
444
+ norm_class = ScaleNorm if use_scalenorm else nn.LayerNorm
445
+ norm_class = RMSNorm if use_rmsnorm else norm_class
446
+ norm_fn = partial(norm_class, dim)
447
+
448
+ norm_fn = nn.Identity if use_rezero else norm_fn
449
+ branch_fn = Rezero if use_rezero else None
450
+
451
+ if cross_attend and not only_cross:
452
+ default_block = ("a", "c", "f")
453
+ elif cross_attend and only_cross:
454
+ default_block = ("c", "f")
455
+ else:
456
+ default_block = ("a", "f")
457
+
458
+ if macaron:
459
+ default_block = ("f",) + default_block
460
+
461
+ if exists(custom_layers):
462
+ layer_types = custom_layers
463
+ elif exists(par_ratio):
464
+ par_depth = depth * len(default_block)
465
+ assert 1 < par_ratio <= par_depth, "par ratio out of range"
466
+ default_block = tuple(filter(not_equals("f"), default_block))
467
+ par_attn = par_depth // par_ratio
468
+ depth_cut = (
469
+ par_depth * 2 // 3
470
+ ) # 2 / 3 attention layer cutoff suggested by PAR paper
471
+ par_width = (depth_cut + depth_cut // par_attn) // par_attn
472
+ assert (
473
+ len(default_block) <= par_width
474
+ ), "default block is too large for par_ratio"
475
+ par_block = default_block + ("f",) * (par_width - len(default_block))
476
+ par_head = par_block * par_attn
477
+ layer_types = par_head + ("f",) * (par_depth - len(par_head))
478
+ elif exists(sandwich_coef):
479
+ assert (
480
+ sandwich_coef > 0 and sandwich_coef <= depth
481
+ ), "sandwich coefficient should be less than the depth"
482
+ layer_types = (
483
+ ("a",) * sandwich_coef
484
+ + default_block * (depth - sandwich_coef)
485
+ + ("f",) * sandwich_coef
486
+ )
487
+ else:
488
+ layer_types = default_block * depth
489
+
490
+ self.layer_types = layer_types
491
+ self.num_attn_layers = len(list(filter(equals("a"), layer_types)))
492
+
493
+ for layer_type in self.layer_types:
494
+ if layer_type == "a":
495
+ layer = Attention(dim, heads=heads, causal=causal, **attn_kwargs)
496
+ elif layer_type == "c":
497
+ layer = Attention(dim, heads=heads, **attn_kwargs)
498
+ elif layer_type == "f":
499
+ layer = FeedForward(dim, **ff_kwargs)
500
+ layer = layer if not macaron else Scale(0.5, layer)
501
+ else:
502
+ raise Exception(f"invalid layer type {layer_type}")
503
+
504
+ if isinstance(layer, Attention) and exists(branch_fn):
505
+ layer = branch_fn(layer)
506
+
507
+ if gate_residual:
508
+ residual_fn = GRUGating(dim)
509
+ else:
510
+ residual_fn = Residual()
511
+
512
+ self.layers.append(nn.ModuleList([norm_fn(), layer, residual_fn]))
513
+
514
+ def forward(
515
+ self,
516
+ x,
517
+ context=None,
518
+ mask=None,
519
+ context_mask=None,
520
+ mems=None,
521
+ return_hiddens=False,
522
+ ):
523
+ hiddens = []
524
+ intermediates = []
525
+ prev_attn = None
526
+ prev_cross_attn = None
527
+
528
+ mems = mems.copy() if exists(mems) else [None] * self.num_attn_layers
529
+
530
+ for ind, (layer_type, (norm, block, residual_fn)) in enumerate(
531
+ zip(self.layer_types, self.layers)
532
+ ):
533
+ is_last = ind == (len(self.layers) - 1)
534
+
535
+ if layer_type == "a":
536
+ hiddens.append(x)
537
+ layer_mem = mems.pop(0)
538
+
539
+ residual = x
540
+
541
+ if self.pre_norm:
542
+ x = norm(x)
543
+
544
+ if layer_type == "a":
545
+ out, inter = block(
546
+ x,
547
+ mask=mask,
548
+ sinusoidal_emb=self.pia_pos_emb,
549
+ rel_pos=self.rel_pos,
550
+ prev_attn=prev_attn,
551
+ mem=layer_mem,
552
+ )
553
+ elif layer_type == "c":
554
+ out, inter = block(
555
+ x,
556
+ context=context,
557
+ mask=mask,
558
+ context_mask=context_mask,
559
+ prev_attn=prev_cross_attn,
560
+ )
561
+ elif layer_type == "f":
562
+ out = block(x)
563
+
564
+ x = residual_fn(out, residual)
565
+
566
+ if layer_type in ("a", "c"):
567
+ intermediates.append(inter)
568
+
569
+ if layer_type == "a" and self.residual_attn:
570
+ prev_attn = inter.pre_softmax_attn
571
+ elif layer_type == "c" and self.cross_residual_attn:
572
+ prev_cross_attn = inter.pre_softmax_attn
573
+
574
+ if not self.pre_norm and not is_last:
575
+ x = norm(x)
576
+
577
+ if return_hiddens:
578
+ intermediates = LayerIntermediates(
579
+ hiddens=hiddens, attn_intermediates=intermediates
580
+ )
581
+
582
+ return x, intermediates
583
+
584
+ return x
585
+
586
+
587
+ class Encoder(AttentionLayers):
588
+ def __init__(self, **kwargs):
589
+ assert "causal" not in kwargs, "cannot set causality on encoder"
590
+ super().__init__(causal=False, **kwargs)
591
+
592
+
593
+ class TransformerWrapper(nn.Module):
594
+ def __init__(
595
+ self,
596
+ *,
597
+ num_tokens,
598
+ max_seq_len,
599
+ attn_layers,
600
+ emb_dim=None,
601
+ max_mem_len=0.0,
602
+ emb_dropout=0.0,
603
+ num_memory_tokens=None,
604
+ tie_embedding=False,
605
+ use_pos_emb=True,
606
+ ):
607
+ super().__init__()
608
+ assert isinstance(
609
+ attn_layers, AttentionLayers
610
+ ), "attention layers must be one of Encoder or Decoder"
611
+
612
+ dim = attn_layers.dim
613
+ emb_dim = default(emb_dim, dim)
614
+
615
+ self.max_seq_len = max_seq_len
616
+ self.max_mem_len = max_mem_len
617
+ self.num_tokens = num_tokens
618
+
619
+ self.token_emb = nn.Embedding(num_tokens, emb_dim)
620
+ self.pos_emb = (
621
+ AbsolutePositionalEmbedding(emb_dim, max_seq_len)
622
+ if (use_pos_emb and not attn_layers.has_pos_emb)
623
+ else always(0)
624
+ )
625
+ self.emb_dropout = nn.Dropout(emb_dropout)
626
+
627
+ self.project_emb = nn.Linear(emb_dim, dim) if emb_dim != dim else nn.Identity()
628
+ self.attn_layers = attn_layers
629
+ self.norm = nn.LayerNorm(dim)
630
+
631
+ self.init_()
632
+
633
+ self.to_logits = (
634
+ nn.Linear(dim, num_tokens)
635
+ if not tie_embedding
636
+ else lambda t: t @ self.token_emb.weight.t()
637
+ )
638
+
639
+ # memory tokens (like [cls]) from Memory Transformers paper
640
+ num_memory_tokens = default(num_memory_tokens, 0)
641
+ self.num_memory_tokens = num_memory_tokens
642
+ if num_memory_tokens > 0:
643
+ self.memory_tokens = nn.Parameter(torch.randn(num_memory_tokens, dim))
644
+
645
+ # let funnel encoder know number of memory tokens, if specified
646
+ if hasattr(attn_layers, "num_memory_tokens"):
647
+ attn_layers.num_memory_tokens = num_memory_tokens
648
+
649
+ def init_(self):
650
+ nn.init.normal_(self.token_emb.weight, std=0.02)
651
+
652
+ def forward(
653
+ self,
654
+ x,
655
+ return_embeddings=False,
656
+ mask=None,
657
+ return_mems=False,
658
+ return_attn=False,
659
+ mems=None,
660
+ **kwargs,
661
+ ):
662
+ b, n, device, num_mem = *x.shape, x.device, self.num_memory_tokens
663
+ x = self.token_emb(x)
664
+ x += self.pos_emb(x)
665
+ x = self.emb_dropout(x)
666
+
667
+ x = self.project_emb(x)
668
+
669
+ if num_mem > 0:
670
+ mem = repeat(self.memory_tokens, "n d -> b n d", b=b)
671
+ x = torch.cat((mem, x), dim=1)
672
+
673
+ # auto-handle masking after appending memory tokens
674
+ if exists(mask):
675
+ mask = F.pad(mask, (num_mem, 0), value=True)
676
+
677
+ x, intermediates = self.attn_layers(
678
+ x, mask=mask, mems=mems, return_hiddens=True, **kwargs
679
+ )
680
+ x = self.norm(x)
681
+
682
+ mem, x = x[:, :num_mem], x[:, num_mem:]
683
+
684
+ out = self.to_logits(x) if not return_embeddings else x
685
+
686
+ if return_mems:
687
+ hiddens = intermediates.hiddens
688
+ new_mems = (
689
+ list(map(lambda pair: torch.cat(pair, dim=-2), zip(mems, hiddens)))
690
+ if exists(mems)
691
+ else hiddens
692
+ )
693
+ new_mems = list(
694
+ map(lambda t: t[..., -self.max_mem_len :, :].detach(), new_mems)
695
+ )
696
+ return out, new_mems
697
+
698
+ if return_attn:
699
+ attn_maps = list(
700
+ map(lambda t: t.post_softmax_attn, intermediates.attn_intermediates)
701
+ )
702
+ return out, attn_maps
703
+
704
+ return out
pipeline/__init__.py ADDED
File without changes
pipeline/__pycache__/__init__.cpython-311.pyc ADDED
Binary file (174 Bytes). View file
 
pipeline/__pycache__/model_scope_vlcm_pipeline.cpython-311.pyc ADDED
Binary file (10 kB). View file
 
pipeline/__pycache__/t2v_turbo_ms_pipeline.cpython-311.pyc ADDED
Binary file (9.86 kB). View file
 
pipeline/__pycache__/t2v_turbo_vc2_pipeline.cpython-311.pyc ADDED
Binary file (9.03 kB). View file
 
pipeline/__pycache__/vlcm_pipeline.cpython-311.pyc ADDED
Binary file (9.09 kB). View file
 
pipeline/t2v_turbo_ms_pipeline.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers import DiffusionPipeline
3
+
4
+ from typing import List, Optional, Tuple, Union, Dict, Any
5
+
6
+ from diffusers import logging
7
+ from diffusers.utils.torch_utils import randn_tensor
8
+ from diffusers.models import AutoencoderKL
9
+ from transformers import CLIPTokenizer, CLIPTextModel
10
+ from scheduler.t2v_turbo_scheduler import T2VTurboScheduler
11
+
12
+ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
13
+
14
+
15
+ class T2VTurboMSPipeline(DiffusionPipeline):
16
+ def __init__(
17
+ self,
18
+ unet,
19
+ vae: AutoencoderKL,
20
+ text_encoder: CLIPTextModel,
21
+ tokenizer: CLIPTokenizer,
22
+ scheduler: T2VTurboScheduler,
23
+ ):
24
+ super().__init__()
25
+
26
+ self.register_modules(
27
+ unet=unet,
28
+ vae=vae,
29
+ text_encoder=text_encoder,
30
+ tokenizer=tokenizer,
31
+ scheduler=scheduler,
32
+ )
33
+
34
+ self.vae_scale_factor = 8
35
+
36
+ def _encode_prompt(
37
+ self,
38
+ prompt,
39
+ device,
40
+ num_videos_per_prompt,
41
+ prompt_embeds: None,
42
+ ):
43
+ r"""
44
+ Encodes the prompt into text encoder hidden states.
45
+ Args:
46
+ prompt (`str` or `List[str]`, *optional*):
47
+ prompt to be encoded
48
+ device: (`torch.device`):
49
+ torch device
50
+ num_videos_per_prompt (`int`):
51
+ number of images that should be generated per prompt
52
+ prompt_embeds (`torch.FloatTensor`, *optional*):
53
+ Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
54
+ provided, text embeddings will be generated from `prompt` input argument.
55
+ """
56
+ if prompt_embeds is None:
57
+ with torch.no_grad():
58
+ text_inputs = self.tokenizer(
59
+ prompt,
60
+ padding="max_length",
61
+ max_length=self.tokenizer.model_max_length,
62
+ truncation=True,
63
+ return_tensors="pt",
64
+ )
65
+ text_input_ids = text_inputs.input_ids.to(self.text_encoder.device)
66
+ prompt_embeds = self.text_encoder(text_input_ids)[0]
67
+
68
+ prompt_embeds = prompt_embeds.to(device=device)
69
+
70
+ bs_embed, seq_len, _ = prompt_embeds.shape
71
+ # duplicate text embeddings for each generation per prompt, using mps friendly method
72
+ prompt_embeds = prompt_embeds.repeat(1, num_videos_per_prompt, 1)
73
+ prompt_embeds = prompt_embeds.view(
74
+ bs_embed * num_videos_per_prompt, seq_len, -1
75
+ )
76
+
77
+ # Don't need to get uncond prompt embedding because of LCM Guided Distillation
78
+ return prompt_embeds
79
+
80
+ def prepare_latents(
81
+ self,
82
+ batch_size,
83
+ num_channels_latents,
84
+ frames,
85
+ height,
86
+ width,
87
+ dtype,
88
+ device,
89
+ generator,
90
+ latents=None,
91
+ ):
92
+ shape = (
93
+ batch_size,
94
+ num_channels_latents,
95
+ frames,
96
+ height // self.vae_scale_factor,
97
+ width // self.vae_scale_factor,
98
+ )
99
+ if latents is None:
100
+ latents = randn_tensor(
101
+ shape, generator=generator, device=device, dtype=dtype
102
+ )
103
+ else:
104
+ latents = latents.to(device)
105
+ # scale the initial noise by the standard deviation required by the scheduler
106
+ latents = latents * self.scheduler.init_noise_sigma
107
+ return latents
108
+
109
+ def get_w_embedding(self, w, embedding_dim=512, dtype=torch.float32):
110
+ """
111
+ see https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298
112
+ Args:
113
+ timesteps: torch.Tensor: generate embedding vectors at these timesteps
114
+ embedding_dim: int: dimension of the embeddings to generate
115
+ dtype: data type of the generated embeddings
116
+ Returns:
117
+ embedding vectors with shape `(len(timesteps), embedding_dim)`
118
+ """
119
+ assert len(w.shape) == 1
120
+ w = w * 1000.0
121
+
122
+ half_dim = embedding_dim // 2
123
+ emb = torch.log(torch.tensor(10000.0)) / (half_dim - 1)
124
+ emb = torch.exp(torch.arange(half_dim, dtype=dtype) * -emb)
125
+ emb = w.to(dtype)[:, None] * emb[None, :]
126
+ emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
127
+ if embedding_dim % 2 == 1: # zero pad
128
+ emb = torch.nn.functional.pad(emb, (0, 1))
129
+ assert emb.shape == (w.shape[0], embedding_dim)
130
+ return emb
131
+
132
+ @torch.no_grad()
133
+ def __call__(
134
+ self,
135
+ prompt: Union[str, List[str]] = None,
136
+ height: Optional[int] = 256,
137
+ width: Optional[int] = 256,
138
+ frames: int = 16,
139
+ guidance_scale: float = 7.5,
140
+ num_videos_per_prompt: Optional[int] = 1,
141
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
142
+ latents: Optional[torch.FloatTensor] = None,
143
+ num_inference_steps: int = 4,
144
+ lcm_origin_steps: int = 50,
145
+ prompt_embeds: Optional[torch.FloatTensor] = None,
146
+ output_type: Optional[str] = "pil",
147
+ ):
148
+ # 2. Define call parameters
149
+ if prompt is not None and isinstance(prompt, str):
150
+ batch_size = 1
151
+ elif prompt is not None and isinstance(prompt, list):
152
+ batch_size = len(prompt)
153
+ else:
154
+ batch_size = prompt_embeds.shape[0]
155
+
156
+ device = self._execution_device
157
+ # do_classifier_free_guidance = guidance_scale > 0.0 # In LCM Implementation: cfg_noise = noise_cond + cfg_scale * (noise_cond - noise_uncond) , (cfg_scale > 0.0 using CFG)
158
+
159
+ # 3. Encode input prompt
160
+ prompt_embeds = self._encode_prompt(
161
+ prompt,
162
+ device,
163
+ num_videos_per_prompt,
164
+ prompt_embeds=prompt_embeds,
165
+ )
166
+
167
+ # 4. Prepare timesteps
168
+ self.scheduler.set_timesteps(num_inference_steps, lcm_origin_steps)
169
+ timesteps = self.scheduler.timesteps
170
+
171
+ # 5. Prepare latent variable
172
+ num_channels_latents = self.unet.config.in_channels
173
+ latents = self.prepare_latents(
174
+ batch_size * num_videos_per_prompt,
175
+ num_channels_latents,
176
+ frames,
177
+ height,
178
+ width,
179
+ prompt_embeds.dtype,
180
+ device,
181
+ generator,
182
+ latents,
183
+ )
184
+
185
+ bs = batch_size * num_videos_per_prompt
186
+
187
+ # 6. Get Guidance Scale Embedding
188
+ w = torch.tensor(guidance_scale).repeat(bs)
189
+ w_embedding = self.get_w_embedding(w, embedding_dim=256).to(device)
190
+
191
+ # 7. LCM MultiStep Sampling Loop:
192
+ with self.progress_bar(total=num_inference_steps) as progress_bar:
193
+ for i, t in enumerate(timesteps):
194
+
195
+ ts = torch.full((bs,), t, device=device, dtype=torch.long)
196
+
197
+ # model prediction (v-prediction, eps, x)
198
+ model_pred = self.unet(
199
+ latents,
200
+ ts,
201
+ timestep_cond=w_embedding,
202
+ encoder_hidden_states=prompt_embeds.float(),
203
+ ).sample
204
+ # compute the previous noisy sample x_t -> x_t-1
205
+ latents, denoised = self.scheduler.step(
206
+ model_pred, i, t, latents, return_dict=False
207
+ )
208
+
209
+ progress_bar.update()
210
+
211
+ if not output_type == "latent":
212
+ t = denoised.shape[2]
213
+ z = denoised.to(self.vae.dtype) / self.vae.config.scaling_factor
214
+ videos = torch.cat(
215
+ [self.vae.decode(z[:, :, i])[0].unsqueeze(2) for i in range(t)],
216
+ dim=2,
217
+ )
218
+ else:
219
+ videos = denoised
220
+
221
+ return videos
pipeline/t2v_turbo_vc2_pipeline.py ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers import DiffusionPipeline
3
+
4
+ from typing import List, Optional, Union, Dict, Any
5
+
6
+ from diffusers import logging
7
+ from diffusers.utils.torch_utils import randn_tensor
8
+ from lvdm.models.ddpm3d import LatentDiffusion
9
+ from scheduler.t2v_turbo_scheduler import T2VTurboScheduler
10
+
11
+ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
12
+
13
+
14
+ class T2VTurboVC2Pipeline(DiffusionPipeline):
15
+ def __init__(
16
+ self,
17
+ pretrained_t2v: LatentDiffusion,
18
+ scheduler: T2VTurboScheduler,
19
+ model_config: Dict[str, Any] = None,
20
+ ):
21
+ super().__init__()
22
+
23
+ self.register_modules(
24
+ pretrained_t2v=pretrained_t2v,
25
+ scheduler=scheduler,
26
+ )
27
+ self.vae = pretrained_t2v.first_stage_model
28
+ self.unet = pretrained_t2v.model.diffusion_model
29
+ self.text_encoder = pretrained_t2v.cond_stage_model
30
+
31
+ self.model_config = model_config
32
+ self.vae_scale_factor = 8
33
+
34
+ def _encode_prompt(
35
+ self,
36
+ prompt,
37
+ device,
38
+ num_videos_per_prompt,
39
+ prompt_embeds: None,
40
+ ):
41
+ r"""
42
+ Encodes the prompt into text encoder hidden states.
43
+ Args:
44
+ prompt (`str` or `List[str]`, *optional*):
45
+ prompt to be encoded
46
+ device: (`torch.device`):
47
+ torch device
48
+ num_videos_per_prompt (`int`):
49
+ number of images that should be generated per prompt
50
+ prompt_embeds (`torch.FloatTensor`, *optional*):
51
+ Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
52
+ provided, text embeddings will be generated from `prompt` input argument.
53
+ """
54
+ if prompt_embeds is None:
55
+
56
+ prompt_embeds = self.text_encoder(prompt)
57
+
58
+ prompt_embeds = prompt_embeds.to(device=device)
59
+
60
+ bs_embed, seq_len, _ = prompt_embeds.shape
61
+ # duplicate text embeddings for each generation per prompt, using mps friendly method
62
+ prompt_embeds = prompt_embeds.repeat(1, num_videos_per_prompt, 1)
63
+ prompt_embeds = prompt_embeds.view(
64
+ bs_embed * num_videos_per_prompt, seq_len, -1
65
+ )
66
+
67
+ # Don't need to get uncond prompt embedding because of LCM Guided Distillation
68
+ return prompt_embeds
69
+
70
+ def prepare_latents(
71
+ self,
72
+ batch_size,
73
+ num_channels_latents,
74
+ frames,
75
+ height,
76
+ width,
77
+ dtype,
78
+ device,
79
+ generator,
80
+ latents=None,
81
+ ):
82
+ shape = (
83
+ batch_size,
84
+ num_channels_latents,
85
+ frames,
86
+ height // self.vae_scale_factor,
87
+ width // self.vae_scale_factor,
88
+ )
89
+ if latents is None:
90
+ latents = randn_tensor(
91
+ shape, generator=generator, device=device, dtype=dtype
92
+ )
93
+ else:
94
+ latents = latents.to(device)
95
+ # scale the initial noise by the standard deviation required by the scheduler
96
+ latents = latents * self.scheduler.init_noise_sigma
97
+ return latents
98
+
99
+ def get_w_embedding(self, w, embedding_dim=512, dtype=torch.float32):
100
+ """
101
+ see https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298
102
+ Args:
103
+ timesteps: torch.Tensor: generate embedding vectors at these timesteps
104
+ embedding_dim: int: dimension of the embeddings to generate
105
+ dtype: data type of the generated embeddings
106
+ Returns:
107
+ embedding vectors with shape `(len(timesteps), embedding_dim)`
108
+ """
109
+ assert len(w.shape) == 1
110
+ w = w * 1000.0
111
+
112
+ half_dim = embedding_dim // 2
113
+ emb = torch.log(torch.tensor(10000.0)) / (half_dim - 1)
114
+ emb = torch.exp(torch.arange(half_dim, dtype=dtype) * -emb)
115
+ emb = w.to(dtype)[:, None] * emb[None, :]
116
+ emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
117
+ if embedding_dim % 2 == 1: # zero pad
118
+ emb = torch.nn.functional.pad(emb, (0, 1))
119
+ assert emb.shape == (w.shape[0], embedding_dim)
120
+ return emb
121
+
122
+ @torch.no_grad()
123
+ def __call__(
124
+ self,
125
+ prompt: Union[str, List[str]] = None,
126
+ height: Optional[int] = 320,
127
+ width: Optional[int] = 512,
128
+ frames: int = 16,
129
+ fps: int = 16,
130
+ guidance_scale: float = 7.5,
131
+ num_videos_per_prompt: Optional[int] = 1,
132
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
133
+ latents: Optional[torch.FloatTensor] = None,
134
+ num_inference_steps: int = 4,
135
+ lcm_origin_steps: int = 50,
136
+ prompt_embeds: Optional[torch.FloatTensor] = None,
137
+ output_type: Optional[str] = "pil",
138
+ ):
139
+ unet_config = self.model_config["params"]["unet_config"]
140
+ # 0. Default height and width to unet
141
+ frames = self.pretrained_t2v.temporal_length if frames < 0 else frames
142
+
143
+ # 2. Define call parameters
144
+ if prompt is not None and isinstance(prompt, str):
145
+ batch_size = 1
146
+ elif prompt is not None and isinstance(prompt, list):
147
+ batch_size = len(prompt)
148
+ else:
149
+ batch_size = prompt_embeds.shape[0]
150
+
151
+ device = self._execution_device
152
+ # do_classifier_free_guidance = guidance_scale > 0.0 # In LCM Implementation: cfg_noise = noise_cond + cfg_scale * (noise_cond - noise_uncond) , (cfg_scale > 0.0 using CFG)
153
+
154
+ # 3. Encode input prompt
155
+ prompt_embeds = self._encode_prompt(
156
+ prompt,
157
+ device,
158
+ num_videos_per_prompt,
159
+ prompt_embeds=prompt_embeds,
160
+ )
161
+
162
+ # 4. Prepare timesteps
163
+ self.scheduler.set_timesteps(num_inference_steps, lcm_origin_steps)
164
+ timesteps = self.scheduler.timesteps
165
+
166
+ # 5. Prepare latent variable
167
+ num_channels_latents = unet_config["params"]["in_channels"]
168
+ latents = self.prepare_latents(
169
+ batch_size * num_videos_per_prompt,
170
+ num_channels_latents,
171
+ frames,
172
+ height,
173
+ width,
174
+ prompt_embeds.dtype,
175
+ device,
176
+ generator,
177
+ latents,
178
+ )
179
+
180
+ bs = batch_size * num_videos_per_prompt
181
+
182
+ # 6. Get Guidance Scale Embedding
183
+ w = torch.tensor(guidance_scale).repeat(bs)
184
+ w_embedding = self.get_w_embedding(w, embedding_dim=256).to(device)
185
+
186
+ # 7. LCM MultiStep Sampling Loop:
187
+ with self.progress_bar(total=num_inference_steps) as progress_bar:
188
+ for i, t in enumerate(timesteps):
189
+
190
+ ts = torch.full((bs,), t, device=device, dtype=torch.long)
191
+
192
+ # model prediction (v-prediction, eps, x)
193
+ context = {"context": torch.cat([prompt_embeds.float()], 1), "fps": fps}
194
+ model_pred = self.unet(
195
+ latents,
196
+ ts,
197
+ **context,
198
+ timestep_cond=w_embedding.to(self.dtype),
199
+ )
200
+ # compute the previous noisy sample x_t -> x_t-1
201
+ latents, denoised = self.scheduler.step(
202
+ model_pred, i, t, latents, return_dict=False
203
+ )
204
+
205
+ # # call the callback, if provided
206
+ # if i == len(timesteps) - 1:
207
+ progress_bar.update()
208
+
209
+ if not output_type == "latent":
210
+ videos = self.pretrained_t2v.decode_first_stage_2DAE(denoised)
211
+ else:
212
+ videos = denoised
213
+
214
+ return videos
requirements.txt CHANGED
@@ -1,6 +1,18 @@
1
- accelerate
2
- diffusers
3
- invisible_watermark
4
- torch
5
- transformers
6
- xformers
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ torch==2.2.2
2
+ torchvision==0.17.2
3
+ diffusers==0.27.2
4
+ transformers==4.40.0
5
+ accelerate==0.29.3
6
+ imageio==2.34.0
7
+ decord==0.6.0
8
+ spaces
9
+ einops
10
+ omegaconf
11
+ safetensors
12
+ moviepy
13
+ scikit-learn
14
+ av
15
+ rotary_embedding_torch
16
+ torchmetrics
17
+ torch-fidelity
18
+ wandb
scheduler/__pycache__/t2v_turbo_scheduler.cpython-311.pyc ADDED
Binary file (24.5 kB). View file
 
scheduler/__pycache__/vlcm_scheduler.cpython-311.pyc ADDED
Binary file (24.5 kB). View file
 
scheduler/t2v_turbo_scheduler.py ADDED
@@ -0,0 +1,518 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2023 Stanford University Team and The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # DISCLAIMER: This code is strongly influenced by https://github.com/pesser/pytorch_diffusion
16
+ # and https://github.com/hojonathanho/diffusion
17
+
18
+ import math
19
+ from dataclasses import dataclass
20
+ from typing import List, Optional, Tuple, Union
21
+
22
+ import numpy as np
23
+ import torch
24
+
25
+ from diffusers import ConfigMixin, SchedulerMixin
26
+ from diffusers.configuration_utils import register_to_config
27
+ from diffusers.utils import BaseOutput
28
+
29
+
30
+ def extract_into_tensor(a, t, x_shape):
31
+ b, *_ = t.shape
32
+ out = a.gather(-1, t)
33
+ return out.reshape(b, *((1,) * (len(x_shape) - 1)))
34
+
35
+
36
+ @dataclass
37
+ # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->DDIM
38
+ class T2VTurboSchedulerOutput(BaseOutput):
39
+ """
40
+ Output class for the scheduler's `step` function output.
41
+ Args:
42
+ prev_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
43
+ Computed sample `(x_{t-1})` of previous timestep. `prev_sample` should be used as next model input in the
44
+ denoising loop.
45
+ pred_original_sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` for images):
46
+ The predicted denoised sample `(x_{0})` based on the model output from the current timestep.
47
+ `pred_original_sample` can be used to preview progress or for guidance.
48
+ """
49
+
50
+ prev_sample: torch.FloatTensor
51
+ denoised: Optional[torch.FloatTensor] = None
52
+
53
+
54
+ # Copied from diffusers.schedulers.scheduling_ddpm.betas_for_alpha_bar
55
+ def betas_for_alpha_bar(
56
+ num_diffusion_timesteps,
57
+ max_beta=0.999,
58
+ alpha_transform_type="cosine",
59
+ ):
60
+ """
61
+ Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of
62
+ (1-beta) over time from t = [0,1].
63
+ Contains a function alpha_bar that takes an argument t and transforms it to the cumulative product of (1-beta) up
64
+ to that part of the diffusion process.
65
+ Args:
66
+ num_diffusion_timesteps (`int`): the number of betas to produce.
67
+ max_beta (`float`): the maximum beta to use; use values lower than 1 to
68
+ prevent singularities.
69
+ alpha_transform_type (`str`, *optional*, default to `cosine`): the type of noise schedule for alpha_bar.
70
+ Choose from `cosine` or `exp`
71
+ Returns:
72
+ betas (`np.ndarray`): the betas used by the scheduler to step the model outputs
73
+ """
74
+ if alpha_transform_type == "cosine":
75
+
76
+ def alpha_bar_fn(t):
77
+ return math.cos((t + 0.008) / 1.008 * math.pi / 2) ** 2
78
+
79
+ elif alpha_transform_type == "exp":
80
+
81
+ def alpha_bar_fn(t):
82
+ return math.exp(t * -12.0)
83
+
84
+ else:
85
+ raise ValueError(f"Unsupported alpha_tranform_type: {alpha_transform_type}")
86
+
87
+ betas = []
88
+ for i in range(num_diffusion_timesteps):
89
+ t1 = i / num_diffusion_timesteps
90
+ t2 = (i + 1) / num_diffusion_timesteps
91
+ betas.append(min(1 - alpha_bar_fn(t2) / alpha_bar_fn(t1), max_beta))
92
+ return torch.tensor(betas, dtype=torch.float32)
93
+
94
+
95
+ def rescale_zero_terminal_snr(betas):
96
+ """
97
+ Rescales betas to have zero terminal SNR Based on https://arxiv.org/pdf/2305.08891.pdf (Algorithm 1)
98
+ Args:
99
+ betas (`torch.FloatTensor`):
100
+ the betas that the scheduler is being initialized with.
101
+ Returns:
102
+ `torch.FloatTensor`: rescaled betas with zero terminal SNR
103
+ """
104
+ # Convert betas to alphas_bar_sqrt
105
+ alphas = 1.0 - betas
106
+ alphas_cumprod = torch.cumprod(alphas, dim=0)
107
+ alphas_bar_sqrt = alphas_cumprod.sqrt()
108
+
109
+ # Store old values.
110
+ alphas_bar_sqrt_0 = alphas_bar_sqrt[0].clone()
111
+ alphas_bar_sqrt_T = alphas_bar_sqrt[-1].clone()
112
+
113
+ # Shift so the last timestep is zero.
114
+ alphas_bar_sqrt -= alphas_bar_sqrt_T
115
+
116
+ # Scale so the first timestep is back to the old value.
117
+ alphas_bar_sqrt *= alphas_bar_sqrt_0 / (alphas_bar_sqrt_0 - alphas_bar_sqrt_T)
118
+
119
+ # Convert alphas_bar_sqrt to betas
120
+ alphas_bar = alphas_bar_sqrt**2 # Revert sqrt
121
+ alphas = alphas_bar[1:] / alphas_bar[:-1] # Revert cumprod
122
+ alphas = torch.cat([alphas_bar[0:1], alphas])
123
+ betas = 1 - alphas
124
+
125
+ return betas
126
+
127
+
128
+ class T2VTurboScheduler(SchedulerMixin, ConfigMixin):
129
+ """
130
+ `T2VTurboScheduler` extends the denoising procedure introduced in denoising diffusion probabilistic models (DDPMs) with
131
+ non-Markovian guidance.
132
+ This model inherits from [`SchedulerMixin`] and [`ConfigMixin`]. Check the superclass documentation for the generic
133
+ methods the library implements for all schedulers such as loading and saving.
134
+ Args:
135
+ num_train_timesteps (`int`, defaults to 1000):
136
+ The number of diffusion steps to train the model.
137
+ beta_start (`float`, defaults to 0.0001):
138
+ The starting `beta` value of inference.
139
+ beta_end (`float`, defaults to 0.02):
140
+ The final `beta` value.
141
+ beta_schedule (`str`, defaults to `"linear"`):
142
+ The beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose from
143
+ `linear`, `scaled_linear`, or `squaredcos_cap_v2`.
144
+ trained_betas (`np.ndarray`, *optional*):
145
+ Pass an array of betas directly to the constructor to bypass `beta_start` and `beta_end`.
146
+ clip_sample (`bool`, defaults to `True`):
147
+ Clip the predicted sample for numerical stability.
148
+ clip_sample_range (`float`, defaults to 1.0):
149
+ The maximum magnitude for sample clipping. Valid only when `clip_sample=True`.
150
+ set_alpha_to_one (`bool`, defaults to `True`):
151
+ Each diffusion step uses the alphas product value at that step and at the previous one. For the final step
152
+ there is no previous alpha. When this option is `True` the previous alpha product is fixed to `1`,
153
+ otherwise it uses the alpha value at step 0.
154
+ steps_offset (`int`, defaults to 0):
155
+ An offset added to the inference steps. You can use a combination of `offset=1` and
156
+ `set_alpha_to_one=False` to make the last step use step 0 for the previous alpha product like in Stable
157
+ Diffusion.
158
+ prediction_type (`str`, defaults to `epsilon`, *optional*):
159
+ Prediction type of the scheduler function; can be `epsilon` (predicts the noise of the diffusion process),
160
+ `sample` (directly predicts the noisy sample`) or `v_prediction` (see section 2.4 of [Imagen
161
+ Video](https://imagen.research.google/video/paper.pdf) paper).
162
+ thresholding (`bool`, defaults to `False`):
163
+ Whether to use the "dynamic thresholding" method. This is unsuitable for latent-space diffusion models such
164
+ as Stable Diffusion.
165
+ dynamic_thresholding_ratio (`float`, defaults to 0.995):
166
+ The ratio for the dynamic thresholding method. Valid only when `thresholding=True`.
167
+ sample_max_value (`float`, defaults to 1.0):
168
+ The threshold value for dynamic thresholding. Valid only when `thresholding=True`.
169
+ timestep_spacing (`str`, defaults to `"leading"`):
170
+ The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
171
+ Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
172
+ rescale_betas_zero_snr (`bool`, defaults to `False`):
173
+ Whether to rescale the betas to have zero terminal SNR. This enables the model to generate very bright and
174
+ dark samples instead of limiting it to samples with medium brightness. Loosely related to
175
+ [`--offset_noise`](https://github.com/huggingface/diffusers/blob/74fd735eb073eb1d774b1ab4154a0876eb82f055/examples/dreambooth/train_dreambooth.py#L506).
176
+ """
177
+
178
+ # _compatibles = [e.name for e in KarrasDiffusionSchedulers]
179
+ order = 1
180
+
181
+ @register_to_config
182
+ def __init__(
183
+ self,
184
+ num_train_timesteps: int = 1000,
185
+ linear_start: float = 0.00085,
186
+ linear_end: float = 0.012,
187
+ beta_schedule: str = "scaled_linear",
188
+ trained_betas: Optional[Union[np.ndarray, List[float]]] = None,
189
+ clip_sample: bool = True,
190
+ set_alpha_to_one: bool = True,
191
+ steps_offset: int = 0,
192
+ prediction_type: str = "epsilon",
193
+ thresholding: bool = False,
194
+ dynamic_thresholding_ratio: float = 0.995,
195
+ clip_sample_range: float = 1.0,
196
+ sample_max_value: float = 1.0,
197
+ timestep_spacing: str = "leading",
198
+ rescale_betas_zero_snr: bool = False,
199
+ ):
200
+ assert beta_schedule == "scaled_linear"
201
+ assert trained_betas is None
202
+ if trained_betas is not None:
203
+ self.betas = torch.tensor(trained_betas, dtype=torch.float32)
204
+ elif beta_schedule == "linear":
205
+ self.betas = torch.linspace(
206
+ linear_start, linear_end, num_train_timesteps, dtype=torch.float32
207
+ )
208
+ elif beta_schedule == "scaled_linear":
209
+ # this schedule is very specific to the latent diffusion model.
210
+ self.betas = (
211
+ torch.linspace(
212
+ linear_start**0.5,
213
+ linear_end**0.5,
214
+ num_train_timesteps,
215
+ dtype=torch.float32,
216
+ )
217
+ ** 2
218
+ )
219
+ elif beta_schedule == "squaredcos_cap_v2":
220
+ # Glide cosine schedule
221
+ self.betas = betas_for_alpha_bar(num_train_timesteps)
222
+ else:
223
+ raise NotImplementedError(
224
+ f"{beta_schedule} does is not implemented for {self.__class__}"
225
+ )
226
+
227
+ # Rescale for zero SNR
228
+ if rescale_betas_zero_snr:
229
+ self.betas = rescale_zero_terminal_snr(self.betas)
230
+
231
+ self.alphas = 1.0 - self.betas
232
+ self.alphas_cumprod = torch.cumprod(self.alphas, dim=0)
233
+
234
+ # At every step in ddim, we are looking into the previous alphas_cumprod
235
+ # For the final step, there is no previous alphas_cumprod because we are already at 0
236
+ # `set_alpha_to_one` decides whether we set this parameter simply to one or
237
+ # whether we use the final alpha of the "non-previous" one.
238
+ self.final_alpha_cumprod = (
239
+ torch.tensor(1.0) if set_alpha_to_one else self.alphas_cumprod[0]
240
+ )
241
+
242
+ # standard deviation of the initial noise distribution
243
+ self.init_noise_sigma = 1.0
244
+
245
+ # setable values
246
+ self.num_inference_steps = None
247
+ self.timesteps = torch.from_numpy(
248
+ np.arange(0, num_train_timesteps)[::-1].copy().astype(np.int64)
249
+ )
250
+
251
+ def scale_model_input(
252
+ self, sample: torch.FloatTensor, timestep: Optional[int] = None
253
+ ) -> torch.FloatTensor:
254
+ """
255
+ Ensures interchangeability with schedulers that need to scale the denoising model input depending on the
256
+ current timestep.
257
+ Args:
258
+ sample (`torch.FloatTensor`):
259
+ The input sample.
260
+ timestep (`int`, *optional*):
261
+ The current timestep in the diffusion chain.
262
+ Returns:
263
+ `torch.FloatTensor`:
264
+ A scaled input sample.
265
+ """
266
+ return sample
267
+
268
+ def _get_variance(self, timestep, prev_timestep):
269
+ alpha_prod_t = self.alphas_cumprod[timestep]
270
+ alpha_prod_t_prev = (
271
+ self.alphas_cumprod[prev_timestep]
272
+ if prev_timestep >= 0
273
+ else self.final_alpha_cumprod
274
+ )
275
+ beta_prod_t = 1 - alpha_prod_t
276
+ beta_prod_t_prev = 1 - alpha_prod_t_prev
277
+
278
+ variance = (beta_prod_t_prev / beta_prod_t) * (
279
+ 1 - alpha_prod_t / alpha_prod_t_prev
280
+ )
281
+
282
+ return variance
283
+
284
+ # Copied from diffusers.schedulers.scheduling_ddpm.DDPMScheduler._threshold_sample
285
+ def _threshold_sample(self, sample: torch.FloatTensor) -> torch.FloatTensor:
286
+ """
287
+ "Dynamic thresholding: At each sampling step we set s to a certain percentile absolute pixel value in xt0 (the
288
+ prediction of x_0 at timestep t), and if s > 1, then we threshold xt0 to the range [-s, s] and then divide by
289
+ s. Dynamic thresholding pushes saturated pixels (those near -1 and 1) inwards, thereby actively preventing
290
+ pixels from saturation at each step. We find that dynamic thresholding results in significantly better
291
+ photorealism as well as better image-text alignment, especially when using very large guidance weights."
292
+ https://arxiv.org/abs/2205.11487
293
+ """
294
+ dtype = sample.dtype
295
+ batch_size, channels, height, width = sample.shape
296
+
297
+ if dtype not in (torch.float32, torch.float64):
298
+ sample = (
299
+ sample.float()
300
+ ) # upcast for quantile calculation, and clamp not implemented for cpu half
301
+
302
+ # Flatten sample for doing quantile calculation along each image
303
+ sample = sample.reshape(batch_size, channels * height * width)
304
+
305
+ abs_sample = sample.abs() # "a certain percentile absolute pixel value"
306
+
307
+ s = torch.quantile(abs_sample, self.config.dynamic_thresholding_ratio, dim=1)
308
+ s = torch.clamp(
309
+ s, min=1, max=self.config.sample_max_value
310
+ ) # When clamped to min=1, equivalent to standard clipping to [-1, 1]
311
+
312
+ s = s.unsqueeze(1) # (batch_size, 1) because clamp will broadcast along dim=0
313
+ sample = (
314
+ torch.clamp(sample, -s, s) / s
315
+ ) # "we threshold xt0 to the range [-s, s] and then divide by s"
316
+
317
+ sample = sample.reshape(batch_size, channels, height, width)
318
+ sample = sample.to(dtype)
319
+
320
+ return sample
321
+
322
+ def set_timesteps(
323
+ self,
324
+ num_inference_steps: int,
325
+ lcm_origin_steps: int,
326
+ device: Union[str, torch.device] = None,
327
+ ):
328
+ """
329
+ Sets the discrete timesteps used for the diffusion chain (to be run before inference).
330
+ Args:
331
+ num_inference_steps (`int`):
332
+ The number of diffusion steps used when generating samples with a pre-trained model.
333
+ """
334
+
335
+ if num_inference_steps > self.config.num_train_timesteps:
336
+ raise ValueError(
337
+ f"`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:"
338
+ f" {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle"
339
+ f" maximal {self.config.num_train_timesteps} timesteps."
340
+ )
341
+
342
+ self.num_inference_steps = num_inference_steps
343
+
344
+ # LCM Timesteps Setting: # Linear Spacing
345
+ c = self.config.num_train_timesteps // lcm_origin_steps
346
+ lcm_origin_timesteps = (
347
+ np.asarray(list(range(1, lcm_origin_steps + 1))) * c - 1
348
+ ) # LCM Training Steps Schedule
349
+ skipping_step = len(lcm_origin_timesteps) // num_inference_steps
350
+ timesteps = lcm_origin_timesteps[::-skipping_step][
351
+ :num_inference_steps
352
+ ] # LCM Inference Steps Schedule
353
+
354
+ self.timesteps = torch.from_numpy(timesteps.copy()).to(device)
355
+
356
+ ## From VideoCrafter 2
357
+
358
+ def get_scalings_for_boundary_condition_discrete(self, t):
359
+ self.sigma_data = 0.5 # Default: 0.5
360
+
361
+ # By dividing 0.1: This is almost a delta function at t=0.
362
+ c_skip = self.sigma_data**2 / ((t / 0.1) ** 2 + self.sigma_data**2)
363
+ c_out = (t / 0.1) / ((t / 0.1) ** 2 + self.sigma_data**2) ** 0.5
364
+ return c_skip, c_out
365
+
366
+ def step(
367
+ self,
368
+ model_output: torch.FloatTensor,
369
+ timeindex: int,
370
+ timestep: int,
371
+ sample: torch.FloatTensor,
372
+ eta: float = 0.0,
373
+ use_clipped_model_output: bool = False,
374
+ generator=None,
375
+ variance_noise: Optional[torch.FloatTensor] = None,
376
+ return_dict: bool = True,
377
+ ) -> Union[T2VTurboSchedulerOutput, Tuple]:
378
+ """
379
+ Predict the sample from the previous timestep by reversing the SDE. This function propagates the diffusion
380
+ process from the learned model outputs (most often the predicted noise).
381
+ Args:
382
+ model_output (`torch.FloatTensor`):
383
+ The direct output from learned diffusion model.
384
+ timestep (`float`):
385
+ The current discrete timestep in the diffusion chain.
386
+ sample (`torch.FloatTensor`):
387
+ A current instance of a sample created by the diffusion process.
388
+ eta (`float`):
389
+ The weight of noise for added noise in diffusion step.
390
+ use_clipped_model_output (`bool`, defaults to `False`):
391
+ If `True`, computes "corrected" `model_output` from the clipped predicted original sample. Necessary
392
+ because predicted original sample is clipped to [-1, 1] when `self.config.clip_sample` is `True`. If no
393
+ clipping has happened, "corrected" `model_output` would coincide with the one provided as input and
394
+ `use_clipped_model_output` has no effect.
395
+ generator (`torch.Generator`, *optional*):
396
+ A random number generator.
397
+ variance_noise (`torch.FloatTensor`):
398
+ Alternative to generating noise with `generator` by directly providing the noise for the variance
399
+ itself. Useful for methods such as [`CycleDiffusion`].
400
+ return_dict (`bool`, *optional*, defaults to `True`):
401
+ Whether or not to return a [`~schedulers.scheduling_lcm.LCMSchedulerOutput`] or `tuple`.
402
+ Returns:
403
+ [`~schedulers.scheduling_utils.LCMSchedulerOutput`] or `tuple`:
404
+ If return_dict is `True`, [`~schedulers.scheduling_lcm.LCMSchedulerOutput`] is returned, otherwise a
405
+ tuple is returned where the first element is the sample tensor.
406
+ """
407
+ if self.num_inference_steps is None:
408
+ raise ValueError(
409
+ "Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler"
410
+ )
411
+
412
+ # 1. get previous step value
413
+ prev_timeindex = timeindex + 1
414
+ if prev_timeindex < len(self.timesteps):
415
+ prev_timestep = self.timesteps[prev_timeindex]
416
+ else:
417
+ prev_timestep = timestep
418
+
419
+ # 2. compute alphas, betas
420
+ alpha_prod_t = self.alphas_cumprod[timestep]
421
+ alpha_prod_t_prev = (
422
+ self.alphas_cumprod[prev_timestep]
423
+ if prev_timestep >= 0
424
+ else self.final_alpha_cumprod
425
+ )
426
+
427
+ beta_prod_t = 1 - alpha_prod_t
428
+ beta_prod_t_prev = 1 - alpha_prod_t_prev
429
+
430
+ # 3. Get scalings for boundary conditions
431
+ c_skip, c_out = self.get_scalings_for_boundary_condition_discrete(timestep)
432
+
433
+ # 4. Different Parameterization:
434
+ parameterization = self.config.prediction_type
435
+
436
+ if parameterization == "epsilon": # noise-prediction
437
+ pred_x0 = (sample - beta_prod_t.sqrt() * model_output) / alpha_prod_t.sqrt()
438
+
439
+ elif parameterization == "sample": # x-prediction
440
+ pred_x0 = model_output
441
+
442
+ elif parameterization == "v_prediction": # v-prediction
443
+ pred_x0 = alpha_prod_t.sqrt() * sample - beta_prod_t.sqrt() * model_output
444
+
445
+ # 4. Denoise model output using boundary conditions
446
+ denoised = c_out * pred_x0 + c_skip * sample
447
+
448
+ # 5. Sample z ~ N(0, I), For MultiStep Inference
449
+ # Noise is not used for one-step sampling.
450
+ if len(self.timesteps) > 1:
451
+ noise = torch.randn(model_output.shape).to(model_output.device)
452
+ prev_sample = (
453
+ alpha_prod_t_prev.sqrt() * denoised + beta_prod_t_prev.sqrt() * noise
454
+ )
455
+ else:
456
+ prev_sample = denoised
457
+
458
+ if not return_dict:
459
+ return (prev_sample, denoised)
460
+
461
+ return T2VTurboSchedulerOutput(prev_sample=prev_sample, denoised=denoised)
462
+
463
+ # Copied from diffusers.schedulers.scheduling_ddpm.DDPMScheduler.add_noise
464
+ def add_noise(
465
+ self,
466
+ original_samples: torch.FloatTensor,
467
+ noise: torch.FloatTensor,
468
+ timesteps: torch.IntTensor,
469
+ ) -> torch.FloatTensor:
470
+ # Make sure alphas_cumprod and timestep have same device and dtype as original_samples
471
+ alphas_cumprod = self.alphas_cumprod.to(
472
+ device=original_samples.device, dtype=original_samples.dtype
473
+ )
474
+ timesteps = timesteps.to(original_samples.device)
475
+
476
+ sqrt_alpha_prod = alphas_cumprod[timesteps] ** 0.5
477
+ sqrt_alpha_prod = sqrt_alpha_prod.flatten()
478
+ while len(sqrt_alpha_prod.shape) < len(original_samples.shape):
479
+ sqrt_alpha_prod = sqrt_alpha_prod.unsqueeze(-1)
480
+
481
+ sqrt_one_minus_alpha_prod = (1 - alphas_cumprod[timesteps]) ** 0.5
482
+ sqrt_one_minus_alpha_prod = sqrt_one_minus_alpha_prod.flatten()
483
+ while len(sqrt_one_minus_alpha_prod.shape) < len(original_samples.shape):
484
+ sqrt_one_minus_alpha_prod = sqrt_one_minus_alpha_prod.unsqueeze(-1)
485
+
486
+ noisy_samples = (
487
+ sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise
488
+ )
489
+ return noisy_samples
490
+
491
+ # Copied from diffusers.schedulers.scheduling_ddpm.DDPMScheduler.get_velocity
492
+ def get_velocity(
493
+ self,
494
+ sample: torch.FloatTensor,
495
+ noise: torch.FloatTensor,
496
+ timesteps: torch.IntTensor,
497
+ ) -> torch.FloatTensor:
498
+ # Make sure alphas_cumprod and timestep have same device and dtype as sample
499
+ alphas_cumprod = self.alphas_cumprod.to(
500
+ device=sample.device, dtype=sample.dtype
501
+ )
502
+ timesteps = timesteps.to(sample.device)
503
+
504
+ sqrt_alpha_prod = alphas_cumprod[timesteps] ** 0.5
505
+ sqrt_alpha_prod = sqrt_alpha_prod.flatten()
506
+ while len(sqrt_alpha_prod.shape) < len(sample.shape):
507
+ sqrt_alpha_prod = sqrt_alpha_prod.unsqueeze(-1)
508
+
509
+ sqrt_one_minus_alpha_prod = (1 - alphas_cumprod[timesteps]) ** 0.5
510
+ sqrt_one_minus_alpha_prod = sqrt_one_minus_alpha_prod.flatten()
511
+ while len(sqrt_one_minus_alpha_prod.shape) < len(sample.shape):
512
+ sqrt_one_minus_alpha_prod = sqrt_one_minus_alpha_prod.unsqueeze(-1)
513
+
514
+ velocity = sqrt_alpha_prod * noise - sqrt_one_minus_alpha_prod * sample
515
+ return velocity
516
+
517
+ def __len__(self):
518
+ return self.config.num_train_timesteps
style.css ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+
5
+ #duplicate-button {
6
+ margin: auto;
7
+ color: #fff;
8
+ background: #1565c0;
9
+ border-radius: 100vh;
10
+ }
11
+
12
+ #component-0 {
13
+ max-width: 830px;
14
+ margin: auto;
15
+ padding-top: 1.5rem;
16
+ }
utils/__init__.py ADDED
File without changes
utils/__pycache__/__init__.cpython-311.pyc ADDED
Binary file (171 Bytes). View file
 
utils/__pycache__/common_utils.cpython-311.pyc ADDED
Binary file (16.3 kB). View file
 
utils/__pycache__/lora.cpython-311.pyc ADDED
Binary file (55.1 kB). View file
 
utils/__pycache__/lora_handler.cpython-311.pyc ADDED
Binary file (6.12 kB). View file
 
utils/__pycache__/utils.cpython-311.pyc ADDED
Binary file (6.82 kB). View file
 
utils/common_utils.py ADDED
@@ -0,0 +1,385 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ast
2
+ import gc
3
+ import torch
4
+
5
+ from collections import OrderedDict
6
+
7
+ from diffusers.models.attention_processor import AttnProcessor2_0
8
+ from diffusers.models.attention import BasicTransformerBlock
9
+ import wandb
10
+
11
+
12
+ def extract_into_tensor(a, t, x_shape):
13
+ b, *_ = t.shape
14
+ out = a.gather(-1, t)
15
+ return out.reshape(b, *((1,) * (len(x_shape) - 1)))
16
+
17
+
18
+ def is_attn(name):
19
+ return "attn1" or "attn2" == name.split(".")[-1]
20
+
21
+
22
+ def set_processors(attentions):
23
+ for attn in attentions:
24
+ attn.set_processor(AttnProcessor2_0())
25
+
26
+
27
+ def set_torch_2_attn(unet):
28
+ optim_count = 0
29
+
30
+ for name, module in unet.named_modules():
31
+ if is_attn(name):
32
+ if isinstance(module, torch.nn.ModuleList):
33
+ for m in module:
34
+ if isinstance(m, BasicTransformerBlock):
35
+ set_processors([m.attn1, m.attn2])
36
+ optim_count += 1
37
+ if optim_count > 0:
38
+ print(f"{optim_count} Attention layers using Scaled Dot Product Attention.")
39
+
40
+
41
+ # From LatentConsistencyModel.get_guidance_scale_embedding
42
+ def guidance_scale_embedding(w, embedding_dim=512, dtype=torch.float32):
43
+ """
44
+ See https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298
45
+
46
+ Args:
47
+ timesteps (`torch.Tensor`):
48
+ generate embedding vectors at these timesteps
49
+ embedding_dim (`int`, *optional*, defaults to 512):
50
+ dimension of the embeddings to generate
51
+ dtype:
52
+ data type of the generated embeddings
53
+
54
+ Returns:
55
+ `torch.FloatTensor`: Embedding vectors with shape `(len(timesteps), embedding_dim)`
56
+ """
57
+ assert len(w.shape) == 1
58
+ w = w * 1000.0
59
+
60
+ half_dim = embedding_dim // 2
61
+ emb = torch.log(torch.tensor(10000.0)) / (half_dim - 1)
62
+ emb = torch.exp(torch.arange(half_dim, dtype=dtype) * -emb)
63
+ emb = w.to(dtype)[:, None] * emb[None, :]
64
+ emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
65
+ if embedding_dim % 2 == 1: # zero pad
66
+ emb = torch.nn.functional.pad(emb, (0, 1))
67
+ assert emb.shape == (w.shape[0], embedding_dim)
68
+ return emb
69
+
70
+
71
+ def append_dims(x, target_dims):
72
+ """Appends dimensions to the end of a tensor until it has target_dims dimensions."""
73
+ dims_to_append = target_dims - x.ndim
74
+ if dims_to_append < 0:
75
+ raise ValueError(
76
+ f"input has {x.ndim} dims but target_dims is {target_dims}, which is less"
77
+ )
78
+ return x[(...,) + (None,) * dims_to_append]
79
+
80
+
81
+ # From LCMScheduler.get_scalings_for_boundary_condition_discrete
82
+ def scalings_for_boundary_conditions(timestep, sigma_data=0.5, timestep_scaling=10.0):
83
+ scaled_timestep = timestep_scaling * timestep
84
+ c_skip = sigma_data**2 / (scaled_timestep**2 + sigma_data**2)
85
+ c_out = scaled_timestep / (scaled_timestep**2 + sigma_data**2) ** 0.5
86
+ return c_skip, c_out
87
+
88
+
89
+ # Compare LCMScheduler.step, Step 4
90
+ def get_predicted_original_sample(
91
+ model_output, timesteps, sample, prediction_type, alphas, sigmas
92
+ ):
93
+ alphas = extract_into_tensor(alphas, timesteps, sample.shape)
94
+ sigmas = extract_into_tensor(sigmas, timesteps, sample.shape)
95
+ if prediction_type == "epsilon":
96
+ pred_x_0 = (sample - sigmas * model_output) / alphas
97
+ elif prediction_type == "sample":
98
+ pred_x_0 = model_output
99
+ elif prediction_type == "v_prediction":
100
+ pred_x_0 = alphas * sample - sigmas * model_output
101
+ else:
102
+ raise ValueError(
103
+ f"Prediction type {prediction_type} is not supported; currently, `epsilon`, `sample`, and `v_prediction`"
104
+ f" are supported."
105
+ )
106
+
107
+ return pred_x_0
108
+
109
+
110
+ # Based on step 4 in DDIMScheduler.step
111
+ def get_predicted_noise(
112
+ model_output, timesteps, sample, prediction_type, alphas, sigmas
113
+ ):
114
+ alphas = extract_into_tensor(alphas, timesteps, sample.shape)
115
+ sigmas = extract_into_tensor(sigmas, timesteps, sample.shape)
116
+ if prediction_type == "epsilon":
117
+ pred_epsilon = model_output
118
+ elif prediction_type == "sample":
119
+ pred_epsilon = (sample - alphas * model_output) / sigmas
120
+ elif prediction_type == "v_prediction":
121
+ pred_epsilon = alphas * model_output + sigmas * sample
122
+ else:
123
+ raise ValueError(
124
+ f"Prediction type {prediction_type} is not supported; currently, `epsilon`, `sample`, and `v_prediction`"
125
+ f" are supported."
126
+ )
127
+
128
+ return pred_epsilon
129
+
130
+
131
+ # From LatentConsistencyModel.get_guidance_scale_embedding
132
+ def guidance_scale_embedding(w, embedding_dim=512, dtype=torch.float32):
133
+ """
134
+ See https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298
135
+
136
+ Args:
137
+ timesteps (`torch.Tensor`):
138
+ generate embedding vectors at these timesteps
139
+ embedding_dim (`int`, *optional*, defaults to 512):
140
+ dimension of the embeddings to generate
141
+ dtype:
142
+ data type of the generated embeddings
143
+
144
+ Returns:
145
+ `torch.FloatTensor`: Embedding vectors with shape `(len(timesteps), embedding_dim)`
146
+ """
147
+ assert len(w.shape) == 1
148
+ w = w * 1000.0
149
+
150
+ half_dim = embedding_dim // 2
151
+ emb = torch.log(torch.tensor(10000.0)) / (half_dim - 1)
152
+ emb = torch.exp(torch.arange(half_dim, dtype=dtype) * -emb)
153
+ emb = w.to(dtype)[:, None] * emb[None, :]
154
+ emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
155
+ if embedding_dim % 2 == 1: # zero pad
156
+ emb = torch.nn.functional.pad(emb, (0, 1))
157
+ assert emb.shape == (w.shape[0], embedding_dim)
158
+ return emb
159
+
160
+
161
+ def append_dims(x, target_dims):
162
+ """Appends dimensions to the end of a tensor until it has target_dims dimensions."""
163
+ dims_to_append = target_dims - x.ndim
164
+ if dims_to_append < 0:
165
+ raise ValueError(
166
+ f"input has {x.ndim} dims but target_dims is {target_dims}, which is less"
167
+ )
168
+ return x[(...,) + (None,) * dims_to_append]
169
+
170
+
171
+ # From LCMScheduler.get_scalings_for_boundary_condition_discrete
172
+ def scalings_for_boundary_conditions(timestep, sigma_data=0.5, timestep_scaling=10.0):
173
+ scaled_timestep = timestep_scaling * timestep
174
+ c_skip = sigma_data**2 / (scaled_timestep**2 + sigma_data**2)
175
+ c_out = scaled_timestep / (scaled_timestep**2 + sigma_data**2) ** 0.5
176
+ return c_skip, c_out
177
+
178
+
179
+ # Compare LCMScheduler.step, Step 4
180
+ def get_predicted_original_sample(
181
+ model_output, timesteps, sample, prediction_type, alphas, sigmas
182
+ ):
183
+ alphas = extract_into_tensor(alphas, timesteps, sample.shape)
184
+ sigmas = extract_into_tensor(sigmas, timesteps, sample.shape)
185
+ if prediction_type == "epsilon":
186
+ pred_x_0 = (sample - sigmas * model_output) / alphas
187
+ elif prediction_type == "sample":
188
+ pred_x_0 = model_output
189
+ elif prediction_type == "v_prediction":
190
+ pred_x_0 = alphas * sample - sigmas * model_output
191
+ else:
192
+ raise ValueError(
193
+ f"Prediction type {prediction_type} is not supported; currently, `epsilon`, `sample`, and `v_prediction`"
194
+ f" are supported."
195
+ )
196
+
197
+ return pred_x_0
198
+
199
+
200
+ # Based on step 4 in DDIMScheduler.step
201
+ def get_predicted_noise(
202
+ model_output, timesteps, sample, prediction_type, alphas, sigmas
203
+ ):
204
+ alphas = extract_into_tensor(alphas, timesteps, sample.shape)
205
+ sigmas = extract_into_tensor(sigmas, timesteps, sample.shape)
206
+ if prediction_type == "epsilon":
207
+ pred_epsilon = model_output
208
+ elif prediction_type == "sample":
209
+ pred_epsilon = (sample - alphas * model_output) / sigmas
210
+ elif prediction_type == "v_prediction":
211
+ pred_epsilon = alphas * model_output + sigmas * sample
212
+ else:
213
+ raise ValueError(
214
+ f"Prediction type {prediction_type} is not supported; currently, `epsilon`, `sample`, and `v_prediction`"
215
+ f" are supported."
216
+ )
217
+
218
+ return pred_epsilon
219
+
220
+
221
+ def param_optim(model, condition, extra_params=None, is_lora=False, negation=None):
222
+ extra_params = extra_params if len(extra_params.keys()) > 0 else None
223
+ return {
224
+ "model": model,
225
+ "condition": condition,
226
+ "extra_params": extra_params,
227
+ "is_lora": is_lora,
228
+ "negation": negation,
229
+ }
230
+
231
+
232
+ def create_optim_params(name="param", params=None, lr=5e-6, extra_params=None):
233
+ params = {"name": name, "params": params, "lr": lr}
234
+ if extra_params is not None:
235
+ for k, v in extra_params.items():
236
+ params[k] = v
237
+
238
+ return params
239
+
240
+
241
+ def create_optimizer_params(model_list, lr):
242
+ import itertools
243
+
244
+ optimizer_params = []
245
+
246
+ for optim in model_list:
247
+ model, condition, extra_params, is_lora, negation = optim.values()
248
+ # Check if we are doing LoRA training.
249
+ if is_lora and condition and isinstance(model, list):
250
+ params = create_optim_params(
251
+ params=itertools.chain(*model), extra_params=extra_params
252
+ )
253
+ optimizer_params.append(params)
254
+ continue
255
+
256
+ if is_lora and condition and not isinstance(model, list):
257
+ for n, p in model.named_parameters():
258
+ if "lora" in n:
259
+ params = create_optim_params(n, p, lr, extra_params)
260
+ optimizer_params.append(params)
261
+ continue
262
+
263
+ # If this is true, we can train it.
264
+ if condition:
265
+ for n, p in model.named_parameters():
266
+ should_negate = "lora" in n and not is_lora
267
+ if should_negate:
268
+ continue
269
+
270
+ params = create_optim_params(n, p, lr, extra_params)
271
+ optimizer_params.append(params)
272
+
273
+ return optimizer_params
274
+
275
+
276
+ def handle_trainable_modules(
277
+ model, trainable_modules=None, is_enabled=True, negation=None
278
+ ):
279
+ acc = []
280
+ unfrozen_params = 0
281
+
282
+ if trainable_modules is not None:
283
+ unlock_all = any([name == "all" for name in trainable_modules])
284
+ if unlock_all:
285
+ model.requires_grad_(True)
286
+ unfrozen_params = len(list(model.parameters()))
287
+ else:
288
+ model.requires_grad_(False)
289
+ for name, param in model.named_parameters():
290
+ for tm in trainable_modules:
291
+ if all([tm in name, name not in acc, "lora" not in name]):
292
+ param.requires_grad_(is_enabled)
293
+ acc.append(name)
294
+ unfrozen_params += 1
295
+
296
+
297
+ def huber_loss(pred, target, huber_c=0.001):
298
+ loss = torch.sqrt((pred.float() - target.float()) ** 2 + huber_c**2) - huber_c
299
+ return loss.mean()
300
+
301
+
302
+ @torch.no_grad()
303
+ def update_ema(target_params, source_params, rate=0.99):
304
+ """
305
+ Update target parameters to be closer to those of source parameters using
306
+ an exponential moving average.
307
+
308
+ :param target_params: the target parameter sequence.
309
+ :param source_params: the source parameter sequence.
310
+ :param rate: the EMA rate (closer to 1 means slower).
311
+ """
312
+ for targ, src in zip(target_params, source_params):
313
+ targ.detach().mul_(rate).add_(src, alpha=1 - rate)
314
+
315
+
316
+ def log_validation_video(pipeline, args, accelerator, save_fps):
317
+ if args.seed is None:
318
+ generator = None
319
+ else:
320
+ generator = torch.Generator(device=accelerator.device).manual_seed(args.seed)
321
+
322
+ validation_prompts = [
323
+ "An astronaut riding a horse.",
324
+ "Darth vader surfing in waves.",
325
+ "Robot dancing in times square.",
326
+ "Clown fish swimming through the coral reef.",
327
+ "A child excitedly swings on a rusty swing set, laughter filling the air.",
328
+ "With the style of van gogh, A young couple dances under the moonlight by the lake.",
329
+ "A young woman with glasses is jogging in the park wearing a pink headband.",
330
+ "Impressionist style, a yellow rubber duck floating on the wave on the sunset",
331
+ ]
332
+
333
+ video_logs = []
334
+
335
+ for _, prompt in enumerate(validation_prompts):
336
+ with torch.autocast("cuda"):
337
+ videos = pipeline(
338
+ prompt=prompt,
339
+ frames=args.n_frames,
340
+ num_inference_steps=4,
341
+ num_videos_per_prompt=2,
342
+ generator=generator,
343
+ )
344
+ videos = (videos.clamp(-1.0, 1.0) + 1.0) / 2.0
345
+ videos = (videos * 255).to(torch.uint8).permute(0, 2, 1, 3, 4).cpu().numpy()
346
+ video_logs.append({"validation_prompt": prompt, "videos": videos})
347
+
348
+ for tracker in accelerator.trackers:
349
+ if tracker.name == "wandb":
350
+ formatted_videos = []
351
+ for log in video_logs:
352
+ videos = log["videos"]
353
+ validation_prompt = log["validation_prompt"]
354
+ for video in videos:
355
+ video = wandb.Video(video, caption=validation_prompt, fps=save_fps)
356
+ formatted_videos.append(video)
357
+
358
+ tracker.log({f"validation": formatted_videos})
359
+
360
+ del pipeline
361
+ gc.collect()
362
+
363
+
364
+ def tuple_type(s):
365
+ if isinstance(s, tuple):
366
+ return s
367
+ value = ast.literal_eval(s)
368
+ if isinstance(value, tuple):
369
+ return value
370
+ raise TypeError("Argument must be a tuple")
371
+
372
+
373
+ def load_model_checkpoint(model, ckpt):
374
+ def load_checkpoint(model, ckpt, full_strict):
375
+ state_dict = torch.load(ckpt, map_location="cpu")
376
+ if "state_dict" in list(state_dict.keys()):
377
+ state_dict = state_dict["state_dict"]
378
+ model.load_state_dict(state_dict, strict=full_strict)
379
+ del state_dict
380
+ gc.collect()
381
+ return model
382
+
383
+ load_checkpoint(model, ckpt, full_strict=True)
384
+ print(">>> model checkpoint loaded.")
385
+ return model