Archived
lfm3_bf16: the sandwich joins as norm_add_norm where a library serves it
A layer's post norm, residual add and feed-forward norm are one launch, and so are its feed-forward's post norm, add and the next layer's input norm: 50 launches for the chain's 144 at 24 layers. The op is asked for with rad_op_declared, as gemma4 does; libref and libavx do not carry it and run the rmsnorm / add / rmsnorm chain, as does the last layer's feed-forward. The arm's output has its own buffer, since a join reads it beside x and writes h.
This commit is contained in:
1 parent
09ad42e633
commit
29057b0c6b
3 files changed
+229
-50
No files matched your search
@@ -16,7 +16,9 @@
|
|||||||
* each followed by a SwiGLU feed-forward (w1 the gate, w3 the up, w2 the down). The norms are a
|
* each followed by a SwiGLU feed-forward (w1 the gate, w3 the up, w2 the down). The norms are a
|
||||||
* sandwich: `operator_norm` before the mixer and `post_operator_norm` on its output, before the
|
* sandwich: `operator_norm` before the mixer and `post_operator_norm` on its output, before the
|
||||||
* residual add; `ffn_norm` and `post_ffn_norm` likewise. Every norm is a plain RMSNorm
|
* residual add; `ffn_norm` and `post_ffn_norm` likewise. Every norm is a plain RMSNorm
|
||||||
* (`x_hat * w`), and the final one is `embedding_norm`. At 1.7B: 24 layers (13 conv, 6 full, 5
|
* (`x_hat * w`), and the final one is `embedding_norm`. A post norm, its add and the norm that
|
||||||
|
* reads the sum are one `norm_add_norm` where a library serves it (Gemma's sandwich join; see
|
||||||
|
* declare). At 1.7B: 24 layers (13 conv, 6 full, 5
|
||||||
* sliding with a window of 256), hidden 2048, 16 heads over 4 at head_dim 128, ffn 7680,
|
* sliding with a window of 256), hidden 2048, 16 heads over 4 at head_dim 128, ffn 7680,
|
||||||
* vocabulary 128000 with the lm_head tied to the embedding, conv width 7; theta 1e6 over half the
|
* vocabulary 128000 with the lm_head tied to the embedding, conv width 7; theta 1e6 over half the
|
||||||
* head on the full layers, 1e4 on the sliding ones.
|
* head on the full layers, 1e4 on the sliding ones.
|
||||||
@@ -80,6 +82,9 @@ struct Layer {
|
|||||||
/* the feed-forward: norm, gate|up (w1 | w3), SiLU(gate) * up, down (w2), norm, residual add */
|
/* the feed-forward: norm, gate|up (w1 | w3), SiLU(gate) * up, down (w2), norm, residual add */
|
||||||
Linear gate_up, down;
|
Linear gate_up, down;
|
||||||
rad_op op_ffn_norm = 0, op_act = 0, op_ffn_post_norm = 0, op_ffn_add = 0;
|
rad_op op_ffn_norm = 0, op_act = 0, op_ffn_post_norm = 0, op_ffn_add = 0;
|
||||||
|
/* The sandwich joins, fused: op_join is post_norm + add + ffn_norm, and op_ffn_join is
|
||||||
|
* ffn_post_norm + ffn_add + the next layer's norm, which that layer then does not declare. */
|
||||||
|
rad_op op_join = 0, op_ffn_join = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Model {
|
struct Model {
|
||||||
@@ -89,7 +94,7 @@ struct Model {
|
|||||||
AttnClass full{}, sliding{};
|
AttnClass full{}, sliding{};
|
||||||
rad_kvgroup kv_conv = 0;
|
rad_kvgroup kv_conv = 0;
|
||||||
|
|
||||||
rad_buf b_x = 0, b_h = 0, b_bcx = 0, b_conv_in = 0, b_conv_out = 0;
|
rad_buf b_x = 0, b_h = 0, b_arm = 0, b_bcx = 0, b_conv_in = 0, b_conv_out = 0;
|
||||||
rad_buf b_q = 0, b_k = 0, b_v = 0, b_attn = 0, b_gate = 0, b_gate_up = 0, b_ffn = 0;
|
rad_buf b_q = 0, b_k = 0, b_v = 0, b_attn = 0, b_gate = 0, b_gate_up = 0, b_ffn = 0;
|
||||||
rad_buf b_hout = 0, b_logits = 0;
|
rad_buf b_hout = 0, b_logits = 0;
|
||||||
|
|
||||||
@@ -260,6 +265,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
|||||||
|
|
||||||
m.b_x = decl_b(b, nm.f("x"), RAD_BF16, {g.max_tok, g.n_embd});
|
m.b_x = decl_b(b, nm.f("x"), RAD_BF16, {g.max_tok, g.n_embd});
|
||||||
m.b_h = decl_b(b, nm.f("h"), RAD_BF16, {g.max_tok, g.n_embd});
|
m.b_h = decl_b(b, nm.f("h"), RAD_BF16, {g.max_tok, g.n_embd});
|
||||||
|
/* a mixer's or a feed-forward's output, before its post norm: a join reads it beside x and
|
||||||
|
* writes h, so it is not h */
|
||||||
|
m.b_arm = decl_b(b, nm.f("arm"), RAD_BF16, {g.max_tok, g.n_embd});
|
||||||
m.b_bcx = decl_b(b, nm.f("conv_bcx"), RAD_BF16, {g.max_tok, 3 * g.n_embd});
|
m.b_bcx = decl_b(b, nm.f("conv_bcx"), RAD_BF16, {g.max_tok, 3 * g.n_embd});
|
||||||
m.b_conv_in = decl_b(b, nm.f("conv_in"), RAD_BF16, {g.max_tok, g.n_embd});
|
m.b_conv_in = decl_b(b, nm.f("conv_in"), RAD_BF16, {g.max_tok, g.n_embd});
|
||||||
m.b_conv_out = decl_b(b, nm.f("conv_out"), RAD_BF16, {g.max_tok, g.n_embd});
|
m.b_conv_out = decl_b(b, nm.f("conv_out"), RAD_BF16, {g.max_tok, g.n_embd});
|
||||||
@@ -354,14 +362,42 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
|||||||
return rw(b, rad_decl_op(b, "rope_rows", ps, n, nullptr, 0), {buf}, {buf});
|
return rw(b, rad_decl_op(b, "rope_rows", ps, n, nullptr, 0), {buf}, {buf});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* ---- the sandwich joins. `norm_add_norm` is a post norm, the residual add and the norm that
|
||||||
|
* reads the sum in one launch: post_norm + add + ffn_norm inside a layer, and ffn_post_norm +
|
||||||
|
* add + the next layer's norm between two. It rounds where the chain rounds. The device
|
||||||
|
* libraries serve it and libref and libavx do not, and declaring an op no loaded library
|
||||||
|
* declares is an error, so it is asked for first; without it, or where it does not resolve,
|
||||||
|
* the rmsnorm / add / rmsnorm chain runs. The last layer's feed-forward keeps the chain: the
|
||||||
|
* final norm is on the gathered rows. */
|
||||||
|
const bool has_join = rad_op_declared(b, "norm_add_norm") != 0;
|
||||||
|
auto join = [&](rad_weight w_a, rad_weight w_y) {
|
||||||
|
return rw(b, RAD_OP(b, "norm_add_norm",
|
||||||
|
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
|
||||||
|
RAD_F64("eps", g.eps), RAD_STR("dtype", g.dtype)),
|
||||||
|
RAD_WEIGHTS(w_a, w_y)),
|
||||||
|
{m.b_arm, m.b_x}, {m.b_x, m.b_h});
|
||||||
|
};
|
||||||
|
int n_joins = 0;
|
||||||
|
|
||||||
|
/* Every layer's input norm gain, ahead of the layers: a feed-forward join names the next
|
||||||
|
* layer's, and an op can only name weights already declared. */
|
||||||
|
for (int l = 0; l < (int)m.layers.size(); ++l)
|
||||||
|
RAD_ARCH_TRY(norm_w(&m.layers[(size_t)l].w_norm, "blk.%d.attn_norm.weight", l,
|
||||||
|
nm.ckpt("%s.layers.%d.%s", pre, l, "operator_norm.weight"), g.n_embd));
|
||||||
|
|
||||||
for (int l = 0; l < (int)m.layers.size(); ++l) {
|
for (int l = 0; l < (int)m.layers.size(); ++l) {
|
||||||
Layer& L = m.layers[(size_t)l];
|
Layer& L = m.layers[(size_t)l];
|
||||||
const char* lay = "%s.layers.%d.%s";
|
const char* lay = "%s.layers.%d.%s";
|
||||||
RAD_ARCH_TRY(norm_w(&L.w_norm, "blk.%d.attn_norm.weight", l,
|
|
||||||
nm.ckpt(lay, pre, l, "operator_norm.weight"), g.n_embd));
|
|
||||||
RAD_ARCH_TRY(norm_w(&L.w_post_norm, "blk.%d.attn_post_norm.weight", l,
|
RAD_ARCH_TRY(norm_w(&L.w_post_norm, "blk.%d.attn_post_norm.weight", l,
|
||||||
nm.ckpt(lay, pre, l, "post_operator_norm.weight"), g.n_embd));
|
nm.ckpt(lay, pre, l, "post_operator_norm.weight"), g.n_embd));
|
||||||
L.op_norm = rmsnorm(g.max_tok, g.n_embd, L.w_norm, m.b_x, m.b_h);
|
RAD_ARCH_TRY(norm_w(&L.w_ffn_norm, "blk.%d.ffn_norm.weight", l,
|
||||||
|
nm.ckpt(lay, pre, l, "ffn_norm.weight"), g.n_embd));
|
||||||
|
RAD_ARCH_TRY(norm_w(&L.w_ffn_post_norm, "blk.%d.ffn_post_norm.weight", l,
|
||||||
|
nm.ckpt(lay, pre, l, "post_ffn_norm.weight"), g.n_embd));
|
||||||
|
/* written by the layer before, where its feed-forward join is fused */
|
||||||
|
if (!(l > 0 && m.layers[(size_t)l - 1].op_ffn_join))
|
||||||
|
L.op_norm = rmsnorm(g.max_tok, g.n_embd, L.w_norm, m.b_x, m.b_h);
|
||||||
|
|
||||||
if (L.kind != kConv) {
|
if (L.kind != kConv) {
|
||||||
const AttnClass& c = m.cls(L);
|
const AttnClass& c = m.cls(L);
|
||||||
@@ -439,7 +475,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
|||||||
{m.b_gate}, {m.b_gate});
|
{m.b_gate}, {m.b_gate});
|
||||||
L.op_gate_mul = mul({m.b_gate, m.b_attn}, m.b_attn, g.q_dim());
|
L.op_gate_mul = mul({m.b_gate, m.b_attn}, m.b_attn, g.q_dim());
|
||||||
RAD_ARCH_TRY(linear(L.out, l, "blk.%d.attn_output.weight", g.n_embd, g.q_dim(),
|
RAD_ARCH_TRY(linear(L.out, l, "blk.%d.attn_output.weight", g.n_embd, g.q_dim(),
|
||||||
m.b_attn, m.b_h, {}));
|
m.b_attn, m.b_arm, {}));
|
||||||
} else {
|
} else {
|
||||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, m.kv_conv));
|
RAD_ARCH_TRY(rad_bind_layer_kv(b, l, m.kv_conv));
|
||||||
const char* c = "%s.layers.%d.conv.%s";
|
const char* c = "%s.layers.%d.conv.%s";
|
||||||
@@ -462,24 +498,24 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
|||||||
{m.b_conv_in}, {m.b_conv_out});
|
{m.b_conv_in}, {m.b_conv_out});
|
||||||
L.op_gate_out = mul({m.b_bcx, m.b_conv_out}, m.b_conv_out, g.n_embd);
|
L.op_gate_out = mul({m.b_bcx, m.b_conv_out}, m.b_conv_out, g.n_embd);
|
||||||
RAD_ARCH_TRY(linear(L.conv_out, l, "blk.%d.conv_out.weight", g.n_embd, g.n_embd,
|
RAD_ARCH_TRY(linear(L.conv_out, l, "blk.%d.conv_out.weight", g.n_embd, g.n_embd,
|
||||||
m.b_conv_out, m.b_h, {}));
|
m.b_conv_out, m.b_arm, {}));
|
||||||
|
}
|
||||||
|
/* the sandwich: the mixer's output is normed before it joins the stream, and the
|
||||||
|
* feed-forward reads the norm of the sum */
|
||||||
|
if (has_join) L.op_join = join(L.w_post_norm, L.w_ffn_norm);
|
||||||
|
if (!L.op_join) {
|
||||||
|
L.op_post_norm = rmsnorm(g.max_tok, g.n_embd, L.w_post_norm, m.b_arm, m.b_arm);
|
||||||
|
L.op_add = add(m.b_x, m.b_arm);
|
||||||
|
L.op_ffn_norm = rmsnorm(g.max_tok, g.n_embd, L.w_ffn_norm, m.b_x, m.b_h);
|
||||||
}
|
}
|
||||||
/* the sandwich: the mixer's output is normed before it joins the stream */
|
|
||||||
L.op_post_norm = rmsnorm(g.max_tok, g.n_embd, L.w_post_norm, m.b_h, m.b_h);
|
|
||||||
L.op_add = add(m.b_x, m.b_h);
|
|
||||||
|
|
||||||
/* ---- the feed-forward */
|
/* ---- the feed-forward */
|
||||||
const char* ffn = "%s.layers.%d.feed_forward.%s";
|
const char* ffn = "%s.layers.%d.feed_forward.%s";
|
||||||
RAD_ARCH_TRY(norm_w(&L.w_ffn_norm, "blk.%d.ffn_norm.weight", l,
|
|
||||||
nm.ckpt(lay, pre, l, "ffn_norm.weight"), g.n_embd));
|
|
||||||
RAD_ARCH_TRY(norm_w(&L.w_ffn_post_norm, "blk.%d.ffn_post_norm.weight", l,
|
|
||||||
nm.ckpt(lay, pre, l, "post_ffn_norm.weight"), g.n_embd));
|
|
||||||
RAD_ARCH_TRY(map_concat(b, nm.f("blk.%d.ffn_gate_up.weight", l),
|
RAD_ARCH_TRY(map_concat(b, nm.f("blk.%d.ffn_gate_up.weight", l),
|
||||||
{ nm.ckpt(ffn, pre, l, "w1.weight"),
|
{ nm.ckpt(ffn, pre, l, "w1.weight"),
|
||||||
nm.ckpt(ffn, pre, l, "w3.weight") }));
|
nm.ckpt(ffn, pre, l, "w3.weight") }));
|
||||||
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ffn_down.weight", l),
|
RAD_ARCH_TRY(map_copy(b, nm.f("blk.%d.ffn_down.weight", l),
|
||||||
nm.ckpt(ffn, pre, l, "w2.weight")));
|
nm.ckpt(ffn, pre, l, "w2.weight")));
|
||||||
L.op_ffn_norm = rmsnorm(g.max_tok, g.n_embd, L.w_ffn_norm, m.b_x, m.b_h);
|
|
||||||
RAD_ARCH_TRY(linear(L.gate_up, l, "blk.%d.ffn_gate_up.weight", 2 * g.n_ff, g.n_embd,
|
RAD_ARCH_TRY(linear(L.gate_up, l, "blk.%d.ffn_gate_up.weight", 2 * g.n_ff, g.n_embd,
|
||||||
m.b_h, m.b_gate_up, { g.n_ff, g.n_ff }));
|
m.b_h, m.b_gate_up, { g.n_ff, g.n_ff }));
|
||||||
L.op_act = rw(b, RAD_OP(b, "silu_mul",
|
L.op_act = rw(b, RAD_OP(b, "silu_mul",
|
||||||
@@ -488,9 +524,14 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
|||||||
RAD_NOWEIGHTS),
|
RAD_NOWEIGHTS),
|
||||||
{m.b_gate_up}, {m.b_ffn});
|
{m.b_gate_up}, {m.b_ffn});
|
||||||
RAD_ARCH_TRY(linear(L.down, l, "blk.%d.ffn_down.weight", g.n_embd, g.n_ff, m.b_ffn,
|
RAD_ARCH_TRY(linear(L.down, l, "blk.%d.ffn_down.weight", g.n_embd, g.n_ff, m.b_ffn,
|
||||||
m.b_h, {}));
|
m.b_arm, {}));
|
||||||
L.op_ffn_post_norm = rmsnorm(g.max_tok, g.n_embd, L.w_ffn_post_norm, m.b_h, m.b_h);
|
if (has_join && l + 1 < (int)m.layers.size())
|
||||||
L.op_ffn_add = add(m.b_x, m.b_h);
|
L.op_ffn_join = join(L.w_ffn_post_norm, m.layers[(size_t)l + 1].w_norm);
|
||||||
|
if (!L.op_ffn_join) {
|
||||||
|
L.op_ffn_post_norm = rmsnorm(g.max_tok, g.n_embd, L.w_ffn_post_norm, m.b_arm, m.b_arm);
|
||||||
|
L.op_ffn_add = add(m.b_x, m.b_arm);
|
||||||
|
}
|
||||||
|
n_joins += (L.op_join ? 1 : 0) + (L.op_ffn_join ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The final norm runs on the gathered rows only: it is per token, and those are the rows the
|
/* The final norm runs on the gathered rows only: it is per token, and those are the rows the
|
||||||
@@ -524,6 +565,9 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
|||||||
(long long)m.sliding.window, (long long)(g.n_layer - n_full - n_sliding),
|
(long long)m.sliding.window, (long long)(g.n_layer - n_full - n_sliding),
|
||||||
(long long)m.width, (long long)g.n_head, (long long)g.n_head_kv,
|
(long long)m.width, (long long)g.n_head, (long long)g.n_head_kv,
|
||||||
(long long)g.head_dim, (long long)g.n_ff, (long long)g.n_vocab);
|
(long long)g.head_dim, (long long)g.n_ff, (long long)g.n_vocab);
|
||||||
|
rad_note(b, "sandwich joins: %d of %lld fused (norm_add_norm)%s", n_joins,
|
||||||
|
(long long)(2 * g.n_layer - 1),
|
||||||
|
n_joins == 0 ? " -- not served, the rmsnorm / add / rmsnorm chain" : "");
|
||||||
return RAD_OK;
|
return RAD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -572,7 +616,8 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
|||||||
|
|
||||||
for (int l = 0; l < (int)m.layers.size(); ++l) {
|
for (int l = 0; l < (int)m.layers.size(); ++l) {
|
||||||
const Layer& L = m.layers[(size_t)l];
|
const Layer& L = m.layers[(size_t)l];
|
||||||
RAD_ISSUE(c, L.op_norm, brows(m.b_x, T), RAD_W(L.w_norm), brows(m.b_h, T));
|
if (!(l > 0 && m.layers[(size_t)l - 1].op_ffn_join))
|
||||||
|
RAD_ISSUE(c, L.op_norm, brows(m.b_x, T), RAD_W(L.w_norm), brows(m.b_h, T));
|
||||||
if (L.kind != kConv) {
|
if (L.kind != kConv) {
|
||||||
const rad_kvgroup grp = m.cls(L).kv;
|
const rad_kvgroup grp = m.cls(L).kv;
|
||||||
const RadKVGroupBatch* kv = kv_batch(batch, grp);
|
const RadKVGroupBatch* kv = kv_batch(batch, grp);
|
||||||
@@ -598,7 +643,7 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
|||||||
brows(m.b_attn, T));
|
brows(m.b_attn, T));
|
||||||
RAD_ISSUE(c, L.op_sigmoid, brows(m.b_gate, T), brows(m.b_gate, T));
|
RAD_ISSUE(c, L.op_sigmoid, brows(m.b_gate, T), brows(m.b_gate, T));
|
||||||
RAD_ISSUE(c, L.op_gate_mul, brows(m.b_gate, T), brows(m.b_attn, T), brows(m.b_attn, T));
|
RAD_ISSUE(c, L.op_gate_mul, brows(m.b_gate, T), brows(m.b_attn, T), brows(m.b_attn, T));
|
||||||
lin_step(c, L.out, m.b_attn, m.b_h, T);
|
lin_step(c, L.out, m.b_attn, m.b_arm, T);
|
||||||
} else {
|
} else {
|
||||||
lin_step(c, L.conv_in, m.b_h, m.b_bcx, T);
|
lin_step(c, L.conv_in, m.b_h, m.b_bcx, T);
|
||||||
RAD_ISSUE(c, L.op_gate_in, bcol(m.b_bcx, 0, n, T), bcol(m.b_bcx, 2 * n, n, T),
|
RAD_ISSUE(c, L.op_gate_in, bcol(m.b_bcx, 0, n, T), bcol(m.b_bcx, 2 * n, n, T),
|
||||||
@@ -606,18 +651,29 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
|||||||
conv_step(m, L, c, batch, l);
|
conv_step(m, L, c, batch, l);
|
||||||
RAD_ISSUE(c, L.op_gate_out, bcol(m.b_bcx, n, n, T), brows(m.b_conv_out, T),
|
RAD_ISSUE(c, L.op_gate_out, bcol(m.b_bcx, n, n, T), brows(m.b_conv_out, T),
|
||||||
brows(m.b_conv_out, T));
|
brows(m.b_conv_out, T));
|
||||||
lin_step(c, L.conv_out, m.b_conv_out, m.b_h, T);
|
lin_step(c, L.conv_out, m.b_conv_out, m.b_arm, T);
|
||||||
|
}
|
||||||
|
if (L.op_join) {
|
||||||
|
RAD_ISSUE(c, L.op_join, brows(m.b_arm, T), RAD_W(L.w_post_norm), brows(m.b_x, T),
|
||||||
|
RAD_W(L.w_ffn_norm), brows(m.b_h, T));
|
||||||
|
} else {
|
||||||
|
RAD_ISSUE(c, L.op_post_norm, brows(m.b_arm, T), RAD_W(L.w_post_norm),
|
||||||
|
brows(m.b_arm, T));
|
||||||
|
RAD_ISSUE(c, L.op_add, brows(m.b_x, T), brows(m.b_arm, T), brows(m.b_x, T));
|
||||||
|
RAD_ISSUE(c, L.op_ffn_norm, brows(m.b_x, T), RAD_W(L.w_ffn_norm), brows(m.b_h, T));
|
||||||
}
|
}
|
||||||
RAD_ISSUE(c, L.op_post_norm, brows(m.b_h, T), RAD_W(L.w_post_norm), brows(m.b_h, T));
|
|
||||||
RAD_ISSUE(c, L.op_add, brows(m.b_x, T), brows(m.b_h, T), brows(m.b_x, T));
|
|
||||||
|
|
||||||
RAD_ISSUE(c, L.op_ffn_norm, brows(m.b_x, T), RAD_W(L.w_ffn_norm), brows(m.b_h, T));
|
|
||||||
lin_step(c, L.gate_up, m.b_h, m.b_gate_up, T);
|
lin_step(c, L.gate_up, m.b_h, m.b_gate_up, T);
|
||||||
RAD_ISSUE(c, L.op_act, brows(m.b_gate_up, T), brows(m.b_ffn, T));
|
RAD_ISSUE(c, L.op_act, brows(m.b_gate_up, T), brows(m.b_ffn, T));
|
||||||
lin_step(c, L.down, m.b_ffn, m.b_h, T);
|
lin_step(c, L.down, m.b_ffn, m.b_arm, T);
|
||||||
RAD_ISSUE(c, L.op_ffn_post_norm, brows(m.b_h, T), RAD_W(L.w_ffn_post_norm),
|
if (L.op_ffn_join) {
|
||||||
brows(m.b_h, T));
|
/* y is the next layer's input norm */
|
||||||
RAD_ISSUE(c, L.op_ffn_add, brows(m.b_x, T), brows(m.b_h, T), brows(m.b_x, T));
|
RAD_ISSUE(c, L.op_ffn_join, brows(m.b_arm, T), RAD_W(L.w_ffn_post_norm),
|
||||||
|
brows(m.b_x, T), RAD_W(m.layers[(size_t)l + 1].w_norm), brows(m.b_h, T));
|
||||||
|
} else {
|
||||||
|
RAD_ISSUE(c, L.op_ffn_post_norm, brows(m.b_arm, T), RAD_W(L.w_ffn_post_norm),
|
||||||
|
brows(m.b_arm, T));
|
||||||
|
RAD_ISSUE(c, L.op_ffn_add, brows(m.b_x, T), brows(m.b_arm, T), brows(m.b_x, T));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The lm_head runs only on the rows the sampler asked for; a prefill chunk that finishes no
|
/* The lm_head runs only on the rows the sampler asked for; a prefill chunk that finishes no
|
||||||
|
|||||||
@@ -1915,6 +1915,17 @@ that, for the full layers (window 0) as for the sliding ones. libhalo carries th
|
|||||||
attention rows, and `rope_rows` and `rmsnorm_rows` under their row-form names (launched as
|
attention rows, and `rope_rows` and `rmsnorm_rows` under their row-form names (launched as
|
||||||
`rope` and `rmsnorm`).
|
`rope` and `rmsnorm`).
|
||||||
|
|
||||||
|
The sandwich joins are `norm_add_norm` where a library serves it (libr4d and libhalo; Gemma's
|
||||||
|
join): a layer's post norm, add and feed-forward norm are one launch, and so are its
|
||||||
|
feed-forward's post norm, add and the next layer's input norm. At 24 layers that is 50 launches
|
||||||
|
for the chain's 144. The last layer's feed-forward keeps the chain (the final norm is on the
|
||||||
|
gathered rows), and so does every layer on libref and libavx, which do not carry the op. The
|
||||||
|
fused kernel rounds where the chain rounds. On libr4d a step gives the chain's bytes. On libhalo
|
||||||
|
it does at prefill row counts and not at decode ones: libhalo's `rmsnorm` folds a row's sum of
|
||||||
|
squares over the workgroup width `halo_act_threads` picks for the row count, the join over 256
|
||||||
|
lanes whatever the count, so below 73 rows (at a hidden width under 4096) the two sum in
|
||||||
|
different orders.
|
||||||
|
|
||||||
Checked against the reference's float32 forward on a tiny random LFM3 (conv, full, conv, sliding;
|
Checked against the reference's float32 forward on a tiny random LFM3 (conv, full, conv, sliding;
|
||||||
window 8) at every position of the golden prompt (`tests/lfm3_golden.h`, made by
|
window 8) at every position of the golden prompt (`tests/lfm3_golden.h`, made by
|
||||||
`tests/mklfm3golden.py`), and in the family tests for chunking, batching, decode against prefill,
|
`tests/mklfm3golden.py`), and in the family tests for chunking, batching, decode against prefill,
|
||||||
|
|||||||
+133
-21
@@ -4459,10 +4459,36 @@ std::vector<std::string> lfm3_layer_ops(bool attention) {
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* The mixed step of the four-layer model: two decode rows (a verify of 3 and a plain decode) and
|
||||||
|
* one prefill chunk of 5. `kv` is the caller's, three groups. */
|
||||||
|
RadBatch lfm3_mixed_batch(RadKVGroupBatch* kv) {
|
||||||
|
static const int32_t tok[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||||
|
static const int32_t pos[9] = { 40, 41, 42, 30, 0, 1, 2, 3, 4 };
|
||||||
|
static const int32_t cu[4] = { 0, 3, 4, 9 };
|
||||||
|
static const int32_t ctxl[3] = { 40, 30, 0 };
|
||||||
|
static const int32_t nacc[3] = { 2, 1, 1 };
|
||||||
|
static const int32_t out[3] = { 2, 3, 8 };
|
||||||
|
static int32_t cv_idx[3] = { 4, 5, 6 };
|
||||||
|
const lfm3_bf16::Model& m = lfm3_bf16::g_model[0];
|
||||||
|
kv[0] = RadKVGroupBatch{}; kv[1] = RadKVGroupBatch{}; kv[2] = RadKVGroupBatch{};
|
||||||
|
kv[0].group = m.full.kv;
|
||||||
|
kv[1].group = m.sliding.kv;
|
||||||
|
kv[2].group = m.kv_conv; kv[2].state_index = cv_idx;
|
||||||
|
RadBatch mixed{};
|
||||||
|
mixed.phase = RAD_PHASE_MIXED;
|
||||||
|
mixed.n_tok = 9; mixed.n_seq = 3; mixed.max_q_len = 5;
|
||||||
|
mixed.n_seq_decode = 2; mixed.n_tok_decode = 4;
|
||||||
|
mixed.token_ids = tok; mixed.positions = pos; mixed.cu_seqlens = cu; mixed.ctx_lens = ctxl;
|
||||||
|
mixed.num_accepted = nacc; mixed.n_out = 3; mixed.out_ids = out;
|
||||||
|
mixed.n_kv_groups = 3; mixed.kv = kv;
|
||||||
|
return mixed;
|
||||||
|
}
|
||||||
|
|
||||||
} /* namespace */
|
} /* namespace */
|
||||||
|
|
||||||
TEST(lfm3_declares_three_layer_kinds_with_their_own_rotary_and_window) {
|
TEST(lfm3_declares_three_layer_kinds_with_their_own_rotary_and_window) {
|
||||||
RadBuilder b;
|
RadBuilder b;
|
||||||
|
b.undeclared.insert("norm_add_norm"); /* a host engine's graph; the joins have their own test */
|
||||||
RadModelMeta meta = lfm3_meta();
|
RadModelMeta meta = lfm3_meta();
|
||||||
RadBuildCtx ctx = build_ctx();
|
RadBuildCtx ctx = build_ctx();
|
||||||
CHECK_OK(lfm3_bf16::declare(&b, &meta, &ctx));
|
CHECK_OK(lfm3_bf16::declare(&b, &meta, &ctx));
|
||||||
@@ -4611,27 +4637,8 @@ TEST(lfm3_issues_a_mixed_step_through_both_attention_groups) {
|
|||||||
RadBuildCtx ctx = build_ctx();
|
RadBuildCtx ctx = build_ctx();
|
||||||
CHECK_OK(lfm3_bf16::declare(&b, &meta, &ctx));
|
CHECK_OK(lfm3_bf16::declare(&b, &meta, &ctx));
|
||||||
|
|
||||||
/* Two decode rows (a verify of 3 and a plain decode) and one prefill chunk of 5. */
|
RadKVGroupBatch kv[3];
|
||||||
static const int32_t tok[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
RadBatch mixed = lfm3_mixed_batch(kv);
|
||||||
static const int32_t pos[9] = { 40, 41, 42, 30, 0, 1, 2, 3, 4 };
|
|
||||||
static const int32_t cu[4] = { 0, 3, 4, 9 };
|
|
||||||
static const int32_t ctxl[3] = { 40, 30, 0 };
|
|
||||||
static const int32_t nacc[3] = { 2, 1, 1 };
|
|
||||||
static const int32_t out[3] = { 2, 3, 8 };
|
|
||||||
static int32_t cv_idx[3] = { 4, 5, 6 };
|
|
||||||
const lfm3_bf16::Model& m = lfm3_bf16::g_model[0];
|
|
||||||
RadKVGroupBatch kv[3]{};
|
|
||||||
kv[0].group = m.full.kv;
|
|
||||||
kv[1].group = m.sliding.kv;
|
|
||||||
kv[2].group = m.kv_conv; kv[2].state_index = cv_idx;
|
|
||||||
RadBatch mixed{};
|
|
||||||
mixed.phase = RAD_PHASE_MIXED;
|
|
||||||
mixed.n_tok = 9; mixed.n_seq = 3; mixed.max_q_len = 5;
|
|
||||||
mixed.n_seq_decode = 2; mixed.n_tok_decode = 4;
|
|
||||||
mixed.token_ids = tok; mixed.positions = pos; mixed.cu_seqlens = cu; mixed.ctx_lens = ctxl;
|
|
||||||
mixed.num_accepted = nacc; mixed.n_out = 3; mixed.out_ids = out;
|
|
||||||
mixed.n_kv_groups = 3; mixed.kv = kv;
|
|
||||||
|
|
||||||
RadCtx c{ .b = &b, .batch = &mixed };
|
RadCtx c{ .b = &b, .batch = &mixed };
|
||||||
lfm3_bf16::step(&c, &mixed);
|
lfm3_bf16::step(&c, &mixed);
|
||||||
CHECK_LINT(b, &c);
|
CHECK_LINT(b, &c);
|
||||||
@@ -4660,6 +4667,111 @@ TEST(lfm3_issues_a_mixed_step_through_both_attention_groups) {
|
|||||||
CHECK(!c2.step_fail.empty());
|
CHECK(!c2.step_fail.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* The sandwich joins. Where a library serves `norm_add_norm`, a layer's post norm, add and
|
||||||
|
* feed-forward norm are one launch, and so are its feed-forward's post norm, add and the next
|
||||||
|
* layer's norm, which that layer then neither declares nor issues; the last layer's feed-forward
|
||||||
|
* keeps the chain, since the final norm is on the gathered rows. Where no loaded library declares
|
||||||
|
* the op (a host engine) it is not asked for, and where it is declared and does not resolve the
|
||||||
|
* chain is declared beside it: both issue the chain. */
|
||||||
|
TEST(lfm3_fuses_its_sandwich_joins_where_a_library_serves_them) {
|
||||||
|
/* 0: served; 1: declared, not resolved; 2: not declared */
|
||||||
|
auto run = [](int form, std::vector<std::string>* declared, std::string* note) {
|
||||||
|
RadBuilder b;
|
||||||
|
if (form == 1) b.refuse.insert("norm_add_norm");
|
||||||
|
if (form == 2) b.undeclared.insert("norm_add_norm");
|
||||||
|
RadModelMeta meta = lfm3_meta();
|
||||||
|
RadBuildCtx ctx = build_ctx();
|
||||||
|
CHECK_OK(lfm3_bf16::declare(&b, &meta, &ctx));
|
||||||
|
CHECK(b.asked_undeclared.empty());
|
||||||
|
RadKVGroupBatch kv[3];
|
||||||
|
RadBatch mixed = lfm3_mixed_batch(kv);
|
||||||
|
RadCtx c{ .b = &b, .batch = &mixed };
|
||||||
|
lfm3_bf16::step(&c, &mixed);
|
||||||
|
CHECK_LINT(b, &c);
|
||||||
|
CHECK(c.step_fail.empty());
|
||||||
|
*declared = op_names(b);
|
||||||
|
for (const auto& n : b.notes)
|
||||||
|
if (n.find("sandwich joins") != std::string::npos) *note = n;
|
||||||
|
return issued_ops(b, c);
|
||||||
|
};
|
||||||
|
/* What a step issues: the convolution once per half of the mixed step. */
|
||||||
|
auto want = [](bool fused) {
|
||||||
|
std::vector<std::string> v = { "embed_lookup" };
|
||||||
|
int l = 0;
|
||||||
|
for (bool att : { false, true, true, false }) {
|
||||||
|
if (!fused || l == 0) v.push_back("rmsnorm");
|
||||||
|
if (att)
|
||||||
|
for (const char* s : { "gemm_nt", "gemm_nt", "gemm_nt", "gemm_nt", "rmsnorm_rows",
|
||||||
|
"rmsnorm_rows", "rope_rows", "rope_rows", "kv_store",
|
||||||
|
"attn_paged", "sigmoid", "mul", "gemm_nt" })
|
||||||
|
v.push_back(s);
|
||||||
|
else
|
||||||
|
for (const char* s : { "gemm_nt", "mul", "causal_conv1d", "causal_conv1d", "mul",
|
||||||
|
"gemm_nt" })
|
||||||
|
v.push_back(s);
|
||||||
|
if (fused) v.push_back("norm_add_norm");
|
||||||
|
else for (const char* s : { "rmsnorm", "add", "rmsnorm" }) v.push_back(s);
|
||||||
|
for (const char* s : { "gemm_nt", "silu_mul", "gemm_nt" }) v.push_back(s);
|
||||||
|
if (fused && l < 3) v.push_back("norm_add_norm");
|
||||||
|
else for (const char* s : { "rmsnorm", "add" }) v.push_back(s);
|
||||||
|
++l;
|
||||||
|
}
|
||||||
|
for (const char* s : { "gather_rows", "rmsnorm", "logits_gemm" }) v.push_back(s);
|
||||||
|
return v;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<std::string> decl;
|
||||||
|
std::string note;
|
||||||
|
const std::vector<std::string> fused = run(0, &decl, ¬e);
|
||||||
|
CHECK_EQ(join(fused), join(want(true)));
|
||||||
|
CHECK_EQ(count_issued(fused, "norm_add_norm"), 2 * 4 - 1);
|
||||||
|
CHECK_EQ(count_issued(fused, "add"), 1);
|
||||||
|
CHECK_EQ(count_issued(fused, "rmsnorm"), 1 + 1 + 1); /* layer 0's, the last post norm, final */
|
||||||
|
/* nothing of the chain is declared where a join stands in for it */
|
||||||
|
CHECK_EQ(count_issued(decl, "rmsnorm"), 3);
|
||||||
|
CHECK_EQ(count_issued(decl, "add"), 1);
|
||||||
|
CHECK(note.find("7 of 7 fused") != std::string::npos);
|
||||||
|
|
||||||
|
const std::vector<std::string> refused = run(1, &decl, ¬e);
|
||||||
|
CHECK_EQ(join(refused), join(want(false)));
|
||||||
|
CHECK_EQ(count_issued(decl, "norm_add_norm"), 7); /* asked for, each time */
|
||||||
|
CHECK(note.find("0 of 7 fused") != std::string::npos);
|
||||||
|
|
||||||
|
const std::vector<std::string> host = run(2, &decl, ¬e);
|
||||||
|
CHECK_EQ(join(host), join(want(false)));
|
||||||
|
CHECK_EQ(count_issued(decl, "norm_add_norm"), 0);
|
||||||
|
CHECK(note.find("0 of 7 fused") != std::string::npos);
|
||||||
|
|
||||||
|
/* The join's operands: the arm's output and the stream in, the stream and the next norm out,
|
||||||
|
* with the post norm's gain and the gain of the norm it writes -- the next layer's between
|
||||||
|
* two layers. */
|
||||||
|
{
|
||||||
|
RadBuilder b;
|
||||||
|
RadModelMeta meta = lfm3_meta();
|
||||||
|
RadBuildCtx ctx = build_ctx();
|
||||||
|
CHECK_OK(lfm3_bf16::declare(&b, &meta, &ctx));
|
||||||
|
const lfm3_bf16::Model& m = lfm3_bf16::g_model[0];
|
||||||
|
const RecOp* a = find_op(b, "norm_add_norm", 0);
|
||||||
|
const RecOp* f = find_op(b, "norm_add_norm", 1);
|
||||||
|
REQUIRE(a != nullptr && f != nullptr);
|
||||||
|
for (const RecOp* r : { a, f }) {
|
||||||
|
CHECK_EQ(pint(r, "n"), 64);
|
||||||
|
CHECK_NEAR(pflt(r, "eps"), 1e-5, 1e-9);
|
||||||
|
CHECK_EQ(pstr(r, "dtype"), "bf16");
|
||||||
|
REQUIRE(r->reads.size() == 2 && r->writes.size() == 2 && r->w.size() == 2);
|
||||||
|
CHECK_EQ(r->reads[0], m.b_arm);
|
||||||
|
CHECK_EQ(r->reads[1], m.b_x);
|
||||||
|
CHECK_EQ(r->writes[0], m.b_x);
|
||||||
|
CHECK_EQ(r->writes[1], m.b_h);
|
||||||
|
}
|
||||||
|
CHECK_EQ(a->w[0], m.layers[0].w_post_norm);
|
||||||
|
CHECK_EQ(a->w[1], m.layers[0].w_ffn_norm);
|
||||||
|
CHECK_EQ(f->w[0], m.layers[0].w_ffn_post_norm);
|
||||||
|
CHECK_EQ(f->w[1], m.layers[1].w_norm);
|
||||||
|
CHECK(m.layers[1].op_norm == 0 && m.layers[3].op_ffn_join == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* ==================================================================== LFM2-VL's tower */
|
/* ==================================================================== LFM2-VL's tower */
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user