PQ training: center subvectors instead of copying the training set #808
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
MarkWolters
Fixes #807.
What
ProductQuantization.computeandrefineno longer build a centered copy of the training set.createCodebooksandextractSubvectorsnow 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.VectorAccess.copySelectedhands back the caller's own vector objects for unshared sources, which is why this does not center the training vectors in place.encodeTopath are untouched.Results
128,000 x 1024 Gaussian vectors,
compute(ravv, 256, 256, true, UNWEIGHTED, pool, pool)thenencodeAll, 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.suballocation from 1,044 MB to 544 MB (the rest isencodeTo, 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
mainand 0.93 s with this change; end to end,computetakes 26.4 s onmainand 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 |
mainuncentered / 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 withgloballyCenter = trueand thenrefined. 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:
mainpasses 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 withIllegalArgumentException: Vectors must have the same length.Run on this branch (rebased onto the current
main), all passing:io.github.jbellis.jvector.quantization.*(42 tests acrossTestProductQuantization,TestCompressedVectors,TestFusedGraphIndex,TestReconstructionError,TestPQClosestCentroid,TestPQCentroidUpdates,TestVectorSourceCompatibility,TestVectorSourceSampling,TestBinaryQuantization), plusTestCompressorCachingandTestCompressedBuildPreflight. I did not run the full suite or the native module.Known limits
encodeTostill allocates a vector per encoded vector (short-lived garbage). ThegetSubVectorallocations during training (about 1.5 GB of short-lived objects over a 128k run) are also unchanged.BatchedVectorValuessources.