Archived
radiance 1.9.0
New models: - LTX-2.5 (base and distilled): video with a stereo soundtrack from text or from text and a first frame, on one card, with its duration head, tiled VAE decoding, the diffusion decoder and the x2 latent upsampler - Krea 2 Raw and Turbo, and FLUX.3 [klein] and its base: text to image, and for FLUX.3 edits from up to ten reference pictures, on one card - GLM-5.3-Flash from AMD's Quark MXFP4 export, on eight cards (John McDonough, Nunze) - Gemma 3 / TranslateGemma 27B with an optional DFlash2 drafter (mici81) Engine: - --kv-cache-dtype fp8_v4 and k6_v4 for Gemma 3 and Qwen3.8-Flash-Next (mici81) - the rotated int8 trunk (i8a8h) for Qwen3.8-27B (eddie168168) - eight-rank all-reduce bands, weighted shards and pieces (Nunze); --host-headroom-mib (sfjes) - the dashboard shows the single-stream decode floor and its most recent window (Memoria) Fixes: prefix-cache reservations kept until their save lands (baKon), tool calls in Responses namespaces (#119), system messages merged when a template refuses several (#137), a prefill continued with no open step (#127), calibration of a Gram accumulation (#122), JSON Schema patterns read unanchored (davetha), GLM-style tool calls, the KV floor counting every sequence's state. Plugin ABI 24.
This commit is contained in:
1 parent
778cbf315e
commit
685ec5bd85
136 files changed
+18917
-3821
No files matched your search
@@ -47,6 +47,7 @@
|
||||
#include <arch/rad_arch.h>
|
||||
#include <arch/rad_fp8.h>
|
||||
#include <arch/rad_block_vit.h>
|
||||
#include <arch/rad_vae2d.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -75,291 +76,6 @@ static inline int64_t ref_tokens_max() { return (int64_t)kRefArea / 256 * 11 / 1
|
||||
|
||||
static const char* kSysPrompt = "Comprehend and analyze the provided prompt.";
|
||||
|
||||
/* ================================================================== the VAE as a recorded graph
|
||||
*
|
||||
* Image size varies per request, so the VAE's ops are declared once for the largest image and
|
||||
* issued with the actual extents: every image operand is a raw [height, width * channels] view of
|
||||
* its buffer (docs/OPS.md, "Image generation"), with extents from the pass's picture size and the
|
||||
* stage's scale. `Vae` records each op with what it needs to rebuild its operands, so declare()
|
||||
* and step() walk the same list.
|
||||
*
|
||||
* Every output is its own buffer, sized for its stage; the planner shares storage between those
|
||||
* whose lives do not overlap, and no buffer is reused by hand. This is sound because ops are issued
|
||||
* in declaration order. */
|
||||
struct VImg {
|
||||
rad_buf buf = 0;
|
||||
int64_t ch = 0;
|
||||
int lvl = 0; /* the side is the pass's base side times 2^lvl (decoder) or / 2^lvl (encoder) */
|
||||
};
|
||||
|
||||
struct VOp {
|
||||
enum Kind { CONV, NORM, ADD, D2S, S2D, ATTN } kind = CONV;
|
||||
rad_op op = 0;
|
||||
VImg x{}, y{}, z{}; /* inputs x (and z for ADD), output y */
|
||||
rad_weight w = 0, b = 0;
|
||||
int k = 1, up = 1, stride = 1;
|
||||
int64_t qkv_ch = 0; /* ATTN: x is the [*, 3C] qkv rows, y the [*, C] output */
|
||||
};
|
||||
|
||||
struct Vae {
|
||||
std::vector<VOp> ops;
|
||||
Names* nm = nullptr;
|
||||
RadBuilder* b = nullptr;
|
||||
const char* scope = ""; /* "dec" or "enc" */
|
||||
bool down = false; /* an encoder: a level halves the side */
|
||||
int64_t base_px = 0; /* pixels at level 0, the largest any pass hands it */
|
||||
int n_buf = 0;
|
||||
int first_group = 0;
|
||||
/* The banded tail. From op `band_from` on, every output pixel reads a bounded window of rows
|
||||
* (3x3 convolutions, nearest upsampling, per-pixel norms and adds, depth-to-space shortcuts),
|
||||
* so a rank can run the tail on its band of level-0 rows plus `halo` rows per side and get its
|
||||
* band's pixels bit for bit: rows spoiled by a conv's zero padding at the band edge never reach
|
||||
* the band. The ops before it include the mid-block's attention, which reads every pixel, and
|
||||
* run whole; `whole` holds their outputs, read by the tail at the band's row offset. -1: no
|
||||
* tail. */
|
||||
int band_from = -1;
|
||||
int64_t halo = 0;
|
||||
std::vector<rad_buf> whole;
|
||||
|
||||
int64_t px_at(int lvl) const {
|
||||
int64_t p = base_px;
|
||||
for (int i = 0; i < lvl; ++i) p = down ? (p + 3) / 4 : p * 4;
|
||||
return p;
|
||||
}
|
||||
VImg img(int64_t ch, int lvl) {
|
||||
VImg v;
|
||||
v.ch = ch;
|
||||
v.lvl = lvl;
|
||||
v.buf = decl_b(b, nm->f("%s.t%d", scope, n_buf++), RAD_BF16, { px_at(lvl), ch });
|
||||
return v;
|
||||
}
|
||||
rad_weight wt(const char* name, const char* src, std::initializer_list<int64_t> shape) {
|
||||
if (map_copy(b, name, src) < 0) return 0;
|
||||
return decl_w(b, name, RAD_BF16, shape, RAD_ACCESS_PER_REQUEST, RAD_SHARD_NONE,
|
||||
grp_layer(first_group));
|
||||
}
|
||||
|
||||
/* y = conv(x): k x k, stride, `up` 2 reads x upsampled, `pad` same or down. */
|
||||
VImg conv(const VImg& x, int64_t cout, int k, int stride, int up, const char* pad,
|
||||
const char* ckpt) {
|
||||
VOp o;
|
||||
o.kind = VOp::CONV;
|
||||
o.x = x;
|
||||
o.up = up;
|
||||
o.stride = stride;
|
||||
int lvl = x.lvl;
|
||||
if (up == 2) lvl = down ? lvl - 1 : lvl + 1;
|
||||
if (stride == 2) lvl = down ? lvl + 1 : lvl - 1;
|
||||
o.y = img(cout, lvl);
|
||||
o.k = k;
|
||||
o.w = wt(nm->f("%s.weight", ckpt), nm->ckpt("%s.weight", ckpt), { cout, x.ch, k, k });
|
||||
o.b = wt(nm->f("%s.bias", ckpt), nm->ckpt("%s.bias", ckpt), { cout });
|
||||
o.op = RAD_OP(b, "conv2d_nhwc",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, px_at(lvl)), RAD_INT("cin", x.ch),
|
||||
RAD_INT("cout", cout), RAD_INT("k", k),
|
||||
RAD_INT("stride", stride), RAD_STR("pad", pad),
|
||||
RAD_INT("up", up), RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(o.w, o.b));
|
||||
/* An input with no buffer is the picture the pass staged, read where it lies. */
|
||||
if (x.buf) rw(b, o.op, { x.buf }, { o.y.buf });
|
||||
else rw(b, o.op, {}, { o.y.buf });
|
||||
ops.push_back(o);
|
||||
return o.y;
|
||||
}
|
||||
/* The channel norm, `silu` or not. `gamma` is [C, 1, 1(, 1)] in the checkpoint. */
|
||||
VImg norm(const VImg& x, bool silu, const char* ckpt, int gamma_rank) {
|
||||
VOp o;
|
||||
o.kind = VOp::NORM;
|
||||
o.x = x;
|
||||
o.y = img(x.ch, x.lvl);
|
||||
const char* dn = nm->f("%s.gamma", ckpt);
|
||||
if (map_copy(b, dn, nm->ckpt("%s.gamma", ckpt)) < 0) return VImg{};
|
||||
o.w = gamma_rank == 4
|
||||
? decl_w(b, dn, RAD_BF16, { x.ch, 1, 1, 1 }, RAD_ACCESS_PER_REQUEST, RAD_SHARD_NONE, grp_layer(first_group))
|
||||
: decl_w(b, dn, RAD_BF16, { x.ch, 1, 1 }, RAD_ACCESS_PER_REQUEST, RAD_SHARD_NONE, grp_layer(first_group));
|
||||
o.op = rw(b, RAD_OP(b, "l2norm_chan",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, px_at(x.lvl)), RAD_INT("n", x.ch),
|
||||
RAD_STR("act", silu ? "silu" : "none"),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_WEIGHTS(o.w)),
|
||||
{ x.buf }, { o.y.buf });
|
||||
ops.push_back(o);
|
||||
return o.y;
|
||||
}
|
||||
VImg add(const VImg& x, const VImg& z) {
|
||||
VOp o;
|
||||
o.kind = VOp::ADD;
|
||||
o.x = x;
|
||||
o.z = z;
|
||||
o.y = img(x.ch, x.lvl);
|
||||
o.op = rw(b, RAD_OP(b, "add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, px_at(x.lvl)), RAD_INT("n", x.ch),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ x.buf, z.buf }, { o.y.buf });
|
||||
ops.push_back(o);
|
||||
return o.y;
|
||||
}
|
||||
/* y += the resampling shortcut of x (in place on y). */
|
||||
void shortcut(const VImg& x, const VImg& y, int ft) {
|
||||
VOp o;
|
||||
o.kind = down ? VOp::S2D : VOp::D2S;
|
||||
o.x = x;
|
||||
o.y = y;
|
||||
o.up = ft;
|
||||
o.op = rw(b, RAD_OP(b, down ? "s2d_avg_add" : "d2s_add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, px_at(y.lvl)), RAD_INT("cin", x.ch),
|
||||
RAD_INT("cout", y.ch), RAD_INT("ft", ft),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
RAD_NOWEIGHTS),
|
||||
{ x.buf, y.buf }, { y.buf });
|
||||
ops.push_back(o);
|
||||
}
|
||||
/* A residual block, in -> out channels. */
|
||||
VImg resblock(const VImg& x, int64_t out, const char* ckpt) {
|
||||
VImg h = x;
|
||||
if (x.ch != out) h = conv(x, out, 1, 1, 1, "same", nm->f("%s.conv_shortcut", ckpt));
|
||||
VImg t = norm(x, true, nm->f("%s.norm1", ckpt), 4);
|
||||
t = conv(t, out, 3, 1, 1, "same", nm->f("%s.conv1", ckpt));
|
||||
t = norm(t, true, nm->f("%s.norm2", ckpt), 4);
|
||||
t = conv(t, out, 3, 1, 1, "same", nm->f("%s.conv2", ckpt));
|
||||
return add(t, h);
|
||||
}
|
||||
/* The mid-block's single-head attention over every pixel. */
|
||||
VImg attention(const VImg& x, const char* ckpt) {
|
||||
const int64_t C = x.ch;
|
||||
VImg t = norm(x, false, nm->f("%s.norm", ckpt), 3);
|
||||
VImg qkv = conv(t, 3 * C, 1, 1, 1, "same", nm->f("%s.to_qkv", ckpt));
|
||||
VOp o;
|
||||
o.kind = VOp::ATTN;
|
||||
o.x = qkv;
|
||||
o.y = img(C, x.lvl);
|
||||
o.qkv_ch = 3 * C;
|
||||
o.op = rw(b, RAD_OP(b, "attn_dense",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, px_at(x.lvl)), RAD_INT("head_dim", C),
|
||||
RAD_INT("gqa", 1), RAD_INT("causal", 0),
|
||||
RAD_STR("dtype", "bf16"),
|
||||
RAD_F64("scale", 1.0 / std::sqrt((double)C))),
|
||||
RAD_NOWEIGHTS),
|
||||
{ qkv.buf }, { o.y.buf });
|
||||
ops.push_back(o);
|
||||
VImg p = conv(o.y, C, 1, 1, 1, "same", nm->f("%s.proj", ckpt));
|
||||
return add(p, x);
|
||||
}
|
||||
|
||||
/* The operands of op `o` for a pass whose level-0 picture is H0 x W0 pixels (or latent tokens,
|
||||
* for the decoder), in the op's schema order, and its ranged value. */
|
||||
static int64_t side(int64_t s0, int lvl, bool dn) {
|
||||
int64_t s = s0;
|
||||
for (int i = 0; i < lvl; ++i) s = dn ? s / 2 : s * 2;
|
||||
return s;
|
||||
}
|
||||
/* `cu` is {0, pixels} at the attention's level -- the pass's latent tokens -- and `input` the
|
||||
* level-0 picture when the first op reads one the pass staged. Ops [from, to) are issued; with
|
||||
* `row0`, H0 is a band's rows and a `whole` buffer is read from its level-0 row `row0`. */
|
||||
void issue(RadCtx* c, int64_t H0, int64_t W0, const RadOperand& cu, const void* input,
|
||||
size_t from = 0, size_t to = (size_t)-1, int64_t row0 = 0) const;
|
||||
|
||||
bool is_whole(rad_buf v) const {
|
||||
for (rad_buf w : whole) if (w == v) return true;
|
||||
return false;
|
||||
}
|
||||
/* The tail's halo in level-0 rows: walked back from the last op, a buffer's margin is the most
|
||||
* any reader of it needs, in its own rows -- a k x k conv adds k/2, reading through a 2x
|
||||
* upsample halves it rounding up, and the depth-to-space shortcut halves it. -1 when the tail
|
||||
* holds an op that is not row-local, or reads a whole buffer above level 0. */
|
||||
int64_t band_halo() const {
|
||||
if (band_from < 0 || ops.empty()) return -1;
|
||||
std::vector<std::pair<rad_buf, int64_t>> need{ { ops.back().y.buf, 0 } };
|
||||
auto get = [&](rad_buf v) -> int64_t {
|
||||
for (const auto& e : need) if (e.first == v) return e.second;
|
||||
return 0;
|
||||
};
|
||||
auto want = [&](rad_buf v, int64_t m) {
|
||||
for (auto& e : need) if (e.first == v) { e.second = std::max(e.second, m); return; }
|
||||
need.push_back({ v, m });
|
||||
};
|
||||
for (size_t i = ops.size(); i-- > (size_t)band_from;) {
|
||||
const VOp& o = ops[i];
|
||||
const int64_t my = get(o.y.buf);
|
||||
switch (o.kind) {
|
||||
case VOp::CONV: {
|
||||
if (o.stride != 1) return -1;
|
||||
const int64_t m = my + o.k / 2;
|
||||
want(o.x.buf, o.up == 2 ? (m + 1) / 2 : m);
|
||||
break;
|
||||
}
|
||||
case VOp::NORM: want(o.x.buf, my); break;
|
||||
case VOp::ADD: want(o.x.buf, my); want(o.z.buf, my); break;
|
||||
case VOp::D2S: want(o.x.buf, (my + 1) / 2); break;
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
int64_t h = 0;
|
||||
for (size_t i = (size_t)band_from; i < ops.size(); ++i) {
|
||||
const VOp& o = ops[i];
|
||||
for (const VImg* v : { &o.x, &o.z })
|
||||
if (v->buf && is_whole(v->buf)) {
|
||||
if (v->lvl != 0) return -1;
|
||||
h = std::max(h, get(v->buf));
|
||||
}
|
||||
}
|
||||
return h;
|
||||
}
|
||||
};
|
||||
|
||||
inline void Vae::issue(RadCtx* c, int64_t H0, int64_t W0, const RadOperand& cu,
|
||||
const void* input, size_t from, size_t to, int64_t row0) const {
|
||||
auto ptr = [&](const VImg& v) -> const void* {
|
||||
if (!v.buf) return input;
|
||||
const char* p = (const char*)rad_buf_ptr(c, v.buf);
|
||||
return row0 && is_whole(v.buf) ? p + row0 * W0 * v.ch * (int64_t)sizeof(uint16_t) : p;
|
||||
};
|
||||
auto view = [&](const VImg& v, int64_t* H, int64_t* W) {
|
||||
*H = side(H0, v.lvl, down);
|
||||
*W = side(W0, v.lvl, down);
|
||||
return RAD_P_T2(ptr(v), RAD_BF16, *H, *W * v.ch);
|
||||
};
|
||||
auto rows = [&](const VImg& v, int64_t cols) {
|
||||
const int64_t H = side(H0, v.lvl, down), W = side(W0, v.lvl, down);
|
||||
return RAD_P_T2(ptr(v), RAD_BF16, H * W * v.ch / cols, cols);
|
||||
};
|
||||
for (size_t i = from; i < to && i < ops.size(); ++i) {
|
||||
const VOp& o = ops[i];
|
||||
int64_t Hx, Wx, Hy, Wy;
|
||||
switch (o.kind) {
|
||||
case VOp::CONV: {
|
||||
const RadOperand x = view(o.x, &Hx, &Wx), y = view(o.y, &Hy, &Wy);
|
||||
RAD_ISSUE_N(c, o.op, Hy * Wy, x, RAD_W(o.w), RAD_W(o.b), y);
|
||||
break;
|
||||
}
|
||||
case VOp::NORM: {
|
||||
const int64_t H = side(H0, o.x.lvl, down), W = side(W0, o.x.lvl, down);
|
||||
RAD_ISSUE_N(c, o.op, H * W, rows(o.x, o.x.ch), RAD_W(o.w), rows(o.y, o.y.ch));
|
||||
break;
|
||||
}
|
||||
case VOp::ADD: {
|
||||
const int64_t H = side(H0, o.x.lvl, down), W = side(W0, o.x.lvl, down);
|
||||
RAD_ISSUE_N(c, o.op, H * W, rows(o.x, o.x.ch), rows(o.z, o.z.ch), rows(o.y, o.y.ch));
|
||||
break;
|
||||
}
|
||||
case VOp::D2S:
|
||||
case VOp::S2D: {
|
||||
const RadOperand x = view(o.x, &Hx, &Wx), y = view(o.y, &Hy, &Wy);
|
||||
RAD_ISSUE_N(c, o.op, Hy * Wy, x, y);
|
||||
break;
|
||||
}
|
||||
case VOp::ATTN: {
|
||||
/* q, k and v are column slices of the qkv rows, at its 3C pitch. */
|
||||
const int64_t H = side(H0, o.x.lvl, down), W = side(W0, o.x.lvl, down);
|
||||
const int64_t C = o.y.ch, n = H * W;
|
||||
RAD_ISSUE_N(c, o.op, n, bcol(o.x.buf, 0, C, n), bcol(o.x.buf, C, C, n),
|
||||
bcol(o.x.buf, 2 * C, C, n), cu, brows(o.y.buf, n));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================== the model */
|
||||
struct TeLayer {
|
||||
|
||||
Reference in new issue
Block a user