Skip to content

llama : fix KV shift for qwen2vl - #13870

Merged
ngxson merged 2 commits into
ggml-org:masterfrom
ngxson:xsn/fix_kv_shift_qwen2vl
May 28, 2025
Merged

llama : fix KV shift for qwen2vl#13870
ngxson merged 2 commits into
ggml-org:masterfrom
ngxson:xsn/fix_kv_shift_qwen2vl

Conversation

@ngxson

@ngxson ngxson commented May 28, 2025

Copy link
Copy Markdown
Collaborator

Fix #13865

Provided a model with 3 mrope sections, a rotated vector looks like this: [time, time+x, time+y]

This works with the assumption that mrope with x == y == 0 is identical to doing a neox rope with theta = +time

For example, when we want to shift the time to time-1, that mean we shift from [time, time+x, time+y] to [time-1, time-1+x, time-1+y] ; We can simply apply neox rope with theta = -1 to archive the same effect

@ngxson
ngxson requested a review from ggerganov May 28, 2025 18:14

@ggerganov ggerganov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🪄

@ngxson
ngxson merged commit 763d06e into ggml-org:master May 28, 2025
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* llama : fix KV shift for qwen2vl

* add ref to the PR
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
* server: add support for vision model
webui: add support for vision model

* server : remove hack for extra parallel slot#10187

* llama : fix KV shift for qwen2vl ggml-org#13870

* add no-context-shift parameter

---------

Co-authored-by: firecoperana <firecoperana>
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
* llama : fix KV shift for qwen2vl

* add ref to the PR
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
* llama : fix KV shift for qwen2vl

* add ref to the PR
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
* llama : fix KV shift for qwen2vl

* add ref to the PR
Anyesh added a commit to Anyesh/llama.cpp that referenced this pull request May 20, 2026
Two extensions land the kv_block save/load across the modern model spectrum:

1. llama_get_kv_cache (in llama-context.cpp) now unwraps llama_memory_hybrid
   and llama_memory_hybrid_iswa via their get_mem_attn() accessor, returning
   the attention sub-cache. Recurrent layers in hybrid models carry a fixed-
   size state that does not need eviction or recovery, so block save/load
   operating on the attention component alone is sufficient.

2. The seq_add() and get_can_shift() guards for n_pos_per_embd > 1 were
   outdated. build_rope_shift already has an mrope workaround (line 1739,
   ref ggml-org#13870) that treats the temporal shift as a
   NeoX-style whole-vector rotation. Cells store a single temporal pos plus
   a separate ext payload for spatial axes; shifting only the temporal pos
   is semantically correct. seq_div is left guarded because EVOKE does not
   use it.

Verified end-to-end with kv_restore on chihiro:
  Qwen2.5-7B-Instruct     pure attention + std RoPE        PASS
  Qwen3.5-9B              hybrid Mamba/Attention + mrope   PASS
  Qwen3.6-35B-A3B         MoE + mrope + thinking           PASS

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
phibya pushed a commit to ziee-ai/llama.cpp that referenced this pull request May 29, 2026
* llama : fix KV shift for qwen2vl

* add ref to the PR
fewtarius pushed a commit to fewtarius/CachyLLama that referenced this pull request May 30, 2026
* llama : fix KV shift for qwen2vl

* add ref to the PR
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* llama : fix KV shift for qwen2vl

* add ref to the PR
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* llama : fix KV shift for qwen2vl

* add ref to the PR
JEF1056 pushed a commit to JEF1056/llama-cpp-turboquant that referenced this pull request Jun 8, 2026
…3-VL)

- kv-cache: allow get_can_shift() to return true for LLAMA_ROPE_TYPE_MROPE
  and LLAMA_ROPE_TYPE_IMROPE; build_rope_shift() NEOX-mode workaround already
  handles whole-vector K rotation for text tokens (ref PR ggml-org#13870)

- server: remove blanket n_cache_reuse=0 disable for multimodal models;
  llama_memory_can_shift() gates shifting without a separate has_mtmd check

- server: remove !has_mtmd gate from can_cache_reuse and dead
  GGML_ASSERT/GGML_ABORT guards; image cells are skipped in the loop below

- server: skip LLAMA_TOKEN_NULL tokens in the chunk-shifting loop so
  spatial M-RoPE positions are never relocated; add NULL guards to the
  inner match loop so text runs never span across media chunks

- server: use pos_next() to convert token indices to actual KV positions
  for common_context_seq_rm/add; M-RoPE images advance the position counter
  by max(nx,ny) not n_tokens so raw indices give wrong ranges after images

Magic applied with Wibey CLI (https://wibey.walmart.com/cli)

Co-Authored-By: Wibey CLI <genai-coding-assistants@walmart.com>
Anyesh added a commit to Anyesh/llama.cpp that referenced this pull request Jun 29, 2026
Two extensions land the kv_block save/load across the modern model spectrum:

1. llama_get_kv_cache (in llama-context.cpp) now unwraps llama_memory_hybrid
   and llama_memory_hybrid_iswa via their get_mem_attn() accessor, returning
   the attention sub-cache. Recurrent layers in hybrid models carry a fixed-
   size state that does not need eviction or recovery, so block save/load
   operating on the attention component alone is sufficient.

2. The seq_add() and get_can_shift() guards for n_pos_per_embd > 1 were
   outdated. build_rope_shift already has an mrope workaround (line 1739,
   ref ggml-org#13870) that treats the temporal shift as a
   NeoX-style whole-vector rotation. Cells store a single temporal pos plus
   a separate ext payload for spatial axes; shifting only the temporal pos
   is semantically correct. seq_div is left guarded because EVOKE does not
   use it.

Verified end-to-end with kv_restore on chihiro:
  Qwen2.5-7B-Instruct     pure attention + std RoPE        PASS
  Qwen3.5-9B              hybrid Mamba/Attention + mrope   PASS
  Qwen3.6-35B-A3B         MoE + mrope + thinking           PASS

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
(cherry picked from commit a29599f)
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
* llama : fix KV shift for qwen2vl

* add ref to the PR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Eval bug: KV cache shifting does not work for Qwen2.5VL

2 participants