Plugin Vector Quantizer
Product quantization (PQ) for vector indexes, powered by WebAssembly.
This plugin is licensed under GPL-3.0-only, unlike ZBSearch and the other @zbsearch/* packages (Apache-2.0). It
ships a compiled WebAssembly build of vector_quantizer, which is
GPL-3.0-only. Make sure that is compatible with how you distribute your application. The built-in int8 and
binary vector quantization need no plugin.
@zbsearch/plugin-vector-quantizer adds product quantization to the flat() and ivf() vector indexes. Each vector is split into m subspaces, and each subspace is stored as the one-byte index of its nearest centroid among ks learned centroids. With the defaults, a 384-dimensional embedding takes 52 bytes instead of 1,536.
Training and encoding run in WebAssembly. The module is inlined in the package and has no imports, so it works in browsers, Node.js, Deno, workers and Gecko (for example Zotero) without bundler configuration, and without Rust.
Installation
npm install @zbsearch/plugin-vector-quantizerUsage
import { create } from "zbsearch";
import { flat } from "zbsearch/trees/vector-flat";
import { ivf } from "zbsearch/trees/vector-ivf";
import { pq } from "@zbsearch/plugin-vector-quantizer";
const quantizer = await pq({ m: 48, ks: 256 });
const db = create({
schema: {
title: "string",
embedding: "vector[384]",
},
indexes: {
embedding: flat({ quantization: quantizer, rescore: "int8" }),
// or: ivf({ nlist: 256, nprobe: 16, quantization: quantizer })
},
});pq() compiles the WebAssembly module asynchronously. In Node.js, Deno, workers or privileged Gecko code you can call pqSync() instead.
| Option | Default | Description |
|---|---|---|
m | dim / 8 | Subspaces, i.e. code bytes per vector. |
ks | 256 | Centroids per subspace (2 to 256). |
iterations | 12 | k-means iterations per subspace. |
trainingSize | max(4 × ks, 1024) | Vectors collected before training. |
maxTrainingSize | max(trainingSize, 4096) | Upper bound on the vectors used by one fit (evenly sampled). |
seed | 42 | Seed of the k-means initialisation, so fits are reproducible. |
Training
Product quantization learns its centroids from your data. Until trainingSize vectors have been inserted, the index keeps them as float32 and searches them exactly. Once the threshold is reached it trains the quantizer, encodes every buffered vector and drops the float32 copies.
If your data drifts, re-train and re-encode everything with refit:
import { refit } from "@zbsearch/plugin-vector-quantizer";
refit(db, "embedding"); // trains on the rescoring copies, or on decoded codes
refit(db, "embedding", sampleVectors); // or on vectors you provideAccuracy
Product quantization is lossy: on its own it is best used to find candidates. Combine it with rescore: 'int8' to rescore the best limit × oversample candidates against int8 copies, which gives results very close to exact search. See the vector quantization section for benchmark numbers.
Persistence
save() and load() store the codebook and the codes as compact base64 typed arrays. Pass the same indexes configuration, with a pq() quantizer, to create() before calling load(): without it, load() throws, because the codes cannot be decoded without the plugin.