merge origin/main: the upstream LoRA-runtime refactor beneath the ref2va port

Merges upstream 1.10.0 (152 commits: the LoRA acquire/runtime refactor, the
form-1 LTX-2 scheduler family, guidance terms + negative prompts, decoder
selection, input_reference, new architectures) into the ref2va port branch.
Resolution rule throughout: keep BOTH feature sets; ABI numbering follows
upstream (RAD_ABI_VERSION 26, RAD_FORMAT_VER 2), with the port's video fields
unioned into RadVideoGenBatch/RadVideoGenDecl and abi_layout_table.h
regenerated from the live table.

Notable reconciliations:
  * videos.cpp: the task grammars are disjoint by capability -- keyframe tasks
    (i2va/l2va/flash-attn l2va/fl2va) are recognized only when decl.encodes,
    so upstream models keep refusing them as unknown tasks; input_reference,
    the LoRA tag/field pipeline, and the guidance/negative_prompt rendering
    coexist with refs/ref2va; the prompt put() helper hoisted to block scope
    for the media branch.
  * video_schedule(): upstream's (steps, video_tokens, refine) keeps its
    positional slots; the port's shift_video/shift_audio overrides move to the
    tail so upstream's schedule tests call unchanged.
  * r4d_selftest conv3d cases: the port's sp/st/causal/down/refl geometry and
    upstream's tpad/tpre fold into one reference (ft = causal ? 2 : tpad==2 ?
    2+tpre : 1); rms_mod rows keep the port's i8/fp8 quant modes alongside
    upstream's wadd/norm_once/norm parameters.
  * tests/oot_plugins.sh: the per-plugin cmake --install gains a --prefix
    staging dir -- plugin .so files still land in the absolute
    RAD_PLUGIN_INSTALL_HOME, but libr4d now also installs r4d_selftest to a
    relative bin/, which needs no root.

Host ctest: 112/113 (mm_test's webp gap pre-exists this merge); r4d_selftest,
engine_e2e, oot_plugins, abi_layout, all LoRA tests green.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
gearwave00001andClaude committed 2026-10-22 18:38:04 -05:00
commit a1a75da7e9
415 files changed
+78837 -5310

No files matched your search

+396 -16
View File
@@ -325,21 +325,27 @@ AVX_KERNEL(ln_mod_quant_fp8) {
/* Both forms: with `gated` the branch added through the gate row (no tanh) and the residual stored
* in x's dtype; then n1 = rd(x rsqrt(mean(x^2) + eps)), n2 = rd(n1 w), m = rd(n2 rd(1 + scale)),
* z = rd(m + shift), rd the norm chain's dtype (z's for rms_mod, bf16 for the quantised form,
* libref's rule). The sum of squares is the vector reduction, held to the tolerance table; every
* libref's rule); `norm` none takes n2 = x. The sum of squares is the vector reduction, held to the tolerance table; every
* rounding is a pass through the stored dtype, so the rest is libref's arithmetic. The per-entry
* rows (1 + scale rounded, the shift and the gate) and the gain are built once a launch. */
static int rms_mod_run(const RadArgs* a, bool quant) {
if (a->n_t < (quant ? 9 : 7)) return RAD_E_INVAL;
const RadTensor *Yv = rad_arg_in(a, 0), *G = rad_arg_in(a, 1), *S = &a->t[2], *W = &a->t[3];
const RadTensor *Yv = rad_arg_in(a, 0), *G = rad_arg_in(a, 1), *S = &a->t[2];
const RadTensor* W = rad_arg_in(a, 3);
const RadTensor *cu = &a->t[4], *X = &a->t[5];
const RadTensor *Q = quant ? &a->t[6] : nullptr, *SC = quant ? &a->t[7] : nullptr;
const RadTensor* Z = quant ? rad_arg_in(a, 8) : &a->t[6];
const int64_t n = p_int(a, "n", 0), grp = quant ? p_int(a, "group", 0) : 1;
const bool gated = p_int(a, "gated", 0) != 0;
const float eps = p_f32(a, "eps", 1e-6f);
const float wadd = p_f32(a, "wadd", 0.0f);
const bool once = p_int(a, "norm_once", 0) != 0;
const char* nk = p_str(a, "norm", "rms");
const bool layer = std::strcmp(nk, "layer") == 0, none = std::strcmp(nk, "none") == 0;
if ((!layer && !none && std::strcmp(nk, "rms") != 0) || (none && W)) return RAD_E_INVAL;
if (n <= 0 || grp <= 0 || n % grp || (gated && (!Yv || !G))) return RAD_E_SHAPE;
const int64_t rows = rows_of(quant ? Q : Z, n), n_seq = numel(cu) - 1, ngrp = n / grp;
if (rows <= 0 || n_seq <= 0 || rows_of(X, n) < rows || numel(W) < n ||
if (rows <= 0 || n_seq <= 0 || rows_of(X, n) < rows || (W && numel(W) < n) ||
numel(S) < n_seq * 2 * n)
return RAD_E_SHAPE;
if (gated && (rows_of(Yv, n) < rows || numel(G) < n_seq * n)) return RAD_E_SHAPE;
@@ -359,7 +365,8 @@ static int rms_mod_run(const RadArgs* a, bool quant) {
row[n + i] = ldt(S, e * s_rs + i * s_cs);
if (gated) row[2 * n + i] = ldt(G, e * g_rs + i * g_cs);
}
for (int64_t i = 0; i < n; ++i) tabs[n_seq * tw + i] = ldt(W, i * laststride(W));
for (int64_t i = 0; i < n; ++i)
tabs[n_seq * tw + i] = W ? ldt(W, i * laststride(W)) + wadd : 1.0f;
}
const float* wrow = tabs + n_seq * tw;
@@ -380,13 +387,23 @@ static int rms_mod_run(const RadArgs* a, bool quant) {
} else {
std::memcpy(v, xv, (size_t)n * sizeof(float));
}
const float rs = 1.0f / std::sqrt(row_sumsq(v, n) / (float)n + eps);
if (layer) {
/* Centred first, the mean an f32 sum in ascending order (libref's). */
float mu = 0.0f;
for (int64_t k = 0; k < n; ++k) mu += v[k];
mu /= (float)n;
for (int64_t k = 0; k < n; ++k) v[k] -= mu;
}
/* `none`: no norm, the residual as it is (libref's n2 = x). */
const float rs = none ? 1.0f : 1.0f / std::sqrt(row_sumsq(v, n) / (float)n + eps);
const vf vr = vf_set1(rs);
int64_t i = 0;
for (; i + VF_N <= n; i += VF_N) vf_storeu(o + i, vf_mul(vf_loadu(v + i), vr));
for (; i < n; ++i) o[i] = v[i] * rs;
narrow_row(dt, o, n, tmp);
mul_rows(o, wrow, n);
if (W) {
if (!once) narrow_row(dt, o, n, tmp);
mul_rows(o, wrow, n);
}
narrow_row(dt, o, n, tmp);
mul_rows(o, ent, n);
narrow_row(dt, o, n, tmp);
@@ -452,7 +469,9 @@ AVX_KERNEL(timestep_embed) {
for (int64_t i = 0; i < h; ++i) f[i] = sc_exp(coef * (float)i / (float)h);
for (int64_t m = 0; m < rows; ++m) {
const float tv = std::fmax(ldt(T, offlin(T, m)), clamp); /* ref pins are floors */
const float tt = round ? scale * narrow1(RAD_BF16, narrow1(RAD_BF16, tv * scale) / scale)
const float tt = round == 3 ? narrow1(RAD_BF16, scale * narrow1(RAD_BF16, tv))
: round == 2 ? scale * narrow1(RAD_BF16, tv)
: round ? scale * narrow1(RAD_BF16, narrow1(RAD_BF16, tv * scale) / scale)
: scale * tv;
for (int64_t i = 0; i < h; ++i) {
const float arg = tt * f[i];
@@ -581,14 +600,15 @@ AVX_KERNEL(randn_lanes) {
* x' = bf16(x + bf16(bf16(sigma_next - sigma) * v'))
*
* flag 4 writes x - sigma v' to the lane's preview, and flag 2 restores the kept region,
* x' = bf16(m x' + (1 - m)((1 - sigma_next) src + sigma_next noise)). libref requires every
* operand, the optional three included. Every entry is checked before any lane is written. */
* x' = bf16(m x' + (1 - m)((1 - sigma_next) src + sigma_next noise)). inp, mask and preview are
* optional, as in libref: an entry whose flags need one that is absent is refused. Every entry is
* checked before any lane is written. */
AVX_KERNEL(flow_euler) {
if (!rad_args_have(a, 13)) return RAD_E_INVAL;
if (!rad_args_have(a, 9) || !rad_arg_in(a, 11)) return RAD_E_INVAL;
const RadTensor *V = &a->t[0], *sg = &a->t[1], *sgn = &a->t[2], *cfg = &a->t[3];
const RadTensor *pair = &a->t[4], *lane = &a->t[5], *fl = &a->t[6], *seed = &a->t[7];
const RadTensor *cu = &a->t[8], *inp = &a->t[9], *mask = &a->t[10], *st = &a->t[11];
const RadTensor* pv = &a->t[12];
const RadTensor *cu = &a->t[8], *inp = rad_arg_in(a, 9), *mask = rad_arg_in(a, 10);
const RadTensor *st = &a->t[11], *pv = rad_arg_in(a, 12);
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), v_rows = rows_of(V, n);
@@ -604,6 +624,8 @@ AVX_KERNEL(flow_euler) {
if (len > 0 && (b0 < 0 || b0 + len > v_rows)) return RAD_E_SHAPE;
if (pr >= 0 && len > 0 && (cu_at(cu, pr) < 0 || cu_at(cu, pr) + len > v_rows))
return RAD_E_SHAPE;
const int64_t f = ldi(fl, offlin(fl, e));
if (((f & kInpaint) && (!inp || !mask)) || ((f & kPreview) && !pv)) return RAD_E_INVAL;
}
const uint32_t dt = st->dtype;
for (int64_t e = 0; e < n_seq; ++e) {
@@ -683,6 +705,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
@@ -825,6 +1044,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
@@ -855,8 +1124,11 @@ AVX_KERNEL(conv2d_nhwc) {
const int64_t Ho = Y->shape[0], Wo = Y->shape[1] / cout;
if (Hin <= 0 || Win <= 0 || Ho <= 0 || Wo <= 0) return RAD_E_SHAPE;
const int64_t Hv = Hin * up, Wv = Win * up;
const int64_t pt = down ? 0 : k / 2, pb = down ? 1 : k / 2;
if ((Hv + pt + pb - k) / stride + 1 != Ho || (Wv + pt + pb - k) / stride + 1 != Wo)
/* `causal` pads k - 1 rows above and none below (libref's). */
const bool causal = pad && !std::strcmp(pad, "causal");
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;
if ((Hv + pt + pb - k) / stride + 1 != Ho || (Wv + pl + pr - k) / stride + 1 != Wo)
return RAD_E_SHAPE;
if (numel(W) < cout * cin * k * k || numel(B) < cout) return RAD_E_SHAPE;
const int64_t xr = X->stride[0], yr = Y->stride[0];
@@ -934,7 +1206,7 @@ AVX_KERNEL(conv2d_nhwc) {
for (int64_t kh = 0; kh < k; ++kh) {
const int64_t iy = oy * stride + kh - pt;
for (int64_t kw = 0; kw < k; ++kw) {
const int64_t ix = ox * stride + kw - pt;
const int64_t ix = ox * stride + kw - pl;
char* d = dst + (size_t)((kh * k + kw) * cin) * ael;
if (iy < 0 || iy >= Hv || ix < 0 || ix >= Wv) {
std::memset(d, 0, (size_t)cin * ael);
@@ -1223,6 +1495,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
@@ -1265,3 +1563,85 @@ AVX_KERNEL(scatter_add_rows) {
for (int64_t m = 0; m < M; ++m) add_row(m);
return RAD_OK;
}
/* ------------------------------------------------------------------ table_add */
/* libref's table_add: y[r, l n + c] = x[r, c] + t[l, c], f32 sum rounded once to y's dtype. */
AVX_KERNEL(table_add) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *T = &a->t[1], *Y = &a->t[2];
const int64_t n = p_int(a, "n", 0), L = p_int(a, "L", 0);
if (n <= 0 || L <= 0 || numel(T) < L * n) return RAD_E_SHAPE;
const int64_t rows = rows_of(Y, L * n);
if (rows <= 0 || rows_of(X, n) < rows) return RAD_E_SHAPE;
AVX_PARALLEL_FOR_IF(rows * L * n >= AVX_PAR_MIN)
for (int64_t r = 0; r < rows; ++r) {
float* buf = scratch_f32((size_t)(3 * n + 64));
float *xs = buf, *ts = buf + n + 16, *p = ts + n + 16;
const float* xv = in_row(X, rowoff(X, r, n), n, xs);
for (int64_t l = 0; l < L; ++l) {
const float* tv = in_row(T, l * n * laststride(T), n, ts);
for (int64_t c = 0; c < n; ++c) p[c] = xv[c] + tv[c];
out_row(Y, rowoff(Y, r, L * n) + l * n * laststride(Y), n, p);
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ mix_rows */
/* libref's mix_rows: y[r, c] = sum_i w[i] x[r k + i, c], f32 in ascending i, rounded once. */
AVX_KERNEL(mix_rows) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *W = &a->t[1], *Y = &a->t[2];
const int64_t n = p_int(a, "n", 0), k = p_int(a, "k", 0);
if (n <= 0 || k <= 0 || numel(W) < k) return RAD_E_SHAPE;
const int64_t rows = rows_of(Y, n);
if (rows <= 0 || rows_of(X, n) < rows * k) return RAD_E_SHAPE;
std::vector<float> w((size_t)k);
for (int64_t i = 0; i < k; ++i) w[(size_t)i] = ldt(W, i * laststride(W));
AVX_PARALLEL_FOR_IF(rows * n * k >= AVX_PAR_MIN)
for (int64_t r = 0; r < rows; ++r) {
float* buf = scratch_f32((size_t)(2 * n + 64));
float *xs = buf, *acc = buf + n + 16;
for (int64_t c = 0; c < n; ++c) acc[c] = 0.0f;
for (int64_t i = 0; i < k; ++i) {
const float* xv = in_row(X, rowoff(X, r * k + i, n), n, xs);
for (int64_t c = 0; c < n; ++c) acc[c] += w[(size_t)i] * xv[c];
}
out_row(Y, rowoff(Y, r, n), n, acc);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ chan_stats */
/* libref's chan_stats: per-channel statistics held as weights, every step rounded as libref. */
AVX_KERNEL(chan_stats) {
if (!rad_args_have(a, 4)) return RAD_E_INVAL;
const RadTensor *X = &a->t[0], *Mn = &a->t[1], *Sd = &a->t[2], *Y = &a->t[3];
const int64_t n = p_int(a, "n", 0);
const bool var = p_int(a, "var", 0) != 0;
const float eps = p_f32(a, "eps", 0.0f);
const char* mode = p_str(a, "mode", "denorm");
const bool norm = std::strcmp(mode, "norm") == 0;
if (!norm && std::strcmp(mode, "denorm") != 0) return RAD_E_INVAL;
if (n <= 0 || numel(Mn) < n || numel(Sd) < n) 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;
std::vector<float> mu((size_t)n), sd((size_t)n);
for (int64_t c = 0; c < n; ++c) {
mu[(size_t)c] = narrow1(dt, ldt(Mn, c * laststride(Mn)));
const float v = ldt(Sd, c * laststride(Sd));
sd[(size_t)c] = narrow1(dt, var ? std::sqrt(v + eps) : v);
}
AVX_PARALLEL_FOR_IF(rows * n >= AVX_PAR_MIN)
for (int64_t r = 0; r < rows; ++r) {
float* buf = scratch_f32((size_t)(2 * n + 32));
float *xs = buf, *o = buf + n + 16;
const float* xv = in_row(X, rowoff(X, r, n), n, xs);
for (int64_t c = 0; c < n; ++c)
o[c] = norm ? narrow1(dt, narrow1(dt, xv[c] - mu[(size_t)c]) / sd[(size_t)c])
: narrow1(dt, narrow1(dt, xv[c] * sd[(size_t)c]) + mu[(size_t)c]);
out_row(Y, rowoff(Y, r, n), n, o);
}
return RAD_OK;
}