radiance 1.1.0

- The VRAM split has no ratio: --expert-vs-cache-ratio is gone (now an unknown option). The
  experts keep what the pinned host pool cannot hold, the KV cache takes the rest and lends idle
  blocks to the experts. Flash-Next at tp2: 7.9 GiB of KV a card, up from 4.7. Startup, /stats,
  /metrics and the dashboard report the most the cache can hold.
- Linear-state snapshots live in pinned host memory, not VRAM (--checkpoint-slots sizes the
  store). Prefix-cache misses on agent sessions fixed; a published conversation is no longer
  trimmed away.
- Any world size from the shipped container (baKon): --tp 3 on Flash-Next with no padded
  checkpoint -- the attention world, uneven delta-net heads, the fill op.
- Kernels (keptn42): moe_gemm_q decode loads and one-trip run search, hc_read's prefill form
  (+3.7% prefill at 18K), hc_up row groups up to eight, the fused router launch. Each is the rule
  where it was measured faster; no environment switches.
- Tool calls: a typed argument is always JSON (Python True/None no longer leak into arguments --
  Codeberg #21); nine further reply-parser fixes; the reply path inside a call is ~12x cheaper on
  the host.
- The kernel changes: prefill +2-3% and decode -2% a step on 2x R9700 at tp2, identical text.
This commit is contained in:
Deadcode committed 2026-10-06 14:04:49 +02:00
1 parent 9f5feb24ee
commit 82b5835c02
101 files changed
+5105 -3767

No files matched your search

+129 -42
View File
@@ -83,9 +83,17 @@ std::pair<uint32_t, const char*> decode_utf8(const char* src) {
std::pair<std::vector<uint32_t>, PartialUtf8> decode_utf8(const std::string& src,
PartialUtf8 partial_start) {
std::vector<uint32_t> cps;
const PartialUtf8 p = decode_utf8(src, partial_start, &cps);
return { std::move(cps), p };
}
PartialUtf8 decode_utf8(const std::string& src, PartialUtf8 partial_start,
std::vector<uint32_t>* out) {
static const int lookup[] = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 3, 4 };
const char* pos = src.c_str();
std::vector<uint32_t> cps;
std::vector<uint32_t>& cps = *out;
cps.clear();
cps.reserve(src.size() + 1);
uint32_t value = partial_start.value;
@@ -95,7 +103,7 @@ std::pair<std::vector<uint32_t>, PartialUtf8> decode_utf8(const std::string& src
const uint8_t next_byte = (uint8_t)*pos;
if ((next_byte >> 6) != 2) {
cps.push_back(0);
return { std::move(cps), PartialUtf8{ 0, -1 } };
return PartialUtf8{ 0, -1 };
}
value = (value << 6) + (next_byte & 0x3F);
++pos;
@@ -110,7 +118,7 @@ std::pair<std::vector<uint32_t>, PartialUtf8> decode_utf8(const std::string& src
if (n_remain < 0) {
cps.clear();
cps.push_back(0);
return { std::move(cps), PartialUtf8{ 0, n_remain } };
return PartialUtf8{ 0, n_remain };
}
const uint8_t mask = (uint8_t)((1 << (7 - n_remain)) - 1);
value = first_byte & mask;
@@ -123,7 +131,7 @@ std::pair<std::vector<uint32_t>, PartialUtf8> decode_utf8(const std::string& src
if (n_remain == 0) cps.push_back(value);
}
cps.push_back(0);
return { std::move(cps), PartialUtf8{ value, n_remain } };
return PartialUtf8{ value, n_remain };
}
} /* namespace gbnf */
@@ -830,22 +838,73 @@ struct GbnfState {
}
};
/* A STATE AS fill_mask HOLDS IT, NOT COPIED: the stacks through pointers, in the order the key
* sorts them into. It hashes and compares as the GbnfState it would become, so the cache is
* probed without building a key -- a copy of every stack, per mask -- and one is built only for a
* mask that is stored. */
struct GbnfStateView {
const std::vector<const GbnfStack*>* stacks;
PartialUtf8 partial;
bool allow_eog;
int64_t words;
};
struct GbnfStateHash {
size_t operator()(const GbnfState& k) const {
using is_transparent = void;
template <class Stacks, class Deref>
static size_t hash(const Stacks& stacks, Deref deref, PartialUtf8 partial, bool allow_eog,
int64_t words) {
size_t h = 1469598103934665603ull;
auto mix = [&h](size_t v) { h ^= v; h *= 1099511628211ull; };
for (const GbnfStack& s : k.stacks) {
for (const auto& x : stacks) {
const GbnfStack& s = deref(x);
mix(s.size());
for (const GbnfElement* e : s) mix((size_t)e);
}
mix(k.partial.value);
mix((size_t)(k.partial.n_remain + 1));
mix(k.allow_eog ? 1u : 0u);
mix((size_t)k.words);
mix(partial.value);
mix((size_t)(partial.n_remain + 1));
mix(allow_eog ? 1u : 0u);
mix((size_t)words);
return h;
}
size_t operator()(const GbnfState& k) const {
return hash(k.stacks, [](const GbnfStack& s) -> const GbnfStack& { return s; },
k.partial, k.allow_eog, k.words);
}
size_t operator()(const GbnfStateView& k) const {
return hash(*k.stacks, [](const GbnfStack* s) -> const GbnfStack& { return *s; },
k.partial, k.allow_eog, k.words);
}
};
struct GbnfStateEq {
using is_transparent = void;
bool operator()(const GbnfState& a, const GbnfState& b) const { return a == b; }
bool operator()(const GbnfStateView& v, const GbnfState& k) const {
if (k.allow_eog != v.allow_eog || k.words != v.words ||
k.partial.value != v.partial.value || k.partial.n_remain != v.partial.n_remain ||
k.stacks.size() != v.stacks->size())
return false;
for (size_t i = 0; i < k.stacks.size(); ++i)
if (k.stacks[i] != *(*v.stacks)[i]) return false;
return true;
}
bool operator()(const GbnfState& k, const GbnfStateView& v) const { return (*this)(v, k); }
};
/* The key a view becomes, for storing. */
static GbnfState state_of(const GbnfStateView& v) {
GbnfState k;
k.stacks.reserve(v.stacks->size());
for (const GbnfStack* s : *v.stacks) k.stacks.push_back(*s);
k.partial = v.partial;
k.allow_eog = v.allow_eog;
k.words = v.words;
return k;
}
struct GbnfProgram {
GbnfRuleSet rules;
GbnfStacks start; /* the root's alternates, advanced once */
@@ -864,7 +923,7 @@ struct GbnfProgram {
* speculative window revisiting a state the work again. */
mutable std::mutex mu;
struct Entry { std::vector<uint32_t> mask; uint64_t used = 0; };
mutable std::unordered_map<GbnfState, Entry, GbnfStateHash> masks;
mutable std::unordered_map<GbnfState, Entry, GbnfStateHash, GbnfStateEq> masks;
mutable uint64_t clock = 0;
/* A tool-calling schema's working set is on the order of a hundred states; 512 is room for a
@@ -873,7 +932,7 @@ struct GbnfProgram {
* that was computed. */
static constexpr size_t kMaxMasks = 512;
bool lookup(const GbnfState& k, uint32_t* dst, int64_t n_words) const {
bool lookup(const GbnfStateView& k, uint32_t* dst, int64_t n_words) const {
std::lock_guard<std::mutex> lk(mu);
auto it = masks.find(k);
if (it == masks.end()) return false;
@@ -1157,29 +1216,42 @@ int GbnfGrammar::accept_token_piece(int32_t token, const std::string& piece) {
RAD_WARN("gbnf: token %d cannot be accepted: %s; the request cannot be continued",
token, e.what());
stacks_.clear();
at_ = At{};
return RAD_E_FULL;
} catch (const std::bad_alloc&) {
RAD_ERR("gbnf: out of memory accepting token %d; the request cannot be continued", token);
stacks_.clear();
at_ = At{};
return RAD_E_NOMEM;
}
}
int GbnfGrammar::accept_token_piece_impl(int32_t token, const std::string& piece) {
const auto decoded = decode_utf8(piece, partial_utf8_);
const auto& cps = decoded.first;
const PartialUtf8 partial = decode_utf8(piece, partial_utf8_, &cps_);
const size_t n_cps = cps_.size() - 1; /* the terminating 0 is not a code point */
OpWork work;
GbnfStacks new_stacks;
SetBuild new_set(work);
new_stacks.reserve(stacks_.size());
/* THE STACKS THAT MATCH CODE POINTS ARE ADVANCED TOGETHER, one code point at a time, as
* accept_str does. Advancing each through the whole piece on its own gives the same set --
* the union of the parts is the part of the union -- but when many stacks converge on one
* continuation, as every alternative of a wide rule does once it matches, each of them
* re-expands that continuation, and the token costs the square of the width. */
GbnfStacks current;
* re-expands that continuation, and the token costs the square of the width.
*
* AND THEY ARE ADVANCED THROUGH THE MASK PLAN'S AUTOMATON (plan_step), which holds each stack
* set as a state with its successors memoised per interval of code points. The set is the
* one accept_chr builds; what changes is that a state seen before -- the same free-text
* value, token after token -- is a lookup per code point rather than every closure rebuilt,
* allocated and de-duplicated again. The set comes back sorted, which nothing reads: the
* mask cache sorts its key, and a mask is a union over the stacks.
*
* The new set is written over the one the previous token left (`spare_`) and the two are
* swapped, so a set of the same shape, token after token, allocates nothing; and when the
* automaton says the token left the set the state it was -- the steady state of a free-text
* value -- the set is not touched at all. */
current_.clear();
for (const auto& stack : stacks_) {
if (stack.empty()) continue;
const GbnfElement* pos = stack.back();
@@ -1190,19 +1262,37 @@ int GbnfGrammar::accept_token_piece_impl(int32_t token, const std::string& piece
if (!is_end_of_sequence(pos + 1)) next.push_back(pos + 1);
advance_stack(prog_->rules, next, new_stacks, new_set);
} else {
current.push_back(stack);
current_.push_back(&stack);
}
}
for (auto it = cps.begin(), end = cps.end() - 1; it != end && !current.empty(); ++it) {
GbnfStacks next;
SetBuild next_set(work);
for (const auto& cs : current) accept_chr(prog_->rules, cs, *it, next, next_set);
current = std::move(next);
GbnfStacks& out = spare_;
if (n_cps > 0 && !current_.empty()) {
/* `at_` names a state only when every stack steps over code points, so with it known no
* stack matched a token terminal above and the set is the stepped stacks alone. */
gbnf::PlanState at{ at_.gen, at_.id };
const bool same = gbnf::plan_step(*prog_->plan, current_, &at, cps_.data(), n_cps, &out,
work);
at_ = At{ at.gen, at.id };
if (same) {
partial_utf8_ = partial;
return RAD_OK;
}
} else {
out.resize(current_.size());
for (size_t i = 0; i < current_.size(); ++i) out[i].assign(current_[i]->begin(),
current_[i]->end());
at_ = At{};
}
for (auto& s : current) new_set.insert(new_stacks, std::move(s));
stacks_ = std::move(new_stacks);
partial_utf8_ = decoded.second;
/* What a token terminal matched goes first, then the rest without the ones it already holds;
* with nothing matched by token, `out` is the set as it stands -- a state of the automaton,
* or a subset of the set before, and either way without duplicates. */
if (!new_stacks.empty()) {
for (auto& s : out) new_set.insert(new_stacks, std::move(s));
out.swap(new_stacks);
at_ = At{};
}
stacks_.swap(out);
partial_utf8_ = partial;
if (stacks_.empty()) {
/* NOT AN ERROR HERE, WHICH IS WHY IT LOGS AT DEBUG. This is reached as a matter of course
@@ -1290,6 +1380,7 @@ int GbnfGrammar::accept_token(int32_t token) {
int GbnfGrammar::accept_str(const std::string& piece) {
const auto decoded = decode_utf8(piece, partial_utf8_);
const auto& cps = decoded.first;
at_ = At{};
try {
OpWork work;
@@ -1318,7 +1409,7 @@ int GbnfGrammar::accept_str(const std::string& piece) {
void GbnfGrammar::push_state() {
saved_.push_back(Snapshot{ stacks_, partial_utf8_, awaiting_trigger_, scan_, recent_ });
saved_.push_back(Snapshot{ stacks_, partial_utf8_, awaiting_trigger_, scan_, recent_, at_ });
}
int GbnfGrammar::rollback() {
@@ -1332,21 +1423,11 @@ int GbnfGrammar::rollback() {
awaiting_trigger_ = s.awaiting_trigger;
scan_ = s.scan;
recent_ = std::move(s.recent);
at_ = s.at;
saved_.pop_back();
return RAD_OK;
}
/* The cache key for the state the machine is in right now. */
GbnfState GbnfGrammar::state_key(bool allow_eog, int64_t n_words) const {
GbnfState k;
k.stacks = stacks_;
k.partial = partial_utf8_;
k.allow_eog = allow_eog;
k.words = n_words;
std::sort(k.stacks.begin(), k.stacks.end());
return k;
}
/* The mask for the current state, with no memo. */
int GbnfGrammar::mask_compute(uint32_t* bitmask, int64_t n_words) const {
std::fill(bitmask, bitmask + n_words, 0u);
@@ -1391,8 +1472,14 @@ int GbnfGrammar::fill_mask(uint32_t* bitmask, int64_t n_words) const {
* because a collision accepted on the hash alone would hand back a mask for a state the
* sequence is not in and admit the wrong token set. The key sorts the stacks: two requests
* that reach the same state by the same path produce the same order anyway, but the sort is
* what makes that a guarantee rather than an observation. */
const GbnfState key = state_key(complete(), n_words);
* what makes that a guarantee rather than an observation. The probe sorts pointers to the
* stacks, in storage of the calling thread's own, and copies them 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; });
const GbnfStateView key{ &sorted, partial_utf8_, complete(), n_words };
if (prog_->lookup(key, bitmask, n_words)) return RAD_OK;
prog_->computed.fetch_add(1, std::memory_order_relaxed);
@@ -1400,7 +1487,7 @@ int GbnfGrammar::fill_mask(uint32_t* bitmask, int64_t n_words) const {
if (st < 0) return st;
/* Store only a mask that stands: a dead state is not worth an entry and returns above. */
prog_->store(key, bitmask, n_words);
prog_->store(state_of(key), bitmask, n_words);
return RAD_OK;
}
+24 -2
View File
@@ -32,6 +32,11 @@
* stacks that match code points are advanced together rather than one at a time, and a
* large set is de-duplicated through an index. Each gives the same set as upstream; without
* them, stacks that converge on one continuation re-expand it once each.
* - Accepting a token steps the stacks that match code points through the mask plan's
* stack-set automaton (gbnf_mask.cpp, plan_step) rather than through a closure per stack per
* code point, and the grammar remembers which automaton state its stacks are. The set is the
* one upstream's accept builds; the test oracle keeps upstream's accept and every accepted
* token is held to it.
* - The token mask is not upstream's candidate walk. It is served from a plan precomputed when
* the grammar is compiled (gbnf_mask.cpp): per stack suffix, the tokens it admits whatever lies
* below it, and a small trie of what depends on what does. Upstream's walk survives only as
@@ -239,12 +244,23 @@ private:
uint64_t start;
};
/* The state of the mask plan's automaton `stacks_` is exactly, when it is one: which
* automaton (`gen`) and which state in it, or an id of -1 when that is not known. It lets
* accepting skip canonicalising the set, and skip rewriting it when a token leaves it where
* it was. Everything that sets `stacks_` other than accepting through the automaton resets
* it, or restores it with the stacks it describes. */
struct At {
uint64_t gen = 0;
int32_t id = -1;
};
struct Snapshot {
GbnfStacks stacks;
PartialUtf8 partial;
bool awaiting_trigger = false;
WordScanner::State scan;
std::vector<Recent> recent;
At at;
};
const VocabView* vocab_ = nullptr;
@@ -256,11 +272,10 @@ private:
std::shared_ptr<const GbnfProgram> prog_;
GbnfStacks stacks_;
PartialUtf8 partial_utf8_{};
At at_;
std::vector<Snapshot> saved_;
GbnfState state_key(bool allow_eog, int64_t n_words) const;
/* The mask itself, from the program's plan, with no memo. */
int mask_compute(uint32_t* bitmask, int64_t n_words) const;
@@ -278,6 +293,13 @@ private:
int accept_token_piece(int32_t token, const std::string& piece);
int accept_token_piece_impl(int32_t token, const std::string& piece);
/* Scratch for accepting, kept so a token in the steady state of a value allocates nothing:
* the piece's code points, the stacks that step over them, and the set before the last one
* accepted, whose storage the next one is written into. None of it is state. */
std::vector<uint32_t> cps_;
std::vector<const GbnfStack*> current_;
GbnfStacks spare_;
};
/* COMPILE A GRAMMAR, on the calling thread, into the program every GbnfGrammar::create for the
+29
View File
@@ -183,6 +183,10 @@ std::pair<uint32_t, const char*> decode_utf8(const char* src);
std::pair<std::vector<uint32_t>, PartialUtf8> decode_utf8(const std::string& src,
PartialUtf8 partial_start);
/* The same into `out`, whose capacity is reused; returns the carried state. */
PartialUtf8 decode_utf8(const std::string& src, PartialUtf8 partial_start,
std::vector<uint32_t>* out);
/* ============================================================ elements */
inline bool is_end_of_sequence(const GbnfElement* pos) {
@@ -331,5 +335,30 @@ int plan_build(const GbnfRuleSet& rules, uint32_t root, const std::shared_ptr<co
int plan_fill(const MaskPlan& plan, const GbnfStacks& stacks, PartialUtf8 partial, bool allow_eog,
uint32_t* bitmask, int64_t n_words);
/* STEP A STACK SET OVER CODE POINTS through the plan's automaton: `out` is the set accepting
* cps[0..n) one at a time leaves -- what accept_chr over every stack, code point by code point,
* builds -- sorted, without duplicates, written over what `out` held so its capacity is reused.
* `stacks` need not be sorted and must not point into `out`.
*
* `at` IS THE STATE THE CALLER'S SET IS, when it knows: on entry, a state an earlier step on this
* plan returned, which spares canonicalising the set, or an id of -1; on return, the state
* reached, or an id of -1 when that state holds an empty stack or a token terminal on top -- a
* set the caller does not step whole, so not one it can name next time. Valid only while the
* automaton that issued it lives; `gen` says which one that was. Returns true when the state
* reached is the one `at` named on entry: the caller's set already is it, and `out` is left
* alone. That is the steady state of a free-text value.
*
* The automaton memoises each state's successor per
* interval of code points, so a state seen before costs a lookup per code point where the stack
* machinery would rebuild every closure; inside a free-text value the state is the same one
* token after token. Throws GbnfLimit past the per-operation bounds. Thread-safe: it takes the
* plan's lock. */
struct PlanState {
uint64_t gen = 0;
int32_t id = -1;
};
bool plan_step(const MaskPlan& plan, const std::vector<const GbnfStack*>& stacks, PlanState* at,
const uint32_t* cps, size_t n, GbnfStacks* out, OpWork& work);
} /* namespace gbnf */
} /* namespace rad */
+75 -11
View File
@@ -909,6 +909,8 @@ struct MaskPlan {
struct Group { int32_t key; std::vector<int32_t> ctx; };
mutable std::vector<Group> groups;
mutable GbnfStack ctx;
mutable std::vector<const GbnfStack*> step_ptrs;
mutable uint64_t dfa_gen = 0; /* which automaton `dfa` is; see plan_step */
};
void MaskPlanDeleter::operator()(MaskPlan* p) const { delete p; }
@@ -1841,23 +1843,85 @@ static void fill_resume(const MaskPlan& P, SetDfa& dfa, const GbnfStacks& stacks
}
}
/* The plan's automaton, for one operation charged to `work`. The caller holds P.mu.
*
* The automaton is a memo, so past its bound it is dropped and rebuilt rather than refused: every
* state in it can be rediscovered, and a grammar whose stacks deepen with every token would
* otherwise hold a new state per token for as long as it is cached. */
static SetDfa& plan_dfa(const MaskPlan& P, OpWork& work) {
if (!P.dfa || P.dfa->elems() > kMaxDfaElems) {
if (P.dfa)
RAD_DEBUG("gbnf: the stack-set automaton holds %zu states, %zu stack elements; "
"rebuilding it", P.dfa->n_states(), P.dfa->elems());
P.dfa = std::make_unique<SetDfa>(P.rules);
++P.dfa_gen;
}
P.dfa->begin(&work);
return *P.dfa;
}
bool plan_step(const MaskPlan& P, const std::vector<const GbnfStack*>& stacks, PlanState* at,
const uint32_t* cps, size_t n, GbnfStacks* out, OpWork& work) {
std::lock_guard<std::mutex> lk(P.mu);
SetDfa& dfa = plan_dfa(P, work);
/* The set as a state: the one the caller names, or canonicalised through pointers -- sorted,
* without duplicates, and copied only if the automaton has not seen it. Charged as intern()
* charges a sort. */
const bool named = at->id >= 0 && at->gen == P.dfa_gen;
int32_t s0;
if (named) {
s0 = at->id;
work.add(stacks.size() + 1);
} else {
std::vector<const GbnfStack*>& ps = P.step_ptrs;
ps.clear();
size_t elems = 0;
for (const GbnfStack* k : stacks) {
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; });
ps.erase(std::unique(ps.begin(), ps.end(),
[](const GbnfStack* a, const GbnfStack* b) { return *a == *b; }),
ps.end());
s0 = dfa.intern_ptrs(ps);
}
int32_t s = s0;
/* One successor per code point: the interval the code point falls in, then that interval's
* successor, each built on first use and served from the memo after. */
for (size_t k = 0; k < n && s >= 0; ++k) {
const SetDfa::St& S = dfa.parted(s);
const size_t i = (size_t)(std::upper_bound(S.lo.begin(), S.lo.end(), cps[k]) -
S.lo.begin()) - 1;
work.add(8);
s = dfa.next(s, i);
}
if (named && s == s0) return true;
at->gen = P.dfa_gen;
if (s < 0) {
at->id = -1;
out->clear();
return false;
}
const SetDfa::St& S = dfa.st(s);
at->id = S.has_empty || !S.ttop.empty() ? -1 : s;
out->resize(S.stacks.size());
for (size_t i = 0; i < S.stacks.size(); ++i)
(*out)[i].assign(S.stacks[i].begin(), S.stacks[i].end());
return false;
}
int plan_fill(const MaskPlan& P, const GbnfStacks& stacks, PartialUtf8 partial, bool allow_eog,
uint32_t* bitmask, int64_t n_words) {
std::lock_guard<std::mutex> lk(P.mu);
std::fill(bitmask, bitmask + n_words, 0u);
OpWork work;
/* The automaton is a memo, so past its bound it is dropped and rebuilt rather than refused:
* every state in it can be rediscovered, and a grammar whose stacks deepen with every token
* would otherwise hold a new state per token for as long as it is cached. */
if (!P.dfa || P.dfa->elems() > kMaxDfaElems) {
if (P.dfa)
RAD_DEBUG("gbnf: the stack-set automaton holds %zu states, %zu stack elements; "
"rebuilding it", P.dfa->n_states(), P.dfa->elems());
P.dfa = std::make_unique<SetDfa>(P.rules);
}
SetDfa& dfa = *P.dfa;
dfa.begin(&work);
SetDfa& dfa = plan_dfa(P, work);
if (allow_eog)
for (int32_t id : P.vx->eog) set_bit(bitmask, id);