Archived
merge 1.12.0: gemma4 E2B/E4B and Nemotron Bolt E4B arrive; the ref2va audio, rows and ABI work carried across
conv1d keeps both stride forms: upstream's causal codec downsample with cu_div (Fish Audio) and the port's explicit-pad non-causal one (the H3 audio encoder's), in libref, libavx and libr4d alike; the ABI takes upstream's 27 on top of the port's RadVideoGenBatch growth.
This commit is contained in:
commit
7c3b5ab7d5
285 files changed
+30500
-2898
No files matched your search
+284
-61
@@ -79,7 +79,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 st, 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;
|
||||
@@ -101,9 +102,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;
|
||||
@@ -124,7 +125,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] * st + 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]);
|
||||
};
|
||||
@@ -216,17 +217,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 st, 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 * st + 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;
|
||||
@@ -386,14 +388,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) {
|
||||
@@ -401,9 +403,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;
|
||||
@@ -411,9 +413,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;
|
||||
}
|
||||
@@ -430,12 +433,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);
|
||||
@@ -460,16 +464,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;
|
||||
@@ -624,6 +629,11 @@ __device__ uint32_t kth_key(F val, int j0, int j1, int kth, int* sh) {
|
||||
// [sup_lo, sup_hi) are optional. The kept values are divided by the temperature, as libref divides:
|
||||
// a multiply by its inverse rounds differently and can move a pick across a CDF boundary. A lane
|
||||
// with no candidate left writes -1.
|
||||
//
|
||||
// `top_p` < 1 cuts the kept list where the whole candidate set's cumulative probability passes it
|
||||
// (Fish Audio's draw). The normaliser is each thread's slice summed in index order, the slices
|
||||
// then summed by thread 0 in thread order: index order as libref sums, associated differently,
|
||||
// which in double moves a cut only for a cumulative within ~1e-16 of top_p.
|
||||
constexpr int kMaxK = 256;
|
||||
|
||||
__global__ __launch_bounds__(kT) void cfg_topk_kernel(
|
||||
@@ -631,9 +641,10 @@ __global__ __launch_bounds__(kT) void cfg_topk_kernel(
|
||||
const int* __restrict__ frame, int* __restrict__ ids, int V, int K, int rep, int cond_k,
|
||||
float cfg, float temp, uint64_t stream, const float* __restrict__ cfgs,
|
||||
const int* __restrict__ lane, int* __restrict__ hist, int n_hist, int sup_lo, int sup_hi,
|
||||
float pen) {
|
||||
float pen, float top_p) {
|
||||
__shared__ int sh[8];
|
||||
__shared__ int s_pre[kT];
|
||||
__shared__ double s_z[kT];
|
||||
__shared__ float s_g[kMaxK];
|
||||
__shared__ int s_i[kMaxK];
|
||||
__shared__ float s_e[kMaxK];
|
||||
@@ -723,8 +734,29 @@ __global__ __launch_bounds__(kT) void cfg_topk_kernel(
|
||||
s_pre[rank] = mi;
|
||||
}
|
||||
__syncthreads();
|
||||
if (top_p < 1.f) {
|
||||
const float m = s_e[0] / temp;
|
||||
double z = 0;
|
||||
for (int j = j0; j < j1; ++j)
|
||||
if (cand(j)) z += exp((double)(gval(j) / temp - m));
|
||||
s_z[tid] = z;
|
||||
}
|
||||
__syncthreads();
|
||||
if (tid == 0) {
|
||||
const float m = s_e[0] / temp;
|
||||
int kp = kk;
|
||||
if (top_p < 1.f) {
|
||||
double z = 0;
|
||||
for (int t = 0; t < kT; ++t) z += s_z[t];
|
||||
double cum = 0;
|
||||
kp = 1;
|
||||
for (int t = 0; t < kk; ++t) {
|
||||
cum += exp((double)(s_e[t] / temp - m)) / z;
|
||||
if (t > 0 && cum > (double)top_p) break;
|
||||
kp = t + 1;
|
||||
}
|
||||
}
|
||||
const int kk = kp;
|
||||
double tot = 0;
|
||||
for (int t = 0; t < kk; ++t) tot += exp((double)(s_e[t] / temp - m));
|
||||
const double u = (double)rad_philox_uniform_s(seed_of(seed, ln),
|
||||
@@ -763,15 +795,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;
|
||||
@@ -780,6 +813,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).
|
||||
@@ -822,6 +935,32 @@ __global__ __launch_bounds__(kT) void frame_mix_kernel(
|
||||
}
|
||||
}
|
||||
|
||||
// Fish Audio's repetition-aware choice between two draws, a thread a lane (libref's
|
||||
// ref_ras_pick): the alternative where the first draw is in [lo, hi) and in the lane's window of
|
||||
// its last `W` picks, the pick then entering the window. A lane whose window lies outside `hist`
|
||||
// takes the first draw and keeps no window.
|
||||
__global__ __launch_bounds__(kT) void ras_pick_kernel(
|
||||
const int* __restrict__ first, const int* __restrict__ alt, const int* __restrict__ lane,
|
||||
int* __restrict__ hist, int* __restrict__ ids, int n, int W, int lo, int hi, int rep,
|
||||
int clear, long long n_hist) {
|
||||
const int i = blockIdx.x * kT + threadIdx.x;
|
||||
if (i >= n) return;
|
||||
const int f = first[rep * i];
|
||||
const long long L = lane[i];
|
||||
int pick = f;
|
||||
if (L >= 0 && (L + 1) * W <= n_hist) {
|
||||
int* h = hist + L * W;
|
||||
if (clear)
|
||||
for (int w = 0; w < W; ++w) h[w] = -1;
|
||||
if (f >= lo && f < hi)
|
||||
for (int w = 0; w < W; ++w)
|
||||
if (h[w] == f) { pick = alt[rep * i]; break; }
|
||||
for (int w = 0; w + 1 < W; ++w) h[w] = h[w + 1];
|
||||
h[W - 1] = pick;
|
||||
}
|
||||
for (int r = 0; r < rep; ++r) ids[rep * i + r] = pick;
|
||||
}
|
||||
|
||||
__global__ __launch_bounds__(kT) void end_status_kernel(
|
||||
const int* __restrict__ ids, const int* __restrict__ lane, int* __restrict__ status, int n,
|
||||
int rep, int end) {
|
||||
@@ -990,6 +1129,13 @@ __global__ __launch_bounds__(kT) void cu_repeat_kernel(const int* __restrict__ l
|
||||
if (j < n) y[j] = j * len[0] * mult;
|
||||
}
|
||||
|
||||
__global__ __launch_bounds__(kT) void sub_i32_kernel(const int* __restrict__ x,
|
||||
const int* __restrict__ s, int* __restrict__ y,
|
||||
int n) {
|
||||
const int j = blockIdx.x * kT + threadIdx.x;
|
||||
if (j < n) y[j] = x[j] - s[0];
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- attention
|
||||
// Dense causal attention over short sequences, head_dim 256, one query head a key head: a depth
|
||||
// decoder runs a frame's handful of positions, the codebooks so far. A workgroup a (sequence,
|
||||
@@ -1075,38 +1221,43 @@ extern "C" int r4d_rad_conv1d(const RadArgs* a, RadStream s) {
|
||||
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);
|
||||
/* The strided form: `stride` > 1 with `pad` explicit, any k, a single sequence covering all of
|
||||
* x (the encoder's downsample, k = 2 stride and pad ceil(stride / 2)). `pad` means nothing
|
||||
* without a stride, and the causal form keeps its own left-only padding. */
|
||||
/* Two strided forms: the causal one (implicit left pad, `cu_div` bounds) and a zero-padded
|
||||
* one with `pad` explicit, any k, a single sequence covering all of x (the encoder's
|
||||
* downsample, k = 2 stride and pad ceil(stride / 2)). `pad` means nothing without a stride,
|
||||
* and the causal form keeps its own left-only padding. */
|
||||
const long long st = r4d_geti_or(a, "stride", 1), pp = r4d_geti_or(a, "pad", -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);
|
||||
const bool padded = pp >= 0 && st > 1;
|
||||
if (cin <= 0 || cout <= 0 || k <= 0 || (!causal && st <= 1 && (k & 1) == 0) || dil <= 0 ||
|
||||
causal < 0 || causal > 1 || st < 1 || (st > 1 && (causal || pp < 0)) ||
|
||||
(st <= 1 && pp >= 0))
|
||||
causal < 0 || causal > 1 || st < 1 || (pp >= 0 && (st <= 1 || causal || cd != 1)) ||
|
||||
(st > 1 && pp < 0 && (!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 || (!padded && rx < ry * st) || (res && rr < ry) ||
|
||||
r4d_numel(w) < cout * cin * k || (b && r4d_numel(b) < cout))
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
/* The strided output length is PyTorch's, exactly (rx and ry are the row counts; xl and yl
|
||||
* are the pitches): a graph that wants a truncated floor drops the last output row itself.
|
||||
* One sequence, and cu_seqlens covers every input row (libref, which reads cu, checks its
|
||||
* ends; the zeroing gather below makes those ends the whole padding, so the length rule is
|
||||
* the part checkable here). */
|
||||
if (st > 1) {
|
||||
/* The padded strided output length is PyTorch's, exactly (rx and ry are the row counts; xl
|
||||
* and yl are the pitches): a graph that wants a truncated floor drops the last output row
|
||||
* itself. One sequence, and cu_seqlens covers every input row (libref, which reads cu,
|
||||
* checks its ends; the zeroing gather below makes those ends the whole padding, so the
|
||||
* length rule is the part checkable here). */
|
||||
if (padded) {
|
||||
if (n_seq != 1 || rx + 2 * pp < dil * (k - 1) + 1 ||
|
||||
ry != (rx + 2 * pp - dil * (k - 1) - 1) / st + 1)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
}
|
||||
const uint16_t* bp = b ? (const uint16_t*)b->data : nullptr;
|
||||
/* Both kernels read input row row * stride + tap * dil - pad: half the reach a side, all of
|
||||
* it on the left for the causal form, or the explicit `pad` around the strided gather. */
|
||||
const int pad = (int)(pp >= 0 ? pp : (causal ? dil * (k - 1) : dil * (k - 1) / 2));
|
||||
* it on the left less the stride for the causal form, or PyTorch's zero padding from the
|
||||
* explicit `pad` around the padded gather (whose bounds then need no cdiv). */
|
||||
const int pad = (int)(padded ? pp : (causal ? dil * (k - 1) + 1 - st : dil * (k - 1) / 2));
|
||||
const int cdiv = (int)(padded ? 1 : cd);
|
||||
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;
|
||||
@@ -1119,8 +1270,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)st, (int)xl,
|
||||
(int)yl, (int)rl, act);
|
||||
(int)cin, (int)cout, (int)k, (int)dil, pad, (int)st, (int)cdiv,
|
||||
(int)xl, (int)yl, (int)rl, act);
|
||||
} else {
|
||||
constexpr int TP = 4, TC = 1, BP = 128, BC = 64;
|
||||
hipLaunchKernelGGL((conv1d_igemm_kernel<TP, TC>),
|
||||
@@ -1128,15 +1279,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)st, (int)xl,
|
||||
(int)yl, (int)rl, act);
|
||||
(int)cin, (int)cout, (int)k, (int)dil, pad, (int)st, (int)cdiv,
|
||||
(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)st, (int)xl, (int)yl, (int)rl, act);
|
||||
(int)st, (int)cdiv, (int)xl, (int)yl, (int)rl, act);
|
||||
}
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
@@ -1200,7 +1351,8 @@ 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);
|
||||
/* The strided form lives on the bf16 rows (the DAC encoder it serves runs there); the f32
|
||||
@@ -1209,7 +1361,7 @@ extern "C" int r4d_rad_conv1d_f32(const RadArgs* a, RadStream s) {
|
||||
if (r4d_geti_or(a, "stride", 1) != 1 || r4d_geti_or(a, "pad", -1) >= 0)
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
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);
|
||||
@@ -1217,18 +1369,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;
|
||||
@@ -1237,7 +1390,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);
|
||||
@@ -1325,9 +1479,11 @@ extern "C" int r4d_rad_cfg_topk_sample(const RadArgs* a, RadStream s) {
|
||||
const long long sup_lo = r4d_geti_or(a, "sup_lo", 0), sup_hi = r4d_geti_or(a, "sup_hi", 0);
|
||||
const double cfg = r4d_getf(a, "cfg", 1.0), temp = r4d_getf(a, "temp", 1.0);
|
||||
const double pen = r4d_getf(a, "penalty", 1.0);
|
||||
/* As f32, as libref and libavx read it, so all three cut at the same value. */
|
||||
const float top_p = (float)r4d_getf(a, "top_p", 1.0);
|
||||
if (V <= 0 || K <= 0 || K > kMaxK || K > V || rep < 1 || rep > 2 || ck < 0 || ck > V ||
|
||||
!(temp > 0) || V > (1 << 16) * kT || sup_lo < 0 || sup_hi < sup_lo || sup_hi > V ||
|
||||
!(pen > 0) || (!hist != !lane))
|
||||
!(pen > 0) || (!hist != !lane) || !(top_p >= 0.f))
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
long long rl = 0;
|
||||
const long long ll = r4d_rows_pitch(lg, V, &rl);
|
||||
@@ -1343,7 +1499,30 @@ extern "C" int r4d_rad_cfg_topk_sample(const RadArgs* a, RadStream s) {
|
||||
cfgs ? (const float*)cfgs->data : nullptr,
|
||||
lane ? (const int*)lane->data : nullptr, hist ? (int*)hist->data : nullptr,
|
||||
hist ? (int)(r4d_numel(hist) / V) : 0, (int)sup_lo, (int)sup_hi,
|
||||
(float)pen);
|
||||
(float)pen, top_p);
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_ras_pick(const RadArgs* a, RadStream s) {
|
||||
const RadTensor *first = r4d_opt(a, 0), *alt = r4d_opt(a, 1), *lane = r4d_opt(a, 2);
|
||||
const RadTensor *hist = r4d_opt(a, 3), *ids = r4d_opt(a, 4);
|
||||
if (!first || !alt || !lane || !hist || !ids) return R4D_NO(RAD_E_INVAL);
|
||||
if (!is_i32(first) || !is_i32(alt) || !is_i32(lane) || !is_i32(hist) || !is_i32(ids))
|
||||
return R4D_NO(RAD_E_DTYPE);
|
||||
if (!r4d_contig(first) || !r4d_contig(alt) || !r4d_contig(lane) || !r4d_contig(hist) ||
|
||||
!r4d_contig(ids))
|
||||
return R4D_NO(RAD_E_STRIDE);
|
||||
const long long W = r4d_geti_or(a, "win", 0), lo = r4d_geti_or(a, "lo", 0);
|
||||
const long long hi = r4d_geti_or(a, "hi", 0), rep = r4d_geti_or(a, "rep", 1);
|
||||
const long long clear = r4d_geti_or(a, "clear", 0);
|
||||
const long long n = r4d_numel(lane);
|
||||
if (W <= 0 || lo < 0 || hi < lo || rep < 1 || clear < 0 || clear > 1 || n <= 0 ||
|
||||
r4d_numel(first) < rep * n || r4d_numel(alt) < rep * n || r4d_numel(ids) < rep * n)
|
||||
return R4D_NO(RAD_E_SHAPE);
|
||||
hipLaunchKernelGGL(ras_pick_kernel, dim3(blocks_for(n)), dim3(kT), 0, (hipStream_t)s,
|
||||
(const int*)first->data, (const int*)alt->data, (const int*)lane->data,
|
||||
(int*)hist->data, (int*)ids->data, (int)n, (int)W, (int)lo, (int)hi,
|
||||
(int)rep, (int)clear, (long long)r4d_numel(hist));
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
@@ -1356,9 +1535,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);
|
||||
@@ -1371,7 +1551,38 @@ 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());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -1663,6 +1874,18 @@ extern "C" int r4d_rad_int_affine(const RadArgs* a, RadStream s) {
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_sub_i32(const RadArgs* a, RadStream s) {
|
||||
const RadTensor *x = r4d_opt(a, 0), *sc = r4d_opt(a, 1), *y = r4d_opt(a, 2);
|
||||
if (!x || !sc || !y) return R4D_NO(RAD_E_INVAL);
|
||||
if (!is_i32(x) || !is_i32(sc) || !is_i32(y)) return R4D_NO(RAD_E_DTYPE);
|
||||
if (!r4d_contig(x) || !r4d_contig(y)) return R4D_NO(RAD_E_STRIDE);
|
||||
const long long n = r4d_numel(y);
|
||||
if (n <= 0 || r4d_numel(x) < n || r4d_numel(sc) < 1) return R4D_NO(RAD_E_SHAPE);
|
||||
hipLaunchKernelGGL(sub_i32_kernel, dim3(blocks_for(n)), dim3(kT), 0, (hipStream_t)s,
|
||||
(const int*)x->data, (const int*)sc->data, (int*)y->data, (int)n);
|
||||
return st_of(hipGetLastError());
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_cu_repeat(const RadArgs* a, RadStream s) {
|
||||
const RadTensor *len = r4d_opt(a, 0), *y = r4d_opt(a, 1);
|
||||
if (!len || !y) return R4D_NO(RAD_E_INVAL);
|
||||
|
||||
Reference in new issue
Block a user