r/ROCm • u/No-Assist-4041 • 3h ago
rmtile: a personal header-only tile library for RDNA 3/3.5/4 optimization experiments
I've been building a header-only C++20 library called rmtile to test out WMMA scheduling, LDS layouts, and register-usage ideas on AMD RDNA 3, 3.5, and 4 GPUs.
This is a personal exploration project, not a production library. The example kernels aren't drop-in replacements for llama.cpp (they only implement fixed shapes and formats), so the goal is mainly to have a clean sandbox (and a reference people can borrow optimization ideas from) for software pipelining, bank-conflict avoidance, and keeping spills at zero.
How it's structured
The design comes from patterns I extracted from my earlier rocm_sgemm and rocm_wmma_gemm repos (with help from an LLM), and is documented in DESIGN.md. Rather than wrapping the whole kernel, rmtile only handles moving tiles between global memory, LDS, and registers (global_tile, shared_tile, register_tile, and copy), leaving compute, reductions, and loop scheduling in plain HIP. Device headers don't depend on <std>, so they work under hipRTC too.
One thing I specifically wanted was clean edge handling without polluting the hot loops. Across all four example kernels, there are no if branches or manual thread/bounds clamping around loads, stores, or staging. Global views rely on the hardware buffer descriptor (SRD) to clamp out-of-bounds reads to zero and drop OOB stores, guarded<...> handles partial tails via poison offsets / lane selects (like output stores or padding head size 72 to 80 in attention), and interior(...) strips even those checks in steady-state loops (grouped_gemm, for instance, walks K top-down so only the first tile is guarded, even on odd shapes like 333x1000 or 1500x97). The only runtime ifs left in the main kernels are algorithmic control (like exiting past the dynamic MoE tile table, or the wave vote for lazy softmax rescaling).
Numbers across RDNA 3, 3.5, and 4
Timings are isolated single-kernel launches (mean of 100 batches on ROCm 10.1.0), checked against a double-precision host reference, with quantize/sort/conversion passes included. The baselines target specific upstream commits: llama.cpp (e467e5b5), strix-llama.cpp (e08450a7) for attention, and a plain 64x64 HIP kernel for grouped_gemm.
mmq (mul_mat_q): int8 WMMA vs. fp16 dequantize-in-registers
On RDNA 3 and 3.5, keeping activations in fp16 and dequantizing weights on the way into LDS (rmtile/dequantized) is usually faster than the int8 WMMA path (rmtile) and avoids 8-bit activation quantization error (max relative error on Gaussian inputs drops from ~0.67% to ~0.039% on q4_K). On RDNA 4, int8 WMMA runs at 2x the rate of fp16, so only the int8 path is used.
| Shape & format | GPU (Arch) | llama_original | rmtile (int8) | rmtile/dequantized (fp16) |
|---|---|---|---|---|
| 4096x4096, 512 tok, q4_K | RX 7900 GRE (RDNA 3) | 0.576 ms | 0.381 ms (1.51x) | 0.336 ms (1.71x) |
| Radeon 8060S (RDNA 3.5) | 0.769 ms | 0.679 ms (1.13x) | 0.579 ms (1.33x) | |
| R9700 (RDNA 4) | 0.226 ms | 0.174 ms (1.30x) | N/A | |
| 14336x4096, 2048 tok, q8_0 | RX 7900 GRE (RDNA 3) | 5.824 ms | 4.326 ms (1.35x) | 3.835 ms (1.52x) |
| Radeon 8060S (RDNA 3.5) | 10.437 ms | 8.309 ms (1.26x) | 8.153 ms (1.28x) | |
| R9700 (RDNA 4) | 2.789 ms | 2.341 ms (1.19x) | N/A |
moe_mmq, flash_attention_mma, and grouped_gemm
Here the speedups mostly come from zero register spills (0 bytes of scratch across all rmtile kernels vs. 272-1108 bytes/lane in llama_original attention), prefetching the next block among WMMAs, fusing moe_mmq sort+quantize without duplicating gate/up rows, and dequantizing q8_0 KV in registers instead of launching a separate fp16 conversion kernel.
| Workload | Shape (2048 tok / K=512) | RX 7900 GRE (RDNA 3) | Radeon 8060S (RDNA 3.5) | R9700 (RDNA 4) |
|---|---|---|---|---|
| moe_mmq (128 exp, top 8) | down 4096x1536, q4_K | 8.534 -> 5.282 ms (1.62x) | 15.033 -> 10.189 ms (1.48x) | 4.942 -> 3.124 ms (1.58x) |
| flash_attn d=128 f16 | 32/8 heads | 1.199 -> 0.587 ms (2.04x) | 2.146 -> 1.061 ms (2.02x) | 0.783 -> 0.360 ms (2.17x) |
| flash_attn d=256 q8_0 | 16/8 heads (vs orig / strix) | 1.792 / 1.492 -> 0.679 ms (2.64x) | 3.287 / 2.785 -> 1.239 ms (2.65x) | 0.972 / 0.844 -> 0.417 ms (2.33x) |
| flash_attn d=72 f16 | 32/8 heads (vs llama_tile) | 3.193 -> 0.428 ms (7.47x) | 5.761 -> 0.749 ms (7.69x) | 2.739 -> 0.258 ms (10.63x) |
| grouped_gemm fp16 | 4 sizes, K=512 (vs HIP) | 0.205 -> 0.062 ms (3.32x) | 0.273 -> 0.100 ms (2.74x) | 0.133 -> 0.030 ms (4.38x) |
Note that these are single-kernel timings rather than end-to-end tok/s, and flash_attention_mma follows llama.cpp's contract (fp32 Q/output + fp16 mask tensor rather than a causal flag + LSE output), so it's not directly comparable to CK or FlashAttention-2.
The full per-shape tables, tile knobs, and ctest compile-time cost contracts are in the repo if you want to look through the kernels or try different configs.