[feat] Add NoDuplicatesFastBatchSampler #3611
Open
+753
−27
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.
The current
NoDuplicatesBatchSamplercan become significantly slow when working with datasets that have many duplicate values acrossquery / positive / negativescolumns, especially with large batch sizes (e.g., bs=8192). This is particularly noticeable with triplet or hard negatives data.Summary of Changes
This PR adds
NoDuplicatesFastBatchSampler, which speeds up duplicate checking by pre-computing xxhash 64-bit values for each sample usingdatasets.map(). It maintains the same batch construction policy asNoDuplicatesBatchSampler(avoiding duplicates within a batch) while significantly improving performance.Since this approach increases memory usage, both options are provided:
NO_DUPLICATES: Existing sampler (memory-efficient)NO_DUPLICATES_FAST: New sampler (faster, but uses more memory)Benchmarks (MS MARCO)
Benchmarked using the following HuggingFace datasets:
sentence-transformers/msmarco-co-condenser-margin-mse-sym-mnrl-mean-v1/triplet-hardsentence-transformers/msmarco-co-condenser-margin-mse-sym-mnrl-mean-v1/triplet-50Conditions
128and8192num_proc=8--no-progress-bar)The table below summarizes execution time, memory usage, and batch counts. Memory is measured using USS (Unique Set Size). The fast sampler stores hash values as NumPy int64 arrays, which accounts for the increased memory usage. The original
NO_DUPLICATESchecks values on-the-fly and does not increase memory usage.Environment: Ryzen 9 7950 (
num_proc=8), Ubuntu 24Memory Considerations
This implementation stores hash values as
int64NumPy ndarrays, which increases memory usage compared to the currentNoDuplicatesBatchSampler.For reference, using
sentence-transformers/msmarco-co-condenser-margin-mse-sym-mnrl-mean-v1:triplet-50(503,302 rows): ~200MiB additional memorytriplet-hard(11,662,655 rows): ~314MiB additional memoryTherefore, users can choose between:
NO_DUPLICATES: Memory-efficient (existing)NO_DUPLICATES_FAST: Faster (new)How It Works
datasets.map()to retrieve all values fromquery / positive / negativescolumns__iter__, use hash arrays for fast duplicate checking while constructing batchesImplementation Notes
datasets.map(..., num_proc=N)for speed.Benchmark Commands
Feedback and suggestions are appreciated!