Skip to content

cuda: fuse the Gemma 4 MoE layer tail (GGML_CUDA_TAIL_FUSION, off by default) - #414

Merged
bri-prism merged 4 commits into
prismfrom
gemma/tail-fusion
Oct 29, 2026
Merged

bri-prism merged 4 commits into
prismfrom
gemma/tail-fusion

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

Depends on #413 (stacked on gemma/tall-ext; the diff here includes its two commits until it merges).

What

  • Fuses the end of each Gemma 4 MoE layer into one kernel: the three RMS_NORM+MUL(+ADD) groups and the layer-scalar MUL.
  • Optionally folds in the next layer's attn_norm (level 2) and the q8_1 quantization of that normalized activation for the next attention mat-vecs (level 3).
  • GGML_CUDA_TAIL_FUSION=0/1/2/3 selects the level (default 0, off).
  • GGML_CUDA_TAIL_VERIFY=1 also runs the unfused nodes and compares the outputs bit-for-bit. It is a debug mode and keeps a 128 MB scratch while enabled.

Why

  • Decode on these models is one long chain of small dependent kernels. The four tail kernels are serial single-block norms on that chain, so removing them shortens every token.

How

  • One block per row (ncols <= 3072), with the same per-thread partial sums, reductions and expression forms as the unfused kernels, so the result is bit-identical.
  • Adds an ffn_res node name in gemma4.cpp so the pattern can be matched.
  • Level 3 writes the q8_1 rows with the same arithmetic as quantize_q8_1. It is only used when every consumer is a plain MUL_MAT with the AOS layout.
  • When the next attn_norm feeds a Hadamard-rotated input, levels 2/3 step aside so the existing norm+FWHT fusion still applies.

Testing (H200, this branch)

…=4096 on Hopper (GGML_CUDA_MMVQ_TALL_EXT)

Extend the 8-rows-per-CTA one-column mat-vec geometry, previously used for PQ2_0 only,
to Q8_0, Q4_0, Q2_0, Q4_K and Q6_K when the matrix has at least 4096 rows (a multiple of 8)
on Hopper. The K partition and reduction order per row are unchanged, so every output row
is bit-identical to the default geometry.

GGML_CUDA_MMVQ_TALL_EXT=0 restores the PQ2_0-only behavior (default: enabled).
Every folded Gemma 4 weight (attention q/k/v/o, shared FFN, routed experts) goes through build_lora_mm / build_lora_mm_id, which apply the activation transform.
…_FUSION=1)

Fuse the three RMS_NORM+MUL(+ADD) groups and the layer-scalar MUL at the end of the
Gemma 4 MoE layer into a single launch (one block per row, ncols <= 3072). The kernel
uses the same per-thread partial sums, reductions and expression forms as the unfused
kernels, so the result is bit-identical. Add an "ffn_res" node name in gemma4.cpp so the
group can be matched.

GGML_CUDA_TAIL_FUSION=1 enables it (default 0: off).
GGML_CUDA_TAIL_VERIFY=1 additionally runs the unfused nodes and compares the outputs
bit-for-bit, printing TAILVERIFY lines on mismatch (default 0: off).
…tail kernel (GGML_CUDA_TAIL_FUSION=2/3)

Level 2: the next layer's attn_norm (RMS_NORM+MUL) joins the same launch.
Level 3: the kernel also writes the q8_1 rows of the normalized activation that the
mat-vecs of the next attention block would otherwise quantize (all consumers are plain
MUL_MATs with the AOS layout), using the same expressions as quantize_q8_1.

GGML_CUDA_TAIL_FUSION=0/1/2/3 selects the level (default 0: off). With
GGML_CUDA_TAIL_VERIFY=1 the level-2/3 outputs are compared bit-for-bit against the
unfused nodes as well.
@bri-prism
bri-prism merged commit 340befd into prism Oct 29, 2026
8 checks passed
bri-prism added a commit that referenced this pull request Oct 29, 2026
Resolve the prism.hadamard loader conflict by keeping load_hparams_hadamard()
and adding Gemma 4 to its arch whitelist (#413). Add the SWIGLU_CLAMP case to
the PTQ1_0 warp-per-row epilogue from #415, since this branch has that op.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant