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.
|
||||
|
||||
@@ -67,6 +67,7 @@ int ref_mhc_contract (const RadArgs*, RadStream);
|
||||
int ref_kda_update (const RadArgs*, RadStream);
|
||||
int ref_hc_read (const RadArgs*, RadStream);
|
||||
int ref_hc_write (const RadArgs*, RadStream);
|
||||
int ref_hc_project (const RadArgs*, RadStream);
|
||||
int ref_mtp_enter (const RadArgs*, RadStream);
|
||||
int ref_layernorm (const RadArgs*, RadStream);
|
||||
int ref_grid_embed (const RadArgs*, RadStream);
|
||||
@@ -317,6 +318,7 @@ REF_SHAPE_FN(ref_mhc_contract);
|
||||
REF_SHAPE_FN(ref_kda_update);
|
||||
REF_SHAPE_FN(ref_hc_read);
|
||||
REF_SHAPE_FN(ref_hc_write);
|
||||
REF_SHAPE_FN(ref_hc_project);
|
||||
REF_SHAPE_FN(ref_mtp_enter);
|
||||
REF_SHAPE_FN(ref_layernorm);
|
||||
REF_SHAPE_FN(ref_grid_embed);
|
||||
|
||||
@@ -118,6 +118,12 @@ static const RadOperandSpec oCausalConv[] = { OPD("x"), WGT("w"), INOUT("conv_st
|
||||
OPD_O("num_accepted"), OUT("y") };
|
||||
|
||||
static const RadParamSpec pHcWrite[] = { P_INT("M"), P_INT("n"), P_INT("hc"), P_STR("dtype") };
|
||||
/* The speed projection (libref/ref_hc.cpp, core/cvec.h): one unit direction per layer and a
|
||||
* per-row switch. `v` is f32 whatever the stream's dtype is, because the engine uploads the table
|
||||
* in f32; `on` is an int32 row mask (core derive.cpp's `projection_on_rows`). */
|
||||
static const RadParamSpec pHcProject[] = { P_INT("M"), P_INT("n"), P_INT("hc"), P_F64("scale"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oHcProject[] = { INOUT("h"), OPD("v"), OPD("on") };
|
||||
static const RadOperandSpec oHcEnter[] = { OPD("x"), OUT("h") };
|
||||
static const RadOperandSpec oHcWrite[] = { OPD("y"), OPD("inj"), INOUT("h") };
|
||||
static const RadParamSpec pMhcPre[] = { P_INT("M"), P_INT("n"), P_INT("hc"),
|
||||
@@ -1003,6 +1009,12 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "hc_write", ARR(pHcWrite), ARR(oHcWrite),
|
||||
"The write half: h[s] += inj[s] * y, for each of the hc streams of h. In place, and `h` is the "
|
||||
"UNNORMED stream the paired hc_read was handed -- the normalisation never enters the residual." },
|
||||
{ "hc_project", ARR(pHcProject), ARR(oHcProject),
|
||||
"The experimental speed projection (llama.cpp's `--cvec-mode project`): for each of the hc "
|
||||
"streams of a row whose `on` flag is 1, h[s] -= scale * (h[s] . v) * v, with v that layer's unit "
|
||||
"direction and scale the norm the file stored. The dot is per stream, not on the stream mean. A "
|
||||
"row whose flag is 0 is not read and not written, so an unprojected request is byte-identical to "
|
||||
"a deployment with no vector loaded." },
|
||||
{ "mtp_enter", ARR(pMtpEnter), ARR(oMtpEnter),
|
||||
"The MTP head's entry for a wide residual stream: x[k*n+i] = fc_h[i] . grouped_norm(h)[k] "
|
||||
"+ fc_e[i] . norm(e), for each of the hc sub-streams k. `ngroup` == hc is one rsqrt per "
|
||||
@@ -1957,6 +1969,9 @@ ROWL("ref_mtp_enter", "mtp_enter", "norm",
|
||||
ref_check_dtypes, ref_mtp_enter, ref::ref_layout_mtp),
|
||||
SIMPLE("ref_hc_write", "hc_write", "norm", "gated residual write: h[s] += inj[s] * y",
|
||||
DT_FLOAT, ref_hc_write),
|
||||
SIMPLE("ref_hc_project", "hc_project", "norm",
|
||||
"speed projection: h[s] -= scale * (h[s] . v) * v where the row's flag is on",
|
||||
DT_FLOAT, ref_hc_project),
|
||||
SIMPLE("ref_layernorm", "layernorm", "norm", "layer norm with gain and bias", DT_FLOAT,
|
||||
ref_layernorm),
|
||||
SIMPLE("ref_grid_embed", "grid_embed", "elem",
|
||||
|
||||
@@ -436,6 +436,17 @@ int ref_hc_write_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
|
||||
return o.finish("hc_write", operand, out);
|
||||
}
|
||||
|
||||
int ref_hc_project_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
const int64_t hc = gi(p, n_p, "hc", 0);
|
||||
Opd o;
|
||||
o.t(D, { M, hc * n }); /* h, in place */
|
||||
o.t((uint32_t) RAD_F32, { n }); /* v: one layer's unit direction */
|
||||
o.t((uint32_t) RAD_I32, { M }); /* on: 1 = this row takes the projection */
|
||||
return o.finish("hc_project", operand, out);
|
||||
}
|
||||
|
||||
int ref_mtp_enter_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
|
||||
Reference in new issue
Block a user