Archived
gemma4k, arch/gemma4: the feed-forward join writes the next layer's input norm
join_add_scale takes an optional gain `w_n` and output `y` (both or neither) and, given them, writes y = rmsnorm(x) * w_n over the residual it has just stored -- the next layer's input_layernorm, or the final norm after the last layer. Those norms read the x the join writes and nothing else, so each was a launch of its own and a second read of the row; now it is a fourth reduction in a block that already holds x in registers. On the 26B-A4B that is 30 launches a forward pass the fewer (29 input norms and the final norm), at decode a step's worth of ~8 us launches. Rounding where the chain rounds: y is taken over the bf16 x the join stores, with libr4d's expression, so g4k_emu holds it to the chain bit for bit (+next cases, decode to prefill heights, odd widths and pitches) and to the host arm within a flipped rounding. arch/gemma4 declares every layer's input-norm gain ahead of the layers so a join can name the next one's; where a join resolves, the norm it replaces is neither declared nor issued, and where it does not, the chain is as before. docs/OPS.md: the row and its paragraph. arch_test: fused, the only x -> h rmsnorm is layer 0's and every join writes h; refused, all thirteen come back. Not yet run on the cards. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3a48ee78de
commit
171dcee15b
6 files changed
+158
-37
No files matched your search
+38
-8
@@ -7,9 +7,10 @@
|
||||
* 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
|
||||
* j = norm(d' + r') * w_j; x = (x + j) * s; and optionally y = norm(x) * w_n
|
||||
* (post_feedforward_layernorm_1 and _2, the arms' sum, post_feedforward_layernorm,
|
||||
* the residual add, layer_scalar)
|
||||
* the residual add, layer_scalar -- and the NEXT layer's input_layernorm, or the
|
||||
* final norm after the last layer, which read the x this writes)
|
||||
*
|
||||
* 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
|
||||
@@ -74,6 +75,7 @@ struct NormArgs {
|
||||
const float* w0; /* w_a, or w_d */
|
||||
const float* w1; /* w_y, or w_r */
|
||||
const float* w2; /* w_j */
|
||||
const float* w3; /* w_n, the next norm's gain (or null) */
|
||||
const uint16_t* s; /* the layer scalar, bf16, one element */
|
||||
int n;
|
||||
float eps;
|
||||
@@ -118,7 +120,9 @@ __global__ __launch_bounds__(kThreads) void norm_add_norm_kernel(NormArgs A) {
|
||||
}
|
||||
}
|
||||
|
||||
/* x = (x + norm(norm(d) * w_d + norm(r) * w_r) * w_j) * s */
|
||||
/* x = (x + norm(norm(d) * w_d + norm(r) * w_r) * w_j) * s, and with NEXT y = norm(x) * w_n: the
|
||||
* next layer's input norm taken over the bf16 x this stores, which is what that norm would read. */
|
||||
template <bool NEXT>
|
||||
__global__ __launch_bounds__(kThreads) void join_add_scale_kernel(NormArgs A) {
|
||||
__shared__ float lds[kWaves];
|
||||
const int tid = (int)threadIdx.x, n = A.n;
|
||||
@@ -155,13 +159,28 @@ __global__ __launch_bounds__(kThreads) void join_add_scale_kernel(NormArgs A) {
|
||||
}
|
||||
const float sj = norm_factor(block_sum(ss, lds), n, A.eps);
|
||||
const float sc = bf2f(*A.s);
|
||||
ss = 0.0f;
|
||||
#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);
|
||||
const uint16_t xn = f2bf(x1 * sc);
|
||||
xr[i] = xn;
|
||||
rv[k] = bf2f(xn); /* the new x, kept for the next norm */
|
||||
} else {
|
||||
rv[k] = 0.0f;
|
||||
}
|
||||
if (NEXT) ss += rv[k] * rv[k];
|
||||
}
|
||||
if (NEXT) {
|
||||
const float sn = norm_factor(block_sum(ss, lds), n, A.eps);
|
||||
uint16_t* yr = A.y + m * A.y_ld;
|
||||
#pragma unroll
|
||||
for (int k = 0; k < kMaxPer; ++k) {
|
||||
const int i = tid + k * kThreads;
|
||||
if (i < n) yr[i] = f2bf((rv[k] * sn) * A.w3[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -219,7 +238,7 @@ int g4k_norm_add_norm_bf16(const RadArgs* a, RadStream stream) {
|
||||
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
|
||||
}
|
||||
|
||||
/* operands: d, w_d, r, w_r, w_j, x (inout), s */
|
||||
/* operands: d, w_d, r, w_r, w_j, x (inout), s, w_n?, y? -- the last two both or neither */
|
||||
int g4k_join_add_scale_bf16(const RadArgs* a, RadStream stream) {
|
||||
using namespace g4kn;
|
||||
if (!a || a->n_t < 7) return RAD_E_INVAL;
|
||||
@@ -232,20 +251,31 @@ int g4k_join_add_scale_bf16(const RadArgs* a, RadStream stream) {
|
||||
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;
|
||||
const RadTensor *Wn = a->n_t > 7 ? rad_arg_in(a, 7) : nullptr,
|
||||
*Y = a->n_t > 8 ? rad_arg_in(a, 8) : nullptr;
|
||||
if (!Wn != !Y) return RAD_E_INVAL;
|
||||
if (Wn && !gain(Wn, n)) return RAD_E_DTYPE;
|
||||
int64_t rd = 0, ld = 0, rr = 0, lr = 0, rx = 0, lx = 0, ry = 0, ly = 0;
|
||||
if (!rows_of(D, n, &rd, &ld) || !rows_of(R, n, &rr, &lr) || !rows_of(X, n, &rx, &lx))
|
||||
return RAD_E_SHAPE;
|
||||
if (Y && !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 (rd < M || rr < M || M > 0x7fffffff) return RAD_E_SHAPE;
|
||||
if (rd < M || rr < M || (Y && ry < 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;
|
||||
if (Y) { P.w3 = (const float*)Wn->data; P.y = (uint16_t*)Y->data; P.y_ld = ly; }
|
||||
P.n = (int)n; P.eps = eps;
|
||||
G4K_LAUNCH(G4K_KNAME(join_add_scale_kernel), dim3((unsigned)M), dim3(kThreads), (hipStream_t)stream, P);
|
||||
if (Y)
|
||||
G4K_LAUNCH(G4K_KNAME(join_add_scale_kernel<true>), dim3((unsigned)M), dim3(kThreads),
|
||||
(hipStream_t)stream, P);
|
||||
else
|
||||
G4K_LAUNCH(G4K_KNAME(join_add_scale_kernel<false>), dim3((unsigned)M), dim3(kThreads),
|
||||
(hipStream_t)stream, P);
|
||||
return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE;
|
||||
}
|
||||
|
||||
|
||||
+19
-4
@@ -82,6 +82,8 @@ static const RadOperandSpec oJas[] = {
|
||||
{ "w_j", RAD_OPD_WEIGHT, 0 },
|
||||
{ "x", RAD_OPD_INOUT, 0 },
|
||||
{ "s", RAD_OPD_WEIGHT, 0 },
|
||||
{ "w_n", RAD_OPD_WEIGHT, 1 },
|
||||
{ "y", RAD_OPD_OUT, 1 },
|
||||
};
|
||||
static const RadOpSchema kSchemas[] = {
|
||||
{ "pair_argmax", ROW(pPick), ROW(oPick),
|
||||
@@ -95,8 +97,9 @@ static const RadOpSchema kSchemas[] = {
|
||||
"point. A post-norm, the residual add and the next pre-norm (Gemma's sandwich)." },
|
||||
{ "join_add_scale", ROW(pJoin), ROW(oJas),
|
||||
"x = (x + rmsnorm(rmsnorm(d) * w_d + rmsnorm(r) * w_r) * w_j) * s, over [M, n] rows, s one "
|
||||
"element; rounding where the unfused chain rounds. Two arms' post-norms, their sum, its norm, "
|
||||
"the residual add and a per-layer scalar (Gemma 4's feed-forward join)." },
|
||||
"element; and with w_n and y, y = rmsnorm(x) * w_n over the x written. Rounding where the "
|
||||
"unfused chain rounds. Two arms' post-norms, their sum, its norm, the residual add, a "
|
||||
"per-layer scalar and the next layer's input norm (Gemma 4's feed-forward join)." },
|
||||
};
|
||||
|
||||
/* ==================================================================== operand descriptions
|
||||
@@ -279,6 +282,9 @@ int norm_add_norm_host(const RadArgs* a, RadStream) {
|
||||
|
||||
int join_add_scale_host(const RadArgs* a, RadStream) {
|
||||
if (!a || a->n_t < 7) return RAD_E_INVAL;
|
||||
const RadTensor *Wn = a->n_t > 7 ? rad_arg_in(a, 7) : nullptr,
|
||||
*Y = a->n_t > 8 ? rad_arg_in(a, 8) : nullptr;
|
||||
if (!Wn != !Y) return RAD_E_INVAL;
|
||||
const int64_t n = rad_args_geti_or(a, "n", 0);
|
||||
const double eps = rad_args_getf_or(a, "eps", 1e-6);
|
||||
const RadTensor *D = rad_arg_in(a, 0), *Wd = rad_arg_in(a, 1), *R = rad_arg_in(a, 2),
|
||||
@@ -287,9 +293,11 @@ int join_add_scale_host(const RadArgs* a, RadStream) {
|
||||
if (n <= 0 || !D || !R || !X || !Sc) return RAD_E_INVAL;
|
||||
if (!hgain(Wd, n) || !hgain(Wr, n) || !hgain(Wj, n)) return RAD_E_DTYPE;
|
||||
if (Sc->dtype != RAD_BF16 || rad_tensor_numel(Sc) != 1) return RAD_E_DTYPE;
|
||||
int64_t rd, ld, rr, lr, rx, lx;
|
||||
if (Wn && !hgain(Wn, n)) return RAD_E_DTYPE;
|
||||
int64_t rd, ld, rr, lr, rx, lx, ry = 0, ly = 0;
|
||||
if (!hrows(D, n, &rd, &ld) || !hrows(R, n, &rr, &lr) || !hrows(X, n, &rx, &lx)) return RAD_E_SHAPE;
|
||||
if (rd < rx || rr < rx) return RAD_E_SHAPE;
|
||||
if (Y && !hrows(Y, n, &ry, &ly)) return RAD_E_SHAPE;
|
||||
if (rd < rx || rr < rx || (Y && ry < rx)) return RAD_E_SHAPE;
|
||||
const float *wd = (const float*)Wd->data, *wr = (const float*)Wr->data, *wj = (const float*)Wj->data;
|
||||
const float sc = hb2f(*(const uint16_t*)Sc->data);
|
||||
std::vector<float> dv((size_t)n), rv((size_t)n);
|
||||
@@ -305,6 +313,13 @@ int join_add_scale_host(const RadArgs* a, RadStream) {
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
const float x1 = hrbf(hb2f(xr[i]) + hrbf((dv[(size_t)i] * sj) * wj[i]));
|
||||
xr[i] = hf2b(x1 * sc);
|
||||
rv[(size_t)i] = hb2f(xr[i]);
|
||||
}
|
||||
if (Y) {
|
||||
const float* wn = (const float*)Wn->data;
|
||||
uint16_t* yr = (uint16_t*)Y->data + m * ly;
|
||||
const float sn = hnorm(rv.data(), n, eps);
|
||||
for (int64_t i = 0; i < n; ++i) yr[i] = hf2b((rv[(size_t)i] * sn) * wn[i]);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
|
||||
Reference in new issue
Block a user