radiance 1.10.0

LoRA for image and video generators:
- civitai and Hugging Face adapter files as published: kohya, ComfyUI and diffusers/PEFT key names;
  LoRA, LoCon, LoHa, LoKr and full-weight diffs; DoRA, OFT and Tucker are refused by name
- stacked per request with a strength each, through the `loras` field or <lora:name:strength> tags
  in the prompt; --lora-dir serves every file in a directory, GET /v1/loras lists them, and
  --max-loras / --max-lora-rank size the slots on the card (generators default to rank 64)
- Krea 2, Qwen-Image 2.1, FLUX.3 [klein], MiniMax H3, LTX-2.5, Anima and SDXL

New models:
- Anima (base, aesthetic and turbo): Cosmos-Predict2 DiT with the Qwen3-0.6B text encoder and the
  Qwen-Image VAE, on one card
- Stable Diffusion XL and its single-file finetunes (Illustrious, NoobAI including V-Pred, Pony),
  read as civitai ships them, with the Euler, Euler a and DPM++ 2M samplers

Recipes:
- q38-27b-mxfp4-df2: any bf16 Qwen3.8-27B or finetune at MXFP4 with the DFlash2 drafter
- Krea 2 finetunes from civitai: scripts/krea2_native.py reads Krea's own key names, and
  krea-2-fp8-unscaled keeps a plain-fp8 file's codes exactly

Changes: --max-num-batched-tokens defaults to 2048 (was 8192). Plugin ABI 25: out-of-tree plugins
must be rebuilt.
This commit is contained in:
Deadcode committed 2026-10-20 21:05:16 +02:00
1 parent 685ec5bd85
commit 4246a1dd04
124 files changed
+14326 -387

No files matched your search

+284
View File
@@ -532,6 +532,221 @@ int ref_flow_x0_step(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------------ kd_input, kd_step */
/* A k-diffusion sampler over a discrete-time (VP-trained) denoiser, run in the variance-exploding
* form x = x0 + sigma eps: the lane holds x in f32, the model reads c_in x with
* c_in = 1 / sqrt(sigma^2 + 1), and a step turns the model's output into the clean estimate D and
* moves x along the schedule (docs/OPS.md, "Image generation"). Every product and sum is one f32
* rounding in the order written; libm's log, expm1 and sqrt. */
namespace {
enum { kSamplerEuler = 0, kSamplerEulerAncestral = 1, kSamplerDpmpp2m = 2 };
/* One entry's per-step constants, from its sigmas and guidance. */
struct KdEntry {
float s = 0, sn = 0, w = 0;
float cskip = 0, cout = 0; /* v-prediction's c_skip and c_out */
float sup = 0, sdown = 0; /* the ancestral step's split of sigma_next */
float ratio = 0, em = 0; /* DPM++ 2M: sigma_next / sigma, expm1(-h) */
float c1 = 0, c2 = 0; /* DPM++ 2M's second-order blend of D and the last D */
bool second = false;
};
KdEntry kd_entry(float s, float sn, float sp, float w, int64_t sampler, bool first) {
KdEntry k;
k.s = s;
k.sn = sn;
k.w = w;
const float s2p = s * s + 1.0f;
k.cskip = 1.0f / s2p;
k.cout = s / std::sqrt(s2p);
if (sampler == kSamplerEulerAncestral) {
const float s2 = s * s, sn2 = sn * sn;
const float up2 = sn2 * (s2 - sn2) / s2;
float sup = std::sqrt(up2);
if (!(sup < sn)) sup = sn;
const float dn2 = sn2 - sup * sup;
k.sup = sup;
k.sdown = std::sqrt(dn2 > 0.0f ? dn2 : 0.0f);
} else if (sampler == kSamplerDpmpp2m && sn > 0.0f) {
const float t = -std::log(s), tn = -std::log(sn);
const float h = tn - t;
k.ratio = sn / s;
k.em = std::expm1(-h);
if (!first) {
const float hl = t - (-std::log(sp));
const float r = hl / h;
k.c2 = 1.0f / (2.0f * r);
k.c1 = 1.0f + k.c2;
k.second = true;
}
}
return k;
}
/* The clean estimate from the model's output `o` at x. */
inline float kd_denoised(const KdEntry& k, bool v_pred, float x, float o) {
return v_pred ? x * k.cskip - o * k.cout : x - k.s * o;
}
/* x after the step, from x, the guided estimate D, the lane's last estimate and the noise. */
inline float kd_next(const KdEntry& k, int64_t sampler, float x, float D, float hist, float nz) {
if (sampler == kSamplerEuler) {
const float d = (x - D) / k.s;
return x + (k.sn - k.s) * d;
}
if (sampler == kSamplerEulerAncestral) {
const float d = (x - D) / k.s;
float xn = x + d * (k.sdown - k.s);
if (k.sn > 0.0f) xn = xn + nz * k.sup;
return xn;
}
if (k.sn == 0.0f) return D;
const float Dd = k.second ? k.c1 * D - k.c2 * hist : D;
return k.ratio * x - k.em * Dd;
}
inline uint64_t kd_seed(const RadTensor* seed, int64_t e) {
return (uint64_t)(uint32_t)ld_int(seed->dtype, seed->data, offlin(seed, 2 * e)) |
((uint64_t)(uint32_t)ld_int(seed->dtype, seed->data, offlin(seed, 2 * e + 1)) << 32);
}
} /* namespace */
/* The model's input for every entry that moves a lane (pair != -2): on a first step the lane
* becomes the seed's normal stream times sqrt(1 + sigma^2) (k-diffusion's start at the schedule's
* largest sigma), then y = rd(x / sqrt(sigma^2 + 1)) at the entry's rows and, when it is guided, at
* its pair's rows too (the negative entry reads the same latent). */
int ref_kd_input(const RadArgs* a, RadStream) {
if (!have(a, 8)) return RAD_E_INVAL;
const RadTensor *sg = &a->t[0], *pair = &a->t[1], *lane = &a->t[2], *fl = &a->t[3];
const RadTensor *seed = &a->t[4], *cu = &a->t[5], *st = &a->t[6], *y = &a->t[7];
const int64_t n = p_int(a, "n", 0), stride = p_int(a, "stride", 0);
if (n <= 0 || stride <= 0) return RAD_E_SHAPE;
const int64_t n_seq = numel(cu) - 1, st_rows = rows_of(st, n), y_rows = rows_of(y, n);
if (n_seq <= 0 || cu_at(cu, n_seq) <= cu_at(cu, 0)) return RAD_E_SHAPE;
if (numel(sg) < n_seq || numel(pair) < n_seq || numel(lane) < n_seq || numel(fl) < n_seq ||
numel(seed) < 2 * n_seq)
return RAD_E_SHAPE;
for (int64_t e = 0; e < n_seq; ++e) {
const int64_t pr = ld_int(pair->dtype, pair->data, offlin(pair, e));
if (pr == -2) continue;
const int64_t L = ld_int(lane->dtype, lane->data, offlin(lane, e));
const int64_t b0 = cu_at(cu, e), len = cu_at(cu, e + 1) - b0;
if (L < 0 || len > stride || (L + 1) * stride > st_rows || b0 < 0 || b0 + len > y_rows)
return RAD_E_SHAPE;
if (pr >= n_seq || (pr >= 0 && (cu_at(cu, pr + 1) - cu_at(cu, pr) != len ||
cu_at(cu, pr) < 0 || cu_at(cu, pr) + len > y_rows)))
return RAD_E_SHAPE;
if (ld_int(fl->dtype, fl->data, offlin(fl, e)) & kInpaint) return RAD_E_UNSUPPORTED;
}
for (int64_t e = 0; e < n_seq; ++e) {
const int64_t pr = ld_int(pair->dtype, pair->data, offlin(pair, e));
if (pr == -2) continue;
const int64_t L = ld_int(lane->dtype, lane->data, offlin(lane, e));
const int64_t b0 = cu_at(cu, e), len = cu_at(cu, e + 1) - b0;
const int64_t p0 = pr >= 0 ? cu_at(cu, pr) : 0;
const float s = ldt(sg, offlin(sg, e));
const float init = std::sqrt(1.0f + s * s), cin = 1.0f / std::sqrt(s * s + 1.0f);
const bool first = (ld_int(fl->dtype, fl->data, offlin(fl, e)) & kFirst) != 0;
const uint64_t sd = kd_seed(seed, e);
REF_PAR_FOR(len * n * 8)
for (int64_t k = 0; k < len; ++k) {
const int64_t sb = rowoff(st, L * stride + k, n), ss = laststride(st);
const int64_t yb = rowoff(y, b0 + k, n), ys = laststride(y);
const int64_t pb = pr >= 0 ? rowoff(y, p0 + k, n) : 0;
for (int64_t c = 0; c < n; ++c) {
if (first) stt(st, sb + c * ss, rad_philox_normal(sd, (uint64_t)(k * n + c)) * init);
const float v = ldt(st, sb + c * ss) * cin;
stt(y, yb + c * ys, v);
if (pr >= 0) stt(y, pb + c * ys, v);
}
}
}
return RAD_OK;
}
/* One sampler step for every entry that moves a lane (pair != -2), x the lane's f32 state:
*
* D = eps: x - sigma o v: x / (sigma^2 + 1) - o sigma / sqrt(sigma^2 + 1)
* D = pair >= 0 ? D(o_u) + cfg (D(o_c) - D(o_u)) : D(o_c)
*
* then by the entry's sampler: 0 Euler, x + (sigma_next - sigma)(x - D) / sigma; 1 Euler ancestral
* (eta 1): the step to sigma_down plus sigma_up times the seed's stream 1 + step; 2 DPM++ 2M:
* (sigma_next / sigma) x - expm1(-h) D', D' blending D with the lane's last estimate after the first
* step, and D itself at sigma_next 0. Flag 4 writes D to the preview; every step leaves D in
* `hist` for the next. */
int ref_kd_step(const RadArgs* a, RadStream) {
if (!have(a, 14)) return RAD_E_INVAL;
const RadTensor *O = &a->t[0], *sg = &a->t[1], *sgn = &a->t[2], *sgp = &a->t[3];
const RadTensor *cfg = &a->t[4], *pair = &a->t[5], *lane = &a->t[6], *fl = &a->t[7];
const RadTensor *seed = &a->t[8], *smp = &a->t[9], *stp = &a->t[10], *cu = &a->t[11];
const RadTensor *st = &a->t[12], *hs = &a->t[13], *pv = t_in(a, 14);
const int64_t n = p_int(a, "n", 0), stride = p_int(a, "stride", 0);
const char* pred = p_str(a, "pred", "eps");
const bool v_pred = pred && !std::strcmp(pred, "v");
if (!v_pred && (!pred || std::strcmp(pred, "eps"))) return RAD_E_INVAL;
if (n <= 0 || stride <= 0) return RAD_E_SHAPE;
const int64_t n_seq = numel(cu) - 1, st_rows = rows_of(st, n), o_rows = rows_of(O, n);
if (n_seq <= 0 || cu_at(cu, n_seq) <= cu_at(cu, 0)) return RAD_E_SHAPE;
for (const RadTensor* t : { sg, sgn, sgp, cfg, pair, lane, fl, smp, stp })
if (numel(t) < n_seq) return RAD_E_SHAPE;
if (numel(seed) < 2 * n_seq || rows_of(hs, n) < st_rows || (pv && rows_of(pv, n) < st_rows))
return RAD_E_SHAPE;
for (int64_t e = 0; e < n_seq; ++e) {
const int64_t pr = ld_int(pair->dtype, pair->data, offlin(pair, e));
if (pr == -2) continue;
const int64_t L = ld_int(lane->dtype, lane->data, offlin(lane, e));
const int64_t b0 = cu_at(cu, e), len = cu_at(cu, e + 1) - b0;
if (L < 0 || len > stride || (L + 1) * stride > st_rows || b0 < 0 || b0 + len > o_rows)
return RAD_E_SHAPE;
if (pr >= n_seq || (pr >= 0 && (cu_at(cu, pr + 1) - cu_at(cu, pr) != len ||
cu_at(cu, pr) < 0 || cu_at(cu, pr) + len > o_rows)))
return RAD_E_SHAPE;
const int64_t f = ld_int(fl->dtype, fl->data, offlin(fl, e));
const int64_t sm = ld_int(smp->dtype, smp->data, offlin(smp, e));
if ((f & kInpaint) || sm < kSamplerEuler || sm > kSamplerDpmpp2m) return RAD_E_UNSUPPORTED;
if ((f & kPreview) && !pv) return RAD_E_INVAL;
}
for (int64_t e = 0; e < n_seq; ++e) {
const int64_t pr = ld_int(pair->dtype, pair->data, offlin(pair, e));
if (pr == -2) continue;
const int64_t L = ld_int(lane->dtype, lane->data, offlin(lane, e));
const int64_t b0 = cu_at(cu, e), len = cu_at(cu, e + 1) - b0;
const int64_t p0 = pr >= 0 ? cu_at(cu, pr) : 0;
const int64_t f = ld_int(fl->dtype, fl->data, offlin(fl, e));
const int64_t sm = ld_int(smp->dtype, smp->data, offlin(smp, e));
const uint64_t stream = (uint64_t)(uint32_t)(1 + ld_int(stp->dtype, stp->data, offlin(stp, e)));
const KdEntry K = kd_entry(ldt(sg, offlin(sg, e)), ldt(sgn, offlin(sgn, e)),
ldt(sgp, offlin(sgp, e)), ldt(cfg, offlin(cfg, e)), sm,
(f & kFirst) != 0);
const uint64_t sd = kd_seed(seed, e);
REF_PAR_FOR(len * n * 8)
for (int64_t k = 0; k < len; ++k) {
const int64_t ob = rowoff(O, b0 + k, n), os = laststride(O);
const int64_t ub = pr >= 0 ? rowoff(O, p0 + k, n) : 0;
const int64_t sb = rowoff(st, L * stride + k, n), ss = laststride(st);
const int64_t hb = rowoff(hs, L * stride + k, n), hss = laststride(hs);
for (int64_t c = 0; c < n; ++c) {
const float x = ldt(st, sb + c * ss);
float D = kd_denoised(K, v_pred, x, ldt(O, ob + c * os));
if (pr >= 0) {
const float Du = kd_denoised(K, v_pred, x, ldt(O, ub + c * os));
D = Du + K.w * (D - Du);
}
if (f & kPreview)
stt(pv, rowoff(pv, L * stride + k, n) + c * laststride(pv), D);
const float nz = sm == kSamplerEulerAncestral && K.sn > 0.0f
? rad_philox_normal_s(sd, stream, (uint64_t)(k * n + c)) : 0.0f;
stt(st, sb + c * ss, kd_next(K, sm, x, D, ldt(hs, hb + c * hss), nz));
stt(hs, hb + c * hss, D);
}
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ chan_affine */
/* A per-channel affine from two parameter lists (a latent's mean and deviation are configuration,
* not weights), each value rounded to bf16 as upstream holds it: mode `fma` is
@@ -621,6 +836,50 @@ int ref_conv2d_nhwc(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------------ im2col_nhwc */
/* conv2d_nhwc's receptive fields as rows, so a convolution is a GEMM against its own weight read as
* [cout, cin k k]: x is [Hin, Win * cin] (rows at their own pitch), y [Ho * Wo, cin * k * k], and
* row oy * Wo + ox, column (ci k + i) k + j holds the value conv2d_nhwc multiplies weight
* [o, ci, i, j] by -- x at the tap's position read through `up` and `pad` as conv2d_nhwc reads it,
* 0 in the padding. The output size follows from x's; y must have exactly Ho * Wo rows. A copy:
* the values are stored as they are (rounded only when y is narrower than x). */
int ref_im2col_nhwc(const RadArgs* a, RadStream) {
if (!have(a, 2)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *y = &a->t[1];
const int64_t cin = p_int(a, "cin", 0), k = p_int(a, "k", 0);
const int64_t stride = p_int(a, "stride", 1), up = p_int(a, "up", 1);
const char* pad = p_str(a, "pad", "same");
const bool down = pad && !std::strcmp(pad, "down");
const bool causal = pad && !std::strcmp(pad, "causal");
if (cin <= 0 || k <= 0 || stride <= 0 || up <= 0) return RAD_E_SHAPE;
if (x->rank != 2 || x->shape[1] % cin) return RAD_E_SHAPE;
const int64_t Hin = x->shape[0], Win = x->shape[1] / cin, kk = cin * k * k;
if (Hin <= 0 || Win <= 0) return RAD_E_SHAPE;
const int64_t Hv = Hin * up, Wv = Win * up;
const int64_t pt = down ? 0 : causal ? k - 1 : k / 2, pb = down ? 1 : causal ? 0 : k / 2;
const int64_t pl = down ? 0 : k / 2, pr = down ? 1 : k / 2;
const int64_t Ho = (Hv + pt + pb - k) / stride + 1, Wo = (Wv + pl + pr - k) / stride + 1;
if (Ho <= 0 || Wo <= 0 || rows_of(y, kk) != Ho * Wo) return RAD_E_SHAPE;
const int64_t xr = x->stride[0], xs = laststride(x), ys = laststride(y);
REF_PAR_FOR(Ho * Wo * kk)
for (int64_t oy = 0; oy < Ho; ++oy)
for (int64_t ox = 0; ox < Wo; ++ox) {
const int64_t yb = rowoff(y, oy * Wo + ox, kk);
for (int64_t ci = 0; ci < cin; ++ci)
for (int64_t i = 0; i < k; ++i) {
const int64_t iy = oy * stride + i - pt;
for (int64_t j = 0; j < k; ++j) {
const int64_t ix = ox * stride + j - pl;
const bool in = iy >= 0 && iy < Hv && ix >= 0 && ix < Wv;
const float v = in ? ldt(x, (iy / up) * xr + ((ix / up) * cin + ci) * xs)
: 0.0f;
stt(y, yb + ((ci * k + i) * k + j) * ys, v);
}
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ l2norm_chan */
/* An image norm over each pixel's channels, as upstream's three bf16 tensors:
* n1 = bf16(x / max(||x||, 1e-12)), n2 = bf16(n1 * sqrt(n)), y = bf16(n2 * gamma), then `act`
@@ -761,6 +1020,31 @@ int ref_gelu_tanh(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------------ gelu_quick */
/* CLIP's quick_gelu as torch evaluates it in a reduced dtype, three stored tensors:
* a = rd(1.702 x), s = rd(sigmoid(a)), y = rd(x s). */
int ref_gelu_quick(const RadArgs* a, RadStream) {
if (!have(a, 2)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *y = &a->t[1];
const int64_t n = p_int(a, "n", 0);
if (n <= 0) return RAD_E_SHAPE;
const int64_t rows = rows_of(y, n);
if (rows <= 0 || rows_of(x, n) < rows) return RAD_E_SHAPE;
const uint32_t dt = y->dtype;
REF_PAR_FOR(rows * n)
for (int64_t r = 0; r < rows; ++r) {
const int64_t xb = rowoff(x, r, n), xs = laststride(x);
const int64_t yb = rowoff(y, r, n), ys = laststride(y);
for (int64_t c = 0; c < n; ++c) {
const float v = ldt(x, xb + c * xs);
const float t = round_dt(dt, 1.702f * v);
const float s = round_dt(dt, 1.0f / (1.0f + std::exp(-t)));
stt(y, yb + c * ys, v * s);
}
}
return RAD_OK;
}
/* x[idx[i]] = x[idx[i]] + v[i], rounded to x's dtype; a negative index is a skip. */
int ref_scatter_add_rows(const RadArgs* a, RadStream) {
if (!have(a, 3)) return RAD_E_INVAL;
+13 -5
View File
@@ -101,8 +101,8 @@ int ref_lora_shrink(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* operands: t, slot, lora_b, scale, y (inout) */
int ref_lora_expand(const RadArgs* a, RadStream) {
/* operands: t, slot, lora_b, scale, y (inout). `bf16_t`: lora_expand_mm, t rounded to bf16. */
static int lora_expand_impl(const RadArgs* a, bool bf16_t) {
if (!have(a, 5)) return RAD_E_INVAL;
const RadTensor *T = &a->t[0], *SL = &a->t[1], *LB = &a->t[2], *SC = &a->t[3], *Y = &a->t[4];
const int64_t N = p_int(a, "N", 0), R = p_int(a, "R", 0), S = p_int(a, "S", 0);
@@ -127,9 +127,11 @@ int ref_lora_expand(const RadArgs* a, RadStream) {
if (s < 0) continue; /* a base row: not read, not written */
const int64_t t0 = m * mt.rs, b0 = s * pb.s0 + n * pb.s1;
float acc = 0.0f;
for (int64_t j = 0; j < R; ++j)
acc += ((const float*)T->data)[t0 + j * mt.cs] *
ld_dt(LB->dtype, LB->data, b0 + j * pb.s2);
for (int64_t j = 0; j < R; ++j) {
float tv = ((const float*)T->data)[t0 + j * mt.cs];
if (bf16_t) tv = rad_bf16_to_f32(rad_f32_to_bf16(tv));
acc += tv * ld_dt(LB->dtype, LB->data, b0 + j * pb.s2);
}
const float sc = ((const float*)SC->data)[s * sc_r + n * sc_c];
const int64_t o = m * ys_r + n * ys_c;
/* `sc * acc` and then the add, each rounded: the order is the definition. */
@@ -138,3 +140,9 @@ int ref_lora_expand(const RadArgs* a, RadStream) {
}
return RAD_OK;
}
int ref_lora_expand(const RadArgs* a, RadStream) { return lora_expand_impl(a, false); }
/* lora_expand with t rounded to bf16; the order of the sum is free, so j ascending is one of the
* answers it allows. */
int ref_lora_expand_mm(const RadArgs* a, RadStream) { return lora_expand_impl(a, true); }
+10
View File
@@ -137,6 +137,7 @@ int ref_gemm_nt_q_bias (const RadArgs*, RadStream);
/* Runtime LoRA's two halves, per-row adapter slot (ref_lora.cpp). */
int ref_lora_shrink (const RadArgs*, RadStream);
int ref_lora_expand (const RadArgs*, RadStream);
int ref_lora_expand_mm (const RadArgs*, RadStream);
int ref_embed_lookup (const RadArgs*, RadStream);
int ref_embed_lookup_q (const RadArgs*, RadStream);
int ref_logits_gemm (const RadArgs*, RadStream);
@@ -162,6 +163,8 @@ int ref_lane_gather(const RadArgs*, RadStream);
int ref_lane_store(const RadArgs*, RadStream);
int ref_randn_lanes(const RadArgs*, RadStream);
int ref_flow_euler(const RadArgs*, RadStream);
int ref_kd_input(const RadArgs*, RadStream);
int ref_kd_step(const RadArgs*, RadStream);
int ref_rms_mod(const RadArgs*, RadStream);
int ref_rms_mod_quant_fp8(const RadArgs*, RadStream);
int ref_flow_x0_step(const RadArgs*, RadStream);
@@ -219,11 +222,13 @@ int ref_cu_repeat(const RadArgs*, RadStream);
int ref_int_affine(const RadArgs*, RadStream);
int ref_chan_affine(const RadArgs*, RadStream);
int ref_conv2d_nhwc(const RadArgs*, RadStream);
int ref_im2col_nhwc(const RadArgs*, RadStream);
int ref_l2norm_chan(const RadArgs*, RadStream);
int ref_d2s_add(const RadArgs*, RadStream);
int ref_s2d_avg_add(const RadArgs*, RadStream);
int ref_to_rgba8(const RadArgs*, RadStream);
int ref_gelu_tanh(const RadArgs*, RadStream);
int ref_gelu_quick(const RadArgs*, RadStream);
int ref_scatter_add_rows(const RadArgs*, RadStream);
int ref_attn_prefix_self(const RadArgs*, RadStream);
int ref_qsa_block_key (const RadArgs*, RadStream);
@@ -379,6 +384,7 @@ REF_SHAPE_FN(ref_gemm_nt_q);
REF_SHAPE_FN(ref_gemm_nt_q_bias);
REF_SHAPE_FN(ref_lora_shrink);
REF_SHAPE_FN(ref_lora_expand);
REF_SHAPE_FN(ref_lora_expand_mm);
REF_SHAPE_FN(ref_embed_lookup);
REF_SHAPE_FN(ref_embed_lookup_q);
REF_SHAPE_FN(ref_logits_gemm);
@@ -402,6 +408,8 @@ REF_SHAPE_FN(ref_lane_gather);
REF_SHAPE_FN(ref_lane_store);
REF_SHAPE_FN(ref_randn_lanes);
REF_SHAPE_FN(ref_flow_euler);
REF_SHAPE_FN(ref_kd_input);
REF_SHAPE_FN(ref_kd_step);
REF_SHAPE_FN(ref_rms_mod);
REF_SHAPE_FN(ref_rms_mod_quant_fp8);
REF_SHAPE_FN(ref_flow_x0_step);
@@ -453,11 +461,13 @@ REF_SHAPE_FN(ref_cu_repeat);
REF_SHAPE_FN(ref_int_affine);
REF_SHAPE_FN(ref_chan_affine);
REF_SHAPE_FN(ref_conv2d_nhwc);
REF_SHAPE_FN(ref_im2col_nhwc);
REF_SHAPE_FN(ref_l2norm_chan);
REF_SHAPE_FN(ref_d2s_add);
REF_SHAPE_FN(ref_s2d_avg_add);
REF_SHAPE_FN(ref_to_rgba8);
REF_SHAPE_FN(ref_gelu_tanh);
REF_SHAPE_FN(ref_gelu_quick);
REF_SHAPE_FN(ref_scatter_add_rows);
REF_SHAPE_FN(ref_attn_prefix_self);
REF_SHAPE_FN(ref_qsa_block_key);
+44
View File
@@ -741,12 +741,25 @@ static const RadOperandSpec oFlowEuler[] = { OPD("v"), OPD("sigma"), OPD("sigma
OPD("pair"), OPD("lane"), OPD("flags"), OPD("seed"),
OPD("cu_seqlens"), OPD_O("inp_lat"), OPD_O("inp_mask"),
INOUT("state"), INOUT_O("preview") };
/* The k-diffusion sampler's input and step: `pred` "eps" or "v", the lanes f32. */
static const RadParamSpec pKdStep[] = { P_INT("M"), P_INT("n"), P_INT("stride"), P_STR("pred"),
P_STR("dtype") };
static const RadOperandSpec oKdInput[] = { OPD("sigma"), OPD("pair"), OPD("lane"), OPD("flags"),
OPD("seed"), OPD("cu_seqlens"), INOUT("state"),
OUT("y") };
static const RadOperandSpec oKdStep[] = { OPD("out"), OPD("sigma"), OPD("sigma_next"),
OPD("sigma_prev"), OPD("cfg"), OPD("pair"), OPD("lane"),
OPD("flags"), OPD("seed"), OPD("sampler"), OPD("step"),
OPD("cu_seqlens"), INOUT("state"), INOUT("hist"),
INOUT_O("preview") };
/* `round` 0 keeps the lists and the arithmetic in f32 (default 1, bf16 lists and roundings). */
static const RadParamSpec pChanAffine[] = { P_INT("M"), P_INT("n"), P_STR("mul"), P_STR("add"),
P_STR("mode"), P_STR("dtype"), P_INTO("round") };
static const RadParamSpec pConvNhwc[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
P_INT("stride"), P_STR("pad"), P_INT("up"), P_STR("dtype") };
static const RadOperandSpec oConvNhwc[] = { OPD("x"), WGT("w"), WGT("b"), OUT("y") };
static const RadParamSpec pIm2col[] = { P_INT("M"), P_INT("cin"), P_INT("k"), P_INT("stride"),
P_STR("pad"), P_INT("up"), P_STR("dtype") };
static const RadParamSpec pL2Norm[] = { P_INT("M"), P_INT("n"), P_STR("act"), P_STR("dtype") };
static const RadOperandSpec oL2Norm[] = { OPD("x"), WGT("gamma"), OUT("y") };
static const RadParamSpec pSpaceDepth[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("ft"),
@@ -1265,6 +1278,11 @@ static const RadOpSchema kSchemas[] = {
"slot[m] is in [0, S), f32 accumulate, j ascending, one rounding to y's dtype. A base row's `y` "
"is neither read nor written, so it stays bit for bit what the GEMM wrote (not y + 0: that "
"turns -0 into +0)." },
{ "lora_expand_mm", ARR(pLoraExpand), ARR(oLoraExpand),
"lora_expand with t rounded to bf16 and the sum over j in any order: y[m, n] = y[m, n] + "
"scale[s, n] * sum_j bf16(t[m, j]) * lora_b[s, n, j], f32 accumulate, one rounding to y's dtype. "
"A base row's `y` is neither read nor written. A row's bytes may depend on M: the generators' "
"expand, whose passes are many rows of one adapter." },
{ "attn_paged", ARR(pAttnPaged), ARR(oAttnPaged),
"Paged attention over kv_cache[num_blocks, kv_heads, block_size, 2*head_dim] (K then V per "
@@ -1550,6 +1568,15 @@ static const RadOpSchema kSchemas[] = {
"one Euler step of a flow-matching sampler per entry that moves a lane: guidance against the "
"paired entry, x' = bf16(x + bf16(bf16(sigma_next - sigma) * v)), a preview of the clean "
"latent with flag 4, the kept region of a repaint restored with flag 2 (docs/OPS.md)." },
{ "kd_input", ARR(pLane), ARR(oKdInput),
"a k-diffusion sampler's model input per entry that moves a lane: on a first step the lane "
"becomes its seed's normal stream times sqrt(1 + sigma^2); y = rd(x / sqrt(sigma^2 + 1)) at the "
"entry's rows and at its pair's (docs/OPS.md)." },
{ "kd_step", ARR(pKdStep), ARR(oKdStep),
"one k-diffusion sampler step over a discrete-time denoiser per entry that moves a lane: the "
"clean estimate D from an eps or v output, guided against the pair, then Euler, Euler ancestral "
"or DPM++ 2M by the entry's sampler, the lane f32; flag 4 previews D, `hist` keeps it "
"(docs/OPS.md)." },
{ "chan_affine", ARR(pChanAffine), ARR(oUnary),
"a per-channel affine from two parameter lists of n values (rounded to bf16): `fma` is "
"bf16(bf16(x * mul) + add), `inv` bf16(bf16(x - add) / mul). `round` 0 keeps the lists and "
@@ -1559,6 +1586,11 @@ static const RadOpSchema kSchemas[] = {
"the sizes read off them; PyTorch's [cout, cin, k, k] weight and a bias; `up` 2 reads the input "
"upsampled nearest-neighbour; `pad` same (k/2 a side) or down (0 above and left, 1 below and "
"right)." },
{ "im2col_nhwc", ARR(pIm2col), ARR(oUnary),
"conv2d_nhwc's receptive fields as rows: x [Hin, Win * cin], y [Hout * Wout, cin * k * k], row "
"oy * Wout + ox and column (ci * k + i) * k + j the value conv2d_nhwc multiplies weight[o, ci, "
"i, j] by (0 in the padding), so y @ w.reshape(cout, cin * k * k)^T + b is the convolution; "
"`up`, `pad` and `stride` as conv2d_nhwc reads them." },
{ "l2norm_chan", ARR(pL2Norm), ARR(oL2Norm),
"each row normalised to unit L2 (eps 1e-12), times sqrt(n), times gamma -- three bf16 roundings "
"-- then `act` none or silu: an image decoder's channel norm." },
@@ -1572,6 +1604,8 @@ static const RadOpSchema kSchemas[] = {
"decoded pixels in [-1, 1] to bytes as diffusers' postprocess makes them: "
"round(clamp(bf16(bf16(clamp(x) / 2) + 0.5), 0, 1) * 255), u8 out." },
{ "gelu_tanh", ARR(pMnD), ARR(oUnary), "the tanh approximation of gelu." },
{ "gelu_quick", ARR(pMnD), ARR(oUnary),
"CLIP's quick_gelu, three roundings: a = rd(1.702 x), s = rd(sigmoid(a)), y = rd(x s)." },
{ "scatter_add_rows", ARR(pMnD), ARR(oScatterRows),
"x[idx[i]] += v[i], rounded to x's dtype; a negative index adds nothing." },
{ "attn_prefix_self", ARR(pAttnPS), ARR(oAttnPS),
@@ -2072,6 +2106,9 @@ SIMPLE("ref_lora_shrink", "lora_shrink", "gemm", "t = x A[slot]^T per row, base
SIMPLE("ref_lora_expand", "lora_expand", "gemm",
"y += scale * (t B[slot]^T) per row, base rows untouched",
"f32 t and scale; bf16 lora_b; i32 slot; bf16 / f16 / f32 y", ref_lora_expand),
SIMPLE("ref_lora_expand_mm", "lora_expand_mm", "gemm",
"y += scale * (bf16(t) B[slot]^T) per row, base rows untouched",
"f32 t and scale; bf16 lora_b; i32 slot; bf16 / f16 / f32 y", ref_lora_expand_mm),
ROW("ref_attn_paged", "attn_paged", "attn",
"paged causal / sliding-window attention, any query length",
@@ -2209,10 +2246,16 @@ SIMPLE("ref_randn_lanes", "randn_lanes", "elem", "a lane's rows from its seed's
DT_FLOAT, ref_randn_lanes),
SIMPLE("ref_flow_euler", "flow_euler", "elem", "a flow-matching Euler step with guidance",
DT_FLOAT, ref_flow_euler),
SIMPLE("ref_kd_input", "kd_input", "elem", "a k-diffusion sampler's model input", DT_FLOAT,
ref_kd_input),
SIMPLE("ref_kd_step", "kd_step", "elem", "a k-diffusion sampler step with guidance", DT_FLOAT,
ref_kd_step),
SIMPLE("ref_chan_affine", "chan_affine", "elem", "a per-channel affine from parameter lists",
DT_FLOAT, ref_chan_affine),
SIMPLE("ref_conv2d_nhwc", "conv2d_nhwc", "elem", "an image convolution, channels innermost",
DT_FLOAT, ref_conv2d_nhwc),
SIMPLE("ref_im2col_nhwc", "im2col_nhwc", "elem", "a convolution's receptive fields as rows",
DT_FLOAT, ref_im2col_nhwc),
SIMPLE("ref_l2norm_chan", "l2norm_chan", "norm", "unit-L2 channel norm, scaled, optional silu",
DT_FLOAT, ref_l2norm_chan),
SIMPLE("ref_d2s_add", "d2s_add", "elem", "an upsampling shortcut added in", DT_FLOAT,
@@ -2221,6 +2264,7 @@ SIMPLE("ref_s2d_avg_add", "s2d_avg_add", "elem", "a downsampling shortcut added
ref_s2d_avg_add),
SIMPLE("ref_to_rgba8", "to_rgba8", "elem", "decoded pixels to bytes", DT_FLOAT, ref_to_rgba8),
SIMPLE("ref_gelu_tanh", "gelu_tanh", "elem", "tanh gelu", DT_FLOAT, ref_gelu_tanh),
SIMPLE("ref_gelu_quick", "gelu_quick", "elem", "quick gelu", DT_FLOAT, ref_gelu_quick),
SIMPLE("ref_scatter_add_rows", "scatter_add_rows", "elem", "x[idx[i]] += v[i]", DT_FLOAT,
ref_scatter_add_rows),
SIMPLE("ref_attn_prefix_self", "attn_prefix_self", "attn",
+80
View File
@@ -1254,6 +1254,10 @@ int ref_lora_expand_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* o
return o.finish("lora_expand", operand, out);
}
int ref_lora_expand_mm_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
return ref_lora_expand_shape(p, n_p, operand, out);
}
/* ================================================================== attention
*
* The paged layout (ref_ops.h):
@@ -2602,6 +2606,53 @@ int ref_flow_euler_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* ou
return o.finish("flow_euler", operand, out);
}
/* The k-diffusion sampler's two ops over flow_euler's entries: two equal-length entries where M
* allows, the lanes f32, sigmas drawn in (0, 1) so every logarithm and root is of a positive
* value. Flags draw only the first step (bit 1): the preview needs its optional operand and
* neither op repaints; the hand-written cases take the rest. */
int ref_kd_input_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0), st = gi(p, n_p, "stride", 0);
if (M <= 0 || n <= 0 || st <= 0) return RAD_E_UNSUPPORTED;
const int64_t S = (M % 2 == 0 && M / 2 <= st) ? 2 : 1;
if (M > S * st) return RAD_E_UNSUPPORTED;
const uint32_t F = (uint32_t)RAD_F32;
Opd o;
o.t_fill(F, { S }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma */
o.t_idx({ S }, S, -1, 0); /* pair */
o.t_idx({ S }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
o.t_idx({ S }, 2, -1, 0); /* flags */
o.t_idx({ 2 * S }, (int64_t)1 << 30, -1, 0); /* seed */
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
o.t(F, { REF_LANES * st, n }); /* state, INOUT */
o.t(act_dt(p, n_p), { M, n }); /* y */
return o.finish("kd_input", operand, out);
}
int ref_kd_step_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0), st = gi(p, n_p, "stride", 0);
if (M <= 0 || n <= 0 || st <= 0) return RAD_E_UNSUPPORTED;
const int64_t S = (M % 2 == 0 && M / 2 <= st) ? 2 : 1;
if (M > S * st) return RAD_E_UNSUPPORTED;
const uint32_t F = (uint32_t)RAD_F32;
Opd o;
o.t(act_dt(p, n_p), { M, n }); /* out */
o.t_fill(F, { S }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma */
o.t_fill(F, { S }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma_next */
o.t_fill(F, { S }, (uint32_t)RAD_FILL_SIGMOID, 0); /* sigma_prev */
o.t(F, { S }); /* cfg */
o.t_idx({ S }, S, -1, 0); /* pair */
o.t_idx({ S }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */
o.t_idx({ S }, 2, -1, 0); /* flags */
o.t_idx({ 2 * S }, (int64_t)1 << 30, -1, 0); /* seed */
o.t_idx({ S }, 3, -1, 0); /* sampler */
o.t_idx({ S }, 64, -1, 0); /* step */
o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */
o.t(F, { REF_LANES * st, n }); /* state, INOUT */
o.t(F, { REF_LANES * st, n }); /* hist, INOUT */
o.t(F, { REF_LANES * st, n }); /* preview, INOUT */
return o.finish("kd_step", operand, out);
}
/* ---- an image decoder's convolutions and shortcuts. Sizes are read off the operands and M counts
* output pixels, so this picks eight columns where M allows (a row with both edges and an interior)
* and the input that produces that output; otherwise it declines. */
@@ -2636,6 +2687,27 @@ int ref_conv2d_nhwc_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* o
return o.finish("conv2d_nhwc", operand, out);
}
/* conv2d_nhwc's geometry with the receptive fields as y's rows: M output pixels of cin k k values. */
int ref_im2col_nhwc_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), k = gi(p, n_p, "k", 0);
const int64_t stride = gi(p, n_p, "stride", 1), up = gi(p, n_p, "up", 1);
const char* pad = gs(p, n_p, "pad");
if (M <= 0 || cin <= 0 || k <= 0 || stride <= 0 || up <= 0) return RAD_E_UNSUPPORTED;
const bool down = pad && !std::strcmp(pad, "down");
const int64_t pads = down ? 1 : 2 * (k / 2);
const int64_t Wo = cols_for(M, 1), Ho = Wo ? M / Wo : 0;
if (!Wo) return RAD_E_UNSUPPORTED;
const int64_t Hv = (Ho - 1) * stride + k - pads, Wv = (Wo - 1) * stride + k - pads;
if (Hv <= 0 || Wv <= 0 || Hv % up || Wv % up) return RAD_E_UNSUPPORTED;
if ((Hv + pads - k) / stride + 1 != Ho || (Wv + pads - k) / stride + 1 != Wo)
return RAD_E_UNSUPPORTED;
const uint32_t D = act_dt(p, n_p);
Opd o;
o.t(D, { Hv / up, (Wv / up) * cin }); /* x */
o.t(D, { Ho * Wo, cin * k * k }); /* y */
return o.finish("im2col_nhwc", operand, out);
}
int ref_d2s_add_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0);
if (M <= 0 || cin <= 0 || cout <= 0) return RAD_E_UNSUPPORTED;
@@ -2745,6 +2817,14 @@ int ref_gelu_tanh_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out
o.t(D, { M, n });
return o.finish("gelu_tanh", operand, out);
}
int ref_gelu_quick_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const uint32_t D = act_dt(p, n_p);
Opd o;
o.t(D, { M, n });
o.t(D, { M, n });
return o.finish("gelu_quick", operand, out);
}
int ref_scatter_add_rows_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);