Repository navigation
feat(python): let IvfIndex.set_centroids take num_threads - #231
Merged
Merged
Conversation
IvfIndex.set_centroids takes a keyword-only num_threads for rotating the centroids and building the routing, as train and add do. Only the HNSW routing graph varies with it; one thread and a fixed seed build a reproducible graph. The 0.7 build passed its thread count through, but the 0.8 split into set_centroids and add lost it. Without the argument, set_centroids used the C++ default, which runs on every CPU and ignores OMP_NUM_THREADS, for every initializer. None and 0 now select the available CPUs capped by OMP_NUM_THREADS, like every other default. That is what processes sharing OpenMP runtimes on macOS rely on. Tests check that a single-thread set_centroids gives identical files and that OMP_NUM_THREADS=1 caps the default. The routing tests that #230 loosened build with explicit thread counts again: the 128-centroid test is seeded and exact once more, and the parallel test's thread count applies to the routing build. The migration guide notes that IVF HNSW routing is no longer single-threaded by default.
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.
IvfIndex.set_centroidsnow takes a keyword-onlynum_threads, astrainandadddo. The C++IVF::set_centroidsalready took it; the binding did not pass it through. The 0.7buildpassed its thread count to the routing build, and the 0.8 split intoset_centroidsplusaddlost it. #230 loosened two routing tests because of that, and this PR restores them.Changes
num_threadsonIvfIndex.set_centroids. It sets the workers that rotate the centroids and build the routing. Only the HNSW routing graph varies with it: one thread and a fixedseedgive the same graph on every run.HnswIndex.set_centroidsis unchanged; it rotates serially.OMP_NUM_THREADS. Without the argument, the binding used the C++ default (SIZE_MAX), which starts an OpenMP team of every CPU and ignoresOMP_NUM_THREADS. That happened for every initializer, not just HNSW.Noneand0now select the available CPUs capped byOMP_NUM_THREADS, like every other Python default. Processes that share OpenMP runtimes on macOS rely on that cap (v0.8.0 causing segfault 11 on macOS #207). The C++ default is unchanged.__init__.pyi,index/ivf.mdandindex/usage.mddescribe the argument. The migration guide notes that 0.7buildbuilt IVF HNSW routing with one thread by default, and that it is multithreaded now unlessnum_threads=1is passed (marked as 0.8.5).Tests
set_centroidscalls with the same seed give identical saved files, including the.hnswsidecar.OMP_NUM_THREADS=1, two defaultset_centroidscalls over 1,024 centroids give identical files. Before this change, every 2-, 4-thread or unlimited build differed (10 of 10 trials each), so the test guards the cap on any runner with at least 2 CPUs.test_hnsw_centroid_routing_roundtrip(128 centroids) is seeded, builds the routing with one thread, and checks exact matches again instead of aggregate self-recall.test_hnsw_parallel_build_routes_and_reloadspasses itsthreadstoset_centroids, so the parallel routing build is what it tests.Checks
On Linux x86-64, Xeon Gold 6418H, GCC 14.3, Release, native optimization off; the package was built from this branch.
check-tidy.shis clean.test_ivf.py,test_ivf_initializers.pyandtest_thread_counts.pyunder-W error::DeprecationWarning: 185 passed;check-python.sh,check-format.sh,mkdocs build --strictandgit diff --checkpass.Release notes
Main has no unreleased notes page, so for the next release:
IvfIndex.set_centroids(..., num_threads=None).IvfIndex.set_centroidswithoutnum_threadsused every CPU and ignoredOMP_NUM_THREADS; it now follows the same default astrainandadd.