libref, libavx, libr4d: conv1d's stride and cu_div, and vq_encode -- a causal codec encoder's downsampling (the left pad the reach less the stride, every sequence a whole number of strides, the bounds read in samples at every rate) and one step of its residual quantiser, the code of greatest cosine and the residual it leaves (Fish Audio's codec encoding a reference)

This commit is contained in:
zivius committed 2026-10-21 01:00:13 +02:00
1 parent 00f9703892
commit 4e750fed01
15 files changed
+673 -110

No files matched your search

+166 -45
View File
@@ -56,7 +56,7 @@ __device__ __forceinline__ uint64_t seed_of(const uint32_t* seed, int e) {
// ---------------------------------------------------------------------------- conv1d
// An implicit GEMM, r4d_gen.hip's conv_igemm_kernel cut to one dimension: y[row][cout] = sum
// over (tap, cin) of x[row + tap * dil - pad][cin] * w[cout][tap][cin], the weight stored
// over (tap, cin) of x[row * stride + tap * dil - pad][cin] * w[cout][tap][cin], the weight stored
// tap-major (r4d_layout.cpp relayouts PyTorch's [cout][cin][k] at load), so a 64-wide K tile is
// eight 16-byte reads of input rows. A chunk outside its row's sequence reads zeros, which is the
// per-sequence padding. Eight waves, BP rows x BC output channels a block, the next K tile's
@@ -77,7 +77,8 @@ __global__ __launch_bounds__(256) void conv1d_igemm_kernel(
const uint16_t* __restrict__ x, const uint16_t* __restrict__ w,
const uint16_t* __restrict__ bias, const uint16_t* __restrict__ res,
const int* __restrict__ cu, uint16_t* __restrict__ y, int n_seq, int r_beg, int r_end,
int cin, int cout, int k, int dil, int pad, int x_ld, int y_ld, int res_ld, int act) {
int cin, int cout, int k, int dil, int pad, int stride, int cdiv, int x_ld, int y_ld,
int res_ld, int act) {
constexpr int BP = 2 * TP * 16, BC = 4 * TC * 16;
constexpr int CPR = C1_BK / 8;
constexpr int AL = BP * CPR / 256, WL = BC * CPR / 256;
@@ -99,9 +100,9 @@ __global__ __launch_bounds__(256) void conv1d_igemm_kernel(
const int m = p0 + r;
a_ok[i] = m < r_end;
a_row[i] = a_ok[i] ? m : r_beg;
const int sq = seq_of(cu, n_seq, a_row[i]);
a_s0[i] = cu[sq];
a_s1[i] = cu[sq + 1];
const int sq = seq_of(cu, n_seq, a_row[i] * stride * cdiv);
a_s0[i] = cu[sq] / cdiv;
a_s1[i] = cu[sq + 1] / cdiv;
a_t[i] = e / cin;
a_ci[i] = e % cin;
adst[i] = sA + r * C1_PITCH + e;
@@ -122,7 +123,7 @@ __global__ __launch_bounds__(256) void conv1d_igemm_kernel(
const r4d_u32x4 zero = {0u, 0u, 0u, 0u};
auto loadA = [&](int i) -> r4d_u32x4 {
if (!a_ok[i] || a_t[i] >= k) return zero;
const int ir = a_row[i] + a_t[i] * dil - pad;
const int ir = a_row[i] * stride + a_t[i] * dil - pad;
if (ir < a_s0[i] || ir >= a_s1[i]) return zero;
return *(const r4d_u32x4*)(x + (size_t)ir * x_ld + a_ci[i]);
};
@@ -214,17 +215,18 @@ __global__ __launch_bounds__(kT) void conv1d_direct_kernel(
const uint16_t* __restrict__ x, const uint16_t* __restrict__ w,
const uint16_t* __restrict__ bias, const uint16_t* __restrict__ res,
const int* __restrict__ cu, uint16_t* __restrict__ y, int n_seq, int r_beg, int r_end,
int cin, int cout, int k, int dil, int pad, int x_ld, int y_ld, int res_ld, int act) {
int cin, int cout, int k, int dil, int pad, int stride, int cdiv, int x_ld, int y_ld,
int res_ld, int act) {
const int64_t t = (int64_t)blockIdx.x * kT + threadIdx.x;
const int64_t total = (int64_t)(r_end - r_beg) * cout;
if (t >= total) return;
const int co = (int)(t % cout);
const int row = r_beg + (int)(t / cout);
const int sq = seq_of(cu, n_seq, row);
const int s0 = cu[sq], s1 = cu[sq + 1];
const int sq = seq_of(cu, n_seq, row * stride * cdiv);
const int s0 = cu[sq] / cdiv, s1 = cu[sq + 1] / cdiv;
float acc = 0.0f;
for (int kk = 0; kk < k; ++kk) {
const int ir = row + kk * dil - pad;
const int ir = row * stride + kk * dil - pad;
if (ir < s0 || ir >= s1) continue;
const uint16_t* xp = x + (size_t)ir * x_ld;
const uint16_t* wp = w + ((size_t)co * k + kk) * cin;
@@ -384,14 +386,14 @@ __global__ __launch_bounds__(256) void conv_t1d_kernel(
// the output channels, its K the (tap, channel) pairs, channel innermost. A thread stages one K
// column a slab and steps it sixteen at a time, so no K index is divided after the first.
// conv1d's A: output row m's tap t reads input row m + t dil - pad of m's own sequence, zero
// outside it and past the last tap. As r4d_f32::RowsLoader, every load is issued at an address
// clamped into x's first `r_end` rows and the zero selected after it.
// conv1d's A: output row m's tap t reads input row m stride + t dil - pad of m's own sequence,
// zero outside it and past the last tap. As r4d_f32::RowsLoader, every load is issued at an
// address clamped into x's first `r_end` stride rows and the zero selected after it.
template <int CNT>
struct Conv1dRows {
const float* x;
const int* cu;
int n_seq, r_end, cin, k, dil, pad, x_ld;
int n_seq, r_end, cin, k, dil, pad, x_ld, stride, cdiv;
int row[CNT], s0[CNT], s1[CNT];
int t = 0, ci = 0;
__device__ void init(int first_row, int kk) {
@@ -399,9 +401,9 @@ struct Conv1dRows {
for (int j = 0; j < CNT; ++j) {
const int m = first_row + 16 * j;
row[j] = m < r_end ? m : -1;
const int sq = seq_of(cu, n_seq, m < r_end ? m : r_end - 1);
s0[j] = cu[sq];
s1[j] = cu[sq + 1];
const int sq = seq_of(cu, n_seq, (m < r_end ? m : r_end - 1) * stride * cdiv);
s0[j] = cu[sq] / cdiv;
s1[j] = cu[sq + 1] / cdiv;
}
t = kk / cin;
ci = kk % cin;
@@ -409,9 +411,10 @@ struct Conv1dRows {
__device__ void fetch(float (&v)[CNT]) const {
#pragma unroll
for (int j = 0; j < CNT; ++j) {
const int ir = row[j] + t * dil - pad;
const int ir = row[j] * stride + t * dil - pad;
const bool live = row[j] >= 0 && t < k && ir >= s0[j] && ir < s1[j];
const int irc = ir < 0 ? 0 : (ir >= r_end ? r_end - 1 : ir);
const int x_rows = r_end * stride;
const int irc = ir < 0 ? 0 : (ir >= x_rows ? x_rows - 1 : ir);
const float xv = x[(uint32_t)irc * (uint32_t)x_ld + (uint32_t)ci];
v[j] = live ? xv : 0.0f;
}
@@ -428,12 +431,13 @@ template <int TN>
__global__ __launch_bounds__(r4d_f32::kThr) void conv1d_f32_kernel(
const float* __restrict__ x, const float* __restrict__ w, const float* __restrict__ bias,
const float* res, const int* __restrict__ cu, float* y, int n_seq, int rows, int cin,
int cout, int k, int dil, int pad, int x_ld, int y_ld, int res_ld, int act) {
int cout, int k, int dil, int pad, int stride, int cdiv, int x_ld, int y_ld, int res_ld,
int act) {
constexpr int TM = 8, BM = 16 * TM, BN = 16 * TN;
const int tid = threadIdx.x, tx = tid & 15, ty = tid >> 4;
const int m0 = blockIdx.y * BM, n0 = blockIdx.x * BN;
const int K = k * cin;
Conv1dRows<TM> la{ x, cu, n_seq, rows, cin, k, dil, pad, x_ld };
Conv1dRows<TM> la{ x, cu, n_seq, rows, cin, k, dil, pad, x_ld, stride, cdiv };
r4d_f32::RowsLoader<TN> lw{ w, K, cout, K };
la.init(m0 + ty, tx);
lw.init(n0 + ty, tx);
@@ -458,16 +462,17 @@ __global__ __launch_bounds__(r4d_f32::kThr) void conv1d_f32_kernel(
__global__ __launch_bounds__(kT) void conv1d_f32_direct_kernel(
const float* __restrict__ x, const float* __restrict__ w, const float* __restrict__ bias,
const float* res, const int* __restrict__ cu, float* y, int n_seq, int rows, int cin,
int cout, int k, int dil, int pad, int x_ld, int y_ld, int res_ld, int act) {
int cout, int k, int dil, int pad, int stride, int cdiv, int x_ld, int y_ld, int res_ld,
int act) {
const int64_t t = (int64_t)blockIdx.x * kT + threadIdx.x;
if (t >= (int64_t)rows * cout) return;
const int co = (int)(t % cout);
const int row = (int)(t / cout);
const int sq = seq_of(cu, n_seq, row);
const int s0 = cu[sq], s1 = cu[sq + 1];
const int sq = seq_of(cu, n_seq, row * stride * cdiv);
const int s0 = cu[sq] / cdiv, s1 = cu[sq + 1] / cdiv;
float acc = 0.0f;
for (int kk = 0; kk < k; ++kk) {
const int ir = row + kk * dil - pad;
const int ir = row * stride + kk * dil - pad;
if (ir < s0 || ir >= s1) continue;
const float* xp = x + (size_t)ir * x_ld;
const float* wp = w + ((size_t)co * k + kk) * cin;
@@ -806,6 +811,86 @@ __global__ __launch_bounds__(kT) void rvq_embed_kernel(
}
}
// One residual quantiser step (DAC's VectorQuantize), a workgroup a row of x. e = in_w x + in_b,
// each of its `dim` sums split over the workgroup and reduced through LDS; each thread then scores
// a strided run of the codebook against e normalised, every row normalised as it is read
// (F.normalize's division), and the workgroup's argmax keeps the lower index on a tie; last the
// chosen raw row's out-projection leaves x's row, rounded once. libref's arithmetic in another
// summation order.
constexpr int kVqMaxDim = 32;
__global__ __launch_bounds__(kT) void vq_encode_kernel(
void* __restrict__ x, int x_f32, int x_ld, const void* __restrict__ iw, int iw_f32,
const void* __restrict__ ib, int ib_f32, const void* __restrict__ cb, int cb_f32,
const void* __restrict__ ow, int ow_f32, const void* __restrict__ ob, int ob_f32,
int* __restrict__ codes, int64_t code_st, int n, int dim, int bins) {
__shared__ float part[kT / 32][kVqMaxDim];
__shared__ float e[kVqMaxDim], eh[kVqMaxDim];
__shared__ float wbest[kT / 32];
__shared__ int wcode[kT / 32];
const int tid = threadIdx.x, wv = tid >> 5, ln = tid & 31;
const size_t xb = (size_t)blockIdx.x * x_ld;
auto ldx = [&](int d) {
return x_f32 ? ((const float*)x)[xb + d] : bf2f(((const uint16_t*)x)[xb + d]);
};
for (int j = 0; j < dim; ++j) {
float acc = 0.0f;
for (int d = tid; d < n; d += kT) acc += ldtab(iw, iw_f32, (size_t)j * n + d) * ldx(d);
#pragma unroll
for (int o = 16; o > 0; o >>= 1) acc += __shfl_xor(acc, o, 32);
if (ln == 0) part[wv][j] = acc;
}
__syncthreads();
if (tid < dim) {
float acc = 0.0f;
for (int w = 0; w < kT / 32; ++w) acc += part[w][tid];
e[tid] = acc + ldtab(ib, ib_f32, (size_t)tid);
}
__syncthreads();
if (tid < dim) {
float ss = 0.0f;
for (int j = 0; j < dim; ++j) ss += e[j] * e[j];
eh[tid] = e[tid] / fmaxf(sqrtf(ss), 1e-12f);
}
__syncthreads();
// Each thread's best over its rows, ascending, so its first maximum is kept; INT_MAX none.
float bs = -INFINITY;
int bc = 0x7fffffff;
for (int c = tid; c < bins; c += kT) {
float ss = 0.0f;
for (int j = 0; j < dim; ++j) {
const float v = ldtab(cb, cb_f32, (size_t)c * dim + j);
ss += v * v;
}
const float nrm = fmaxf(sqrtf(ss), 1e-12f);
float dot = 0.0f;
for (int j = 0; j < dim; ++j) dot += eh[j] * (ldtab(cb, cb_f32, (size_t)c * dim + j) / nrm);
if (dot > bs) { bs = dot; bc = c; }
}
#pragma unroll
for (int o = 16; o > 0; o >>= 1) {
const float os = __shfl_xor(bs, o, 32);
const int oc = __shfl_xor(bc, o, 32);
if (os > bs || (os == bs && oc < bc)) { bs = os; bc = oc; }
}
if (ln == 0) { wbest[wv] = bs; wcode[wv] = bc; }
__syncthreads();
float fs = wbest[0];
int fc = wcode[0];
for (int w = 1; w < kT / 32; ++w)
if (wbest[w] > fs || (wbest[w] == fs && wcode[w] < fc)) { fs = wbest[w]; fc = wcode[w]; }
const int code = fc == 0x7fffffff ? 0 : fc; // no score above -inf: code 0, as libref
if (tid == 0) codes[(size_t)blockIdx.x * code_st] = code;
for (int d = tid; d < n; d += kT) {
float q = 0.0f;
for (int j = 0; j < dim; ++j)
q += ldtab(ow, ow_f32, (size_t)d * dim + j) * ldtab(cb, cb_f32, (size_t)code * dim + j);
const float v = ldx(d) - (q + ldtab(ob, ob_f32, (size_t)d));
if (x_f32) ((float*)x)[xb + d] = v;
else ((uint16_t*)x)[xb + d] = f32_to_bf16(v);
}
}
// dst[(lane n_codes + k) stride + frame - skip] = codes[k, rep i]; a thread a (lane, code). A lane
// outside `status`, or a frame past `stride` or a lane past `dst`, stores nothing (libref refuses
// the call).
@@ -1126,24 +1211,26 @@ extern "C" int r4d_rad_conv1d(const RadArgs* a, RadStream s) {
return R4D_NO(RAD_E_DTYPE);
const long long cin = r4d_geti_or(a, "cin", 0), cout = r4d_geti_or(a, "cout", 0);
const long long k = r4d_geti_or(a, "k", 0), dil = r4d_geti_or(a, "dil", 1);
const long long causal = r4d_geti_or(a, "causal", 0);
const long long causal = r4d_geti_or(a, "causal", 0), st = r4d_geti_or(a, "stride", 1);
const long long cd = r4d_geti_or(a, "cu_div", 1);
const int act = conv1d_act(r4d_gets(a, "act"));
if (act < 0) return R4D_NO(RAD_E_INVAL);
if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0) || dil <= 0 || causal < 0 ||
causal > 1)
causal > 1 || st < 1 || (st > 1 && (!causal || dil * (k - 1) + 1 < st)) || cd < 1)
return R4D_NO(RAD_E_SHAPE);
long long rx = 0, ry = 0, rr = 0;
const long long xl = r4d_rows_pitch(x, cin, &rx), yl = r4d_rows_pitch(y, cout, &ry);
const long long rl = res ? r4d_rows_pitch(res, cout, &rr) : 0;
if (xl < 0 || yl < 0 || rl < 0) return R4D_NO(RAD_E_STRIDE);
const long long n_seq = r4d_numel(cu) - 1;
if (n_seq <= 0 || ry <= 0 || rx < ry || (res && rr < ry) || r4d_numel(w) < cout * cin * k ||
(b && r4d_numel(b) < cout))
if (n_seq <= 0 || ry <= 0 || rx < ry * st || (res && rr < ry) ||
r4d_numel(w) < cout * cin * k || (b && r4d_numel(b) < cout))
return R4D_NO(RAD_E_SHAPE);
const uint16_t* bp = b ? (const uint16_t*)b->data : nullptr;
/* Both kernels read input row row + tap * dil - pad: half the reach a side, or all of it on
* the left for the causal form. */
const int pad = (int)(causal ? dil * (k - 1) : dil * (k - 1) / 2);
/* Both kernels read input row row * stride + tap * dil - pad: half the reach a side, or for
* the causal form all of it on the left less the stride (the sequences' bounds whole
* strides, as the op requires). */
const int pad = (int)(causal ? dil * (k - 1) + 1 - st : dil * (k - 1) / 2);
const bool tiled = (cin % 8) == 0 && cout >= 16 && (xl % 8) == 0 &&
((uintptr_t)x->data & 15) == 0 && ((uintptr_t)w->data & 15) == 0;
const uint16_t* rp = res ? (const uint16_t*)res->data : nullptr;
@@ -1156,8 +1243,8 @@ extern "C" int r4d_rad_conv1d(const RadArgs* a, RadStream s) {
dim3(256), 0, (hipStream_t)s, (const uint16_t*)x->data,
(const uint16_t*)w->data, bp, rp,
(const int*)cu->data, (uint16_t*)y->data, (int)n_seq, 0, (int)ry,
(int)cin, (int)cout, (int)k, (int)dil, pad, (int)xl, (int)yl,
(int)rl, act);
(int)cin, (int)cout, (int)k, (int)dil, pad, (int)st, (int)cd,
(int)xl, (int)yl, (int)rl, act);
} else {
constexpr int TP = 4, TC = 1, BP = 128, BC = 64;
hipLaunchKernelGGL((conv1d_igemm_kernel<TP, TC>),
@@ -1165,15 +1252,15 @@ extern "C" int r4d_rad_conv1d(const RadArgs* a, RadStream s) {
dim3(256), 0, (hipStream_t)s, (const uint16_t*)x->data,
(const uint16_t*)w->data, bp, rp,
(const int*)cu->data, (uint16_t*)y->data, (int)n_seq, 0, (int)ry,
(int)cin, (int)cout, (int)k, (int)dil, pad, (int)xl, (int)yl,
(int)rl, act);
(int)cin, (int)cout, (int)k, (int)dil, pad, (int)st, (int)cd,
(int)xl, (int)yl, (int)rl, act);
}
} else {
hipLaunchKernelGGL(conv1d_direct_kernel, dim3(blocks_for(ry * cout)), dim3(kT), 0,
(hipStream_t)s, (const uint16_t*)x->data, (const uint16_t*)w->data,
bp, rp, (const int*)cu->data, (uint16_t*)y->data,
(int)n_seq, 0, (int)ry, (int)cin, (int)cout, (int)k, (int)dil, pad,
(int)xl, (int)yl, (int)rl, act);
(int)st, (int)cd, (int)xl, (int)yl, (int)rl, act);
}
return st_of(hipGetLastError());
}
@@ -1237,11 +1324,12 @@ extern "C" int r4d_rad_conv1d_f32(const RadArgs* a, RadStream s) {
if (!r4d_contig(w) || (b && !r4d_contig(b)) || !r4d_contig(cu)) return R4D_NO(RAD_E_STRIDE);
const long long cin = r4d_geti_or(a, "cin", 0), cout = r4d_geti_or(a, "cout", 0);
const long long k = r4d_geti_or(a, "k", 0), dil = r4d_geti_or(a, "dil", 1);
const long long causal = r4d_geti_or(a, "causal", 0);
const long long causal = r4d_geti_or(a, "causal", 0), st = r4d_geti_or(a, "stride", 1);
const long long cd = r4d_geti_or(a, "cu_div", 1);
const int act = conv1d_act(r4d_gets(a, "act"));
if (act < 0) return R4D_NO(RAD_E_INVAL);
if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && (k & 1) == 0) || dil <= 0 || causal < 0 ||
causal > 1)
causal > 1 || st < 1 || (st > 1 && (!causal || dil * (k - 1) + 1 < st)) || cd < 1)
return R4D_NO(RAD_E_SHAPE);
long long rx = 0, ry = 0, rr = 0;
const long long xl = r4d_rows_pitch(x, cin, &rx), yl = r4d_rows_pitch(y, cout, &ry);
@@ -1249,18 +1337,19 @@ extern "C" int r4d_rad_conv1d_f32(const RadArgs* a, RadStream s) {
if (xl < 0 || yl < 0 || rl < 0) return R4D_NO(RAD_E_STRIDE);
const long long n_seq = r4d_numel(cu) - 1;
/* The tile's loaders index x and w with 32-bit element offsets. */
if (n_seq <= 0 || ry <= 0 || rx < ry || (res && rr < ry) || r4d_numel(w) < cout * cin * k ||
(b && r4d_numel(b) < cout) || ry > 0x7fffffffll || (ry + 127) / 128 > 65535 ||
ry * xl >= (1ll << 32) || cout * cin * k >= (1ll << 32))
if (n_seq <= 0 || ry <= 0 || rx < ry * st || (res && rr < ry) ||
r4d_numel(w) < cout * cin * k || (b && r4d_numel(b) < cout) || ry > 0x7fffffffll ||
(ry + 127) / 128 > 65535 || ry * st * xl >= (1ll << 32) || cout * cin * k >= (1ll << 32))
return R4D_NO(RAD_E_SHAPE);
const int pad = (int)(causal ? dil * (k - 1) : dil * (k - 1) / 2);
const int pad = (int)(causal ? dil * (k - 1) + 1 - st : dil * (k - 1) / 2);
const float* bp = b ? (const float*)b->data : nullptr;
const float* rp = res ? (const float*)res->data : nullptr;
if (cout < 16) {
hipLaunchKernelGGL(conv1d_f32_direct_kernel, dim3(blocks_for(ry * cout)), dim3(kT), 0,
(hipStream_t)s, (const float*)x->data, (const float*)w->data, bp, rp,
(const int*)cu->data, (float*)y->data, (int)n_seq, (int)ry, (int)cin,
(int)cout, (int)k, (int)dil, pad, (int)xl, (int)yl, (int)rl, act);
(int)cout, (int)k, (int)dil, pad, (int)st, (int)cd, (int)xl, (int)yl,
(int)rl, act);
return st_of(hipGetLastError());
}
const int tn = cout > 64 ? 8 : cout > 32 ? 4 : 2;
@@ -1269,7 +1358,8 @@ extern "C" int r4d_rad_conv1d_f32(const RadArgs* a, RadStream s) {
hipLaunchKernelGGL((conv1d_f32_kernel<TN>), grid, dim3(r4d_f32::kThr), 0, (hipStream_t)s, \
(const float*)x->data, (const float*)w->data, bp, rp, \
(const int*)cu->data, (float*)y->data, (int)n_seq, (int)ry, (int)cin, \
(int)cout, (int)k, (int)dil, pad, (int)xl, (int)yl, (int)rl, act)
(int)cout, (int)k, (int)dil, pad, (int)st, (int)cd, (int)xl, (int)yl, \
(int)rl, act)
if (tn == 8) C1F(8);
else if (tn == 4) C1F(4);
else C1F(2);
@@ -1433,6 +1523,37 @@ extern "C" int r4d_rad_rvq_embed(const RadArgs* a, RadStream s) {
return st_of(hipGetLastError());
}
extern "C" int r4d_rad_vq_encode(const RadArgs* a, RadStream s) {
const RadTensor *x = r4d_opt(a, 0), *iw = r4d_opt(a, 1), *ib = r4d_opt(a, 2);
const RadTensor *cb = r4d_opt(a, 3), *ow = r4d_opt(a, 4), *ob = r4d_opt(a, 5);
const RadTensor* codes = r4d_opt(a, 6);
if (!x || !iw || !ib || !cb || !ow || !ob || !codes) return R4D_NO(RAD_E_INVAL);
auto bf_or_f32 = [](const RadTensor* t) { return t->dtype == RAD_BF16 || t->dtype == RAD_F32; };
if (!is_i32(codes) || !bf_or_f32(x) || !bf_or_f32(iw) || !bf_or_f32(ib) || !bf_or_f32(cb) ||
!bf_or_f32(ow) || !bf_or_f32(ob))
return R4D_NO(RAD_E_DTYPE);
const long long n = r4d_geti_or(a, "n", 0), dim = r4d_geti_or(a, "dim", 0);
const long long bins = r4d_geti_or(a, "bins", 0);
if (n <= 0 || dim <= 0 || dim > kVqMaxDim || bins <= 0) return R4D_NO(RAD_E_SHAPE);
long long rx = 0;
const long long xl = r4d_rows_pitch(x, n, &rx);
if (xl < 0 || !r4d_contig(iw) || !r4d_contig(ib) || !r4d_contig(cb) || !r4d_contig(ow) ||
!r4d_contig(ob))
return R4D_NO(RAD_E_STRIDE);
if (rx <= 0 || r4d_numel(codes) < rx || r4d_numel(iw) < dim * n || r4d_numel(ib) < dim ||
r4d_numel(cb) < bins * dim || r4d_numel(ow) < n * dim || r4d_numel(ob) < n ||
rx > 0x7fffffffll)
return R4D_NO(RAD_E_SHAPE);
/* A code at the operand's own stride: a row of a code-major plane, or a column of one. */
const long long cs = codes->rank >= 1 ? codes->stride[codes->rank - 1] : 1;
auto f32 = [](const RadTensor* t) { return t->dtype == RAD_F32 ? 1 : 0; };
hipLaunchKernelGGL(vq_encode_kernel, dim3((unsigned)rx), dim3(kT), 0, (hipStream_t)s, x->data,
f32(x), (int)xl, iw->data, f32(iw), ib->data, f32(ib), cb->data, f32(cb),
ow->data, f32(ow), ob->data, f32(ob), (int*)codes->data, (int64_t)cs,
(int)n, (int)dim, (int)bins);
return st_of(hipGetLastError());
}
extern "C" int r4d_rad_frame_mix_store(const RadArgs* a, RadStream s) {
const RadTensor *hid = r4d_opt(a, 0), *lw = r4d_opt(a, 1), *sc = r4d_opt(a, 2);
const RadTensor *lane = r4d_opt(a, 3), *fr = r4d_opt(a, 4), *st = r4d_opt(a, 5);
+1
View File
@@ -200,6 +200,7 @@ int r4d_rad_conv_t1d_f32 (const RadArgs*, RadStream);
int r4d_rad_snake (const RadArgs*, RadStream);
int r4d_rad_cfg_topk_sample (const RadArgs*, RadStream);
int r4d_rad_rvq_embed (const RadArgs*, RadStream);
int r4d_rad_vq_encode (const RadArgs*, RadStream);
int r4d_rad_frame_mix_store (const RadArgs*, RadStream);
int r4d_rad_end_status (const RadArgs*, RadStream);
int r4d_rad_ras_pick (const RadArgs*, RadStream);
+21 -1
View File
@@ -2372,7 +2372,8 @@ static const RadOperandSpec oAttnPS[] = {
/* ---- audio generation (r4d_audio.hip); the same specs as libref's (ref_registry.cpp). */
static const RadParamSpec pConv1d[] = { P_INT("M"), P_INT("cin"), P_INT("cout"), P_INT("k"),
P_INT("dil"), P_STRO("act"), P_STR("dtype"),
P_INTO("causal") };
P_INTO("causal"), P_INTO("stride"),
P_INTO("cu_div") };
static const RadOperandSpec oConv1d[] = {
{ "x", RAD_OPD_IN, 0 }, { "w", RAD_OPD_WEIGHT, 0 }, { "cu_seqlens", RAD_OPD_IN, 0 },
{ "res", RAD_OPD_IN, 1 }, { "y", RAD_OPD_OUT, 0 }, { "b", RAD_OPD_WEIGHT, 1 },
@@ -2405,6 +2406,13 @@ static const RadOperandSpec oRvqEmbed[] = {
{ "codes", RAD_OPD_IN, 0 }, { "t0", RAD_OPD_WEIGHT, 0 }, { "t1", RAD_OPD_WEIGHT, 0 },
{ "y", RAD_OPD_OUT, 0 },
};
static const RadParamSpec pVqEncode[] = { P_INT("M"), P_INT("n"), P_INT("dim"), P_INT("bins"),
P_STR("dtype") };
static const RadOperandSpec oVqEncode[] = {
{ "x", RAD_OPD_INOUT, 0 }, { "in_w", RAD_OPD_WEIGHT, 0 }, { "in_b", RAD_OPD_WEIGHT, 0 },
{ "cb", RAD_OPD_WEIGHT, 0 }, { "out_w", RAD_OPD_WEIGHT, 0 }, { "out_b", RAD_OPD_WEIGHT, 0 },
{ "codes", RAD_OPD_OUT, 0 },
};
static const RadParamSpec pFrameMix[] = { P_INT("M"), P_INT("n_embd"), P_INT("L"), P_INT("stride"),
P_INTO("rep"), P_STR("dtype") };
static const RadOperandSpec oFrameMix[] = {
@@ -3338,6 +3346,9 @@ static const RadOpSchema kSchemas[] = {
SCHEMA("rvq_embed", pRvqEmbed, oRvqEmbed,
"a frame's embedding from code-major residual codes, the first codebook's table and the "
"others' stacked one, times `scale`; `round` 1 rounds the f32 sum once."),
SCHEMA("vq_encode", pVqEncode, oVqEncode,
"one residual vector quantiser step: the code of greatest cosine with in_w x + in_b "
"(the lower index on a tie), then x -= out_w cb[code] + out_b."),
SCHEMA("frame_mix_store", pFrameMix, oFrameMix,
"a frame's conditioning row (softmax-weighted planes times scale) into its lane."),
SCHEMA("end_status", pEndStatus, oEndStatus,
@@ -4825,6 +4836,8 @@ static const RadConstraint cRmsMod[] = { C_IN("dtype", "bf16"), C_DIV("n", 8), C
static const RadConstraint cRmsModQ[] = { C_IN("dtype", "bf16"), C_DIV("n", 128), C_LE("n", 8192),
C_EQ("group", 128) };
static const RadConstraint cGenF32[] = { C_IN("dtype", "f32") };
/* vq_encode: e's sums staged in LDS, a codebook dimension of 32 at most. */
static const RadConstraint cVqEncode[] = { C_IN("dtype", "bf16 f32"), C_LE("dim", 32) };
static const RadConstraint cPatchUnfold[] = { C_IN("dtype", "f32"), C_LE("C", 64) };
static const RadConstraint cGenAnyFloat[] = { C_IN("dtype", "bf16 f32") };
static const RadConstraint cSnakeAa[] = { C_IN("dtype", "f32"), C_LE("k", 32) };
@@ -8289,6 +8302,13 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
ROW(cGenBf16), NOTUNE,
nullptr, nullptr, r4d_rad_rvq_embed },
{ "vq_encode", "vq_encode", "elem",
"a residual quantiser's step: the nearest code by cosine", "a workgroup a row, dim <= 32",
"bf16 or f32 rows and weights, f32 arithmetic, the row rounded once",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cVqEncode), NOTUNE,
nullptr, nullptr, r4d_rad_vq_encode },
{ "frame_mix_store_bf16", "frame_mix_store", "elem",
"a frame's conditioning row into its lane", "a workgroup a lane, L <= 16",
"bf16 planes and rows, f32 logits and scale",
+152 -32
View File
@@ -5607,18 +5607,28 @@ int64_t seq_of(const std::vector<int32_t>& cu, int64_t r) {
return s;
}
/* conv1d: the causal form beside the symmetric one, on the tiled and the direct kernels. */
/* conv1d: the causal form beside the symmetric one, on the tiled and the direct kernels; strided
* causal forms (a codec encoder's downsampling, kernels of two strides and of one) over sequences
* of whole strides, the bounds also in units of 8 rows (cu_div, an encoder's samples). */
void conv1d_cases(const Plugin& pl) {
const RadKernelInfo* r = find(pl, "conv1d_bf16");
if (!r) { ok("conv1d_bf16", false, "row absent"); return; }
struct C { int64_t cin, cout, k, dil; int causal, extras; };
for (const C& c : { C{ 16, 32, 3, 2, 0, 0 }, C{ 16, 32, 3, 2, 1, 1 }, C{ 16, 128, 4, 1, 1, 0 },
C{ 6, 3, 7, 3, 1, 1 }, C{ 6, 3, 2, 1, 1, 0 } }) {
const std::vector<int32_t> cu = { 0, 13, 30 };
const int64_t M = 30;
struct C { int64_t cin, cout, k, dil; int causal, extras; int64_t st, cd = 1; };
for (const C& c : { C{ 16, 32, 3, 2, 0, 0, 1 }, C{ 16, 32, 3, 2, 1, 1, 1 },
C{ 16, 128, 4, 1, 1, 0, 1 }, C{ 6, 3, 7, 3, 1, 1, 1 },
C{ 6, 3, 2, 1, 1, 0, 1 }, C{ 16, 32, 4, 1, 1, 0, 2 },
C{ 16, 128, 16, 1, 1, 1, 8 }, C{ 6, 3, 8, 1, 1, 1, 4 },
C{ 16, 32, 2, 1, 1, 0, 2 }, C{ 16, 64, 4, 1, 1, 1, 2, 8 },
C{ 6, 3, 8, 1, 1, 0, 4, 8 } }) {
/* Bounds of whole strides: 13 and 30 rounded up to the stride; the op reads them times
* cu_div. */
const int64_t s1 = (13 + c.st - 1) / c.st * c.st, s2 = (30 + c.st - 1) / c.st * c.st;
const std::vector<int32_t> cu = { 0, (int32_t)s1, (int32_t)s2 };
const std::vector<int32_t> cu_op = { 0, (int32_t)(s1 * c.cd), (int32_t)(s2 * c.cd) };
const int64_t M = s2, MO = M / c.st;
std::vector<float> x = randv((size_t)(M * c.cin), 1.0f);
std::vector<float> w = randv((size_t)(c.cout * c.cin * c.k), 0.3f);
std::vector<float> b = randv((size_t)c.cout, 0.5f), res = randv((size_t)(M * c.cout), 0.5f);
std::vector<float> b = randv((size_t)c.cout, 0.5f), res = randv((size_t)(MO * c.cout), 0.5f);
void* dx = up_bf16(x);
void* db = up_bf16(b);
void* dres = up_bf16(res);
@@ -5626,27 +5636,31 @@ void conv1d_cases(const Plugin& pl) {
std::vector<RadParam> ps = { PI("M", M), PI("cin", c.cin), PI("cout", c.cout),
PI("k", c.k), PI("dil", c.dil), PS("act", c.extras ? "tanh" : "none"),
PS("dtype", "bf16"), PI("causal", c.causal) };
const std::string nm = fmt("conv1d_bf16 cin %lld cout %lld k %lld dil %lld causal %d%s",
if (c.st > 1) ps.push_back(PI("stride", c.st));
if (c.cd > 1) ps.push_back(PI("cu_div", c.cd));
const std::string nm = fmt("conv1d_bf16 cin %lld cout %lld k %lld dil %lld causal %d "
"stride %lld cu_div %lld%s",
(long long)c.cin, (long long)c.cout, (long long)c.k,
(long long)c.dil, c.causal, c.extras ? " +bias+res+tanh" : "");
(long long)c.dil, c.causal, (long long)c.st, (long long)c.cd,
c.extras ? " +bias+res+tanh" : "");
void* dw = stored_weight(r, ps, w, c.cout, c.cin * c.k, nm.c_str());
void* dcu = up_i32(cu);
void* dy = dalloc((size_t)(M * c.cout) * 2);
void* dcu = up_i32(cu_op);
void* dy = dalloc((size_t)(MO * c.cout) * 2);
if (!dw) continue;
RadTensor tres = T(dres, RAD_BF16, { M, c.cout }), tb = T(db, RAD_BF16, { c.cout });
RadTensor tres = T(dres, RAD_BF16, { MO, c.cout }), tb = T(db, RAD_BF16, { c.cout });
if (!c.extras) { absent(tres); absent(tb); }
const int rc = launch(r, { T(dx, RAD_BF16, { M, c.cin }), T(dw, RAD_BF16, { c.cout, c.cin, c.k }),
T(dcu, RAD_I32, { 3 }), tres, T(dy, RAD_BF16, { M, c.cout }), tb },
T(dcu, RAD_I32, { 3 }), tres, T(dy, RAD_BF16, { MO, c.cout }), tb },
ps);
if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; }
const int64_t pad = c.causal ? c.dil * (c.k - 1) : c.dil * (c.k - 1) / 2;
std::vector<float> want((size_t)(M * c.cout));
for (int64_t row = 0; row < M; ++row) {
const int64_t e = seq_of(cu, row), s0 = cu[(size_t)e], s1 = cu[(size_t)e + 1];
const int64_t pad = c.causal ? c.dil * (c.k - 1) + 1 - c.st : c.dil * (c.k - 1) / 2;
std::vector<float> want((size_t)(MO * c.cout));
for (int64_t row = 0; row < MO; ++row) {
const int64_t e = seq_of(cu, row * c.st), s0 = cu[(size_t)e], s1 = cu[(size_t)e + 1];
for (int64_t co = 0; co < c.cout; ++co) {
float acc = 0.0f;
for (int64_t kk = 0; kk < c.k; ++kk) {
const int64_t ir = row + kk * c.dil - pad;
const int64_t ir = row * c.st + kk * c.dil - pad;
if (ir < s0 || ir >= s1) continue;
for (int64_t ci = 0; ci < c.cin; ++ci)
acc += w[(size_t)((co * c.cin + ci) * c.k + kk)] * x[(size_t)(ir * c.cin + ci)];
@@ -6081,6 +6095,104 @@ void rvq_embed_cases(const Plugin& pl) {
}
}
/* vq_encode at Fish Audio's codec geometry (n 1024, codes of dimension 8, a semantic codebook of
* 4096 and a residual one of 1024) and an odd one, bf16 and f32 rows, f32 and bf16 weights, the
* codes a dense row and a strided column: each code against the host's cosine argmax in double (a
* pick within 1e-5 of the best score is a near tie, not a miss), the row after against
* x - out_w cb[code] - out_b for the code the kernel took. */
void vq_encode_cases(const Plugin& pl) {
const RadKernelInfo* r = find(pl, "vq_encode");
if (!r) { ok("vq_encode", false, "row absent"); return; }
struct C { int64_t n, dim, bins, M; bool f32x, f32w; int64_t cst; };
for (const C& c : { C{ 1024, 8, 4096, 37, false, true, 1 }, C{ 1024, 8, 1024, 37, false, true, 1 },
C{ 1024, 8, 1024, 20, true, true, 3 }, C{ 1024, 8, 4096, 9, false, false, 1 },
C{ 200, 5, 300, 11, true, false, 2 } }) {
const int64_t n = c.n, dim = c.dim, bins = c.bins, M = c.M;
std::vector<float> x = randv((size_t)(M * n), 1.0f);
std::vector<float> iw = randv((size_t)(dim * n), 1.0f / std::sqrt((float)n));
std::vector<float> ib = randv((size_t)dim, 0.1f), cb = randv((size_t)(bins * dim), 1.0f);
std::vector<float> ow = randv((size_t)(n * dim), 0.3f), ob = randv((size_t)n, 0.1f);
/* A bf16 upload rounds the host copy to what the device holds. */
const uint32_t wdt = c.f32w ? RAD_F32 : RAD_BF16, xdt = c.f32x ? RAD_F32 : RAD_BF16;
auto upw = [&](std::vector<float>& w) { return c.f32w ? up_f32(w) : up_bf16(w); };
void* dx = c.f32x ? up_f32(x) : up_bf16(x);
const std::vector<int32_t> plane((size_t)(M * c.cst), -7);
void* dc = up_i32(plane);
RadTensor tc = T(dc, RAD_I32, { M });
tc.stride[0] = c.cst;
std::vector<RadParam> ps = { PI("M", M), PI("n", n), PI("dim", dim), PI("bins", bins),
PS("dtype", c.f32x ? "f32" : "bf16") };
const std::string nm = fmt("vq_encode n %lld dim %lld bins %lld, %s rows, %s weights, "
"code stride %lld", (long long)n, (long long)dim,
(long long)bins, c.f32x ? "f32" : "bf16",
c.f32w ? "f32" : "bf16", (long long)c.cst);
const int rc = launch(r, { T(dx, xdt, { M, n }), T(upw(iw), wdt, { dim, n }),
T(upw(ib), wdt, { dim }), T(upw(cb), wdt, { bins, dim }),
T(upw(ow), wdt, { n, dim }), T(upw(ob), wdt, { n }), tc },
ps);
if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; }
const std::vector<int32_t> got = down_i32(dc, plane.size());
const std::vector<float> gx = c.f32x ? down_f32(dx, x.size()) : down_bf16(dx, x.size());
int64_t miss = 0, near = 0, gaps = 0;
std::vector<float> want = x;
for (int64_t i = 0; i < M; ++i) {
std::vector<double> e((size_t)dim);
double ss = 0;
for (int64_t j = 0; j < dim; ++j) {
double acc = ib[(size_t)j];
for (int64_t d = 0; d < n; ++d)
acc += (double)iw[(size_t)(j * n + d)] * x[(size_t)(i * n + d)];
e[(size_t)j] = acc;
ss += acc * acc;
}
const double en = std::max(std::sqrt(ss), 1e-12);
auto score = [&](int64_t k) {
const float* row = cb.data() + k * dim;
double cs = 0, dot = 0;
for (int64_t j = 0; j < dim; ++j) cs += (double)row[j] * row[j];
const double cn = std::max(std::sqrt(cs), 1e-12);
for (int64_t j = 0; j < dim; ++j) dot += e[(size_t)j] / en * (row[j] / cn);
return dot;
};
int64_t best = 0;
double bs = -1e300;
for (int64_t k = 0; k < bins; ++k) {
const double s = score(k);
if (s > bs) { bs = s; best = k; }
}
for (int64_t g = 1; g < c.cst; ++g)
if (got[(size_t)(i * c.cst + g)] != -7) ++gaps;
const int64_t k = got[(size_t)(i * c.cst)];
if (k < 0 || k >= bins) { ++miss; continue; }
if (k != best) {
if (bs - score(k) <= 1e-5) ++near;
else ++miss;
}
for (int64_t d = 0; d < n; ++d) {
double q = ob[(size_t)d];
for (int64_t j = 0; j < dim; ++j)
q += (double)ow[(size_t)(d * dim + j)] * cb[(size_t)(k * dim + j)];
want[(size_t)(i * n + d)] = (float)(x[(size_t)(i * n + d)] - q);
}
}
ok((nm + " codes").c_str(), miss == 0 && gaps == 0,
fmt("%lld of %lld codes wrong (%lld near ties taken), %lld gaps of the strided codes "
"written", (long long)miss, (long long)M, (long long)near, (long long)gaps));
if (c.f32x) {
/* f32 rows: against the f32 rows themselves (check() rounds its reference to bf16). */
double e = 0, r2 = 0;
for (size_t i = 0; i < want.size(); ++i) {
e += ((double)gx[i] - want[i]) * ((double)gx[i] - want[i]);
r2 += (double)want[i] * want[i];
}
const double rl = std::sqrt(e / std::max(r2, 1e-30));
ok(fmt("%s residual (rel_l2 %.3g, bound 1e-05)", nm.c_str(), rl).c_str(), rl <= 1e-5);
} else {
check(nm + " residual", gx, want, 1e-2);
}
}
}
/* frame_store: each lane's codes into its block of the history, frames before `skip` and lanes
* whose status ended the piece left alone. Exact. */
void frame_store_cases(const Plugin& pl) {
@@ -9331,36 +9443,43 @@ void gate_add_none_case(const Plugin& pl) {
/* conv1d at f32 at a BigVGAN's geometries: conv_pre's 2048 -> 1024 over 7 taps, the AMP blocks'
* dilated 3/7/11 taps with the residual the output itself, the direct kernel's 8 channels and the
* final conv's one with its clamp. Two sequences, so a tap crossing a sequence's edge reads zero. */
* final conv's one with its clamp; then a DAC encoder's strided causal downsampling (kernels of two
* strides, 2 to 8, and the quantizer's of one). Two sequences, so a tap crossing a sequence's edge
* reads zero; a strided case's sequences are whole strides. */
void conv1d_f32_cases(const Plugin& pl) {
const RadKernelInfo* r = find(pl, "conv1d_f32");
if (!r) { ok("conv1d_f32", false, "row absent"); return; }
struct C { int64_t cin, cout, k, dil, M0, M1; int act, bias, res; }; /* res 2: y itself */
struct C { int64_t cin, cout, k, dil, M0, M1; int act, bias, res; int64_t st = 1; }; /* res 2: y itself */
for (const C& c : { C{ 2048, 1024, 7, 1, 9, 15, 0, 1, 0 }, C{ 64, 128, 7, 1, 37, 60, 0, 1, 0 },
C{ 32, 32, 11, 5, 70, 41, 0, 1, 2 }, C{ 16, 16, 3, 3, 50, 77, 0, 1, 1 },
C{ 48, 40, 3, 1, 33, 30, 1, 1, 0 }, C{ 8, 8, 11, 5, 90, 64, 0, 1, 2 },
C{ 8, 1, 7, 1, 100, 99, 2, 0, 0 }, C{ 64, 64, 11, 1, 200, 137, 2, 1, 2 } }) {
C{ 8, 1, 7, 1, 100, 99, 2, 0, 0 }, C{ 64, 64, 11, 1, 200, 137, 2, 1, 2 },
C{ 64, 128, 4, 1, 36, 60, 0, 1, 0, 2 }, C{ 128, 256, 8, 1, 64, 40, 0, 1, 1, 4 },
C{ 256, 512, 16, 1, 96, 136, 0, 1, 0, 8 }, C{ 8, 8, 16, 1, 32, 48, 0, 1, 0, 8 },
C{ 64, 64, 2, 1, 50, 30, 0, 1, 2, 2 } }) {
const std::vector<int32_t> cu = { 0, (int32_t)c.M0, (int32_t)(c.M0 + c.M1) };
const int64_t M = c.M0 + c.M1;
const int64_t M = c.M0 + c.M1, MO = M / c.st;
std::vector<float> x = randv((size_t)(M * c.cin), 1.0f);
std::vector<float> w = randv((size_t)(c.cout * c.cin * c.k), 1.0f / std::sqrt((float)(c.cin * c.k)));
std::vector<float> b = randv((size_t)c.cout, 0.5f), res = randv((size_t)(M * c.cout), 0.5f);
std::vector<float> b = randv((size_t)c.cout, 0.5f), res = randv((size_t)(MO * c.cout), 0.5f);
std::vector<RadParam> ps = { PI("M", M), PI("cin", c.cin), PI("cout", c.cout), PI("k", c.k),
PI("dil", c.dil),
PS("act", c.act == 1 ? "tanh" : c.act == 2 ? "clamp" : "none"),
PS("dtype", "f32") };
const std::string nm = fmt("conv1d_f32 cin %lld cout %lld k %lld dil %lld%s%s%s", (long long)c.cin,
if (c.st > 1) { ps.push_back(PI("causal", 1)); ps.push_back(PI("stride", c.st)); }
const std::string nm = fmt("conv1d_f32 cin %lld cout %lld k %lld dil %lld%s%s%s%s", (long long)c.cin,
(long long)c.cout, (long long)c.k, (long long)c.dil,
c.st > 1 ? fmt(" causal stride %lld", (long long)c.st).c_str() : "",
c.bias ? " +bias" : "", c.res == 2 ? " +res in place" : c.res ? " +res" : "",
c.act == 1 ? " tanh" : c.act == 2 ? " clamp" : "");
void* dw = stored_weight_f32(r, ps, w, c.cout, c.cin * c.k, nm.c_str());
if (!dw) continue;
void* dx = up_f32(x);
void* db = up_f32(b);
void* dy = up_f32(c.res == 2 ? res : std::vector<float>((size_t)(M * c.cout), 0.0f));
void* dy = up_f32(c.res == 2 ? res : std::vector<float>((size_t)(MO * c.cout), 0.0f));
void* dr = up_f32(res);
RadTensor ty = T(dy, RAD_F32, { M, c.cout });
RadTensor tr = c.res == 2 ? ty : T(dr, RAD_F32, { M, c.cout });
RadTensor ty = T(dy, RAD_F32, { MO, c.cout });
RadTensor tr = c.res == 2 ? ty : T(dr, RAD_F32, { MO, c.cout });
RadTensor tb = T(db, RAD_F32, { c.cout });
if (!c.res) absent(tr);
if (!c.bias) absent(tb);
@@ -9369,14 +9488,14 @@ void conv1d_f32_cases(const Plugin& pl) {
T(up_i32(cu), RAD_I32, { 3 }), tr, ty, tb },
ps);
if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; }
const int64_t pad = c.dil * (c.k - 1) / 2;
std::vector<float> want((size_t)(M * c.cout));
for (int64_t row = 0; row < M; ++row) {
const int64_t e = seq_of(cu, row), s0 = cu[(size_t)e], s1 = cu[(size_t)e + 1];
const int64_t pad = c.st > 1 ? c.dil * (c.k - 1) + 1 - c.st : c.dil * (c.k - 1) / 2;
std::vector<float> want((size_t)(MO * c.cout));
for (int64_t row = 0; row < MO; ++row) {
const int64_t e = seq_of(cu, row * c.st), s0 = cu[(size_t)e], s1 = cu[(size_t)e + 1];
for (int64_t co = 0; co < c.cout; ++co) {
double acc = 0;
for (int64_t kk = 0; kk < c.k; ++kk) {
const int64_t ir = row + kk * c.dil - pad;
const int64_t ir = row * c.st + kk * c.dil - pad;
if (ir < s0 || ir >= s1) continue;
for (int64_t ci = 0; ci < c.cin; ++ci)
acc += (double)w[(size_t)((co * c.cin + ci) * c.k + kk)] * x[(size_t)(ir * c.cin + ci)];
@@ -25065,6 +25184,7 @@ static void device_phase(const Plugin& pl) {
if (sel("cfg_topk_sample")) au::cfg_topk_cases(pl);
if (sel("ras_pick")) au::ras_pick_cases(pl);
if (sel("rvq_embed_bf16")) au::rvq_embed_cases(pl);
if (sel("vq_encode")) au::vq_encode_cases(pl);
if (sel("frame_store")) au::frame_store_cases(pl);
if (sel("iota")) au::iota_cases(pl);
if (sel("gelu_bf16")) au::gelu_case(pl);