Repository navigation
feat(index): add rabitqlib::SymqgIndex and build the Python binding on it - #244
Merged
Merged
Conversation
The workflow of the Python SymqgIndex was implemented in its binding, so a C++ caller had to rebuild it on symqg::QuantizedGraph and QGBuilder. SymqgIndex implements it in C++, as step 3 of #234: - SymqgIndex(dim, SymqgOptions), a static load, and train, add, search and save with the names, defaults and state errors of the Python class. The constructor checks the options, because the graph is only created when add knows the point count. - add builds the graph from every vector, once, into a new graph that replaces the empty state only after the build succeeds, so a failure leaves the index without a graph. A built or loaded index throws std::logic_error on add. - train only checks the coordinates of its input. - search takes a GraphSearchOptions, defaults ef_search to max(k, 64), raises a smaller value to k, rejects 0 and pads with kNoId and +inf. - seed(), ef_construction() and init() have no value for a file of a release up to 0.8. symqg::QGInitialization moves to the light header rabitqlib/index/symqg/types.hpp, which qg.hpp includes, so the public header does not include qg.hpp. The consumer header-boundary target now covers SymqgIndex. symqg::QuantizedGraph and QGBuilder are otherwise unchanged.
The binding drops the option checks, the graph construction, the default ef_search, the result padding and the state checks, which rabitqlib::SymqgIndex now implements. It keeps the NumPy conversions, shape checks, the checks whose messages name Python values (the init string, the seed and ef_search), the coordinate check of train, the k check before the outputs are allocated, the GIL release and the IndexAccess guards. python_ids, SearchResults and unfilled_search_results have no caller left and are removed. Results, messages and the Python suite are unchanged. A call with two faults may now report a different one first; the release notes list the cases.
This was referenced Nov 16, 2026
Open
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.
Part of #234 (step 3 of 4:
SymqgIndexand the SymphonyQG binding; steps 1 and 2 were #240 and #243).Summary
The workflow of the Python
SymqgIndexlived inpython_bindings/symqg_bindings.cpp. This addsrabitqlib::SymqgIndexinrabitqlib/index/symqg_index.hpp, which implements it in C++, and rebuilds the binding on it. It follows theIvfIndexandHnswIndexpattern from #240 and #243.SymqgIndex(dim, SymqgOptions), a staticload, andtrain,add,searchandsave, with the names, defaults and state errors of the Python class. The constructor checks the options, because the graph is only created whenaddknows the point count.addbuilds the graph from every vector, once. It builds into a newsymqg::QuantizedGraphand keeps it only afterQGBuilder::buildsucceeds, so a failedaddleaves the index without a graph. A built or loaded index throwsstd::logic_erroronadd.adduses every available CPU unlessnum_threadsis set, as Python does.trainonly checks the coordinates of its input; SymphonyQG has nothing to train.searchtakes theGraphSearchOptionsthatHnswIndexuses. Without anef_searchit usesmax(k, 64), a smaller value is raised tok, an explicit 0 throws, and missing results are padded withkNoIdand+inf.symqg::QuantizedGraphitself rejects anef_searchbelowk.seed(),ef_construction()andinit()have no value for a file of a release up to 0.8.symqg::QGInitializationmoves to a new light header,rabitqlib/index/symqg/types.hpp, whichqg.hppincludes, sosymqg_index.hppdoes not includeqg.hpp.symqg::QuantizedGraph,symqg::QGBuilderand the file formats are otherwise unchanged.The binding keeps the NumPy conversions and shape checks, the checks whose messages name Python values (the
initstring, the seed andef_search), the coordinate check oftrain, thekcheck before the outputs are allocated, the GIL release andIndexAccess.python_ids,SearchResultsandunfilled_search_resultsinbindings_common.hpphave no caller left and are removed.Behaviour changes
addon a built index: data that is not a 2-D array convertible to float32, or has the wrong dimension, raisesValueErrorinstead of theRuntimeErrorfor incremental addition.searchbeforeadd: a wrong dimension, an invalidk(0 or above 2**31) oref_search=0raisesValueErrorinstead ofRuntimeError.initis reported before an invaliddim,nbitsormax_degree, and an invalidseedbefore an invaliddim,nbits,max_degreeoref_construction.Notes for review
addon a built Python index andsearchbeforeaddnow convert the input and allocate their outputs before they refuse. This costs time and memory, and a failed allocation raisesMemoryErrorinstead ofRuntimeError: forsearch, outputs too large to allocate; foradd, the int64 ID array. On a built index, an input copy that fails for lack of memory raises theValueErrorfor data that cannot be converted. The release notes do not mention it.index_api_header_boundarynow also compiles a smoke test ofsymqg_index.hppand poisonsindex/symqg/qg.hppandqg_builder.hpp.__init__.pyiphrase "checked like the data of add" fortrainis unchanged.searchfails withstd::bad_alloc, its outputs may be partly written.IvfIndexandHnswIndex, there is no C++ test of concurrent searches or of loading a damaged file. The Python suite covers concurrent use.Tests
tests/unit/rabitqlib/index/symqg_index_test.cpp, 14 tests:addbuilding byte-identical files toQuantizedGraphandQGBuilderfor 3 settings;addonly once;add(too few points, a NaN) leaving no graph;add;trainonly checking its input, with the bad value in the last row;ef_search;kbounds;qg4_l2andqg0_ip, which have no seed or construction settings;tests/failures/construction_failure_test.cpp,FailedSymqgIndexAddKeepsNoGraph, for 1 and 4 threads and both initializations. It counts the allocations inside OpenMP regions of anadd, then makes the first, a middle and the last fail. The index must keep no graph and accept a lateradd. None of theSymqgIndexTestunit tests fails ifaddinstalls the graph beforebuild()succeeds; this test does.symqg_index.cpp, 34 mutants:build(), caught by the failure test on both thread counts, and atraincoordinate check overnum_pointsvalues instead ofnum_points * dim, added after the review.addorsearch. Results do not depend on it in these tests.SymqgIndexTest.*, and the staging mutant also againstconstruction_failure_test. Two (no null check intrain, no output check insearch) were caught by a crash rather than an assertion.Verification
Local, Linux x86-64, GCC 14,
RABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF:ctest: 606 tests, 605 passed, 1 skipped (ThreadCount.BoundParallelForUsesRuntimePlacementAndPropagatesErrors, which needs OpenMP thread binding).qg.hppfrom the SymqgIndex smoke test makesindex_api_header_boundaryfail.check-tidy.sh(clang-tidy 23),check-format.sh,mkdocs build --strict,git diff --check.__init__.pyiare unchanged.-fsyntax-only).Benchmark
Python
SymqgIndex, before (897c41b) and after, on the first 200,000 SIFT1M base vectors with 10,000 queries and exact top-10 ground truth computed for that subset. Defaults:nbits=32,max_degree=32,ef_construction=200, PiPNN; seed 1 (seed 2 for the 20k check below); k=10.Setup:
pip wheel, native optimization ON (-march=native), the same way for both. This is not the portable release configuration.add200k, 4 threadssearch10k, ef 64, 1 thread (recall@10 0.9674)search10k, ef 64, 4 threadssearch10k, ef 256, 1 thread (recall@10 0.99915)search10k, ef 256, 4 threadsCorrectness check: in each of the six runs, a 20,000-point single-threaded build (deterministic) saved byte-identical files and returned identical IDs and distances for 1,000 queries in both builds. Recall is identical between the builds in every row.
Three search rows were measured again on one saved 200k index, loaded by 5 interleaved processes per build with 3 searches each (15 per build, same cores, load average 42–44):
The ef 256, 4-thread median is lower after in both measurements (−5% and −18%), but individual processes run at one of two levels on this loaded host and the ranges overlap (before 29338–41827, after 21633–41624 QPS). The ef 64, 4-thread row moved in opposite directions (−0.01%, then +13%). The search loop is the same
QuantizedGraph::search_batch_with_efcall in both builds. I make no performance claim.