veloxquant_mlx.integration package¶
Integration shims that wire VeloxQuant-MLX caches into third-party inference loops.
Monkey-patches mlx_lm and mlx_vlm model instances so their normal
generate() entry points build a VeloxQuant KV cache instead of the
stock mlx_lm.models.cache.KVCache, without requiring callers to modify
the host library. Re-exports patch_model_kv_cache()
and patch_vlm_kv_cache().
- veloxquant_mlx.integration.patch_model_kv_cache(model, config)[source]
Wire a quantized KV cache into an mlx-lm model’s generation path.
mlx_lm.generate()builds its prompt cache viamlx_lm.models.cache.make_prompt_cache(), which callsmodel.make_cache()if the model defines one, and falls back to a default fp16 cache otherwise. There is no pre-existing.cacheattribute on attention sub-modules to overwrite — caches are only ever constructed lazily, on demand, through that hook.This function builds the real per-layer cache list via
KVCacheBuilder.for_model()(which already handles VLM wrappers, MoE-gate fallback layers, per-layer bit-width lists, and cross-layer methods like XQuant/MiniCache/PyramidKV) and overridesmodel.make_cacheso every call builds a fresh cache list — matchingveloxquant_mlx.integration.mlx_vlm_patch.patch_vlm_kv_cache(). A prior version closed over a single cache list built once at patch time, so a secondgenerate()call on the same patched model would reuse the first call’s leftover KV state instead of starting clean.- Parameters:
model (
Any) – A loaded mlx_lm model instance.config (
KVCacheConfig) – KVCacheConfig describing the quantization scheme.
- Return type:
- Returns:
The first freshly built cache list (also validates the config eagerly). Subsequent
generate()calls receive their own fresh lists via the installedmake_cachehook.- Raises:
QuantizerConfigError – If
config.methodis a standalone method (does not implement the mlx_lm KVCache serving contract — seeveloxquant_mlx.cache.base.STANDALONE_METHODS). Raised immediately by the underlyingKVCacheBuilder.for_model()call, beforemodel.make_cacheis touched.
Example:
config = KVCacheConfig( method="turboquant_rvq", bit_width_inlier=1, seed=42, ) patch_model_kv_cache(model, config)
- veloxquant_mlx.integration.patch_vlm_kv_cache(model, config)[source]
Wire a quantized KV cache into an mlx-vlm model’s generation path.
- Parameters:
model (
Any) – A loaded mlx-vlm model instance (must expose.language_model).config (
KVCacheConfig) – KVCacheConfig describing the quantization scheme.
- Return type:
- Returns:
The first freshly built cache list (also validates the config eagerly). Subsequent
generate()calls receive their own fresh lists via the installedmake_cachehook.- Raises:
ValueError – If
modelhas nolanguage_modelattribute — for text-only mlx_lm models useveloxquant_mlx.integration.mlx_lm_patch.patch_model_kv_cache().QuantizerConfigError – If
config.methodis a standalone method (does not implement the mlx_lm KVCache serving contract — seeveloxquant_mlx.cache.base.STANDALONE_METHODS). Raised immediately by the underlyingKVCacheBuilder.for_model()call, beforelanguage_model.make_cacheis touched.