Skip to content

CUDA: refactor swizzling code - #29612

Merged
JohannesGaessler merged 2 commits into
ggml-org:masterfrom
JohannesGaessler:cuda-fa-swizzle-refactor-2
Oct 4, 2026
Merged

JohannesGaessler merged 2 commits into
ggml-org:masterfrom
JohannesGaessler:cuda-fa-swizzle-refactor-2

Conversation

@JohannesGaessler

Copy link
Copy Markdown
Contributor

I refactored and generalized the CUDA swizzling code a bit with support for strides that are not a multiple of 128 bytes as well as Volta and AMD. Unfortunately the performance for those cases was worse than with padding so the corresponding code paths are disabled. Still, I think we should keep them since they will be potentially useful in the future due to the reduced VRAM requirements.

The functions load_ldmatrix_swizzled and load_ldmatrix_trans_swizzled could become replacements for the non-_swizzled variants in the future since they are a superset of the functionality if the stride is known at compile time. However, I am not yet 100% sure about this so I'm avoiding mass-changing the kernels that use them for now.

Requirements

@JohannesGaessler
JohannesGaessler requested a review from a team as a code owner September 28, 2026 20:14
@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 28, 2026
@ynankani ynankani self-assigned this Sep 29, 2026
Comment thread ggml/src/ggml-cuda/mma.cuh Outdated
: "l"(xs0 + offset_ij));
#elif defined(VOLTA_MMA_AVAILABLE)
#pragma unroll
for (int o = 0; o < 8; o += 4) {

@ynankani ynankani Sep 30, 2026 •

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.

the array bound should be below?

Suggested change
for (int o = 0; o < 8; o += 4) {
for (int o = 0; o < t.ne; o += 4) {

Comment thread ggml/src/ggml-cuda/fattn-mma-f16.cuh Outdated

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.

looks like bool swz is no longer needed in the template declaration.

Comment thread ggml/src/ggml-cuda/fattn-mma-f16.cuh Outdated

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.

same swz arg is not needed in this call

Comment thread ggml/src/ggml-cuda/fattn-mma-f16.cuh Outdated

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.

same swz arg is not needed in this call

Comment thread ggml/src/ggml-cuda/fattn-mma-f16.cuh Outdated

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.

same swz arg is not needed in this call

Comment thread ggml/src/ggml-cuda/fattn-mma-f16.cuh Outdated

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.

same swz arg is not needed in this call

Comment thread ggml/src/ggml-cuda/fattn-mma-f16.cuh Outdated

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.

same swz arg is not needed in this call

@JohannesGaessler
JohannesGaessler force-pushed the cuda-fa-swizzle-refactor-2 branch from 7f23352 to 3d2fb8f Compare October 1, 2026 21:49
@ynankani

ynankani commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

@IMbackK would you like to take look at AMD side changes and approve?

@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.

Its fine from a hip perspective, It looks correct statically to me and passes tests when enabled on cdna.
I dident bother trying to determine performance or if the layout is ideal since it will remain disabled on hip for now

@JohannesGaessler
JohannesGaessler merged commit 2ca15f5 into ggml-org:master Oct 4, 2026
14 of 15 checks passed
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 8, 2026
* CUDA: refactor swizzling code

* fix templates/loop bounds

(cherry picked from commit 2ca15f5)
@ServeurpersoCom

Copy link
Copy Markdown
Contributor

Hey @JohannesGaessler, since this refactor the HIP quality check is red on master: flash_attn_ext_f16<320, 256, 2, 32> now uses 257 VGPRs on gfx908, one spill over the 256 limit. Would you rather recover that register in the kernel, or should I add it to the ignore list in gcn-cdna-vgpr-check.py next to the other big head sizes?

@JohannesGaessler

Copy link
Copy Markdown
Contributor Author

I don't remember where it was but I think in some slop fork someone claimed that the original swizzling PR was problematic for compiler optimizations on AMD because of the cast to char * and back again. I was unable to measure any performance differences on my AMD GPUs. So I would say to ignore it.

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

I don't remember where it was but I think in some slop fork someone claimed that the original swizzling PR was problematic for compiler optimizations on AMD because of the cast to char * and back again. I was unable to measure any performance differences on my AMD GPUs. So I would say to ignore it.

Perfect, thanks! Small fix CI here then: #30212

mclrcha added a commit to mclrcha/llama.cpp that referenced this pull request Oct 10, 2026
…-10-10)

Conflicts:
- ggml-backend.cpp: upstream moved input copies to ggml_backend_sched_copy_input with host weights last (ggml-org#29943);
  the GGML_SCHED_BATCH_INPUTS fast path now runs in the first loop before falling back to it.
- ggml-cuda/fattn-common.cuh: launch_fattn takes upstream async_kv_preload, then our parallel_blocks_fixed
  (GQA-6 tile launcher updated).
- ggml-cuda/fattn-mma-f16.cuh: upstream swizzle refactor (ggml-org#29612); its helpers take ncols2 like the rest of
  the fork's config getters.
- ggml-cuda/gated_delta_net.cu: keep the fork's kernel and launch geometry (its columns_per_block template
  argument is not upstream's cols_per_warp, ggml-org#30087).
- ggml-cuda/mmq.cu: upstream per-tile src1 padding (ggml-org#29953); ggml_cuda_mul_mat_q_src1_nbytes and the MMQ input
  cache use the worst-case padding so producers and projections with other tiles can share the buffer.
- ggml-cuda/mmq-vec-dot.cuh: fork vec_dot kernels pass GGML_PREC_Q8 to the config helpers (ggml-org#30168).
- ggml-cuda/mmvf.cu/.cuh: keep both includes and the HIP declarations, upstream warp_size argument.
- ggml-cuda/rope.cu: upstream grid-stride rms_norm+mul+rope (ggml-org#28175) with the fork's M-RoPE theta (nchannels
  instead of gridDim.y) and rope_store_cast.
- ggml-cuda/top-k.cu: upstream shape-based selection (ggml-org#28713), RDNA4 tournament kept for few long rows.
- src/llama-batch.*: mixed token/embd batches (ggml-org#29622) copy embd rows per entry; other batches keep pointing at
  the ext rows.
- src/llama-context.cpp: expert copy callback also on the second MTP scheduler.
- src/llama-graph.cpp: build_rs keeps the GET_ROWS of the ubatch states (GDN fusions, deferred state) and gathers
  the extra states from the tail like upstream's custom getter path (ggml-org#29856).
thelittlefireman pushed a commit to thelittlefireman/llama.cpp that referenced this pull request Oct 12, 2026
* CUDA: refactor swizzling code

* fix templates/loop bounds
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.

4 participants