From 0c35fc8070b170764aba2a5131de898e89df61bb Mon Sep 17 00:00:00 2001 From: zivius Date: Tue, 20 Oct 2026 20:52:57 +0200 Subject: [PATCH] libref, libavx, libr4d: cfg_topk_sample's top_p and ras_pick -- Fish Audio's nucleus over the whole candidate set, cut before the top-k, and its repetition-aware choice between two draws --- docs/OPS.md | 16 ++++-- libavx/avx_audio.cpp | 60 ++++++++++++++++++++++- libavx/avx_cases.cpp | 9 ++++ libavx/avx_isa.h | 2 +- libavx/avx_registry.cpp | 12 ++++- libr4d/r4d_audio.hip | 84 +++++++++++++++++++++++++++++-- libr4d/r4d_plugin.h | 1 + libr4d/r4d_rows.cpp | 18 ++++++- libr4d/r4d_selftest.cpp | 106 ++++++++++++++++++++++++++++++++++++---- libref/ref_audio.cpp | 66 ++++++++++++++++++++++++- libref/ref_ops.h | 2 + libref/ref_registry.cpp | 13 ++++- libref/ref_shapes.cpp | 15 ++++++ tests/ref_test.cpp | 3 ++ 14 files changed, 384 insertions(+), 23 deletions(-) diff --git a/docs/OPS.md b/docs/OPS.md index 0897ac51..1714484c 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -1171,10 +1171,11 @@ libref function says which (`libref/ref_audio.cpp`). | `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 | `logits`, `seed`, `frame` → `ids`; `cfg`?, `lane`?, `hist`:inout? | +| `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 | `codes`, `t0`w, `t1`w → `y` | | `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` | | `frame_store` | `M`(range) `n_codes` `stride` `rep`/opt `skip`/opt | `codes`, `lane`, `frame`, `status`, `dst`:inout | | `resample_nearest` | `M`(range) `n` `dtype` | `x` → `y` | | `fourier_embed` | `M`(range) `n` `round`/opt `dtype` | `t`, `w`w → `y` | @@ -1198,8 +1199,17 @@ conditional first, then the unconditional) are consecutive rows, `rep` of them a `cond_k` > 0 only the entries where `c` reaches its `cond_k`-th largest value are. 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`, `u = rad_philox_uniform_s(seed, 2^32 + frame, stream)` - (`abi/rad_philox.h`); the pick goes to every one of the lane's `rep` slots. `logits` - `[rep n, n_vocab]` f32 or bf16, `seed` `[n, 2]` u32 low word first, `frame` `[n]`. + (`abi/rad_philox.h`); the pick goes to every one of the lane's `rep` slots. With `top_p` < 1 + (Fish Audio's draw) the kept list is cut first: over the softmax of every candidate's `g / temp` + (its normaliser summed in index order), a kept rank whose inclusive cumulative probability in + descending `g` exceeds `top_p` is dropped, rank 0 never, so the nucleus is taken over the whole + distribution and the `k` cut applies after it. `logits` `[rep n, n_vocab]` f32 or bf16, `seed` + `[n, 2]` u32 low word first, `frame` `[n]`. +- `ras_pick`: Fish Audio's repetition-aware sampling, a choice between two draws of each lane: + `ids[rep i] = alt[rep i]` when `first[rep i]` is in `[lo, hi)` and the lane's window holds it, + else `first[rep i]`. The window `hist` `[lanes, win]` i32 is the lane's last `win` picks, oldest + first, -1 empty (`clear` 1 empties it first, for a piece's first pick); the pick then enters it + and the oldest leaves. A pick outside `[lo, hi)` (an end of speech) is never redrawn. - `end_status`: `status[lane[i]] = ids[rep i] == end ? -1 : ids[rep i]`. - `rvq_embed`: `codes` is code-major `[n_codes, rows]`, read at the operand's own strides (a block of a wider plane is a view); diff --git a/libavx/avx_audio.cpp b/libavx/avx_audio.cpp index 99e16e1a..7f2559f4 100644 --- a/libavx/avx_audio.cpp +++ b/libavx/avx_audio.cpp @@ -590,8 +590,10 @@ AVX_KERNEL(cfg_topk_sample) { 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 || @@ -650,11 +652,29 @@ AVX_KERNEL(cfg_topk_sample) { } else { for (int64_t j = 0; j < V; ++j) if (open(j)) idx[nc++] = j; } - const int64_t kk = std::min(K, nc); + /* top_p's normaliser over every candidate in index order (idx still is), as libref. */ + double z = 0; + if (top_p < 1.0) { + float gm = g[idx[0]]; + for (int64_t t = 1; t < nc; ++t) gm = std::max(gm, g[idx[t]]); + const float m0 = gm / temp; + for (int64_t t = 0; t < nc; ++t) z += std::exp((double)(g[idx[t]] / temp - m0)); + } + int64_t kk = std::min(K, nc); std::partial_sort(idx, idx + kk, idx + nc, [g](int64_t p, int64_t q) { return g[p] > g[q] || (g[p] == g[q] && p < q); }); const float m = g[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[idx[t]] / temp - m)) / z; + if (t > 0 && cum > top_p) break; + keep = t + 1; + } + kk = keep; + } double tot = 0; for (int64_t t = 0; t < kk; ++t) { e[t] = std::exp((double)(g[idx[t]] / temp - m)); @@ -789,6 +809,42 @@ AVX_KERNEL(frame_mix_store) { return RAD_OK; } +/* ------------------------------------------------------------------ ras_pick */ +/* libref's rule: the first draw unless it lies in [lo, hi) and the lane's window of its last `win` + * picks holds it, then the alternative; the pick enters the window. Lanes are checked before + * anything is written and run in order, as two entries may share a lane's window. */ +AVX_KERNEL(ras_pick) { + if (!rad_args_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 = at(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 = at(lane, i) * W; + if (clear) + for (int64_t w = 0; w < W; ++w) st_int_dt(hist->dtype, hist->data, offlin(hist, hb + w), -1); + const int64_t f = at(first, rep * i); + int64_t pick = f; + if (f >= lo && f < hi) + for (int64_t w = 0; w < W; ++w) + if (at(hist, hb + w) == f) { pick = at(alt, rep * i); break; } + for (int64_t r = 0; r < rep; ++r) + st_int_dt(ids->dtype, ids->data, offlin(ids, rep * i + r), pick); + for (int64_t w = 0; w + 1 < W; ++w) + st_int_dt(hist->dtype, hist->data, offlin(hist, hb + w), at(hist, hb + w + 1)); + st_int_dt(hist->dtype, hist->data, offlin(hist, hb + W - 1), pick); + } + return RAD_OK; +} + /* ------------------------------------------------------------------ end_status */ /* status[lane[i]] = ids[rep i] == end ? -1 : ids[rep i]. */ AVX_KERNEL(end_status) { diff --git a/libavx/avx_cases.cpp b/libavx/avx_cases.cpp index cdc34b1b..5be9c307 100644 --- a/libavx/avx_cases.cpp +++ b/libavx/avx_cases.cpp @@ -998,6 +998,12 @@ std::vector build_cases() { add("cfg_topk_sample", { HI("M", 6), HI("n_vocab", 645), HI("k", 645), HI("rep", 2), HF("cfg", 1.7), HF("temp", 0.8), HI("stream", 0), HS("dtype", "f32") }); + /* Fish Audio's draw: the nucleus over the whole candidate set's softmax, then the top-k cut; + * at a small top_p the head alone, and above 1 none. */ + for (double tp : { 0.7, 0.05, 0.95 }) + add("cfg_topk_sample", { HI("M", 4), HI("n_vocab", 645), HI("k", 30), HI("rep", 1), + HF("cfg", 1.0), HF("temp", 0.7), HI("stream", 2), + HF("top_p", tp), HS("dtype", "f32") }); /* A speech model's backbone pick: reserved ids suppressed (the vocabulary's last class stays a * candidate), a temperature, the penalty idle without a history. */ for (int rep : { 1, 2 }) @@ -1038,6 +1044,9 @@ std::vector build_cases() { } add("end_status", { HI("M", 2), HI("end", 5), HI("rep", 2) }); add("end_status", { HI("M", 4), HI("end", 3), HI("rep", 1) }); + 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) }); 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_isa.h b/libavx/avx_isa.h index 57fac4df..09b53658 100644 --- a/libavx/avx_isa.h +++ b/libavx/avx_isa.h @@ -134,7 +134,7 @@ int avx_par_force(int threads); 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(int_affine) \ + X(cu_repeat) X(snake_aa) X(int_affine) X(ras_pick) \ /* ---- 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 11470960..ba115f6f 100644 --- a/libavx/avx_registry.cpp +++ b/libavx/avx_registry.cpp @@ -699,7 +699,8 @@ static const RadOperandSpec oSnakeAa[] = { OPD("x"), WGT("alpha"), WGT("beta"), static const RadParamSpec pCfgTopk[] = { P_INT("M"), P_INT("n_vocab"), P_INT("k"), P_INT("rep"), P_INTO("cond_k"), P_F64("cfg"), P_F64O("temp"), P_INT("stream"), P_STR("dtype"), P_INTO("sup_lo"), - P_INTO("sup_hi"), P_F64O("penalty") }; + P_INTO("sup_hi"), P_F64O("penalty"), + P_F64O("top_p") }; static const RadOperandSpec oCfgTopk[] = { OPD("logits"), OPD("seed"), OPD("frame"), OUT("ids"), OPD_O("cfg"), OPD_O("lane"), INOUT_O("hist") }; static const RadParamSpec pRvqEmbed[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_codes"), @@ -712,6 +713,10 @@ static const RadOperandSpec oFrameMix[] = { OPD("hid"), WGT("logits"), WGT("scal OPD("frame"), OPD("status"), INOUT("dst") }; static const RadParamSpec pEndStatus[] = { P_INT("M"), P_INT("end"), P_INT("rep") }; static const RadOperandSpec oEndStatus[] = { OPD("ids"), OPD("lane"), INOUT("status") }; +static const RadParamSpec pRasPick[] = { P_INT("M"), P_INT("win"), P_INT("lo"), P_INT("hi"), + P_INTO("rep"), P_INTO("clear") }; +static const RadOperandSpec oRasPick[] = { OPD("first"), OPD("alt"), OPD("lane"), INOUT("hist"), + OUT("ids") }; static const RadParamSpec pFrameStore[] = { P_INT("M"), P_INT("n_codes"), P_INT("stride"), P_INTO("rep"), P_INTO("skip") }; static const RadOperandSpec oFrameStore[] = { OPD("codes"), OPD("lane"), OPD("frame"), @@ -1185,6 +1190,9 @@ static const RadOpSchema kSchemas[] = { "a frame's softmax-weighted mix of hidden planes, stored into its lane, bit for bit libref's." }, { "end_status", ARR(pEndStatus), ARR(oEndStatus), "status[lane[i]] = ids[rep i] == end ? -1 : ids[rep i]." }, +{ "ras_pick", ARR(pRasPick), ARR(oRasPick), + "repetition-aware sampling: the alternative draw where the first is in [lo, hi) and in the " + "lane's window of its last picks, else the first; the pick enters the window." }, { "frame_store", ARR(pFrameStore), ARR(oFrameStore), "a frame's codes into its lane's history, code-major rows over the lane's frames." }, { "resample_nearest", ARR(pMnD), ARR(oUnary), @@ -1714,6 +1722,8 @@ 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, L(end_status)), +SIMPLE("avx_ras_pick", "ras_pick", "elem", "a repetition-aware choice between two draws a lane", + DT_ANY, L(ras_pick)), SIMPLE("avx_frame_store", "frame_store", "elem", "a frame's codes into its lane's history", DT_ANY, L(frame_store)), SIMPLE("avx_resample_nearest", "resample_nearest", "elem", "rows resampled nearest-neighbour", diff --git a/libr4d/r4d_audio.hip b/libr4d/r4d_audio.hip index 1cf06249..95f974ff 100644 --- a/libr4d/r4d_audio.hip +++ b/libr4d/r4d_audio.hip @@ -622,6 +622,11 @@ __device__ uint32_t kth_key(F val, int j0, int j1, int kth, int* sh) { // [sup_lo, sup_hi) are optional. The kept values are divided by the temperature, as libref divides: // a multiply by its inverse rounds differently and can move a pick across a CDF boundary. A lane // with no candidate left writes -1. +// +// `top_p` < 1 cuts the kept list where the whole candidate set's cumulative probability passes it +// (Fish Audio's draw). The normaliser is each thread's slice summed in index order, the slices +// then summed by thread 0 in thread order: index order as libref sums, associated differently, +// which in double moves a cut only for a cumulative within ~1e-16 of top_p. constexpr int kMaxK = 256; __global__ __launch_bounds__(kT) void cfg_topk_kernel( @@ -629,9 +634,10 @@ __global__ __launch_bounds__(kT) void cfg_topk_kernel( const int* __restrict__ frame, int* __restrict__ ids, int V, int K, int rep, int cond_k, float cfg, float temp, uint64_t stream, const float* __restrict__ cfgs, const int* __restrict__ lane, int* __restrict__ hist, int n_hist, int sup_lo, int sup_hi, - float pen) { + float pen, float top_p) { __shared__ int sh[8]; __shared__ int s_pre[kT]; + __shared__ double s_z[kT]; __shared__ float s_g[kMaxK]; __shared__ int s_i[kMaxK]; __shared__ float s_e[kMaxK]; @@ -721,8 +727,29 @@ __global__ __launch_bounds__(kT) void cfg_topk_kernel( s_pre[rank] = mi; } __syncthreads(); + if (top_p < 1.f) { + const float m = s_e[0] / temp; + double z = 0; + for (int j = j0; j < j1; ++j) + if (cand(j)) z += exp((double)(gval(j) / temp - m)); + s_z[tid] = z; + } + __syncthreads(); if (tid == 0) { const float m = s_e[0] / temp; + int kp = kk; + if (top_p < 1.f) { + double z = 0; + for (int t = 0; t < kT; ++t) z += s_z[t]; + double cum = 0; + kp = 1; + for (int t = 0; t < kk; ++t) { + cum += exp((double)(s_e[t] / temp - m)) / z; + if (t > 0 && cum > (double)top_p) break; + kp = t + 1; + } + } + const int kk = kp; double tot = 0; for (int t = 0; t < kk; ++t) tot += exp((double)(s_e[t] / temp - m)); const double u = (double)rad_philox_uniform_s(seed_of(seed, ln), @@ -820,6 +847,32 @@ __global__ __launch_bounds__(kT) void frame_mix_kernel( } } +// Fish Audio's repetition-aware choice between two draws, a thread a lane (libref's +// ref_ras_pick): the alternative where the first draw is in [lo, hi) and in the lane's window of +// its last `W` picks, the pick then entering the window. A lane whose window lies outside `hist` +// takes the first draw and keeps no window. +__global__ __launch_bounds__(kT) void ras_pick_kernel( + const int* __restrict__ first, const int* __restrict__ alt, const int* __restrict__ lane, + int* __restrict__ hist, int* __restrict__ ids, int n, int W, int lo, int hi, int rep, + int clear, long long n_hist) { + const int i = blockIdx.x * kT + threadIdx.x; + if (i >= n) return; + const int f = first[rep * i]; + const long long L = lane[i]; + int pick = f; + if (L >= 0 && (L + 1) * W <= n_hist) { + int* h = hist + L * W; + if (clear) + for (int w = 0; w < W; ++w) h[w] = -1; + if (f >= lo && f < hi) + for (int w = 0; w < W; ++w) + if (h[w] == f) { pick = alt[rep * i]; break; } + for (int w = 0; w + 1 < W; ++w) h[w] = h[w + 1]; + h[W - 1] = pick; + } + for (int r = 0; r < rep; ++r) ids[rep * i + r] = pick; +} + __global__ __launch_bounds__(kT) void end_status_kernel( const int* __restrict__ ids, const int* __restrict__ lane, int* __restrict__ status, int n, int rep, int end) { @@ -1303,9 +1356,11 @@ extern "C" int r4d_rad_cfg_topk_sample(const RadArgs* a, RadStream s) { const long long sup_lo = r4d_geti_or(a, "sup_lo", 0), sup_hi = r4d_geti_or(a, "sup_hi", 0); const double cfg = r4d_getf(a, "cfg", 1.0), temp = r4d_getf(a, "temp", 1.0); const double pen = r4d_getf(a, "penalty", 1.0); + /* As f32, as libref and libavx read it, so all three cut at the same value. */ + const float top_p = (float)r4d_getf(a, "top_p", 1.0); if (V <= 0 || K <= 0 || K > kMaxK || K > V || rep < 1 || rep > 2 || ck < 0 || ck > V || !(temp > 0) || V > (1 << 16) * kT || sup_lo < 0 || sup_hi < sup_lo || sup_hi > V || - !(pen > 0) || (!hist != !lane)) + !(pen > 0) || (!hist != !lane) || !(top_p >= 0.f)) return R4D_NO(RAD_E_SHAPE); long long rl = 0; const long long ll = r4d_rows_pitch(lg, V, &rl); @@ -1321,7 +1376,30 @@ extern "C" int r4d_rad_cfg_topk_sample(const RadArgs* a, RadStream s) { cfgs ? (const float*)cfgs->data : nullptr, lane ? (const int*)lane->data : nullptr, hist ? (int*)hist->data : nullptr, hist ? (int)(r4d_numel(hist) / V) : 0, (int)sup_lo, (int)sup_hi, - (float)pen); + (float)pen, top_p); + return st_of(hipGetLastError()); +} + +extern "C" int r4d_rad_ras_pick(const RadArgs* a, RadStream s) { + const RadTensor *first = r4d_opt(a, 0), *alt = r4d_opt(a, 1), *lane = r4d_opt(a, 2); + const RadTensor *hist = r4d_opt(a, 3), *ids = r4d_opt(a, 4); + if (!first || !alt || !lane || !hist || !ids) return R4D_NO(RAD_E_INVAL); + if (!is_i32(first) || !is_i32(alt) || !is_i32(lane) || !is_i32(hist) || !is_i32(ids)) + return R4D_NO(RAD_E_DTYPE); + if (!r4d_contig(first) || !r4d_contig(alt) || !r4d_contig(lane) || !r4d_contig(hist) || + !r4d_contig(ids)) + return R4D_NO(RAD_E_STRIDE); + const long long W = r4d_geti_or(a, "win", 0), lo = r4d_geti_or(a, "lo", 0); + const long long hi = r4d_geti_or(a, "hi", 0), rep = r4d_geti_or(a, "rep", 1); + const long long clear = r4d_geti_or(a, "clear", 0); + const long long n = r4d_numel(lane); + if (W <= 0 || lo < 0 || hi < lo || rep < 1 || clear < 0 || clear > 1 || n <= 0 || + r4d_numel(first) < rep * n || r4d_numel(alt) < rep * n || r4d_numel(ids) < rep * n) + return R4D_NO(RAD_E_SHAPE); + hipLaunchKernelGGL(ras_pick_kernel, dim3(blocks_for(n)), dim3(kT), 0, (hipStream_t)s, + (const int*)first->data, (const int*)alt->data, (const int*)lane->data, + (int*)hist->data, (int*)ids->data, (int)n, (int)W, (int)lo, (int)hi, + (int)rep, (int)clear, (long long)r4d_numel(hist)); return st_of(hipGetLastError()); } diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index 18c7625a..fcb48fb1 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -202,6 +202,7 @@ int r4d_rad_cfg_topk_sample (const RadArgs*, RadStream); int r4d_rad_rvq_embed (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); int r4d_rad_frame_store (const RadArgs*, RadStream); int r4d_rad_resample_nearest (const RadArgs*, RadStream); int r4d_rad_fourier_embed (const RadArgs*, RadStream); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index a2930559..1bfaed4e 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -2391,7 +2391,8 @@ static const RadOperandSpec oSnake[] = { static const RadParamSpec pCfgTopk[] = { P_INT("M"), P_INT("n_vocab"), P_INT("k"), P_INT("rep"), P_INTO("cond_k"), P_F64("cfg"), P_F64O("temp"), P_INT("stream"), P_STR("dtype"), P_INTO("sup_lo"), - P_INTO("sup_hi"), P_F64O("penalty") }; + P_INTO("sup_hi"), P_F64O("penalty"), + P_F64O("top_p") }; static const RadOperandSpec oCfgTopk[] = { { "logits", RAD_OPD_IN, 0 }, { "seed", RAD_OPD_IN, 0 }, { "frame", RAD_OPD_IN, 0 }, { "ids", RAD_OPD_OUT, 0 }, { "cfg", RAD_OPD_IN, 1 }, { "lane", RAD_OPD_IN, 1 }, @@ -2415,6 +2416,12 @@ static const RadParamSpec pEndStatus[] = { P_INT("M"), P_INT("end"), P_INT("rep" static const RadOperandSpec oEndStatus[] = { { "ids", RAD_OPD_IN, 0 }, { "lane", RAD_OPD_IN, 0 }, { "status", RAD_OPD_INOUT, 0 }, }; +static const RadParamSpec pRasPick[] = { P_INT("M"), P_INT("win"), P_INT("lo"), P_INT("hi"), + P_INTO("rep"), P_INTO("clear") }; +static const RadOperandSpec oRasPick[] = { + { "first", RAD_OPD_IN, 0 }, { "alt", RAD_OPD_IN, 0 }, { "lane", RAD_OPD_IN, 0 }, + { "hist", RAD_OPD_INOUT, 0 }, { "ids", RAD_OPD_OUT, 0 }, +}; static const RadParamSpec pFrameStore[] = { P_INT("M"), P_INT("n_codes"), P_INT("stride"), P_INTO("rep"), P_INTO("skip") }; static const RadOperandSpec oFrameStore[] = { @@ -3335,6 +3342,9 @@ static const RadOpSchema kSchemas[] = { "a frame's conditioning row (softmax-weighted planes times scale) into its lane."), SCHEMA("end_status", pEndStatus, oEndStatus, "status[lane[i]] = ids[rep i] == end ? -1 : ids[rep i]."), + SCHEMA("ras_pick", pRasPick, oRasPick, + "repetition-aware sampling: the alternative draw where the first is in [lo, hi) and in " + "the lane's window of its last picks, else the first; the pick enters the window."), SCHEMA("frame_store", pFrameStore, oFrameStore, "a frame's codes into its lane's history: dst[(lane n_codes + k) stride + frame - skip] " "= codes[k, rep i] when frame >= skip and status[lane] >= 0."), @@ -8292,6 +8302,12 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, nullptr, 0, NOTUNE, nullptr, nullptr, r4d_rad_end_status }, +{ "ras_pick", "ras_pick", "elem", + "a repetition-aware choice between two draws a lane", "a thread a lane", "i32", + RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, + nullptr, 0, NOTUNE, + nullptr, nullptr, r4d_rad_ras_pick }, + { "frame_store", "frame_store", "elem", "a frame's codes into its lane's history", "a thread a (lane, code); dst contiguous", "i32", RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index afb0e265..a071e8ba 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -5779,10 +5779,14 @@ void snake_cases(const Plugin& pl) { } } +/* Picks whose nucleus ended inside the top k, so that a top_p case is known to test the cut. */ +int64_t g_topp_cuts = 0; + /* ref_cfg_topk_sample's rule on the host, for one lane, over f32 logits. */ int ref_pick(const std::vector& lg, int64_t V, int64_t lane_row, int rep, float cfg, int64_t K, int64_t cond_k, float temp, int64_t sup_lo, int64_t sup_hi, float pen, - std::vector* hist, int64_t hb, uint64_t seed, int32_t frame, uint64_t stream) { + std::vector* hist, int64_t hb, uint64_t seed, int32_t frame, uint64_t stream, + float top_p = 1.0f) { std::vector c((size_t)V), g((size_t)V); for (int64_t j = 0; j < V; ++j) c[(size_t)j] = lg[(size_t)(rep * lane_row * V + j)]; if (rep == 2) { @@ -5815,11 +5819,28 @@ int ref_pick(const std::vector& lg, int64_t V, int64_t lane_row, int rep, if (open(j)) idx.push_back(j); } if (idx.empty()) return -1; - const int64_t kk = std::min(K, (int64_t)idx.size()); + double z = 0; + if (top_p < 1.0f) { + float gm = g[(size_t)idx[0]]; + for (const int64_t j : idx) gm = std::max(gm, g[(size_t)j]); + for (const int64_t j : idx) z += std::exp((double)(g[(size_t)j] / temp - gm / temp)); + } + int64_t kk = std::min(K, (int64_t)idx.size()); std::partial_sort(idx.begin(), idx.begin() + kk, idx.end(), [&](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.0f) { + 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 > (double)top_p) break; + keep = t + 1; + } + if (keep < kk) ++g_topp_cuts; + kk = keep; + } double tot = 0; std::vector e((size_t)kk); for (int64_t t = 0; t < kk; ++t) { @@ -5844,16 +5865,26 @@ int ref_pick(const std::vector& lg, int64_t V, int64_t lane_row, int rep, void cfg_topk_cases(const Plugin& pl) { const RadKernelInfo* r = find(pl, "cfg_topk_sample"); if (!r) { ok("cfg_topk_sample", false, "row absent"); return; } - struct C { int64_t V; int rep; bool hist, cfgs; int64_t sup_lo, sup_hi, cond_k; float temp; }; + struct C { + int64_t V; int rep; bool hist, cfgs; int64_t sup_lo, sup_hi, cond_k; float temp; + float top_p = 1.0f; int64_t K = 50; + }; + /* The last two are Fish Audio's draws (arch/fishomni): its main pick over eos and 4096 + * semantic ids at temperature 0.7, nucleus 0.7, top 30, and its repetition-aware redraw at 1.0, + * 0.95, 30. */ for (const C& c : { C{ 2052, 2, true, true, 2048, 2051, 0, 0.9f }, C{ 2051, 1, true, false, 2048, 2051, 0, 0.9f }, C{ 2051, 2, false, true, 2048, 2051, 0, 0.9f }, - C{ 16385, 2, false, false, 0, 0, 50, 1.0f } }) { - const int64_t n = 3, lanes = 4, K = 50; - const std::string nm = fmt("cfg_topk_sample V %lld rep %d%s%s sup [%lld, %lld) cond_k %lld", + C{ 16385, 2, false, false, 0, 0, 50, 1.0f }, + C{ 4097, 1, false, false, 0, 0, 0, 0.7f, 0.7f, 30 }, + C{ 4097, 1, false, false, 0, 0, 0, 1.0f, 0.95f, 30 } }) { + const int64_t n = 3, lanes = 4, K = c.K; + const std::string nm = fmt("cfg_topk_sample V %lld rep %d%s%s sup [%lld, %lld) cond_k %lld" + " top_p %g k %lld", (long long)c.V, c.rep, c.hist ? " hist" : "", c.cfgs ? " per-lane cfg" : "", (long long)c.sup_lo, - (long long)c.sup_hi, (long long)c.cond_k); + (long long)c.sup_hi, (long long)c.cond_k, (double)c.top_p, + (long long)K); std::vector lane = { 2, 0, 3 }; std::vector cfgv = { 1.0f, 3.0f, 4.0f }; std::vector seed = { 7u, 0u, 0x9e3779b9u, 1u, 0xdeadbeefu, 0x12345u }; @@ -5876,6 +5907,9 @@ void cfg_topk_cases(const Plugin& pl) { for (size_t i = 0; i < lg.size(); ++i) { const float v = rnd() * 6.0f; lg[i] = (i % 97 == 0) ? 5.95f : v; + /* About 19 peaks a row hold most of the mass, so the nucleus ends inside the top + * 30 and the cut is what decides the pick. */ + if (c.top_p < 1.0f && i % 211 == 5) lg[i] = 12.0f + 4.0f * rnd(); } void* dlg = up_f32(lg); std::vector frame((size_t)n, f); @@ -5888,7 +5922,7 @@ void cfg_topk_cases(const Plugin& pl) { PI("rep", c.rep), PI("cond_k", c.cond_k), PF("cfg", 1.5), PF("temp", c.temp), PI("stream", 3), PS("dtype", "f32"), PI("sup_lo", c.sup_lo), PI("sup_hi", c.sup_hi), - PF("penalty", 1.1) }; + PF("penalty", 1.1), PF("top_p", c.top_p) }; const int rc = launch(r, { T(dlg, RAD_F32, { c.rep * n, c.V }), T(dseed, RAD_U32, { n, 2 }), T(dfr, RAD_I32, { n }), T(dids, RAD_I32, { c.rep * n }), tcfg, tlane, thist }, @@ -5901,7 +5935,8 @@ void cfg_topk_cases(const Plugin& pl) { const int want = ref_pick(lg, c.V, i, c.rep, c.cfgs ? cfgv[(size_t)i] : 1.5f, K, c.cond_k, c.temp, c.sup_lo, c.sup_hi, 1.1f, c.hist ? &hist : nullptr, - c.hist ? lane[(size_t)i] * c.V : -1, sd, f, 3); + c.hist ? lane[(size_t)i] * c.V : -1, sd, f, 3, + c.top_p); for (int t = 0; t < c.rep; ++t) if (got[(size_t)(c.rep * i + t)] != want) ++wrong; } @@ -5909,12 +5944,64 @@ void cfg_topk_cases(const Plugin& pl) { } ok((nm + " picks").c_str(), wrong == 0, fmt("%lld of %lld picks differ", (long long)wrong, (long long)(4 * c.rep * n))); + if (c.top_p < 1.0f) { + ok((nm + " cuts").c_str(), g_topp_cuts > 0, "the nucleus never ended inside the top k"); + g_topp_cuts = 0; + } if (c.hist) ok((nm + " history").c_str(), hist_wrong == 0, fmt("%lld history entries differ", (long long)hist_wrong)); } } +/* ras_pick over four picks of three lanes, the first clearing their windows: the picks and the + * windows against libref's rule on the host, exactly. Draws repeat often (a band of 6 ids) so the + * redraw is exercised, and one is outside [lo, hi) (never redrawn). */ +void ras_pick_cases(const Plugin& pl) { + const RadKernelInfo* r = find(pl, "ras_pick"); + if (!r) { ok("ras_pick", false, "row absent"); return; } + const int64_t n = 3, lanes = 4, W = 4, lo = 1, hi = 6; + std::vector lane = { 2, 0, 3 }; + std::vector hist((size_t)(lanes * W), 5); + void* dlane = up_i32(lane); + void* dhist = up_i32(hist); + void* dids = dalloc((size_t)n * 4); + int64_t wrong = 0; + for (int step = 0; step < 4; ++step) { + std::vector first((size_t)n), alt((size_t)n); + for (int64_t i = 0; i < n; ++i) { + first[(size_t)i] = (int32_t)((step * 2 + i * 3) % 7); /* 0 is outside [lo, hi) */ + alt[(size_t)i] = (int32_t)(10 + step * 3 + i); + } + void* df = up_i32(first); + void* da = up_i32(alt); + const int clear = step == 0 ? 1 : 0; + std::vector ps = { PI("M", n), PI("win", W), PI("lo", lo), PI("hi", hi), + PI("rep", 1), PI("clear", clear) }; + const int rc = launch(r, { T(df, RAD_I32, { n }), T(da, RAD_I32, { n }), + T(dlane, RAD_I32, { n }), T(dhist, RAD_I32, { lanes, W }), + T(dids, RAD_I32, { n }) }, + ps); + if (rc != RAD_OK) { ok("ras_pick", false, "rc " + std::to_string(rc)); return; } + const std::vector got = down_i32(dids, (size_t)n); + for (int64_t i = 0; i < n; ++i) { + int32_t* h = hist.data() + lane[(size_t)i] * W; + if (clear) for (int64_t w = 0; w < W; ++w) h[w] = -1; + int32_t pick = first[(size_t)i]; + if (pick >= lo && pick < hi) + for (int64_t w = 0; w < W; ++w) + if (h[w] == pick) { pick = alt[(size_t)i]; break; } + for (int64_t w = 0; w + 1 < W; ++w) h[w] = h[w + 1]; + h[W - 1] = pick; + if (got[(size_t)i] != pick) ++wrong; + } + } + ok("ras_pick picks", wrong == 0, fmt("%lld of %lld picks differ", (long long)wrong, + (long long)(4 * n))); + ok("ras_pick windows", differ(down_i32(dhist, hist.size()), hist) == 0, + "the windows differ from libref's rule"); +} + /* rvq_embed: libref's two roundings, the single rounding over bf16 and f32 tables into a bf16 or * f32 row, codes read as a block of a wider frame-major history (strides other than the dense * plane's), and a first code outside its table leaving the row unwritten. Exact. */ @@ -24971,6 +25058,7 @@ static void device_phase(const Plugin& pl) { if (sel("conv_t1d_bf16")) au::conv_t1d_cases(pl); if (sel("snake_bf16")) au::snake_cases(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("frame_store")) au::frame_store_cases(pl); if (sel("iota")) au::iota_cases(pl); diff --git a/libref/ref_audio.cpp b/libref/ref_audio.cpp index e7bb319a..fc87808a 100644 --- a/libref/ref_audio.cpp +++ b/libref/ref_audio.cpp @@ -256,6 +256,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) { @@ -267,8 +270,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 || @@ -319,13 +324,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 e((size_t)kk); for (int64_t t = 0; t < kk; ++t) { @@ -437,6 +461,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. */ diff --git a/libref/ref_ops.h b/libref/ref_ops.h index 4c164a65..ac9ce75a 100644 --- a/libref/ref_ops.h +++ b/libref/ref_ops.h @@ -209,6 +209,7 @@ int ref_cfg_topk_sample(const RadArgs*, RadStream); int ref_rvq_embed(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); int ref_frame_store(const RadArgs*, RadStream); int ref_resample_nearest(const RadArgs*, RadStream); int ref_fourier_embed(const RadArgs*, RadStream); @@ -448,6 +449,7 @@ REF_SHAPE_FN(ref_cfg_topk_sample); REF_SHAPE_FN(ref_rvq_embed); REF_SHAPE_FN(ref_frame_mix_store); REF_SHAPE_FN(ref_end_status); +REF_SHAPE_FN(ref_ras_pick); REF_SHAPE_FN(ref_frame_store); REF_SHAPE_FN(ref_resample_nearest); REF_SHAPE_FN(ref_fourier_embed); diff --git a/libref/ref_registry.cpp b/libref/ref_registry.cpp index 827188ca..60b0ef55 100644 --- a/libref/ref_registry.cpp +++ b/libref/ref_registry.cpp @@ -900,7 +900,8 @@ static const RadOperandSpec oSnakeAa[] = { OPD("x"), WGT("alpha"), WGT("beta"), static const RadParamSpec pCfgTopk[] = { P_INT("M"), P_INT("n_vocab"), P_INT("k"), P_INT("rep"), P_INTO("cond_k"), P_F64("cfg"), P_F64O("temp"), P_INT("stream"), P_STR("dtype"), P_INTO("sup_lo"), - P_INTO("sup_hi"), P_F64O("penalty") }; + P_INTO("sup_hi"), P_F64O("penalty"), + P_F64O("top_p") }; static const RadOperandSpec oCfgTopk[] = { OPD("logits"), OPD("seed"), OPD("frame"), OUT("ids"), OPD_O("cfg"), OPD_O("lane"), INOUT_O("hist") }; static const RadParamSpec pRvqEmbed[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_codes"), @@ -913,6 +914,10 @@ static const RadOperandSpec oFrameMix[] = { OPD("hid"), WGT("logits"), WGT("scal OPD("frame"), OPD("status"), INOUT("dst") }; static const RadParamSpec pEndStatus[] = { P_INT("M"), P_INT("end"), P_INT("rep") }; static const RadOperandSpec oEndStatus[] = { OPD("ids"), OPD("lane"), INOUT("status") }; +static const RadParamSpec pRasPick[] = { P_INT("M"), P_INT("win"), P_INT("lo"), P_INT("hi"), + P_INTO("rep"), P_INTO("clear") }; +static const RadOperandSpec oRasPick[] = { OPD("first"), OPD("alt"), OPD("lane"), INOUT("hist"), + OUT("ids") }; static const RadParamSpec pFrameStore[] = { P_INT("M"), P_INT("n_codes"), P_INT("stride"), P_INTO("rep"), P_INTO("skip") }; static const RadOperandSpec oFrameStore[] = { OPD("codes"), OPD("lane"), OPD("frame"), @@ -1769,6 +1774,10 @@ static const RadOpSchema kSchemas[] = { "stored at dst[lane * stride + frame - 1] for frame >= 1 and status[lane] >= 0." }, { "end_status", ARR(pEndStatus), ARR(oEndStatus), "status[lane[i]] = ids[rep i] == end ? -1 : ids[rep i]." }, +{ "ras_pick", ARR(pRasPick), ARR(oRasPick), + "repetition-aware sampling: ids[rep i] = alt[rep i] where first[rep i] is in [lo, hi) and in " + "the lane's window of its last `win` picks (hist [lanes, win], -1 empty, cleared first with " + "`clear` 1), else first[rep i]; the pick then enters the window." }, { "frame_store", ARR(pFrameStore), ARR(oFrameStore), "a frame's codes into its lane's history: dst[(lane n_codes + k) stride + frame - skip] = " "codes[k, rep i] when frame >= skip and status[lane] >= 0." }, @@ -2340,6 +2349,8 @@ SIMPLE("ref_frame_mix_store", "frame_mix_store", "elem", "a frame's conditioning DT_FLOAT, ref_frame_mix_store), SIMPLE("ref_end_status", "end_status", "elem", "a frame's status from its first pick", DT_ANY, ref_end_status), +SIMPLE("ref_ras_pick", "ras_pick", "elem", "a repetition-aware choice between two draws a lane", + DT_ANY, ref_ras_pick), SIMPLE("ref_frame_store", "frame_store", "elem", "a frame's codes into its lane's history", DT_ANY, ref_frame_store), SIMPLE("ref_resample_nearest", "resample_nearest", "elem", "rows resampled nearest-neighbour", diff --git a/libref/ref_shapes.cpp b/libref/ref_shapes.cpp index f1fd4d8a..b9f074d7 100644 --- a/libref/ref_shapes.cpp +++ b/libref/ref_shapes.cpp @@ -3226,6 +3226,21 @@ int ref_end_status_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* ou return o.finish("end_status", operand, out); } +int ref_ras_pick_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { + const int64_t M = gi(p, n_p, "M", 0), rep = gi(p, n_p, "rep", 1), W = gi(p, n_p, "win", 0); + const int64_t hi = gi(p, n_p, "hi", 0); + if (M <= 0 || rep < 1 || W <= 0 || hi <= 0 || M > REF_LANES) return RAD_E_UNSUPPORTED; + Opd o; + /* Picks over a small band so the windows repeat them often enough to exercise the redraw. */ + const int64_t band = std::min(hi + 2, 12); + o.t_idx({ rep * M }, band, -1, 0); /* first */ + o.t_idx({ rep * M }, band, -1, 0); /* alt */ + o.t_idx({ M }, REF_LANES, -1, (uint32_t)RAD_OPD_F_IDX_UNIQUE); /* lane */ + o.t_idx({ REF_LANES * W }, band, -1, 0); /* hist, INOUT */ + o.t_idx({ rep * M }, band, -1, 0); /* ids, OUT */ + return o.finish("ras_pick", operand, out); +} + int ref_frame_store_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { const int64_t M = gi(p, n_p, "M", 0), K = gi(p, n_p, "n_codes", 0); const int64_t st = gi(p, n_p, "stride", 0), rep = gi(p, n_p, "rep", 1); diff --git a/tests/ref_test.cpp b/tests/ref_test.cpp index 844b5be8..6d182677 100644 --- a/tests/ref_test.cpp +++ b/tests/ref_test.cpp @@ -970,6 +970,8 @@ 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", /* 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. */ @@ -8124,6 +8126,7 @@ std::vector geometries() { G("frame_mix_store", "M", 2, a.i("M", R).i("n_embd", 8).i("L", 3).i("stride", 4) .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("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)