Skip to content

feat: Qwen3.8-27B on AMD GPUs (ROCm), measured on a Radeon AI PRO R9700 - #100

Closed
jkuepker wants to merge 12 commits into
ashhart:mainfrom
jkuepker:rocm-r9700
Closed

jkuepker wants to merge 12 commits into
ashhart:mainfrom
jkuepker:rocm-r9700

Conversation

@jkuepker

@jkuepker jkuepker commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

This PR lets the CUDA engine serve Qwen3.8-27B (the MLX 4-bit checkpoint, with DFlash2 drafts) on AMD GPUs under ROCm's PyTorch. Drafted replies equal "draft": false ones and any prompt chunking gives the same bits, as on NVIDIA. It also removes about 200 kernel launches and 3 host syncs from every drafted round on both backends. Other families, EXL3 and NVFP4 checkpoints, image input and --tp 2 are refused on ROCm at startup with a message.

Benchmarks

One AMD Radeon AI PRO R9700: gfx1201, 32 GB, 640 GB/s; 639 GB/s measured streaming read. ROCm 7.14, torch 2.11.0+rocm7.14, Triton 3.6. Checkpoints Vontra/Qwen3.8-27B-MLX-4bit@70ae7fac and z-lab/Qwen3.8-27B-DFlash2@50307d4c. Launch: tensorfold serve Vontra/Qwen3.8-27B-MLX-4bit --name bench, which fits a 32,768-token window beside the model and drafter.

Public fixtures. tools/bench_openai.py --tokens 64 --reps 5 --temperatures 1.0,0, median decode tok/s after the first token (seeds 1234-1238):

Prompt DFlash2, T=1 DFlash2, greedy --no-drafts, T=1 --no-drafts, greedy
fibonacci-raw 126.1 117.9 33.9 34.2
gpu-chat-no-think 95.7 96.8 33.9 34.2

Cold prefill. tools/prefill_cold.py, median of 3, unique first line per prompt. The 32k and 64k prompts don't fit the 32k window on a 32 GB card.

Prompt tokens TTFT Prompt tok/s
2,048 1.12 s 1,834
8,192 3.86 s 2,121
16,384 7.92 s 2,069

Longer prompts. A private streamed sweep: fresh-nonce chat prompts, 256 forced tokens, greedy, thinking off, 3 reps. Marginal prefill is the rate between successive prompt lengths. Decode depends on how many drafts are accepted per prompt and spreads 10-40% across reps.

Prompt Prefill (overall) Prefill (marginal) Decode
4,088 2,049 tok/s 2,360 tok/s 106.2 tok/s
16,677 1,992 tok/s 1,974 tok/s 100.1 tok/s
30,949 1,890 tok/s 1,784 tok/s 92.7 tok/s

Where the time goes (torch profiler on the R9700):

  • Drafted round, code prompt, 12 verify rows, 6.4 tokens a round: 37 ms. Of that, 29 ms is the decode matmuls, which stream the weights at about 80% of the 639 GB/s read rate. The DeltaNet verify chain takes 1.3 ms and tree attention 0.8 ms.
  • A 4,096-token prompt chunk at 28k-32k context takes 2.41 s of GPU time: 52% FP8 matmuls, 30% attention (0.72 s; 1.04 s on the Triton kernel). With the KV preallocated past this PR's window, a chunk at 64k / 96k tokens of context takes 3.19 / 3.88 s (1,285 / 1,056 tok/s), 46% / 55% of it prompt attention; the 32-row prompt layout took it from 3.35 / 4.19 s.

Decode at long context. A 12-row drafted round on one fixed code prompt behind filler, with the KV preallocated beyond this PR's 32k window (a follow-up adds a larger window). Round time in ms; tokens and rounds are identical across columns:

Context Triton tree attention, before Triton, chunk-major items (now in main) WMMA tree attention Decode, WMMA
3,350 - 40.5 40.4 170 tok/s
15,926 - 43.0 43.0 148 tok/s
37,771 50.9 47.8 44.4 164 tok/s
76,633 101.5 63.3 51.9 126 tok/s
120,340 160.4 75.2 61.7 101 tok/s

Tree attention alone, 12 rows, 24 query / 4 KV heads, µs per layer call (committed chunks + tail + merge): 144 → 113 at 4k, 534 → 465 at 30k, 1,908 → 1,324 at 120k keys (chunk-major Triton → WMMA).

Decode at 16k (a code prompt from the stdlib corpus, greedy, 12 verify rows, 2.8 tokens a round; ms a round from the round profiler): 43.0 → 41.2 ms with the drafter's block attention in parts (2.3 → 0.27 ms of it), → 40.0 ms with stacked input projections and a K split target of 64 (matmuls 30.3 → 29.2 ms, launches 1,007 → 943); 65.8 → 71.6 tok/s. 16 verify rows accepted no more on this prompt and cost 1.2 ms a round.

Kernel level (M = 4,096, the 27B's projection shapes):

  • FP8 prompt GEMM: 150-158 TFLOPS.
  • Prompt attention (4k queries over 32k keys, 24 query / 4 KV heads, head size 256): about 78 TFLOPS on WMMA (70.5 with 16-row blocks and no loader waves) against 48.9 for the Triton kernel. 1-57 queries after 24k keys: 1.3 ms, from 1.8-1.9.
  • Old bf16 two-phase path: 81-115 TFLOPS. hipBLASLt bf16: 123-129 TFLOPS. A plain Triton e4m3 GEMM with no per-group scales: 196 TFLOPS.

How the numbers moved during development (same card and sweep as the longer-prompts table):

Stage Marginal prefill 4k / 16k Decode 4k / 16k
First working build: Triton lane matmul, bf16 prompts 661 / 617 44 / 42
Tiled 4-bit layout, bf16 prompt GEMM, 16-key attention tiles, HIP WMMA decode 1,599 / 1,418 98 / 98
FP8 prompt GEMM, persistent WMMA decode, fused gate/up, launch cuts 2,339 / 1,854 128 / 120
WMMA prompt attention (this PR's head) 2,360 / 1,974 106 / 100

FP8 keys and values (--kv-dtype fp8, opt-in, its own commit): per token and KV head, 256 e4m3 codes and a power-of-two exponent, 272 bytes instead of 512. With the same rounding applied to bf16 caches: localeval mmlu 0.860 → 0.864 (228 questions, noise), ifeval 0.900 → 0.900 (60), judged core set 1.000 → 0.990 (10); after 4k/16k/32k tokens of code the next-token distribution kept bf16's top token 96.9/99.2/97.7% of the time (mean KL 0.007-0.012). The packed caches give exactly those logits. A drafted round at 64k tokens of context: 48.7 → 46.5 ms (tree attention 9.7 → 7.3 ms); at 16k 40.1 → 39.6 ms. gsm8k, now scorable with main's JSON-schema replies, on the served packed caches against bf16 (same server otherwise): 0.980 vs 0.980 with thinking on (200 questions), 0.500 vs 0.500 with thinking off (100); ifeval 0.860 vs 0.880 (100, noise).

The change

  • Build (cuda/build.py): hip(); extensions compile with hipcc for the GPU present (--offload-arch, or the first of PYTORCH_ROCM_ARCH on a host with no GPU); nvcc flags map to clang's (--fmad=false becomes -ffp-contract=off). gdn.cu and gdn_prefill.cu take ROCm's 64-bit shuffle mask and bf16 rounding.
  • Decode matmul (cuda/kernels/qmm_rocm.cu, qmm_groups.py): WMMA on 4-bit words repacked at load into 16-output tiles.
    • Nibbles are reordered so a bf16 pair is one AND-OR (the 128 + q trick, folded back through the group sums).
    • Blocks are persistent and loop over work items.
    • The K split is fixed by the weight's shape; the last-arriving block adds the slices in slice order, the same arithmetic as a separate reduce, with one launch.
    • Up to 32 rows share each weight read.
    • [gate|up], [z|b|a] and [k|v] are stacked, with the members kept as views, so no memory is added.
    • A row's arithmetic never depends on the row count, pass or grid. tests/cuda/test_qmm_rocm.py checks 1-40 rows against each other and against float64.
  • Prompt matmul: the FP8 prompt arithmetic NVIDIA already uses (prefill_glue's e4m3 rows, the 4-bit codes exact in e4m3, per-group scales, bias on the group sums, row scale). The codes are widened once per chunk, then a fixed-tile Triton e4m3 GEMM runs with grouped tile order.
  • Prompt attention on ROCm (cuda/kernels/attention_rocm.cu, its own commit): flash attention on WMMA for head size 256. A block per 16 query rows and KV head, a wave per query head sharing each 16-key K/V tile in shared memory. S^T = K Q^T puts a query's scores in its lane pair, so P is already the A operand of P V. A row is rescaled only by its own factor, so chunking never changes its bits. The Triton kernel stays for other head sizes and behind TF_ROCM_ATTN_KERNEL=triton.
    • A later commit (perf(rocm), same bits) gives it 32-row blocks (16 below 192 prompt rows) and four loader waves that keep the next K/V tiles in flight through a double buffer, sharing the tree kernel's fold; checked bit for bit against the first version on every head grouping and layout. TF_ROCM_ATTN_RB / TF_ROCM_ATTN_PIPE override the layout.
  • DFlash2 block attention in parts on ROCm (draft_attention.py, its own commit): the drafter's window keeps up to 2,047 keys and its attention ran one program per KV head (8 a call). A program now takes one query head and 512 keys (or the block's own), then a merge adds the parts in key order. A stream's drafts stay independent of the streams beside it; NVIDIA keeps the one-pass kernel.
  • Stacked input projections on ROCm (its own commit): a GDN layer's [qkv | z | b | a] and an attention layer's [q | k | v] are one matmul each, their members kept as views (no memory added). gdn_pre, attn_prep and gate_mul take row strides for the views (NVIDIA passes the contiguous stride). The WMMA K split target goes from 128 to 64 blocks: every 27B projection read at 12 rows as fast or faster. Decode bits change for every row count alike, so drafted still equals serial.
  • FP8 key/value cache (--kv-dtype fp8, cuda/kernels/kv8.py, its own commit): one Triton launch per attention layer call packs a layer's keys and values; the WMMA prompt and tree attention kernels widen packed rows to bf16 while staging tiles (the same tile and fold as bf16 caches holding the rounded values); a verify window's own keys and values are rounded the same way, so drafted still equals serial. The startup estimate counts the smaller cache. Refused on NVIDIA, with --tp 2, with the Triton attention kernels, and for head sizes other than 256.
  • Tree attention on WMMA (cuda/kernels/attention_rocm.cu with the prompt kernel, its own commit): replaces the Triton _shared and _tail kernels on ROCm; the Triton merge stays.
    • A block per committed 512-key chunk and KV head stages each 16-key tile once for all of a window's (row, head) pairs, a wave per 16 pairs, with KV heads launched fastest so each key's 2 KiB of K is read together.
    • Up to 64 chunks, four loader waves keep two tiles in flight through a double buffer; past that, every wave loads then folds (twice the blocks fit). Loader and compute waves run separate loops with matching barriers, so each role's registers are its own (no spills).
    • A row's tail chunk (last committed keys, then its path) folds with the same function, so a chunk a serial row reads as committed keys gets the bits a drafted row's tail gives it. The number of compute waves and the loader schedule never change a pair's bits; a new test checks 1-8 waves with both schedules.
    • TF_ROCM_TREE_KERNEL=triton keeps the Triton kernels (different bits; a process uses one).
  • Tree attention chunk order: main (0.5.0) now orders tree attention's work items chunk-major and launches a chunk's KV heads together, the same change this PR carried, so that commit is gone from here. The ROCm port builds on it; the Triton _shared keeps a key-tile parameter for ROCm's fallback.
  • Tree attention and DeltaNet on ROCm:
    • 16-key tiles for the Triton tree and prompt kernels (kept behind TF_ROCM_TREE_KERNEL / TF_ROCM_ATTN_KERNEL).
    • Verify chains run 4 rows a warp, prompt chains 64 rows a block; a row's arithmetic is the same either way.
    • Triton buffer loads are turned off (AMDGCN_USE_BUFFER_OPS=0), because they read zeros at negative offsets from attention.base.
  • DFlash2 on ROCm (its own commit, fix(rocm)): ROCm's unsorted topk returns tied logits in a varying order and set, so the same drafter context could propose different trees. On ROCm the top 16 are now picked by value, then column. Replies were never affected, since drafts are verified.
  • Startup estimate: counts the 4-bit drafter at its packed size. It counted every tensor as fp32 (7.2 GiB instead of 1.0) and refused every window on 32 GB.
  • Every backend:
    • draft_decode synchronizes for its stage timers only when trace is given; DecodeResult stage times are host-side otherwise.
    • Fused projection slices reach gdn_pre, gated_norm, attn_prep, swiglu and tree attention as strided views instead of copies (176 launches a round).
    • The drafter computes shared group sums, block-attention pointer tables and its window update once a round.
  • Docs: an AMD section in docs/recipes/qwen3.8-27b.md with the setup, tuning variables and limits, plus a pointer from the README.
  • Separate commit (perf(mlx)), MLX only: on M5, prompt attention with head size 256 is one fused SDPA call instead of 128-row parts. It measures 1.38x at the kernel on an M5 Max, but end to end the gain is within noise (+1.6% at 16k, +5.7% at 32k marginal prefill). Drop the commit if you'd rather keep the parts.

Checked

Rebased on main at 0.5.0 (JSON-schema replies, faster CUDA loading, chunk-major tree attention). Re-run on the R9700 after the rebase, on #106's branch (these commits plus the tier):

  • The 27B's GPU suites pass, with main's JSON-schema and direct-reader tests (257 tests), and again with TF_ROCM_TREE_KERNEL=triton TF_ROCM_ATTN_KERNEL=triton for the Triton fallbacks (57).

  • Two NVIDIA-only tests are skipped on ROCm: the CUDA prompt kernel's Triton-bits check, and the MoE experts pack, whose kernels ROCm doesn't build.

  • Served greedy replies drafted and with "draft": false are identical at this branch's head too, and a JSON-schema request parses to the schema with the same reply both ways.

  • The speed tables above were measured before the rebase (main's _merge now runs in column parts); not re-measured.

  • The 409-test count below is from before the rebase.

  • On the R9700, 409 tests pass: the 27B's GPU suites (forward, prefill, multi-stream, glue, attention, DeltaNet, sampling), the new ROCm matmul tests, and the CUDA CLI/capacity tests. Details:

    • Among them: every prompt chunking gives the same state, resumes equal fresh prefills, and drafted equals serial.
    • test_qwen27_dflash2_blocks.py::test_blocks_of_several_streams_equal_their_own failed on ROCm until the DFlash2 commit above; it passes now.
  • Served greedy replies drafted and with "draft": false are identical at this head (4,726 and 28,676-token prompts, bf16 and --kv-dtype fp8; 44,826 tokens with the WMMA tree attention). The stacked input projections commit broke this on the real model: attn_prep read the rotary partner of every verify row past the first by the old row stride. The fix(rocm) commit after it corrects that, and test_qwen27_glue.py now reads every glue input as a slice of one stacked output (it fails on the old line).

  • On a Mac, 3,952 CPU tests pass (on main at 0.5.0). tests/test_cuda_build.py::test_the_lock_named_is_the_one_torch_waits_on fails here and on main alike (this torch reaches its Ninja check before the lock).

  • Triton 3.6 miscompiles on gfx1201, found and avoided:

    • bf16 GEMM tiles of 128x256 and 256x128 give wrong sums at K=128 with 256+ outputs.
    • Pipelined (num_stages 2 or 3) e4m3 tiles give wrong sums for calls of a few rows at K=6144.
    • The defaults avoid both, and tests/cuda/test_qwen27_prefill.py checks both prompt GEMMs at the model's shapes and at 1-, 2- and 5-row chunks.

Not claimed

  • NVIDIA: tests, a served check and speed against main. On a DGX Spark (GB10, NGC PyTorch 26.07: torch 2.13, Triton 3.7.1):
    • tests/cuda against main at 0.5.0: 828 pass on this branch plus the tier (feat(cuda): host-RAM tier for Qwen3.8-27B prompt states (--ram-tier-gib), 126k window on a 32 GB R9700 #106), 783 on main. Failures in the parallel runs were memory contention on the Spark's unified memory; each passes run alone, on both trees. One test of this PR, the ROCm-only group-major matmul, built the ROCm extension there and now skips off ROCm.
    • Served from this branch (Vontra/Qwen3.8-27B-MLX-4bit@70ae7fac with DFlash2, an 8,192-token window beside the Spark's other services): greedy replies drafted and with "draft": false are identical at 926, 4,726 and 7,446 prompt tokens, and a JSON-schema reply is the same both ways.
    • Speed against main, served the same way:
      • Fixed greedy prompts: this branch and main give byte-identical replies at 926, 4,726 and 7,446 prompt tokens, so NVIDIA's arithmetic is unchanged. Decoding those identical replies takes the same time: 2.953 / 3.047 / 2.704 s on this branch against 2.962 / 3.055 / 2.706 s on main, 256 tokens each.
      • localeval, two runs of each (1k / 4k / 7k prompts, 256 tokens, 5 reps): prefill within 1.2% (1,566-1,920 tok/s). Decode medians land 47-57 tok/s on both and swing up to 16% between runs of the same code, because draft acceptance follows each call's random prompt prefix.
      • main as released refuses to start the 27B with DFlash2 at any window on a Spark whose other services hold their memory, because its estimate counts the drafter at fp32. This branch's packed-drafter estimate fits 8,192 tokens there. For the comparison, main's copy took only that estimate (three admission lines, no kernels).
        These edits touch code NVIDIA runs:
    • gdn.cu: the mask macro and cudaFuncSetAttribute cast.
    • glue.py: new stride arguments, including gdn_pre's qkv and attn_prep/gate_mul's [q | gate] rows.
    • cuda/kernels/attention.py: value row stride, and a key-tile parameter for ROCm's Triton fallback.
    • forward.py: views instead of copies, and the input projections through _gdn_inputs/_attn_inputs (unstacked on NVIDIA).
    • decode.py: sync gating.
    • dflash2.py, draft_attention.py: the drafter changes.
    • capacity.py: the drafter estimate.
    • Everything else ROCm-specific sits behind hip() or __HIPCC__.
  • Only one AMD GPU tested. No gfx942/MI300 or RDNA3.
  • No quality evaluation of the FP8 prompts on ROCm. It is the same arithmetic as NVIDIA's default, and greedy replies to the smoke prompts are unchanged, but no eval suite ran.
  • --parallel is untested on ROCm beyond the multi-stream tests above.
  • max_rows stays 12. 16 rows helped code prompts (+13-19%) and hurt prose (-9%) in one test each.

🤖 Generated with Claude Code

jkuepker and others added 11 commits September 29, 2026 17:05
…round

The CUDA backend now runs under ROCm's PyTorch on one GPU for Qwen3.8-27B's MLX 4-bit checkpoint with DFlash2
drafts. Drafted replies equal serial ones and any prompt chunking gives the same bits, as on NVIDIA. Other
families, EXL3 packs and two ranks are refused at startup with a message.

- Build: extensions compile with hipcc for the GPU present (--offload-arch), nvcc flags mapped to clang's.
  DeltaNet kernels take ROCm's 64-bit shuffle mask and bf16 rounding.
- Decode matmul (qmm_rocm.cu): WMMA on 4-bit words repacked at load into 16-output tiles, persistent blocks,
  split-K reduced in-kernel by the last block, up to 32 rows per weight read; [gate|up], [z|b|a] and [k|v] fused
  as views. Row arithmetic never depends on the row count.
- Prompt matmul: NVIDIA's FP8 prompt arithmetic (prefill_glue's e4m3 rows, exact e4m3 codes, per-group scales)
  as a fixed-tile Triton e4m3 GEMM with grouped tile order.
- Attention and DeltaNet: 16-key attention tiles and ROCm schedules for the verify and prompt chains; buffer
  loads off (they read zeros for negative offsets from the attention base).
- Startup estimate counts the 4-bit drafter at its packed size (it counted fp32 and refused every window on 32 GB).
- Every backend: timing-only syncs in draft_decode run only when tracing; fused projection slices reach their
  kernels as strided views instead of copies; the drafter computes shared group sums, pointer tables and window
  updates once per round (~200 fewer launches a round).

Measured on a Radeon AI PRO R9700 (localeval speed sweep, greedy, thinking off): prompts 2,326 tok/s at 4k,
1,849 at 16k, 1,582 at 30k (marginal); decode 112 / 130 / 95 tok/s at 4k / 16k / 30k. The recipe's AMD section
has the setup, tuning variables and limits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
M5's fused SDPA takes head size 256 in flat memory, so the 128-row parts (and their evals) that bound memory on
M1-M4 are not needed there. On an M5 Max, 2,048 queries against 6k-32k keys: 1.38x the parts (41.4-41.9 against
28.6-30.8 TFLOPS). End to end the gain is within run-to-run noise: marginal prefill +1.6% at 16k and +5.7% at
32k (localeval speed sweep, 3 reps). tests/test_prompt_attention.py still pins the stock causal bits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A HIP flash attention kernel for ROCm prompts (attention_rocm.cu): a block per 16 query rows and KV head, a wave
per query head of the group sharing each 16-key K/V tile in shared memory. S^T = K Q^T puts a query's scores in
its lane pair, so its row max and sum take one shuffle and P is already the A operand of P V. Keys fold in 16-key
tiles by absolute position and a row is rescaled only by its own factor, so any chunking gives a row the same bits.
The Triton kernel stays for other head sizes and behind TF_ROCM_ATTN_KERNEL=triton.

On an R9700: 70.5 TFLOPS at 4k queries over 32k keys, against 48.9 for the Triton kernel. A 4,096-token chunk at
28k-32k context takes 2.41 s of GPU time instead of 2.89 (attention 0.72 s instead of 1.04). Served, marginal
prefill 1,974 tok/s at 16k (was 1,854) and 1,784 at 30k (was 1,577); cold 16k prompts 2,069 tok/s (was 1,971).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ROCm's unsorted top-k returns tied logits in a varying order and set, and the
tree policy breaks ties by position, so the same drafter context could propose
different trees. On ROCm the top 16 now go by value, then lower column.
Replies were unaffected (every draft is verified); proposals are now repeatable,
and test_blocks_of_several_streams_equal_their_own passes on the R9700.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tree_attention_rocm.cu replaces the Triton _shared and _tail kernels on ROCm
(the Triton merge stays). A block per committed 512-key chunk and KV head
stages each 16-key tile once for all of a window's (row, head) pairs, a wave
per 16 pairs; up to 64 chunks, four loader waves keep two tiles in flight
through a double buffer, past it every wave loads then folds (twice the blocks
fit). A row's tail chunk folds the same way, so a chunk read whole as
committed keys gets the bits a drafted row's tail gives it; compute waves and
loaders never change a pair's bits (a new test checks 1-8 waves, both
schedules). TF_ROCM_TREE_KERNEL=triton keeps the Triton kernels.

R9700, 12-row drafted rounds on a fixed prompt, WMMA against Triton:
40.4 / 40.5 ms at 3k, 43.0 / 43.0 at 16k, 44.4 / 47.8 at 38k,
51.9 / 63.3 at 77k, 61.7 / 75.2 at 120k tokens of context (decode 101
against 83 tok/s at 120k). Served greedy replies drafted and with
"draft": false were identical on 44,826- and 4,726-token prompts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e bits

attention_rocm.cu now holds both WMMA attention kernels (tree attention moves
in from tree_attention_rocm.cu; one extension). The prompt kernel shares the
tree kernel's 16-key fold and loader machinery: a block per 32 prompt rows (16
below 192 rows) and KV head, a compute wave per 16 rows and query head, and
four loader waves keeping the next tiles in flight through a double buffer.
Every row folds the same tiles in the same order, so its bits are the
committed kernel's (checked on all head groupings and layouts, 60 of 60) and
chunking still never changes them; a new test checks rows a block and loaders
for every head grouping against each other and float64.
TF_ROCM_ATTN_RB and TF_ROCM_ATTN_PIPE override the layout for tuning.

R9700, 24 query / 4 KV heads: 4,096 queries after 28,672 / 61,440 keys went
from 42.9 / 93.0 ms to 39.9 / 82.0 ms (about 78 TFLOPS); 1-57 queries after
24k keys from 1.8-1.9 to 1.3 ms. A 4,096-token prompt chunk at 64k / 96k
tokens of context: 3.35 -> 3.19 s and 4.19 -> 3.88 s (1,223 -> 1,285 and
977 -> 1,056 tok/s). Splitting head dims across wave pairs measured 66-72
TFLOPS before any exchange of scores, so this layout stays.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The drafter's window keeps up to 2,047 context keys, and its block attention
ran one program per KV head: 8 programs a call on a 64-CU R9700, 0.46 ms each,
2.3 ms a drafted round. On ROCm a program now takes one query head and 512
context keys (or the block's own keys), and a merge adds the parts in key
order: 0.27 ms a round. A stream's drafts stay independent of the streams
beside it; NVIDIA keeps the one-pass kernel.

R9700, 16k tokens of context, a code prompt: 43.0 -> 41.2 ms a round.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rget 64

On ROCm, prepare stacks a GDN layer's [qkv | z | b | a] and an attention
layer's [q | k | v] into one matmul each, the members kept as views (no
memory added); the forward reads qkv and [q | gate] as strided views, so
gdn_pre, attn_prep and gate_mul take their row strides (NVIDIA passes the
contiguous stride and runs as before). Prefill still calls the members.
The WMMA K split now aims for 64 blocks instead of 128: every 27B projection
read at 12 rows as fast or faster. Both change decode bits alike for every
row count, so drafted still equals serial.

R9700, 16k tokens of context, a code prompt, 12 verify rows: 41.2 -> 40.0 ms
a round (matmuls 30.3 -> 29.2 ms, launches 1,007 -> 943), 68.7 -> 71.6 tok/s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… stride

With the stacked [q | k | v] projection (0b469e7), [q | gate] rows are a view
with a wider row stride. attn_prep loaded a query's first values by that
stride but its rotary partner by the old contiguous one, so every verify
window row past the first got another row's partner: drafted replies could
differ from "draft": false ones and fewer drafts were accepted. A lone row
(serial decoding, a window's first row) was unaffected.

The glue test now reads every input as a slice of one stacked output, as the
forward does; it fails with the old line. On the R9700: served greedy replies
drafted and with "draft": false are identical again (4,726 and 28,676-token
prompts), and a code prompt went from 6.79 back to 7.92 tokens a round.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he bytes

Opt-in. Per token and KV head, the 256 values are stored as e4m3 codes of
x * 2^-e (e the smallest exponent putting the row's largest magnitude at or
under 448) followed by e: 272 bytes instead of 512 (cuda/kernels/kv8.py, one
Triton launch per layer call). The WMMA prompt and tree attention kernels
widen each packed row to bf16 while staging it in shared memory, so the tile
and every fold are those of bf16 caches holding the rounded values; a verify
window's own keys and values are rounded the same way before its attention.
Drafted still equals serial, and any chunking gives the same bits.

Quality, the same rounding on bf16 caches: localeval mmlu 0.860 -> 0.864
(228, noise), ifeval 0.900 -> 0.900 (60), judged core set 1.000 -> 0.990 (10);
next-token distributions after 4k/16k/32k tokens of code: top-1 kept
96.9/99.2/97.7%, mean KL 0.007-0.012. The packed caches give those logits
exactly. Speed on the R9700, a drafted round at 64k tokens of context:
48.7 -> 46.5 ms (tree attention 9.7 -> 7.3 ms); 40.1 -> 39.6 ms at 16k. The
startup estimate counts the smaller cache.

Refused on NVIDIA, with --tp 2, with the Triton attention kernels, and for
head sizes other than 256.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The CUDA prompt kernel's Triton-bits test: on ROCm, prompt attention runs
attention_rocm.cu (head size 256) or _attend with ROCm's tiles, each with bits
of its own; the chunking test still runs. The MoE experts pack tests: those
kernels are NVIDIA's (ROCm serves Qwen3.8-27B only); the direct reader's own
tests run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The groups layout and its lane matmul serve ROCm only; on NVIDIA the test
tried to build qmm_rocm.cu. Found running tests/cuda on a DGX Spark (GB10):
with it skipped, the branch passes everything main passes there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Nipale-ai

Copy link
Copy Markdown
Contributor

Independent run on a second DGX Spark (GB10), container nvcr.io/nvidia/pytorch:26.07-py3, suites run one after the other (no parallel runs), tests/cuda:

checkout result time
PR head a263481 (rocm-r9700) 809 passed, 117 skipped, 0 failed 539 s
main 9cd52ab (0.5.0) 791 passed, 82 skipped, 0 failed 353 s

Your description reports 828 passes on this branch plus the tier (#106) and 783 on main, with the parallel-run failures put down to memory contention. Ours run sequentially and have no failures. The counts differ because the checkouts differ; we did not analyze the delta.

Served from the PR head, Vontra/Qwen3.8-27B-MLX-4bit with DFlash2 on an idle Spark: startup estimate 84.85 GiB within 96.31 GiB; window 262144; loaded in 13.8s. Released 0.3.7 on the same box gives 91.05 GiB for the same model and window. That fits your note that main counts the drafter at fp32 and this branch packs it.

That is relevant to #112: an RTX 5090 (32 GB) refuses the same model with drafts (weights 21.93 GiB + staging 7.27 GiB = 29.20 GiB against a 26.90 GiB budget, real load peak 16.6 GiB with a local test patch). We will try this PR head on that 5090 and report the result on #112.

Runs and write-up by AI agents working for Niklas Lenz (Devin, Claude).

@jkuepker

Copy link
Copy Markdown
Contributor Author

Thanks, this helps. Your one-at-a-time runs pass everything on both trees, so the 8 failures in our parallel main run were contention. The 35 extra skips on the PR head are the ROCm-only tests.

91.05 → 84.85 GiB is the drafter going from fp32 (7.2 GiB) to its packed size (1.0 GiB). The packed estimate applies on NVIDIA too, so on your 5090 the resident term should drop from 21.93 to about 15.7 GiB. Resident plus loading memory then comes to about 23.0 GiB against the 26.90 GiB budget, so it should admit a window, unless 0.5.0 changed the 7.27 GiB loading term. This PR doesn't change how #112 adds loading memory to resident memory; it only makes the drafter smaller. Looking forward to the 5090 result.

@Nipale-ai

Copy link
Copy Markdown
Contributor

@jkuepker the 5090 result is posted on #112: #112 (comment). Short version: on a free card the PR head serves Qwen3.8-27B with DFlash2 at window 16384 (startup estimate 23.44 GiB within 26.90 GiB), 214 to 216 tok/s drafted vs 75 serial, identical token hash; main still refuses. Thanks for the packed estimate.

Runs and write-up by AI agents working for Niklas Lenz (Devin, Claude).

@ashhart

ashhart commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thank you for this. It's a big piece of work, and the numbers are strong: 95.7-126.1 tok/s with DFlash2 on the R9700 against about 34 without, plus two real finds along the way (ROCm's unsorted topk giving the drafter a different tree each call, and buffer loads reading zeros at negative offsets). Your drafter-size fix is already in 0.6.0 with your name on it (#112).

We're closing this one because it's built on 0.3.4.1, and a lot has moved underneath it since: the memory estimate and much of the glue it touches were rebuilt. 0.6.0 also changes a default this PR relies on. CUDA now reads prompts at bf16 by default, with FP8 prompt inputs only behind an explicit flag, because we measured what FP8 prompts cost: on the 27B's code prompts, perplexity +4.1% against +0.03% at bf16.

We'd love to have it back. If you rebase onto 0.6.0 once it's out, with bf16 prompts as the default on ROCm too and FP8 as the opt-in, we'll run the NVIDIA side here the day it lands. We don't have an AMD card, so your R9700 numbers are the ones the docs will quote.

@ashhart ashhart closed this Sep 30, 2026
@jkuepker

Copy link
Copy Markdown
Contributor Author

Thanks for the close note, and for landing the drafter-size fix in 0.6.0.

A small correction for when we come back to this: #100 and #106 are based on 0.5.0, not 0.3.4.1 (merge-base 9cd52ab, "release: TensorFold 0.5.0"; both descriptions' test runs used that tree). Your reason still holds, since 0.6.0 rebuilds the memory estimate and glue underneath both.

Once 0.6.0 is public, we'll rebase #100 onto it with bf16 prompts as the ROCm default and FP8 prompt inputs behind 0.6.0's opt-in, then put #106 on top. We'll re-measure on the R9700: prefill at bf16 and FP8, and drafted replies against "draft": false. #106 also has fixes from a review since it closed (pinned-memory headroom, refusals before any download, restores that skip rows the GPU already holds), and those will come along.

Two things would help us match 0.6.0: the name of the FP8-prompt flag, and whether your perplexity numbers (+4.1% against +0.03%) come from a script we can run on the R9700, so the ROCm figures compare directly with yours.

@ashhart

ashhart commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thank you, and you're right about the base: both #100 and #106 sit on 0.5.0 (9cd52ab). My note was wrong there, sorry. 0.6.0 goes out tonight and has moved a lot underneath both, so we'll do the rebase ourselves in 0.6.1, keeping your commits, with bf16 prompts as the ROCm default and FP8 as the opt-in. If you push #106's review fixes to its branch, they'll come along too. Then we'll ask you to re-measure on the R9700, since we have no AMD card. The flag is --prefill-fp8; bf16 prompts are the default. The quality figures come from a harness that scores 8 x 4,096 tokens of an evals mix against a separate fp32 forward: KL, top-1, and perplexity on wikitext and code. It isn't in the repo yet. We'll add it under tools/ in 0.6.1 so your R9700 figures compare directly with ours.

@jkuepker

jkuepker commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

0.6.1 is out, and the ROCm work isn't in it: not #100, #106 or #144, and not the quality harness under tools/. No problem if it's queued for 0.6.2. Or, if it's easier for you, we can rebase #100 and #106 onto 0.6.1 ourselves:

Which would you prefer?

@millaguie

millaguie commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

@ashhart @jkuepker I've rebased #100 and #106 onto 0.6.2 in a fork and kept going on a Radeon AI PRO R9700, so you may not need to do the rebase yourselves. The PRs would go onto 0.6.3. Before I open anything, I'd like to know which shape you want, so nobody builds it twice.

Branch: https://github.com/millaguie/TensorFold/tree/rocm-r9700. A note on it: much of this was written with AI models (Claude Opus 5.5, Claude Fable 5.1, GLM-5.3 Flash and Kimi K3), and the branch's own commits still carry session lines. The PR branches will drop them, as CONTRIBUTING asks, and keep only commits I've reviewed and measured. The branch carries jkuepker's and ThinkOffApp's commits under their names, and follows the plan from this thread: bf16 prompts by default, --prefill-fp8 and --kv-dtype fp8 opt-in on gfx12 only, the WMMA kernels gated to gfx12 so #144's qgemv and Triton path serve other GPUs, and the tier hooked into PrefixCache (_drop, add, clear) without #106's one-window buffer, as jkuepker's note 1 on #106 suggested.

What it adds on top:

  1. gfx1201 prompt kernels: an FP8 prompt GEMM on v_wmma_f32_16x16x16_fp8_fp8 (activations loaded in WMMA fragment order) and a WMMA prompt attention. Bits don't depend on the chunking or the row count; the tests check both.
  2. --parallel on ROCm: three fixes found at three 48K-token streams. Expandable segments are off on ROCm: with them, near full VRAM, values changed after kernels had finished (NaN in the drafter's context). The concurrent decoder's memory gate counts packed FP8 rows (kv8.ROW8) instead of bf16 ones. And a prompt end restored from the tier keeps its own DeltaNet states (kept()), since --parallel commits replay them in place.
  3. The host-RAM tier (feat(cuda): host-RAM tier for Qwen3.8-27B prompt states (--ram-tier-gib), 126k window on a 32 GB R9700 #106, ported). Six 30K-token conversations served in turn: 0.22 s to a repeated turn's first token instead of 13.2 s, and the 18 replies have the same token hashes with the tier as without it.
  4. A Quark MXFP4 checkpoint of Qwen3.8-27B (W4A8 prompts, bf16 decode). This one runs FP8 activations defined by the checkpoint, so per CONTRIBUTING it's a separate conversation; I'd open an issue for it rather than a PR.

On one R9700 with that MXFP4 checkpoint, against vLLM-radiance on the same card and checkpoint: prompts at 6.7K tokens 2,656-2,678 tok/s (radiance 2,588), at 67K 1,896-1,915 (2,066); decode 45-48 tok/s chat and 119-120 code (54.5 and 99.5); three clients 95-104 tok/s total with a 3.2 s first token (113.5, 5.9 s). These come from my own bench. The PRs would carry your receipt (bench_concurrent.py --alone --serial, bench_openai.py, prefill_cold.py, before and after) and the MLX checkpoint as well.

For quality I ported tools/prompt_precision.py to the 27B on CUDA/ROCm (tools/prompt_precision_cuda.py on the branch). It uses the same sequences and the same sums, against reference.forward: fp32 matmuls of the dequantized weights. These are the R9700 figures on the wikitext and code sequences (24,576 rows):

Checkpoint, prompts, keys and values KL top-1 wikitext ppl vs fp32
MLX 4-bit, bf16, bf16 0.00072 98.74% -0.07%
MLX 4-bit, FP8, bf16 0.02751 94.43% +1.63%
MXFP4, bf16, bf16 0.00055 98.74% +0.04%
MXFP4, FP8, bf16 0.00476 97.49% +0.28%
MXFP4, bf16, FP8 0.00115 98.43% +0.18%
MXFP4, FP8, FP8 0.00503 97.18% +0.21%

MLX's FP8 prompts cost about 38x the bf16 KL here, against 20x in your 0.6.0 figures (0.0031 and 0.0624). The MXFP4 path's FP8 is 6x closer than MLX's, because its weight scales fold exactly into e4m3. I left the two chat sequences out. With them every configuration lands at KL 0.14-0.36. That comes from the model, not the kernels: on the tiled chat block the 27B, fp32 reference included, predicts '\n\n' where the block repeats, instead of copying it (next-token accuracy is about 60% on both sides). Every row is then a near-tie, and the smallest difference flips it. This may be worth knowing for the harness itself.

My proposal is a stack of small PRs: (1) ROCm on 0.6.x with the --parallel fixes, (2) the gfx1201 prompt kernels, (3) the tier, which is plain CUDA code you can check on your own machines, and (4) the MXFP4 issue. Would you take that, or do you prefer jkuepker or yourselves to carry #100/#106 and I send only what's on top? jkuepker, happy to fold this into your branches instead if you'd rather keep them.

@jkuepker

jkuepker commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

@millaguie thanks for carrying this forward and for the numbers. The prompt_precision_cuda.py table is especially useful.

Option A works for me: go ahead with the stack. (1) ROCm on 0.6.x with the --parallel fixes, (2) the gfx1201 prompt kernels, (3) the tier, and (4) the MXFP4 checkpoint as an issue. I'm happy to stop carrying #100/#106 myself. @ashhart, please say if you'd rather do it differently.

Two requests:

  1. Keep authorship on my commits where they survive the rebase, as you've done so far.
  2. Add my latest tier commits to (3). My rocm-r9700-ramtier branch has 5 commits your branch doesn't, d03f003 through 7bed996: a restore into the window copies only the rows it doesn't already hold, /health reports the tier's entries, bytes and drops, the tier and the GPU prefix cache find a prompt's entry with one lookup, and two tests for the one-window buffer. They should apply cleanly on top of your tier work. Tell me if they don't and I'll rebase them onto your branch.

I'll re-measure each PR on my R9700 as it opens: prompts at bf16 and with --prefill-fp8, decode, --parallel, your quality script, and --ram-tier-gib restore exactness. I'll post the numbers on each PR.

@jkuepker

jkuepker commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Correction to my request 2: none of those five commits needs adding. Checked against rocm-r9700 (d3954f9):

  • 8f764cf (/health stats) is already there as de3963c.
  • a66769c's shared lookup (streams.extended) is already in your tier port, ecd9906.
  • d03f003, 308e6f6 and 7bed996 test or speed up the one-window buffer, which your port drops, so leave them out.

One small ask instead: ecd9906 carries host_tier.py and its tests as I wrote them, so a Co-authored-by: jkuepker <[email protected]> trailer on it would keep that visible on GitHub.

@millaguie

millaguie commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

@jkuepker thank you, both for the go-ahead and for offering to re-measure each PR on your card. That helps a lot, since your R9700 is a second machine we can't otherwise get. Your commits will keep your authorship.

Edit: I wrote this before seeing your correction above, sorry, so part of it repeats what you had already checked. Done on your ask: ecd9906 now carries Co-authored-by: jkuepker. That rewrote the branch, so it is now 77c2d23 and de3963c is 9dd6046. The old hashes stay reachable under the tag r9700-before-coauthor.

About the five tier commits, I owe you a clearer explanation than my first comment gave. When I ported the tier to 0.6.x I followed your note 1 on #106 and left the one-window buffer out, hooking the tier into PrefixCache instead. I didn't say that plainly, sorry. Where each commit stands:

  • a66769c (one lookup through streams.extended): already in the branch, with the port (77c2d23).
  • 8f764cf (/health reports the tier): applied as 9dd6046, under your name. Its host_tier.py part was already there.
  • 7bed996, d03f003, 308e6f6: their host_tier.py changes (take(have=)) are in, but the rest builds on the window buffer (window_ids, _vacate, the window geometry and its tests), which the 0.6.x port doesn't have.

As you say, they can stay out. I still wanted to check that leaving the window out doesn't cost anything. So I ran both branches on one R9700, at the same 250 W cap and in the same session, alternating ours, yours, ours, yours. Both used the MLX 4-bit 27B with DFlash2, one stream, --kv-dtype fp8 --ram-tier-gib 8 --context 60000 (the same window on both), and tools/bench_conversations.py with six 30K-token conversations, three turns each, served in turn:

rocm-r9700 (0.6.x, no window buffer) rocm-r9700-ramtier (0.5.0, window buffer)
Startup estimate 18.80 GiB 21.63 GiB
First turn, median 21.7-22.0 s 44.8-44.9 s
Repeated turn, median 0.34 s 0.48 s
Repeated turn, p90 0.38 s 40.4 s

Some caveats. Your branch sits on 0.5.0, so the first-turn gap is mostly 0.6.x's prompt path and the gfx1201 kernels, not your tier. The repeated-turn p90 looks like some turns on your branch being prefilled again rather than restored. I may well be missing a setting there, so if there is a flag or a step I should have used, please tell me and I'll rerun it. Without --context, the 0.6.x branch also admits the full 262,144-token window against about 64K on yours, but that comes from 0.6's capacity planning, not from either tier.

My proposal for (3) is to bring your original tier commits over first, under your name, and put my 0.6.x port on top, so the PR shows what's yours and what's the adaptation. If you think the window buffer is still worth having on top of 0.6's KVRoom, I'm happy to measure whatever you'd like to try.

And is there something we're not seeing? You know the window buffer far better than I do. If it solves a case this bench doesn't exercise (other context lengths, more or fewer conversations, a different memory budget, something you saw on your setup), I'd rather hear it now and measure that too than leave it out by mistake.

@jkuepker

jkuepker commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for measuring this so carefully. You didn't miss a flag: the p90 is a bug in my window-buffer path. When a conversation comes back, _resume finds its entry in the RAM tier, then keep_path moves the other GPU states to RAM before take copies the entry back. With 8 GiB full, put throws out the oldest entry, which with conversations served in turn is often the one about to be restored. So 2 of your 12 repeated turns were prefilled again. dropped doesn't count this, so /health looks clean. Each turn also keeps one more ~190 MiB DeltaNet+drafter copy per conversation, so six 30K conversations need about 9.5 GiB by turn 3.

Your port doesn't have this path, and your numbers show the window buffer costs 2.8 GiB at startup without a measured gain on this bench. So I agree: leave it out, and bring my tier commits over first with your port on top, as you proposed. If you want to confirm, a rerun of my branch with --ram-tier-gib 12 should bring its p90 down to about the median.

ThinkOffApp added a commit to ThinkOffApp/TensorFold that referenced this pull request Oct 6, 2026
On gfx1201 (RDNA4) this path's forward gives non-finite logits; with drafts the verify loop
then never returns (reported on ashhart#144 with an R9700). gfx12 runs on ashhart#100's WMMA kernels, so
this path now stops at startup there with a message that says so. Nothing changes on gfx1151.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jkuepker

Copy link
Copy Markdown
Contributor Author

Reran it on the R9700 (rocm-r9700-ramtier @ 7bed996, six 30K conversations, 4 turns, --kv-dtype fp8; --context 48000, because 60000 doesn't fit on this box): with --ram-tier-gib 8 the repeat-turn p90 is 16.17 s (conversation 5's turns 2–4 prefill again, with /health showing dropped: 0); with --ram-tier-gib 12 p90 is 0.30 s, the same as the median, with no repeat prefilled. That's the window-buffer bug, not a missing flag, so please go ahead with your port (no window buffer) on top of my original tier commits.

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.

4 participants