libr4d: the gated prefill fold, the short-prompt split row, and the rows forms of rmsnorm and rope

Four rows of our own, each declared only by a model that asks for it, so every
other caller keeps the row and the bits it had:

  gemm_nt_q_gated_wide   gemm_nt_q_gated at every M: the narrow tile to 64 rows
                         and, past them, the gated prefill tile (128 gate and
                         128 up rows against 64 activation rows a block), whose
                         accumulators are the unfused prefill GEMM's. The
                         narrow row keeps its M <= 64 ceiling and kernel.
  gemm_nt_q_short        the narrow fp8 row with the short-prompt split rule:
                         N <= 512, K <= 5120 and 41..64 rows run unsplit, where
                         the split's per-slice floors dominate. Its scratch hook
                         makes the same choice, so the arena matches the launch.
  rmsnorm_rows           the same norm; at n <= 256 and 1024 or more rows a wave
                         a row, its sums in rmsnorm_kernel's wave order.
  rope_rows              the same rotation; at 64 or more rows and 4 or more
                         heads one angle a (token, pair), every head turned with
                         it, byte-identical to rope.

gate_quant_fp8 gains act none: the plain product (`mul` then the quantiser) in
one pass, rounded once as mul_bf16 rounds it. Its `mode` stays 1 by default, so
the sigmoid producer every other model issues is unchanged.

r4d_selftest: the act-none chain against mul + quant_act_fp8 at M 1, 8 and 65 with
strided sources and without out_bf16; gemm_nt_q_gated_wide against the tiled
GEMM + gated_quant_fp8 at 65 and 129 rows; and the RDNA4 device phase accepts
gfx1200 beside gfx1201, where the card this was measured on lives.

docs/OPS.md carries the four rows ops_doc checks.

Co-Authored-By: Pi <pi@localhost>
This commit is contained in:
Code000andPi authored and StillDeadcode committed 2026-10-29 16:23:28 +01:00
1 parent ed1e5caf9d
commit b8cd1c950a
9 files changed
+750 -37

No files matched your search

+19 -9
View File
@@ -245,6 +245,9 @@ __global__ __launch_bounds__(1024) void gated_quant_fp8_kernel(
// then contiguous, the gate a fixed stride apart, and the scale plane [T * n_head, head_dim / 128]
// is byte-identical to the [T, n_head * head_dim / 128] the out projection reads. Replaces three
// launches (sigmoid, multiply, quantise) per attention layer.
//
// ACT 2 is the product with no activation: the unfused pair is `mul` then the quantiser, so the
// gate is not rounded on its own and the product is rounded once, as mul_bf16 does.
template <int ACT, bool I8 = false>
__global__ __launch_bounds__(1024) void gate_quant_fp8_kernel(
const uint16_t* __restrict__ g, const uint16_t* __restrict__ x, uint16_t* __restrict__ y,
@@ -270,13 +273,18 @@ __global__ __launch_bounds__(1024) void gate_quant_fp8_kernel(
}
#pragma unroll
for (int j = 0; j < 4; ++j) {
const float gv = bf2f(gb[j]);
// The activation is rounded to bf16 before the multiply, unlike gated_quant_fp8:
// the unfused reference here is `sigmoid` then `mul` with a bf16 buffer between
// them (silu_mul_bf16 keeps f32). Skipping the round trip changes the output.
const uint16_t ab =
f2bf(ACT == 0 ? gv / (1.0f + expf(-gv)) : 1.0f / (1.0f + expf(-gv)));
const uint16_t h = f2bf(bf2f(ab) * bf2f(xb[j]));
uint16_t h;
if (ACT == 2) {
h = f2bf(bf2f(gb[j]) * bf2f(xb[j]));
} else {
const float gv = bf2f(gb[j]);
// The activation is rounded to bf16 before the multiply, unlike gated_quant_fp8:
// the unfused reference here is `sigmoid` then `mul` with a bf16 buffer between
// them (silu_mul_bf16 keeps f32). Skipping the round trip changes the output.
const uint16_t ab =
f2bf(ACT == 0 ? gv / (1.0f + expf(-gv)) : 1.0f / (1.0f + expf(-gv)));
h = f2bf(bf2f(ab) * bf2f(xb[j]));
}
if (yr) yr[c0 + j] = h;
v[j] = bf2f(h);
}
@@ -850,7 +858,7 @@ static int gate_quant_fp8_emit(long g, long x, long y, long q, long s, int M, in
if (!g || !x || !q || !s) return -1;
if (M <= 0 || n <= 0) return -2;
if (n % 4) return -3;
if (mode != 0 && mode != 1) return -5;
if (mode < 0 || mode > 2) return -5;
if (!g_ld) g_ld = n;
if (!x_ld) x_ld = n;
if (!y_ld) y_ld = n;
@@ -867,7 +875,8 @@ static int gate_quant_fp8_emit(long g, long x, long y, long q, long s, int M, in
(uint32_t)s_ld)
#define R4D_GTQ(a) do { if (i8) R4D_GTQ_I(a, true); else R4D_GTQ_I(a, false); } while (0)
if (mode == 0) R4D_GTQ(0);
else R4D_GTQ(1);
else if (mode == 1) R4D_GTQ(1);
else R4D_GTQ(2);
#undef R4D_GTQ
#undef R4D_GTQ_I
if (emit_rc != RAD_OK) return -4;
@@ -1058,6 +1067,7 @@ static int act_mode(const RadArgs* a, int dflt) {
if (!act) return (int)r4d_geti_or(a, "mode", dflt);
if (!std::strcmp(act, "silu")) return 0;
if (!std::strcmp(act, "sigmoid")) return 1;
if (!std::strcmp(act, "none")) return 2;
return -1;
}