-
Notifications
You must be signed in to change notification settings - Fork 37
/
trainT2I.py
867 lines (740 loc) · 37.3 KB
/
trainT2I.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
import argparse
import logging
import math
import os
import random
import shutil
from pathlib import Path
import accelerate
import datasets
import numpy as np
import torch
import torch.nn.functional as F
import torch.utils.checkpoint
import transformers
from accelerate import Accelerator
from accelerate.logging import get_logger
from accelerate.state import AcceleratorState
from accelerate.utils import ProjectConfiguration, set_seed
from datasets import load_dataset, Dataset
from huggingface_hub import create_repo, upload_folder
from packaging import version
from PIL import Image
from torchvision import transforms
from tqdm.auto import tqdm
from transformers import CLIPTextModel, CLIPTokenizer
from transformers.utils import ContextManagers
import diffusers
from diffusers import AutoencoderKL, DDPMScheduler, StableDiffusionPipeline, UNet2DConditionModel
from diffusers.optimization import get_scheduler
from diffusers.training_utils import EMAModel
from diffusers.utils import check_min_version, deprecate, is_wandb_available
from diffusers.utils.import_utils import is_xformers_available
import gc
if is_wandb_available():
import wandb
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.19.0.dev0")
logger = get_logger(__name__, log_level="INFO")
DATASET_NAME_MAPPING = {
"lambdalabs/pokemon-blip-captions": ("image", "text"),
}
def make_image_grid(imgs, rows, cols):
assert len(imgs) == rows * cols
w, h = imgs[0].size
grid = Image.new("RGB", size=(cols * w, rows * h))
for i, img in enumerate(imgs):
grid.paste(img, box=(i % cols * w, i // cols * h))
return grid
def log_validation(vae, text_encoder, tokenizer, unet, args, accelerator, weight_dtype, epoch):
logger.info("Running validation... ")
pipeline = StableDiffusionPipeline.from_pretrained(
args.pretrained_model_name_or_path,
vae=accelerator.unwrap_model(vae),
text_encoder=accelerator.unwrap_model(text_encoder),
tokenizer=tokenizer,
unet=accelerator.unwrap_model(unet),
safety_checker=None,
revision=args.revision,
torch_dtype=weight_dtype
)
pipeline = pipeline.to(accelerator.device)
pipeline.set_progress_bar_config(disable=True)
if args.enable_xformers_memory_efficient_attention:
pipeline.enable_xformers_memory_efficient_attention()
if args.seed is None:
generator = None
else:
generator = torch.Generator(device=accelerator.device).manual_seed(args.seed)
images = []
for i in range(len(args.validation_prompts)):
with torch.autocast("cuda"):
image = pipeline(args.validation_prompts[i], num_inference_steps=20, generator=generator).images[0]
images.append(image)
for tracker in accelerator.trackers:
if tracker.name == "tensorboard":
np_images = np.stack([np.asarray(img) for img in images])
tracker.writer.add_images("validation", np_images, epoch, dataformats="NHWC")
elif tracker.name == "wandb":
tracker.log(
{
"validation": [
wandb.Image(image, caption=f"{i}: {args.validation_prompts[i]}")
for i, image in enumerate(images)
]
}
)
else:
logger.warn(f"image logging not implemented for {tracker.name}")
del pipeline
torch.cuda.empty_cache()
return images
from dataclasses import dataclass
@dataclass
class TrainingConfig:
pretrained_model_name_or_path = "SG161222/Realistic_Vision_V4.0"
image_size = 512 # the generated image resolution
train_batch_size = 32
max_train_steps = 10000
gradient_accumulation_steps = 4
learning_rate = 1e-4
feature_weight=0.5
output_weight=0.5
lr_warmup_steps = 0
eval_batch_size = 16 # how many images to sample during evaluation
num_train_epochs = 1
mixed_precision = "fp16" # `no` for float32, `fp16` for automatic mixed precision
output_dir = "Checkpoints"
train_data_dir = "data"
save_dir = "Saved"
logging_dir = "logs"
overwrite_output_dir = True # overwrite the old model when re-running the notebook
seed = 647
report_to = "wandb"
revision = None
distill_level = "sd_small" # One of "sd_small" or "sd_tiny"
resume_from_checkpoint = None
use_ema = False
enable_xformers_memory_efficient_attention = True
gradient_checkpointing = True
allow_tf32 = True #True for Ampere Backend
scale_lr = False
use_8bit_adam = False
adam_beta1 = 0.9
adam_beta2 = 0.999
adam_weight_decay = 1e-2
adam_epsilon = 1e-8
max_grad_norm = 1
dataset_name = None
cache_dir = None
image_column = "image"
caption_column = "caption"
max_train_samples = None
validation_prompts = None
resolution = 512
center_crop = True
random_flip = True
dataloader_num_workers = 0
lr_scheduler = "cosine"
tracker_project_name = "text2image-fine-tune"
noise_offset = 0
input_perturbation = 0
prediction_type = None
checkpointing_steps = 1000
checkpoints_total_limit = 1
non_ema_revision = None
validation_prompts = None
saving_steps = 5000
def main():
config = TrainingConfig()
logging_dir = os.path.join(config.output_dir, config.logging_dir)
accelerator_project_config = ProjectConfiguration(project_dir=config.output_dir, logging_dir=logging_dir)
accelerator = Accelerator(
gradient_accumulation_steps=config.gradient_accumulation_steps,
mixed_precision=config.mixed_precision,
log_with=config.report_to,
project_config=accelerator_project_config,
)
# Make one log on every process with the configuration for debugging.
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)
if accelerator.is_local_main_process:
datasets.utils.logging.set_verbosity_warning()
transformers.utils.logging.set_verbosity_warning()
diffusers.utils.logging.set_verbosity_info()
else:
datasets.utils.logging.set_verbosity_error()
transformers.utils.logging.set_verbosity_error()
diffusers.utils.logging.set_verbosity_error()
# If passed along, set the training seed now.
if config.seed is not None:
set_seed(config.seed)
# Handle the repository creation
if accelerator.is_main_process:
if config.output_dir is not None:
os.makedirs(config.output_dir, exist_ok=True)
# if args.push_to_hub:
# repo_id = create_repo(
# repo_id=args.hub_model_id or Path(args.output_dir).name, exist_ok=True, token=args.hub_token
# ).repo_id
# Load scheduler, tokenizer and models.
noise_scheduler = DDPMScheduler.from_pretrained(config.pretrained_model_name_or_path, subfolder="scheduler")
tokenizer = CLIPTokenizer.from_pretrained(
config.pretrained_model_name_or_path, subfolder="tokenizer", revision=config.revision
)
def deepspeed_zero_init_disabled_context_manager():
"""
returns either a context list that includes one that will disable zero.Init or an empty context list
"""
deepspeed_plugin = AcceleratorState().deepspeed_plugin if accelerate.state.is_initialized() else None
if deepspeed_plugin is None:
return []
return [deepspeed_plugin.zero3_init_context_manager(enable=False)]
# Currently Accelerate doesn't know how to handle multiple models under Deepspeed ZeRO stage 3.
# For this to work properly all models must be run through `accelerate.prepare`. But accelerate
# will try to assign the same optimizer with the same weights to all models during
# `deepspeed.initialize`, which of course doesn't work.
#
# For now the following workaround will partially support Deepspeed ZeRO-3, by excluding the 2
# frozen models from being partitioned during `zero.Init` which gets called during
# `from_pretrained` So CLIPTextModel and AutoencoderKL will not enjoy the parameter sharding
# across multiple gpus and only UNet2DConditionModel will get ZeRO sharded.
with ContextManagers(deepspeed_zero_init_disabled_context_manager()):
text_encoder = CLIPTextModel.from_pretrained(
config.pretrained_model_name_or_path, subfolder="text_encoder", revision=config.revision
)
vae = AutoencoderKL.from_pretrained(
config.pretrained_model_name_or_path, subfolder="vae", revision=config.revision
)
def prepare_unet(unet, model_type):
assert model_type in ["sd_tiny", "sd_small"]
# Set mid block to None if mode is other than base
if model_type != "sd_small":
unet.mid_block = None
# Commence deletion of resnets/attentions inside the U-net
# Handle Down Blocks
for i in range(3):
delattr(unet.down_blocks[i].resnets, "1")
delattr(unet.down_blocks[i].attentions, "1")
if model_type == "sd_tiny":
delattr(unet.down_blocks, "3")
unet.down_blocks[2].downsamplers = None
else:
delattr(unet.down_blocks[3].resnets, "1")
# Handle Up blocks
unet.up_blocks[0].resnets[1] = unet.up_blocks[0].resnets[2]
delattr(unet.up_blocks[0].resnets, "2")
for i in range(1, 4):
unet.up_blocks[i].resnets[1] = unet.up_blocks[i].resnets[2]
unet.up_blocks[i].attentions[1] = unet.up_blocks[i].attentions[2]
delattr(unet.up_blocks[i].attentions, "2")
delattr(unet.up_blocks[i].resnets, "2")
if model_type == "sd_tiny":
for i in range(3):
unet.up_blocks[i] = unet.up_blocks[i + 1]
delattr(unet.up_blocks, "3")
torch.cuda.empty_cache()
gc.collect()
unet = UNet2DConditionModel.from_pretrained(
config.pretrained_model_name_or_path, subfolder="unet", revision=config.non_ema_revision
)
KD_teacher_unet=UNet2DConditionModel.from_pretrained(
config.pretrained_model_name_or_path, subfolder="unet", revision=config.non_ema_revision
)
assert config.distill_level=="sd_small" or config.distill_level=="sd_tiny"
if config.distill_level:
prepare_unet(unet, config.distill_level)
unet.load_state_dict(torch.load("trained_unet/unet.pt"))
# Freeze vae and text_encoder
vae.requires_grad_(False)
text_encoder.requires_grad_(False)
KD_teacher_unet.requires_grad_(False)
# Create EMA for the unet.
if config.use_ema:
ema_unet = UNet2DConditionModel.from_pretrained(
config.pretrained_model_name_or_path, subfolder="unet", revision=config.revision
)
ema_unet = EMAModel(ema_unet.parameters(), model_cls=UNet2DConditionModel, model_config=ema_unet.config)
if config.enable_xformers_memory_efficient_attention:
if is_xformers_available():
import xformers
xformers_version = version.parse(xformers.__version__)
if xformers_version == version.parse("0.0.16"):
logger.warn(
"xFormers 0.0.16 cannot be used for training in some GPUs. If you observe problems during training, please update xFormers to at least 0.0.17. See https://huggingface.co/docs/diffusers/main/en/optimization/xformers for more details."
)
unet.enable_xformers_memory_efficient_attention()
else:
raise ValueError("xformers is not available. Make sure it is installed correctly")
def compute_snr(timesteps):
"""
Computes SNR as per https://github.com/TiankaiHang/Min-SNR-Diffusion-Training/blob/521b624bd70c67cee4bdf49225915f5945a872e3/guided_diffusion/gaussian_diffusion.py#L847-L849
"""
alphas_cumprod = noise_scheduler.alphas_cumprod
sqrt_alphas_cumprod = alphas_cumprod**0.5
sqrt_one_minus_alphas_cumprod = (1.0 - alphas_cumprod) ** 0.5
# Expand the tensors.
# Adapted from https://github.com/TiankaiHang/Min-SNR-Diffusion-Training/blob/521b624bd70c67cee4bdf49225915f5945a872e3/guided_diffusion/gaussian_diffusion.py#L1026
sqrt_alphas_cumprod = sqrt_alphas_cumprod.to(device=timesteps.device)[timesteps].float()
while len(sqrt_alphas_cumprod.shape) < len(timesteps.shape):
sqrt_alphas_cumprod = sqrt_alphas_cumprod[..., None]
alpha = sqrt_alphas_cumprod.expand(timesteps.shape)
sqrt_one_minus_alphas_cumprod = sqrt_one_minus_alphas_cumprod.to(device=timesteps.device)[timesteps].float()
while len(sqrt_one_minus_alphas_cumprod.shape) < len(timesteps.shape):
sqrt_one_minus_alphas_cumprod = sqrt_one_minus_alphas_cumprod[..., None]
sigma = sqrt_one_minus_alphas_cumprod.expand(timesteps.shape)
# Compute SNR.
snr = (alpha / sigma) ** 2
return snr
# `accelerate` 0.16.0 will have better support for customized saving
if version.parse(accelerate.__version__) >= version.parse("0.16.0"):
# create custom saving & loading hooks so that `accelerator.save_state(...)` serializes in a nice format
def save_model_hook(models, weights, output_dir):
if config.use_ema:
ema_unet.save_pretrained(os.path.join(output_dir, "unet_ema"))
for i, model in enumerate(models):
model.save_pretrained(os.path.join(output_dir, "unet"))
# make sure to pop weight so that corresponding model is not saved again
weights.pop()
def load_model_hook(models, input_dir):
if config.use_ema:
load_model = EMAModel.from_pretrained(os.path.join(input_dir, "unet_ema"), UNet2DConditionModel)
ema_unet.load_state_dict(load_model.state_dict())
ema_unet.to(accelerator.device)
del load_model
for i in range(len(models)):
# pop models so that they are not loaded again
model = models.pop()
# load diffusers style into model
load_model = UNet2DConditionModel.from_pretrained(input_dir, subfolder="unet")
model.register_to_config(**load_model.config)
model.load_state_dict(load_model.state_dict())
del load_model
accelerator.register_save_state_pre_hook(save_model_hook)
accelerator.register_load_state_pre_hook(load_model_hook)
if config.gradient_checkpointing:
unet.enable_gradient_checkpointing()
# Enable TF32 for faster training on Ampere GPUs,
# cf https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices
if config.allow_tf32:
torch.backends.cuda.matmul.allow_tf32 = True
if config.scale_lr:
config.learning_rate = (
config.learning_rate * config.gradient_accumulation_steps * config.train_batch_size * accelerator.num_processes
)
# Initialize the optimizer
if config.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`"
)
optimizer_cls = bnb.optim.AdamW8bit
else:
optimizer_cls = torch.optim.AdamW
optimizer = optimizer_cls(
unet.parameters(),
lr=config.learning_rate,
betas=(config.adam_beta1, config.adam_beta2),
weight_decay=config.adam_weight_decay,
eps=config.adam_epsilon,
)
# Get the datasets: you can either provide your own training and evaluation files (see below)
# or specify a Dataset from the hub (the dataset will be downloaded automatically from the datasets Hub).
# In distributed training, the load_dataset function guarantees that only one local process can concurrently
# download the dataset.
if config.dataset_name is not None:
# Downloading and loading a dataset from the hub.
dataset = load_dataset(
config.dataset_name,
config.dataset_config_name,
cache_dir=config.cache_dir,
)
else:
def get_text(x):
with open(x, 'rb') as f:
data = f.readline()
return data
data_files = {}
data_files["image"] = [os.path.join(config.train_data_dir, path) for path in os.listdir(config.train_data_dir) if path.endswith(".jpg")]
data_files["caption"] = [os.path.join(config.train_data_dir, path) for path in os.listdir(config.train_data_dir) if path.endswith(".txt")]
dataset = Dataset.from_dict(
data_files
).cast_column("image", datasets.Image())
dataset = dataset.map(lambda batch: {"image": batch["image"], "caption":get_text(batch['caption'])})
# data_files = {}
# if config.train_data_dir is not None:
# data_files["train"] = os.path.join(config.train_data_dir, "*.parquet")
# dataset = load_dataset(
# "imagefolder",
# data_files=data_files,
# cache_dir=config.cache_dir,
# )
# See more about loading custom images at
# https://huggingface.co/docs/datasets/v2.4.0/en/image_load#imagefolder
# Preprocessing the datasets.
# We need to tokenize inputs and targets.
# column_names = dataset["train"].column_names
column_names = dataset.column_names
# 6. Get the column names for input/target.
dataset_columns = DATASET_NAME_MAPPING.get(config.dataset_name, None)
if config.image_column is None:
image_column = dataset_columns[0] if dataset_columns is not None else column_names[0]
else:
image_column = config.image_column
if image_column not in column_names:
raise ValueError(
f"--image_column' value '{config.image_column}' needs to be one of: {', '.join(column_names)}"
)
if config.caption_column is None:
caption_column = dataset_columns[1] if dataset_columns is not None else column_names[1]
else:
caption_column = config.caption_column
if caption_column not in column_names:
raise ValueError(
f"--caption_column' value '{config.caption_column}' needs to be one of: {', '.join(column_names)}"
)
# Preprocessing the datasets.
# We need to tokenize input captions and transform the images.
def tokenize_captions(examples, is_train=True):
captions = []
for caption in examples[caption_column]:
if isinstance(caption, str):
captions.append(caption)
elif isinstance(caption, (list, np.ndarray)):
# take a random caption if there are multiple
captions.append(random.choice(caption) if is_train else caption[0])
else:
raise ValueError(
f"Caption column `{caption_column}` should contain either strings or lists of strings."
)
inputs = tokenizer(
captions, max_length=tokenizer.model_max_length, padding="max_length", truncation=True, return_tensors="pt"
)
return inputs.input_ids
# Preprocessing the datasets.
train_transforms = transforms.Compose(
[
transforms.Resize(config.resolution, interpolation=transforms.InterpolationMode.BILINEAR),
transforms.CenterCrop(config.resolution) if config.center_crop else transforms.RandomCrop(config.resolution),
transforms.RandomHorizontalFlip() if config.random_flip else transforms.Lambda(lambda x: x),
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5]),
]
)
def preprocess_train(examples):
images = [image.convert("RGB") for image in examples[image_column]]
examples["pixel_values"] = [train_transforms(image) for image in images]
examples["input_ids"] = tokenize_captions(examples)
return examples
with accelerator.main_process_first():
if config.max_train_samples is not None:
# dataset["train"] = dataset["train"].shuffle(seed=config.seed).select(range(config.max_train_samples))
dataset = dataset.shuffle(seed=config.seed).select(range(config.max_train_samples))
# Set the training transforms
# train_dataset = dataset["train"].with_transform(preprocess_train)
train_dataset = dataset.with_transform(preprocess_train)
def collate_fn(examples):
pixel_values = torch.stack([example["pixel_values"] for example in examples])
pixel_values = pixel_values.to(memory_format=torch.contiguous_format).float()
input_ids = torch.stack([example["input_ids"] for example in examples])
return {"pixel_values": pixel_values, "input_ids": input_ids}
# DataLoaders creation:
train_dataloader = torch.utils.data.DataLoader(
train_dataset,
shuffle=True,
collate_fn=collate_fn,
batch_size=config.train_batch_size,
num_workers=config.dataloader_num_workers,
)
# Scheduler and math around the number of training steps.
overrode_max_train_steps = False
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / config.gradient_accumulation_steps)
if config.max_train_steps is None:
config.max_train_steps = config.num_train_epochs * num_update_steps_per_epoch
overrode_max_train_steps = True
lr_scheduler = get_scheduler(
config.lr_scheduler,
optimizer=optimizer,
num_warmup_steps=config.lr_warmup_steps * config.gradient_accumulation_steps,
num_training_steps=config.max_train_steps * config.gradient_accumulation_steps,
)
# Prepare everything with our `accelerator`.
unet, optimizer, train_dataloader, lr_scheduler,KD_teacher_unet = accelerator.prepare(
unet, optimizer, train_dataloader, lr_scheduler,KD_teacher_unet
)
if config.use_ema:
ema_unet.to(accelerator.device)
# For mixed precision training we cast all non-trainable weigths (vae, non-lora text_encoder and non-lora unet) to half-precision
# as these weights are only used for inference, keeping weights in full precision is not required.
weight_dtype = torch.float32
if accelerator.mixed_precision == "fp16":
weight_dtype = torch.float16
config.mixed_precision = accelerator.mixed_precision
elif accelerator.mixed_precision == "bf16":
weight_dtype = torch.bfloat16
config.mixed_precision = accelerator.mixed_precision
# Move text_encode and vae to gpu and cast to weight_dtype
text_encoder.to(accelerator.device, dtype=weight_dtype)
vae.to(accelerator.device, dtype=weight_dtype)
# We need to recalculate our total training steps as the size of the training dataloader may have changed.
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / config.gradient_accumulation_steps)
if overrode_max_train_steps:
config.max_train_steps = config.num_train_epochs * num_update_steps_per_epoch
# Afterwards we recalculate our number of training epochs
config.num_train_epochs = math.ceil(config.max_train_steps / num_update_steps_per_epoch)
# We need to initialize the trackers we use, and also store our configuration.
# The trackers initializes automatically on the main process.
if accelerator.is_main_process:
tracker_config = dict(vars(config))
# print(tracker_config)
# tracker_config.pop("validation_prompts")
accelerator.init_trackers(config.tracker_project_name, tracker_config)
# Train!
total_batch_size = config.train_batch_size * accelerator.num_processes * config.gradient_accumulation_steps
logger.info("***** Running training *****")
logger.info(f" Num examples = {len(train_dataset)}")
logger.info(f" Num Epochs = {config.num_train_epochs}")
logger.info(f" Instantaneous batch size per device = {config.train_batch_size}")
logger.info(f" Total train batch size (w. parallel, distributed & accumulation) = {total_batch_size}")
logger.info(f" Gradient Accumulation steps = {config.gradient_accumulation_steps}")
logger.info(f" Total optimization steps = {config.max_train_steps}")
global_step = 0
first_epoch = 0
# Potentially load in the weights and states from a previous save
if config.resume_from_checkpoint:
if config.resume_from_checkpoint != "latest":
path = os.path.basename(config.resume_from_checkpoint)
else:
# Get the most recent checkpoint
dirs = os.listdir(config.output_dir)
dirs = [d for d in dirs if d.startswith("checkpoint")]
dirs = sorted(dirs, key=lambda x: int(x.split("-")[1]))
path = dirs[-1] if len(dirs) > 0 else None
if path is None:
accelerator.print(
f"Checkpoint '{config.resume_from_checkpoint}' does not exist. Starting a new training run."
)
config.resume_from_checkpoint = None
else:
accelerator.print(f"Resuming from checkpoint {path}")
accelerator.load_state(os.path.join(config.output_dir, path))
global_step = int(path.split("-")[1])
resume_global_step = global_step * config.gradient_accumulation_steps
first_epoch = global_step // num_update_steps_per_epoch
resume_step = resume_global_step % (num_update_steps_per_epoch * config.gradient_accumulation_steps)
# Only show the progress bar once on each machine.
progress_bar = tqdm(range(global_step, config.max_train_steps), disable=not accelerator.is_local_main_process)
progress_bar.set_description("Steps")
KD_teacher = {}
KD_student= {}
def getActivation(activation,name,residuals_present):
# the hook signature
if residuals_present:
def hook(model, input, output):
activation[name] = output[0]
else:
def hook(model, input, output):
activation[name] = output
return hook
def cast_hook(unet,dicts,model_type,teacher=False):
if teacher:
for i in range(4):
unet.down_blocks[i].register_forward_hook(getActivation(dicts,'d'+str(i),True))
unet.mid_block.register_forward_hook(getActivation(dicts,'m',False))
for i in range(4):
unet.up_blocks[i].register_forward_hook(getActivation(dicts,'u'+str(i),False))
else:
num_blocks= 4 if model_type=="sd_small" else 3
for i in range(num_blocks):
unet.down_blocks[i].register_forward_hook(getActivation(dicts,'d'+str(i),True))
if model_type=="sd_small"
unet.mid_block.register_forward_hook(getActivation(dicts,'m',False))
for i in range(num_blocks):
unet.up_blocks[i].register_forward_hook(getActivation(dicts,'u'+str(i),False))
cast_hook(unet,KD_student,config.distill_level,False)
cast_hook(KD_teacher_unet,KD_teacher,config.distill_level,True)
for epoch in range(first_epoch, config.num_train_epochs):
unet.train()
train_loss = 0.0
for step, batch in enumerate(train_dataloader):
# Skip steps until we reach the resumed step
if config.resume_from_checkpoint and epoch == first_epoch and step < resume_step:
if step % config.gradient_accumulation_steps == 0:
progress_bar.update(1)
continue
with accelerator.accumulate(unet):
# Convert images to latent space
latents = vae.encode(batch["pixel_values"].to(weight_dtype)).latent_dist.sample()
latents = latents * vae.config.scaling_factor
# Sample noise that we'll add to the latents
noise = torch.randn_like(latents)
if config.noise_offset:
# https://www.crosslabs.org//blog/diffusion-with-offset-noise
noise += config.noise_offset * torch.randn(
(latents.shape[0], latents.shape[1], 1, 1), device=latents.device
)
if config.input_perturbation:
new_noise = noise + config.input_perturbation * torch.randn_like(noise)
bsz = latents.shape[0]
# Sample a random timestep for each image
timesteps = torch.randint(0, noise_scheduler.config.num_train_timesteps, (bsz,), device=latents.device)
timesteps = timesteps.long()
# Add noise to the latents according to the noise magnitude at each timestep
# (this is the forward diffusion process)
if config.input_perturbation:
noisy_latents = noise_scheduler.add_noise(latents, new_noise, timesteps)
else:
noisy_latents = noise_scheduler.add_noise(latents, noise, timesteps)
# Get the text embedding for conditioning
encoder_hidden_states = text_encoder(batch["input_ids"])[0]
# Get the target for loss depending on the prediction type
if config.prediction_type is not None:
# set prediction_type of scheduler if defined
noise_scheduler.register_to_config(prediction_type=config.prediction_type)
if noise_scheduler.config.prediction_type == "epsilon":
target = noise
elif noise_scheduler.config.prediction_type == "v_prediction":
target = noise_scheduler.get_velocity(latents, noise, timesteps)
else:
raise ValueError(f"Unknown prediction type {noise_scheduler.config.prediction_type}")
# Predict the noise residual and compute loss
model_pred = unet(noisy_latents, timesteps, encoder_hidden_states).sample
with torch.no_grad():
teacher_pred=KD_teacher_unet(noisy_latents, timesteps, encoder_hidden_states).sample
loss_features=0
if configs.distill_level=="sd_small":
for i in range(4):
loss_features=loss_features+F.mse_loss(KD_teacher['d'+str(i)],KD_student['d'+str(i)])
loss_features=loss_features+F.mse_loss(KD_teacher['m'],KD_student['m'])
for i in range(4):
loss_features=loss_features+F.mse_loss(KD_teacher['u'+str(i)],KD_student['u'+str(i)])
else:
for i in range(2):
loss_features=loss_features+F.mse_loss(KD_teacher['d'+str(i)],KD_student['d'+str(i)])
loss_features=loss_features+F.mse_loss(KD_teacher['u'+str(0)],KD_student['d'+str(2)])
for i in range(3):
loss_features=loss_features+F.mse_loss(KD_teacher['u'+str(i+1)],KD_student['u'+str(i)])
loss_KD=F.mse_loss(model_pred.float(), teacher_pred.float(), reduction="mean")
loss_task = F.mse_loss(model_pred.float(), target.float(), reduction="mean")
loss=loss_task+config.output_weight*loss_KD+config.feature_weight*loss_features
# Gather the losses across all processes for logging (if we use distributed training).
avg_loss = accelerator.gather(loss.repeat(config.train_batch_size)).mean()
train_loss += avg_loss.item() / config.gradient_accumulation_steps
# Backpropagate
accelerator.backward(loss)
if accelerator.sync_gradients:
accelerator.clip_grad_norm_(unet.parameters(), config.max_grad_norm)
optimizer.step()
lr_scheduler.step()
optimizer.zero_grad()
torch.cuda.empty_cache()
# Checks if the accelerator has performed an optimization step behind the scenes
if accelerator.sync_gradients:
if config.use_ema:
ema_unet.step(unet.parameters())
progress_bar.update(1)
global_step += 1
accelerator.log({"train_loss": train_loss}, step=global_step)
train_loss = 0.0
if global_step % config.saving_steps == 0:
pipeline = StableDiffusionPipeline.from_pretrained(
config.pretrained_model_name_or_path,
text_encoder=text_encoder,
vae=vae,
unet=unet,
revision=config.revision,
)
pipeline.save_pretrained(config.save_dir + f"/{str(global_step)}")
if global_step % config.checkpointing_steps == 0:
if accelerator.is_main_process:
# _before_ saving state, check if this save would set us over the `checkpoints_total_limit`
if config.checkpoints_total_limit is not None:
checkpoints = os.listdir(config.output_dir)
checkpoints = [d for d in checkpoints if d.startswith("checkpoint")]
checkpoints = sorted(checkpoints, key=lambda x: int(x.split("-")[1]))
# before we save the new checkpoint, we need to have at _most_ `checkpoints_total_limit - 1` checkpoints
if len(checkpoints) >= config.checkpoints_total_limit:
num_to_remove = len(checkpoints) - config.checkpoints_total_limit + 1
removing_checkpoints = checkpoints[0:num_to_remove]
logger.info(
f"{len(checkpoints)} checkpoints already exist, removing {len(removing_checkpoints)} checkpoints"
)
logger.info(f"removing checkpoints: {', '.join(removing_checkpoints)}")
for removing_checkpoint in removing_checkpoints:
removing_checkpoint = os.path.join(config.output_dir, removing_checkpoint)
shutil.rmtree(removing_checkpoint)
save_path = os.path.join(config.output_dir, f"checkpoint-{global_step}")
accelerator.save_state(save_path)
logger.info(f"Saved state to {save_path}")
logs = {"step_loss": loss.detach().item(), "lr": lr_scheduler.get_last_lr()[0]}
progress_bar.set_postfix(**logs)
if global_step >= config.max_train_steps:
break
if accelerator.is_main_process:
if config.validation_prompts is not None and epoch % config.validation_epochs == 0:
if config.use_ema:
# Store the UNet parameters temporarily and load the EMA parameters to perform inference.
ema_unet.store(unet.parameters())
ema_unet.copy_to(unet.parameters())
log_validation(
vae,
text_encoder,
tokenizer,
unet,
config,
accelerator,
weight_dtype,
global_step,
)
if config.use_ema:
# Switch back to the original UNet parameters.
ema_unet.restore(unet.parameters())
# Create the pipeline using the trained modules and save it.
accelerator.wait_for_everyone()
if accelerator.is_main_process:
unet = accelerator.unwrap_model(unet)
if config.use_ema:
ema_unet.copy_to(unet.parameters())
torch.save(unet.state_dict(), "trained_unet/unet.pt")
pipeline = StableDiffusionPipeline.from_pretrained(
config.pretrained_model_name_or_path,
text_encoder=text_encoder,
vae=vae,
unet=unet,
revision=config.revision,
)
pipeline.save_pretrained(config.output_dir)
# Run a final round of inference.
images = []
if config.validation_prompts is not None:
logger.info("Running inference for collecting generated images...")
pipeline = pipeline.to(accelerator.device)
pipeline.torch_dtype = weight_dtype
pipeline.set_progress_bar_config(disable=True)
if config.enable_xformers_memory_efficient_attention:
pipeline.enable_xformers_memory_efficient_attention()
if config.seed is None:
generator = None
else:
generator = torch.Generator(device=accelerator.device).manual_seed(config.seed)
for i in range(len(config.validation_prompts)):
with torch.autocast("cuda"):
image = pipeline(args.validation_prompts[i], num_inference_steps=20, generator=generator).images[0]
images.append(image)
# if config.push_to_hub:
# save_model_card(args, repo_id, images, repo_folder=args.output_dir)
# upload_folder(
# repo_id=repo_id,
# folder_path=args.output_dir,
# commit_message="End of training",
# ignore_patterns=["step_*", "epoch_*"],
# )
accelerator.end_training()
if __name__ == "__main__":
main()