Skip to content

HIP: native Zig engine for Qwen3.5 MLX affine on RDNA2 / RDNA3 - #490

Closed
BlivionIaG wants to merge 176 commits into
ashhart:mainfrom
BlivionIaG:hip-port
Closed

BlivionIaG wants to merge 176 commits into
ashhart:mainfrom
BlivionIaG:hip-port

Conversation

@BlivionIaG

@BlivionIaG BlivionIaG commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Draft, for reference only: this will be split. The branch carries the whole HIP port, so each piece can be read
against a working end-to-end result. It will land as small pull requests in the order agreed in #426: the runtime,
then the exact matmul and kernels, then the Qwen3.5 family on the shared lane core. The runtime part can sit on #463
or replace it, whichever structure reviewers prefer.

What this changes

Adds a native HIP backend to the Zig engine: tensorfold-native serves the Qwen3.5 family on AMD Radeon GPUs.

  • Models: Qwen3.5-0.8B and 9B, Qwen3.8-27B and Qwen3.6-35B-A3B (MoE), with or without the MTP head.
  • Weights: MLX affine, 2 to 8 bits, groups of 32 / 64 / 128, mixed widths per tensor.
  • GPUs: RDNA2 (gfx1030, fp16) and RDNA3 (gfx1100, bf16, matrix cores on or off). gfx1151 is built, not run.
  • Serving: shared lane rounds with MTP drafts, 1,024-row prompt chunks between decode rounds, HIP graphs, a radix
    prompt cache and --tp 1/2/4/8 over RCCL.

Kernels are code objects embedded in the binary and launched from Zig, as on CUDA. The CUDA and Metal sources are upstream's, byte for byte.

zig build native-hip -Dhipcc=/opt/rocm/bin/hipcc   # zig-out/native-hip/bin; -Dgfx=rdna2,rdna3,rdna3.5 for every part
tensorfold-native serve Qwen3.6-35B-A3B-MLX-4bit-MTP --name m
tensorfold-native serve MODEL --tp 2 --rank 0 --master 127.0.0.1 &   # rank 0 serves HTTP
tensorfold-native serve MODEL --tp 2 --rank 1 --master 127.0.0.1

serve flags

The HIP engine takes upstream's flags: --tp (now 1, 2, 4 or 8), --rank, --master, --master-port,
--checkpoint-slots, --mtp-drafts (at most 3 on HIP), --mtp-confidence, --device, --prompt-cache-gib and
--parallel. --backend gains rocm. Memory follows the CUDA variables: TENSORFOLD_MEMORY_RESERVE_GIB, and
TENSORFOLD_HIP_MEMORY_LIMIT_GB as CUDA's TENSORFOLD_CUDA_MEMORY_LIMIT_GB.

One flag is new, --policy key=value,... (or TF_POLICY), for the HIP engine's run choices:

key default effect
matrix auto Matrix cores (gfx11): on, off, or auto to follow the tuning table.
graphs auto Replay rounds as HIP graphs: auto is on at tp 1, off under tp.
prefill_step 1024 Prompt rows filled a chunk (a multiple of 64).
kernels, stream, gemm, fuse, gdn auto reference swaps in reference kernels (all, or one family), for checking.
attention, launch auto, zig f32 drops the 64-row attention tile; library launches through C.
rccl_lib system The RCCL library to load.

How it's built

zig/src/hip/                 runtime/, comm/ (RCCL), caps.zig, policy.zig, launch/, ops/, tuning/*.zon
zig/src/core/                quant/ (formats), registry.zig, prompt_radix*.zig, round_shape.zig, admission.zig
zig/src/families/qwen3_5/    weights/ (any backend), hip.zig + backend/hip/{model,forward,engine}/
zig/kernels/hip/             common/, quant/, tiles/, attention/, recurrence/, decode/, ops/, comm/, capi/
zig/tests/hip/, qwen35/      tf-hip-test (runtime, kernels), tf-qwen35-test check, matrix.sh
  • Caps: what the GPU can do (wave size, dot2, sdot, matrix cores), from its gfx id. Kernels test caps, never GPU
    names; unknown GPUs are refused.
  • Policy: --policy plus the MTP and prompt-cache flags, resolved once at open, logged, and sent by rank 0 to
    the other ranks.
  • Formats (core/quant/): detect, read, tp slices, upload and an fp64 reference. Today mlx.zig and
    dense.zig; the model never names a format.
  • Tiles: template <Decoder, Act, Dot, Epilogue>. A new format adds a decoder, a new instruction adds a Dot.
  • Registry: (op, format, path, shape) → kernel by cost from a per-GPU table, one kernel family per path at any
    row count (check --explain-kernels prints the choices).
  • Graphs: a round is keyed by its bucketed shape; kernels read the round's rows and page tables from one device
    buffer. A speculative round is two replays: verify, then the MTP drafts.
  • Radix prompt cache: full-attention K/V in shared 64-position pages; a request resumes from its deepest match with a
    linear-state snapshot. Under tp, rank 0 owns the tree.
  • Memory uses the CUDA path's admission (core/admission.zig, now shared): measured weights, scratch sized by
    running the real forwards on a counting stream, and a startup check that held bytes equal the plan. --parallel auto
    serves what fits (up to 8); a fixed --parallel N that does not fit is refused. A draft window keeps recurrence
    inputs, not a state copy per row, and the keep replays them: the same bits for about 2 % of the memory.
  • Exactness: fp16 / bf16 operands with fp32 sums. A row's bits do not depend on its batch, prompt cut, drafting or
    graph replay. Accuracy is measured against an fp64 CPU forward of the same checkpoint (tools/truth/).
flowchart TB
  subgraph Server["tensorfold-native (rank 0 serves HTTP)"]
    API[OpenAI / Anthropic routes] --> Host[lane host: chunked prompt fill, cancel]
    Host --> Core[lane core: rounds, drafts, keeps]
  end
  Core --> Backend[HIP lanes backend]
  Backend --> Radix[radix prompt cache<br/>pages + linear snapshots]
  Backend --> MTP[MTP head<br/>rank 0 only]
  Backend --> Fwd[forward: prefill chunk / round window]
  Backend -. "steps of rank 0 (prefill, verify, keep, pages, snapshots)" .-> Followers[ranks 1..N-1]
  Fwd --> Ops[Ops: project, attention, recurrence, moe, norms]
  Ops --> Reg[registry: op, format, path, shape → kernel<br/>under Caps and Policy]
  Reg --> CO2["gfx1030 code object<br/>fp16, dot2"]
  Reg --> CO3["gfx11 code object<br/>bf16, dot2 + WMMA"]
  Ops --> RCCL[RCCL: rank-order sums, gathers]
  Followers --> RCCL
Loading
sequenceDiagram
  participant C as lane core
  participant B as HIP backend
  participant G as GPU
  C->>B: windows: each stream's pending token + MTP drafts
  B->>G: copy the round plan, run verify (graph replay at tp 1)
  G-->>B: tokens drawn on the device
  B-->>C: tokens, and the core decides how many rows each stream keeps
  C->>B: keep k rows
  B->>G: keep: rebuild conv windows, replay DeltaNet states through the kept rows
  B->>G: MTP head drafts every stream's chain (rank 0)
  G-->>B: next drafts and their probabilities
Loading

Not met yet:

  • MTP with four streams on one GPU is below no drafts (35B-A3B V620 195 → 185 tok/s); it helps one stream
    (101 → 126) and four streams at tp 4 (218 → 268).
  • tp 8 across two PCIe switches is slower than tp 4 on one switch: prefill 3,175 against 4,070 tok/s at 2k, one
    stream 126 against 149 tok/s. Every other rank's traffic crosses the CPU's root ports. (Before ACS-off applied to both
    switches it was 694 tok/s, as with P2P off.)
  • Graphs are off for prefill chunks, and opt-in under tp (graphs=on).

Engine checks, one GPU (tf-qwen35-test check, one process a model; W7800 with matrix cores on)

model GPU status invariants KL prefill KL decode top-1 % prefill 2k tok/s prefill 8k decode 1 stream 1 + MTP decode 4 streams 4 + MTP
0.8B 5-bit (requantized) V620 PASS 35/35 2.522e-6 2.313e-6 99.76 13,119 11,467 300.7 - 862.1 -
0.8B 8-bit V620 PASS 35/35 2.458e-6 2.331e-6 100.00 13,082 11,476 254.1 - 763.7 -
9B 2-bit (requantized) V620 PASS 35/35 1.954e-4 1.752e-4 98.97 1,331 1,287 93.3 - 231.0 -
9B 4-bit MTP V620 PASS 39/39 1.892e-5 1.518e-5 99.90 1,304 1,257 66.6 116.1 198.5 159.7
9B 5-bit (requantized) V620 PASS 35/35 2.372e-6 2.046e-6 99.90 1,279 1,238 57.9 - 173.8 -
9B 6-bit V620 PASS 35/35 2.943e-6 2.609e-6 99.90 1,270 1,223 51.2 - 164.2 -
9B 8-bit V620 PASS 35/35 2.840e-6 4.654e-6 99.85 1,208 1,178 42.7 - 141.7 -
27B 3-bit MTP V620 PASS 39/39 3.882e-6 3.585e-6 99.85 389 376 25.7 41.2 64.4 57.9
27B 2-bit V620 PASS 35/35 1.308e-5 1.273e-5 99.95 390 379 32.7 - 73.8 -
27B 4-bit V620 PASS 35/35 5.861e-6 6.163e-6 99.90 380 364 22.2 - 61.4 -
35B-A3B 4-bit MTP V620 PASS 39/39 1.893e-3 2.190e-3 98.44 2,627 2,394 101.3 125.8 194.7 185.1
0.8B 5-bit (requantized) W7800 PASS 35/35 1.480e-4 1.422e-4 99.07 17,522 14,693 355.3 - 976.1 -
0.8B 8-bit W7800 PASS 35/35 1.503e-4 1.609e-4 99.56 17,429 14,724 313.3 - 863.9 -
9B 2-bit (requantized) W7800 below bar¹ 35/35 3.034e-2 1.312e-2 90.19 2,159 2,033 95.4 - 235.9 -
9B 4-bit MTP W7800 PASS 39/39 2.017e-4 1.722e-4 99.71 2,106 1,996 75.5 108.4 205.6 193.0
9B 5-bit (requantized) W7800 PASS 35/35 1.074e-4 1.441e-4 99.61 2,093 1,981 71.5 - 199.5 -
9B 6-bit W7800 PASS 35/35 2.310e-4 2.738e-4 99.61 2,090 1,973 63.2 - 183.2 -
9B 8-bit W7800 PASS 35/35 1.680e-4 2.236e-4 99.07 2,100 1,987 57.0 - 168.3 -
27B 3-bit MTP W7800 PASS 39/39 2.426e-4 2.130e-4 99.56 628 593 28.8 40.8 74.2 64.5
27B 2-bit W7800 PASS 35/35 8.810e-4 8.630e-4 98.54 669 637 33.4 - 82.0 -
27B 4-bit W7800 PASS 35/35 1.465e-4 1.539e-4 99.37 629 593 25.9 - 67.4 -
35B-A3B 4-bit MTP W7800 PASS 39/39 2.879e-3 3.084e-3 98.00 3,660 3,237 122.6 126.7 220.1 203.7

Matrix cores off on the W7800 (0.8B 8-bit, 9B 4-bit MTP, 9B 8-bit, 27B 4-bit): the same KL and top-1 digits as on; prefill 2k 17,429 → 12,708, 2,106 → 1,312, 2,100 → 1,313, 629 → 394 tok/s; decode within 5 %.

Engine checks under tp (invariants and speed; accuracy is checked at tp 1)

model GPUs status invariants decode 1 stream 1 + MTP decode 4 streams 4 + MTP
9B 4-bit MTP 2 × V620 PASS 9/9 81.0 137.9 259.1 169.1
27B 3-bit MTP 2 × V620 PASS 9/9 34.9 54.1 95.8 67.2
27B 4-bit 2 × V620 PASS 5/5 31.2 - 90.8 -
35B-A3B 4-bit MTP 2 × V620 PASS 9/9 76.2 108.2 213.7 214.5
9B 4-bit MTP 4 × V620 (one switch) PASS 9/9 93.2 152.5 295.5 215.2
27B 3-bit MTP 4 × V620 (one switch) PASS 9/9 41.9 64.3 120.5 90.7
35B-A3B 4-bit MTP 4 × V620 (one switch) PASS 9/9 73.7 106.0 218.3 268.4
9B 4-bit MTP 8 × V620 (two switches) PASS 9/9 72.5 103.5 179.5 113.9
35B-A3B 4-bit MTP 8 × V620 (two switches) PASS 9/9 56.2 85.3 165.4 162.0
27B 4-bit 2 × W7800 PASS 5/5 40.9 - 116.6 -
35B-A3B 4-bit MTP 2 × W7800 PASS 9/9 104.1 144.5 268.8 258.3

Served (tensorfold-native serve; decode: tools/bench_openai.py, tools/bench_concurrent.py, 256 tokens; prefill: serve_prefill.py, cold, median of 3)

GPU model decode 1 stream, code / chat tok/s 4 streams total, code / chat prefill 2k tok/s 8k 32k 64k memory plan (prefill server)
V620 0.8B 8-bit 254.5 / 255.7 774.7 / 764.9 12,131 10,819 7,368 5,104 weights 0.75 GiB, 0.77 GiB a stream at 65,600 tokens, 25.0 GiB free after a 4.0 GiB reserve
V620 9B 4-bit MTP 114.7 / 99.2 214.7 / 197.3 1,289 1,236 1,056 888 weights 4.82 GiB, 2.05 GiB a stream at 65,600 tokens, 20.9 GiB free after a 4.0 GiB reserve
V620 27B 3-bit MTP 42.9 / 38.6 70.6 / 64.8 390 375 331 287 weights 11.65 GiB, 4.15 GiB a stream at 65,600 tokens, 13.4 GiB free after a 4.0 GiB reserve
V620 35B-A3B 4-bit MTP 160.2 / 134.0 258.2 / 242.6 2,281 2,114 1,583 1,190 weights 18.71 GiB, 1.31 GiB a stream at 65,600 tokens, 6.5 GiB free after a 4.0 GiB reserve
2 × V620 35B-A3B 4-bit MTP 148.2 / 126.3 340.6 / 285.5 3,486 3,288 2,629 2,060 weights 10.07 GiB, 0.66 GiB a stream at 65,600 tokens, 15.0 GiB free after a 4.0 GiB reserve
4 × V620, one switch 35B-A3B 4-bit MTP 149.4 / 125.6 399.8 / 327.9 4,070 3,940 3,426 2,905 weights 5.62 GiB, 0.64 GiB a stream at 65,600 tokens, 19.4 GiB free after a 4.0 GiB reserve
8 × V620, two switches 35B-A3B 4-bit MTP 126.1 / 97.6 378.5 / 292.4 3,175 3,133 2,917 2,666 weights 3.39 GiB, 0.63 GiB a stream at 65,600 tokens, 21.8 GiB free after a 4.0 GiB reserve
V620 27B 4-bit 22.6 / 22.7 64.1 / 63.3 379 366 325 283 weights 14.10 GiB, 4.15 GiB a stream at 65,600 tokens, 10.6 GiB free after a 4.0 GiB reserve
2 × V620 27B 3-bit MTP 58.9 / 55.2 107.3 / 91.5 710 686 606 526 weights 7.02 GiB, 2.08 GiB a stream at 65,600 tokens, 18.3 GiB free after a 4.0 GiB reserve
4 × V620, one switch 27B 3-bit MTP 70.7 / 65.9 156.4 / 132.7 1,028 1,003 911 816 weights 4.37 GiB, 1.04 GiB a stream at 65,600 tokens, 21.0 GiB free after a 4.0 GiB reserve
W7800 27B 3-bit MTP 53.4 / 45.8 84.8 / 81.9 640 610 514 425 weights 11.65 GiB, 4.15 GiB a stream at 65,600 tokens, 27.8 GiB free after a 4.5 GiB reserve
W7800 27B 4-bit 25.8 / 27.2 70.5 / 68.9 648 618 522 431 weights 14.10 GiB, 4.15 GiB a stream at 65,600 tokens, 25.1 GiB free after a 4.5 GiB reserve
2 × W7800 27B 4-bit 41.2 / 41.2 116.1 / 119.2 1,123 1,074 907 757 weights 7.38 GiB, 2.08 GiB a stream at 65,600 tokens, 14.4 GiB free after a 4.5 GiB reserve
W7800 35B-A3B 4-bit MTP 183.8 / 145.4 291.8 / 248.1 3,174 2,896 2,074 1,506 weights 18.71 GiB, 1.31 GiB a stream at 65,600 tokens, 21.0 GiB free after a 4.5 GiB reserve

Qwen3.8-27B matrix (ddalcu MLX builds with the MTP head; served as above; prefill only without drafts)

GPU bits tp MTP drafts matrix cores decode 1 stream, code / chat 4 streams total, code / chat prefill 2k / 8k / 32k / 64k streams served
W7800 4 1 0 off 27.7 / 28.0 74.1 / 72.2 394 / 381 / 342 / 300 4
W7800 4 1 0 on 27.7 / 28.1 75.9 / 74.1 639 / 608 / 512 / 424 4
W7800 4 1 2 off 27.0 / 27.4 72.6 / 70.8 - 4
W7800 4 1 2 on 27.2 / 27.7 74.3 / 72.3 - 4
W7800 4 2 0 off 41.5 / 42.3 116.5 / 116.4 698 / 677 / 608 / 537 4
W7800 4 2 0 on 41.6 / 42.4 - / - 1,129 / 1,078 / 911 / 760 4
W7800 4 2 2 off 36.7 / 37.4 72.6 / 72.7 - 4
W7800 4 2 2 on 36.7 / 37.4 72.6 / 72.7 - 4
W7800 6 1 0 off 23.1 / 23.3 68.0 / 66.5 384 / 373 / 335 / 295 4
W7800 6 1 0 on 23.1 / 23.3 67.9 / 66.3 621 / 593 / 502 / 417 4
W7800 6 1 2 off 22.9 / 23.2 67.3 / 65.8 - 4
W7800 6 1 2 on 22.9 / 23.1 64.4 / 65.4 - 4
W7800 6 2 0 off 35.0 / 35.5 111.8 / 106.3 697 / 675 / 606 / 536 4
W7800 6 2 0 on 35.1 / 35.6 111.9 / 106.4 1,111 / 1,062 / 899 / 752 4
W7800 6 2 2 off 31.6 / 32.0 70.2 / 68.0 - 4
W7800 6 2 2 on 31.6 / 32.0 70.2 / 68.0 - 4
W7800 8 1 0 off 19.8 / 20.0 35.4 / 35.4 379 / 369 / 331 / 292 2
W7800 8 1 0 on 19.9 / 20.1 35.5 / 35.5 612 / 585 / 496 / 411 2
W7800 8 1 2 off 19.7 / 19.9 35.3 / 35.2 - 2
W7800 8 1 2 on 19.6 / 19.9 35.2 / 35.1 - 2
W7800 8 2 0 off 30.5 / 30.3 99.6 / 97.2 697 / 674 / 606 / 535 4
W7800 8 2 0 on 30.5 / 30.4 99.4 / 97.0 1,111 / 1,060 / 898 / 751 4
W7800 8 2 2 off 27.5 / 27.4 64.4 / 63.4 - 4
W7800 8 2 2 on 27.6 / 27.5 64.4 / 63.5 - 4
V620 4 1 0 - 22.6 / 22.7 41.0 / 40.7 379 / 366 / 324 / 282 2
V620 4 1 2 - 22.4 / 22.4 40.7 / 40.4 - 2
V620 4 2 0 - 33.1 / 33.0 93.7 / 94.7 704 / 678 / 598 / 523 4
V620 4 2 2 - 29.2 / 29.1 63.9 / 64.3 - 4
V620 4 4 0 - 40.1 / 39.7 123.6 / 124.8 1,001 / 976 / 890 / 797 4
V620 4 4 2 - 35.2 / 35.0 84.9 / 85.2 - 4
V620 4 8 0 - 35.2 / 35.0 123.3 / 124.0 884 / 874 / 834 / 785 4
V620 4 8 2 - 31.9 / 31.8 89.1 / 89.5 - 4
V620 6 1 0 - 17.4 / 17.4 17.2 / 17.1 - 1
V620 6 1 2 - 17.2 / 17.2 17.1 / 17.0 - 1
V620 6 2 0 - 26.5 / 26.1 85.2 / 83.5 679 / 655 / 587 / 514 4
V620 6 2 2 - 23.6 / 23.5 58.1 / 57.3 - 4
V620 6 4 0 - 34.8 / 34.7 115.2 / 113.7 987 / 962 / 881 / 791 4
V620 6 4 2 - 31.2 / 31.1 78.2 / 77.4 - 4
V620 6 8 0 - 32.7 / 32.5 117.6 / 116.4 883 / 874 / 833 / 785 4
V620 6 8 2 - 29.4 / 29.4 84.2 / 83.7 - 4
V620 8 2 0 - 22.0 / 22.6 74.7 / 72.9 692 / 668 / 593 / 520 4
V620 8 2 2 - 20.3 / 20.7 51.6 / 50.9 - 4
V620 8 4 0 - 32.1 / 32.9 108.9 / 106.8 994 / 970 / 884 / 794 4
V620 8 4 2 - 28.4 / 29.1 74.4 / 73.9 - 4
V620 8 8 0 - 30.6 / 31.5 112.3 / 111.9 880 / 871 / 831 / 783 4
V620 8 8 2 - 27.3 / 28.1 81.0 / 81.1 - 4

Receipt

  • Measured at hip-port 170df51 on upstream 1.0.1, then merged with upstream main 1.0.2 and aligned to upstream's flags
    (be6b679). Zig 0.17.0, ROCm 7.14, Linux 7.1.7.
  • Host: 8 × Radeon PRO V620 (gfx1030, 32 GB) behind two PCIe switches, 2 × Radeon PRO W7800 (gfx1100, 48 GB,
    capped at 180 W).
  • Checkpoints: MLX community releases; 5-bit and the 9B 2-bit were requantized from bf16 (tools/mlx_requant.py).
    27B 2-bit is lukaskremla/Qwen3.8-27B-2bit-MLX-TextOnly.
  • Accuracy bar (2,048 Wikitext-2 tokens against fp64): KL mean ≤ 1e-2, top-1 ≥ 90 %, perplexity within 2 %.
  • Full matrix again at be6b679 (every checkpoint at tp 1 on both GPUs, matrix cores off, tp 2, 4 and 8): 35 of 36 rows pass, the 36th the 9B 2-bit below the bar¹ with the same numbers; KL digits unchanged.
  • 27B matrix: every finished row has its alone and serial outputs equal (34 a run, 0 unequal). Where "streams served" is
    below 4, memory capped it (V620 4-bit tp 1: 2; V620 6-bit tp 1: 1; W7800 8-bit tp 1: 2, the fp32 copy of the bf16 embedding
    costing 2.5 GiB) and the 4-stream column ran that many at once. V620 6-bit tp 1 has no prefill row: a 65,600-token window
    does not fit one 32 GB card. W7800 4-bit tp 2 with matrix cores on is pieced together: decode from a clean rerun, prefill from the first run
    (measured after a stray server on that GPU was gone); its 4-stream and alone/serial runs were cut by a host reset.
  • Exactness: every check invariant passes on every row; every served --alone --serial comparison is equal
    (34 a configuration).
  • Memory: the 27B 4-bit now serves 4 streams at a 32k window on one 32 GB V620.
  • Tests: zig build test on Metal 111/111 steps (with the HIP mock-library host tests) and test-golden; Linux host tests pass except upstream's direct_io
    O_DIRECT test, which fails the same way on upstream main; tf-hip-test runtime and kernels pass on gfx1030 and
    gfx1100, and check passes on both GPUs and at tp 2, at be6b679 too; lean_check.py adds no problems.
  • Not run: CUDA and Metal device suites (no NVIDIA GPU here). 35B-A3B and 0.8B 2-bit are too degraded to judge an
    engine by (35B-A3B 2-bit fp64 perplexity 67.8 against 4.77 at 4-bit). vLLM was stopped before a result.
  • ¹ The requantized 9B 2-bit is below the bar on bf16 (its fp64 perplexity is 210).

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Prompt processing is no slower than the last release. (CUDA and Metal are untouched; HIP is new.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. zig build test
    runs the HIP host tests on any host, macOS included, against a stand-in libamdhip64; tf-hip-test and
    tf-qwen35-test are separate programs that need an AMD GPU.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

…and tf-hip-test

zig/src/hip mirrors zig/src/cuda: the entry points declared by hand from ROCm's ABI (checked on 7.14), the
device, memory, streams, modules, launches, cooperative grids and graphs. zig/build/hip.zig builds each
zig/kernels/hip source into one offload bundle for gfx1030, gfx1100 and gfx1151 (-Dhipcc, or -Dhsaco for
prebuilt ones). Stream-less fills and device copies finish before they return, so a non-blocking stream's
next kernel cannot race them. tf-hip-test passes info, smoke, graph, cooperative, image and overhead on a
Radeon PRO V620 (gfx1030) and a W7800 (gfx1100); zig build test passes.
…ry a GPU family

zig/kernels/hip/rocm holds the Python ROCm extensions' kernels as they are (rocm-pr 7a0ab2a). capi.hip puts
their launchers behind a C ABI that reports errors instead of throwing; ops.hip adds the torch ops between
them with torch's rounding and libm calls (embedding rows, casts, silu products, the attention gate, prefill
RoPE, conv and DeltaNet gate, cache writes, the MoE route plan). zig/build/hip.zig builds them with the
extensions' own hipcc flags into libtf_rdna2.so (gfx103x, dot2) and libtf_rdna3.so (gfx11 / gfx12, WMMA);
the binary embeds both and opens its GPU's from memory. tf-hip-test library passes on a V620 and a W7800.
…against the Python engine

zig/src/hip/ops.zig launches the kernel library with its shapes checked and its scratch from a per-forward
arena, making the Python wrappers' choices the same way (dot2 splits, the RDNA2 decode tile's fp16 output,
the cast to the activation dtype). tools/zig/qwen_rocm_dump.py dumps the Python ROCm engine's affine
products and per-layer residuals as fixtures; tf-hip-test affine matches all 126 cases (bits 2 to 8,
groups 32 to 128, 1 to 300 rows) bit for bit on a V620 (fp16) and a W7800 (bf16).
…rwards, bit-exact layer by layer

zig/src/families/qwen35 reads a Qwen3.5 / 3.6 MLX checkpoint as the Python ROCm loader does (every tensor
byte-identical on seven checkpoints, dense, MoE and MTP heads), uploads it, and runs the Python forward on
the kernel library: prompt spans with the prefill formulas, lane windows with the decode kernels and per-row
states for the commit, the routed experts, and the draw rule on the host. tf-qwen35-test layers runs a
prompt and three greedy steps of Qwen3.5-0.8B 8-bit against tools/zig/qwen_rocm_dump.py: every layer's
residual, the final rows, the logits and the tokens equal on a V620 (gfx1030) and a W7800 (gfx1100).
…on engine's

engine.zig holds a loaded model on one device (library, stream, scratch, pinned ids and logits) and runs
prompts and shared windows; hip_lanes.zig is the lane core's backend over it: a stream's caches each, every
round's windows in one forward, each row drawn on the host at the engine's keyed position, a window kept whole
unless the round trims it. tf-qwen35-test lanes runs a prompts file through the core. Every checkpoint
matches the Python engine layer by layer on a V620 and a W7800 (0.8B 8-bit, 4B 3-bit, 9B 4- and 6-bit, 27B
4-bit, 35B-A3B 4-bit MoE); four prompts' 64-token replies equal its generate, greedy and sampled, alone and
together. qwen_rocm_dump.py gains generate, the replies' reference.
…idates in one pass

Every window width keeps a row's bits, so the backend declares 16-row windows and the core shares rounds
between streams (and copies context drafts into them). The keyed draw takes its top_k + 8 candidates in one
pass instead of sorting the vocabulary. Four 64-token replies of Qwen3.5-0.8B on a V620: 241 tok/s together
against 126 alone, greedy and sampled, each equal to the Python engine's.
native/hip.zig opens Qwen3.5 / Qwen3.6 MLX checkpoints (qwen3_5, qwen3_5_moe) on the lane core over the HIP
backend, behind the same lane host as Metal; zig build native on Linux builds tensorfold-native with it. The
server's key-file checks read file metadata with statx on Linux (fstat and stat elsewhere), the one
macOS-only call on its path. The family imports core's safetensors reader alone, so the server's tokenizer
stays in one module. Serving Qwen3.5-0.8B 8-bit on a V620 answers chat completions at 122 tok/s.
…al one

mtp.zig runs the checkpoint's MTP head as the Python drafter: the last kept final row and the token after it,
normed and projected (fc_e, fc_h; an unquantized fc through an fp32 row projection), the head's gated
attention over the chain's own cache, its MLP or experts, the final norm and the logits; each draft drawn
with the request's sampling at its slot. The backend holds a stream's drafts for its next window, keeps the
last kept final row for the head, and commits the rows a shared round's draft request names before its keep.
Scratch for multi-row windows counts every linear layer's per-row states. Qwen3.5-9B 4-bit with its head:
four 64-token replies equal the Python engine's serial ones, greedy and sampled, alone and together.
The lane core carries a request's history_len and shared_prefixes to its backend, and the host reports the
tokens a backend reused. The HIP backend keeps a stream's caches at the cuts (the shared prefixes, the history,
the prompt's end) within a byte budget, and a later prompt with a kept prefix copies them in and prefills only
the rest: a resumed reply equals a fresh one and the Python engine's. memory.zig ports the Python plan: the
largest window where the scratch and every lane's caches plus a kept copy fit beside the weights and the
reserve; the server refuses a --context that does not fit and prints the plan. Stream caches are sized per
request.
# Conflicts:
#	zig/src/families/qwen35/engine.zig
#	zig/src/families/qwen35/hip_lanes.zig
…n without host round trips

Two kernels draw a round's rows on the GPU: argmax with torch's rule (a NaN wins, else the first maximum) and
the top_k + 8 candidates by value then id (a monotone 16-bit key, two histogram passes, one ordered
compaction), so a round reads back ids or candidate pairs instead of whole logits rows; the keyed choose stays
on the host. A greedy MTP chain feeds each step's argmax to the next step's embedding on the device and reads
the drafts once. Replies stay equal to the Python engine's (0.8B and 9B with its head, greedy and sampled,
alone and together).
The device code builds per GPU family as hipcc --genco code objects with the library's own flags (the same
device code), embedded in the binary; launches.zig and affine_launch.zig port every launcher the engine uses
(the affine row, lane and GEMM tiles and their RDNA2 paths, routed experts, norms, conv, RoPE, attention's
decode walk and prefill tile, the recurrence, MoE routing, every torch-op kernel) with the same grids, blocks,
shared memory and arguments, symbols resolved by name when the family loads. TF_HIP_LAUNCH=library keeps the
C launchers for an A/B. The byte table of the RDNA2 8-bit wide path is filled per device. Affine products,
layers and replies stay equal to the Python engine on a V620 and a W7800 (0.8B, and the 35B MoE).
# Conflicts:
#	zig/src/families/qwen35/engine.zig
#	zig/src/families/qwen35/hip_lanes.zig
…engine's tp

rccl.zig opens RCCL through dlopen (all-reduce, all-gather, broadcast on the engine's stream); link.zig hands
rank 0's unique id to the other ranks over TCP and then carries its commands. slicing.zig cuts each rank's
share of the host tensors as slicing.py does: heads and rows per rank, o, out and down by input groups
returning fp32 shares, KV heads replicated past their count, experts by rank with the shared one on rank 0
and a remap, the vocabulary rows of the head. reduce.zig sums the shares as the Python ROCm engine does (one
fp32 all-reduce at two ranks, the rank-order sum past two) and the residual adds the sum to its widened self,
rounding once; the MoE share follows _run_share. Rank 0 runs the lane core; worker.zig mirrors its steps.
At tp 2 on two V620s, 64 to 128-token replies of the 0.8B, 4B, 9B (4- and 6-bit), 27B and 35B-A3B equal
the Python engine's tp 2 replies, greedy and sampled, alone and together; layer by layer equal for the
0.8B and the 35B. The slices equal slice_for_tp's bytes at tp 2, 4 and 8. Programs that start RCCL keep
glibc's own signal stacks (std_options.signal_stack_size = null) so its threads start.
The lane backend keeps prompts, the MTP head and device draws, and under tensor parallelism sends every step
to the other ranks (prefill, verify, each commit's keep, release, stop); worker.zig runs them on ranks above 0
over the new engine. The ranks' vocabulary slices are joined on the device for the drawer. The MTP head and
kept prompts stay off under tensor parallelism for now.
A round's window forward and its logits projection are captured per shape (each window's stream and rows)
on the second sighting and replayed after: the rounds' scratch repeats its addresses, the entry keeps the
arena's mark and the windows' snapshots for the commit, and a stream's graphs go with its caches. The cache
write reads its slots on the device (kv_write_at, the same copy), so a replay writes the round's positions.
A failed capture keeps that shape eager; TF_HIP_GRAPHS=0 turns graphs off. Qwen3.5-0.8B on a W7800: the same
replies with and without graphs; the host's launch time per round falls from 1.7 ms to 0.06 ms.
A round's forward and projection replay from its shape's graph; the device draw runs after the graph on the
round's scratch. Stream caches carry the serial graphs key them by and a freed stream drops its graphs.
Graphs stay off under tensor parallelism (its collectives run between the layers).
The prefill conv takes a thread a (row, channel) with each output's sum unchanged, and writes the new window
beside the old (every row reads the old) before it replaces the cache's. The MoE route places a round's
pairs in one pass with a cursor an expert instead of counting each pair's earlier ones.
…en rows at a time

The route counts each segment of pairs by expert in shared memory, takes an expert's prefix over the segments
and places every segment's pairs in order: the same stable plan in a few hundred steps a thread. The router's
blocks stage their 8 token rows in shared memory and each wave reads its expert row once for them; every
logit keeps moe_router_kernel's sum. Qwen3.6-35B-A3B, 2,048-token prompt on a V620: 1,445 tok/s (from 1,352),
every layer of a 600-token prompt equal to the Python engine's.
…'s bits

prefill.hip's tile takes 64 query rows a block (fa_prefill takes 16) over the same 64-key tiles: a score keeps
fa_pair's four accumulators over the head dimension, staged 64 dimensions at a time, the tile's online softmax
stays one row a thread, and each output's key-ordered sum and rescale are fa_prefill's. A 16-bit cache with
whole 64-wide head chunks takes it (TF_FA_WIDE=0 keeps the 16-row tile). Qwen3.6-35B-A3B on a V620, 32,768
prompt tokens: 41.4 s from 49.6; every layer of a 600-token prompt still equal to the Python engine's.
…ax in registers

q is rounded to fp16 once and stays in swizzled LDS, a tile's scores never leave registers (a row's keys on 16
lanes, max and sum by shuffles), and the probabilities reach PV as fp16 key pairs. Not bit-equal to fa_prefill:
accuracy is measured against the high-precision reference.
A prompt's router no longer reads each expert row per 8 rows; short rows (a head's q or k) no longer take a
256-thread block each. The norm keeps rms_kernel's sums, so its bits are the same.
affine_gemm_block replaces affine_dot2_block on both activation types, dense and routed. Its outputs are the same bits;
the work around the dot2s is spread differently:

- x is shared inside a quad of lanes through DPP (the FP16 dot2c takes it in the instruction on RDNA2, a v_mov a row
  on gfx11), so a lane reads a third of the LDS bytes of the 8 x 8 tile;
- the row sums of x come from their own threads, and a group's bias term runs a stage after its scale term;
- the code words and the rows of x of two stages are fetched at once, scales and biases with them, and the codes become
  pairs with a few bit operations;
- a wave whose rows are all past the end (a routed item's few rows) does no dot2.

TF_AFFINE_GEMM=old keeps affine_dot2_block in the Zig launches and the library.

tf-hip-test gemm [reps] [filter] times both tiles on the projection shapes of Qwen3.6-35B-A3B, Qwen3.5-9B and
Qwen3.8-27B (routed experts included) and checks each against a float64 dequant(W) . x on sampled outputs; gemm sweep
runs ragged shapes of every width and group, relaunching the new tile to catch a race.

V620, best of 5, TFLOPS old -> new: 35B qkv m2048 18.1 -> 26.9, o m8192 17.7 -> 27.0, routed gate_up (2k prompt) 9.1 ->
16.6, routed down 9.2 -> 15.9, 9B gate_up m2048 16.8 -> 22.1, 27B down m2048 16.6 -> 22.5. Prefill of the 35B: 2048
tokens 1404 -> 1890 tok/s, 8192 tokens 1161 -> 1480; of the 9B: 857 -> 1205 and 779 -> 1055.
ashhart pushed a commit that referenced this pull request Oct 16, 2026
… and model checks

The HIP branch's engine (#490) on #463's runtime and the kernel launches of #617 and #621: the lane rounds with
MTP drafts, the paged attention cache and the radix prompt cache (#625), chunked prompts (#623), CUDA-graph
rounds, the memory plan, and tensor parallelism over RCCL (comm/: the library, the rank link, the group). The
native server takes it with `zig build native-hip -Dhip-arch=...` and the serve flags it lists (#624);
`tf-qwen35-test` runs the model checks on a GPU. `zig build test` compiles both against stub kernels.

The engine reads the policy (`--policy`, TF_POLICY) once and hands the kernels `Launcher.Choices`; a GPU other
than the one the kernels were built for is refused with both names. round_shape.zig stays in the family: its
only reader is the plan.
ashhart pushed a commit that referenced this pull request Oct 16, 2026
… and model checks

The HIP branch's engine (#490) on #463's runtime and the kernel launches of #617 and #621: the lane rounds with
MTP drafts, the paged attention cache and the radix prompt cache (#625), chunked prompts (#623), CUDA-graph
rounds, the memory plan, and tensor parallelism over RCCL (comm/: the library, the rank link, the group). The
native server takes it with `zig build native-hip -Dhip-arch=...` and the serve flags it lists (#624);
`tf-qwen35-test` runs the model checks on a GPU. `zig build test` compiles both against stub kernels.

The engine reads the policy (`--policy`, TF_POLICY) once and hands the kernels `Launcher.Choices`; a GPU other
than the one the kernels were built for is refused with both names. round_shape.zig stays in the family: its
only reader is the plan.
BlivionIaG added a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… and model checks

The HIP branch's engine (ashhart#490) on ashhart#463's runtime and the kernel launches of ashhart#617 and ashhart#621: the lane rounds with
MTP drafts, the paged attention cache and the radix prompt cache (ashhart#625), chunked prompts (ashhart#623), CUDA-graph
rounds, the memory plan, and tensor parallelism over RCCL (comm/: the library, the rank link, the group). The
native server takes it with `zig build native-hip -Dhip-arch=...` and the serve flags it lists (ashhart#624);
`tf-qwen35-test` runs the model checks on a GPU. `zig build test` compiles both against stub kernels.

The engine reads the policy (`--policy`, TF_POLICY) once and hands the kernels `Launcher.Choices`; a GPU other
than the one the kernels were built for is refused with both names. round_shape.zig stays in the family: its
only reader is the plan.
BlivionIaG added a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… and model checks

The HIP branch's engine (ashhart#490) on ashhart#463's runtime and the kernel launches of ashhart#617 and ashhart#621: the lane rounds with
MTP drafts, the paged attention cache and the radix prompt cache (ashhart#625), chunked prompts (ashhart#623), CUDA-graph
rounds, the memory plan, and tensor parallelism over RCCL (comm/: the library, the rank link, the group). The
native server takes it with `zig build native-hip -Dhip-arch=...` and the serve flags it lists (ashhart#624);
`tf-qwen35-test` runs the model checks on a GPU. `zig build test` compiles both against stub kernels.

The engine reads the policy (`--policy`, TF_POLICY) once and hands the kernels `Launcher.Choices`; a GPU other
than the one the kernels were built for is refused with both names. round_shape.zig stays in the family: its
only reader is the plan.
This was referenced Oct 20, 2026
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