Fix BucketBatchSampler cache alignment in DreamBooth scripts#13353
Open
azolotenkov wants to merge 2 commits intohuggingface:mainfrom
Open
Fix BucketBatchSampler cache alignment in DreamBooth scripts#13353azolotenkov wants to merge 2 commits intohuggingface:mainfrom
azolotenkov wants to merge 2 commits intohuggingface:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes cache misalignment in DreamBooth training examples by making BucketBatchSampler yield batches in a stable, precomputed order across __iter__() calls, preventing reshuffles that break step-indexed latent/prompt-embedding caches.
Changes:
- Removed
random.shuffle(self.batches)fromBucketBatchSampler.__iter__()in multiple DreamBooth example scripts. - Added an explanatory comment describing why batch order must remain stable for cache alignment.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| examples/dreambooth/train_dreambooth_lora_flux2.py | Keep precomputed batch order stable in BucketBatchSampler.__iter__() to avoid cache misalignment. |
| examples/dreambooth/train_dreambooth_lora_flux2_img2img.py | Same sampler iteration stabilization for cache alignment. |
| examples/dreambooth/train_dreambooth_lora_flux2_klein.py | Same sampler iteration stabilization for cache alignment. |
| examples/dreambooth/train_dreambooth_lora_flux2_klein_img2img.py | Same sampler iteration stabilization for cache alignment. |
| examples/dreambooth/train_dreambooth_lora_z_image.py | Same sampler iteration stabilization for cache alignment. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
Author
|
Updated based on review: the sampler now shuffles precomputed batches once at construction time, while keeping iteration order fixed across epochs for cache alignment. |
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.
What does this PR do?
This PR fixes a bug where
BucketBatchSamplerreshuffled precomputed batches on each__iter__()call.DreamBooth training scripts precompute latents and/or prompt embeddings and later consume them by dataloader step index, so changing batch order between the caching pass and the training pass can misalign the cached tensors with the current batch.
This PR removes the per-iteration
random.shuffle(self.batches)call fromBucketBatchSampler.__iter__()and instead shuffles the precomputed batch list once at sampler construction time, keeping the batch order fixed across epochs.A future rework may still be needed to support true epoch-wise reshuffling, since batch membership is currently fixed once at sampler construction time.
Applied to:
examples/dreambooth/train_dreambooth_lora_flux2.pyexamples/dreambooth/train_dreambooth_lora_flux2_img2img.pyexamples/dreambooth/train_dreambooth_lora_flux2_klein.pyexamples/dreambooth/train_dreambooth_lora_flux2_klein_img2img.pyexamples/dreambooth/train_dreambooth_lora_z_image.pyBefore submitting
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Training examples: @sayakpaul