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

+289 -1
View File
@@ -6,6 +6,7 @@
* [--fuzz N] random replies per model and request (default 300)
* [--long a,b,c] reply sizes the legacy path is timed at (default 4096,16384,65536)
* [--new-long a,b] sizes only the streaming parser is timed at (default 262144,1048576)
* [--reps N] runs of the per-token breakdown (default 5)
*
* A folder holds the checkpoint's tokenizer.json and chat_template.jinja. The model names are
* the ones tests/legacy/chatdiff.h knows -- qwen3.8, qwen3.6, minicpm5 -- which fix the BOS/EOS
@@ -28,10 +29,19 @@
* against streaming ("new"), and the streaming parser alone at sizes the legacy path cannot reach
* in reasonable time.
*
* And the whole host cost of a streamed token, by component: the decoder, the server's text
* bookkeeping, the parser, the reader bridge, the chunk JSON, and the tool-call grammar the
* sampler drives (accept_token, and fill_mask once triggered) in prose, call structure, string
* values and typed values -- with every SSE event held to nlohmann's own serialisation of it.
*
* Exit status is non-zero when any reply breaks the contract or a verdict is not allowed. */
#include "chatdiff.h"
#include "sample/grammar.h"
#include "sample/vocab_view.h"
#include "server/adapters.h"
#include "server/json_partial.h"
#include "server/oai.h"
#include "text/chatfmt.h"
#include "text/tokenizer.h"
#include <nlohmann/json.hpp>
@@ -275,11 +285,283 @@ Timing time_old(const Bench& b, const std::vector<std::string>& pieces) {
return t;
}
/* ------------------------------------------------------------------ the per-token breakdown */
/* The sampler's view of the real vocabulary, built as core/engine_bringup.cpp builds it: pieces
* are what the decoder chain writes to the output stream, special tokens rendered. */
class RealView final : public VocabView {
public:
int init(std::shared_ptr<const Vocab> v) {
v_ = std::move(v);
RAD_TRY(tok_.init(v_));
pieces_.resize((size_t)v_->n_tokens());
for (int32_t i = 0; i < v_->n_tokens(); ++i)
pieces_[(size_t)i] = tok_.piece(i, /*render_special=*/true);
return RAD_OK;
}
int32_t n_tokens() const override { return v_ ? v_->n_tokens() : 0; }
const std::string& token_piece(int32_t id) const override {
static const std::string kEmpty;
return (id >= 0 && (size_t)id < pieces_.size()) ? pieces_[(size_t)id] : kEmpty;
}
bool is_eog(int32_t id) const override { return v_ && v_->is_eog(id); }
int tokenize(const std::string& s, std::vector<int32_t>* out) const override {
return out ? tok_.encode(s, *out, false, false) : RAD_E_INVAL;
}
int tokenize_special(const std::string& s, std::vector<int32_t>* out) const override {
return out ? tok_.encode(s, *out, false, true) : RAD_E_INVAL;
}
private:
std::shared_ptr<const Vocab> v_;
Tokenizer tok_;
std::vector<std::string> pieces_;
};
double median(std::vector<double> v) {
std::sort(v.begin(), v.end());
return v.empty() ? 0.0 : v[v.size() / 2];
}
/* Median and spread over the repetitions, in nanoseconds per unit. */
std::string per(const std::vector<double>& secs, size_t units) {
char b[96];
const double lo = *std::min_element(secs.begin(), secs.end());
const double hi = *std::max_element(secs.begin(), secs.end());
std::snprintf(b, sizeof b, "%8.1f ns (%.1f..%.1f)", median(secs) / (double)units * 1e9,
lo / (double)units * 1e9, hi / (double)units * 1e9);
return b;
}
/* Every SSE event in `out` must be exactly what nlohmann writes for the value it carries: the
* payload parsed and dumped again gives the same bytes. A chunk writer that is not a DOM is held
* to the DOM's output this way. Returns the events that are not. */
size_t sse_not_canonical(const std::string& out, size_t* events) {
size_t bad = 0;
for (size_t at = 0; at < out.size();) {
const size_t end = out.find("\n\n", at);
if (end == std::string::npos || out.compare(at, 6, "data: ") != 0) return bad + 1;
++*events;
const std::string payload = out.substr(at + 6, end - at - 6);
const ojson j = ojson::parse(payload, nullptr, false);
if (j.is_discarded() || j.dump() != payload) {
if (bad++ < 3) std::fprintf(stderr, " NOT CANONICAL: %s\n", payload.c_str());
}
at = end + 2;
}
return bad;
}
/* THE HOST COST OF ONE STREAMED TOKEN, by component, on the real tokenizer: the decoder, the
* server's text bookkeeping, the reply reader, the chunk JSON, and the tool-call grammar the
* sampler runs (accept_token, and fill_mask once it has triggered). Each component runs over the
* whole reply on its own, `reps` times; the medians are reported, with the spread. Returns the
* contract violations it saw: chunks that are not canonical JSON, or a grammar that refused the
* reply. */
int breakdown(const Bench& b, const Tok& tk, const RealView& view, int reps) {
const Model& m = b.model();
/* A long write with string values, and calls with integer and boolean values. */
std::string text = long_reply(m, 262144);
const std::string vfirst = " x = compute(a, b)";
const size_t v0 = text.find(vfirst);
const size_t v1 = text.rfind(" \n") + 2;
std::string typed;
for (int i = 0; i < 40; ++i)
typed += C(m, "bash", { { "command", "grep -rn TODO src | head -" + std::to_string(i) },
{ "timeout", std::to_string(1000 * i) } }) + "\n" +
C(m, "configure", { { "verbose", i % 2 ? "true" : "false" },
{ "level", std::to_string(i) } }) + "\n";
const size_t t0 = text.size();
text += "\n" + typed;
std::vector<int32_t> ids;
tk.tok.encode(text, ids, false, true);
/* Pieces as the decoder hands them over, and where each token starts in the reply. */
std::vector<std::string> pieces;
std::vector<size_t> start;
{
Detokenizer d(tk.vocab, {}, false, b.prompt().preserved_tokens);
size_t at = 0;
for (int32_t id : ids) {
start.push_back(at);
std::string p = d.push(id);
at += p.size();
pieces.push_back(std::move(p));
}
pieces.push_back(d.flush());
}
const size_t n = ids.size();
std::printf(" per-token breakdown: %zu bytes, %zu tokens, %d runs, ns/token median "
"(min..max)\n", text.size(), n, reps);
std::vector<double> t_detok, t_text, t_parse, t_reader, t_stream;
size_t events = 0, bad = 0;
for (int r = 0; r < reps; ++r) {
auto c0 = clk::now();
{
Detokenizer d(tk.vocab, {}, false, b.prompt().preserved_tokens);
size_t bytes = 0;
for (int32_t id : ids) bytes += d.push(id).size();
bytes += d.flush().size();
if (bytes != text.size()) std::fprintf(stderr, " the decoder changed the reply\n");
}
t_detok.push_back(since(c0));
/* The server's side of a token: the choice's text grows, and the stable prefix -- a
* UTF-8 boundary, no half escape -- is what goes to the stream. */
c0 = clk::now();
{
std::string acc;
size_t emitted = 0, sent = 0;
for (const std::string& p : pieces) {
acc += p;
const size_t safe = server::json_stable_prefix(acc);
if (safe <= emitted) continue;
sent += std::string_view(acc).substr(emitted, safe - emitted).size();
emitted = safe;
}
if (sent > acc.size()) std::fprintf(stderr, " impossible\n");
}
t_text.push_back(since(c0));
c0 = clk::now();
{
std::unique_ptr<ReplyStream> s = b.parser().open();
std::vector<ReplyEvent> ev;
for (const std::string& p : pieces) {
s->feed(p, &ev);
ev.clear();
}
s->finish(&ev);
}
t_parse.push_back(since(c0));
server::ReplyParserBridge bridge(b.parser_ptr());
c0 = clk::now();
{
std::unique_ptr<server::IReplyReader> rd = bridge.open();
std::vector<server::ReplyDelta> ev;
for (const std::string& p : pieces) {
rd->feed(p, &ev);
ev.clear();
}
rd->finish(&ev);
}
t_reader.push_back(since(c0));
server::OaiRequest req;
req.id = "chatcmpl-breakdown";
req.model = m.name;
req.created = 1759700000;
req.parse_tools = true;
std::string all;
c0 = clk::now();
{
server::ChatDeltaStream st(req, 0, &bridge);
std::string o = st.first_chunk();
for (const std::string& p : pieces) {
std::string c = st.push(p, false);
if (r == 0) all += c;
}
o += st.final_chunk(server::Finish::Stop);
if (r == 0) all += o;
}
t_stream.push_back(since(c0));
if (r == 0) bad += sse_not_canonical(all, &events);
}
std::printf(" detokenize %s\n", per(t_detok, n).c_str());
std::printf(" server text + stable cut %s\n", per(t_text, n).c_str());
std::printf(" parser feed %s\n", per(t_parse, n).c_str());
std::printf(" reader bridge (w/ parser) %s\n", per(t_reader, n).c_str());
std::printf(" ChatDeltaStream::push %s (chunk JSON + SSE: %.1f ns)\n",
per(t_stream, n).c_str(), (median(t_stream) - median(t_reader)) / (double)n * 1e9);
std::printf(" %zu SSE events, %zu not canonical\n", events, bad);
int violations = (int)bad;
/* The grammar, as the server builds it for this request and the sampler drives it, one token
* at a time: a mask for every position once the grammar constrains, then the token. */
ChatGrammarRequest gq;
gq.tools = &agent_tools();
gq.generation_prompt = b.prompt().generation_prompt;
gq.parallel_tool_calls = b.request().parallel;
gq.extract_reasoning = b.request().reasoning;
ChatGrammar cg;
std::string why;
if (chat_format_grammar(b.format(), gq, &cg, &why) < 0 || cg.text.empty()) {
std::printf(" grammar: none (%s)\n", why.c_str());
return violations;
}
GrammarSpec spec;
spec.text = cg.text;
spec.lazy = cg.lazy;
for (const std::string& w : cg.trigger_words) {
std::vector<int32_t> t;
view.tokenize_special(w, &t);
if (t.size() == 1) spec.trigger_tokens.push_back(t[0]);
else spec.trigger_words.push_back(w);
}
const int64_t words = grammar_mask_words(view.n_tokens());
std::vector<uint32_t> mask((size_t)words);
enum { Lazy, Structure, Value, Typed, NReg };
const char* reg_name[NReg] = { "untriggered (prose)", "call structure", "string value",
"typed calls" };
std::vector<double> acc_s[NReg], mask_s[NReg];
size_t cnt[NReg] = {}, masks[NReg] = {};
for (int r = 0; r <= reps; ++r) { /* run 0 compiles the program and warms its cache */
std::unique_ptr<GrammarBackend> g;
if (grammar_create(spec, view, &g, &why) < 0) {
std::printf(" grammar: refused (%s)\n", why.c_str());
return violations + 1;
}
double a[NReg] = {}, k[NReg] = {};
size_t c[NReg] = {}, mk[NReg] = {};
for (size_t i = 0; i < n; ++i) {
const int reg = !g->constrains() ? Lazy
: start[i] >= t0 ? Typed
: start[i] >= v0 && start[i] < v1 ? Value : Structure;
++c[reg];
if (g->constrains()) {
const auto q0 = clk::now();
const int s = g->fill_mask(mask.data(), words);
k[reg] += since(q0);
++mk[reg];
if (s < 0 || !((mask[(size_t)ids[i] >> 5] >> (ids[i] & 31)) & 1u)) {
std::printf(" grammar: token %zu refused by the mask\n", i);
return violations + 1;
}
}
const auto q0 = clk::now();
const int s = g->accept_token(ids[i]);
a[reg] += since(q0);
if (s < 0) {
std::printf(" grammar: token %zu not accepted\n", i);
return violations + 1;
}
}
if (r == 0) continue;
for (int x = 0; x < NReg; ++x) {
acc_s[x].push_back(a[x]);
mask_s[x].push_back(k[x]);
cnt[x] = c[x];
masks[x] = mk[x];
}
}
for (int x = 0; x < NReg; ++x) {
if (!cnt[x]) continue;
std::printf(" grammar %-20s %6zu tokens: accept %s", reg_name[x], cnt[x],
per(acc_s[x], cnt[x]).c_str());
if (masks[x]) std::printf(", mask %s", per(mask_s[x], masks[x]).c_str());
std::printf("\n");
}
return violations;
}
} /* namespace */
int main(int argc, char** argv) {
std::vector<std::pair<std::string, std::string>> want; /* model name, folder */
int fuzz = 300;
int reps = 5;
std::vector<size_t> old_sizes = { 4096, 16384, 65536 };
std::vector<size_t> new_sizes = { 262144, 1048576 };
for (int i = 1; i < argc; ++i) {
@@ -293,6 +575,8 @@ int main(int argc, char** argv) {
fuzz = std::atoi(argv[++i]);
} else if (a == "--long" && i + 1 < argc) {
old_sizes = sizes(argv[++i]);
} else if (a == "--reps" && i + 1 < argc) {
reps = std::max(1, std::atoi(argv[++i]));
} else if (a == "--new-long" && i + 1 < argc) {
new_sizes = sizes(argv[++i]);
} else if (a[0] != '-') {
@@ -306,7 +590,7 @@ int main(int argc, char** argv) {
if (is_dir(a + "/" + d)) { want.push_back({ name, a + "/" + d }); break; }
} else {
std::fprintf(stderr, "usage: rad-chatparse <fixtures root> | --model name=dir ... "
"[--fuzz N] [--long a,b] [--new-long a,b]\n");
"[--fuzz N] [--long a,b] [--new-long a,b] [--reps N]\n");
return 2;
}
}
@@ -404,6 +688,10 @@ int main(int argc, char** argv) {
text.size(), p.size(), "-", nw.sse * 1e3, "", "-", nw.parser * 1e3,
nw.parser / text.size() * 1e9, nw.parser / p.size() * 1e6);
}
RealView view;
if (view.init(tk.vocab) < 0) return 1;
violations += breakdown(b, tk, view, reps);
}
std::printf("%s\n", violations ? "FAILED" : "ok");
return violations ? 1 : 0;