Fix latent/caption mismatch in SD3 dreambooth lora trainer with cache_latents + shuffle - #14431
Open
lcheng321 wants to merge 1 commit into
Open
Conversation
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.
Fixes #14430
Root cause
Latents were cached keyed by dataloader step, but the dataloader has
shuffle=Trueand is traversed twice: once during caching, once during training. Each traversal produces a different sample order.latents_cache[step]returns whatever image happened to land at that step during caching, not the image in the current training batch. No exception is raised, the mismatch is silent.Fix
Cache latents keyed by stable dataset index instead of step. Index is generated in
DreamBoothDataset.__getitem__and passed throughcollate_fn.Also added a check that rejects
--cache_latentscombined with--with_prior_preservation, since class images are not indexed the same way and would hit the same bug through a different path.Scope
This PR only covers
train_dreambooth_lora_sd3.py. It does not touch the Qwen-Image trainer or #12124, which is a separate file with a similar but independent issue.Testing
1. Isolated repro, no model or GPU required
Dataset index used directly as the tensor value, so any mismatch between cached value and actual batch value is visible by eye. Same dataloader, same seed, two traversals.
Before fix, 4/4 batches mismatched:
step=0: latents_cache[0]=[2.0, 0.0] vs actual pixel_values=[0.0, 3.0] MISMATCH
step=1: latents_cache[1]=[5.0, 3.0] vs actual pixel_values=[7.0, 1.0] MISMATCH
step=2: latents_cache[2]=[1.0, 4.0] vs actual pixel_values=[6.0, 2.0] MISMATCH
step=3: latents_cache[3]=[7.0, 6.0] vs actual pixel_values=[5.0, 4.0] MISMATCH
After fix, 4/4 batches matched:
step=0: indices=[0, 3] cached=[0.0, 3.0] actual=[0.0, 3.0] OK
step=1: indices=[7, 1] cached=[7.0, 1.0] actual=[7.0, 1.0] OK
step=2: indices=[6, 2] cached=[6.0, 2.0] actual=[6.0, 2.0] OK
step=3: indices=[5, 4] cached=[5.0, 4.0] actual=[5.0, 4.0] OK
all_match: True
2. Existing test suite
pytest examples/dreambooth/test_dreambooth_lora_sd3.py examples/dreambooth/test_dreambooth_sd3.py -v
11 passed in 344.51s
All existing tests pass, including
test_dreambooth_lora_latent_caching.