← Back to Pull requests

PQ training: center subvectors instead of copying the training set #808

Closed
unknown closed this pull request without merging on Nov 30, 2026 22:41 UTC
View on GitHub
unknown unknown commented on Nov 20, 2026 21:38 UTC
Author

Fixes #807.

What

ProductQuantization.compute and refine no longer build a centered copy of the training set. createCodebooks and extractSubvectors now take the global centroid, and each subvector has the matching slice of the centroid subtracted in the same pass that copies the slice (VectorUtil.sub(vector, offset, centroid, offset, size)), so the slice is copied and centered with one call into the vector provider.

  • Same values. Element-wise subtraction commutes with slicing, so k-means receives exactly the values it did before.

  • Caller data is never modified. VectorAccess.copySelected hands back the caller's own vector objects for unshared sources, which is why this does not center the training vectors in place.

  • Nothing else changes: public API, serialized format and the encodeTo path are untouched.
  • Results

    128,000 x 1024 Gaussian vectors, compute(ravv, 256, 256, true, UNWEIGHTED, pool, pool) then encodeAll, run with -XX:+ExitOnOutOfMemoryError (JDK 26, G1, macOS arm64, ListRandomAccessVectorValues, corpus 524 MB):

    | -Xmx | main | this branch |
    |---|---|---|
    | 700 MB | OOM | completes |
    | 850 MB | OOM | completes |
    | 1000 MB | OOM | completes |
    | 1200 MB | completes | completes |

    700 MB was the lowest I tried. Total allocation fell from 3,752 MB to 3,223 MB, and VectorUtil.sub allocation from 1,044 MB to 544 MB (the rest is encodeTo, unchanged).

    Speed, measured separately on an idle machine (AVX-512 Xeon, 4 physical cores, JDK 25, Panama provider, 128,000 x 1024-dimension vectors, 256 subspaces): the step that produces the k-means input takes 0.94 s on main and 0.93 s with this change; end to end, compute takes 26.4 s on main and 26.2 s with this change, with the peak heap down from 2.0 GB to 0.95 GB and the bytes allocated down from 2.5 GB to 2.0 GB. On the native provider the step is slower (2.06 s versus 1.07 s), because it makes one provider call per training vector per subspace (32.7M calls at that shape) instead of one per training vector; that is the price of not materializing the centered copy, and it is a small part of the training. See the comment on this PR for the full table.

    Quality check, 6,000 vectors with a non-zero mean, mean squared reconstruction error (k-means is seeded from ThreadLocalRandom, so runs differ slightly):

    | dim | M | main uncentered / centered | this branch uncentered / centered |
    |---|---|---|---|
    | 8 | 2 | 0.0852 / 0.0848 | 0.0851 / 0.0853 |
    | 130 | 32 | 0.0883 / 0.0884 | 0.0882 / 0.0882 |
    | 1024 | 64 | 0.5081 / 0.5082 | 0.5082 / 0.5083 |

    The caller's vectors were byte-for-byte unchanged afterwards in all of these runs.

    Tests

    TestProductQuantization.testGlobalCenteringLeavesTrainingVectorsUntouched: 256 distinct 7-dimensional points (7 is not a multiple of the 3 subspaces, so subvector sizes differ) with a common offset of 1000, trained with globallyCenter = true and then refined. Both quantizations must decode every vector back to within 0.01, and the training vectors must be unchanged.

    This test is a guard, not a red/green test: main passes it too, because it never mutated the inputs. I checked that it can catch a mistake in the new code by temporarily slicing the centroid at the wrong subspace (getSubVector(globalCentroid, 0, ...)): it failed with IllegalArgumentException: Vectors must have the same length.

    Run on this branch (rebased onto the current main), all passing: io.github.jbellis.jvector.quantization.* (42 tests across TestProductQuantization, TestCompressedVectors, TestFusedGraphIndex, TestReconstructionError, TestPQClosestCentroid, TestPQCentroidUpdates, TestVectorSourceCompatibility, TestVectorSourceSampling, TestBinaryQuantization), plus TestCompressorCaching and TestCompressedBuildPreflight. I did not run the full suite or the native module.

    Known limits

  • The per-vector centering in encodeTo still allocates a vector per encoded vector (short-lived garbage). The getSubVector allocations during training (about 1.5 GB of short-lived objects over a 128k run) are also unchanged.

  • The centering is now one provider call per training vector per subspace instead of one per training vector, which costs about a second at 128,000 x 1024 dimensions on the native provider and nothing measurable on the default and Panama providers.

  • Measured on one machine (Apple silicon for the memory numbers, x86 for the timing); not tried with BatchedVectorValues sources.
  • Diff: 563 lines (+422 / -141) across 7 files Reviewer: MarkWolters Review duration: 14462m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 563 lines diff across 7 files
    0.50
    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%
    60%
    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.379
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #808"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 49.7% (MEDIUM) · Higher = larger blast radius
    49.7%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 19.6% · Safe to merge
    19.6%