Eval bug: deepseek4 file with no ratio-4 (CSA) layer crashes with integer division by zero in the llama_kv_cache constructor

Open Beginner friendly
#32,424 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@rishitha2625 is already working on this.

Since Nov 17, 2026.

  • #32501 by @rishitha2625 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

Start in src/llama-kv-cache.cpp at the llama_kv_cache constructor’s attention rotation setup and trace how the DeepSeek override handles a cache with no layers. Guard rotation setup or the loop against zero head width; done means loading the described DeepSeek4 model without a ratio-4 layer no longer crashes during context creation. The report gives a reproduction command, but names no specific test.

Written by the indexing model from the issue text.

Description

Name and Version

Reproduced on a CPU-only Windows build (MSVC) of commit 5676dfbe0. The code involved is unchanged on master at c7518dbec5.

Operating systems

Windows (the arithmetic is not platform-specific; on Linux this should surface as SIGFPE)

GGML backends

CPU

Models

A deepseek4 GGUF whose compress_ratios contain no ratio-4 layer, i.e. only ratio 0, or ratio 0 and ratio 128. These were small synthetic test files. The published DeepSeek-V4-Flash file has ratio-4 layers and is not affected.

Problem description & steps to reproduce

Loading a deepseek4 model that has no ratio-4 (CSA) layer crashes while the context is created, before any token is evaluated:

llama-debug -m <deepseek4 file without a ratio-4 layer> -p "..." --save-logits -ngl 0 --no-warmup

The process exits with 0xC0000094 (STATUS_INTEGER_DIVIDE_BY_ZERO).

The cause is in the llama_kv_cache constructor (src/llama-kv-cache.cpp), in the branch that sets up the attention rotation:

bool attn_rot_k =
    !attn_rot_disable &&
    n_embd_head_k_all > 0 &&
    ggml_is_quantized(type_k) &&
    hparams.n_embd_head_k() % 64 == 0;

// always create Hadamard rotation tensors for DeepSeek lightning indexers
if ((model.arch == LLM_ARCH_DEEPSEEK32 || model.arch == LLM_ARCH_DEEPSEEK4 || ...) &&
        hparams.n_embd_head_k_full == hparams.indexer_head_size) {
    attn_rot_k = true;
}

if (attn_rot_k) {
    n_rot_k = 64;
    while (n_embd_head_k_all % (2*n_rot_k) == 0) {
        n_rot_k *= 2;
    }
}

The first condition requires n_embd_head_k_all > 0, but the DeepSeek override sets attn_rot_k = true without that check. For a deepseek4 model with no ratio-4 layer, the lightning-indexer (LID) cache is constructed with no layers, so its n_embd_head_k_all is 0. Then 0 % (2*n_rot_k) == 0 is always true, n_rot_k doubles until the uint32_t wraps to 0, and the next evaluation of the loop condition computes 0 % 0.

Expected behaviour

A cache with no layers should not get a rotation: either the DeepSeek override should also require n_embd_head_k_all > 0, or the loop should be guarded against a zero head width.

Workaround

Giving the file one ratio-4 layer avoids the crash. In the test files this was a trailing layer with an all-zero attn_output_b, so that it contributes nothing.

First Bad Commit

Not bisected.

Relevant log output

No log line is printed for the affected cache before the crash; the n_rot_k = ... line that follows the loop is never reached.

Dominant language
C++
Stars
136k
Forks
26.1k
Avg merge
1d 19h
Merged PRs (30d)
521

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ggml-org/llama.cpp

All issues in ggml-org/llama.cpp

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.