llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Support quantized KV cache with --split-mode tensor

Open yanghoeg opened this issue 6 months ago • 6 comments

Summary

--split-mode tensor currently rejects all quantized KV cache types (--cache-type-k/v other than f16/f32) with a hard guard:

llama_init_from_model: simultaneous use of SPLIT_MODE_TENSOR and KV cache quantization not implemented

This forces users to choose between tensor parallelism speed gains and quantized KV context capacity — they cannot have both.

Environment

  • Hardware: 2× Radeon AI PRO R9700 AI TOP D6 (RDNA 4, gfx1201, 32GB GDDR6 each)
  • Platform: Talos Linux, ROCm 7.2, RCCL 2.27.7
  • Model: Gemma 4 26B A4B Q8_0
  • Build: -DGGML_HIP=ON -DGGML_HIP_RCCL=ON

What I tested

  1. Bypassed the guard via sed patch (return nullptr; → warning + continue)
  2. RCCL native init completed successfully (no deadlock):
    ncclCommInitAll_impl comm ... rank 0 nranks 2 ... - Init COMPLETE
    ncclCommInitAll_impl comm ... rank 1 nranks 2 ... - Init COMPLETE
    
  3. Crash during warmup:
    ggml-backend-meta.cpp:981: ggml op not implemented: TURBO_WHT
    

The tensor split path routes some KV cache operations through the meta-backend, which only supports f16/f32. When quantized KV is used, the meta-backend encounters dequant ops it cannot execute.

Observed behavior (f16 vs quantized KV)

Configuration Speed Context (4 slots) Status
--split-mode tensor + f16 KV 29.7 t/s ~32K/slot Works
--split-mode layer + turbo4 KV 19 t/s 131K/slot Works (production)
--split-mode tensor + turbo4 KV — — Crash (meta-backend)

Proposal

When the KV cache block size aligns with the tensor split (e.g., turbo4 block_size=32, 2 GPUs → embedding dim divisible by 64), quantized KV + tensor split should be safe. The combination would give the best of both worlds: tensor TP speed + quantized KV context capacity.

Options:

  1. Implement quantized KV dequant ops in meta-backend
  2. Route KV cache ops through device backends even in tensor split mode
  3. Relax the guard to an alignment check instead of a hard block

Block alignment examples

  • Gemma 26B: dim=4608, 2 GPUs → 4608÷64=72 ✓
  • Mistral 128B: dim=12288, 2 GPUs → 12288÷64=192 ✓

yanghoeg avatar May 23 '26 11:05 yanghoeg

please support q8_0

wx33398-ctrl avatar May 23 '26 12:05 wx33398-ctrl

+1

joeldeteves avatar May 31 '26 22:05 joeldeteves

this has been implemented in https://github.com/ggml-org/llama.cpp/pull/23792 and can be closed

NickM-27 avatar Jun 01 '26 20:06 NickM-27

Implemented via https://github.com/ggml-org/llama.cpp/pull/23792 .

JohannesGaessler avatar Jun 01 '26 21:06 JohannesGaessler

This may be a lot, i spent a long time fixing it. Heres a compressed claude summary of my findings:

-sm tensor with quantized KV cache aborts: null data pointer in ggml_backend_cuda_buffer_init_tensor Build d59d455fd (b10523) · CUDA 12.0 · 2× Tesla P100-PCIE-16GB (cc 6.0) · driver 580.173.02 · built with -DCMAKE_CUDA_ARCHITECTURES=60 -DGGML_CUDA_NCCL=OFF Repro (qwen35 arch; any quantized K or V): llama-server -m model.gguf -sm tensor -fa on -ngl 99 -c 32768 -ctk q8_0 -ctv q8_0 -np 1

ggml-cuda.cu:106: CUDA error CUDA error: invalid argument current device: 0, in function ggml_backend_cuda_buffer_init_tensor at ggml-cuda.cu:768 -ctk f16 -ctv f16 works. All quantized combinations fail (q8_0/q8_0, f16/q8_0, q8_0/f16, q8_0/q4_0, q4_0/q4_0). -DGGML_CUDA_FA_ALL_QUANTS=ON does not help. Cause. Instrumenting the function shows the failing tensor: name=cache_k_l3 (view) type=q8_0 ne=[256,2,256,1] nbytes=139264 alloc=139536 usage=0 view_src=(nil) data=(nil) The meta backend produces view-like KV tensors with view_src == nullptr and data == nullptr. The guard at line 758 only tests view_src, so these reach the padding memset, which evaluates (char *)nullptr + original_size and returns invalid argument. It only affects quantized types because f16 has padded_size == original_size, so the memset is skipped. Fix: diff

  • if (ggml_is_quantized(tensor->type) && tensor->view_src == nullptr && ggml_backend_buffer_get_usage(buffer) != GGML_BACKEND_BUFFER_USAGE_COMPUTE) {
  • if (ggml_is_quantized(tensor->type) && tensor->view_src == nullptr && tensor->data != nullptr && ggml_backend_buffer_get_usage(buffer) != GGML_BACKEND_BUFFER_USAGE_COMPUTE) { Same binary and flags: crashes without the null check, runs correctly with it. Validation — llama-perplexity, 30 chunks, n_ctx=4096, Qwen3.8-27B Q6_K, -sm tensor -fa on: KV cache PPL f16 / f16 2.6175 ± 0.0199 q8_0 / q8_0 2.6170 ± 0.0199 q8_0 / q4_0 2.6204 ± 0.0199 q4_0 / q4_0 2.6209 ± 0.0199

All within error bars of the f16 baseline. Needle-in-haystack retrieval also passes at 80K tokens with q4_0/q4_0 (needle at position 0). Related: #21788. The init-time guard described there appears to have been removed without completing support, converting a clean refusal into an abort during graph allocation.

backtrace #2 ggml_abort () #3 ggml_cuda_error(char const*, char const*, char const*, int, char const*) () #4 ggml_backend_cuda_buffer_init_tensor(ggml_backend_buffer*, ggml_tensor*) () #5 ggml_backend_meta_buffer_init_tensor_impl(ggml_backend_meta_simple_tensor_container&, ggml_tensor*) () #6 ggml_gallocr_alloc_graph () #7 ggml_backend_sched_alloc_graph () #8 llama_context::process_ubatch(llama_ubatch const&, llm_graph_type, llama_memory_context_i*, ggml_status&) () #9 llama_context::decode(llama_batch const&) () #10 llama_decode () #11 common_init_from_params(common_params&, bool) () #12 server_context_impl::load_model(common_params&) ()

Kmic-68 avatar Aug 21 '26 01:08 Kmic-68

@Kmic-68 Awesome job! It seems your fix works. This was a quite cryptic error that caused this mess and I spent a while debugging and came to the same behavior. I am astonished that this was closed and not merged as a pull request to main llama cpp (at least in the version I last checked).

@JohannesGaessler have a look at this fix from Kmic, I implemented it in my PR (https://github.com/ggml-org/llama.cpp/pull/30727) but might make more sense to reopen this issue and implement it as a standalone PR or something as its relevant to all cuda arches.

theIvanR avatar Nov 24 '26 22:11 theIvanR