llama : Add FP8 KV-cache support (#28392)

* Add FP8 KV-cache support

* Add tests for scaled, FP8 KV-cache to test-backend-ops

* Fix get_*_scale mapping and dequnatize FP8 also before rope shift

* Normalizes cache-scale names independently of projection layout.

While fused KQV is currently impractival for non-shared scales, this is
an improvement for future use regardless

* Enable Gemma 4 Assistant with FP8 by storing and fetching scale ptrs

* Update comment wording

* Move E2E FP8 KV cache tests to test-llama-archs

They belong there as they are not just a test for ggml's backend ops

* Increase support surface, remove seed

* make scaled kv generic

* Fix CI failings for test-kv-cache

https://github.com/ggml-org/llama.cpp/actions/runs/38718763447/job/116367842578?pr=28392

* CI: execute kv-cache-scale tests

* exclude fp8-kv tests from OpenVINO

* Fix linting errors of test_kv_cache_scales

* Disable kv-ffload=false + multi-GPU tests for FP8

See https://github.com/ggml-org/llama.cpp/actions/runs/38952722084/job/117148353699?pr=28413

* Adjust test label for test-kv-cache.cpp

* Fix conflict-resolution mistakes in test-kv-cache.cpp

https://github.com/ggml-org/llama.cpp/actions/runs/40554674463/job/122367426461?pr=28392

* Move KV tests to test-fp8.cpp

https://github.com/ggml-org/llama.cpp/pull/27512#discussion_r4410518158

* Remove tmp file for test-kv-cache-scales.py on Windows as well

* Fix fp8 kv cache test by population kv_source_impl

* Exclude models that consume KV cache directly

* Remove obsolete comment

* Allow BF16 scales as well for KV cache

* Rename *_cache_scale to *_cache_s

https://github.com/ggml-org/llama.cpp/pull/28392#discussion_r4517194999

* Move `is_type_scaled()` to `llama-kv-cache.cpp`
This commit is contained in:
Oliver Simons authored and GitHub committed 2026-11-10 18:08:32 +02:00
1 parent e24d0b4c34
commit 202c1a229f
16 files changed
+710 -25

No files matched your search

+39
View File
@@ -4531,6 +4531,45 @@ llama_context * llama_init_from_model(
return nullptr;
}
const bool fp8_cache = params.type_k == GGML_TYPE_F8_E4M3 || params.type_v == GGML_TYPE_F8_E4M3;
// Direct cache consumers do not use the shared attention scaling helpers.
if (fp8_cache && (model->hparams.is_mla() ||
model->arch == LLM_ARCH_DEEPSEEK32 ||
model->arch == LLM_ARCH_DEEPSEEK4 ||
model->arch == LLM_ARCH_DEEPSEEK41 ||
model->arch == LLM_ARCH_GLM_DSA ||
model->arch == LLM_ARCH_GLM5_NEXT ||
model->arch == LLM_ARCH_DOTS3NOTE ||
model->arch == LLM_ARCH_MISTRAL_LARGE4 ||
model->arch == LLM_ARCH_HY_V4 ||
model->arch == LLM_ARCH_MINIMAX_M3 ||
model->arch == LLM_ARCH_QWEN4EXP ||
model->arch == LLM_ARCH_DFLASH)) {
LLAMA_LOG_ERROR("%s: FP8 cache is not supported for architecture '%s'\n", __func__, llm_arch_name(model->arch));
return nullptr;
}
if (fp8_cache) {
for (uint32_t il = 0; il < model->hparams.n_layer_all; ++il) {
if (!model->hparams.has_kv(il)) {
continue;
}
for (ggml_tensor * scale : { model->layers[il].k_cache_s, model->layers[il].v_cache_s }) {
if (!scale || !scale->data) {
continue;
}
float value;
ggml_backend_tensor_get(scale, &value, 0, sizeof(value));
if (!std::isfinite(value) || value == 0.0f) {
LLAMA_LOG_ERROR("%s: FP8 cache scale '%s' must be finite and nonzero\n", __func__, ggml_get_name(scale));
return nullptr;
}
}
}
}
if (ggml_is_quantized(params.type_v) && params.flash_attn_type != LLAMA_FLASH_ATTN_TYPE_ENABLED) {
if (params.flash_attn_type == LLAMA_FLASH_ATTN_TYPE_AUTO) {
LLAMA_LOG_INFO("%s: enabling flash_attn since it is required for quantized V cache\n", __func__);