Archived
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.
405 lines
18 KiB
C++
405 lines
18 KiB
C++
#include "json-partial.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "util/json.h"
|
|
|
|
#include <string>
|
|
#include <regex>
|
|
|
|
using json = rad::Json;
|
|
|
|
enum common_json_stack_element_type {
|
|
COMMON_JSON_STACK_ELEMENT_OBJECT,
|
|
COMMON_JSON_STACK_ELEMENT_KEY,
|
|
COMMON_JSON_STACK_ELEMENT_ARRAY,
|
|
};
|
|
|
|
struct common_json_stack_element {
|
|
common_json_stack_element_type type;
|
|
std::string key;
|
|
};
|
|
|
|
bool common_json_parse(
|
|
const std::string & input,
|
|
const std::string & healing_marker,
|
|
common_json & out)
|
|
{
|
|
std::string::const_iterator it = input.begin();
|
|
const auto end = input.end();
|
|
return common_json_parse(it, end, healing_marker, out);
|
|
}
|
|
|
|
bool common_json_parse(
|
|
std::string::const_iterator & it,
|
|
const std::string::const_iterator & end,
|
|
const std::string & healing_marker,
|
|
common_json & out)
|
|
{
|
|
// What the healing below needs from a failed parse: where it stopped and which
|
|
// containers (and object key) were still open there. This was a SAX walk
|
|
// before the DOM swap; a small scanner over the (usually truncated) input produces the
|
|
// same stack and cut point. It only ever runs on text json::parse has already refused,
|
|
// and every healing candidate is re-parsed before it is believed, so the scanner tracks
|
|
// structure, not validity in the small (number and literal spellings are consumed, not
|
|
// checked).
|
|
struct json_error_locator {
|
|
std::size_t position = 0;
|
|
bool found_error = false;
|
|
std::vector<common_json_stack_element> stack;
|
|
|
|
void close_value() {
|
|
if (!stack.empty() && (stack.back().type == COMMON_JSON_STACK_ELEMENT_KEY)) {
|
|
stack.pop_back();
|
|
}
|
|
}
|
|
};
|
|
json_error_locator err_loc;
|
|
auto start = it;
|
|
{
|
|
const std::string text(it, end);
|
|
const char * s = text.data();
|
|
const size_t n = text.size();
|
|
size_t i = 0;
|
|
struct scan_stop {};
|
|
auto fail = [&](size_t at) {
|
|
err_loc.found_error = true;
|
|
err_loc.position = at;
|
|
throw scan_stop{};
|
|
};
|
|
std::function<void()> skip_ws = [&]() {
|
|
while (i < n && (s[i] == ' ' || s[i] == '\n' || s[i] == '\r' || s[i] == '\t')) {
|
|
++i;
|
|
}
|
|
};
|
|
std::function<bool()> scan_string = [&]() {
|
|
++i; // opening quote
|
|
while (true) {
|
|
if (i >= n) fail(n);
|
|
const char c = s[i];
|
|
if (c == '"') {
|
|
++i;
|
|
return true;
|
|
}
|
|
if (c == '\\') {
|
|
++i;
|
|
if (i >= n) fail(n);
|
|
if (s[i] == 'u') {
|
|
for (int k = 0; k < 4; ++k) {
|
|
++i;
|
|
if (i >= n || !std::isxdigit((unsigned char) s[i])) fail(i >= n ? n : i);
|
|
}
|
|
}
|
|
++i;
|
|
continue;
|
|
}
|
|
if ((unsigned char) c < 0x20) fail(i);
|
|
++i;
|
|
}
|
|
};
|
|
std::function<void(int)> scan_value = [&](int depth) {
|
|
if (depth > 512) fail(i);
|
|
skip_ws();
|
|
if (i >= n) fail(n);
|
|
const char c = s[i];
|
|
if (c == '{') {
|
|
++i;
|
|
err_loc.stack.push_back({COMMON_JSON_STACK_ELEMENT_OBJECT, ""});
|
|
skip_ws();
|
|
if (i < n && s[i] == '}') {
|
|
++i;
|
|
err_loc.stack.pop_back();
|
|
err_loc.close_value();
|
|
return;
|
|
}
|
|
while (true) {
|
|
skip_ws();
|
|
if (i >= n) fail(n);
|
|
if (s[i] != '"') fail(i);
|
|
scan_string();
|
|
err_loc.stack.push_back({COMMON_JSON_STACK_ELEMENT_KEY, ""});
|
|
skip_ws();
|
|
if (i >= n) fail(n);
|
|
if (s[i] != ':') fail(i);
|
|
++i;
|
|
scan_value(depth + 1);
|
|
skip_ws();
|
|
if (i >= n) fail(n);
|
|
if (s[i] == ',') {
|
|
++i;
|
|
continue;
|
|
}
|
|
if (s[i] == '}') {
|
|
++i;
|
|
err_loc.stack.pop_back();
|
|
err_loc.close_value();
|
|
return;
|
|
}
|
|
fail(i);
|
|
}
|
|
}
|
|
if (c == '[') {
|
|
++i;
|
|
err_loc.stack.push_back({COMMON_JSON_STACK_ELEMENT_ARRAY, ""});
|
|
skip_ws();
|
|
if (i < n && s[i] == ']') {
|
|
++i;
|
|
err_loc.stack.pop_back();
|
|
err_loc.close_value();
|
|
return;
|
|
}
|
|
while (true) {
|
|
scan_value(depth + 1);
|
|
skip_ws();
|
|
if (i >= n) fail(n);
|
|
if (s[i] == ',') {
|
|
++i;
|
|
continue;
|
|
}
|
|
if (s[i] == ']') {
|
|
++i;
|
|
err_loc.stack.pop_back();
|
|
err_loc.close_value();
|
|
return;
|
|
}
|
|
fail(i);
|
|
}
|
|
}
|
|
if (c == '"') {
|
|
scan_string();
|
|
err_loc.close_value();
|
|
return;
|
|
}
|
|
if (c == '-' || (c >= '0' && c <= '9')) {
|
|
while (i < n && (std::isdigit((unsigned char) s[i]) || s[i] == '-' || s[i] == '+' ||
|
|
s[i] == '.' || s[i] == 'e' || s[i] == 'E')) {
|
|
++i;
|
|
}
|
|
err_loc.close_value();
|
|
return;
|
|
}
|
|
if (std::isalpha((unsigned char) c)) {
|
|
while (i < n && std::isalpha((unsigned char) s[i])) {
|
|
++i;
|
|
}
|
|
err_loc.close_value();
|
|
return;
|
|
}
|
|
fail(i);
|
|
};
|
|
try {
|
|
scan_value(0);
|
|
skip_ws();
|
|
if (i < n) fail(i);
|
|
} catch (const scan_stop &) {
|
|
}
|
|
}
|
|
|
|
if (err_loc.found_error) {
|
|
it = start;
|
|
auto temptative_end = it + err_loc.position;
|
|
// LOG_DBG("Error at position %zu (is_end = %s): %s\n", err_loc.position, temptative_end == end ? "true" : "false", err_loc.exception_message.c_str());
|
|
|
|
auto input = std::string(it, temptative_end);
|
|
try {
|
|
out.json = json::parse(input);
|
|
// out.json = json::parse(it, temptative_end);
|
|
it = temptative_end;
|
|
return true;
|
|
} catch (const std::exception & ex) {
|
|
// No, needs healing.
|
|
LOG_DBG("Failed to parse up to error: %s: <<<%s>>>\n", ex.what(), std::string(it, temptative_end).c_str());
|
|
}
|
|
auto can_parse = [](const std::string & str) {
|
|
try {
|
|
auto _ = json::parse(str); // NOLINT
|
|
return true;
|
|
} catch (const std::exception &) {
|
|
return false;
|
|
}
|
|
};
|
|
if (!healing_marker.empty() && !err_loc.stack.empty()) {
|
|
std::string str(it, temptative_end);
|
|
auto last_non_sp_pos = str.find_last_not_of(" \n\r\t");
|
|
if (last_non_sp_pos == std::string::npos) {
|
|
throw std::runtime_error("Cannot heal a truncated JSON that stopped in an unknown location");
|
|
}
|
|
auto last_non_sp_char = str[last_non_sp_pos];
|
|
// Used to detect stops on a number, which may not be complete.
|
|
auto was_maybe_number = [&]() {
|
|
if (!str.empty() && std::isspace(str.back())) {
|
|
return false;
|
|
}
|
|
return std::isdigit(last_non_sp_char) ||
|
|
last_non_sp_char == '.' ||
|
|
last_non_sp_char == 'e' ||
|
|
last_non_sp_char == 'E' ||
|
|
last_non_sp_char == '-';
|
|
};
|
|
|
|
std::string closing;
|
|
for (size_t i = err_loc.stack.size(); i > 0; i--) {
|
|
auto & el = err_loc.stack[i - 1];
|
|
if (el.type == COMMON_JSON_STACK_ELEMENT_OBJECT) {
|
|
closing += "}";
|
|
} else if (el.type == COMMON_JSON_STACK_ELEMENT_ARRAY) {
|
|
closing += "]";
|
|
} else if (el.type != COMMON_JSON_STACK_ELEMENT_KEY) {
|
|
throw std::runtime_error("Unexpected stack element type");
|
|
}
|
|
}
|
|
|
|
// Matches a potentially partial unicode escape sequence, e.g. \u, \uX, \uXX, \uXXX, \uXXXX
|
|
static const std::regex partial_unicode_regex(R"(\\u(?:[0-9a-fA-F](?:[0-9a-fA-F](?:[0-9a-fA-F](?:[0-9a-fA-F])?)?)?)?$)");
|
|
|
|
auto is_high_surrogate = [&](const std::string & s) {
|
|
// Check if a partial of a high surrogate (U+D800-U+DBFF)
|
|
return s.length() >= 4 &&
|
|
s[0] == '\\' && s[1] == 'u' &&
|
|
std::tolower(s[2]) == 'd' &&
|
|
(s[3] == '8' || s[3] == '9' || std::tolower(s[3]) == 'a' || std::tolower(s[3]) == 'b');
|
|
};
|
|
|
|
// Initialize the unicode marker to a low surrogate to handle the edge case
|
|
// where a high surrogate (U+D800-U+DBFF) is immediately followed by a
|
|
// backslash (\)
|
|
std::string unicode_marker_padding = "udc00";
|
|
std::smatch last_unicode_seq;
|
|
|
|
if (std::regex_search(str, last_unicode_seq, partial_unicode_regex)) {
|
|
std::smatch second_last_seq;
|
|
std::string prelude = str.substr(0, last_unicode_seq.position());
|
|
|
|
// Pad the escape sequence with 0s until it forms a complete sequence of 6 characters
|
|
unicode_marker_padding = std::string(6 - last_unicode_seq.length(), '0');
|
|
|
|
if (is_high_surrogate(last_unicode_seq.str())) {
|
|
// If the sequence is a partial match for a high surrogate, add a low surrogate (U+DC00-U+UDFF)
|
|
unicode_marker_padding += "\\udc00";
|
|
} else if (std::regex_search(prelude, second_last_seq, partial_unicode_regex)) {
|
|
if (is_high_surrogate(second_last_seq.str())) {
|
|
// If this follows a high surrogate, pad it to be a low surrogate
|
|
if (last_unicode_seq.length() == 2) {
|
|
unicode_marker_padding = "dc00";
|
|
} else if (last_unicode_seq.length() == 3) {
|
|
unicode_marker_padding = "c00";
|
|
} else {
|
|
// The original unicode_marker_padding is already padded with 0s
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const auto & magic_seed = out.healing_marker.marker = healing_marker;//"$llama.cpp.json$";
|
|
|
|
if (err_loc.stack.back().type == COMMON_JSON_STACK_ELEMENT_KEY) {
|
|
// We're inside an object value
|
|
if (last_non_sp_char == ':' && can_parse(str + "1" + closing)) {
|
|
// Was about to create an object value
|
|
str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
|
|
} else if (can_parse(str + ": 1" + closing)) {
|
|
str += (out.healing_marker.json_dump_marker = ":\"" + magic_seed) + "\"" + closing;
|
|
} else if (last_non_sp_char == '{' && can_parse(str + closing)) {
|
|
// Was about to create an object
|
|
str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\": 1" + closing;
|
|
} else if (can_parse(str + "\"" + closing)) {
|
|
// Was inside an object value string
|
|
str += (out.healing_marker.json_dump_marker = magic_seed) + "\"" + closing;
|
|
} else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\"" + closing)) {
|
|
// Was inside an object value string after an escape
|
|
str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\"" + closing;
|
|
} else if (can_parse(str + unicode_marker_padding + "\"" + closing)) {
|
|
// Was inside an object value string after a partial unicode escape
|
|
str += (out.healing_marker.json_dump_marker = unicode_marker_padding + magic_seed) + "\"" + closing;
|
|
} else {
|
|
// find last :
|
|
auto last_pos = str.find_last_of(':');
|
|
if (last_pos == std::string::npos) {
|
|
throw std::runtime_error("Cannot heal a truncated JSON that stopped in an unknown location");
|
|
}
|
|
// Cutting back to opening : for object value
|
|
str = str.substr(0, last_pos + 1) + (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
|
|
}
|
|
} else if (err_loc.stack.back().type == COMMON_JSON_STACK_ELEMENT_ARRAY) {
|
|
if ((last_non_sp_char == ',' || last_non_sp_char == '[') && can_parse(str + "1" + closing)) {
|
|
// Was about to create an array value
|
|
str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
|
|
} else if (can_parse(str + "\"" + closing)) {
|
|
// Was inside an array value string
|
|
str += (out.healing_marker.json_dump_marker = magic_seed) + "\"" + closing;
|
|
} else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\"" + closing)) {
|
|
// Was inside an array value string after an escape
|
|
str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\"" + closing;
|
|
} else if (can_parse(str + unicode_marker_padding + "\"" + closing)) {
|
|
// Was inside an array value string after a partial unicode escape
|
|
str += (out.healing_marker.json_dump_marker = unicode_marker_padding + magic_seed) + "\"" + closing;
|
|
} else if (!was_maybe_number() && can_parse(str + ", 1" + closing)) {
|
|
// Had just finished a value
|
|
str += (out.healing_marker.json_dump_marker = ",\"" + magic_seed) + "\"" + closing;
|
|
} else {
|
|
auto last_pos = str.find_last_of("[,");
|
|
if (last_pos == std::string::npos) {
|
|
throw std::runtime_error("Cannot heal a truncated JSON array stopped in an unknown location");
|
|
}
|
|
// Cutting back to last [ or , for array value
|
|
str = str.substr(0, last_pos + 1) + (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
|
|
}
|
|
} else if (err_loc.stack.back().type == COMMON_JSON_STACK_ELEMENT_OBJECT) {
|
|
if ((last_non_sp_char == '{' && can_parse(str + closing)) ||
|
|
(last_non_sp_char == ',' && can_parse(str + "\"\": 1" + closing))) {
|
|
// Was about to create an object key+value
|
|
str += (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\": 1" + closing;
|
|
} else if (!was_maybe_number() && can_parse(str + ",\"\": 1" + closing)) {
|
|
// Was about to create an object key+value
|
|
str += (out.healing_marker.json_dump_marker = ",\"" + magic_seed) + "\": 1" + closing;
|
|
} else if (can_parse(str + "\": 1" + closing)) {
|
|
// Was inside an object key string
|
|
str += (out.healing_marker.json_dump_marker = magic_seed) + "\": 1" + closing;
|
|
} else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\": 1" + closing)) {
|
|
// Was inside an object key string after an escape
|
|
str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\": 1" + closing;
|
|
} else if (can_parse(str + unicode_marker_padding + "\": 1" + closing)) {
|
|
// Was inside an object key string after a partial unicode escape
|
|
str += (out.healing_marker.json_dump_marker = unicode_marker_padding + magic_seed) + "\": 1" + closing;
|
|
} else {
|
|
auto last_pos = str.find_last_of(':');
|
|
if (last_pos == std::string::npos) {
|
|
throw std::runtime_error("Cannot heal a truncated JSON object stopped in an unknown location");
|
|
}
|
|
// fprintf(stderr, "Cutting back to last : for object key+value\n");
|
|
str = str.substr(0, last_pos + 1) + (out.healing_marker.json_dump_marker = "\"" + magic_seed) + "\"" + closing;
|
|
}
|
|
} else {
|
|
throw std::runtime_error("Cannot heal a truncated JSON object stopped in an unknown location");
|
|
}
|
|
// fprintf(stderr, "HEALED:\nSTRING <<<\n%s\n>>>\n\nmagic_cut: <<<\n%s\n>>>\n\n", str.c_str(), out.healing_marker.json_dump_marker.c_str());
|
|
out.json = json::parse(str);
|
|
it = temptative_end;
|
|
return true;
|
|
}
|
|
// handle unclosed top-level primitive
|
|
if (err_loc.position != 0 && !healing_marker.empty() && err_loc.stack.empty()) {
|
|
std::string str(it, temptative_end);
|
|
const auto & magic_seed = out.healing_marker.marker = healing_marker;
|
|
if (can_parse(str + "\"")) {
|
|
// Was inside an string
|
|
str += (out.healing_marker.json_dump_marker = magic_seed) + "\"";
|
|
} else if (str[str.length() - 1] == '\\' && can_parse(str + "\\\"")) {
|
|
// Was inside an string after an escape
|
|
str += (out.healing_marker.json_dump_marker = "\\" + magic_seed) + "\"";
|
|
} else {
|
|
// TODO: handle more unclosed top-level primitive if the stack was empty but we got an error (e.g. "tru", "\"", etc...)
|
|
// fprintf(stderr, "Closing: TODO\n");
|
|
return false;
|
|
}
|
|
out.json = json::parse(str);
|
|
it = temptative_end;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
out.json = json::parse(std::string(it, end));
|
|
it = end;
|
|
return true;
|
|
}
|