Skip to content

fix(samples): accept cosine and reject unknown metrics in the indexing examples - #250

Merged
gouyt13 merged 2 commits into
dev/0.9from
fix/docs-samples-09
Nov 19, 2026
Merged

gouyt13 merged 2 commits into
dev/0.9from
fix/docs-samples-09

Conversation

@gouyt13

@gouyt13 gouyt13 commented Nov 19, 2026

Copy link
Copy Markdown
Member

Summary

Examples. ivf_rabitq_indexing, hnsw_rabitq_indexing and symqg_indexing built an L2 index for every metric argument other than ip/IP, including cosine, although the index options accept METRIC_COSINE. They now accept l2, ip and cosine (also uppercase) and throw Metric type must be l2, ip or cosine for anything else, before reading any file. The printed label adds Cosine; L2 and IP are unchanged. tests/integration/cpp_example_index_check.cpp learns cosine.

Docs on dev/0.9, corrected against the code:

  • Cosine is listed where pages named only L2 and inner product (index.md, the IVF routing paragraph, rabitq/rabitq.md). Every IVF routing choice works with cosine, which the index stores as inner product.
  • "Updates preserve the file format" (IVF, HNSW) becomes "save always writes index format 2". The HNSW removal-compatibility paragraph is limited to the pre-0.9 layout and names 0.5.2, the release that added removal.
  • HNSW set_centroids takes no num_threads. SymphonyQG search with num_threads=0 is capped by OMP_NUM_THREADS and the number of queries.
  • The IVF, HNSW and SymphonyQG parameter tables give k as 1 to 2^31, the range the Python bindings and the C++ classes enforce.
  • README: HNSW lists reserve instead of the removed resize. The quick-start migration pointer goes to "From 0.8 to 0.9", and "Further reading" adds the Python migration guide.
  • modular_migration.md: the max_elements sentence is in the past tense.
  • The release notes mention the example change.

Found during a pre-release check of dev/0.9.

Testing

Local, Linux x86-64, GCC 14, Release, RABITQ_BUILD_SAMPLES=ON, RABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF:

  • RABITQ_REQUIRE_CPP_EXAMPLES=1 python -m pytest tests/python/test_cpp_clustering_examples.py: 23 passed. New cases: IVF and HNSW examples build cosine indexes that the check helper verifies (metric, counts, self-retrieval), and all three indexing examples reject dot without creating the index file.
  • Against the unchanged dev/0.9 examples, with the new helper and tests, the 5 new cases fail and the other 18 pass.
  • ./scripts/check-format.sh, ./scripts/check-python.sh, mkdocs build --strict, git diff --check.
  • Reviewer probes: k = 2**31 is accepted and 0 or 2**31 + 1 raise ValueError on all three Python classes; a cosine IvfIndex finds each point itself with auto, flat, flat_rabitq and hnsw routing.

clang-tidy was not run: CI excludes examples and tests from it, and no library or binding code changes.

…g examples

ivf_rabitq_indexing, hnsw_rabitq_indexing and symqg_indexing built an L2
index for every metric argument other than "ip", including "cosine". They
now accept l2, ip and cosine (also uppercase) and reject anything else
before reading any file. The index check helper learns "cosine", and the
example tests build and check cosine indexes and cover the rejection.
Mention cosine where pages listed only L2 and inner product, replace
statements that updates keep the file format (every save writes index
format 2), note that HNSW set_centroids takes no num_threads, add the
OMP_NUM_THREADS cap to SymphonyQG search threads and the 1..2^31 range of
k to the parameter tables. In the README, HNSW lists reserve instead of
the removed resize, and the migration links point to the 0.9 guide.
@github-actions github-actions Bot added the release:fixed Release category managed from the PR title label Nov 19, 2026
@gouyt13
gouyt13 merged commit 6f655bc into dev/0.9 Nov 19, 2026
26 checks passed
@gouyt13
gouyt13 deleted the fix/docs-samples-09 branch November 19, 2026 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fixed Release category managed from the PR title

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant