← Back to Pull requests

Score PQ diversity from vectors decoded once per prune #858

Closed
unknown closed this pull request without merging on Nov 30, 2026 10:01 UTC
View on GitHub
unknown unknown commented on Nov 29, 2026 05:46 UTC
Author

PQ graph builds spend most of their insertion time on diversity pruning, which scores each candidate neighbor against up to a few dozen neighbors already kept. Each score read both codes' centroids from the codebooks: two scattered loads per subspace, about 7 billion in a 1M-vector build. Their speed also depended on where the codebooks landed: if a codebook did not start on a 32-byte boundary, many 8-float loads spanned two cache lines. The same build therefore ran at one of two speeds from one JVM to the next (about 42 s or 49.5 s of insertion on Ice Lake (32 cores)). This PR decodes each vector once per prune and compares dense, aligned rows, so pruning reads the codebooks about 20x less often and no longer depends on where they land.

Themes

1. Decode once per prune. PQDiversityDecoder decodes each candidate and each kept neighbor once per prune into rows of floats, then scores rows against rows. A candidate's first comparison decodes it and scores it in the same pass. Dot product, cosine and Euclidean are covered, for immutable and mutable PQ vectors, when the widest subspace is 3 to 8 floats or every subspace is 12 floats. Mixed and odd widths (such as 8 and 7, or 4 and 3, as PQ sized to a byte budget often has) are decoded from #850's padded codebook copy, whose zeros add nothing to norms, dot products or distances. Other layouts keep the #850 pair kernel or pair table, unchanged.

2. Aligned rows on both providers. On JDK 22 and later, rows live in 64-byte-aligned off-heap memory, in Panama22VectorUtilSupport (jvector-native, no native code). The native provider extends it, and PanamaVectorizationProvider uses it whenever jvector-native is present, keeping its name. JDK 20 and 21 remain supported with heap rows.

3. Every vector width. The 8- and 12-float kernels use 256-bit vectors; hardware with 128-bit vectors, such as NEON and SVE on Graviton4, decodes them in 128-bit parts instead, so ARM gains too. The 4-float kernel (for 3- and 4-float layouts) and the 6-float kernel use 128-bit vectors everywhere. Row comparisons use vectors of at most 512 bits, since rows are padded to 64 floats.

4. Small API, scratch tied to the build. VectorUtilSupport gains pqDecodeSupported, pqDecodeScratch, pqDecode, pqDecodeAndCompare, pqScratchDot, pqScratchSquareDistance and pqScratchClear. BuildScoreProvider gains a no-op beginDiversityPrune(), which VamanaDiversityProvider calls once per prune; a provider that wraps another must pass it on. diversityFunctionFor is for graph construction: its function is used on the creating thread, within the current prune, until that thread's next call. Each building thread has one scratch area of about 0.25-1.5 MB and one reusable score function, so scoring allocates nothing. The scratch is shared by every index and layout the thread builds (a new layout reuses its memory and clears each new row's padding), held weakly by the thread and strongly by each build's decoder, so a thread that has exited drops out, and the decoders and scratch are released when the build is cleaned up or closed (releaseBuildCaches, from #850). Builds running at the same time may share a model but not one set of PQ vectors. A decoder keeps the padded codebook copy it started with, so overlapping builds on one model can hold one extra copy (about 1–5 MB) until they end. Per-index decoders are counted in ramBytesUsed(); the per-thread scratch is not.

5. Guards. The new bulk kernels are listed in PanamaNoBoxingTest. The decode-only kernels are not listed, because they are small enough to inline. An assert catches a score function used on another thread; it is free with assertions off and cost nothing measurable with them on. PanamaVectorizationProvider falls back silently only when the JDK 22 support is absent, and logs a warning for any other failure to load it.

Results

On-disk PQ graph builds of cohere-english-v3-1M (1,024 dimensions, 128 subspaces of 8 floats; degree 32, beam 100, overflow 1.2, hierarchy). All runs used JDK 22, numactl --interleave=all and one fresh JVM per build, in ABBA order. Complete construction is PQ training, encoding, graph build and index write. Recall@10 matched across builds.

GNR (48 cores):

| Provider | Graph insertion: #850 / this PR | Complete construction: #850 / this PR |
| --- | --- | --- |
| Native | 21.0 / 19.2 s | 31.8 / 29.2 s (-8%) |
| Panama | 23.1 / 18.4 s | 34.5 / 29.5 s (-15%) |

Ice Lake (32 cores), native: graph insertion fell from 49.3 s to 34.7-34.8 s, and complete construction from 68.2 s to 51.0-51.5 s (-25%).

Build times are also steadier. Six fresh JVMs each on GNR (48 cores), native:

| | Graph insertion | Complete construction |
| --- | --- | --- |
| #850 | 18.7-21.6 s | 28.8-32.6 s |
| This PR | 18.0-18.6 s | 27.6-29.7 s |

Before, each comparison read centroids from a heap codebook whose alignment changed from JVM to JVM, and a misaligned codebook split many loads across cache lines. Rows decoded once into 64-byte-aligned memory make the codebook reads about 20x rarer, so its placement hardly matters. On Ice Lake (32 cores), #850 builds landed at about 42 s or 49.5 s of insertion; six fresh JVMs of a prototype all took 33.4-34.3 s.

Other machines and settings, complete construction, #850 / this PR, mean of two fresh JVMs each in ABBA order (cohere-english-v3-1M unless noted):

| Machine and setting | Provider | #850 / this PR |
| --- | --- | --- |
| GNR (48 cores), JDK 25 | Native | 30.8 / 28.3 s (-8%) |
| GNR (48 cores), JDK 25 | Panama | 35.6 / 31.1 s (-13%) |
| GNR (48 cores), JDK 22, AVX2 only | Native | 31.9 / 29.0 s (-9%) |
| GNR (48 cores), JDK 22, AVX2 only | Panama | 38.8 / 30.3 s (-22%) |
| Graviton4 (16 cores), JDK 22, 8 floats | Native | 148.1 / 125.4 s (-15%) |
| Graviton4 (16 cores), JDK 22, 8 floats | Panama | 180.9 / 136.2 s (-25%) |
| Graviton4 (16 cores), JDK 22, mpnet-1m, 6 floats | Native | 167.1 / 117.9 s (-29%) |
| Graviton4 (16 cores), JDK 22, ada002-1M, 12 floats | Native | 171.0 / 158.2 s (-7%) |

AVX2 only means -XX:UseAVX=2 with JVECTOR_MAX_ISA=avx2. A streaming build with in-progress PQ codes (the #852 harness merged locally, refining the codebook after 128,000 vectors in one of its two cases) took 21.8 s of insertion instead of 24.1 s with native, and 19.7 s instead of 28.1 s with Panama, on GNR (48 cores), JDK 22. The x86 runs predate two later commits: the 128-bit parts, which x86 never selects (two GNR builds afterwards matched), and keeping the Panama provider's name, which selects the same support class.

PQ sized to a byte budget usually has mixed widths. On cohere-english-v3-1M at the code sizes of 2-bit and 4-bit ASH, Ice Lake (32 cores), JDK 22, mean of two fresh JVMs each in ABBA order:

| Subspaces | Provider | Graph insertion: #850 / this PR | Complete construction: #850 / this PR |
| --- | --- | --- | --- |
| 133 (8 and 7 floats) | Native | 51.2 / 37.0 s (-28%) | 71.0 / 55.2 s (-22%) |
| 261 (4 and 3 floats) | Native | 72.8 / 50.4 s (-31%) | 98.1 / 73.3 s (-25%) |
| 133 (8 and 7 floats) | Panama | 52.6 / 41.1 s (-22%) | 81.9 / 79.8 s |
| 261 (4 and 3 floats) | Panama | 91.3 / 50.0 s (-45%) | 139.2 / 102.7 s (-26%) |

With Panama, PQ training took 10-11 s in some JVMs and 33-63 s in others, on #850 and this PR alike; it accounts for the Panama construction spread. This PR does not change PQ training.

A prototype also covered other shapes and similarities; graph insertion in seconds, before / prototype, on Ice Lake (32 cores) unless noted:

| Dataset, subspaces, similarity | Insertion |
| --- | --- |
| dpr-gemma-1m, 6 floats, dot product | 70.5 / 34.5 |
| cohere-english-v3-1M, 8 floats, cosine | 78.1 / 39.4 |
| ada002-1M, 8 floats, Euclidean (centered PQ) | 60.5 / 47.5 |
| sift-1M, 16 subspaces, Euclidean | 18.0 / 15.0 |
| openai-1536-1m, 12 floats, dot product, GNR (48 cores) | 24.1 / 19.1 |

I also ran the new scorer alongside the old one on the same prunes. Of about 6.3 million prunes, 11-177 kept a different neighbor set, all because of near-ties within float rounding.

Layouts taking turns. On Cohere 1M (single thread, GNR), prunes that alternate between two layouts on one thread take 4.0 s per pass instead of 10.3 s, with 17 MB of off-heap memory instead of 30-39 GB; single-layout prunes are unchanged.

Known limits

  • When a candidate is rejected by its first comparison, scoring it costs more than before at 6- and 12-float subspaces, because the whole candidate is decoded. Builds still gain, since such rejections are a minority of evaluations; a very low maximum degree was not measured.
  • Tests

    TestPQDiversityDecoding checks decoded scores against the pair kernel for all three similarities at 4, 6, 8 and 12 floats and for mixed (8 and 7, 4 and 3) and odd (3, 7) widths, on immutable and mutable vectors of a centered model. It also covers prunes larger than the row limit, two vector sets used in turn on one thread, layouts taking turns on one thread (including padding left by another layout), a code changed between prunes, a score function used on another thread, and threads that exit while a decoder lives. Panama22DecodeTest checks aligned off-heap rows against heap rows, including odd subspace counts, and that the Panama provider uses the JDK 22 support. These tests pass with the native and Panama providers on JDK 22 and 25, and with Panama on JDK 20, on Ice Lake (32 cores) and Graviton4 (16 cores), as do TestVectorizationProviderSelection, TestVectorizationProvider, TestProductQuantization, TestCompressedVectors, NativeVectorUtilSupportTest, PanamaNoBoxingTest and the memory-accounting tests. They also pass on GNR (48 cores) with -XX:MaxVectorSize=16, which selects the 128-bit parts on x86.

    PQDiversityBenchmark.pruneScore times scoring as builds prune; pairScore now starts a prune per pair, the decode-once worst case.

    Release note

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

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: MarkWolters Review duration: 1694m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #858"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%