veloxquant_mlx.codebooks.adaptive_codebook module

Data-driven codebook that refits its centroids from observed key statistics.

Unlike the closed-form distributions in strategies (Gaussian, Beta, uniform), AdaptiveScalarCodebook buffers real post-rotation vectors during a calibration window, builds an empirical histogram, and runs Lloyd-Max against that histogram to fit centroids to the actual data distribution — proxying to a default closed-form codebook until enough vectors have been observed. Drop-in API-compatible with ScalarCodebook.

class veloxquant_mlx.codebooks.adaptive_codebook.AdaptiveScalarCodebook(b, d, n_calib=64, default_codebook=None, n_hist_bins=64, seed=0)[source]

Bases: object

ScalarCodebook that refits its centroids from observed key vectors.

Phase 1 (calibration, first n_calib batches passed through observe()):
  • Buffer raw post-rotation vectors.

  • quantize() / dequantize() proxy to a default N(0, 1/d) ScalarCodebook so encode/decode keep working before calibration completes.

Phase 2 (after n_calib vectors observed):
  • Build an empirical histogram from the buffer.

  • Run Lloyd-Max with that empirical PDF to fit centroids.

  • Replace the proxy codebook with a ScalarCodebook of fitted centroids.

The wrapper is API-compatible with ScalarCodebook (quantize/dequantize/ centroids_mx/centroids_numpy/k/b), so existing quantizers can use it as a drop-in replacement.

Parameters:
  • b (int) – Bit-width.

  • d (int) – Vector dimension (used for default codebook shape).

  • n_calib (int) – Number of vectors to buffer before fitting.

  • default_codebook (ScalarCodebook | None) – Initial ScalarCodebook used during calibration.

  • n_hist_bins (int) – Histogram bin count for empirical PDF estimation.

  • seed (int)

property is_calibrated: bool

True once the buffer has been fit into a data-driven codebook.

property k: int

Number of codebook levels (2^b).

property b: int

Bit-width of the codebook.

get_codebook()[source]

Return (centroids, boundaries) of the current codebook.

Boundaries are recomputed as midpoints between sorted centroids.

Return type:

tuple[ndarray, ndarray]

observe(y)[source]

Accumulate post-rotation vectors during calibration.

The full batch is buffered (not truncated to the first remaining rows) so a single large call – e.g. an entire prefill batch – does not silently bias calibration toward whichever rows happened to be first. _fit() randomly subsamples the buffer down to n_calib.

Parameters:

y (Any) – Array of shape (batch, d), fp16 mx or numpy.

Return type:

None

quantize(y)[source]

Quantize y with the current (proxy or fitted) codebook.

Return type:

Any

Parameters:

y (Any)

dequantize(idx)[source]

Dequantize indices with the current (proxy or fitted) codebook.

Return type:

Any

Parameters:

idx (Any)

centroids_numpy()[source]

Return the current codebook’s centroids as a numpy array.

Return type:

ndarray

centroids_mx()[source]

Return the current codebook’s centroids as an mx array.

Return type:

Any