Skip to content

cuda: enable the Hopper wgmma Q1_0/PQ2_0 prefill path by default - #169

Closed
bri-prism wants to merge 4 commits into
prismfrom
perf/hopper-q1-default-on-v7
Closed

bri-prism wants to merge 4 commits into
prismfrom
perf/hopper-q1-default-on-v7

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

What

Flips the runtime gate on the Hopper (sm_90a) wgmma prefill path from opt-in to opt-out. In a build configured with GGML_CUDA_HOPPER_Q1, the path is now active unless GGML_HOPPER_Q1_DISABLE is set.

The build-time option is deliberately left off by default, because it requires a CUTLASS checkout via GGML_CUDA_CUTLASS_DIR and would otherwise break configuration for everyone else. See "Scope" below for what that means in practice.

Lineage

Supersedes #106, which was opened against the now-closed prism base. Its head branch
had fallen 1131 commits behind that base, so the PR rendered as a 118-file diff of fork
divergence rather than this change. This PR targets prism-v7 from a cherry-pick of the
same commit, and the resulting diff is the one file below.

ggml/src/ggml-cuda/mmq-hopper-q1.cu, +13/-6: the gate inversion at line 452 plus the
header comment that documents the new default and the non-bit-exact activation
quantization. The group-128 rename to PQ2_0 was already in place on prism-v7, so
nothing else needed touching.

Why

The path had never been built or exercised. It turns out to work, and to be worth having on for the shapes it serves.

Validated internally on Hopper with a three-arm comparison: baseline build, this build with the path off, and this build with the path on. The path-off arm matches baseline closely on every measurement, so the sm_90a plus CUTLASS build costs nothing by itself and the improvement is attributable to the path rather than to the build. Prefill improves; decode is unaffected, which is the expected behaviour for a prefill-only path and a useful check that nothing leaked into the wrong dispatch.

The gain is larger for the wider of the two low-bit types, consistent with it having more unpack work for int8 wgmma to absorb, and shrinks as the token count grows, so standard MMQ closes the gap on long prompts. Numbers are in the internal notes rather than here.

Numerics

This path is not bit-identical to standard MMQ. It quantizes activations to int8 with a coarser per-128-K absmax scale where q8_1 uses per-32, and the original source comment flagged it for validation. That validation has now been done, using the same binary with only the env flag differing, against a same-path control to establish the floor.

The deviation is small and the tail is bounded. Perplexity moves well inside its error bar, and a small fraction of tokens change their argmax. It is acceptable for the prefill gain, but it is a behaviour change rather than a pure optimization, which is why the opt-out exists and why the measured figures are recorded in the source comment for whoever reads this next.

Scope and what is not changed

  • GGML_CUDA_HOPPER_Q1 still defaults off at configure time. Shipping this in release binaries is a separate CI question, since the build image would need CUTLASS. Happy to do that as a follow-up if we want it in prebuilt artifacts.
  • Hardware gating is unchanged: Hopper only, explicitly excluding Ada and Blackwell, which lack wgmma and would need the tcgen05 path.
  • Shape gating is unchanged: supported weight types, F32 activations, contiguous, and M, N and K all divisible by 128. Everything else falls through to standard MMQ as before.

Test plan

  • Patched translation unit compiles clean at arch=compute_90a,code=[compute_90a,sm_90a] with CUTLASS includes.
  • Path confirmed to fire by profiler kernel trace: the wgmma kernel, its activation quantizer and its repack all appear when enabled, and the standard MMQ variant disappears, so it displaces work rather than running alongside.
  • test-backend-ops test -b CUDA0, zero failures in both arms.
  • Perplexity and logit-KLD with a same-path control.
  • Not yet re-run end to end after the gate inversion itself. The change is a two-line boolean inversion and the enabled path is byte-for-byte the code that was validated, but a confirming run on the built branch would be reasonable before merge.

…134)

ggufs packed before the Q2_0_G128 -> PQ2_0 rename carry the old ftype value 142.
The format did not change, only the enum did, so these files load and compute
correctly but print

  print_info: file type   = unknown, may not work

which reads as a broken file. With this they print

  print_info: file type   = PQ2_0 - 2.13 bpw (group 128, legacy ftype)

Naming only. No load path, no quantizer path, and no behaviour changes; the new
constant exists so the switch has a case for the value.

Worth noting the collision that makes this confusing: 142 is also
GGML_TYPE_PQ2_0, the tensor type id, which is current and correct. Only the
ftype was renumbered, to 141.

Verified on a published gguf that carries ftype 142: reports unknown on the
current branch, reports PQ2_0 with this change, same output either way.
…share an input (#137)

* hadamard: share the activation transform between folded weights that share an input

Folded weights that consume the same activation were each building their own
identical transform. ggml does no common-subexpression elimination, so the
sign-multiply and the FWHT ran once per weight rather than once per activation.

Three groups share an input on this architecture:

  ffn_gate + ffn_up      build_ffn LLM_FFN_PAR, both take cur     64 redundant
  attn_qkv + attn_gate   qwen35.cpp:237,241, both take input      48 redundant
  attn_q + attn_k + attn_v  qwen35.cpp:270,282,285, all take cur  32 redundant

That is 144 of 401 folded weights doing a transform that already exists, so 36%
of the FWHT calls per token were duplicates.

build_lora_mm and build_lora_mm_id now memoize on (input, rotation). Both must
match for the transform to be shared: same input tensor and same rotation, which
also implies the same width and therefore the same sign vector. The map lives for
one graph build.

Measured on M5, Metal, a folded low-bit checkpoint, three interleaved pairs
on a warm machine:

  pp512   358.29 -> 370.74   +3.5%
  tg128    23.20 ->  23.91   +3.1%

Run-to-run spread was at most 1.1 on pp512 and 0.11 on tg128, so the gap is well
outside the noise. Greedy output is unchanged, byte for byte, on the same prompt.

The size of the win matches the prediction: the transform stack was about 10% of
traced GPU time, and removing 36% of it should be worth about 3.5%.

* hadamard: trim the memo comment to two lines per AGENTS.md
Inverts the runtime gate from opt-in (GGML_HOPPER_Q1) to opt-out
(GGML_HOPPER_Q1_DISABLE) for builds configured with GGML_CUDA_HOPPER_Q1.
The build-time flag is unchanged and still off by default, since it
requires a CUTLASS checkout.

Validated internally: prefill improves for the shapes this path serves
and decode is unaffected. A control arm with the path disabled matches
baseline, so the sm_90a plus CUTLASS build costs nothing by itself.

The per-128-K activation scale is coarser than q8_1's per-32, so results
are not bit-identical. The deviation was measured against a same-path
control; it is small and the tail is bounded.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Default dispatch can incorrectly reach unsupported Blackwell devices, and unrelated API and graph changes are incomplete or undocumented.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Enables the Hopper WGMMA prefill path by default, but also includes undocumented graph memoization and legacy ftype changes.

Changes:

  • Inverts the Hopper runtime gate to opt-out.
  • Adds Hadamard transform memoization.
  • Adds legacy PQ2_0 ftype naming.
File summaries
File Description
ggml/src/ggml-cuda/mmq-hopper-q1.cu Enables WGMMA by default and documents numerics.
src/llama-graph.h Adds the Hadamard memo cache.
src/llama-graph.cpp Reuses cached Hadamard transforms.
include/llama.h Adds a legacy PQ2_0 ftype.
src/llama-model-loader.cpp Names the legacy ftype.
Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 4
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread ggml/src/ggml-cuda/mmq-hopper-q1.cu Resolved
Comment thread include/llama.h
LLAMA_FTYPE_MOSTLY_Q1_0 = 40, // except 1d tensors
LLAMA_FTYPE_MOSTLY_Q2_0 = 41, // except 1d tensors
LLAMA_FTYPE_MOSTLY_PQ2_0 = 141, // except 1d tensors (Prism group-128 Q2_0; matches published PQ2_0 ggufs)
LLAMA_FTYPE_MOSTLY_PQ2_0_LEGACY = 142, // pre-rename value for the same format, still found in published ggufs
Comment thread ggml/src/ggml-cuda/mmq-hopper-q1.cu Outdated
Comment on lines +6 to +11
// Activations are quantized fp32 -> int8 with a per-128-K absmax scale, coarser than q8_1's
// per-32, so this path is NOT bit-identical to standard MMQ. The deviation has been measured
// against the same build with the path disabled, using a same-path control to establish the
// floor: it is small, the tail is bounded, and only a small fraction of tokens change their
// argmax. Figures are recorded in the internal notes. If you need bit-exact output, set
// GGML_HOPPER_Q1_DISABLE.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in d5f32d9. Trimmed to three single-statement lines: what the path is, that it is default-on under GGML_CUDA_HOPPER_Q1 with GGML_HOPPER_Q1_DISABLE as the opt-out, and that it is not bit-identical because activations use a per-128-K int8 absmax scale rather than q8_1's per-32.

The pointer to internal notes is gone, since it was unreadable for anyone outside the org.

Comment thread src/llama-graph.h
Comment on lines +1081 to +1083
// Transforms shared by folded weights on the same activation. Key is (input, rotation);
// both must match. Valid for one graph build only.
mutable std::map<std::pair<const ggml_tensor *, const ggml_tensor *>, ggml_tensor *> hadamard_memo;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No longer applies. This PR was retargeted from prism to prism-v7, and the diff is now one file, ggml/src/ggml-cuda/mmq-hopper-q1.cu at +7/-7. The llama.h ftype and the Hadamard graph memoization were never part of this change; they came from diffing against the wrong base.

The observation stands on its own merits for whenever that ftype does land: a public llama_ftype value needs mirroring in gguf-py/gguf/constants.py and adding to llama_ftype_get_default_type.

@bri-prism
bri-prism changed the base branch from prism to prism-v7 September 10, 2026 00:30
…ment

GGML_CUDA_CC_BLACKWELL is 1200, the sm120 family, not the start of
Blackwell. Blackwell spans CC 1000, 1100 and 1200, so the old
cc >= GGML_CUDA_CC_BLACKWELL bound let sm_100 and sm_110 reach an sm_90a
wgmma kernel. That was unreachable while the path was opt-in and becomes
reachable once it defaults on, so bound the dispatch to the 900 series.

Also use GGML_CUDA_CC_HOPPER instead of a literal 900. The macro does
exist in this tree, contrary to the comment that was there.

The file comment is shortened to the concise form AGENTS.md asks for and
no longer points at notes a reader of this repo cannot open.
@khosravipasha

Copy link
Copy Markdown
Collaborator

Agent review: drafted and posted by the PrismML maintainers' coding agent from the maintainer account; the decisions are the team's.

This PR targets prism-v7, but that branch is frozen; prism is the live main line and the base every PR should use (it is not closed, the earlier confusion came from #106 being based on a stale head). Please retarget: a fresh branch from prism with the two commits cherry-picked applies without conflict (mmq-hopper-q1.cu is identical on both branches), then open the PR against prism. Changing the base of this PR in place would pull in the four prism-v7-only commits, so a new branch is cleaner.

Test notes from our side (L40S, sm_89, CUDA 12.8, the two commits cherry-picked onto prism head):

  • Default configure: builds; the file is compiled out, binary behaviour unchanged.
  • GGML_CUDA_HOPPER_Q1=ON with CUTLASS, arch 89 + 90a: builds clean, GGML_CUDA_CC_HOPPER resolves, test-backend-ops -o MUL_MAT 1283/1283 in both builds.
  • llama-bench on 2B/9B/27B PQ2_0 and PTQ1_0: decode identical within 0.5% across prism head, default build and Hopper-enabled build; prefill within this card's run-to-run spread.
  • KLD vs the F16 golden on the Hopper-enabled build matches the certification values to every printed digit (9B PQ2_0 5.02e-4 / 98.490%, 2B PQ2_0 2.68e-4 / 98.951%), so the runtime gate keeps Ada on standard MMQ even with the path compiled in.

We have no Hopper here, so the H100 numerics behind the flip are unverified by us; the unticked re-run after the gate inversion would be good to have in the retargeted PR. Nothing in shipped release binaries changes, since the build option stays off.

@bri-prism

Copy link
Copy Markdown
Collaborator Author

Superseded by #171, which is the re-branch you asked for: fresh branch off prism, only the two Hopper commits cherry-picked, applies without conflict. One file, ggml/src/ggml-cuda/mmq-hopper-q1.cu, +7/-7.

Closing this one rather than repairing it. Changing the base here in place did exactly what you predicted: it pulled in 555c5cda2 (Hadamard activation sharing, #137) and 3c12dbd5f (PQ2_0 ftype naming, #134), taking the PR from 1 file to 5 and leaving it conflicting. Your L40S results and the note about the outstanding H100 re-run are both carried over to #171.

Also worth flagging for anyone who lands here from search: the branch name perf/hopper-q1-default-on still points at #106's stale-head branch, which is 47 commits ahead of prism but differs by 2053 files against current prism. Do not reuse it. #171 is on perf/hopper-q1-default-on-prism.

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.

3 participants