Repository navigation
vulkan: copy a misaligned top_k input to an aligned scratch instead of aborting - #328
danielhanchen wants to merge 3 commits into
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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).
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, atop_k(10)chain per draft sequence).llm_graph_context::build_samplinggives each output row to the sampler asggml_view_1d(logits, n_vocab, row * logits->nb[1]), so rowrstartsr * n_vocab * 4bytes into the logits buffer.ggml_vk_topkbinds its input withggml_vk_tensor_subbuffer(ctx, src0)(noallow_misalign), and the top_k shaders index from the binding start, so any row whose offset is not a multiple ofminStorageBufferOffsetAlignmentaborts: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_srcreturns the input binding unchanged when it is aligned. Otherwise it copies the view with the existing f32 cpy pipeline into an aligned region ofprealloc_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_contiguousgains a defaultedin_misalign_elemsargument 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_viewin 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:test-backend-ops -o TOP_K -b Vulkan0 -p row=GGML_ASSERT(allow_misalign || misalign_bytes == 0)on the first casetest-backend-ops -o TOP_K,ARGSORTwithout the new cases--spec-type draft-dspark --spec-draft-n-max 3, backend draft sampling on