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

This commit is contained in:
zivius committed 2026-10-20 23:07:16 +02:00
1 parent 8a993b7de9
commit 0c35fc8070
14 files changed
+384 -23

No files matched your search

+58 -2
View File
@@ -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) {
+9
View File
@@ -998,6 +998,12 @@ std::vector<Case> 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<Case> 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),
+1 -1
View File
@@ -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) \
+11 -1
View File
@@ -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",