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
+28
-14
@@ -135,28 +135,30 @@ static int case_nan(int M, int n, int pad, int extra_rows) {
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
|
||||
static int case_jas(int M, int n, int pad, int extra_rows) {
|
||||
/* `next`: also the next layer's input norm, y = norm(x) * w_n over the x the join writes */
|
||||
static int case_jas(int M, int n, int pad, int extra_rows, bool next = false) {
|
||||
const int ld = n + pad, R = M + extra_rows;
|
||||
const float eps = 1e-6f;
|
||||
/* the two arms as one [2M, n] buffer, as the plugin holds them: dense rows, then routed */
|
||||
auto ff = randb((size_t)2 * R * ld, 2.f), x = randb((size_t)R * ld, 4.f);
|
||||
auto wd = gains(n), wr = gains(n), wj = gains(n);
|
||||
auto ff = randb((size_t)2 * R * ld, 2.f), x = randb((size_t)R * ld, 4.f), y0 = randb((size_t)R * ld, 1.f);
|
||||
auto wd = gains(n), wr = gains(n), wj = gains(n), wn = gains(n);
|
||||
uint16_t sc = f2b(0.4375f + (float)(rng() % 64) / 128.f);
|
||||
auto xd = x, xh = x;
|
||||
RadTensor t[7] = { rows(ff.data(), RAD_BF16, M, n, ld), vec(wd.data(), RAD_F32, n),
|
||||
auto xd = x, xh = x, yd = y0, yh = y0;
|
||||
RadTensor t[9] = { rows(ff.data(), RAD_BF16, M, n, ld), vec(wd.data(), RAD_F32, n),
|
||||
rows(ff.data() + (size_t)M * ld, RAD_BF16, M, n, ld), vec(wr.data(), RAD_F32, n),
|
||||
vec(wj.data(), RAD_F32, n), rows(xd.data(), RAD_BF16, M, n, ld), vec(&sc, RAD_BF16, 1) };
|
||||
vec(wj.data(), RAD_F32, n), rows(xd.data(), RAD_BF16, M, n, ld), vec(&sc, RAD_BF16, 1),
|
||||
vec(wn.data(), RAD_F32, n), rows(yd.data(), RAD_BF16, M, n, ld) };
|
||||
RadParam p[4]{}; p[0] = {"M", RAD_P_INT, M}; p[1] = {"n", RAD_P_INT, n};
|
||||
p[2].key = "eps"; p[2].kind = RAD_P_F64; p[2].dval = eps; p[3].key = "dtype"; p[3].kind = RAD_P_STR; p[3].sval = "bf16";
|
||||
RadArgs ar{}; ar.t = t; ar.n_t = 7; ar.p = p; ar.n_p = 4;
|
||||
RadArgs ar{}; ar.t = t; ar.n_t = next ? 9 : 7; ar.p = p; ar.n_p = 4;
|
||||
if (g4k_join_add_scale_bf16(&ar, nullptr) != RAD_OK) { std::puts("join_add_scale: device launch failed"); return 1; }
|
||||
t[5].data = xh.data();
|
||||
t[5].data = xh.data(); t[8].data = yh.data();
|
||||
if (host_row("join_add_scale")->launch(&ar, nullptr) != RAD_OK) { std::puts("join_add_scale: host launch failed"); return 1; }
|
||||
Tally tx;
|
||||
Tally tx, ty;
|
||||
for (int m = 0; m < R; ++m) {
|
||||
const size_t o = (size_t)m * ld;
|
||||
if (m >= M) { for (int i = 0; i < ld; ++i) tx.pad += xd[o + i] != x[o + i]; continue; }
|
||||
for (int i = n; i < ld; ++i) tx.pad += xd[o + i] != x[o + i];
|
||||
if (m >= M) { for (int i = 0; i < ld; ++i) tx.pad += (xd[o + i] != x[o + i]) + (yd[o + i] != y0[o + i]); continue; }
|
||||
for (int i = n; i < ld; ++i) tx.pad += (xd[o + i] != x[o + i]) + (yd[o + i] != y0[o + i]);
|
||||
std::vector<float> dv((size_t)n), rv((size_t)n), sv((size_t)n);
|
||||
for (int i = 0; i < n; ++i) { dv[(size_t)i] = b2f(ff[o + i]); rv[(size_t)i] = b2f(ff[(size_t)M * ld + o + i]); }
|
||||
const auto d1 = chain_norm(dv, wd.data(), n, eps); /* rmsnorm (arm 1) */
|
||||
@@ -166,10 +168,19 @@ static int case_jas(int M, int n, int pad, int extra_rows) {
|
||||
std::vector<uint16_t> cx((size_t)n);
|
||||
for (int i = 0; i < n; ++i) cx[(size_t)i] = f2b(rb(b2f(x[o + i]) + j[(size_t)i]) * b2f(sc)); /* add, mul_weight */
|
||||
cmp(&xd[o], cx.data(), &xh[o], n, tx);
|
||||
if (next) { /* the next rmsnorm */
|
||||
std::vector<float> xv((size_t)n);
|
||||
for (int i = 0; i < n; ++i) xv[(size_t)i] = b2f(cx[(size_t)i]);
|
||||
const auto yn = chain_norm(xv, wn.data(), n, eps);
|
||||
std::vector<uint16_t> cy((size_t)n);
|
||||
for (int i = 0; i < n; ++i) cy[(size_t)i] = f2b(yn[(size_t)i]);
|
||||
cmp(&yd[o], cy.data(), &yh[o], n, ty);
|
||||
}
|
||||
}
|
||||
const bool ok = tx.chain_bad == 0 && tx.host_bad == 0 && tx.pad == 0;
|
||||
std::printf("join_add_scale M %3d n %4d pitch %4d: vs chain %ld differ; vs host arm %ld differ, worst %.1e of the "
|
||||
"row rms; padding touched %ld -> %s\n", M, n, ld, tx.chain_bad, tx.host_diff, tx.worst, tx.pad,
|
||||
const bool ok = tx.chain_bad == 0 && ty.chain_bad == 0 && tx.host_bad == 0 && ty.host_bad == 0 && tx.pad == 0;
|
||||
std::printf("join_add_scale%s M %3d n %4d pitch %4d: vs chain %ld/%ld differ; vs host arm %ld/%ld differ, worst %.1e "
|
||||
"of the row rms; padding touched %ld -> %s\n", next ? "+next" : " ", M, n, ld, tx.chain_bad,
|
||||
ty.chain_bad, tx.host_diff, ty.host_diff, tx.worst > ty.worst ? tx.worst : ty.worst, tx.pad,
|
||||
ok ? "PASS" : "FAIL");
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
@@ -183,6 +194,9 @@ int main() {
|
||||
fails += case_nan(3, 1000, 24, 1) + case_jas(3, 1000, 24, 1);
|
||||
fails += case_nan(2, 6144, 0, 0) + case_jas(2, 6144, 16, 0);
|
||||
fails += case_nan(5, 256, 8, 1) + case_jas(5, 5376, 0, 1);
|
||||
/* and with the next layer's input norm folded in */
|
||||
for (int M : Ms) fails += case_jas(M, 2816, M % 2 ? 64 : 0, 2, true);
|
||||
fails += case_jas(3, 1000, 24, 1, true) + case_jas(2, 6144, 16, 0, true);
|
||||
std::printf("norm joins: %d failure(s)\n", fails);
|
||||
return fails ? 1 : 0;
|
||||
}
|
||||
Reference in new issue
Block a user