radiance 1.0.5

- Answers no longer stop partway through. A request without max_tokens got 512 tokens, and a
  reasoning model can think for thousands before it answers, so the reply ended inside the
  reasoning with finish_reason "length", which chat clients do not show. It now gets whatever
  the context leaves after the prompt, as in vLLM. --default-max-tokens N sets a number, and
  --max-tokens-cap bounds every request.
- Qwen3.8-Flash-Next in Docker without --security-opt seccomp=unconfined is refused at startup,
  in about a second, with a message naming that option. It used to load the whole model and then
  fail every request as "unsupported", and a restart policy reloaded it over and over. The
  n-gram table is read through io_uring, which Docker's default seccomp profile refuses.
- A request the engine fails says what failed, in the 500 or the stream's error event. It read
  "the engine failed this request: ok".
- Flash-Next no longer crashes at startup about one start in seven (a race between the
  parallel declares that size the activation arena).
- rad-convert no longer needs io_uring, so it converts in a sandbox that forbids it.
This commit is contained in:
Deadcode committed 2026-10-04 18:44:29 +02:00
1 parent 09fb41735b
commit 6c11671bdd
30 files changed
+541 -120

No files matched your search

+15 -6
View File
@@ -1228,7 +1228,7 @@ static double seconds_to(const std::function<void()>& f, int reps = 3) {
}
static double stream_bytes(const ReplyParser& p, const std::string& text, size_t piece,
std::vector<double>* per_call = nullptr) {
std::vector<double>* per_call = nullptr, int reps = 3) {
return seconds_to([&] {
std::unique_ptr<ReplyStream> s = p.open();
std::vector<ReplyEvent> ev;
@@ -1240,7 +1240,7 @@ static double stream_bytes(const ReplyParser& p, const std::string& text, size_t
ev.clear();
}
s->finish(&ev);
}, per_call ? 1 : 3);
}, per_call ? 1 : reps);
}
TEST(a_long_reply_streamed_byte_by_byte_costs_a_constant_factor_of_parsing_it_once) {
@@ -1264,16 +1264,25 @@ TEST(a_long_reply_streamed_byte_by_byte_costs_a_constant_factor_of_parsing_it_on
}
}
TEST(twice_the_reply_costs_twice_the_time) {
/* FOUR TIMES THE REPLY COSTS FOUR TIMES THE TIME, and a quadratic path sixteen: the bound of 8 sits
* a factor of two from each. The two sizes are timed in turns, best of five each, so a burst of load
* from whatever else the machine runs lands on both: timed one size after the other, best of three,
* a 2x step read x3.9 beside three other test binaries and a server, against the 4 a quadratic
* path gives. */
TEST(four_times_the_reply_costs_four_times_the_time) {
for (const Model& m : models()) {
Bench* b = bench(m, ChatRequest{});
if (!b) continue;
const std::string a = long_reply(m, 200 * 1024), c = long_reply(m, 400 * 1024);
const double ta = stream_bytes(b->parser(), a, 1), tc = stream_bytes(b->parser(), c, 1);
const std::string a = long_reply(m, 100 * 1024), c = long_reply(m, 400 * 1024);
double ta = 1e30, tc = 1e30;
for (int rep = 0; rep < 5; ++rep) {
ta = std::min(ta, stream_bytes(b->parser(), a, 1, nullptr, 1));
tc = std::min(tc, stream_bytes(b->parser(), c, 1, nullptr, 1));
}
const double ratio = tc / ta;
fprintf(stderr, " %s: %zu -> %zu bytes byte by byte: %.3f -> %.3f ms (x%.2f)\n",
m.name.c_str(), a.size(), c.size(), ta * 1e3, tc * 1e3, ratio);
CHECK(ratio < 3.0);
CHECK(ratio < 8.0);
}
}
+32 -6
View File
@@ -119,10 +119,17 @@ say "4. the installed rad-kbench replays the fixture from the out-of-tree home:
# 5. The device library's machine code, against the tree's, object by object. A code object also
# carries its compilation unit's ID, which clang hashes from the command line and the output path,
# so two builds of one source never agree byte for byte; their instructions (.text) and kernel
# descriptors (.rodata) do, unless the flags that reach the device compiler differ.
# descriptors do, unless the flags that reach the device compiler differ.
#
# THE DESCRIPTORS ARE COMPARED BY NAME, NOT AS .rodata's BYTES. The compiler lays them out in an
# order that changes from one run of the same command to the next -- ROCm 7's clang does it to the
# eight instances of the template in r4d_gdn_chunk_scan_k128_v128_c64_bf16.hip -- and each holds
# its kernel's entry as an offset from itself, so the section's bytes differ while every kernel and
# every descriptor is the same. Each descriptor is read by its symbol, its entry resolved to the
# address in .text it names, and the rest of .rodata is compared with the descriptors blanked.
if [ -n "$HIPC" ]; then
L=$(dirname "$HIPC")
for t in llvm-objcopy clang-offload-bundler; do
for t in llvm-objcopy llvm-readelf clang-offload-bundler; do
[ -x "$L/$t" ] || fail "no $t beside $HIPC"
done
td="$B/libr4d/CMakeFiles/libr4d.dir"
@@ -131,15 +138,34 @@ if [ -n "$HIPC" ]; then
fatbin() { # fatbin OBJ -- its fat binary into $W/co/fb; false for a source with no kernels
"$L/llvm-objcopy" --dump-section .hip_fatbin="$W/co/fb" "$1" /dev/null 2>/dev/null
}
code() { # code OBJ TARGET -- checksums of that target's instructions and kernel descriptors
descriptors() { # descriptors CO RODATA -- each descriptor by name; blanks it in RODATA
set -- "$1" "$2" $("$L/llvm-readelf" -SW "$1" |
awk '{ for (i = 1; i < NF; ++i) if ($i == ".rodata") print $(i + 2), $(i + 3) }')
[ $# -eq 4 ] || return 0
base=$((0x$3)); foff=$((0x$4))
"$L/llvm-readelf" -sW "$1" | awk '$4 == "OBJECT" && $8 ~ /\.kd$/ { print $2, $3, $8 }' |
sort -u -k3 |
while read -r val size name; do
a=$((0x$val)); o=$((foff + a - base))
e=$(od -An -t d8 -j $((o + 16)) -N 8 "$1" | tr -d ' ')
printf '%s %s entry=%x %s %s\n' "$name" "$size" $((a + e)) \
"$(od -An -v -t x1 -j "$o" -N 16 "$1" | tr -d ' \n')" \
"$(od -An -v -t x1 -j $((o + 24)) -N $((size - 24)) "$1" | tr -d ' \n')"
dd if=/dev/zero of="$2" bs=1 seek=$((a - base)) count="$size" conv=notrunc 2>/dev/null
done
}
code() { # code OBJ TARGET -- checksums of that target's instructions, its descriptors by
# name, and the rest of its read-only data
fatbin "$1"
"$L/clang-offload-bundler" --type=o --input="$W/co/fb" --unbundle --targets="$2" \
--output="$W/co/co"
for sec in .text .rodata; do
"$L/llvm-objcopy" --dump-section "$sec=$W/co/sec" "$W/co/co" /dev/null 2>/dev/null ||
: > "$W/co/sec"
cksum < "$W/co/sec"
"$L/llvm-objcopy" --dump-section "$sec=$W/co/$sec" "$W/co/co" /dev/null 2>/dev/null ||
: > "$W/co/$sec"
done
cksum < "$W/co/.text"
descriptors "$W/co/co" "$W/co/.rodata" | cksum
cksum < "$W/co/.rodata"
}
[ "$(ls "$td"/*.hip.o | wc -l)" = "$(ls "$od"/*.hip.o | wc -l)" ] ||
fail "libr4d compiles a different set of HIP sources out of tree"
+37
View File
@@ -10,6 +10,7 @@
#include "sched/scheduler.h"
#include "sched/derive.h"
#include "server/sink.h"
#include <algorithm>
#include <cstdlib>
@@ -546,6 +547,19 @@ struct Fixture {
return id;
}
/* The same, with the server's sink attached, for what a request tells its client. */
uint64_t submit_to(server::Sink* sink, int n_prompt, int max_tokens) {
auto r = std::make_unique<Request>();
r->id = next_id++;
r->max_tokens = max_tokens;
r->sink = sink;
r->prompt.resize((size_t)n_prompt);
for (int i = 0; i < n_prompt; ++i) r->prompt[(size_t)i] = 1000 + i;
uint64_t id = r->id;
if (sch.add(std::move(r)) < 0) return 0;
return id;
}
/* Commit a step in which every sequence produced one token and none ended. */
void commit_plain(int32_t token = 7) {
const StepPlan& p = sch.plan();
@@ -2039,4 +2053,27 @@ TEST(shutdown_fails_what_is_queued_and_refuses_what_follows) {
CHECK(f.sch.step() == nullptr);
}
/* AND EACH OF THOSE REQUESTS SAYS WHAT STOPPED IT. The sink carries the status and the sentence
* the server puts in the 500: without them a client read "the engine failed this request: ok"
* after a step failed as unsupported. */
TEST(shutdown_fails_each_sink_with_the_status_that_stopped_the_engine) {
server::Sink running, waiting, late;
Fixture f;
f.build(/*blocks=*/20, /*states=*/64);
CHECK_OK(f.start());
CHECK(f.submit_to(&running, 160, 64) != 0);
CHECK(f.sch.step() != nullptr);
CHECK(f.submit_to(&waiting, 160, 64) != 0);
f.sch.shutdown("the engine stopped", RAD_E_UNSUPPORTED);
CHECK(f.submit_to(&late, 8, 8) == 0);
for (server::Sink* s : { &running, &waiting, &late }) {
CHECK(s->done());
CHECK_EQ((int)s->finish_reason(), (int)server::Finish::Error);
CHECK_EQ(s->status(), (int)RAD_E_UNSUPPORTED);
CHECK_EQ(std::string(s->why() ? s->why() : ""), std::string("the engine stopped"));
}
}
RAD_TEST_MAIN()
+57 -2
View File
@@ -226,6 +226,10 @@ struct FakeMultimodal : IMultimodal {
struct FakeScheduler : IScheduler {
std::string reply = "hello";
bool hang = false;
/* Nonzero: after the reply, the request is failed with this status and reason, as the engine
* fails every request it holds when a step fails. */
int fail_status = 0;
const char* fail_why = nullptr;
SchedMetrics m;
std::mutex mu;
@@ -251,9 +255,11 @@ struct FakeScheduler : IScheduler {
int32_t cap = r.max_tokens;
std::string text = reply;
bool h = hang;
const int fst = fail_status;
const char* fwhy = fail_why;
/* Under the lock: over a real socket, submits arrive from several HTTP workers at once. */
std::lock_guard<std::mutex> lk(mu);
workers.emplace_back([sink, cap, text, h] {
workers.emplace_back([sink, cap, text, h, fst, fwhy] {
sink_prompt_stats(sink, 8, 4);
int32_t n = 0;
for (char c : text) {
@@ -271,6 +277,7 @@ struct FakeScheduler : IScheduler {
sink_finish(sink, Finish::Length);
return;
}
if (fst) { sink_fail(sink, fst, fwhy); return; }
sink_finish(sink, n >= cap ? Finish::Length : Finish::Stop);
});
return id;
@@ -1055,6 +1062,17 @@ TEST(the_max_tokens_default_and_cap_bound_a_request_like_the_context) {
CHECK_EQ(chat_with(f, lim, R"("max_tokens":9999)").max_tokens, 10);
CHECK_EQ(chat_with(f, lim, R"("max_tokens":5)").max_tokens, 5);
/* AND `auto` IS THE DEFAULT: limits built with nothing said about max_tokens give a request
* that names none what the context leaves. 512 ended a reasoning model's answers inside its
* reasoning, with a finish_reason "length" a chat client does not show. */
{
OaiLimits d;
d.model_id = lim.model_id;
d.max_ctx = 64;
OaiRequest a = chat_with(f, d, "");
CHECK_EQ((int64_t)a.prompts[0].tokens.size() + a.max_tokens, d.max_ctx);
}
/* The cap holds where no context is declared, and on /v1/completions. */
lim.max_ctx = 0;
lim.default_max_tokens = 512;
@@ -1239,7 +1257,7 @@ TEST(server_info_reports_what_a_request_that_leaves_a_field_out_gets) {
ServerOptions o;
Server srv(d, o);
const json j = json::parse(srv.router().call("GET", "/server_info").body);
CHECK_EQ(j["server"]["default_max_tokens"].get<int64_t>(), 512);
CHECK(j["server"]["default_max_tokens"].is_null()); /* auto */
CHECK(j["server"]["max_tokens_cap"].is_null());
CHECK_EQ(j["server"]["max_stop_strings"].get<int64_t>(), 64);
const json& r = j["request_defaults"];
@@ -1442,6 +1460,43 @@ TEST(streaming_completions_use_the_text_completion_chunk_shape) {
CHECK_EQ(text, std::string("pq"));
}
/* A REQUEST THE ENGINE FAILED SAYS WHAT FAILED, in the 500 and in a stream's error event: the
* reason and the status the engine stopped on. It read "the engine failed this request: ok" when
* a step failed, because nothing carried the status to the request. */
TEST(a_request_the_engine_failed_says_what_failed) {
Fixture f;
f.sched.reply = "he";
f.sched.fail_status = RAD_E_UNSUPPORTED;
f.sched.fail_why = "the engine stopped on an error and is not serving";
const std::string want = "the engine failed this request: the engine stopped on an error and "
"is not serving (unsupported)";
HttpResponse r = f.post("/v1/chat/completions",
R"({"messages":[{"role":"user","content":"hi"}]})");
CHECK_EQ(r.status, 500);
CHECK_EQ(json::parse(r.body)["error"]["message"].get<std::string>(), want);
HttpResponse s = f.post("/v1/completions", R"({"prompt":"hi","stream":true})");
CHECK_EQ(s.status, 200);
int bad = 0, errors = 0;
for (const std::string& p : sse_payloads(drain(s), &bad)) {
if (p == "[DONE]") continue;
json j = json::parse(p);
if (!j.contains("error")) continue;
++errors;
CHECK_EQ(j["error"]["message"].get<std::string>(), want);
}
CHECK_EQ(bad, 0);
CHECK_EQ(errors, 1);
/* With no reason given, the status alone -- and never "ok". */
f.sched.fail_why = nullptr;
HttpResponse t = f.post("/v1/chat/completions",
R"({"messages":[{"role":"user","content":"hi"}]})");
CHECK_EQ(json::parse(t.body)["error"]["message"].get<std::string>(),
std::string("the engine failed this request: unsupported"));
}
TEST(a_streamed_tool_call_is_named_before_its_arguments_flow) {
Fixture f;
f.sched.reply = "TOOL:lookup:{\"q\":\"x\"}";
+33 -16
View File
@@ -7,8 +7,8 @@
* - throughput on prose-and-code text, against an absolute floor and against the reference
* encoder (tools/tokref) on the same text -- the ratio holds on a loaded machine where an
* absolute number would not;
* - LINEAR SCALING: twice the text takes about twice the time, on ordinary text and on the
* inputs a pre-tokeniser is quadratic on when it goes wrong (one enormous word, one
* - LINEAR SCALING: four times the text takes about four times the time, on ordinary text and
* on the inputs a pre-tokeniser is quadratic on when it goes wrong (one enormous word, one
* enormous run of whitespace, digits, alternating classes);
* - the segment cache: the last turn of a long conversation costs a fraction of encoding it.
*
@@ -82,6 +82,18 @@ double encode_seconds(const Tokenizer& t, const std::string& text, int runs = 5)
return best_of(runs, [&] { ids.clear(); t.encode(text, ids, false, true); });
}
/* TWO TEXTS TIMED IN TURNS, best of five each, so a burst of load from whatever else the machine
* runs lands on both: timed one after the other, a parallel ctest beside a server read a linear
* input as x3.6 for twice the text, past the bound a quadratic one is held to. */
void encode_both(const Tokenizer& t, const std::string& small, const std::string& big,
double* ts, double* tb) {
*ts = *tb = 1e30;
for (int i = 0; i < 5; ++i) {
*ts = std::min(*ts, encode_seconds(t, small, 1));
*tb = std::min(*tb, encode_seconds(t, big, 1));
}
}
} /* namespace */
TEST(throughput_has_a_floor_and_beats_the_reference) {
@@ -100,20 +112,23 @@ TEST(throughput_has_a_floor_and_beats_the_reference) {
CHECK(r / s > 4.0);
}
/* TWICE THE TEXT, ABOUT TWICE THE TIME. The texts differ, so a cache that remembered one would
* not help the other; the ratio is allowed well past 2 because the larger one also walks more of
* memory. */
/* FOUR TIMES THE TEXT, ABOUT FOUR TIMES THE TIME. The texts differ, so a cache that remembered one
* would not help the other; the ratio is allowed well past 4 because the larger one also walks
* more of memory. A quarter and a whole megabyte: the cost of a byte steps up by about half
* between one and two megabytes on the machine these were measured on, where the working set
* leaves the core's cache, and a step across that reads as growth the encoder does not have. */
TEST(encoding_time_is_linear_in_the_text) {
auto v = vocab();
REQUIRE(v != nullptr);
Tokenizer t;
t.init(v);
t.set_segment_cache(false);
const std::string one = document(3, 1 << 20), two = document(4, 2 << 20);
const double a = encode_seconds(t, one), b = encode_seconds(t, two);
std::fprintf(stderr, " 1 MB %.2f ms, 2 MB %.2f ms, ratio %.2f\n", a * 1e3, b * 1e3, b / a);
CHECK(b / a > 1.3);
CHECK(b / a < 2.8);
const std::string one = document(3, 256 << 10), four = document(4, 1 << 20);
double a, b;
encode_both(t, one, four, &a, &b);
std::fprintf(stderr, " 256 KB %.2f ms, 1 MB %.2f ms, ratio %.2f\n", a * 1e3, b * 1e3, b / a);
CHECK(b / a > 2.6);
CHECK(b / a < 6.0);
}
/* THE INPUTS A SPLITTER GOES QUADRATIC ON. A backtracking `\s+(?!\S)` rescans a whitespace run
@@ -133,14 +148,16 @@ TEST(pathological_inputs_stay_linear) {
{ "alternating", [](size_t n) { std::string s; while (s.size() < n) s += "a1!"; return s; } },
{ "apostrophes", [](size_t n) { std::string s; while (s.size() < n) s += "''s"; return s; } },
};
/* A megabyte and two, best of five: at a few hundred kilobytes one of these runs in a couple of
* milliseconds, which one preemption under a parallel ctest turns into a false quadratic. */
/* 125 and 500 KB: linear is 4, quadratic 16, and the bound of 8 sits a factor of two from
* each. Both stay below the size where a byte's cost steps up (see above); a run of whitespace
* costs a little more a byte as it grows, about x4.8 here, and that is not what this holds. */
for (const auto& in : inputs) {
const std::string one = in.make(1000000), two = in.make(2000000);
const double a = encode_seconds(t, one, 5), b = encode_seconds(t, two, 5);
std::fprintf(stderr, " %-17s 1 MB %7.2f ms, 2 MB %7.2f ms, ratio %.2f\n", in.name,
const std::string one = in.make(125000), four = in.make(500000);
double a, b;
encode_both(t, one, four, &a, &b);
std::fprintf(stderr, " %-17s 125 KB %7.2f ms, 500 KB %7.2f ms, ratio %.2f\n", in.name,
a * 1e3, b * 1e3, b / a);
CHECK(b / a < 3.0);
CHECK(b / a < 8.0);
}
}
+54 -6
View File
@@ -14,7 +14,10 @@
#include "rad_internal.h"
#include <atomic>
#include <string>
#include <thread>
#include <vector>
using namespace rad;
@@ -162,8 +165,8 @@ TEST(the_param_view_is_rebuilt_after_a_mutation) {
CHECK_EQ(g.n_params(), 1);
CHECK_EQ(g.params()[0].ival, 8ll);
/* The view is cached, so the question is whether the cache is invalidated. A stale view hands
* a kernel the geometry it was resolved against rather than the one it is being run on. */
/* The view is built by the writes, so the question is whether every write rebuilds it. A stale
* view hands a kernel the geometry it was resolved against rather than the one it is run on. */
g.set_i("M", 9);
CHECK_EQ(g.params()[0].ival, 9ll);
g.set_i("N", 4096);
@@ -186,15 +189,15 @@ TEST(the_param_view_is_rebuilt_after_a_mutation) {
CHECK_EQ(seen, 1);
}
/* THE COPY MUST OWN ITS OWN STRINGS, and the header says why at length: the cached view holds
* char* into the object's own storage, so a copy that took the view along with the storage would
/* THE COPY MUST OWN ITS OWN STRINGS, and the header says why at length: the view holds char*
* into the object's own storage, so a copy that took the view along with the storage would
* hand a kernel pointers into the source -- which dies first about as often as not, since every
* Resolved holds a Geometry and every Band holds two. */
TEST(a_copied_geometry_points_into_itself_and_not_at_the_source) {
Geometry a;
a.set_s("dtype", "w4a8");
a.set_i("M", 64);
const char* a_key = a.params()[0].key; /* force the source to build its view */
const char* a_key = a.params()[0].key;
Geometry b = a;
CHECK_EQ(b.n_params(), 2);
@@ -202,6 +205,7 @@ TEST(a_copied_geometry_points_into_itself_and_not_at_the_source) {
CHECK(b.params()[0].key != a_key); /* a different object's storage */
CHECK(b.params()[0].sval != a.params()[0].sval);
CHECK_EQ(std::string(b.params()[0].sval), std::string("w4a8"));
CHECK(b.params()[0].sval == b.get_s("dtype")); /* and into the copy's own string */
/* And the two are independent afterwards, in both directions. */
a.set_i("M", 1);
@@ -225,11 +229,12 @@ TEST(a_moved_geometry_does_not_leave_the_view_pointing_at_a_stolen_buffer) {
Geometry a;
a.set_s("dtype", "fp8");
a.set_i("K", 5120);
(void)a.params(); /* build the view, then move out from under it */
Geometry b = std::move(a);
CHECK_EQ(b.n_params(), 2);
CHECK_EQ(std::string(b.params()[0].sval), std::string("fp8"));
CHECK(b.params()[0].sval == b.get_s("dtype")); /* the view went with the strings it names */
CHECK_EQ(std::string(b.params()[0].key), std::string("dtype"));
/* The moved-from object is empty rather than holding a view of a buffer it no longer owns. */
CHECK_EQ(a.n_params(), 0);
@@ -241,6 +246,49 @@ TEST(a_moved_geometry_does_not_leave_the_view_pointing_at_a_stolen_buffer) {
CHECK_EQ(b.n_params(), 0);
}
/* READING A GEOMETRY WRITES NOTHING, so one that nobody is writing can be read from any number of
* threads. The sizing declares of one rank run side by side and every one of them reads the real
* program's op geometries (Engine::probe_arena_levels); a view built lazily on first read was a
* write each of them made, and two threads clearing and filling one vector corrupted the heap at
* startup. Each round reads a fresh geometry -- a moved one, as every OpInfo's is -- from eight
* threads released together, and every reader must see the whole view and the same one. */
TEST(a_geometry_read_from_many_threads_at_once_is_not_written) {
constexpr int kThreads = 8, kRounds = 400;
int bad = 0;
for (int round = 0; round < kRounds && bad == 0; ++round) {
Geometry src;
src.set_s("dtype", "fp8_e4m3_with_a_name_past_the_short_string_buffer");
for (int k = 0; k < 12; ++k) src.set_i(fmt("key_%d", k), k * 1000 + round);
const Geometry g = std::move(src);
std::atomic<int> ready{0};
std::atomic<bool> go{false};
std::vector<const RadParam*> seen(kThreads, nullptr);
std::vector<int> ok(kThreads, 0);
std::vector<std::thread> th;
for (int t = 0; t < kThreads; ++t)
th.emplace_back([&, t] {
ready.fetch_add(1);
while (!go.load()) {}
const RadParam* p = g.params();
const int n = g.n_params();
bool good = n == 13 && std::string(p[0].key) == "dtype" && p[0].sval &&
std::string(p[0].sval).rfind("fp8_e4m3", 0) == 0;
for (int k = 0; good && k < 12; ++k)
good = p[k + 1].kind == RAD_P_INT && p[k + 1].ival == k * 1000 + round &&
std::string(p[k + 1].key) == fmt("key_%d", k);
seen[t] = p;
ok[t] = good;
});
while (ready.load() < kThreads) {}
go.store(true);
for (auto& x : th) x.join();
for (int t = 0; t < kThreads; ++t)
if (!ok[t] || seen[t] != seen[0]) ++bad;
}
CHECK_EQ(bad, 0);
}
TEST(geometry_str_prints_every_kind_in_declaration_order) {
Geometry g;
g.set_i("M", 64);