From 0ab303b9d1c0e13e95897889e3ce97ca72057bf9 Mon Sep 17 00:00:00 2001 From: bssrdf Date: Wed, 10 Apr 2024 08:57:13 -0400 Subject: [PATCH 1/8] fixed vae loading issue #7619 --- examples/dreambooth/train_dreambooth.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/examples/dreambooth/train_dreambooth.py b/examples/dreambooth/train_dreambooth.py index a18c443e7d4d..d9701c2437a7 100644 --- a/examples/dreambooth/train_dreambooth.py +++ b/examples/dreambooth/train_dreambooth.py @@ -33,7 +33,7 @@ from accelerate import Accelerator from accelerate.logging import get_logger from accelerate.utils import ProjectConfiguration, set_seed -from huggingface_hub import create_repo, model_info, upload_folder +from huggingface_hub import create_repo, upload_folder from huggingface_hub.utils import insecure_hashlib from packaging import version from PIL import Image @@ -758,15 +758,6 @@ def __getitem__(self, index): return example -def model_has_vae(args): - config_file_name = os.path.join("vae", AutoencoderKL.config_name) - if os.path.isdir(args.pretrained_model_name_or_path): - config_file_name = os.path.join(args.pretrained_model_name_or_path, config_file_name) - return os.path.isfile(config_file_name) - else: - files_in_repo = model_info(args.pretrained_model_name_or_path, revision=args.revision).siblings - return any(file.rfilename == config_file_name for file in files_in_repo) - def tokenize_prompt(tokenizer, prompt, tokenizer_max_length=None): if tokenizer_max_length is not None: @@ -933,11 +924,13 @@ def main(args): args.pretrained_model_name_or_path, subfolder="text_encoder", revision=args.revision, variant=args.variant ) - if model_has_vae(args): + try: vae = AutoencoderKL.from_pretrained( args.pretrained_model_name_or_path, subfolder="vae", revision=args.revision, variant=args.variant ) - else: + except OSError: + # IF does not have a VAE so let's just set it to None + # We don't have to error out here vae = None unet = UNet2DConditionModel.from_pretrained( From 8a541a309c55896c0ba49016bd5d947a8e9c74f2 Mon Sep 17 00:00:00 2001 From: bssrdf Date: Fri, 12 Apr 2024 08:41:00 -0400 Subject: [PATCH 2/8] rerun make style && make quality --- examples/dreambooth/train_dreambooth.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/dreambooth/train_dreambooth.py b/examples/dreambooth/train_dreambooth.py index d9701c2437a7..ebc88185421a 100644 --- a/examples/dreambooth/train_dreambooth.py +++ b/examples/dreambooth/train_dreambooth.py @@ -758,7 +758,6 @@ def __getitem__(self, index): return example - def tokenize_prompt(tokenizer, prompt, tokenizer_max_length=None): if tokenizer_max_length is not None: max_length = tokenizer_max_length From 80e0acfa7b025faa143243e547cfde5e6ec6e6e8 Mon Sep 17 00:00:00 2001 From: bssrdf Date: Sat, 27 Apr 2024 21:57:41 -0400 Subject: [PATCH 3/8] bring back model_has_vae and add change \ to / in config_file_name on windows os to make match work --- examples/dreambooth/train_dreambooth.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/dreambooth/train_dreambooth.py b/examples/dreambooth/train_dreambooth.py index ebc88185421a..01cdaad2c25d 100644 --- a/examples/dreambooth/train_dreambooth.py +++ b/examples/dreambooth/train_dreambooth.py @@ -757,6 +757,16 @@ def __getitem__(self, index): example["index"] = index return example +def model_has_vae(args): + config_file_name = os.path.join("vae", AutoencoderKL.config_name) + if platform.system() == "Windows": + config_file_name = config_file_name.replace('\\', '/') + if os.path.isdir(args.pretrained_model_name_or_path): + config_file_name = os.path.join(args.pretrained_model_name_or_path, config_file_name) + return os.path.isfile(config_file_name) + else: + files_in_repo = model_info(args.pretrained_model_name_or_path, revision=args.revision).siblings + return any(file.rfilename == config_file_name for file in files_in_repo) def tokenize_prompt(tokenizer, prompt, tokenizer_max_length=None): if tokenizer_max_length is not None: @@ -923,13 +933,11 @@ def main(args): args.pretrained_model_name_or_path, subfolder="text_encoder", revision=args.revision, variant=args.variant ) - try: + if model_has_vae(args): vae = AutoencoderKL.from_pretrained( args.pretrained_model_name_or_path, subfolder="vae", revision=args.revision, variant=args.variant ) - except OSError: - # IF does not have a VAE so let's just set it to None - # We don't have to error out here + else: vae = None unet = UNet2DConditionModel.from_pretrained( From 6ad49e2952b9d1d0b822ced0b6c8d9d5c1211c51 Mon Sep 17 00:00:00 2001 From: bssrdf Date: Sat, 27 Apr 2024 22:00:05 -0400 Subject: [PATCH 4/8] add missing import platform --- examples/dreambooth/train_dreambooth.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/dreambooth/train_dreambooth.py b/examples/dreambooth/train_dreambooth.py index 01cdaad2c25d..67044abfa4f3 100644 --- a/examples/dreambooth/train_dreambooth.py +++ b/examples/dreambooth/train_dreambooth.py @@ -21,6 +21,7 @@ import logging import math import os +import platform import shutil import warnings from pathlib import Path From e2e3274bec2e0b806274e5ec8261b26fae2d2ad1 Mon Sep 17 00:00:00 2001 From: bssrdf Date: Sat, 27 Apr 2024 22:04:50 -0400 Subject: [PATCH 5/8] bring back import model_info --- examples/dreambooth/train_dreambooth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dreambooth/train_dreambooth.py b/examples/dreambooth/train_dreambooth.py index 67044abfa4f3..ef79f7a3e0d2 100644 --- a/examples/dreambooth/train_dreambooth.py +++ b/examples/dreambooth/train_dreambooth.py @@ -34,7 +34,7 @@ from accelerate import Accelerator from accelerate.logging import get_logger from accelerate.utils import ProjectConfiguration, set_seed -from huggingface_hub import create_repo, upload_folder +from huggingface_hub import create_repo, model_info, upload_folder from huggingface_hub.utils import insecure_hashlib from packaging import version from PIL import Image From 0f8ef799aeba32726e239a0a3001f39aa5c973c5 Mon Sep 17 00:00:00 2001 From: bssrdf Date: Sun, 28 Apr 2024 08:24:00 -0400 Subject: [PATCH 6/8] make config_file_name OS independent --- examples/dreambooth/train_dreambooth.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/dreambooth/train_dreambooth.py b/examples/dreambooth/train_dreambooth.py index ef79f7a3e0d2..edeec1887427 100644 --- a/examples/dreambooth/train_dreambooth.py +++ b/examples/dreambooth/train_dreambooth.py @@ -21,7 +21,6 @@ import logging import math import os -import platform import shutil import warnings from pathlib import Path @@ -759,9 +758,7 @@ def __getitem__(self, index): return example def model_has_vae(args): - config_file_name = os.path.join("vae", AutoencoderKL.config_name) - if platform.system() == "Windows": - config_file_name = config_file_name.replace('\\', '/') + config_file_name = f"vae/{AutoencoderKL.config_name}" if os.path.isdir(args.pretrained_model_name_or_path): config_file_name = os.path.join(args.pretrained_model_name_or_path, config_file_name) return os.path.isfile(config_file_name) From 1a9b15e82aa0753fc44cd5d52b969d03c2d9a171 Mon Sep 17 00:00:00 2001 From: bssrdf Date: Mon, 29 Apr 2024 08:56:41 -0400 Subject: [PATCH 7/8] switch to using Path.as_posix() to resolve OS dependence --- examples/dreambooth/train_dreambooth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dreambooth/train_dreambooth.py b/examples/dreambooth/train_dreambooth.py index edeec1887427..770ec79a4d93 100644 --- a/examples/dreambooth/train_dreambooth.py +++ b/examples/dreambooth/train_dreambooth.py @@ -758,7 +758,7 @@ def __getitem__(self, index): return example def model_has_vae(args): - config_file_name = f"vae/{AutoencoderKL.config_name}" + config_file_name = Path("vae", AutoencoderKL.config_name).as_posix() if os.path.isdir(args.pretrained_model_name_or_path): config_file_name = os.path.join(args.pretrained_model_name_or_path, config_file_name) return os.path.isfile(config_file_name) From c92f20ed7d2a552591708fda0d8f1be49a61ecbb Mon Sep 17 00:00:00 2001 From: bssrdf Date: Mon, 29 Apr 2024 09:25:07 -0400 Subject: [PATCH 8/8] improve style --- examples/dreambooth/train_dreambooth.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/dreambooth/train_dreambooth.py b/examples/dreambooth/train_dreambooth.py index 770ec79a4d93..103e3b5b10b8 100644 --- a/examples/dreambooth/train_dreambooth.py +++ b/examples/dreambooth/train_dreambooth.py @@ -757,6 +757,7 @@ def __getitem__(self, index): example["index"] = index return example + def model_has_vae(args): config_file_name = Path("vae", AutoencoderKL.config_name).as_posix() if os.path.isdir(args.pretrained_model_name_or_path): @@ -766,6 +767,7 @@ def model_has_vae(args): files_in_repo = model_info(args.pretrained_model_name_or_path, revision=args.revision).siblings return any(file.rfilename == config_file_name for file in files_in_repo) + def tokenize_prompt(tokenizer, prompt, tokenizer_max_length=None): if tokenizer_max_length is not None: max_length = tokenizer_max_length