llama : fix null pointer dereference by checking for rotation buffer in K-cache shifts#25636
Open
engeldlgado wants to merge 1 commit into
Open
llama : fix null pointer dereference by checking for rotation buffer in K-cache shifts#25636engeldlgado wants to merge 1 commit into
engeldlgado wants to merge 1 commit into
Conversation
… in K-cache shifts
|
Hi @engeldlgado, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Upstream AUTO-ACTIVATES Hadamard rotation (
attn_rot_k) when the K cache is quantized and the head size is divisible by 64... In these cases,rotis not null and DOES NOT crash (with Qwen head-128 it does not reproduce). The actual window:LLAMA_ATTN_ROT_DISABLE=1(src/llama-kv-cache.cpp ~329) + quantized K + context shift -> crash.Models with head size NOT divisible by 64 (there are: 80, 96, 112) + quantized K + context shift -> same crash, without touching any environment.
Mechanics:
build_input_k_rotreturns nullptr whenattn_rot_kis false (~1413)...build_rope_shiftcallsllama_mul_mat_hadamard(ctx, tmp, rot)unconditionally on the quantized branch (~1862/1869)... the helper doesrot->ne[0]without checking (src/llama-impl.h ~61). The fix: skip rotation when no rotation is configured. Withattn_rot_kactive, nothing changes.To reproduce the error, you can run these commands in main and it should crash as it did for me.
Without fix: dies upon reaching the "slot context shift" (~450 tokens). With fix: completes the 700.
Requirements
Yes, I used assistance to reproduce the error, but the fix and implementation are mine. I had to fix it to implement llama.cpp in the ToshLLM app for Intel Macs.