Skip to content

feat(kv_cache): expose the estimate on the model and log it at load - #4510

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
Lumea-art:feat/kv-cache-load-api
Nov 17, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
Lumea-art:feat/kv-cache-load-api

Conversation

@Lumea-art

Copy link
Copy Markdown
Contributor

Summary

The KV-cache estimate only appeared at the end of a quantization run. A user who loads a quantized checkpoint - which is most of them - never saw the number that decides how much context fits next to the weights.

model = GPTQModel.load("ModelCloud/Qwen2.5-0.5B-gptq-4bit")

estimate = model.kv_cache_estimate()      # KVCacheEstimate | None
estimate.bytes_per_token                  # 12 KiB
estimate.bytes_for(32768)                 # 384 MiB
estimate.tokens_for(free_vram_bytes)      # how much context fits in what is left

and load() logs the same line the quantize path already logs:

INFO  KV cache: 256 B/token (2 growing layer(s), gqa, bfloat16) | 8k ctx 2 MiB | 32k ctx 8 MiB

Three entry points now agree because they share one implementation (_kv_cache_estimate_for): the public method, the load/quantize logging call, and the underlying util.

Dtype

The dtype comes from the model's own weights, not config.torch_dtype, which is usually unset:

estimate.dtype                  # torch.bfloat16, from the weights
model.kv_cache_estimate(dtype=torch.float32)   # explicit override

A cache is as large as the activations that fill it. Quantized checkpoints hold integer tensors, so the lookup skips non-floating parameters and falls back to the config (fp16 when it says nothing) rather than reporting an int8-sized cache.

Tests

tests/test_kv_cache_model_api.py       6 passed
tests/test_kv_cache_estimate.py       24 passed
tests/test_kv_cache_all_models.py    137 passed, 71 skipped
tests/test_quantize_run_summary.py    24 passed
format/check.sh                       clean

The API tests cover dtype inheritance, the explicit override, the integer-weights fallback, a config that cannot be resolved (None, and no log line emitted), and one end-to-end GPTQModel.load() on a tiny CPU model asserting the KV cache: line reaches the console.

Note on the estimate itself

This PR does not change the estimation logic from #4158 - only where it can be reached from. tests/test_kv_cache_all_models.py still sweeps every supported model type (206 total: 132 estimated, 3 without a per-token cache, 71 whose config class is not installed locally).

The KV-cache estimate only appeared at the end of a quantization run, so a user
who *loads* a quantized checkpoint - which is most of them - never saw the number
that decides how much context fits next to the weights.

`BaseQModel.kv_cache_estimate()` is now the public entry point:

    model = GPTQModel.load("...")
    estimate = model.kv_cache_estimate()
    estimate.bytes_per_token
    estimate.tokens_for(free_vram_bytes)

The dtype comes from the model's own weights, not from `config.torch_dtype`
(usually unset): a cache is as large as the activations that fill it. Passing
`dtype=` overrides that. `log_kv_cache_estimate()` prints the same line the
quantize path prints, and `GPTQModel.load()` calls it, so both entry points agree:

    INFO  KV cache: 256 B/token (2 growing layer(s), gqa, bfloat16) | 8k ctx 2 MiB | 32k ctx 8 MiB

Internals: the dtype lookup and the estimate now live in one module-level helper
(`_kv_cache_estimate_for`) used by the method, the logging call and the quantize
path, so the three cannot drift apart.

    tests/test_kv_cache_model_api.py       6 passed
    tests/test_kv_cache_estimate.py       24 passed
    tests/test_kv_cache_all_models.py    137 passed, 71 skipped
    tests/test_quantize_run_summary.py    24 passed
    format/check.sh                       clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants