Skip to content

ggml-cuda : fall back to cuBLAS when no MMQ tile size fits in shared memory - #26141

Open
KakaruHayate wants to merge 1 commit into
ggml-org:masterfrom
KakaruHayate:fix-mmq-smem-fallback
Open

ggml-cuda : fall back to cuBLAS when no MMQ tile size fits in shared memory#26141
KakaruHayate wants to merge 1 commit into
ggml-org:masterfrom
KakaruHayate:fix-mmq-smem-fallback

Conversation

@KakaruHayate

@KakaruHayate KakaruHayate commented Jul 26, 2026

Copy link
Copy Markdown

Overview

ggml_cuda_should_use_mmq() selects MMQ purely by quantization type
without checking the device's shared memory budget. When all tile
sizes J are skipped by mul_mat_q_switch_J() because they exceed
smpbo, J_best stays 0 and the code hits GGML_ABORT.

Fix this by verifying that at least one J fits in smpbo before
declaring MMQ as usable, falling back to cuBLAS otherwise.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES, This is an issue that Claude found and fixed when I was adapting LLAMA.CPP for MUSA RWKV7 downstream

@KakaruHayate
KakaruHayate requested a review from a team as a code owner July 26, 2026 15:15
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Jul 26, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

Hi @KakaruHayate, thanks for your contribution!

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

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

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

@KakaruHayate KakaruHayate changed the title CUDA/MUSA: fall back to cuB when no MMQ tile size fits in smem CUDA/MUSA: fall back to cuBlass when no MMQ tile size fits in smem Jul 27, 2026
@KakaruHayate KakaruHayate changed the title CUDA/MUSA: fall back to cuBlass when no MMQ tile size fits in smem ggml-cuda : fall back to cuBLAS when no MMQ tile size fits in shared memory Jul 27, 2026
…emory

ggml_cuda_should_use_mmq() decides MMQ eligibility purely from the
quantization type and never considers the device's shared memory budget.
mul_mat_q_switch_J() then skips every tile size J whose shared memory
requirement exceeds smpbo -- and when all of them are skipped, J_best stays 0,
falls through to the default case and hits GGML_ABORT("fatal error").

So on devices with a small shared memory budget, the eligibility check admits
a code path that is guaranteed to abort.

Reproduced on a Moore Threads MTT S70 (arch mp_21, 28 KiB shared memory per
block) with an RWKV-7 0.1B Q8_0 model:

  $ llama-bench -m rwkv7-g1d-0.1b-Q8_0.gguf -p 128 -n 0
  J_best=0
  ggml/src/ggml-cuda/template-instances/../mmq.cuh:1521: fatal error
  (core dumped)

Only prefill (batch > 1) is affected; token generation is fine. FP16 and
Q4_K_M work on the same device -- their MMQ configs do fit.

Fix: check in ggml_cuda_should_use_mmq() whether any J fits into smpbo, and
return false if none does, so the caller falls back to cuBLAS instead of
aborting.

After the fix on the same device:

  Q8_0    pp128 1470.7 t/s, tg8 55.3 t/s   (was: abort)
  FP16    unchanged
  Q4_K_M  unchanged

This matches a -DGGML_CUDA_FORCE_CUBLAS=ON build (pp128 1464.2 t/s), which
confirms the fallback path is the one being taken.

This is not MUSA-specific -- any device with a small enough shared memory
budget can hit it.

Clarify fallback behavior for MMQ in comments

Updated comment to clarify fallback behavior for MMQ.
@KakaruHayate
KakaruHayate force-pushed the fix-mmq-smem-fallback branch from d8776de to 1eee668 Compare July 27, 2026 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant