Files
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

310 lines
16 KiB
C++

/* engine_mm.cpp -- the encoder driver (spec §11).
*
* The scheduler holds back a request whose media is not encoded (Scheduler::media_ready) and hands
* its items here. They are encoded between steps, never inside one: a pass runs with no step in
* flight, is read back, and the item is marked done, so admission finds its rows in host memory and
* the batch builder copies them in with the chunk that reaches them. The step path never waits on
* the tower.
*
* A pass holds whole segments: an image is one segment, a video one per temporal group, and the
* encoder attends within a segment (the processor keeps each within a pass). Several items share a
* pass when they fit. An audio clip is one segment of the audio encoder, whose passes are separate.
*
* Every rank runs the whole tower from its own copy of the patches: every rank embeds every token,
* so this costs compute per card instead of a transfer between them.
*/
#include "engine_priv.h"
#include "mm/processor.h"
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
namespace rad {
int Engine::run_encodes() {
std::vector<std::shared_ptr<mm::Item>> items;
sched_.take_encodes(&items);
if (items.empty()) return RAD_OK;
const auto t0 = std::chrono::steady_clock::now();
/* Diagnostic: RADIANCE_DEBUG_MM_DUMP=<dir> writes each item's patches (`.pix.bf16`, processor
* output in merge-block order) and rows (`.bf16`) as raw bf16, named by content key, for
* comparison against a reference encoder. */
static const char* const dump_dir = std::getenv("RADIANCE_DEBUG_MM_DUMP");
auto dump = [&](const mm::Item& it, const char* ext, const uint16_t* p, size_t n) {
char path[512];
std::snprintf(path, sizeof path, "%s/mm_%016llx%016llx_%dx%dx%d%s", dump_dir,
(unsigned long long)it.key_hi, (unsigned long long)it.key_lo, it.grid_t,
it.grid_h, it.grid_w, ext);
if (FILE* f = std::fopen(path, "wb")) {
std::fwrite(p, sizeof(uint16_t), n, f);
std::fclose(f);
RAD_INFO("encoder: wrote %s", path);
}
};
Rank& r0 = *ranks_[0];
const Program& prog = r0.program;
/* Diagnostic: RADIANCE_DEBUG_MM_BUFFERS names encoder buffers (comma separated, with an
* optional `:rows` cap) and each pass writes them, as raw bytes, to `<dump dir>/buf.<name>`.
* The pass has finished and been read back, so the copy sees each buffer's last writer; the
* tower's per-stage intermediates are tapped inside the pass instead (rad_block_sam.h). */
struct DumpBuf { std::string name; int64_t rows; };
std::vector<DumpBuf> dump_bufs;
static const char* const dump_list = std::getenv("RADIANCE_DEBUG_MM_BUFFERS");
if (dump_dir && *dump_dir && dump_list) {
for (const char* s = dump_list; *s; ) {
const char* e = s;
while (*e && *e != ',') ++e;
std::string want(s, (size_t)(e - s));
s = *e ? e + 1 : e;
int64_t rows = 0;
if (const size_t colon = want.find(':'); colon != std::string::npos) {
rows = std::strtoll(want.c_str() + colon + 1, nullptr, 10);
want.resize(colon);
}
if (!want.empty()) dump_bufs.push_back({ want, rows });
}
}
auto answers_to = [](const std::string& name, const std::string& want) {
if (name.size() < want.size() ||
name.compare(name.size() - want.size(), want.size(), want) != 0) return false;
return name.size() == want.size() || name[name.size() - want.size() - 1] == '.';
};
/* What this run encodes, for the counters: an item another run already encoded is not. The
* run ends on every return, a failed one included, or the encoder would read busy forever. */
int64_t n_items = 0, n_patches = 0;
struct RunEnd {
Scheduler& s; const int64_t& items; const int64_t& patches;
~RunEnd() { s.encodes_done(items, patches); }
} run_end{sched_, n_items, n_patches};
for (auto& ip : items) {
if (ip->encoded) continue;
++n_items;
n_patches += ip->patch_at(ip->n_seg);
const RadEncoderDecl& e = ip->kind == mm::Kind::Audio ? prog.audio_encoder : prog.encoder;
ip->embd.assign((size_t)(ip->n_rows() * e.n_embd), 0);
if (dump_dir && *dump_dir) dump(*ip, ".pix.bf16", ip->pixels.data(), ip->pixels.size());
}
/* Passes per encoder: audio through the audio encoder, pictures and video through the vision
* encoder, each with its own pass size and output buffer. */
for (int which = 0; which < 2; ++which) {
const bool audio = which == 1;
const RadEncoderDecl& enc = audio ? prog.audio_encoder : prog.encoder;
const std::string& enc_name = audio ? prog.audio_encoder_name : prog.encoder_name;
std::vector<mm::Item*> mine;
for (auto& ip : items)
if (!ip->encoded && (ip->kind == mm::Kind::Audio) == audio) mine.push_back(ip.get());
if (mine.empty()) continue;
if (enc.modalities == 0) {
RAD_ERR("encoder: a %s item reached the encoder and this program declares no %s "
"encoder", audio ? "audio" : "picture", audio ? "audio" : "image");
return RAD_E_STATE;
}
const int64_t max_p = enc.max_patches;
const int64_t n_embd = enc.n_embd;
size_t i = 0;
int32_t seg = 0;
/* The pass's grid, when the encoder declares that a pass is one geometry
* (RadEncoderDecl::one_grid). */
const bool one_grid = enc.one_grid != 0;
int32_t seg_grid = 0;
while (i < mine.size()) {
std::vector<BatchBuilder::EncoderPart> parts;
int64_t np = 0;
while (i < mine.size()) {
mm::Item& it = *mine[i];
const int32_t first = seg;
for (; seg < it.n_seg; ++seg) {
const int32_t g = it.seg_h(seg);
/* The one-output-row view separator (core/mm marks it explicitly) belongs to
* the pass of the view before it, so it neither breaks at a "grid change" nor
* sets the pass's grid. A square segment that is a real view (a chunked line
* of one tile) keeps the ordinary geometry rule below. */
const bool sep = it.seg_is_sep(seg);
const int64_t seg_p = it.patch_at(seg + 1) - it.patch_at(seg);
if (seg_p > max_p) {
RAD_ERR("encoder: a segment of %lld patches does not fit a pass of %lld",
(long long)seg_p, (long long)max_p);
return RAD_E_STATE;
}
if (one_grid && !sep && np > 0 && seg_grid != 0 && g != seg_grid) break;
if (np + seg_p > max_p) break;
np += seg_p;
if (!sep) seg_grid = g;
}
if (seg == first) {
/* Nothing of this item packed into the pass: its first segment did not fit
* a pass that already has content (a segment larger than the whole pass was
* refused above). `seg` addresses a real segment only below `it.n_seg`;
* a degenerate zero-segment item leaves seg_grid alone. */
if (seg_grid == 0 && seg < it.n_seg) seg_grid = it.seg_h(seg);
break;
}
parts.push_back({ &it, first, seg - first });
if (seg == it.n_seg) { ++i; seg = 0; }
}
if (parts.empty()) break;
const RadBatch* b = sched_.build_encoder(parts, enc_step_++, enc);
if (!b) return RAD_E_STATE;
RAD_TRY(prepare_release(b));
/* Diagnostic: RADIANCE_DEBUG_MM_DUMP also writes the card's pixel staging exactly
* as the pass reads it, so a bad upload is visible without a host probe. */
if (dump_dir && *dump_dir && b->enc_pixels && b->enc_n_patch > 0) {
const int64_t nbytes = (int64_t)b->enc_n_patch * enc.patch_dim *
(int64_t)sizeof(uint16_t);
std::vector<char> pix((size_t)nbytes);
if (rad_memcpy_async(pix.data(), b->enc_pixels, nbytes, r0.ctx.stream()) >= 0) {
rad_stream_sync(r0.ctx.stream());
char path[512];
std::snprintf(path, sizeof path, "%s/mm_pix.%016llx%016llx.raw", dump_dir,
(unsigned long long)parts.front().item->key_hi,
(unsigned long long)parts.front().item->key_lo);
if (FILE* f = std::fopen(path, "wb")) {
std::fwrite(pix.data(), 1, pix.size(), f);
std::fclose(f);
RAD_INFO("encoder: wrote %s (%lld patches)", path,
(long long)b->enc_n_patch);
}
}
}
/* Read the output address after prepare_release: a pass runs at the arena's full plan
* (Engine::arena_step), and a buffer's offset differs per level. */
void* out = r0.ctx.buf_ptr(enc.out);
if (!out) {
RAD_ERR("encoder '%s': its output buffer was declared but never planned",
enc_name.c_str());
return RAD_E_STATE;
}
/* kIssueModel: the pass only, with no sampler and no tiering tick. */
issue_what_.store(kIssueModel, std::memory_order_release);
step_batch_.store(b, std::memory_order_release);
barrier_->arrive_and_wait();
barrier_->arrive_and_wait();
const int st = step_status_.load(std::memory_order_acquire);
if (st < 0) {
RAD_ERR("encoder pass failed: %s -- the engine cannot continue", rad_strerror(st));
return st;
}
/* The rows, in pass order: each part's segments are consecutive rows of its item. A
* typed span (Item::types) covers every span position: its image rows are copied in
* place from the pass's output, its other rows from the encoder's span_rows table,
* assembled after the sync below. */
int64_t row = 0;
struct TypedPart { mm::Item* it; int64_t off; int64_t rows; };
std::vector<TypedPart> typed;
for (const BatchBuilder::EncoderPart& p : parts) {
mm::Item& it = *const_cast<mm::Item*>(p.item);
const int64_t first = it.row_at(p.seg0);
const int64_t rows = it.row_at(p.seg0 + p.n_seg) - first;
const int64_t bytes = rows * n_embd * (int64_t)sizeof(uint16_t);
if (!it.types.empty()) {
typed.push_back(TypedPart{ &it, row * n_embd * (int64_t)sizeof(uint16_t), rows });
row += rows;
continue;
}
RAD_TRY(rad_memcpy_async(it.embd.data() + first * n_embd,
static_cast<const char*>(out) +
row * n_embd * (int64_t)sizeof(uint16_t),
bytes, r0.ctx.stream()));
row += rows;
}
/* Sync every rank's stream: the next pass rewrites every rank's staging and arena. */
for (auto& rk : ranks_) RAD_TRY(rad_stream_sync(rk->ctx.stream()));
/* Typed spans: the image rows take the pass's output, every other position the
* encoder's learned row for its type (0 start, 2 newline, 3 end). */
if (!typed.empty()) {
int64_t ncopy = 0;
for (const TypedPart& t : typed)
ncopy = std::max(ncopy, t.off / (int64_t)sizeof(uint16_t) + t.rows * n_embd);
/* `out` is the encoder's device buffer. The untyped path copies with
* rad_memcpy_async; a host memcpy of that pointer faults. */
std::vector<uint16_t> host_out((size_t)ncopy, 0);
if (ncopy > 0 &&
rad_memcpy_async(host_out.data(), out,
host_out.size() * sizeof(uint16_t), r0.ctx.stream()) < 0)
return RAD_E_STATE;
std::vector<uint16_t> sr((size_t)(4 * n_embd), 0);
if (enc.span_rows) {
void* sp = r0.ctx.buf_ptr(enc.span_rows);
if (sp &&
rad_memcpy_async(sr.data(), sp, (size_t)(4 * n_embd) * sizeof(uint16_t),
r0.ctx.stream()) < 0)
return RAD_E_STATE;
}
RAD_TRY(rad_stream_sync(r0.ctx.stream()));
for (const TypedPart& t : typed) {
mm::Item& it = *t.it;
const uint16_t* src = host_out.data() + t.off / (int64_t)sizeof(uint16_t);
it.embd.assign((size_t)it.expand.size() * (size_t)n_embd, 0);
int64_t k = 0;
for (size_t pos = 0; pos < it.types.size() && pos < it.expand.size(); ++pos) {
const size_t ty = it.types[pos];
const uint16_t* from = src + k * n_embd;
if (ty != 1) {
if (ty > 3) return RAD_E_INVAL;
from = sr.data() + ty * (size_t)n_embd;
}
std::memcpy(it.embd.data() + pos * (size_t)n_embd, from,
(size_t)n_embd * sizeof(uint16_t));
if (ty == 1) ++k;
}
}
}
if (dump_dir && *dump_dir && !dump_bufs.empty()) {
for (const DumpBuf& d : dump_bufs) {
for (size_t bi = 1; bi < prog.buffers.size(); ++bi) {
const BufferInfo& info = prog.buffers[bi];
if (!answers_to(info.name, d.name) || info.bytes <= 0) continue;
const int64_t nrow = info.decl.rank >= 1 ? info.decl.shape[0] : 1;
const int64_t rowb = nrow > 0 ? info.bytes / nrow : info.bytes;
const int64_t rows = d.rows > 0 ? std::min<int64_t>(d.rows, nrow) : nrow;
void* ptr = r0.ctx.buf_ptr((rad_buf)bi);
std::vector<char> raw((size_t)(rows * rowb));
if (!ptr || rad_memcpy_async(raw.data(), ptr, raw.size(),
r0.ctx.stream()) < 0)
continue;
rad_stream_sync(r0.ctx.stream());
char path[512];
std::snprintf(path, sizeof path, "%s/mm_%016llx%016llx_buf.%s.raw",
dump_dir, (unsigned long long)parts.front().item->key_hi,
(unsigned long long)parts.front().item->key_lo,
info.name.c_str());
if (FILE* f = std::fopen(path, "wb")) {
std::fwrite(raw.data(), 1, raw.size(), f);
std::fclose(f);
RAD_INFO("encoder: wrote %s (%lld rows of %lld bytes)", path,
(long long)rows, (long long)rowb);
}
}
}
}
}
}
for (auto& ip : items) {
mm::Item& it = *ip;
if (it.encoded) continue;
it.encoded = true;
it.pixels.clear();
it.pixels.shrink_to_fit();
if (dump_dir && *dump_dir) dump(it, ".bf16", it.embd.data(), it.embd.size());
}
const double ms = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - t0).count();
RAD_DEBUG("encoder: %zu item(s) encoded in %.1f ms", items.size(), ms);
return RAD_OK;
}
} /* namespace rad */