From 809257e393bba4dc1a2f5a177e9e7a469e813f79 Mon Sep 17 00:00:00 2001 From: C_Tamas Date: Tue, 20 Oct 2026 14:39:19 +0700 Subject: [PATCH] arch/qwen4exp: the control vector's projection after every block's write HcProject in arch/common/rad_block_hc.h owns the block: its per-token switch buffer (`projection_on_rows`, a derived buffer), the per-layer op and the issue point. The plugin declares it with one call per layer and issues it with one call after the feed-forward write -- the place llama.cpp's build_cvec(res_hc, il) sits in the qwen4exp graph, in both the serial and the pipelined step. An unsteered layer, or a deployment with no vector, declares nothing and issues nothing. --- arch/common/rad_block_hc.h | 98 ++++++++++++++++++++++++++++++ arch/qwen4exp_fp8/qwen4exp_fp8.cpp | 17 ++++++ 2 files changed, 115 insertions(+) diff --git a/arch/common/rad_block_hc.h b/arch/common/rad_block_hc.h index 44c538f7..523f1f3d 100644 --- a/arch/common/rad_block_hc.h +++ b/arch/common/rad_block_hc.h @@ -368,6 +368,104 @@ inline void HyperConn::read(RadCtx* c_, int64_t T, int64_t r0, int64_t rows) con /* The block's output `y` is read from `w.x.x`, the buffer the read wrote the block's input to: * every block leaves its delta in the activation its norm produced, and the input is dead by * then. */ +/* ================================================================== the control-vector projection + * + * A control vector is one unit direction per layer (a `controlvector` GGUF, core/cvec.h). Turned on + * for a request, a steered layer's wide stream is projected off that layer's direction right after + * the feed-forward's write: + * + * h[s] -= scale * (h[s] . v) * v for each of the hc streams s + * + * which is llama.cpp's `--cvec-mode project` and the operation the Qwen3.8-Flash-Next package + * documents. The dot is per stream, not on the stream mean: the patch applies it to the + * [n_embd, hc, tokens] view where `mul_mat(v, cur)` reduces over dim 0 alone. + * + * Two properties the architecture plugin does not have to think about, both here: + * + * - **the switch is per request.** `on` is a derived buffer of one int32 per token row + * (core/sched/derive.cpp's `projection_on_rows`), so a step that mixes a projected + * conversation with a stock one projects exactly the rows that asked for it. All zeros is the + * stock model: the kernel skips a zero row without reading or writing it, so an unprojected + * request's stream is the one the stock model computes. (The *deployment* is not bit-identical + * to one with no vector: the direction table's VRAM can move an expert or a KV block, which is + * the tie-level variance radiance documents elsewhere. Measured, docs/GUIDE.md 4.10.) + * - **the parameters travel with the file.** `scale` is the norm the file stored for that layer + * (times the package's own `scale`, read from its JSON), and the direction row is sliced from + * the table at issue, so nothing is configured per deployment or scanned per layer. + * + * One instance per layer: `declare` emits an op only when the vector steers that layer, so a + * deployment without a vector declares no op, no buffer and no branch in the step. */ +struct HcProject { + rad_op op = 0; /* 0: this layer is not steered (or there is no vector) */ + const float* dir = nullptr; /* this layer's row of the device direction table */ + rad_buf on = 0; /* [rows] int32: 1 = project this row */ + rad_buf h = 0; /* the model's wide stream */ + int64_t n = 0, hc = 0; + + bool steered() const { return op != 0; } + + /* The per-token switch, declared once per model as a derived buffer. 0 when the deployment has + * no vector (or one of another width), which leaves every layer unsteered. */ + static rad_buf declare_mask(RadBuilder* b, const char* name, const Geom& g, + const RadControlVector* cv) { + if (!b || g.n_embd <= 0 || g.max_tok <= 0) return 0; + if (!cv || !cv->dir || cv->n_layers <= 0) return 0; + if (cv->n_embd != g.n_embd) { + fprintf(stderr, "radiance: %s: the control vector is %lld wide and this model's " + "residual streams are %lld; not steering it\n", + name, (long long) cv->n_embd, (long long) g.n_embd); + return 0; + } + RadBufDecl d{}; + d.dtype = RAD_I32; + d.rank = 1; + d.shape[0] = g.max_tok; + d.kind = RAD_BUF_DERIVED; + d.domain = RAD_DOMAIN_DEVICE; + d.derive = "projection_on_rows"; + return rad_decl_buffer(b, name, &d); + } + + /* `layer`'s projection, when the vector steers it. Called for every layer; the unsteered ones + * (outside the file, outside the range, or no vector at all) declare nothing. */ + int declare(RadBuilder* b, const char* name, const Geom& g, int64_t layer, + const RadControlVector* cv, rad_buf mask, rad_buf stream, int64_t hc_streams) { + if (!b || !mask || !stream || !cv || !cv->dir || hc_streams <= 1) return RAD_OK; + if (layer <= 0 || layer >= cv->n_layers || layer >= g.n_layer) return RAD_OK; + const float scale = cv->scales ? cv->scales[(size_t) layer] : 0.0f; + if (scale == 0.0f) return RAD_OK; + + n = g.n_embd; + hc = hc_streams; + on = mask; + h = stream; + dir = cv->dir + (size_t) layer * (size_t) n; + + op = rw(b, RAD_OP(b, "hc_project", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", n), RAD_INT("hc", hc), + RAD_F64("scale", (double) scale), RAD_STR("dtype", g.dtype)), + RAD_NOWEIGHTS), + {h, mask}, {h}); + if (!op) { + fprintf(stderr, "radiance: %s: hc_project resolves to no kernel at n %lld, hc %lld -- " + "this rank cannot serve the control vector\n", + name, (long long) n, (long long) hc); + return RAD_E_INVAL; + } + return RAD_OK; + } + + /* Rows [r0, r0 + rows) of the stream. The whole step is r0 = 0; the pipelined step issues one + * call per piece, in the same place in the graph. */ + void step(RadCtx* c_, int64_t r0, int64_t rows) const { + if (!op) return; + RAD_ISSUE_N(c_, op, rows, + brow_slice(h, r0, rows, hc * n), + praw(dir, RAD_F32, n), + praw((const int32_t*) rad_buf_ptr(c_, on) + r0, RAD_I32, rows)); + } +}; + inline void HyperConn::write(RadCtx* c_, int64_t T, int64_t r0, int64_t rows) const { if (!op_write) return; if (takes_gather(T)) { diff --git a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp index d30c4b58..002d48f9 100644 --- a/arch/qwen4exp_fp8/qwen4exp_fp8.cpp +++ b/arch/qwen4exp_fp8/qwen4exp_fp8.cpp @@ -109,6 +109,11 @@ struct Layer { QsaIndexer qsa; /* full-attention layers only; inert when the checkpoint has none */ GdnFP8 gdn; MoeFP8 mlp; + /* The control vector's projection, applied to the wide stream right after this layer's + * feed-forward write -- where llama.cpp's `build_cvec(res_hc, il)` sits in the `qwen4exp` + * graph. Inert (no op) when the deployment has no vector or this layer is outside its range + * (arch/common/rad_block_hc.h). */ + HcProject proj; }; struct Model { @@ -941,6 +946,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c /* The stream's initial value: hc identical copies of the embedding. */ RAD_ARCH_TRY(m.enter.declare(b, g, m.hccfg.hc, m.a_x.x, m.b_h)); + /* The control vector's per-token switch, when this deployment found one (HcProject). All zeros + * is the stock model, so a request that does not ask for the vector is unaffected. */ + const rad_buf proj_on = HcProject::declare_mask(b, nm.f("projection_on"), g, ctx->cvec); + /* ---- the layers. * * Declaration order is the buffer planner's liveness: a transient lives over [first @@ -1170,6 +1179,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c lay.mlp.gather_out_rows = lay.hc_ffn.gather_rows; lay.mlp.gather_out_rows6 = lay.hc_ffn.gather_rows6; } + + /* The control vector's projection for this layer, if the vector steers it. */ + RAD_ARCH_TRY(lay.proj.declare(b, nm.f("blk.%lld.proj", (long long) l), g, l, ctx->cvec, + proj_on, m.b_h, m.hccfg.hc)); } /* Each routed layer predicts the next one's experts at its router input (MoeFP8::xp_next). */ @@ -1541,6 +1554,8 @@ static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int for (int64_t p = 0; p < K; ++p) { if (p == K - 1) wait1(); l.hc_ffn.write(c, T, r0(p), rn(p)); + /* The projection of this piece, before the next layer reads it. */ + l.proj.step(c, r0(p), rn(p)); if (nx && !ple_next) nx->hc_mix.read(c, T, r0(p), rn(p)); } if (ple_next) { @@ -1584,6 +1599,8 @@ static void step_serial(RadCtx* c, const RadBatch* batch, Model& m, const int32_ l.hc_ffn.read_pieces(c, T); l.mlp.step(c, batch); l.hc_ffn.write(c, T, 0, T); + /* The request's control vector, on the stream the write just finished. */ + l.proj.step(c, 0, T); dbg_resid(c, (int)li, "ffn", g.n_embd, m.b_h, m.a_x.x); } }