unlimitedocr: the decoder plugin, the fused router ops and an h128 MHA attention leg

The text side of Unlimited-OCR: a 12-layer, 1280-wide decoder, 10 query heads over 10 KV
heads at head_dim 128, a dense layer 0 and 64 routed experts (top-6, softmax, no
renormalisation) plus one ungated shared arm in layers 1-11. arch/unlimitedocr_bf16/
carries its own plain-Llama bf16 attention, the shared MoE block in its bf16
external-input mode, and the tied LM head; data/recipes/unlimited-ocr-bf16.recipe is the
bf16 passthrough convert. core/format/checkpoint.cpp learns n_routed_experts, so n_expert
is read from the checkpoint's own key.

The router is issued as one op rather than a selection and a sort: libref gains
router_topk_scatter and router_gemm_topk_scatter (the second with the shared expert's
one-column gate at N = 1), libavx composes both from its router/gemm/scatter kernels, and
both get sweep geometries and the gate-absent/gate-present tests.

libr4d had no head_dim 128 leg at ratio 1, so every attn_paged band of this geometry
resolved to no kernel and the server refused to start on device.
r4d_attn_paged_h128_gqa1.hip instantiates the generic body at GQ 1 for both legs; the
rows and the selftest cases come with it.

Device numbers, one R9700 (gfx1201, HIP 7.16), bf16 weights and KV, no reference-kernel
fallback: a 523-token prompt prefills at 35.0-35.8k tok/s (15 ms) and a 4043-token prompt
at 54.2k tok/s (75 ms); decode is 351-354 tok/s (2.82-2.85 ms/token) at short context and
315 tok/s at 4k. r4d_selftest is 0 failures; the host suite is 86/87, the miss being
mm_test's lossless-webp decode, which fails on this box in code this change does not touch.
This commit is contained in:
Juup committed 2026-10-20 07:03:49 +00:00
1 parent 685ec5bd85
commit 543cba7416
20 files changed
+1179 -5

No files matched your search

+3
View File
@@ -46,6 +46,9 @@ set(R4D_UNITS
"r4d_attn_paged_h256_gqa2|"
# Gemma 3 sliding window: head_dim 128, 2 queries per KV head, windowed decode and prefill.
"r4d_attn_paged_h128_gqa2w|"
# One query head per KV head at 128 wide: plain MHA, the Unlimited-OCR decoder (10 over 10).
# Both legs, prefill and decode; see the file's header for why it is not a drafter-shaped unit.
"r4d_attn_paged_h128_gqa1|"
# Eight queries per KV head, one body instantiated at head_dim 256 (the routed 35B-A3B) and 128
# (MiniCPM5-2B).
"r4d_attn_paged_gqa8|"
+20
View File
@@ -0,0 +1,20 @@
/* r4d_attn_paged_h128_gqa1.hip -- head_dim 128 with ONE query head per KV head: plain MHA.
*
* Everything else in this library pages attention through a grouped ratio (4, 6, 8 or 12 queries
* per KV head). The Unlimited-OCR decoder has 10 query heads over 10 KV heads at 128 wide, so the
* ratio is 1 and none of the existing legs matches it; the engine reports every band of that
* geometry as having no kernel rather than serving it on the host.
*
* The device code is the generic body (r4d_attn_paged_gqa8.hip, which is parameterised by
* R4D_ATTN_GQA rather than fixed at 8) instantiated at ratio 1: the same templated prefill and
* split-KV decode kernels, with GQA=1 substituted. No new kernel, no new masking path.
*
* This unit ships BOTH legs, unlike the h128/gqa4 drafter unit: a decoder runs prefill as well as
* decode, and the caller-side fallback the drafter relies on is an order of magnitude slower.
* The prefill option set is the h128 set (G8_P_OPT_128), which is the one compiled and measured
* for this head width. */
#define R4D_ATTN_GQA 1
#define R4D_ATTN_H128 1
#define R4D_ATTN_H256 0
#include "r4d_attn_paged_gqa8.hip"
+7
View File
@@ -261,6 +261,13 @@ int r4d_rad_attn_prefill_h128_gqa8_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_decode_h128_gqa8_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_decode_h128_gqa8_bf16 (const RadArgs*, RadStream);
int64_t r4d_rad_attn_decode_h128_gqa8_scratch(const RadArgs*);
/* ONE query a KV head, head_dim 128 -- plain MHA, the Unlimited-OCR decoder (10 over 10). Both
* legs from the generic body instantiated at ratio 1 (r4d_attn_paged_h128_gqa1.hip). */
int r4d_rad_attn_prefill_h128_gqa1_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_prefill_h128_gqa1_bf16(const RadArgs*, RadStream);
int r4d_rad_attn_decode_h128_gqa1_fp8 (const RadArgs*, RadStream);
int r4d_rad_attn_decode_h128_gqa1_bf16 (const RadArgs*, RadStream);
int64_t r4d_rad_attn_decode_h128_gqa1_scratch(const RadArgs*);
/* Four queries per KV head, head_dim 256 only (Qwen3.5-0.8B); same body, another instantiation
* (r4d_attn_paged_h256_gqa4.hip). */
int r4d_rad_attn_prefill_h256_gqa4_fp8 (const RadArgs*, RadStream);
+60
View File
@@ -3731,6 +3731,28 @@ static const RadConstraint cAttnDecodeH128G8Bf16[] = {
C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 8), C_EQ("block_size", 16),
C_GE("causal", 0), C_LE("q_len", 8), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
};
/* head_dim 128 at ratio ONE: plain MHA (the Unlimited-OCR decoder, 10 query heads over 10), from
* r4d_attn_paged_h128_gqa1.hip. The decode leg caps at 64 query rows, which at this ratio is a
* plain q_len <= 64; the prefill leg has no query-row cap. */
static const RadConstraint cAttnPrefillH128G1Fp8[] = {
C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 1), C_EQ("block_size", 16),
C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"),
};
static const RadConstraint cAttnPrefillH128G1Bf16[] = {
C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 1), C_EQ("block_size", 16),
C_EQ("causal", 1), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
};
static const RadConstraint cAttnDecodeH128G1Fp8[] = {
C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 1), C_EQ("block_size", 16),
C_GE("causal", 0), C_LE("q_len", 64), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "fp8_e4m3"),
};
static const RadConstraint cAttnDecodeH128G1Bf16[] = {
C_EQ("window", 0), C_EQ("head_dim", 128), C_EQ("gqa", 1), C_EQ("block_size", 16),
C_GE("causal", 0), C_LE("q_len", 64), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", "bf16"),
};/* KEY RENAME. libr4d writes C_IN("q_dtype","bf16") and C_IN("kv_dtype","bf16"); docs/OPS.md's
* `attn_dense` has one `dtype` because nothing about a dense bf16 tower is mixed-precision. The
* predicate -- "bf16, and nothing else" -- is unchanged. */
/* Two queries per KV head at head_dim 128 (Qwen3-1.7B, Qwen3-ASR's decoder): the gqa8 pair's
* constraints with the ratio and the decode verify width changed together. A decode workgroup
* holds 64 (query, head) rows, so q_len <= 32 at gqa 2. */
@@ -5775,6 +5797,44 @@ static const RadKernelInfo kKernels[] = {
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_prefill_h128_gqa1_fp8kv", "attn_paged", "attn",
"paged causal attention, query-tiled (prefill / chunked prefill)",
"head_dim 128, 1 query per kv head (plain MHA), paged block 16, varlen",
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH128G1Fp8), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h128_gqa1_fp8, nullptr, nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_prefill_h128_gqa1_bf16kv", "attn_paged", "attn",
"paged causal attention, query-tiled (prefill / chunked prefill)",
"head_dim 128, 1 query per kv head (plain MHA), paged block 16, varlen",
"bf16 query, bf16 kv cache, bf16 out",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_PREFILL,
ROW(cAttnPrefillH128G1Bf16), NOTUNE,
nullptr, nullptr, r4d_rad_attn_prefill_h128_gqa1_bf16, nullptr, nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_decode_h128_gqa1_fp8kv", "attn_paged", "attn",
"paged attention, causal or not, split-KV (decode / speculative verify)",
"head_dim 128, 1 query per kv head (plain MHA), paged block 16, q_len <= 64, no window",
"bf16 query, fp8-e4m3 kv cache with per-(seq,head) descales, bf16 out, f16 partials",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE,
ROW(cAttnDecodeH128G1Fp8), ROW(tAttnSplit), nullptr,
nullptr, nullptr, r4d_rad_attn_decode_h128_gqa1_fp8, r4d_rad_attn_decode_h128_gqa1_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_decode_h128_gqa1_bf16kv", "attn_paged", "attn",
"paged attention, causal or not, split-KV (decode / speculative verify)",
"head_dim 128, 1 query per kv head (plain MHA), paged block 16, q_len <= 64, no window",
"bf16 query, bf16 kv cache, bf16 out, f16 partials",
RAD_DOMAIN_DEVICE, R4D_PRIO_ATTN_DECODE,
ROW(cAttnDecodeH128G1Bf16), ROW(tAttnSplit), nullptr,
nullptr, nullptr, r4d_rad_attn_decode_h128_gqa1_bf16, r4d_rad_attn_decode_h128_gqa1_scratch,
nullptr, nullptr,
r4d_shape_attn_paged },
{ "attn_prefill_h256_gqa8_fp8kv", "attn_paged", "attn",
"paged causal attention, query-tiled (prefill / chunked prefill)",
"head_dim 256, 8 queries per kv head, paged block 16, varlen",
+4
View File
@@ -22968,6 +22968,10 @@ static void device_phase(const Plugin& pl) {
attn_case(pl, "attn_decode_h256_gqa6_bf16kv", 256, 6, 1, "bf16", 0);
attn_case(pl, "attn_decode_h128_gqa4_fp8kv", 128, 4, 1, "fp8_e4m3", 0);
attn_case(pl, "attn_decode_h128_gqa4_bf16kv", 128, 4, 1, "bf16", 0);
attn_case(pl, "attn_prefill_h128_gqa1_bf16kv", 128, 1, 32, "bf16", 1);
attn_case(pl, "attn_prefill_h128_gqa1_fp8kv", 128, 1, 32, "fp8_e4m3", 1);
attn_case(pl, "attn_decode_h128_gqa1_bf16kv", 128, 1, 1, "bf16", 0);
attn_case(pl, "attn_decode_h128_gqa1_fp8kv", 128, 1, 1, "fp8_e4m3", 0);
attn_case(pl, "attn_prefill_h64_gqa4_bf16kv", 64, 4, 32, "bf16", 1);
attn_case(pl, "attn_prefill_h64_gqa4_bf16kv", 64, 4, 1, "bf16", 1);
/* Gemma 4 dense sliding window, decode, with the model's window of 1024: exercises the