libhalo: windowed attention at head_dim 128 with 4 queries per kv head, and the row-form names of rope and rmsnorm

What LFM3 needs on gfx1151 beyond the rows libhalo has. The window kernel is
instantiated at (128, 4) as in libr4d: the decode leg keeps q_len <= 16 and
every longer q_len, windowed or not, lands on the new pair. rope_rows and
rmsnorm_rows are registered under their row-form names and launched as rope
and rmsnorm, as libavx and libref register them, so a plugin written for
libr4d's row forms resolves on this part instead of falling to the host.

Emulator cases as libr4d's (split verify, ragged window, fp8, non-causal with
a missing block, WMMA prefill with and without the window) and selftest
launches.
This commit is contained in:
DokterSmol authored and StillDeadcode committed 2026-11-17 18:12:32 +01:00
1 parent 21c4544b61
commit 1f00224501
7 files changed
+74 -8

No files matched your search

+35 -2
View File
@@ -2887,6 +2887,9 @@ static const RadOpSchema kSchemas[] = {
SCHEMA("rmsnorm", pRmsNorm, oRmsNorm,
"y = x * rsqrt(mean(x^2) + eps) * (w + wadd). `wadd` is GemmaRMSNorm's `1 +`, added in "
"f32 after the load because forming it in bf16 first costs 0.3% per channel."),
SCHEMA("rmsnorm_rows", pRmsNorm, oRmsNorm,
"the same norm under its row-form name: a plugin written for libr4d's short-row launch "
"resolves here, and libhalo launches it as `rmsnorm` (byte-identical)."),
SCHEMA("rmsnorm_p", pLayernorm, oUnary,
"y = x * rsqrt(mean(x^2) + eps); an rmsnorm with NO gain tensor at all. Gemma's "
"attention normalises the value with a bare RMS (a weightless ggml_rms_norm) and the "
@@ -3009,6 +3012,9 @@ static const RadOpSchema kSchemas[] = {
"that follow are untouched. The multi-component modes (`mrope`, `imrope`, `axial`) "
"take [C, M] component-major positions and `sections`; with [M] positions every "
"component is that one, so mrope and imrope over a text batch are neox exactly."),
SCHEMA("rope_rows", pRope, oRope,
"the same rotation under its row-form name: a plugin written for libr4d's one-angle-a-"
"(token, pair) launch resolves here, and libhalo launches it as `rope` (byte-identical)."),
SCHEMA("layernorm", pLayernorm, oLayernorm,
"y = (x - mean) * rsqrt(var + eps) * w + b, biased variance, two passes. `b` optional."),
SCHEMA("grid_embed", pGridEmbed, oGridEmbed,
@@ -3997,7 +4003,9 @@ static const RadConstraint cAttnPrefillH64G4Bf16[] = {
/* The windowed kernels (halo_attn_paged_window.hip): head_dim 128, 256 and 512, 1/2/4/8 queries per
* KV head, any window, causal or not. Gemma 4's sliding layers are h256/gqa2 with a 1024-token
* window, its full layers h512 at gqa 8 (TP1/TP2), 4 (TP4) or 2 (TP8); h128/gqa1 is Unlimited-OCR's
* MHA decoder. One kernel serves every q_len (splitting the key range when there are too few tiles
* MHA decoder; h128/gqa4 is LFM3 (full layers, and sliding ones with a 256-token window), whose
* decode band (q_len <= 16) stays on the h128/gqa4 decode leg, which ranks above these rows. One
* kernel serves every q_len (splitting the key range when there are too few tiles
* to fill the card), so `q_len` cuts no band. `causal` and `window` are runtime fields; block_size
* is derived: 16.
*
@@ -4010,6 +4018,7 @@ static const RadConstraint cAttnPrefillH64G4Bf16[] = {
C_GE("window", WMIN), C_IN("q_dtype", "bf16"), C_IN("kv_dtype", KV), \
};
HALO_WIN_C(cWinH128G1Bf16, 128, 1, "bf16", 0) HALO_WIN_C(cWinH128G1Fp8, 128, 1, "fp8_e4m3", 0)
HALO_WIN_C(cWinH128G4Bf16, 128, 4, "bf16", 0) HALO_WIN_C(cWinH128G4Fp8, 128, 4, "fp8_e4m3", 0)
HALO_WIN_C(cWinH256G1Bf16, 256, 1, "bf16", 0) HALO_WIN_C(cWinH256G1Fp8, 256, 1, "fp8_e4m3", 0)
HALO_WIN_C(cWinH256G2Bf16, 256, 2, "bf16", 0) HALO_WIN_C(cWinH256G2Fp8, 256, 2, "fp8_e4m3", 0)
HALO_WIN_C(cWinH256G4Bf16, 256, 4, "bf16", 1) HALO_WIN_C(cWinH256G4Fp8, 256, 4, "fp8_e4m3", 1)
@@ -5768,6 +5777,18 @@ static const RadKernelInfo kKernels[] = {
nullptr, nullptr, halo_rad_rmsnorm, nullptr, nullptr, nullptr,
halo_shape_rmsnorm },
/* rmsnorm under its row-form name (libr4d's rmsnorm_rows): the same launch, so a per-head q/k
* norm declared for the rows form has a device row on this part too. */
{ "rmsnorm_rows_bf16", "rmsnorm_rows", "norm",
"root-mean-square norm with a gain, two passes over the row, under its row-form name",
"any n; the sum of squares accumulates in f32 and only the store narrows; byte-identical to "
"rmsnorm",
"bf16 y, x bf16 or (with a gain) f32, bf16 or f32 gain, fp32 reduction",
RAD_DOMAIN_DEVICE, HALO_PRIO_ONLY,
ROW(cBf16Row), NOTUNE,
nullptr, nullptr, halo_rad_rmsnorm, nullptr, nullptr, nullptr,
halo_shape_rmsnorm },
{ "rmsnorm_p_bf16", "rmsnorm_p", "norm",
"weightless root-mean-square norm (no gain), two passes over the row",
"any n; the sum of squares accumulates in f32 and only the store narrows",
@@ -6026,6 +6047,18 @@ static const RadKernelInfo kKernels[] = {
nullptr, nullptr, halo_rad_rope, nullptr, nullptr, nullptr,
halo_shape_rope },
/* rope under its row-form name (libr4d's rope_rows): the same launch. */
{ "rope_rows_bf16", "rope_rows", "attn",
"rotary embedding in place, one thread a rotated pair, under its row-form name",
"any head_dim; rotary_dim even and no wider than head_dim -- the frequency exponent divides by "
"rotary_dim, so a partial rotary is a complete rotation of its own width; neox, mrope (which is "
"neox over one position component) and gptj pairings",
"bf16 qkv, i32 positions, fp32 angles through the accurate sincosf",
RAD_DOMAIN_DEVICE, HALO_PRIO_ONLY,
ROW(cRopeRow), NOTUNE,
nullptr, nullptr, halo_rad_rope, nullptr, nullptr, nullptr,
halo_shape_rope },
{ "kv_store_bf16", "kv_store", "attn",
"scatter k and v into the paged cache, one workgroup a token",
"cache exactly [n_blocks, kv_heads, block_size, 2*head_dim]; a negative or out-of-range slot "
@@ -6775,7 +6808,7 @@ static const RadKernelInfo kKernels[] = {
#define HALO_WIN_PAIR(HD, GQ) \
HALO_WIN_ROW(HD, GQ, bf16kv, bf16, cWinH##HD##G##GQ##Bf16, "bf16") \
HALO_WIN_ROW(HD, GQ, fp8kv, fp8, cWinH##HD##G##GQ##Fp8, "fp8-e4m3 (per-(seq,head) descales)")
HALO_WIN_PAIR(128, 1)
HALO_WIN_PAIR(128, 1) HALO_WIN_PAIR(128, 4)
HALO_WIN_PAIR(256, 1) HALO_WIN_PAIR(256, 2) HALO_WIN_PAIR(256, 4) HALO_WIN_PAIR(256, 8)
HALO_WIN_PAIR(512, 1) HALO_WIN_PAIR(512, 2) HALO_WIN_PAIR(512, 4) HALO_WIN_PAIR(512, 8)
#undef HALO_WIN_PAIR