Archived
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:
1 parent
8a993b7de9
commit
0c35fc8070
14 files changed
+384
-23
No files matched your search
+64
-2
@@ -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<double> 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. */
|
||||
|
||||
@@ -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);
|
||||
|
||||
+12
-1
@@ -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",
|
||||
|
||||
@@ -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<int64_t>(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);
|
||||
|
||||
Reference in new issue
Block a user