This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/gemma4k/g4k_norm.hip
T
AshlynandClaude Opus 5.5 833173cc04 gemma4k: Gemma 4's kernels beyond libr4d -- paged attention, pair_argmax, the sandwich joins
A kernel library beside libr4d, gfx12 only, and AHEAD of it for Gemma 4:
`--kernels gemma4k,libr4d,...`. Its own library rather than rows in libr4d: a smaller change,
its own gfx12 gating, and no churn in libr4d's row table; folding it in can follow.

- attn_paged at head_dim 256 and 512, gqa 1/2/4/8, bf16 or fp8 cache, any window, causal or not.
  One scalar kernel serves decode, verify and prefill, with a split-KV path whose merge is one
  wave a row (live pieces found by ballot, four at a time; a piece that saw no key writes only
  its max and sum); a WMMA prefill kernel takes a query of at least 64 rows and 32 a sequence on
  average -- Q and K bf16, P and V f16 (bf16 P measured 0.25% of the V range in error), the
  denominator summed from the rounded P. RADIANCE_G4K_WMMA=0 keeps every launch on the scalar
  kernel.
- pair_argmax, device and host, described as libref describes it so rad-kbench checks both.
- norm_add_norm and join_add_scale, ops of its own (docs/OPS.md, a new subsection): each one
  kernel for a post-norm / residual add / pre-norm chain, rounding where the chain rounds. libref
  has no row for them on purpose -- a reference row would resolve them where gemma4k is not
  loaded and the architecture would never fall back to the chain -- and no row declares
  `replaces`, since rad-kbench assembles a replaced chain from the fusing library's own rows.

WHY FIRST. libr4d on this tree also has rows at h256/gqa2 and h512/gqa8, Gemma 4's own
geometries, written for the dense 31B (which has no drafter) and not measured on the 26B-A4B or
under a DFlash verify; the hierarchy is absolute, so the first library takes them. These rows are
the ones every Gemma 4 number below was measured with. No other row overlaps libr4d's (the h256
gqa4/gqa8 rows take a window only), so putting this library first takes nothing else from it. The
softcap is libr4d's and libavx's `logit_softcap`.

Measured on 4x R9700, gemma-4-26B-A4B-it + z-lab DFlash, TP4: decode attention 3.234 -> 0.104 ms
a step on the full layers and 1.906 -> 0.722 on the sliding ones (the split-KV merge); a
20,489-token prefill 4.01 -> 2.83 s (the WMMA kernel); rad-kbench against libref 176/176. Not yet
run on the cards: the joins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 14:40:04 -05:00

253 lines
11 KiB
Plaintext

/* g4k_norm.hip -- Gemma 4's sandwich-norm joins, each one kernel instead of a chain of small ones.
*
* A decode step of the 26B-A4B launches ~250 rmsnorms and ~90 adds a step, each a few hundred
* bytes of work behind a full launch: --profile-ops measured 2.0 ms and 0.85 ms of a 16.6 ms step.
* Two places in every layer are pure chains of them:
*
* norm_add_norm the attention's join: t = norm(a) * w_a; x += t; y = norm(x) * w_y
* (post_attention_layernorm, the residual add, pre_feedforward_layernorm)
* join_add_scale the feed-forward's: d' = norm(d) * w_d; r' = norm(r) * w_r;
* j = norm(d' + r') * w_j; x = (x + j) * s
* (post_feedforward_layernorm_1 and _2, the arms' sum, post_feedforward_layernorm,
* the residual add, layer_scalar)
*
* THE SAME FUNCTION AS THE CHAIN, ROUNDING FOR ROUNDING. Every value the chain stores as bf16 is
* rounded to bf16 here at the same point, and every element is computed with libr4d's own
* expression: a norm is bf16((v * rsqrtf(mean(v^2) + eps)) * g), an add bf16(u + v), a scale
* bf16(x * s). The one thing that can differ is the ORDER the sum of squares is added in -- libr4d
* picks its block width from a measured table -- which moves the f32 norm factor by an ulp at most
* and, rarely, a bf16 output by one ulp. The arch plugin asks for these first and falls back to
* the chain where nothing serves them (gemma4.cpp).
*
* One 256-thread block a row: a decode step is a handful of rows, so the row is latency-bound and
* a block's three reductions beat seven launches. Values a later stage reuses stay in registers
* (kMaxPer a thread, so n <= 256 * kMaxPer; the row table says so).
*/
#include <hip/hip_runtime.h>
#include <cstdint>
#include "g4k.h"
#include "g4k_launch.h"
#include "rad_plugin.h"
namespace g4kn {
constexpr int kThreads = 256;
constexpr int kWaves = kThreads / 32;
constexpr int kMaxPer = 24; /* elements a thread holds: n <= 6144 */
__device__ __forceinline__ float bf2f(uint16_t h) { return __uint_as_float((unsigned)h << 16); }
/* round to nearest even, as libr4d's f32_to_bf16 */
__device__ __forceinline__ uint16_t f2bf(float f) {
const unsigned u = __float_as_uint(f);
return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
}
__device__ __forceinline__ float rbf(float f) { return bf2f(f2bf(f)); }
__device__ __forceinline__ int lane_id() { return (int)(threadIdx.x & 31u); }
__device__ __forceinline__ float shfl_xor(float v, int m) {
return __int_as_float(__builtin_amdgcn_ds_bpermute((lane_id() ^ m) << 2, __float_as_int(v)));
}
/* the block's sum, every thread the same value: a wave butterfly, then the waves in order */
__device__ __forceinline__ float block_sum(float v, float* lds) {
#pragma unroll
for (int d = 16; d >= 1; d >>= 1) v += shfl_xor(v, d);
const int lane = lane_id(), wave = (int)(threadIdx.x >> 5);
__syncthreads(); /* lds is free: the last read of it is done */
if (lane == 0) lds[wave] = v;
__syncthreads();
float s = 0.0f;
#pragma unroll
for (int w = 0; w < kWaves; ++w) s += lds[w];
return s;
}
__device__ __forceinline__ float norm_factor(float ss, int n, float eps) {
return rsqrtf(ss / (float)n + eps);
}
struct NormArgs {
const uint16_t* a; long long a_ld; /* norm_add_norm: the block output */
const uint16_t* d; long long d_ld; /* join_add_scale: the dense partial */
const uint16_t* r; long long r_ld; /* the routed partial */
uint16_t* x; long long x_ld; /* the residual stream, read and written */
uint16_t* y; long long y_ld; /* norm_add_norm: the next norm's output */
const float* w0; /* w_a, or w_d */
const float* w1; /* w_y, or w_r */
const float* w2; /* w_j */
const uint16_t* s; /* the layer scalar, bf16, one element */
int n;
float eps;
};
/* t = norm(a) * w_a; x += t; y = norm(x) * w_y */
__global__ __launch_bounds__(kThreads) void norm_add_norm_kernel(NormArgs A) {
__shared__ float lds[kWaves];
const int tid = (int)threadIdx.x, n = A.n;
const long long m = (long long)blockIdx.x;
const uint16_t* ar = A.a + m * A.a_ld;
uint16_t* xr = A.x + m * A.x_ld;
uint16_t* yr = A.y + m * A.y_ld;
float av[kMaxPer], xv[kMaxPer];
float ss = 0.0f;
#pragma unroll
for (int k = 0; k < kMaxPer; ++k) {
const int i = tid + k * kThreads;
av[k] = i < n ? bf2f(ar[i]) : 0.0f;
ss += av[k] * av[k];
}
const float sa = norm_factor(block_sum(ss, lds), n, A.eps);
ss = 0.0f;
#pragma unroll
for (int k = 0; k < kMaxPer; ++k) {
const int i = tid + k * kThreads;
if (i < n) {
const float t = rbf((av[k] * sa) * A.w0[i]);
xv[k] = rbf(bf2f(xr[i]) + t);
xr[i] = f2bf(xv[k]);
} else {
xv[k] = 0.0f;
}
ss += xv[k] * xv[k];
}
const float sx = norm_factor(block_sum(ss, lds), n, A.eps);
#pragma unroll
for (int k = 0; k < kMaxPer; ++k) {
const int i = tid + k * kThreads;
if (i < n) yr[i] = f2bf((xv[k] * sx) * A.w1[i]);
}
}
/* x = (x + norm(norm(d) * w_d + norm(r) * w_r) * w_j) * s */
__global__ __launch_bounds__(kThreads) void join_add_scale_kernel(NormArgs A) {
__shared__ float lds[kWaves];
const int tid = (int)threadIdx.x, n = A.n;
const long long m = (long long)blockIdx.x;
const uint16_t* dr = A.d + m * A.d_ld;
const uint16_t* rr = A.r + m * A.r_ld;
uint16_t* xr = A.x + m * A.x_ld;
float dv[kMaxPer], rv[kMaxPer];
float sd2 = 0.0f, sr2 = 0.0f;
#pragma unroll
for (int k = 0; k < kMaxPer; ++k) {
const int i = tid + k * kThreads;
dv[k] = i < n ? bf2f(dr[i]) : 0.0f;
rv[k] = i < n ? bf2f(rr[i]) : 0.0f;
sd2 += dv[k] * dv[k];
sr2 += rv[k] * rv[k];
}
const float sd = norm_factor(block_sum(sd2, lds), n, A.eps);
const float sr = norm_factor(block_sum(sr2, lds), n, A.eps);
/* the join, kept in dv */
float ss = 0.0f;
#pragma unroll
for (int k = 0; k < kMaxPer; ++k) {
const int i = tid + k * kThreads;
if (i < n) {
const float u = rbf((dv[k] * sd) * A.w0[i]);
const float v = rbf((rv[k] * sr) * A.w1[i]);
dv[k] = rbf(u + v);
} else {
dv[k] = 0.0f;
}
ss += dv[k] * dv[k];
}
const float sj = norm_factor(block_sum(ss, lds), n, A.eps);
const float sc = bf2f(*A.s);
#pragma unroll
for (int k = 0; k < kMaxPer; ++k) {
const int i = tid + k * kThreads;
if (i < n) {
const float j = rbf((dv[k] * sj) * A.w2[i]);
const float x1 = rbf(bf2f(xr[i]) + j);
xr[i] = f2bf(x1 * sc);
}
}
}
/* ------------------------------------------------------------------ host: the launch adapters */
/* [rows..., n] with a dense last axis: its row count and pitch, or false */
static bool rows_of(const RadTensor* t, int64_t n, int64_t* rows, int64_t* ld) {
if (!t || t->dtype != RAD_BF16 || t->rank < 1) return false;
if (rad_tensor_lastdim(t) != n || (t->stride[t->rank - 1] != 1)) return false;
*rows = rad_tensor_rows(t);
*ld = rad_tensor_row_pitch(t);
return *ld >= n || *rows <= 1;
}
static bool gain(const RadTensor* w, int64_t n) {
return w && w->dtype == RAD_F32 && rad_tensor_numel(w) == n &&
(w->rank == 0 || w->stride[w->rank - 1] == 1);
}
static int params(const RadArgs* a, int64_t* n, float* eps) {
*n = rad_args_geti_or(a, "n", 0);
*eps = (float)rad_args_getf_or(a, "eps", 1e-6);
if (*n <= 0 || *n > (int64_t)kThreads * kMaxPer) return RAD_E_SHAPE;
return RAD_OK;
}
} /* namespace g4kn */
extern "C" {
/* operands: a, w_a, x (inout), w_y, y */
int g4k_norm_add_norm_bf16(const RadArgs* a, RadStream stream) {
using namespace g4kn;
if (!a || a->n_t < 5) return RAD_E_INVAL;
int64_t n = 0;
float eps = 0.0f;
if (int rc = params(a, &n, &eps)) return rc;
const RadTensor *A_ = rad_arg_in(a, 0), *Wa = rad_arg_in(a, 1), *X = rad_arg_in(a, 2),
*Wy = rad_arg_in(a, 3), *Y = rad_arg_in(a, 4);
if (!A_ || !X || !Y) return RAD_E_INVAL;
if (!gain(Wa, n) || !gain(Wy, n)) return RAD_E_DTYPE;
int64_t ra = 0, la = 0, rx = 0, lx = 0, ry = 0, ly = 0;
if (!rows_of(A_, n, &ra, &la) || !rows_of(X, n, &rx, &lx) || !rows_of(Y, n, &ry, &ly))
return RAD_E_SHAPE;
const int64_t M = rx; /* the step's rows: the stream's view */
if (M <= 0) return RAD_OK;
if (ra < M || ry < M || M > 0x7fffffff) return RAD_E_SHAPE;
NormArgs P{};
P.a = (const uint16_t*)A_->data; P.a_ld = la;
P.x = (uint16_t*)X->data; P.x_ld = lx;
P.y = (uint16_t*)Y->data; P.y_ld = ly;
P.w0 = (const float*)Wa->data; P.w1 = (const float*)Wy->data;
P.n = (int)n; P.eps = eps;
G4K_LAUNCH(G4K_KNAME(norm_add_norm_kernel), dim3((unsigned)M), dim3(kThreads), (hipStream_t)stream, P);
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
}
/* operands: d, w_d, r, w_r, w_j, x (inout), s */
int g4k_join_add_scale_bf16(const RadArgs* a, RadStream stream) {
using namespace g4kn;
if (!a || a->n_t < 7) return RAD_E_INVAL;
int64_t n = 0;
float eps = 0.0f;
if (int rc = params(a, &n, &eps)) return rc;
const RadTensor *D = rad_arg_in(a, 0), *Wd = rad_arg_in(a, 1), *R = rad_arg_in(a, 2),
*Wr = rad_arg_in(a, 3), *Wj = rad_arg_in(a, 4), *X = rad_arg_in(a, 5),
*Sc = rad_arg_in(a, 6);
if (!D || !R || !X || !Sc) return RAD_E_INVAL;
if (!gain(Wd, n) || !gain(Wr, n) || !gain(Wj, n)) return RAD_E_DTYPE;
if (Sc->dtype != RAD_BF16 || rad_tensor_numel(Sc) != 1) return RAD_E_DTYPE;
int64_t rd = 0, ld = 0, rr = 0, lr = 0, rx = 0, lx = 0;
if (!rows_of(D, n, &rd, &ld) || !rows_of(R, n, &rr, &lr) || !rows_of(X, n, &rx, &lx))
return RAD_E_SHAPE;
const int64_t M = rx; /* the step's rows: the stream's view */
if (M <= 0) return RAD_OK;
if (rd < M || rr < M || M > 0x7fffffff) return RAD_E_SHAPE;
NormArgs P{};
P.d = (const uint16_t*)D->data; P.d_ld = ld;
P.r = (const uint16_t*)R->data; P.r_ld = lr;
P.x = (uint16_t*)X->data; P.x_ld = lx;
P.w0 = (const float*)Wd->data; P.w1 = (const float*)Wr->data; P.w2 = (const float*)Wj->data;
P.s = (const uint16_t*)Sc->data;
P.n = (int)n; P.eps = eps;
G4K_LAUNCH(G4K_KNAME(join_add_scale_kernel), dim3((unsigned)M), dim3(kThreads), (hipStream_t)stream, P);
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
}
} /* extern "C" */