Skip to content

Latest commit

 

History

History
277 lines (208 loc) · 14.5 KB

HIP.md

File metadata and controls

277 lines (208 loc) · 14.5 KB

HIP (ROCm) backend

--backend hip runs RustingStrata on an AMD card through ROCm. Status: experimental. It was brought up on one card, an RX 9070 XT (gfx1201) with ROCm 7.2.3. Every number in this file was measured on that card.

The Vulkan backend on AMD cards is in AMD.md. The tested cards are in SUPPORTED_GPUS.md.

Requirements

  • A ROCm runtime with hiprtc: libamdhip64.so and libhiprtc.so.
  • An RDNA card (wave32, gfx10+).
  • For the matrix cores: a gfx11 or gfx12 target. Other targets run the slower CUDA-core paths.

You do not need hipcc, ROCm headers or a C++ build step. cargo build --release is the whole build. hiprtc compiles the kernels at run time. The result is cached in $XDG_CACHE_HOME/rusting_strata/<hash>.amdgcn.

RustingStrata looks for the two libraries on the loader path, then in /opt/rocm/lib, /usr/lib/x86_64-linux-gnu and /usr/local/lib. It also tries the names that end in .so.7 and .so.6.

Variable What it does
STRATA_HIP Path or name of libamdhip64.so. Replaces the search.
STRATA_HIPRTC Path or name of libhiprtc.so. Replaces the search.
STRATA_HIP_ARCH The card's gfx target, for example gfx1201. Default: read from the driver.

Run

rustingstrata serve --model model.gguf --gpu --backend hip
rustingstrata bench --model model.gguf --gpu --backend hip --pp 4096 --tg 48
# measure chunk, MTP drafts and the short-prompt threshold once; later runs read strata-<model>-hip.json
rustingstrata calibrate --model model.gguf --backend hip

HIP is never picked on its own. Without --backend hip, CUDA opens when it loads, else Vulkan. The first line of output names the card: hip: <card> (<gfx>, wave32, <N> B shared memory a block). Every other flag is in USAGE.md.

What works and what does not

HIP runs everything the Vulkan backend runs.

  • Models tried: Qwen3.6-35B-A3B, Ornith-1.5-35B-A3B, Qwen3.5-9B (dense), Qwen3.8-27B (dense), GigaChat 4 37B-A4B and Inkling 2 Mini.
  • serve, checked on Qwen3.5-9B: GET /v1/models, chat completions with and without stream: true, and the prompt cache across turns (prompt_tokens_details.cached_tokens on a follow-up request).

Not supported:

  • A model split over two cards. --device 0,1 is refused. Use CUDA or Vulkan for a split.
  • A card that reports a 64-wide wave. It is refused. Use --backend vulkan.
  • GigaChat 4 with the default expert budget on a 16 GB card. See Run GigaChat 4 on a 16 GB card.

Output compared with Vulkan

For a repeatable run use --fixed-experts --mtp 0. With these two flags:

  • Decode: greedy output is bit-identical to Vulkan on the first four models above.
  • Batched prefill: output can differ at a near-tie. Of long prose prompts (64-79 tokens), 4 of 5 agree token for token on the 35B, 2 of 2 on the dense 9B and 1 of 2 on the dense 27B.

With the calibrated MTP drafts on, the two backends verify different batch shapes, and 1 of 7 such probes flips too.

Inkling 2 output under --fixed-experts must stay bit-identical to Vulkan. For that reason the flag keeps the older kernels and the expert slabs. Without it, greedy text can differ at a near tie. See Inkling 2.

Speed

One process each. Other backends and cards are in BENCHMARKS.md.

Short prompts: Qwen3.6-35B-A3B UD-Q4_K_XL (first two rows) and Qwen3.8-27B UD-IQ3_S, --ctx 4096 --vram-margin 256. calibrate was run for each backend (chunk 4096; HIP picks --mtp 3, Vulkan --mtp 1).

HIP Vulkan (same machine, same model)
Prefill, pp 1024 / 4096, calibrated 1104 / 2993 tok/s 1097 / 2325 tok/s
Decode, tg 48, calibrated 209 / 219 tok/s 199 / 163 tok/s
Qwen3.8-27B dense, pp 1024, prefill / decode 784 / 39.1 tok/s 508 / 35.3 tok/s

The decode row is the decode that follows each prompt. The pp 1024 run is the first in the process, before expert placement settles, so it understates both backends. HIP measured 1577-2072 tok/s there when it was not the first run.

Long prompts: Qwen3.6-35B-A3B UD-Q4_K_XL, --mtp 0 --reps 1 --vram-margin 256, KV f16, --fixed-experts. A range is two runs.

pp HIP Vulkan HIP, everything on the CUDA cores (STRATA_HIP_WMMA=0)
8192 2647-2666 tok/s 2148-2152 tok/s 1303-1308 tok/s
32768 2026-2029 tok/s 1661 tok/s 776-794 tok/s
131072 872 tok/s 731 tok/s (not run)
decode at 8k / 32k / 128k 68-73 / 64 / 48 tok/s 71-72 / 68 / 44 tok/s 74 / 64-65 / -

A 128k prompt costs more VRAM in the KV cache than a 16 GB card has left for experts, so most experts stream from host memory. A shorter --ctx or --kv q8 gives expert slots back. Token-by-token prefill (--chunk 0) does not help.

Run GigaChat 4 on a 16 GB card

STRATA_DS_GPU_EXPERTS=4 rustingstrata serve --model GigaChat4-37B-A4B-Q4_K_M.gguf --backend hip --mtp 0

GigaChat 4 37B-A4B Q4_K_M (23 GB) does not fit in VRAM. The plain weights and part of the experts sit on the card. The rest of the experts are streamed to it or run on the CPU. The model is described in MODELS.md.

On HIP, cap the expert cache by hand with STRATA_DS_GPU_EXPERTS. The default budget (95% of free VRAM) fills the card: the cache takes about 1.6x the bytes it counts, and about 1.6 GiB of scratch and state is outside the budget.

STRATA_DS_GPU_EXPERTS Result
unset A 512-token prompt ran at 29.6 tok/s and decode at 15.5 (with the host-side fallback of the next table).
8 Reached 14.5 GiB of the 16 GiB within the first prompt chunk.
4 Peaks at 7.9 GiB including 1.1 GiB held by the desktop.

bench --backend hip --ctx 8192 --mtp 0 --pp 4096 --tg 4 --reps 1, STRATA_DS_GPU_EXPERTS=4 unless noted, one run each. The last row is the default. STRATA_MLA_SIMT=1 selects the one-wave-a-head kernel, with the same greedy output.

pp 4096 (tok/s) decode after it (tok/s)
MLA through the host-side fallback (mla_heads) 137.2 18.3
fused MLA, one-wave-a-head kernel (mla_qpack) 198.4, 200.5 18.9
fused MLA on the matrix cores (mla_attn_wmma) 338.7 19.9

Long prompts, same settings with --ctx set to fit. The rows "indexer grid fixed" and "idx_score_dec" are the default.

pp 32768 pp 65536
prefill, one-wave-a-head kernel 162.3 tok/s -
prefill, mla_attn_wmma 272.8 tok/s 203.0 tok/s
decode after the prompt, mla_attn_wmma 11.9 tok/s 9.2 tok/s
prefill, indexer grid fixed 319.4 tok/s 274.8 tok/s
decode, idx_score_dec 17.4 tok/s 16.1 tok/s
prefill, STRATA_IDX_WMMA=1 372.1 tok/s -
peak VRAM, including 1.1 GiB of desktop 8.4 GiB 9.2 GiB

STRATA_IDX_WMMA=1 computes the indexer scores of a prompt on the matrix cores, with f16 products. Where scores are close, it changes which keys a query selects. On a 3k-token prompt, 24% of the top-2048 key sets are identical to the default f32 kernel's. The mean overlap is 99.7% and the lowest 77%. Greedy output flips at the first near-tie, so the switch is off by default.

Fix STRATA_DS_CPU_SHARE for a repeatable run. By default the CPU's share of the missed experts adapts to timing, and the CPU computes them at a different precision. Two default runs can differ after a near-tie.

Faster prefill

  • STRATA_DS_PIN=1 page-locks the experts in the mapped model file at load (93 tensors, 21.2 GB, 2.0 s). The pages stay locked in RAM for the life of the process, so it is off by default. Greedy output does not change.
  • STRATA_DS_CHUNK sets the prompt chunk in tokens (default 1024). With STRATA_DS_PIN=1 and budget 4, more than 1536 does not help: 447.2 tok/s at 2048, 448.1 at 3072 and 411.8 at 4096.

At pp 4096, --ctx 5120, one run each:

prefill (tok/s) decode (tok/s) peak VRAM
STRATA_DS_GPU_EXPERTS=4, chunk 1024 335.9 19.3 7.9 GiB
budget 4, STRATA_DS_CHUNK=1536 365.8, 368.9 19.6, 19.4 8.1 GiB
budget 6, chunk 1024 338.0 20.7 11.3 GiB
budget 6, chunk 1536 383.5 20.8 11.5 GiB
budget 4, chunk 1536, STRATA_DS_PIN=1 452.0 24.7 8.1 GiB

Inkling 2

Inkling 2 Mini UD-Q4_K_XL, 9070 XT, X470 (PCIe 3.0) with dual-channel DDR4 (36 GB/s read). The model and its command are in MODELS.md.

Expert pool

By default every layer keeps its most used experts in VRAM, and the CPU computes the rest. The routing counts are saved as ~/.cache/rusting_strata/inkling2-profile-*.f32 when the model unloads. They place the experts of the next load, so the first run of a model is slower.

12 GB weight budget, desktop holds 3 GB pp 512 pp 4096 decode
slabs and hybrid layers 443 tok/s 438 tok/s 39.8 / 38.9 tok/s
pool, no profile yet 329 378 49.7 / 55.7
pool, profile of one earlier run 436 426 60.5 / 58.9

Decode is the decode after the prompt of each column. With the pool, greedy text is not bit-identical to Vulkan. --fixed-experts keeps the slabs, and its text is. STRATA_INK_SLABS=1 keeps the slabs and pools only the layers they leave out.

A slab is a layer with all 128 experts in VRAM. The first row above is this older layout. Its decode, with bench --pp 512 --tg 64, before and after changes to the expert split and the VRAM cache:

18 slabs (desktop holds 3.3 GB) 22 slabs (desktop idle)
before 34.5 tok/s 48.2 tok/s
after 39.5 tok/s 51.8 tok/s

Prompt chunk size

STRATA_DS_PF_TOKENS sets the rows of a prompt chunk (default 2048 on one card). The scratch costs about 0.55 GB of VRAM per 1024 rows. The chunk halves when it does not fit.

9.5 GB weight budget, desktop at 2.2 GB, bench --pp 512,4096 --tg 32:

rows pp 512 pp 4096 decode peak VRAM, card
256 265 tok/s 278 tok/s 49.5 tok/s 13.47 GB
2048 387 767 48.9 14.61 GB
4096 393 891 48.8 15.48 GB

The code clamps STRATA_DS_PF_TOKENS to 2048, so this build cannot repeat the 4096 row.

STRATA_INK_PIN=1 page-locks the pooled experts in the mapped file (15.8 GB in 1.8 s): pp 4096 855 tok/s, decode 51-53. That much RAM stays locked while the model is loaded, so it is off by default.

Faster kernels

By default HIP runs Inkling 2 on the faster attention and controller kernels. They sum in another order, so greedy text can differ from the older kernels and from Vulkan at a near tie. --fixed-experts keeps the older kernels. STRATA_DS_FAST_KERNELS=0 selects the older kernels, and 1 the faster ones under --fixed-experts too.

bench --pp 512,4096 --tg 64, alternating runs with a browser open on the desktop:

pp 4096 decode
older kernels 701, 908, 1001 tok/s 51-62 tok/s
faster kernels 1531, 1648, 1686 tok/s 52-66 tok/s

Errors

Message or symptom Fix
no HIP runtime: ... Install the ROCm runtime, or point STRATA_HIP at libamdhip64.so.
hiprtc is required to compile the kernels Install hiprtc, or point STRATA_HIPRTC at libhiprtc.so.
hipInit: ... Check that rocminfo lists the card and that your user can open /dev/kfd.
cannot read the device's gfx architecture; set STRATA_HIP_ARCH (e.g. gfx1201) Set STRATA_HIP_ARCH.
HIP backend needs wave32; <gfx> reports warp size 64 Use --backend vulkan.
HIP device N: M device(s) present Pass a --device index that exists.
the hip backend runs one device: --device 0,1 needs cuda or vulkan Pass one index, or use CUDA or Vulkan.
--kv q8/q4 does not combine with --llama-numerics (its attention reads f16 KV) Drop one of the two flags.
hiprtc (<gfx>): ... and a compiler log Open an issue with the full output.
A fault that names no kernel Rerun with STRATA_HIP_SYNC=1, or STRATA_HIP_TRACE=1 if the process dies.
Output you suspect the matrix cores of Compare with STRATA_HIP_WMMA=0.
VRAM fills with GigaChat 4 Set STRATA_DS_GPU_EXPERTS=4.

Switches

Environment variables for tests and tuning.

Variable What it does
STRATA_HIP_WMMA=0 CUDA cores instead of matrix cores in prefill. Prefill at pp 1024: 737 tok/s, against 1684.
STRATA_HIP_GRAPH=0 Runs the forward every time. By default it is captured (+5% decode).
STRATA_NO_GRAPH=1 Turns capture off for both GPU backends.
STRATA_HIP_SYNC=1 Waits after every launch, so a hardware exception names its kernel.
STRATA_HIP_TRACE=1 Prints every launch before it goes out (grid, block, shared memory).
STRATA_FIXED_EXPERTS Same as --fixed-experts.
STRATA_DS_GPU_EXPERTS=N Weight budget in GB (plain weights and expert cache).
STRATA_DS_CPU_SHARE=X Fixes the CPU's share of the experts that miss the cache.
STRATA_DS_PROF=1 Prints timers of the forward (route, experts, attention).
STRATA_DS_CHUNK=N GigaChat 4: prompt chunk in tokens.
STRATA_DS_PIN=1 GigaChat 4: page-locks the routed experts (about 21 GB of RAM).
STRATA_MLA_SIMT=1 GigaChat 4: MLA prompt attention on the one-wave-a-head kernel.
STRATA_IDX_WMMA=1 GigaChat 4: prompt indexer scores on the matrix cores.
STRATA_GIGA_CPU_ATTN=1 GigaChat 4: MLA attention on the CPU. Output matches the GPU until the first near-tie.
STRATA_DS_FAST_KERNELS Inkling 2: 0 older kernels, 1 faster kernels.
STRATA_CTRL_TILE=0, STRATA_DS_ATTN_OLD=1, STRATA_DS_ATTN_KP4=1 Inkling 2: per-kernel overrides. See USAGE.md.
STRATA_INK_SLABS=1 Inkling 2: slabs, and a pool only for the layers they leave out.
STRATA_INK_PIN=1 Inkling 2: page-locks the pooled experts.
STRATA_DS_PF_TOKENS=N Inkling 2: rows of a prompt chunk.
STRATA_RPW, STRATA_RPW_MOE, STRATA_RPW_DENSE GEMV rows per warp: all, MoE or dense.
STRATA_GEMV_BLOCK, STRATA_GEMV_BLOCK_MOE, STRATA_GEMV_BLOCK_DENSE Threads per block of the integer GEMV.
STRATA_GEMV_LOADW Load width of the GEMV kernels, 0 or 1.

Credits

The compatibility shim src/hip/compat.h follows the structure and several implementations of include/strata/hip_compat/intrinsics.hpp from the Strata project (MIT, (c) 2025 Niko1221, https://github.com/Niko1221/Strata). See THANKS.md.

Strata also has prompt attention on the matrix cores (src/kernels/cuda/qsa_prompt_attn.cu) and a chunked gated delta-rule scan (gdn_chunk.cu, gdn_tc.cuh, gdn_wmma12.inc). The matrix-core attention in this backend was written independently and matches that work in a different design. The chunked scan was measured and not ported.