-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_train.py
484 lines (385 loc) · 16.7 KB
/
utils_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# Copyright 2024 Adobe Research. All rights reserved.
# To view a copy of the license, visit LICENSE.md.
import argparse
import datetime
import logging
import inspect
import math
import os
import random
import gc
import copy
from typing import Dict, Optional, Tuple
from omegaconf import OmegaConf
import cv2
import torch
import torch.nn.functional as F
import torch.utils.checkpoint
import torchvision.transforms as T
import diffusers
import transformers
from torchvision import transforms
from tqdm.auto import tqdm
from accelerate import Accelerator
from accelerate.logging import get_logger
from accelerate.utils import set_seed
from models.unet_3d_condition import UNet3DConditionModel, CustomDiffusionAttnProcessor
from diffusers.models import AutoencoderKL
from diffusers import DPMSolverMultistepScheduler, DDPMScheduler, TextToVideoSDPipeline
from diffusers.optimization import get_scheduler
from diffusers.utils import check_min_version, export_to_video
from diffusers.utils.import_utils import is_xformers_available
from diffusers.models.attention_processor import AttnProcessor2_0, Attention
from diffusers.models.attention import BasicTransformerBlock
from transformers import CLIPTextModel, CLIPTokenizer
from transformers.models.clip.modeling_clip import CLIPEncoder
from utils.dataset import SingleVideoDataset, \
ImageDataset, VideoFolderDataset, CachedDataset
from einops import rearrange, repeat
import numpy as np
import imageio
already_printed_trainables = False
def create_logging(logging, logger, accelerator):
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger.info(accelerator.state, main_process_only=False)
def accelerate_set_verbose(accelerator):
if accelerator.is_local_main_process:
transformers.utils.logging.set_verbosity_warning()
diffusers.utils.logging.set_verbosity_info()
else:
transformers.utils.logging.set_verbosity_error()
diffusers.utils.logging.set_verbosity_error()
def get_train_dataset(dataset_types, train_data, tokenizer):
train_datasets = []
# Loop through all available datasets, get the name, then add to list of data to process.
for DataSet in [SingleVideoDataset, ImageDataset, VideoFolderDataset]:
for dataset in dataset_types:
if dataset == DataSet.__getname__():
train_datasets.append(DataSet(**train_data, tokenizer=tokenizer))
if len(train_datasets) > 0:
return train_datasets
else:
raise ValueError("Dataset type not found: 'json', 'single_video', 'folder', 'image'")
def extend_datasets(datasets, dataset_items, extend=False):
biggest_data_len = max(x.__len__() for x in datasets)
extended = []
for dataset in datasets:
if dataset.__len__() == 0:
del dataset
continue
if dataset.__len__() < biggest_data_len:
for item in dataset_items:
if extend and item not in extended and hasattr(dataset, item):
print(f"Extending {item}")
value = getattr(dataset, item)
value *= biggest_data_len
value = value[:biggest_data_len]
setattr(dataset, item, value)
print(f"New {item} dataset length: {dataset.__len__()}")
extended.append(item)
def export_to_video(video_frames, output_video_path, fps):
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
h, w, _ = video_frames[0].shape
video_writer = cv2.VideoWriter(output_video_path, fourcc, fps=fps, frameSize=(w, h))
for i in range(len(video_frames)):
img = cv2.cvtColor(video_frames[i], cv2.COLOR_RGB2BGR)
video_writer.write(img)
def create_output_folders(output_dir, name, config):
now = datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
# out_dir = os.path.join(output_dir, f"{now}_{name}")
out_dir = os.path.join(output_dir, f"{name}_{now}")
os.makedirs(out_dir, exist_ok=True)
os.makedirs(f"{out_dir}/samples", exist_ok=True)
OmegaConf.save(config, os.path.join(out_dir, 'config.yaml'))
return out_dir
def load_primary_models(pretrained_model_path):
noise_scheduler = DDPMScheduler.from_pretrained(pretrained_model_path, subfolder="scheduler")
tokenizer = CLIPTokenizer.from_pretrained(pretrained_model_path, subfolder="tokenizer")
text_encoder = CLIPTextModel.from_pretrained(pretrained_model_path, subfolder="text_encoder")
vae = AutoencoderKL.from_pretrained(pretrained_model_path, subfolder="vae")
unet = UNet3DConditionModel.from_pretrained(pretrained_model_path, subfolder="unet")
return noise_scheduler, tokenizer, text_encoder, vae, unet
def unet_and_text_g_c(unet, text_encoder, unet_enable, text_enable):
unet._set_gradient_checkpointing(value=unet_enable)
text_encoder._set_gradient_checkpointing(CLIPEncoder, value=text_enable)
def freeze_models(models_to_freeze):
for model in models_to_freeze:
if model is not None: model.requires_grad_(False)
def is_attn(name):
return ('attn1' or 'attn2' == name.split('.')[-1])
def set_processors(attentions):
for attn in attentions: attn.set_processor(AttnProcessor2_0())
def set_torch_2_attn(unet):
optim_count = 0
for name, module in unet.named_modules():
if is_attn(name):
if isinstance(module, torch.nn.ModuleList):
for m in module:
if isinstance(m, BasicTransformerBlock):
set_processors([m.attn1, m.attn2])
optim_count += 1
if optim_count > 0:
print(f"{optim_count} Attention layers using Scaled Dot Product Attention.")
def handle_memory_attention(enable_xformers_memory_efficient_attention, enable_torch_2_attn, unet):
try:
is_torch_2 = hasattr(F, 'scaled_dot_product_attention')
if enable_xformers_memory_efficient_attention and not is_torch_2:
if is_xformers_available():
from xformers.ops import MemoryEfficientAttentionFlashAttentionOp
unet.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)
else:
raise ValueError("xformers is not available. Make sure it is installed correctly")
if enable_torch_2_attn and is_torch_2:
set_torch_2_attn(unet)
except:
print("Could not enable memory efficient attention for xformers or Torch 2.0.")
def param_optim(model, condition, extra_params=None, is_lora=False, negation=None):
return {
"model": model,
"condition": condition,
'extra_params': extra_params,
'is_lora': is_lora,
"negation": negation
}
def create_optim_params(name='param', params=None, lr=5e-6, extra_params=None):
params = {
"name": name,
"params": params,
"lr": lr
}
if extra_params is not None:
for k, v in extra_params.items():
params[k] = v
return params
def create_optimizer_params(model_list, lr):
import itertools
optimizer_params = []
for optim in model_list:
model, condition, extra_params, is_lora, negation = optim.values()
# Check if we are doing LoRA training.
if is_lora and condition:
params = create_optim_params(
params=itertools.chain(*model),
extra_params=extra_params
)
optimizer_params.append(params)
continue
# If this is true, we can train it.
if condition:
for n, p in model.named_parameters():
should_negate = 'lora' in n
if should_negate: continue
params = create_optim_params(n, p, lr, extra_params)
optimizer_params.append(params)
return optimizer_params
def get_optimizer(use_8bit_adam):
if use_8bit_adam:
try:
import bitsandbytes as bnb
except ImportError:
raise ImportError(
"Please install bitsandbytes to use 8-bit Adam. You can do so by running `pip install bitsandbytes`"
)
return bnb.optim.AdamW8bit
else:
return torch.optim.AdamW
def is_mixed_precision(accelerator):
weight_dtype = torch.float32
if accelerator.mixed_precision == "fp16":
weight_dtype = torch.float16
elif accelerator.mixed_precision == "bf16":
weight_dtype = torch.bfloat16
return weight_dtype
def cast_to_gpu_and_type(model_list, accelerator, weight_dtype):
for model in model_list:
if model is not None: model.to(accelerator.device, dtype=weight_dtype)
def handle_cache_latents(
should_cache,
output_dir,
train_dataloader,
train_batch_size,
vae,
cached_latent_dir=None
):
# Cache latents by storing them in VRAM.
# Speeds up training and saves memory by not encoding during the train loop.
if not should_cache: return None
vae.to('cuda', dtype=torch.float16)
vae.enable_slicing()
cached_latent_dir = (
os.path.abspath(cached_latent_dir) if cached_latent_dir is not None else None
)
if cached_latent_dir is None:
cache_save_dir = f"{output_dir}/cached_latents"
os.makedirs(cache_save_dir, exist_ok=True)
for i, batch in enumerate(tqdm(train_dataloader, desc="Caching Latents.")):
save_name = f"cached_{i}"
full_out_path = f"{cache_save_dir}/{save_name}.pt"
pixel_values = batch['pixel_values'].to('cuda', dtype=torch.float16)
batch['pixel_values'] = tensor_to_vae_latent(pixel_values, vae)
if 'pixel_valuesclass' in batch.keys():
pixel_values_class = batch['pixel_valuesclass'].to('cuda', dtype=torch.float16)
batch['pixel_valuesclass'] = tensor_to_vae_latent(pixel_values, vae)
# this will be wrong because will only save 5 class examples but lets check the loading and pickling
for k, v in batch.items(): batch[k] = v[0]
torch.save(batch, full_out_path)
del pixel_values
if 'pixel_valuesclass' in batch.keys():
del pixel_values_class
del batch
# We do this to avoid fragmentation from casting latents between devices.
torch.cuda.empty_cache()
else:
cache_save_dir = cached_latent_dir
return torch.utils.data.DataLoader(
CachedDataset(cache_dir=cache_save_dir),
batch_size=train_batch_size,
shuffle=True,
num_workers=0
)
def get_children(model: torch.nn.Module):
# get children form model!
children = list(model.children())
flatt_children = []
if children == []:
# if model has no children; model is last child! :O
return model
else:
# look for children from children... to the last child!
for child in children:
try:
flatt_children.extend(get_children(child))
except TypeError:
flatt_children.append(get_children(child))
return flatt_children
def handle_trainable_modules(model, trainable_modules=None, is_enabled=True, negation=None):
global already_printed_trainables
# This can most definitely be refactored :-)
unfrozen_params = 0
names = []
if trainable_modules is not None:
for name, module in model.named_modules():
for tm in tuple(trainable_modules):
if tm == 'all':
model.requires_grad_(is_enabled)
unfrozen_params =len(list(model.parameters()))
break
elif tm == 'cross':
if 'attn2' in name and ('to_k' in name or 'to_v' in name) and ('transformer_in' not in name) and ('temp_attentions' not in name):
print('cross name:', name)
for m in module.parameters():
m.requires_grad_(is_enabled)
if is_enabled: unfrozen_params +=1
elif tm == 'self':
if 'attn1' in name and ('to_k' in name or 'to_v' in name) and ('transformer_in' not in name) and ('temp_attentions' not in name):
for m in module.parameters():
m.requires_grad_(is_enabled)
if is_enabled: unfrozen_params +=1
elif tm == 'temporal':
if 'temp_attentions' in name or 'transformer_in' in name:
for nn, m in module.named_parameters():
m.requires_grad_(is_enabled)
if is_enabled: unfrozen_params +=1
names.append(nn)
elif tm == 'temp_attentions':
if 'temp_attentions' in name:
for m in module.parameters():
m.requires_grad_(is_enabled)
if is_enabled: unfrozen_params +=1
elif tm == 'temp_convs':
if 'temp_convs' in name:
for m in module.parameters():
m.requires_grad_(is_enabled)
if is_enabled: unfrozen_params +=1
elif tm in name:
for m in module.parameters():
m.requires_grad_(is_enabled)
if is_enabled: unfrozen_params +=1
else:
module.requires_grad_(False)
unforzen = 0
allp = 0
nnn = []
notn = []
for name, param in model.named_parameters():
allp += 1
if param.requires_grad:
unforzen +=1
nnn.append(name)
else:
notn.append(name)
# breakpoint()
if unforzen > 0 and not already_printed_trainables:
already_printed_trainables = True
print(f"{unforzen} params have been unfrozen for training.")
else:
print(f'No parameters have been unfrozen for training')
def tensor_to_vae_latent(t, vae):
video_length = t.shape[1]
t = rearrange(t, "b f c h w -> (b f) c h w")
latents = vae.encode(t).latent_dist.sample()
latents = rearrange(latents, "(b f) c h w -> b c f h w", f=video_length)
latents = latents * 0.18215
return latents
def sample_noise(latents, noise_strength, use_offset_noise):
b ,c, f, *_ = latents.shape
noise_latents = torch.randn_like(latents, device=latents.device)
offset_noise = None
if use_offset_noise:
offset_noise = torch.randn(b, c, f, 1, 1, device=latents.device)
noise_latents = noise_latents + noise_strength * offset_noise
return noise_latents
def should_sample(global_step, validation_steps, validation_data):
return (global_step % validation_steps == 0) and (validation_data.sample_preview or global_step == 1)
def save_pipe(
path,
global_step,
accelerator,
unet,
text_encoder,
tokenizer,
vae,
output_dir,
is_checkpoint=False,
logger=None,
):
if is_checkpoint:
save_path = os.path.join(output_dir, f"checkpoint-{global_step}")
os.makedirs(save_path, exist_ok=True)
else:
save_path = output_dir
# Save the dtypes so we can continue training at the same precision.
u_dtype, t_dtype, v_dtype = unet.dtype, text_encoder.dtype, vae.dtype
# Copy the model without creating a reference to it. This allows keeping the state of our lora training if enabled.
unet_out = copy.deepcopy(accelerator.unwrap_model(unet, keep_fp32_wrapper=False))
text_encoder_out = copy.deepcopy(accelerator.unwrap_model(text_encoder, keep_fp32_wrapper=False))
tokenizer = copy.deepcopy(tokenizer)
pipeline = TextToVideoSDPipeline.from_pretrained(
path,
unet=unet_out,
text_encoder=text_encoder_out,
tokenizer=tokenizer,
vae=vae,
).to(torch_dtype=torch.float16)
pipeline.save_pretrained(save_path)
if is_checkpoint:
unet, text_encoder = accelerator.prepare(unet, text_encoder)
models_to_cast_back = [(unet, u_dtype), (text_encoder, t_dtype), (vae, v_dtype)]
[x[0].to(accelerator.device, dtype=x[1]) for x in models_to_cast_back]
logger.info(f"Saved model at {save_path} on step {global_step}")
del pipeline
del unet_out
del text_encoder_out
torch.cuda.empty_cache()
gc.collect()
def replace_prompt(prompt, token, wlist):
for w in wlist:
if w in prompt: return prompt.replace(w, token)
return prompt
def freeze_params(params):
for param in params:
param.requires_grad = False