Archived
radiance 1.12.0
New models: - Fish Audio drama-3: speech with speaker tags and inline direction, voices cloned from reference clips (the `references` field of /v1/audio/speech and the streaming session), two cards - Gemma 4 E2B/E4B (KV-shared layers, per-layer embeddings, clipped vision linears), with NVIDIA-Nemotron-3.5-Bolt-E4B in bf16 with DFlash and vision on one card, and E4B recipes Speed: - MXFP4 GEMMs for one to 64 rows and a tiled prefill GEMM at TN=4 from 1024 rows: the 27B MXFP4 with DFlash2 decodes about 1% faster at c1, 5% at c4 and 14% at c16 - The fp8 and int8 LM heads read their weight once for 65 to 128 rows - A prefill step of one sequence runs in two pieces on a second lane at two ranks (qwen35_fp8, gemma3): a 45K-token prompt on the 27B fp8 in 15.1 s instead of 16.2 s - Gemma 3's fused all-reduce junctions also under a lossy wire - --draft-short-from N: a block drafter runs its shorter block from N decoding sequences on (off by default; with 8 on the 27B MXFP4: c8 +12%, c16 +18%, c64 +32%, c1 unchanged) - --tp-attn-split rows: GLM-5.3-Flash's MLA attention split by rows over the ranks, 18 to 21% faster prefill; lossy, `heads` stays the default - --draft-coupling on: Qwen3.8-Flash-Next's MTP drafter samples its proposals from its reranked top-8 and the target's draw is coupled to it; pays at high temperatures (c1 at T 1.0: 155 -> 178 tok/s), costs at low ones; off by default Features: - Edited prompts on windowed models (Gemma 4) reuse their prefix: periodic window snapshots, and snapshot-backed prefix KV kept across recomputes (an edited tail on Gemma 4 31B: 96 s -> 2.6 s) - DELETE /sessions/<label> and DELETE /sessions?id=<id> forget one stored conversation - --state-cache-dtype f16: the delta-net state cache at half the memory (qwen35 plugins; lossy) Fixes: - A browser's CORS preflight allows DELETE Changes: Plugin ABI 27: out-of-tree plugins must be rebuilt. The environment switches the PRs added are flags now (--tp-attn-split, --draft-coupling, --state-cache-dtype).
This commit is contained in:
10 parents
4bca30369b
b1f4fccc52
d90e19dc15
3aded1bbf8
d912c694f3
824e238996
58142a6638
9322ea0b92
e9d03362f0
4d68a01a08
commit
5a9ce4912b
137 files changed
+12959
-1126
No files matched your search
+255
-54
@@ -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;
|
||||
@@ -622,6 +627,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(
|
||||
@@ -629,9 +639,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];
|
||||
@@ -721,8 +732,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),
|
||||
@@ -761,15 +793,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;
|
||||
@@ -778,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).
|
||||
@@ -820,6 +933,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) {
|
||||
@@ -1079,24 +1218,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;
|
||||
@@ -1109,8 +1250,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>),
|
||||
@@ -1118,15 +1259,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());
|
||||
}
|
||||
@@ -1190,11 +1331,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);
|
||||
@@ -1202,18 +1344,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;
|
||||
@@ -1222,7 +1365,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);
|
||||
@@ -1310,9 +1454,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);
|
||||
@@ -1328,7 +1474,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());
|
||||
}
|
||||
|
||||
@@ -1341,9 +1510,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);
|
||||
@@ -1356,7 +1526,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());
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user