Files
radiance/libref/ref_collective.cpp
Deadcode 3b13fd3266 radiance 2.3.0
New models. Thinking Machines' Inkling 2 Mini by gostar (#584-#586): its text backbone and loop, the
release's NVFP4 and MXFP8 checkpoint read by the converter, an int8 trunk, the vision tower, tensor
parallelism, tool calls (with Warzuponus) and an image processor that matches transformers bit for
bit (millaguie). Inkling's routed experts are served in IQR-V4's four-bit trellis (moe_gemm_iqr),
fitted by GPTQ against Hessians the plugin now records (`--calib-dir`), its token embedding in int8
(read in place by embed_lookup), and incoai's DFlash2 drafter in four bits in place of the release's
DSpark. That container, 14.4 GiB with the vision tower, decodes 190 / 427 / 658 tokens a second at 1
/ 4 / 8 streams at tp 2 on two R9700 with `draft-window: adaptive` (code 240 / 550 / 790,
step-by-step arithmetic 358 / 811 / 1116): 19-43% ahead of the same target with the release's DSpark
drafter and 45-83% ahead of the all-int8 container (26.1 GiB, 124 / 263 / 386), at a mean KL of
0.0066 against the release's weights (int8: 0.0042). DeepSeek-V4.1-Flash on 2.x by baKon (#583),
with its vision tower, its IQ1_S and IQ2_XXS containers and linears and experts served in
llama.cpp's own blocks; parts ported from mikoskinen/r9700-exact-serving by Dad_Vibes' fork.
GLM-5.3-Flash on 2.x with its IQR / IQ8R container, an int8 latent cache and four-card serving by
AnimusNull (#566). Gemma 4 dense on EXL3-V3 by keptn42 (#537), on the EXL3 dense rows and
qwen35_exl3 tensor parallelism by Hero422. NVIDIA's 2-bit Nemotron-3.5-Bolt-74B-A9B on one card by
keptn42 (#477). Wan 2.2 TI2V-5B text-to-video and first-frame video by rkcth (#535). LFM3-1.7B on
RDNA3 iGPUs by DokterSmol (#578). Qwen3.8-27B in bf16 with the DFlash2 drafter, and bf16x12, bf16
weights packed losslessly in 12.25 bits, by eddie168 (#577). Int8 experts for drama-3 by morevgalex
(#598).

Speculative decoding. Draft trees for Qwen3.8-27B's DFlash2 drafts by Kezi (#450, opt-in): one card,
+17% at one stream and +14% at two. --draft-max-lanes and --draft-min-accept, two gates on a block
drafter, and --draft-window-ckpt, its window KV kept in the checkpoint slots, by rkcth (#600-#602).
`draft-window: adaptive` verifies each sequence's drafts up to the first one unlikely to be kept. A
verify's rows share each K and V tile in Inkling's attention by gostar (#608): 93 to 118 tokens a
second at a 70k-token prompt on one card.

Generation. Krea 2 instruction edits with one or two references and registered outfill, LoRA on the
image generators with bias deltas and row-mixer adapters applied per request, and MiniMax-H3's
model-declared sparse attention profiles, by rkcth (#534, #542, #543, #605-#609). MiniMax-H3's audio
reference guide pinned from a frame by 1beb (#595). LTX-2's chunk ceiling raised to 40 by Itoash
(#570).

Fleets and memory. memory.queue-reserve, the hardware queues a card keeps ready, by kevinjd (#589).
memory.pinned-numa-node by Foundry (#580). memory.card-headroom-mib on single-model tensor-parallel
budgets by zihaomu (#624). Parked weights rebuilt on the CPU, memory ownership kept across a failed
handoff and quiescent callbacks serialised, by rkcth (#557-#561). A warm load may unload a parked
model (#592); the first card listed chooses the architecture (#458).

Kernels. libr3d's packed dense IQR GEMM and routed IQR expert rows on gfx1100, and libhalo's GEMV
decode for Qwen3.8-Flash-Next's MoE down, by zihaomu (#562, #620, #622). GGML IQ2_XXS and IQ3_XXS
packed on the card by baKon.

Dashboard. KV cache in tokens (in use, capacity now and with all flexible space), live figures
through long prefill steps, step / decode / prefill / acceptance lines without holes, and expert hit
rates only on tiered models. Seven tile and history defects fixed by Noblistic (#597), vision
encoder and per-card hardware figures by davetha (#574), per-LoRA attribution by dowjames (#552).

Fixed. return_tokens / return_token_ids on the OpenAI endpoints (#571). reasoning_effort "none" and
"minimal" reach a template that takes them as levels, by millaguie (#608). Misaligned loads in the
windowed paged attention over an int8 cache (#540). A retired 27B head refused by name (#541).
rad-convert's GPU trellis jobs kept fed, and --reuse-only, by gostar (#575). jinja's tojson takes
sort_keys by Warzuponus (#582). hfget measures file sizes with stat, by kepler_62f (#623). The
tested C++ compiler is stated in the build guide by lowell80 (#551). The codebase was cleaned up:
shared helpers instead of repeated blocks, and the int8 linear, activation and GEMM code no longer
named after the retired fp8 formats.

Inkling 2 Mini is served in one form: IQR-V4 experts with the DFlash2 drafter
(StillDeadcode/inkling-2-mini-iq4r-v4 on Hugging Face, or
data/recipes/inkling2-mini-iq4r-v4.recipe). A container with the release's DSpark drafter, or with
NVFP4, int8 or 4-bit experts, is refused by name and needs converting again.

Plugin ABI 44: out-of-tree plugins need to be rebuilt.

Thanks to @gostar, @baKon, @Dad_Vibes, @AnimusNull, @keptn42, @Hero422, @rkcth, @DokterSmol,
@eddie168, @morevgalex, @Kezi, @1beb, @Itoash, @kevinjd, @Foundry, @zihaomu, @Noblistic, @davetha,
@dowjames, @Warzuponus, @millaguie, @kepler_62f, @lowell80 and @Code000.
2026-11-26 15:28:37 +01:00

237 lines
11 KiB
C++

/* ref_collective.cpp -- all_reduce and all_gather on the host backend.
*
* 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.
*
* 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 (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=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>
#include <cstring>
using namespace ref;
namespace {
enum { REF_MAX_RANKS = 8 };
/* 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] = {};
int64_t nel [REF_MAX_RANKS] = {};
uint32_t dt [REF_MAX_RANKS] = {};
};
static Group& group() { static Group g; return g; }
/* 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). The
* 30 s default 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;
if (++g.arrived == g.world) {
g.arrived = 0;
++g.gen;
g.cv.notify_all();
return true;
}
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;
}
/* 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.members == 0 && g.arrived == 0)) g.world = world;
else if (g.world != world) return RAD_E_INVAL;
if (member) ++g.members;
return RAD_OK;
}
/* 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;
return ws_get((size_t)nel);
}
} /* namespace */
int ref_collective_init(const RadParam* p, int n_p, int rank, int world_size,
void** out) {
/* 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;
RAD_REF_TRY(join(rank, ws, true));
if (out) *out = &group();
return RAD_OK;
}
/* 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);
if (ws <= 1) return 0;
const int64_t nel = p_int(a, "numel", a->n_t > 0 ? numel(&a->t[0]) : 0);
return nel * (int64_t)sizeof(float);
}
/* ------------------------------------------------------------------ all_reduce */
/* `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];
const RadTensor* y = t_in(a, 1);
/* The message is the operand; `numel` is the band. See band_rows in ref_common.h. */
const int64_t nel = band_rows(numel(x), p_int(a, "numel", 0));
if (nel <= 0) return RAD_E_SHAPE;
if (y && numel(y) < nel) return RAD_E_SHAPE;
const RadTensor* dst = y ? y : x;
const int world = (int)p_int(a, "world_size", a->world_size > 0 ? a->world_size : 1);
if (world <= 1) {
/* 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;
}
RAD_REF_TRY(publish(a->rank, world, x, nel));
Group& g = group();
float* acc = staging(a, nel);
for (int64_t i = 0; i < nel; ++i) {
float s = 0.0f;
for (int r = 0; r < world; ++r) s += ld_dt(g.dt[r], g.slot[r], i);
acc[i] = s;
}
/* 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;
}
/* ------------------------------------------------------------------ all_gather */
/* y is [world_size * numel] with rank r's contribution at offset r * numel.
*
* 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];
/* The contribution is the operand; `numel` is the band. See band_rows in ref_common.h. */
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;
/* 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;
if (world <= 1) {
for (int64_t i = 0; i < nel; ++i)
std::memcpy((char*)y->data + offlin(y, i) * esz,
(const char*)x->data + offlin(x, i) * esz, (size_t)esz);
return RAD_OK;
}
RAD_REF_TRY(publish(a->rank, world, x, nel));
Group& g = group();
/* 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)
for (int64_t i = 0; i < nel; ++i) {
const int64_t o = (row > 0 && row < nel)
? (i / row) * (world * row) + (int64_t)r * row + (i % row)
: (int64_t)r * nel + i;
std::memcpy((char*)y->data + offlin(y, o) * esz,
(const char*)g.slot[r] + i * esz, (size_t)esz);
}
/* 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;
}