Archived
radiance 1.2.2
- Static YaRN for the Qwen rotary paths (#37): a container whose config names YaRN gets it in attention, the QSA index keys and a DFlash2 drafter; one reader for every rotary type, and a LLaMA container whose type is not served (llama3, longrope, dynamic) is refused by name. - --rope-scaling yarn:FACTOR[:ORIG] (#38) writes YaRN over a container's own rotary keys at load, no reconvert; the persisted prefix cache's fingerprint carries the resolved rotary. - An uneven world's cheaper rank (#36) pages the common token count and gives the rest of its VRAM to its experts; a plan and pool that disagree refuse to start. - MoE prefill (#35): wide gather, parallel scan and run-map prefix, 256-column down tile, early weight loads -- by rule, bit-identical. - MoE decode (#34): the sorted operand's own down kernel, its column tiles by routed rows. - --draft-window fixed|adaptive (#24): a per-sequence draft window priced by measured step time, one head depth per step over the batch; fixed stays the default. New metric radiance:draft_position_accepted_total and Grafana panels. - Out-of-tree plugin builds compile to the tree's device code from any build directory.
This commit is contained in:
1 parent
6fa7707dff
commit
ac79f4dc8a
63 files changed
+2744
-8759
No files matched your search
@@ -600,3 +600,90 @@ int ref_rope_table(const RadArgs* a, RadStream) {
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ qsa_block_key */
|
||||
/* The QSA indexer's compressed key (docs/OPS.md, docs/QSA.md): for each of M blocks,
|
||||
*
|
||||
* pooled = mean of the block's `ratio` rows of k, accumulated in f32 and ROUNDED TO k's dtype
|
||||
* normed = pooled * rsqrt(mean(pooled^2) + eps) * (wadd + w)
|
||||
* out = NeoX rotary of normed's first `rotary_dim` lanes at the block's position, the rest
|
||||
* passed through, rounded once at the store
|
||||
*
|
||||
* The rounding after the mean is the reference model's (`.float().mean(dim=1).to(dtype)`) and is
|
||||
* part of the answer. The block's position is `pos[b]` (a [>= M, 1 or 3] list carries a block's
|
||||
* components side by side, read under `mode` mrope/imrope and `sections`), or `pos0 + b*ratio`
|
||||
* without one. `page[b]`, when given, is the output row, and a negative entry writes nothing. The
|
||||
* rotary is `rope`'s ladder with YaRN's blend and magnitude when the coefficients are given. */
|
||||
int ref_qsa_block_key(const RadArgs* a, RadStream) {
|
||||
const RadTensor* k = t_in(a, 0);
|
||||
const RadTensor* w = t_in(a, 1);
|
||||
const RadTensor* page = t_in(a, 2);
|
||||
const RadTensor* pos = t_in(a, 3);
|
||||
const RadTensor* out = t_in(a, 4);
|
||||
if (!k || !w || !out) return RAD_E_INVAL;
|
||||
const int64_t n = p_int(a, "n", 0), ratio = p_int(a, "ratio", 0);
|
||||
const int64_t rot = p_int(a, "rotary_dim", 0), pos0 = p_int(a, "pos0", -1);
|
||||
if (n <= 0 || n > REF_HD_MAX || ratio <= 0 || (pos0 < 0 && !pos)) return RAD_E_INVAL;
|
||||
if (rot < 0 || rot > n || (rot & 1)) return RAD_E_SHAPE;
|
||||
if (p_int(a, "block_size", ratio) != ratio) return RAD_E_SHAPE;
|
||||
if (k->rank != 2 || out->rank < 2 || out->shape[0] < 1) return RAD_E_SHAPE;
|
||||
const int64_t M = page ? numel(page) : out->shape[0];
|
||||
if (M < 1 || numel(out) / out->shape[0] != n) return RAD_E_SHAPE;
|
||||
if (k->shape[0] != M * ratio || k->shape[1] != n || numel(w) < n) return RAD_E_SHAPE;
|
||||
const bool pos2 = pos && pos->rank == 2;
|
||||
if (pos && (pos2 ? pos->shape[0] < M : numel(pos) < M)) return RAD_E_SHAPE;
|
||||
|
||||
const float eps = p_f32(a, "eps", 1e-6f), wadd = p_f32(a, "wadd", 1.0f);
|
||||
const float theta = p_f32(a, "theta", 10000.0f), rscale = p_f32(a, "scale", 1.0f);
|
||||
static const char* kModes[] = { "neox", "mrope", "imrope" };
|
||||
const int mi = p_enum(a, "mode", kModes, 3, 0);
|
||||
if (mi < 0) return RAD_E_UNSUPPORTED;
|
||||
const int mode = mi == 1 ? ROPE_MROPE : mi == 2 ? ROPE_IMROPE : ROPE_NEOX;
|
||||
int64_t sec[8] = {};
|
||||
const int n_sec = rope_sections(a, sec);
|
||||
if (n_sec < 0 || n_sec > 3) return RAD_E_SHAPE;
|
||||
/* Several components only with a mode, its sections and three columns of `pos`; otherwise
|
||||
* every frequency reads the block's one position, which every mode reads as NeoX. */
|
||||
const bool mc = mode != ROPE_NEOX && n_sec > 0 && pos2 && pos->shape[1] >= 3;
|
||||
const RadYarn yarn = rad_yarn_args(a);
|
||||
if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && (rot <= 0 || rscale != 1.0f)))
|
||||
return RAD_E_INVAL;
|
||||
RopeFreq f;
|
||||
RAD_REF_TRY(rope_freqs(mc ? mode : ROPE_NEOX, rot, theta, sec, mc ? n_sec : 0, &f));
|
||||
for (int64_t i = 0; i < rot / 2; ++i) f.inv[i] = yarn.frequency(f.inv[i], (int)i);
|
||||
|
||||
#pragma omp parallel for schedule(static)
|
||||
for (int64_t b = 0; b < M; ++b) {
|
||||
const int64_t orow = page ? ld_int(page->dtype, page->data, offlin(page, b)) : b;
|
||||
if (orow < 0) continue;
|
||||
float x[REF_HD_MAX], y[REF_HD_MAX];
|
||||
double ss = 0.0;
|
||||
for (int64_t t = 0; t < n; ++t) {
|
||||
float acc = 0.0f;
|
||||
for (int64_t j = 0; j < ratio; ++j) acc += ldt(k, offlin(k, (b * ratio + j) * n + t));
|
||||
uint64_t tmp = 0;
|
||||
st_dt(k->dtype, &tmp, 0, acc / (float)ratio);
|
||||
x[t] = ld_dt(k->dtype, &tmp, 0);
|
||||
ss += (double)x[t] * x[t];
|
||||
}
|
||||
const float inv = 1.0f / std::sqrt((float)(ss / (double)n) + eps);
|
||||
for (int64_t t = 0; t < n; ++t) y[t] = x[t] * inv * (wadd + ldt(w, offlin(w, t)));
|
||||
float p[3] = {};
|
||||
for (int c = 0; c < (mc ? 3 : 1); ++c) {
|
||||
const int64_t bp = !pos ? pos0 + b * ratio
|
||||
: ld_int(pos->dtype, pos->data,
|
||||
offlin(pos, pos2 ? b * pos->shape[1] + c : b));
|
||||
p[c] = (float)bp / (rscale != 0.0f ? rscale : 1.0f);
|
||||
}
|
||||
const int64_t half = rot / 2;
|
||||
for (int64_t i = 0; i < half; ++i) {
|
||||
const float ang = p[f.comp[i]] * f.inv[i];
|
||||
const float c = std::cos(ang) * yarn.magnitude, s = std::sin(ang) * yarn.magnitude;
|
||||
const float x0 = y[i], x1 = y[half + i];
|
||||
y[i] = x0 * c - x1 * s;
|
||||
y[half + i] = x0 * s + x1 * c;
|
||||
}
|
||||
for (int64_t t = 0; t < n; ++t) stt(out, offlin(out, orow * n + t), y[t]);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
@@ -115,6 +115,7 @@ int ref_kv_store (const RadArgs*, RadStream);
|
||||
int ref_rope (const RadArgs*, RadStream);
|
||||
int ref_rope_table (const RadArgs*, RadStream);
|
||||
int ref_qk_norm_rope (const RadArgs*, RadStream);
|
||||
int ref_qsa_block_key (const RadArgs*, RadStream);
|
||||
|
||||
/* ---- mixture of experts */
|
||||
int ref_router_topk (const RadArgs*, RadStream);
|
||||
@@ -229,6 +230,7 @@ REF_SHAPE_FN(ref_kv_store);
|
||||
REF_SHAPE_FN(ref_rope);
|
||||
REF_SHAPE_FN(ref_rope_table);
|
||||
REF_SHAPE_FN(ref_qk_norm_rope);
|
||||
REF_SHAPE_FN(ref_qsa_block_key);
|
||||
|
||||
REF_SHAPE_FN(ref_router_topk);
|
||||
REF_SHAPE_FN(ref_moe_scatter);
|
||||
|
||||
@@ -292,6 +292,18 @@ static const RadParamSpec pRopeTable[] = { P_INT("M"), P_INT("rot"), P_F64("thet
|
||||
P_F64O("yarn_high"), P_F64O("yarn_magnitude") };
|
||||
static const RadOperandSpec oRopeTable[] = { OPD("positions"), OUT("cos_sin") };
|
||||
|
||||
/* QSA's compressed key, libr4d's schema exactly (libr4d/r4d_rows.cpp): `block_size` is `ratio`
|
||||
* spelled for the KV group the block-key store is, `page` and `pos` are optional. */
|
||||
static const RadParamSpec pQsaBlockKey[] = { P_INT("M"), P_INT("n"), P_INT("ratio"),
|
||||
P_INT("rotary_dim"), P_INTO("pos0"),
|
||||
P_INTO("block_size"), P_F64("eps"), P_F64("theta"),
|
||||
P_F64O("scale"), P_F64O("wadd"), P_STR("dtype"),
|
||||
P_STRO("mode"), P_STRO("sections"),
|
||||
P_F64O("yarn_factor"), P_F64O("yarn_low"),
|
||||
P_F64O("yarn_high"), P_F64O("yarn_magnitude") };
|
||||
static const RadOperandSpec oQsaBlockKey[] = { OPD("k"), WGT("w"), OPD_O("page"), OPD_O("pos"),
|
||||
OUT("out") };
|
||||
|
||||
static const RadParamSpec pQkNormRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"),
|
||||
P_INT("n_head_kv"), P_F64("theta"), P_F64("eps"),
|
||||
P_F64O("scale"), P_STRO("mode"), P_INTO("rotary_dim"),
|
||||
@@ -751,6 +763,12 @@ static const RadOpSchema kSchemas[] = {
|
||||
"cos/sin for the positions a step names, at each position's own row of a [max_ctx, rot] plane: "
|
||||
"rot values a row, cos for the first half and sin for the second. The angles are `rope`'s "
|
||||
"exactly. A row no position names is not written; a negative position writes nothing." },
|
||||
{ "qsa_block_key", ARR(pQsaBlockKey), ARR(oQsaBlockKey),
|
||||
"QSA's compressed key, one vector per `ratio` rows of k: the block's mean accumulated in f32 and "
|
||||
"rounded to k's dtype, RMS-normed with gain (wadd + w), then NeoX-rotated over `rotary_dim` at "
|
||||
"the block's position -- `pos[b]` (with `mode` mrope/imrope and `sections`, a [>= M, 3] list's "
|
||||
"three components) or `pos0 + b*ratio` -- with YaRN's blend and magnitude when given. `page[b]`, "
|
||||
"when given, is the output row; a negative entry writes nothing." },
|
||||
{ "rope", ARR(pRope), ARR(oRope),
|
||||
"Rotary embedding in place over the q and k heads of a fused [q|k|v] row; the v heads are left "
|
||||
"alone. `mode` is \"neox\" or \"gptj\", or a multi-component form: \"mrope\" (contiguous "
|
||||
@@ -1084,6 +1102,9 @@ SIMPLE("ref_moe_gemm_q_mod4", "moe_gemm_q_mod4", "moe",
|
||||
SIMPLE("ref_moe_gather", "moe_gather", "moe", "weighted scatter back to token order", DT_FLOAT,
|
||||
ref_moe_gather),
|
||||
SIMPLE("ref_rope_table", "rope_table", "attn", "cos/sin at each position's own row", DT_FLOAT, ref_rope_table),
|
||||
SIMPLE("ref_qsa_block_key", "qsa_block_key", "attn",
|
||||
"QSA compressed key: pooled, normed and rotated at the block's position", DT_FLOAT,
|
||||
ref_qsa_block_key),
|
||||
SIMPLE("ref_row_topk", "row_topk", "moe", "exact per-row top-R", DT_FLOAT, ref_row_topk),
|
||||
SIMPLE("ref_row_topk_merge", "row_topk_merge", "moe", "the global top-R of the gathered per-rank candidate sets", DT_FLOAT, ref_row_topk_merge),
|
||||
SIMPLE("ref_logit_rerank", "logit_rerank", "moe", "R candidates a row rescored exactly against the full-precision head", DT_FLOAT, ref_logit_rerank),
|
||||
|
||||
@@ -1040,6 +1040,23 @@ int ref_rope_table_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* ou
|
||||
return o.finish("rope_table", operand, out);
|
||||
}
|
||||
|
||||
int ref_qsa_block_key_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0);
|
||||
const int64_t n = gi(p, n_p, "n", 0);
|
||||
const int64_t r = gi(p, n_p, "ratio", 0);
|
||||
if (M <= 0 || n <= 0 || r <= 0) return RAD_E_UNSUPPORTED;
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
Opd o;
|
||||
o.t(D, { M * r, n }); /* k -- `ratio` rows a block */
|
||||
o.t((uint32_t)RAD_F32, { n }); /* w -- the norm gain */
|
||||
/* page: absent, so the write is contiguous. A drawn page list may repeat an entry, and two
|
||||
* blocks onto one row is a race whose winner the comparison would then measure. */
|
||||
o.skip();
|
||||
o.t_idx({ M }, M * r, -1, 0); /* pos -- each block's first-token position */
|
||||
o.t(D, { M, n }); /* out */
|
||||
return o.finish("qsa_block_key", operand, out);
|
||||
}
|
||||
|
||||
/* ================================================================== mixture of experts */
|
||||
|
||||
int ref_scale_rows_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
|
||||
Reference in new issue
Block a user