libref, libavx, libr4d: rvq_embed's clamp -- a code past a codebook smaller than the heads that wrote it reads the codebook's last row, as Fish Audio's codec reads its 1024-entry residual codebooks

This commit is contained in:
zivius committed 2026-10-20 23:07:33 +02:00
1 parent 832a88faac
commit 346efdeb2e
9 files changed
+41 -25

No files matched your search

+5 -4
View File
@@ -713,17 +713,18 @@ AVX_KERNEL(rvq_embed) {
const int64_t D = p_int(a, "n_embd", 0), K = p_int(a, "n_codes", 0);
const int64_t voc = p_int(a, "vocab", 0), off0 = p_int(a, "off0", 0);
const float scale = p_f32(a, "scale", 1.0f);
const int64_t round1 = p_int(a, "round", 0);
if (D <= 0 || K < 1 || voc <= 0 || round1 < 0 || round1 > 1) return RAD_E_SHAPE;
const int64_t round1 = p_int(a, "round", 0), clamp = p_int(a, "clamp", 0);
if (D <= 0 || K < 1 || voc <= 0 || round1 < 0 || round1 > 1 || clamp < 0) return RAD_E_SHAPE;
const int64_t rows = rows_of(Y, D);
if (rows <= 0 || numel(codes) < K * rows) return RAD_E_SHAPE;
const int64_t n0 = rows_of(t0, D), n1 = rows_of(t1, D);
auto cl = [clamp](int64_t c) { return clamp > 0 && c > clamp - 1 ? clamp - 1 : c; };
/* Code (k, r): a codebook's row at the plane's own pitch, a frame's code at its own stride. */
const int64_t kp = codes->rank >= 2 ? codes->stride[codes->rank - 2] : rows;
const int64_t rp = codes->rank >= 1 ? codes->stride[codes->rank - 1] : 1;
AVX_PARALLEL_FOR_IF(rows * D * K >= AVX_PAR_MIN)
for (int64_t r = 0; r < rows; ++r) {
const int64_t c0 = ld_int_dt(codes->dtype, codes->data, r * rp) + off0;
const int64_t c0 = cl(ld_int_dt(codes->dtype, codes->data, r * rp)) + off0;
if (c0 < 0 || c0 >= n0) continue;
float* acc = scratch_f32((size_t)D + 16);
float* buf = scratch_f32_b((size_t)D + 16);
@@ -736,7 +737,7 @@ AVX_KERNEL(rvq_embed) {
}
for (int64_t k = 1; k < K; ++k) {
const int64_t cb =
ld_int_dt(codes->dtype, codes->data, k * kp + r * rp) + (k - 1) * voc;
cl(ld_int_dt(codes->dtype, codes->data, k * kp + r * rp)) + (k - 1) * voc;
if (cb < 0 || cb >= n1) continue;
const float* tr = in_row(t1, rowoff(t1, cb, D), D, buf);
for (int64_t d = 0; d < D; ++d) acc[d] += tr[d];
+3
View File
@@ -1015,6 +1015,9 @@ std::vector<Case> build_cases() {
for (double sc : { 1.0, 0.7 })
add("rvq_embed", { HI("M", 7), HI("n_embd", 645), HI("n_codes", 8), HI("vocab", 64),
HI("off0", 5), HF("scale", sc), HS("dtype", dt) });
/* Codes past a codebook smaller than its heads read its last row (Fish Audio's codec). */
add("rvq_embed", { HI("M", 7), HI("n_embd", 645), HI("n_codes", 4), HI("vocab", 64),
HI("off0", 0), HF("scale", 1.0), HI("clamp", 40), HS("dtype", dt) });
/* One rounding of the whole sum: sixteen codebooks, as a speech model's frame embedding. */
for (double sc : { 1.0, 0.7 })
add("rvq_embed", { HI("M", 7), HI("n_embd", 645), HI("n_codes", 16), HI("vocab", 64),
+1 -1
View File
@@ -705,7 +705,7 @@ static const RadOperandSpec oCfgTopk[] = { OPD("logits"), OPD("seed"), OPD("fram
OPD_O("cfg"), OPD_O("lane"), INOUT_O("hist") };
static const RadParamSpec pRvqEmbed[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_codes"),
P_INT("vocab"), P_INT("off0"), P_F64("scale"),
P_STR("dtype"), P_INTO("round") };
P_STR("dtype"), P_INTO("round"), P_INTO("clamp") };
static const RadOperandSpec oRvqEmbed[] = { OPD("codes"), WGT("t0"), WGT("t1"), OUT("y") };
static const RadParamSpec pFrameMix[] = { P_INT("M"), P_INT("n_embd"), P_INT("L"), P_INT("stride"),
P_INTO("rep"), P_STR("dtype") };