Skip to content

vulkan: copy a misaligned top_k input to an aligned scratch instead of aborting - #328

Open
danielhanchen wants to merge 3 commits into
base/upstream-d854d98bafrom
fix/vulkan-topk-misaligned-src
Open

danielhanchen wants to merge 3 commits into
base/upstream-d854d98bafrom
fix/vulkan-topk-misaligned-src

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes the Vulkan crash behind unslothai/unsloth#14987 (MiniCPM-V-4.7 with DSpark speculative decoding prints one token, then llama-server aborts).

Problem

Draft sampling runs on the backend by default (--spec-draft-backend-sampling, a top_k(10) chain per draft sequence). llm_graph_context::build_sampling gives each output row to the sampler as ggml_view_1d(logits, n_vocab, row * logits->nb[1]), so row r starts r * n_vocab * 4 bytes into the logits buffer. ggml_vk_topk binds its input with ggml_vk_tensor_subbuffer(ctx, src0) (no allow_misalign), and the top_k shaders index from the binding start, so any row whose offset is not a multiple of minStorageBufferOffsetAlignment aborts:

ggml-vulkan.cpp: GGML_ASSERT(allow_misalign || misalign_bytes == 0) failed
ggml_vk_tensor_subbuffer
ggml_vk_topk
ggml_vk_build_graph
...
common_speculative_impl_draft_dflash::draft

MiniCPM-V-4.7 has 248094 tokens, so row 1 starts at byte 992376: 8 past a 16-byte boundary, 56 past a 64-byte one (Intel Arc on Windows reports 64, NVIDIA 16). Vocabs that are a multiple of 64 never hit it, which is why most models are fine. CUDA has no such restriction.

Fix

ggml_vk_topk_src returns the input binding unchanged when it is aligned. Otherwise it copies the view with the existing f32 cpy pipeline into an aligned region of prealloc_x, past the bytes top_k already uses there (nothing for radix-select, the double buffer for the tournament path), and binds that. ggml_vk_cpy_to_contiguous gains a defaulted in_misalign_elems argument so the copy reads from the view's real start; existing callers pass nothing and still push 0. No shader changes, and the aligned path is untouched.

test_top_k_row_view in test-backend-ops runs top_k on row 1 and row 3 viewed out of an odd-width tensor (1001 and 248094 columns), with k from 1 to 4000 so both the tournament and radix-select paths run.

Evidence

lavapipe through a wrapper ICD that reports a 64-byte minStorageBufferOffsetAlignment (the reporter's Intel Arc 140V value), base = d854d98, head = this branch:

Check base head
test-backend-ops -o TOP_K -b Vulkan0 -p row= abort: GGML_ASSERT(allow_misalign || misalign_bytes == 0) on the first case 16/16 passed
test-backend-ops -o TOP_K,ARGSORT without the new cases 631/631 passed 631/631 passed
llama-server, tiny MiniCPM-V-4.7 target + DSpark drafter (real metadata and tokenizer, vocab 248094), --spec-type draft-dspark --spec-draft-n-max 3, backend draft sampling on server aborts on the first draft 48 tokens, 135 drafted

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T02:43:02.005307Z 02428e2 Manual request
🔒 Security Review ✅ Completed 2026-10-26T00:38:05.574224Z 6fa27bc PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen
danielhanchen changed the base branch from master to base/upstream-d854d98ba October 26, 2026 00:34
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 9deac32c5f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 26, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 02428e283c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 26, 2026
…logits rows (unslothai#15258)

Fixes unslothai#14987

## Problem

On the Vulkan build, loading `openbmb/MiniCPM-V-4.7-1B-gguf` with
speculative decoding on Auto (the default, which picks the DSpark
sidecar) prints one token and then llama-server dies:

```
ggml-vulkan.cpp:5759: GGML_ASSERT(allow_misalign || misalign_bytes == 0) failed
```

It happens with or without an image, and turning speculative decoding
off avoids it.

## Root cause

llama.cpp samples drafts on the backend by default
(`--spec-draft-backend-sampling`, a `top_k(10)` chain per draft
sequence). `llm_graph_context::build_sampling` hands each output row to
the sampler as `ggml_view_1d(logits, n_vocab, row * logits->nb[1])`, so
row `r` starts `r * n_vocab * 4` bytes into the logits buffer. The
Vulkan `TOP_K` path binds that view with `ggml_vk_tensor_subbuffer(ctx,
src0)` without `allow_misalign`, so any row offset that is not a
multiple of the device's `minStorageBufferOffsetAlignment` aborts.

MiniCPM-V-4.7 has 248094 tokens, so row 1 starts at byte 992376, which
is 8 bytes past a 16-byte boundary and 56 past a 64-byte one. Vocabs
that are a multiple of 64 (151936, 248320, 262144, 128256, 201088, ...)
keep every row aligned for any alignment up to the Vulkan maximum of
256, which is why other models are fine. CUDA has no such restriction.

Backtrace from a reproduction on the same prebuilt line (b11734, and
still on the latest b11798):

```
ggml_vk_tensor_subbuffer(...) [clone .constprop.1]
ggml_vk_topk(...)
ggml_vk_build_graph(...)
ggml_backend_vk_graph_compute(...)
llama_context::decode(...)
common_speculative_impl_draft_dflash::draft(...)
```

## Fix

When the llama.cpp build is Vulkan, a draft speculative mode is emitted
(DSpark, DFlash, MTP, EAGLE-3), the target vocab is not a multiple of
64, and the build advertises the flag in `--help`, Studio appends
`--no-spec-draft-backend-sampling` to its spec flags. Drafts are then
sampled on the CPU with the same `top_k(10)`; acceptance logic and every
UI setting are unchanged.

Everything else launches byte-identically: non-Vulkan builds, aligned
vocabs, `Off` / n-gram modes, and builds without the flag. A
user-supplied `--spec-draft-backend-sampling` in the extra args still
wins (llama.cpp is last-wins and the extras follow the spec block). The
flag sits inside the spec block, so the drafter-failure retry that swaps
that block for `--spec-default` drops it too.

The root fix is in llama.cpp: unslothai/llama.cpp#328 makes the Vulkan
`TOP_K` copy a misaligned input to an aligned scratch instead of
aborting, and unslothai/llama.cpp#329 pins it into the prebuilt. This PR
keeps Studio users on existing prebuilts working until a release
carrying that pin is installed; on a fixed prebuilt it only costs the
CPU-side sampling of the drafts for those models.

## Evidence

Tiny random-weight copies of the target and the DSpark drafter (real
GGUF metadata and tokenizer, vocab 248094 kept, hidden size shrunk),
Studio's exact launch flags, a CPU Vulkan device reporting the
reporter's 64-byte storage-buffer alignment:

| Arm | Result |
|---|---|
| b11734, backend draft sampling on | crash, `ggml-vulkan.cpp:5759
GGML_ASSERT(allow_misalign \|\| misalign_bytes == 0)` |
| b11798 (latest prebuilt), backend draft sampling on | same crash |
| alignment 16 or 32 instead of 64 | same crash |
| `GGML_VK_DISABLE_FUSION=1` or `GGML_VK_DISABLE_GRAPH_OPTIMIZE=1` |
same crash |
| speculative decoding off | 48 tokens, no crash |
| b11734 and b11798 with `--no-spec-draft-backend-sampling` | 48 tokens,
drafting active (135 drafted), no crash |
| same model with the vocab padded to 248128 (multiple of 64), backend
sampling on | 48 tokens, no crash |

Tests: `studio/backend/tests/test_vulkan_draft_backend_sampling.py` (19
cases) covers the predicate and drives the real `load_model` for a
DSpark launch. With the one line that appends the flag reverted, the two
launch tests fail.



### On real hardware (AMD Radeon 8060S, RADV GFX1151)

The same differential on a gfx1151 box: the released b11798 Vulkan
llama-server with Studio's DSpark launch, base (main) Studio code vs
this PR, RADV wrapped to report the reporter's 64-byte storage-buffer
alignment (RADV's own is 4, where every f32 row is aligned):

| state | Studio extra flags | server | drafted |
|---|---|---|---|
| base | (none) | aborts: `ggml-vulkan.cpp:5759:
GGML_ASSERT(allow_misalign \|\| misalign_bytes == 0) failed` | - |
| head | `--no-spec-draft-backend-sampling` | answers | 135 |
| head merged into main | `--no-spec-draft-backend-sampling` | answers |
135 |

Verdict: CONFIRMED (the defect reproduces at base and is absent at
head).

This branch has not been deployed

No deployments
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.

1 participant