diff --git a/docs/OPS.md b/docs/OPS.md index 911623b2..f072e385 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -1167,12 +1167,13 @@ libref function says which (`libref/ref_audio.cpp`). | op | params | operands | |---|---|---| -| `conv1d` | `M`(range) `cin` `cout` `k` `dil` `act`/opt `dtype` `causal`/opt | `x`, `w`w, `cu_seqlens`, `res`? → `y`; `b`w? | +| `conv1d` | `M`(range) `cin` `cout` `k` `dil` `act`/opt `dtype` `causal`/opt `stride`/opt `cu_div`/opt | `x`, `w`w, `cu_seqlens`, `res`? → `y`; `b`w? | | `conv_t1d` | `M`(range) `cin` `cout` `stride` `dtype` `pad`/opt `k`/opt | `x`, `w`w, `b`w, `cu_seqlens` → `y` | | `snake` | `M`(range) `n` `dtype` `log`/opt | `x`, `alpha`w → `y`; `beta`w? | | `snake_aa` | `M`(range) `n` `ratio` `k` `log`/opt `dtype` | `x`, `alpha`w, `beta`w, `f_up`w, `f_down`w, `cu_seqlens` → `y` | | `cfg_topk_sample` | `M`(range) `n_vocab` `k` `rep` `cond_k`/opt `cfg`:f64 `temp`:f64/opt `stream` `dtype` `sup_lo`/opt `sup_hi`/opt `penalty`:f64/opt `top_p`:f64/opt | `logits`, `seed`, `frame` → `ids`; `cfg`?, `lane`?, `hist`:inout? | | `rvq_embed` | `M`(range) `n_embd` `n_codes` `vocab` `off0` `scale`:f64 `dtype` `round`/opt `clamp`/opt | `codes`, `t0`w, `t1`w → `y` | +| `vq_encode` | `M`(range) `n` `dim` `bins` `dtype` | `x`:inout, `in_w`w, `in_b`w, `cb`w, `out_w`w, `out_b`w → `codes` | | `frame_mix_store` | `M`(range) `n_embd` `L` `stride` `rep`/opt `dtype` | `hid`, `logits`w, `scale`w, `lane`, `frame`, `status`, `dst`:inout | | `end_status` | `M`(range) `end` `rep` | `ids`, `lane`, `status`:inout | | `ras_pick` | `M`(range) `win` `lo` `hi` `rep`/opt `clear`/opt | `first`, `alt`, `lane`, `hist`:inout → `ids` | @@ -1219,6 +1220,14 @@ conditional first, then the unconditional) are consecutive rows, `rep` of them a With `clamp` n > 0 a code above `n - 1` reads row `n - 1` (before `off0` and the codebook's offset; a negative code still reads nothing): a codec whose codebooks are smaller than the heads that write its codes, as Fish Audio's 1024-entry residual codebooks under 4096-way heads. +- `vq_encode`: one step of a residual vector quantiser with factorised, L2-normalised codes (DAC's, + as Fish Audio's codec encodes a reference): per row of `x` `[rows, n]`, `e = in_w x + in_b` + (`in_w` `[dim, n]`, a 1x1 convolution's weight); 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 (0 when no score is above -inf), written to `codes[i]` at the operand's own stride; then + `x -= out_w cb[code] + out_b` (`out_w` `[n, dim]`, the raw codebook row), rounded once into `x`. + f32 arithmetic. A chain of these over a codec's codebooks, each quantising what the last left, is + its residual quantiser. - `frame_store`: `dst[(lane n_codes + k) stride + frame - skip] = codes[k, rep i]` for every `k`, when `frame >= skip` and `status[lane] >= 0`: a frame's codes into its lane's history, each codebook a row over the lane's frames, so a run of a lane's frames is a block of `n_codes` @@ -1259,7 +1268,14 @@ input reads zero outside it. - `conv1d`: PyTorch's `[cout, cin, k]` weight, `k` odd, dilation `dil` and padding `dil (k - 1) / 2` a side, or with `causal` 1 (any `k`) `dil (k - 1)` on the left; `res` is added after the bias, then `act` `none`, `tanh` or `clamp`, `min(max(y, -1), 1)` (BigVGAN's output - clamp; a NaN stays NaN). f32 accumulation, one rounding. + clamp; a NaN stays NaN). f32 accumulation, one rounding. `stride` s > 1 (causal only, the reach + `dil (k - 1) + 1` at least s) pads `dil (k - 1) + 1 - s` on the left and keeps every s-th row: + output row `o` of a sequence reads input rows `o s - pad + t dil`, and sequence `e`'s rows + `[cu[e], cu[e+1])` become `[cu[e] / s, cu[e+1] / s)`, so every bound is a multiple of s; `res` + and `y` have the output's rows (a causal codec encoder's downsampling, as Fish Audio's). + `cu_div` reads the bounds in units that many input rows apart, as the other encoder ops do: + sequence `e` is input rows `[cu[e] / cu_div, cu[e+1] / cu_div)`, each bound a multiple of + `cu_div` times the stride, so one bounds array in samples serves every rate of an encoder. - `conv_t1d`: PyTorch's `[cin, cout, k]` transposed weight, any `k` of at least a stride (two strides by default), padding `pad` (`(k - stride) / 2` by default, which needs `k - stride` even; 0 is the causal form, whose full output's last `k - stride` rows are dropped; it may exceed a diff --git a/libavx/avx_audio.cpp b/libavx/avx_audio.cpp index e5ae6799..b59d4805 100644 --- a/libavx/avx_audio.cpp +++ b/libavx/avx_audio.cpp @@ -227,7 +227,10 @@ inline float sin_sq1(float v) { * k input rows' channel runs back to back, zeros outside its own sequence (padded on both sides, * or with `causal` on the left only). A band's patches are one gemm_nt_into call into f32 rows (the * bias added there); `res` and the tanh or the clamp to [-1, 1] are applied to the f32 rows, which - * are rounded once into y. A 1-wide kernel over a dense x reads x itself as the patches. */ + * are rounded once into y. A 1-wide kernel over a dense x reads x itself as the patches. With a + * `stride` (causal only) output row o's patch starts at input row o stride - pad, the pad the + * reach less the stride, and a sequence of L rows makes L / stride. `cu_div`: the bounds in units + * that many input rows apart, as libref reads them. */ AVX_KERNEL(conv1d) { if (!rad_args_have(a, 3) || a->n_t < 5) return RAD_E_INVAL; const RadTensor *X = &a->t[0], *W = &a->t[1], *cu = &a->t[2]; @@ -235,20 +238,26 @@ AVX_KERNEL(conv1d) { 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), 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; if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0) || dil <= 0 || - causal < 0 || causal > 1) + causal < 0 || causal > 1 || st < 1 || (st > 1 && (!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); - if (r0 < 0 || r1 <= r0 || rows_of(Y, cout) < r1 || rows_of(X, cin) < r1) return RAD_E_SHAPE; + for (int64_t e = 0; e <= n_seq; ++e) + if (cu_at(cu, e) % (cd * st)) return RAD_E_SHAPE; + /* r0, r1: the output rows, the input's over the stride. */ + const int64_t r0 = cu_at(cu, 0) / (cd * st), r1 = cu_at(cu, n_seq) / (cd * st); + if (r0 < 0 || r1 <= r0 || rows_of(Y, cout) < r1 || rows_of(X, cin) < r1 * st) + return RAD_E_SHAPE; if (numel(W) < cout * cin * k || (B && numel(B) < cout)) return RAD_E_SHAPE; if (res && rows_of(res, cout) < r1) return RAD_E_SHAPE; - const int64_t pad = causal ? dil * (k - 1) : dil * (k - 1) / 2, KK = k * cin; + const int64_t pad = causal ? dil * (k - 1) + 1 - st : dil * (k - 1) / 2, KK = k * cin; static thread_local std::vector wbuf, pbuf; static thread_local std::vector ybuf; @@ -264,8 +273,8 @@ AVX_KERNEL(conv1d) { const uint32_t adt = b16 ? (uint32_t)RAD_BF16 : (uint32_t)RAD_F32; const size_t ael = b16 ? 2 : 4; /* A 1-wide kernel's patch row is x's own row. */ - const bool direct = k == 1 && X->rank == 2 && X->shape[1] == cin && X->stride[1] == 1 && - (X->dtype == RAD_BF16 || X->dtype == RAD_F32); + const bool direct = k == 1 && st == 1 && X->rank == 2 && X->shape[1] == cin && + X->stride[1] == 1 && (X->dtype == RAD_BF16 || X->dtype == RAD_F32); const int64_t band = band_rows(KK, cout, r1 - r0); if (!direct) pbuf.resize((size_t)(band * KK) * ael + 64); ybuf.resize((size_t)(band * cout) + 16); @@ -282,10 +291,10 @@ AVX_KERNEL(conv1d) { pt = mat_view(pp, adt, nb, KK); AVX_PARALLEL_FOR_IF(nb * KK >= AVX_PAR_MIN) for (int64_t p = 0; p < nb; ++p) { - const int64_t r = b0 + p, e = seq_of(cu, n_seq, r); - const int64_t s0 = cu_at(cu, e), s1 = cu_at(cu, e + 1); + const int64_t r = b0 + p, e = seq_of(cu, n_seq, r * st * cd); + const int64_t s0 = cu_at(cu, e) / cd, s1 = cu_at(cu, e + 1) / cd; for (int64_t t = 0; t < k; ++t) { - const int64_t ir = r + t * dil - pad; + const int64_t ir = r * st + t * dil - pad; patch_slot(X, ir, cin, ir >= s0 && ir < s1, b16, pp + (size_t)(p * KK + t * cin) * ael); } @@ -757,6 +766,70 @@ AVX_KERNEL(rvq_embed) { return RAD_OK; } +/* ------------------------------------------------------------------ vq_encode */ +/* libref's step a row at a time: e as dim dot products over x's row, the codebook normalised once + * a launch (F.normalize's division) and scored against e normalised, ties to the lower index, the + * chosen raw row's out-projection subtracted and the row rounded once into x. */ +AVX_KERNEL(vq_encode) { + if (!rad_args_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 weights dense in f32, held by this thread for the launch's parallel region. */ + auto dense = [](const RadTensor* t, int64_t m) { + std::vector v((size_t)m); + for (int64_t i = 0; i < m; ++i) v[(size_t)i] = ldt(t, i); + return v; + }; + const std::vector wi = dense(iw, dim * n), bi = dense(ib, dim); + const std::vector raw = dense(cb, bins * dim), wo = dense(ow, n * dim), bo = dense(ob, n); + std::vector cn((size_t)(bins * dim)); + for (int64_t c = 0; c < bins; ++c) { + const float* cr = raw.data() + c * dim; + float ss = 0.0f; + for (int64_t j = 0; j < dim; ++j) ss += cr[j] * cr[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)] = cr[j] / nrm; + } + const float *Wi = wi.data(), *Bi = bi.data(), *Raw = raw.data(), *Cn = cn.data(); + const float *Wo = wo.data(), *Bo = bo.data(); + AVX_PARALLEL_FOR_IF(rows * (2 * n + bins) * dim >= AVX_PAR_MIN) + for (int64_t r = 0; r < rows; ++r) { + const int64_t xb = rowoff(X, r, n); + const float* xv = in_row(X, xb, n, scratch_f32((size_t)n + 16)); + float* e = scratch_f32_c((size_t)dim + 16); + float ss = 0.0f; + for (int64_t j = 0; j < dim; ++j) { + e[j] = dotf(Wi + j * n, xv, n) + Bi[j]; + ss += e[j] * e[j]; + } + const float nrm = std::max(std::sqrt(ss), 1e-12f); + for (int64_t j = 0; j < dim; ++j) e[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[j] * Cn[c * dim + j]; + if (s > bs) { bs = s; best = c; } + } + st_int_dt(codes->dtype, codes->data, offlin(codes, r), best); + float* y = scratch_f32_b((size_t)n + 16); + const float* row = Raw + best * dim; + for (int64_t d = 0; d < n; ++d) { + float q = 0.0f; + for (int64_t j = 0; j < dim; ++j) q += Wo[d * dim + j] * row[j]; + y[d] = xv[d] - (q + Bo[d]); + } + out_row(X, xb, n, y); + } + return RAD_OK; +} + /* ------------------------------------------------------------------ frame_mix_store */ /* m = bf16(bf16(sum_l w_l h_l) * bf16(scale)), w = bf16(softmax(bf16(logits))) in libref's double * arithmetic, the planes summed in order with each product rounded before its add (`keep`). Entries diff --git a/libavx/avx_cases.cpp b/libavx/avx_cases.cpp index 315f00eb..82717a48 100644 --- a/libavx/avx_cases.cpp +++ b/libavx/avx_cases.cpp @@ -954,6 +954,13 @@ std::vector build_cases() { for (int dil : { 1, 3 }) add("conv1d", { HI("M", 24), HI("cin", 20), HI("cout", 13), HI("k", 7), HI("dil", dil), HI("causal", 1), HS("dtype", dt) }); + /* Strided, causal: Fish Audio's codec encoder downsamples with kernels of two strides + * (2, 4, 8) and its quantiser with a kernel of one (k 2, stride 2). */ + for (int s : { 2, 4, 8 }) + add("conv1d", { HI("M", 64), HI("cin", 20), HI("cout", 13), HI("k", 2 * s), + HI("dil", 1), HI("causal", 1), HI("stride", s), HS("dtype", dt) }); + add("conv1d", { HI("M", 64), HI("cin", 24), HI("cout", 16), HI("k", 2), HI("dil", 1), + HI("causal", 1), HI("stride", 2), HS("dtype", dt) }); for (int s : { 2, 4, 8 }) add("conv_t1d", { HI("M", 12), HI("cin", 20), HI("cout", 13), HI("stride", s), HS("dtype", dt) }); @@ -1050,6 +1057,16 @@ std::vector build_cases() { add("ras_pick", { HI("M", 4), HI("win", 16), HI("lo", 1), HI("hi", 9), HI("rep", 1) }); add("ras_pick", { HI("M", 2), HI("win", 4), HI("lo", 0), HI("hi", 12), HI("rep", 2), HI("clear", 1) }); + /* A codec encoder's residual quantiser step: DAC's eight-wide codes over a semantic codebook + * and a residual one, both dtypes; a dimension that is no vector's width. */ + for (const char* dt : { "f32", "bf16" }) { + add("vq_encode", { HI("M", 7), HI("n", 64), HI("dim", 8), HI("bins", 4096), + HS("dtype", dt) }); + add("vq_encode", { HI("M", 5), HI("n", 128), HI("dim", 8), HI("bins", 1024), + HS("dtype", dt) }); + } + add("vq_encode", { HI("M", 3), HI("n", 40), HI("dim", 5), HI("bins", 33), + HS("dtype", "f32") }); add("frame_store", { HI("M", 2), HI("n_codes", 16), HI("stride", 9), HI("rep", 2), HI("skip", 0) }); add("frame_store", { HI("M", 4), HI("n_codes", 3), HI("stride", 5), HI("rep", 1), diff --git a/libavx/avx_check_hand.cpp b/libavx/avx_check_hand.cpp index dc50cbc5..01c3e195 100644 --- a/libavx/avx_check_hand.cpp +++ b/libavx/avx_check_hand.cpp @@ -4010,6 +4010,22 @@ void audio_cases(std::vector& hs) { .pi("causal", 1).ps("dtype", dn); hs.push_back(std::move(h)); } + for (const int64_t s : { 2, 4 }) { + /* A codec encoder's downsampling: kernel 2 stride, causal, the bounds in samples + * (cu_div 8 input rows), two clips of 4 and 2 strides from input row 2 s. */ + const int64_t cin = 12, cout = 9, M = 8 * s; + Hand h(("stride " + std::to_string(s) + ", bounds in units of 8 rows" + sfx).c_str(), + "conv1d", norm_tol); + h.dense(dt, { M, cin }, hfill(330 + s)) + .dense(dt, { cout, cin, 2 * s }, hfill(334, 0.3f)) + .idx({ (int32_t)(16 * s), (int32_t)(48 * s), (int32_t)(64 * s) }) + .none() + .dense(dt, { M / s, cout }, hfill(335), true) + .dense(dt, { cout }, hfill(336, 0.5f)) + .pi("M", M).pi("cin", cin).pi("cout", cout).pi("k", 2 * s).pi("dil", 1) + .pi("causal", 1).pi("stride", s).pi("cu_div", 8).ps("dtype", dn); + hs.push_back(std::move(h)); + } for (const int64_t s : { 5, 3 }) { /* A codec's odd strides at kernel 2 stride, causal, over sequences of 3, 1 and 5 rows * from row 1. */ diff --git a/libavx/avx_isa.h b/libavx/avx_isa.h index 09b53658..a2feb9bf 100644 --- a/libavx/avx_isa.h +++ b/libavx/avx_isa.h @@ -135,6 +135,7 @@ int avx_par_force(int threads); 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(int_affine) X(ras_pick) \ + X(vq_encode) \ /* ---- 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) \ diff --git a/libavx/avx_registry.cpp b/libavx/avx_registry.cpp index cc0eb1e9..ae4cedf6 100644 --- a/libavx/avx_registry.cpp +++ b/libavx/avx_registry.cpp @@ -683,7 +683,8 @@ 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"), - P_INTO("causal") }; + P_INTO("causal"), P_INTO("stride"), + P_INTO("cu_div") }; static const RadOperandSpec oConv1d[] = { OPD("x"), WGT("w"), OPD("cu_seqlens"), OPD_O("res"), OUT("y"), WGT_O("b") }; static const RadParamSpec pConvT1d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("stride"), @@ -707,6 +708,10 @@ static const RadParamSpec pRvqEmbed[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_ P_INT("vocab"), P_INT("off0"), P_F64("scale"), P_STR("dtype"), P_INTO("round"), P_INTO("clamp") }; static const RadOperandSpec oRvqEmbed[] = { OPD("codes"), WGT("t0"), WGT("t1"), OUT("y") }; +static const RadParamSpec pVqEncode[] = { P_INT("M"), P_INT("n"), P_INT("dim"), P_INT("bins"), + P_STR("dtype") }; +static const RadOperandSpec oVqEncode[] = { INOUT("x"), WGT("in_w"), WGT("in_b"), WGT("cb"), + WGT("out_w"), WGT("out_b"), OUT("codes") }; static const RadParamSpec pFrameMix[] = { P_INT("M"), P_INT("n_embd"), P_INT("L"), P_INT("stride"), P_INTO("rep"), P_STR("dtype") }; static const RadOperandSpec oFrameMix[] = { OPD("hid"), WGT("logits"), WGT("scale"), OPD("lane"), @@ -1186,6 +1191,9 @@ static const RadOpSchema kSchemas[] = { "libref's." }, { "rvq_embed", ARR(pRvqEmbed), ARR(oRvqEmbed), "a frame's embedding from code-major residual codes, libref's roundings (`round` 1: one)." }, +{ "vq_encode", ARR(pVqEncode), ARR(oVqEncode), + "one residual vector quantiser step: the code of greatest cosine with in_w x + in_b, then " + "x -= out_w cb[code] + out_b; libref's arithmetic in another summation order." }, { "frame_mix_store", ARR(pFrameMix), ARR(oFrameMix), "a frame's softmax-weighted mix of hidden planes, stored into its lane, bit for bit libref's." }, { "end_status", ARR(pEndStatus), ARR(oEndStatus), @@ -1718,6 +1726,8 @@ SIMPLE("avx_cfg_topk_sample", "cfg_topk_sample", "sample", "a guided top-k pick L(cfg_topk_sample)), SIMPLE("avx_rvq_embed", "rvq_embed", "elem", "a frame's embedding from its residual codes", DT_FLOAT, L(rvq_embed)), +SIMPLE("avx_vq_encode", "vq_encode", "elem", + "a residual quantiser's step: the nearest code by cosine", DT_FLOAT, L(vq_encode)), SIMPLE("avx_frame_mix_store", "frame_mix_store", "elem", "a frame's conditioning row into its lane", DT_FLOAT, L(frame_mix_store)), SIMPLE("avx_end_status", "end_status", "elem", "a frame's status from its first pick", DT_ANY, diff --git a/libr4d/r4d_audio.hip b/libr4d/r4d_audio.hip index 0d4ecdf5..ca9c65b8 100644 --- a/libr4d/r4d_audio.hip +++ b/libr4d/r4d_audio.hip @@ -56,7 +56,7 @@ __device__ __forceinline__ uint64_t seed_of(const uint32_t* seed, int e) { // ---------------------------------------------------------------------------- conv1d // An implicit GEMM, r4d_gen.hip's conv_igemm_kernel cut to one dimension: y[row][cout] = sum -// over (tap, cin) of x[row + tap * dil - pad][cin] * w[cout][tap][cin], the weight stored +// over (tap, cin) of x[row * stride + tap * dil - pad][cin] * w[cout][tap][cin], the weight stored // tap-major (r4d_layout.cpp relayouts PyTorch's [cout][cin][k] at load), so a 64-wide K tile is // eight 16-byte reads of input rows. A chunk outside its row's sequence reads zeros, which is the // per-sequence padding. Eight waves, BP rows x BC output channels a block, the next K tile's @@ -77,7 +77,8 @@ __global__ __launch_bounds__(256) void conv1d_igemm_kernel( const uint16_t* __restrict__ x, const uint16_t* __restrict__ w, const uint16_t* __restrict__ bias, const uint16_t* __restrict__ res, const int* __restrict__ cu, uint16_t* __restrict__ y, int n_seq, int r_beg, int r_end, - int cin, int cout, int k, int dil, int pad, int x_ld, int y_ld, int res_ld, int act) { + int cin, int cout, int k, int dil, int pad, int stride, int cdiv, int x_ld, int y_ld, + int res_ld, int act) { constexpr int BP = 2 * TP * 16, BC = 4 * TC * 16; constexpr int CPR = C1_BK / 8; constexpr int AL = BP * CPR / 256, WL = BC * CPR / 256; @@ -99,9 +100,9 @@ __global__ __launch_bounds__(256) void conv1d_igemm_kernel( const int m = p0 + r; a_ok[i] = m < r_end; a_row[i] = a_ok[i] ? m : r_beg; - const int sq = seq_of(cu, n_seq, a_row[i]); - a_s0[i] = cu[sq]; - a_s1[i] = cu[sq + 1]; + const int sq = seq_of(cu, n_seq, a_row[i] * stride * cdiv); + a_s0[i] = cu[sq] / cdiv; + a_s1[i] = cu[sq + 1] / cdiv; a_t[i] = e / cin; a_ci[i] = e % cin; adst[i] = sA + r * C1_PITCH + e; @@ -122,7 +123,7 @@ __global__ __launch_bounds__(256) void conv1d_igemm_kernel( const r4d_u32x4 zero = {0u, 0u, 0u, 0u}; auto loadA = [&](int i) -> r4d_u32x4 { if (!a_ok[i] || a_t[i] >= k) return zero; - const int ir = a_row[i] + a_t[i] * dil - pad; + const int ir = a_row[i] * stride + a_t[i] * dil - pad; if (ir < a_s0[i] || ir >= a_s1[i]) return zero; return *(const r4d_u32x4*)(x + (size_t)ir * x_ld + a_ci[i]); }; @@ -214,17 +215,18 @@ __global__ __launch_bounds__(kT) void conv1d_direct_kernel( const uint16_t* __restrict__ x, const uint16_t* __restrict__ w, const uint16_t* __restrict__ bias, const uint16_t* __restrict__ res, const int* __restrict__ cu, uint16_t* __restrict__ y, int n_seq, int r_beg, int r_end, - int cin, int cout, int k, int dil, int pad, int x_ld, int y_ld, int res_ld, int act) { + int cin, int cout, int k, int dil, int pad, int stride, int cdiv, int x_ld, int y_ld, + int res_ld, int act) { const int64_t t = (int64_t)blockIdx.x * kT + threadIdx.x; const int64_t total = (int64_t)(r_end - r_beg) * cout; if (t >= total) return; const int co = (int)(t % cout); const int row = r_beg + (int)(t / cout); - const int sq = seq_of(cu, n_seq, row); - const int s0 = cu[sq], s1 = cu[sq + 1]; + const int sq = seq_of(cu, n_seq, row * stride * cdiv); + const int s0 = cu[sq] / cdiv, s1 = cu[sq + 1] / cdiv; float acc = 0.0f; for (int kk = 0; kk < k; ++kk) { - const int ir = row + kk * dil - pad; + const int ir = row * stride + kk * dil - pad; if (ir < s0 || ir >= s1) continue; const uint16_t* xp = x + (size_t)ir * x_ld; const uint16_t* wp = w + ((size_t)co * k + kk) * cin; @@ -384,14 +386,14 @@ __global__ __launch_bounds__(256) void conv_t1d_kernel( // the output channels, its K the (tap, channel) pairs, channel innermost. A thread stages one K // column a slab and steps it sixteen at a time, so no K index is divided after the first. -// conv1d's A: output row m's tap t reads input row m + t dil - pad of m's own sequence, zero -// outside it and past the last tap. As r4d_f32::RowsLoader, every load is issued at an address -// clamped into x's first `r_end` rows and the zero selected after it. +// conv1d's A: output row m's tap t reads input row m stride + t dil - pad of m's own sequence, +// zero outside it and past the last tap. As r4d_f32::RowsLoader, every load is issued at an +// address clamped into x's first `r_end` stride rows and the zero selected after it. template struct Conv1dRows { const float* x; const int* cu; - int n_seq, r_end, cin, k, dil, pad, x_ld; + int n_seq, r_end, cin, k, dil, pad, x_ld, stride, cdiv; int row[CNT], s0[CNT], s1[CNT]; int t = 0, ci = 0; __device__ void init(int first_row, int kk) { @@ -399,9 +401,9 @@ struct Conv1dRows { for (int j = 0; j < CNT; ++j) { const int m = first_row + 16 * j; row[j] = m < r_end ? m : -1; - const int sq = seq_of(cu, n_seq, m < r_end ? m : r_end - 1); - s0[j] = cu[sq]; - s1[j] = cu[sq + 1]; + const int sq = seq_of(cu, n_seq, (m < r_end ? m : r_end - 1) * stride * cdiv); + s0[j] = cu[sq] / cdiv; + s1[j] = cu[sq + 1] / cdiv; } t = kk / cin; ci = kk % cin; @@ -409,9 +411,10 @@ struct Conv1dRows { __device__ void fetch(float (&v)[CNT]) const { #pragma unroll for (int j = 0; j < CNT; ++j) { - const int ir = row[j] + t * dil - pad; + const int ir = row[j] * stride + t * dil - pad; const bool live = row[j] >= 0 && t < k && ir >= s0[j] && ir < s1[j]; - const int irc = ir < 0 ? 0 : (ir >= r_end ? r_end - 1 : ir); + const int x_rows = r_end * stride; + const int irc = ir < 0 ? 0 : (ir >= x_rows ? x_rows - 1 : ir); const float xv = x[(uint32_t)irc * (uint32_t)x_ld + (uint32_t)ci]; v[j] = live ? xv : 0.0f; } @@ -428,12 +431,13 @@ template __global__ __launch_bounds__(r4d_f32::kThr) void conv1d_f32_kernel( const float* __restrict__ x, const float* __restrict__ w, const float* __restrict__ bias, const float* res, const int* __restrict__ cu, float* y, int n_seq, int rows, int cin, - int cout, int k, int dil, int pad, int x_ld, int y_ld, int res_ld, int act) { + int cout, int k, int dil, int pad, int stride, int cdiv, int x_ld, int y_ld, int res_ld, + int act) { constexpr int TM = 8, BM = 16 * TM, BN = 16 * TN; const int tid = threadIdx.x, tx = tid & 15, ty = tid >> 4; const int m0 = blockIdx.y * BM, n0 = blockIdx.x * BN; const int K = k * cin; - Conv1dRows la{ x, cu, n_seq, rows, cin, k, dil, pad, x_ld }; + Conv1dRows la{ x, cu, n_seq, rows, cin, k, dil, pad, x_ld, stride, cdiv }; r4d_f32::RowsLoader lw{ w, K, cout, K }; la.init(m0 + ty, tx); lw.init(n0 + ty, tx); @@ -458,16 +462,17 @@ __global__ __launch_bounds__(r4d_f32::kThr) void conv1d_f32_kernel( __global__ __launch_bounds__(kT) void conv1d_f32_direct_kernel( const float* __restrict__ x, const float* __restrict__ w, const float* __restrict__ bias, const float* res, const int* __restrict__ cu, float* y, int n_seq, int rows, int cin, - int cout, int k, int dil, int pad, int x_ld, int y_ld, int res_ld, int act) { + int cout, int k, int dil, int pad, int stride, int cdiv, int x_ld, int y_ld, int res_ld, + int act) { const int64_t t = (int64_t)blockIdx.x * kT + threadIdx.x; if (t >= (int64_t)rows * cout) return; const int co = (int)(t % cout); const int row = (int)(t / cout); - const int sq = seq_of(cu, n_seq, row); - const int s0 = cu[sq], s1 = cu[sq + 1]; + const int sq = seq_of(cu, n_seq, row * stride * cdiv); + const int s0 = cu[sq] / cdiv, s1 = cu[sq + 1] / cdiv; float acc = 0.0f; for (int kk = 0; kk < k; ++kk) { - const int ir = row + kk * dil - pad; + const int ir = row * stride + kk * dil - pad; if (ir < s0 || ir >= s1) continue; const float* xp = x + (size_t)ir * x_ld; const float* wp = w + ((size_t)co * k + kk) * cin; @@ -806,6 +811,86 @@ __global__ __launch_bounds__(kT) void rvq_embed_kernel( } } +// One residual quantiser step (DAC's VectorQuantize), a workgroup a row of x. e = in_w x + in_b, +// each of its `dim` sums split over the workgroup and reduced through LDS; each thread then scores +// a strided run of the codebook against e normalised, every row normalised as it is read +// (F.normalize's division), and the workgroup's argmax keeps the lower index on a tie; last the +// chosen raw row's out-projection leaves x's row, rounded once. libref's arithmetic in another +// summation order. +constexpr int kVqMaxDim = 32; + +__global__ __launch_bounds__(kT) void vq_encode_kernel( + void* __restrict__ x, int x_f32, int x_ld, const void* __restrict__ iw, int iw_f32, + const void* __restrict__ ib, int ib_f32, const void* __restrict__ cb, int cb_f32, + const void* __restrict__ ow, int ow_f32, const void* __restrict__ ob, int ob_f32, + int* __restrict__ codes, int64_t code_st, int n, int dim, int bins) { + __shared__ float part[kT / 32][kVqMaxDim]; + __shared__ float e[kVqMaxDim], eh[kVqMaxDim]; + __shared__ float wbest[kT / 32]; + __shared__ int wcode[kT / 32]; + const int tid = threadIdx.x, wv = tid >> 5, ln = tid & 31; + const size_t xb = (size_t)blockIdx.x * x_ld; + auto ldx = [&](int d) { + return x_f32 ? ((const float*)x)[xb + d] : bf2f(((const uint16_t*)x)[xb + d]); + }; + for (int j = 0; j < dim; ++j) { + float acc = 0.0f; + for (int d = tid; d < n; d += kT) acc += ldtab(iw, iw_f32, (size_t)j * n + d) * ldx(d); + #pragma unroll + for (int o = 16; o > 0; o >>= 1) acc += __shfl_xor(acc, o, 32); + if (ln == 0) part[wv][j] = acc; + } + __syncthreads(); + if (tid < dim) { + float acc = 0.0f; + for (int w = 0; w < kT / 32; ++w) acc += part[w][tid]; + e[tid] = acc + ldtab(ib, ib_f32, (size_t)tid); + } + __syncthreads(); + if (tid < dim) { + float ss = 0.0f; + for (int j = 0; j < dim; ++j) ss += e[j] * e[j]; + eh[tid] = e[tid] / fmaxf(sqrtf(ss), 1e-12f); + } + __syncthreads(); + // Each thread's best over its rows, ascending, so its first maximum is kept; INT_MAX none. + float bs = -INFINITY; + int bc = 0x7fffffff; + for (int c = tid; c < bins; c += kT) { + float ss = 0.0f; + for (int j = 0; j < dim; ++j) { + const float v = ldtab(cb, cb_f32, (size_t)c * dim + j); + ss += v * v; + } + const float nrm = fmaxf(sqrtf(ss), 1e-12f); + float dot = 0.0f; + for (int j = 0; j < dim; ++j) dot += eh[j] * (ldtab(cb, cb_f32, (size_t)c * dim + j) / nrm); + if (dot > bs) { bs = dot; bc = c; } + } + #pragma unroll + for (int o = 16; o > 0; o >>= 1) { + const float os = __shfl_xor(bs, o, 32); + const int oc = __shfl_xor(bc, o, 32); + if (os > bs || (os == bs && oc < bc)) { bs = os; bc = oc; } + } + if (ln == 0) { wbest[wv] = bs; wcode[wv] = bc; } + __syncthreads(); + float fs = wbest[0]; + int fc = wcode[0]; + for (int w = 1; w < kT / 32; ++w) + if (wbest[w] > fs || (wbest[w] == fs && wcode[w] < fc)) { fs = wbest[w]; fc = wcode[w]; } + const int code = fc == 0x7fffffff ? 0 : fc; // no score above -inf: code 0, as libref + if (tid == 0) codes[(size_t)blockIdx.x * code_st] = code; + for (int d = tid; d < n; d += kT) { + float q = 0.0f; + for (int j = 0; j < dim; ++j) + q += ldtab(ow, ow_f32, (size_t)d * dim + j) * ldtab(cb, cb_f32, (size_t)code * dim + j); + const float v = ldx(d) - (q + ldtab(ob, ob_f32, (size_t)d)); + if (x_f32) ((float*)x)[xb + d] = v; + else ((uint16_t*)x)[xb + d] = f32_to_bf16(v); + } +} + // dst[(lane n_codes + k) stride + frame - skip] = codes[k, rep i]; a thread a (lane, code). A lane // outside `status`, or a frame past `stride` or a lane past `dst`, stores nothing (libref refuses // the call). @@ -1126,24 +1211,26 @@ extern "C" int r4d_rad_conv1d(const RadArgs* a, RadStream s) { return R4D_NO(RAD_E_DTYPE); const long long cin = r4d_geti_or(a, "cin", 0), cout = r4d_geti_or(a, "cout", 0); const long long k = r4d_geti_or(a, "k", 0), dil = r4d_geti_or(a, "dil", 1); - const long long causal = r4d_geti_or(a, "causal", 0); + const long long causal = r4d_geti_or(a, "causal", 0), st = r4d_geti_or(a, "stride", 1); + const long long cd = r4d_geti_or(a, "cu_div", 1); const int act = conv1d_act(r4d_gets(a, "act")); if (act < 0) return R4D_NO(RAD_E_INVAL); if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0) || dil <= 0 || causal < 0 || - causal > 1) + causal > 1 || st < 1 || (st > 1 && (!causal || dil * (k - 1) + 1 < st)) || cd < 1) return R4D_NO(RAD_E_SHAPE); long long rx = 0, ry = 0, rr = 0; const long long xl = r4d_rows_pitch(x, cin, &rx), yl = r4d_rows_pitch(y, cout, &ry); const long long rl = res ? r4d_rows_pitch(res, cout, &rr) : 0; if (xl < 0 || yl < 0 || rl < 0) return R4D_NO(RAD_E_STRIDE); const long long n_seq = r4d_numel(cu) - 1; - if (n_seq <= 0 || ry <= 0 || rx < ry || (res && rr < ry) || r4d_numel(w) < cout * cin * k || - (b && r4d_numel(b) < cout)) + if (n_seq <= 0 || ry <= 0 || rx < ry * st || (res && rr < ry) || + r4d_numel(w) < cout * cin * k || (b && r4d_numel(b) < cout)) return R4D_NO(RAD_E_SHAPE); const uint16_t* bp = b ? (const uint16_t*)b->data : nullptr; - /* Both kernels read input row row + tap * dil - pad: half the reach a side, or all of it on - * the left for the causal form. */ - const int pad = (int)(causal ? dil * (k - 1) : dil * (k - 1) / 2); + /* Both kernels read input row row * stride + tap * dil - pad: half the reach a side, or for + * the causal form all of it on the left less the stride (the sequences' bounds whole + * strides, as the op requires). */ + const int pad = (int)(causal ? dil * (k - 1) + 1 - st : dil * (k - 1) / 2); const bool tiled = (cin % 8) == 0 && cout >= 16 && (xl % 8) == 0 && ((uintptr_t)x->data & 15) == 0 && ((uintptr_t)w->data & 15) == 0; const uint16_t* rp = res ? (const uint16_t*)res->data : nullptr; @@ -1156,8 +1243,8 @@ extern "C" int r4d_rad_conv1d(const RadArgs* a, RadStream s) { dim3(256), 0, (hipStream_t)s, (const uint16_t*)x->data, (const uint16_t*)w->data, bp, rp, (const int*)cu->data, (uint16_t*)y->data, (int)n_seq, 0, (int)ry, - (int)cin, (int)cout, (int)k, (int)dil, pad, (int)xl, (int)yl, - (int)rl, act); + (int)cin, (int)cout, (int)k, (int)dil, pad, (int)st, (int)cd, + (int)xl, (int)yl, (int)rl, act); } else { constexpr int TP = 4, TC = 1, BP = 128, BC = 64; hipLaunchKernelGGL((conv1d_igemm_kernel), @@ -1165,15 +1252,15 @@ extern "C" int r4d_rad_conv1d(const RadArgs* a, RadStream s) { dim3(256), 0, (hipStream_t)s, (const uint16_t*)x->data, (const uint16_t*)w->data, bp, rp, (const int*)cu->data, (uint16_t*)y->data, (int)n_seq, 0, (int)ry, - (int)cin, (int)cout, (int)k, (int)dil, pad, (int)xl, (int)yl, - (int)rl, act); + (int)cin, (int)cout, (int)k, (int)dil, pad, (int)st, (int)cd, + (int)xl, (int)yl, (int)rl, act); } } else { hipLaunchKernelGGL(conv1d_direct_kernel, dim3(blocks_for(ry * cout)), dim3(kT), 0, (hipStream_t)s, (const uint16_t*)x->data, (const uint16_t*)w->data, bp, rp, (const int*)cu->data, (uint16_t*)y->data, (int)n_seq, 0, (int)ry, (int)cin, (int)cout, (int)k, (int)dil, pad, - (int)xl, (int)yl, (int)rl, act); + (int)st, (int)cd, (int)xl, (int)yl, (int)rl, act); } return st_of(hipGetLastError()); } @@ -1237,11 +1324,12 @@ extern "C" int r4d_rad_conv1d_f32(const RadArgs* a, RadStream s) { if (!r4d_contig(w) || (b && !r4d_contig(b)) || !r4d_contig(cu)) return R4D_NO(RAD_E_STRIDE); const long long cin = r4d_geti_or(a, "cin", 0), cout = r4d_geti_or(a, "cout", 0); const long long k = r4d_geti_or(a, "k", 0), dil = r4d_geti_or(a, "dil", 1); - const long long causal = r4d_geti_or(a, "causal", 0); + const long long causal = r4d_geti_or(a, "causal", 0), st = r4d_geti_or(a, "stride", 1); + const long long cd = r4d_geti_or(a, "cu_div", 1); const int act = conv1d_act(r4d_gets(a, "act")); if (act < 0) return R4D_NO(RAD_E_INVAL); if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0) || dil <= 0 || causal < 0 || - causal > 1) + causal > 1 || st < 1 || (st > 1 && (!causal || dil * (k - 1) + 1 < st)) || cd < 1) return R4D_NO(RAD_E_SHAPE); long long rx = 0, ry = 0, rr = 0; const long long xl = r4d_rows_pitch(x, cin, &rx), yl = r4d_rows_pitch(y, cout, &ry); @@ -1249,18 +1337,19 @@ extern "C" int r4d_rad_conv1d_f32(const RadArgs* a, RadStream s) { if (xl < 0 || yl < 0 || rl < 0) return R4D_NO(RAD_E_STRIDE); const long long n_seq = r4d_numel(cu) - 1; /* The tile's loaders index x and w with 32-bit element offsets. */ - if (n_seq <= 0 || ry <= 0 || rx < ry || (res && rr < ry) || r4d_numel(w) < cout * cin * k || - (b && r4d_numel(b) < cout) || ry > 0x7fffffffll || (ry + 127) / 128 > 65535 || - ry * xl >= (1ll << 32) || cout * cin * k >= (1ll << 32)) + if (n_seq <= 0 || ry <= 0 || rx < ry * st || (res && rr < ry) || + r4d_numel(w) < cout * cin * k || (b && r4d_numel(b) < cout) || ry > 0x7fffffffll || + (ry + 127) / 128 > 65535 || ry * st * xl >= (1ll << 32) || cout * cin * k >= (1ll << 32)) return R4D_NO(RAD_E_SHAPE); - const int pad = (int)(causal ? dil * (k - 1) : dil * (k - 1) / 2); + const int pad = (int)(causal ? dil * (k - 1) + 1 - st : dil * (k - 1) / 2); const float* bp = b ? (const float*)b->data : nullptr; const float* rp = res ? (const float*)res->data : nullptr; if (cout < 16) { hipLaunchKernelGGL(conv1d_f32_direct_kernel, dim3(blocks_for(ry * cout)), dim3(kT), 0, (hipStream_t)s, (const float*)x->data, (const float*)w->data, bp, rp, (const int*)cu->data, (float*)y->data, (int)n_seq, (int)ry, (int)cin, - (int)cout, (int)k, (int)dil, pad, (int)xl, (int)yl, (int)rl, act); + (int)cout, (int)k, (int)dil, pad, (int)st, (int)cd, (int)xl, (int)yl, + (int)rl, act); return st_of(hipGetLastError()); } const int tn = cout > 64 ? 8 : cout > 32 ? 4 : 2; @@ -1269,7 +1358,8 @@ extern "C" int r4d_rad_conv1d_f32(const RadArgs* a, RadStream s) { hipLaunchKernelGGL((conv1d_f32_kernel), grid, dim3(r4d_f32::kThr), 0, (hipStream_t)s, \ (const float*)x->data, (const float*)w->data, bp, rp, \ (const int*)cu->data, (float*)y->data, (int)n_seq, (int)ry, (int)cin, \ - (int)cout, (int)k, (int)dil, pad, (int)xl, (int)yl, (int)rl, act) + (int)cout, (int)k, (int)dil, pad, (int)st, (int)cd, (int)xl, (int)yl, \ + (int)rl, act) if (tn == 8) C1F(8); else if (tn == 4) C1F(4); else C1F(2); @@ -1433,6 +1523,37 @@ extern "C" int r4d_rad_rvq_embed(const RadArgs* a, RadStream s) { return st_of(hipGetLastError()); } +extern "C" int r4d_rad_vq_encode(const RadArgs* a, RadStream s) { + const RadTensor *x = r4d_opt(a, 0), *iw = r4d_opt(a, 1), *ib = r4d_opt(a, 2); + const RadTensor *cb = r4d_opt(a, 3), *ow = r4d_opt(a, 4), *ob = r4d_opt(a, 5); + const RadTensor* codes = r4d_opt(a, 6); + if (!x || !iw || !ib || !cb || !ow || !ob || !codes) return R4D_NO(RAD_E_INVAL); + auto bf_or_f32 = [](const RadTensor* t) { return t->dtype == RAD_BF16 || t->dtype == RAD_F32; }; + if (!is_i32(codes) || !bf_or_f32(x) || !bf_or_f32(iw) || !bf_or_f32(ib) || !bf_or_f32(cb) || + !bf_or_f32(ow) || !bf_or_f32(ob)) + return R4D_NO(RAD_E_DTYPE); + const long long n = r4d_geti_or(a, "n", 0), dim = r4d_geti_or(a, "dim", 0); + const long long bins = r4d_geti_or(a, "bins", 0); + if (n <= 0 || dim <= 0 || dim > kVqMaxDim || bins <= 0) return R4D_NO(RAD_E_SHAPE); + long long rx = 0; + const long long xl = r4d_rows_pitch(x, n, &rx); + if (xl < 0 || !r4d_contig(iw) || !r4d_contig(ib) || !r4d_contig(cb) || !r4d_contig(ow) || + !r4d_contig(ob)) + return R4D_NO(RAD_E_STRIDE); + if (rx <= 0 || r4d_numel(codes) < rx || r4d_numel(iw) < dim * n || r4d_numel(ib) < dim || + r4d_numel(cb) < bins * dim || r4d_numel(ow) < n * dim || r4d_numel(ob) < n || + rx > 0x7fffffffll) + return R4D_NO(RAD_E_SHAPE); + /* A code at the operand's own stride: a row of a code-major plane, or a column of one. */ + const long long cs = codes->rank >= 1 ? codes->stride[codes->rank - 1] : 1; + auto f32 = [](const RadTensor* t) { return t->dtype == RAD_F32 ? 1 : 0; }; + hipLaunchKernelGGL(vq_encode_kernel, dim3((unsigned)rx), dim3(kT), 0, (hipStream_t)s, x->data, + f32(x), (int)xl, iw->data, f32(iw), ib->data, f32(ib), cb->data, f32(cb), + ow->data, f32(ow), ob->data, f32(ob), (int*)codes->data, (int64_t)cs, + (int)n, (int)dim, (int)bins); + return st_of(hipGetLastError()); +} + extern "C" int r4d_rad_frame_mix_store(const RadArgs* a, RadStream s) { const RadTensor *hid = r4d_opt(a, 0), *lw = r4d_opt(a, 1), *sc = r4d_opt(a, 2); const RadTensor *lane = r4d_opt(a, 3), *fr = r4d_opt(a, 4), *st = r4d_opt(a, 5); diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index fcb48fb1..d106e1cc 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -200,6 +200,7 @@ int r4d_rad_conv_t1d_f32 (const RadArgs*, RadStream); int r4d_rad_snake (const RadArgs*, RadStream); int r4d_rad_cfg_topk_sample (const RadArgs*, RadStream); int r4d_rad_rvq_embed (const RadArgs*, RadStream); +int r4d_rad_vq_encode (const RadArgs*, RadStream); int r4d_rad_frame_mix_store (const RadArgs*, RadStream); int r4d_rad_end_status (const RadArgs*, RadStream); int r4d_rad_ras_pick (const RadArgs*, RadStream); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 1ff67849..f6dc9b7e 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -2372,7 +2372,8 @@ static const RadOperandSpec oAttnPS[] = { /* ---- audio generation (r4d_audio.hip); the same specs as libref's (ref_registry.cpp). */ 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"), - P_INTO("causal") }; + P_INTO("causal"), P_INTO("stride"), + P_INTO("cu_div") }; static const RadOperandSpec oConv1d[] = { { "x", RAD_OPD_IN, 0 }, { "w", RAD_OPD_WEIGHT, 0 }, { "cu_seqlens", RAD_OPD_IN, 0 }, { "res", RAD_OPD_IN, 1 }, { "y", RAD_OPD_OUT, 0 }, { "b", RAD_OPD_WEIGHT, 1 }, @@ -2405,6 +2406,13 @@ static const RadOperandSpec oRvqEmbed[] = { { "codes", RAD_OPD_IN, 0 }, { "t0", RAD_OPD_WEIGHT, 0 }, { "t1", RAD_OPD_WEIGHT, 0 }, { "y", RAD_OPD_OUT, 0 }, }; +static const RadParamSpec pVqEncode[] = { P_INT("M"), P_INT("n"), P_INT("dim"), P_INT("bins"), + P_STR("dtype") }; +static const RadOperandSpec oVqEncode[] = { + { "x", RAD_OPD_INOUT, 0 }, { "in_w", RAD_OPD_WEIGHT, 0 }, { "in_b", RAD_OPD_WEIGHT, 0 }, + { "cb", RAD_OPD_WEIGHT, 0 }, { "out_w", RAD_OPD_WEIGHT, 0 }, { "out_b", RAD_OPD_WEIGHT, 0 }, + { "codes", RAD_OPD_OUT, 0 }, +}; static const RadParamSpec pFrameMix[] = { P_INT("M"), P_INT("n_embd"), P_INT("L"), P_INT("stride"), P_INTO("rep"), P_STR("dtype") }; static const RadOperandSpec oFrameMix[] = { @@ -3338,6 +3346,9 @@ static const RadOpSchema kSchemas[] = { SCHEMA("rvq_embed", pRvqEmbed, oRvqEmbed, "a frame's embedding from code-major residual codes, the first codebook's table and the " "others' stacked one, times `scale`; `round` 1 rounds the f32 sum once."), + SCHEMA("vq_encode", pVqEncode, oVqEncode, + "one residual vector quantiser step: the code of greatest cosine with in_w x + in_b " + "(the lower index on a tie), then x -= out_w cb[code] + out_b."), SCHEMA("frame_mix_store", pFrameMix, oFrameMix, "a frame's conditioning row (softmax-weighted planes times scale) into its lane."), SCHEMA("end_status", pEndStatus, oEndStatus, @@ -4825,6 +4836,8 @@ static const RadConstraint cRmsMod[] = { C_IN("dtype", "bf16"), C_DIV("n", 8), C static const RadConstraint cRmsModQ[] = { C_IN("dtype", "bf16"), C_DIV("n", 128), C_LE("n", 8192), C_EQ("group", 128) }; static const RadConstraint cGenF32[] = { C_IN("dtype", "f32") }; +/* vq_encode: e's sums staged in LDS, a codebook dimension of 32 at most. */ +static const RadConstraint cVqEncode[] = { C_IN("dtype", "bf16 f32"), C_LE("dim", 32) }; static const RadConstraint cPatchUnfold[] = { C_IN("dtype", "f32"), C_LE("C", 64) }; static const RadConstraint cGenAnyFloat[] = { C_IN("dtype", "bf16 f32") }; static const RadConstraint cSnakeAa[] = { C_IN("dtype", "f32"), C_LE("k", 32) }; @@ -8289,6 +8302,13 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, ROW(cGenBf16), NOTUNE, nullptr, nullptr, r4d_rad_rvq_embed }, +{ "vq_encode", "vq_encode", "elem", + "a residual quantiser's step: the nearest code by cosine", "a workgroup a row, dim <= 32", + "bf16 or f32 rows and weights, f32 arithmetic, the row rounded once", + RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, + ROW(cVqEncode), NOTUNE, + nullptr, nullptr, r4d_rad_vq_encode }, + { "frame_mix_store_bf16", "frame_mix_store", "elem", "a frame's conditioning row into its lane", "a workgroup a lane, L <= 16", "bf16 planes and rows, f32 logits and scale", diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 5371652b..f7f852fd 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -5607,18 +5607,28 @@ int64_t seq_of(const std::vector& cu, int64_t r) { return s; } -/* conv1d: the causal form beside the symmetric one, on the tiled and the direct kernels. */ +/* conv1d: the causal form beside the symmetric one, on the tiled and the direct kernels; strided + * causal forms (a codec encoder's downsampling, kernels of two strides and of one) over sequences + * of whole strides, the bounds also in units of 8 rows (cu_div, an encoder's samples). */ void conv1d_cases(const Plugin& pl) { const RadKernelInfo* r = find(pl, "conv1d_bf16"); if (!r) { ok("conv1d_bf16", false, "row absent"); return; } - struct C { int64_t cin, cout, k, dil; int causal, extras; }; - for (const C& c : { C{ 16, 32, 3, 2, 0, 0 }, C{ 16, 32, 3, 2, 1, 1 }, C{ 16, 128, 4, 1, 1, 0 }, - C{ 6, 3, 7, 3, 1, 1 }, C{ 6, 3, 2, 1, 1, 0 } }) { - const std::vector cu = { 0, 13, 30 }; - const int64_t M = 30; + struct C { int64_t cin, cout, k, dil; int causal, extras; int64_t st, cd = 1; }; + for (const C& c : { C{ 16, 32, 3, 2, 0, 0, 1 }, C{ 16, 32, 3, 2, 1, 1, 1 }, + C{ 16, 128, 4, 1, 1, 0, 1 }, C{ 6, 3, 7, 3, 1, 1, 1 }, + C{ 6, 3, 2, 1, 1, 0, 1 }, C{ 16, 32, 4, 1, 1, 0, 2 }, + C{ 16, 128, 16, 1, 1, 1, 8 }, C{ 6, 3, 8, 1, 1, 1, 4 }, + C{ 16, 32, 2, 1, 1, 0, 2 }, C{ 16, 64, 4, 1, 1, 1, 2, 8 }, + C{ 6, 3, 8, 1, 1, 0, 4, 8 } }) { + /* Bounds of whole strides: 13 and 30 rounded up to the stride; the op reads them times + * cu_div. */ + const int64_t s1 = (13 + c.st - 1) / c.st * c.st, s2 = (30 + c.st - 1) / c.st * c.st; + const std::vector cu = { 0, (int32_t)s1, (int32_t)s2 }; + const std::vector cu_op = { 0, (int32_t)(s1 * c.cd), (int32_t)(s2 * c.cd) }; + const int64_t M = s2, MO = M / c.st; std::vector x = randv((size_t)(M * c.cin), 1.0f); std::vector w = randv((size_t)(c.cout * c.cin * c.k), 0.3f); - std::vector b = randv((size_t)c.cout, 0.5f), res = randv((size_t)(M * c.cout), 0.5f); + std::vector b = randv((size_t)c.cout, 0.5f), res = randv((size_t)(MO * c.cout), 0.5f); void* dx = up_bf16(x); void* db = up_bf16(b); void* dres = up_bf16(res); @@ -5626,27 +5636,31 @@ void conv1d_cases(const Plugin& pl) { std::vector ps = { PI("M", M), PI("cin", c.cin), PI("cout", c.cout), PI("k", c.k), PI("dil", c.dil), PS("act", c.extras ? "tanh" : "none"), PS("dtype", "bf16"), PI("causal", c.causal) }; - const std::string nm = fmt("conv1d_bf16 cin %lld cout %lld k %lld dil %lld causal %d%s", + if (c.st > 1) ps.push_back(PI("stride", c.st)); + if (c.cd > 1) ps.push_back(PI("cu_div", c.cd)); + const std::string nm = fmt("conv1d_bf16 cin %lld cout %lld k %lld dil %lld causal %d " + "stride %lld cu_div %lld%s", (long long)c.cin, (long long)c.cout, (long long)c.k, - (long long)c.dil, c.causal, c.extras ? " +bias+res+tanh" : ""); + (long long)c.dil, c.causal, (long long)c.st, (long long)c.cd, + c.extras ? " +bias+res+tanh" : ""); void* dw = stored_weight(r, ps, w, c.cout, c.cin * c.k, nm.c_str()); - void* dcu = up_i32(cu); - void* dy = dalloc((size_t)(M * c.cout) * 2); + void* dcu = up_i32(cu_op); + void* dy = dalloc((size_t)(MO * c.cout) * 2); if (!dw) continue; - RadTensor tres = T(dres, RAD_BF16, { M, c.cout }), tb = T(db, RAD_BF16, { c.cout }); + RadTensor tres = T(dres, RAD_BF16, { MO, c.cout }), tb = T(db, RAD_BF16, { c.cout }); if (!c.extras) { absent(tres); absent(tb); } const int rc = launch(r, { T(dx, RAD_BF16, { M, c.cin }), T(dw, RAD_BF16, { c.cout, c.cin, c.k }), - T(dcu, RAD_I32, { 3 }), tres, T(dy, RAD_BF16, { M, c.cout }), tb }, + T(dcu, RAD_I32, { 3 }), tres, T(dy, RAD_BF16, { MO, c.cout }), tb }, ps); if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; } - const int64_t pad = c.causal ? c.dil * (c.k - 1) : c.dil * (c.k - 1) / 2; - std::vector want((size_t)(M * c.cout)); - for (int64_t row = 0; row < M; ++row) { - const int64_t e = seq_of(cu, row), s0 = cu[(size_t)e], s1 = cu[(size_t)e + 1]; + const int64_t pad = c.causal ? c.dil * (c.k - 1) + 1 - c.st : c.dil * (c.k - 1) / 2; + std::vector want((size_t)(MO * c.cout)); + for (int64_t row = 0; row < MO; ++row) { + const int64_t e = seq_of(cu, row * c.st), s0 = cu[(size_t)e], s1 = cu[(size_t)e + 1]; for (int64_t co = 0; co < c.cout; ++co) { float acc = 0.0f; for (int64_t kk = 0; kk < c.k; ++kk) { - const int64_t ir = row + kk * c.dil - pad; + const int64_t ir = row * c.st + kk * c.dil - pad; if (ir < s0 || ir >= s1) continue; for (int64_t ci = 0; ci < c.cin; ++ci) acc += w[(size_t)((co * c.cin + ci) * c.k + kk)] * x[(size_t)(ir * c.cin + ci)]; @@ -6081,6 +6095,104 @@ void rvq_embed_cases(const Plugin& pl) { } } +/* vq_encode at Fish Audio's codec geometry (n 1024, codes of dimension 8, a semantic codebook of + * 4096 and a residual one of 1024) and an odd one, bf16 and f32 rows, f32 and bf16 weights, the + * codes a dense row and a strided column: each code against the host's cosine argmax in double (a + * pick within 1e-5 of the best score is a near tie, not a miss), the row after against + * x - out_w cb[code] - out_b for the code the kernel took. */ +void vq_encode_cases(const Plugin& pl) { + const RadKernelInfo* r = find(pl, "vq_encode"); + if (!r) { ok("vq_encode", false, "row absent"); return; } + struct C { int64_t n, dim, bins, M; bool f32x, f32w; int64_t cst; }; + for (const C& c : { C{ 1024, 8, 4096, 37, false, true, 1 }, C{ 1024, 8, 1024, 37, false, true, 1 }, + C{ 1024, 8, 1024, 20, true, true, 3 }, C{ 1024, 8, 4096, 9, false, false, 1 }, + C{ 200, 5, 300, 11, true, false, 2 } }) { + const int64_t n = c.n, dim = c.dim, bins = c.bins, M = c.M; + std::vector x = randv((size_t)(M * n), 1.0f); + std::vector iw = randv((size_t)(dim * n), 1.0f / std::sqrt((float)n)); + std::vector ib = randv((size_t)dim, 0.1f), cb = randv((size_t)(bins * dim), 1.0f); + std::vector ow = randv((size_t)(n * dim), 0.3f), ob = randv((size_t)n, 0.1f); + /* A bf16 upload rounds the host copy to what the device holds. */ + const uint32_t wdt = c.f32w ? RAD_F32 : RAD_BF16, xdt = c.f32x ? RAD_F32 : RAD_BF16; + auto upw = [&](std::vector& w) { return c.f32w ? up_f32(w) : up_bf16(w); }; + void* dx = c.f32x ? up_f32(x) : up_bf16(x); + const std::vector plane((size_t)(M * c.cst), -7); + void* dc = up_i32(plane); + RadTensor tc = T(dc, RAD_I32, { M }); + tc.stride[0] = c.cst; + std::vector ps = { PI("M", M), PI("n", n), PI("dim", dim), PI("bins", bins), + PS("dtype", c.f32x ? "f32" : "bf16") }; + const std::string nm = fmt("vq_encode n %lld dim %lld bins %lld, %s rows, %s weights, " + "code stride %lld", (long long)n, (long long)dim, + (long long)bins, c.f32x ? "f32" : "bf16", + c.f32w ? "f32" : "bf16", (long long)c.cst); + const int rc = launch(r, { T(dx, xdt, { M, n }), T(upw(iw), wdt, { dim, n }), + T(upw(ib), wdt, { dim }), T(upw(cb), wdt, { bins, dim }), + T(upw(ow), wdt, { n, dim }), T(upw(ob), wdt, { n }), tc }, + ps); + if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; } + const std::vector got = down_i32(dc, plane.size()); + const std::vector gx = c.f32x ? down_f32(dx, x.size()) : down_bf16(dx, x.size()); + int64_t miss = 0, near = 0, gaps = 0; + std::vector want = x; + for (int64_t i = 0; i < M; ++i) { + std::vector e((size_t)dim); + double ss = 0; + for (int64_t j = 0; j < dim; ++j) { + double acc = ib[(size_t)j]; + for (int64_t d = 0; d < n; ++d) + acc += (double)iw[(size_t)(j * n + d)] * x[(size_t)(i * n + d)]; + e[(size_t)j] = acc; + ss += acc * acc; + } + const double en = std::max(std::sqrt(ss), 1e-12); + auto score = [&](int64_t k) { + const float* row = cb.data() + k * dim; + double cs = 0, dot = 0; + for (int64_t j = 0; j < dim; ++j) cs += (double)row[j] * row[j]; + const double cn = std::max(std::sqrt(cs), 1e-12); + for (int64_t j = 0; j < dim; ++j) dot += e[(size_t)j] / en * (row[j] / cn); + return dot; + }; + int64_t best = 0; + double bs = -1e300; + for (int64_t k = 0; k < bins; ++k) { + const double s = score(k); + if (s > bs) { bs = s; best = k; } + } + for (int64_t g = 1; g < c.cst; ++g) + if (got[(size_t)(i * c.cst + g)] != -7) ++gaps; + const int64_t k = got[(size_t)(i * c.cst)]; + if (k < 0 || k >= bins) { ++miss; continue; } + if (k != best) { + if (bs - score(k) <= 1e-5) ++near; + else ++miss; + } + for (int64_t d = 0; d < n; ++d) { + double q = ob[(size_t)d]; + for (int64_t j = 0; j < dim; ++j) + q += (double)ow[(size_t)(d * dim + j)] * cb[(size_t)(k * dim + j)]; + want[(size_t)(i * n + d)] = (float)(x[(size_t)(i * n + d)] - q); + } + } + ok((nm + " codes").c_str(), miss == 0 && gaps == 0, + fmt("%lld of %lld codes wrong (%lld near ties taken), %lld gaps of the strided codes " + "written", (long long)miss, (long long)M, (long long)near, (long long)gaps)); + if (c.f32x) { + /* f32 rows: against the f32 rows themselves (check() rounds its reference to bf16). */ + double e = 0, r2 = 0; + for (size_t i = 0; i < want.size(); ++i) { + e += ((double)gx[i] - want[i]) * ((double)gx[i] - want[i]); + r2 += (double)want[i] * want[i]; + } + const double rl = std::sqrt(e / std::max(r2, 1e-30)); + ok(fmt("%s residual (rel_l2 %.3g, bound 1e-05)", nm.c_str(), rl).c_str(), rl <= 1e-5); + } else { + check(nm + " residual", gx, want, 1e-2); + } + } +} + /* frame_store: each lane's codes into its block of the history, frames before `skip` and lanes * whose status ended the piece left alone. Exact. */ void frame_store_cases(const Plugin& pl) { @@ -9331,36 +9443,43 @@ void gate_add_none_case(const Plugin& pl) { /* conv1d at f32 at a BigVGAN's geometries: conv_pre's 2048 -> 1024 over 7 taps, the AMP blocks' * dilated 3/7/11 taps with the residual the output itself, the direct kernel's 8 channels and the - * final conv's one with its clamp. Two sequences, so a tap crossing a sequence's edge reads zero. */ + * final conv's one with its clamp; then a DAC encoder's strided causal downsampling (kernels of two + * strides, 2 to 8, and the quantizer's of one). Two sequences, so a tap crossing a sequence's edge + * reads zero; a strided case's sequences are whole strides. */ void conv1d_f32_cases(const Plugin& pl) { const RadKernelInfo* r = find(pl, "conv1d_f32"); if (!r) { ok("conv1d_f32", false, "row absent"); return; } - struct C { int64_t cin, cout, k, dil, M0, M1; int act, bias, res; }; /* res 2: y itself */ + struct C { int64_t cin, cout, k, dil, M0, M1; int act, bias, res; int64_t st = 1; }; /* res 2: y itself */ for (const C& c : { C{ 2048, 1024, 7, 1, 9, 15, 0, 1, 0 }, C{ 64, 128, 7, 1, 37, 60, 0, 1, 0 }, C{ 32, 32, 11, 5, 70, 41, 0, 1, 2 }, C{ 16, 16, 3, 3, 50, 77, 0, 1, 1 }, C{ 48, 40, 3, 1, 33, 30, 1, 1, 0 }, C{ 8, 8, 11, 5, 90, 64, 0, 1, 2 }, - C{ 8, 1, 7, 1, 100, 99, 2, 0, 0 }, C{ 64, 64, 11, 1, 200, 137, 2, 1, 2 } }) { + C{ 8, 1, 7, 1, 100, 99, 2, 0, 0 }, C{ 64, 64, 11, 1, 200, 137, 2, 1, 2 }, + C{ 64, 128, 4, 1, 36, 60, 0, 1, 0, 2 }, C{ 128, 256, 8, 1, 64, 40, 0, 1, 1, 4 }, + C{ 256, 512, 16, 1, 96, 136, 0, 1, 0, 8 }, C{ 8, 8, 16, 1, 32, 48, 0, 1, 0, 8 }, + C{ 64, 64, 2, 1, 50, 30, 0, 1, 2, 2 } }) { const std::vector cu = { 0, (int32_t)c.M0, (int32_t)(c.M0 + c.M1) }; - const int64_t M = c.M0 + c.M1; + const int64_t M = c.M0 + c.M1, MO = M / c.st; std::vector x = randv((size_t)(M * c.cin), 1.0f); std::vector w = randv((size_t)(c.cout * c.cin * c.k), 1.0f / std::sqrt((float)(c.cin * c.k))); - std::vector b = randv((size_t)c.cout, 0.5f), res = randv((size_t)(M * c.cout), 0.5f); + std::vector b = randv((size_t)c.cout, 0.5f), res = randv((size_t)(MO * c.cout), 0.5f); std::vector ps = { PI("M", M), PI("cin", c.cin), PI("cout", c.cout), PI("k", c.k), PI("dil", c.dil), PS("act", c.act == 1 ? "tanh" : c.act == 2 ? "clamp" : "none"), PS("dtype", "f32") }; - const std::string nm = fmt("conv1d_f32 cin %lld cout %lld k %lld dil %lld%s%s%s", (long long)c.cin, + if (c.st > 1) { ps.push_back(PI("causal", 1)); ps.push_back(PI("stride", c.st)); } + const std::string nm = fmt("conv1d_f32 cin %lld cout %lld k %lld dil %lld%s%s%s%s", (long long)c.cin, (long long)c.cout, (long long)c.k, (long long)c.dil, + c.st > 1 ? fmt(" causal stride %lld", (long long)c.st).c_str() : "", c.bias ? " +bias" : "", c.res == 2 ? " +res in place" : c.res ? " +res" : "", c.act == 1 ? " tanh" : c.act == 2 ? " clamp" : ""); void* dw = stored_weight_f32(r, ps, w, c.cout, c.cin * c.k, nm.c_str()); if (!dw) continue; void* dx = up_f32(x); void* db = up_f32(b); - void* dy = up_f32(c.res == 2 ? res : std::vector((size_t)(M * c.cout), 0.0f)); + void* dy = up_f32(c.res == 2 ? res : std::vector((size_t)(MO * c.cout), 0.0f)); void* dr = up_f32(res); - RadTensor ty = T(dy, RAD_F32, { M, c.cout }); - RadTensor tr = c.res == 2 ? ty : T(dr, RAD_F32, { M, c.cout }); + RadTensor ty = T(dy, RAD_F32, { MO, c.cout }); + RadTensor tr = c.res == 2 ? ty : T(dr, RAD_F32, { MO, c.cout }); RadTensor tb = T(db, RAD_F32, { c.cout }); if (!c.res) absent(tr); if (!c.bias) absent(tb); @@ -9369,14 +9488,14 @@ void conv1d_f32_cases(const Plugin& pl) { T(up_i32(cu), RAD_I32, { 3 }), tr, ty, tb }, ps); if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; } - const int64_t pad = c.dil * (c.k - 1) / 2; - std::vector want((size_t)(M * c.cout)); - for (int64_t row = 0; row < M; ++row) { - const int64_t e = seq_of(cu, row), s0 = cu[(size_t)e], s1 = cu[(size_t)e + 1]; + const int64_t pad = c.st > 1 ? c.dil * (c.k - 1) + 1 - c.st : c.dil * (c.k - 1) / 2; + std::vector want((size_t)(MO * c.cout)); + for (int64_t row = 0; row < MO; ++row) { + const int64_t e = seq_of(cu, row * c.st), s0 = cu[(size_t)e], s1 = cu[(size_t)e + 1]; for (int64_t co = 0; co < c.cout; ++co) { double acc = 0; for (int64_t kk = 0; kk < c.k; ++kk) { - const int64_t ir = row + kk * c.dil - pad; + const int64_t ir = row * c.st + kk * c.dil - pad; if (ir < s0 || ir >= s1) continue; for (int64_t ci = 0; ci < c.cin; ++ci) acc += (double)w[(size_t)((co * c.cin + ci) * c.k + kk)] * x[(size_t)(ir * c.cin + ci)]; @@ -25065,6 +25184,7 @@ static void device_phase(const Plugin& pl) { if (sel("cfg_topk_sample")) au::cfg_topk_cases(pl); if (sel("ras_pick")) au::ras_pick_cases(pl); if (sel("rvq_embed_bf16")) au::rvq_embed_cases(pl); + if (sel("vq_encode")) au::vq_encode_cases(pl); if (sel("frame_store")) au::frame_store_cases(pl); if (sel("iota")) au::iota_cases(pl); if (sel("gelu_bf16")) au::gelu_case(pl); diff --git a/libref/ref_audio.cpp b/libref/ref_audio.cpp index 2144cc07..68ba7b2e 100644 --- a/libref/ref_audio.cpp +++ b/libref/ref_audio.cpp @@ -51,8 +51,13 @@ 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). `res`, when bound, is added after the bias; then `act` (none, - * tanh, or clamp to [-1, 1], a vocoder's output clamp). f32 accumulation, one rounding. */ + * dil (k - 1), on the left (any k). A `stride` s > 1 (causal only, 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: sequence e is input rows [cu[e] / cu_div, + * cu[e+1] / cu_div), each bound a multiple of cu_div stride. `res`, when bound, is added after the + * bias; then `act` (none, tanh, or clamp to [-1, 1], a vocoder's output clamp). f32 + * accumulation, one rounding. */ int ref_conv1d(const RadArgs* a, RadStream) { if (!have(a, 3)) return RAD_E_INVAL; const RadTensor *x = &a->t[0], *w = &a->t[1], *cu = &a->t[2]; @@ -60,29 +65,38 @@ 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), 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; if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0) || dil <= 0 || - causal < 0 || causal > 1) + causal < 0 || causal > 1 || st < 1 || (st > 1 && (!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); - if (r0 < 0 || r1 <= r0 || rows_of(y, cout) < r1 || rows_of(x, cin) < r1) return RAD_E_SHAPE; + /* 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). */ + for (int64_t e = 0; e <= n_seq; ++e) + if (cu_at(cu, e) % (cd * st)) return RAD_E_SHAPE; + const int64_t r0 = cu_at(cu, 0) / cd, r1 = cu_at(cu, n_seq) / cd; + if (r0 < 0 || r1 <= r0 || rows_of(y, cout) < r1 / st || rows_of(x, cin) < r1) + return RAD_E_SHAPE; if (numel(w) < cout * cin * k || (b && numel(b) < cout)) return RAD_E_SHAPE; - if (res && rows_of(res, cout) < r1) return RAD_E_SHAPE; - const int64_t pad = causal ? dil * (k - 1) : dil * (k - 1) / 2; - REF_PAR_FOR((r1 - r0) * cout * cin * k) - for (int64_t r = r0; r < r1; ++r) { - const int64_t e = seq_of(cu, n_seq, r); - const int64_t s0 = cu_at(cu, e), s1 = cu_at(cu, e + 1); + if (res && rows_of(res, cout) < r1 / st) 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. */ + const int64_t pad = causal ? dil * (k - 1) + 1 - st : dil * (k - 1) / 2; + REF_PAR_FOR((r1 - r0) / st * cout * cin * k) + for (int64_t r = r0 / st; r < r1 / st; ++r) { + const int64_t e = seq_of(cu, n_seq, r * st * cd); + const int64_t s0 = cu_at(cu, e) / cd, s1 = cu_at(cu, e + 1) / cd; const int64_t yb = rowoff(y, r, cout), ys = laststride(y); for (int64_t co = 0; co < cout; ++co) { float acc = 0.0f; for (int64_t kk = 0; kk < k; ++kk) { - const int64_t ir = r + kk * dil - pad; + const int64_t ir = r * st + kk * dil - pad; if (ir < s0 || ir >= s1) continue; const int64_t xb = rowoff(x, ir, cin), xs = laststride(x); for (int64_t ci = 0; ci < cin; ++ci) @@ -418,6 +432,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 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 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 diff --git a/libref/ref_ops.h b/libref/ref_ops.h index ac9ce75a..622725bb 100644 --- a/libref/ref_ops.h +++ b/libref/ref_ops.h @@ -207,6 +207,7 @@ int ref_snake(const RadArgs*, RadStream); int ref_snake_aa(const RadArgs*, RadStream); int ref_cfg_topk_sample(const RadArgs*, RadStream); int ref_rvq_embed(const RadArgs*, RadStream); +int ref_vq_encode(const RadArgs*, RadStream); int ref_frame_mix_store(const RadArgs*, RadStream); int ref_end_status(const RadArgs*, RadStream); int ref_ras_pick(const RadArgs*, RadStream); @@ -447,6 +448,7 @@ REF_SHAPE_FN(ref_snake); REF_SHAPE_FN(ref_snake_aa); REF_SHAPE_FN(ref_cfg_topk_sample); REF_SHAPE_FN(ref_rvq_embed); +REF_SHAPE_FN(ref_vq_encode); REF_SHAPE_FN(ref_frame_mix_store); REF_SHAPE_FN(ref_end_status); REF_SHAPE_FN(ref_ras_pick); diff --git a/libref/ref_registry.cpp b/libref/ref_registry.cpp index a4c99769..7503a0a0 100644 --- a/libref/ref_registry.cpp +++ b/libref/ref_registry.cpp @@ -884,7 +884,8 @@ static const RadOperandSpec oBweUp[] = { OPD("x"), OPD("res"), OUT("y") }; /* ---- audio generation (ref_audio.cpp) */ 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"), - P_INTO("causal") }; + P_INTO("causal"), P_INTO("stride"), + P_INTO("cu_div") }; static const RadOperandSpec oConv1d[] = { OPD("x"), WGT("w"), OPD("cu_seqlens"), OPD_O("res"), OUT("y"), WGT_O("b") }; static const RadParamSpec pConvT1d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("stride"), @@ -908,6 +909,10 @@ static const RadParamSpec pRvqEmbed[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_ P_INT("vocab"), P_INT("off0"), P_F64("scale"), P_STR("dtype"), P_INTO("round"), P_INTO("clamp") }; static const RadOperandSpec oRvqEmbed[] = { OPD("codes"), WGT("t0"), WGT("t1"), OUT("y") }; +static const RadParamSpec pVqEncode[] = { P_INT("M"), P_INT("n"), P_INT("dim"), P_INT("bins"), + P_STR("dtype") }; +static const RadOperandSpec oVqEncode[] = { INOUT("x"), WGT("in_w"), WGT("in_b"), WGT("cb"), + WGT("out_w"), WGT("out_b"), OUT("codes") }; static const RadParamSpec pFrameMix[] = { P_INT("M"), P_INT("n_embd"), P_INT("L"), P_INT("stride"), P_INTO("rep"), P_STR("dtype") }; static const RadOperandSpec oFrameMix[] = { OPD("hid"), WGT("logits"), WGT("scale"), OPD("lane"), @@ -1769,6 +1774,10 @@ static const RadOpSchema kSchemas[] = { "a frame's embedding from code-major residual codes [n_codes, rows]: t0[c0 + off0] plus the sum " "of t1[c_k + (k - 1) vocab], times `scale`, rounded as bf16(bf16(t0 + bf16(sum)) * scale), or " "with `round` 1 one f32 sum in codebook order times scale, rounded once." }, +{ "vq_encode", ARR(pVqEncode), ARR(oVqEncode), + "one residual vector quantiser step: e = in_w x + in_b, the code the row of cb of greatest " + "cosine with e (the lower index on a tie), then x -= out_w cb[code] + out_b; f32, x rounded " + "once." }, { "frame_mix_store", ARR(pFrameMix), ARR(oFrameMix), "a frame's conditioning row: the softmax(logits)-weighted sum of hid's L planes times scale, " "stored at dst[lane * stride + frame - 1] for frame >= 1 and status[lane] >= 0." }, @@ -2345,6 +2354,8 @@ SIMPLE("ref_cfg_topk_sample", "cfg_topk_sample", "sample", "a guided top-k pick DT_FLOAT, ref_cfg_topk_sample), SIMPLE("ref_rvq_embed", "rvq_embed", "elem", "a frame's embedding from its residual codes", DT_FLOAT, ref_rvq_embed), +SIMPLE("ref_vq_encode", "vq_encode", "elem", + "a residual quantiser's step: the nearest code by cosine", DT_FLOAT, ref_vq_encode), SIMPLE("ref_frame_mix_store", "frame_mix_store", "elem", "a frame's conditioning row into its lane", DT_FLOAT, ref_frame_mix_store), SIMPLE("ref_end_status", "end_status", "elem", "a frame's status from its first pick", DT_ANY, diff --git a/libref/ref_shapes.cpp b/libref/ref_shapes.cpp index b9f074d7..def649be 100644 --- a/libref/ref_shapes.cpp +++ b/libref/ref_shapes.cpp @@ -3112,16 +3112,20 @@ static int64_t two_seqs(int64_t M) { return M >= 4 ? 2 : 1; } int ref_conv1d_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { const int64_t M = gi(p, n_p, "M", 0), cin = gi(p, n_p, "cin", 0), cout = gi(p, n_p, "cout", 0); const int64_t k = gi(p, n_p, "k", 0), causal = gi(p, n_p, "causal", 0); - if (M <= 0 || cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0)) + const int64_t st = gi(p, n_p, "stride", 1), dil = gi(p, n_p, "dil", 1); + if (M <= 0 || cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0) || st < 1 || + (st > 1 && (!causal || dil * (k - 1) + 1 < st || M % st)) || gi(p, n_p, "cu_div", 1) != 1) return RAD_E_UNSUPPORTED; const uint32_t D = act_dt(p, n_p); - const int64_t S = two_seqs(M); + /* A strided convolution is described over one sequence: two would each need a whole number + * of strides. Bounds in coarser units (cu_div) are not drawn; ref_test checks them. */ + const int64_t S = st > 1 ? 1 : two_seqs(M); Opd o; o.t(D, { M, cin }); /* x */ o.t(D, { cout, cin, k }); /* w */ o.t_idx({ S + 1 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */ - o.t(D, { M, cout }); /* res */ - o.t(D, { M, cout }); /* y */ + o.t(D, { M / st, cout }); /* res */ + o.t(D, { M / st, cout }); /* y */ o.t(D, { cout }); /* b */ return o.finish("conv1d", operand, out); } @@ -3199,6 +3203,22 @@ int ref_rvq_embed_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out return o.finish("rvq_embed", operand, out); } +int ref_vq_encode_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { + const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0); + const int64_t dim = gi(p, n_p, "dim", 0), bins = gi(p, n_p, "bins", 0); + if (M <= 0 || n <= 0 || dim <= 0 || bins <= 0) return RAD_E_UNSUPPORTED; + const uint32_t F = (uint32_t)RAD_F32; + Opd o; + o.t(act_dt(p, n_p), { M, n }); /* x, INOUT */ + o.t(F, { dim, n }); /* in_w */ + o.t(F, { dim }); /* in_b */ + o.t(F, { bins, dim }); /* cb */ + o.t(F, { n, dim }); /* out_w */ + o.t(F, { n }); /* out_b */ + o.t_idx({ M }, bins, -1, 0); /* codes, OUT */ + return o.finish("vq_encode", operand, out); +} + int ref_frame_mix_store_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { const int64_t M = gi(p, n_p, "M", 0), D = gi(p, n_p, "n_embd", 0), L = gi(p, n_p, "L", 0); const int64_t st = gi(p, n_p, "stride", 0); diff --git a/tests/ref_test.cpp b/tests/ref_test.cpp index 6d182677..560b6661 100644 --- a/tests/ref_test.cpp +++ b/tests/ref_test.cpp @@ -970,8 +970,9 @@ TEST(op_vocabulary) { "conv1d", "conv_t1d", "snake", "cfg_topk_sample", "rvq_embed", "frame_mix_store", "end_status", "resample_nearest", "fourier_embed", "win_input", "win_prep", "win_finish", "chan_split", "interleave2", "iota", "cu_repeat", "int_affine", "frame_store", - /* Fish Audio drama-3's (arch/fishomni): the repetition-aware choice between two draws. */ - "ras_pick", + /* Fish Audio drama-3's (arch/fishomni): the repetition-aware choice between two draws, + * and its codec encoder's residual quantiser step. */ + "ras_pick", "vq_encode", /* The video generator's (MiniMax-H3): its DiT's modulated norm and sampler step, its * decoder's patch unfold, tiles, stitch and positions, and the vocoder's alias-free * activation. */ @@ -6998,6 +6999,68 @@ TEST(conv1d_act_clamp_bounds_the_output) { CHECK_EQ(y.at(2), -1.0f); CHECK_EQ(y.at(3), -0.75f); } +/* conv1d `stride`: Fish Audio's causal downsampling conv (modded_dac.py, CausalConvNet), the reach + * k less the stride padded on the left. k 4, stride 2 over [1..8] pads 2: windows [0 0 1 2], + * [1 2 3 4], [3 4 5 6], [5 6 7 8] against taps 1, 10, 100, 1000 are 2100, 4321, 6543, 8765. A + * second sequence [1 2 3 4] starts its own padding; a bound that is not a whole stride, and a + * stride on the symmetric form, are refused. `cu_div` reads the same bounds in samples. */ +TEST(conv1d_stride_downsamples_each_sequence_causally) { + T x(RAD_F32, {12, 1}); x.f({1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4}); + T w(RAD_F32, {1, 1, 4}); w.f({1, 10, 100, 1000}); + T cu(RAD_I32, {3}); cu.n({0, 8, 12}); + T y(RAD_F32, {6, 1}); + A a; a.t(x).t(w).t(cu).tnull().t(y).i("M", 12).i("cin", 1).i("cout", 1).i("k", 4) + .i("dil", 1).i("causal", 1).i("stride", 2).s("dtype", "f32"); + CHECK_OK(run("conv1d", a)); + CHECK_EQ(y.at(0), 2100.0f); CHECK_EQ(y.at(1), 4321.0f); + CHECK_EQ(y.at(2), 6543.0f); CHECK_EQ(y.at(3), 8765.0f); + CHECK_EQ(y.at(4), 2100.0f); CHECK_EQ(y.at(5), 4321.0f); + T cu2(RAD_I32, {3}); cu2.n({0, 7, 12}); + A b; b.t(x).t(w).t(cu2).tnull().t(y).i("M", 12).i("cin", 1).i("cout", 1).i("k", 4) + .i("dil", 1).i("causal", 1).i("stride", 2).s("dtype", "f32"); + CHECK_EQ(run("conv1d", b), RAD_E_SHAPE); + T w3(RAD_F32, {1, 1, 3}); w3.f({1, 1, 1}); + A c; c.t(x).t(w3).t(cu).tnull().t(y).i("M", 12).i("cin", 1).i("cout", 1).i("k", 3) + .i("dil", 1).i("stride", 2).s("dtype", "f32"); + CHECK_EQ(run("conv1d", c), RAD_E_SHAPE); + /* The same bounds in units of 4 rows (samples, 4 a row): the same rows; a bound that is not + * a whole number of strides in rows is refused. */ + T y2(RAD_F32, {6, 1}); + T cu4(RAD_I32, {3}); cu4.n({0, 32, 48}); + A d; d.t(x).t(w).t(cu4).tnull().t(y2).i("M", 12).i("cin", 1).i("cout", 1).i("k", 4) + .i("dil", 1).i("causal", 1).i("stride", 2).i("cu_div", 4).s("dtype", "f32"); + CHECK_OK(run("conv1d", d)); + for (int64_t i = 0; i < 6; ++i) CHECK_EQ(y2.at(i), y.at(i)); + T cu6(RAD_I32, {3}); cu6.n({0, 28, 48}); + A e; e.t(x).t(w).t(cu6).tnull().t(y2).i("M", 12).i("cin", 1).i("cout", 1).i("k", 4) + .i("dil", 1).i("causal", 1).i("stride", 2).i("cu_div", 4).s("dtype", "f32"); + CHECK_EQ(run("conv1d", e), RAD_E_SHAPE); +} + +/* vq_encode by hand. in_w scales the first channel by 2; codebook rows 0 and 2 point the same way + * (a cosine tie, so the lower index), row 1 the other; out_w the identity and out_b + * [0.5, -0.5]. Row [4, 1]: e [8, 1], code 0, x - [1.5, -0.5] = [2.5, 1.5]. Row [-1, 3]: e [-2, 3], + * code 1, x - [0.5, 1.5] = [-1.5, 1.5]. Row [0, 0]: e 0, every cosine 0, code 0. */ +TEST(vq_encode_takes_the_nearest_code_by_cosine_and_leaves_the_residual) { + T x(RAD_F32, {3, 2}); x.f({4, 1, -1, 3, 0, 0}); + T iw(RAD_F32, {2, 2}); iw.f({2, 0, 0, 1}); + T ib(RAD_F32, {2}); ib.f({0, 0}); + T cb(RAD_F32, {3, 2}); cb.f({1, 0, 0, 2, 3, 0}); + T ow(RAD_F32, {2, 2}); ow.f({1, 0, 0, 1}); + T ob(RAD_F32, {2}); ob.f({0.5f, -0.5f}); + T codes(RAD_I32, {3}); codes.n({-1, -1, -1}); + A a; a.t(x).t(iw).t(ib).t(cb).t(ow).t(ob).t(codes).i("M", 3).i("n", 2).i("dim", 2) + .i("bins", 3).s("dtype", "f32"); + CHECK_OK(run("vq_encode", a)); + CHECK_EQ(codes.iat(0), 0); CHECK_EQ(codes.iat(1), 1); CHECK_EQ(codes.iat(2), 0); + CHECK_EQ(x.at(0), 2.5f); CHECK_EQ(x.at(1), 1.5f); + CHECK_EQ(x.at(2), -1.5f); CHECK_EQ(x.at(3), 1.5f); + CHECK_EQ(x.at(4), -1.5f); CHECK_EQ(x.at(5), 0.5f); + A b; b.t(x).t(iw).t(ib).t(cb).t(ow).t(ob).t(codes).i("M", 3).i("n", 2).i("dim", 2) + .i("bins", 4).s("dtype", "f32"); + CHECK_EQ(run("vq_encode", b), RAD_E_SHAPE); +} + /* snake_aa's paddings, with the activation the identity (alpha 0: sin(0 u) = 0). Ratio 2, filters * of 4: P = 1. Sequence [10, 20, 40] pads to [10, 10, 20, 40, 40]; the transposed f_up [1, 2, 3, 4] * gives [10, 20, 40, 60, 50, 80, 100, 160, 160, 240, 120, 160], doubled and cropped by 3 a side: @@ -8127,6 +8190,8 @@ std::vector geometries() { .i("rep", 2).s("dtype", "bf16")), G("end_status", "M", 2, a.i("M", R).i("end", 5).i("rep", 2)), G("ras_pick", "M", 2, a.i("M", R).i("win", 4).i("lo", 1).i("hi", 9).i("rep", 1)), + G("vq_encode", "M", 3, a.i("M", R).i("n", 16).i("dim", 4).i("bins", 24) + .s("dtype", "f32")), G("resample_nearest", "M", 6, a.i("M", R).i("n", 8).s("dtype", "f32")), G("fourier_embed", "M", 3, a.i("M", R).i("n", 8).s("dtype", "f32")), G("win_input", "M", 8, a.i("M", R).i("n_lat", 4).i("n_zero", 2).i("n_cond", 3)