Skip to content

Fix PQ build slowdown and the Panama "slow mode" - #850

Open
tlwillke wants to merge 6 commits into
mainfrom
native-pq-pair-direct
Open

tlwillke wants to merge 6 commits into
mainfrom
native-pq-pair-direct

Conversation

@tlwillke

@tlwillke tlwillke commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #845 (native PQ builds about 1.8x slower since #836).

The regression in #845 and the Panama "slow mode" have the same root: vector work split into many small calls. Natively, each subspace of a PQ code pair was its own call. In Panama, a value that crosses a call C2 doesn't inline is put on the heap, so the same build or search ran up to about 2x slower in one JVM than in the next. This PR does each job in one call, with the native and Panama providers sharing one design: the same entry points, layouts and thresholds, with only the language of the inner loop differing.

Themes

1. PQ code pairs in one call (the #845 fix). Both providers score a PQ code pair from a copy of the codebooks padded with zeros, so odd and mixed subspace widths run like even ones: one native call per pair, or one allocation-free Java loop. Subspaces of 16 floats or more use a centroid-pair table, kept with the PQ vectors being built and counted in their ramBytesUsed(), so a model shared with searchers does not hold it. Saved and in-progress codes both use it.

2. One call per job. PQ query tables are built in one call per query from transposed codebooks, with the query centered inside the kernel, on both providers. ASH on Panama gets the same treatment that native has: whole fused neighborhoods (nibble codes and INT8 field blocks), all rows of a symmetric comparison, and a whole batch of queued query projections in one call each, plus INT8 and FP16 query projection on request.

3. No Panama slow mode. Panama kernels are written so C2 keeps their vectors in registers however a JVM compiles them: each body does its whole job and is compiled on its own (above the 325-bytecode inline limit), never takes or returns a vector, names its species directly, calls lanewise at its own call sites (the JDK's fma/add wrappers share one type profile across all callers), and states a vector's exact class where a store or reduction needs it. Without that, C2 guessed the class from a profile and in some JVMs lost it for one accumulator, so the PQ query table boxed in about one JVM in four.

4. Guards. PanamaNoBoxingTest checks that no Panama method takes or returns vectors and that the bulk bodies stay above the inline limit; an opt-in PanamaAllocationProbe (-Djvector.test.allocationProbe=true) measures bytes per operation from callers 0-10 deep, alone or after every other case (-Dprobe.mixed=true); PanamaNativeParityTest checks the new Panama paths against independent oracles and, where the library is loaded, against native.

Results

On-disk PQ graph builds, Ice Lake (32 cores), JDK 22, graph insertion time in seconds, one fresh JVM per value (degree 32, beam 100, overflow 1.2, hierarchy, refinement, k=256, FP32 rerank). Recall@10 matched across builds.

Dataset, subspaces Panama: this PR / #833 / main Native: this PR / #833 / main
cohere-english-v3-1M, 512 x 2 floats 151.9 / 242.5 / 376.4 148.7 / 208.7 / 239.3
cohere-english-v3-1M, 128 x 8 58.9 / 76.0 / 82.1 41.8 / 45.1 / 90.1
cohere-english-v3-1M, 32 x 32 (pair table) 24.4 / 28.7 / 28.3 18.8 / 19.1 / 28.7
dpr-gemma-1m, 256 x 3 86.3 / 441.9 / 227.1 78.2 / 90.4 / 242.4
dpr-gemma-1m, 96 x 8 35.7 / 60.1 / 105.1 30.6 / 32.0 / 65.2
dpr-gemma-1m, 24 x 32 17.9 / 30.6 / 23.0 18.1 / 19.4 / 24.6

Against main, builds are 1.2-2.9x faster with Panama and 1.4-3.1x faster with native. #833's 441.9 s run was in the slow mode (2,029 young GCs). Two native first runs of this PR were outliers (24.1 s and 35.7 s) and are replaced by their first alternating rerun. These builds predate the final review fixes, which change no arithmetic.

ASH with Panama, mpnet 1M, 2-bit codes, GNR (48 cores), JDK 22, mean of 3 fresh query JVMs (warm index, cold queries):

main this PR
Build 18.8 s 17.1 s
QPS, 1 core, rerank 1x 7,680 11,453
QPS, 8 cores, rerank 1x 44,844 82,991
QPS, 8 cores, rerank 2x 30,973 62,722

The native provider remains faster for ASH, and the docs now recommend it: with the same build, Panama reached 60-65% of native's QPS on OpenAI 3072 1M (2-bit, 1 and 8 cores).

Known limits

  • In a JVM that has already run many other vector shapes ("mixed" mode in the probe), three ASH Panama paths still allocate: the nibble-code neighborhood kernel, the canonical LUT dot and the FP16 query view. A fresh JVM per index shape does not.
  • Panama INT8 ASH neighborhoods (3 and 5-9 bits) are several times slower than native: the Vector API has no 8-bit dot-product or multi-shift operation (JDK-8377386 proposes one).
  • With Panama, the first encoding burst of a build runs partly in C1 before C2 compiles the batch projection kernel.
  • Some kernels still allocate per call with Panama, exactly as much as on main: exact cosine at some dimensions, Hamming distance, the element-wise utilities, the ASH dot row and the 1-bit symmetric dot. Prevent temporary SIMD allocations in Panama vector kernels #755 addresses all but exact cosine, which is still open.

Tests

Full jvector-native and jvector-tests suites on Ice Lake (8 cores), JDK 22 and JDK 25, native and Panama providers, compared with main on the same machine: no new failures. The remaining failures also fail on main (provider-selection tests that see the harness's JDK_JAVA_OPTIONS, and JDK 25 Panama ASH tests that fail at random). After the review fixes, the PQ, ASH, parity and guard tests (243) ran again on JDK 22 and 25 with both providers: no new failures. The allocation probe ran every case in four fresh JVMs: every PQ query-table and code-pair case allocated nothing in every JVM. Parity tests also ran on GNR (48 cores).

Release note

docs/release notes/4.1.0-rc.2/850.performance.md

Native scores code pairs from zero-padded codebooks in one loop, or from a
centroid-pair table when every subspace is at least 16 floats. Fixes #845.
Also corrects the #836 note: native keeps the pair table for wide subspaces.
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Before you submit for review:

  • Does your PR follow guidelines from CONTRIBUTIONS.md?
  • Did you summarize what this PR does clearly and concisely?
  • Did you include performance data for changes which may be performance impacting?
  • Did you include useful docs for any user-facing changes or features?
  • Did you include useful javadocs for developer oriented changes, explaining new concepts or key changes?
  • Did you rebase your branch onto the latest main for regression testing and PR submission?
  • Did you trigger regression testing via Run Bench Main and review results?
  • Did you adhere to the code formatting guidelines (TBD)
  • Did you group your changes for easy review, providing meaningful descriptions for each commit?
  • Did you ensure that all files contain the correct copyright header?
  • Did you add documentation for this feature to the release notes directory?

If you did not complete any of these, then please explain below.

…e call

Code pairs score from padded codebooks or a centroid-pair table; tables are counted in PQVectors.ramBytesUsed().
@tlwillke tlwillke self-assigned this Nov 28, 2026
Each kernel does its job in one body, takes no vectors and states a vector's exact class where a store needs it.
New C kernels and bindings; argument checks are overflow-safe.
No-boxing and parity tests, allocation probe, kernel benchmarks, release notes; ASH docs recommend the native provider.
@tlwillke tlwillke changed the title Score PQ code pairs in one native call for every subspace width Score PQ and ASH in one call per job on native and Panama Nov 28, 2026
@tlwillke tlwillke changed the title Score PQ and ASH in one call per job on native and Panama Fix PQ build slowdown (#845) and the Panama slow mode Nov 28, 2026
@tlwillke tlwillke changed the title Fix PQ build slowdown (#845) and the Panama slow mode Fix PQ build slowdown and the Panama "slow mode" Nov 28, 2026
@tlwillke
tlwillke marked this pull request as ready for review November 28, 2026 07:00
@tlwillke tlwillke added bug Something isn't working performance improvement A contribution that aims to improve library performance, possibly along with functionality. 4.1.0-rc.2 labels Nov 28, 2026
@r-devulap

Copy link
Copy Markdown
Contributor

Here are results of BenchYAML (on disk, immutable graph index construction) results and it addresses the regression caused by #836

graph_insertion

@r-devulap r-devulap left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The part that fixes the #836 regression with a hybrid approach looks good to me, but the PR does much more than that, and I haven't yet reviewed the other performance changes it brings. I wonder if we should split it into multiple PRs. Right now it does too many things (PQ index builds, a rewrite of the Panama kernels, and new ASH kernels), which makes it hard to review and hard to bisect if something regresses.

This branch has not been deployed

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

Labels

4.1.0-rc.2 bug Something isn't working performance improvement A contribution that aims to improve library performance, possibly along with functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native PQ index build ~1.8x slower since #836 (wide subspaces, e.g. cohere-1M)

2 participants