Skip to content

CUDA: concat of non-contiguous sources with one element per thread on Blackwell - #418

Open
danielhanchen wants to merge 2 commits into
base/upstream-418fe97b5from
inkperf4-up-concat
Open

danielhanchen wants to merge 2 commits into
base/upstream-418fe97b5from
inkperf4-up-concat

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Pin for the prebuilt: upstream b12602 (418fe97) plus this one commit. Base branch base/upstream-418fe97b5.

What it changes

A concat with a non-contiguous source ran one block of 256 threads per row of the destination. That leaves most threads idle for short rows (a convolution window concatenated per channel and sequence: ne0 = 4) and most SMs idle for few long rows (one token of a strided view). This copies one element of the contiguous destination per thread instead, with fastdiv for the indices. A copy, so the output is bit-identical.

It is taken on NVIDIA cc 10.0 and newer only, where it measured faster. Older NVIDIA GPUs and other vendors keep the one-block-per-row kernel. GGML_CUDA_DISABLE_CONCAT_FLAT=1 keeps the old kernel everywhere. test-backend-ops: CONCAT of short strided rows (the existing CONCAT cases cover the other layouts).

Evidence (B200, one exclusive card, load-gated runs)

  • Inkling-2-Mini, with the rest of the decode set, llama-batched-bench -c 4096 -npp 128 -ntg 64 -kvu, 5 rounds ABBA, median [min-max], aggregate tok/s:

    file sequences old kernel (kill switch) this change
    UD-Q8_K_XL 4 261.7 [251.7-264.0] 268.8 [260.6-269.4] +2.7%
    UD-Q8_K_XL 8 352.3 [347.3-373.8] 380.8 [350.4-384.4] +8.1%
    UD-Q4_K_XL 4 288.8 [262.9-295.7] 304.7 [296.2-305.4] +5.5%
    UD-Q4_K_XL 8 401.8 [388.1-404.1] 412.2 [411.3-413.6] +2.6%
  • tg128 at 1 sequence: tie (most of the single-token concats are absorbed by the convolution and depth-mix fusions).

  • KLD against the pin set: PPL string equal, mean KLD 0, top-1 100%.

Other GPUs (Colab, one card each, the decode set with only this switched off against the set in the same rounds; llama-bench pp512 / tg128)

GPU (cc) file pp512 with it off tg128 with it off
T4 (7.5) UD-IQ1_S +2.8% +2.0%
L4 (8.9) UD-IQ1_S / UD-Q4_K_XL +0.0% / +0.3% +0.0% / +0.0%
A100 (8.0) UD-IQ1_S / UD-Q4_K_XL / UD-Q8_K_XL -0.3% / -0.1% / +0.1% -0.1% / -0.1% / +0.1%
RTX PRO 6000 (12.0) UD-IQ1_S / UD-Q4_K_XL / UD-Q8_K_XL -0.5% / -0.2% / -0.8% -0.7% / +0.0% / +0.0%

… Blackwell

A concat with a non-contiguous source ran one block of 256 threads per row of
the destination. That leaves most threads idle for short rows (a convolution
window concatenated per channel and sequence: ne0 = 4) and most SMs idle for
few long rows (one token of a strided view). Copy one element of the
contiguous destination per thread instead, with fastdiv for the indices.

Taken on NVIDIA cc 10.0 and newer, where it was measured faster: B200 4 / 8
sequence decode +3-7% (Inkling-2-Mini), RTX PRO 6000 prefill +0.2-0.8%. It is
a tie on A100 and L4 and 2-3% slower on T4, so those and other vendors keep
the one-block-per-row kernel.

Kill switch: GGML_CUDA_DISABLE_CONCAT_FLAT=1.

A copy, so the output is bit-identical. test-backend-ops: concat of short
strided rows (the existing CONCAT cases cover the other layouts).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 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-23T04:18:13.347658Z 153a686 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: 73acc9a0c8

ℹ️ 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/concat.cu Outdated
Comment on lines +155 to +157
// non-contiguous sources: one thread per element of the contiguous dst instead of one block per row, which leaves
// most threads idle for short rows (e.g. a convolution window per channel) and most SMs idle for few long rows
// (e.g. one token of a view)

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 explanatory comments on sentence-aligned lines

This comment splits one sentence across three lines, contrary to the explicit rule in AGENTS.md against splitting sentences or hard-wrapping comments. Keep the explanation on one line or rewrite it as separate concise sentences; the performance explanation above concat_flat_cc_ok() needs the same adjustment.

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.

Fixed in 153a686. Both comments, here and above concat_flat_cc_ok(), now put one sentence on each line.

Comment thread ggml/src/ggml-cuda/concat.cu Outdated
} else if (ggml_is_contiguous(dst) && ggml_nelements(dst) < INT_MAX && !ggml_is_quantized(src0->type) &&
concat_flat_cc_ok() && !concat_flat_disabled()) {
const int n = (int) ggml_nelements(dst);
const int num_blocks = (n + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;

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 Widen the block-count arithmetic before rounding up

On Blackwell, a non-contiguous concat whose output has an element count near INT_MAX passes the new gate but overflows the signed addition used to calculate num_blocks. For example, a contiguous destination shaped [8, 268435455, 1, 1] has 2,147,483,640 elements: adding 255 exceeds INT_MAX, producing an invalid launch dimension under ordinary wrapping behavior instead of the required 8,388,608 blocks. The previous strided kernel does not perform this overflowing calculation. Use 64-bit arithmetic for the rounding expression or tighten the dispatch bound.

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 153a686. The rounding is now done in 64 bits on ggml_nelements(dst) and the gate still requires fewer than INT_MAX elements. That caps the grid at 8388608 blocks, well under the x-dimension limit, and keeps the largest thread index at INT_MAX. I checked the [8, 268435455] case from this comment with a standalone test. The old code fails the launch with invalid argument and the new code gives exact output. I did not add it to test-backend-ops because it needs about 17 GB of VRAM and a CPU reference over 2^31 elements.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 153a68697f

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

1 participant