Skip to content

ggml-cuda: key the MUL_MAT_ID MMVQ batch cap on the compiled arch - #287

Open
danielhanchen wants to merge 2 commits into
masterfrom
fix/mmvq-mmid-compiled-arch
Open

danielhanchen wants to merge 2 commits into
masterfrom
fix/mmvq-mmid-compiled-arch

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Overview

Fixes unslothai/unsloth#14109 (a P100 crashes in mul_mat_vec_q_switch_ncols_dst with CUDA error: invalid argument since the last update).

One file, host side only:

ggml/src/ggml-cuda/mmvq.cu    +4/-2

The defect

ggml-org#27828 lets a P100 (cc 600) use MMVQ for MUL_MAT_ID up to 8 tokens. It changed two places that have to agree:

  • host, get_mmvq_mmid_max_batch(type, cc): keyed on the device's cc
  • device, get_mmvq_mmid_max_batch_for_device<type>(): keyed on __CUDA_ARCH__, and it sets the __launch_bounds__ of mul_mat_vec_q_moe

They only agree when the build contains the device's own arch. The cuda12-legacy prebuilt (and the upstream default list) is 50-virtual;61-virtual, with no sm_60, so a P100 runs the compute_50 PTX. That kernel is compiled against the pascal_older table, while the host now says 8 tokens:

quant host cap on a P100 kernel .maxntid in the legacy build launched at 8 tokens
Q4_K 8 160 (5 warps) 256 threads
Q5_K 8 160 256
Q6_K, Q8_0 8 128 (4 warps) 256

A launch with more threads than __launch_bounds__ is rejected with cudaErrorInvalidValue, which is the invalid argument in the report. The .maxntid values were read from cuobjdump -ptx of mmvq.cu.o built with the legacy arch list.

The fix

Key the host decision on ggml_cuda_highest_compiled_arch(cc), the arch the driver actually loads, which is what get_device_table_id(cc) in the same file already does. A build that contains the device's arch gets arch == cc, so nothing changes there. The device code is untouched (the PTX of mmvq.cu.o is byte identical before and after).

Enumerating host cap vs kernel bound over 9 build profiles (the 8 prebuilt CUDA profiles plus the upstream default list), 17 NVIDIA compute capabilities and 21 quant types: 2226 cells, 294 over-launch before, 0 after, and every cell where the build contains the device's arch is unchanged.

Same change as ggml-org#28578, which was closed on 2026-10-05 for not following the PR template (no technical review), before ggml-org#27828 added the Pascal case that makes it reachable on the legacy prebuilt.

Evidence

Tiny random Qwen3-MoE (hidden 256, 8 experts, top-2), pure Q4_K, llama-bench -ngl 99 -p N -ub N -n 0. Real P100s are no longer handed out by Kaggle (a NvidiaTeslaP100 push runs on T4s), so this ran on an RTX PRO 6000 (sm_120), which hits the same mismatch with the legacy build: the driver JITs the compute_61 PTX (pascal_older bounds) while the host takes the >= ADA branch (8 tokens).

binary pp1 pp4 pp5 pp6 pp7 pp8
b11696 cuda12-legacy prebuilt (what the reporter runs) ok ok ok CUDA error: invalid argument same same
b11696 cuda12-older prebuilt (contains the device arch, control) ok ok ok

Same source as the b11696 prebuilt, built with the legacy arch list (50-virtual;61-virtual, CUDA 12.8), on a B200 (sm_100, same mismatch):

build pp1 pp4 pp5 pp6 pp7 pp8 test-backend-ops -o MUL_MAT_ID -b CUDA0
b11696 cuda12-legacy prebuilt ok ok ok invalid argument same same
b11696 source ok ok ok invalid argument same same aborts, CUDA error: invalid argument
b11696 source + this PR ok ok ok ok ok ok 993/993 passed

The crash line matches the report exactly: current device: 0, in function ggml_cuda_kernel_launch at .../ggml-cuda/common.cuh:1743.

The first prebuilt carrying ggml-org#27828 is b11642-mix (2026-10-15); b11541-mix and older are unaffected, which matches "since last update".

get_mmvq_mmid_max_batch() compared the device's cc, while the kernel's
__launch_bounds__ come from get_mmvq_mmid_max_batch_for_device(), i.e. the
__CUDA_ARCH__ the driver actually loaded. Since ggml-org#27828 a P100 (cc 600) is
allowed 8 tokens, but a build without sm_60 (e.g. the default
50-virtual;61-virtual list) runs compute_50 PTX there, whose Q4_K MoE kernel
is bounded at 160 threads. Launching 6-8 tokens then fails with
'CUDA error: invalid argument' in mul_mat_vec_q_switch_ncols_dst.

Use ggml_cuda_highest_compiled_arch(cc), as get_device_table_id() already
does. Builds that contain the device's own arch are unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 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-18T14:37:58.032435Z 22fb037 Manual request
🔒 Security Review ✅ Completed 2026-10-18T13:38:26.389080Z 838118f 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-ceefd16da October 18, 2026 13:34
@danielhanchen danielhanchen self-assigned this Oct 18, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 838118fdeb

ℹ️ 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".

if (cc == GGML_CUDA_CC_PASCAL || cc == GGML_CUDA_CC_VOLTA || cc >= GGML_CUDA_CC_ADA_LOVELACE) {
// Key on the arch the kernels were compiled for, as get_mmvq_mmid_max_batch_for_device does:
// a P100 running 50-virtual PTX has __launch_bounds__ from the pascal_older table.
const int arch = ggml_cuda_highest_compiled_arch(cc);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Distinguish loadable PTX from real-only cubins

For a valid mixed build such as CMAKE_CUDA_ARCHITECTURES=61-virtual;75-real running on an sm_89 GPU, __CUDA_ARCH_LIST__ loses the -virtual/-real distinction, so this helper returns 750 even though the sm_75 cubin is not binary-compatible with sm_89 and the driver must JIT the compute_61 PTX. The device kernel therefore uses the Pascal/older launch bound while the host selects the Turing cap (for example, Q2_K permits 7 warps instead of 4), recreating the invalid launch this change is intended to prevent. Preserve whether each target supplies PTX or conservatively account for every image the driver can load rather than treating the numerically highest compiled target as the loaded architecture.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not reachable from the builds this ships with: every prebuilt profile lists plain arches (real + PTX) or -virtual only, and the upstream default list keeps a PTX target per major, so the highest compiled arch <= cc is what the driver loads. A hand-made real-only cross-major list (61-virtual;75-real on sm_89) also misleads get_device_table_id() and the other ggml_cuda_highest_compiled_arch() callers today; that is a broader pre-existing limitation of CUDA_ARCH_LIST, not something this fix introduces.

Comment thread ggml/src/ggml-cuda/mmvq.cu Outdated
Comment on lines +288 to +289
// Key on the arch the kernels were compiled for, as get_mmvq_mmid_max_batch_for_device does:
// a P100 running 50-virtual PTX has __launch_bounds__ from the pascal_older table.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep the explanatory comment on one line

The new explanation is a single sentence split after the colon, contrary to the repository rule that comments must not be hard-wrapped or split mid-sentence; collapse it into one comment line so the change follows the required review conventions.

AGENTS.md reference: AGENTS.md:L77-L84

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 22fb037: one line now.

@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. Breezy!

Reviewed commit: 838118fdeb

ℹ️ 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".

@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. Keep them coming!

Reviewed commit: 22fb037b1b

ℹ️ 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".

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.

[Bug] P100 now crashing since last update

1 participant