Skip to content

ggml-hrx: an opt-in prefill path over the repacked q6_k tiles (engine #856) - #250

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
hrx/856-packed-prefill
Nov 8, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
hrx/856-packed-prefill

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

Refs engine ggml-org#856 (weights resident twice in GTT at -ngl 99). The engine-side accounting for this is tracked separately; this PR
is the kernel half — a prefill path that can read the same layout decode already uses.

At -ngl 99 a weight can be resident twice: the loader's stored-layout buffer plus the repacked copy the decode path builds on
first use. The two exist because the consumers disagree about layout. This adds a second prefill kernel that reads the repacked
tiles, so both consumers can share one resident layout.

What it does

  • the prefill body gains a layout selector — 0 = stored rows (unchanged, and what every existing kernel still passes), 1 =
    repacked tiles — plus a sibling exported kernel for 1;
  • the packed read mirrors the decode kernel's own arithmetic, so the two paths compute the same values by construction;
  • registered in the corpus manifest (exports 147 → 148) and therefore in the generated catalog;
  • opt-in: selected only with GGML_HRX_Q6K_PACKED_PREFILL=1 at the single call site that already hands the f16 prefill
    kernel to the builder. Unset is the shipped behaviour.

Measured (Qwen3-4B-Q4_K_M, one binary, both arms in one hold, five repeats; each arm logs which layout it read, because a
dispatch histogram cannot tell the two apart — the registration name is identical either way):

cell stored (default) repacked (=1) ratio
prefill pp512 3,159.57 ± 12.20 tok/s 3,217.45 ± 21.09 1.018x
decode tg32 75.87 ± 0.46 tok/s 78.18 ± 0.45 1.031x
PPL 8×512 15.0931 ± 1.18508 15.0931 ± 1.18508 identical

Speed-neutral within a few percent, numerically identical, and the point of the change is the resident copy rather than
throughput. A single-repeat run earlier suggested 1.57x on prefill; that was a cold-start artifact — the stored arm alone
moved from 1,809.92 at one repeat to 3,159.57 at five — and it is explicitly not claimed here.

Default stays off. Flipping it wants the GTT before/after from the issue's own BO census, which is a separate measurement;
this PR only makes the option exist and proves it does not cost speed or accuracy.

Verification status, stated plainly: built and exercised at 6bf216283 (the base my rig is on), and applied cleanly to
b970d85ba where this branch starts; the CI build is the first check at the exact PR head. Nothing here changes behaviour unless
the env var is set, so the risk of the PR itself is confined to code that cannot run by default.

…gml-org#856)

At -ngl 99 a weight can be resident twice: the loader's stored-layout buffer plus the repacked copy the decode path builds on
first use. Issue ggml-org#856 measured that as 5.9 GiB of GTT for a 2.38 GiB model. The two copies exist because the consumers disagree
about layout: decode reads the repacked tiles, prefill decodes the stored rows.

This adds a second prefill kernel that reads the repacked tiles, so both consumers can share ONE resident layout:

- the prefill body gains a layout selector (0 = stored rows, unchanged and still the default; 1 = repacked tiles), and the
  existing kernels pass 0, so their behaviour is untouched;
- the mapping is exact rather than approximate: prefill covers a 32-channel group as two 16-wide halves, which is one packed
  (group, half) pair, and a prefill workgroup covers 128 output channels against 64 rows per packed tile, so
  tile = (channel_tile*2 + (row > 63)) * quant_block_count + quant_block with the in-tile row at row mod 64;
- the packed read mirrors the decode kernel's own arithmetic (int8 code x per-half int8 scale x per-row f16 d);
- the new symbol is exported, registered in the corpus manifest (exports 147 -> 148) and therefore in the generated catalog;
- selection is opt-in behind GGML_HRX_Q6K_PACKED_PREFILL=1 at the single call site that already hands the f16 prefill kernel to
  the builder. Unset is the shipped behaviour, so this cannot change anything until someone asks for it.

Measured on Qwen3-4B-Q4_K_M, one binary, both arms in one hold, with each arm logging which layout it read (the selection is
logged, because a dispatch histogram cannot distinguish the two - the registration name is the same):

| cell | stored (default) | repacked (=1) | ratio |
|---|---:|---:|---:|
| prefill pp512 | 3,159.57 +/- 12.20 tok/s | 3,217.45 +/- 21.09 | 1.018x |
| decode tg32 | 75.87 +/- 0.46 tok/s | 78.18 +/- 0.45 | 1.031x |
| PPL 8x512 | 15.0931 +/- 1.18508 | 15.0931 +/- 1.18508 | identical |

So it is speed-neutral within a few percent and numerically identical. An earlier single-repeat run suggested 1.57x on prefill;
that was a cold-start artifact - the stored arm alone moved from 1,809.92 at one repeat to 3,159.57 at five - and is not
claimed here. The value of the change is the resident copy, not throughput.

Default stays off. Flipping it needs the GTT accounting from the issue's own BO census, which is measured separately.

Refs engine ggml-org#856.
@bong-water-water-bong

Copy link
Copy Markdown
Author

#250 ready for review at 63addbb (engine ggml-org#856 kernel half: opt-in prefill over the repacked q6_k tiles so prefill and decode can share one resident layout; stored path unchanged and every existing kernel still passes layout=0; measured speed-neutral 1.018x prefill / 1.031x decode at 5 repeats with PPL identical 15.0931, selection logged per arm; an earlier 1.57x single-repeat figure is withdrawn as a cold-start artifact); 3 files, +215/-9, CI build is the first check at the exact head

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

VERDICT: READY at 63addbb88bd4e38d40cc48cfa2a933b177621619

The important question for an "opt-in second path" PR is whether the default path changes, and the answer is no by construction: the discriminator is a compile-time constant per export, so the branch folds away.

export("ggml_mul_mat_q6_k_f32_wmma_prefill_wave32")   -> %weight_layout = index.constant 0
export("ggml_mul_mat_q6_k_f16_wmma_prefill_wave32")   -> %weight_layout = index.constant 0     (today's path, unchanged)
export("ggml_mul_mat_q6_k_f16_wmma_prefill_from_packed") -> %weight_layout = index.constant 1  (new, opt-in)

so %is_packed = index.cmp eq, %weight_layout, %c1 is false at compile time for both existing kernels and the scf.if never reaches the device. The body gained a parameter but the two shipped entry points instantiate it with a literal — that is the right way to add a layout variant: one template, three specialisations, no runtime branch in the hot loop.

The packed mapping is stated exactly enough to check against the decode kernel's own convention: "prefill covers 32 channels per call as two 16-wide halves, which is one packed (group,half) pair; and a prefill workgroup covers 128 output channels against 64 rows per packed tile, so tile = channel_tile*2 + (row >= 64) and the in-tile row is row & 63" — with %tile_half = row > 63 ? 1 : 0, %tile_lo = channel_tile * 2. That is a 1:1 restatement of the mapping, not a hand-wave, and it is the part a reviewer cannot otherwise verify without the device.

The manifest entry is structurally identical to its sibling (same key set, same family, same source, workload_parameters: [token_count]), so nothing new is required of the loader.

The logging decision is the right one and the reason is measured, which is worth keeping: a dispatch histogram prints registration names, and this knob swaps the kernel ref inside one registration — "a run with the knob on looks identical to one with it off (measured 2026-11-08, which is why this line exists)", and it logs at ERROR because the CLI's sink drops WARN and below. That is the difference between an opt-in flag that can be validated and one that produced "inconclusive" last time.

And the PR does not claim the win: "the stored path stays the default until PPL and tok/s are measured against it." Correct — the motivation (one resident copy of a q6_k weight instead of two) is a memory claim, and the speed claim has to be measured.

Not run: the kernel (needs the Loom toolchain and an HRX device, plus the PPL/tok/s A/B the PR defers). What I verified above is the IR-level constant folding, the manifest entry's shape against its sibling, and the registration swap point.

@bong-water-water-bong
bong-water-water-bong merged commit 6663e37 into 1bit/hrx-vulkan-patched Nov 8, 2026
@bong-water-water-bong

Copy link
Copy Markdown
Author

ISA citations for the claims this kernel rests on

Per the owner's 2026-11-08 reference (~/amd-oss/docs/amd-pdf/rdna3-shader-instruction-set-architecture-feb-2023_0.{pdf,txt}, 609 pp), citing by section — and naming where the guide does not cover a claim rather than implying it does.

  • §2.1 Wave32 and Wave64 (p.9) — the kernel is declared for wave64 (subgroup_size = 64), and the guide's statement that "Wave64 waves typically issue each instruction twice" is why the packed read maps a 32-value group onto two 16-wide halves with a per-half scale, instead of one 32-wide operand.
  • §7.9 Wave Matrix Multiply Accumulate (WMMA) (pp.72-75) — establishes D = A*B + C, the VOP3P encoding, and that WMMA raises no ALU exceptions. It does not tabulate fragment-to-register mappings; it defers those to AMD's Matrix Instruction Calculator, linked in that section. So the fragment-width fact this change relies on comes from the instruction name V_WMMA_F32_16X16X16_F16 plus the existing kernel's own operand usage — not from a table in §7.9. Flagged explicitly so the citation is not read as stronger than it is.
  • §3.3 Storage State: SGPR, VGPR, LDS and §3.3.4 LDS (pp.15-19) — the register/LDS model behind the weight staging this body feeds; relevant to the stored path's decode loop, which this change leaves untouched.

RDNA 3.5 caveat (gfx1151) handled by measurement rather than assumption: gfx1151 is RDNA 3.5 and this guide is RDNA3. Every number in the PR body was measured on gfx1151 — PPL 15.0931 in both arms and the five-repeat tok/s rows — so any 3.5 difference in these instructions is inside the measurement rather than something I am reasoning about. The compiled target is gfx11-generic, and this change emits no new instruction: the packed branch uses the same fragment loads, the same VALU set (sitofp / mulf / fptrunc) and the same staging as the decode kernel it mirrors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant