Skip to content

ggml-cuda : convert contiguous tensors four elements at a time - #29155

Merged
IMbackK merged 2 commits into
ggml-org:masterfrom
pwilkin:cuda-convert-contiguous-vec4
Sep 21, 2026
Merged

IMbackK merged 2 commits into
ggml-org:masterfrom
pwilkin:cuda-convert-contiguous-vec4

Conversation

@pwilkin

@pwilkin pwilkin commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

As in topic. Helps BF16 prefill quite a bit.

Additional information

Columns are grouped by GPU:

  • A — AMD Radeon 8060S Graphics (local, hip/ROCm0)
  • B — AMD Radeon AI PRO R9700 (jaskinia, hip/ROCm0)
  • C — NVIDIA GeForce RTX 5060 Ti (jaskinia, cuda/CUDA0)
model · quant depth A pp before A pp after A pp ±% A tg before A tg after A tg ±% A ppl before A ppl after A ppl ±% B pp before B pp after B pp ±% B tg before B tg after B tg ±% B ppl before B ppl after B ppl ±% C pp before C pp after C pp ±% C tg before C tg after C tg ±% C ppl before C ppl after C ppl ±%
qwen35-0.8b · BF16 0 7,516.3 7,936.7 +5.4% 93.0 92.9 -0.3% 17.0671 17.0671 +0.0% 14,072.7 14,176.9 +1.9% 156.1 156.8 +0.6% 17.0635 17.0635 +0.0% 15,462.7 16,372.3 +5.9% 210.6 210.4 -0.0% 17.0672 17.0672 +0.0%
qwen35-0.8b · Q4_0 0 8,249.4 8,235.0 -0.3% 213.7 210.6 -0.2% 19.6528 19.6528 +0.0% 13,325.0 13,140.8 -1.3% 222.4 222.1 -0.4% 19.6315 19.6315 +0.0% 17,358.8 17,366.2 -0.0% 406.7 406.3 -0.1% 19.6324 19.6324 +0.0%
qwen35-4b · Q3_K_M 0 1,616.8 1,637.2 +1.3% 63.5 63.5 +0.0% 9.4722 9.4722 +0.0% 5,017.1 5,027.5 +2.0% 109.7 109.5 -0.1% 9.4614 9.4614 +0.0% 4,414.6 4,414.8 +0.0% 115.6 115.6 +0.0% 9.4693 9.4693 +0.0%
qwen35-4b · IQ4_XS 0 1,741.7 1,749.0 -0.0% 63.0 63.1 +0.1% 9.1881 9.1881 +0.0% 5,887.8 5,901.2 +0.3% 119.3 118.3 -0.9% 9.1678 9.1678 +0.0% 5,095.6 5,096.4 -0.0% 128.4 128.4 +0.0% 9.1666 9.1666 +0.0%
qwen35-9b · Q5_K_M 0 944.8 944.6 +0.3% 31.1 30.9 -0.5% 7.5606 7.5606 +0.0% 3,347.8 3,322.8 -1.2% 72.6 72.5 +0.1% 7.5303 7.5303 +0.0% 2,962.9 2,961.7 -0.1% 61.1 61.1 -0.0% 7.5288 7.5288 +0.0%
llama32-1b · Q3_K_L 0 7,012.9 6,961.8 -0.6% 212.6 212.9 +0.3% 15.7118 15.7118 +0.0% 14,565.5 14,551.7 -0.2% 362.1 361.7 -0.3% 15.7061 15.7061 +0.0% 18,777.5 18,777.7 +0.0% 387.6 387.6 -0.0% 15.6803 15.6803 +0.0%
llama32-1b · Q4_K_S 0 7,263.5 7,268.8 -0.6% 215.0 215.0 +0.0% 14.9546 14.9546 +0.0% 16,458.0 16,746.9 +1.8% 389.6 389.2 -0.1% 14.9410 14.9410 +0.0% 21,242.2 21,250.2 +0.0% 415.0 415.0 +0.0% 14.9313 14.9313 +0.0%
llama32-1b · Q8_0 0 7,502.4 7,478.9 +0.6% 141.9 141.9 +0.4% 14.3507 14.3507 +0.0% 18,553.9 18,555.7 -0.0% 305.3 305.5 +0.0% 14.3518 14.3518 +0.0% 21,909.0 21,913.2 -0.0% 271.3 271.2 -0.0% 14.3563 14.3563 +0.0%
llama32-3b · Q5_K_S 0 2,550.6 2,554.2 -0.0% 79.4 79.8 -0.3% 10.6707 10.6707 +0.0% 9,646.3 9,622.6 -0.4% 174.3 174.2 +0.3% 10.6527 10.6527 +0.0% 8,021.6 8,019.4 -0.1% 156.8 156.8 +0.0% 10.6647 10.6647 +0.0%
llama32-3b · Q6_K 0 2,004.0 2,045.7 +2.1% 69.0 68.5 +0.0% 10.6735 10.6735 +0.0% 5,902.4 5,898.7 -0.1% 153.7 153.8 +0.1% 10.6513 10.6513 +0.0% 6,680.7 6,679.5 +0.0% 138.7 138.7 +0.0% 10.6551 10.6551 +0.0%
lfm25-8b-a1b · Q4_K_M 0 3,964.6 3,959.5 -1.6% 148.3 148.5 -0.1% 27.2198 27.2198 +0.0% 10,536.4 10,655.0 +4.8% 254.1 253.5 -0.1% 27.5204 27.5204 +0.0% 8,842.0 8,836.7 -0.1% 299.0 299.1 +0.1% 27.3439 27.3439 +0.0%
gemma4-12b · Q3_K_S 0 697.2 697.8 -0.3% 30.3 30.2 +0.1% 1065.8745 1065.8745 +0.0% 2,166.6 2,166.0 -0.2% 59.0 59.0 +0.2% 1085.2913 1085.2913 +0.0% 1,853.2 1,853.4 +0.2% 55.5 55.5 +0.3% 1115.2470 1115.2470 +0.0%
gemma4-12b · IQ4_NL 0 777.1 776.4 -0.3% 27.1 27.1 -0.4% 521.4601 521.4601 +0.0% 2,794.4 2,792.3 +0.6% 63.1 63.4 +0.3% 520.4930 520.4930 +0.0% 2,351.1 2,353.1 -0.0% 49.5 50.9 +0.8% 532.3087 532.3087 +0.0%

Requirements

convert_unary handles the contiguous case through the general strided kernel,
one element per thread: each lane reads 4 bytes and writes 2. Converting the
activations for a bf16 matrix multiplication that way moves 126 MB in 1021 us
on gfx1151, about 65% of what the memory system can do.

Give the contiguous path its own kernel that takes four elements per thread
through a vector type, so a warp loads 512 bytes at a time instead of 128. It
is used only when the element count is a multiple of four and both pointers
carry the alignment the vector type needs, and falls back to the strided
kernel otherwise.

Model level, Qwen3.8-Next-Flash IQ3_XXS on gfx1151, llama-bench -ub 2048 -r 6,
mean of the last 3 reps, ABBA counterbalanced:

    pp2048   688.0 680.0  ->  694.3 691.1   +1.26%
    tg128     24.8  24.8  ->   24.8  24.8   +0.14%

Every conversion in a prefill takes the new kernel (kernel trace: 1146
convert_unary_cont_vec4, no convert_unary). Output is bit identical; MUL_MAT,
MUL_MAT_ID, CPY, CONT, GET_ROWS and SET_ROWS pass.

Assisted-by: Claude Opus 5
@pwilkin
pwilkin requested a review from a team as a code owner September 19, 2026 20:07
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Sep 19, 2026

@IMbackK IMbackK left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this complies down to global_load_dwordx4 on hip so nice.
tiny nit: the comment is wrong on cdna where we load 1K per warp.

Clarified comment on memory transfer size for different architectures.
@pwilkin

pwilkin commented Sep 21, 2026

Copy link
Copy Markdown
Member Author

Aight, corrected the comment, can I get a reapproval please?

@IMbackK
IMbackK merged commit f4e276a into ggml-org:master Sep 21, 2026
12 checks passed
@BrewTestBot BrewTestBot mentioned this pull request Sep 23, 2026
1 task done
iki007 added a commit to iki007/llama.cpp that referenced this pull request Sep 27, 2026
Port of CUDA ggml-org#29155: aligned contiguous conversions with a multiple of four
elements take a kernel that moves four elements per work-item; the rest keep
the strided kernel.

Arc Pro B70 pp2048: Qwen3.8-27B UD-Q4_K_M +1.75%, Qwen3.6-35B-A3B Q4_K_M +0.69%.

Assisted-by: Claude Opus 5.5
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 2, 2026
…org#29155)

convert_unary handles the contiguous case through the general strided kernel,
one element per thread: each lane reads 4 bytes and writes 2. Converting the
activations for a bf16 matrix multiplication that way moves 126 MB in 1021 us
on gfx1151, about 65% of what the memory system can do.

Give the contiguous path its own kernel that takes four elements per thread
through a vector type, so a warp loads 512 bytes at a time instead of 128. It
is used only when the element count is a multiple of four and both pointers
carry the alignment the vector type needs, and falls back to the strided
kernel otherwise.

Model level, Qwen3.8-Next-Flash IQ3_XXS on gfx1151, llama-bench -ub 2048 -r 6,
mean of the last 3 reps, ABBA counterbalanced:

    pp2048   688.0 680.0  ->  694.3 691.1   +1.26%
    tg128     24.8  24.8  ->   24.8  24.8   +0.14%

Every conversion in a prefill takes the new kernel (kernel trace: 1146
convert_unary_cont_vec4, no convert_unary). Output is bit identical; MUL_MAT,
MUL_MAT_ID, CPY, CONT, GET_ROWS and SET_ROWS pass.

Assisted-by: Claude Opus 5
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 4, 2026
…org#29155)

convert_unary handles the contiguous case through the general strided kernel,
one element per thread: each lane reads 4 bytes and writes 2. Converting the
activations for a bf16 matrix multiplication that way moves 126 MB in 1021 us
on gfx1151, about 65% of what the memory system can do.

Give the contiguous path its own kernel that takes four elements per thread
through a vector type, so a warp loads 512 bytes at a time instead of 128. It
is used only when the element count is a multiple of four and both pointers
carry the alignment the vector type needs, and falls back to the strided
kernel otherwise.

Model level, Qwen3.8-Next-Flash IQ3_XXS on gfx1151, llama-bench -ub 2048 -r 6,
mean of the last 3 reps, ABBA counterbalanced:

    pp2048   688.0 680.0  ->  694.3 691.1   +1.26%
    tg128     24.8  24.8  ->   24.8  24.8   +0.14%

Every conversion in a prefill takes the new kernel (kernel trace: 1146
convert_unary_cont_vec4, no convert_unary). Output is bit identical; MUL_MAT,
MUL_MAT_ID, CPY, CONT, GET_ROWS and SET_ROWS pass.

Assisted-by: Claude Opus 5
(cherry picked from commit f4e276a)
LadislavSopko pushed a commit to 0ics-srls/llama.cpp that referenced this pull request Oct 5, 2026
…org#29155)

convert_unary handles the contiguous case through the general strided kernel,
one element per thread: each lane reads 4 bytes and writes 2. Converting the
activations for a bf16 matrix multiplication that way moves 126 MB in 1021 us
on gfx1151, about 65% of what the memory system can do.

Give the contiguous path its own kernel that takes four elements per thread
through a vector type, so a warp loads 512 bytes at a time instead of 128. It
is used only when the element count is a multiple of four and both pointers
carry the alignment the vector type needs, and falls back to the strided
kernel otherwise.

Model level, Qwen3.8-Next-Flash IQ3_XXS on gfx1151, llama-bench -ub 2048 -r 6,
mean of the last 3 reps, ABBA counterbalanced:

    pp2048   688.0 680.0  ->  694.3 691.1   +1.26%
    tg128     24.8  24.8  ->   24.8  24.8   +0.14%

Every conversion in a prefill takes the new kernel (kernel trace: 1146
convert_unary_cont_vec4, no convert_unary). Output is bit identical; MUL_MAT,
MUL_MAT_ID, CPY, CONT, GET_ROWS and SET_ROWS pass.

Assisted-by: Claude Opus 5
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 21, 2026
Port of CUDA ggml-org#29155: aligned contiguous conversions with a multiple of four
elements take a kernel that moves four elements per work-item; the rest keep
the strided kernel.

Arc Pro B70 pp2048: Qwen3.8-27B UD-Q4_K_M +1.75%, Qwen3.6-35B-A3B Q4_K_M +0.69%.

Assisted-by: Claude Opus 5.5
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 22, 2026
Port of CUDA ggml-org#29155: aligned contiguous conversions with a multiple of four
elements take a kernel that moves four elements per work-item; the rest keep
the strided kernel.

Arc Pro B70 pp2048: Qwen3.8-27B UD-Q4_K_M +1.75%, Qwen3.6-35B-A3B Q4_K_M +0.69%.

Assisted-by: Claude Opus 5.5
nicheng251 added a commit to nicheng251/bonsai-turboquant-llama.cpp that referenced this pull request Oct 23, 2026
Port five small upstream fixes that apply to code this fork ships:

- fattn-vec: sync the warp at the end of the VKQ loop (ggml-org#30573). The next
  iteration's KQ write reused the shared buffer while other lanes were
  still reading it.
- cpy: the transposed fast path requires a contiguous destination (ggml-org#27663).
  It wrote to a non-contiguous dst as if it were dense.
- llama-kv-cache: only upload k_rot when the tensor has a buffer (ggml-org#27967).
  An unquantized K cache has no k_rot buffer and context shift crashed.
- ggml_permute: keep dims and strides 64-bit (ggml-org#29227). The int temporaries
  truncated large tensors.
- convert: run contiguous conversions four elements per thread (ggml-org#29155).

Verified: FLASH_ATTN_EXT turbo 25/25, 27B Bonsai PTQ1_0 model smoke with
q8_0 K + turbo3 V (prefill 547 tok/s, decode 31.2 tok/s at 2K; 30.0 at 17.4K).

The stock q8_0/q8_0 long-sweep crash is not caused by the WAR hazard: it
still aborts after 361 cases with this fix.
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 29, 2026
Port of CUDA ggml-org#29155: aligned contiguous conversions with a multiple of four
elements take a kernel that moves four elements per work-item; the rest keep
the strided kernel.

Arc Pro B70 pp2048: Qwen3.8-27B UD-Q4_K_M +1.75%, Qwen3.6-35B-A3B Q4_K_M +0.69%.

Assisted-by: Claude Opus 5.5
iki007 added a commit to iki007/llama.cpp that referenced this pull request Nov 4, 2026
Port of CUDA ggml-org#29155: aligned contiguous conversions with a multiple of four
elements take a kernel that moves four elements per work-item; the rest keep
the strided kernel.

Arc Pro B70 pp2048: Qwen3.8-27B UD-Q4_K_M +1.75%, Qwen3.6-35B-A3B Q4_K_M +0.69%.

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants