diff --git a/arch/gemma4/gemma4.cpp b/arch/gemma4/gemma4.cpp index dcd735ba..3831efd9 100644 --- a/arch/gemma4/gemma4.cpp +++ b/arch/gemma4/gemma4.cpp @@ -266,6 +266,9 @@ struct Model { bool moe = false; int64_t n_expert = 0, n_local = 0, expert_base = 0, top_k = 0, n_ffe = 0; int64_t moe_rows = 0; /* tokens a routed pass */ + /* A prefill chunk of 2 * moe_rows tokens or more runs as two slices, the trunk's all-reduces + * on the second lane beside the other slice's compute (trunk_overlap). Set at declare. */ + bool overlap = false; double escale = 1.0, softcap = 0.0; rad_buf b_x = 0, b_h = 0, b_o = 0; @@ -1440,6 +1443,25 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c {m.b_x}, {m.b_dfsrc}); } + /* ---- THE PREFILL OVERLAP (trunk_overlap): where there is a reduction to hide, a routed arm + * whose passes a slice can be a whole number of, and every join fused -- the slices issue the + * fused forms only. On the exact wire alone: nothing here shows the lossy one gives a slice + * the bits it gives the whole. RADIANCE_GEMMA4_OVERLAP=0 runs every chunk whole, the A/B + * switch. The two buffers the second lane reduces in place are taken out of the arena's + * sharing, which the runtime asks of every buffer a second lane touches (rad_buf_concurrent); + * both live across the whole trunk already, so what that costs is only the sharing with the + * drafter's and the head's buffers. */ + const char* ov_env = std::getenv("RADIANCE_GEMMA4_OVERLAP"); + const bool ov_off = ov_env && ov_env[0] == '0'; + m.overlap = W > 1 && m.moe && g.wire_exact && T >= 2 * m.moe_rows && !ov_off; + for (const Layer& y : m.layers) + m.overlap = m.overlap && y.op_join_a && y.op_join_f && y.next_norm && y.op_ar_o && + y.op_ar_f; + if (m.overlap) { + RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_o)); + RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_ff2)); + } + if (m.layers.empty() || !m.layers.back().next_norm) m.op_onorm = rmsnorm(T, E, m.w_onorm, 1.0, m.b_x, m.b_h); m.op_gather = rw(b, RAD_OP(b, "gather_rows", @@ -1504,6 +1526,14 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c (long long)m.n_local, (long long)m.expert_base, (long long)m.n_expert, (long long)m.top_k, (long long)m.n_ffe, how(m.n_gu_fp8), m.n_gu_fp8, (long long)m.n_layer, how(m.n_dn_fp8), m.n_dn_fp8, (long long)m.moe_rows); + if (W > 1) + rad_note(b, "prefill overlap: %s", m.overlap + ? "a chunk of two routed passes or more runs as two slices, each slice's " + "all-reduces on a second stream beside the other's compute" + : ov_off ? "off (RADIANCE_GEMMA4_OVERLAP=0)" + : !g.wire_exact ? "off (the lossy all-reduce wire)" + : T < 2 * m.moe_rows ? "off (a step holds fewer than two routed passes)" + : "off (the sandwich joins are not fused)"); } if (m.kf.rot_pairs < m.kf.hd / 2) rad_note(b, "full layers: PROPORTIONAL rope -- %lld of %lld frequency pairs rotate at " @@ -1515,10 +1545,15 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c /* ============================================================== step */ -static void routed(RadCtx* c, const Model& m, const Layer& y, int64_t T) { +/* The routed arm over rows [first, first + rows) of a T-token step, in passes of m.moe_rows, its + * partial into rows [dst, dst + rows) of the join: [T, 2T) for a whole step, and right after the + * slice's dense rows for a slice of one (trunk_overlap). */ +static void routed(RadCtx* c, const Model& m, const Layer& y, int64_t T, int64_t first, + int64_t rows, int64_t dst) { const int64_t E = m.E, K = m.top_k, NL = m.n_local, NE = m.n_expert; - for (int64_t r0 = 0; r0 < T; r0 += m.moe_rows) { - const int64_t n = (T - r0 < m.moe_rows) ? (T - r0) : m.moe_rows; + const int64_t end = first + rows; + for (int64_t r0 = first; r0 < end; r0 += m.moe_rows) { + const int64_t n = (end - r0 < m.moe_rows) ? (end - r0) : m.moe_rows; int32_t* const counts = rad_route_counts(c, (int)(&y - m.layers.data()), NL); const RadOperand ecnt = counts ? praw(counts, RAD_I32, NL) : brows(m.b_ecnt, NL); RAD_ISSUE_N(c, y.op_rnorm, n, brow_slice(m.b_x, r0, n, E), RAD_W(y.w_rscale), @@ -1592,10 +1627,10 @@ static void routed(RadCtx* c, const Model& m, const Layer& y, int64_t T) { RAD_ISSUE_N(c, y.op_edn, n, brows(m.b_eact, n * K), RAD_WTAB(y.w_edn[0], NL), brows(m.b_sorted, n * K), brows(m.b_eoff, NL + 1), brows(m.b_edn, n * K)); } - /* The routed partial into rows [T + r0, T + r0 + n) of the join. */ + /* The routed partial into its rows of the join. */ RAD_ISSUE_N(c, y.op_gather, n, brows(m.b_edn, n * K), brow_slice(m.b_ew, r0, n, K), brows(m.b_sorted, n * K), RAD_NONE, RAD_NONE, - brow_slice(m.b_ff2, T + r0, n, E)); + brow_slice(m.b_ff2, dst + (r0 - first), n, E)); if (r0 + n >= T) { RadRouting rt{}; rt.expert_ids = ids; @@ -1610,68 +1645,56 @@ static void routed(RadCtx* c, const Model& m, const Layer& y, int64_t T) { } } -static void step(RadCtx* c, const RadBatch* batch) { - const Model& m = g_model[rad_rank(c)]; - if (batch->draft_pass != 0) { - if (!m.have_df) { - rad_step_fail(c, "the batch asks for a drafter pass and no drafter is declared: the " - "container carries no DFlash `draft.` block, or declare ran at " - "max_spec 0"); - return; - } - if (batch->draft_pass > 0) m.df.step(c, batch); - else m.df.context(c, batch); - return; - } - if (batch->enc) { - rad_step_fail(c, "gemma4 declares no encoder; serve this container with --mm-max-patches 0"); - return; - } - const int64_t T = batch->n_tok, E = m.E; - if (T <= 0) return; +/* A layer's attention between its projections and its output projection, over the whole step: + * the norms and the rotaries, the cache write, the attention. */ +static void attend(RadCtx* c, const Model& m, const Layer& y, size_t li, const RadBatch* batch) { + const int64_t T = batch->n_tok; const RadOperand pos = praw(batch->positions, RAD_I32, T); const RadOperand cu = praw(batch->cu_seqlens, RAD_I32, batch->n_seq + 1); + const Kind& k = y.full ? m.kf : m.ks; + const RadKVGroupBatch* kvb = kv_batch(batch, k.kv); + const int32_t* slot = kvb ? kvb->slot_mapping : nullptr; + const int32_t* table = kvb ? kvb->block_table : nullptr; + const int32_t* used = kvb ? kvb->seqused : nullptr; + const RadOperand bt = praw2(table, RAD_I32, batch->n_seq, kvb ? kvb->block_table_pitch : 0); + const RadOperand su = praw(used, RAD_I32, batch->n_seq); - RAD_ISSUE(c, m.op_embed, praw(batch->token_ids, RAD_I32, T), RAD_W(m.w_tok), brows(m.b_x, T)); - if (m.op_embed_ar) - RAD_ISSUE_N(c, m.op_embed_ar, T * E, brows(m.b_x, T), RAD_NONE); + if (!y.full) { + const int64_t qd = k.q(), kvd = k.kv_dim(); + RAD_ISSUE(c, y.op_qknr, brows(m.b_qkv, T), pos, RAD_W(y.w_qn), RAD_W(y.w_kn)); + RAD_ISSUE(c, y.op_store, bcol(m.b_qkv, qd, kvd, T), bcol(m.b_qkv, qd + kvd, kvd, T), + praw(slot, RAD_I32, T), kv_cache(k.kv, (int)li)); + if (y.op_lift) RAD_ISSUE(c, y.op_lift, bcol(m.b_qkv, 0, qd, T), brows(m.b_qs, T)); + RAD_ISSUE_N(c, y.op_attn, batch->max_q_len, + y.op_lift ? brows(m.b_qs, T) : bcol(m.b_qkv, 0, qd, T), + kv_cache(k.kv, (int)li), bt, su, RAD_NONE, RAD_NONE, cu, brows(m.b_atts, T)); + } else { + RAD_ISSUE_N(c, y.op_vnorm, T * k.nkv, brows(m.b_kf, T), RAD_NONE, brows(m.b_vf, T)); + RAD_ISSUE_N(c, y.op_qn, T * k.nh, brows(m.b_qf, T), RAD_W(y.w_qn), brows(m.b_qf, T)); + RAD_ISSUE_N(c, y.op_kn, T * k.nkv, brows(m.b_kf, T), RAD_W(y.w_kn), brows(m.b_kf, T)); + RAD_ISSUE(c, y.op_qrope, brows(m.b_qf, T), pos); + RAD_ISSUE(c, y.op_krope, brows(m.b_kf, T), pos); + RAD_ISSUE(c, y.op_store, brows(m.b_kf, T), brows(m.b_vf, T), praw(slot, RAD_I32, T), + kv_cache(k.kv, (int)li)); + RAD_ISSUE_N(c, y.op_attn, batch->max_q_len, brows(m.b_qf, T), kv_cache(k.kv, (int)li), + bt, su, RAD_NONE, RAD_NONE, cu, brows(m.b_attf, T)); + } +} +/* The trunk over the whole step. */ +static void trunk(RadCtx* c, const Model& m, const RadBatch* batch) { + const int64_t T = batch->n_tok, E = m.E; for (size_t li = 0; li < m.layers.size(); ++li) { const Layer& y = m.layers[li]; - const Kind& k = y.full ? m.kf : m.ks; - const RadKVGroupBatch* kvb = kv_batch(batch, k.kv); - const int32_t* slot = kvb ? kvb->slot_mapping : nullptr; - const int32_t* table = kvb ? kvb->block_table : nullptr; - const int32_t* used = kvb ? kvb->seqused : nullptr; - const RadOperand bt = praw2(table, RAD_I32, batch->n_seq, kvb ? kvb->block_table_pitch : 0); - const RadOperand su = praw(used, RAD_I32, batch->n_seq); - if (y.op_anorm) RAD_ISSUE(c, y.op_anorm, brows(m.b_x, T), RAD_W(y.w_anorm), brows(m.b_h, T)); if (!y.full) { - const int64_t qd = k.q(), kvd = k.kv_dim(); RAD_ISSUE(c, y.op_qkv, brows(m.b_h, T), RAD_W(y.w_qkv), brows(m.b_qkv, T)); - RAD_ISSUE(c, y.op_qknr, brows(m.b_qkv, T), pos, RAD_W(y.w_qn), RAD_W(y.w_kn)); - RAD_ISSUE(c, y.op_store, bcol(m.b_qkv, qd, kvd, T), bcol(m.b_qkv, qd + kvd, kvd, T), - praw(slot, RAD_I32, T), kv_cache(k.kv, (int)li)); - if (y.op_lift) RAD_ISSUE(c, y.op_lift, bcol(m.b_qkv, 0, qd, T), brows(m.b_qs, T)); - RAD_ISSUE_N(c, y.op_attn, batch->max_q_len, - y.op_lift ? brows(m.b_qs, T) : bcol(m.b_qkv, 0, qd, T), - kv_cache(k.kv, (int)li), bt, su, RAD_NONE, RAD_NONE, cu, brows(m.b_atts, T)); - RAD_ISSUE(c, y.op_o, brows(m.b_atts, T), RAD_W(y.w_o), brows(m.b_o, T)); } else { RAD_ISSUE(c, y.op_q, brows(m.b_h, T), RAD_W(y.w_q), brows(m.b_qf, T)); RAD_ISSUE(c, y.op_k, brows(m.b_h, T), RAD_W(y.w_k), brows(m.b_kf, T)); - RAD_ISSUE_N(c, y.op_vnorm, T * k.nkv, brows(m.b_kf, T), RAD_NONE, brows(m.b_vf, T)); - RAD_ISSUE_N(c, y.op_qn, T * k.nh, brows(m.b_qf, T), RAD_W(y.w_qn), brows(m.b_qf, T)); - RAD_ISSUE_N(c, y.op_kn, T * k.nkv, brows(m.b_kf, T), RAD_W(y.w_kn), brows(m.b_kf, T)); - RAD_ISSUE(c, y.op_qrope, brows(m.b_qf, T), pos); - RAD_ISSUE(c, y.op_krope, brows(m.b_kf, T), pos); - RAD_ISSUE(c, y.op_store, brows(m.b_kf, T), brows(m.b_vf, T), praw(slot, RAD_I32, T), - kv_cache(k.kv, (int)li)); - RAD_ISSUE_N(c, y.op_attn, batch->max_q_len, brows(m.b_qf, T), kv_cache(k.kv, (int)li), - bt, su, RAD_NONE, RAD_NONE, cu, brows(m.b_attf, T)); - RAD_ISSUE(c, y.op_o, brows(m.b_attf, T), RAD_W(y.w_o), brows(m.b_o, T)); } + attend(c, m, y, li, batch); + RAD_ISSUE(c, y.op_o, brows(y.full ? m.b_attf : m.b_atts, T), RAD_W(y.w_o), brows(m.b_o, T)); if (y.op_ar_o) RAD_ISSUE_N(c, y.op_ar_o, T * E, brows(m.b_o, T), RAD_NONE); if (y.op_join_a) { RAD_ISSUE(c, y.op_join_a, brows(m.b_o, T), RAD_W(y.w_apost), brows(m.b_x, T), @@ -1688,7 +1711,7 @@ static void step(RadCtx* c, const RadBatch* batch) { brows(m.b_act, T)); RAD_ISSUE(c, y.op_dn, brows(m.b_act, T), RAD_W(y.w_dn), brows(m.b_ff2, T)); if (m.moe) { - routed(c, m, y, T); + routed(c, m, y, T, 0, T, T); if (y.op_ar_f) RAD_ISSUE_N(c, y.op_ar_f, 2 * T * E, brows(m.b_ff2, 2 * T), RAD_NONE); if (y.op_join_f) { RAD_ISSUE(c, y.op_join_f, brows(m.b_ff2, T), RAD_W(y.w_fpost1), @@ -1718,6 +1741,172 @@ static void step(RadCtx* c, const RadBatch* batch) { bcol(m.b_dfsrc, (int64_t)t * E, E, T)); } } +} + +/* ============================================================== the prefill overlap */ +/* A PREFILL CHUNK IN TWO SLICES, each slice's all-reduces on the second lane (rad_lane) beside the + * other slice's compute. On gemma-4-26B-A4B at tp 4 on 4x R9700 the reductions were 47% of a + * 20K-token prefill under --profile-ops: a layer reduces T x E after attention and both arms' 2T x + * E after the feed-forward, at ~16.5 GB/s a rank on the link, against ~8 ms of compute a layer at + * the average chunk. Run whole, the two take turns. Cut in two, a slice's reduction runs while the + * other slice computes: + * + * lane 0 o(A) o(B) . nan(A) ff(A) . nan(B) ff(B) . jas(A) qkv(A) . jas(B) qkv(B) attn o(A) .. + * lane 1 ar_o(A) ar_o(B) ar_f(A) ar_f(B) + * + * ATTENTION RUNS WHOLE: it reads every row's keys, and its batch bounds describe the whole step. + * The rest is a row at a time -- the projections, the joins, both arms -- and so is the reduction, + * which folds a word in fp32 in ascending rank order whichever message carries it. + * + * THE SAME KERNELS, SO THE SAME BITS (core/runtime/ctx.h, the second lane's rule FOUR). A cut is + * a whole number of routed passes from the step's first row, so the routed arm makes exactly the + * passes the whole step makes, and both slices are at least one pass (2048 rows): above every row + * band the projections, the joins, the activation and the reduction have -- the GEMM's at 16 and + * 64, the reduction's one-shot edge at 524288 elements. None of those kernels' row results + * depends on the row count, so the step's output is the whole step's. + * + * THE ORDER. Every reduction rides lane 1 behind a join from lane 0, which holds its producer; + * lane 0 waits for lane 1 before the reduction's consumer. A join orders behind EVERYTHING its + * lane has issued, so each wait goes in after the lane-0 work that does not need it and before + * the next reduction is issued -- which is what fixes the issue order below. The embedding's + * reduction stays on lane 0, ahead of everything lane 1 is first joined behind, so the + * collectives keep one order on every rank; the runtime joins lane 1 back at the end of a step. */ + +/* The first row of slice B, or 0 for a step that runs whole: the whole number of routed passes + * nearest half the step. Both slices are then at least one pass. */ +static int64_t split_at(const Model& m, int64_t T) { + if (!m.overlap || T < 2 * m.moe_rows) return 0; + const int64_t P = m.moe_rows; + return P * ((T + P) / (2 * P)); +} + +/* Rows [r0, r0 + n) of a layer's projections: q|k|v on a sliding layer, q and k on a full one. */ +static void project(RadCtx* c, const Model& m, const Layer& y, int64_t r0, int64_t n) { + const RadOperand h = brow_slice(m.b_h, r0, n, m.E); + if (!y.full) { + RAD_ISSUE_N(c, y.op_qkv, n, h, RAD_W(y.w_qkv), + brow_slice(m.b_qkv, r0, n, m.ks.q() + 2 * m.ks.kv_dim())); + } else { + RAD_ISSUE_N(c, y.op_q, n, h, RAD_W(y.w_q), brow_slice(m.b_qf, r0, n, m.kf.q())); + RAD_ISSUE_N(c, y.op_k, n, h, RAD_W(y.w_k), brow_slice(m.b_kf, r0, n, m.kf.kv_dim())); + } +} + +/* Rows [r0, r0 + n) of the output projection. */ +static void out_proj(RadCtx* c, const Model& m, const Layer& y, int64_t r0, int64_t n) { + const int64_t q = y.full ? m.kf.q() : m.ks.q(); + RAD_ISSUE_N(c, y.op_o, n, brow_slice(y.full ? m.b_attf : m.b_atts, r0, n, q), RAD_W(y.w_o), + brow_slice(m.b_o, r0, n, m.E)); +} + +/* Rows [r0, r0 + n) from the attention join to the arms' partials, once those rows' attention + * reduction is in. The slice's partials land in rows [2 r0, 2 r0 + 2n) of the join, dense then + * routed, so its feed-forward reduction is one run. */ +static void arms(RadCtx* c, const Model& m, const Layer& y, int64_t T, int64_t r0, int64_t n) { + const int64_t E = m.E, F = m.n_ff; + RAD_ISSUE_N(c, y.op_join_a, n, brow_slice(m.b_o, r0, n, E), RAD_W(y.w_apost), + brow_slice(m.b_x, r0, n, E), RAD_W(y.w_fnorm), brow_slice(m.b_h, r0, n, E)); + RAD_ISSUE_N(c, y.op_gu, n, brow_slice(m.b_h, r0, n, E), RAD_W(y.w_gu), + brow_slice(m.b_gu, r0, n, 2 * F)); + RAD_ISSUE_N(c, y.op_act, n, bcol_at(m.b_gu, r0, 2 * F, 0, F, n), + bcol_at(m.b_gu, r0, 2 * F, F, F, n), brow_slice(m.b_act, r0, n, F)); + RAD_ISSUE_N(c, y.op_dn, n, brow_slice(m.b_act, r0, n, F), RAD_W(y.w_dn), + brow_slice(m.b_ff2, 2 * r0, n, E)); + routed(c, m, y, T, r0, n, 2 * r0 + n); +} + +/* Rows [r0, r0 + n) of layer li's feed-forward join, once their reduction is in -- with the next + * norm it writes -- and the drafter's taps of those rows. */ +static void join_ff(RadCtx* c, const Model& m, size_t li, int64_t r0, int64_t n) { + const Layer& y = m.layers[li]; + const int64_t E = m.E; + RAD_ISSUE_N(c, y.op_join_f, n, brow_slice(m.b_ff2, 2 * r0, n, E), RAD_W(y.w_fpost1), + brow_slice(m.b_ff2, 2 * r0 + n, n, E), RAD_W(y.w_fpost2), RAD_W(y.w_fpost), + brow_slice(m.b_x, r0, n, E), RAD_W(y.w_scalar), RAD_W(y.w_next), + brow_slice(m.b_h, r0, n, E)); + for (int t = 0; t < m.n_taps && y.op_tap; ++t) { + if (m.tap_layer[t] != (int)li) continue; + RAD_ISSUE_N(c, y.op_tap, n, brow_slice(m.b_x, r0, n, E), + bcol_at(m.b_dfsrc, r0, (int64_t)m.n_taps * E, (int64_t)t * E, E, n)); + } +} + +static void trunk_overlap(RadCtx* c, const Model& m, const RadBatch* batch, int64_t cut) { + const int64_t T = batch->n_tok, E = m.E; + const int64_t a0 = 0, an = cut, b0 = cut, bn = T - cut; + bool lanes_ok = true; + /* An all-reduce in place over `rows` rows of `buf` from `row`, on lane 1 behind everything + * lane 0 has issued -- its producer among it. */ + auto reduce = [&](rad_op op, rad_buf buf, int64_t row, int64_t rows) { + lanes_ok = lanes_ok && rad_lane_join(c, 0, 1) == RAD_OK && rad_lane(c, 1) == RAD_OK; + RAD_ISSUE_N(c, op, rows * E, brow_slice(buf, row, rows, E), RAD_NONE); + lanes_ok = rad_lane(c, 0) == RAD_OK && lanes_ok; + }; + /* Lane 0 behind every reduction issued so far. */ + auto join_back = [&] { lanes_ok = rad_lane_join(c, 1, 0) == RAD_OK && lanes_ok; }; + + const size_t L = m.layers.size(); + for (size_t li = 0; li < L; ++li) { + const Layer& y = m.layers[li]; + if (li == 0) { + RAD_ISSUE(c, y.op_anorm, brows(m.b_x, T), RAD_W(y.w_anorm), brows(m.b_h, T)); + project(c, m, y, a0, an); + project(c, m, y, b0, bn); + } else { + join_ff(c, m, li - 1, a0, an); /* its reduction was waited for last layer */ + project(c, m, y, a0, an); + join_back(); /* the last layer's feed-forward, slice B */ + join_ff(c, m, li - 1, b0, bn); + project(c, m, y, b0, bn); + } + attend(c, m, y, li, batch); + out_proj(c, m, y, a0, an); + reduce(y.op_ar_o, m.b_o, a0, an); + out_proj(c, m, y, b0, bn); + join_back(); /* attention, slice A */ + reduce(y.op_ar_o, m.b_o, b0, bn); + arms(c, m, y, T, a0, an); + join_back(); /* attention, slice B */ + reduce(y.op_ar_f, m.b_ff2, 2 * a0, 2 * an); + arms(c, m, y, T, b0, bn); + join_back(); /* the feed-forward, slice A */ + reduce(y.op_ar_f, m.b_ff2, 2 * b0, 2 * bn); + } + join_ff(c, m, L - 1, a0, an); + join_back(); /* the last feed-forward, slice B */ + join_ff(c, m, L - 1, b0, bn); + if (!lanes_ok) + rad_step_fail(c, "gemma4: the prefill overlap could not order its second lane against " + "the first (RADIANCE_GEMMA4_OVERLAP=0 runs every chunk on one)"); +} + +static void step(RadCtx* c, const RadBatch* batch) { + const Model& m = g_model[rad_rank(c)]; + if (batch->draft_pass != 0) { + if (!m.have_df) { + rad_step_fail(c, "the batch asks for a drafter pass and no drafter is declared: the " + "container carries no DFlash `draft.` block, or declare ran at " + "max_spec 0"); + return; + } + if (batch->draft_pass > 0) m.df.step(c, batch); + else m.df.context(c, batch); + return; + } + if (batch->enc) { + rad_step_fail(c, "gemma4 declares no encoder; serve this container with --mm-max-patches 0"); + return; + } + const int64_t T = batch->n_tok, E = m.E; + if (T <= 0) return; + + RAD_ISSUE(c, m.op_embed, praw(batch->token_ids, RAD_I32, T), RAD_W(m.w_tok), brows(m.b_x, T)); + if (m.op_embed_ar) + RAD_ISSUE_N(c, m.op_embed_ar, T * E, brows(m.b_x, T), RAD_NONE); + + const int64_t cut = split_at(m, T); + if (cut > 0) trunk_overlap(c, m, batch, cut); + else trunk(c, m, batch); if (m.op_onorm) RAD_ISSUE(c, m.op_onorm, brows(m.b_x, T), RAD_W(m.w_onorm), brows(m.b_h, T)); if (batch->n_out > 0) { diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index 423f8fca..30338aed 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -492,8 +492,8 @@ The readings that stay plausible when wrong: pre_feedforward_layernorm_2's output. `per_expert_scale` multiplies the weight AFTER the renormalised top-k. - **The arms' two post-norms are per arm**, so under tensor parallel each arm's partial sum is - reduced before its norm. The partials share one [2T, hidden] buffer, dense rows first, so one - all-reduce carries both. + reduced before its norm. The partials share one [2T, hidden] buffer, dense rows first -- each + slice's own run of it when a prefill chunk is cut in two -- so one all-reduce carries both. - **The embedding scale is sqrt(hidden) rounded to bf16**: 53.0, not 53.066. The drafter reads the same scaled embedding. - **The sliding layers' KV group is `RAD_KV_FULL`**, with the window on the attention op. A @@ -511,6 +511,18 @@ The drafter's head is vocab-sharded with the trunk's: each rank's top-2 are all- `pair_argmax` picks the winner (two a rank, because a row-interleaved `all_gather` moves 16-byte words and one pair is 8 bytes). +**The prefill overlap.** At more than one rank, a prefill chunk of two routed passes or more (4096 +tokens, at the 2048-token pass) runs as two slices, and the trunk's all-reduces ride the runtime's +second lane beside the other slice's compute: slice A's attention reduction runs under slice B's +output projection, slice B's under slice A's two arms, and so on through the layer. Attention runs +whole, since it reads every row's keys. The cut is a whole number of routed passes and both slices +are above every row band the sliced kernels have, so the step issues the same kernels over the +same rows and its output is the whole step's, bit for bit. On 4x R9700 the reductions were 47% of +a 20K-token prefill under `--profile-ops`; a model of the measured times puts the overlap at up to +a quarter off an unprofiled prefill. The per-op profile syncs every op, so it cannot show it -- +time a prefill without `--profile-ops`. `RADIANCE_GEMMA4_OVERLAP=0` runs every chunk whole, and the +startup note says which. A decode step never takes the second lane. + **The draft head in fp8.** The drafter's head is the widest read on the draft path: the target's bf16 table, 352 MiB a rank at tp 4, every draft pass. A container converted with the line `dflash.draft_head.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16` (the three `-df` recipes diff --git a/docs/GUIDE.md b/docs/GUIDE.md index 54b9f5c1..2ff9fe8d 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1860,6 +1860,7 @@ rad-kbench --home $RADIANCE_HOME --max-cases 1 # are the kernels correct at al | `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (§4.5) | | `RADIANCE_G4K_WMMA` | `0` keeps `gemma4k`'s prefill attention on its scalar kernel instead of the WMMA one. An A/B switch and a way out if the matrix path misbehaves on a card; read once | | `RADIANCE_GEMMA4_FUSE` | `0` makes `arch/gemma4` declare the chains instead of `gemma4k`'s fusions: the rmsnorm / add chain for the sandwich joins (`norm_add_norm`, `join_add_scale`), `embed_lookup` + `mul` for `expert_scale`, and a `cast` lifting q for `attn_paged` for `attn_paged_qrow`. An A/B switch; read at declare | +| `RADIANCE_GEMMA4_OVERLAP` | `0` runs every `arch/gemma4` prefill chunk whole, its all-reduces on the compute stream, instead of as two slices whose all-reduces ride a second stream beside the other slice's compute. An A/B switch; read at declare | There is also a family of instruments for engine development — `RADIANCE_DEBUG_*` (residual probes, idle scrubs, routing and issue dumps), `RADIANCE_OP_ORACLE*` (named ops checked against `libref` on diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 43c641ef..1a34c3ed 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -71,7 +72,10 @@ struct RecIssue { rad_op op = 0; std::vector opd; int64_t n = 0; + int lane = 0; /* the lane it was issued on (rad_lane) */ }; +/* A rad_lane_join: `to` behind everything `from` issued before issue number `at`. */ +struct RecJoin { size_t at = 0; int from = 0, to = 0; }; } /* namespace */ @@ -91,6 +95,9 @@ struct RadBuilder { /* The buffer the plugin named as this step's logits (rad_declare_logits). */ rad_buf logits = 0; + /* The buffers named to rad_buf_concurrent: the ones a second lane touches. */ + std::vector concurrent; + /* The vision tower's declaration (rad_declare_encoder), when the plugin made one, and the * audio encoder's. */ RadEncoderDecl encoder{}; @@ -113,6 +120,8 @@ struct RadCtx { std::vector issues; std::vector routed_layers; std::string step_fail; + int lane = 0; /* where the next issue goes */ + std::vector joins; }; /* -------------------------------------------------------------------- the declare-side ABI */ @@ -212,8 +221,12 @@ extern "C" int rad_declare_logits(RadBuilder* b, rad_buf logits) { return RAD_OK; } -/* Recorded as calls and nothing else: no test here reads a lane or a drafter declaration. */ -extern "C" int rad_buf_concurrent(RadBuilder* b, rad_buf buf) { (void)b; (void)buf; return RAD_OK; } +/* Recorded: the gemma4 prefill overlap names the buffers its second lane reduces in place. */ +extern "C" int rad_buf_concurrent(RadBuilder* b, rad_buf buf) { + if (!b || !buf) return RAD_E_INVAL; + b->concurrent.push_back(buf); + return RAD_OK; +} extern "C" int rad_weight_shard_span(RadBuilder* b, rad_weight w, int64_t lo, int64_t hi) { if (!b || w == 0 || w > b->weights.size()) return RAD_E_INVAL; if (lo < 0 || hi <= lo) return RAD_E_INVAL; @@ -285,9 +298,21 @@ extern "C" int rad_issue(RadCtx* c, rad_op op, const RadOperand* opd, int n_opd, r.op = op; r.n = n; for (int i = 0; i < n_opd; ++i) r.opd.push_back(opd[i]); + r.lane = c->lane; c->issues.push_back(r); return RAD_OK; } +/* The second lane, recorded: the lane each issue went to, and where each join sits among them. */ +extern "C" int rad_lane(RadCtx* c, int lane) { + if (lane != 0 && lane != 1) return RAD_E_INVAL; + c->lane = lane; + return RAD_OK; +} +extern "C" int rad_lane_join(RadCtx* c, int from, int to) { + if (from < 0 || from > 1 || to < 0 || to > 1) return RAD_E_INVAL; + c->joins.push_back({ c->issues.size(), from, to }); + return RAD_OK; +} /* A block that refuses the batch records the reason; the recorder is what the tests read. */ extern "C" int rad_step_fail(RadCtx* c, const char* what) { c->step_fail = what ? what : ""; @@ -3805,3 +3830,183 @@ TEST(gemma4_dflash_head_reads_the_fp8_copy_where_the_container_holds_one) { CHECK_EQ(head_issues, 1); } } + +/* ============ gemma4: the prefill overlap -- two slices, the reductions on lane 1 ============ */ + +namespace { + +/* The elements [lo, hi) of its buffer an operand can reach, whole rows at the declared width. False + * for an operand that is not a buffer. */ +bool reach(const RadBuilder& b, const RadOperand& o, rad_buf* buf, int64_t* lo, int64_t* hi) { + if (o.kind != RAD_OPK_BUF || o.handle == 0 || o.handle > b.bufs.size()) return false; + const RadBufDecl& d = b.bufs[o.handle - 1].d; + int64_t pitch = 1; + for (uint32_t i = 1; i < d.rank; ++i) pitch *= d.shape[i]; + *buf = o.handle; + *lo = o.offset - o.offset % pitch; + *hi = *lo + (o.rows > 0 ? o.rows : d.shape[0]) * pitch; + return true; +} + +/* Whether issue a runs before issue b (a < b): on one lane by issue order, across the two only + * through a join from a's lane to b's issued between them. */ +bool ordered(const RadCtx& c, size_t a, size_t b) { + const int la = c.issues[a].lane, lb = c.issues[b].lane; + if (la == lb) return true; + for (const RecJoin& j : c.joins) + if (j.from == la && j.to == lb && j.at > a && j.at <= b) return true; + return false; +} + +/* Pairs of issues on different lanes that reach a common element with nothing ordering them. + * Every operand is taken as read and written, which can only over-count. */ +int races(const RadBuilder& b, const RadCtx& c) { + int n = 0; + for (size_t i = 0; i < c.issues.size(); ++i) + for (size_t j = i + 1; j < c.issues.size(); ++j) { + if (ordered(c, i, j)) continue; + bool meet = false; + for (const RadOperand& p : c.issues[i].opd) + for (const RadOperand& q : c.issues[j].opd) { + rad_buf bp = 0, bq = 0; + int64_t pl = 0, ph = 0, ql = 0, qh = 0; + if (reach(b, p, &bp, &pl, &ph) && reach(b, q, &bq, &ql, &qh) && bp == bq && + pl < qh && ql < ph) + meet = true; + } + n += meet ? 1 : 0; + } + return n; +} + +/* One prompt of T tokens, its last sampled. */ +RadBatch one_prompt(int64_t T, const int32_t* out) { + RadBatch b{}; + b.phase = RAD_PHASE_PREFILL; + b.n_tok = T; + b.n_seq = 1; + b.max_q_len = T; + b.max_ctx_len = T; + b.n_out = 1; + b.out_ids = out; + return b; +} + +/* Rows issued, summed by op: a slice's n is its rows, a whole step's RAD_N_BATCH is T. */ +std::map rows_by_op(const RadBuilder& b, const RadCtx& c, int64_t T) { + std::map w; + for (const auto& i : c.issues) { + const RecOp* o = b.op_of(i.op); + w[o ? o->op : ""] += i.n < 0 ? T : i.n; + } + return w; +} + +/* The n of each issue of one op, in issue order. */ +std::vector ns_of(const RadBuilder& b, const RadCtx& c, const char* op) { + std::vector v; + for (const auto& i : c.issues) { + const RecOp* o = b.op_of(i.op); + if (o && o->op == op) v.push_back(i.n); + } + return v; +} + +bool noted(const RadBuilder& b, const char* text) { + for (const auto& n : b.notes) + if (n.find(text) != std::string::npos) return true; + return false; +} + +} /* namespace */ + +/* A PREFILL CHUNK IN TWO SLICES (arch/gemma4, trunk_overlap). What a recorder can check is the + * order and the work: every reduction rides lane 1 behind its producer and ahead of its consumer, + * nothing else does, the cut is a whole number of routed passes, and the slices issue exactly the + * rows the whole step issues -- the same routed passes, the drafter's taps -- which is what makes + * the output the whole step's. A chunk under two passes, and a decode step, run whole on one + * lane. */ +TEST(a_gemma4_prefill_chunk_runs_as_two_slices_with_its_reductions_on_the_second_lane) { + const std::vector keys = g4df_keys(), vals = g4df_vals(); + const int64_t E = 2816, L = 6; + auto model = [&](RadBuilder& b, bool off) { + RadModelMeta meta = gemma4_moe_meta(); + meta.n_kv = (int)keys.size(); + meta.kv_key = keys.data(); + meta.kv_val = vals.data(); + RadBuildCtx ctx = build_ctx(0, 4); + ctx.max_tok = 8192; + ctx.max_spec = 15; + if (off) setenv("RADIANCE_GEMMA4_OVERLAP", "0", 1); + const int st = gemma4::declare(&b, &meta, &ctx); + if (off) unsetenv("RADIANCE_GEMMA4_OVERLAP"); + return st; + }; + auto run = [&](RadBuilder& b, RadCtx& c, RadBatch& batch) { + c.b = &b; c.rank = 0; c.world = 4; c.batch = &batch; + gemma4::step(&c, &batch); + }; + + struct Case { int64_t T, cut; }; + for (const Case k : { Case{8192, 4096}, Case{6144, 4096}, Case{6143, 2048}, Case{4096, 2048}, + Case{4095, 0} }) { + const int32_t out[1] = { (int32_t)(k.T - 1) }; + RadBuilder b; + CHECK_OK(model(b, false)); + CHECK(noted(b, "prefill overlap: a chunk of two routed passes")); + CHECK_EQ((int)b.concurrent.size(), 2); + for (const char* name : { "o", "ff2" }) + CHECK(std::count(b.concurrent.begin(), b.concurrent.end(), + (rad_buf)buf_named(b, name)) == 1); + RadBatch batch = one_prompt(k.T, out); + RadCtx c; + run(b, c, batch); + CHECK(c.step_fail.empty()); + CHECK_EQ(c.lane, 0); + CHECK_EQ(races(b, c), 0); + + std::vector lane1; + for (const auto& i : c.issues) { + if (i.lane != 1) continue; + const RecOp* o = b.op_of(i.op); + CHECK(o && o->op == "all_reduce"); + lane1.push_back(i.n); + } + if (k.cut == 0) { + CHECK(lane1.empty()); + CHECK(c.joins.empty()); + } else { + REQUIRE(lane1.size() == (size_t)(4 * L)); + for (int64_t l = 0; l < L; ++l) { + CHECK_EQ(lane1[4 * l + 0], k.cut * E); /* attention, slice A */ + CHECK_EQ(lane1[4 * l + 1], (k.T - k.cut) * E); /* attention, slice B */ + CHECK_EQ(lane1[4 * l + 2], 2 * k.cut * E); /* both arms, slice A */ + CHECK_EQ(lane1[4 * l + 3], 2 * (k.T - k.cut) * E); /* both arms, slice B */ + } + } + + /* The whole step, with the switch off: the same rows by op, the same routed passes. */ + RadBuilder w; + CHECK_OK(model(w, true)); + CHECK(noted(w, "prefill overlap: off (RADIANCE_GEMMA4_OVERLAP=0)")); + CHECK(w.concurrent.empty()); + RadBatch wbatch = one_prompt(k.T, out); + RadCtx wc; + run(w, wc, wbatch); + CHECK(wc.step_fail.empty()); + CHECK(wc.joins.empty()); + CHECK(rows_by_op(b, c, k.T) == rows_by_op(w, wc, k.T)); + CHECK(ns_of(b, c, "moe_gather") == ns_of(w, wc, "moe_gather")); + CHECK_EQ((int)ns_of(b, c, "cast").size(), k.cut ? 6 : 3); /* three taps, a slice each */ + } + + /* A decode step never leaves lane 0. */ + RadBuilder b; + CHECK_OK(model(b, false)); + RadBatch d = decode_batch(); + RadCtx c; + run(b, c, d); + CHECK(c.step_fail.empty()); + CHECK(c.joins.empty()); + for (const auto& i : c.issues) CHECK_EQ(i.lane, 0); +}