diff --git a/libr4d/r4d_args.h b/libr4d/r4d_args.h index 31187233..7be88687 100644 --- a/libr4d/r4d_args.h +++ b/libr4d/r4d_args.h @@ -359,6 +359,9 @@ void r4d_cfg_split (const RadArgs*, struct R4dSplitCfg*); void r4d_cfg_ar (const RadArgs*, struct R4dArCfg*); void r4d_cfg_topk (const RadArgs*, struct R4dTopkCfg*); void r4d_cfg_mv (const RadArgs*, struct R4dMvCfg*); +/* rmsnorm_quant_i8's decode form: 1 the row's operands in registers before the reductions, 0 the + * pass-per-read kernel; the row's `reg` axis. */ +int r4d_cfg_rmsq8_reg(const RadArgs*); } /* extern "C" */ diff --git a/libr4d/r4d_rmsnorm_quant_i8.hip b/libr4d/r4d_rmsnorm_quant_i8.hip index 30b24824..1fade643 100644 --- a/libr4d/r4d_rmsnorm_quant_i8.hip +++ b/libr4d/r4d_rmsnorm_quant_i8.hip @@ -149,6 +149,104 @@ __global__ __launch_bounds__(THR) void rmsnorm_quant_i8_kernel( ybuf, l2, lm); } +// The same kernel at decode heights (THR 1024, n <= 1024 * EPT), with every operand of the row +// loaded in one go before the first reduction. The kernel above reads the row again in each pass, +// each read behind a barrier, so a row costs three dependent memory round trips; on gfx1201 that +// held the kernel at 11 us at any height against a 4.2 us launch floor, and this form at 8.2 us +// (4x R9700, 1 to 16 rows of 5376, 2026-10-17). Every pass partitions the row the same way (thread +// t takes columns t, t+THR, ...), so each thread keeps its own columns in registers and none needs +// another's; only the fragment-order store crosses threads, through `ybuf` as before. The sums, +// the order they are taken in and every element's arithmetic are junction_row's, so the bytes are. +template +__global__ __launch_bounds__(THR) void rmsnorm_quant_i8_reg_kernel( + const uint16_t* __restrict__ x, uint16_t* __restrict__ res, const void* __restrict__ w, + const void* __restrict__ wp, const void* __restrict__ sc, uint32_t sc_f32, + signed char* __restrict__ q, float* __restrict__ S, uint16_t* __restrict__ yo, + uint32_t n, float eps, float wadd, uint32_t x_ld, uint32_t r_ld, uint32_t y_ld) { + extern __shared__ uint16_t ybuf[]; + __shared__ float l1[THR / kWave], l2[THR / kWave], lm[THR / kWave]; + const uint32_t row = blockIdx.x, tid = threadIdx.x; + const uint16_t* __restrict__ xr = x + (size_t)row * x_ld; + uint16_t* __restrict__ rr = RES ? res + (size_t)row * r_ld : nullptr; + uint16_t* __restrict__ yr = yo ? yo + (size_t)row * y_ld : nullptr; + + uint16_t xv[EPT], rv[EPT]; + float gp[EPT], gw[EPT]; +#pragma unroll + for (uint32_t e = 0; e < EPT; ++e) { + const uint32_t i = tid + e * THR; + xv[e] = 0; rv[e] = 0; gp[e] = 0.0f; gw[e] = 0.0f; + if (i < n) { + xv[e] = xr[i]; + if (RES) rv[e] = rr[i]; + if (POST) gp[e] = gain(wp, i, WF32) + wadd; + gw[e] = gain(w, i, WF32) + wadd; + } + } + const float sv = SCL ? scalar_of(sc, sc_f32) : 1.0f; + + // ---- the post-norm's sum of squares, over the branch output + float s1 = 0.0f; + if (POST) { + float ss = 0.0f; +#pragma unroll + for (uint32_t e = 0; e < EPT; ++e) + if (tid + e * THR < n) { const float v = bf2f(xv[e]); ss += v * v; } + s1 = rsqrtf(block_sum(ss, l1) / (float)n + eps); + } + + // ---- the junction (post-norm, add, scalar), stored, and the norm's sum of squares over it + float ss2 = 0.0f; +#pragma unroll + for (uint32_t e = 0; e < EPT; ++e) { + const uint32_t i = tid + e * THR; + if (i < n) { + uint16_t v = xv[e]; + if (RES) { + if (POST) v = f32_to_bf16(bf2f(v) * s1 * gp[e]); + v = f32_to_bf16(bf2f(rv[e]) + bf2f(v)); + if (SCL) v = f32_to_bf16(bf2f(v) * sv); + rr[i] = v; + } + xv[e] = v; + const float f = bf2f(v); + ss2 += f * f; + } + } + const float s2 = rsqrtf(block_sum(ss2, l2) / (float)n + eps); + + // ---- the norm output, rounded to bf16 as the standalone norm stores it, and its absmax + float m = 0.0f; +#pragma unroll + for (uint32_t e = 0; e < EPT; ++e) { + const uint32_t i = tid + e * THR; + if (i < n) { + const uint16_t y = f32_to_bf16(bf2f(xv[e]) * s2 * gw[e]); + ybuf[i] = y; + if (yr) yr[i] = y; + m = fmaxf(m, fabsf(bf2f(y))); + } + } + m = r4d_wave_max(m); + if ((tid & 31) == 0) lm[tid >> 5] = m; + __syncthreads(); // also publishes ybuf to every thread + m = lm[0]; +#pragma unroll + for (uint32_t i = 1; i < THR / kWave; i++) m = fmaxf(m, lm[i]); + const float scale = fmaxf(m, 1e-8f) / 127.f; + const float inv = 1.f / scale; + if (tid == 0) S[row] = scale; + + // ---- the codes, in junction_row's fragment order + signed char* __restrict__ qr = q + (size_t)row * n; + for (uint32_t i = tid; i < n; i += THR) { + const uint32_t s = i >> 4, j = i & 15; + const uint32_t src = (s << 4) + 8 * ((j & 7) >> 2) + 4 * (j >> 3) + (j & 3); + const int t = __float2int_rn(bf2f(ybuf[src]) * inv); + qr[i] = (signed char)(t < -128 ? -128 : (t > 127 ? 127 : t)); + } +} + // --------------------------------------------------------------------------------------------- // The same junction with the two-rank all-reduce that precedes it: the branch output is a rank's // partial sum of a column-sharded linear (o_proj, down_proj), and the post-norm is of the whole @@ -368,13 +466,25 @@ extern "C" int r4d_rad_rmsnorm_quant_i8(const RadArgs* a, RadStream stream) { const float wadd = (float)r4d_getf(a, "wadd", 0.0); const uint32_t scf = sc ? (sc->dtype == RAD_F32) : 0u; + // The decode form keeps a thread's columns in registers: at 1024 threads, EPT of them. + const int ept = (thr != 1024 || !r4d_cfg_rmsq8_reg(a)) ? 0 + : n <= 4096 ? 4 : n <= 6144 ? 6 : n <= 8192 ? 8 : 0; + #define R4D_RQI8_T(WF, RS, PO, SC, T) emit_rc = R4D_EMIT(stream, \ (rmsnorm_quant_i8_kernel), dim3((uint32_t)M), dim3(T), lds, \ (const uint16_t*)r4d_p(x), (uint16_t*)r4d_wp(rs), r4d_p(w), r4d_p(wp), r4d_p(sc), scf, \ (signed char*)r4d_wp(q), (float*)r4d_wp(s), (uint16_t*)r4d_wp(y), (uint32_t)n, eps, wadd, \ (uint32_t)xp, (uint32_t)rp, (uint32_t)yp) +#define R4D_RQI8_R(WF, RS, PO, SC, E) emit_rc = R4D_EMIT(stream, \ + (rmsnorm_quant_i8_reg_kernel), dim3((uint32_t)M), dim3(1024), lds, \ + (const uint16_t*)r4d_p(x), (uint16_t*)r4d_wp(rs), r4d_p(w), r4d_p(wp), r4d_p(sc), scf, \ + (signed char*)r4d_wp(q), (float*)r4d_wp(s), (uint16_t*)r4d_wp(y), (uint32_t)n, eps, wadd, \ + (uint32_t)xp, (uint32_t)rp, (uint32_t)yp) #define R4D_RQI8(WF, RS, PO, SC) \ - do { if (thr == 1024) R4D_RQI8_T(WF, RS, PO, SC, 1024); \ + do { if (ept == 4) R4D_RQI8_R(WF, RS, PO, SC, 4); \ + else if (ept == 6) R4D_RQI8_R(WF, RS, PO, SC, 6); \ + else if (ept == 8) R4D_RQI8_R(WF, RS, PO, SC, 8); \ + else if (thr == 1024) R4D_RQI8_T(WF, RS, PO, SC, 1024); \ else if (thr == 512) R4D_RQI8_T(WF, RS, PO, SC, 512); \ else if (thr == 256) R4D_RQI8_T(WF, RS, PO, SC, 256); \ else return R4D_NO(RAD_E_INVAL); } while (0) @@ -388,6 +498,7 @@ extern "C" int r4d_rad_rmsnorm_quant_i8(const RadArgs* a, RadStream stream) { else R4D_RQI8_W(false); #undef R4D_RQI8_W #undef R4D_RQI8 +#undef R4D_RQI8_R #undef R4D_RQI8_T return emit_rc; } diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index b85668ae..e27aa13e 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -4754,6 +4754,14 @@ static const RadTunable tMv[] = { { "rb", kMvRb, NELEM(kMvRb), 0, "rows of the weight one wave owns. 0 = libr4d's own rule" }, }; +/* rmsnorm_quant_i8 at decode heights (1024 threads, n <= 8192): 1 loads every operand of the row + * into registers before the first reduction (rmsnorm_quant_i8_reg_kernel), 0 re-reads the row in + * each pass. The same bytes either way; on 4x R9700 the first is 8.2 us a launch, the second 11. */ +static const int64_t kRmsQ8Reg[] = { 1, 0 }; +static const RadTunable tRmsQ8[] = { + { "reg", kRmsQ8Reg, NELEM(kRmsQ8Reg), 1, "decode heights: 1 = the row's operands loaded before the reductions, 0 = re-read each pass" }, +}; + /* ==================================================================================== the rows */ /* ------------------------------------------- the small model ops and the sampler chain ------- @@ -7546,7 +7554,7 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, "bf16 in, int8 out in the w4a8 GEMM's fragment order with an f32 per-row scale, optional bf16 " "out; every stored intermediate rounded to bf16 as the unfused chain stores it", RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, - ROW(cRmsNormQuantI8), NOTUNE, + ROW(cRmsNormQuantI8), ROW(tRmsQ8), nullptr, nullptr, nullptr, r4d_rad_rmsnorm_quant_i8 }, { "qkv_norm_rope_bf16", "qkv_norm_rope", "attn", @@ -8918,3 +8926,6 @@ extern "C" void r4d_cfg_topk(const RadArgs* a, R4dTopkCfg* out) { extern "C" void r4d_cfg_mv(const RadArgs* a, R4dMvCfg* out) { out->rb = TUNE(a, tMv, "rb"); } +extern "C" int r4d_cfg_rmsq8_reg(const RadArgs* a) { + return TUNE(a, tRmsQ8, "reg"); +} diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 905f6dee..1046df5b 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -10961,7 +10961,7 @@ static void qkv_norm_rope_numeric(const Plugin& pl) { * feed-forward) and with one (feed-forward -> next layer). The unfused chain, kernel by kernel, is * rmsnorm(x; w_post) -> add(residual) -> scale -> rmsnorm(residual; w) -> quant_act_i8. bf16 gains * (as Gemma's checkpoint stores), and two batch heights covering both r4d_act_threads widths a - * decode step meets (1024 at M <= 32, 512 above). */ + * decode step meets (1024 at M <= 32, 512 above), the first in both of its forms. */ static void rmsnorm_quant_i8_numeric(const Plugin& pl) { const RadKernelInfo* nrm = find(pl, "rmsnorm_bf16"); const RadKernelInfo* ad = find(pl, "add_bf16"); @@ -10969,7 +10969,12 @@ static void rmsnorm_quant_i8_numeric(const Plugin& pl) { const RadKernelInfo* qz = find(pl, "quant_act_i8"); const RadKernelInfo* rq = find(pl, "rmsnorm_quant_i8"); if (!nrm || !ad || !scl || !qz || !rq) { ok("rmsnorm_quant_i8 vs its chain", false, "rows absent"); return; } - for (const int64_t M : { (int64_t)1, (int64_t)8, (int64_t)40 }) { + /* The row's reg axis at 1024 threads: 1 (the default) holds a thread's columns in registers, + * 0 re-reads the row each pass; both must leave the chain's bytes. */ + struct RqCase { int64_t M; int reg; }; + for (const RqCase rqc : { RqCase{ 1, 1 }, RqCase{ 8, 1 }, RqCase{ 8, 0 }, RqCase{ 40, 1 } }) { + const int64_t M = rqc.M; + const std::string form = rqc.reg ? "" : " (re-read form)"; const int64_t K = 5376; /* Gemma 4 31B's hidden width: 336 sixteen-byte k steps */ std::vector hx((size_t)M * K), hr((size_t)M * K), hw((size_t)K), hp((size_t)K); for (int64_t r = 0; r < M; ++r) @@ -11005,6 +11010,8 @@ static void rmsnorm_quant_i8_numeric(const Plugin& pl) { const std::vector pn = { PI("M", M), PI("n", K), PF("eps", eps), PS("dtype", "bf16"), PF("wadd", wadd) }; const std::vector pe = { PI("M", M), PI("n", K), PS("dtype", "bf16") }; const std::vector pq = { PI("M", M), PI("n", K), PI("group", K), PS("dtype", "bf16") }; + std::vector prq = pn; + prq.push_back(PI("reg", rqc.reg)); auto compare = [&](const std::string& what, bool res) { std::vector y0((size_t)M * K), y1((size_t)M * K), r0((size_t)M * K), r1((size_t)M * K); @@ -11041,9 +11048,9 @@ static void rmsnorm_quant_i8_numeric(const Plugin& pl) { if (rc == RAD_OK) rc = launch_row(rq, { T(d_x, RAD_BF16, { M, K }), T(nullptr, RAD_BF16, {}), T(d_w, RAD_BF16, { K }), T(d_q[1], RAD_I8, { M, K }), T(d_s[1], RAD_F32, { M }), T(d_y[1], RAD_BF16, { M, K }), - T(nullptr, RAD_BF16, {}), T(nullptr, RAD_BF16, {}) }, pn); - if (rc != RAD_OK) ok("rmsnorm_quant_i8 vs rmsnorm + quant_act_i8", false, "rc=" + std::to_string(rc)); - else compare("rmsnorm_quant_i8 is byte-identical to rmsnorm + quant_act_i8", false); + T(nullptr, RAD_BF16, {}), T(nullptr, RAD_BF16, {}) }, prq); + if (rc != RAD_OK) ok(("rmsnorm_quant_i8 vs rmsnorm + quant_act_i8" + form).c_str(), false, "rc=" + std::to_string(rc)); + else compare("rmsnorm_quant_i8 is byte-identical to rmsnorm + quant_act_i8" + form, false); /* --- the junction, without and with the scalar --- */ for (const bool with_sc : { false, true }) { @@ -11059,8 +11066,8 @@ static void rmsnorm_quant_i8_numeric(const Plugin& pl) { rc = launch_row(rq, { T(d_x, RAD_BF16, { M, K }), T(d_r[1], RAD_BF16, { M, K }), T(d_w, RAD_BF16, { K }), T(d_q[1], RAD_I8, { M, K }), T(d_s[1], RAD_F32, { M }), T(d_y[1], RAD_BF16, { M, K }), T(d_p, RAD_BF16, { K }), - with_sc ? T(d_sc, RAD_BF16, { 1 }) : T(nullptr, RAD_BF16, {}) }, pn); - const std::string what = with_sc ? "rmsnorm_quant_i8 junction with scalar" : "rmsnorm_quant_i8 junction"; + with_sc ? T(d_sc, RAD_BF16, { 1 }) : T(nullptr, RAD_BF16, {}) }, prq); + const std::string what = (with_sc ? "rmsnorm_quant_i8 junction with scalar" : "rmsnorm_quant_i8 junction") + form; if (rc != RAD_OK) ok((what + " vs rmsnorm + add + scale + rmsnorm + quant_act_i8").c_str(), false, "rc=" + std::to_string(rc)); else compare(what + " is byte-identical to its chain", true); }