diff --git a/docs/OPS.md b/docs/OPS.md index 1714484c..911623b2 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -1172,7 +1172,7 @@ libref function says which (`libref/ref_audio.cpp`). | `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 | `codes`, `t0`w, `t1`w → `y` | +| `rvq_embed` | `M`(range) `n_embd` `n_codes` `vocab` `off0` `scale`:f64 `dtype` `round`/opt `clamp`/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` | @@ -1216,6 +1216,9 @@ conditional first, then the unconditional) are consecutive rows, `rep` of them a `y = bf16(bf16(t0[c_0 + off0] + bf16(sum_k t1[c_k + (k - 1) vocab])) * scale)`, or with `round` 1 one f32 sum of every codebook's row in codebook order, times `scale`, rounded once to `y`'s dtype. A first code outside `t0` leaves its row unwritten; a later code outside `t1` adds nothing. + 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. - `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` diff --git a/libavx/avx_audio.cpp b/libavx/avx_audio.cpp index 7f2559f4..e5ae6799 100644 --- a/libavx/avx_audio.cpp +++ b/libavx/avx_audio.cpp @@ -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]; diff --git a/libavx/avx_cases.cpp b/libavx/avx_cases.cpp index 5be9c307..315f00eb 100644 --- a/libavx/avx_cases.cpp +++ b/libavx/avx_cases.cpp @@ -1015,6 +1015,9 @@ std::vector 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), diff --git a/libavx/avx_registry.cpp b/libavx/avx_registry.cpp index ba115f6f..cc0eb1e9 100644 --- a/libavx/avx_registry.cpp +++ b/libavx/avx_registry.cpp @@ -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") }; diff --git a/libr4d/r4d_audio.hip b/libr4d/r4d_audio.hip index 95f974ff..0d4ecdf5 100644 --- a/libr4d/r4d_audio.hip +++ b/libr4d/r4d_audio.hip @@ -788,15 +788,16 @@ __device__ __forceinline__ float ldtab(const void* t, int f32, size_t i) { __global__ __launch_bounds__(kT) void rvq_embed_kernel( const int* __restrict__ codes, int64_t kp, int64_t rp, const void* __restrict__ t0, int t0_f32, const void* __restrict__ t1, int t1_f32, void* __restrict__ y, int y_f32, int D, - int K, int voc, int off0, int r0, int r1, float scale, int y_ld, int round1) { + int K, int voc, int off0, int r0, int r1, float scale, int y_ld, int round1, int clamp) { const int r = blockIdx.x; - const int c0 = codes[(size_t)r * rp] + off0; + auto cl = [clamp](int c) { return clamp > 0 && c > clamp - 1 ? clamp - 1 : c; }; + const int c0 = cl(codes[(size_t)r * rp]) + off0; if (c0 < 0 || c0 >= r0) return; for (int d = threadIdx.x; d < D; d += kT) { const float e0 = ldtab(t0, t0_f32, (size_t)c0 * D + d); float acc = round1 ? e0 : 0.0f; for (int k = 1; k < K; ++k) { - const int ck = codes[(size_t)k * kp + (size_t)r * rp] + (k - 1) * voc; + const int ck = cl(codes[(size_t)k * kp + (size_t)r * rp]) + (k - 1) * voc; if (ck >= 0 && ck < r1) acc += ldtab(t1, t1_f32, (size_t)ck * D + d); } const float v = (round1 ? acc : bfr(e0 + bfr(acc))) * scale; @@ -1412,9 +1413,10 @@ extern "C" int r4d_rad_rvq_embed(const RadArgs* a, RadStream s) { return R4D_NO(RAD_E_DTYPE); const long long D = r4d_geti_or(a, "n_embd", 0), K = r4d_geti_or(a, "n_codes", 0); const long long voc = r4d_geti_or(a, "vocab", 0), off0 = r4d_geti_or(a, "off0", 0); - const long long round1 = r4d_geti_or(a, "round", 0); + const long long round1 = r4d_geti_or(a, "round", 0), clamp = r4d_geti_or(a, "clamp", 0); const double scale = r4d_getf(a, "scale", 1.0); - if (D <= 0 || K < 1 || voc <= 0 || round1 < 0 || round1 > 1) return R4D_NO(RAD_E_SHAPE); + if (D <= 0 || K < 1 || voc <= 0 || round1 < 0 || round1 > 1 || clamp < 0) + return R4D_NO(RAD_E_SHAPE); long long ry = 0; const long long yl = r4d_rows_pitch(y, D, &ry); if (yl < 0 || !r4d_contig(t0) || !r4d_contig(t1)) return R4D_NO(RAD_E_STRIDE); @@ -1427,7 +1429,7 @@ extern "C" int r4d_rad_rvq_embed(const RadArgs* a, RadStream s) { t0->dtype == RAD_F32 ? 1 : 0, t1->data, t1->dtype == RAD_F32 ? 1 : 0, y->data, y->dtype == RAD_F32 ? 1 : 0, (int)D, (int)K, (int)voc, (int)off0, (int)(r4d_numel(t0) / D), (int)(r4d_numel(t1) / D), (float)scale, (int)yl, - (int)round1); + (int)round1, (int)clamp); return st_of(hipGetLastError()); } diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 1bfaed4e..1ff67849 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -2400,7 +2400,7 @@ static const RadOperandSpec oCfgTopk[] = { }; 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[] = { { "codes", RAD_OPD_IN, 0 }, { "t0", RAD_OPD_WEIGHT, 0 }, { "t1", RAD_OPD_WEIGHT, 0 }, { "y", RAD_OPD_OUT, 0 }, diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index a071e8ba..5371652b 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -6009,9 +6009,12 @@ void rvq_embed_cases(const Plugin& pl) { const RadKernelInfo* r = find(pl, "rvq_embed_bf16"); if (!r) { ok("rvq_embed_bf16", false, "row absent"); return; } const int64_t D = 96, K = 5, voc = 11, off0 = 3, rows = 6, lanes = 2, stride = 9; - struct C { int round1; bool f32tab, f32y, block; }; + /* The last case clamps every code at 8 (`clamp`): codes 8..10 read row 7, and the later code + * past t1 reads its codebook's row 7 instead of nothing. */ + struct C { int round1; bool f32tab, f32y, block; int clamp = 0; }; for (const C& c : { C{ 0, false, false, false }, C{ 1, false, false, false }, - C{ 1, true, true, true }, C{ 1, false, true, true } }) { + C{ 1, true, true, true }, C{ 1, false, true, true }, + C{ 0, false, false, false, 8 } }) { std::vector t0 = randv((size_t)((voc + off0) * D), 1.0f); std::vector t1 = randv((size_t)((K - 1) * voc * D), 1.0f); void* d0 = c.f32tab ? up_f32(t0) : up_bf16(t0); @@ -6045,10 +6048,12 @@ void rvq_embed_cases(const Plugin& pl) { const float scale = 0.5f; std::vector ps = { PI("M", rows), PI("n_embd", D), PI("n_codes", K), PI("vocab", voc), PI("off0", off0), PF("scale", scale), - PS("dtype", c.f32y ? "f32" : "bf16"), PI("round", c.round1) }; - const std::string nm = fmt("rvq_embed_bf16 round %d %s tables -> %s%s", c.round1, + PS("dtype", c.f32y ? "f32" : "bf16"), PI("round", c.round1), + PI("clamp", c.clamp) }; + const std::string nm = fmt("rvq_embed_bf16 round %d %s tables -> %s%s clamp %d", c.round1, c.f32tab ? "f32" : "bf16", c.f32y ? "f32" : "bf16", - c.block ? ", codes a history block" : ""); + c.block ? ", codes a history block" : "", c.clamp); + auto cl = [&](int64_t v) { return c.clamp > 0 && v > c.clamp - 1 ? c.clamp - 1 : v; }; const int rc = launch(r, { tc, T(d0, c.f32tab ? RAD_F32 : RAD_BF16, { voc + off0, D }), T(d1, c.f32tab ? RAD_F32 : RAD_BF16, { (K - 1) * voc, D }), T(dy, ydt, { rows, D }) }, @@ -6056,13 +6061,13 @@ void rvq_embed_cases(const Plugin& pl) { if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; } std::vector want = sentinel; for (int64_t i = 0; i < rows; ++i) { - const int64_t c0 = code[(size_t)i] + off0; + const int64_t c0 = cl(code[(size_t)i]) + off0; if (c0 < 0 || c0 >= voc + off0) continue; for (int64_t d = 0; d < D; ++d) { const float e0 = t0[(size_t)(c0 * D + d)]; float acc = c.round1 ? e0 : 0.0f; for (int64_t k = 1; k < K; ++k) { - const int64_t ck = code[(size_t)(k * rows + i)] + (k - 1) * voc; + const int64_t ck = cl(code[(size_t)(k * rows + i)]) + (k - 1) * voc; if (ck >= 0 && ck < (K - 1) * voc) acc += t1[(size_t)(ck * D + d)]; } const float s = c.round1 ? acc : bfr(e0 + bfr(acc)); diff --git a/libref/ref_audio.cpp b/libref/ref_audio.cpp index fc87808a..2144cc07 100644 --- a/libref/ref_audio.cpp +++ b/libref/ref_audio.cpp @@ -385,24 +385,26 @@ int ref_rvq_embed(const RadArgs* a, RadStream) { 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 r0 = rows_of(t0, D), r1 = rows_of(t1, D); + /* `clamp`: a code above clamp - 1 reads row clamp - 1 (a negative one still reads nothing). */ + 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; REF_PAR_FOR(rows * D * K) for (int64_t r = 0; r < rows; ++r) { - const int64_t c0 = ld_int(codes->dtype, codes->data, r * rp) + off0; + const int64_t c0 = cl(ld_int(codes->dtype, codes->data, r * rp)) + off0; if (c0 < 0 || c0 >= r0) continue; std::vector acc((size_t)D, 0.0f); const int64_t b0 = rowoff(t0, c0, D), yb = rowoff(y, r, D); if (round1) for (int64_t d = 0; d < D; ++d) acc[(size_t)d] = ldt(t0, b0 + d * laststride(t0)); for (int64_t k = 1; k < K; ++k) { - const int64_t ck = ld_int(codes->dtype, codes->data, k * kp + r * rp) + (k - 1) * voc; + const int64_t ck = cl(ld_int(codes->dtype, codes->data, k * kp + r * rp)) + (k - 1) * voc; if (ck < 0 || ck >= r1) continue; const int64_t tb = rowoff(t1, ck, D); for (int64_t d = 0; d < D; ++d) acc[(size_t)d] += ldt(t1, tb + d * laststride(t1)); diff --git a/libref/ref_registry.cpp b/libref/ref_registry.cpp index 60b0ef55..a4c99769 100644 --- a/libref/ref_registry.cpp +++ b/libref/ref_registry.cpp @@ -906,7 +906,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") };