Repository navigation
[executorch][llm-cache] Add per-layer quantized KV storage config - #26079
digantdesai wants to merge 2 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/26079
Note: Links to docs will display an error until the docs builds have been completed. ❌ 7 Pending, 2 Unclassified FailuresAs of commit bf5341e with merge base 4ce60cb ( UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
There was a problem hiding this comment.
🟡 Changes recommended
Finite FP32 values in a narrow range, such as 1.0–1.0001, can overflow the stored FP16 zero point and decode the entire group as infinity. Separately, encoding under FE_UPWARD violates the reference ties-to-even behavior; the default empty storage configuration makes storage_for(0) report FP32 while uses(FP32) returns false; and direct MLXSequenceCache construction accepts storage-vector lengths that violate the per-layer contract and are rejected by CacheFactory::build.
4 open issues
What changed in this PR
Adds per-layer KV storage configuration and reference quantization codecs to ExecuTorch’s shared LLM cache API, independently of the model’s floating-point dtype.
Changes:
- Adds storage selection, quantization mode, and per-layer validation.
- Implements Q4_GS32_ZPF16 and Q4_1 codecs with layout and rounding tests.
- Makes existing cache backends reject unsupported quantized storage.
| File | Description |
|---|---|
| extension/llm/cache/test/cache_test.cpp | Tests storage configuration and codec behavior. |
| extension/llm/cache/targets.bzl | Exports the codec header and scalar-type dependency. |
| extension/llm/cache/kv_codec.h | Adds reference quantization and dequantization. |
| extension/llm/cache/cache.h | Defines storage options, validation, and selection helpers. |
| backends/native/runtime/engine/ggml/GgmlCache.cpp | Rejects quantized storage in ggml caches. |
| backends/mlx/runtime/MLXSequenceCache.h | Rejects quantized storage in MLX sequence caches. |
| backends/mlx/runtime/MLXCellCache.h | Rejects quantized storage in MLX cell caches. |
| backends/cuda/runtime/cuda_kv_cache.cpp | Rejects quantized storage in CUDA cache factories. |
Review effort: Balanced
| @@ -851,6 +851,9 @@ class MLXSequenceCache final : public cache::Cache, | |||
| : capacity_(cfg.capacity) { | |||
| using sequence::detail::require; | |||
| require(cache::valid(cfg), "sequence: invalid config"); | |||
| const float c = std::nearbyint(v[j + k] / scale - zp); | ||
| codes[k] = static_cast<uint8_t>( | ||
| c >= 15.0f ? 15 : (c > 0.0f ? static_cast<int>(c) : 0)); |
|
@claude review |
|
Claude finished @Gasoonjia's task in 4m 35s —— View job One change is needed before merge: the Q4_GS32_ZPF16 reference codec stores its zero point as FP16 in scale units, which overflows to inf for groups with a small spread and a large offset, so the whole group decodes to inf. The other findings are minor API-naming, consistency and test-coverage issues. Detailed reviewPR Review: #26079SummaryThis PR adds per-layer Code Quality
API Design
Testing
RecommendationRequest Changes. The FP16 zero-point overflow is a defect in a reference format that is meant to be frozen, and fixing it later means a new enum value. The rest can be handled in this PR or a follow-up. |
There was a problem hiding this comment.
🟡 Changes recommended
Direct construction of MLXSequenceCache and MLXBatchedCache accepts an all-FP32 layer_storage list with the wrong entry count, violating the one-entry-per-layer contract enforced by CacheFactory. Separately, codec encode/decode tests use only 32-value inputs, leaving multi-group offsets and the distinct Q4_GS32_ZPF16 and Q4_1 layouts unverified.
3 open issues
2 resolved since last review
Review effort: Balanced
| } | ||
| } | ||
|
|
||
| // Q4_GS32_ZPF16 rows: all head_dim / 2 code bytes first (value 2m in the low |


Add CacheConfig::layer_storage (one KVStorage per layer; empty keeps
unquantized storage) and kv_quant_impl (auto or emulated).
kv_storage_traits() gives each type's name, group size and group bytes, and
kv_codec.h holds the reference quantize/dequantize for Q4_GS32_ZPF16 (4-bit
codes, FP16 scale and FP16 zero point in scale units per group of 32) and
Q4_1 (ggml block_q4_1). full_attention_storage() selects window-0 layers.
Every cache backend refuses quantized storage until it implements it.
The storage encoding is a runtime choice, independent of kv_dtype, so the
same exported program runs with or without a quantized KV cache.
Authored with assistance from Claude and Codex.