New models. Thinking Machines' Inkling 2 Mini by gostar (#584-#586): its text backbone and loop, the release's NVFP4 and MXFP8 checkpoint read by the converter, an int8 trunk, the vision tower, tensor parallelism, tool calls (with Warzuponus) and an image processor that matches transformers bit for bit (millaguie). Inkling's routed experts are served in IQR-V4's four-bit trellis (moe_gemm_iqr), fitted by GPTQ against Hessians the plugin now records (`--calib-dir`), its token embedding in int8 (read in place by embed_lookup), and incoai's DFlash2 drafter in four bits in place of the release's DSpark. That container, 14.4 GiB with the vision tower, decodes 190 / 427 / 658 tokens a second at 1 / 4 / 8 streams at tp 2 on two R9700 with `draft-window: adaptive` (code 240 / 550 / 790, step-by-step arithmetic 358 / 811 / 1116): 19-43% ahead of the same target with the release's DSpark drafter and 45-83% ahead of the all-int8 container (26.1 GiB, 124 / 263 / 386), at a mean KL of 0.0066 against the release's weights (int8: 0.0042). DeepSeek-V4.1-Flash on 2.x by baKon (#583), with its vision tower, its IQ1_S and IQ2_XXS containers and linears and experts served in llama.cpp's own blocks; parts ported from mikoskinen/r9700-exact-serving by Dad_Vibes' fork. GLM-5.3-Flash on 2.x with its IQR / IQ8R container, an int8 latent cache and four-card serving by AnimusNull (#566). Gemma 4 dense on EXL3-V3 by keptn42 (#537), on the EXL3 dense rows and qwen35_exl3 tensor parallelism by Hero422. NVIDIA's 2-bit Nemotron-3.5-Bolt-74B-A9B on one card by keptn42 (#477). Wan 2.2 TI2V-5B text-to-video and first-frame video by rkcth (#535). LFM3-1.7B on RDNA3 iGPUs by DokterSmol (#578). Qwen3.8-27B in bf16 with the DFlash2 drafter, and bf16x12, bf16 weights packed losslessly in 12.25 bits, by eddie168 (#577). Int8 experts for drama-3 by morevgalex (#598). Speculative decoding. Draft trees for Qwen3.8-27B's DFlash2 drafts by Kezi (#450, opt-in): one card, +17% at one stream and +14% at two. --draft-max-lanes and --draft-min-accept, two gates on a block drafter, and --draft-window-ckpt, its window KV kept in the checkpoint slots, by rkcth (#600-#602). `draft-window: adaptive` verifies each sequence's drafts up to the first one unlikely to be kept. A verify's rows share each K and V tile in Inkling's attention by gostar (#608): 93 to 118 tokens a second at a 70k-token prompt on one card. Generation. Krea 2 instruction edits with one or two references and registered outfill, LoRA on the image generators with bias deltas and row-mixer adapters applied per request, and MiniMax-H3's model-declared sparse attention profiles, by rkcth (#534, #542, #543, #605-#609). MiniMax-H3's audio reference guide pinned from a frame by 1beb (#595). LTX-2's chunk ceiling raised to 40 by Itoash (#570). Fleets and memory. memory.queue-reserve, the hardware queues a card keeps ready, by kevinjd (#589). memory.pinned-numa-node by Foundry (#580). memory.card-headroom-mib on single-model tensor-parallel budgets by zihaomu (#624). Parked weights rebuilt on the CPU, memory ownership kept across a failed handoff and quiescent callbacks serialised, by rkcth (#557-#561). A warm load may unload a parked model (#592); the first card listed chooses the architecture (#458). Kernels. libr3d's packed dense IQR GEMM and routed IQR expert rows on gfx1100, and libhalo's GEMV decode for Qwen3.8-Flash-Next's MoE down, by zihaomu (#562, #620, #622). GGML IQ2_XXS and IQ3_XXS packed on the card by baKon. Dashboard. KV cache in tokens (in use, capacity now and with all flexible space), live figures through long prefill steps, step / decode / prefill / acceptance lines without holes, and expert hit rates only on tiered models. Seven tile and history defects fixed by Noblistic (#597), vision encoder and per-card hardware figures by davetha (#574), per-LoRA attribution by dowjames (#552). Fixed. return_tokens / return_token_ids on the OpenAI endpoints (#571). reasoning_effort "none" and "minimal" reach a template that takes them as levels, by millaguie (#608). Misaligned loads in the windowed paged attention over an int8 cache (#540). A retired 27B head refused by name (#541). rad-convert's GPU trellis jobs kept fed, and --reuse-only, by gostar (#575). jinja's tojson takes sort_keys by Warzuponus (#582). hfget measures file sizes with stat, by kepler_62f (#623). The tested C++ compiler is stated in the build guide by lowell80 (#551). The codebase was cleaned up: shared helpers instead of repeated blocks, and the int8 linear, activation and GEMM code no longer named after the retired fp8 formats. Inkling 2 Mini is served in one form: IQR-V4 experts with the DFlash2 drafter (StillDeadcode/inkling-2-mini-iq4r-v4 on Hugging Face, or data/recipes/inkling2-mini-iq4r-v4.recipe). A container with the release's DSpark drafter, or with NVFP4, int8 or 4-bit experts, is refused by name and needs converting again. Plugin ABI 44: out-of-tree plugins need to be rebuilt. Thanks to @gostar, @baKon, @Dad_Vibes, @AnimusNull, @keptn42, @Hero422, @rkcth, @DokterSmol, @eddie168, @morevgalex, @Kezi, @1beb, @Itoash, @kevinjd, @Foundry, @zihaomu, @Noblistic, @davetha, @dowjames, @Warzuponus, @millaguie, @kepler_62f, @lowell80 and @Code000.
442 lines
21 KiB
C++
442 lines
21 KiB
C++
/* ref_hc.cpp -- the gated residual, Qwen4-Exp's replacement for a pre-norm and a residual add.
|
|
*
|
|
* ============================== What it is ==============================
|
|
*
|
|
* The residual stream is `hc` times as wide as the model: hidden_size 2560 at hc_count 4 is a
|
|
* 10240-wide stream through every layer, entered as four identical copies of the embedding and
|
|
* collapsed once at the end. Each block reads a 2560 mix of the four and writes its output back
|
|
* into all four; both the read's mix and the write's per-stream gain are data-dependent ("gated"),
|
|
* so this is a pair of ops rather than a norm.
|
|
*
|
|
* hc_read N = grouped_rmsnorm(h) one rsqrt per 2560 stream, one gain of
|
|
* 10240 over all four, `1 + w`
|
|
* g = sigmoid(W_up @ silu(W_down @ N / hc))
|
|
* x = mean_over_streams(g * N) the block's 2560 input
|
|
* inj[s] = 2 * sigmoid(W_inj[s] . N / hc) four scalars in (0, 2)
|
|
* hc_write h[s] += inj[s] * y for each stream s
|
|
*
|
|
* The write reads the unnormed `h`, not `N`: upstream computes `hyper_input + injection` with
|
|
* `hyper_input` the read's argument, so the normalisation feeds only the block input and the gates.
|
|
* Writing into `N` would re-normalise the residual at each of the 96 connections and diverge within
|
|
* a few layers while still producing fluent text.
|
|
*
|
|
* The mix is a mean (`.mean(dim=-2)` over hc), and both gate arguments are separately divided by
|
|
* hc (`/ self.hc_count` before the silu and before the inject sigmoid): three independent
|
|
* divisions, all upstream's.
|
|
*
|
|
* ============================== Why the quantiser is in the read ==============================
|
|
*
|
|
* `q` and `s` are optional outputs. Present, the read also emits the fp8 block-scaled `x` the next
|
|
* projection's GEMM wants, keeping the launch count equal to a pre-norm model's (each launch has a
|
|
* fixed cost, and there are 96 connections). Absent, `x` is bf16 and the caller quantises it: the
|
|
* final mixer feeds the bf16 lm_head.
|
|
*
|
|
* Codes are taken from `x` narrowed to its dtype, not from the f32 accumulator, as a fused kernel
|
|
* must (the GDN out-projection quantiser follows the same rule, libref/ref_gdn.cpp).
|
|
*
|
|
* `rq` and `rs` extend this one consumer further: had_quant_act_fp8 of the stored `x`, for expert
|
|
* planes stored with a 128-wide Hadamard.
|
|
*/
|
|
#include "ref_common.h"
|
|
#include "ref_ops.h"
|
|
#include "ref_planes.h"
|
|
|
|
#include <cmath>
|
|
|
|
using namespace ref;
|
|
|
|
/* ------------------------------------------------------------------ hc_enter */
|
|
/* The wide stream's initial value: hc identical copies of the embedding. Separate from the
|
|
* embedding lookup, whose output width is n. The streams diverge at the first connection, since
|
|
* each block writes into them with different per-stream gains. */
|
|
int ref_hc_enter(const RadArgs* a, RadStream) {
|
|
if (!have(a, 2)) return RAD_E_INVAL;
|
|
const RadTensor* x = &a->t[0];
|
|
const RadTensor* h = &a->t[1];
|
|
const int64_t n = p_int(a, "n", 0);
|
|
const int64_t hc = p_int(a, "hc", 0);
|
|
if (n <= 0 || hc <= 1) return RAD_E_SHAPE;
|
|
const int64_t rows = numel(x) / n;
|
|
if (rows <= 0 || numel(h) < rows * hc * n) return RAD_E_SHAPE;
|
|
REF_PAR_FOR(rows * hc * n)
|
|
for (int64_t r = 0; r < rows; ++r) {
|
|
const int64_t xb = rowoff(x, r, n), hb = rowoff(h, r, hc * n);
|
|
const int64_t xs = laststride(x), hs = laststride(h);
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
const float v = ldt(x, xb + i * xs);
|
|
for (int64_t c = 0; c < hc; ++c) stt(h, hb + (c * n + i) * hs, v);
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ hc_read */
|
|
int ref_hc_read(const RadArgs* a, RadStream) {
|
|
/* Not `have(a, 6)`: operand 4, the inject weight, is optional (the final mixer has none).
|
|
* Operands 0..3 and 5 are required. */
|
|
if (a->n_t < 6) return RAD_E_INVAL;
|
|
if (!rad_arg_in(a, 0) || !rad_arg_in(a, 1) || !rad_arg_in(a, 2) || !rad_arg_in(a, 3) ||
|
|
!rad_arg_in(a, 5)) return RAD_E_INVAL;
|
|
const RadTensor* h = &a->t[0];
|
|
const RadTensor* w = &a->t[1];
|
|
const RadTensor* down = &a->t[2];
|
|
const RadTensor* up = &a->t[3];
|
|
const RadTensor* injw = t_in(a, 4); /* absent on the final mixer */
|
|
const RadTensor* x = &a->t[5];
|
|
const RadTensor* inj = rad_arg_in(a, 6); /* an OUT; same optional test */
|
|
const RadTensor* q = rad_arg_in(a, 7);
|
|
const RadTensor* s = rad_arg_in(a, 8);
|
|
const RadTensor* rq = rad_arg_in(a, 9); /* the rotated fp8 form, for a w4a8h consumer */
|
|
const RadTensor* rs = rad_arg_in(a, 10);
|
|
|
|
const int64_t n = p_int(a, "n", 0);
|
|
const int64_t hc = p_int(a, "hc", 0);
|
|
const int64_t lr = p_int(a, "lowrank", 0);
|
|
const float eps = p_f32(a, "eps", 1e-6f);
|
|
const float wadd = p_f32(a, "wadd", 0.0f);
|
|
if (n <= 0 || hc <= 1 || lr <= 0) return RAD_E_SHAPE;
|
|
const int64_t hn = hc * n;
|
|
|
|
const int64_t rows = numel(h) / hn;
|
|
if (rows <= 0 || numel(x) < rows * n || numel(w) < hn) return RAD_E_SHAPE;
|
|
/* The mixing matrices are plain, or codes and a scale plane joined (ref_planes.h; `mix`
|
|
* e4m3), read element by element as the core's decoder gives them. A plain matrix's dtype is
|
|
* what the gate it feeds is rounded to; a joined one's gates are rounded to h's. */
|
|
ScaledRows jd, ju;
|
|
const bool jdown = is_joined(down), jup = is_joined(up);
|
|
if (jdown) RAD_REF_TRY(scaled_rows(down, lr, hn, &jd));
|
|
else if (numel(down) < lr * hn) return RAD_E_SHAPE;
|
|
if (jup) RAD_REF_TRY(scaled_rows(up, hn, lr, &ju));
|
|
else if (numel(up) < hn * lr) return RAD_E_SHAPE;
|
|
const uint32_t ddt = jdown ? h->dtype : down->dtype, udt = jup ? h->dtype : up->dtype;
|
|
if (injw && numel(injw) < hc * hn) return RAD_E_SHAPE;
|
|
/* An inject weight without an inject output would compute scalars nobody reads; refused. */
|
|
if (injw && (!inj || !inj->data || numel(inj) < rows * hc)) return RAD_E_SHAPE;
|
|
|
|
int64_t g = 0;
|
|
int64_t ngrp = 0;
|
|
if (q && q->data) {
|
|
g = p_int(a, "group", 0);
|
|
if (g <= 0 || g > n) g = n;
|
|
ngrp = (n + g - 1) / g;
|
|
if (numel(q) < rows * n || !s || !s->data || numel(s) < rows * ngrp) return RAD_E_SHAPE;
|
|
}
|
|
/* The rotated form rotates each scale group whole, so the group must be a power of two that
|
|
* tiles the row; no weight block was rotated across padding. The result is the
|
|
* rotated quantiser's (had_quant_act_i8g's at int8 codes) of the stored `x`. */
|
|
int64_t rg = 0;
|
|
if (rq) {
|
|
rg = p_int(a, "group", 0);
|
|
if (!pow2(rg) || n % rg != 0 || !rs || numel(rq) < rows * n || numel(rs) < rows * (n / rg))
|
|
return RAD_E_SHAPE;
|
|
if (rg > REF_HAD_MAX) return RAD_E_UNSUPPORTED;
|
|
}
|
|
|
|
REF_PAR_FOR(rows * hn * (2 * lr + hc))
|
|
for (int64_t r = 0; r < rows; ++r) {
|
|
const int64_t hb = rowoff(h, r, hn), xb = rowoff(x, r, n);
|
|
const int64_t hs = laststride(h), xs = laststride(x), ws = laststride(w);
|
|
|
|
/* ---- grouped norm: one rsqrt per stream over its n, then the full-width gain. N is
|
|
* rounded to h's dtype: upstream's norm returns `.type_as(x)`, so every product reads it
|
|
* rounded. */
|
|
std::vector<float> N((size_t)hn);
|
|
for (int64_t c = 0; c < hc; ++c) {
|
|
double ss = 0.0;
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
const float v = ldt(h, hb + (c * n + i) * hs);
|
|
ss += (double)v * (double)v;
|
|
}
|
|
const float sc = 1.0f / std::sqrt((float)(ss / (double)n) + eps);
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
const int64_t j = c * n + i;
|
|
N[(size_t)j] = round_dt(h->dtype,
|
|
ldt(h, hb + j * hs) * sc * (ldt(w, j * ws) + wadd));
|
|
}
|
|
}
|
|
|
|
/* ---- read gate: hc*n -> lowrank -> hc*n, silu between and sigmoid after. */
|
|
std::vector<float> d((size_t)lr);
|
|
for (int64_t j = 0; j < lr; ++j) {
|
|
float acc = 0.0f;
|
|
const int64_t db = jdown ? 0 : rowoff(down, j, hn);
|
|
const int64_t ds = laststride(down);
|
|
for (int64_t i = 0; i < hn; ++i)
|
|
acc += N[(size_t)i] * (jdown ? jd.at(j, i) : ldt(down, db + i * ds));
|
|
d[(size_t)j] = round_dt(ddt, act_silu(acc / (float)hc));
|
|
}
|
|
|
|
/* ---- the gated mix, averaged over the streams. */
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
float acc = 0.0f;
|
|
for (int64_t c = 0; c < hc; ++c) {
|
|
const int64_t row = c * n + i;
|
|
const int64_t ub = jup ? 0 : rowoff(up, row, lr);
|
|
const int64_t us = laststride(up);
|
|
float gv = 0.0f;
|
|
for (int64_t j = 0; j < lr; ++j)
|
|
gv += d[(size_t)j] * (jup ? ju.at(row, j) : ldt(up, ub + j * us));
|
|
acc += round_dt(udt, act_sigmoid(gv)) * N[(size_t)row];
|
|
}
|
|
stt(x, xb + i * xs, acc / (float)hc);
|
|
}
|
|
|
|
/* ---- per-stream write gates. */
|
|
if (injw && inj) {
|
|
const int64_t ib = rowoff(inj, r, hc), is = laststride(inj);
|
|
for (int64_t c = 0; c < hc; ++c) {
|
|
float acc = 0.0f;
|
|
const int64_t jb = rowoff(injw, c, hn), js = laststride(injw);
|
|
for (int64_t i = 0; i < hn; ++i) acc += N[(size_t)i] * ldt(injw, jb + i * js);
|
|
stt(inj, ib + c * is, 2.0f * act_sigmoid(acc / (float)hc));
|
|
}
|
|
}
|
|
|
|
/* ---- optional fp8 form of x, from the stored values. */
|
|
if (q && q->data) {
|
|
const int64_t qb = rowoff(q, r, n), qs = laststride(q);
|
|
for (int64_t gi = 0; gi < ngrp; ++gi) {
|
|
const int64_t k0 = gi * g, k1 = (k0 + g < n) ? k0 + g : n;
|
|
const float sc = q8_group([&](int64_t i) { return ldt(x, xb + (k0 + i) * xs); },
|
|
k1 - k0, q->dtype == RAD_I8 ? 127.0f : 448.0f, q,
|
|
qb + k0 * qs, qs);
|
|
stt(s, rowoff(s, r, ngrp) + gi * laststride(s), sc);
|
|
}
|
|
}
|
|
|
|
/* ---- optional rotated form of x, from the same stored values: E4M3 codes, or int8
|
|
* (had_quant_act_i8g's) where the buffer is. */
|
|
if (rq) {
|
|
float buf[REF_HAD_MAX];
|
|
const int64_t qb = rowoff(rq, r, n), qs = laststride(rq), rgrp = n / rg;
|
|
for (int64_t gi = 0; gi < rgrp; ++gi) {
|
|
const int64_t k0 = gi * rg;
|
|
for (int64_t i = 0; i < rg; ++i) buf[i] = ldt(x, xb + (k0 + i) * xs);
|
|
fwht(buf, rg);
|
|
const float sc = q8_group([&](int64_t i) { return buf[i]; }, rg,
|
|
rq->dtype == RAD_I8 ? 127.0f : 448.0f, rq,
|
|
qb + k0 * qs, qs);
|
|
stt(rs, rowoff(rs, r, rgrp) + gi * laststride(rs), sc);
|
|
}
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ hc_write */
|
|
/* h[s] += inj[s] * y for each of the hc streams, in place on the stream the paired read was given
|
|
* (which the read left unchanged). */
|
|
int ref_hc_write(const RadArgs* a, RadStream) {
|
|
if (!have(a, 3)) return RAD_E_INVAL;
|
|
const RadTensor* y = &a->t[0];
|
|
const RadTensor* inj = &a->t[1];
|
|
const RadTensor* h = &a->t[2];
|
|
|
|
const int64_t n = p_int(a, "n", 0);
|
|
const int64_t hc = p_int(a, "hc", 0);
|
|
if (n <= 0 || hc <= 1) return RAD_E_SHAPE;
|
|
const int64_t hn = hc * n;
|
|
|
|
const int64_t rows = numel(y) / n;
|
|
if (rows <= 0 || numel(h) < rows * hn || numel(inj) < rows * hc) return RAD_E_SHAPE;
|
|
|
|
REF_PAR_FOR(rows * hn)
|
|
for (int64_t r = 0; r < rows; ++r) {
|
|
const int64_t yb = rowoff(y, r, n), hb = rowoff(h, r, hn), ib = rowoff(inj, r, hc);
|
|
const int64_t ys = laststride(y), hs = laststride(h), is = laststride(inj);
|
|
for (int64_t c = 0; c < hc; ++c) {
|
|
const float gain = ldt(inj, ib + c * is);
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
const int64_t j = hb + (c * n + i) * hs;
|
|
stt(h, j, ldt(h, j) + gain * ldt(y, yb + i * ys));
|
|
}
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ hc_project */
|
|
/* The control vector's 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 an unprojected request's activations
|
|
* are the stock model's. (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.
|
|
*
|
|
* A pre-norm MTP head (Qwen3.5, Qwen3-Next, DeepSeek-V3) joins the trunk's hidden state and the
|
|
* next token's embedding with one `fc` over their concatenation (the architecture's MTP block:
|
|
* two rmsnorms into one buffer, one gemm_nt). Qwen4-Exp's hidden state is the wide stream and the
|
|
* checkpoint carries two square matrices,
|
|
*
|
|
* mtp.fc_hidden.weight [n, n] mtp.pre_fc_norm_hidden.weight [hc * n]
|
|
* mtp.fc_embedding.weight [n, n] mtp.pre_fc_norm_embedding.weight [n]
|
|
*
|
|
* so the hidden half is `fc_h` applied to each of the hc sub-streams (upstream's
|
|
* `.unflatten(-1, (hc_count, hidden_size))` idiom) and the embedding half is one n-wide product
|
|
* added into all of them, the broadcast the trunk enters the stream with (`hidden_states.repeat`).
|
|
*
|
|
* rs_e = rsqrt(mean(e^2) + eps)
|
|
* ne[j] = e[j] * rs_e * (w_e[j] + wadd)
|
|
* fe[i] = sum_j fc_e[i][j] * ne[j]
|
|
* rs[k] = rsqrt(mean(h[k*n .. k*n+n)^2) + eps) -- `ngroup` == hc
|
|
* nh[k][j] = h[k*n+j] * rs[k] * (w_h[k*n+j] + wadd)
|
|
* x[k*n+i] = sum_j fc_h[i][j] * nh[k][j] + fe[i]
|
|
*
|
|
* One op rather than six because the intermediates are different views of the same bytes and the
|
|
* ABI has no reinterpreting view: `nh` is [M*hc, n] to the GEMM and [M, hc*n] to the norm, while
|
|
* core/runtime/issue.cpp only narrows dim 0 or the last dimension with strides unchanged, so a
|
|
* row-pitch change is inexpressible. It also saves launches in the latency-critical draft round.
|
|
*
|
|
* `ngroup` is the one thing the checkpoint does not say: a [hc * n] gain fits both a grouped norm
|
|
* (hc rsqrts, one per sub-stream, as Qwen4ExpTextGatedResidual does) and a flat one (one rsqrt over
|
|
* the wide row). No per-op oracle can tell them apart; the draft acceptance rate can. `ngroup`
|
|
* makes it a declare-time choice.
|
|
*/
|
|
int ref_mtp_enter(const RadArgs* a, RadStream) {
|
|
if (!have(a, 7)) return RAD_E_INVAL;
|
|
const RadTensor* h = &a->t[0];
|
|
const RadTensor* e = &a->t[1];
|
|
const RadTensor* w_h = &a->t[2];
|
|
const RadTensor* w_e = &a->t[3];
|
|
const RadTensor* fc_h = &a->t[4];
|
|
const RadTensor* fc_e = &a->t[5];
|
|
const RadTensor* x = &a->t[6];
|
|
|
|
const int64_t n = p_int(a, "n", 0);
|
|
const int64_t hc = p_int(a, "hc", 0);
|
|
const float eps = p_f32(a, "eps", 1e-6f);
|
|
const float wadd = p_f32(a, "wadd", 0.0f);
|
|
int64_t ngroup = p_int(a, "ngroup", hc);
|
|
if (n <= 0 || hc <= 1) return RAD_E_SHAPE;
|
|
if (ngroup != 1 && ngroup != hc) return RAD_E_SHAPE;
|
|
const int64_t hn = hc * n;
|
|
|
|
const int64_t rows = numel(h) / hn;
|
|
if (rows <= 0 || numel(e) < rows * n || numel(x) < rows * hn) return RAD_E_SHAPE;
|
|
if (numel(w_h) < hn || numel(w_e) < n) return RAD_E_SHAPE;
|
|
/* The fc matrices are plain, or codes and a scale plane joined (`fc` e4m3), as hc_read's. */
|
|
ScaledRows jh, je;
|
|
const bool jfh = is_joined(fc_h), jfe = is_joined(fc_e);
|
|
if (jfh) RAD_REF_TRY(scaled_rows(fc_h, n, n, &jh));
|
|
else if (numel(fc_h) < n * n) return RAD_E_SHAPE;
|
|
if (jfe) RAD_REF_TRY(scaled_rows(fc_e, n, n, &je));
|
|
else if (numel(fc_e) < n * n) return RAD_E_SHAPE;
|
|
|
|
REF_PAR_FOR(rows * hn * n)
|
|
for (int64_t r = 0; r < rows; ++r) {
|
|
const int64_t hb = rowoff(h, r, hn), eb = rowoff(e, r, n), xb = rowoff(x, r, hn);
|
|
const int64_t hs = laststride(h), es = laststride(e), xs = laststride(x);
|
|
const int64_t whs = laststride(w_h), wes = laststride(w_e);
|
|
|
|
/* ---- embedding half: one norm and one n-wide product, shared by every stream. */
|
|
double ss = 0.0;
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
const float v = ldt(e, eb + i * es);
|
|
ss += (double)v * (double)v;
|
|
}
|
|
const float rse = 1.0f / std::sqrt((float)(ss / (double)n) + eps);
|
|
std::vector<float> ne((size_t)n);
|
|
for (int64_t i = 0; i < n; ++i)
|
|
ne[(size_t)i] = round_dt(e->dtype,
|
|
ldt(e, eb + i * es) * rse * (ldt(w_e, i * wes) + wadd));
|
|
|
|
std::vector<float> fe((size_t)n);
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
float acc = 0.0f;
|
|
const int64_t fb = jfe ? 0 : rowoff(fc_e, i, n);
|
|
const int64_t fs = laststride(fc_e);
|
|
for (int64_t j = 0; j < n; ++j)
|
|
acc += ne[(size_t)j] * (jfe ? je.at(i, j) : ldt(fc_e, fb + j * fs));
|
|
fe[(size_t)i] = acc;
|
|
}
|
|
|
|
/* ---- hidden half: the norm (grouped, or flat per `ngroup`), then the same square matrix
|
|
* over each stream. */
|
|
float flat = 0.0f;
|
|
if (ngroup == 1) {
|
|
double sw = 0.0;
|
|
for (int64_t j = 0; j < hn; ++j) {
|
|
const float v = ldt(h, hb + j * hs);
|
|
sw += (double)v * (double)v;
|
|
}
|
|
flat = 1.0f / std::sqrt((float)(sw / (double)hn) + eps);
|
|
}
|
|
|
|
std::vector<float> nh((size_t)n);
|
|
for (int64_t c = 0; c < hc; ++c) {
|
|
float sc = flat;
|
|
if (ngroup != 1) {
|
|
double sk = 0.0;
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
const float v = ldt(h, hb + (c * n + i) * hs);
|
|
sk += (double)v * (double)v;
|
|
}
|
|
sc = 1.0f / std::sqrt((float)(sk / (double)n) + eps);
|
|
}
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
const int64_t j = c * n + i;
|
|
nh[(size_t)i] = round_dt(h->dtype,
|
|
ldt(h, hb + j * hs) * sc * (ldt(w_h, j * whs) + wadd));
|
|
}
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
float acc = 0.0f;
|
|
const int64_t fb = jfh ? 0 : rowoff(fc_h, i, n);
|
|
const int64_t fs = laststride(fc_h);
|
|
for (int64_t j = 0; j < n; ++j)
|
|
acc += nh[(size_t)j] * (jfh ? jh.at(i, j) : ldt(fc_h, fb + j * fs));
|
|
stt(x, xb + (c * n + i) * xs, acc + fe[(size_t)i]);
|
|
}
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|