Archived
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:
1 parent
685ec5bd85
commit
4246a1dd04
124 files changed
+14326
-387
No files matched your search
+19
-2
@@ -314,8 +314,9 @@ std::vector<Case> build_cases() {
|
||||
add("lora_shrink", { HI("M", M), HI("K", K), HI("R", 48), HI("S", 4),
|
||||
HI("group", 128), HS("dtype", dt) }, 0, 2.0 * M * 48 * K);
|
||||
for (const char* dt : { "f32", "bf16", "f16" })
|
||||
add("lora_expand", { HI("M", M), HI("N", 645), HI("R", 48), HI("S", 4),
|
||||
HS("dtype", dt) }, 0, 2.0 * M * 645 * 48);
|
||||
for (const char* op : { "lora_expand", "lora_expand_mm" })
|
||||
add(op, { HI("M", M), HI("N", 645), HI("R", 48), HI("S", 4), HS("dtype", dt) }, 0,
|
||||
2.0 * M * 645 * 48);
|
||||
}
|
||||
for (const char* dt : { "f32", "bf16" }) {
|
||||
add("embed_lookup", { HI("M", 9), HI("n_embd", 128), HI("n_vocab", 64), HS("dtype", dt),
|
||||
@@ -728,6 +729,10 @@ std::vector<Case> build_cases() {
|
||||
add("randn_lanes", { HI("M", 24), HI("n", 64), HI("stride", 16), HI("n_seq", 2),
|
||||
HS("dtype", dt) });
|
||||
add("flow_euler", { HI("M", 24), HI("n", 64), HI("stride", 16), HS("dtype", dt) });
|
||||
add("kd_input", { HI("M", 24), HI("n", 4), HI("stride", 16), HS("dtype", dt) });
|
||||
for (const char* pred : { "eps", "v" })
|
||||
add("kd_step", { HI("M", 24), HI("n", 4), HI("stride", 16), HS("pred", pred),
|
||||
HS("dtype", dt) });
|
||||
for (const char* mode : { "fma", "inv" })
|
||||
for (int round : { 1, 0 })
|
||||
add("chan_affine", { HI("M", 9), HI("n", 67), HS("mul", kMul.c_str()),
|
||||
@@ -737,6 +742,7 @@ std::vector<Case> build_cases() {
|
||||
add("l2norm_chan", { HI("M", 9), HI("n", 645), HS("act", act), HS("dtype", dt) });
|
||||
add("to_rgba8", { HI("M", 9), HI("n", 4), HS("dtype", dt) });
|
||||
add("gelu_tanh", { HI("M", 6), HI("n", 645), HS("dtype", dt) });
|
||||
add("gelu_quick", { HI("M", 6), HI("n", 645), HS("dtype", dt) });
|
||||
add("scatter_add_rows", { HI("M", 6), HI("n", 645), HS("dtype", dt) });
|
||||
struct Conv { int M, cin, cout, k, stride, up; const char* pad; };
|
||||
static const Conv kConv[] = {
|
||||
@@ -751,6 +757,17 @@ std::vector<Case> build_cases() {
|
||||
HI("stride", v.stride), HS("pad", v.pad), HI("up", v.up),
|
||||
HS("dtype", dt) },
|
||||
0, 2.0 * v.M * v.cout * v.cin * v.k * v.k);
|
||||
/* The convolutions' geometries once each: cout is not a parameter of the unfold. */
|
||||
for (const Conv& v : kConv) {
|
||||
bool seen = false;
|
||||
for (const Conv* u = kConv; u != &v; ++u)
|
||||
seen = seen || (u->M == v.M && u->cin == v.cin && u->k == v.k &&
|
||||
u->stride == v.stride && u->up == v.up && u->pad == v.pad);
|
||||
if (seen) continue;
|
||||
add("im2col_nhwc", { HI("M", v.M), HI("cin", v.cin), HI("k", v.k),
|
||||
HI("stride", v.stride), HS("pad", v.pad), HI("up", v.up),
|
||||
HS("dtype", dt) });
|
||||
}
|
||||
for (int ft : { 1, 2 }) {
|
||||
add("d2s_add", { HI("M", 64), HI("cin", 24), HI("cout", 24), HI("ft", ft),
|
||||
HS("dtype", dt) });
|
||||
|
||||
@@ -1913,6 +1913,51 @@ void gen_cases(std::vector<Hand>& hs) {
|
||||
.pi("M", 17).pi("n", n).pi("stride", 6).ps("dtype", dn);
|
||||
hs.push_back(std::move(h));
|
||||
}
|
||||
{
|
||||
/* The k-diffusion input: entry 0 a first step guided against entry 1 (pair -2, which
|
||||
* only its positive entry writes), entry 2 a later step, unguided, in lane 3. */
|
||||
static const uint32_t kSeeds[6] = { 11u, 0u, 0u, 0u, 0xfeedbeefu, 3u };
|
||||
Hand h(("first, parked, later, n 4" + sfx).c_str(), "kd_input", 0.0);
|
||||
h.dense(RAD_F32, { 3 }, [](int64_t i) { return 14.6f - 6.0f * (float)i; })
|
||||
.idx({ 1, -2, -1 })
|
||||
.idx({ 0, 1, 3 })
|
||||
.idx({ 1, 0, 0 })
|
||||
.raw(RAD_U32, { 6 }, kSeeds)
|
||||
.idx({ 0, 5, 10, 13 })
|
||||
.dense(RAD_F32, { 24, 4 }, hfill(227), true)
|
||||
.dense(dt, { 13, 4 }, zero, true)
|
||||
.pi("M", 13).pi("n", 4).pi("stride", 6).ps("dtype", dn);
|
||||
hs.push_back(std::move(h));
|
||||
}
|
||||
for (const char* pred : { "eps", "v" }) {
|
||||
/* Every sampler: entry 0 Euler guided against entry 1 with a preview, entry 2 Euler
|
||||
* ancestral at step 7, entry 3 DPM++ 2M past its first step, entry 4 DPM++ 2M's first
|
||||
* step, entry 5 Euler ancestral to sigma 0, entry 6 DPM++ 2M to sigma 0. */
|
||||
static const uint32_t kSeeds[14] = { 3u, 0u, 4u, 0u, 5u, 9u, 6u, 1u, 7u, 0u, 8u, 0u,
|
||||
9u, 2u };
|
||||
Hand h(("seven entries, every sampler, " + std::string(pred) + sfx).c_str(), "kd_step",
|
||||
0.0);
|
||||
static const float kS[7] = { 6.0f, 6.0f, 3.5f, 1.25f, 9.0f, 0.4f, 0.3f };
|
||||
static const float kSn[7] = { 4.5f, 4.5f, 2.0f, 0.7f, 6.5f, 0.0f, 0.0f };
|
||||
static const float kSp[7] = { 0.0f, 0.0f, 5.0f, 2.0f, 0.0f, 0.9f, 0.6f };
|
||||
h.dense(dt, { 21, 5 }, hfill(228))
|
||||
.dense(RAD_F32, { 7 }, [](int64_t i) { return kS[i]; })
|
||||
.dense(RAD_F32, { 7 }, [](int64_t i) { return kSn[i]; })
|
||||
.dense(RAD_F32, { 7 }, [](int64_t i) { return kSp[i]; })
|
||||
.dense(RAD_F32, { 7 }, [](int64_t) { return 6.5f; })
|
||||
.idx({ 1, -2, -1, -1, -1, -1, -1 })
|
||||
.idx({ 0, 1, 2, 3, 4, 5, 6 })
|
||||
.idx({ 4, 0, 0, 0, 1, 0, 0 })
|
||||
.raw(RAD_U32, { 14 }, kSeeds)
|
||||
.idx({ 0, 0, 1, 2, 2, 1, 2 })
|
||||
.idx({ 0, 0, 7, 4, 0, 20, 21 })
|
||||
.idx({ 0, 3, 6, 9, 12, 15, 18, 21 })
|
||||
.dense(RAD_F32, { 28, 5 }, hfill(229, 3.0f), true)
|
||||
.dense(RAD_F32, { 28, 5 }, hfill(230), true)
|
||||
.dense(RAD_F32, { 28, 5 }, zero, true)
|
||||
.pi("M", 21).pi("n", 5).pi("stride", 4).ps("pred", pred).ps("dtype", dn);
|
||||
hs.push_back(std::move(h));
|
||||
}
|
||||
{
|
||||
Hand h(("a mask column into three lanes" + sfx).c_str(), "lane_store", 0.0);
|
||||
h.dense(dt, { 9, 1 }, hfill(225))
|
||||
|
||||
@@ -677,6 +677,203 @@ AVX_KERNEL(flow_euler) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ kd_input, kd_step */
|
||||
/* The k-diffusion sampler over a discrete-time denoiser (libref/ref_gen.cpp): libref's f32 steps
|
||||
* bit for bit, element by element, `kd_keep` holding every product to its own rounding before the
|
||||
* sum that follows it. The lanes are a few thousand rows of four channels, so a scalar loop costs
|
||||
* nothing next to the denoiser. */
|
||||
namespace {
|
||||
|
||||
enum { kSamplerEuler = 0, kSamplerEulerAncestral = 1, kSamplerDpmpp2m = 2 };
|
||||
|
||||
/* A product the compiler may not fuse into the next add (any ISA level: a scalar in an xmm). */
|
||||
inline float kd_keep(float x) { __asm__("" : "+x"(x)); return x; }
|
||||
|
||||
struct KdEntry {
|
||||
float s = 0, sn = 0, w = 0;
|
||||
float cskip = 0, cout = 0;
|
||||
float sup = 0, sdown = 0;
|
||||
float ratio = 0, em = 0;
|
||||
float c1 = 0, c2 = 0;
|
||||
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 = kd_keep(s * s) + 1.0f;
|
||||
k.cskip = 1.0f / s2p;
|
||||
k.cout = s / std::sqrt(s2p);
|
||||
if (sampler == kSamplerEulerAncestral) {
|
||||
const float s2 = kd_keep(s * s), sn2 = kd_keep(sn * sn);
|
||||
const float up2 = kd_keep(sn2 * (s2 - sn2)) / s2;
|
||||
float sup = std::sqrt(up2);
|
||||
if (!(sup < sn)) sup = sn;
|
||||
const float dn2 = sn2 - kd_keep(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 / kd_keep(2.0f * r);
|
||||
k.c1 = 1.0f + k.c2;
|
||||
k.second = true;
|
||||
}
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
inline float kd_denoised(const KdEntry& k, bool v_pred, float x, float o) {
|
||||
return v_pred ? kd_keep(x * k.cskip) - kd_keep(o * k.cout) : x - kd_keep(k.s * o);
|
||||
}
|
||||
|
||||
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 + kd_keep((k.sn - k.s) * d);
|
||||
}
|
||||
if (sampler == kSamplerEulerAncestral) {
|
||||
const float d = (x - D) / k.s;
|
||||
float xn = x + kd_keep(d * (k.sdown - k.s));
|
||||
if (k.sn > 0.0f) xn = xn + kd_keep(nz * k.sup);
|
||||
return xn;
|
||||
}
|
||||
if (k.sn == 0.0f) return D;
|
||||
const float Dd = k.second ? kd_keep(k.c1 * D) - kd_keep(k.c2 * hist) : D;
|
||||
return kd_keep(k.ratio * x) - kd_keep(k.em * Dd);
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* The model's input per entry that moves a lane: a first step's noise times sqrt(1 + sigma^2)
|
||||
* into the lane, then rd(x / sqrt(sigma^2 + 1)) at the entry's rows and its pair's. Every entry is
|
||||
* checked before any is written. */
|
||||
AVX_KERNEL(kd_input) {
|
||||
if (!rad_args_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 = ldi(pair, offlin(pair, e));
|
||||
if (pr == -2) continue;
|
||||
const int64_t L = ldi(lane, 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 (ldi(fl, offlin(fl, e)) & kInpaint) return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
for (int64_t e = 0; e < n_seq; ++e) {
|
||||
const int64_t pr = ldi(pair, offlin(pair, e));
|
||||
if (pr == -2) continue;
|
||||
const int64_t L = ldi(lane, 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 + kd_keep(s * s));
|
||||
const float cin = 1.0f / std::sqrt(kd_keep(s * s) + 1.0f);
|
||||
const bool first = (ldi(fl, offlin(fl, e)) & kFirst) != 0;
|
||||
const uint64_t sd = seed_of(seed, e);
|
||||
AVX_PARALLEL_FOR_IF(len * n * 8 >= AVX_PAR_MIN)
|
||||
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 per entry that moves a lane, libref's arithmetic (docs/OPS.md, kd_step). */
|
||||
AVX_KERNEL(kd_step) {
|
||||
if (!rad_args_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 = rad_arg_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 = ldi(pair, offlin(pair, e));
|
||||
if (pr == -2) continue;
|
||||
const int64_t L = ldi(lane, 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 = ldi(fl, offlin(fl, e)), sm = ldi(smp, 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 = ldi(pair, offlin(pair, e));
|
||||
if (pr == -2) continue;
|
||||
const int64_t L = ldi(lane, 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 = ldi(fl, offlin(fl, e)), sm = ldi(smp, offlin(smp, e));
|
||||
const uint64_t stream = (uint64_t)(uint32_t)(1 + ldi(stp, 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 = seed_of(seed, e);
|
||||
AVX_PARALLEL_FOR_IF(len * n * 8 >= AVX_PAR_MIN)
|
||||
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 + kd_keep(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;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ flow_x0_step */
|
||||
/* x' = f32(f32(r x) + f32(f32(1 - r) d)) with d = f32(x + f32((1 - t) v)) and
|
||||
* r = sigma_next / sigma: libref's f32 steps bit for bit, `keep` holding each product to its own
|
||||
@@ -768,6 +965,56 @@ AVX_KERNEL(chan_affine) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ im2col_nhwc */
|
||||
/* conv2d_nhwc's receptive fields as rows (libref's definition): y [Ho * Wo, cin * k * k], column
|
||||
* (ci k + i) k + j of row oy * Wo + ox the input at that tap, 0 in the padding. Each output row is
|
||||
* filled tap by tap from the input pixel's channel run; bf16 into bf16 copies the bits. Parallel
|
||||
* over output image rows. */
|
||||
AVX_KERNEL(im2col_nhwc) {
|
||||
if (!rad_args_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);
|
||||
const bool bits = X->dtype == RAD_BF16 && Y->dtype == RAD_BF16;
|
||||
AVX_PARALLEL_FOR_IF(Ho * Wo * kk >= AVX_PAR_MIN)
|
||||
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 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 int64_t xb = in ? (iy / up) * xr + (ix / up) * cin * xs : 0;
|
||||
const int64_t yt = yb + (i * k + j) * ys;
|
||||
for (int64_t ci = 0; ci < cin; ++ci) {
|
||||
const int64_t yo = yt + ci * k * k * ys;
|
||||
if (bits) {
|
||||
((uint16_t*)Y->data)[yo] =
|
||||
in ? ((const uint16_t*)X->data)[xb + ci * xs] : (uint16_t)0;
|
||||
} else {
|
||||
stt(Y, yo, in ? ldt(X, xb + ci * xs) : 0.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ conv2d_nhwc */
|
||||
/* An image convolution as GEMMs over patch rows. x [Hin, Win * cin] and y [Ho, Wo * cout] are
|
||||
* images whose sizes come from the operands, rows at their own pitch and channels contiguous, as
|
||||
@@ -1169,6 +1416,32 @@ AVX_KERNEL(gelu_tanh) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ gelu_quick */
|
||||
/* CLIP's quick_gelu in libref's three roundings: a = rd(1.702 x), s = rd(sigmoid(a)),
|
||||
* y = rd(x s), libm's exp, element by element: a text encoder's feed-forward, small beside its
|
||||
* GEMMs. */
|
||||
AVX_KERNEL(gelu_quick) {
|
||||
if (!rad_args_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;
|
||||
AVX_PARALLEL_FOR_IF(rows * n >= AVX_PAR_MIN)
|
||||
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 = narrow1(dt, 1.702f * v);
|
||||
const float s = narrow1(dt, 1.0f / (1.0f + std::exp(-t)));
|
||||
stt(Y, yb + c * ys, v * s);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ scatter_add_rows */
|
||||
/* x[idx[i]] = x[idx[i]] + v[i], rounded to x's dtype; a negative index adds nothing. Every index
|
||||
* is checked before the first write. Rows run in parallel only when no destination repeats: a
|
||||
|
||||
+3
-2
@@ -98,7 +98,7 @@ int avx_par_force(int threads);
|
||||
/* ---- gemm and the vocabulary edges */ \
|
||||
X(gemm_nt) X(gemm_nt_bias) X(gemm_nt_q) X(gemm_nt_q_bias) \
|
||||
X(gemm_nt_q_gated) X(embed_lookup) X(embed_lookup_q) X(logits_gemm) \
|
||||
X(lora_shrink) X(lora_expand) \
|
||||
X(lora_shrink) X(lora_expand) X(lora_expand_mm) \
|
||||
/* ---- attention */ \
|
||||
X(attn_paged) X(attn_dense) X(attn_cross) X(attn_na3d) X(table_add) X(mix_rows) X(chan_stats) X(kv_store) X(rope) \
|
||||
X(rope_table) X(qk_norm_rope) X(qsa_block_key) X(attn_local_rel) \
|
||||
@@ -117,7 +117,8 @@ int avx_par_force(int threads);
|
||||
X(gather_rows2) X(lane_gather) X(lane_store) X(randn_lanes) \
|
||||
X(flow_euler) X(chan_affine) X(conv2d_nhwc) X(l2norm_chan) \
|
||||
X(d2s_add) X(s2d_avg_add) X(to_rgba8) X(gelu_tanh) \
|
||||
X(scatter_add_rows) X(attn_prefix_self) \
|
||||
X(scatter_add_rows) X(attn_prefix_self) X(kd_input) X(kd_step) \
|
||||
X(gelu_quick) X(im2col_nhwc) \
|
||||
/* ---- video generation */ \
|
||||
X(rms_mod) X(rms_mod_quant_fp8) X(flow_x0_step) X(patch_unfold) \
|
||||
X(vtile_gather) X(vtile_stitch) X(grid_pos3) X(vsa_select) \
|
||||
|
||||
+13
-2
@@ -150,8 +150,8 @@ AVX_KERNEL(lora_shrink) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* operands: t, slot, lora_b, scale, y (inout) */
|
||||
AVX_KERNEL(lora_expand) {
|
||||
/* operands: t, slot, lora_b, scale, y (inout). `bf16_t`: lora_expand_mm, t rounded to bf16. */
|
||||
static int expand_impl(const RadArgs* a, bool bf16_t) {
|
||||
if (!rad_args_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);
|
||||
@@ -176,6 +176,12 @@ AVX_KERNEL(lora_expand) {
|
||||
const int64_t s = row_slot(SL, m, S);
|
||||
if (s < 0) continue; /* a base row: not read, not written */
|
||||
const float* tv = in_row(T, m * mt.rs, R, scratch_f32((size_t)R));
|
||||
if (bf16_t) {
|
||||
float* tr = scratch_f32((size_t)R);
|
||||
if (tr != tv) for (int64_t j = 0; j < R; ++j) tr[j] = tv[j];
|
||||
for (int64_t j = 0; j < R; ++j) tr[j] = rad_bf16_to_f32(rad_f32_to_bf16(tr[j]));
|
||||
tv = tr;
|
||||
}
|
||||
const float* bv = in_row(LB, s * pb.s0 + n * pb.s1, R, scratch_f32_b((size_t)R));
|
||||
const float d = scd[s * sc_r + n * sc_c] * dotf(tv, bv, R);
|
||||
const int64_t o = m * ys_r + n * ys_c;
|
||||
@@ -183,3 +189,8 @@ AVX_KERNEL(lora_expand) {
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
AVX_KERNEL(lora_expand) { return expand_impl(a, false); }
|
||||
|
||||
/* lora_expand with t rounded to bf16 (docs/OPS.md). */
|
||||
AVX_KERNEL(lora_expand_mm) { return expand_impl(a, true); }
|
||||
@@ -550,11 +550,24 @@ 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") };
|
||||
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"),
|
||||
@@ -937,6 +950,9 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "lora_expand", ARR(pLoraExpand), ARR(oLoraExpand),
|
||||
"y[m, n] += scale[slot[m], n] * sum_j t[m, j] * lora_b[slot[m], n, j]; a base row is neither "
|
||||
"read nor written." },
|
||||
{ "lora_expand_mm", ARR(pLoraExpand), ARR(oLoraExpand),
|
||||
"lora_expand with t rounded to bf16, the sum over j in any order; a base row is neither read nor "
|
||||
"written." },
|
||||
{ "gemm_nt_q_gated", ARR(pGemmQGated), ARR(oGemmQGated),
|
||||
"gemm_nt_q over a [N, K] gate_up plane with gated_quant_fp8's pass in the epilogue, byte for "
|
||||
"byte the pair. `N` is the weight's rows, twice the fused width; the output is [M, N/2]." },
|
||||
@@ -1044,12 +1060,22 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "flow_euler", ARR(pLane), ARR(oFlowEuler),
|
||||
"one flow-matching Euler step per entry that moves a lane, with guidance, the preview and the "
|
||||
"repaint's kept region; libref's roundings bit for bit." },
|
||||
{ "kd_input", ARR(pLane), ARR(oKdInput),
|
||||
"a k-diffusion sampler's model input per entry that moves a lane: a first step's noise times "
|
||||
"sqrt(1 + sigma^2) into the lane, then rd(x / sqrt(sigma^2 + 1)) at the entry's and its pair's "
|
||||
"rows; libref's arithmetic bit for bit." },
|
||||
{ "kd_step", ARR(pKdStep), ARR(oKdStep),
|
||||
"one k-diffusion sampler step per entry that moves a lane: the guided clean estimate from an "
|
||||
"eps or v output, then Euler, Euler ancestral or DPM++ 2M; libref's arithmetic bit for bit." },
|
||||
{ "chan_affine", ARR(pChanAffine), ARR(oUnary),
|
||||
"a per-channel affine from two lists of n bf16 values, `fma` or `inv`; `round` 0 keeps the "
|
||||
"lists and the arithmetic in f32, rounded once." },
|
||||
{ "conv2d_nhwc", ARR(pConvNhwc), ARR(oConvNhwc),
|
||||
"an image convolution, channels innermost, as GEMMs over bands of patch rows; `up` 2 reads the "
|
||||
"input upsampled nearest-neighbour, `pad` same or down." },
|
||||
{ "im2col_nhwc", ARR(pIm2col), ARR(oUnary),
|
||||
"conv2d_nhwc's receptive fields as rows, y [Hout * Wout, cin * k * k], column (ci * k + i) * k "
|
||||
"+ j the value conv2d_nhwc multiplies weight[o, ci, i, j] by; libref's copies bit for bit." },
|
||||
{ "l2norm_chan", ARR(pL2Norm), ARR(oL2Norm),
|
||||
"each pixel's channels to unit L2, times sqrt(n), times gamma (three roundings), then `act`." },
|
||||
{ "d2s_add", ARR(pSpaceDepth), ARR(oSpaceDepth),
|
||||
@@ -1060,6 +1086,8 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "to_rgba8", ARR(pMnD), ARR(oUnary),
|
||||
"decoded pixels in [-1, 1] to bytes as diffusers' postprocess makes them, 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),
|
||||
@@ -1473,6 +1501,9 @@ SIMPLE("avx_lora_shrink", "lora_shrink", "gemm", "t = x A[slot]^T per row, base
|
||||
SIMPLE("avx_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", L(lora_expand)),
|
||||
SIMPLE("avx_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", L(lora_expand_mm)),
|
||||
SIMPLE("avx_gemm_nt_q_gated", "gemm_nt_q_gated", "gemm",
|
||||
"gemm_nt_q over a gate_up plane with the gated E4M3 quantiser in the epilogue",
|
||||
"i8 / fp8 / float A; w4, w2, mxfp4, i8 or float B; fp8_e4m3 out, f32 scale",
|
||||
@@ -1586,11 +1617,17 @@ SIMPLE("avx_randn_lanes", "randn_lanes", "elem", "a lane's rows from its seed's
|
||||
DT_FLOAT, L(randn_lanes)),
|
||||
SIMPLE("avx_flow_euler", "flow_euler", "elem", "a flow-matching Euler step with guidance",
|
||||
DT_FLOAT, L(flow_euler)),
|
||||
SIMPLE("avx_kd_input", "kd_input", "elem", "a k-diffusion sampler's model input", DT_FLOAT,
|
||||
L(kd_input)),
|
||||
SIMPLE("avx_kd_step", "kd_step", "elem", "a k-diffusion sampler step with guidance", DT_FLOAT,
|
||||
L(kd_step)),
|
||||
SIMPLE("avx_chan_affine", "chan_affine", "elem", "a per-channel affine from parameter lists",
|
||||
DT_FLOAT, L(chan_affine)),
|
||||
SIMPLE("avx_conv2d_nhwc", "conv2d_nhwc", "conv",
|
||||
"an image convolution, channels innermost, as GEMMs over patch rows", DT_FLOAT,
|
||||
L(conv2d_nhwc)),
|
||||
SIMPLE("avx_im2col_nhwc", "im2col_nhwc", "elem", "a convolution's receptive fields as rows",
|
||||
DT_FLOAT, L(im2col_nhwc)),
|
||||
SIMPLE("avx_l2norm_chan", "l2norm_chan", "norm", "unit-L2 channel norm, scaled, optional silu",
|
||||
DT_FLOAT, L(l2norm_chan)),
|
||||
SIMPLE("avx_d2s_add", "d2s_add", "elem", "an upsampling shortcut added in", DT_FLOAT, L(d2s_add)),
|
||||
@@ -1598,6 +1635,7 @@ SIMPLE("avx_s2d_avg_add", "s2d_avg_add", "elem", "a downsampling shortcut added
|
||||
L(s2d_avg_add)),
|
||||
SIMPLE("avx_to_rgba8", "to_rgba8", "elem", "decoded pixels to bytes", DT_FLOAT, L(to_rgba8)),
|
||||
SIMPLE("avx_gelu_tanh", "gelu_tanh", "elem", "tanh gelu", DT_FLOAT, L(gelu_tanh)),
|
||||
SIMPLE("avx_gelu_quick", "gelu_quick", "elem", "quick gelu", DT_FLOAT, L(gelu_quick)),
|
||||
SIMPLE("avx_scatter_add_rows", "scatter_add_rows", "elem", "x[idx[i]] += v[i]", DT_FLOAT,
|
||||
L(scatter_add_rows)),
|
||||
SIMPLE("avx_attn_prefix_self", "attn_prefix_self", "attn",
|
||||
|
||||
Reference in new issue
Block a user