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

+8
View File
@@ -802,6 +802,8 @@ static bool schema_same(const RadOpSchema& a, const RadOpSchema& b, std::string*
/* embed_lookup_q over a file-backed table, two ranks and the reader racing (avx_check_rows.cpp). */
bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool verbose,
bool bf16);
/* embed_lookup_q's init, where io_uring is refused and here (avx_check_rows.cpp). */
bool ngram_init_case(const Lib& avx);
int main(int argc, char** argv) {
std::string ref_path = "libref.so", avx_path = "libavx.so", only_op;
@@ -885,6 +887,12 @@ int main(int argc, char** argv) {
}
int rc = missing ? 1 : 0;
/* Before anything runs a gather: the case forks, and a child is cleanest taken from a process
* that has started none of the gather's threads. */
if (only_op.empty() || only_op == "embed_lookup_q") {
std::printf("\n");
if (!ngram_init_case(avx)) rc = 1;
}
const int lo = only_level >= 0 ? only_level : 0;
const int hi = only_level >= 0 ? only_level : AVX_N_LEVELS - 1;
std::vector<std::string> declined_ops;
+91
View File
@@ -28,13 +28,104 @@
#include <cstring>
#include <thread>
#include <cstddef>
#include <fcntl.h>
#include <linux/filter.h>
#include <linux/io_uring.h>
#include <linux/seccomp.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>
/* EMBED_LOOKUP_Q'S INIT REFUSES A PROCESS THAT MAY NOT MAKE AN io_uring RING, and says why: Docker's
* default seccomp profile answers EPERM to io_uring_setup, and a server started that way used to
* load the whole model and then fail every request as "unsupported". A child process installs the
* same refusal as a seccomp filter and asks the kernel's init, which must refuse a gather into
* bf16 -- the one that reads through the ring -- name the Docker option on stderr, and accept a
* gather into f32, which reads the table as memory. Then this process asks: it must accept where a
* ring can be made and refuse where it cannot, as in a build sandbox. */
bool ngram_init_case(const Lib& avx) {
const char* what = "embed_lookup_q's init";
const RadKernelInfo* r = avx.row("embed_lookup_q");
if (!r || !r->init) {
std::printf(" %s: the row has no init\n", what);
return false;
}
RadParam bf16[2]{}, f32[2]{};
bf16[0].key = f32[0].key = "n_embd";
bf16[0].kind = f32[0].kind = RAD_P_INT;
bf16[0].ival = f32[0].ival = 160;
bf16[1].key = f32[1].key = "dtype";
bf16[1].kind = f32[1].kind = RAD_P_STR;
bf16[1].sval = "bf16";
f32[1].sval = "f32";
int out[2];
if (::pipe(out) != 0) {
std::printf(" %s: pipe: %s\n", what, std::strerror(errno));
return false;
}
std::fflush(nullptr);
const pid_t pid = ::fork();
if (pid == 0) {
::dup2(out[1], 2);
::close(out[0]);
sock_filter f[] = {
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(seccomp_data, nr)),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_io_uring_setup, 0, 1),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | (EPERM & SECCOMP_RET_DATA)),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
};
sock_fprog prog{ (unsigned short)(sizeof f / sizeof f[0]), f };
if (::prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0 ||
::prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) != 0)
::_exit(3);
void* inst = nullptr;
const int a = r->init(bf16, 2, 0, 1, &inst);
const int b = r->init(f32, 2, 0, 1, &inst);
std::fflush(nullptr);
::_exit(a == RAD_E_UNSUPPORTED && b == RAD_OK ? 0 : 1);
}
::close(out[1]);
std::string said;
char buf[512];
for (ssize_t k; (k = ::read(out[0], buf, sizeof buf)) > 0;) said.append(buf, (size_t)k);
::close(out[0]);
int ws = 0;
if (pid < 0 || ::waitpid(pid, &ws, 0) != pid || !WIFEXITED(ws)) {
std::printf(" %s: the child did not run to the end\n", what);
return false;
}
bool ok = true;
if (WEXITSTATUS(ws) == 3) {
std::printf(" %s, where io_uring is refused: SKIPPED -- no seccomp filter can be installed "
"here\n", what);
} else if (WEXITSTATUS(ws) != 0 ||
said.find("--security-opt seccomp=unconfined") == std::string::npos) {
std::printf(" %s, where io_uring is refused: FAIL -- %s; it said: %s\n", what,
WEXITSTATUS(ws) ? "the statuses were wrong" : "the reason was not named",
said.c_str());
ok = false;
} else {
std::printf(" %s, where io_uring is refused: refused, naming the Docker option\n", what);
}
io_uring_params p{};
const long ring = syscall(__NR_io_uring_setup, 1, &p);
const bool here = ring >= 0;
if (here) ::close((int)ring);
void* inst = nullptr;
const int st = r->init(bf16, 2, 0, 1, &inst);
const bool right = here ? st == RAD_OK : st == RAD_E_UNSUPPORTED;
std::printf(" %s, in this process (io_uring %s): %s\n", what, here ? "available" : "refused",
right ? (here ? "accepted" : "refused") : "FAIL");
return ok && right;
}
bool row_gather_case(const Lib& avx, const std::string& dir, int64_t calls, bool verbose,
bool bf16) {
const RadKernelInfo* r = avx.row("embed_lookup_q");
+76 -8
View File
@@ -439,6 +439,7 @@ struct ReadRing {
io_uring_cqe *cqes = nullptr;
void *sq_ring = nullptr, *cq_ring = nullptr;
size_t sq_sz = 0, cq_sz = 0, sqe_sz = 0;
int err = 0; /* the errno that stopped init(), for ring_refusal() */
/* One thread submits and reaps (SINGLE_ISSUER), and completions are run when it asks for them
* rather than by interrupting it (DEFER_TASKRUN): the ring is thread-local and the thread only
@@ -448,7 +449,7 @@ struct ReadRing {
std::memset(&p, 0, sizeof p);
p.flags = IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN;
const long r = syscall(__NR_io_uring_setup, kEntries, &p);
if (r < 0) return false;
if (r < 0) { err = errno; return false; }
fd = (int)r;
entries = p.sq_entries;
sq_sz = p.sq_off.array + (size_t)p.sq_entries * sizeof(unsigned);
@@ -459,16 +460,16 @@ struct ReadRing {
}
sq_ring = mmap(nullptr, sq_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
fd, IORING_OFF_SQ_RING);
if (sq_ring == MAP_FAILED) { sq_ring = nullptr; shut(); return false; }
if (sq_ring == MAP_FAILED) { err = errno; sq_ring = nullptr; shut(); return false; }
cq_ring = (p.features & IORING_FEAT_SINGLE_MMAP)
? sq_ring
: mmap(nullptr, cq_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
fd, IORING_OFF_CQ_RING);
if (cq_ring == MAP_FAILED) { cq_ring = nullptr; shut(); return false; }
if (cq_ring == MAP_FAILED) { err = errno; cq_ring = nullptr; shut(); return false; }
sqe_sz = (size_t)p.sq_entries * sizeof(io_uring_sqe);
sqes = (io_uring_sqe*)mmap(nullptr, sqe_sz, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES);
if (sqes == MAP_FAILED) { sqes = nullptr; shut(); return false; }
if (sqes == MAP_FAILED) { err = errno; sqes = nullptr; shut(); return false; }
sq_tail = (unsigned*)((char*)sq_ring + p.sq_off.tail);
sq_mask = (unsigned*)((char*)sq_ring + p.sq_off.ring_mask);
sq_array = (unsigned*)((char*)sq_ring + p.sq_off.array);
@@ -532,17 +533,51 @@ struct ReadRing {
/* Thread-local: each rank thread issues its own step, so each gets its own ring and the two never
* contend. Built on that thread's first gather over a file and torn down when the thread exits. */
inline ReadRing* read_ring() {
/* This thread's ring, made on its first gather. Null if it could not be made, with the errno in
* `*why`. */
inline ReadRing* read_ring(int* why) {
static thread_local bool tried = false;
static thread_local int err = 0;
static thread_local std::unique_ptr<ReadRing> r;
if (!tried) {
tried = true;
std::unique_ptr<ReadRing> p(new ReadRing());
if (p->init()) r = std::move(p);
else err = p->err ? p->err : EIO;
}
if (!r && why) *why = err;
return r.get();
}
/* WHY THIS PROCESS CANNOT MAKE A RING, said in terms of what to change. Docker's default seccomp
* profile is by far the commonest: it answers EPERM to io_uring_setup, and so does the
* kernel.io_uring_disabled sysctl. A kernel too old for SINGLE_ISSUER and DEFER_TASKRUN answers
* EINVAL. */
std::string ring_refusal(int e) {
std::string s = "libavx: the n-gram table is read through io_uring, and this process cannot "
"make a ring (" + std::string(std::strerror(e)) + "). ";
if (e == EPERM || e == EACCES)
s += "Docker's default seccomp profile refuses io_uring: run the container with "
"--security-opt seccomp=unconfined (docs/DOCKER.md). Outside a container, the "
"kernel.io_uring_disabled sysctl refuses it the same way.";
else if (e == ENOSYS)
s += "This kernel is built without io_uring.";
else if (e == EINVAL)
s += "The ring needs IORING_SETUP_SINGLE_ISSUER and IORING_SETUP_DEFER_TASKRUN, which "
"Linux 6.1 added.";
return s;
}
/* Whether this process can make the ring every gathering thread makes: 0, or the errno that
* stopped it. Asked once, by making one and closing it. */
int ring_errno() {
static const int e = [] {
ReadRing r;
return r.init() ? 0 : (r.err ? r.err : EIO);
}();
return e;
}
/* Read `reads` through the ring, a batch at a time: a row with a slot is copied into the cache and
* published READY; a row without one is decoded straight into its output row. On a failed read,
* every claim not yet published is given up (its word back to EMPTY) so no other thread waits on
@@ -711,7 +746,7 @@ struct AheadReader {
void run() {
Gather& g = gather_state();
ReadRing* rr = read_ring();
ReadRing* rr = read_ring(nullptr);
const uint64_t me = owner_tag();
for (;;) {
AheadJob j;
@@ -861,9 +896,15 @@ int avx::row_gather(const RowGather& q, RowDecode decode) {
if (!tfile) return ferr;
#if defined(__linux__)
if (tfile->fd >= 0) {
ReadRing* rr = read_ring();
int why = 0;
ReadRing* rr = read_ring(&why);
if (!rr) {
static std::once_flag said;
std::call_once(said, [why] { std::fprintf(stderr, "%s\n", ring_refusal(why).c_str()); });
return RAD_E_UNSUPPORTED;
}
RowCache* rc = row_cache(tfile, q.row_bytes);
if (!rr || !rc) return RAD_E_UNSUPPORTED;
if (!rc) return RAD_E_NOMEM;
const long long ws = q.world_size > 1 ? q.world_size : 1;
const long long first = q.rank > 0 && q.rank < ws ? q.M * q.rank / ws : 0;
const int rs = gather_rows(*tfile, *rc, *rr, q, first, decode, lut);
@@ -943,3 +984,30 @@ extern "C" int avx_layout_ngram(const RadParam* p, int n_p, int operand, const R
if (pl[0].rank != 2 || pl[0].shape[0] != r || pl[0].shape[1] != c) return RAD_E_SHAPE;
return RAD_E_UNSUPPORTED;
}
/* ============================== THE INSTANCE: CAN THIS PROCESS READ THE TABLE ==============================
*
* Every miss of the gather into bf16 is an io_uring read, and a process that may not make a ring
* fails every step that gathers: the first request fails, after the whole model has loaded, and
* the engine stops. So the instance asks at declare, before a byte is loaded, and refuses with
* the reason. A gather into any other dtype takes the general path, which reads the table as
* memory and makes no ring. rad-convert declares without instances -- it launches nothing -- so a
* sandbox that forbids io_uring still converts the model. */
extern "C" int avx_init_ngram(const RadParam* p, int n_p, int rank, int world_size, void** out) {
(void)rank;
(void)world_size;
if (out) *out = nullptr;
#if defined(__linux__)
const char* dt = rad_param_gets(p, n_p, "dtype", nullptr);
if (!dt || std::strcmp(dt, "bf16") != 0) return RAD_OK;
if (const int e = ring_errno()) {
static std::once_flag said;
std::call_once(said, [e] { std::fprintf(stderr, "%s\n", ring_refusal(e).c_str()); });
return RAD_E_UNSUPPORTED;
}
#else
(void)p;
(void)n_p;
#endif
return RAD_OK;
}
+5 -3
View File
@@ -725,14 +725,16 @@ SIMPLE("avx_all_gather", "all_gather", "ar", "gather every rank's shard; world_s
SIMPLE("avx_embed_lookup", "embed_lookup", "vocab", "embedding gather", DT_FLOAT, L(embed_lookup)),
/* THE ONE ROW WITH A LAYOUT HOOK, and it only checks: the table is read as it is stored, and the
* hook refuses a table encoded any other way (avx_ngram.cpp). An E4M3 table into bf16 goes through
* the row cache; every other dtype pair takes the general gather. */
* the row cache; every other dtype pair takes the general gather. Its init refuses a process that
* may not make the io_uring ring the cache's misses are read through, at declare and not at the
* first request. */
{ "avx_embed_lookup_q", "embed_lookup_q", "vocab",
"embedding gather, one scale for the table: an E4M3 table into bf16 through a shared row cache "
"and direct reads of the container, a later call's rows read ahead",
SHAPE_ANY, DT_ANY, RAD_DOMAIN_HOST, 0,
nullptr, 0, nullptr, 0, nullptr,
/* init, fini, LAUNCH, scratch, layout, relayout, opd_shape, unrelayout */
nullptr, nullptr, L(embed_lookup_q), nullptr, avx_layout_ngram, nullptr, nullptr, nullptr,
/* INIT, fini, LAUNCH, scratch, layout, relayout, opd_shape, unrelayout */
avx_init_ngram, nullptr, L(embed_lookup_q), nullptr, avx_layout_ngram, nullptr, nullptr, nullptr,
nullptr, nullptr },
SIMPLE("avx_logits_gemm", "logits_gemm", "vocab", "the lm_head projection", DT_FLOAT,
L(logits_gemm)),
+4
View File
@@ -52,4 +52,8 @@ int row_gather(const RowGather& g, RowDecode decode);
extern "C" int avx_layout_ngram(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, RadLayout*);
/* Whether this process can read the table's rows: refused, with the reason on stderr, where it may
* not make an io_uring ring (avx_ngram.cpp). */
extern "C" int avx_init_ngram(const RadParam*, int, int, int, void**);
#endif /* RAD_AVX_ROWS_H */