Skip to content

CUDA: pack several short rows per block in mul_mat_vec_f - #400

Open
danielhanchen wants to merge 1 commit into
base/upstream-418fe97b5from
inkperf-mmvf-rows
Open

danielhanchen wants to merge 1 commit into
base/upstream-418fe97b5from
inkperf-mmvf-rows

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

mul_mat_vec_f launches one block per output row. When a row is short enough for a single warp (block size == warp size), a matrix with few columns and many rows or channels launches millions of single-warp blocks. The Inkling 2 attention residual mix is such a product ([n, n_embd, n_tokens] x [n, 1, n_tokens], n <= 25), and in prefill it was a third of the GPU time. This change gives each block up to 8 rows, one warp per row (rows_per_block 8, 4 or 2, whichever divides the launch). Every warp computes its row exactly as before, so results are bit-identical.

Evidence (B200, one exclusive card, ABBA, llama-bench -fa 1)

  • Inkling-2-Mini UD-Q4_K_XL, this commit alone on the pin set: pp512 +36%, pp4096 +25%, tg128 unchanged.
  • Inkling-2-Mini UD-IQ1_S: pp512 +45%, pp4096 +33%.
  • The mix product drops from 124 ms to 22 ms of nsys time in one pp512 forward.
  • test-backend-ops CUDA0: MUL_MAT 2829/2829 and MUL_MAT_ID pass.
  • llama-perplexity KLD against the pin set (wikitext-2, -c 512, 20 chunks): PPL string equal, mean KLD 0, top-1 100%.

Other backends

CUDA and HIP only (mmvf.cu). On HIP the warp size is 64, so a block holds 64 x up to 8 = 512 threads. On gfx1151 (Linux ROCm and Windows) MUL_MAT 2829 and MUL_MAT_ID 1177 pass on base and head.

Cross-platform base vs head (pin set with all four speed pins): HIP gfx1151 Linux ROCm and Windows, Vulkan lavapipe, Metal (macos-15), CPU and CUDA B200, no regression on any leg; test-llama-archs and the tiny Inkling 2 perplexity are identical. The GitHub macos-15 runner reports MUL_MAT unsupported, so Metal is untested for matmul (this change does not touch Metal code).

Rows short enough for one warp (block size == warp size) were launched one
block per row, so a matrix with few columns and many rows or channels, such as
the [n, n_embd, n_tokens] x [n, 1, n_tokens] product of the Inkling 2 attention
residual mix, launched millions of single-warp blocks. Give each block up to 8
rows, one warp per row; every warp computes its row exactly as before.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 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-21T21:50:01.415859Z a5667a9 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-418fe97b5 November 21, 2026 21:48
paleluna82-del added a commit to paleluna82-del/llama.cpp that referenced this pull request Nov 22, 2026
…ry depth mix

Both were n_tokens batched tiny GEMMs (rel table 1024x24x16, mix 2304x1xB), which hipBLASLt runs with
the GPU mostly idle (pp2048 767 t/s by default, 1257 with ROCBLAS_USE_HIPBLASLT_BATCHED=0).
- rel table: one GEMM over all heads and tokens (same bias matrix), Rcur flattened to 2D.
- depth mix with one query: sum over blocks as a broadcast multiply and a row sum.
R9700 with default hipBLASLt, with unslothai#400-unslothai#402 applied: pp512 1519 / pp2048 1460 / pp16384 1377 / tg128 41.8, i.e. the env
variable is no longer needed. KLD vs the previous build 0.0023, top-1 97.8% (switching only the BLAS
library on the previous build: 0.0021 / 97.9%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
paleluna82-del added a commit to paleluna82-del/llama.cpp that referenced this pull request Nov 22, 2026
Decode on this model is bound by the number of small kernels (~5600 per
token), and each tiny elementwise kernel costs ~2.2 us inside a HIP graph.

- ssm_conv: optionally fuse a residual add and a scale,
  (res + conv(x, w))*s, as SSM_CONV -> ADD -> SCALE. inkling2 builds the
  non-dynamic short convolutions in that order (192 per token).
- new fused tanh gate mix, (x - tanh(f)*x) + tanh(g)*m, for the
  UNARY(TANH) -> MUL -> SUB -> UNARY(TANH) -> MUL -> ADD subgraph of the
  loop controllers. inkling2 expands the projections and the message
  first so that the elementwise tail is contiguous.
- test-backend-ops cases for both.

R9700, Q4_K_M, q8_0 KV, with unslothai#400-unslothai#402 applied: tg128 41.8 -> 45.8 t/s, pp512 1519 -> 1559.
PPL unchanged at c512 (7.6923 -> 7.6899), KLD 0.001 vs the unfused path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
paleluna82-del added a commit to paleluna82-del/llama.cpp that referenced this pull request Nov 22, 2026
…conv on CUDA/HIP

Each non-dynamic short convolution concatenates the past columns from the
recurrent state with the new inputs, copies the last columns back to the
state, convolves, adds the residual and scales: five kernels, 192 times
per token. Fuse CONCAT -> VIEW -> VIEW -> CPY -> SSM_CONV -> ADD -> SCALE
into one kernel that reads the past from the state, keeps the window in
registers and writes the new past back (n_t <= 32, d_conv 4). inkling2
expands the conv output right after the state store so that the nodes are
adjacent. Disable with GGML_CUDA_DISABLE_SSM_CONV_HIST_FUSION.

R9700, Q4_K_M, q8_0 KV, with unslothai#400-unslothai#402 applied: tg128 45.8 -> 47.5 t/s. KLD vs unfused 0.000000
(max 5.6e-5) with ubatch 16 and with 4 sequences per ubatch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
paleluna82-del added a commit to paleluna82-del/llama.cpp that referenced this pull request Nov 22, 2026
…e same input

Several projections of one input (Q, rel, sink, gate, ...) each quantized
the same src1 to q8_1 again: 385 quantize kernels per token on
Inkling-2-Mini decode for ~290 products. Keep the q8_1 copy of the last
mul_mat_vec_q src1 in a per-context buffer and reuse it when the next
mul_mat_vec_q has the same src1 tensor, data, shape and strides. The graph
loop invalidates it when any node writes the src1 memory, at every graph
evaluation and off the main stream. The buffer (4 MiB) is allocated
outside stream capture and kept until the context is freed, so captured
graphs stay valid. Disable with GGML_CUDA_DISABLE_Q8_1_REUSE.

R9700, Inkling-2-Mini Q4_K_M, with unslothai#400-unslothai#402 applied: tg128 47.6 -> 48.4
t/s, bitwise identical (KLD 0 with ubatch 16, 4 sequences). New
test-backend-ops case MUL_MAT_SHARED_SRC1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
paleluna82-del added a commit to paleluna82-del/llama.cpp that referenced this pull request Nov 22, 2026
The depth mix (attention over the RMS-normalized earlier block outputs
with a learned query) was RMS_NORM -> RESHAPE -> CONCAT -> CONCAT ->
MUL_MAT -> SOFT_MAX -> MUL -> SUM_ROWS, 88 times per token. Fuse it into
one kernel per token that reads the stacked states and the current state
once (squares, scores), does the softmax in registers and writes the
weighted sum. The kernel reads the stack in its [E, n, T] layout only, so
the fusion checks that the transposed stack is built from the same
normalized states by the same concats. inkling2 expands the per-token
query repeat first so that the nodes stay adjacent for ubatches > 1.
Disable with GGML_CUDA_DISABLE_DEPTH_MIX_FUSION.

R9700, Inkling-2-Mini Q4_K_M, with unslothai#400-unslothai#402 applied: tg128 48.3 -> 50.8
t/s, pp512 1550 -> 1640. KLD vs unfused 0.0024 at ubatch 16 (rounding
from the reordered reductions; PPL c512 7.6899 -> 7.6851). New
test-backend-ops case DEPTH_MIX.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
erokhins pushed a commit to JetBrains-Hardware/llama.cpp that referenced this pull request Nov 24, 2026
abeloin added a commit to abeloin/llama.cpp-mix that referenced this pull request Nov 24, 2026
…hai#400, commit a5667a9)

Merge --no-ff du commit épinglé par unsloth (scripts/unsloth/pr-set.json).
Couche 2, liste A (banc/notes/couche2-unsloth-liste-2026-11-24.md côté
notes).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QBjnvFeJ4P7Jp71oC6pzWf

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