Skip to content

perf(hip): on gfx1151 a plain prompt of 1 or 2 rows takes the 2-row K-parallel tile - #3273

Merged
ashhart merged 4 commits into
ashhart:mainfrom
AbrahamCete:perf/hip-kp3-few-rows
Nov 15, 2026
Merged

ashhart merged 4 commits into
ashhart:mainfrom
AbrahamCete:perf/hip-kp3-few-rows

Conversation

@AbrahamCete

@AbrahamCete AbrahamCete commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

On gfx1151 a plain prompt-path product of 1 or 2 rows ran on kp0 (8 columns a lane set, 1 row a pass), as on every card. kp3 (4 columns, 2 rows a pass) is ahead there at 2 rows at every width and at 1 row from 4-bit codes up (#2923). gfx1151's table now sends those products to kp3; 1 row of 2-bit or 3-bit codes stays on kp0, which is ahead there. The bytes are the same.

A prompt ends in such a piece whenever 1 or 2 rows are left after its last full chunk, and a resumed prompt whenever 1 or 2 tokens follow the kept state.

Two commits, as agreed on #2923:

  • feat(hip): a tuning row can bound the width of the codes it takes. max_bits on the tuning row: the most bits a weight code holds for the row to take a product (0, the default: any). The registry's cost check refuses wider codes, for plain products and plans alike. No table sets it in this commit, so no choice changes.
    • A bound that is no code width (2, 3, 4, 5, 6 or 8) takes no product, where 7 or 9 would have read as "up to 6" or "any". tuning.build stops the build on such a bound, naming the entry, so a table reports it where it is written; a row made by hand with one takes no product.
    • tuning.build copied an entry's fields by name and dropped the ones the row type lacks, so a misspelled bound would have left its row taking every product. Such a field now stops the build, naming the entry and the field. This is a behavior change of the parser: main builds with a stray field. Every field the four tables carry is the row type's.
    • hip-affine-tune prints the bound with a row it proposes.
  • perf(hip): on gfx1151 a plain prompt of 1 or 2 rows takes the 2-row K-parallel tile. Two rows of gfx1151.zon: kp0 at .rows = 1, .max_bits = 3 and kp3 at .rows = 2, .items = 300, plain products allowed. Costs stay as they were, so no row is shadowed. The other three tables are untouched.

Size: 187 lines (+183/-4) in 11 files.

The asks on #2923

  1. The kp3 row only. The kp2 arm at 3-4 rows is out, with numbers: against kp1 there it reads 1.19 to 2.10 on 8-bit codes (slower in all 12 cells) and over 1.02 on 2-bit codes in 9 of 12.
  2. Exactness. Both tiles are one template and write the 128-row tile's bytes.
    • A kernel-suite test (kernel_tests/affine/rows.zig) launches kp0, kp3 and the 128-row dot2 tile by id, whatever the card's table picks, and compares bytes at 1 and 2 rows: every width of the registry's own list, groups of 32, 64 and 128, two shapes, fp32 and rounded into out16; an output left unwritten fails too. 72 products on gfx1151.
    • A host test (launches/mlx_entries.zig) pins the selection, below.
    • The byte comparison is a device test beside a host test, where the ask read "a host or contract check": bytes can only be compared on the device. If you prefer a contract case for affine_gemm_kp (about 170 lines, with records for four chips), I can add it here or after.
    • Token hashes equal main's on both checkpoints (Receipt).
  3. What else the table now picks. A scratch probe walks the real entries through Registry.select on the base and on the head: 207,028,224 points (every op and path; project prefill fully crossed over rows, widths, groups, alignments, items, pairs and all 32 environments).
    • 10,240 points differ, in 10 groups, all project prefill, plain, kp0 to kp3: 2 rows at each of the six widths, and 1 row at 4, 5, 6 and 8 bits.
    • Nothing differs at 1 row of 2-bit or 3-bit codes, with the dot2 GEMM tiles off, on decode, on routed plans, on lane rounds or past 2 rows.
    • gfx1030, gfx1100 and gfx1201 select identically at every point, with the bound commit alone and with both.
    • Each changed pick is exact for the reason of point 2: the test compares the two tiles' bytes at exactly those rows, on every width and group.
    • tf-qwen35-test check --explain-kernels differs in one line on both checkpoints: project prefill: 1-2 kp0 3-4 kp1 5-32 kp2 33-256 skew becomes 1-2 kp3 ....
  4. The B C C B B C table on this head, both checkpoints, cold and resumed: under Receipt.
  5. The width bound (your answer to the sweep): in its own commit ahead of the rows; a row whose bound does not parse is refused; the host test pins that 1 row of 3-bit codes still picks kp0 and 1 row of 4-bit codes picks kp3, and walks every width.

Checks on this head

  • Head commit: 7fb753d, on main f8dacd7. Both sides of every line below were built from git archive trees.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 341 on the base and 341 on this head, 0 new (tools/zig/lean_new.py).
  • Tests run, with the command and pass, fail and skip counts:
    • zig build test test-golden -Dhip=true --summary all: 754 of 754 steps, 10,048 tests passed, 79 skipped, 0 failed.
    • zig build hip-kernel-build -Dhip=true -Dhip-arch=gfx1151 and its test binary on gfx1151: 219 passed, 6 skipped, 0 failed. The new test is among the passed, at 72 products.
    • zig build hip-host-test -Dhip=true: 80 of 80.
    • New tests, each failing before its change and passing after it:
      • registry, an entry bounded by the codes' width takes no wider product, and none when its bound is no code width, and tuning, a field the row type lacks is found in an entry, so a misspelled bound stops the build instead of being dropped: both fail with only the declarations in place and pass with the check.
      • launches, gfx1151 takes the 2-row K-parallel tile for a plain prompt of 1 or 2 rows, the 1-row tile for 1 row of 2-bit or 3-bit codes, and the table test's line that gfx1151's bound is set: both fail on the bound commit and pass with the rows.
      • tuning, a width bound that is no code width is told apart, so a table that writes one stops the build instead of taking nothing: it fails while the check accepts every bound and passes with it.
    • Planted faults, each in a scratch copy, never committed:
      • .max_bit = 3 in gfx1151.zon stops the build: error: tuning: mlx.project.prefill.kp0 has a field no row has: max_bit.
      • .max_bits = 7 there stops it too: error: tuning: mlx.project.prefill.kp0 bounds its codes at 7 bits, which is no width they come in.
      • In tiles/gemm_kp.hpp, every row of a pass reading the pass's first row: the kernel test fails at 2 rows naming kp3, fp32 and rounded.
      • There too, a 2-row tile folding a round's groups last to first: it fails at 1 row naming kp3 in fp32 only; the rounded output hides it at that shape, which is why both are compared.
    • hip-kernel-contract on gfx1151 with ROCm 10.1's hipcc: the same SUMMARY on the base and on this head (161 kernels pass gates 1-4, 2 known findings, 42 controls red as planted, 2 limit problems). No kernel changes, so the contract section is deleted.
    • zig fmt --check build.zig zig and git diff --check: both pass.
  • CI: green, 8 of 8 on 7fb753d (fmt, zig, zig-windows, zig-macos-dev-tools, release-paths, pytest, benchmark-clients on Ubuntu and Windows; the two nightly host jobs skip).
  • Not run, and why:
    • gfx1030, gfx1100 and gfx1201, because I have only gfx1151. Their tables do not change and they select identically at every probed point.
    • gfx1150, gfx1152 and gfx1153: they share gfx1151's table, so they take the rows too, unmeasured.
    • A 2-bit or 3-bit checkpoint end to end: none is on this machine. Those widths are measured at kernel level only (below).
    • Nemotron and Flash Next end to end, for the same reason. Their prompt passes reach these rows, and their shapes are in the kernel sweep.
    • Sampled decode and a separate --drafter: prompt processing is the only caller of the changed rows. Decode ran greedy, with the checkpoint's MTP drafts on.

Receipt

  • Main against branch: the tables under "Prompt speed" below.

  • Boxes it reaches: gfx1151 (Strix Halo) serving any model on HIP whose prompt pass runs plain products: Qwen3.5, 3.6 and 3.8 (q/k/v, out, DeltaNet, the dense MLP, the MoE shared expert), Nemotron and Flash Next.

  • New switch? Default, accepted values, profile row: no switch. max_bits is a field of a tuning row (0 by default, 3 on gfx1151's kp0 row), not a setting.

  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision:

    • Machine: Ryzen AI Max+ 395 (gfx1151), Ubuntu 26.04.1, Zig 0.17.0. One GPU process at a time, no other load.
    • Builds and runs: hipcc with HIP 7.1.52801 and Ubuntu clang 21.1.8.
    • Contract: ROCm 10.1's hipcc (HIP 7.16.26385, AMD clang 24.0.0git) with its own device library.
    • Checkpoints: Qwen3.6-35B-A3B-MLX-4bit-MTP, with its MTP head, and Qwen3.8-27B-MLX-4bit. No drafter.
    • Base and head: main f8dacd7 and 7fb753d for every line.
  • Paths and platforms the change reaches, which I ran and which I could not:

    • Every plain prompt-path product of 1 or 2 rows on gfx1151, through Ops.affine on the prompt path. I ran both Qwen checkpoints, 4-bit.
    • Routed plans, lane rounds, logits heads and decode do not pick these rows.
    • The stricter parser and the registry's bound check run for every HIP card; no other table sets a bound or carries a stray field, and the host suite covers all four.
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false:

    • tf-qwen35-test hidden over real text, both checkpoints: byte-equal to main's at 1,025 and 1,026 tokens (whole, and cut at 1,024 so the last chunk is 1 and 2 rows) and at 130 tokens (whole, and cut at 64, 2 and 1 rows, so every row goes through the changed products).
    • Served cold and resumed prompts (below): token_sha equal to main's in every cell.
    • tools/bench_concurrent.py --levels 1,4 --tokens 256 --reps 3 --temperatures 0 --alone --serial at --parallel 4, 3 servers a side: token hashes equal on every server (35B-A3B 9f2923ac14d0 and a8c28b8297b4, 27B 2cbda70e0639 and ded27583c51f); 0 failed.
  • Main against this branch, same machine and checkpoint: all of it, below.

  • Decode speed before and after: decode picks no prompt entry. bench_concurrent above, tok/s, B C C B B C, median of 3:

    checkpoint, cell main this PR change
    35B-A3B, code x1 123.0 122.9 -0.1%
    35B-A3B, code x4 244.2 243.0 -0.5%
    35B-A3B, chat x1 98.1 99.4 +1.3%
    35B-A3B, chat x4 203.0 202.6 -0.2%
    27B, code x1 14.4 14.4 +0.0%
    27B, code x4 46.3 46.2 -0.2%
    27B, chat x1 14.8 14.8 +0.0%
    27B, chat x4 45.8 45.9 +0.2%
  • Prompt speed before and after. Runs alternate B C C B B C, one fresh process or server a run; each cell is the median of 3.

    • Whole prompt pass, tf-qwen35-test prefill <model> 1 2 3 4 64, ms:

      rows 35B-A3B main 35B-A3B this PR change 27B main 27B this PR change
      1 22 16 -27.3% 178 81 -54.5%
      2 29 19 -34.5% 272 82 -69.9%
      3 27 27 +0.0% 113 111 -1.8%
      4 28 28 +0.0% 114 112 -1.8%
      64 91 91 +0.0% 298 298 +0.0%
    • Cold first token, tools/bench_cache.py --require-cold --tokens 16 at --context 34816 --parallel 1 --prompt-cache-gib 0, ttft_s in ms. 1,025 and 1,026 tokens leave a last chunk of 1 and 2 rows; 1,024 and 2,048 leave none.

      tokens 35B-A3B main 35B-A3B this PR change 27B main 27B this PR change
      1,024 509.4 511.6 +0.4% 2,383.2 2,383.1 -0.0%
      1,025 535.1 527.8 -1.4% 2,569.4 2,470.2 -3.9%
      1,026 541.5 530.1 -2.1% 2,669.5 2,472.8 -7.4%
      2,048 1,027.8 1,024.8 -0.3% 4,797.1 4,795.8 -0.0%
    • Resumed first token at --parallel 1 --prompt-cache-gib 4, ttft_s in ms. A prompt is sent once, its state is kept at 1,024 tokens, and it is sent 10 times more; each cell is the median over those repeats, then over the servers. 3 and 64 rows left are controls. The client is resume.py (it uses tools/bench_concurrent.py's stream); raw summaries, with the kernel sweep's: the same gist.

      rows left 35B-A3B main 35B-A3B this PR change 27B main 27B this PR change
      1 33.0 27.2 -17.7% 205.1 107.3 -47.7%
      2 42.3 30.8 -27.1% 300.8 108.0 -64.1%
      3 39.7 39.5 -0.3% 138.4 138.7 +0.2%
      64 125.2 124.9 -0.2% 334.9 334.5 -0.1%
    • Kernel level, every width. A scratch probe (not committed) takes the tuner's place and times as it does: us a launch, the weight tables rotated over copies so every launch reads them from DRAM. kp3 against kp0, least / median / most over the cells (the two Qwen checkpoints', Nemotron's and Flash Next's shapes, and 4096 x 2048 at groups of 32, 64 and 128):

      codes 1 row 2 rows cells
      2-bit 0.93 / 1.05 / 1.14 (stays on kp0) 0.79 / 0.84 / 0.93 12
      3-bit 0.93 / 1.02 / 1.15 (stays on kp0) 0.84 / 0.93 / 0.98 11
      4-bit 0.35 / 0.44 / 0.55 0.23 / 0.31 / 0.40 12 and 13
      5-bit 0.64 0.54 1
      6-bit 0.63 0.57 1
      8-bit 0.72 / 0.92 / 0.98 0.76 / 0.85 / 0.96 12

      Where the margin is thin: 7 cells moved to kp3 read 0.95 to 0.98 with noise up to 3.9%, at 2 rows of 3-bit codes and on 8-bit codes; 5-bit and 6-bit rest on one cell each. None reads over 1.

  • Licence of any new weights or drafter: n/a.

Notes for review

  • The bound sits on kp0. The alternative is a lower bound (min_bits) on kp3's row. An upper bound on the tile that keeps the narrow codes needs one field and leaves every other row as it reads today.
  • Out-of-domain widths. A product whose codes are 0, 1, 7, 9 or 16 bits wide passes the bound's comparison, but both tiles refuse it as they do on main, so nothing new is launched.
  • Registry.verify does not look at bounds. tuning.build does: a bound that is no code width stops the build. The table test pins that gfx1151's bound is set.

Not in this PR

  • kp2 at 3-4 rows (point 1).
  • Any other pick at 3-4 rows. The sweep shows the tile that is ahead there turns with the codes' width, which one row an entry cannot say; I'll open its own issue with the numbers.

Checklist

  • The title and description match the head commit, including the receipt and the stack.

  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (The host tests fail before their change. The kernel test launches the tiles by id whatever the table picks, so it does not depend on the rows; it fails by name with each planted fault and runs only in the GPU suite.)

  • Comments and docstrings are one line. No measurements or history in the source.

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (A plain prompt product changes tile inside the same launch, not the engine's path.)

  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Hidden states are byte-equal to main's. The served token hashes equal main's in every cold, resumed and decode cell, and a resumed prompt's hash equals its first, cold request's. Decode picks none of these rows; its hashes equal main's at 1 and 4 streams with the MTP drafts on.)

  • No precision traded for speed. If the bits change, the description says which sums change and why. (The bits do not change: both tiles are one template and write the 128-row tile's bytes, and a kernel test compares them byte for byte.)

  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled. (Prompts are level or faster in every cell measured. Decode is level at 1 and 4 streams, greedy, with the MTP drafts on; it picks none of these rows, so I did not run sampled decode or a separate drafter.)

@ashhart

ashhart commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Thanks @AbrahamCete. The two-commit split, the planted faults and the 207M-point selection probe made this easy to review. I went through the full base..head diff and ran the host checks.

Host review is clean: zig fmt --check build.zig zig and git diff --check pass, lean is 341 before and after with nothing new, zigb hip-host-test -Dhip=true passes, and every file is well under 600 lines.

The selection change reads correctly. The max_bits bound is a no-op for every existing row and only gates gfx1151's kp0 at 1 row with codes of 3 bits or fewer, with kp3 taking plain 1-2 row products. Routed plans and the other three cards are unchanged, and the pinned host test covers all of that. Sharing one widths list between the registry and the kernel cases is a good call. The stricter parser does change behavior, but you documented it, every in-tree table entry uses only Row fields, and all four tables compile. On exactness: no kernel source changed, both tiles are instantiations of the same gemm_kp template that matches the 128-row tile bit for bit, and the by-id byte comparison at 72 products is in the diff.

Must-fixes: none.

We haven't run the gfx1151 tile byte comparison, the token-hash comparisons or the prefill speed numbers ourselves yet, so those rest on your posted runs (Ryzen AI Max+ 395, HIP 7.1.52801, the two Qwen checkpoints). If you can, keep the gfx1151 kernel-test output and the B C C B B C token-hash table attached here. That is the last piece before merge.

  • Synthena (Ash's Agent)

@AbrahamCete

Copy link
Copy Markdown
Contributor Author

Thanks. Here are the two pieces, from the runs in the description (main f8dacd7 against 7fb753d, gfx1151, HIP 7.1.52801).

The gfx1151 kernel-test output on this head: t5f-ktest.log, the whole run: 219 passed, 6 skipped, 0 failed. The new test's line is FEW ROWS 72 products on 3 tiles each, fp32 and out16.

Token hashes, B C C B B C. Three servers a side; each cell's hash is the same on all six:

cell 35B-A3B 27B
cold, 1,024 tokens c556b0e10d37 da6132c3a659
cold, 1,025 177ab5dd4256 6739cfe5196a
cold, 1,026 8c62f0e852c9 f4f057fa126d
cold, 2,048 981a2ce769f6 faceb02af600
resumed, 1 row left 105ebfcc415d 8f3befc260fe
resumed, 2 rows left c633b84e0e5d f32de28f1d1f
resumed, 3 rows left 977618b8614b 68e61267d2ed
resumed, 64 rows left f3e830707a14 f3e830707a14
decode at 1 and 4 streams (bench_concurrent) 9f2923ac14d0, a8c28b8297b4 2cbda70e0639, ded27583c51f

A resumed prompt's hash also equals its own first, cold request's, on both trees. The per-run lines are in t5-35-summary.txt and t5-27-summary.txt in the same gist, and the byte comparison of the hidden states (1,025, 1,026 and 130 tokens, whole and cut) in t5x.out.

Bizuayeu and others added 4 commits November 15, 2026 02:54
A row said which products its entry takes by rows, items and pairs, and
nothing about the weights' width. Timings on gfx1151 show a choice that
turns with it: of the two K-parallel tiles a plain prompt of 1 row can
run on, one is ahead on 2-bit and 3-bit codes and the other from 4-bit up.

`max_bits` is the most bits a weight code holds for the row to take a
product (0, the default: any). The registry's cost check refuses wider
codes for plain products and plans alike.

A bound that does not parse refuses its row instead of being ignored:

- a bound that is no code width (2, 3, 4, 5, 6 or 8) stops the build of
  the table that writes it, naming the entry and the bound, where 7 or 9
  would have read as "up to 6" or "any"; a row made by hand with such a
  bound takes no product. The registry and the kernel cases read the
  widths from one list, beside the row type;
- `tuning.build` copies an entry's fields by name and dropped the ones
  the row type lacks, so a misspelled bound left its row taking every
  product. Such a field now stops the build, naming the entry and the
  field. Every field the four tables carry is the row type's.

`hip-affine-tune` prints the bound with a row it proposes. No table sets
the bound yet, so no choice changes.

Refs ashhart#2923
…-parallel tile

A plain prompt product of 1 or 2 rows ran on `kp0` (8 columns a lane set,
1 row a pass) on every card. On gfx1151 `kp3` (4 columns, 2 rows a pass)
is ahead at 2 rows at every width, and at 1 row from 4-bit codes up; at
1 row of 2-bit or 3-bit codes `kp0` stays ahead. A resumed prompt ends in
such a piece whenever 1 or 2 tokens follow the kept state.

gfx1151's table now gives `kp0` one row of codes of at most 3 bits and
`kp3` the other plain products of up to 2 rows. Costs stay as they were,
so `kp0` keeps the products its row still takes and no row is shadowed.
Nothing else is chosen differently: products of 3 rows and more, routed
plans, decode and the other three tables keep their tiles.

Both tiles are one template and write the 128-row tile's bytes. Tests:

- launches: at every width gfx1151 picks `kp3` at 2 rows and, at 1 row,
  `kp0` for 2-bit and 3-bit codes and `kp3` from 4-bit up, with the
  matrix cores on or off; it keeps `kp1` and `kp2` past 2 rows and the
  reference tile with the dot2 GEMM tiles off; gfx1030, gfx1100 and
  gfx1201 still pick `kp0`.
- tuning: gfx1151's bound is set, so a table that lost it fails.
- kernel suite: `kp0`, `kp3` and the 128-row dot2 tile, launched by id
  whatever the card's table picks, write the same bytes at 1 and 2 rows,
  every width and group, fp32 and rounded into out16; an output left
  unwritten fails too.

Refs ashhart#2923
@ashhart
ashhart force-pushed the perf/hip-kp3-few-rows branch from 7fb753d to 94d9906 Compare November 15, 2026 02:54
@ashhart
ashhart merged commit 41a72fd into ashhart:main Nov 15, 2026
2 of 10 checks passed
@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Thank you @AbrahamCete. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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

Labels

None yet

Projects

None yet

3 participants