Archived
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:
1 parent
832a88faac
commit
346efdeb2e
9 files changed
+41
-25
No files matched your search
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -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 },
|
||||
|
||||
+12
-7
@@ -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<float> t0 = randv((size_t)((voc + off0) * D), 1.0f);
|
||||
std::vector<float> 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<RadParam> 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<float> 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));
|
||||
|
||||
Reference in new issue
Block a user