Repository navigation
feat(clustering): add opt-in max_rebuild_gap to QGEMeans - #267
Draft
TamimEhsan wants to merge 6 commits into
Draft
TamimEhsan wants to merge 6 commits into
TamimEhsan wants to merge 6 commits into
Conversation
…fresh and nsplit docs
…qualify revival docs
This branch has not been deployed
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
QGEMeans rebuilds its SymphonyQG centroid graph before every mini-batch. On Cohere 10M (k = 40,000) the rebuilds take about two thirds of training time. Late in training the centroids move little, so consecutive graphs are nearly the same: the share of each centroid's edges that survive from one batch's graph to the next rises from about 0.7 at the start to 0.95 at the end.
This PR adds
max_rebuild_gaptoQGEMeansParameters(Python:QGEMeans(..., max_rebuild_gap=...)), the largest number of mini-batches between graph rebuilds.1: rebuild before every mini-batch. Results are bit-identical tomain.max_rebuild_gapat the end.QGBuilder::refresh).API: one new field and keyword argument with a backward-compatible default. There are no saved-format or dependency changes.
Changes:
QGBuilder::refresh(const float*): re-encodes every row's neighbour codes for the new vertex positions and re-picks the entry point as a build does.reset; null and non-finite data raisestd::invalid_argument.std::logic_error.GraphScheduleandrebuild_gapinclustering/detail/emeans.hppdecide between rebuild and refresh before each step, and when revival runs. The e-meansAssignmentinterface gainsrefresh(centroids).QGEMeansvalidatesmax_rebuild_gap >= 1("QGEMeans max_rebuild_gap must be positive").__init__.pyi, docstrings anddocs/docs/clustering.mddocument the option and the schedule.Benchmark
Seed 42, Xeon Gold 6418H, GCC 14
-march=native, one run of each. The machine is shared, so times vary between runs.GIST1M, k = 4096, measured as in #261: one thread pinned to an idle physical core, exact WCSS (total), Recall@10 with 1% probes (40 clusters). The default row reproduces #261's QGEMeans quality exactly. All times here are 9–23% above #261's because of other users' load (load average 8–21), but the four runs share the same conditions.
max_rebuild_gap=1(default)max_rebuild_gap=8max_rebuild_gap=16The Cohere runs below use 16 threads pinned to one socket with
numactl, and the earlier protocol: WCSS per point from the trainer's labels, and Recall@100 at an equal number of vectors scanned (each query probes its nearest centroids until it has scanned N vectors; 1,000 queries).Cohere 10M (768-d, VectorDBBench
cohere_large_10m), k = 40000, 2 iterations:max_rebuild_gap=1(default)max_rebuild_gap=16Cohere 10M, k = 40000, 8 iterations:
max_rebuild_gap=1(default)max_rebuild_gap=16Design choices that were measured (prototype, GIST and Cohere, 2 passes):
Testing
RABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF):ctest --no-tests=error, 667 passed (1 skipped by design). New tests:QgConstructionTest.RefreshMovesVerticesAndKeepsEdges: edges kept; stored vectors replaced; built rows reproduced by refreshing with the original vectors; entry point matches a fresh build over the moved vectors. Runs with and without the build cache, at d 65 and 1025, L2 and IP, on 1 and 3 threads.QgConstructionTest.RefreshRejectsUnbuiltQuantizedAndInvalidInput.QGEMeansRecipeTest.RebuildGapGrowsLinearlyAfterTheFirstQuarterandQGEMeansRecipeTest.RevivalWaitsForTheNextRebuild.QGEMeansAssignmentTest.RefreshSearchesTheMovedCentroids: searches after a refresh return exact distances to the moved centroids.QGEMeansTest.SpacedRebuildsAreDeterministicAndKeepTheObjective: identical results on 1 and 4 threads, objective within 2% of gap 1.python -m pytest, 1255 passed, 4 skipped. New tests intests/python/test_qgemeans.pycover the default, the keyword andcpsetting, the error message, rejection of negative values, and determinism across thread counts.tests/python/test_editor_support.pyandtests/test_mypy.pypass;tests/python/typing/public_api.pyuses the new keyword../scripts/check-format.sh,./scripts/check-python.sh,python -m mkdocs build --strict --config-file docs/mkdocs.ymlandgit diff --checkare clean.docs/development.md) onsrc/index/qg_builder.cppandpython_bindings/kmeans_bindings.cpp: passed.Not run:
CXX=clang++-23 ./scripts/check-tidy.sh build-tidy): to do before merge.tests/test_pyright.py: pyright isn't installed in the test environment, and Node 24 is unavailable.