Archived
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.
This commit is contained in:
1 parent
459dd4f42d
commit
809257e393
2 files changed
+115
No files matched your search
@@ -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)) {
|
||||
|
||||
Reference in new issue
Block a user