Skip to content

hip: faster Q1_0, Q2_0 and PQ2_0 decode on AMD - #125

Merged
bri-prism merged 2 commits into
prism-v7from
hip/q1q2-cdna-vperm
Aug 27, 2026
Merged

bri-prism merged 2 commits into
prism-v7from
hip/q1q2-cdna-vperm

Conversation

@bri-prism

Copy link
Copy Markdown
Collaborator

What

HIP (AMD) speedups for the 1-bit and 2-bit weight kernels in ggml-cuda, guarded so CUDA and MUSA are untouched:

  1. vecdotq.cuh: on HIP, __byte_perm is a software routine, so the Q1_0 sign unpack (ten permutes per 16 weights) and the Q2_0 / PQ2_0 symbol expansion (four permutes per 8 codes) were the dominant VALU cost of every decode dot product. Both now use the hardware byte permute (v_perm_b32 via __builtin_amdgcn_perm): Q1_0 spreads each nibble's bits into byte lanes and selects the constant +1/-1 bytes; Q2_0 and PQ2_0 spread the four 2-bit codes into byte lanes and do one LUT lookup for {-1, 0, 1, 2}.
  2. mmvq.cu: on CDNA, never pick the small_k MMVQ geometry (rows_per_block = nwarps) for Q1_0 / Q2_0 / PQ2_0. With wave64 it triggers for every K < 4096 projection and runs 2 to 4x slower per call than the default split-K geometry.

Why

Profiling on an MI300X (ROCm 6.4.1) showed decode for these formats running at a few percent of HBM bandwidth with the mat-vec kernel bound on the software permutes, and the 4B model decoding slower than 8B because its K = 2560 projections fell into the small_k path.

How verified

  • test-backend-ops -b ROCm0 -o MUL_MAT and -o MUL_MAT_ID: all Q1_0 / Q2_0 / PQ2_0 cases pass before and after. The change is bit-exact by construction; the HIP helpers were also checked offline against the CUDA __byte_perm sequences for all 65536 16-bit inputs.
  • Greedy 48-token generations on ROCm are byte-identical to the CPU backend for a 1-bit and a 2-bit model.
  • llama-bench on MI300X, -ngl 99 -fa 1, 5 repetitions, interleaved A-B-A-B on this branch (prism-v7); prompt processing is unchanged because it goes through MMQ. Decode tokens/s:
model before (r1 / r2) after (r1 / r2) change
4B Q1_0 112.7 / 112.2 323.7 / 320.0 2.9x
8B Q1_0 88.6 / 73.2 269.7 / 269.3 3.0 to 3.7x
27B Q1_0 31.5 / 31.5 96.9 / 97.5 3.1x
8B PQ2_0 127.7 / 98.3 258.2 / 259.9 2.0 to 2.6x
27B PQ2_0 48.6 / 48.7 91.7 / 91.7 1.9x

No change on NVIDIA (the HIP branches are behind GGML_USE_HIP device-compile guards) and no change for other quant types (the small_k rule is type-scoped).

Use the hardware byte permute (v_perm_b32) for the Q1_0 sign unpack and the
Q2_0/PQ2_0 symbol expansion on HIP, where __byte_perm is a software routine,
and keep the default split-K MMVQ geometry for these types on CDNA, where the
small_k geometry runs several times slower per call.

Bit-exact: test-backend-ops MUL_MAT and MUL_MAT_ID pass unchanged and greedy
generations match the CPU backend. MI300X decode: 4B Q1_0 112 -> 322 tok/s,
8B Q1_0 81 -> 269, 27B Q1_0 31.5 -> 97, 8B PQ2_0 113 -> 259, 27B PQ2_0 48.6 -> 91.7.
CUDA and MUSA are unchanged (HIP device-compile guards); other quant types are
unchanged (type-scoped rule).

@khosravipasha khosravipasha left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good catch, seem the __byte_perm optimization made things slower for AMD lol

We probably need to test on few AMD GPUs for more evidence, and try to upstream.

That might be why more people complain AMD gpu is slow, the __byte_perm was merged in mainline too.

Did we run KL test too for accuracy? Should not change I guess much.

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.

Pull request overview

Optimizes HIP decode kernels for low-bit quantization while leaving CUDA and MUSA paths unchanged.

Changes:

  • Uses AMD hardware byte permutation for Q1_0, Q2_0, and PQ2_0 decoding.
  • Disables slower small-K MMVQ geometry for these types on CDNA.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
ggml/src/ggml-cuda/vecdotq.cuh Adds HIP-specific quantized symbol unpacking.
ggml/src/ggml-cuda/mmvq.cu Adjusts CDNA MMVQ geometry selection.

💡 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/vecdotq.cuh Resolved
Comment thread ggml/src/ggml-cuda/mmvq.cu Resolved
@bri-prism

Copy link
Copy Markdown
Collaborator Author

KL run done on a fresh MI300X (ROCm 6.4.1), this branch vs prism-v7 base, both built the same way. wikitext-2 test, ctx 512, -fa on, public GGUFs. Both builds pass test-backend-ops -o MUL_MAT for Q1_0 / Q2_0 / PQ2_0 (135 OK, 0 FAIL, identical case lists).

Method: the base build saves logits with --kl-divergence-base over 200 chunks (102k tokens), the patched build is scored against them. As a control, the base build is also scored against its own saved logits, which shows the floor of the reference format (the tool stores reference log-probs as uint16 with a per-token scale, so even an identical run does not score exactly 0).

model patched vs base: mean KLD max KLD same top token base PPL
8B Q1_0 0.000000 6.4e-5 99.998% 14.13
8B PQ2_0 0.000000 6.5e-5 99.994% 11.49
27B Q1_0 0.000000 6.6e-5 99.996% 12.77
27B PQ2_0 0.000000 6.7e-5 99.998% 11.14
control: base vs its own logits (8B Q1_0) 0.000000 6.4e-5 99.998%

The patched rows match the self-score control, so the two builds produce the same logits to the limit the tool can express. Second check: scoring both builds against CPU-backend logits (40 chunks) gives identical statistics to every printed digit for 8B Q1_0, 8B PQ2_0 and 27B Q1_0 (the 27B PQ2_0 CPU control did not finish before the pod was shut down). That HIP vs CPU floor is max KLD 0.03 to 0.06 for the 8B models, about 1000x the patched vs base numbers.

On more AMD GPUs: RunPod only has MI300X, so RDNA will have to wait for a laptop or desktop card. The v_perm change applies to every AMD target; the small_k rule is scoped to CDNA on purpose until wave32 parts are measured.

@bri-prism
bri-prism merged commit a7cc296 into prism-v7 Aug 27, 2026
3 checks passed
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 13, 2026
Add group-128 ternary MMQ, MMVQ, conversion and row lookup support. Use native byte permutes on HIP and retain the CDNA split-K geometry. Use the PQ2_0 type name and explicit Q8 MMQ precision.

Source: PrismML-Eng#125

Source commits (PrismML-Eng/llama.cpp):
c09e6de
8bbb28b
a7cc296
5476d46

Co-authored-by: bri-prism <[email protected]>
Co-authored-by: Mark Schatza <[email protected]>
Assisted-by: GitHub Copilot
Assisted-by: SWE-2 Max (Devin)
Assisted-by: OpenAI Codex (Astra)
Assisted-by: OpenAI Codex

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 13, 2026
Add group-128 ternary MMQ, MMVQ, conversion and row lookup support. Use native byte permutes on HIP and retain the CDNA split-K geometry. Use the PQ2_0 type name and explicit Q8 MMQ precision.

Source: PrismML-Eng#125

Source commits (PrismML-Eng/llama.cpp):
c09e6de
8bbb28b
a7cc296
5476d46

Co-authored-by: bri-prism <[email protected]>
Co-authored-by: Mark Schatza <[email protected]>
Assisted-by: GitHub Copilot
Assisted-by: SWE-2 Max (Devin)
Assisted-by: OpenAI Codex (Astra)
Assisted-by: OpenAI Codex

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 2, 2026
Add group-128 ternary MMQ, MMVQ, conversion and row lookup support. Use native byte permutes on HIP and retain the CDNA split-K geometry. Use the PQ2_0 type name and explicit Q8 MMQ precision.

Source: PrismML-Eng#125

Source commits (PrismML-Eng/llama.cpp):
c09e6de
8bbb28b
a7cc296
5476d46

Co-authored-by: bri-prism <[email protected]>
Co-authored-by: Mark Schatza <[email protected]>
Assisted-by: GitHub Copilot
Assisted-by: SWE-2 Max (Devin)
Assisted-by: OpenAI Codex (Astra)
Assisted-by: OpenAI Codex
Assisted-by: Claude (Opus 5.5), rebase onto ggml-org master with conflict resolution

Ported and rebased by Codex.
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Nov 24, 2026
Add group-128 ternary MMQ, MMVQ, conversion and row lookup support. Use native byte permutes on HIP and retain the CDNA split-K geometry. Use the PQ2_0 type name and explicit Q8 MMQ precision.

Source: PrismML-Eng#125

Source commits (PrismML-Eng/llama.cpp):
c09e6de
8bbb28b
a7cc296
5476d46

Co-authored-by: bri-prism <[email protected]>
Co-authored-by: Mark Schatza <[email protected]>
Assisted-by: GitHub Copilot
Assisted-by: SWE-2 Max (Devin)
Assisted-by: OpenAI Codex (Astra)
Assisted-by: OpenAI Codex
Assisted-by: Claude (Opus 5.5), rebase onto ggml-org master with conflict resolution

Ported and rebased by Codex.
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