Repository navigation
CUDA: concat of non-contiguous sources with one element per thread on Blackwell - #418
danielhanchen wants to merge 2 commits into
Conversation
… 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).
Codex Review Summary: ✅ CompletedThis 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. |
There was a problem hiding this comment.
💡 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".
| // 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
Fixed in 153a686. Both comments, here and above concat_flat_cc_ok(), now put one sentence on each line.
| } 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; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 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". |
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: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)