Repository navigation
feat(distributions): add chunked nearest-neighbor search to DistanceMatrix and optimize distance calculation - #311
Open
SimpingOjou wants to merge 1 commit into
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.
Summary
This PR introduces a memory-efficient and faster chunked nearest-neighbor retrieval method (
argmin) toDistanceMatrix, and updatesContinuousIndex.searchto use it.When training continuous supervised CEBRA ($N > 10^6$ samples), CEBRA computes pairwise distances between all candidate dataset points ($N$ ) and batch query points ($B$ ). In the current implementation, $(N, B)$ float32 tensor at once. For large-scale continuous recordings (e.g., $N = 7.67 \times 10^6$ points with $B = 512$ ), this single matrix requires $\approx 14.6\text{ GiB}$ of contiguous VRAM, causing out-of-memory crashes (
conditional='time_delta'orconditional='delta_normal') on large datasets (e.g.,DistanceMatrix.__call__allocates an entiretorch.OutOfMemoryError) on 16–24 GB GPUs.By chunking candidate points along$N$ (default: 500,000 samples) and maintaining running minimums:
torch.einsumwith optimized in-place BLAS GEMM (torch.mm), speeding up CPU/GPU search.Changes
cebra/distributions/index.py:chunk_size: int = 500_000parameter toDistanceMatrix.__init__.DistanceMatrix.argmin(self, query, mask=None) -> torch.Tensorperforming chunk-wise running minimums without materializing the fullContinuousIndex.search(self, query)to delegate directly toself.dist_matrix.argmin(query).Test Results
Tested on simulated data with fixed seeds with CPU:


Ran the distribution test suite:
pytest tests/test_distributions.py # ======================== 26 passed, 4 skipped in 6.04s =========================