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:
Deadcode committed 2026-10-16 18:25:18 +02:00
1 parent b068190326
commit 1e968f6e1d
739 files changed
+128447 -84632

No files matched your search

+48 -68
View File
@@ -15,8 +15,8 @@ constexpr int64_t kAlign = 256;
int64_t align_up(int64_t v) { return (v + kAlign - 1) / kAlign * kAlign; }
/* A reference picture's latent tokens at most: its area, a little over for the rounding of its
* sides to the size multiple. */
/* Upper bound on a reference picture's latent tokens: its area plus slack for rounding its sides
* to the size multiple. */
int64_t ref_tokens_max(const RadGeneratorDecl& d) {
const int64_t s2 = d.latent_scale * d.latent_scale;
return (d.ref_area + s2 - 1) / s2 * 11 / 10 + 64;
@@ -42,8 +42,8 @@ Sizes sizes_of(const Program& prog, int64_t block_size) {
return z;
}
/* The regions, in order, and the total. One function for both the allocation and the budget's
* count of it, so the two cannot disagree. */
/* Region offsets and sizes, in order, and the total. Shared by the allocation and the VRAM budget
* so the two always agree. */
int64_t carve(const Sizes& z, int64_t pixel_ch, int64_t (*bytes)[2], int n) {
const int64_t I = 4, H = 2;
const int64_t rope_rows = std::max(z.P, z.T);
@@ -58,7 +58,7 @@ int64_t carve(const Sizes& z, int64_t pixel_ch, int64_t (*bytes)[2], int n) {
{ 0, (z.R + 2) * I }, /* R_ENCCU */
{ 0, z.P * I }, /* R_SRC */
{ 0, 3 * rope_rows * I }, /* R_GROPE */
{ 0, (12 * z.R + 24) * I }, /* R_SEG: offsets, {0, length} and start a segment, {0, tokens} a picture */
{ 0, (12 * z.R + 24) * I }, /* R_SEG: offsets; per segment a {0, length} pair and a start; per picture a {0, tokens} pair */
{ 0, z.P * I }, /* R_SLOT */
{ 0, z.PX * pixel_ch * H }, /* R_PX */
{ 0, z.NT * 4 }, /* R_MASK */
@@ -83,26 +83,22 @@ int64_t carve(const Sizes& z, int64_t pixel_ch, int64_t (*bytes)[2], int n) {
} /* namespace */
int64_t Driver::device_bytes(const Program& prog, const Config& cfg) {
(void)cfg;
int64_t Driver::device_bytes(const Program& prog) {
if (!prog.is_generator() || prog.kv_groups.size() < 2) return 0;
const Sizes z = sizes_of(prog, prog.kv_groups[1].block_size);
int64_t b[R_N][2];
return carve(z, prog.generator.pixel_channels, b, R_N);
}
int Driver::init(const Program& prog, const Config& cfg, IKVManager* kv,
const std::vector<RankIO>& ranks) {
(void)cfg;
int Driver::init(const Program& prog, IKVManager* kv, const std::vector<RankIO>& ranks) {
fini();
if (!prog.is_generator()) return RAD_E_STATE;
decl_ = prog.generator;
fixed_ = prog.gen_str.sigmas;
kv_ = kv;
ranks_ = ranks;
/* ONE KV GROUP, PAGED: the conditioning's keys and values. A generator with no conditioning
* cache, or two, is not a shape this driver stages. Group handles start at 1 -- the program's
* group 0 is the null handle -- and the KV manager and a batch both index by handle. */
/* Exactly one paged full-attention KV group, holding the conditioning. Group handles start at
* 1 (group 0 is the null handle); the KV manager and a batch both index by handle. */
if (prog.kv_groups.size() != 2 || prog.kv_groups[1].decl.kind != RAD_KV_FULL ||
prog.kv_groups[1].block_size <= 0) {
RAD_ERR("image generator: the program declares %zu KV group(s); the driver stages exactly "
@@ -262,12 +258,16 @@ Driver::Job* Driver::job_locked(uint64_t id) {
return nullptr;
}
void Driver::release_locked(Job& j) {
void Driver::release_kv_locked(Job& j) {
for (int b = 0; b < 2; ++b)
if (j.cond[b].have_kv) {
kv_->free_sequence(j.cond[b].seq);
j.cond[b].have_kv = false;
}
}
void Driver::release_locked(Job& j) {
release_kv_locked(j);
for (int32_t l : j.lanes) {
lanes_[(size_t)l] = Lane{};
free_lanes_.push_back(l);
@@ -280,14 +280,14 @@ void Driver::fail_locked(Job& j, int status, const std::string& why) {
j.req->ticket->finish(status, why);
}
/* IN ORDER, AND A REQUEST THAT DOES NOT FIT STOPS THE ONES BEHIND IT. */
/* In arrival order; a request that does not fit stops the ones behind it. */
int Driver::admit_locked() {
for (auto& jp : jobs_) {
Job& j = *jp;
if (j.admitted || j.failed) continue;
Request& r = *j.req;
if ((int64_t)free_lanes_.size() < r.n) break;
/* The sequences, once: their length is what the KV has to hold. */
/* Build the layouts once; their length sizes the KV. */
if (j.cond[0].lay.rows == 0) {
j.ref_hw.clear();
for (const Pixels& p : r.refs)
@@ -329,9 +329,8 @@ int Driver::admit_locked() {
}
if (j.failed) continue;
if (!ok) {
/* The KV is short now: give back what this one took and wait. */
for (int b = 0; b < 2; ++b)
if (j.cond[b].have_kv) { kv_->free_sequence(j.cond[b].seq); j.cond[b].have_kv = false; }
/* KV is short: release what this request took and wait. */
release_kv_locked(j);
break;
}
j.sigmas = schedule(decl_, fixed_, r.steps, j.tokens);
@@ -346,7 +345,7 @@ int Driver::admit_locked() {
free_lanes_.pop_back();
Lane& ln = lanes_[(size_t)l];
ln = Lane{};
ln.job = 0;
ln.busy = true;
ln.job_id = j.id;
ln.index = i;
ln.seed = r.seed + (uint64_t)i;
@@ -398,6 +397,16 @@ void Driver::finish_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int ste
B.n_seq_decode = 0;
B.n_tok_decode = 0;
B.max_q_len_prefill = max_q;
/* No rotary table and no encoder input; build_cond sets its own. */
B.rope_pos = nullptr;
B.rope_mixed = 0;
B.n_mm_rows = 0;
B.mm_rows = nullptr;
B.enc_n_patch = 0;
B.enc_pixels = nullptr;
B.enc_coord = nullptr;
B.enc_n_seg = 0;
B.enc_cu = nullptr;
K.group = (rad_kvgroup)kv_group_;
K.slot_mapping = reg_[R_SLOT].used > 0 ? dptr<int32_t>(ri, R_SLOT) : nullptr;
K.block_table = dptr<int32_t>(ri, R_BT);
@@ -431,15 +440,14 @@ const RadBatch* Driver::next(int step) {
pass_ = Pass{};
dec_bytes_ = 0;
if (closed_) return nullptr;
/* What finished, failed or was given up on since the last pass leaves now: its KV and lanes
* go back before anything is admitted. Nothing is in flight -- next() runs between passes. */
/* Retire finished, failed and cancelled jobs, returning their KV and lanes before admitting.
* Safe because next() runs between passes, with nothing in flight. */
for (auto it = jobs_.begin(); it != jobs_.end();) {
Job& j = **it;
const bool gone = j.failed || j.req->ticket->cancel.load(std::memory_order_acquire) ||
(j.admitted && j.images_done >= j.req->n);
const bool cancelled = j.req->ticket->cancel.load(std::memory_order_acquire);
const bool gone = j.failed || cancelled || (j.admitted && j.images_done >= j.req->n);
if (!gone) { ++it; continue; }
if (j.req->ticket->cancel.load(std::memory_order_acquire) && !j.failed &&
j.images_done < j.req->n)
if (cancelled && !j.failed && j.images_done < j.req->n)
j.req->ticket->finish(RAD_E_CANCELLED, "the request was cancelled");
release_locked(j);
it = jobs_.erase(it);
@@ -449,7 +457,7 @@ const RadBatch* Driver::next(int step) {
/* 1. A decode an image is owed. */
for (int32_t l = 0; l < (int32_t)lanes_.size(); ++l) {
Lane& ln = lanes_[(size_t)l];
if (ln.job < 0 || ln.finished) continue;
if (!ln.busy || ln.finished) continue;
if (ln.preview_due) return build_decode(l, true, step);
if (ln.final_due) return build_decode(l, false, step);
}
@@ -515,8 +523,8 @@ const RadBatch* Driver::build_cond(Job& j, int branch, int step) {
reg_[R_ROPE].used = 3 * T * 4;
reg_[R_CU].used = 2 * 4;
/* The pictures, for the conditioning network's encoder: every item's patches in prompt order,
* one segment an item, and the prompt rows the encoder's output rows replace. */
/* Pictures for the conditioning network's encoder: every item's patches in prompt order, its
* segments, and the prompt rows the encoder's output replaces. */
int64_t n_patch = 0, n_seg = 0, n_mm = 0;
int32_t max_seg = 0;
if (br.media) {
@@ -525,8 +533,6 @@ const RadBatch* Driver::build_cond(Job& j, int branch, int step) {
int32_t* ecu = hptr<int32_t>(R_ENCCU);
int64_t total = 0;
for (const mm::Placed& p : br.media->items) total += p.item->n_patch;
std::vector<int64_t> row0;
int64_t rows_at = 0;
ecu[0] = 0;
std::vector<int32_t> tmp;
for (const mm::Placed& p : br.media->items) {
@@ -544,24 +550,19 @@ const RadBatch* Driver::build_cond(Job& j, int branch, int step) {
max_seg = std::max<int32_t>(max_seg, (int32_t)(e - a));
++n_seg;
}
row0.push_back(rows_at);
rows_at += it.n_rows();
n_patch += it.n_patch;
}
int32_t* mmr = hptr<int32_t>(R_MMROW);
for (const mm::RopeLayout::Run& run : br.media->rope.runs) {
const int64_t base = row0[(size_t)run.item] + run.row0;
(void)base;
for (const mm::RopeLayout::Run& run : br.media->rope.runs)
for (int32_t k = 0; k < run.n; ++k) mmr[n_mm++] = run.tok + k;
}
reg_[R_ENCPIX].used = n_patch * patch_dim_ * 2;
reg_[R_ENCCRD].used = 4 * total * 4;
reg_[R_ENCCU].used = (n_seg + 1) * 4;
reg_[R_MMROW].used = n_mm * 4;
}
/* The generator's sequence, which the plugin runs a segment at a time: each segment's rotary
* positions as one dense [3, length] block, its own {0, length} and its first row. */
/* The generator's sequence, run by the plugin a segment at a time: per segment, its rotary
* positions as a dense [3, length] block, a {0, length} cu pair and its first row. */
std::memcpy(hptr<int32_t>(R_SRC), c.lay.src.data(), (size_t)P * 4);
const int64_t nsg = (int64_t)c.lay.bidi.size();
int32_t* grope = hptr<int32_t>(R_GROPE);
@@ -593,8 +594,8 @@ const RadBatch* Driver::build_cond(Job& j, int branch, int step) {
hptr<int32_t>(R_USED)[0] = (int32_t)P;
reg_[R_USED].used = 4;
/* The pictures again, for the generator's own encoder, and with a mask the picture being
* repainted -- only the positive conditioning's pass writes the lanes. */
/* Pictures again for the generator's own encoder, plus the inpainting source when masked.
* Only the positive conditioning's pass writes the lanes. */
const int64_t ch = decl_.pixel_channels;
uint16_t* px = hptr<uint16_t>(R_PX);
std::vector<int32_t> pxh;
@@ -626,13 +627,12 @@ const RadBatch* Driver::build_cond(Job& j, int branch, int step) {
/* The conditioning is modulated at t = 0. */
hptr<float>(R_SIG)[0] = 0.0f;
reg_[R_SIG].used = 4;
/* The host arrays the plugin reads on the host: the segments' kinds, the pixel blocks and the
* lanes. Kept here, in the driver, for the life of the pass. */
/* Host-side arrays the plugin reads (segment kinds, pixel blocks, lanes), owned by the driver
* for the life of the pass. */
host_bidi_ = c.lay.bidi;
host_px_ = pxh;
host_lanes_ = inp ? j.lanes : std::vector<int32_t>{};
/* ...and the lanes on the device too: the repainted picture's latent and mask are copied into
* each of them by an op. */
/* The lanes on the device too: an op copies the inpainting latent and mask into each. */
if (!host_lanes_.empty()) {
std::memcpy(hptr<int32_t>(R_LANE), host_lanes_.data(), host_lanes_.size() * 4);
reg_[R_LANE].used = (int64_t)host_lanes_.size() * 4;
@@ -689,12 +689,11 @@ const RadBatch* Driver::build_cond(Job& j, int branch, int step) {
const RadBatch* Driver::build_denoise(const std::vector<int32_t>& take, int step) {
for (int g = 0; g < R_N; ++g) reg_[g].used = 0;
/* Two passes over the images: the entries and their rows, then the per-entry arrays. */
/* Two passes: collect the entries and their rows, then fill the per-entry arrays. */
struct E { const Job* j; int32_t lane; int branch; };
std::vector<E> es;
for (int32_t l : take) {
const Job* j = nullptr;
for (auto& jp : jobs_) if (jp->id == lanes_[(size_t)l].job_id) { j = jp.get(); break; }
const Job* j = job_locked(lanes_[(size_t)l].job_id);
if (!j) continue;
for (int b = 0; b < j->n_branch; ++b) es.push_back({ j, l, b });
}
@@ -777,15 +776,6 @@ const RadBatch* Driver::build_denoise(const std::vector<int32_t>& take, int step
RadBatch& B = out_[rr];
const int ri = (int)rr;
B.max_ctx_len = max_ctx;
B.rope_pos = nullptr;
B.rope_mixed = 0;
B.n_mm_rows = 0;
B.mm_rows = nullptr;
B.enc_n_patch = 0;
B.enc_pixels = nullptr;
B.enc_coord = nullptr;
B.enc_n_seg = 0;
B.enc_cu = nullptr;
B.gen = RAD_GEN_PASS_DENOISE;
kout_[rr].block_table_pitch = pitch;
kout_[rr].slot_mapping = nullptr;
@@ -808,8 +798,7 @@ const RadBatch* Driver::build_denoise(const std::vector<int32_t>& take, int step
const RadBatch* Driver::build_decode(int32_t l, bool preview, int step) {
for (int g = 0; g < R_N; ++g) reg_[g].used = 0;
const Lane& ln = lanes_[(size_t)l];
const Job* j = nullptr;
for (auto& jp : jobs_) if (jp->id == ln.job_id) { j = jp.get(); break; }
const Job* j = job_locked(ln.job_id);
if (!j) return nullptr;
int32_t* cu = hptr<int32_t>(R_CU);
cu[0] = 0;
@@ -820,15 +809,6 @@ const RadBatch* Driver::build_decode(int32_t l, bool preview, int step) {
for (size_t rr = 0; rr < ranks_.size(); ++rr) {
RadBatch& B = out_[rr];
B.max_ctx_len = 0;
B.rope_pos = nullptr;
B.rope_mixed = 0;
B.n_mm_rows = 0;
B.mm_rows = nullptr;
B.enc_n_patch = 0;
B.enc_pixels = nullptr;
B.enc_coord = nullptr;
B.enc_n_seg = 0;
B.enc_cu = nullptr;
B.gen = RAD_GEN_PASS_DECODE;
B.n_kv_groups = 0;
B.kv = nullptr;
+26 -29
View File
@@ -1,28 +1,25 @@
/* driver.h -- the image generator's pass driver (gen.h says what the passes are).
/* driver.h -- the image generator's pass driver (gen.h describes the passes).
*
* ONE THREAD DRIVES IT, the engine's, and it asks two things a pass: `next()` decides what runs
* and stages it, `done()` folds the result back once every rank has run it. Requests arrive from
* HTTP threads through `submit()` and leave through their Ticket; the queue is the only thing the
* two kinds of thread share, behind one mutex.
* Driven by the engine thread: `next()` picks and stages a pass, `done()` folds its result back
* once every rank has run it. Requests arrive from HTTP threads through `submit()` and leave
* through their Ticket; the job queue is the only shared state, behind one mutex.
*
* WHAT RUNS NEXT, in this order, one pass at a time:
* Pass priority, one pass at a time:
*
* 1. a DECODE an image is owed -- a finished image, or a preview its last step marked. First,
* 1. DECODE an image is owed: a finished image, or a preview its last step marked. Goes first
* because a finished image frees its lane and answers its caller, and a preview must be read
* before the lane's next step writes the preview latent again.
* 2. a COND an admitted request is owed: its conditioning, so its images can join the next
* denoise pass.
* 3. a DENOISE over as many images in flight as fit the step's rows, oldest request first, each
* before the lane's next step overwrites the preview latent.
* 2. COND an admitted request is owed, so its images can join the next denoise pass.
* 3. DENOISE over as many in-flight images as fit the step's rows, oldest request first, each
* at its own step.
*
* A request is ADMITTED when it can get lanes for every image and KV for every conditioning
* sequence; until then it waits, in order -- a request that does not fit blocks the ones behind
* it, so a large request is not starved by a stream of small ones.
* A request is admitted once it can get lanes for every image and KV for every conditioning
* sequence. Admission is in order: a request that does not fit blocks those behind it, so a large
* request is not starved by a stream of small ones.
*
* THE STAGING is one region per rank on its card and one pinned host region with the same layout.
* Every pass is synchronised before the next is built (Engine::gen_loop), so one host region is
* enough: an image pass is tens of milliseconds and more, and the host work between two is
* microseconds.
* Staging is one device region per rank plus one pinned host region with the same layout. Every
* pass is synchronised before the next is built (Engine::gen_loop), so one host region suffices:
* a pass takes tens of milliseconds, the host work between passes microseconds.
*/
#pragma once
#include "gen.h"
@@ -42,21 +39,20 @@ public:
};
/* `kv` is rank 0's KV manager: one block table serves every rank. */
int init(const Program& prog, const Config& cfg, IKVManager* kv, const std::vector<RankIO>& ranks);
int init(const Program& prog, IKVManager* kv, const std::vector<RankIO>& ranks);
void fini();
~Driver() { fini(); }
/* The bytes init() allocates on each rank's card, for the VRAM budget. */
static int64_t device_bytes(const Program& prog, const Config& cfg);
static int64_t device_bytes(const Program& prog);
/* ---------------------------------------------------------- HTTP threads */
/* Queue a request. Refused at once -- with `why` -- when it can never run: a size past the
/* Queue a request. Refused at once, with `why`, when it can never run: a size past the
* largest image, more pictures than the model takes, a conditioning longer than a pass. */
int submit(std::unique_ptr<Request> r, std::string* why);
/* What a request may ask for: the model's own limits, for the server to check before it does
* the expensive part of preparing one. */
/* The model's limits, for the server to check before the expensive part of preparing a
* request. */
const RadGeneratorDecl& decl() const { return decl_; }
const std::vector<double>& fixed_sigmas() const { return fixed_; }
int64_t max_cond_tokens() const { return max_tok_; }
/* Fail every request queued or in flight, and refuse new ones. */
void shutdown(const char* why, int status);
@@ -66,8 +62,8 @@ public:
* `step` is the engine's pass counter, carried into RadBatch::step. */
const RadBatch* next(int step);
const RadBatch* batch_for(int rank) const;
/* The bytes of the generator's image buffer the pass just staged makes, 0 for a pass that
* makes none: the engine copies them to `host()` behind the pass. */
/* Bytes of image output the staged pass produces, 0 if none; the engine copies them to
* host_image() behind the pass. */
int64_t decode_bytes() const { return dec_bytes_; }
void* host_image() const { return img_host_; }
/* The pass has run on every rank and its streams are drained. */
@@ -101,7 +97,7 @@ private:
bool failed = false;
};
struct Lane {
int64_t job = -1; /* index into jobs_ by id; -1 free */
bool busy = false; /* held by request job_id */
uint64_t job_id = 0;
int32_t index = 0;
int32_t step = 0;
@@ -112,7 +108,7 @@ private:
bool final_due = false;
bool finished = false;
};
/* What the pass being run is, for done(). */
/* The pass in flight, for done(). */
struct Pass {
int kind = 0;
uint64_t job = 0;
@@ -138,6 +134,7 @@ private:
void finish_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int step);
int admit_locked();
void release_kv_locked(Job& j);
void release_locked(Job& j);
void fail_locked(Job& j, int status, const std::string& why);
Job* job_locked(uint64_t id);
@@ -168,7 +165,7 @@ private:
std::vector<RadGenBatch> gout_;
std::vector<RadKVGroupBatch> kout_;
Pass pass_;
/* The host arrays a COND pass's plugin reads on the host (RadGenBatch's _h fields). */
/* Host arrays a COND pass's plugin reads on the host (RadGenBatch's _h fields). */
std::vector<int32_t> host_bidi_, host_px_, host_lanes_;
mutable std::mutex mu_;
+2 -7
View File
@@ -29,11 +29,6 @@ void Ticket::finish(int status, const std::string& why) {
cv_.notify_all();
}
void Ticket::step(int32_t done, int32_t total) {
(void)done;
(void)total;
}
bool Ticket::next(Image* out, int ms, bool* timed_out) {
std::unique_lock<std::mutex> lk(mu_);
if (timed_out) *timed_out = false;
@@ -65,8 +60,8 @@ std::string Ticket::why() const {
/* ================================================================== the schedule */
/* diffusers' FlowMatchEulerDiscreteScheduler.set_timesteps with explicit sigmas, as the pipeline
* calls it, in the precision it runs at: the sigmas are an f32 array and every shift is numpy f32
* arithmetic, the Python floats in it (exp(mu), the shift, the terminal) taking the array's type. */
* calls it and at its precision: the sigmas are f32 and every shift is numpy f32 arithmetic, with
* the Python scalars (exp(mu), the shift, the terminal) taking the array's type. */
std::vector<float> schedule(const RadGeneratorDecl& d, const std::vector<double>& fixed,
int32_t steps, int64_t tokens) {
std::vector<float> s;
+30 -34
View File
@@ -1,25 +1,23 @@
/* gen.h -- image generation: what a request is, what comes back, and the driver that turns one
/* gen.h -- image generation: the request, what comes back, and the driver that turns a request
* into the passes an image generator declares (rad_declare_generator, abi/rad_builder.h).
*
* A GENERATOR IS NOT DRIVEN BY TOKEN STEPS. Its work is three kinds of pass -- conditioning,
* denoising, decoding -- and an image is tens of denoising passes over thousands of latent rows,
* every one of them read whole, none of them sampled. The scheduler that serves text owns token
* positions, prefix reuse, preemption and speculation, and none of that has a meaning here, so the
* generator gets a driver of its own (this file) and shares everything below the step with the rest
* of the engine: the ranks, the plugin's step function, the KV manager the conditioning lives in.
* A generator is not driven by token steps: an image is tens of denoising passes over thousands of
* latent rows, all read whole, none sampled. Token positions, prefix reuse, preemption and
* speculation have no meaning here, so the generator has its own driver and shares only what lies
* below the step: the ranks, the plugin's step function and the KV manager holding conditioning.
*
* THE PASSES, as the driver runs them:
* Passes, as the driver runs them:
*
* COND once for every conditioning sequence a request has -- its prompt, and with guidance
* its negative prompt -- the prompt (and its reference pictures) encoded into the
* generator's KV group. A request is admitted once its lanes and its KV are free.
* DENOISE one step for each image in flight that fits the pass, any mix of requests, sizes and
* step counts: each image carries its own sigma. With guidance an image is two entries,
* its positive and its negative conditioning, which the plugin combines.
* COND once per conditioning sequence (the prompt, plus the negative prompt with guidance):
* the prompt and its reference pictures encoded into the generator's KV group. A
* request is admitted once its lanes and its KV are free.
* DENOISE one step for each in-flight image that fits the pass, any mix of requests, sizes and
* step counts; each image carries its own sigma. With guidance an image is two entries,
* positive and negative conditioning, which the plugin combines.
* DECODE one image's latent to pixels, read back and handed to its request.
*
* A request's images run in LANES -- the plugin's own latent state, `max_lanes` of them -- so one
* request of n images takes n lanes and shares one conditioning.
* Images run in lanes, the plugin's own latent state (`max_lanes` of them): a request of n images
* takes n lanes and shares one conditioning.
*/
#pragma once
#include "rad_core.h"
@@ -44,15 +42,15 @@ struct Pixels {
std::vector<uint16_t> bf16;
};
/* ONE CONDITIONING SEQUENCE: the prompt as tokens, with the media it carries placed, and how many
* of its first tokens the generator does not see (RadGeneratorDecl::drop_prefix). */
/* One conditioning sequence: the prompt tokens with their media placed, and how many leading
* tokens the generator does not see (RadGeneratorDecl::drop_prefix). */
struct Branch {
std::vector<int32_t> tokens;
std::shared_ptr<mm::PromptMedia> media; /* null for a prompt with no pictures */
int32_t n_drop = 0;
};
/* ONE IMAGE, or one preview of one, on its way back. */
/* One finished image, or a preview of one. */
struct Image {
int32_t index = 0; /* which of the request's n */
int32_t w = 0, h = 0, ch = 4;
@@ -62,18 +60,17 @@ struct Image {
uint64_t seed = 0;
};
/* THE CHANNEL BACK TO THE CALLER: images as they finish, then the end. The driver's thread writes,
* the caller's reads; a mutex is enough -- an image is megabytes and there are a handful a
* request. `cancel` is the caller's, read by the driver at its next pass. */
/* The channel back to the caller: images as they finish, then the end. The driver thread writes,
* the caller reads; a mutex suffices for a handful of megabyte images per request. `cancel` is set
* by the caller and read by the driver at its next pass. */
class Ticket {
public:
/* driver side */
void push(Image&& im);
void finish(int status, const std::string& why = std::string());
void step(int32_t done, int32_t total);
/* caller side: the next image, or false once the request has ended and every image was taken.
* Waits at most `ms` milliseconds; `*timed_out` says a false was a timeout, not the end. */
* Waits at most `ms` milliseconds; `*timed_out` marks a false that was a timeout, not the end. */
bool next(Image* out, int ms, bool* timed_out);
bool finished() const;
int status() const;
@@ -81,8 +78,8 @@ public:
std::atomic<bool> cancel{false};
/* What the request cost, for the response: the conditioning's tokens, the latent rows made, and
* the time each phase took. Written by the driver before finish(). */
/* Usage for the response: conditioning tokens, latent rows made, and per-phase times.
* Written by the driver before finish(). */
int64_t in_tokens = 0, out_tokens = 0;
int64_t t_submit_ns = 0, t_cond_ns = 0, t_first_step_ns = 0, t_denoised_ns = 0, t_done_ns = 0;
int32_t steps = 0;
@@ -107,8 +104,8 @@ struct Request {
int32_t previews = 0; /* partial images per image, 0..3 */
/* The reference pictures' encoder view, one per media item of `pos`, in order. */
std::vector<Pixels> refs;
/* REPAINTING (an edit with a mask): the picture at the size being made, and per latent token
* how much of it is repainted (1) and how much kept (0). */
/* Inpainting (an edit with a mask): the source picture at the output size, and per latent
* token how much is repainted (1) versus kept (0). */
bool inpaint = false;
Pixels inp_src;
std::vector<float> inp_mask;
@@ -116,16 +113,15 @@ struct Request {
};
/* ================================================================== the sampler's schedule */
/* The sigmas a request steps through, n + 1 of them, the last 0: the decl's fixed schedule, or
* `steps` (0: the default) shifted the way the decl says. `tokens` is the image's latent token
* count, which a dynamic shift reads. f32, as the reference holds them. */
/* The n + 1 sigmas a request steps through, the last 0: the decl's fixed schedule, or `steps`
* (0: the default) shifted as the decl says. `tokens` is the image's latent token count, read by a
* dynamic shift. f32, matching the reference. */
std::vector<float> schedule(const RadGeneratorDecl& d, const std::vector<double>& fixed,
int32_t steps, int64_t tokens);
/* ================================================================== the generator's sequence */
/* ONE CONDITIONING SEQUENCE AS THE GENERATOR SEES IT (RadGeneratorDecl::layout): which prompt row
* or reference latent row each of its rows is, its rotary positions, its segments, and where the
* image being made starts. */
/* One conditioning sequence as the generator sees it (RadGeneratorDecl::layout): the prompt row or
* reference latent row behind each row, rotary positions, segments, and the output image's frame. */
struct Layout {
int64_t rows = 0;
std::vector<int32_t> src; /* [rows] */