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:
C_Tamas authored and StillDeadcode committed 2026-10-21 09:26:17 +02:00
1 parent 64adee3ece
commit 459dd4f42d
14 files changed
+362 -11

No files matched your search

+7
View File
@@ -147,6 +147,13 @@ std::vector<Case> build_cases() {
HF("lower_bound", -5), HF("scale", 0.1767766952966369),
HF("l2_eps", 1e-6), HS("mode", mode), HS("dtype", dt) });
add("hc_write", { HI("M", 5), HI("n", 128), HI("hc", 4), HS("dtype", dt) });
add("hc_project",{ HI("M", 5), HI("n", 128), HI("hc", 4), HF("scale", 1.0),
HS("dtype", dt) });
/* The model's own width: Qwen3.8-Flash-Next's stream is 2560 wide with hc 4, and a decode
* step is one row per sequence (with speculation, up to 1 + depth). One case at that
* geometry is what says whether 41 of these a step is affordable. */
add("hc_project",{ HI("M", 4), HI("n", 2560), HI("hc", 4), HF("scale", 1.0),
HS("dtype", "bf16") });
add("hc_read", { HI("M", 5), HI("n", 128), HI("hc", 4), HI("lowrank", 8),
HF("eps", 1e-6), HS("dtype", dt), HF("wadd", 1), HI("inject", 1),
HI("group", 0) });
+58
View File
@@ -331,6 +331,64 @@ AVX_KERNEL(hc_write) {
return RAD_OK;
}
/* ------------------------------------------------------------------ hc_project */
/* The experimental speed projection (core/cvec.h): h[s] -= scale * (h[s] . v) * v for each of the
* hc streams of a row whose switch is on. v is f32, the stream may be bf16/f16/f32; a row with a
* zero switch is skipped without a read, so an unprojected request is byte-identical to one on a
* deployment with no vector. */
AVX_KERNEL(hc_project) {
if (!rad_args_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 || numel(v) < n) return RAD_E_SHAPE;
const int64_t hn = hc * n;
const int64_t rows = numel(h) / hn;
if (rows <= 0 || numel(on) < rows) return RAD_E_SHAPE;
const int64_t vb = rowoff(v, 0, n);
float* vbuf = scratch_f32_c((size_t) n);
const float* vec = in_row(v, vb, n, vbuf);
AVX_PARALLEL_FOR_IF(rows * hn >= AVX_PAR_MIN)
for (int64_t k = 0; k < rows * hc; ++k) {
const int64_t r = k / hc, c = k % hc;
if (ldi(on, offlin(on, r)) == 0) continue;
const int64_t hb = rowoff(h, r, hn), hs = laststride(h);
const int64_t base = hb + c * n;
float* hv = scratch_f32((size_t) n);
const float* row = in_row(h, base, n, hv);
if (hs != 1) {
float dot = 0.0f;
for (int64_t i = 0; i < n; ++i) dot += row[i] * vec[i];
const float g = dot * sc;
for (int64_t i = 0; i < n; ++i)
stt(h, base + i * hs, row[i] - g * vec[i]);
continue;
}
if (row != hv) std::memcpy(hv, row, (size_t) n * sizeof(float));
/* The dot reads the row and the direction; the update then rewrites the same elements, so
* the direction's own copy is not needed (it is never written). */
vf acc = vf_zero();
int64_t i = 0;
for (; i + VF_N <= n; i += VF_N)
acc = vf_fma(vf_loadu(hv + i), vf_loadu(vec + i), acc);
float dot = vf_hsum(acc);
for (; i < n; ++i) dot += hv[i] * vec[i];
const vf vg = vf_set1(dot * sc);
i = 0;
for (; i + VF_N <= n; i += VF_N)
vf_storeu(hv + i, vf_sub(vf_loadu(hv + i), vf_mul(vg, vf_loadu(vec + i))));
for (; i < n; ++i) hv[i] -= (dot * sc) * vec[i];
out_row(h, base, n, hv);
}
return RAD_OK;
}
/* ================================================================== mtp_enter
*
* Entry of the multi-token-prediction head for a model with an `hc`-wide residual stream.
+11 -11
View File
@@ -70,17 +70,17 @@ int avx_par_force(int threads);
#define AVX_OP_LIST(X) \
/* ---- elementwise and norms */ \
X(rmsnorm) X(rmsnorm_p) X(rmsnorm_add) X(hc_enter) \
X(hc_read) X(hc_write) X(mtp_enter) X(layernorm) \
X(mhc_pre) X(mhc_post) X(mhc_contract) X(kda_update) \
X(mhc_pre_partial) X(mhc_pre_finish) \
X(swiglu_clamped) \
X(router_sigmoid_topk) \
X(dsa_pool) \
X(dsa_query) \
X(gemm_nt_f32) \
X(mla_project) \
X(dsa_score) \
X(mla_attn) \
X(hc_read) X(hc_write) X(hc_project) X(mtp_enter) \
X(layernorm) X(mhc_pre) X(mhc_post) X(mhc_contract) \
X(kda_update) X(mhc_pre_partial) X(mhc_pre_finish) \
X(swiglu_clamped) \
X(router_sigmoid_topk) \
X(dsa_pool) \
X(dsa_query) \
X(gemm_nt_f32) \
X(mla_project) \
X(dsa_score) \
X(mla_attn) \
X(add) X(grid_embed) X(grid_embed_aa) X(causal_conv1d) \
X(mul) X(silu_mul) X(gelu_mul) X(gelu) \
X(silu) X(sigmoid) X(logit_softcap) X(gather_rows) \
+10
View File
@@ -114,6 +114,10 @@ static const RadOperandSpec oCausalConv[] = { OPD("x"), WGT("w"), INOUT("conv_st
static const RadParamSpec pHcWrite[] = { P_INT("M"), P_INT("n"), P_INT("hc"), P_STR("dtype") };
static const RadOperandSpec oHcEnter[] = { OPD("x"), OUT("h") };
static const RadOperandSpec oHcWrite[] = { OPD("y"), OPD("inj"), INOUT("h") };
/* The speed projection: one unit direction per layer (f32) and an int32 per-row switch. */
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 RadParamSpec pMtpEnter[] = { P_INT("M"), P_INT("n"), P_INT("hc"), P_F64("eps"),
P_STR("dtype"), P_F64O("wadd"), P_INTO("ngroup"),
@@ -826,6 +830,9 @@ static const RadOpSchema kSchemas[] = {
"gate argument, and hc_write adds into the UNNORMED h). `mix` names the matrices' stored "
"format; a host kernel reads them as the dense tensors it is handed." },
{ "hc_write", ARR(pHcWrite), ARR(oHcWrite), "h[s] += inj[s] * y, into the unnormed stream." },
{ "hc_project", ARR(pHcProject), ARR(oHcProject),
"The experimental speed projection (llama.cpp's `--cvec-mode project`): h[s] -= scale * "
"(h[s] . v) * v for each of the hc streams of a row whose flag is on. A zero flag is not read." },
{ "mtp_enter", ARR(pMtpEnter), ARR(oMtpEnter),
"MTP head entry: two norms, a per-sub-stream fc and a broadcast fc. `fc` names the two "
"matrices' stored format; a host kernel reads them as the dense tensors it is handed." },
@@ -1362,6 +1369,9 @@ ROWL("avx_hc_read", "hc_read", "norm",
avx_layout_hc),
SIMPLE("avx_hc_write", "hc_write", "norm", "gated residual write: h[s] += inj[s] * y", DT_FLOAT,
L(hc_write)),
SIMPLE("avx_hc_project", "hc_project", "norm",
"speed projection: h[s] -= scale * (h[s] . v) * v where the row's flag is on", DT_FLOAT,
L(hc_project)),
ROWL("avx_mtp_enter", "mtp_enter", "norm",
"MTP head entry: two norms, a per-sub-stream fc and a broadcast fc",
DT_FLOAT ", or fc matrices of E4M3 / int8 codes with a scale plane, joined", L(mtp_enter),