Archived
vocabulary: hc_project -- a control vector's direction, projected out of the wide stream
h[s] -= scale * (h[s] . v) * v for each of the hyper-connection streams s llama.cpp's --cvec-mode project semantics: the dot is per stream, because the patch applies it to the [n_embd, hc, tokens] view where mul_mat(v, cur) reduces over dim 0. v is one layer's unit direction (f32), scale is the norm its file stored, and 'on' is a per-row int32 switch: a zero row is skipped without a read or a write, so an unprojected request is byte-identical to a deployment with no vector loaded. libref is the oracle and the host fallback, libavx carries it at every ISA level, libr4d (one workgroup per (row, stream), LDS reduction) serves the card. docs/OPS.md gains the row and the operation's three facts.
This commit is contained in:
1 parent
64adee3ece
commit
459dd4f42d
14 files changed
+362
-11
No files matched your search
@@ -252,6 +252,59 @@ int ref_hc_write(const RadArgs* a, RadStream) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ hc_project */
|
||||
/* The experimental speed projection: h[row, s*n + i] -= scale * (h . v) * v, for each of the hc
|
||||
* streams s, on the rows whose `on` flag is 1.
|
||||
*
|
||||
* `v` is one layer's unit direction (the engine normalises the file's rows when it uploads them)
|
||||
* and `scale` is the norm the file stored for that layer, so this is exactly llama.cpp's
|
||||
* `--cvec-mode project` with its `llama_adapter_cvec` (the package's 02-cvec-projection-mode
|
||||
* patch): the dot is taken per hyper-connection stream, as `mul_mat` over a [n, hc, tokens] view
|
||||
* gives it, and not on the stream mean.
|
||||
*
|
||||
* A row whose flag is 0 is not touched: no read, no write, so a request that did not ask for the
|
||||
* vector's activations are the stock model's: no read, no write. (A deployment that merely *has* the
|
||||
* table loaded is not bit-identical to one without it -- the table's VRAM moves the expert/KV plan --
|
||||
* but that is placement, not this op.)
|
||||
*
|
||||
* `on` is an int32 per row (core derive.cpp's `projection_on_rows`); the direction is f32. */
|
||||
int ref_hc_project(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 3)) return RAD_E_INVAL;
|
||||
const RadTensor* h = &a->t[0];
|
||||
const RadTensor* v = &a->t[1];
|
||||
const RadTensor* on = &a->t[2];
|
||||
|
||||
const int64_t n = p_int(a, "n", 0);
|
||||
const int64_t hc = p_int(a, "hc", 0);
|
||||
const float sc = p_f32(a, "scale", 1.0f);
|
||||
if (n <= 0 || hc <= 1) return RAD_E_SHAPE;
|
||||
if (numel(v) < n) return RAD_E_SHAPE;
|
||||
const int64_t hn = hc * n;
|
||||
|
||||
const int64_t rows = rows_of(h, hn);
|
||||
if (rows <= 0 || numel(on) < rows) return RAD_E_SHAPE;
|
||||
|
||||
/* Rows are independent: each dot is over one row's own streams. The parallel index is the
|
||||
* (row, stream) pair, so one row's hc streams may run on different threads without sharing an
|
||||
* accumulator. */
|
||||
REF_PAR_FOR(rows * hc)
|
||||
for (int64_t k = 0; k < rows * hc; ++k) {
|
||||
const int64_t r = k / hc, c = k % hc;
|
||||
if (ld_int(on->dtype, on->data, offlin(on, r)) == 0) continue;
|
||||
const int64_t hb = rowoff(h, r, hn), hs = laststride(h);
|
||||
const int64_t vb = rowoff(v, 0, n), vl = laststride(v);
|
||||
double acc = 0.0;
|
||||
for (int64_t i = 0; i < n; ++i)
|
||||
acc += (double) ldt(h, hb + (c * n + i) * hs) * (double) ldt(v, vb + i * vl);
|
||||
const float g = (float) (acc * (double) sc);
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
const int64_t j = hb + (c * n + i) * hs;
|
||||
stt(h, j, ldt(h, j) - g * ldt(v, vb + i * vl));
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ================================================================== mtp_enter
|
||||
*
|
||||
* The multi-token-prediction head's entry for a model whose residual stream is `hc` wide.
|
||||
|
||||
Reference in new issue
Block a user