Skip to content

perf(ivf): review blocked batch routing for upstream #265 - #2

Draft
ColinLeeo wants to merge 1 commit into
review/issue-218-basefrom
perf/ivf-batched-flat-routing
Draft

ColinLeeo wants to merge 1 commit into
review/issue-218-basefrom
perf/ivf-batched-flat-routing

Conversation

@ColinLeeo

@ColinLeeo ColinLeeo commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Summary

Review mirror of VectorDB-NTU#265, which addresses upstream issue VectorDB-NTU#218. This PR is for independent Copilot review; the implementation is proposed for merging upstream.

The review uses exactly the upstream PR's base and head:

  • Base: 9121a79ae0f5e23d3c5f5aef2431e872b89afd92
  • Head: a41d52dd5791414fa450ce361efd99d2517863ca
  • Diff: 10 files, 612 additions, 4 deletions.

Flat IVF batch routing uses blocked GEMM screening and a top-nprobe heap, then recomputes retained candidates with the existing float kernels. It computes centroid norms once per call, streams reusable worker scratch, limits GEMM to one thread per worker, and orders both paths by returned float distance then centroid ID. Small batches and large nprobe retain scalar routing. Cosine normalization and raw-vector reranking inputs are preserved.

Please pay particular attention to numerical screening bounds and rounded L2 ties, nested OpenMP/thread-setting behavior, cosine normalization, query/centroid block boundaries, removed-point padding, and size/overflow handling.

Testing

The upstream PR contains the full protocol, commands and performance tables. For this same commit on Apple M5 Pro / AppleClang 17 / Release / NEON / native optimization OFF:

  • C++: 629 passed, 14 skipped, 0 failed.
  • Python: 1188 passed, 42 skipped, 0 failed, using a freshly built wheel.
  • clang-format 23.1.5, Ruff 0.16.1, full clang-tidy 23.1.4 and a focused analysis of the new routing test passed.
  • GIST1M fingerprints and Recall@10 match the baseline in every measured configuration.
  • Batch 1000: about 1.09–1.70x single-thread speedup; nprobe64/four threads about 1.23x.
  • Single-query recheck: aggregate median about 1.2% slower, with mixed per-pair direction on a loaded host. Idle-host and x86 performance confirmation remain outstanding.

Relevant review findings will be checked against the implementation and summarized with links on upstream PR VectorDB-NTU#265.

Native x86 correctness follow-up (2026-11-24)

Verified the completed jobs in fork CI run 45357507822 on the exact PR commit a41d52dd5791414fa450ce361efd99d2517863ca:

Native x86 job Full CTest result Evidence
Linux GCC, portable Release, native optimization OFF 622 passed, 8 skipped, 0 failed (630 registered) Job log
Linux Clang 23, OpenMP ON, portable Release, native optimization OFF 629 passed, 1 skipped, 0 failed (630 registered) Job log
Windows x86-64, Visual Studio 2026, Release 617 passed, 9 skipped, 0 failed (626 registered) Job log

All three jobs executed and passed the new IvfFlatRoutingTest cases for tiled top-nprobe parity, float-distance ties, and streamed batch search with removed-ID padding. Skips are explicit platform/runtime/ISA limitations; they are not counted as passes. ASan/UBSan, the Clang OpenMP OFF core/FastScan checks, and benchmark smoke also passed. OpenMP OFF does not build the IVF index.

The GCC host reports an AMD EPYC 7763 (x86-64). Its separate Intel SDE checks passed for AVX2-only and AVX-512 dispatch; those emulation checks are correctness evidence, not native performance measurements. These CI results address the native x86 runtime-validation gap raised by Copilot. Native x86 before/after GIST measurements are now reported in the performance follow-up. The single-query repeat is 1.5% slower, so the no-regression criterion remains unresolved and the PR remains a draft.

Native x86-64 measurements (2026-11-24)

Measured base 9121a79ae0f5e23d3c5f5aef2431e872b89afd92 against PR head a41d52dd5791414fa450ce361efd99d2517863ca on an AMD EPYC 9J14 KVM guest, Ubuntu 24.04.4, 32 vCPUs, approximately 62 GiB RAM, GCC 13.3.0, CMake 3.28.3, Release -O3 -DNDEBUG, C++17, OpenMP ON, native optimization OFF. The guest exposes AVX2 and AVX-512; the library retains its runtime-dispatched kernels. The baseline receives only this PR's identical standalone search benchmark driver and its CMake target, with no changes to its search implementation.

Before timing, the candidate's complete CTest run finished with 644 passed, 1 skipped, 0 failed (645 registered). The sole skip was ThreadCount.BoundParallelForUsesRuntimePlacementAndPropagatesErrors. The ISA symbol check also passed for 49 translation units. No build or other CPU-heavy guest workload ran concurrently with the benchmark.

Workload: GIST1M, 1,000,000 × 960, L2, 4096 Flat centroids, 5 bits, k=10, default high-accuracy scanning. Both revisions read the exact same prebuilt index used by the earlier ARM64 measurements (built at 2122ab2, sample 262144 without replacement using NumPy default_rng(42), seed 42, 10 iterations, 8 construction threads). The transferred index, query file and ground truth were SHA-256 verified. Index SHA-256: 897a89a212b75ff7db681cb55e632bc3568ee9022d7000955e7faf05e0784037.

Pin each process before loading with numactl --physcpubind=0 --membind=0 for one thread, or CPUs 0-3 for four threads; set OMP_PROC_BIND=close, OMP_PLACES=cores, OMP_DYNAMIC=FALSE and the matching OMP_NUM_THREADS. Use 1000 queries, alternating process order, one untimed warmup per process. Batch 1 and 8 use 7 pairs × 7 timed repetitions; other cases use 3 pairs × 3 repetitions. The 4095/4096-probe cases use the first 64 queries. Loading, warmup, hashing and recall are excluded from timing. Report the median over all timed repetitions and its reciprocal QPS. Every before/after comparison has identical ID/distance fingerprints and unchanged Recall@10.

Batch nprobe Threads Before µs/query After µs/query Before QPS After QPS Speedup Recall@10 (both)
1 64 1 615.35 613.45 1625 1630 1.003× 0.8832
8 64 1 606.02 621.05 1650 1610 0.976× 0.8832
64 64 1 603.25 573.76 1658 1743 1.051× 0.8832
1000 64 1 609.41 539.13 1641 1855 1.130× 0.8832
1000 16 1 383.05 308.22 2611 3244 1.243× 0.6598
1000 128 1 896.58 829.58 1115 1205 1.081× 0.9437
64 64 4 171.29 171.79 5838 5821 0.997× 0.8832
1000 64 4 169.88 144.17 5887 6936 1.178× 0.8832
64 4095 1 11407.90 11449.71 88 87 0.996× 0.9750
64 4096 1 11388.79 11449.48 88 87 0.995× 0.9750

The batch-8 control was 2.5% slower in the first sweep, including one unusually slow candidate process. Both controls were therefore repeated, retaining all original results, with the same CPU binding, binaries, data, and 7 pairs × 7 repetitions:

Batch nprobe Threads Before µs/query After µs/query Before QPS After QPS Speedup Recall@10 (both)
1 64 1 613.21 622.58 1631 1606 0.985× 0.8832
8 64 1 606.35 612.92 1649 1632 0.989× 0.8832

The single-query no-regression requirement is not yet established. Its first sweep was essentially flat (1.003×), but the repeat was 1.5% slower, with all seven paired medians slower (speedups 0.959×–0.993×, median 0.987×). Batch 8 was 2.5% slower initially and 1.1% slower on repeat. These control results need investigation; the batch throughput gains do not resolve them. At batch 1000 / nprobe 64, the measured throughput gain is 13.0% with one thread and 17.8% with four threads; nprobe 16 gains 24.3% at its separately reported recall.

Environment limit: this is native x86 instruction execution in a KVM guest, not a dedicated bare-metal result. Bound-vCPU steal time was at most 0.058% over the recorded comparison windows, but guest measurements cannot exclude physical-host cache/frequency or scheduling interference. CPU affinity and an otherwise idle guest improve repeatability without proving zero noise. The PR remains Draft for the control-latency investigation and human review.

x86 reproduction commands

Build both source snapshots with the same compiler/options; install the identical ivf_search.cpp benchmark driver and its target into the baseline only. Set BUILD_TESTS=OFF for the baseline and BUILD_TESTS=ON for the candidate. No Python binding or package installation is required for this C++ benchmark.

cmake -S "$SOURCE" -B "$BUILD" -G Ninja -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_CXX_COMPILER=g++ -DRABITQ_BUILD_SAMPLES=OFF \
  -DRABITQ_BUILD_BENCHMARKS=ON -DRABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF \
  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
  -DRABITQ_BUILD_TESTS="$BUILD_TESTS" -DBUILD_GMOCK=OFF
cmake --build "$BUILD" --parallel 8
OMP_NUM_THREADS=4 ctest --test-dir "$AFTER_BUILD" --no-tests=error --output-on-failure --parallel 4
# Run in the candidate repository root (the checker uses git rev-parse):
./scripts/check-isa-symbols.sh "$AFTER_BUILD"

export OMP_NUM_THREADS=1 OMP_PROC_BIND=close OMP_PLACES=cores OMP_DYNAMIC=FALSE
python3 benchmarks/hotpaths/compare.py \
  --before "numactl --physcpubind=0 --membind=0 $BEFORE gist.index gist_query.fvecs gist_groundtruth.ivecs 1 64 1 7 1000" \
  --after  "numactl --physcpubind=0 --membind=0 $AFTER gist.index gist_query.fvecs gist_groundtruth.ivecs 1 64 1 7 1000" \
  --pairs 7 --output x86-single-query \
  --notes "9121a79 vs a41d52d; EPYC 9J14 KVM; GCC 13.3 -O3 native OFF; CPU0/memory node0; GIST1M 4096 Flat, 5 bits, k10"

Use batch 8 with the same control settings; for other rows use three pairs and three repetitions. Four-thread rows use OMP_NUM_THREADS=4 and --physcpubind=0-3. Near-all-cluster rows use 64 queries. Repeat controls into a fresh output directory. Raw CSV/stdout/stderr, paired medians, CPU counters, exact compile commands, build/CTest/ISA logs and scripts have been retained.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

NEON-only validation leaves native x86 correctness/performance and idle-host single-query latency unconfirmed, requiring human review against issue VectorDB-NTU#218’s no-regression requirement.

No open issues 🎉

What changed in this PR

This PR optimizes flat IVF batch routing with blocked matrix multiplication and top-nprobe selection, while retaining existing float-distance kernels and scalar fallbacks.

Changes:

  • Adds blocked centroid screening with reusable worker buffers and per-worker thread limits.
  • Shares search logic between routing paths and makes distance ties deterministic by centroid ID.
  • Adds C++ and Python regression coverage plus an external-data search benchmark.
File Description
tests/​unit/​rabitqlib/​index/​flat_routing_test.cpp Tests routing equivalence, rounded ties, block boundaries, threading, and removed-result padding.
tests/​python/​test_ivf.py Tests batch equivalence across metrics, storage widths, persistence, and thread counts.
src/​index/​ivf.cpp Integrates blocked batch routing and extracts shared preassigned search logic.
include/​rabitqlib/​index/​ivf/​ivf.hpp Declares the preassigned search helper.
include/​rabitqlib/​index/​ivf/​initializer.hpp Applies deterministic distance-then-ID ordering to scalar routing.
include/​rabitqlib/​index/​detail/​candidate.hpp Defines the shared candidate comparator.
include/​rabitqlib/​clustering/​detail/​kmeans.hpp Adds blocked top-nprobe screening with float-distance recomputation.
benchmarks/​hotpaths/​README.md Documents the search benchmark and comparison protocol.
benchmarks/​hotpaths/​ivf_search.cpp Adds batched-search timing, result fingerprints, and Recall@10 reporting.
benchmarks/​hotpaths/​CMakeLists.txt Registers the IVF search benchmark target.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants