merge 1.12.0: gemma4 E2B/E4B and Nemotron Bolt E4B arrive; the ref2va audio, rows and ABI work carried across

conv1d keeps both stride forms: upstream's causal codec downsample with cu_div (Fish Audio) and the port's explicit-pad non-causal one (the H3 audio encoder's), in libref, libavx and libr4d alike; the ABI takes upstream's 27 on top of the port's RadVideoGenBatch growth.
This commit is contained in:
gearwave00001 committed 2026-10-24 19:03:26 -05:00
commit 7c3b5ab7d5
285 files changed
+30500 -2898

No files matched your search

+173 -26
View File
@@ -51,8 +51,12 @@ inline int64_t ldi(const RadTensor* t, int64_t i) {
* them), outside of which the input reads zero (each sequence is padded on its own). w is
* PyTorch's Conv1d weight [cout, cin, k], b [cout], `dil` the dilation and the padding
* dil (k - 1) / 2 on each side (k odd), so a sequence keeps its length; with `causal` 1 all of it,
* dil (k - 1), on the left (any k). With `stride` > 1 - bf16 only, the encoder's downsample - the
* op is one sequence [0, rows of x], `pad` explicit and any k, and output row m reads
* dil (k - 1), on the left (any k). A `stride` s > 1 comes in two forms. A causal one (a codec
* encoder's downsampling, a reach of at least s) keeps every s-th output row: the left pad is the
* reach less s, and a sequence of L rows (every bound a whole number of strides) gives L / s, y's
* and res's rows the input's over s; `cu_div` reads the bounds in units that many input rows
* apart, as the other encoder ops do. A non-causal one with an explicit `pad` (bf16 only, the
* audio encoder's downsample) is one sequence [0, rows of x] with any k, and output row m reads
* x[m * stride + tap * dil - pad]: PyTorch's zero-padded strided conv, whose output length
* (rows + 2 pad - dil (k - 1) - 1) / stride + 1 the y operand must have exactly. `res`, when
* bound, is added after the bias; then `act` (none, tanh, or clamp to [-1, 1], a vocoder's output
@@ -64,37 +68,47 @@ int ref_conv1d(const RadArgs* a, RadStream) {
if (!y->data) return RAD_E_INVAL;
const int64_t cin = p_int(a, "cin", 0), cout = p_int(a, "cout", 0), k = p_int(a, "k", 0);
const int64_t dil = p_int(a, "dil", 1), causal = p_int(a, "causal", 0);
const int64_t st = p_int(a, "stride", 1), pp = p_int(a, "pad", -1);
const int64_t st = p_int(a, "stride", 1), pp = p_int(a, "pad", -1), cd = p_int(a, "cu_div", 1);
const char* act = p_str(a, "act", "none");
const bool tanh_out = act && !std::strcmp(act, "tanh");
const bool clamp_out = act && !std::strcmp(act, "clamp");
if (!tanh_out && !clamp_out && act && std::strcmp(act, "none")) return RAD_E_INVAL;
/* A stride comes in the causal form (no pad operand, cu_div bounds) or the zero-padded one
* with an explicit `pad` (bf16, one full sequence, PyTorch's formula) - never both. */
const bool padded = pp >= 0 && st > 1;
if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && st <= 1 && (k & 1) == 0) || dil <= 0 ||
causal < 0 || causal > 1 || st < 1 || (st > 1 && (causal || pp < 0)) ||
(st <= 1 && pp >= 0))
causal < 0 || causal > 1 || st < 1 || (pp >= 0 && (st <= 1 || causal || cd != 1)) ||
(st > 1 && pp < 0 && (!causal || dil * (k - 1) + 1 < st)) || cd < 1)
return RAD_E_SHAPE;
const int64_t n_seq = numel(cu) - 1;
if (n_seq <= 0) return RAD_E_SHAPE;
const int64_t r0 = cu_at(cu, 0), r1 = cu_at(cu, n_seq);
const int64_t cdv = padded ? 1 : cd;
/* A stride takes each sequence of L rows to L / stride, so every bound is a whole number of
* strides (of cu_div units each); the padded form bounds only its one full sequence. */
for (int64_t e = 0; !padded && e <= n_seq; ++e)
if (cu_at(cu, e) % (cd * st)) return RAD_E_SHAPE;
const int64_t r0 = cu_at(cu, 0) / cdv, r1 = cu_at(cu, n_seq) / cdv;
if (r0 < 0 || r1 <= r0 || rows_of(x, cin) < r1) return RAD_E_SHAPE;
if (numel(w) < cout * cin * k || (b && numel(b) < cout)) return RAD_E_SHAPE;
/* The strided form: one full sequence, bf16, and y exactly the formula's rows. */
int64_t hi = r1;
if (st > 1) {
/* The padded strided form: one full sequence, bf16, and y exactly the formula's rows. */
int64_t rmin = r0 / st, rmax = r1 / st;
if (padded) {
if (x->dtype != RAD_BF16) return RAD_E_DTYPE;
if (n_seq != 1 || r0 != 0 || r1 != rows_of(x, cin)) return RAD_E_SHAPE;
hi = (r1 + 2 * pp - dil * (k - 1) - 1) / st + 1;
if (hi <= 0 || rows_of(y, cout) != hi) return RAD_E_SHAPE;
} else if (rows_of(y, cout) < r1) {
return RAD_E_SHAPE;
rmin = 0;
rmax = (r1 + 2 * pp - dil * (k - 1) - 1) / st + 1;
}
if (res && rows_of(res, cout) < hi) return RAD_E_SHAPE;
const int64_t pad = pp >= 0 ? pp : (causal ? dil * (k - 1) : dil * (k - 1) / 2);
const int64_t lo = st > 1 ? 0 : r0; /* a strided op starts at row 0 of its one sequence */
REF_PAR_FOR((hi - lo) * cout * cin * k)
for (int64_t r = lo; r < hi; ++r) {
const int64_t e = seq_of(cu, n_seq, st > 1 ? r * st : r);
const int64_t s0 = cu_at(cu, e), s1 = cu_at(cu, e + 1);
if (rmin >= rmax) return RAD_E_SHAPE;
if (padded ? rows_of(y, cout) != rmax : rows_of(y, cout) < rmax) return RAD_E_SHAPE;
if (res && rows_of(res, cout) < rmax) return RAD_E_SHAPE;
/* Causal: the reach is dil (k - 1) + 1 input rows and the left pad that less the stride, so
* output row o of a sequence reads input rows o stride - pad .. o stride + stride - 1. The
* padded form takes PyTorch's zero padding from the explicit `pad` instead. */
const int64_t pad = padded ? pp : (causal ? dil * (k - 1) + 1 - st : dil * (k - 1) / 2);
REF_PAR_FOR((rmax - rmin) * cout * cin * k)
for (int64_t r = rmin; r < rmax; ++r) {
const int64_t e = seq_of(cu, n_seq, r * st * cdv);
const int64_t s0 = cu_at(cu, e) / cdv, s1 = cu_at(cu, e + 1) / cdv;
const int64_t yb = rowoff(y, r, cout), ys = laststride(y);
for (int64_t co = 0; co < cout; ++co) {
float acc = 0.0f;
@@ -273,6 +287,9 @@ int ref_snake_aa(const RadArgs* a, RadStream) {
* `cond_k` > 0 only the entries where c reaches its cond_k-th largest value are. Of the candidates
* the `k` largest g (ties to the lower index) are kept, divided by `temp`, softmaxed in f32 and
* drawn by inverse CDF in descending g with u = rad_philox_uniform_s(seed, 2^32 + frame, stream).
* With `top_p` < 1 the kept list is cut first where the cumulative probability of the whole
* candidate set's softmax over temp (summed in index order) passes top_p, as Fish Audio's sampler
* cuts it: a rank whose inclusive cumulative probability exceeds top_p is dropped, rank 0 never.
* The pick is written to all of the lane's `rep` slots of `ids`. logits [rep n, n_vocab] f32 or
* bf16; seed [n, 2]; frame [n]. */
int ref_cfg_topk_sample(const RadArgs* a, RadStream) {
@@ -284,8 +301,10 @@ int ref_cfg_topk_sample(const RadArgs* a, RadStream) {
const int64_t sup_lo = p_int(a, "sup_lo", 0), sup_hi = p_int(a, "sup_hi", 0);
const float cfg = p_f32(a, "cfg", 1.0f), temp = p_f32(a, "temp", 1.0f);
const float pen = p_f32(a, "penalty", 1.0f);
const double top_p = (double)p_f32(a, "top_p", 1.0f);
if (V <= 0 || K <= 0 || K > V || rep < 1 || rep > 2 || ck < 0 || ck > V || !(temp > 0) ||
sup_lo < 0 || sup_hi < sup_lo || sup_hi > V || !(pen > 0) || (!hist != !lane))
sup_lo < 0 || sup_hi < sup_lo || sup_hi > V || !(pen > 0) || (!hist != !lane) ||
!(top_p >= 0))
return RAD_E_SHAPE;
const int64_t n = numel(ids) / rep;
if (n <= 0 || rows_of(lg, V) < rep * n || numel(fr) < n || numel(seed) < 2 * n ||
@@ -336,13 +355,32 @@ int ref_cfg_topk_sample(const RadArgs* a, RadStream) {
if (open(j)) idx[(size_t)nc++] = j;
}
if (nc == 0) return RAD_E_SHAPE;
const int64_t kk = std::min(K, nc);
/* The whole candidate set's normaliser, in index order (idx still is), for top_p. */
double z = 0;
if (top_p < 1.0) {
float gm = g[(size_t)idx[0]];
for (int64_t t = 1; t < nc; ++t) gm = std::max(gm, g[(size_t)idx[(size_t)t]]);
const float m0 = gm / temp;
for (int64_t t = 0; t < nc; ++t)
z += std::exp((double)(g[(size_t)idx[(size_t)t]] / temp - m0));
}
int64_t kk = std::min(K, nc);
std::partial_sort(idx.begin(), idx.begin() + kk, idx.begin() + nc,
[&](int64_t p, int64_t q) {
return g[(size_t)p] > g[(size_t)q] ||
(g[(size_t)p] == g[(size_t)q] && p < q);
});
const float m = g[(size_t)idx[0]] / temp;
if (top_p < 1.0) {
double cum = 0;
int64_t keep = 1;
for (int64_t t = 0; t < kk; ++t) {
cum += std::exp((double)(g[(size_t)idx[(size_t)t]] / temp - m)) / z;
if (t > 0 && cum > top_p) break;
keep = t + 1;
}
kk = keep;
}
double tot = 0;
std::vector<double> e((size_t)kk);
for (int64_t t = 0; t < kk; ++t) {
@@ -378,24 +416,26 @@ int ref_rvq_embed(const RadArgs* a, RadStream) {
const int64_t D = p_int(a, "n_embd", 0), K = p_int(a, "n_codes", 0);
const int64_t voc = p_int(a, "vocab", 0), off0 = p_int(a, "off0", 0);
const float scale = p_f32(a, "scale", 1.0f);
const int64_t round1 = p_int(a, "round", 0);
if (D <= 0 || K < 1 || voc <= 0 || round1 < 0 || round1 > 1) return RAD_E_SHAPE;
const int64_t round1 = p_int(a, "round", 0), clamp = p_int(a, "clamp", 0);
if (D <= 0 || K < 1 || voc <= 0 || round1 < 0 || round1 > 1 || clamp < 0) return RAD_E_SHAPE;
const int64_t rows = rows_of(y, D);
if (rows <= 0 || numel(codes) < K * rows) return RAD_E_SHAPE;
const int64_t r0 = rows_of(t0, D), r1 = rows_of(t1, D);
/* `clamp`: a code above clamp - 1 reads row clamp - 1 (a negative one still reads nothing). */
auto cl = [clamp](int64_t c) { return clamp > 0 && c > clamp - 1 ? clamp - 1 : c; };
/* Code (k, r): a codebook's row at the plane's own pitch, a frame's code at its own stride. */
const int64_t kp = codes->rank >= 2 ? codes->stride[codes->rank - 2] : rows;
const int64_t rp = codes->rank >= 1 ? codes->stride[codes->rank - 1] : 1;
REF_PAR_FOR(rows * D * K)
for (int64_t r = 0; r < rows; ++r) {
const int64_t c0 = ld_int(codes->dtype, codes->data, r * rp) + off0;
const int64_t c0 = cl(ld_int(codes->dtype, codes->data, r * rp)) + off0;
if (c0 < 0 || c0 >= r0) continue;
std::vector<float> acc((size_t)D, 0.0f);
const int64_t b0 = rowoff(t0, c0, D), yb = rowoff(y, r, D);
if (round1)
for (int64_t d = 0; d < D; ++d) acc[(size_t)d] = ldt(t0, b0 + d * laststride(t0));
for (int64_t k = 1; k < K; ++k) {
const int64_t ck = ld_int(codes->dtype, codes->data, k * kp + r * rp) + (k - 1) * voc;
const int64_t ck = cl(ld_int(codes->dtype, codes->data, k * kp + r * rp)) + (k - 1) * voc;
if (ck < 0 || ck >= r1) continue;
const int64_t tb = rowoff(t1, ck, D);
for (int64_t d = 0; d < D; ++d) acc[(size_t)d] += ldt(t1, tb + d * laststride(t1));
@@ -409,6 +449,62 @@ int ref_rvq_embed(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------------ vq_encode */
/* One step of a residual vector quantiser with factorised, L2-normalised codes (DAC's
* VectorQuantize, as Fish Audio's codec encodes): per row of x [rows, n], e = in_w x + in_b
* (in_w [dim, n]); the code is the row of cb [bins, dim] of greatest cosine with e,
* (e / max(|e|, 1e-12)) . (cb_c / max(|cb_c|, 1e-12)), the lower index on a tie; then
* x -= out_w cb[code] + out_b (out_w [n, dim], the raw codebook row), rounded once into x. f32,
* every sum in index order. A chain of these over a codec's codebooks is its residual quantiser,
* each step quantising what the last left. */
int ref_vq_encode(const RadArgs* a, RadStream) {
if (!have(a, 7)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *iw = &a->t[1], *ib = &a->t[2], *cb = &a->t[3];
const RadTensor *ow = &a->t[4], *ob = &a->t[5], *codes = &a->t[6];
const int64_t n = p_int(a, "n", 0), dim = p_int(a, "dim", 0), bins = p_int(a, "bins", 0);
if (n <= 0 || dim <= 0 || bins <= 0) return RAD_E_SHAPE;
const int64_t rows = rows_of(x, n);
if (rows <= 0 || numel(codes) < rows || numel(iw) < dim * n || numel(ib) < dim ||
numel(cb) < bins * dim || numel(ow) < n * dim || numel(ob) < n)
return RAD_E_SHAPE;
/* The codebook normalised once, as F.normalize divides. */
std::vector<float> cn((size_t)(bins * dim));
for (int64_t c = 0; c < bins; ++c) {
float ss = 0.0f;
for (int64_t j = 0; j < dim; ++j) ss += ldt(cb, c * dim + j) * ldt(cb, c * dim + j);
const float nrm = std::max(std::sqrt(ss), 1e-12f);
for (int64_t j = 0; j < dim; ++j) cn[(size_t)(c * dim + j)] = ldt(cb, c * dim + j) / nrm;
}
REF_PAR_FOR(rows * (dim * n + bins * dim + n * dim))
for (int64_t r = 0; r < rows; ++r) {
const int64_t xb = rowoff(x, r, n), xs = laststride(x);
std::vector<float> e((size_t)dim);
float ss = 0.0f;
for (int64_t j = 0; j < dim; ++j) {
float acc = 0.0f;
for (int64_t d = 0; d < n; ++d) acc += ldt(iw, j * n + d) * ldt(x, xb + d * xs);
e[(size_t)j] = acc + ldt(ib, j);
ss += e[(size_t)j] * e[(size_t)j];
}
const float nrm = std::max(std::sqrt(ss), 1e-12f);
for (int64_t j = 0; j < dim; ++j) e[(size_t)j] /= nrm;
int64_t best = 0;
float bs = -INFINITY;
for (int64_t c = 0; c < bins; ++c) {
float s = 0.0f;
for (int64_t j = 0; j < dim; ++j) s += e[(size_t)j] * cn[(size_t)(c * dim + j)];
if (s > bs) { bs = s; best = c; }
}
st_int(codes->dtype, codes->data, offlin(codes, r), best);
for (int64_t d = 0; d < n; ++d) {
float q = 0.0f;
for (int64_t j = 0; j < dim; ++j) q += ldt(ow, d * dim + j) * ldt(cb, best * dim + j);
stt(x, xb + d * xs, ldt(x, xb + d * xs) - (q + ldt(ob, d)));
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ frame_mix_store */
/* A frame's conditioning row, stored into its lane: m = bf16(bf16(sum_l w_l h_l) * bf16(scale))
* with w = bf16(softmax(bf16(logits))) over the `L` hidden planes of hid [L, rows, n_embd], row
@@ -454,6 +550,44 @@ int ref_frame_mix_store(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------------ ras_pick */
/* Fish Audio's repetition-aware sampling, the choice between two draws a lane: the first draw
* ids[rep i] = first[rep i], unless it lies in [lo, hi) and the lane's window (its last `win`
* picks, `hist` [lanes, win], oldest first, -1 empty) holds it, in which case the alternative
* draw alt[rep i] is taken. The pick then enters the window, its oldest entry leaving. `clear` 1
* empties the lane's window first (a piece's first pick). A pick outside [lo, hi) (Fish's end of
* speech) is never redrawn. */
int ref_ras_pick(const RadArgs* a, RadStream) {
if (!have(a, 5)) return RAD_E_INVAL;
const RadTensor *first = &a->t[0], *alt = &a->t[1], *lane = &a->t[2], *hist = &a->t[3];
const RadTensor* ids = &a->t[4];
const int64_t W = p_int(a, "win", 0), lo = p_int(a, "lo", 0), hi = p_int(a, "hi", 0);
const int64_t rep = p_int(a, "rep", 1), clear = p_int(a, "clear", 0);
const int64_t n = numel(lane);
if (W <= 0 || lo < 0 || hi < lo || rep < 1 || clear < 0 || clear > 1 || n <= 0 ||
numel(first) < rep * n || numel(alt) < rep * n || numel(ids) < rep * n)
return RAD_E_SHAPE;
for (int64_t i = 0; i < n; ++i) {
const int64_t L = ldi(lane, i);
if (L < 0 || (L + 1) * W > numel(hist)) return RAD_E_SHAPE;
}
for (int64_t i = 0; i < n; ++i) {
const int64_t hb = ldi(lane, i) * W;
if (clear)
for (int64_t w = 0; w < W; ++w) st_int(hist->dtype, hist->data, offlin(hist, hb + w), -1);
const int64_t f = ldi(first, rep * i);
int64_t pick = f;
if (f >= lo && f < hi)
for (int64_t w = 0; w < W; ++w)
if (ldi(hist, hb + w) == f) { pick = ldi(alt, rep * i); break; }
for (int64_t r = 0; r < rep; ++r) st_int(ids->dtype, ids->data, offlin(ids, rep * i + r), pick);
for (int64_t w = 0; w + 1 < W; ++w)
st_int(hist->dtype, hist->data, offlin(hist, hb + w), ldi(hist, hb + w + 1));
st_int(hist->dtype, hist->data, offlin(hist, hb + W - 1), pick);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ end_status */
/* A frame's status per lane from its first pick: status[lane[i]] = -1 when ids[rep i] is `end`
* (the model ended the piece), else the pick. */
@@ -763,6 +897,19 @@ int ref_int_affine(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------------ sub_i32 */
/* y[i] = x[i] - s[0], int32: a device length less a device offset. */
int ref_sub_i32(const RadArgs* a, RadStream) {
if (!have(a, 3)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *s = &a->t[1], *y = &a->t[2];
const int64_t n = numel(y);
if (n <= 0 || numel(x) < n || numel(s) < 1) return RAD_E_SHAPE;
const int64_t s0 = ldi(s, 0);
for (int64_t i = 0; i < n; ++i)
st_int(y->dtype, y->data, offlin(y, i), (int64_t)(int32_t)(ldi(x, i) - s0));
return RAD_OK;
}
/* ------------------------------------------------------------------ cu_repeat */
/* Sequence bounds of equal sequences: y[j] = j * len[0] * mult over y's elements. A pass's
* lengths live on the device (a window's frames, the latents a vocoder decodes per channel group),