Repository navigation
perf(data): avoid repeated full-bucket scans in the sampler - #3243
Merged
Merged
Conversation
…ned index MultiAspectSampler.__iter__ rebuilt a bucket's unseen list for every batch by filtering the whole bucket through the occurrence counts. The cost grew with the bucket (about 0.66 s per batch on a 257k-image rank shard) and, because loading is synchronous, every other rank waited for it at the first collective. Late in an epoch only the largest buckets remain, so it hit almost every step. The iterator now keeps one index per bucket with the unseen occurrences in bucket order. Marking a filepath seen raises its occurrence count by one, which retires exactly its earliest unseen occurrence, so each consumed sample is removed from the index instead of rescanning. The list handed to random.sample is the same as before, so batches are unchanged; an index is rebuilt when its bucket's contents differ from the snapshot it was built from (the VAE cache filter pads a split shard back to its old length in place), when the seen dict is replaced, and after resume or an epoch reset. _get_unseen_images keeps recomputing for its other callers. Claude-Session: https://claude.ai/code/session_01Ai6TFdNTtZTBiGjCsZgyF3
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
Follow-up to #2897. The sampler rebuilds a bucket's unseen list for every batch, making sampling expensive for large buckets. This change maintains a per-bucket index and removes consumed occurrences while preserving duplicate handling and batch order.
Indices are rebuilt when bucket contents change, including same-length VAE-filter refills, and invalidated on state restore or epoch reset. Each lookup returns a copy to preserve iterator behavior. The index uses additional memory proportional to bucket contents; list comparison, copying, and deletion remain linear operations.
Verification
tests.test_sampler: 31/31 pass, covering duplicates, partial batches, bucket changes, and resume.f1cb3800c: the same checkpoint state, seed, and batch size produced identical batch and within-batch image order across 200 batches each for ranks 0 and 3.Mean sampler time per batch, including initial index construction:
These measurements cover sampler execution with image validation and conditioning mocked, while production was running on the same host.