Skip to content

llama: fix quantized kv-cache for dsv4#25202

Merged
am17an merged 2 commits into
ggml-org:masterfrom
am17an:dsv4-quant-kv-cahce
Jul 7, 2026
Merged

llama: fix quantized kv-cache for dsv4#25202
am17an merged 2 commits into
ggml-org:masterfrom
am17an:dsv4-quant-kv-cahce

Conversation

@am17an

@am17an am17an commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Overview

DSV4's compressed caches (CSA and HCA) did not respect the hadamard rotation when quantized kv-cache is used. This PR adds that, a side effect being that concat'ed caches need to be %512, so some extra padding is required.

Additional information

Requirements

@am17an
am17an requested review from CISC and ggerganov as code owners July 1, 2026 12:44
@am17an
am17an requested a review from fairydreaming July 1, 2026 12:44
@github-actions github-actions Bot added the model Model specific label Jul 1, 2026
Comment thread src/llama-kv-cache-dsv4.cpp Outdated
static constexpr uint32_t DSV4_HCA_RATIO = 128;
// [TAG_DSV4_CACHE_PAD]
// matches MATRIX_ROW_PADDING used by backends for quantized row padding
static constexpr uint32_t DSV4_CACHE_PAD = 512;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for using this padding? I get it that it matches MATRIX_ROW_PADDING, but it seems to be only used internally by backends, so I don't understand why is it needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I didn't add this padding it would crash because quantized tensors need that padding. Not sure if there's a better way to do this

@fairydreaming fairydreaming Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to replicate this crash? Tried this PR without padding changes (llama-kv-cache-dsv4.cpp file unchanged) with CUDA backend and can't get it to fail.

Edit: maybe it's related to me using CPU expert offloading. I see more graph splits with quantized KV cache.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you using a quantized cache?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you using a quantized cache?

Sure, tried with --cache-type-k q8_0 --cache-type-v q8_0.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides, from what I see in backends code MATRIX_ROW_PADDING applies to ne0 dimension, how KV cache length would appear there?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked why I have more graph splits with quantized KV cache and it's caused by GGML_OP_CONCAT of Q8_0 KV cache (when csa_k_all tensor is created), so I don't think it's related to the crash you mentioned. But these concats will run on CPU when using quantized cache.

@kevincox

kevincox commented Jul 3, 2026

Copy link
Copy Markdown

I can confirm that this resolves the gibberish text. Comparing to b9840 I was getting nonsense output and on this commit it works fine.

Command line: llama-server --no-warmup --port 8085 --ctx-size 131072 -np 1 --no-mmproj --flash-attn on --spec-default --cache-type-k q4_1 --cache-type-v q4_1 -hf teamblobfish/DeepSeek-V4-Flash-GGUF:Q2_K --temp 1.0 --top-p 1.0 --top-k 0 --min-p 0.0

@fairydreaming

fairydreaming commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

@am17an let me know if you have any reproducible case that shows the crashes you mentioned related to MATRIX_ROW_PADDING pad fixes.

I posted info about my dsv4 branch (that among other fixes includes this PR and PRs for quantized CONCAT) on Reddit few days ago. I omitted all changes in llama-kv-cache-dsv4.cpp and llama-kv-cache-iswa.cpp when merging this PR and so far nobody complained.

I don't know, maybe some specific combination of options is needed to trigger it. I'd like to investigate it more before this is merged.

Edit: mystery solved, see #25851

@am17an

am17an commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

If the quantized kv-cache works after your changes I'm ok closing this one

@fairydreaming

Copy link
Copy Markdown
Collaborator

If the quantized kv-cache works after your changes I'm ok closing this one

@am17an No, I used this PR and it works, but without the changes in llama-kv-cache-dsv4.cpp and llama-kv-cache-iswa.cpp (DSV4_CACHE_PAD etc) since I don't understand why they are needed. You mentioned that it crashes without them, so I'd like to reproduce this case to investigate.

@am17an

am17an commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Ah okay, let me revert those changes then. If I face the crash again I'll take a better look

@fairydreaming fairydreaming left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it's good to merge in this state, we can fix any remaining problems with padding if/when they appear.

@am17an am17an added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Jul 7, 2026
@am17an
am17an merged commit 024c46a into ggml-org:master Jul 7, 2026
25 checks passed
CowboyTim pushed a commit to aardbeiplantje/llama.cpp that referenced this pull request Jul 21, 2026
gianni-cor pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. model Model specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants