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.
353 lines
15 KiB
C++
353 lines
15 KiB
C++
/* clef.cpp -- clef's prompt, spans and answer (clef.h).
|
|
*
|
|
* Every string below is the release's own, from joint_schema_model.py: the system prompt, the
|
|
* state and schema framing, the field and option lines, and the noul defaults. The prompt is
|
|
* tokenised piece by piece and the ids concatenated, as encode_record does, because each span is
|
|
* the ids of one piece: BPE over the joined text could merge across a span's edge.
|
|
*/
|
|
#include "clef.h"
|
|
#include "systemone.h"
|
|
|
|
#include "rad_clef.h"
|
|
|
|
#include "util/json.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
namespace rad {
|
|
namespace server {
|
|
|
|
namespace {
|
|
|
|
const char* const kSystemPrompt =
|
|
"Read the complete state and schema. Decide every field jointly. Each answer "
|
|
"must be exactly one of that field's allowed options.";
|
|
|
|
/* question_options: each allowed option and its description (null for none). */
|
|
int question_options(const std::string& path, const std::string& type, const json& criteria,
|
|
ClefQuestion* q, std::vector<json>* descs, ApiError* err) {
|
|
if (type == "noul") {
|
|
json c = { { "true", "The proposition is true or the answer is yes." },
|
|
{ "false", "The proposition is false or the answer is no." } };
|
|
if (!criteria.is_null()) {
|
|
if (!criteria.is_object()) {
|
|
*err = err_bad(path + ".criteria", "must be an object with `true` and `false`, or absent");
|
|
return RAD_E_INVAL;
|
|
}
|
|
if (!only_keys(criteria, { "true", "false" }, path + ".criteria.", err)) return RAD_E_INVAL;
|
|
for (const auto& kv : criteria.items()) c[kv.key()] = kv.value();
|
|
}
|
|
q->options = { "true", "false" };
|
|
*descs = { c["true"], c["false"] };
|
|
q->response_order = q->options;
|
|
return RAD_OK;
|
|
}
|
|
if (type == "choice") {
|
|
if (!criteria.is_object() || criteria.empty()) {
|
|
*err = err_bad(path + ".criteria", "must be a non-empty object of option names to descriptions");
|
|
return RAD_E_INVAL;
|
|
}
|
|
std::vector<std::pair<std::string, json>> o;
|
|
for (const auto& kv : criteria.items()) {
|
|
o.emplace_back(kv.key(), kv.value());
|
|
q->response_order.push_back(kv.key());
|
|
}
|
|
/* Python sorts str keys by code point, which UTF-8 byte order preserves. */
|
|
std::sort(o.begin(), o.end(), [](const auto& a, const auto& b) { return a.first < b.first; });
|
|
for (auto& p : o) {
|
|
q->options.push_back(p.first);
|
|
descs->push_back(std::move(p.second));
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
if (!criteria.is_array() || criteria.empty()) {
|
|
*err = err_bad(path + ".criteria", "must be a non-empty array of level descriptions, lowest first");
|
|
return RAD_E_INVAL;
|
|
}
|
|
for (size_t i = 0; i < criteria.size(); ++i) {
|
|
q->options.push_back(std::to_string(i));
|
|
descs->push_back(criteria[i]);
|
|
}
|
|
q->response_order = q->options;
|
|
for (const json& c : criteria) q->legend.push_back(c.dump());
|
|
return RAD_OK;
|
|
}
|
|
|
|
double round4(double v) { return std::round(v * 10000.0) / 10000.0; }
|
|
|
|
} /* namespace */
|
|
|
|
std::string clef_render(const json& v) {
|
|
return v.is_string() ? v.get<std::string>() : py_json_compact_sorted(v);
|
|
}
|
|
|
|
int parse_clef_request(const std::string& body, const OaiDeps& d, const OaiLimits& lim,
|
|
const std::string& model_id, ClefRequest* out, ApiError* err) {
|
|
*out = ClefRequest{};
|
|
json b;
|
|
RAD_TRY(parse_request_object(body, lim, &b, err));
|
|
if (!only_keys(b, { "model", "state", "questions", "images" }, "", err)) return RAD_E_INVAL;
|
|
if (!names_loaded_model(b, model_id, err)) return RAD_E_INVAL;
|
|
if (!b.contains("state") || (b["state"].is_string() && b["state"].get<std::string>().empty())) {
|
|
*err = err_bad("state", "required: a non-empty string or any JSON value");
|
|
return RAD_E_INVAL;
|
|
}
|
|
if (b.contains("images") && !(b["images"].is_array() && b["images"].empty())) {
|
|
*err = err_not_implemented("images: this server reads clef's state as text only");
|
|
return RAD_E_UNSUPPORTED;
|
|
}
|
|
const json& qs = b.contains("questions") ? b["questions"] : json();
|
|
if (!qs.is_object() || qs.empty()) {
|
|
*err = err_bad("questions", "must be a non-empty object of named questions");
|
|
return RAD_E_INVAL;
|
|
}
|
|
|
|
const ITokenizer& tok = *d.tok;
|
|
auto ids = [&](const std::string& text) { return tok.encode(text, false, true); };
|
|
auto append = [](std::vector<int32_t>* to, const std::vector<int32_t>& v) {
|
|
to->insert(to->end(), v.begin(), v.end());
|
|
};
|
|
|
|
/* encode_record: the schema first, with spans relative to its own start. */
|
|
std::vector<int32_t> schema = ids("\n\nSCHEMA FIELDS:\n");
|
|
size_t index = 0;
|
|
for (const auto& kv : qs.items()) {
|
|
const std::string path = "questions." + kv.key();
|
|
const json& src = kv.value();
|
|
if (!src.is_object()) { *err = err_bad(path, "must be an object"); return RAD_E_INVAL; }
|
|
if (!only_keys(src, { "type", "instructions", "criteria" }, path + ".", err)) return RAD_E_INVAL;
|
|
const auto ty = src.find("type");
|
|
if (ty == src.end() || !ty->is_string() ||
|
|
(*ty != "noul" && *ty != "choice" && *ty != "score")) {
|
|
*err = err_bad(path + ".type", "must be noul, choice or score");
|
|
return RAD_E_INVAL;
|
|
}
|
|
ClefQuestion q;
|
|
q.name = kv.key();
|
|
q.type = ty->get<std::string>();
|
|
q.type_id = q.type == "noul" ? 0 : q.type == "choice" ? 1 : 2;
|
|
std::vector<json> descs;
|
|
RAD_TRY(question_options(path, q.type, src.contains("criteria") ? src.at("criteria") : json(),
|
|
&q, &descs, err));
|
|
|
|
append(&schema, ids("\nFIELD " + std::to_string(++index) + "\nID: " + q.name + "\nTYPE: " +
|
|
q.type + "\nINSTRUCTION: "));
|
|
json instructions = src.contains("instructions") ? src.at("instructions") : json();
|
|
if (instructions.is_null() || instructions == "") instructions = q.name;
|
|
q.question.first = (int64_t)schema.size();
|
|
append(&schema, ids(clef_render(instructions)));
|
|
q.question.second = (int64_t)schema.size();
|
|
append(&schema, ids("\nALLOWED OPTIONS:\n"));
|
|
for (size_t i = 0; i < q.options.size(); ++i) {
|
|
append(&schema, ids("OPTION " + std::to_string(i + 1) + ": "));
|
|
json semantics = { { "option_id", q.options[i] } };
|
|
if (!descs[i].is_null()) semantics["description"] = descs[i];
|
|
const int64_t start = (int64_t)schema.size();
|
|
append(&schema, ids(clef_render(semantics)));
|
|
q.option_spans.emplace_back(start, (int64_t)schema.size());
|
|
append(&schema, ids("\n"));
|
|
}
|
|
append(&schema, ids("END FIELD\n"));
|
|
/* A span the tokenizer left empty would make the head average nothing. */
|
|
if (q.question.first == q.question.second) {
|
|
*err = err_bad(path + ".instructions", "renders to no tokens");
|
|
return RAD_E_INVAL;
|
|
}
|
|
out->questions.push_back(std::move(q));
|
|
}
|
|
|
|
const std::vector<int32_t> prefix = ids(std::string("<|im_start|>system\n") + kSystemPrompt +
|
|
"<|im_end|>\n<|im_start|>user\nSTATE:\n");
|
|
const std::vector<int32_t> state = ids(clef_render(b["state"]));
|
|
const std::vector<int32_t> suffix =
|
|
ids("\n<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\nJOINT SCHEMA DECISIONS:");
|
|
const int64_t n = (int64_t)(prefix.size() + state.size() + schema.size() + suffix.size());
|
|
/* The context, or the head's own bound where that is less (abi/rad_clef.h). */
|
|
const int64_t cap = lim.max_ctx > 0 && lim.max_ctx < RAD_CLEF_MAX_PROMPT ? lim.max_ctx
|
|
: RAD_CLEF_MAX_PROMPT;
|
|
if (n > cap) {
|
|
ApiError e = err_bad("state", "the prompt is " + std::to_string(n) + " tokens; " +
|
|
(cap == lim.max_ctx ? "this deployment's context holds "
|
|
: "clef's head takes at most ") +
|
|
std::to_string(cap));
|
|
e.code = "context_length_exceeded";
|
|
e.n_prompt_tokens = n;
|
|
e.n_ctx = cap;
|
|
*err = e;
|
|
return RAD_E_INVAL;
|
|
}
|
|
const int64_t offset = (int64_t)(prefix.size() + state.size());
|
|
for (ClefQuestion& q : out->questions) {
|
|
q.question.first += offset;
|
|
q.question.second += offset;
|
|
for (TokenSpan& s : q.option_spans) { s.first += offset; s.second += offset; }
|
|
}
|
|
out->tokens = prefix;
|
|
append(&out->tokens, state);
|
|
append(&out->tokens, schema);
|
|
append(&out->tokens, suffix);
|
|
return RAD_OK;
|
|
}
|
|
|
|
int64_t clef_option_count(const ClefRequest& r) {
|
|
int64_t o = 0;
|
|
for (const ClefQuestion& q : r.questions) o += (int64_t)q.options.size();
|
|
return o;
|
|
}
|
|
|
|
int clef_words(const ClefRequest& r, std::vector<int32_t>* out, ApiError* err) {
|
|
const int64_t N = (int64_t)r.tokens.size(), Q = (int64_t)r.questions.size();
|
|
const int64_t O = clef_option_count(r);
|
|
int64_t L = 0;
|
|
for (const ClefQuestion& q : r.questions)
|
|
for (const TokenSpan& s : q.option_spans) L += s.second - s.first;
|
|
if (Q > RAD_CLEF_MAX_QUESTIONS) {
|
|
*err = err_bad("questions", "at most " + std::to_string(RAD_CLEF_MAX_QUESTIONS) +
|
|
" questions a request");
|
|
return RAD_E_INVAL;
|
|
}
|
|
if (O > RAD_CLEF_MAX_OPTIONS) {
|
|
*err = err_bad("questions", "at most " + std::to_string(RAD_CLEF_MAX_OPTIONS) +
|
|
" options a request, every question together");
|
|
return RAD_E_INVAL;
|
|
}
|
|
if (L > RAD_CLEF_MAX_LEX) {
|
|
*err = err_bad("questions", "the options render to " + std::to_string(L) +
|
|
" tokens; at most " + std::to_string(RAD_CLEF_MAX_LEX) +
|
|
" a request");
|
|
return RAD_E_INVAL;
|
|
}
|
|
/* The prompt cut at every span's edges. Spans are disjoint, so each is one whole segment. */
|
|
std::vector<int32_t> cuts = { 0, (int32_t)N };
|
|
for (const ClefQuestion& q : r.questions) {
|
|
cuts.push_back((int32_t)q.question.first);
|
|
cuts.push_back((int32_t)q.question.second);
|
|
for (const TokenSpan& s : q.option_spans) {
|
|
cuts.push_back((int32_t)s.first);
|
|
cuts.push_back((int32_t)s.second);
|
|
}
|
|
}
|
|
std::sort(cuts.begin(), cuts.end());
|
|
cuts.erase(std::unique(cuts.begin(), cuts.end()), cuts.end());
|
|
const int64_t S = (int64_t)cuts.size() - 1;
|
|
auto seg_at = [&](int64_t start) {
|
|
return (int32_t)(std::lower_bound(cuts.begin(), cuts.end(), (int32_t)start) - cuts.begin());
|
|
};
|
|
|
|
const RadClefWords w = rad_clef_words_layout((int32_t)N, (int32_t)Q, (int32_t)O, (int32_t)L,
|
|
(int32_t)S);
|
|
std::vector<int32_t>& v = *out;
|
|
v.assign((size_t)w.total, 0);
|
|
v[RAD_CLEF_W_N] = (int32_t)N;
|
|
v[RAD_CLEF_W_Q] = (int32_t)Q;
|
|
v[RAD_CLEF_W_O] = (int32_t)O;
|
|
v[RAD_CLEF_W_L] = (int32_t)L;
|
|
v[RAD_CLEF_W_S] = (int32_t)S;
|
|
std::copy(cuts.begin(), cuts.end(), v.begin() + w.seg_cu);
|
|
int64_t o = 0, l = 0;
|
|
for (int64_t qi = 0; qi < Q; ++qi) {
|
|
const ClefQuestion& q = r.questions[(size_t)qi];
|
|
v[(size_t)(w.q_seg + qi)] = seg_at(q.question.first);
|
|
v[(size_t)(w.type + qi)] = q.type_id;
|
|
v[(size_t)(w.opt_cu + qi)] = (int32_t)o;
|
|
for (const TokenSpan& s : q.option_spans) {
|
|
v[(size_t)(w.o_seg + o)] = seg_at(s.first);
|
|
v[(size_t)(w.o_q + o)] = (int32_t)qi;
|
|
v[(size_t)(w.lex_cu + o)] = (int32_t)l;
|
|
for (int64_t t = s.first; t < s.second; ++t) v[(size_t)(w.lex_ids + l++)] = r.tokens[(size_t)t];
|
|
++o;
|
|
}
|
|
}
|
|
v[(size_t)(w.opt_cu + Q)] = (int32_t)O;
|
|
v[(size_t)(w.lex_cu + O)] = (int32_t)L;
|
|
v[(size_t)w.last] = (int32_t)(N - 1);
|
|
v[(size_t)w.cu_q] = 0; v[(size_t)w.cu_q + 1] = (int32_t)Q;
|
|
v[(size_t)w.cu_o] = 0; v[(size_t)w.cu_o + 1] = (int32_t)O;
|
|
v[(size_t)w.cu_k] = 0; v[(size_t)w.cu_k + 1] = (int32_t)N;
|
|
return RAD_OK;
|
|
}
|
|
|
|
std::vector<int32_t> clef_score_ids(int64_t n_options) {
|
|
std::vector<int32_t> ids;
|
|
ids.reserve((size_t)(3 * n_options));
|
|
for (int k = 0; k < 3; ++k)
|
|
for (int64_t o = 0; o < n_options; ++o) ids.push_back((int32_t)(k * RAD_CLEF_MAX_OPTIONS + o));
|
|
return ids;
|
|
}
|
|
|
|
std::vector<float> clef_logits(const std::vector<float>& scores, int64_t n_options,
|
|
const ClefScalars& s) {
|
|
const double cap = std::log(100.0);
|
|
const double ps = std::exp(std::min(s.prior_logit_scale, cap));
|
|
const double js = std::exp(std::min(s.joint_logit_scale, cap));
|
|
const double gate = 1.0 / (1.0 + std::exp(-s.residual_gate));
|
|
std::vector<float> out((size_t)n_options, 0.0f);
|
|
if ((int64_t)scores.size() < 3 * n_options) return out;
|
|
for (int64_t o = 0; o < n_options; ++o) {
|
|
const double prior = scores[(size_t)o], cosine = scores[(size_t)(n_options + o)];
|
|
const double residual = scores[(size_t)(2 * n_options + o)];
|
|
out[(size_t)o] = (float)(ps * prior + gate * (js * cosine + residual));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
int clef_answer(const ClefQuestion& q, const std::vector<float>& logits, json* out, ApiError* err) {
|
|
if (logits.size() != q.options.size()) {
|
|
*err = err_internal("the head returned no logits for question " + q.name);
|
|
return RAD_E_STATE;
|
|
}
|
|
double peak = -std::numeric_limits<double>::infinity();
|
|
for (float l : logits) {
|
|
if (!std::isfinite(l)) {
|
|
*err = err_internal("question " + q.name + " scored a non-finite logit");
|
|
return RAD_E_STATE;
|
|
}
|
|
peak = std::max(peak, (double)l);
|
|
}
|
|
std::vector<double> p;
|
|
double total = 0.0;
|
|
for (float l : logits) total += p.emplace_back(std::exp((double)l - peak));
|
|
for (double& v : p) v /= total;
|
|
auto prob = [&](const std::string& option) {
|
|
return p[(size_t)(std::find(q.options.begin(), q.options.end(), option) - q.options.begin())];
|
|
};
|
|
|
|
/* systemone_answer. */
|
|
json a = json::object();
|
|
a["type"] = q.type;
|
|
if (q.type == "noul") {
|
|
a["noul"] = round4(prob("true"));
|
|
*out = std::move(a);
|
|
return RAD_OK;
|
|
}
|
|
json probs = json::object();
|
|
if (q.type == "choice") {
|
|
std::string best = q.response_order[0];
|
|
for (const std::string& o : q.response_order) if (prob(o) > prob(best)) best = o;
|
|
a["choice"] = best;
|
|
a["confidence"] = round4(prob(best));
|
|
for (const std::string& o : q.response_order) probs[o] = round4(prob(o));
|
|
a["probabilities"] = std::move(probs);
|
|
} else {
|
|
double expected = 0.0, top = 0.0;
|
|
json legend = json::object();
|
|
for (size_t i = 0; i < q.options.size(); ++i) {
|
|
expected += (double)i * p[i];
|
|
top = std::max(top, p[i]);
|
|
legend[q.options[i]] = json::parse(q.legend[i]);
|
|
probs[q.options[i]] = round4(p[i]);
|
|
}
|
|
a["score"] = round4(expected);
|
|
a["confidence"] = round4(top);
|
|
a["legend"] = std::move(legend);
|
|
a["probabilities"] = std::move(probs);
|
|
}
|
|
*out = std::move(a);
|
|
return RAD_OK;
|
|
}
|
|
|
|
} /* namespace server */
|
|
} /* namespace rad */
|