Skip to content

feat(index): add rabitqlib::SymqgIndex and build the Python binding on it - #244

Merged
gouyt13 merged 2 commits into
dev/0.9from
feat/symqg-index
Nov 16, 2026
Merged

gouyt13 merged 2 commits into
dev/0.9from
feat/symqg-index

Conversation

@gouyt13

@gouyt13 gouyt13 commented Nov 16, 2026

Copy link
Copy Markdown
Member

Part of #234 (step 3 of 4: SymqgIndex and the SymphonyQG binding; steps 1 and 2 were #240 and #243).

Summary

The workflow of the Python SymqgIndex lived in python_bindings/symqg_bindings.cpp. This adds rabitqlib::SymqgIndex in rabitqlib/index/symqg_index.hpp, which implements it in C++, and rebuilds the binding on it. It follows the IvfIndex and HnswIndex pattern from #240 and #243.

  • 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. It builds into a new symqg::QuantizedGraph and keeps it only after QGBuilder::build succeeds, so a failed add leaves the index without a graph. A built or loaded index throws std::logic_error on add.
  • add uses every available CPU unless num_threads is set, as Python does.
  • train only checks the coordinates of its input; SymphonyQG has nothing to train.
  • search takes the GraphSearchOptions that HnswIndex uses. Without an ef_search it uses max(k, 64), a smaller value is raised to k, an explicit 0 throws, and missing results are padded with kNoId and +inf. symqg::QuantizedGraph itself rejects an ef_search below k.
  • seed(), ef_construction() and init() have no value for a file of a release up to 0.8.
  • symqg::QGInitialization moves to a new light header, rabitqlib/index/symqg/types.hpp, which qg.hpp includes, so symqg_index.hpp does not include qg.hpp.
  • symqg::QuantizedGraph, symqg::QGBuilder and the file formats are otherwise unchanged.

The binding keeps the NumPy conversions and 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 IndexAccess. python_ids, SearchResults and unfilled_search_results in bindings_common.hpp have no caller left and are removed.

Behaviour changes

  • Results and the messages of single-fault calls do not change, apart from the allocation failures in the notes below. The Python suite passes without edits.
  • A call with two faults may report a different one first. The release notes state this rule with examples:
    • add on a built index: data that is not a 2-D array convertible to float32, or has the wrong dimension, raises ValueError instead of the RuntimeError for incremental addition.
    • search before add: a wrong dimension, an invalid k (0 or above 2**31) or ef_search=0 raises ValueError instead of RuntimeError.
    • The constructor: an invalid init is reported before an invalid dim, nbits or max_degree, and an invalid seed before an invalid dim, nbits, max_degree or ef_construction.

Notes for review

  • add on a built Python index and search before add now convert the input and allocate their outputs before they refuse. This costs time and memory, and a failed allocation raises MemoryError instead of RuntimeError: for search, outputs too large to allocate; for add, the int64 ID array. On a built index, an input copy that fails for lack of memory raises the ValueError for data that cannot be converted. The release notes do not mention it.
  • The consumer target index_api_header_boundary now also compiles a smoke test of symqg_index.hpp and poisons index/symqg/qg.hpp and qg_builder.hpp.
  • The docstring and __init__.pyi phrase "checked like the data of add" for train is unchanged.
  • If search fails with std::bad_alloc, its outputs may be partly written.
  • As for IvfIndex and HnswIndex, there is no C++ test of concurrent searches or of loading a damaged file. The Python suite covers concurrent use.
  • No Python test covers the reordered two-fault cases, as in feat(index): add rabitqlib::HnswIndex and build the Python binding on it #243.

Tests

  • New tests/unit/rabitqlib/index/symqg_index_test.cpp, 14 tests:
    • defaults;
    • constructor errors and their order;
    • drawn seeds;
    • add building byte-identical files to QuantizedGraph and QGBuilder for 3 settings;
    • add only once;
    • a failed add (too few points, a NaN) leaving no graph;
    • operations before add;
    • train only checking its input, with the bad value in the last row;
    • the default and raised ef_search;
    • zero breadth and k bounds;
    • padding;
    • save and load, and loading the 0.8.3 fixtures qg4_l2 and qg0_ip, which have no seed or construction settings;
    • moves.
  • A new test in tests/failures/construction_failure_test.cpp, FailedSymqgIndexAddKeepsNoGraph, for 1 and 4 threads and both initializations. It counts the allocations inside OpenMP regions of an add, then makes the first, a middle and the last fail. The index must keep no graph and accept a later add. None of the SymqgIndexTest unit tests fails if add installs the graph before build() succeeds; this test does.
  • Mutation checks on symqg_index.cpp, 34 mutants:
    • 32 are caught. This includes keeping the graph before build(), caught by the failure test on both thread counts, and a train coordinate check over num_points values instead of num_points * dim, added after the review.
    • The 2 survivors ignore the thread count of add or search. Results do not depend on it in these tests.
    • Mutants were run against SymqgIndexTest.*, and the staging mutant also against construction_failure_test. Two (no null check in train, no output check in search) 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).
  • Python suite with the branch installed: 1196 passed, 2 skipped (the macOS and Linux ARM64 wheel tests).
  • Installed consumer: 4 passed. Including qg.hpp from the SymqgIndex smoke test makes index_api_header_boundary fail.
  • Full check-tidy.sh (clang-tidy 23), check-format.sh, mkdocs build --strict, git diff --check.
  • The editor and type checks were not run: the Python signatures and __init__.pyi are unchanged.
  • The first commit alone keeps the old binding compiling against its headers (checked with -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:

  • Build: GCC 14.3 Release wheels built with pip wheel, native optimization ON (-march=native), the same way for both. This is not the portable release configuration.
  • CPU and memory: Xeon Gold 6418H, 4 physical cores of NUMA node 0 (16, 17, 19, 21) that, with their SMT siblings, had no busy thread when I picked them, with memory on node 0. The host load average was 43–44 during these runs.
  • Runs: 3 interleaved runs per build, each a fresh process with one timed call per measurement and no warm-up.
Measurement Before: runs, median After: runs, median
add 200k, 4 threads 10.54 / 10.78 / 10.66 s, 10.66 10.58 / 10.68 / 11.75 s, 10.68
search 10k, ef 64, 1 thread (recall@10 0.9674) 39280 / 38637 / 38943 QPS, 38943 38711 / 37992 / 38944 QPS, 38711
search 10k, ef 64, 4 threads 155043 / 152064 / 150786 QPS, 152064 152044 / 152737 / 142567 QPS, 152044
search 10k, ef 256, 1 thread (recall@10 0.99915) 11264 / 11125 / 11150 QPS, 11150 11133 / 11069 / 11047 QPS, 11069
search 10k, ef 256, 4 threads 44796 / 44086 / 44351 QPS, 44351 44161 / 41959 / 41174 QPS, 41959
2000 single queries, ef 64: p50 28.0 / 28.1 / 27.6 µs 27.7 / 27.7 / 28.0 µs
same: p99 37.5 / 36.3 / 35.3 µs 36.0 / 36.0 / 37.4 µs

Correctness 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):

  • ef 256, 1 thread: before median 11244 QPS (10767–11289), after 11254 (10756–11283).
  • ef 256, 4 threads: before 39791 (29338–41827), after 32691 (21633–41624).
  • ef 64, 4 threads: before 107644 (98104–145745), after 121758 (107613–145585).

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_ef call in both builds. I make no performance claim.

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.
@github-actions github-actions Bot added the release:added Release category managed from the PR title label Nov 16, 2026
@gouyt13
gouyt13 merged commit 2ea3b22 into dev/0.9 Nov 16, 2026
35 checks passed
@gouyt13
gouyt13 deleted the feat/symqg-index branch November 16, 2026 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:added Release category managed from the PR title

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant