diff --git a/arch/clef_bf16/clef_bf16.cpp b/arch/clef_bf16/clef_bf16.cpp index 0562d6dc..c7238869 100644 --- a/arch/clef_bf16/clef_bf16.cpp +++ b/arch/clef_bf16/clef_bf16.cpp @@ -37,6 +37,11 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c * head's are the real ones. The head goes after the vision tower: its buffers join the same * arena, and it reads the final norm's rows, the lm_head and the trunk's cache block size. */ qwen35_bf16::Model& m = qwen35_bf16::g_model[ctx->rank]; + /* The lm_head's row would be sampled once and discarded: with this fill declared, the trunk + * skips the lm_head on a step every sampled row of which carries a request's words + * (plugin_words_cover_rows) and leaves zero rows instead. */ + m.op_logits_fill = clef_logits_fill(b, m.nm, m.g, m.b_logits); + if (!m.op_logits_fill) return RAD_E_INVAL; ClefHead& h = g_head[ctx->rank]; h = ClefHead{}; return h.declare(b, meta, ctx, m.nm, m.g, m.b_h, m.b_logits, m.w_lm_head, m.kv_full, diff --git a/arch/clef_fp8/clef_fp8.cpp b/arch/clef_fp8/clef_fp8.cpp index b813af72..a4231c52 100644 --- a/arch/clef_fp8/clef_fp8.cpp +++ b/arch/clef_fp8/clef_fp8.cpp @@ -34,6 +34,11 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c } RAD_ARCH_TRY(qwen35_clef_fp8::declare(b, meta, ctx)); qwen35_clef_fp8::Model& m = qwen35_clef_fp8::g_model[ctx->rank]; + /* The lm_head's row would be sampled once and discarded: with this fill declared, the trunk + * skips the lm_head on a step every sampled row of which carries a request's words + * (plugin_words_cover_rows) and leaves zero rows instead. */ + m.op_logits_fill = clef_logits_fill(b, m.nm, m.g, m.b_logits); + if (!m.op_logits_fill) return RAD_E_INVAL; ClefHead& h = g_head[ctx->rank]; h = ClefHead{}; return h.declare(b, meta, ctx, m.nm, m.g, m.a_h.x, m.b_logits, m.w_lm_head, m.kv_full, diff --git a/arch/common/rad_arch.h b/arch/common/rad_arch.h index 90b88443..65e48614 100644 --- a/arch/common/rad_arch.h +++ b/arch/common/rad_arch.h @@ -1212,6 +1212,17 @@ inline RadOperand bcol_at(rad_buf h, int64_t first_row, int64_t pitch, return o; } +/* Whether every logits row this step samples belongs to a sequence that carries words for the + * plugin (RadBatch::plugin_words). A head that answers from those words then has no use for the + * lm_head's row: it is sampled once and discarded. One row a sampled sequence; a speculative + * step samples more and answers false. */ +inline bool plugin_words_cover_rows(const RadBatch* b) { + if (b->n_out <= 0 || !b->plugin_cu_h) return false; + int64_t n = 0; + for (int64_t s = 0; s < b->n_seq; ++s) n += b->plugin_cu_h[s + 1] > b->plugin_cu_h[s] ? 1 : 0; + return n == b->n_out; +} + /* Rows [first, first + n), seen by the kernel as [n, pitch]; RadOperand::offset is in elements. * * Lets one op be issued in slices: an op whose widest kernel stops at M=64 (the delta net's diff --git a/arch/common/rad_block_clef.h b/arch/common/rad_block_clef.h index 6e3ffd9c..86fa7048 100644 --- a/arch/common/rad_block_clef.h +++ b/arch/common/rad_block_clef.h @@ -651,5 +651,16 @@ inline void ClefHead::step_heads(RadCtx* c, const RadBatch* batch, rad_buf b_log } } +/* The zero logits rows a trunk leaves when it skips its lm_head for rows a decision head answers + * (the qwen35 plugins' `op_logits_fill`): declared here, by the plugin that wants the skip, so a + * trunk serving a plain container declares the same graph as before. */ +inline rad_op clef_logits_fill(RadBuilder* b, Names& nm, const Geom& g, rad_buf b_logits) { + return rw(b, RAD_OP(b, "fill", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows), RAD_INT("n", g.n_vocab), + RAD_STR("dtype", "f32"), RAD_F64("value", 0.0)), + RAD_NOWEIGHTS), + {}, { b_logits }); +} + } /* namespace arch */ } /* namespace rad */ diff --git a/arch/qwen35_bf16/qwen35_bf16.cpp b/arch/qwen35_bf16/qwen35_bf16.cpp index b23b9472..9b78e74c 100644 --- a/arch/qwen35_bf16/qwen35_bf16.cpp +++ b/arch/qwen35_bf16/qwen35_bf16.cpp @@ -78,6 +78,12 @@ struct Model { rad_weight w_tok = 0, w_out_norm = 0, w_lm_head = 0; rad_op op_embed = 0, op_embed_ar = 0, op_final_norm = 0, op_gather = 0, op_logits = 0; + /* Set by a plugin built over this one whose head answers a request from its words + * (RadBatch::plugin_words): a `fill` of the logits rows that plugin declared. A step every + * sampled row of which carries words then skips the lm_head and leaves those zero rows for the + * sampler, whose token the request discards. Zero, the lm_head runs as it always has, and the + * graph this plugin declares is unchanged. */ + rad_op op_logits_fill = 0; /* ---- media (arch/common/rad_block_vit.h): the vision tower and the scatter of its rows, * declared only when the container carries a tower and the deployment asked for media. */ @@ -459,6 +465,10 @@ static void step(RadCtx* c, const RadBatch* batch) { /* The lm_head runs only on the rows the sampler asked for; a mid-prompt prefill chunk has * n_out zero and skips it. */ if (batch->n_out > 0) { + if (m.op_logits_fill && plugin_words_cover_rows(batch)) { + RAD_ISSUE_N(c, m.op_logits_fill, batch->n_out, brows(m.b_logits, batch->n_out)); + return; + } RAD_ISSUE_N(c, m.op_gather, batch->n_out, brows(m.b_h, batch->n_tok), praw(batch->out_ids, RAD_I32, batch->n_out), diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index 88aac2eb..d63eec13 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -197,6 +197,12 @@ struct Model { rad_weight w_tok = 0, w_out_norm = 0, w_lm_head = 0; rad_op op_embed = 0, op_embed_ar = 0, op_final_norm = 0, op_gather = 0, op_logits = 0; + /* Set by a plugin built over this one whose head answers a request from its words + * (RadBatch::plugin_words): a `fill` of the logits rows that plugin declared. A step every + * sampled row of which carries words then skips the lm_head and leaves those zero rows for the + * sampler, whose token the request discards. Zero, the lm_head runs as it always has, and the + * graph this plugin declares is unchanged. */ + rad_op op_logits_fill = 0; rad_op op_rope_cs = 0; /* rope_table, if the fused prologue is reachable */ /* ---- media (arch/common/rad_block_vit.h): the vision tower and the scatter of its rows, @@ -1495,6 +1501,10 @@ static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int if (L > 0) pipe_taps(c, m, L - 1, R, T - R); if (batch->n_out > 0) { + if (m.op_logits_fill && plugin_words_cover_rows(batch)) { + RAD_ISSUE_N(c, m.op_logits_fill, batch->n_out, brows(m.b_logits, batch->n_out)); + return; + } 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)); @@ -1598,6 +1608,10 @@ static void step(RadCtx* c, const RadBatch* batch) { brows(m.b_x, batch->n_tok), RAD_W(m.w_out_norm), brows(m.a_h.x, batch->n_tok)); if (batch->n_out > 0) { + if (m.op_logits_fill && plugin_words_cover_rows(batch)) { + RAD_ISSUE_N(c, m.op_logits_fill, batch->n_out, brows(m.b_logits, batch->n_out)); + return; + } RAD_ISSUE_N(c, m.op_gather, batch->n_out, brows(m.a_h.x, batch->n_tok), praw(batch->out_ids, RAD_I32, batch->n_out),