Archived
radiance 1.7.0
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
b068190326
commit
1e968f6e1d
739 files changed
+128447
-84632
No files matched your search
+94
-97
@@ -1,33 +1,26 @@
|
||||
/* ref_collective.cpp -- all_reduce and all_gather on the host backend.
|
||||
*
|
||||
* At world_size 1 both are the degenerate case: all_reduce is inout and already holds the answer,
|
||||
* all_gather is a copy. Above 1 they are real, and they work because of how this engine is shaped:
|
||||
* ONE PROCESS, ONE THREAD PER RANK, ONE ADDRESS SPACE (spec §1). There is no IPC to set up and no
|
||||
* serialisation to do -- a peer's buffer is a pointer -- so the whole collective is a rendezvous, a
|
||||
* read of every rank's pointer, and a second rendezvous before anyone overwrites theirs.
|
||||
* At world_size 1 all_reduce is a no-op (inout) and all_gather a copy. Above 1 they rely on the
|
||||
* engine's shape: one process, one thread per rank, one address space (spec §1). A peer's buffer is
|
||||
* a pointer, so a collective is a rendezvous, a read of every rank's pointer, and a second
|
||||
* rendezvous before anyone overwrites theirs.
|
||||
*
|
||||
* That makes the host backend a FAKE MULTI-DEVICE BACKEND, which is the point: tensor-parallel code
|
||||
* paths -- the sharded declare, the collective placement, the distributed top-k -- can be exercised
|
||||
* on a machine with no accelerator at all. What it does not test is a device all-reduce's numerics,
|
||||
* and it does not pretend to.
|
||||
* This makes the host backend a fake multi-device backend, so tensor-parallel paths (sharded
|
||||
* declare, collective placement, distributed top-k) can run without an accelerator. It does not
|
||||
* test a device all-reduce's numerics.
|
||||
*
|
||||
* REDUCTION ORDER IS ASCENDING RANK, in f32, on every rank. That is libr4d's rule ("fp32
|
||||
* accumulate in ascending rank order, bit-identical across ranks") and it is a correctness
|
||||
* requirement rather than a convention: tensor-parallel ranks hold replicated state that must stay
|
||||
* bit-identical, and two ranks that summed in different orders would drift apart over a
|
||||
* generation. The `-ffp-contract=off` this plugin builds with is part of the same rule
|
||||
* (docs/OPS.md).
|
||||
* Reduction order is ascending rank, in f32, on every rank (libr4d's rule). Tensor-parallel ranks
|
||||
* hold replicated state that must stay bit-identical; different summation orders would drift apart
|
||||
* over a generation. -ffp-contract=off is part of the same rule (docs/OPS.md).
|
||||
*
|
||||
* `exact` is honoured in one direction only. exact=1 gets the exact sum. exact=0 -- a caller that
|
||||
* has declared it will accept a lossy quantised wire -- also gets the exact sum, because there is
|
||||
* no wire here to quantise. Serving a lossy request with an exact answer is safe; the constraint
|
||||
* exists so a caller who wanted exactness cannot be handed a lossy result by accident, not the
|
||||
* reverse. `hops` is accepted and ignored: one-shot and two-shot are the same walk over an array
|
||||
* when the peers are pointers.
|
||||
* exact=0 (caller accepts a lossy quantised wire) also gets the exact sum, since there is no wire;
|
||||
* the constraint only forbids handing a lossy result to a caller that asked for exact. `hops` is
|
||||
* ignored: one-shot and two-shot are the same walk when peers are pointers.
|
||||
*/
|
||||
#include "ref_common.h"
|
||||
#include "ref_ops.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
@@ -39,15 +32,15 @@ namespace {
|
||||
|
||||
enum { REF_MAX_RANKS = 8 };
|
||||
|
||||
/* One group per process. There is exactly one world size in a radiance process and the rank
|
||||
* threads run the same declared graph in lockstep against one batch, so the sequence of collective
|
||||
* calls is identical on every rank and a single sense-reversing barrier is enough to pair them up.
|
||||
* A per-op group would need a key the ABI does not carry -- init() is called once per rank per
|
||||
* resolved instance and cannot tell two ops apart from two ranks of one op. */
|
||||
/* One group per process. A process has one world size and the rank threads run the same graph in
|
||||
* lockstep, so every rank issues the same sequence of collectives and one generation-counting
|
||||
* barrier pairs them. A per-op group would need a key the ABI does not carry: init() cannot tell
|
||||
* two ops apart from two ranks of one op. */
|
||||
struct Group {
|
||||
std::mutex m;
|
||||
std::condition_variable cv;
|
||||
int world = 0;
|
||||
int members = 0; /* initialised instances not yet finalised (see join) */
|
||||
int arrived = 0;
|
||||
unsigned gen = 0;
|
||||
const void* slot[REF_MAX_RANKS] = {};
|
||||
@@ -57,11 +50,14 @@ struct Group {
|
||||
|
||||
static Group& group() { static Group g; return g; }
|
||||
|
||||
/* Returns false on timeout. A collective IS a synchronisation, so the no-blocking rule cannot
|
||||
* apply to it -- but an unbounded wait would turn a mismatched call sequence into a wedged test
|
||||
* run with no output, and this engine exits non-zero rather than serving from a wedged queue
|
||||
* (spec §17). Thirty seconds is far longer than any correct step and far shorter than a CI
|
||||
* timeout. */
|
||||
/* How long a rank waits for its peers. A collective must block, but an unbounded wait would turn a
|
||||
* mismatched call sequence into a silent hang; the engine exits non-zero instead (spec §17). 30 s is
|
||||
* far longer than any correct step and shorter than a CI timeout. ref_collective_timeout_ms sets it
|
||||
* for a test. */
|
||||
static std::atomic<int> g_timeout_ms{ 30000 };
|
||||
|
||||
/* Returns false on timeout. A rank that gives up withdraws its arrival, so the group's count is
|
||||
* again that of the ranks still waiting and the next collective pairs from a clean start. */
|
||||
static bool barrier(Group& g) {
|
||||
std::unique_lock<std::mutex> lk(g.m);
|
||||
const unsigned mygen = g.gen;
|
||||
@@ -71,23 +67,48 @@ static bool barrier(Group& g) {
|
||||
g.cv.notify_all();
|
||||
return true;
|
||||
}
|
||||
return g.cv.wait_for(lk, std::chrono::seconds(30), [&] { return g.gen != mygen; });
|
||||
const bool met = g.cv.wait_for(lk, std::chrono::milliseconds(g_timeout_ms.load()),
|
||||
[&] { return g.gen != mygen; });
|
||||
if (!met) --g.arrived;
|
||||
return met;
|
||||
}
|
||||
|
||||
static int join(int rank, int world) {
|
||||
/* The world size is fixed while it is in use: while an initialised instance is alive or a rank is
|
||||
* mid-collective. Otherwise the next join may state another one, so a process can run one group
|
||||
* after another (a re-initialised engine, a test). `member` counts an init, which fini releases. */
|
||||
static int join(int rank, int world, bool member) {
|
||||
if (world <= 0 || world > REF_MAX_RANKS) return RAD_E_UNSUPPORTED;
|
||||
if (rank < 0 || rank >= world) return RAD_E_INVAL;
|
||||
Group& g = group();
|
||||
std::lock_guard<std::mutex> lk(g.m);
|
||||
if (g.world == 0) g.world = world;
|
||||
if (g.world == 0 || (g.members == 0 && g.arrived == 0)) g.world = world;
|
||||
else if (g.world != world) return RAD_E_INVAL;
|
||||
if (member) ++g.members;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Staging, so a rank does not overwrite its own buffer while a peer is still reading it. The
|
||||
* scratch the arena hands us is used when it is big enough -- that is what the scratch hook exists
|
||||
* for -- and the per-thread workspace covers a caller that passed none (a test, or a tool calling
|
||||
* the kernel directly). */
|
||||
/* Publishes this rank's input and waits until every rank has published a matching one. Peers read
|
||||
* each other's input as `nel` contiguous elements, since only the pointer is published. */
|
||||
static int publish(int rank, int world, const RadTensor* x, int64_t nel) {
|
||||
if (!rad_tensor_is_contiguous(x)) return RAD_E_STRIDE;
|
||||
RAD_REF_TRY(join(rank, world, false));
|
||||
Group& g = group();
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g.m);
|
||||
g.slot[rank] = x->data;
|
||||
g.nel [rank] = nel;
|
||||
g.dt [rank] = x->dtype;
|
||||
}
|
||||
if (!barrier(g)) return RAD_E_DEVICE;
|
||||
for (int r = 0; r < world; ++r) {
|
||||
if (!g.slot[r] || g.nel[r] != nel) return RAD_E_SHAPE;
|
||||
if (g.dt[r] != x->dtype) return RAD_E_DTYPE;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Staging, so a rank does not overwrite its buffer while a peer still reads it. Uses the arena's
|
||||
* scratch when large enough, else the per-thread workspace (for direct callers that pass none). */
|
||||
static float* staging(const RadArgs* a, int64_t nel) {
|
||||
if (a->scratch && a->scratch_bytes >= nel * (int64_t)sizeof(float))
|
||||
return (float*)a->scratch;
|
||||
@@ -98,18 +119,30 @@ static float* staging(const RadArgs* a, int64_t nel) {
|
||||
|
||||
int ref_collective_init(const RadParam* p, int n_p, int rank, int world_size,
|
||||
void** out) {
|
||||
/* The world size in the geometry wins: declare states it, and RadBuildCtx already divided the
|
||||
* model by it (spec §9). */
|
||||
/* The geometry's world size wins: declare states it and RadBuildCtx divided the model by it
|
||||
* (spec §9). */
|
||||
int ws = world_size;
|
||||
for (int i = 0; i < n_p; ++i)
|
||||
if (p[i].key && std::strcmp(p[i].key, "world_size") == 0 && p[i].kind == RAD_P_INT)
|
||||
ws = (int)p[i].ival;
|
||||
if (out) *out = nullptr;
|
||||
if (ws <= 1) return RAD_OK;
|
||||
return join(rank, ws);
|
||||
RAD_REF_TRY(join(rank, ws, true));
|
||||
if (out) *out = &group();
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
void ref_collective_fini(void*) {}
|
||||
/* Pairs with an init that joined (its instance is the group); one that did not has none. */
|
||||
void ref_collective_fini(void* inst) {
|
||||
if (!inst) return;
|
||||
Group& g = group();
|
||||
std::lock_guard<std::mutex> lk(g.m);
|
||||
if (g.members > 0) --g.members;
|
||||
}
|
||||
|
||||
/* Test hook, exported beside the ABI entry points: how long a rank waits for its peers before
|
||||
* the collective returns RAD_E_DEVICE. A non-positive value restores the default. */
|
||||
extern "C" void ref_collective_timeout_ms(int ms) { g_timeout_ms.store(ms > 0 ? ms : 30000); }
|
||||
|
||||
int64_t ref_all_reduce_scratch(const RadArgs* a) {
|
||||
const int64_t ws = p_int(a, "world_size", a->world_size > 0 ? a->world_size : 1);
|
||||
@@ -119,10 +152,9 @@ int64_t ref_all_reduce_scratch(const RadArgs* a) {
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ all_reduce */
|
||||
/* `y` is optional and absent means IN PLACE, which is the form docs/OPS.md draws and the only one
|
||||
* the one-shot kernels need. libr4d's two-shot rows are not safe in place -- a rank gathers into
|
||||
* the output while its own input is still being read for the scatter -- so the destination has to
|
||||
* be expressible, and here it costs one branch on the store. */
|
||||
/* `y` is optional; absent means in place (docs/OPS.md's form, all the one-shot kernels need).
|
||||
* libr4d's two-shot rows are not safe in place, since a rank gathers into the output while its
|
||||
* input is still read for the scatter, so a separate destination must be expressible. */
|
||||
int ref_all_reduce(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 1)) return RAD_E_INVAL;
|
||||
const RadTensor* x = &a->t[0];
|
||||
@@ -135,26 +167,12 @@ int ref_all_reduce(const RadArgs* a, RadStream) {
|
||||
|
||||
const int world = (int)p_int(a, "world_size", a->world_size > 0 ? a->world_size : 1);
|
||||
if (world <= 1) {
|
||||
/* inout: the answer is already there. With a separate destination it still has to be
|
||||
* COPIED there, because a caller that passed one reads it and not x. */
|
||||
/* x already holds the answer; copy it to a separate destination if one was given. */
|
||||
if (y) for (int64_t i = 0; i < nel; ++i) stt(y, offlin(y, i), ldt(x, offlin(x, i)));
|
||||
return RAD_OK;
|
||||
}
|
||||
const int rank = a->rank;
|
||||
RAD_REF_TRY(join(rank, world));
|
||||
|
||||
RAD_REF_TRY(publish(a->rank, world, x, nel));
|
||||
Group& g = group();
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g.m);
|
||||
g.slot[rank] = x->data;
|
||||
g.nel [rank] = nel;
|
||||
g.dt [rank] = x->dtype;
|
||||
}
|
||||
if (!barrier(g)) return RAD_E_DEVICE;
|
||||
|
||||
for (int r = 0; r < world; ++r)
|
||||
if (!g.slot[r] || g.nel[r] != nel || g.dt[r] != x->dtype) return RAD_E_SHAPE;
|
||||
|
||||
float* acc = staging(a, nel);
|
||||
for (int64_t i = 0; i < nel; ++i) {
|
||||
float s = 0.0f;
|
||||
@@ -162,9 +180,8 @@ int ref_all_reduce(const RadArgs* a, RadStream) {
|
||||
acc[i] = s;
|
||||
}
|
||||
|
||||
/* Everyone has finished READING before anyone WRITES. Without this the rank that finishes
|
||||
* first would overwrite an input a slower peer has not consumed yet, and the bug would show up
|
||||
* as a rank-dependent answer under load and never in a small test. */
|
||||
/* Every rank finishes reading before any writes; otherwise a fast rank overwrites input a
|
||||
* slower peer has not consumed, giving rank-dependent answers under load. */
|
||||
if (!barrier(g)) return RAD_E_DEVICE;
|
||||
for (int64_t i = 0; i < nel; ++i) stt(dst, offlin(dst, i), acc[i]);
|
||||
return RAD_OK;
|
||||
@@ -173,14 +190,10 @@ int ref_all_reduce(const RadArgs* a, RadStream) {
|
||||
/* ------------------------------------------------------------------ all_gather */
|
||||
/* y is [world_size * numel] with rank r's contribution at offset r * numel.
|
||||
*
|
||||
* A GATHER MOVES BYTES, NOT NUMBERS, and here the difference is not academic. The vocab-parallel
|
||||
* candidate merge gathers (id, score) pairs as an opaque u32 payload -- the ids are i32 and the
|
||||
* scores are f32 bit patterns, in one buffer. Carried through float, an id of -1 (0xFFFFFFFF, the
|
||||
* empty-candidate marker) becomes 4294967296.0f, which is one past the largest u32, and storing
|
||||
* that back is undefined; it lands as zero. A float-carrying reference therefore disagrees with a
|
||||
* correct kernel on exactly the padding of every gathered candidate list, and rounds every id
|
||||
* above 2^24 as well. Copying the stored representation is also simply what the operation IS: no
|
||||
* arithmetic happens here. */
|
||||
* A gather moves bytes, not numbers. The vocab-parallel candidate merge gathers (id, score) pairs
|
||||
* as an opaque u32 payload of i32 ids and f32 bit patterns. Through float, the -1 empty-candidate
|
||||
* id (0xFFFFFFFF) becomes 4294967296.0f, which stores back as zero (undefined), and ids above 2^24
|
||||
* round. */
|
||||
int ref_all_gather(const RadArgs* a, RadStream) {
|
||||
if (!have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *x = &a->t[0], *y = &a->t[1];
|
||||
@@ -188,9 +201,8 @@ int ref_all_gather(const RadArgs* a, RadStream) {
|
||||
const int64_t nel = band_rows(numel(x), p_int(a, "numel", 0));
|
||||
const int world = (int)p_int(a, "world_size", a->world_size > 0 ? a->world_size : 1);
|
||||
if (nel <= 0 || numel(y) < nel * world) return RAD_E_SHAPE;
|
||||
/* One element has to have an address of its own for a byte copy to mean anything, which rules
|
||||
* out the sub-byte codes -- and a gather of packed weights is not a thing any caller asks
|
||||
* for, so refusing says more than a silent unpack would. */
|
||||
/* A byte copy needs whole-byte elements; sub-byte codes are refused (no caller gathers packed
|
||||
* weights). */
|
||||
const int64_t esz = rad_dtype_bytes(x->dtype, 1);
|
||||
if (esz <= 0 || y->dtype != x->dtype) return RAD_E_DTYPE;
|
||||
|
||||
@@ -201,25 +213,11 @@ int ref_all_gather(const RadArgs* a, RadStream) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
const int rank = a->rank;
|
||||
RAD_REF_TRY(join(rank, world));
|
||||
RAD_REF_TRY(publish(a->rank, world, x, nel));
|
||||
Group& g = group();
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g.m);
|
||||
g.slot[rank] = x->data;
|
||||
g.nel [rank] = nel;
|
||||
g.dt [rank] = x->dtype;
|
||||
}
|
||||
if (!barrier(g)) return RAD_E_DEVICE;
|
||||
|
||||
for (int r = 0; r < world; ++r) {
|
||||
if (!g.slot[r] || g.nel[r] != nel) return RAD_E_SHAPE;
|
||||
if (g.dt[r] != x->dtype) return RAD_E_DTYPE;
|
||||
}
|
||||
/* WHERE A RANK'S ELEMENTS LAND. `row` absent (or the whole message) is the plain
|
||||
* concatenation; present, the placement is row-interleaved -- x is [rows][row] and y is
|
||||
* [rows][world][row] -- which is the form the vocab-parallel candidate merge reads, because it
|
||||
* takes one row of gathered candidates per sampled position at a single row stride. */
|
||||
/* Placement. `row` absent (or the whole message) is plain concatenation; present, it is
|
||||
* row-interleaved, x [rows][row] into y [rows][world][row], the form the vocab-parallel
|
||||
* candidate merge reads (one row of candidates per sampled position at one stride). */
|
||||
const int64_t row = p_int(a, "row", 0);
|
||||
if (row < 0 || (row > 0 && nel % row)) return RAD_E_SHAPE;
|
||||
for (int r = 0; r < world; ++r)
|
||||
@@ -231,9 +229,8 @@ int ref_all_gather(const RadArgs* a, RadStream) {
|
||||
(const char*)g.slot[r] + i * esz, (size_t)esz);
|
||||
}
|
||||
|
||||
/* y is a different buffer from x, so no second barrier is needed for correctness -- but the
|
||||
* ranks must not race ahead into the NEXT collective while a peer still reads this one's
|
||||
* input, and the pairing of barriers is what keeps the call sequences aligned. */
|
||||
/* y is separate from x, but ranks must not start the next collective while a peer still reads
|
||||
* this one's input; the barrier also keeps call sequences paired. */
|
||||
if (!barrier(g)) return RAD_E_DEVICE;
|
||||
return RAD_OK;
|
||||
}
|
||||
Reference in new issue
Block a user