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
@@ -54,6 +54,7 @@ set(AVX_KERNEL_SRC
|
||||
avx_gen.cpp
|
||||
avx_gen_attn.cpp
|
||||
avx_video.cpp
|
||||
avx_ltx.cpp
|
||||
avx_vsa.cpp
|
||||
avx_audio.cpp
|
||||
)
|
||||
|
||||
@@ -1646,3 +1646,181 @@ AVX_KERNEL(qsa_block_key) {
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ attn_cross */
|
||||
/* attn_dense with each entry's keys a run of their own (`cu_k`), libref's attn_cross: causal
|
||||
* counts from each run's start, and a query that sees no key is zero. The same tiled online
|
||||
* softmax as attn_dense's portable path. */
|
||||
AVX_KERNEL(attn_cross) {
|
||||
if (!rad_args_have(a, 6)) return RAD_E_INVAL;
|
||||
const RadTensor *q = &a->t[0], *k = &a->t[1], *v = &a->t[2], *cu = &a->t[3], *cuk = &a->t[4];
|
||||
const RadTensor *o = &a->t[5];
|
||||
const int64_t head_dim = p_int(a, "head_dim", q->rank >= 3 ? q->shape[q->rank - 1] : 0);
|
||||
const int64_t gqa = p_int(a, "gqa", 1);
|
||||
const int causal = (int)p_int(a, "causal", 0);
|
||||
if (head_dim <= 0 || gqa <= 0) return RAD_E_SHAPE;
|
||||
if (head_dim > AVX_HD_MAX) return RAD_E_UNSUPPORTED;
|
||||
const int64_t q_heads = q->rank >= 3 ? q->shape[q->rank - 2]
|
||||
: (q->rank == 2 ? q->shape[1] / head_dim : 1);
|
||||
const int64_t kv_heads = q_heads / gqa;
|
||||
if (kv_heads <= 0 || q_heads % gqa != 0) return RAD_E_SHAPE;
|
||||
const int64_t n_seq = numel(cu) - 1;
|
||||
if (n_seq <= 0 || numel(cuk) < n_seq + 1) return RAD_E_SHAPE;
|
||||
const int64_t total_q = numel(q) / (q_heads * head_dim);
|
||||
const int64_t total_k = numel(k) / (kv_heads * head_dim);
|
||||
if (total_q <= 0 || ldi(cu, offlin(cu, n_seq)) <= ldi(cu, offlin(cu, 0))) return RAD_E_SHAPE; /* zero rows */
|
||||
if (numel(o) < total_q * q_heads * head_dim) return RAD_E_SHAPE;
|
||||
for (int64_t s = 0; s < n_seq; ++s)
|
||||
if (ldi(cu, offlin(cu, s + 1)) < ldi(cu, offlin(cu, s)) ||
|
||||
ldi(cuk, offlin(cuk, s + 1)) < ldi(cuk, offlin(cuk, s)) ||
|
||||
ldi(cu, offlin(cu, s + 1)) > total_q || ldi(cuk, offlin(cuk, s + 1)) > total_k)
|
||||
return RAD_E_SHAPE;
|
||||
|
||||
const int64_t q_ds = q->rank >= 2 ? q->stride[q->rank - 1] : 1;
|
||||
const int64_t q_hs = q->rank >= 3 ? q->stride[q->rank - 2] : head_dim * q_ds;
|
||||
const int64_t q_rs = row_stride(q, q_heads * head_dim);
|
||||
const int64_t k_ds = k->rank >= 2 ? k->stride[k->rank - 1] : 1;
|
||||
const int64_t k_hs = k->rank >= 3 ? k->stride[k->rank - 2] : head_dim * k_ds;
|
||||
const int64_t k_rs = row_stride(k, kv_heads * head_dim);
|
||||
const int64_t v_ds = v->rank >= 2 ? v->stride[v->rank - 1] : 1;
|
||||
const int64_t v_hs = v->rank >= 3 ? v->stride[v->rank - 2] : head_dim * v_ds;
|
||||
const int64_t v_rs = row_stride(v, kv_heads * head_dim);
|
||||
const int64_t o_ds = o->rank >= 2 ? o->stride[o->rank - 1] : 1;
|
||||
const int64_t o_hs = o->rank >= 3 ? o->stride[o->rank - 2] : head_dim * o_ds;
|
||||
const int64_t o_rs = row_stride(o, q_heads * head_dim);
|
||||
const float scale = p_f32(a, "scale", 1.0f / std::sqrt((float)head_dim));
|
||||
|
||||
AVX_PARALLEL_FOR_IF(n_seq * q_heads >= 2)
|
||||
for (int64_t sh = 0; sh < n_seq * q_heads; ++sh) {
|
||||
const int64_t s = sh / q_heads, h = sh % q_heads, kh = h / gqa;
|
||||
const int64_t b0 = ldi(cu, offlin(cu, s)), b1 = ldi(cu, offlin(cu, s + 1));
|
||||
const int64_t k0 = ldi(cuk, offlin(cuk, s)), k1 = ldi(cuk, offlin(cuk, s + 1));
|
||||
float* frame = (float*)scratch_f32((size_t)(4 * head_dim + AVX_KV_TILE) + 16);
|
||||
float* qbuf = frame;
|
||||
float* kbuf = frame + head_dim;
|
||||
float* vbuf = frame + 2 * head_dim;
|
||||
float* acc = frame + 3 * head_dim;
|
||||
float* sbuf = frame + 4 * head_dim;
|
||||
int64_t vbase[AVX_KV_TILE];
|
||||
for (int64_t i = b0; i < b1; ++i) {
|
||||
const int64_t qb = i * q_rs + h * q_hs, ob = i * o_rs + h * o_hs;
|
||||
const int64_t jend = causal ? std::min<int64_t>(k1, k0 + (i - b0) + 1) : k1;
|
||||
const float* qv = head_f32(q, qb, q_ds, head_dim, 1.0f, qbuf);
|
||||
for (int64_t d = 0; d < head_dim; ++d) acc[d] = 0.0f;
|
||||
float m = -INFINITY, l = 0.0f;
|
||||
int ntile = 0;
|
||||
auto flush = [&]() {
|
||||
if (ntile == 0) return;
|
||||
float tmax = sbuf[0];
|
||||
for (int t = 1; t < ntile; ++t) if (sbuf[t] > tmax) tmax = sbuf[t];
|
||||
if (tmax > m) {
|
||||
const float r = (m == -INFINITY) ? 0.0f : sc_exp(m - tmax);
|
||||
if (r != 1.0f) {
|
||||
for (int64_t d = 0; d < head_dim; ++d) acc[d] *= r;
|
||||
l *= r;
|
||||
}
|
||||
m = tmax;
|
||||
}
|
||||
for (int t = 0; t < ntile; ++t) sbuf[t] = sc_exp(sbuf[t] - m);
|
||||
for (int t = 0; t < ntile; ++t) {
|
||||
const float w = sbuf[t];
|
||||
l += w;
|
||||
axpy(acc, head_f32(v, vbase[t], v_ds, head_dim, 1.0f, vbuf), w, head_dim);
|
||||
}
|
||||
ntile = 0;
|
||||
};
|
||||
for (int64_t j = k0; j < jend; ++j) {
|
||||
const float* kvr = head_f32(k, j * k_rs + kh * k_hs, k_ds, head_dim, 1.0f, kbuf);
|
||||
sbuf[ntile] = dotf(qv, kvr, head_dim) * scale;
|
||||
vbase[ntile] = j * v_rs + kh * v_hs;
|
||||
if (++ntile == AVX_KV_TILE) flush();
|
||||
}
|
||||
flush();
|
||||
if (m == -INFINITY) {
|
||||
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, 0.0f);
|
||||
continue;
|
||||
}
|
||||
const float inv = l > 0.0f ? 1.0f / l : 0.0f;
|
||||
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, acc[d] * inv);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* attn_na3d: libref's neighborhood attention (libref/ref_attn.cpp), the window's keys through
|
||||
* attn_cross's online softmax. */
|
||||
AVX_KERNEL(attn_na3d) {
|
||||
if (!rad_args_have(a, 5)) return RAD_E_INVAL;
|
||||
const RadTensor *q = &a->t[0], *k = &a->t[1], *v = &a->t[2], *G = &a->t[3], *o = &a->t[4];
|
||||
const int64_t head_dim = p_int(a, "head_dim", 0);
|
||||
const int64_t kt = p_int(a, "kt", 0), kh = p_int(a, "kh", 0), kw = p_int(a, "kw", 0);
|
||||
if (head_dim <= 0 || kt <= 0 || kh <= 0 || kw <= 0 || numel(G) < 3 || q->rank != 2)
|
||||
return RAD_E_SHAPE;
|
||||
if (head_dim > AVX_HD_MAX) return RAD_E_UNSUPPORTED;
|
||||
const int64_t heads = q->shape[1] / head_dim;
|
||||
const int64_t T = ldi(G, offlin(G, 0)), H = ldi(G, offlin(G, 1)), W = ldi(G, offlin(G, 2));
|
||||
if (heads <= 0 || q->shape[1] % head_dim || T <= 0 || H <= 0 || W <= 0) return RAD_E_SHAPE;
|
||||
const int64_t N = std::min(q->shape[0], T * H * W);
|
||||
if (N <= 0 || numel(k) < N * heads * head_dim || numel(v) < N * heads * head_dim ||
|
||||
numel(o) < N * heads * head_dim)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t nt = std::min(kt, T), nh = std::min(kh, H), nw = std::min(kw, W);
|
||||
const int64_t q_ds = q->stride[1], q_rs = row_stride(q, heads * head_dim);
|
||||
const int64_t k_ds = k->rank >= 2 ? k->stride[k->rank - 1] : 1, k_rs = row_stride(k, heads * head_dim);
|
||||
const int64_t v_ds = v->rank >= 2 ? v->stride[v->rank - 1] : 1, v_rs = row_stride(v, heads * head_dim);
|
||||
const int64_t o_ds = o->rank >= 2 ? o->stride[o->rank - 1] : 1, o_rs = row_stride(o, heads * head_dim);
|
||||
const float scale = p_f32(a, "scale", 1.0f / std::sqrt((float)head_dim));
|
||||
auto start = [](int64_t c, int64_t n, int64_t L) {
|
||||
return std::min(std::max<int64_t>(c - n / 2, 0), L - n);
|
||||
};
|
||||
AVX_PARALLEL_FOR_IF(N * heads >= 2)
|
||||
for (int64_t rh = 0; rh < N * heads; ++rh) {
|
||||
const int64_t r = rh / heads, h = rh % heads;
|
||||
const int64_t t = r / (H * W), i = (r / W) % H, j = r % W;
|
||||
const int64_t t0 = start(t, nt, T), i0 = start(i, nh, H), j0 = start(j, nw, W);
|
||||
float* frame = (float*)scratch_f32((size_t)(4 * head_dim + AVX_KV_TILE) + 16);
|
||||
float* qbuf = frame;
|
||||
float* kbuf = frame + head_dim;
|
||||
float* vbuf = frame + 2 * head_dim;
|
||||
float* acc = frame + 3 * head_dim;
|
||||
float* sbuf = frame + 4 * head_dim;
|
||||
int64_t vbase[AVX_KV_TILE];
|
||||
const int64_t ob = r * o_rs + h * head_dim * o_ds;
|
||||
const float* qv = head_f32(q, r * q_rs + h * head_dim * q_ds, q_ds, head_dim, 1.0f, qbuf);
|
||||
for (int64_t d = 0; d < head_dim; ++d) acc[d] = 0.0f;
|
||||
float m = -INFINITY, l = 0.0f;
|
||||
int ntile = 0;
|
||||
auto flush = [&]() {
|
||||
if (ntile == 0) return;
|
||||
float tmax = sbuf[0];
|
||||
for (int u = 1; u < ntile; ++u) if (sbuf[u] > tmax) tmax = sbuf[u];
|
||||
if (tmax > m) {
|
||||
const float rr = (m == -INFINITY) ? 0.0f : sc_exp(m - tmax);
|
||||
if (rr != 1.0f) {
|
||||
for (int64_t d = 0; d < head_dim; ++d) acc[d] *= rr;
|
||||
l *= rr;
|
||||
}
|
||||
m = tmax;
|
||||
}
|
||||
for (int u = 0; u < ntile; ++u) sbuf[u] = sc_exp(sbuf[u] - m);
|
||||
for (int u = 0; u < ntile; ++u) {
|
||||
l += sbuf[u];
|
||||
axpy(acc, head_f32(v, vbase[u], v_ds, head_dim, 1.0f, vbuf), sbuf[u], head_dim);
|
||||
}
|
||||
ntile = 0;
|
||||
};
|
||||
for (int64_t a1 = t0; a1 < t0 + nt; ++a1)
|
||||
for (int64_t b1 = i0; b1 < i0 + nh; ++b1)
|
||||
for (int64_t c1 = j0; c1 < j0 + nw; ++c1) {
|
||||
const int64_t key = (a1 * H + b1) * W + c1;
|
||||
const float* kr = head_f32(k, key * k_rs + h * head_dim * k_ds, k_ds, head_dim, 1.0f, kbuf);
|
||||
sbuf[ntile] = dotf(qv, kr, head_dim) * scale;
|
||||
vbase[ntile] = key * v_rs + h * head_dim * v_ds;
|
||||
if (++ntile == AVX_KV_TILE) flush();
|
||||
}
|
||||
flush();
|
||||
const float inv = l > 0.0f ? 1.0f / l : 0.0f;
|
||||
for (int64_t d = 0; d < head_dim; ++d) stt(o, ob + d * o_ds, acc[d] * inv);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
@@ -1094,6 +1094,37 @@ AVX_KERNEL(iota) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ int_affine */
|
||||
/* y[j] = (src[j] < 0 ? 0 : x[src[j]]) * mul[j] + add[j] over three integer lists. */
|
||||
static int avx_parse_ints(const char* s, int64_t* v, int cap) {
|
||||
int n = 0;
|
||||
while (s && *s) {
|
||||
char* e = nullptr;
|
||||
const long long x = std::strtoll(s, &e, 10);
|
||||
if (e == s) break;
|
||||
if (n == cap) return -1;
|
||||
v[n++] = x;
|
||||
s = e;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
AVX_KERNEL(int_affine) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *Y = &a->t[1];
|
||||
int64_t src[16], mul[16], add[16];
|
||||
const int n = avx_parse_ints(p_str(a, "src", ""), src, 16);
|
||||
if (n <= 0 || avx_parse_ints(p_str(a, "mul", ""), mul, 16) != n ||
|
||||
avx_parse_ints(p_str(a, "add", ""), add, 16) != n || numel(Y) < n)
|
||||
return RAD_E_SHAPE;
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (src[j] >= numel(X)) return RAD_E_SHAPE;
|
||||
const int64_t v = src[j] < 0 ? 0 : at(X, src[j]);
|
||||
st_int_dt(Y->dtype, Y->data, offlin(Y, j), v * mul[j] + add[j]);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ cu_repeat */
|
||||
/* y[j] = j * len[0] * mult over y's elements: the bounds of equal sequences. */
|
||||
AVX_KERNEL(cu_repeat) {
|
||||
|
||||
+64
-1
@@ -359,6 +359,17 @@ std::vector<Case> build_cases() {
|
||||
add("attn_dense", { HI("M", 24), HI("head_dim", 64), HI("gqa", 1), HI("causal", 0),
|
||||
HS("dtype", dt), HF("scale", 0.125), HI("max_seqlen", 24),
|
||||
HI("block", 7) });
|
||||
/* Cross attention: each entry's keys a run of their own, both sides of `causal`. */
|
||||
for (const char* dt : { "f32", "bf16" })
|
||||
for (int causal : { 0, 1 })
|
||||
add("attn_cross", { HI("M", 24), HI("head_dim", 64), HI("gqa", 2),
|
||||
HI("causal", causal), HS("dtype", dt), HF("scale", 0.125) });
|
||||
/* Neighborhood attention: a 4 x 4 x 4 volume (the description's), windows narrower and as wide
|
||||
* as its axes. */
|
||||
for (const char* dt : { "f32", "bf16" })
|
||||
for (int kw : { 3, 4 })
|
||||
add("attn_na3d", { HI("M", 64), HI("head_dim", 64), HI("kt", 3), HI("kh", 3),
|
||||
HI("kw", kw), HS("dtype", dt) });
|
||||
for (const char* dt : { "f32", "bf16" })
|
||||
add("seg_sinusoid", { HI("M", 30), HI("n", 64), HI("period", 13), HS("dtype", dt) });
|
||||
/* Gemma 4's audio attention: left 12, over segments longer than the window. */
|
||||
@@ -698,10 +709,16 @@ std::vector<Case> build_cases() {
|
||||
EB(7, 645, dt[0] == 'f' ? 4 : 2, 3));
|
||||
add("gate_add", { HI("M", 7), HI("n", 645), HS("dtype", dt), HS("act", "none") },
|
||||
EB(7, 645, dt[0] == 'f' ? 4 : 2, 3));
|
||||
add("table_add", { HI("M", 3), HI("n", 645), HI("L", 5), HS("dtype", dt) });
|
||||
add("mix_rows", { HI("M", 7), HI("n", 645), HI("k", 12), HS("dtype", dt) });
|
||||
for (const char* mode : { "denorm", "norm" })
|
||||
for (int var : { 0, 1 })
|
||||
add("chan_stats", { HI("M", 7), HI("n", 645), HS("mode", mode), HI("var", var),
|
||||
HF("eps", 1e-4), HS("dtype", dt) });
|
||||
for (int gated : { 1, 0 })
|
||||
add("ln_mod_quant_fp8", { HI("M", 5), HI("n", 256), HF("eps", 1e-6), HI("group", 128),
|
||||
HI("gated", gated), HS("dtype", dt) });
|
||||
for (int round : { 1, 0 })
|
||||
for (int round : { 3, 2, 1, 0 })
|
||||
add("timestep_embed", { HI("M", 3), HI("n", 256), HF("scale", 1000.0),
|
||||
HF("max_period", 10000.0), HI("round", round),
|
||||
HS("dtype", dt) });
|
||||
@@ -776,6 +793,17 @@ std::vector<Case> build_cases() {
|
||||
HS("dtype", dt) });
|
||||
add("rms_mod_quant_fp8", { HI("M", 5), HI("n", 256), HF("eps", 1e-5), HI("group", 128),
|
||||
HI("gated", gated), HS("dtype", dt) });
|
||||
/* A zero-centred gain (1 + w), the norm and gain rounded once (Krea 2's). */
|
||||
add("rms_mod", { HI("M", 7), HI("n", 645), HF("eps", 1e-5), HI("gated", gated),
|
||||
HS("dtype", dt), HF("wadd", 1.0), HI("norm_once", 1) });
|
||||
add("rms_mod_quant_fp8", { HI("M", 5), HI("n", 256), HF("eps", 1e-5), HI("group", 128),
|
||||
HI("gated", gated), HS("dtype", dt), HF("wadd", 1.0),
|
||||
HI("norm_once", 1) });
|
||||
/* A LayerNorm without a gain (FLUX's). */
|
||||
add("rms_mod", { HI("M", 7), HI("n", 645), HF("eps", 1e-6), HI("gated", gated),
|
||||
HS("dtype", dt), HS("norm", "layer") });
|
||||
add("rms_mod_quant_fp8", { HI("M", 5), HI("n", 256), HF("eps", 1e-6), HI("group", 128),
|
||||
HI("gated", gated), HS("dtype", dt), HS("norm", "layer") });
|
||||
}
|
||||
add("flow_x0_step", { HI("M", 9), HI("n", 645), HS("dtype", dt) });
|
||||
for (int pt : { 1, 4 })
|
||||
@@ -833,6 +861,37 @@ std::vector<Case> build_cases() {
|
||||
add("flow_renoise", { HI("M", 9), HI("n", 96), HI("stream", 1), HS("dtype", "f32") });
|
||||
add("flow_renoise", { HI("M", 5), HI("n", 645), HI("stream", 7), HS("dtype", "f32") });
|
||||
|
||||
/* LTX-2: the video's 3-axis rotary (two padded pairs, a rank's second head), the cross
|
||||
* attentions' 1-axis one, the per-head gate and the guided step. */
|
||||
for (const char* dt : { "f32", "bf16" }) {
|
||||
add("rope_split", { HI("M", 7), HI("n_head", 3), HI("head_dim", 128), HI("dim", 4096),
|
||||
HI("n_dims", 3), HF("theta", 10000.0), HS("max", "20 2048 2048"),
|
||||
HI("head0", 1), HS("dtype", dt) });
|
||||
add("rope_split", { HI("M", 5), HI("n_head", 4), HI("head_dim", 64), HI("dim", 256),
|
||||
HI("n_dims", 1), HF("theta", 10000.0), HS("max", "20"),
|
||||
HS("dtype", dt) });
|
||||
add("head_gate", { HI("M", 7), HI("n_head", 5), HI("head_dim", 64), HS("dtype", dt) });
|
||||
add("ltx_guide", { HI("M", 9), HI("n", 128), HS("dtype", dt) });
|
||||
add("pixel_norm", { HI("M", 26), HI("n", 96), HF("eps", 1e-6), HS("form", "rsqrt"),
|
||||
HI("ileave", 13), HF("mult", 3.7), HS("dtype", dt) });
|
||||
add("pixel_norm", { HI("M", 7), HI("n", 256), HF("eps", 1e-8), HS("form", "div"),
|
||||
HS("act", "silu"), HS("dtype", dt) });
|
||||
add("d2s3d", { HI("M", 27), HI("C", 5), HI("st", 2), HI("sh", 2), HI("sw", 2),
|
||||
HS("dtype", dt) });
|
||||
add("s2d3d", { HI("M", 27), HI("C", 4), HI("st", 2), HI("sh", 1), HI("sw", 1),
|
||||
HI("tpre", 1), HS("mode", "copy"), HS("dtype", dt) });
|
||||
add("s2d3d", { HI("M", 64), HI("C", 6), HI("st", 2), HI("sh", 2), HI("sw", 2),
|
||||
HI("group", 4), HS("mode", "mean"), HI("add", 1), HS("dtype", dt) });
|
||||
add("pix_patch", { HI("M", 12), HI("p", 4), HI("C", 3), HS("dtype", dt) });
|
||||
add("patch_pix8", { HI("M", 12), HI("p", 4), HI("C", 3), HS("dtype", dt) });
|
||||
add("stft_logmel", { HI("M", 6), HI("hop", 80), HI("nfft", 512), HI("n_mel", 64),
|
||||
HI("ch", 2), HS("dtype", dt) });
|
||||
add("bwe_up", { HI("M", 600), HI("ratio", 3), HI("ch", 2), HS("dtype", dt) });
|
||||
add("tile_crop", { HI("M", 125), HI("C", 7), HS("dtype", dt) });
|
||||
add("tile_stitch", { HI("M", 4000), HI("tile", 4), HI("stride", 3), HI("scale", 2),
|
||||
HI("p", 4), HI("C", 3), HS("dtype", dt) });
|
||||
}
|
||||
|
||||
/* Video sparse attention over the cubes libref describes: a 3 x 3 x 3 grid in boxes of 2
|
||||
* after two dense segments of as many rows (P 8, V 8; sparsity 0.8 keeps 2), with and without
|
||||
* the coarse operands; and MiniMax-H3's boxes of 4 at patch 2 over a 10 x 5 x 5 grid after
|
||||
@@ -967,9 +1026,13 @@ std::vector<Case> build_cases() {
|
||||
add("frame_store", { HI("M", 4), HI("n_codes", 3), HI("stride", 5), HI("rep", 1),
|
||||
HI("skip", 1) });
|
||||
add("iota", { HI("M", 645), HI("start", 3), HI("step", 2) });
|
||||
add("tile_grid", { HI("M", 9), HI("tile", 16), HI("stride", 14) });
|
||||
add("vol_index", { HI("M", 125) });
|
||||
add("iota", { HI("M", 7), HI("start", -5), HI("step", 1) });
|
||||
add("cu_repeat", { HI("M", 5), HI("mult", 3) });
|
||||
add("cu_repeat", { HI("M", 9), HI("mult", 1) });
|
||||
add("int_affine", { HI("M", 4), HS("src", "0 -1 2 1"), HS("mul", "2 0 8 4"),
|
||||
HS("add", "-1 7 -7 -3") });
|
||||
|
||||
/* ================================================================== speed-only, at model size
|
||||
*
|
||||
|
||||
+123
-16
@@ -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);
|
||||
@@ -424,7 +441,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 = ldt(T, offlin(T, m));
|
||||
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];
|
||||
@@ -553,14 +572,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);
|
||||
@@ -576,6 +596,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) {
|
||||
@@ -776,8 +798,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];
|
||||
@@ -855,7 +880,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);
|
||||
@@ -1186,3 +1211,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;
|
||||
}
|
||||
+7
-2
@@ -100,7 +100,7 @@ int avx_par_force(int threads);
|
||||
X(gemm_nt_q_gated) X(embed_lookup) X(embed_lookup_q) X(logits_gemm) \
|
||||
X(lora_shrink) X(lora_expand) \
|
||||
/* ---- attention */ \
|
||||
X(attn_paged) X(attn_dense) X(kv_store) X(rope) \
|
||||
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) \
|
||||
X(qsa_score) X(qsa_select) X(qsa_tail_store) X(qsa_work) \
|
||||
/* ---- mixture of experts */ \
|
||||
@@ -123,12 +123,17 @@ int avx_par_force(int threads);
|
||||
X(vtile_gather) X(vtile_stitch) X(grid_pos3) X(vsa_select) \
|
||||
X(attn_vsa) X(conv3d_ndhwc) X(group_norm) X(tconv_dw) \
|
||||
X(resize_bilinear) X(patch_fold) X(flow_renoise) \
|
||||
/* ---- LTX-2 */ \
|
||||
X(rope_split) X(head_gate) X(ltx_guide) X(pixel_norm) \
|
||||
X(d2s3d) X(s2d3d) X(pix_patch) X(patch_pix8) \
|
||||
X(stft_logmel) X(bwe_up) X(tile_grid) X(tile_crop) \
|
||||
X(tile_stitch) X(vol_index) \
|
||||
/* ---- audio generation */ \
|
||||
X(conv1d) X(conv_t1d) X(snake) X(cfg_topk_sample) \
|
||||
X(rvq_embed) X(frame_mix_store) X(end_status) X(frame_store) \
|
||||
X(resample_nearest) X(fourier_embed) X(win_input) X(win_prep) \
|
||||
X(win_finish) X(chan_split) X(interleave2) X(iota) \
|
||||
X(cu_repeat) X(snake_aa) \
|
||||
X(cu_repeat) X(snake_aa) X(int_affine) \
|
||||
/* ---- sampling, and the drafter's token walk */ \
|
||||
X(sample_penalties) X(sample_dry) X(sample_temp) X(sample_topk) \
|
||||
X(sample_topp) X(sample_minp) X(sample_typical) X(sample_xtc) \
|
||||
|
||||
@@ -0,0 +1,604 @@
|
||||
/* avx_ltx.cpp -- LTX-2's ops (docs/OPS.md, "LTX-2"): its rotary over fractional coordinates, the
|
||||
* per-head attention gate, the guided sampler step, its VAE's norm, resampling and patches, and its
|
||||
* vocoder's spectrogram and resampler. libref's arithmetic, element by element
|
||||
* (libref/ref_ltx.cpp states the roundings): LTX-2 is a GPU model, and these run on the CPU only
|
||||
* where a whole model does, so they are written for agreement rather than speed.
|
||||
*/
|
||||
#include "avx_vec.h"
|
||||
#include "avx_common.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
using namespace avx;
|
||||
|
||||
namespace {
|
||||
|
||||
/* A product the compiler may not fuse into the add after it (libavx builds with
|
||||
* -ffp-contract=fast; libref rounds the product first). */
|
||||
inline float keep(float x) { __asm__("" : "+x"(x)); return x; }
|
||||
|
||||
double linspace01(int64_t i, int64_t steps) {
|
||||
if (steps <= 1) return 0.0;
|
||||
const double step = 1.0 / (double)(steps - 1);
|
||||
return i < steps / 2 ? step * (double)i : 1.0 - step * (double)(steps - 1 - i);
|
||||
}
|
||||
|
||||
int parse_floats(const char* s, std::vector<float>* out) {
|
||||
out->clear();
|
||||
while (s && *s) {
|
||||
char* end = nullptr;
|
||||
const double v = std::strtod(s, &end);
|
||||
if (end == s) break;
|
||||
out->push_back((float)v);
|
||||
s = end;
|
||||
}
|
||||
return (int)out->size();
|
||||
}
|
||||
|
||||
inline int64_t at_i(const RadTensor* t, int64_t i) {
|
||||
return (int64_t)ld_elem(t->data, t->dtype, i * laststride(t));
|
||||
}
|
||||
|
||||
float pos_at(const RadTensor* p, int64_t d, int64_t r) {
|
||||
const int64_t off = p->rank >= 2 ? d * p->stride[0] + r * p->stride[1] : r * laststride(p);
|
||||
return ldt(p, off);
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* ------------------------------------------------------------------ rope_split */
|
||||
AVX_KERNEL(rope_split) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *P = &a->t[1];
|
||||
const int64_t nh = p_int(a, "n_head", 0), hd = p_int(a, "head_dim", 0);
|
||||
const int64_t dim = p_int(a, "dim", 0), nd = p_int(a, "n_dims", 0), h0 = p_int(a, "head0", 0);
|
||||
const double theta = (double)p_f32(a, "theta", 10000.0f);
|
||||
std::vector<float> mx;
|
||||
if (nh <= 0 || hd <= 0 || (hd & 1) || nd <= 0 || dim < (h0 + nh) * hd ||
|
||||
parse_floats(p_str(a, "max", ""), &mx) != (int)nd)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t P2 = dim / 2, steps = P2 / nd, pad = P2 - steps * nd, half = hd / 2;
|
||||
const int64_t rows = rows_of(X, nh * hd);
|
||||
if (rows <= 0 || P->rank < 1) return RAD_E_SHAPE;
|
||||
std::vector<float> freq((size_t)steps);
|
||||
for (int64_t k = 0; k < steps; ++k)
|
||||
freq[(size_t)k] = (float)(std::pow(theta, linspace01(k, steps)) * M_PI / 2.0);
|
||||
const int64_t xs = laststride(X);
|
||||
AVX_PARALLEL_FOR_IF(rows * nh * hd >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < rows; ++r) {
|
||||
const int64_t xb = rowoff(X, r, nh * hd);
|
||||
for (int64_t h = 0; h < nh; ++h)
|
||||
for (int64_t i = 0; i < half; ++i) {
|
||||
const int64_t p = (h0 + h) * half + i;
|
||||
if (p < pad) continue;
|
||||
const int64_t j = p - pad, d = j % nd, k = j / nd;
|
||||
const float g = pos_at(P, d, r) / mx[(size_t)d];
|
||||
const float ang = (2.0f * g - 1.0f) * freq[(size_t)k];
|
||||
const float c = (float)std::cos((double)ang), s = (float)std::sin((double)ang);
|
||||
const int64_t o1 = xb + (h * hd + i) * xs, o2 = xb + (h * hd + half + i) * xs;
|
||||
const float x1 = ldt(X, o1), x2 = ldt(X, o2);
|
||||
st_elem(X->data, X->dtype, o1, keep(x1 * c) - keep(x2 * s));
|
||||
st_elem(X->data, X->dtype, o2, keep(x2 * c) + keep(x1 * s));
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ head_gate */
|
||||
AVX_KERNEL(head_gate) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *G = &a->t[0], *X = &a->t[1];
|
||||
const int64_t nh = p_int(a, "n_head", 0), hd = p_int(a, "head_dim", 0);
|
||||
if (nh <= 0 || hd <= 0) return RAD_E_SHAPE;
|
||||
const int64_t rows = rows_of(X, nh * hd);
|
||||
if (rows <= 0 || rows_of(G, nh) < rows) return RAD_E_SHAPE;
|
||||
const int64_t xs = laststride(X), gs = laststride(G);
|
||||
AVX_PARALLEL_FOR_IF(rows * nh * hd >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < rows; ++r) {
|
||||
const int64_t xb = rowoff(X, r, nh * hd), gb = rowoff(G, r, nh);
|
||||
for (int64_t h = 0; h < nh; ++h) {
|
||||
const float lg = ldt(G, gb + h * gs);
|
||||
const float sg = narrow1(G->dtype, 1.0f / (1.0f + std::exp(-lg)));
|
||||
const float gate = narrow1(X->dtype, 2.0f * sg);
|
||||
for (int64_t i = 0; i < hd; ++i) {
|
||||
const int64_t o = xb + (h * hd + i) * xs;
|
||||
st_elem(X->data, X->dtype, o, ldt(X, o) * gate);
|
||||
}
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ ltx_guide */
|
||||
AVX_KERNEL(ltx_guide) {
|
||||
if (a->n_t < 8) return RAD_E_INVAL;
|
||||
const RadTensor *Vc = &a->t[0], *Vu = rad_arg_in(a, 1), *Vs = rad_arg_in(a, 2);
|
||||
const RadTensor *Vm = rad_arg_in(a, 3), *Sc = &a->t[4], *Sh = &a->t[5], *X = &a->t[7];
|
||||
const int64_t n = p_int(a, "n", 0);
|
||||
if (n <= 0 || numel(Sc) < 4 || numel(Sh) < 2) return RAD_E_SHAPE;
|
||||
const int64_t rows = rows_of(X, n);
|
||||
if (rows <= 0 || rows_of(Vc, n) < rows || (Vu && rows_of(Vu, n) < rows) ||
|
||||
(Vs && rows_of(Vs, n) < rows) || (Vm && rows_of(Vm, n) < rows))
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t ss = laststride(Sc);
|
||||
const float gs = ldt(Sc, 0), stg = ldt(Sc, ss), ms = ldt(Sc, 2 * ss), rr = ldt(Sc, 3 * ss);
|
||||
const float sig = ldt(Sh, 0), sig_n = ldt(Sh, laststride(Sh));
|
||||
const bool use_u = Vu && gs != 1.0f, use_s = Vs && stg != 0.0f, use_m = Vm && ms != 1.0f;
|
||||
const float cu = gs - 1.0f, cm = ms - 1.0f;
|
||||
const int64_t N = rows * n;
|
||||
std::vector<float> x0c((size_t)N), g((size_t)N);
|
||||
auto x0 = [&](const RadTensor* V, int64_t r, int64_t c, float x) {
|
||||
return x - keep(ldt(V, rowoff(V, r, n) + c * laststride(V)) * sig);
|
||||
};
|
||||
AVX_PARALLEL_FOR_IF(N >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < rows; ++r)
|
||||
for (int64_t c = 0; c < n; ++c) {
|
||||
const float x = ldt(X, rowoff(X, r, n) + c * laststride(X));
|
||||
const float xc = x0(Vc, r, c, x);
|
||||
float v = xc;
|
||||
if (use_u) v = v + keep(cu * (xc - x0(Vu, r, c, x)));
|
||||
if (use_s) v = v + keep(stg * (xc - x0(Vs, r, c, x)));
|
||||
if (use_m) v = v + keep(cm * (xc - x0(Vm, r, c, x)));
|
||||
x0c[(size_t)(r * n + c)] = xc;
|
||||
g[(size_t)(r * n + c)] = v;
|
||||
}
|
||||
float fac = 1.0f, kept = 1.0f;
|
||||
if (rr > 0.0f) {
|
||||
auto stdev = [&](const std::vector<float>& v) {
|
||||
double m = 0.0;
|
||||
for (float e : v) m += e;
|
||||
m /= (double)N;
|
||||
double sq = 0.0;
|
||||
for (float e : v) sq += ((double)e - m) * ((double)e - m);
|
||||
return (float)std::sqrt(sq / (double)(N > 1 ? N - 1 : 1));
|
||||
};
|
||||
fac = stdev(x0c) / stdev(g);
|
||||
kept = (float)(1.0 - (double)rr);
|
||||
}
|
||||
const float dt = sig_n - sig;
|
||||
AVX_PARALLEL_FOR_IF(N >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < rows; ++r)
|
||||
for (int64_t c = 0; c < n; ++c) {
|
||||
float v = g[(size_t)(r * n + c)];
|
||||
if (rr > 0.0f) v = keep(rr * keep(v * fac)) + keep(kept * v);
|
||||
const int64_t o = rowoff(X, r, n) + c * laststride(X);
|
||||
const float x = ldt(X, o);
|
||||
const float vel = (x - v) / sig;
|
||||
st_elem(X->data, X->dtype, o, x + keep(dt * vel));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ pixel_norm */
|
||||
AVX_KERNEL(pixel_norm) {
|
||||
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), L = p_int(a, "ileave", 1);
|
||||
const float eps = p_f32(a, "eps", 1e-6f), mult = p_f32(a, "mult", 1.0f);
|
||||
const char* fs = p_str(a, "form", "rsqrt");
|
||||
const char* as = p_str(a, "act", "none");
|
||||
const int form = !std::strcmp(fs, "rsqrt") ? 0 : !std::strcmp(fs, "div") ? 1 : -1;
|
||||
const int act = !std::strcmp(as, "none") ? 0 : !std::strcmp(as, "silu") ? 1 : -1;
|
||||
if (form < 0 || act < 0 || n <= 0 || L <= 0) return RAD_E_INVAL;
|
||||
const int64_t rows = rows_of(Y, n * L) * L;
|
||||
if (rows <= 0 || rows_of(X, n) < rows) return RAD_E_SHAPE;
|
||||
const uint32_t dt = Y->dtype;
|
||||
const int64_t xs = laststride(X), ys = laststride(Y);
|
||||
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), yb = rowoff(Y, r / L, n * L), l = r % L;
|
||||
float ss = 0.0f;
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
const float v = ldt(X, xb + i * xs);
|
||||
ss += narrow1(dt, v * v);
|
||||
}
|
||||
const float v = narrow1(dt, narrow1(dt, ss / (float)n) + eps);
|
||||
const float sc = form == 0 ? narrow1(dt, 1.0f / std::sqrt(v)) : narrow1(dt, std::sqrt(v));
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
const float x = ldt(X, xb + i * xs);
|
||||
float z = narrow1(dt, form == 0 ? x * sc : x / sc);
|
||||
if (act == 1) z = narrow1(dt, z / (1.0f + std::exp(-z)));
|
||||
if (mult != 1.0f) z = narrow1(dt, z * mult);
|
||||
st_elem(Y->data, dt, yb + (i * L + l) * ys, z);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ d2s3d */
|
||||
AVX_KERNEL(d2s3d) {
|
||||
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *G = &a->t[1], *Y = &a->t[2];
|
||||
const int64_t C = p_int(a, "C", 0), st = p_int(a, "st", 1), sh = p_int(a, "sh", 1);
|
||||
const int64_t sw = p_int(a, "sw", 1), drop = p_int(a, "drop", st - 1);
|
||||
if (C <= 0 || st <= 0 || sh <= 0 || sw <= 0 || drop < 0 || drop >= st || numel(G) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t T = at_i(G, 0), H = at_i(G, 1), W = at_i(G, 2), P = st * sh * sw;
|
||||
const int64_t To = T * st - drop, Ho = H * sh, Wo = W * sw;
|
||||
if (T <= 0 || H <= 0 || W <= 0 || rows_of(X, C * P) < T * H * W ||
|
||||
rows_of(Y, C) < To * Ho * Wo)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t xs = laststride(X), ys = laststride(Y);
|
||||
AVX_PARALLEL_FOR_IF(To * Ho * Wo * C >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < To * Ho * Wo; ++r) {
|
||||
const int64_t tt = r / (Ho * Wo) + drop, ii = (r / Wo) % Ho, jj = r % Wo;
|
||||
const int64_t t = tt / st, aa = tt % st, i = ii / sh, b = ii % sh, j = jj / sw, d = jj % sw;
|
||||
const int64_t xb = rowoff(X, (t * H + i) * W + j, C * P), yb = rowoff(Y, r, C);
|
||||
for (int64_t c = 0; c < C; ++c)
|
||||
st_elem(Y->data, Y->dtype, yb + c * ys,
|
||||
ldt(X, xb + (((c * st + aa) * sh + b) * sw + d) * xs));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ s2d3d */
|
||||
AVX_KERNEL(s2d3d) {
|
||||
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *G = &a->t[1], *Y = &a->t[2];
|
||||
const int64_t C = p_int(a, "C", 0), st = p_int(a, "st", 1), sh = p_int(a, "sh", 1);
|
||||
const int64_t sw = p_int(a, "sw", 1), tpre = p_int(a, "tpre", 0), grp = p_int(a, "group", 1);
|
||||
const bool add = p_int(a, "add", 0) != 0;
|
||||
const char* ms = p_str(a, "mode", "copy");
|
||||
const int mode = !std::strcmp(ms, "copy") ? 0 : !std::strcmp(ms, "mean") ? 1 : -1;
|
||||
if (mode < 0) return RAD_E_INVAL;
|
||||
if (C <= 0 || st <= 0 || sh <= 0 || sw <= 0 || tpre < 0 || grp <= 0 || numel(G) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t T = at_i(G, 0), H = at_i(G, 1), W = at_i(G, 2), P = C * st * sh * sw;
|
||||
const int64_t Tv = T + tpre;
|
||||
if (T <= 0 || Tv % st || H % sh || W % sw || (mode == 1 && P % grp)) return RAD_E_SHAPE;
|
||||
const int64_t To = Tv / st, Ho = H / sh, Wo = W / sw, Co = mode == 1 ? P / grp : P;
|
||||
if (rows_of(X, C) < T * H * W || rows_of(Y, Co) < To * Ho * Wo) return RAD_E_SHAPE;
|
||||
const int64_t xs = laststride(X), ys = laststride(Y);
|
||||
const uint32_t dt = Y->dtype;
|
||||
AVX_PARALLEL_FOR_IF(To * Ho * Wo * P >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < To * Ho * Wo; ++r) {
|
||||
const int64_t t = r / (Ho * Wo), i = (r / Wo) % Ho, j = r % Wo;
|
||||
const int64_t yb = rowoff(Y, r, Co);
|
||||
auto val = [&](int64_t q) {
|
||||
const int64_t d = q % sw, b = (q / sw) % sh, aa = (q / (sw * sh)) % st;
|
||||
const int64_t c = q / (sw * sh * st);
|
||||
const int64_t v = t * st + aa, tr = v < tpre ? v : v - tpre;
|
||||
return ldt(X, rowoff(X, (tr * H + i * sh + b) * W + j * sw + d, C) + c * xs);
|
||||
};
|
||||
for (int64_t o = 0; o < Co; ++o) {
|
||||
float v;
|
||||
if (mode == 0) {
|
||||
v = val(o);
|
||||
} else {
|
||||
float s = 0.0f;
|
||||
for (int64_t q = o * grp; q < (o + 1) * grp; ++q) s += val(q);
|
||||
v = narrow1(dt, s / (float)grp);
|
||||
}
|
||||
const int64_t oy = yb + o * ys;
|
||||
st_elem(Y->data, dt, oy, add ? narrow1(dt, ldt(Y, oy)) + narrow1(dt, v) : v);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ pix_patch / patch_pix8 */
|
||||
static inline int64_t patch_col(int64_t c, int64_t ph, int64_t pw, int64_t p, bool wh) {
|
||||
return c * p * p + (wh ? pw * p + ph : ph * p + pw);
|
||||
}
|
||||
|
||||
AVX_KERNEL(pix_patch) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *Y = &a->t[1];
|
||||
const int64_t p = p_int(a, "p", 0), C = p_int(a, "C", 0);
|
||||
const bool wh = std::strcmp(p_str(a, "order", "wh"), "hw") != 0;
|
||||
if (p <= 0 || C <= 0 || X->rank != 2 || Y->rank != 2 || X->shape[1] % C) return RAD_E_SHAPE;
|
||||
const int64_t H = X->shape[0], W = X->shape[1] / C;
|
||||
if (H <= 0 || W <= 0 || H % p || W % p || Y->shape[0] != H / p ||
|
||||
Y->shape[1] != (W / p) * C * p * p)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t xr = X->stride[0], yr = Y->stride[0];
|
||||
AVX_PARALLEL_FOR_IF(H * W * C >= AVX_PAR_MIN)
|
||||
for (int64_t y = 0; y < H; ++y)
|
||||
for (int64_t x = 0; x < W; ++x)
|
||||
for (int64_t c = 0; c < C; ++c)
|
||||
st_elem(Y->data, Y->dtype,
|
||||
(y / p) * yr + (x / p) * C * p * p + patch_col(c, y % p, x % p, p, wh),
|
||||
ldt(X, y * xr + x * C + c));
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
AVX_KERNEL(patch_pix8) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *Y = &a->t[1];
|
||||
const int64_t p = p_int(a, "p", 0), C = p_int(a, "C", 0);
|
||||
const bool wh = std::strcmp(p_str(a, "order", "wh"), "hw") != 0;
|
||||
if (p <= 0 || C <= 0 || X->rank != 2 || Y->rank != 2 || Y->dtype != RAD_U8 ||
|
||||
X->shape[1] % (C * p * p))
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t R = X->shape[0], w = X->shape[1] / (C * p * p);
|
||||
if (R <= 0 || w <= 0 || Y->shape[0] != R * p || Y->shape[1] != w * p * C) return RAD_E_SHAPE;
|
||||
const int64_t xr = X->stride[0], yr = Y->stride[0];
|
||||
const uint32_t dt = X->dtype;
|
||||
uint8_t* yb = (uint8_t*)Y->data;
|
||||
AVX_PARALLEL_FOR_IF(R * p * w * p * C >= AVX_PAR_MIN)
|
||||
for (int64_t y = 0; y < R * p; ++y)
|
||||
for (int64_t x = 0; x < w * p; ++x)
|
||||
for (int64_t c = 0; c < C; ++c) {
|
||||
const float v = ldt(X, (y / p) * xr + (x / p) * C * p * p +
|
||||
patch_col(c, y % p, x % p, p, wh));
|
||||
const float cl = std::min(std::max(v, -1.0f), 1.0f);
|
||||
float u = narrow1(dt, narrow1(dt, cl / 2.0f) + 0.5f);
|
||||
u = std::min(std::max(u, 0.0f), 1.0f);
|
||||
yb[y * yr + x * C + c] = (uint8_t)std::nearbyint(u * 255.0f);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ vol_index */
|
||||
AVX_KERNEL(vol_index) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *G = &a->t[0], *Y = &a->t[1];
|
||||
if (numel(G) < 3 || Y->rank != 2 || Y->shape[0] != 3) return RAD_E_SHAPE;
|
||||
const int64_t T = at_i(G, 0), H = at_i(G, 1), W = at_i(G, 2), n = Y->shape[1];
|
||||
if (T <= 0 || H <= 0 || W <= 0 || n <= 0) return RAD_E_SHAPE;
|
||||
const int64_t m1 = std::min(n, T * H * W);
|
||||
for (int64_t m = 0; m < m1; ++m) {
|
||||
const int64_t v[3] = { m / (H * W), (m / W) % H, m % W };
|
||||
for (int64_t c = 0; c < 3; ++c)
|
||||
st_int_dt(Y->dtype, Y->data, c * Y->stride[0] + m * Y->stride[1], v[c]);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ tile_grid / tile_crop / tile_stitch */
|
||||
/* diffusers' tiled decodes: libref/ref_ltx.cpp states the cuts and the blends. */
|
||||
namespace {
|
||||
struct TileCut {
|
||||
int64_t tile = 1, stride = 1, min = 1;
|
||||
bool intervals = false;
|
||||
int64_t count(int64_t L) const {
|
||||
if (intervals && L <= tile) return 1;
|
||||
int64_t n = (L + stride - 1) / stride;
|
||||
while (intervals && n > 1 && L - (n - 1) * stride < min) --n;
|
||||
return n;
|
||||
}
|
||||
int64_t len(int64_t L, int64_t k) const {
|
||||
return k == count(L) - 1 ? L - k * stride : std::min(tile, L - k * stride);
|
||||
}
|
||||
};
|
||||
|
||||
bool tile_cuts(const RadArgs* a, TileCut* s, TileCut* t) {
|
||||
static const char* const kRules[] = { "stride", "intervals" };
|
||||
const int rule = p_enum(a, "rule", kRules, 2, 0);
|
||||
s->tile = p_int(a, "tile", 0);
|
||||
s->stride = p_int(a, "stride", 0);
|
||||
s->min = p_int(a, "min", 1);
|
||||
s->intervals = rule == 1;
|
||||
t->tile = p_int(a, "tile_t", 1 << 30);
|
||||
t->stride = p_int(a, "stride_t", 1 << 30);
|
||||
t->min = p_int(a, "min_t", 1);
|
||||
t->intervals = rule == 1;
|
||||
return rule >= 0 && s->tile > 0 && s->stride > 0 && s->stride <= s->tile && s->min > 0 &&
|
||||
t->tile > 0 && t->stride > 0 && t->stride <= t->tile && t->min > 0;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
AVX_KERNEL(tile_grid) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const RadTensor *G = &a->t[0], *Y = &a->t[1];
|
||||
TileCut cs, ct;
|
||||
const int64_t n = numel(Y) / 8, tail = p_int(a, "tail", 0);
|
||||
if (!tile_cuts(a, &cs, &ct) || tail < 0 || n <= 0 || numel(G) < 3) return RAD_E_SHAPE;
|
||||
const int64_t T = at_i(G, 0), H = at_i(G, 1), W = at_i(G, 2);
|
||||
if (T <= 0 || H <= 0 || W <= 0) return RAD_E_SHAPE;
|
||||
const int64_t nt = ct.count(T), ni = cs.count(H), nj = cs.count(W), count = nt * ni * nj;
|
||||
for (int64_t k = 0; k < n; ++k) {
|
||||
int64_t row[8] = {};
|
||||
if (k < count) {
|
||||
const int64_t kt = k / (ni * nj), i = (k / nj) % ni, j = k % nj;
|
||||
const int64_t v[8] = { ct.len(T, kt) + (kt == nt - 1 ? tail : 0), cs.len(H, i),
|
||||
cs.len(W, j), kt * ct.stride, i * cs.stride, j * cs.stride, k,
|
||||
count };
|
||||
std::memcpy(row, v, sizeof row);
|
||||
}
|
||||
for (int64_t e = 0; e < 8; ++e) st_int_dt(Y->dtype, Y->data, offlin(Y, k * 8 + e), row[e]);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
AVX_KERNEL(tile_crop) {
|
||||
if (!rad_args_have(a, 4)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *G = &a->t[1], *B = &a->t[2], *Y = &a->t[3];
|
||||
const int64_t C = p_int(a, "C", 0);
|
||||
if (C <= 0 || numel(G) < 3 || numel(B) < 6) return RAD_E_SHAPE;
|
||||
const int64_t T = at_i(G, 0), H = at_i(G, 1), W = at_i(G, 2);
|
||||
const int64_t t = at_i(B, 0), h = at_i(B, 1), w = at_i(B, 2);
|
||||
const int64_t t0 = at_i(B, 3), y0 = at_i(B, 4), x0 = at_i(B, 5);
|
||||
if (t <= 0 || h <= 0 || w <= 0 || t0 < 0 || y0 < 0 || x0 < 0 || t0 + t > T || y0 + h > H ||
|
||||
x0 + w > W || rows_of(X, C) < T * H * W || rows_of(Y, C) < t * h * w)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t xs = laststride(X), ys = laststride(Y);
|
||||
AVX_PARALLEL_FOR_IF(t * h * w * C >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < t * h * w; ++r) {
|
||||
const int64_t f = r / (h * w), i = (r / w) % h, j = r % w;
|
||||
const int64_t xb = rowoff(X, ((t0 + f) * H + y0 + i) * W + x0 + j, C), yb = rowoff(Y, r, C);
|
||||
for (int64_t c = 0; c < C; ++c) st_elem(Y->data, Y->dtype, yb + c * ys, ldt(X, xb + c * xs));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
namespace {
|
||||
struct TileStore {
|
||||
TileCut cs, ct;
|
||||
int64_t T, H, W, sc, st, nt, ni, nj, hsum, wsum;
|
||||
TileStore(const TileCut& s, const TileCut& t, int64_t T_, int64_t H_, int64_t W_, int64_t sc_,
|
||||
int64_t st_)
|
||||
: cs(s), ct(t), T(T_), H(H_), W(W_), sc(sc_), st(st_), nt(t.count(T_)), ni(s.count(H_)),
|
||||
nj(s.count(W_)), hsum(0), wsum(0) {
|
||||
for (int64_t i = 0; i < ni; ++i) hsum += cs.len(H, i);
|
||||
for (int64_t j = 0; j < nj; ++j) wsum += cs.len(W, j);
|
||||
}
|
||||
int64_t frames(int64_t kt) const {
|
||||
return st * ct.len(T, kt) - (kt == 0 ? st - 1 : 0);
|
||||
}
|
||||
int64_t offset(int64_t kt, int64_t i, int64_t j) const {
|
||||
int64_t before = 0;
|
||||
for (int64_t q = 0; q < kt; ++q) before += frames(q);
|
||||
int64_t left = 0, above = 0;
|
||||
for (int64_t q = 0; q < j; ++q) left += cs.len(W, q);
|
||||
for (int64_t q = 0; q < i; ++q) above += cs.len(H, q);
|
||||
return sc * sc * (before * hsum * wsum + frames(kt) * (above * wsum + cs.len(H, i) * left));
|
||||
}
|
||||
int64_t rows() const { return offset(nt, 0, 0); }
|
||||
};
|
||||
} /* namespace */
|
||||
|
||||
AVX_KERNEL(tile_stitch) {
|
||||
if (!rad_args_have(a, 4)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *G = &a->t[1], *Fst = &a->t[2], *Y = &a->t[3];
|
||||
TileCut cs, ct;
|
||||
const int64_t sc = p_int(a, "scale", 0), st = p_int(a, "scale_t", 1);
|
||||
const int64_t p = p_int(a, "p", 0), C = p_int(a, "C", 0);
|
||||
const bool wh = std::strcmp(p_str(a, "order", "wh"), "hw") != 0;
|
||||
if (!tile_cuts(a, &cs, &ct) || sc <= 0 || st <= 0 || p <= 0 || C <= 0 || numel(G) < 3 ||
|
||||
numel(Fst) < 1 || Y->dtype != RAD_U8 || Y->rank != 2)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t T = at_i(G, 0), H = at_i(G, 1), W = at_i(G, 2), f0 = at_i(Fst, 0);
|
||||
const int64_t Hpx = H * sc * p, Wpx = W * sc * p, P = C * p * p;
|
||||
if (T <= 0 || H <= 0 || W <= 0 || Y->shape[1] != Wpx * C || Y->shape[0] % Hpx) return RAD_E_SHAPE;
|
||||
const int64_t nf = Y->shape[0] / Hpx;
|
||||
const TileStore ts(cs, ct, T, H, W, sc, st);
|
||||
if (nf <= 0 || f0 < 0 || f0 + nf > st * T - (st - 1) || rows_of(X, P) < ts.rows())
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t s = cs.stride * sc * p, E = (cs.tile - cs.stride) * sc * p;
|
||||
const int64_t Et = (ct.tile - ct.stride) * st, xs = laststride(X);
|
||||
const uint32_t dt = X->dtype;
|
||||
auto R = [&](int64_t kt, int64_t i, int64_t j, int64_t f, int64_t yy, int64_t xx, int64_t c) {
|
||||
const int64_t wp = cs.len(W, j) * sc, hp = cs.len(H, i) * sc;
|
||||
const int64_t row = ts.offset(kt, i, j) + (f * hp + yy / p) * wp + xx / p;
|
||||
return ldt(X, rowoff(X, row, P) + patch_col(c, yy % p, xx % p, p, wh) * xs);
|
||||
};
|
||||
auto mix = [&](float va, float vb, int64_t k, int64_t e) {
|
||||
const float wa = (float)(1.0 - (double)k / (double)e), wb = (float)((double)k / (double)e);
|
||||
return narrow1(dt, narrow1(dt, keep(va * wa)) + narrow1(dt, keep(vb * wb)));
|
||||
};
|
||||
auto S = [&](int64_t kt, int64_t f, int64_t Yp, int64_t Xp, int64_t c) {
|
||||
const int64_t i = std::min(Yp / s, ts.ni - 1), ly = Yp - i * s;
|
||||
const int64_t j = std::min(Xp / s, ts.nj - 1), lx = Xp - j * s;
|
||||
const int64_t ha = i > 0 ? cs.len(H, i - 1) * sc * p : 0;
|
||||
const int64_t ev = i > 0 ? std::min(std::min(ha, cs.len(H, i) * sc * p), E) : 0;
|
||||
const int64_t wl = j > 0 ? cs.len(W, j - 1) * sc * p : 0;
|
||||
const int64_t eh = j > 0 ? std::min(std::min(wl, cs.len(W, j) * sc * p), E) : 0;
|
||||
const bool vb = i > 0 && ly < ev, hb = j > 0 && lx < eh;
|
||||
float V = R(kt, i, j, f, ly, lx, c);
|
||||
if (vb) {
|
||||
const int64_t ya = ha - ev + ly;
|
||||
float A = R(kt, i - 1, j, f, ya, lx, c);
|
||||
if (hb) A = mix(R(kt, i - 1, j - 1, f, ya, wl - eh + lx, c), A, lx, eh);
|
||||
V = mix(A, V, ly, ev);
|
||||
}
|
||||
if (hb) {
|
||||
float L = R(kt, i, j - 1, f, ly, wl - eh + lx, c);
|
||||
if (vb) L = mix(R(kt, i - 1, j - 1, f, ha - ev + ly, wl - eh + lx, c), L, ly, ev);
|
||||
V = mix(L, V, lx, eh);
|
||||
}
|
||||
return V;
|
||||
};
|
||||
uint8_t* yb = (uint8_t*)Y->data;
|
||||
const int64_t yr = Y->stride[0], band = ct.stride * st;
|
||||
AVX_PARALLEL_FOR_IF(nf * Hpx * Wpx * C >= AVX_PAR_MIN)
|
||||
for (int64_t r = 0; r < nf * Hpx; ++r) {
|
||||
const int64_t n = f0 + r / Hpx, Yp = r % Hpx;
|
||||
const int64_t kt = std::min((n + st - 1) / band, ts.nt - 1);
|
||||
const int64_t f = n - (kt == 0 ? 0 : kt * band - (st - 1));
|
||||
const int64_t et = kt > 0 ? std::min(std::min(ts.frames(kt - 1), ts.frames(kt)), Et) : 0;
|
||||
for (int64_t Xp = 0; Xp < Wpx; ++Xp)
|
||||
for (int64_t c = 0; c < C; ++c) {
|
||||
float v = S(kt, f, Yp, Xp, c);
|
||||
if (kt > 0 && f < et) v = mix(S(kt - 1, ts.frames(kt - 1) - et + f, Yp, Xp, c), v, f, et);
|
||||
const float cl = std::min(std::max(v, -1.0f), 1.0f);
|
||||
float u = narrow1(dt, narrow1(dt, cl / 2.0f) + 0.5f);
|
||||
u = std::min(std::max(u, 0.0f), 1.0f);
|
||||
yb[r * yr + Xp * C + c] = (uint8_t)std::nearbyint(u * 255.0f);
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ stft_logmel */
|
||||
AVX_KERNEL(stft_logmel) {
|
||||
if (!rad_args_have(a, 4)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *B = &a->t[1], *Mb = &a->t[2], *Y = &a->t[3];
|
||||
const int64_t hop = p_int(a, "hop", 0), nfft = p_int(a, "nfft", 0), nm = p_int(a, "n_mel", 0);
|
||||
const int64_t ch = p_int(a, "ch", 0);
|
||||
if (hop <= 0 || nfft < hop || nm <= 0 || ch <= 0) return RAD_E_SHAPE;
|
||||
const int64_t F = nfft / 2 + 1, S = rows_of(X, ch), frames = rows_of(Y, ch * nm);
|
||||
if (S <= 0 || frames <= 0 || frames * hop > S || numel(B) < 2 * F * nfft || numel(Mb) < nm * F)
|
||||
return RAD_E_SHAPE;
|
||||
const uint32_t dt = Y->dtype;
|
||||
const int64_t pad = nfft - hop, xs = laststride(X), ys = laststride(Y);
|
||||
const float floor_v = narrow1(dt, 1e-5f);
|
||||
AVX_PARALLEL_FOR_IF(frames * ch * F * nfft >= AVX_PAR_MIN)
|
||||
for (int64_t t = 0; t < frames; ++t)
|
||||
for (int64_t h = 0; h < ch; ++h) {
|
||||
std::vector<float> mag((size_t)F);
|
||||
for (int64_t k = 0; k < F; ++k) {
|
||||
float re = 0.0f, im = 0.0f;
|
||||
for (int64_t j = 0; j < nfft; ++j) {
|
||||
const int64_t s = t * hop + j - pad;
|
||||
if (s < 0) continue;
|
||||
const float v = ldt(X, rowoff(X, s, ch) + h * xs);
|
||||
re += keep(ldt(B, k * nfft + j) * v);
|
||||
im += keep(ldt(B, (F + k) * nfft + j) * v);
|
||||
}
|
||||
re = narrow1(dt, re);
|
||||
im = narrow1(dt, im);
|
||||
mag[(size_t)k] = narrow1(dt, std::sqrt(narrow1(dt, narrow1(dt, re * re) +
|
||||
narrow1(dt, im * im))));
|
||||
}
|
||||
const int64_t yb = rowoff(Y, t, ch * nm);
|
||||
for (int64_t m = 0; m < nm; ++m) {
|
||||
float s = 0.0f;
|
||||
for (int64_t k = 0; k < F; ++k) s += keep(ldt(Mb, m * F + k) * mag[(size_t)k]);
|
||||
const float mel = std::max(narrow1(dt, s), floor_v);
|
||||
st_elem(Y->data, dt, yb + (h * nm + m) * ys, std::log(mel));
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ bwe_up */
|
||||
AVX_KERNEL(bwe_up) {
|
||||
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
|
||||
const RadTensor *X = &a->t[0], *Rz = &a->t[1], *Y = &a->t[2];
|
||||
const int64_t r = p_int(a, "ratio", 0), ch = p_int(a, "ch", 0);
|
||||
if (r <= 0 || ch <= 0) return RAD_E_SHAPE;
|
||||
const int64_t S = rows_of(X, ch), O = rows_of(Y, ch);
|
||||
if (S <= 0 || O <= 0 || O > S * r || rows_of(Rz, ch) < O) return RAD_E_SHAPE;
|
||||
const uint32_t dt = X->dtype;
|
||||
const int64_t w = (int64_t)std::ceil(6.0 / 0.99), K = 2 * w * r + 1, off = 2 * w * r;
|
||||
std::vector<float> f((size_t)K);
|
||||
for (int64_t n = 0; n < K; ++n) {
|
||||
const float u = keep(keep((float)n / (float)r - (float)w) * 0.99f);
|
||||
const float uc = std::min(std::max(u, -6.0f), 6.0f);
|
||||
const float cw = std::cos(keep(keep(uc * (float)M_PI) / 6.0f) / 2.0f);
|
||||
const float pu = keep((float)M_PI * u);
|
||||
const float sinc = u == 0.0f ? 1.0f : std::sin(pu) / pu;
|
||||
f[(size_t)n] = narrow1(dt, keep(keep(sinc * keep(cw * cw)) * 0.99f) / (float)r);
|
||||
}
|
||||
const int64_t xs = laststride(X), rs = laststride(Rz), ys = laststride(Y);
|
||||
AVX_PARALLEL_FOR_IF(O * ch * K >= AVX_PAR_MIN)
|
||||
for (int64_t o = 0; o < O; ++o)
|
||||
for (int64_t h = 0; h < ch; ++h) {
|
||||
const int64_t u = o + off;
|
||||
float acc = 0.0f;
|
||||
for (int64_t k = u % r; k < K; k += r) {
|
||||
const int64_t i = (u - k) / r;
|
||||
const int64_t s = std::min(std::max<int64_t>(i - w, 0), S - 1);
|
||||
acc += keep(f[(size_t)k] * ldt(X, rowoff(X, s, ch) + h * xs));
|
||||
}
|
||||
const float up = narrow1(dt, (float)r * narrow1(dt, acc));
|
||||
const float v = narrow1(dt, ldt(Rz, rowoff(Rz, o, ch) + h * rs) + up);
|
||||
st_elem(Y->data, Y->dtype, rowoff(Y, o, ch) + h * ys, std::min(std::max(v, -1.0f), 1.0f));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
+141
-5
@@ -297,6 +297,13 @@ static const RadParamSpec pAttnDense[] = { P_INT("M"), P_INT("head_dim"), P_IN
|
||||
P_INTO("window_right") };
|
||||
static const RadOperandSpec oAttnDense[] = { OPD("q"), OPD("k"), OPD("v"), OPD("cu_seqlens"),
|
||||
OUT("out") };
|
||||
static const RadParamSpec pAttnCross[] = { P_INT("M"), P_INT("head_dim"), P_INT("gqa"),
|
||||
P_INT("causal"), P_STR("dtype"), P_F64O("scale") };
|
||||
static const RadOperandSpec oAttnCross[] = { OPD("q"), OPD("k"), OPD("v"), OPD("cu_seqlens"),
|
||||
OPD("cu_k"), OUT("out") };
|
||||
static const RadParamSpec pAttnNa3d[] = { P_INT("M"), P_INT("head_dim"), P_INT("kt"), P_INT("kh"),
|
||||
P_INT("kw"), P_STR("dtype"), P_F64O("scale") };
|
||||
static const RadOperandSpec oAttnNa3d[] = { OPD("q"), OPD("k"), OPD("v"), OPD("geom"), OUT("out") };
|
||||
|
||||
static const RadParamSpec pKvStore[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head_kv"),
|
||||
P_INT("block_size"), P_STR("kv_dtype"),
|
||||
@@ -504,6 +511,13 @@ static const RadOperandSpec oAllGather[] = { OPD("x"), OUT("y") };
|
||||
/* ---- image generation --------------------------------------------------------------------- */
|
||||
static const RadParamSpec pLnMod[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_STR("dtype") };
|
||||
static const RadOperandSpec oLnMod[] = { OPD("x"), OPD("s"), OPD("cu_seqlens"), OUT("y") };
|
||||
static const RadParamSpec pTableAdd[] = { P_INT("M"), P_INT("n"), P_INT("L"), P_STR("dtype") };
|
||||
static const RadOperandSpec oTableAdd[] = { OPD("x"), WGT("t"), OUT("y") };
|
||||
static const RadParamSpec pMixRows[] = { P_INT("M"), P_INT("n"), P_INT("k"), P_STR("dtype") };
|
||||
static const RadParamSpec pChanStats[] = { P_INT("M"), P_INT("n"), P_STR("mode"), P_INTO("var"),
|
||||
P_F64O("eps"), P_STR("dtype") };
|
||||
static const RadOperandSpec oChanStats[] = { OPD("x"), WGT("mean"), WGT("s"), OUT("y") };
|
||||
static const RadOperandSpec oMixRows[] = { OPD("x"), WGT("w"), OUT("y") };
|
||||
static const RadParamSpec pGateAdd[] = { P_INT("M"), P_INT("n"), P_STR("dtype"), P_STRO("act") };
|
||||
static const RadOperandSpec oGateAdd[] = { OPD("y"), OPD("g"), OPD("cu_seqlens"), INOUT("x") };
|
||||
static const RadParamSpec pLnModQ[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_INT("group"),
|
||||
@@ -511,12 +525,14 @@ static const RadParamSpec pLnModQ[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P
|
||||
static const RadOperandSpec oLnModQ[] = { OPD_O("y"), OPD_O("g"), OPD("s"), OPD("cu_seqlens"),
|
||||
INOUT("x"), OUT("q"), OUT("scale"), OUT_O("z") };
|
||||
static const RadParamSpec pRmsMod[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_INTO("gated"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oRmsMod[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT("w"),
|
||||
P_STR("dtype"), P_F64O("wadd"),
|
||||
P_INTO("norm_once"), P_STRO("norm") };
|
||||
static const RadOperandSpec oRmsMod[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT_O("w"),
|
||||
OPD("cu_seqlens"), INOUT("x"), OUT("z") };
|
||||
static const RadParamSpec pRmsModQ[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_INT("group"),
|
||||
P_INT("gated"), P_STR("dtype") };
|
||||
static const RadOperandSpec oRmsModQ[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT("w"),
|
||||
P_INT("gated"), P_STR("dtype"), P_F64O("wadd"),
|
||||
P_INTO("norm_once"), P_STRO("norm") };
|
||||
static const RadOperandSpec oRmsModQ[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT_O("w"),
|
||||
OPD("cu_seqlens"), INOUT("x"), OUT("q"), OUT("scale"),
|
||||
OUT_O("z") };
|
||||
static const RadOperandSpec oFlowX0[] = { OPD("v"), OPD("t"), OPD("sigma"), OPD("sigma_next"),
|
||||
@@ -590,7 +606,8 @@ static const RadOperandSpec oAttnVsa[] = { OPD("q"), OPD("k"), OPD("v"), OPD("se
|
||||
/* ---- the latent upscaler (avx_video.cpp), as libref declares it; patch_fold takes
|
||||
* patch_unfold's parameters and operands ------------------------------------------------------ */
|
||||
static const RadParamSpec pConv3d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
|
||||
P_INT("kt"), P_STR("dtype") };
|
||||
P_INT("kt"), P_STR("dtype"), P_STRO("tpad"),
|
||||
P_INTO("tpre") };
|
||||
static const RadOperandSpec oConv3d[] = { OPD("x"), WGT("w"), WGT("b"), OPD("geom"),
|
||||
OPD_O("res"), OUT("y") };
|
||||
static const RadParamSpec pGroupNorm[] = { P_INT("M"), P_INT("n"), P_INT("groups"), P_F64("eps"),
|
||||
@@ -604,6 +621,52 @@ static const RadParamSpec pFlowRenoise[] = { P_INT("M"), P_INT("n"), P_INT("stre
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oFlowRenoise[] = { OPD("sigma"), OPD("seed"), INOUT("x") };
|
||||
|
||||
/* ---- LTX-2 (avx_ltx.cpp) */
|
||||
static const RadParamSpec pRopeSplit[] = { P_INT("M"), P_INT("n_head"), P_INT("head_dim"),
|
||||
P_INT("dim"), P_INT("n_dims"), P_F64("theta"),
|
||||
P_STR("max"), P_INTO("head0"), P_STR("dtype") };
|
||||
static const RadOperandSpec oRopeSplit[] = { INOUT("x"), OPD("positions") };
|
||||
static const RadParamSpec pHeadGate[] = { P_INT("M"), P_INT("n_head"), P_INT("head_dim"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oHeadGate[] = { OPD("g"), INOUT("x") };
|
||||
static const RadParamSpec pLtxGuide[] = { P_INT("M"), P_INT("n"), P_STR("dtype") };
|
||||
static const RadOperandSpec oLtxGuide[] = { OPD("vc"), OPD_O("vu"), OPD_O("vs"), OPD_O("vm"),
|
||||
OPD("scales"), OPD("sched"), INOUT("stats"),
|
||||
INOUT("x") };
|
||||
static const RadParamSpec pPixelNorm[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_STR("form"),
|
||||
P_STRO("act"), P_INTO("ileave"), P_STR("dtype"),
|
||||
P_F64O("mult") };
|
||||
static const RadOperandSpec oPixelNorm[] = { OPD("x"), OUT("y") };
|
||||
static const RadParamSpec pD2s3d[] = { P_INT("M"), P_INT("C"), P_INT("st"), P_INT("sh"), P_INT("sw"),
|
||||
P_INTO("drop"), P_STR("dtype") };
|
||||
static const RadOperandSpec oD2s3d[] = { OPD("x"), OPD("geom"), OUT("y") };
|
||||
static const RadParamSpec pS2d3d[] = { P_INT("M"), P_INT("C"), P_INT("st"), P_INT("sh"), P_INT("sw"),
|
||||
P_INTO("tpre"), P_INTO("group"), P_STR("mode"),
|
||||
P_INTO("add"), P_STR("dtype") };
|
||||
static const RadOperandSpec oS2d3d[] = { OPD("x"), OPD("geom"), INOUT("y") };
|
||||
static const RadParamSpec pPixPatch[] = { P_INT("M"), P_INT("p"), P_INT("C"), P_STRO("order"),
|
||||
P_STR("dtype") };
|
||||
static const RadOperandSpec oPixPatch[] = { OPD("x"), OUT("y") };
|
||||
static const RadParamSpec pVolIndex[] = { P_INT("M") };
|
||||
static const RadOperandSpec oVolIndex[] = { OPD("geom"), OUT("y") };
|
||||
static const RadParamSpec pTileGrid[] = { P_INT("M"), P_INT("tile"), P_INT("stride"), P_STRO("rule"),
|
||||
P_INTO("min"), P_INTO("tile_t"), P_INTO("stride_t"),
|
||||
P_INTO("min_t"), P_INTO("tail") };
|
||||
static const RadOperandSpec oTileGrid[] = { OPD("geom"), OUT("y") };
|
||||
static const RadParamSpec pTileCrop[] = { P_INT("M"), P_INT("C"), P_STR("dtype") };
|
||||
static const RadOperandSpec oTileCrop[] = { OPD("x"), OPD("geom"), OPD("box"), OUT("y") };
|
||||
static const RadParamSpec pTileStitch[] = { P_INT("M"), P_INT("tile"), P_INT("stride"),
|
||||
P_STRO("rule"), P_INTO("min"), P_INTO("tile_t"),
|
||||
P_INTO("stride_t"), P_INTO("min_t"), P_INT("scale"),
|
||||
P_INTO("scale_t"), P_INT("p"), P_INT("C"),
|
||||
P_STRO("order"), P_STR("dtype") };
|
||||
static const RadOperandSpec oTileStitch[] = { OPD("x"), OPD("geom"), OPD("first"), OUT("y") };
|
||||
static const RadParamSpec pStftMel[] = { P_INT("M"), P_INT("hop"), P_INT("nfft"), P_INT("n_mel"),
|
||||
P_INT("ch"), P_STR("dtype") };
|
||||
static const RadOperandSpec oStftMel[] = { OPD("x"), WGT("basis"), WGT("mel"), OUT("y") };
|
||||
static const RadParamSpec pBweUp[] = { P_INT("M"), P_INT("ratio"), P_INT("ch"), P_STR("dtype") };
|
||||
static const RadOperandSpec oBweUp[] = { OPD("x"), OPD("res"), OUT("y") };
|
||||
|
||||
/* ---- audio generation (avx_audio.cpp), as libref declares it ------------------------------ */
|
||||
static const RadParamSpec pConv1d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
|
||||
P_INT("dil"), P_STRO("act"), P_STR("dtype"),
|
||||
@@ -664,6 +727,8 @@ static const RadParamSpec pIota[] = { P_INT("M"), P_INT("start"), P_INT("step"),
|
||||
static const RadOperandSpec oIota[] = { OUT("y") };
|
||||
static const RadParamSpec pCuRepeat[] = { P_INT("M"), P_INT("mult") };
|
||||
static const RadOperandSpec oCuRepeat[] = { OPD("len"), OUT("y") };
|
||||
static const RadParamSpec pIntAffine[] = { P_INT("M"), P_STR("src"), P_STR("mul"), P_STR("add") };
|
||||
static const RadOperandSpec oIntAffine[] = { OPD("x"), OUT("y") };
|
||||
|
||||
/* ---- vocabulary edges --------------------------------------------------------------------- */
|
||||
static const RadParamSpec pEmbed[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_vocab"),
|
||||
@@ -881,6 +946,11 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "attn_dense", ARR(pAttnDense), ARR(oAttnDense),
|
||||
"dense varlen attention over cu_seqlens; a non-causal window may reach further right "
|
||||
"(`window_right`)." },
|
||||
{ "attn_cross", ARR(pAttnCross), ARR(oAttnCross),
|
||||
"attn_dense with each entry's keys a run of their own (cu_k); causal counts from each run's "
|
||||
"start." },
|
||||
{ "attn_na3d", ARR(pAttnNa3d), ARR(oAttnNa3d),
|
||||
"3-D neighborhood attention over a volume, libref's window and arithmetic." },
|
||||
{ "kv_store", ARR(pKvStore), ARR(oKvStore), "scatter k and v into the paged cache." },
|
||||
{ "rope_table", ARR(pRopeTable), ARR(oRopeTable), "cos/sin at each position's own row." },
|
||||
{ "qsa_block_key", ARR(pQsaBlockKey), ARR(oQsaBlockKey),
|
||||
@@ -949,6 +1019,13 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "ln_mod", ARR(pLnMod), ARR(oLnMod),
|
||||
"y = bf16(bf16(LN(x)) * bf16(1 + s[seq])): a gainless layer norm and the row's entry's "
|
||||
"modulation row of `s` ([n_seq, >= n] at its own pitch)." },
|
||||
{ "table_add", ARR(pTableAdd), ARR(oTableAdd),
|
||||
"y[r, l n + c] = x[r, c] + t[l, c], f32 sum rounded once." },
|
||||
{ "chan_stats", ARR(pChanStats), ARR(oChanStats),
|
||||
"per-channel statistics held as weights: denorm rd(rd(x std) + mean), norm rd(rd(x - mean) / "
|
||||
"std)." },
|
||||
{ "mix_rows", ARR(pMixRows), ARR(oMixRows),
|
||||
"y[r, c] = sum_i w[i] x[r k + i, c], f32 in ascending i, rounded once." },
|
||||
{ "gate_add", ARR(pGateAdd), ARR(oGateAdd),
|
||||
"x = bf16(x + bf16(bf16(tanh(g[seq])) * y)), or with `act` none the gate g itself, bit for bit "
|
||||
"libref's." },
|
||||
@@ -1032,6 +1109,29 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "flow_renoise", ARR(pFlowRenoise), ARR(oFlowRenoise),
|
||||
"x = f32(f32(1 - s) x) + f32(s z), z the (seed, stream) normal stream at k n + c, bit for bit "
|
||||
"libref's." },
|
||||
{ "rope_split", ARR(pRopeSplit), ARR(oRopeSplit),
|
||||
"LTX-2's split rotary over fractional coordinates, libref's arithmetic element by element." },
|
||||
{ "head_gate", ARR(pHeadGate), ARR(oHeadGate),
|
||||
"x = rd(x rd(2 rd(sigmoid(g)))) per head, libref's." },
|
||||
{ "ltx_guide", ARR(pLtxGuide), ARR(oLtxGuide),
|
||||
"LTX-2's guided sampler step in f32, libref's arithmetic with the deviations in f64." },
|
||||
{ "pixel_norm", ARR(pPixelNorm), ARR(oPixelNorm),
|
||||
"an RMS norm over each row as bf16 tensors compute it, every step rounded; rsqrt or div form, "
|
||||
"optionally silu, `ileave` interleaving the output; libref's." },
|
||||
{ "d2s3d", ARR(pD2s3d), ARR(oD2s3d), "depth to space over voxel rows, libref's copy." },
|
||||
{ "s2d3d", ARR(pS2d3d), ARR(oS2d3d),
|
||||
"space to depth over voxel rows, a copy or the mean of each group, libref's." },
|
||||
{ "pix_patch", ARR(pPixPatch), ARR(oPixPatch), "an image to patch rows, libref's copy." },
|
||||
{ "patch_pix8", ARR(pPixPatch), ARR(oPixPatch), "patch rows to RGB bytes, libref's rule." },
|
||||
{ "vol_index", ARR(pVolIndex), ARR(oVolIndex), "a volume's voxel coordinates, libref's." },
|
||||
{ "tile_grid", ARR(pTileGrid), ARR(oTileGrid), "a tiled decode's boxes, libref's cut." },
|
||||
{ "tile_crop", ARR(pTileCrop), ARR(oTileCrop), "a tile out of a volume, libref's copy." },
|
||||
{ "tile_stitch", ARR(pTileStitch), ARR(oTileStitch),
|
||||
"decoded tiles blended to RGB bytes, libref's blends and rounding." },
|
||||
{ "stft_logmel", ARR(pStftMel), ARR(oStftMel),
|
||||
"each channel's causal log-mel spectrogram, libref's roundings." },
|
||||
{ "bwe_up", ARR(pBweUp), ARR(oBweUp),
|
||||
"a Hann-windowed sinc resampler plus a residual, clamped, libref's roundings." },
|
||||
{ "conv1d", ARR(pConv1d), ARR(oConv1d),
|
||||
"a 1-D convolution over the sequences `cu_seqlens` bounds, channels innermost, PyTorch's "
|
||||
"[cout, cin, k] weight, dilation `dil`, centred or (`causal`) all on the left, zeros outside "
|
||||
@@ -1073,6 +1173,8 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "interleave2", ARR(pInterleave), ARR(oInterleave2), "two streams interleaved, y [rows, 2]." },
|
||||
{ "iota", ARR(pIota), ARR(oIota), "y[i] = start + i * step." },
|
||||
{ "cu_repeat", ARR(pCuRepeat), ARR(oCuRepeat), "y[j] = j * len[0] * mult." },
|
||||
{ "int_affine", ARR(pIntAffine), ARR(oIntAffine),
|
||||
"y[j] = (src[j] < 0 ? 0 : x[src[j]]) * mul[j] + add[j] over three integer lists." },
|
||||
{ "embed_lookup", ARR(pEmbedM), ARR(oEmbed),
|
||||
"embedding gather, vocab-parallel with an offset; `mult` multiplies every row (rounded once)." },
|
||||
{ "embed_lookup_q", ARR(pEmbed), ARR(oEmbedQ),
|
||||
@@ -1382,6 +1484,11 @@ ROW("avx_attn_paged", "attn_paged", "attn",
|
||||
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])), L(attn_paged)),
|
||||
SIMPLE("avx_attn_dense", "attn_dense", "attn", "dense varlen attention over cu_seqlens", DT_FLOAT,
|
||||
L(attn_dense)),
|
||||
SIMPLE("avx_attn_cross", "attn_cross", "attn",
|
||||
"dense varlen attention, keys bounded apart from the queries (cu_k)", DT_FLOAT,
|
||||
L(attn_cross)),
|
||||
SIMPLE("avx_attn_na3d", "attn_na3d", "attn", "3-D neighborhood attention over a volume", DT_FLOAT,
|
||||
L(attn_na3d)),
|
||||
ROW("avx_kv_store", "kv_store", "attn", "scatter k and v into the paged cache", DT_ANY,
|
||||
cPagedBlock, (int)(sizeof(cPagedBlock) / sizeof(cPagedBlock[0])), L(kv_store)),
|
||||
SIMPLE("avx_rope", "rope", "attn", "rotary embedding in place", DT_FLOAT, L(rope)),
|
||||
@@ -1458,6 +1565,12 @@ SIMPLE("avx_ln_mod", "ln_mod", "norm", "gainless layer norm times (1 + an entry'
|
||||
DT_FLOAT, L(ln_mod)),
|
||||
SIMPLE("avx_gate_add", "gate_add", "elem", "x += act(gate row) * y, act tanh or none", DT_FLOAT,
|
||||
L(gate_add)),
|
||||
SIMPLE("avx_table_add", "table_add", "elem", "a row plus each of L table rows", DT_FLOAT,
|
||||
L(table_add)),
|
||||
SIMPLE("avx_mix_rows", "mix_rows", "elem", "a weighted sum of k consecutive rows", DT_FLOAT,
|
||||
L(mix_rows)),
|
||||
SIMPLE("avx_chan_stats", "chan_stats", "elem", "per-channel statistics held as weights",
|
||||
DT_FLOAT, L(chan_stats)),
|
||||
SIMPLE("avx_ln_mod_quant_fp8", "ln_mod_quant_fp8", "norm",
|
||||
"gated residual, adaptive norm and the E4M3 quantiser in one pass",
|
||||
"float in, fp8_e4m3 out, f32 scale, bf16 z", L(ln_mod_quant_fp8)),
|
||||
@@ -1526,6 +1639,27 @@ SIMPLE("avx_patch_fold", "patch_fold", "elem", "voxel rows to normalised patch r
|
||||
L(patch_fold)),
|
||||
SIMPLE("avx_flow_renoise", "flow_renoise", "elem", "a clean latent re-noised to a sigma",
|
||||
DT_FLOAT, L(flow_renoise)),
|
||||
SIMPLE("avx_rope_split", "rope_split", "elem", "LTX-2's split rotary over coordinates", DT_FLOAT,
|
||||
L(rope_split)),
|
||||
SIMPLE("avx_head_gate", "head_gate", "elem", "each head times 2 sigmoid of its logit", DT_FLOAT,
|
||||
L(head_gate)),
|
||||
SIMPLE("avx_ltx_guide", "ltx_guide", "elem", "LTX-2's guided sampler step", DT_FLOAT,
|
||||
L(ltx_guide)),
|
||||
SIMPLE("avx_pixel_norm", "pixel_norm", "norm", "an RMS norm as bf16 tensors compute it", DT_FLOAT,
|
||||
L(pixel_norm)),
|
||||
SIMPLE("avx_d2s3d", "d2s3d", "elem", "depth to space over voxel rows", DT_FLOAT, L(d2s3d)),
|
||||
SIMPLE("avx_s2d3d", "s2d3d", "elem", "space to depth over voxel rows", DT_FLOAT, L(s2d3d)),
|
||||
SIMPLE("avx_pix_patch", "pix_patch", "elem", "an image to patch rows", DT_FLOAT, L(pix_patch)),
|
||||
SIMPLE("avx_patch_pix8", "patch_pix8", "elem", "patch rows to RGB bytes", DT_FLOAT, L(patch_pix8)),
|
||||
SIMPLE("avx_vol_index", "vol_index", "elem", "a volume's voxel coordinates", DT_ANY, L(vol_index)),
|
||||
SIMPLE("avx_tile_grid", "tile_grid", "elem", "a tiled decode's boxes", DT_ANY, L(tile_grid)),
|
||||
SIMPLE("avx_tile_crop", "tile_crop", "elem", "a tile out of a volume", DT_FLOAT, L(tile_crop)),
|
||||
SIMPLE("avx_tile_stitch", "tile_stitch", "elem", "decoded tiles blended to RGB bytes", DT_FLOAT,
|
||||
L(tile_stitch)),
|
||||
SIMPLE("avx_stft_logmel", "stft_logmel", "elem", "a causal log-mel spectrogram", DT_FLOAT,
|
||||
L(stft_logmel)),
|
||||
SIMPLE("avx_bwe_up", "bwe_up", "elem", "a sinc-resampled waveform plus a residual, clamped",
|
||||
DT_FLOAT, L(bwe_up)),
|
||||
|
||||
SIMPLE("avx_conv1d", "conv1d", "conv", "a 1-D convolution over sequences, as GEMMs over patch rows",
|
||||
DT_FLOAT, L(conv1d)),
|
||||
@@ -1560,6 +1694,8 @@ SIMPLE("avx_interleave2", "interleave2", "elem", "two streams interleaved", DT_A
|
||||
L(interleave2)),
|
||||
SIMPLE("avx_iota", "iota", "elem", "an arithmetic index sequence", DT_ANY, L(iota)),
|
||||
SIMPLE("avx_cu_repeat", "cu_repeat", "elem", "bounds of equal sequences", DT_ANY, L(cu_repeat)),
|
||||
SIMPLE("avx_int_affine", "int_affine", "elem", "integers derived from device geometry", DT_ANY,
|
||||
L(int_affine)),
|
||||
|
||||
SIMPLE("avx_embed_lookup", "embed_lookup", "vocab", "embedding gather", DT_FLOAT, L(embed_lookup)),
|
||||
/* Its layout hook only validates: the table is read as stored and other encodings are refused
|
||||
|
||||
+16
-6
@@ -458,14 +458,21 @@ AVX_KERNEL(conv3d_ndhwc) {
|
||||
const RadTensor *X = &a->t[0], *W = &a->t[1], *B = &a->t[2], *G = &a->t[3];
|
||||
const RadTensor *R = rad_arg_in(a, 4), *Y = &a->t[5];
|
||||
const int64_t cin = p_int(a, "cin", 0), cout = p_int(a, "cout", 0);
|
||||
const int64_t k = p_int(a, "k", 0), kt = p_int(a, "kt", 0);
|
||||
if (cin <= 0 || cout <= 0 || k <= 0 || kt <= 0 || !(k & 1) || !(kt & 1) || numel(G) < 3)
|
||||
const int64_t k = p_int(a, "k", 0), kt = p_int(a, "kt", 0), tpre = p_int(a, "tpre", 0);
|
||||
/* The frames' padding (libref's): zero, the edge frame repeated, or causal with `tpre` more
|
||||
* copies of the first frame in front (T + tpre frames out). */
|
||||
const char* tps = p_str(a, "tpad", "zero");
|
||||
const int tpad = !std::strcmp(tps, "zero") ? 0 : !std::strcmp(tps, "replicate") ? 1
|
||||
: !std::strcmp(tps, "causal") ? 2 : -1;
|
||||
if (tpad < 0 || (tpre && tpad != 2)) return RAD_E_INVAL;
|
||||
if (cin <= 0 || cout <= 0 || k <= 0 || kt <= 0 || !(k & 1) || !(kt & 1) || tpre < 0 ||
|
||||
numel(G) < 3)
|
||||
return RAD_E_SHAPE;
|
||||
const int64_t T = at(G, 0), H = at(G, 1), Wd = at(G, 2);
|
||||
if (T <= 0 || H <= 0 || Wd <= 0) return RAD_E_SHAPE;
|
||||
const int64_t rows = T * H * Wd, taps = kt * k * k, KK = taps * cin;
|
||||
if (rows_of(Y, cout) < rows || rows_of(X, cin) < rows || (R && rows_of(R, cout) < rows) ||
|
||||
numel(W) < cout * KK || numel(B) < cout)
|
||||
const int64_t rows = (T + tpre) * H * Wd, taps = kt * k * k, KK = taps * cin;
|
||||
if (rows_of(Y, cout) < rows || rows_of(X, cin) < T * H * Wd ||
|
||||
(R && rows_of(R, cout) < rows) || numel(W) < cout * KK || numel(B) < cout)
|
||||
return RAD_E_SHAPE;
|
||||
static thread_local std::vector<uint8_t> wbuf, pbuf;
|
||||
static thread_local std::vector<float> ybuf;
|
||||
@@ -505,7 +512,10 @@ AVX_KERNEL(conv3d_ndhwc) {
|
||||
for (int64_t dz = 0; dz < kt; ++dz)
|
||||
for (int64_t kh = 0; kh < k; ++kh)
|
||||
for (int64_t kw = 0; kw < k; ++kw, d += (size_t)cin * ael) {
|
||||
const int64_t tz = t + dz - pz, iy = i + kh - ps, ix = j + kw - ps;
|
||||
int64_t tz = t + dz - pz;
|
||||
if (tpad == 1) tz = std::min<int64_t>(std::max<int64_t>(tz, 0), T - 1);
|
||||
else if (tpad == 2) tz = std::max<int64_t>(t + dz - (kt - 1) - tpre, 0);
|
||||
const int64_t iy = i + kh - ps, ix = j + kw - ps;
|
||||
if (tz < 0 || tz >= T || iy < 0 || iy >= H || ix < 0 || ix >= Wd) {
|
||||
std::memset(d, 0, (size_t)cin * ael);
|
||||
continue;
|
||||
|
||||
Reference in new issue
Block a user