radiance 2.0.0

Radiance becomes a multi-model engine: one process serves several models behind one port, loads
each when a request names it, parks it in host memory or restores it from its container, and
shares the cards between them (docs/MULTIMODEL.md). The configuration is one YAML file; the
command line flags are gone (`radiance --config FILE`, hot reload on SIGHUP).

Formats. IQ8R (rotated int8) and IQR-V4 replace FP8 and MXFP4 as whole-model formats, with an
int8 KV cache (`kv-cache-dtype: i8`) in place of the fp8 one. Containers whose weights are block
FP8, MXFP4, M2XFP4, NVFP4 or ParoQuant are refused at startup by the name of the first such
weight. Not served until they have int8 containers: GLM-5.3-Flash, Ornith-1.5, Sage-A1.5,
Nemotron-3.5-Bolt-74B, Ling-3.0-flash, drama-3 and Anima's FP8 container; Nemotron-3.5-Bolt-E4B,
the Gemma 4 31B DFlash containers and MiniCPM5-2B bf16 serve without their drafter. 1.17.0
serves all of them.

Speed. Chained launches for Qwen3.8-27B, Gemma 4, Gemma 3 and the small bf16 models, a new
Qwen3.8-Flash-Next container (IQR-V4 experts, int8 trunk) with its decode step and prefill
reworked, and a reworked adaptive draft window.

Deployment. deploy/compose has two fleet files, tp1.yaml and tp2.yaml, each with its
configuration beside it, in place of the per-model files. `host-pool-mib` is a model's host pool
in all, split among its ranks. The dashboard is rebuilt: an hour of history kept by the server,
each accelerator's memory by model, model states over time.

Merged since 1.17.0: Nemotron-3.5-Bolt-74B-A9B by keptn42 (#339); GLM-5.3-Flash on four cards by
AnimusNull (#397); chained launches for Flash-Next's decode step (#396) and preempted requests
keeping their prefix and place (#405) by mici81; --prefill-interleave auto by Code000 (#410);
eight more libr3d kernels and the w4a4 reference by RikshaDriver (#403, #407, #408); libr3d
large-BAR and E4M3 decode fixes by neuromanMD (#438, #439); int4/int5 decode tiles by Kezi
(#426); attention for Nanbeige 4.3 33B by everplays (#417); expert_stats_report.py by baKon
(#431). Fixed: #398, #402, #419, #437 (dschmitz's patch); added the metrics of #392, and
multi-model serving as asked for in #367 and #109.

Plugin ABI 39: out-of-tree plugins need to be rebuilt.

Thanks to @keptn42, @AnimusNull, @mici81, @Code000, @RikshaDriver, @neuromanMD, @Kezi, @everplays, @baKon and @dschmitz.
This commit is contained in:
Deadcode committed 2026-11-14 22:32:28 +01:00
1 parent b4c04b6042
commit ddcb9c69bb
696 files changed
+120770 -56596

No files matched your search

+9 -3
View File
@@ -1453,9 +1453,15 @@ int GbnfGrammar::fill_mask(uint32_t* bitmask, int64_t n_words) const {
* probe sorts pointers in thread-local storage and copies stacks only to store. */
thread_local std::vector<const GbnfStack*> sorted;
sorted.clear();
for (const GbnfStack& s : stacks_) sorted.push_back(&s);
std::sort(sorted.begin(), sorted.end(),
[](const GbnfStack* a, const GbnfStack* b) { return *a < *b; });
bool is_sorted = true;
for (const GbnfStack& s : stacks_) {
if (is_sorted && !sorted.empty() && s < *sorted.back()) is_sorted = false;
sorted.push_back(&s);
}
if (!is_sorted) {
std::sort(sorted.begin(), sorted.end(),
[](const GbnfStack* a, const GbnfStack* b) { return *a < *b; });
}
const GbnfStateView key{ &sorted, partial_utf8_, complete(), n_words };
if (prog_->lookup(key, bitmask, n_words)) return RAD_OK;
+61 -20
View File
@@ -149,12 +149,30 @@ struct TrieEntry {
static void trie_build(TokenTrie& t, const uint32_t* cps, std::vector<TrieEntry>& ents,
int32_t unique_over) {
const uint32_t n = (uint32_t)ents.size();
std::sort(ents.begin(), ents.end(), [cps](const TrieEntry& a, const TrieEntry& b) {
const uint32_t* pa = cps + a.off;
const uint32_t* pb = cps + b.off;
while (*pa != 0 && *pa == *pb) { ++pa; ++pb; }
return *pa < *pb;
});
/* Sort by a packed key of the first three code points (21 bits each; the terminator is 0, as
* is the padding past a shorter sequence, which is exactly the comparison's own rule), and
* walk the strings only where the keys tie: a vocabulary's 48k entries nearly all differ
* inside three code points, so the sort compares integers where it walked both strings. */
{
struct Keyed { uint64_t key; TrieEntry e; };
std::vector<Keyed> keyed;
keyed.reserve(n);
for (const TrieEntry& e : ents) {
const uint32_t* p = cps + e.off;
uint64_t k = p[0];
k = (k << 21) | (p[0] ? p[1] : 0u);
k = (k << 21) | (p[0] && p[1] ? p[2] : 0u);
keyed.push_back({ k, e });
}
std::sort(keyed.begin(), keyed.end(), [cps](const Keyed& a, const Keyed& b) {
if (a.key != b.key) return a.key < b.key;
const uint32_t* pa = cps + a.e.off;
const uint32_t* pb = cps + b.e.off;
while (*pa != 0 && *pa == *pb) { ++pa; ++pb; }
return *pa < *pb;
});
for (uint32_t i = 0; i < n; ++i) ents[i] = keyed[i].e;
}
t.lex.resize(n);
for (uint32_t i = 0; i < n; ++i) t.lex[i] = ents[i].id;
@@ -374,11 +392,16 @@ public:
int32_t intern(GbnfStacks s) {
if (s.empty()) return -1;
/* A sort is n log n stack comparisons, each as long as the common prefix; charged as every
* element once per sort level, so a set too wide to sort within the bound is refused. */
* element once per sort level, so a set too wide to sort within the bound is refused. Sets
* built in canonical order only pay the adjacent checks that establish it. */
size_t elems = 0;
for (const GbnfStack& k : s) elems += k.size() + 1;
work_->add(elems * (1 + (uint64_t)std::bit_width(s.size())));
std::sort(s.begin(), s.end());
bool sorted = true;
for (size_t i = 0; i < s.size(); ++i) {
elems += s[i].size() + 1;
if (sorted && i && s[i] < s[i - 1]) sorted = false;
}
work_->add(elems * (sorted ? 1 : 1 + (uint64_t)std::bit_width(s.size())));
if (!sorted) std::sort(s.begin(), s.end());
s.erase(std::unique(s.begin(), s.end()), s.end());
return intern_sorted(std::move(s));
}
@@ -654,7 +677,15 @@ struct Walk {
};
struct WalkScratch {
std::vector<std::pair<uint32_t, int32_t>> cur, nxt;
/* A live (node, state) pair, with the state itself: states live in a deque (references
* survive interning), and carrying the pointer saves re-indexing it per visit. The id is
* still needed to build a successor on a memo miss. */
struct Live {
uint32_t n;
int32_t s;
const SetDfa::St* S;
};
std::vector<Live> cur, nxt;
};
static inline void admit(Walk& w, int32_t id) {
@@ -758,7 +789,7 @@ static void walk(SetDfa& dfa, int32_t start, Walk& w, OpWork& work, WalkScratch&
cur.clear();
{
const VisitState vs = visit_state(dfa, w, start);
if (walk_visit(w, work, nd, 0, vs, 0)) cur.push_back({ 0u, start });
if (walk_visit(w, work, nd, 0, vs, 0)) cur.push_back({ 0u, start, vs.S });
if (w.aborted) return;
}
@@ -770,9 +801,14 @@ static void walk(SetDfa& dfa, int32_t start, Walk& w, OpWork& work, WalkScratch&
return remap ? (uint64_t)(base | (nd[c].cp - 1)) : (uint64_t)nd[c].cp;
};
for (size_t e = 0; e < cur.size(); ++e) {
const uint32_t n = cur[e].first;
const int32_t s = cur[e].second;
const SetDfa::St& S = dfa.parted(s);
const uint32_t n = cur[e].n;
const int32_t s = cur[e].s;
/* parted() builds the intervals on first use; the walk visits states far oftener
* than it parts them, so the built check is inlined (likewise next(), below, whose
* memo S.next is read directly). The deque keeps S valid across the interning a
* miss does, and S.lo/S.next are assigned once, at parting. */
const SetDfa::St& S = *cur[e].S;
if (!S.parted) dfa.parted(s);
const uint32_t* lo = S.lo.data();
const size_t m = S.lo.size();
uint32_t c = nd[n].first + (depth == 1 && w.skip_first ? 1u : 0u);
@@ -792,7 +828,8 @@ static void walk(SetDfa& dfa, int32_t start, Walk& w, OpWork& work, WalkScratch&
[](uint64_t x, uint32_t y) { return x < y; }) - lo) - 1;
}
const uint64_t hi = i + 1 < m ? (uint64_t)lo[i + 1] : (uint64_t)1 << 33;
const int32_t ns = dfa.next(s, i);
int32_t ns = S.next[i];
if (ns == kUnbuilt) ns = dfa.next(s, i);
if (ns < 0) {
uint32_t a = c, b = c1;
while (a < b) {
@@ -806,7 +843,7 @@ static void walk(SetDfa& dfa, int32_t start, Walk& w, OpWork& work, WalkScratch&
const VisitState vs = visit_state(dfa, w, ns);
const uint32_t c0 = c;
for (; c < c1 && cp_of(c) < hi; ++c)
if (walk_visit(w, work, nd, c, vs, depth)) nxt.push_back({ c, ns });
if (walk_visit(w, work, nd, c, vs, depth)) nxt.push_back({ c, ns, vs.S });
work.add(8 + (c - c0));
if (w.aborted) return;
}
@@ -1805,13 +1842,17 @@ bool plan_step(const MaskPlan& P, const std::vector<const GbnfStack*>& stacks, P
std::vector<const GbnfStack*>& ps = P.step_ptrs;
ps.clear();
size_t elems = 0;
bool sorted = true;
for (const GbnfStack* k : stacks) {
if (sorted && !ps.empty() && *k < *ps.back()) sorted = false;
ps.push_back(k);
elems += k->size() + 1;
}
work.add(elems * (1 + (uint64_t)std::bit_width(ps.size())));
std::sort(ps.begin(), ps.end(),
[](const GbnfStack* a, const GbnfStack* b) { return *a < *b; });
work.add(elems * (sorted ? 1 : 1 + (uint64_t)std::bit_width(ps.size())));
if (!sorted) {
std::sort(ps.begin(), ps.end(),
[](const GbnfStack* a, const GbnfStack* b) { return *a < *b; });
}
ps.erase(std::unique(ps.begin(), ps.end(),
[](const GbnfStack* a, const GbnfStack* b) { return *a == *b; }),
ps.end());
+59 -4
View File
@@ -4,10 +4,12 @@
#include "gbnf.h"
#include <json-schema-to-grammar.h>
#include <nlohmann/json.hpp>
#include "util/json.h"
#include <algorithm>
#include <exception>
#include <map>
#include <mutex>
#include <new>
namespace rad {
@@ -21,8 +23,8 @@ int64_t grammar_mask_words(int64_t n_vocab) { return bitmask_words(n_vocab); }
static constexpr size_t kMaxSchemaDepth = 512;
/* Iterative, because the values it must catch are too deep to recurse over. */
static size_t json_depth(const nlohmann::ordered_json& root) {
std::vector<std::pair<const nlohmann::ordered_json*, size_t>> todo{ { &root, 1 } };
static size_t json_depth(const Json& root) {
std::vector<std::pair<const Json*, size_t>> todo{ { &root, 1 } };
size_t deepest = 0;
while (!todo.empty()) {
const auto [j, d] = todo.back();
@@ -34,6 +36,57 @@ static size_t json_depth(const nlohmann::ordered_json& root) {
return deepest;
}
namespace {
/* Converted grammars by schema text. A client resends its schema on every request, and
* conversion is the expensive half of what the compiled-program cache downstream cannot
* save (it keys on grammar text). Capped like that cache because keys come from clients;
* only successful conversions are kept, and conversion runs outside the lock. */
struct SchemaCache {
struct Entry {
std::string gbnf;
uint64_t used = 0;
};
std::mutex mu;
std::map<std::string, Entry> by_schema;
uint64_t clock = 0;
static constexpr size_t kMaxEntries = 32;
static constexpr size_t kMaxBytes = 64ull << 20; /* schema text plus grammar text */
bool find(const std::string& schema, std::string* out) {
std::lock_guard<std::mutex> lk(mu);
const auto it = by_schema.find(schema);
if (it == by_schema.end()) return false;
it->second.used = ++clock;
*out = it->second.gbnf;
return true;
}
void put(const std::string& schema, const std::string& gbnf) {
std::lock_guard<std::mutex> lk(mu);
by_schema[schema] = Entry{ gbnf, ++clock };
for (;;) {
size_t bytes = 0;
auto victim = by_schema.end();
for (auto it = by_schema.begin(); it != by_schema.end(); ++it) {
bytes += it->first.size() + it->second.gbnf.size();
if (victim == by_schema.end() || it->second.used < victim->second.used)
victim = it;
}
if ((by_schema.size() <= kMaxEntries && bytes <= kMaxBytes) ||
by_schema.size() <= 1)
break;
by_schema.erase(victim);
}
}
};
SchemaCache& schema_cache() {
static SchemaCache c;
return c;
}
} /* namespace */
/* Uses the vendored converter, the only JSON Schema compiler in the tree; its hardening for
* client data is in vendor/patches 0003 and 0004. Client schemas reach it from /v1/completions
* (`json_schema`, `response_format`) through the server's grammar bridge; on
@@ -48,8 +101,9 @@ int grammar_from_json_schema(const std::string& schema_json, const std::string&
if (err) *err = "this converter always names the top rule 'root'";
return RAD_E_UNSUPPORTED;
}
if (schema_cache().find(schema_json, out_gbnf)) return RAD_OK;
try {
const auto schema = nlohmann::ordered_json::parse(schema_json);
const auto schema = Json::parse(schema_json);
if (json_depth(schema) > kMaxSchemaDepth) {
if (err) *err = "the schema nests more than " + std::to_string(kMaxSchemaDepth) +
" levels deep";
@@ -64,6 +118,7 @@ int grammar_from_json_schema(const std::string& schema_json, const std::string&
if (err) *err = "the schema compiled to an empty grammar";
return RAD_E_INVAL;
}
schema_cache().put(schema_json, *out_gbnf);
return RAD_OK;
}
+64 -15
View File
@@ -4,6 +4,7 @@
#include "sampler.h"
#include "gbnf.h"
#include "../sched/draft.h" /* RAD_SCHED_MAX_SPEC */
#include "../util/simd.h"
#include <algorithm>
#include <chrono>
@@ -85,11 +86,34 @@ void Sampler::shard_mask(uint32_t* dst) const {
if (n > 0) std::memcpy(dst, mask_full_.data() + w0, (size_t)n * 4);
return;
}
for (int64_t i = 0; i < cfg_.n_vocab_local; ++i) {
const int64_t g = cfg_.vocab_off + i;
if (g >= cfg_.n_vocab) break;
if (mask_full_[(size_t)(g >> 5)] & (1u << (uint32_t)(g & 31)))
dst[i >> 5] |= 1u << (uint32_t)(i & 31);
/* The window is the full mask's bitstream shifted right by vocab_off: word i of the shard is
* (full[w0+i] >> sh) | (full[w0+i+1] << (32-sh)), which simd::bitstream_shr does a vector at
* a time where the per-bit loop did one bit (and one dependent read-modify-write) at a time.
* Bits at or past the last global token this shard may name are cleared after, as the loop's
* `g >= n_vocab` break cleared them: the shift can pull in bits past n_vocab that
* mask_full_'s tail word happens to hold. */
const int64_t w0 = cfg_.vocab_off >> 5;
const unsigned sh = (unsigned)(cfg_.vocab_off & 31);
const int64_t have = full - w0; /* source words at and after w0 */
const int64_t local = std::min<int64_t>(cfg_.n_vocab_local, cfg_.n_vocab - cfg_.vocab_off);
if (have <= 0 || local <= 0) return;
/* bitstream_shr pairs word i with i+1 and reads one past its count as zero. Given words-1, it
* reads source word words-1 as zero for dst[words-2], so that word's high bits are added
* after; the last word -- whose high half is a real source word when the shard is narrower
* than the source -- is finished by hand. */
const int64_t words = std::min<int64_t>(mask_words_, have);
if (words > 1) {
simd::bitstream_shr(dst, mask_full_.data() + w0, (size_t)(words - 1), sh);
dst[words - 2] |= mask_full_[(size_t)(w0 + words - 1)] << (32 - sh);
}
if (words >= 1) {
const int64_t i = words - 1;
const uint32_t hi = w0 + i + 1 < full ? mask_full_[(size_t)(w0 + i + 1)] : 0u;
dst[i] = (mask_full_[(size_t)(w0 + i)] >> sh) | (hi << (32 - sh));
}
if (local < words * 32) {
if (local & 31) dst[local >> 5] &= (1u << (uint32_t)(local & 31)) - 1u;
for (int64_t i = (local + 31) >> 5; i < words; ++i) dst[i] = 0;
}
}
@@ -695,7 +719,13 @@ int Sampler::build_step(const std::vector<Request*>& reqs, int64_t step) {
const int rep = dry_rep_limit(*brk, h + w0, (int)win);
if (rep < allowed) return false;
p.dry_rep_limit = rep < win ? rep : 0;
for (int64_t i = 0; i < win; ++i) f[i] = brk->is_single(h[w0 + i]) ? 1 : 0;
/* Marked from the indexed bitset when there is one (dry_prepare_breakers built it);
* is_single answers the same from the map for a set filled by hand. */
if (!brk->singles.empty())
simd::bitmap_marks(f, h + w0, (size_t)win, brk->singles.data(),
brk->singles.size());
else
for (int64_t i = 0; i < win; ++i) f[i] = brk->is_single(h[w0 + i]) ? 1 : 0;
any_brk_ = true;
return true;
};
@@ -902,11 +932,24 @@ int Sampler::build_step(const std::vector<Request*>& reqs, int64_t step) {
const int64_t len = n_total + dk;
const int64_t take = std::min<int64_t>(len, cfg_.max_hist);
int32_t* h = hist_stage_ + row * cfg_.max_hist;
for (int64_t i = 0; i < take; ++i) {
const int64_t src = len - take + i;
h[i] = src < n_prompt ? r->prompt[(size_t)src]
: src < n_total ? r->output[(size_t)(src - n_prompt)]
: anc[src - n_total];
/* The three sources are contiguous runs of one logical sequence, so the tail
* `take` long is at most three memcpys where the element loop branched per token
* (per row per step: at 8k of history the copies are the difference between a
* few microseconds of memcpy and tens of the branchy loop). */
{
int64_t i = 0, src = len - take;
auto seg = [&](const int32_t* p, int64_t base, int64_t end) {
if (src >= end || i >= take) return;
const int64_t n = std::min(end - src, take - i);
if (n > 0) {
std::memcpy(h + i, p + (src - base), (size_t)n * sizeof(int32_t));
i += n;
src += n;
}
};
seg(r->prompt.data(), 0, n_prompt);
seg(r->output.data(), n_prompt, n_total);
seg(anc, n_total, len);
}
p.hist_len = (int32_t)take;
if (pen) p.flags |= RAD_SP_PENALTY;
@@ -1189,8 +1232,14 @@ int Sampler::run(RadCtx* c, rad_buf logits, rad_buf out_tokens) {
const RadOperand o_tok = opd_rows(out_tokens, n_seq_);
auto issue = [&](rad_op op, std::initializer_list<RadOperand> ops) -> int {
std::vector<RadOperand> v(ops);
return rad_issue(c, op, v.data(), (int)v.size(), n_seq_);
/* On the stack, not in a vector: this runs for every sampler stage of every step and a
* heap allocation each was pure overhead beside the launch. Sixteen covers every call
* below (the widest is five operands); a wider one is refused, never truncated --
* operands are positional, so dropping one would be silent numerical garbage. */
if (ops.size() > 16) return RAD_E_INVAL;
RadOperand v[16];
std::memcpy(v, ops.begin(), ops.size() * sizeof(RadOperand));
return rad_issue(c, op, v, (int)ops.size(), n_seq_);
};
/* The logprobs summary reads the raw logits, before any stage writes them in place. Only for
@@ -1317,8 +1366,8 @@ float Sampler::raw_logit(const LpRow& L, int32_t token, float v) const {
if (p.flags & RAD_SP_PENALTY) {
int64_t n = (int64_t)L.hist.size(), o = 0;
if (p.penalty_last_n > 0 && n > p.penalty_last_n) { o = n - p.penalty_last_n; n = p.penalty_last_n; }
int64_t count = 0;
for (int64_t i = 0; i < n; ++i) count += L.hist[(size_t)(o + i)] == token;
const int64_t count = (int64_t)simd::count_eq_i32(
n > 0 ? L.hist.data() + o : L.hist.data(), (size_t)std::max<int64_t>(n, 0), token);
if (count > 0) {
y += (double)count * p.freq_penalty + p.pres_penalty;
y = y > 0.0 ? y * p.rep_penalty : y / p.rep_penalty;