fix(dreambooth): batch size mismatch with --with_prior_preservation in flux2 scripts#13307
Open
agarwalprakhar2511 wants to merge 1 commit intohuggingface:mainfrom
Conversation
…n flux2 scripts When `--with_prior_preservation` is enabled, `collate_fn` doubles the `prompts` list (instance + class), but `prompt_embeds` was already doubled via `torch.cat([instance, class])` during pre-computation. Using `len(prompts)` as the repeat count produces 2x too many embeddings, causing a shape mismatch against the latent batch. Fix: use `len(prompts) // 2` when prior preservation is active, so the repeat count matches the actual number of unique prompt groups rather than the doubled collated list. Applied to all three affected scripts: - train_dreambooth_lora_flux2_klein.py - train_dreambooth_lora_flux2.py - train_dreambooth_lora_flux2_klein_img2img.py Fixes huggingface#13292 Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
--with_prior_preservationis enabled in the Flux2 dreambooth LoRA training scripts, the prompt embedding repeat logic double-counts the batch size, producing a shape mismatch against the latent tensor.Root cause:
collate_fnappends class prompts to the instance prompts list (doublinglen(prompts)), butprompt_embedsis already doubled earlier viatorch.cat([instance_embeds, class_embeds]). Using the fulllen(prompts)as the repeat count produces4embeddings for2latents atbatch_size=1.Fix: Use
len(prompts) // 2whenargs.with_prior_preservationis active, so the repeat count matches the number of unique prompt groups rather than the doubled collated list.Applied to all three affected scripts:
examples/dreambooth/train_dreambooth_lora_flux2_klein.pyexamples/dreambooth/train_dreambooth_lora_flux2.pyexamples/dreambooth/train_dreambooth_lora_flux2_klein_img2img.pyTest plan
Dimension trace verified for all four scenarios:
batch_sizewith_prior_preservationlen(prompts)prompt_embedspre-repeatFixes #13292