diff --git a/arch/common/rad_block_attn_gated_fp8.h b/arch/common/rad_block_attn_gated_fp8.h index 7627b58e..47a01531 100644 --- a/arch/common/rad_block_attn_gated_fp8.h +++ b/arch/common/rad_block_attn_gated_fp8.h @@ -157,6 +157,21 @@ struct AttnGatedFP8 { /* Set before declare: the `overlap` issue_ar's collective is declared at, 0 for none. */ int ar_overlap = 0; rad_op op_ar_ov = 0; + + /* Whether step() with `own_ar` false runs the dense path a piece at a time over a caller's + * normed pieces (ArPieces::in_ready): one prefill sequence in two pieces, each the chunk a + * span of 1 makes, on the unfused prologue with one rotary component per token (every token's + * components agree, so plane 0 of an M-RoPE batch's positions is each token's position) and + * the gate quantised by gate_quant_fp8. */ + bool takes_pieces(const RadBatch* batch, const ArPieces* pieces) const { + const int64_t T = batch->n_tok; + const RadKVGroupBatch* kvb = kv_batch(batch, kv); + return pieces && pieces->in_ready && pieces->cu && pieces->cuts(T) && + T <= 2 * pieces->rows && !op_add && !op_fill && op_gq && !op_sig && !qsa_sel && + batch->n_seq == 1 && batch->phase == RAD_PHASE_PREFILL && + !(T <= fused_rows) && batch->positions && !rope_mixed(batch) && + kvb && kvb->slot_mapping && kvb->block_table && kvb->seqused; + } }; inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, @@ -477,6 +492,51 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar, const int64_t T = batch->n_tok; + /* One prefill sequence in two pieces over the caller's normed input (takes_pieces): the + * projections, prologue, cache store, attention, gate and out projection of the first piece, + * then the second's, so the first piece's reduction runs beside all of the second piece's + * work. Each piece is the chunk a span of 1 makes: its rows, its positions, its slots, and an + * attention over its own query bounds (ArPieces::cu) that sees the cache up to its own end -- + * the first piece's position after its last row, the step's seqused for the second, which + * reads the first piece's keys from the cache as a later chunk would. */ + if (!own_ar && takes_pieces(batch, pieces)) { + const int64_t R = pieces->rows, hd = g.head_dim, qw = g.q_dim(); + const int64_t qgw = g.n_head * 2 * hd, kvw = g.n_head_kv * hd; + const int64_t tpitch = kvb->block_table_pitch; + for (int64_t p = 0; p < 2; ++p) { + const int64_t r0 = p == 0 ? 0 : R; + const int64_t n = p == 0 ? R : T - R; + const int32_t* pos = (batch->rope_pos ? batch->rope_pos : batch->positions) + r0; + qg.step(c, w.h, w.qg, T, r0, n); + kp.step(c, w.h, w.k, T, r0, n); + vp.step(c, w.h, w.v, T, r0, n); + RAD_ISSUE_N(c, op_q_norm, n * g.n_head, + bcol_at(w.qg, r0, qgw, 0, hd, n), RAD_W(w_q_norm), + brow_slice(w.q, r0, n, qw)); + RAD_ISSUE_N(c, op_k_norm, n * g.n_head_kv, + brow_slice(w.k, r0, n, kvw), RAD_W(w_k_norm), + brow_slice(w.k, r0, n, kvw)); + RAD_ISSUE_N(c, op_rope_q, n, brow_slice(w.q, r0, n, qw), praw(pos, RAD_I32, n)); + RAD_ISSUE_N(c, op_rope_k, n, brow_slice(w.k, r0, n, kvw), praw(pos, RAD_I32, n)); + RAD_ISSUE_N(c, op_kv_store, n, + brow_slice(w.k, r0, n, kvw), brow_slice(w.v, r0, n, kvw), + praw(slot + r0, RAD_I32, n), kv_cache(kv, layer)); + RAD_ISSUE_N(c, op_attn, n, + brows(w.q, T), kv_cache(kv, layer), praw2(table, RAD_I32, 1, tpitch), + praw(p == 0 ? batch->positions + R : used, RAD_I32, 1), + RAD_NONE, RAD_NONE, praw(pieces->cu + p, RAD_I32, 2), + brows(w.attn.x, T)); + RAD_ISSUE_N(c, op_gq, n * g.n_head, + bcol_at(w.qg, r0, qgw, hd, hd, n), brow_slice(w.attn.x, r0, n, qw), + brow_slice(w.attn.cq(), r0, n, qw), + brow_slice(w.attn.cs(), r0, n, fp8_blocks(qw)), + brow_slice(w.attn.x, r0, n, qw)); + o.step(c, w.attn, w.h.x, T, r0, n); + if (p == 0) pieces->hand(r0, n); + } + return; + } + if (!ext_in) nq_h.step(c, T); if (op_fill) { /* Attention-zero rank: zero output, then the reduce and the residual. */ diff --git a/arch/common/rad_block_gdn_fp8.h b/arch/common/rad_block_gdn_fp8.h index 7a8f0d34..78e08c58 100644 --- a/arch/common/rad_block_gdn_fp8.h +++ b/arch/common/rad_block_gdn_fp8.h @@ -139,6 +139,13 @@ struct GdnFP8 { * trailing add, which would read the delta before the reduction. */ void step(RadCtx* c, const RadBatch* batch, bool own_ar = true, ArPieces* pieces = nullptr) const; + /* Whether step() with `own_ar` false runs the whole block a piece at a time over `pieces` + * (its piece path), asked by a caller that must know before it commits to a schedule. */ + bool takes_pieces(const RadBatch* batch, const ArPieces* pieces) const { + return pieces && pieces->cu && pieces->cuts(batch->n_tok) && + batch->n_tok <= 2 * pieces->rows && (ext_in || pieces->in_ready) && !op_add && + batch->n_seq == 1 && batch->phase == RAD_PHASE_PREFILL; + } /* Whether a step of T rows issues the all-reduce itself, asked by a caller before taking it * over; and that all-reduce over rows [r0, r0 + rows) of the output. */ bool issues_ar(int64_t T) const { return op_ar && !ar_taken(g, T, ar_out, ar_out_take); } @@ -389,10 +396,10 @@ inline void GdnFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar, * piece the chunk a span of 1 makes (the projection over its rows; the convolution, solve and * scan over its bounds, with the state handed on through the cache; the gated norm, quantiser * and out projection), so the first piece's reduction runs beside all of the second piece's - * work. The second piece reads the history the first left in the convolution slot: a spanned - * step is past the prompt's first interval, so its context length is never zero. */ - if (!own_ar && pieces && pieces->cu && pieces->cuts(T) && T <= 2 * pieces->rows && ext_in && - !op_add && S == 1 && batch->phase == RAD_PHASE_PREFILL) { + * work. The second piece reads the history the first left in the convolution slot, so each + * piece passes its own context length, its first row's position: the step's would tell the + * second piece of a prompt's first chunk that it has none. */ + if (!own_ar && takes_pieces(batch, pieces)) { const int64_t in_w = conv_dim + v_dim; for (int64_t p = 0; p < 2; ++p) { const int64_t r0 = p * pieces->rows; @@ -405,7 +412,7 @@ inline void GdnFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar, bcol(w.in.x, 0, conv_dim, T), RAD_W(w_conv), RAD_NONE, RAD_W(w_a_log), RAD_W(w_dt_bias), kv_cache(kv_conv, layer), praw(cu, RAD_I32, 2), bcol(w.ab, 0, H, T), bcol(w.ab, H, H, T), - praw(cv_idx, RAD_I32, 1), praw(batch->ctx_lens, RAD_I32, 1), + praw(cv_idx, RAD_I32, 1), praw(batch->positions + r0, RAD_I32, 1), brows(w.q, T), brows(w.k, T), brows(w.v, T), brows(w.gdec, T), brows(w.beta, T)); RAD_ISSUE_N(c, op_kkt, n, diff --git a/arch/common/rad_block_mlp_fp8.h b/arch/common/rad_block_mlp_fp8.h index aea3c883..e9460530 100644 --- a/arch/common/rad_block_mlp_fp8.h +++ b/arch/common/rad_block_mlp_fp8.h @@ -75,6 +75,20 @@ struct MlpFP8 { void step_rows(RadCtx* c, int64_t T) const { slice(c, T, 0, T); } /* One row slice of the whole block; `rows <= 0` means the rest of the step. */ void slice(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const; + /* The slice in its parts, for a caller that runs the norm and the tail elsewhere (qwen35_fp8's + * pipelined prefill puts both on the second lane): the projections from the normed input to + * the delta in `w.h.x`, then the all-reduce and add this block issues itself. */ + void body(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const; + void tail(RadCtx* c, int64_t r0, int64_t rows) const; + /* The block's all-reduce over rows [r0, r0 + rows) of the output, at `overlap` where declared + * (a caller that runs it beside other work). */ + void issue_ar(RadCtx* c, int64_t r0, int64_t rows) const { + RAD_ISSUE_N(c, op_ar_ov ? op_ar_ov : op_ar, rows * g.n_embd, + brow_slice(w.h.x, r0, rows, g.n_embd), RAD_NONE); + } + /* Set before declare: the `overlap` issue_ar's collective is declared at, 0 for none. */ + int ar_overlap = 0; + rad_op op_ar_ov = 0; }; inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire, @@ -129,6 +143,17 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co RAD_INT("min_bytes", g.wire_min_bytes)), RAD_NOWEIGHTS), {w.h.x}, {w.h.x}); + /* The same collective for a caller that runs it beside other work (issue_ar): at `overlap` 1 + * it gets the rows that hold fewer CUs while waiting on the link. */ + if (g.world > 1 && ar_overlap > 0) + op_ar_ov = rw(b, RAD_OP(b, "all_reduce", + RAD_PARAMS(RAD_INT("world_size", g.world), + RAD_RANGE("numel", g.n_embd, g.max_tok * g.n_embd), + RAD_STR("dtype", g.dtype), RAD_INT("exact", g.wire_exact), + RAD_INT("min_bytes", g.wire_min_bytes), + RAD_INT("overlap", ar_overlap)), + RAD_NOWEIGHTS), + {w.h.x}, {w.h.x}); /* Only when the next block's norm will not add (rad_fp8.h: NormQuantFP8). */ if (add_out) @@ -149,6 +174,11 @@ inline int MlpFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, co inline void MlpFP8::slice(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const { if (rows <= 0) rows = T - r0; nq_h.step(c, T, r0, rows); + body(c, T, r0, rows); + tail(c, r0, rows); +} + +inline void MlpFP8::body(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const { if (op_gated && rows <= gated_rows) { RAD_ISSUE_N(c, op_gated, rows, brow_slice(w.h.q, r0, rows, g.n_embd), brow_slice(w.h.s, r0, rows, g.n_embd / RAD_FP8_BLOCK), RAD_W(gate_up.w), @@ -160,6 +190,9 @@ inline void MlpFP8::slice(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const gq_ffn.step(c, w.gate_up, T, r0, rows); } down.step(c, w.ffn, w.h.x, T, r0, rows); +} + +inline void MlpFP8::tail(RadCtx* c, int64_t r0, int64_t rows) const { if (op_ar && !(ar_out && ar_fused_ok(g, rows))) RAD_ISSUE_N(c, op_ar, rows * g.n_embd, brow_slice(w.h.x, r0, rows, g.n_embd), RAD_NONE); if (op_add) diff --git a/arch/common/rad_fp8.h b/arch/common/rad_fp8.h index 6f0cca5c..864efd60 100644 --- a/arch/common/rad_fp8.h +++ b/arch/common/rad_fp8.h @@ -290,6 +290,10 @@ struct ArPieces { * chunk a span of 1 makes, the state carried through the cache) and hand off the first * piece's reduction before the second piece's projection. Null for any other step. */ const int32_t* cu = nullptr; + /* The caller normed each piece's input itself (qwen35_fp8's pipelined prefill, on the second + * lane behind the previous block's piece), so a block with its own norm takes the piece path + * as one with an external input does and skips that norm. */ + bool in_ready = false; bool cuts(int64_t T) const { return done && rows > 0 && T > rows; } void hand(int64_t r0, int64_t n) { done(u, r0, n); ++handed; } }; @@ -389,6 +393,9 @@ struct NormQuantFP8 { } /* Asked by the preceding block before issuing its own collective. */ bool takes_ar(int64_t T) const { return pick(T) != op; } + /* The plain op whatever the wire, for a caller that issued the preceding all-reduce itself + * (qwen35_fp8's pipelined prefill, which runs it narrow beside compute). */ + void step_plain(RadCtx* c, int64_t T, int64_t r0 = 0, int64_t rows = 0) const; }; inline int NormQuantFP8::declare(RadBuilder* b, const Geom& g, rad_weight gain, rad_buf x, @@ -470,6 +477,15 @@ inline int NormQuantFP8::declare(RadBuilder* b, const Geom& g, rad_weight gain, * `brows`). Slicing lets a block run in passes (the routed feed-forward). Each slice must land in * the same bucket-table band as the whole (core/runtime/ctx.h), or a different kernel with a * different summation order would run. */ +inline void NormQuantFP8::step_plain(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const { + if (rows <= 0) rows = T - r0; + RAD_ISSUE_N(c, op, rows, brow_slice(src, r0, rows, n), + res ? brow_slice(res, r0, rows, n) : RAD_NONE, RAD_W(w), + brow_slice(a.q, r0, rows, n), + brow_slice(a.s, r0, rows, n / RAD_FP8_BLOCK), + brow_slice(a.x, r0, rows, n)); +} + inline void NormQuantFP8::step(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const { if (rows <= 0) rows = T - r0; RAD_ISSUE_N(c, pick(rows), rows, brow_slice(src, r0, rows, n), diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index ddfe97b5..42e0e698 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -88,6 +88,7 @@ #include #include +#include #include #include @@ -193,10 +194,28 @@ struct Model { MediaRows mrows{}; std::vector layers; + + /* The pipelined prefill (step_pipelined): on where declare allowed it; the bounds buffer it + * cuts a step by, rewritten by every pipelined step from the pinned table `pipe_tbl` of + * {0, pipe_rows(T), T} for every T, made once; and whether rank 0 has said it pipelines. */ + bool pipe_on = false; + rad_buf b_pipe_cu = 0; + int32_t* pipe_tbl = nullptr; + bool pipe_tbl_tried = false; + bool pipe_noted = false; + bool pipe_serial_noted = false; }; static Model g_model[MAX_RANKS]; +#if !QWEN35_MOE +/* The narrowest step the pipelined prefill takes, and its first piece: half the step rounded up + * to 64 rows, so both pieces sit in one bucket band (core/runtime/ctx.h) and the second is never + * wider than the first. */ +static const int64_t kPipeMinRows = 1024; +static int64_t pipe_rows(int64_t T) { return ((T + 1) / 2 + 63) / 64 * 64; } +#endif + /* Checks the container's statement of its format, since every mismatch is silent: a * `weight_block_size` of [64,64] against kernels that shift by 7 reads the right bytes with the * wrong gain, and e5m2 decoded as e4m3 gives merely implausible numbers. A container that states @@ -862,6 +881,27 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c RAD_STR("dtype", g.dtype), RAD_INT("exact", 1)), RAD_NOWEIGHTS), {m.b_x}, {m.b_x}); +#if !QWEN35_MOE + /* The pipelined prefill (step_pipelined), at two ranks wherever a step can be wide enough; + * RADIANCE_QWEN35_PIPE=0 turns it off. Its second lane touches the residual stream, the normed + * input in both forms, the taps and the bounds, so those leave the sharing pool + * (Ctx::lane1_); the bounds also because no declared op reads them, which would leave a + * transient dead before the kernels that take them raw. */ + { + const char* e = std::getenv("RADIANCE_QWEN35_PIPE"); + m.pipe_on = g.world == 2 && g.max_tok >= kPipeMinRows && !(e && std::strcmp(e, "0") == 0); + } + if (m.pipe_on) { + RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_x)); + RAD_ARCH_TRY(rad_buf_concurrent(b, m.a_h.x)); + RAD_ARCH_TRY(rad_buf_concurrent(b, m.a_h.q)); + RAD_ARCH_TRY(rad_buf_concurrent(b, m.a_h.s)); + if (m.b_dfsrc) RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_dfsrc)); + m.b_pipe_cu = decl_b(b, nm.f("pipe_cu"), RAD_I32, {4}); + RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_pipe_cu)); + } +#endif + /* ---- the layers. */ /* The rope table's fill, declared before the layers that read it. If it does not resolve, * `aw.cos_sin` is cleared and every attention block uses its unfused norm + rope pair. Its band @@ -904,6 +944,13 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c for (int64_t l = 0; l < m.n_main; ++l) { Layer& lay = m.layers[(size_t)l]; lay.full = is_full_attention(l, interval) ? 1 : 0; +#if !QWEN35_MOE + /* The pipelined prefill runs every block's all-reduce beside the other piece's work, so + * each declares its collective a second time at `overlap` 1, a launch that leaves the CUs + * to that work (the lane rows of libr4d/r4d_rows.cpp); the fused all-reduce and norm + * would hold the card while it waits on the link. */ + lay.attn.ar_overlap = lay.gdn.ar_overlap = lay.mlp.ar_overlap = m.pipe_on ? 1 : 0; +#endif const LayerFold f = layer_fold(g, l, m.n_main, m.tap_layer, m.n_taps, m.op_embed_ar != 0); @@ -1138,6 +1185,206 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c return RAD_OK; } +#if !QWEN35_MOE +/* ============================== A prefill step in two pieces ============================== + * + * At two ranks every layer of a prefill chunk makes two all-reduces, each folded into the next + * block's norm (layer_fold) and each in a hard chain on one stream: block -> all-reduce + norm -> + * block. On a 2048-token chunk they are about a fifth of the step, nearly all of it the link, with + * nothing beside them. + * + * A step of one prefill sequence of at least kPipeMinRows tokens is cut into two pieces of about + * half (pipe_rows), each the chunk a narrower batch makes. Every block runs a piece at a time on + * the first lane, and the work between two blocks runs a piece at a time on the second: the + * block's all-reduce at `overlap` 1 (a narrow launch that leaves the CUs to the compute beside it; + * the fused all-reduce and norm would hold its blocks on the card while it waits on the link and + * slow the GEMM beside it by a third), then the next block's plain norm, and after a feed-forward + * that keeps its add, the add, the drafter taps and the final norm. Measured on Qwen3.8-27B at two + * R9700s: a 41k-token prompt 9.0 -> 8.4 s. + * + * mixer the delta net and the dense attention run whole a piece at a time and hand the + * first piece off behind its out projection (ArPieces), so its boundary runs beside + * the second piece's mixer; + * ffn its first piece runs beside the mixer's second boundary, its second beside its own + * first boundary; the next mixer's first piece runs beside its second. + * + * A piece computes the bytes the narrower chunk does: every op is per row, both pieces sit in the + * band of a whole step (pipe_rows), the all-reduce reduces within a row, the delta net carries its + * state through the cache as between two chunks, and each attention piece sees the cache up to its + * own end. The narrow all-reduce and the plain norm write the fused op's bytes, so a pipelined + * step is bit for bit the serial one (Qwen's KL recordings compare equal). + * + * The second lane's requirements (Ctx::lane1_) hold: every collective of the step is on it, the + * embedding's included; none carries a device-wide arrival counter (the all-reduce's sequence + * counters order its own launches, which one lane keeps in order); and the buffers it touches left + * the sharing pool at declare. */ + +template +static void pipe_on1(RadCtx* c, const F& issue) { + rad_lane(c, 1); + rad_lane_join(c, 0, 1); + issue(); + rad_lane(c, 0); +} +/* Lane 0 waits for everything issued on lane 1 so far. */ +static void pipe_wait1(RadCtx* c) { rad_lane_join(c, 1, 0); } + +/* The work after block `s` (2l the mixer of layer l, 2l + 1 its feed-forward) for rows + * [r0, r0 + n). */ +static void pipe_boundary(RadCtx* c, const Model& m, int64_t T, int64_t s, int64_t r0, int64_t n) { + const Geom& g = m.g; + const int64_t li = s / 2; + const Layer& l = m.layers[(size_t)li]; + if (s % 2 == 0) { + if (l.full) l.attn.issue_ar(c, r0, n); + else l.gdn.issue_ar(c, r0, n); + l.mlp.nq_h.step_plain(c, T, r0, n); + return; + } + l.mlp.issue_ar(c, r0, n); + if (l.mlp.op_add) + RAD_ISSUE_N(c, l.mlp.op_add, n, brow_slice(m.b_x, r0, n, g.n_embd), + brow_slice(m.a_h.x, r0, n, g.n_embd), brow_slice(m.b_x, r0, n, g.n_embd)); + for (int t = 0; t < m.n_taps; ++t) { + if (m.tap_layer[t] != (int)li) continue; + RAD_ISSUE_N(c, m.op_tap, n, brow_slice(m.b_x, r0, n, g.n_embd), + bcol_at(m.b_dfsrc, r0, (int64_t)m.n_taps * g.n_embd, (int64_t)t * g.n_embd, + g.n_embd, n)); + } + if (li + 1 < (int64_t)m.layers.size()) { + const Layer& nx = m.layers[(size_t)li + 1]; + (nx.full ? nx.attn.nq_h : nx.gdn.nq_h).step_plain(c, T, r0, n); + } else { + RAD_ISSUE_N(c, m.op_final_norm, n, brow_slice(m.b_x, r0, n, g.n_embd), RAD_W(m.w_out_norm), + brow_slice(m.a_h.x, r0, n, g.n_embd)); + } +} + +/* A mixer's first piece is done (ArPieces): lane 0 takes the previous block's second boundary + * before the second piece, and lane 1 this block's first behind it. */ +struct PipeHand { + RadCtx* c = nullptr; + const Model* m = nullptr; + int64_t T = 0, s = 0; +}; +static void pipe_hand(void* u, int64_t r0, int64_t n) { + const PipeHand& h = *(const PipeHand*)u; + pipe_wait1(h.c); + pipe_on1(h.c, [&] { pipe_boundary(h.c, *h.m, h.T, h.s, r0, n); }); +} + +/* The bounds of this step's pieces on the card, {0, pipe_rows(T), T}, if the step is pipelined; + * null for a serial step. Copied on the step's stream ahead of every layer from the pinned table, + * whose rows never change, so a captured step replays the same copy. */ +static const int32_t* pipe_ready(RadCtx* c, const RadBatch* batch, Model& m) { + const int64_t T = batch->n_tok; + if (!m.pipe_on || !m.b_pipe_cu || T < kPipeMinRows || T > m.g.max_tok || + batch->phase != RAD_PHASE_PREFILL) + return nullptr; + /* A prefill step wide enough that runs serially says why, once, so a deployment that never + * pipelines does not look like one that gains nothing from it. */ + auto serial = [&](const char* why) -> const int32_t* { + if (!m.pipe_serial_noted && m.g.rank == 0) + fprintf(stderr, "radiance: qwen35: a %lld-row prefill step runs serially: %s\n", + (long long)T, why); + m.pipe_serial_noted = true; + return nullptr; + }; + if (batch->n_seq != 1 || batch->n_seq_decode != 0) return serial("more than one sequence"); + if (!batch->positions) return serial("no positions"); + if (rope_mixed(batch) || batch->n_mm_rows > 0) return serial("media rows in the step"); + if (dbg_resid_on()) return serial("residual debugging is on"); + int32_t* d = (int32_t*)rad_buf_ptr(c, m.b_pipe_cu); + if (!d) return nullptr; + const int64_t R = pipe_rows(T); + ArPieces pc{}; + pc.rows = R; pc.done = pipe_hand; pc.cu = d; pc.in_ready = true; + for (const Layer& l : m.layers) { + if (!(l.full ? l.attn.takes_pieces(batch, &pc) : l.gdn.takes_pieces(batch, &pc))) + return serial(l.full ? "an attention block cannot run in pieces" + : "a delta-net block cannot run in pieces"); + /* Every boundary issues its block's all-reduce itself, narrow, then the plain norm. */ + if (!(l.full ? l.attn.op_ar_ov : l.gdn.op_ar_ov) || !l.mlp.op_ar_ov) + return serial("a block declares no overlap all-reduce"); + } + if (!m.pipe_tbl_tried) { + m.pipe_tbl_tried = true; + const int64_t n = m.g.max_tok + 1; + m.pipe_tbl = (int32_t*)rad_dev_alloc(3 * n * (int64_t)sizeof(int32_t), RAD_MEM_HOST_PINNED); + if (m.pipe_tbl) { + for (int64_t t = 0; t < n; ++t) { + m.pipe_tbl[3 * t] = 0; + m.pipe_tbl[3 * t + 1] = (int32_t)pipe_rows(t); + m.pipe_tbl[3 * t + 2] = (int32_t)t; + } + } + } + if (!m.pipe_tbl) return serial("no pinned host memory for the piece bounds"); + if (rad_memcpy_async(d, m.pipe_tbl + 3 * T, 3 * (int64_t)sizeof(int32_t), rad_stream(c)) != + RAD_OK) + return nullptr; + return d; +} + +static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int32_t* pcu) { + const Geom& g = m.g; + const int64_t T = batch->n_tok, R = pipe_rows(T); + const int64_t L = (int64_t)m.layers.size(); + if (!m.pipe_noted) { + m.pipe_noted = true; + if (g.rank == 0) + fprintf(stderr, "radiance: qwen35: prefill steps of one sequence from %lld rows are " + "pipelined: two pieces, block boundaries on the second lane\n", + (long long)kPipeMinRows); + } + + RAD_ISSUE(c, m.op_embed, + praw(batch->token_ids, RAD_I32, T), RAD_W(m.w_tok), brows(m.b_x, T)); + /* The embedding's reduction and layer 0's norm over the whole step, on lane 1 with every + * other collective (the norm takes the reduction where the wire is exact). */ + pipe_on1(c, [&] { + if (m.op_embed_ar && !ar_wire_is_exact(g, T)) + RAD_ISSUE_N(c, m.op_embed_ar, T * g.n_embd, brows(m.b_x, T), RAD_NONE); + if (L > 0) (m.layers[0].full ? m.layers[0].attn.nq_h : m.layers[0].gdn.nq_h).step(c, T); + }); + pipe_wait1(c); + if (m.op_rope_cs && T <= qk_fuse_rows(g) && !rope_mixed(batch)) + RAD_ISSUE_N(c, m.op_rope_cs, T, rope_pos1(batch, T), RAD_B(m.b_rope_cs)); + + for (int64_t li = 0; li < L; ++li) { + const Layer& l = m.layers[(size_t)li]; + PipeHand h{c, &m, T, 2 * li}; + ArPieces pc{}; + pc.rows = R; pc.done = pipe_hand; pc.u = &h; pc.cu = pcu; pc.in_ready = true; + if (l.full) l.attn.step(c, batch, false, &pc); + else l.gdn.step(c, batch, false, &pc); + if (pc.handed != 1) { + rad_step_fail(c, "qwen35: a mixer did not run its two pieces in a pipelined prefill " + "step (pipe_ready and the block's takes_pieces disagree)"); + return; + } + pipe_wait1(c); + pipe_on1(c, [&] { pipe_boundary(c, m, T, 2 * li, R, T - R); }); + l.mlp.body(c, T, 0, R); + pipe_wait1(c); + pipe_on1(c, [&] { pipe_boundary(c, m, T, 2 * li + 1, 0, R); }); + l.mlp.body(c, T, R, T - R); + pipe_wait1(c); + pipe_on1(c, [&] { pipe_boundary(c, m, T, 2 * li + 1, R, T - R); }); + } + pipe_wait1(c); + + if (batch->n_out > 0) { + RAD_ISSUE_N(c, m.op_gather, batch->n_out, + brows(m.a_h.x, T), praw(batch->out_ids, RAD_I32, batch->n_out), + brows(m.b_hout, batch->n_out)); + RAD_ISSUE_N(c, m.op_logits, batch->n_out, + brows(m.b_hout, batch->n_out), RAD_W(m.w_lm_head), + brows(m.b_logits, batch->n_out)); + } +} +#endif + static void step(RadCtx* c, const RadBatch* batch) { Model& m = g_model[rad_rank(c)]; @@ -1176,6 +1423,13 @@ static void step(RadCtx* c, const RadBatch* batch) { return; } +#if !QWEN35_MOE + if (const int32_t* pcu = pipe_ready(c, batch, m)) { + step_pipelined(c, batch, m, pcu); + return; + } +#endif + RAD_ISSUE(c, m.op_embed, praw(batch->token_ids, RAD_I32, batch->n_tok), RAD_W(m.w_tok), brows(m.b_x, batch->n_tok)); diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index c44abaa3..090d03f6 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -9122,6 +9122,90 @@ TEST(a_4096_budget_without_a_span_is_the_pinned_graph_and_steps) { } } +/* RADIANCE_QWEN35_PIPE=0 turns the pipelined prefill off: the two-rank build is then the serial + * one, graph and steps, as pinned before the pipeline existed. */ +TEST(qwen35_pipe_off_is_the_serial_graph_and_steps) { + MetaKV q35 = q35fp8_meta(); + setenv("RADIANCE_QWEN35_PIPE", "0", 1); + RadBuilder b; + RadBuildCtx ctx = spec_ctx(1, 2, 3); + ctx.max_tok = 4096; + ctx.max_seqs = 8; + ctx.max_ctx = 200000; + const int rc = stderr_of_rc([&] { return qwen35_fp8::declare(&b, q35.meta(), &ctx); }); + unsetenv("RADIANCE_QWEN35_PIPE"); + REQUIRE_EQ(rc, RAD_OK); + CHECK(!qwen35_fp8::g_model[1].pipe_on); + CHECK_EQ(graph_hash(b), 0xc55656a96b3db1ceull); + CHECK_EQ(pinned_steps(b, qwen35_fp8::step, 1, 2), 0xc03bdef23d685cc5ull); +} + +/* A Qwen3.5/3.8 prefill step of one sequence at two ranks runs in two pieces of 1024 rows + * (step_pipelined): every collective is on the second lane, the layers' all-reduces a piece at a + * time at `overlap` 1, the embedding's whole (standalone, or folded into layer 0's norm where the + * wire is exact, as this geometry's is); no other fused all-reduce + norm is issued; the delta net + * and the attention run twice, each piece over its own bounds; the DFlash2 taps are copied a piece + * at a time. A prompt's first chunk and a later one alike. One rank runs serially. */ +TEST(qwen35_a_wide_prefill_runs_in_two_pieces_with_its_boundaries_on_the_second_lane) { + MetaKV mk = with_dflash2(q35fp8_meta()); + for (int world : { 1, 2 }) + for (int rank = 0; rank < world; ++rank) + for (int32_t pctx : { 0, 4096 }) { + RadBuilder b; + RadBuildCtx ctx = spec_ctx(rank, world, 7); + ctx.max_tok = 2048; + REQUIRE_EQ(stderr_of_rc([&] { return qwen35_fp8::declare(&b, mk.meta(), &ctx); }), + RAD_OK); + const qwen35_fp8::Model& m = qwen35_fp8::g_model[rank]; + CHECK_EQ(m.pipe_on, world == 2); + CHECK(m.n_taps > 0); + const auto pf = kit(b, {}, { 2048 }, pctx); + const auto v = run_passes(b, qwen35_fp8::step, { pf.get() }, rank, world); + CHECK_PASSES(b, v); + const RadCtx& c = *v[0]; + CHECK(c.step_fail.empty()); + const int64_t E = m.g.n_embd, piece = 1024 * E; + int ar = 0, ar_lane0 = 0, ar_piece = 0, ar_overlap = 0, arn = 0, arn_lane0 = 0; + int conv = 0, attn = 0, wide_attn = 0, taps = 0, wide_taps = 0, gdn = 0; + for (const auto& l : m.layers) gdn += !l.full; + const int full = (int)m.layers.size() - gdn; + for (const RecIssue& i : c.issues) { + const RecOp* o = b.op_of(i.op); + if (!o) continue; + if (o->op == "all_reduce") { + ++ar; + ar_lane0 += i.lane == 0; + ar_piece += i.n == piece; + ar_overlap += pint(o, "overlap") == 1; + } + if (o->op == "ar_rmsnorm_quant_fp8") { ++arn; arn_lane0 += i.lane == 0; } + if (o->op == "gdn_conv_prep") ++conv; + if (o->op == "attn_paged") { ++attn; wide_attn += i.n > 1024; } + if (o->op == "cast") { ++taps; wide_taps += i.n > 1024; } + } + const int pieces = world == 2 ? 2 : 1; + CHECK(gdn > 0 && full > 0); + CHECK_EQ(conv, pieces * gdn); + CHECK_EQ(attn, pieces * full); + CHECK_EQ(taps, pieces * m.n_taps); + if (world == 1) { + CHECK_EQ(ar + arn, 0); + CHECK(c.joins.empty()); + CHECK_EQ(wide_attn, attn); + continue; + } + CHECK_EQ(ar_lane0 + arn_lane0, 0); + /* Two blocks a layer, two pieces a block, and the embedding's whole. */ + CHECK_EQ(ar + arn, 4 * (int)m.layers.size() + 1); + CHECK(arn <= 1); + CHECK_EQ(ar_piece, 4 * (int)m.layers.size()); + CHECK_EQ(ar_overlap, 4 * (int)m.layers.size()); + CHECK_EQ(wide_attn, 0); + CHECK_EQ(wide_taps, 0); + CHECK(!c.joins.empty()); + } +} + /* Under a span a Flash-Next prefill of two intervals runs a pass at a time. At every world its * connection reads take a pass each and the indexer scores its pass-tall plane in windows, the MTP * head's pass included. At four ranks the step is pipelined (step_pipelined): every layer's