Skip to content

ggml-hrx: read the IQ3_XXS grid code from a workgroup table instead of a 256-way select chain (engine #572) - #216

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
hrx/572-iq3s-table-decode
Nov 6, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
hrx/572-iq3s-table-decode

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

What

The routed gate/up swiglu kernel (ggml_mul_mat_id_swiglu_f32_f32_wmma) is 39.6 % of pp4096 device time on
Qwen3.8-Flash-Next, and inside it the IQ3_XXS layers cost 2.4x the IQ4_XS layers on identical geometry
(79.94 ms vs 33.34 ms at 10x639x1, per-dispatch device timestamps from the HRX profiler).

Compiling the same kernel twice with only gate/up_weight_format changed said where the time goes:
4.04x the vector ALU instructions, identical MMA count, identical global/local memory traffic, no spills, and
IQ3_XXS reading slightly fewer bytes.

The cause is the grid decode. @ggml_iq3xxs_grid_code_i32, which @ggml_iq3xxs_f32_vector4 calls once per 4-value
packet inside the k-loop
, materialises all 256 IQ3S grid codes as literal scalar constants and selects one with a
~30-deep chain. The other decode route keeps the same table live as 1,024 f32 SSA values.

This treats the grid as what it is — 1 KiB of constant data. A second kernel symbol,
ggml_mul_mat_id_swiglu_f32_f32_wmma_iq3s_table, shares the template body through a new %iq3s_table_mode
parameter: 128 workitems fill a 256-entry workgroup table from the same function once per workgroup behind a
barrier, and every packet does one indexed load.

The baseline symbol keeps mode 0, so nothing in the corpus changes behaviour until it opts in, and the new dispatch
knob GGML_HRX_IQ3S_TABLE_DECODE defaults to the baseline path.

Compile report (loom-compile --compile-report=details, amdgpu:gfx11-generic)

field baseline table IQ4_XS (either)
vector_alu_count 3,557 1,592 (-55 %) 881
vector VGPR final / peak 153 / 153 100 / 100 96 / 84
scalar SGPR final 80 72 57
local_memory_count 93 103 (+10, the table) 93
private memory / spills 0 / 0 0 / 0 0 / 0
code body bytes 46,088 21,528 (-53 %) 17,416

The IQ4_XS column is a null test and it holds exactly: every field identical between the two modes, because the
table path is dead code for a format that never indexes it. So the kernel's other 17 blocks are provably untouched.

Measured (all same-hold pairs; the knob is the only variable)

measurement before after
kernel bench, shipped prefill shape (2048 tok, 640 partitions, 640 logical ops) 51.57 ms 18.95 ms 2.72x
product path pp4096 (window 1) 270.62 tok/s 318.52 1.18x
product path pp4096 (window 2) 140.84 182.10 1.29x
product path pp512 177.62 221.38 1.25x
PPL 8x512 1.7198 ± 0.05749 1.7198 ± 0.05749 identical

PPL is the correctness gate and it is identical to four decimals, measured with both arms inside one hold on the same
build. Absolute tok/s levels vary with box state between windows — only the paired ratios are meaningful.

Correctness by construction

The variant differs from the baseline in one statement, and the substitution is exact: %gi is extui of an i8, so it
is in [0,256); workitems t and t+128 of the 128-workitem group write entries t and t+128, covering all 256; the
fill precedes the barrier that precedes the first read; and the table is filled with @ggml_iq3xxs_grid_code_i32(i) —
the same function the baseline calls. Both decode branches are gated on format == 28 per weight, so a mixed gate/up
quant is safe.

Also in this PR

benchmarks/loom/qwen38_flash_next_ud_iq4_xs.{loom,pp2048.json} — a two-minute A/B fixture for this kernel at the
shipped shape (2048 tokens, 10 routes, 512 experts, input 2560, expert ffn 640, 640 partitions of 32 rows), with the
authored partition table the kernel needs and three cases: baseline, table variant, and the IQ4_XS null.
Note for anyone reusing it: a check.benchmark scenario cannot do final-batch profiling, so the runner needs
--profile-final-batch=false.

Refs engine ggml-org#572 (item 4).

…gml-org#572)

The routed gate/up swiglu kernel is 39.6 % of pp4096 device time on Flash-Next, and inside it the IQ3_XXS
layers cost 2.4x the IQ4_XS layers on identical geometry (79.94 ms vs 33.34 ms at 10x639x1, per-dispatch device
timestamps from the HRX profiler). Compiling the same kernel with only the weight format changed showed why:
4.04x the vector ALU instructions, identical MMA count, identical memory traffic, no spills.

The cost is the grid decode. @ggml_iq3xxs_grid_code_i32, reached from @ggml_iq3xxs_f32_vector4 once per 4-value
packet inside the k-loop, materialises all 256 IQ3S grid codes as literal scalar constants and selects one with
a ~30-deep chain; the other route keeps the same table live as 1024 f32 SSA values.

This treats the grid as what it is - 1 KiB of constant data. A second kernel symbol,
ggml_mul_mat_id_swiglu_f32_f32_wmma_iq3s_table, shares the template body through a new %iq3s_table_mode
parameter: 128 workitems fill a 256-entry workgroup table from the same function once per workgroup behind a
barrier, and each packet does one indexed load.

Compile report (loom-compile --compile-report=details, amdgpu:gfx11-generic), same configs:

  field                      baseline   table     IQ4_XS (either)
  vector_alu_count            3,557     1,592     881
  vector VGPR final / peak    153/153   100/100   96/84
  local_memory_count          93        103       93
  private memory / spills     0/0       0/0       0/0
  code body bytes             46,088    21,528    17,416

The IQ4_XS column is a null test and holds exactly: the table path is dead code for a format that never indexes
it, so the kernel's other 17 blocks are provably untouched.

Measured, all same-hold pairs, variant opt-in via GGML_HRX_IQ3S_TABLE_DECODE (unset = baseline):
  - kernel benchmark at the shipped prefill shape (2048 tokens, 640 partitions, 640 logical ops each):
    51.57 ms -> 18.95 ms, 2.72x
  - product path, pp4096: 270.62 -> 318.52 tok/s (1.18x) and 140.84 -> 182.10 (1.29x) in two windows
  - product path, pp512: 177.62 -> 221.38 tok/s (1.25x)
  - PPL 8x512, both arms in one window: 1.7198 +/- 0.05749 both, identical to four decimals

Closes ggml-org#572 (part).

Refs: engine ggml-org#572. Benchmarks fixture: benchmarks/loom/qwen38_flash_next_ud_iq4_xs.{loom,pp2048.json}
@bong-water-water-bong

Copy link
Copy Markdown
Author

#216 ready for review at 7a9501d (engine ggml-org#572 item 4: IQ3_XXS grid decode from a workgroup table; kernel bench 2.72x, product pp4096 1.18x/1.29x, pp512 1.25x, PPL identical 1.7198 both arms; knob GGML_HRX_IQ3S_TABLE_DECODE defaults off)

@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 7a9501d7 on construction and on an unchanged shipping path — and I want to be exact about that, because I could not re-derive a single one of this PR's numbers: the compile report and the kernel bench need loom-compile and an HRX build, and the c31 gate chain is running under tonight's device hold. What that leaves is stated at the bottom; the two things I can prove, I did.

1. The shipping path is provably identical, not "defaults to the old one" by convention. The baseline symbol sets the new parameter as a constant:

kernel.def … @ggml_mul_mat_id_swiglu_f32_f32_wmma(%token_count) …
  %iq3s_table_mode = index.constant 0            # ops/mul_mat_id_swiglu_f32_f32_wmma.loom
  template.apply<@ggml.mul_mat_id_swiglu.body>(… %iq3s_table_mode, …)
kernel.def … @ggml_mul_mat_id_swiglu_f32_f32_wmma_iq3s_table(%token_count) …    # same body, mode 1

so the scf.if that would fill the table is dead in the old symbol at compile time, and common/dispatch-gated-mul-mat-id.cpp only reaches the new one through iq3s_table_decode_enabled() — a static const bool read once from GGML_HRX_IQ3S_TABLE_DECODE, unset = false.

2. The manifest entry is a mechanical copy of the one next to it, which is what makes the catalog static_assert able to resolve it at build: exports[37] differs from exports[36] in name and symbol only — family, source, bindings, binding_access, launch_parameters, workload_parameters, compile_dependencies, compile_recipe, target_selector are identical fields. The source is the same .loom, and both symbols are real kernel.defs in it.

3. The construction argument holds when read against the code, which is the part I could check line by line:

  • the table index is %gi, and in motifs/dequant.loom it is %gi_i8 = view.load … -> i8 then %gi = scalar.extui %gi_i8 : i8 to i32, so it is in [0, 256) — in range for view<256xi32>;
  • the fill is %fill_lo = workitem ∈ [0,128) writing %fill_lo and %fill_lo + 128, covering all 256 with workgroup_size(%c128, …) — and the source carries the comment saying exactly that, plus what to do if the group size ever changes;
  • kernel.barrier<workgroup> is placed outside the scf.if %fills_table block, so no workitem diverges around a barrier (the failure mode a fill like this usually has);
  • both the fill and both decode selections are gated on format == 28 per side (%gate_table, %up_table separately), so a mixed gate/up quant takes the old path on the non-IQ3 side;
  • the fill calls @ggml_iq3xxs_grid_code_i32 — the very function the baseline selects from inline — so the table cannot disagree with the chain it replaces.

What I did not verify, and it is the substance of the PR's value: vector_alu_count 3,557 → 1,592, VGPR 153 → 100, code body 46,088 → 21,528 B, the IQ4_XS null column being exactly identical, the kernel bench 51.57 → 18.95 ms, the four product-path throughput pairs, and PPL 1.7198 ± 0.05749 on both arms. All of those need loom-compile --compile-report=details and an HRX build with a GPU window; tonight's hold (TISSUES-ALL rule 1, c31 gates running) is not the time for a fresh HRX dependency build, and I would rather say so than let a READY imply I ran them. The re-derivation is two commands for whoever has the window: build with GGML_HRX=ON, then the same llama-bench/kernel-bench pair with GGML_HRX_IQ3S_TABLE_DECODE unset and set, and perplexity -f on the same 8×512 window for the PPL row. If that PPL is not identical when it is re-run, this verdict does not stand.

Given (1)-(3) the PR as it stands cannot change any shipped number — which is why I am comfortable at READY — and everything the PR is for stays a claim until that window exists.

@bong-water-water-bong
bong-water-water-bong merged commit 469c69f into 1bit/hrx-vulkan-patched Nov 6, 2026
bong-water-water-bong added a commit that referenced this pull request Nov 10, 2026
…efault (engine ggml-org#572) (#281)

The table decode from #216 was opt-in behind GGML_HRX_IQ3S_TABLE_DECODE=1, and with it set the matcher swapped the
kernel for every format pair. Select it only when the gate or up weight is IQ3_XXS, by default; =0 restores the
select chain. One log line states which decode a run used, because both kernels share one registration name.

tests: MUL_MAT_VEC_FUSION cases at the Flash-Next routed prefill shape (2560 -> 640, 10 routes, broadcast input,
80 and 512 tokens) for IQ3_XXS and IQ4_XS, and GGML_TEST_PRINT_ERR=1 to print the measured error of a passing case.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Fable 5.2 <[email protected]>
@bong-water-water-bong
bong-water-water-bong deleted the hrx/572-iq3s-table-decode branch November 12, 2026 21:18
bong-water-water-bong added a commit that referenced this pull request Nov 22, 2026
…es with (engine#679) (#487)

ggml_mul_mat_id_swiglu_f32_f32_wmma_iq3s_table (ours, #216) builds the IQ3_XXS
grid in workgroup memory and launches with six buffers. The #327 AMD-pin merge
added a seventh codebook binding for AMD's tiled/skinny kernels and applied it
to every IQ3_XXS dispatch, so with the table decode on by default the first
Flash-Next UD-IQ3_XXS graph fails:

  command 169 kernel loom_libs:ggml_mul_mat_id_swiglu_f32_f32_wmma_iq3s_table
  has 7 bindings but its ABI requires 6

(rc-2026-11-22-c35 package on strixhalo, every request HTTP 500). The codebook
binding now goes only to the kernels that take it;
GGML_HRX_IQ3S_TABLE_DECODE=0 was the workaround.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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