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.
279 lines
13 KiB
C++
279 lines
13 KiB
C++
#include "ref_common.h"
|
|
#include "ref_ops.h"
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
using namespace ref;
|
|
|
|
/* Routing and index head gates are F32 projections in GLM, even with BF16 hidden states. */
|
|
int ref_gemm_nt_f32(const RadArgs* a, RadStream) {
|
|
if (!have(a, 3)) return RAD_E_INVAL;
|
|
const auto &x = a->t[0], &w = a->t[1], &y = a->t[2];
|
|
const int64_t n = p_int(a, "N", 0), k = p_int(a, "K", 0);
|
|
if (n < 1 || k < 1 || x.rank != 2 || w.rank != 2 || y.rank != 2 || x.shape[0] < 1 ||
|
|
x.shape[1] != k || w.shape[0] != n || w.shape[1] != k || y.shape[0] != x.shape[0] ||
|
|
y.shape[1] != n)
|
|
return RAD_E_SHAPE;
|
|
if (x.dtype != RAD_BF16 || w.dtype != RAD_F32 || y.dtype != RAD_F32) return RAD_E_DTYPE;
|
|
if (!x.data || !w.data || !y.data) return RAD_E_INVAL;
|
|
#pragma omp parallel for schedule(static)
|
|
for (int64_t r = 0; r < x.shape[0]; ++r)
|
|
for (int64_t col = 0; col < n; ++col) {
|
|
float sum = 0;
|
|
for (int64_t j = 0; j < k; ++j)
|
|
sum += ldt(&x, r * x.stride[0] + j * x.stride[1]) *
|
|
ldt(&w, col * w.stride[0] + j * w.stride[1]);
|
|
stt(&y, r * y.stride[0] + col * y.stride[1], sum);
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* Single-group noaux_tc routing: correction changes selection, never the mixture weights. */
|
|
namespace {
|
|
enum class Score { Sigmoid, SqrtSoftplus, Softmax };
|
|
|
|
/* torch.nn.functional.softplus with its default threshold: x itself above 20. */
|
|
float softplus(float x) { return x > 20.0f ? x : std::log1p(std::exp(x)); }
|
|
|
|
/* The shared body: scores from the logits, the correction bias picks top_k experts, the unbiased
|
|
* scores of the picks are the weights, renormalised (+1e-20) when `norm`, times route_scale. */
|
|
int router_topk_scored(const RadArgs* a, Score score) {
|
|
if (!have(a, 4)) return RAD_E_INVAL;
|
|
const auto &lg = a->t[0], &bias = a->t[1], &ids = a->t[2], &weights = a->t[3];
|
|
const int64_t ne = p_int(a, "n_expert", 0), k = p_int(a, "top_k", 0);
|
|
const float scale = p_f32(a, "route_scale", 0);
|
|
const bool norm = p_int(a, "norm", 0) != 0;
|
|
if (ne < 1 || ne > 512 || k < 1 || k > ne || !std::isfinite(scale) || scale <= 0 ||
|
|
lg.rank != 2 || lg.shape[0] < 1 || lg.shape[1] != ne || bias.rank != 1 ||
|
|
bias.shape[0] != ne || bias.dtype != RAD_F32 || ids.dtype != RAD_I32 || ids.rank != 2 ||
|
|
weights.rank != 2 || ids.shape[0] != lg.shape[0] || weights.shape[0] != lg.shape[0] ||
|
|
ids.shape[1] != k || weights.shape[1] != k || !lg.data || !bias.data || !ids.data ||
|
|
!weights.data)
|
|
return RAD_E_SHAPE;
|
|
for (int64_t r = 0; r < lg.shape[0]; ++r) {
|
|
float scores[512], select[512], chosen[512];
|
|
int index[512];
|
|
float mx = -INFINITY, den = 0;
|
|
for (int64_t e = 0; e < ne; ++e) {
|
|
const float l = ldt(&lg, rowoff(&lg, r, ne) + e * laststride(&lg));
|
|
scores[e] = l;
|
|
mx = std::max(mx, l);
|
|
}
|
|
for (int64_t e = 0; e < ne; ++e) {
|
|
if (score == Score::Sigmoid) scores[e] = act_sigmoid(scores[e]);
|
|
else if (score == Score::SqrtSoftplus) scores[e] = std::sqrt(softplus(scores[e]));
|
|
else { scores[e] = std::exp(scores[e] - mx); den += scores[e]; }
|
|
}
|
|
for (int64_t e = 0; e < ne; ++e) {
|
|
if (score == Score::Softmax) scores[e] /= den;
|
|
select[e] = scores[e] + ldt(&bias, e * laststride(&bias));
|
|
}
|
|
float sum = 0;
|
|
for (int64_t j = 0; j < k; ++j) {
|
|
int best = -1;
|
|
for (int64_t e = 0; e < ne; ++e)
|
|
if (select[e] > -INFINITY && (best < 0 || select[e] > select[best])) best = (int)e;
|
|
if (best < 0) return RAD_E_SHAPE;
|
|
index[j] = best;
|
|
chosen[j] = scores[best];
|
|
sum += chosen[j];
|
|
select[best] = -INFINITY;
|
|
}
|
|
for (int64_t j = 0; j < k; ++j) {
|
|
st_int(ids.dtype, ids.data, rowoff(&ids, r, k) + j * laststride(&ids), index[j]);
|
|
stt(&weights, rowoff(&weights, r, k) + j * laststride(&weights),
|
|
chosen[j] * (norm ? scale / (sum + 1e-20f) : scale));
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
}
|
|
|
|
int ref_router_sigmoid_topk(const RadArgs* a, RadStream) {
|
|
return router_topk_scored(a, Score::Sigmoid);
|
|
}
|
|
|
|
/* router_sigmoid_topk with the score function named: "sigmoid", "sqrtsoftplus" (DeepSeek-V4's
|
|
* sqrt(softplus(logit))) or "softmax" (over all n_expert logits). */
|
|
int ref_router_scored_topk(const RadArgs* a, RadStream) {
|
|
const char* sc = p_str(a, "score", "");
|
|
Score score;
|
|
if (!std::strcmp(sc, "sigmoid")) score = Score::Sigmoid;
|
|
else if (!std::strcmp(sc, "sqrtsoftplus")) score = Score::SqrtSoftplus;
|
|
else if (!std::strcmp(sc, "softmax")) score = Score::Softmax;
|
|
else return RAD_E_UNSUPPORTED;
|
|
return router_topk_scored(a, score);
|
|
}
|
|
|
|
/* Group-limited noaux_tc routing (DeepSeek-V3's, Ling-3.0's): the experts fall into `n_group`
|
|
* equal groups, a group ranks by the sum of its two highest `p + bias`, and selection runs over
|
|
* the experts of the `topk_group` best groups only. Weights as in router_sigmoid_topk. */
|
|
int ref_router_sigmoid_group_topk(const RadArgs* a, RadStream) {
|
|
if (!have(a, 4)) return RAD_E_INVAL;
|
|
const auto &lg = a->t[0], &bias = a->t[1], &ids = a->t[2], &weights = a->t[3];
|
|
const int64_t ne = p_int(a, "n_expert", 0), k = p_int(a, "top_k", 0),
|
|
ng = p_int(a, "n_group", 0), kg = p_int(a, "topk_group", 0);
|
|
const float scale = p_f32(a, "route_scale", 0);
|
|
const bool norm = p_int(a, "norm", 0) != 0;
|
|
if (ne < 1 || ne > 512 || k < 1 || ng < 1 || ne % ng || ne / ng < 2 || kg < 1 || kg > ng ||
|
|
k > kg * (ne / ng) || !std::isfinite(scale) || scale <= 0 || lg.rank != 2 ||
|
|
lg.shape[0] < 1 || lg.shape[1] != ne || bias.rank != 1 || bias.shape[0] != ne ||
|
|
bias.dtype != RAD_F32 || ids.dtype != RAD_I32 || ids.rank != 2 || weights.rank != 2 ||
|
|
ids.shape[0] != lg.shape[0] || weights.shape[0] != lg.shape[0] || ids.shape[1] != k ||
|
|
weights.shape[1] != k || !lg.data || !bias.data || !ids.data || !weights.data)
|
|
return RAD_E_SHAPE;
|
|
const int64_t gsz = ne / ng;
|
|
for (int64_t r = 0; r < lg.shape[0]; ++r) {
|
|
float scores[512], select[512], chosen[512], gscore[512];
|
|
int index[512];
|
|
bool keep[512] = {};
|
|
for (int64_t e = 0; e < ne; ++e) {
|
|
scores[e] = act_sigmoid(ldt(&lg, rowoff(&lg, r, ne) + e * laststride(&lg)));
|
|
select[e] = scores[e] + ldt(&bias, e * laststride(&bias));
|
|
}
|
|
for (int64_t gi = 0; gi < ng; ++gi) {
|
|
float t0 = -INFINITY, t1 = -INFINITY;
|
|
for (int64_t e = gi * gsz; e < (gi + 1) * gsz; ++e) {
|
|
if (select[e] > t0) { t1 = t0; t0 = select[e]; }
|
|
else if (select[e] > t1) t1 = select[e];
|
|
}
|
|
gscore[gi] = t0 + t1;
|
|
}
|
|
for (int64_t j = 0; j < kg; ++j) {
|
|
int best = -1;
|
|
for (int64_t gi = 0; gi < ng; ++gi)
|
|
if (gscore[gi] > -INFINITY && (best < 0 || gscore[gi] > gscore[best]))
|
|
best = (int)gi;
|
|
if (best < 0) return RAD_E_SHAPE;
|
|
for (int64_t e = best * gsz; e < (best + 1) * gsz; ++e) keep[e] = true;
|
|
gscore[best] = -INFINITY;
|
|
}
|
|
float sum = 0;
|
|
for (int64_t j = 0; j < k; ++j) {
|
|
int best = -1;
|
|
for (int64_t e = 0; e < ne; ++e)
|
|
if (keep[e] && select[e] > -INFINITY && (best < 0 || select[e] > select[best]))
|
|
best = (int)e;
|
|
if (best < 0) return RAD_E_SHAPE;
|
|
index[j] = best;
|
|
chosen[j] = scores[best];
|
|
sum += chosen[j];
|
|
select[best] = -INFINITY;
|
|
}
|
|
for (int64_t j = 0; j < k; ++j) {
|
|
st_int(ids.dtype, ids.data, rowoff(&ids, r, k) + j * laststride(&ids), index[j]);
|
|
stt(&weights, rowoff(&weights, r, k) + j * laststride(&weights),
|
|
chosen[j] * (norm ? scale / (sum + 1e-20f) : scale));
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* Ling-3.0's clamp: the SiLU is taken first and its value clipped above, the up branch on both
|
|
* sides (vLLM's SwigluStepAndMul). swiglu_clamped clips the gate before the SiLU instead. */
|
|
int ref_swiglu_step(const RadArgs* a, RadStream) {
|
|
if (!have(a, 2)) return RAD_E_INVAL;
|
|
const auto &gu = a->t[0], &y = a->t[1];
|
|
const int64_t n = p_int(a, "n", 0);
|
|
const float limit = p_f32(a, "limit", 0);
|
|
if (n < 1 || !std::isfinite(limit) || limit <= 0 || gu.rank != 2 || y.rank != 2 || !gu.data ||
|
|
!y.data || y.shape[0] < 1 || gu.shape[0] != y.shape[0] || gu.shape[1] != 2 * n ||
|
|
y.shape[1] != n)
|
|
return RAD_E_SHAPE;
|
|
#pragma omp parallel for schedule(static)
|
|
for (int64_t r = 0; r < y.shape[0]; ++r)
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
const int64_t b = rowoff(&gu, r, 2 * n), s = laststride(&gu);
|
|
const float g = std::min(act_silu(ldt(&gu, b + i * s)), limit);
|
|
const float u = std::clamp(ldt(&gu, b + (n + i) * s), -limit, limit);
|
|
stt(&y, rowoff(&y, r, n) + i * laststride(&y), g * u);
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* GLM clamps the gate only above, but clamps the up branch on both sides. */
|
|
int ref_swiglu_clamped(const RadArgs* a, RadStream) {
|
|
if (!have(a, 2)) return RAD_E_INVAL;
|
|
const auto &gu = a->t[0], &y = a->t[1];
|
|
const int64_t n = p_int(a, "n", 0);
|
|
const float limit = p_f32(a, "limit", 0);
|
|
if (n < 1 || !std::isfinite(limit) || limit <= 0 || gu.rank != 2 || y.rank != 2 || !gu.data ||
|
|
!y.data || y.shape[0] < 1 || gu.shape[0] != y.shape[0] || gu.shape[1] != 2 * n ||
|
|
y.shape[1] != n)
|
|
return RAD_E_SHAPE;
|
|
#pragma omp parallel for schedule(static)
|
|
for (int64_t r = 0; r < y.shape[0]; ++r)
|
|
for (int64_t i = 0; i < n; ++i) {
|
|
const int64_t b = rowoff(&gu, r, 2 * n), s = laststride(&gu);
|
|
const float g = std::min(ldt(&gu, b + i * s), limit);
|
|
const float u = std::clamp(ldt(&gu, b + (n + i) * s), -limit, limit);
|
|
stt(&y, rowoff(&y, r, n) + i * laststride(&y), act_silu(g) * u);
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* router_scored_topk_vl: router_scored_topk with a per-row bias swap. HF's MoE gate swaps its
|
|
* correction bias for `bias_vl` on image-span tokens (image_mask in model.py's MoEGate.forward);
|
|
* the swap changes selection only, the weights stay the unbiased scores. `mask` is i32 [rows]. */
|
|
int ref_router_scored_topk_vl(const RadArgs* a, RadStream) {
|
|
if (!have(a, 6)) return RAD_E_INVAL;
|
|
const char* sc = p_str(a, "score", "");
|
|
Score score;
|
|
if (!std::strcmp(sc, "sigmoid")) score = Score::Sigmoid;
|
|
else if (!std::strcmp(sc, "sqrtsoftplus")) score = Score::SqrtSoftplus;
|
|
else if (!std::strcmp(sc, "softmax")) score = Score::Softmax;
|
|
else return RAD_E_UNSUPPORTED;
|
|
const auto &lg = a->t[0], &bias = a->t[1], &vl = a->t[2], &mask = a->t[3], &ids = a->t[4],
|
|
&weights = a->t[5];
|
|
const int64_t ne = p_int(a, "n_expert", 0), k = p_int(a, "top_k", 0);
|
|
const float scale = p_f32(a, "route_scale", 0);
|
|
const bool norm = p_int(a, "norm", 0) != 0;
|
|
if (ne < 1 || ne > 512 || k < 1 || k > ne || !std::isfinite(scale) || scale <= 0 ||
|
|
lg.rank != 2 || lg.shape[0] < 1 || lg.shape[1] != ne || bias.rank != 1 ||
|
|
bias.shape[0] != ne || bias.dtype != RAD_F32 || vl.rank != 1 || vl.shape[0] != ne ||
|
|
vl.dtype != RAD_F32 || mask.rank != 1 || mask.shape[0] < lg.shape[0] ||
|
|
mask.dtype != RAD_I32 || ids.dtype != RAD_I32 || ids.rank != 2 ||
|
|
weights.rank != 2 || ids.shape[0] != lg.shape[0] || weights.shape[0] != lg.shape[0] ||
|
|
ids.shape[1] != k || weights.shape[1] != k || !lg.data || !bias.data || !vl.data ||
|
|
!mask.data || !ids.data || !weights.data)
|
|
return RAD_E_SHAPE;
|
|
for (int64_t r = 0; r < lg.shape[0]; ++r) {
|
|
const RadTensor* bsel = ld_int(mask.dtype, mask.data, r * laststride(&mask)) != 0 ? &vl
|
|
: &bias;
|
|
float scores[512], select[512], chosen[512];
|
|
int index[512];
|
|
float mx = -INFINITY, den = 0;
|
|
for (int64_t e = 0; e < ne; ++e) {
|
|
const float l = ldt(&lg, rowoff(&lg, r, ne) + e * laststride(&lg));
|
|
scores[e] = l;
|
|
mx = std::max(mx, l);
|
|
}
|
|
for (int64_t e = 0; e < ne; ++e) {
|
|
if (score == Score::Sigmoid) scores[e] = act_sigmoid(scores[e]);
|
|
else if (score == Score::SqrtSoftplus) scores[e] = std::sqrt(softplus(scores[e]));
|
|
else { scores[e] = std::exp(scores[e] - mx); den += scores[e]; }
|
|
}
|
|
for (int64_t e = 0; e < ne; ++e) {
|
|
if (score == Score::Softmax) scores[e] /= den;
|
|
select[e] = scores[e] + ldt(bsel, e * laststride(bsel));
|
|
}
|
|
float sum = 0;
|
|
for (int64_t j = 0; j < k; ++j) {
|
|
int best = -1;
|
|
for (int64_t e = 0; e < ne; ++e)
|
|
if (select[e] > -INFINITY && (best < 0 || select[e] > select[best])) best = (int)e;
|
|
if (best < 0) return RAD_E_SHAPE;
|
|
index[j] = best;
|
|
chosen[j] = scores[best];
|
|
sum += chosen[j];
|
|
select[best] = -INFINITY;
|
|
}
|
|
for (int64_t j = 0; j < k; ++j) {
|
|
st_int(ids.dtype, ids.data, rowoff(&ids, r, k) + j * laststride(&ids), index[j]);
|
|
stt(&weights, rowoff(&weights, r, k) + j * laststride(&weights),
|
|
chosen[j] * (norm ? scale / (sum + 1e-20f) : scale));
|
|
}
|
|
}
|
|
return RAD_OK;
|
|
}
|