Skip to content

Commit 57e57cf

Browse files
Terikslinoytsabanyiyixuxu
authored
Store vae.config.scaling_factor to prevent missing attr reference (sdxl advanced dreambooth training script) (#12346)
Store vae.config.scaling_factor to prevent missing attr reference In sdxl advanced dreambooth training script vae.config.scaling_factor becomes inaccessible after: del vae when: --cache_latents, and no --validation_prompt Co-authored-by: Teriks <Teriks@users.noreply.github.com> Co-authored-by: Linoy Tsaban <57615435+linoytsaban@users.noreply.github.com> Co-authored-by: YiYi Xu <yixu310@gmail.com>
1 parent 6441694 commit 57e57cf

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

examples/advanced_diffusion_training/train_dreambooth_lora_sdxl_advanced.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,8 @@ def compute_text_embeddings(prompt, text_encoders, tokenizers, clip_skip):
19291929

19301930
if args.cache_latents:
19311931
latents_cache = []
1932+
# Store vae config before potential deletion
1933+
vae_scaling_factor = vae.config.scaling_factor
19321934
for batch in tqdm(train_dataloader, desc="Caching latents"):
19331935
with torch.no_grad():
19341936
batch["pixel_values"] = batch["pixel_values"].to(
@@ -1940,6 +1942,8 @@ def compute_text_embeddings(prompt, text_encoders, tokenizers, clip_skip):
19401942
del vae
19411943
if torch.cuda.is_available():
19421944
torch.cuda.empty_cache()
1945+
else:
1946+
vae_scaling_factor = vae.config.scaling_factor
19431947

19441948
# Scheduler and math around the number of training steps.
19451949
# Check the PR https://github.com/huggingface/diffusers/pull/8312 for detailed explanation.
@@ -2109,13 +2113,13 @@ def get_sigmas(timesteps, n_dim=4, dtype=torch.float32):
21092113
model_input = vae.encode(pixel_values).latent_dist.sample()
21102114

21112115
if latents_mean is None and latents_std is None:
2112-
model_input = model_input * vae.config.scaling_factor
2116+
model_input = model_input * vae_scaling_factor
21132117
if args.pretrained_vae_model_name_or_path is None:
21142118
model_input = model_input.to(weight_dtype)
21152119
else:
21162120
latents_mean = latents_mean.to(device=model_input.device, dtype=model_input.dtype)
21172121
latents_std = latents_std.to(device=model_input.device, dtype=model_input.dtype)
2118-
model_input = (model_input - latents_mean) * vae.config.scaling_factor / latents_std
2122+
model_input = (model_input - latents_mean) * vae_scaling_factor / latents_std
21192123
model_input = model_input.to(dtype=weight_dtype)
21202124

21212125
# Sample noise that we'll add to the latents

0 commit comments

Comments
 (0)