This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/server/json_partial.cpp
T
Deadcode 82b5835c02 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.
2026-10-06 14:04:49 +02:00

237 lines
7.8 KiB
C++

#include "json_partial.h"
namespace rad {
namespace server {
namespace {
struct Scanner {
std::string_view s;
size_t i = 0;
void ws() {
while (i < s.size() && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r')) ++i;
}
bool eof() const { return i >= s.size(); }
char peek() const { return s[i]; }
bool lit(std::string_view w) {
if (s.size() - i < w.size()) return false;
if (s.compare(i, w.size(), w) != 0) return false;
i += w.size();
return true;
}
bool str() {
if (eof() || s[i] != '"') return false;
++i;
while (!eof()) {
char c = s[i];
if (c == '\\') {
if (i + 1 >= s.size()) return false;
char e = s[i + 1];
if (e == 'u') {
if (i + 5 >= s.size()) return false;
i += 6;
} else {
i += 2;
}
continue;
}
if (c == '"') { ++i; return true; }
++i;
}
return false; /* unterminated */
}
bool num() {
size_t start = i;
if (!eof() && s[i] == '-') ++i;
size_t d0 = i;
while (!eof() && s[i] >= '0' && s[i] <= '9') ++i;
if (i == d0) { i = start; return false; }
if (!eof() && s[i] == '.') {
++i;
size_t d1 = i;
while (!eof() && s[i] >= '0' && s[i] <= '9') ++i;
if (i == d1) { i = start; return false; }
}
if (!eof() && (s[i] == 'e' || s[i] == 'E')) {
++i;
if (!eof() && (s[i] == '+' || s[i] == '-')) ++i;
size_t d2 = i;
while (!eof() && s[i] >= '0' && s[i] <= '9') ++i;
if (i == d2) { i = start; return false; }
}
return true;
}
bool value(int depth) {
/* A hard depth cap, because the input is model output and a runaway "[[[[[..." would
* otherwise recurse until the stack ends. 256 is far past any real tool schema. */
if (depth > 256) return false;
ws();
if (eof()) return false;
char c = peek();
if (c == '"') return str();
if (c == '{') return obj(depth + 1);
if (c == '[') return arr(depth + 1);
if (c == 't') return lit("true");
if (c == 'f') return lit("false");
if (c == 'n') return lit("null");
return num();
}
bool obj(int depth) {
++i; /* '{' */
ws();
if (eof()) return false;
if (peek() == '}') { ++i; return true; }
for (;;) {
ws();
if (!str()) return false;
ws();
if (eof() || peek() != ':') return false;
++i;
if (!value(depth)) return false;
ws();
if (eof()) return false;
if (peek() == ',') { ++i; continue; }
if (peek() == '}') { ++i; return true; }
return false;
}
}
bool arr(int depth) {
++i; /* '[' */
ws();
if (eof()) return false;
if (peek() == ']') { ++i; return true; }
for (;;) {
if (!value(depth)) return false;
ws();
if (eof()) return false;
if (peek() == ',') { ++i; continue; }
if (peek() == ']') { ++i; return true; }
return false;
}
}
};
} /* namespace */
bool json_complete(std::string_view s) {
Scanner sc{s};
if (!sc.value(0)) return false;
sc.ws();
return sc.eof();
}
size_t utf8_trunc(std::string_view s) {
size_t n = s.size();
if (n == 0) return 0;
/* Walk back over at most three continuation bytes to find the lead byte, then decide whether
* the sequence it starts is complete. */
size_t k = n;
size_t back = 0;
while (k > 0 && back < 4) {
unsigned char c = (unsigned char)s[k - 1];
if ((c & 0xC0) == 0x80) { --k; ++back; continue; }
size_t need = 1;
if ((c & 0x80) == 0x00) need = 1;
else if ((c & 0xE0) == 0xC0) need = 2;
else if ((c & 0xF0) == 0xE0) need = 3;
else if ((c & 0xF8) == 0xF0) need = 4;
else return n; /* not a lead byte: not our truncation to fix */
size_t have = n - (k - 1);
return have >= need ? n : k - 1;
}
return n;
}
size_t json_stable_prefix(std::string_view s) {
size_t n = utf8_trunc(s);
if (n == 0) return 0;
/* Back out of an unfinished escape. Count the run of backslashes immediately before the cut:
* an odd run means the last one is an opening escape whose payload has not arrived. */
size_t bs = 0;
while (bs < n && s[n - 1 - bs] == '\\') ++bs;
if (bs % 2 == 1) return n - 1;
/* Back out of an unfinished "\uXXXX". `k` is how many characters have arrived after the 'u',
* so the escape is complete at k == 4 and short before it. Only six bytes ever need looking
* at, which is why this is a loop and not a parser. */
for (size_t k = 0; k <= 4 && n >= k + 2; ++k) {
if (s[n - 1 - k] != 'u') continue;
if (s[n - 2 - k] != '\\') break;
size_t bs2 = 0, j = n - 2 - k;
while (j > 0 && s[j - 1] == '\\') { ++bs2; --j; }
if (bs2 % 2 == 1) break; /* the backslash was itself escaped */
if (k < 4) return n - 2 - k; /* fewer than four hex digits have arrived */
break;
}
return n;
}
bool json_quote_append(std::string& out, std::string_view s) {
static const char kHex[] = "0123456789abcdef";
const size_t mark = out.size();
out.reserve(mark + s.size() + 2);
out.push_back('"');
const unsigned char* p = (const unsigned char*)s.data();
const unsigned char* e = p + s.size();
const unsigned char* run = p; /* [run, p) is verified and goes out as it is */
while (p < e) {
const unsigned char c = *p;
if (c >= 0x80) {
/* Length, and the range the SECOND byte is restricted to, which is what rejects
* overlong forms, surrogates and anything past U+10FFFF without decoding. */
size_t len;
unsigned char lo = 0x80, hi = 0xBF;
if (c >= 0xC2 && c <= 0xDF) len = 2;
else if (c == 0xE0) { len = 3; lo = 0xA0; }
else if (c >= 0xE1 && c <= 0xEC) len = 3;
else if (c == 0xED) { len = 3; hi = 0x9F; }
else if (c >= 0xEE && c <= 0xEF) len = 3;
else if (c == 0xF0) { len = 4; lo = 0x90; }
else if (c >= 0xF1 && c <= 0xF3) len = 4;
else if (c == 0xF4) { len = 4; hi = 0x8F; }
else len = 0;
bool ok = len != 0 && (size_t)(e - p) >= len && p[1] >= lo && p[1] <= hi;
for (size_t k = 2; ok && k < len; ++k) ok = p[k] >= 0x80 && p[k] <= 0xBF;
if (!ok) {
out.resize(mark);
return false;
}
p += len;
continue;
}
if (c >= 0x20 && c != '"' && c != '\\') {
++p;
continue;
}
out.append((const char*)run, (size_t)(p - run));
switch (c) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b"; break;
case '\t': out += "\\t"; break;
case '\n': out += "\\n"; break;
case '\f': out += "\\f"; break;
case '\r': out += "\\r"; break;
default: {
const char u[6] = { '\\', 'u', '0', '0', kHex[c >> 4], kHex[c & 15] };
out.append(u, 6);
}
}
run = ++p;
}
out.append((const char*)run, (size_t)(p - run));
out.push_back('"');
return true;
}
} /* namespace server */
} /* namespace rad */