Eval bug: deepseek4 file with no ratio-4 (CSA) layer crashes with integer division by zero in the llama_kv_cache constructor
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.
Hi @mxaddict and maintainers,
I'd like to work on a fix for this issue and open a PR shortly.
To prevent both the n_embd_head_k_all == 0 division-by-zero and any uint32_t overflow in n_rot_k, I plan to:
- Require
n_embd_head_k_all > 0in the DeepSeek lightning-indexerattn_rot_koverride insrc/llama-kv-cache.cpp(matching the baseattn_rot_kguard so a layerless cache does not enable rotation). - Guard the
n_rot_kdoubling loop againstn_embd_head_k_all == 0/ overflow, and add a regression test verifying that a syntheticdeepseek4GGUF without ratio-4 layers initializes its context cleanly without crashing.