Repository navigation
feat(kv_cache): expose the estimate on the model and log it at load - #4510
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
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 MiBThree 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: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
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-endGPTQModel.load()on a tiny CPU model asserting theKV 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.pystill sweeps every supported model type (206 total: 132 estimated, 3 without a per-token cache, 71 whose config class is not installed locally).