From 2a65b4eb29e4874c153a9517ab06b93481c2d238 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Wed, 28 Sep 2022 18:32:15 +0200 Subject: Batches of size 1 cause error: Expected query.is_contiguous() to be true, but got false --- dreambooth.py | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) (limited to 'dreambooth.py') diff --git a/dreambooth.py b/dreambooth.py index 2df6858..0c58ab5 100644 --- a/dreambooth.py +++ b/dreambooth.py @@ -433,7 +433,7 @@ class Checkpointer: del image_grid del stable_latents - for data, pool in [(train_data, "train"), (val_data, "val")]: + for data, pool in [(val_data, "val"), (train_data, "train")]: all_samples = [] filename = f"step_{step}_{pool}.png" @@ -492,12 +492,11 @@ def main(): if args.with_prior_preservation: class_images_dir = Path(args.class_data_dir) - if not class_images_dir.exists(): - class_images_dir.mkdir(parents=True) + class_images_dir.mkdir(parents=True, exist_ok=True) cur_class_images = len(list(class_images_dir.iterdir())) if cur_class_images < args.num_class_images: - torch_dtype = torch.float16 if accelerator.device.type == "cuda" else torch.float32 + torch_dtype = torch.bfloat16 if accelerator.device.type == "cuda" else torch.float32 pipeline = StableDiffusionPipeline.from_pretrained( args.pretrained_model_name_or_path, torch_dtype=torch_dtype) pipeline.enable_attention_slicing() @@ -581,7 +580,6 @@ def main(): eps=args.adam_epsilon, ) - # TODO (patil-suraj): laod scheduler using args noise_scheduler = DDPMScheduler( beta_start=0.00085, beta_end=0.012, @@ -595,7 +593,7 @@ def main(): pixel_values = [example["instance_images"] for example in examples] # concat class and instance examples for prior preservation - if args.with_prior_preservation: + if args.with_prior_preservation and "class_prompt_ids" in examples[0]: input_ids += [example["class_prompt_ids"] for example in examples] pixel_values += [example["class_images"] for example in examples] @@ -789,6 +787,8 @@ def main(): train_loss /= len(train_dataloader) + accelerator.wait_for_everyone() + unet.eval() val_loss = 0.0 @@ -812,18 +812,7 @@ def main(): noise_pred, noise = accelerator.gather_for_metrics((noise_pred, noise)) with accelerator.autocast(): - if args.with_prior_preservation: - noise_pred, noise_pred_prior = torch.chunk(noise_pred, 2, dim=0) - noise, noise_prior = torch.chunk(noise, 2, dim=0) - - loss = F.mse_loss(noise_pred, noise, reduction="none").mean([1, 2, 3]).mean() - - prior_loss = F.mse_loss(noise_pred_prior, noise_prior, - reduction="none").mean([1, 2, 3]).mean() - - loss = loss + args.prior_loss_weight * prior_loss - else: - loss = F.mse_loss(noise_pred, noise, reduction="none").mean([1, 2, 3]).mean() + loss = F.mse_loss(noise_pred, noise, reduction="none").mean([1, 2, 3]).mean() loss = loss.detach().item() val_loss += loss @@ -851,8 +840,6 @@ def main(): global_step, args.resolution, args.resolution, 7.5, 0.0, args.sample_steps) - accelerator.wait_for_everyone() - # Create the pipeline using using the trained modules and save it. if accelerator.is_main_process: print("Finished! Saving final checkpoint and resume state.") -- cgit v1.2.3-54-g00ecf