Skip to content

CUDA: few-channel MUL_MAT without a batched cuBLAS GEMM - #460

Open
danielhanchen wants to merge 6 commits into
base/upstream-8f50b1db9from
cuda-few-channel-mm
Open

danielhanchen wants to merge 6 commits into
base/upstream-8f50b1db9from
cuda-few-channel-mm

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

CUDA: few-channel MUL_MAT without a batched cuBLAS GEMM

A speculative verify batch of a few tokens turns some per-token products into cuBLAS batched GEMMs over tiny matrices, and each of those is far slower than the single product decode runs. Two shapes are affected, both at 2 to 8 src1 channels:

  • A 2D src0 broadcast over the channels (for example Inkling 2's relative-bias table, 16 features by 24 heads). This is now one GEMM over all the columns of all channels: 5.9 us down to 2.6 us.
  • A thin F32 src0 (at most 16 rows) with its own matrix per channel (Inkling 2's per-token loop controllers). This now goes to MMVF: 26.3 us down to 2.8 us.

One channel (decode) and more than 8 channels (prefill) keep their kernels, so their results do not change. The MMVF branch also requires a src1 that MMVF can read (unit element stride, data and row, channel and sample strides aligned to 2 elements). Other src1 views keep the path they took before. Both new branches return false, so the caller still applies a weight scale when the tensor has one, exactly as for the other MMVF and cuBLAS paths.

Tests

New test-backend-ops cases cover both shapes (m 1, 2, 16 by n 1, 4, 8 by k 2304 at 2, 3, 8 and 9 channels; the 1280 / 1024 by 24 by 16 table at 2, 3 and 8 channels, also with a second batch dim). Further cases use a src1 view with an odd row stride, a misaligned offset or an aligned stride, at 5 and 8 columns and 2 and 3 channels. B200: MUL_MAT 3061/3061 including the 54 new cases.

Accuracy

Inkling-2-Mini, wikitext-2 -c 512, KLD against the same build without the change at the same ubatch:

ubatch KLD same top token
1 (decode) 0 (identical) 100%
512 (prefill) 0 (identical) 100%
3 0.00076 99.1%

At 3 tokens the reordered reduction is rounding level: the unchanged build's own difference between -ub 1 and -ub 3 on the same text is 0.0023.

Speed

B200, Inkling-2-Mini UD-Q4_K_XL, llama-bench -p 3 -d 1024 (the 3-token verify step), together with the finer banded split of the Inkling 2 attention: 221 -> 250 t/s. Decode (tg) and prefill (pp512) unchanged.

Pinned for the prebuilt on b12774, with b12818 merged in to follow ggml_cuda_mul_mat now returning bool and ggml_cuda_should_use_mmvf taking the src1 type.

A speculative verify batch of a few tokens turns per-token products into cuBLAS batched GEMMs of tiny matrices, each far slower than the single product decode runs. For 2..8 src1 channels: a 2D src0 broadcast over the channels is one GEMM over all their columns (Inkling 2 relative-bias table, 5.9 -> 2.6 us), and a thin F32 src0 (<= 16 rows) with its own matrix per channel goes to MMVF (Inkling 2 loop controllers, 26.3 -> 2.8 us). One channel (decode) and more than 8 (prefill) keep their kernels, so their results are unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T12:49:30.405497Z eae1b4d Manual request
ℹ️ 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.

@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: 4e7281e16f

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

Comment thread ggml/src/ggml-cuda/ggml-cuda.cu Outdated
Comment on lines +2238 to +2240
// a thin F32 src0 with its own matrix per src1 channel at a few channels (e.g. per-token 2304x2 Inkling 2 loop
// controllers in a speculative verify batch): MMVF up to its batch size, where cuBLAS runs a batched GEMM of tiny
// products that costs 9x the single product. Decode (one channel) and prefill (many channels) keep their kernels.

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 Remove mid-sentence wrapping from the new comments

The new dispatch comments wrap sentences across lines, contrary to the explicit rule in AGENTS.md. Keep each sentence on one line and shorten the explanation where possible; the same issue occurs in the broadcast-path comment below.

AGENTS.md reference: AGENTS.md:L84-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.

Done in eae1b4d. Each comment sentence is on its own line now, and both comments are shorter. The speed numbers are gone from the code and stay in the PR description.

Comment on lines +2241 to +2244
if (src0->type == GGML_TYPE_F32 && ne01 <= 16 && ne02 == ne12 && ne03 == ne13 &&
ne12*ne13 > 1 && ne12*ne13 <= MMVF_MAX_BATCH_SIZE && ne11 <= MMVF_MAX_BATCH_SIZE &&
ggml_cuda_should_use_mmvf(src0, src1->type, cc, warp_size, /*ne11 =*/ 1, /*mul_mat_id =*/ false)) {
ggml_cuda_mul_mat_vec_f(ctx, src0, src1, nullptr, dst);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Check src1 stride alignment before forcing MMVF

On an Ampere GPU, an F32 src0 shaped [16, 2, 2, 1] and an F32 src1 view shaped [16, 4, 2, 1] with a 17-float row stride now enter this branch, but launch_mul_mat_vec_f_cuda asserts that stride_col_y % 2 == 0, aborting evaluation. Previously this case bypassed MMVF and MMF and ran successfully through cuBLAS. The eligibility helper checks only src0 alignment, so also validate src1's MMVF stride/alignment requirements before selecting this path and retain the fallback for incompatible views.

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.

Confirmed. launch_mul_mat_vec_f_cuda asserts stride_col_y % 2 == 0, ggml_cuda_mul_mat_vec_f asserts that nb10 is the src1 type size, and the kernel reads src1 as pairs of values, so the src1 data pointer and its channel and sample strides must also be 2-element aligned. ggml_cuda_should_use_mmvf only checks src0. On Ampere and newer, the old path kept 4 or more src1 columns off MMVF, and the new branch skipped that limit.

Fixed in eae1b4d: the few-channel branch now also requires nb10 == ggml_type_size(src1->type) and ggml_cuda_is_aligned(src1, 2*ggml_type_size(src1->type)) (data pointer, nb1, nb2, nb3). Any other src1 view falls through to the path it took before, which is cuBLAS for these shapes.

Test: new test_mul_mat_view_b cases under -o MUL_MAT. src0 is [16, m, bs, 1] and contiguous. src1 is a [16, n, bs, 1] view, for m 2 and 16, n 5 and 8, bs 2 and 3, in three layouts:

  • rows of 17 floats (odd stride);
  • rows of 18 floats at a 1-float offset (misaligned data);
  • rows of 18 floats at a 2-float offset (aligned, so the branch still runs MMVF on a strided view).

On a B200, using the new test binary with the old library, the odd-stride cases abort on GGML_ASSERT(stride_col_y % 2 == 0) and the misaligned cases abort with CUDA error: misaligned address. With this commit all 12 pass. Your exact shape (src0 [16, 2, 2, 1], src1 [16, 4, 2, 1] with a 17-float row stride) also aborts before and passes after.

The cases use 5 and 8 columns because at 3 or fewer columns (4 or fewer on Turing), upstream's own MMVF branch already takes such a view without checking src1. That branch aborts the same way with or without this PR, also at a single channel. I left it alone here because it is not part of this change.

On the pin set rebuilt with this commit: MUL_MAT 3061/3061 (the previous 3049 plus these 12), and the few-channel feature checks 36/36 and 6/6. Inkling-2-Mini perplexity and KLD at -ub 512 and -ub 3 are identical to the previous head (KLD 0).

MMVF reads src1 in pairs and asserts an even row stride, so a src1 view
with an odd row stride, an odd offset or a non-unit element stride now
skips the few-channel MMVF branch and takes cuBLAS as before. Add
test cases with such views, and shorten the comments.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: eae1b4db8f

ℹ️ 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 added a commit that referenced this pull request Dec 6, 2026
 for the macOS -Wformat failure and the b12986+ / b13007+ / b13013+ conflicts (#500)
MMVF reads src1 two values at a time and asserts an even column stride. The few-channel path already keeps such views
off MMVF, but the general MMVF route did not check src1 at all. On AMD it takes F32 weights up to 8 columns, so the
odd-stride view cases abort test-backend-ops (GGML_ASSERT(stride_col_y % 2 == 0)); on NVIDIA the same happens at 1 to 3
columns. A float src0 with such a src1 now goes to cuBLAS, which those views used before. The view tests also cover 1
and 3 columns, which reach MMVF on NVIDIA.

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