Skip to content

ggml-cuda: honor view_offs in cpy data pointer - #22913

Closed
dusterbloom wants to merge 1 commit into
ggml-org:masterfrom
dusterbloom:fix/cpy-view-offs
Closed

dusterbloom wants to merge 1 commit into
ggml-org:masterfrom
dusterbloom:fix/cpy-view-offs

Conversation

@dusterbloom

@dusterbloom dusterbloom commented May 10, 2026 •

Copy link
Copy Markdown

$\textcolor{red}{\textbf{(USER WAS BANNED FOR THIS POST)}}$

Summary

ggml_cuda_cpy() was using src->data raw, ignoring view_offs. For a destination tensor created via ggml_view_3d at non-zero offset (e.g., a per-chunk SWA KV cache write at offset cache_k->nb[1] * kv_start), the kernel wrote to the wrong location, silently overwriting earlier chunks' data.

This patch adds a small ggml_cuda_cpy_data() helper that resolves view_src + view_offs correctly, and uses it for both src0 and src1 pointers. For non-view tensors view_offs == 0 so behavior is unchanged.

Symptom this fixes

Multi-chunk Gemma4 prefill produced silently corrupted KV cache state, causing argmax to return token 0 (or NaN propagation + SIGSEGV on TQ3_0). Diagnosed by stashing/reverting and confirming chunk-1 writes were landing at the wrong offset.

Diff scope

  • ggml/src/ggml-cuda/cpy.cu — 6 lines added, 2 removed (one new helper + two call-site swaps)
  • No other files touched
  • No public-API changes
  • No new dependencies

Applies cleanly on current master.

Test plan

  • Builds clean against current master
  • Behaviour unchanged for non-view tensors (view_offs == 0 preserves the old code path)
  • Maintainers' usual CI

This is a generic bugfix surfaced by long-context prefill with chunked attention; happy to add a regression test if you have a preferred place for one.

ggml_cuda_cpy() was using src->data raw, ignoring view_offs. For a
destination tensor created via ggml_view_3d at non-zero offset (e.g.,
the per-chunk SWA KV cache write at offset cache_k->nb[1]*kv_start),
the kernel wrote to the wrong location, silently overwriting earlier
chunks' data.

Add a small ggml_cuda_cpy_data() helper that resolves view_src + view_offs
correctly, and use it for both src0 and src1 pointers. For non-view
tensors view_offs == 0 so behavior is unchanged.

Symptom this fixes: multi-chunk Gemma4 prefill produced silently corrupted
KV cache state, causing argmax to return token 0 (or NaN propagation +
SIGSEGV on TQ3_0). Diagnosed by stashing/reverting and confirming chunk-1
writes were landing at the wrong offset.

Co-discovered with the OpenAI Codex assistant ("codex:rescue" handoff).
@ggml-gh-bot

ggml-gh-bot Bot commented May 10, 2026

Copy link
Copy Markdown

Hi @dusterbloom, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • AI-generated content: This project does not accept PRs, descriptions or commit messages that are fully or predominantly AI-generated. If you have used AI to assist you in writing code, please make sure to disclose that explicitly.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@am17an

am17an commented May 10, 2026

Copy link
Copy Markdown
Contributor

Create an issue with a reproduction first

@am17an am17an closed this May 10, 2026
@github-actions github-actions Bot added Nvidia GPU Issues specific to Nvidia GPUs ggml changes relating to the ggml tensor library for machine learning labels May 10, 2026
@JohannesGaessler

Copy link
Copy Markdown
Contributor

The logic here is fundamentally wrong. Don't just submit hallucinated "fixes".

@dusterbloom
dusterbloom deleted the fix/cpy-view-offs branch May 11, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning Nvidia GPU Issues specific to Nvidia GPUs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants