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
+40
-3
@@ -71,7 +71,6 @@ The table lists a selection; `r4d_rows.cpp` and `r4d.h` hold the full set.
|
||||
| `gdn_gated_norm_had_quant_i8` | the GDN gated norm with the rotation and quantiser fused onto it |
|
||||
| `qk_norm_rope_gate` | q/k RMS norm, rope and the attention gate in one kernel |
|
||||
| `ar_oneshot_{3,4,8}rank_exact`, `ar_twoshot_{3,4,8}rank_exact`, `ar_twoshot_{3,4}rank_ti8`, `ar_gather_3rank` | the wider-world collectives (TP3/TP4/TP8), exact and int8-wire |
|
||||
| `ar_twoshot_{3,4,8}rank_exact_lane`, `ar_twoshot_{3,4}rank_ti8_lane` | the same two-shots at `overlap` 1, beside compute: the wide rows' entry points, the same bits; at four ranks a narrow launch (4 blocks of 1024 threads), at three and eight the wide launch unless `R4D_AR_LANE_3_8=1` gives them the narrow one (unmeasured on such cards) |
|
||||
| `p2p_copy2d`, `p2p_copy2d_kernel`, `p2p_fence` | strided peer device-to-device copy, through the copy engines or a kernel |
|
||||
|
||||
## Constraints as data
|
||||
@@ -189,14 +188,52 @@ this kernel: the 128-wide slab always, and a split only while every split keeps
|
||||
48-block grid or, at K ≥ 8192, regardless, up to ks·M = 1024 split rows. At 80 / 96 / 128 rows:
|
||||
17408 × 5120 161 / 187 / 215 → 161 / 175 / 197 µs, 5120 × 3072 71 / 79 / 67 → 55 / 60 / 69, 21504 ×
|
||||
5376 192 / 207 / 228 → 187 / 200 / 217. A different split sums a row in a different order, so at
|
||||
65..128 rows the re-tuned rule moves output bits within the selftest's bound: the one change in
|
||||
this family that is not bit for bit. Up to 64 rows the rule is as it was.
|
||||
65..128 rows the re-tuned rule moves output bits within the selftest's bound. Up to 64 rows the
|
||||
decode kernel's bits are as they were.
|
||||
|
||||
**Bits against the rows these replace.** `gemv` and `gemv_wide` sum a row in their own order (the
|
||||
K split over eight waves and 128-wide chunks), not the decode kernel's split-K order, so at 1..64
|
||||
rows their outputs differ from the decode row's in the last bf16 place on a few elements in ten
|
||||
thousand (within the selftest's bound). A greedy text can therefore differ from one produced
|
||||
with the decode row at those rows. Within the family a row's bits do not depend on M, as above.
|
||||
|
||||
**Tunables.** `gemv` and `gemv_wide` have none, so a `RADIANCE_TUNE` pin on `gemm_nt_q` passes
|
||||
them by. `decode_shortk` shares the decode row's axes (`ks`, `bk`, `nt`; 0 the rule), so a pin of
|
||||
those reaches both. A pin applies to every row serving the op, and an axis a serving row lacks
|
||||
stops startup by name.
|
||||
|
||||
## The OCP-MXFP4 tiled GEMM (prefill)
|
||||
|
||||
**`gemm_mxfp4a8_tiled`** (`gemm_nt_q` at `mxfp4a8`, the rows past the decode band). TN = 4 (256 ×
|
||||
128 blocks) from 1024 rows (it was 2048), unless its blocks would number under 128: at the
|
||||
crossover Qwen3.8-27B's gate_up -2 %, down -3 %, TranslateGemma 27B's gate_up -9 %, its down +1 %,
|
||||
and at 1536 rows all four better; TranslateGemma's 1024-row chunks run there. The weight words a
|
||||
thread stages are loaded into registers a slab ahead, right after the staging barrier, so their
|
||||
DRAM round trip runs under the slab's WMMAs (+1 VGPR at TN = 4, no spills). At 1024 rows Qwen
|
||||
gate_up 804 → 768 µs, down 443 → 417, ssm_in 396 → 384, ssm_out 249 → 231, q|gate 306 → 318;
|
||||
TranslateGemma gate_up 1063 → 1029, down 651 → 617; at 2048 rows 0..-4 %; a 41k-token Qwen3.8-27B
|
||||
prefill on two cards 8.57 → 8.28 s. A thin last row of blocks (1..160 rows, M under 1024) runs a
|
||||
second kernel whose blocks of that row skip the WMMAs of fragments past M, where the plain kernel
|
||||
computes them on clamped copies (half the only block's math at 129 rows); every other M runs the
|
||||
plain kernel, whose code is instruction for instruction the single kernel's from before the split
|
||||
(a guard inside the one kernel cost full blocks 1..15 %). Same-session A/B over 12 shapes: M
|
||||
129..160 -8..-20 %, 320..384 -2..-16 % (Qwen gate_up at 320 rows 402 → 339 µs, TranslateGemma's 513
|
||||
→ 441). Each of the three keeps the fold, the WMMAs and their order per output byte. Times are one
|
||||
rank's weight at TP2 on an R9700, the row alone, clocks warmed; over 20 shapes, M 129..2048: median
|
||||
-7.4 % over 140 cells, none slower beyond run-to-run noise.
|
||||
|
||||
`tn` (0 the rule, 2, 4) pins the TN choice through `RADIANCE_TUNE`.
|
||||
|
||||
## The fp8 and int8 heads past 64 rows
|
||||
|
||||
**`logits_gemm_fp8`, `logits_gemm_i8`** took passes of 64 rows and read the head again for each.
|
||||
With `MH` = 2 a workgroup's eight waves split into two M halves of four 16-row tiles over 64 weight
|
||||
rows that share one staged tile, so a pass takes 128 rows and reads the head once. Qwen3.8's
|
||||
per-rank head (124160 × 5120) at 128 rows 3059 → 1984 µs, TranslateGemma's (131072 × 5376) 3242 →
|
||||
2036; 65..128 rows -33..-39 %, 160..256 -26..-37 %, 64 and fewer unchanged; about 1 ms a step at 16
|
||||
streams with speculation. Same scale blocks, WMMAs and K order per output: the same bytes (the
|
||||
numeric cases at 65..150 rows for the fp8 head, 100 for the int8 head).
|
||||
|
||||
## Notes on the kernels
|
||||
|
||||
Three properties of the architecture are assumed throughout and not restated per kernel:
|
||||
|
||||
@@ -689,6 +689,10 @@ void r4d_hc_read_pin_prefill(int form);
|
||||
// wherever the cache rows are aligned to them), 1 the element-wise load the rule falls back to.
|
||||
// Bit-identical; r4d_selftest compares them and --perfmla times both.
|
||||
void r4d_mla_matrix_pin_form(int form);
|
||||
// Test hook: pins the MLA rows row's form. 0 is the launcher's rule (64 heads a workgroup, vector
|
||||
// key loads where aligned; the matrix row's kernel under 32 rows), 1 the element-wise key load
|
||||
// (bit-identical to 3), 2 32 heads a workgroup, 3 the rows kernel of the rule at any row count.
|
||||
void r4d_mla_rows_pin_form(int form);
|
||||
// Test hook: records the function each launch dispatches into `fns[0..cap)`, in order, until the
|
||||
// next call, and returns how many launches the previous recording saw (which may exceed its cap).
|
||||
// Null `fns` stops recording. r4d_selftest uses it to show that a pinned form dispatches a kernel
|
||||
|
||||
+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());
|
||||
}
|
||||
|
||||
|
||||
@@ -481,13 +481,15 @@ constexpr uint32_t lm_waves(uint32_t mt, uint32_t fkb) {
|
||||
}
|
||||
|
||||
// The body; lm_wmma_kernel and its chained form (RadKernelChainFn) wrap it.
|
||||
template <uint32_t MT, uint32_t FKB, bool I8>
|
||||
template <uint32_t MT, uint32_t FKB, bool I8, uint32_t MH = 1>
|
||||
__device__ __forceinline__ void lm_wmma_body(
|
||||
const uint8_t* __restrict__ W, const uint16_t* __restrict__ A, uint32_t a_ld,
|
||||
float* __restrict__ C, uint32_t c_ld,
|
||||
uint32_t rows, uint32_t cols, uint32_t w_ld, uint32_t m_real) {
|
||||
constexpr uint32_t kFK = FKB * kBlock; /* K a staging round */
|
||||
constexpr uint32_t kRows = kDenseWaves * 16u; /* weight rows a workgroup covers */
|
||||
/* Weight rows a workgroup covers: 16 a wave, the waves split into MH halves along M that share
|
||||
* the staged tile (MH = 2: 64 rows, each half four 16-row activation tiles). */
|
||||
constexpr uint32_t kRows = kDenseWaves * 16u / MH;
|
||||
constexpr uint32_t kPitch = lds_pitch<kFK>(); /* BYTES a staged weight row */
|
||||
constexpr uint32_t kWUnits = kRows * (kFK / 16u); /* 16-byte staging units, weight */
|
||||
constexpr bool kPre = MT <= 3u; /* activation a K step ahead, below */
|
||||
@@ -497,12 +499,14 @@ __device__ __forceinline__ void lm_wmma_body(
|
||||
const uint32_t tid = (uint32_t)threadIdx.x;
|
||||
const uint32_t lane = tid & (kWave - 1u);
|
||||
const uint32_t wv = tid / kWave;
|
||||
const uint32_t wn = wv % (kDenseWaves / MH); /* this wave's 16 weight rows */
|
||||
const uint32_t mo = (wv / (kDenseWaves / MH)) * 16u * MT; /* its M half's first row */
|
||||
const uint32_t c = lane & 15u; /* this lane's weight row inside its 16-row tile */
|
||||
const uint32_t h = lane >> 4; /* which half of the K step */
|
||||
|
||||
for (uint32_t base = (uint32_t)blockIdx.x * kRows; base < rows; base += gridDim.x * kRows) {
|
||||
/* Clamped, not skipped: a tail workgroup recomputes the last row; the store drops it. */
|
||||
const uint32_t n = base + wv * 16u + c;
|
||||
const uint32_t n = base + wn * 16u + c;
|
||||
const uint32_t nc = (n < rows) ? n : (rows - 1u);
|
||||
const uint16_t* scl = (const uint16_t*)(W + (size_t)nc * w_ld + cols);
|
||||
|
||||
@@ -519,7 +523,7 @@ __device__ __forceinline__ void lm_wmma_body(
|
||||
auto aload = [&](uint32_t kf) {
|
||||
#pragma unroll
|
||||
for (uint32_t t = 0; t < MT; ++t) {
|
||||
const uint32_t m = t * 16u + c, mr = (m < m_real) ? m : (m_real - 1u);
|
||||
const uint32_t m = mo + t * 16u + c, mr = (m < m_real) ? m : (m_real - 1u);
|
||||
an[t] = *(const lm_s8v*)(A + (size_t)mr * a_ld + kt + kf + h * 8u);
|
||||
}
|
||||
};
|
||||
@@ -563,7 +567,7 @@ __device__ __forceinline__ void lm_wmma_body(
|
||||
for (uint32_t t = 0; t < MT; ++t) ac[t] = an[t];
|
||||
aload(kf + 16u < kFK ? kf + 16u : kf);
|
||||
}
|
||||
const i2v raw = frag<kFK>(s_w, wv * 16u, kf, lane);
|
||||
const i2v raw = frag<kFK>(s_w, wn * 16u, kf, lane);
|
||||
const lm_s8v bf =
|
||||
e4m3x8_to_bf16<I8>((uint32_t)raw[0], (uint32_t)raw[1]);
|
||||
/* The activation load is unconditional: a padding row re-reads the last real row and a select
|
||||
@@ -571,7 +575,7 @@ __device__ __forceinline__ void lm_wmma_body(
|
||||
* per K step). */
|
||||
#pragma unroll
|
||||
for (uint32_t t = 0; t < MT; ++t) {
|
||||
const uint32_t m = t * 16u + c;
|
||||
const uint32_t m = mo + t * 16u + c;
|
||||
[[maybe_unused]] const uint32_t mr = (m < m_real) ? m : (m_real - 1u);
|
||||
lm_s8v af;
|
||||
if constexpr (kPre) af = ac[t];
|
||||
@@ -594,7 +598,7 @@ __device__ __forceinline__ void lm_wmma_body(
|
||||
for (uint32_t t = 0; t < MT; ++t)
|
||||
#pragma unroll
|
||||
for (uint32_t j = 0; j < 8; ++j) {
|
||||
const uint32_t m = t * 16u + h * 8u + j;
|
||||
const uint32_t m = mo + t * 16u + h * 8u + j;
|
||||
if (m < m_real) C[(size_t)m * c_ld + n] = acc[t][j];
|
||||
}
|
||||
}
|
||||
@@ -603,16 +607,16 @@ __device__ __forceinline__ void lm_wmma_body(
|
||||
#define R4D_LMW_PARAMS const uint8_t* __restrict__ W, const uint16_t* __restrict__ A, \
|
||||
uint32_t a_ld, float* __restrict__ C, uint32_t c_ld, uint32_t rows, uint32_t cols, \
|
||||
uint32_t w_ld, uint32_t m_real
|
||||
template <uint32_t MT, uint32_t FKB, bool I8 = false>
|
||||
template <uint32_t MT, uint32_t FKB, bool I8 = false, uint32_t MH = 1>
|
||||
__global__ __launch_bounds__(kDenseThreads) R4D_WAVES(lm_waves(MT, FKB)) void lm_wmma_kernel(
|
||||
R4D_LMW_PARAMS) {
|
||||
lm_wmma_body<MT, FKB, I8>(W, A, a_ld, C, c_ld, rows, cols, w_ld, m_real);
|
||||
lm_wmma_body<MT, FKB, I8, MH>(W, A, a_ld, C, c_ld, rows, cols, w_ld, m_real);
|
||||
}
|
||||
template <uint32_t MT, uint32_t FKB, bool I8 = false>
|
||||
template <uint32_t MT, uint32_t FKB, bool I8 = false, uint32_t MH = 1>
|
||||
__global__ __launch_bounds__(kDenseThreads) R4D_WAVES(lm_waves(MT, FKB)) void r4dchain_lm_wmma(
|
||||
R4D_LMW_PARAMS) {
|
||||
r4d_chain_wait();
|
||||
lm_wmma_body<MT, FKB, I8>(W, A, a_ld, C, c_ld, rows, cols, w_ld, m_real);
|
||||
lm_wmma_body<MT, FKB, I8, MH>(W, A, a_ld, C, c_ld, rows, cols, w_ld, m_real);
|
||||
r4d_chain_done();
|
||||
}
|
||||
#undef R4D_LMW_PARAMS
|
||||
@@ -688,8 +692,8 @@ extern "C" int r4d_gemm_fp8a16_nt_mt(long a, long wq, long ws, long c,
|
||||
// wq [N][lm_row_bytes(K)] E4M3 codes, nkb bf16 scales, pad; `w_ld` BYTES a row (0 = that)
|
||||
// c [M][N] f32, `c_ld` elements a row (0 = N)
|
||||
//
|
||||
// The WMMA path takes M > 2 in chunks of 64 rows, each re-reading the weight; the matvec takes
|
||||
// M <= 2.
|
||||
// The WMMA path takes M > 2 in passes of up to 128 rows, each reading the weight once (past 64
|
||||
// rows in two M halves over one staged tile); the matvec takes M <= 2.
|
||||
//
|
||||
// This row must serve every M: all bands of one op read the same stored bytes, so a band falling
|
||||
// to another kernel (e.g. the bf16 projection) would misread the E4M3 codes.
|
||||
@@ -730,9 +734,31 @@ static int logits_gemm_8(long a, long wq, long c, int M, int K, int N, int a_ld,
|
||||
/* WMMA from M = 3, the matvec at or below. The tile's rate is nearly flat in M; the matvec is
|
||||
* marginally ahead at M = 2 and clearly at M = 1. Both read the same stored bytes. */
|
||||
if (M > 2) {
|
||||
/* Sixty-four rows per pass: four WMMA accumulator tiles (32 registers). */
|
||||
for (int m0 = 0; m0 < M; m0 += 64) {
|
||||
const int Mc = (M - m0) < 64 ? (M - m0) : 64;
|
||||
/* Passes of up to 128 rows. Up to 64, one tile of four WMMA accumulator tiles (32
|
||||
* registers) a wave; past 64, the same four a wave in two M halves over one staged weight
|
||||
* tile (MH = 2), so the head is read once a pass. */
|
||||
for (int m0 = 0; m0 < M; m0 += 128) {
|
||||
const int Mc = (M - m0) < 128 ? (M - m0) : 128;
|
||||
if (Mc > 64) {
|
||||
uint32_t fkb = lm_fkb_rule(4u);
|
||||
while (fkb > 1u && (cols % (fkb * kBlock)) != 0u) fkb >>= 1;
|
||||
const uint32_t per = kDenseWaves * 16u / 2u;
|
||||
const uint32_t blocks = std::min<uint32_t>(4096u, (rows + per - 1u) / per);
|
||||
const uint16_t* ac = (const uint16_t*)a + (size_t)m0 * a_ld;
|
||||
float* cc = (float*)c + (size_t)m0 * c_ld;
|
||||
if (fkb == 2u)
|
||||
hipLaunchKernelGGL((chain ? r4dchain_lm_wmma<4, 2, I8, 2> : lm_wmma_kernel<4, 2, I8, 2>),
|
||||
dim3(blocks), dim3(kDenseThreads), 0,
|
||||
(hipStream_t)stream, (const uint8_t*)wq, ac, (uint32_t)a_ld, cc,
|
||||
(uint32_t)c_ld, rows, cols, (uint32_t)w_ld, (uint32_t)Mc);
|
||||
else
|
||||
hipLaunchKernelGGL((chain ? r4dchain_lm_wmma<4, 1, I8, 2> : lm_wmma_kernel<4, 1, I8, 2>),
|
||||
dim3(blocks), dim3(kDenseThreads), 0,
|
||||
(hipStream_t)stream, (const uint8_t*)wq, ac, (uint32_t)a_ld, cc,
|
||||
(uint32_t)c_ld, rows, cols, (uint32_t)w_ld, (uint32_t)Mc);
|
||||
if (hipGetLastError() != hipSuccess) return -6;
|
||||
continue;
|
||||
}
|
||||
const uint32_t mt = (uint32_t)((Mc + 15) / 16);
|
||||
/* FKB = 4 would need kRows * (512 + 8) = 66560 bytes of LDS, over the 64 KiB limit. */
|
||||
uint32_t fkb = lm_fkb_rule(mt);
|
||||
|
||||
@@ -17,23 +17,19 @@
|
||||
// split the K slabs; each writes an fp32 partial [DKS][M][N], and the last to arrive (an
|
||||
// agent-scope counter per column block) sums them, with no second launch.
|
||||
//
|
||||
// Split, slab and load hint follow a rule tuned per cell on the 27B's per-rank shapes at
|
||||
// M = 8..128 (tunables `ks`, `bk`, `nt`; 0 selects the rule):
|
||||
// - split: the smallest power of two whose grid reaches 48 blocks at M <= 16, 56 at M <= 96 and
|
||||
// 40 above; for K >= 8192 keep doubling while a split has >= 24 128-wide slabs; never past
|
||||
// Split, slab and load hint follow a rule tuned on the 27B's and TranslateGemma's per-rank shapes
|
||||
// (tunables `ks`, `bk`, `nt`; 0 selects the rule):
|
||||
// - split up to 64 rows: the smallest power of two whose grid reaches 48 blocks at M <= 16 and
|
||||
// 56 above; for K >= 8192 keep doubling while a split has >= 24 128-wide slabs; never past
|
||||
// ks*M = 512, where partial traffic outweighs the gain.
|
||||
// - slab: 128, or 64 where LDS per block would cost a wide or deeply split grid its resident
|
||||
// blocks (see mxd_plan). A K not a multiple of 128 takes 64 (and a 2-way split widens to 4).
|
||||
// The rule was tuned when the weight was staged through LDS too, (16*DTM + 128) * (DBK+8)
|
||||
// bytes a block; the multi-fragment kernel now takes 2 * 16*DTM * (DBK+8).
|
||||
// - past 64 rows, re-tuned for the register kernel (r4d_selftest --perfmxd, warmed, every split
|
||||
// 1..8 at both slabs on the 27B's and TranslateGemma's per-rank shapes at 96 and 128 rows): the
|
||||
// 128-wide slab always (it never lost to 64), and a split only while every split keeps 16 slabs,
|
||||
// towards a 48-block grid or, at K >= 8192, regardless, up to ks*M = 1024 split rows. Against the
|
||||
// old rule at 96 / 128 rows: 5120 x 8704 116 / 160 -> 116 / 149 us (a split of 4), 6144 x 5120
|
||||
// 97 / 105 -> 87 / 109, 5120 x 3072 79 / 67 -> 60 / 69 (none), 17408 x 5120 187 / 215 -> 175 / 197
|
||||
// (the wide slab). A different split changes a split row's summation order, so bits move here.
|
||||
// - split past 64 rows: double only while every split keeps 16 128-wide slabs, towards a
|
||||
// 48-block grid or, at K >= 8192, regardless; never past ks*M = 1024.
|
||||
// - slab: 128, or 64 where LDS per block would cost a wide grid its resident blocks (see
|
||||
// mxd_plan); past 64 rows always 128. A K not a multiple of 128 takes 64 (and a 2-way split
|
||||
// widens to 4).
|
||||
// - non-temporal weight loads for weights of 4 MiB or more.
|
||||
// The split count sets a row's summation order, so a change to the rule moves the bits of the rows
|
||||
// it splits differently.
|
||||
//
|
||||
// No state is keyed on the stream, since a recorded pass may replay on another stream. The split
|
||||
// partial is the op's scratch (sized by the scratch hook, read+write, so overlapping ops get
|
||||
@@ -89,8 +85,8 @@ static __device__ __forceinline__ T mxd_ld(const T* p) {
|
||||
// The (row, 128) scale grid: thread t < ROWS owns row t, loads the next boundary's two scales a
|
||||
// boundary early and writes s_prev / s_next to LDS for the boundary's slab (the one-fragment kernel
|
||||
// at that slab, published by its staging barrier; the multi-fragment kernel a slab early into the
|
||||
// other half of its double buffer, published by the slab's closing barrier). A zero next block keeps the carried scale; after a zero block the carried scale is
|
||||
// looked up from the grid.
|
||||
// other half of its double buffer, published by the slab's closing barrier). A zero next block
|
||||
// keeps the carried scale; after a zero block the carried scale is looked up from the grid.
|
||||
static __device__ __forceinline__ float mxd_ratio(float ns, float np, const float* __restrict__ row,
|
||||
int kb) {
|
||||
return ns > 0.f ? (np > 0.f ? np : r4d_mxfp4_carried(row, kb - 2)) / ns : 1.f;
|
||||
@@ -206,15 +202,12 @@ constexpr int mxd_waves(int dbk, int dtm, bool ablk) {
|
||||
: 6;
|
||||
}
|
||||
|
||||
// The multi-fragment form (M > 16). A fragment-order weight word is the WMMA fragment's own register
|
||||
// (one dword a lane a k-step), so a wave loads its tile's words for a slab straight to registers and
|
||||
// folds them there, with no barrier between a wave and its weight; only the activation, shared by the
|
||||
// eight tiles, goes through LDS, double-buffered so a slab takes one barrier. The next slab's
|
||||
// activation and weight are loaded while this slab computes. The arithmetic -- the fold, the k-step
|
||||
// and slab order, the scale carry and the epilogue -- is the one the kernel staging the weight through
|
||||
// LDS had, so the bits are its own (checked byte for byte when it was replaced). r4d_selftest
|
||||
// --perfmxd, clocks warmed, LDS-staged -> registers: 17408 x 5120 at 64 rows 149 -> 129 us, at 96
|
||||
// 205 -> 159, at 128 245 -> 214; 5120 x 8704 at 128 169 -> 139; 6144 x 5120 at 128 121 -> 92.
|
||||
// The multi-fragment form (M > 16). A fragment-order weight word is the WMMA fragment's own
|
||||
// register (one dword a lane a k-step), so a wave loads its tile's words for a slab straight to
|
||||
// registers and folds them there, with no barrier between a wave and its weight. Only the
|
||||
// activation, shared by the eight tiles, goes through LDS, double-buffered so a slab takes one
|
||||
// barrier. The next slab's activation and weight are loaded while this slab computes. The fold,
|
||||
// k-step and slab order, scale carry and epilogue are the one-fragment form's (below).
|
||||
template <int DBK, int DKS, int DTM, bool NT, bool ABLK>
|
||||
__global__ __launch_bounds__(R4D_MXD_DWN * 32) R4D_WAVES(mxd_waves(DBK, DTM, ABLK))
|
||||
void r4d_gemm_mxfp4a8_decode_kernel(
|
||||
@@ -345,9 +338,10 @@ void r4d_gemm_mxfp4a8_decode_kernel(
|
||||
}
|
||||
|
||||
// The one-fragment form (M <= 16), every single-sequence verify step. Same tile, fold and split as
|
||||
// above, with the weight staged through LDS, plus: the next slab's A, codes and exponents are loaded
|
||||
// into registers while this slab computes, keeping loads in flight across barriers; a thread takes four adjacent slots (one 16 B
|
||||
// load); and each row's reference exponent is read once. Bit-identical to the kernel above.
|
||||
// above, with the weight staged through LDS, plus: the next slab's A, codes and exponents are
|
||||
// loaded into registers while this slab computes, keeping loads in flight across barriers; a thread
|
||||
// takes four adjacent slots (one 16 B load); and each row's reference exponent is read once.
|
||||
// Bit-identical to the kernel above.
|
||||
template <int DBK, int DKS, bool NT, bool ABLK>
|
||||
__global__ __launch_bounds__(R4D_MXD_DWN * 32) R4D_WAVES(DBK == 64 ? 16 : 12)
|
||||
void r4d_gemm_mxfp4a8_decode1_kernel(
|
||||
@@ -527,22 +521,22 @@ MxdPlan mxd_plan(int M, int K, int N, int KS, int BK, int NT) {
|
||||
MxdPlan p;
|
||||
int ks = 1;
|
||||
if (tm > 4) {
|
||||
// Past 64 rows (the register kernel; see the header): split only while every split keeps 16
|
||||
// 128-wide slabs, towards a 48-block grid, or at a deep K regardless.
|
||||
// Past 64 rows (see the header): split only while every split keeps 16 128-wide slabs, towards
|
||||
// a 48-block grid, or at a deep K regardless.
|
||||
const int slabs = K / 128;
|
||||
while (ks < R4D_MXD_MAX_KS && (nblk * ks < 48 || K >= 8192) && slabs / (2 * ks) >= 16 &&
|
||||
2 * ks * M <= 1024)
|
||||
ks <<= 1;
|
||||
} else {
|
||||
const int fill = M <= 16 ? 48 : (M <= 96 ? 56 : 40);
|
||||
const int fill = M <= 16 ? 48 : 56;
|
||||
while (ks < R4D_MXD_MAX_KS && nblk * ks < fill && 2 * ks * M <= 512) ks <<= 1;
|
||||
if (K >= 8192) // a deep K: a split of 24 or more slabs still waits on them
|
||||
while (ks < R4D_MXD_MAX_KS && (K / 128) / ks >= 24 && 2 * ks * M <= 512) ks <<= 1;
|
||||
}
|
||||
p.ks = KS > 0 ? KS : ks;
|
||||
// 64-wide slabs where LDS per block would cost resident blocks: DTM 4 unsplit on 110+ column
|
||||
// blocks, by the split the launch runs (a pin or a tuned point included), as before the register
|
||||
// kernel; past 64 rows the 128-wide slab always.
|
||||
// blocks, by the split the launch runs (a pin or a tuned point included); past 64 rows the
|
||||
// 128-wide slab always.
|
||||
const bool slab64 = tm == 4 && p.ks == 1 && nblk >= 110;
|
||||
p.bk = BK > 0 ? BK : (slab64 ? 64 : 128);
|
||||
p.nt = NT == 1 || (NT == 0 && (long)N * K / 2 >= (4l << 20));
|
||||
|
||||
@@ -13,15 +13,15 @@
|
||||
// Two forms, chosen from K alone so that a row's output bits do not depend on how many rows the
|
||||
// step carries (a verify step's first row is bit-identical to a plain decode of it):
|
||||
//
|
||||
// - K a multiple of 128: the WMMA form. One 16-column tile a block (two from 25 rows where N >= 4096,
|
||||
// K >= 3072 and the tile count is even), eight waves on
|
||||
// its K in chunks of one 128 block, and up to four 16-row tiles (M <= 64), each weight word
|
||||
// multiplied with every row tile's fragment once it is loaded. A lane loads its own B-fragment word of each k-step straight
|
||||
// from the fragment-order weight (the order is the fragment's), the rows sit in the A fragments
|
||||
// (rows >= M zero), and v_wmma_f32_16x16x16_fp8_fp8 multiplies. The per-(row, 128) activation scale is
|
||||
// applied to each chunk's partial. At M = 1 the WMMA does sixteen times the work it needs, which
|
||||
// costs nothing next to the weight stream; from M = 2 up it is what keeps the kernel on the
|
||||
// stream (the dot form pays a dot per row).
|
||||
// - K a multiple of 128: the WMMA form. One 16-column tile a block (two from 25 rows where
|
||||
// N >= 4096, K >= 3072 and the tile count is even), eight waves on its K in chunks of one 128
|
||||
// block, and up to four 16-row tiles (M <= 64), each weight word multiplied with every row
|
||||
// tile's fragment once it is loaded. A lane loads its own B-fragment word of each k-step
|
||||
// straight from the fragment-order weight (the order is the fragment's), the rows sit in the A
|
||||
// fragments (rows >= M zero), and v_wmma_f32_16x16x16_fp8_fp8 multiplies. The per-(row, 128)
|
||||
// activation scale is applied to each chunk's partial. At M = 1 the WMMA does sixteen times the
|
||||
// work it needs, which costs nothing next to the weight stream; from M = 2 up it is what keeps
|
||||
// the kernel on the stream (the dot form pays a dot per row).
|
||||
// - Otherwise (K = 64 mod 128): the dot form. A lane loads 16 bytes -- four columns' eight codes of
|
||||
// one k-step -- so a wave reads four k-steps, 512 contiguous bytes, from each of its TN tiles, and
|
||||
// multiplies with v_dot4_f32_fp8_fp8; the activation a lane reads (eight k of each row) serves its
|
||||
@@ -160,16 +160,11 @@ void r4d_gemm_mxfp4a8_gemv_kernel(
|
||||
// weight streams once whatever M is, and a row's sum is its tile's chain alone -- the same at every RT
|
||||
// and TN.
|
||||
//
|
||||
// TN: the activation is read per block, so one column tile a block reads all of it N/16 times (356 MB
|
||||
// from L2 at M = 64 on a 27B's 17408-row gate_up, against its 47 MB weight). Two tiles a block load
|
||||
// each activation fragment once for both. r4d_selftest --perfmxd, clocks warmed, TN 1 -> 2: 17408 x 5120
|
||||
// at 32 rows 125 -> 85 us, at 64 157 -> 122; 5120 x 8704 75 -> 56, 95 -> 75; 21504 x 5376 154 -> 111,
|
||||
// 183 -> 150; at 16 to 24 rows within a few percent either way. Four tiles spill past 32 rows.
|
||||
// Narrow or shallow weights lose with two (fewer blocks than the part has units, a short K a wave):
|
||||
// at 32..64 rows 1024 x 2048 +7..30 %, 2048 x 2048 +6..28 %, 2048 x 4096 +4..37 %, 6144 x 1536 at 32
|
||||
// rows +14..30 % (two repetitions each), so the launcher takes two from N 4096 and K 3072 only, where
|
||||
// every measured shape gained or held (4096 x 2048 even, 14336 x 4096 64 rows 120 -> 90 us,
|
||||
// 4096 x 7168 67 -> 55).
|
||||
// TN: the activation is read per block, so with one column tile a block it is read N/16 times
|
||||
// (356 MB from L2 at M = 64 on a 17408 x 5120 weight, against its 47 MB). Two tiles a block load
|
||||
// each activation fragment once for both. On narrow or shallow weights two tiles lose (fewer
|
||||
// blocks than the part has units, a short K a wave), so the launcher takes two only from N 4096
|
||||
// and K 3072, past 24 rows. Four tiles spill past 32 rows.
|
||||
template <bool ABLK, int RT, int TN = 1>
|
||||
__global__ __launch_bounds__(R4D_MXG_KW * 32)
|
||||
void r4d_gemm_mxfp4a8_gemvw_kernel(
|
||||
|
||||
@@ -24,13 +24,24 @@
|
||||
// 2^(Wref[n]-127) is applied in the epilogue (see r4d_gemm_mxfp4a8_nt_m64.hip).
|
||||
//
|
||||
// Tile: 8 waves as 4 (M) x 2 (N), each owning TM=4 x TN fragments, so a block covers BMF = 256
|
||||
// rows and BNF = 2*TN*16 columns. TN=4 halves the column-block count and only wins on long prompts
|
||||
// (crossover near M=2048 on a 17408x5120 weight), so tn=0 picks TN=4 from M >= 2048. The K slab is
|
||||
// 128 at TN=2 (64 if K % 128 != 0) and 64 at TN=4, which holds twice the accumulators.
|
||||
// rows and BNF = 2*TN*16 columns. TN=4 halves the column-block count and wins from M = 1024 on
|
||||
// (on gfx1201 at a 27B's per-rank weights, mixed below), so tn=0 picks TN=4 from M >= 1024. The K
|
||||
// slab is 128 at TN=2 (64 if K % 128 != 0) and 64 at TN=4, which holds twice the accumulators.
|
||||
// TN does not change an output's bits: each fragment runs the same WMMAs in the same K order.
|
||||
//
|
||||
// Barriers fence LDS only: __syncthreads() also drains the global load counter, and nothing in
|
||||
// global memory is shared between the waves, so the next slab's A loads stay in flight.
|
||||
//
|
||||
// The weight words and exponents a thread stages are loaded a slab ahead, into registers, right
|
||||
// after the staging barrier, so their DRAM round trip runs under the slab's WMMAs instead of in
|
||||
// front of the next unpack (+1 VGPR at TN=4). Loading the A fragments a slab ahead too (+38 VGPRs)
|
||||
// is slower.
|
||||
//
|
||||
// A thin last row of blocks (1..160 rows, under 1024 rows in all) runs a second kernel whose
|
||||
// blocks of that row skip the WMMAs of their padding fragments past M. Every other M runs the
|
||||
// plain kernel, whose body declares its own LDS: a guard inside the one kernel, or the body
|
||||
// taking the LDS by pointer, slows the full blocks.
|
||||
//
|
||||
// Staging is clamped, never predicated: a predicated load gets its own exec-mask region and the
|
||||
// compiler cannot carry a counted s_wait_loadcnt across the merge. Clamped loads (tile past M ->
|
||||
// last real tile, row past N -> last aligned group) issue back to back; the epilogue drops them.
|
||||
@@ -63,7 +74,7 @@
|
||||
#define R4D_MXT_NTHR (R4D_MXT_WM * R4D_MXT_WN * 32)
|
||||
#define R4D_MXT_BMF (R4D_MXT_WM * R4D_MXT_TM * 16)
|
||||
#define R4D_MXT_KUNIT 64 // the narrowest K slab, so K must be a multiple of it
|
||||
#define R4D_MXT_TN4_MIN_M 2048 // tn=0's crossover, see above
|
||||
#define R4D_MXT_TN4_MIN_M 1024 // tn=0's crossover, see above
|
||||
|
||||
typedef int mxt_v2i __attribute__((ext_vector_type(2)));
|
||||
typedef unsigned mxt_u2 __attribute__((ext_vector_type(2)));
|
||||
@@ -85,12 +96,16 @@ __global__ __launch_bounds__(256) void r4d_mxfp4a8_tile_a_kernel(
|
||||
*(mxt_u2*)(AT + o * 8) = v;
|
||||
}
|
||||
|
||||
template <int TN, int LBK, bool ABLK>
|
||||
__global__ __launch_bounds__(R4D_MXT_NTHR) void r4d_gemm_mxfp4a8_tiled_kernel(
|
||||
// The kernel's body. OWN: it declares its LDS arrays itself (the plain kernel); otherwise it takes
|
||||
// the caller's (the two-body kernel, one set for both bodies). PART: a block the M edge cuts,
|
||||
// which skips the WMMAs of its padding fragments past M.
|
||||
template <int TN, int LBK, bool ABLK, bool PART, bool OWN>
|
||||
__device__ __forceinline__ void r4d_gemm_mxfp4a8_tiled_body(
|
||||
const unsigned char* __restrict__ AT, const float* __restrict__ As,
|
||||
const unsigned char* __restrict__ W, const unsigned char* __restrict__ Ws,
|
||||
const unsigned char* __restrict__ Wref, __hip_bfloat16* __restrict__ C,
|
||||
int M, int K, int N, int as_rs, int ldws, int ldc) {
|
||||
int M, int K, int N, int as_rs, int ldws, int ldc, unsigned char* __restrict__ sW_in,
|
||||
unsigned int* __restrict__ sMag_in, float* __restrict__ sR_in) {
|
||||
constexpr int TM = R4D_MXT_TM, WN = R4D_MXT_WN, NTHR = R4D_MXT_NTHR, BMF = R4D_MXT_BMF;
|
||||
constexpr int NS = LBK / 16, STR = LBK + R4D_MXT_PAD, BNF = WN * TN * 16;
|
||||
constexpr int NTILES_T = BNF / 16, TOT_SLOTS = NTILES_T * NS * 32;
|
||||
@@ -98,15 +113,23 @@ __global__ __launch_bounds__(R4D_MXT_NTHR) void r4d_gemm_mxfp4a8_tiled_kernel(
|
||||
constexpr int WSLOTS = (TOT_SLOTS >= NTHR * 4) ? 4 : (TOT_SLOTS >= NTHR * 2 ? 2 : 1);
|
||||
constexpr int NW = TOT_SLOTS / (NTHR * WSLOTS);
|
||||
static_assert(NW * NTHR * WSLOTS == TOT_SLOTS, "the W tile must split evenly over the block");
|
||||
__shared__ unsigned char sW[BNF * STR];
|
||||
__shared__ unsigned int sMag[32];
|
||||
__shared__ float sR[ABLK ? BMF : 1]; // ABLK: a boundary's ratio a row, then each row's final scale
|
||||
__shared__ unsigned char sW_own[OWN ? BNF * STR : 1];
|
||||
__shared__ unsigned int sMag_own[OWN ? 32 : 1];
|
||||
__shared__ float sR_own[OWN && ABLK ? BMF : 1]; // ABLK: a boundary's ratio a row, then each row's final scale
|
||||
unsigned char* __restrict__ sW = OWN ? sW_own : sW_in;
|
||||
unsigned int* __restrict__ sMag = OWN ? sMag_own : sMag_in;
|
||||
float* __restrict__ sR = OWN ? sR_own : sR_in;
|
||||
|
||||
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5;
|
||||
const int wm = wave / WN, wn = wave % WN;
|
||||
const int col = lane & 15, kb8 = (lane >> 4) * 8;
|
||||
const int m0 = blockIdx.y * BMF, n0 = blockIdx.x * BNF;
|
||||
const int ksteps = K / 16, Mt = (M + 15) >> 4;
|
||||
// PART: this wave's M fragments that hold a row below M, 0..TM and wave-uniform; the rest are
|
||||
// padding whose WMMAs are skipped (their rows are dropped at the store). Their A loads stay,
|
||||
// clamped, in the slab's one clause.
|
||||
const int live = PART ? __builtin_amdgcn_readfirstlane(min(TM, max(0, Mt - ((m0 >> 4) + wm * TM))))
|
||||
: TM;
|
||||
|
||||
if (tid < 32) sMag[tid] = (&kMxfp4Mag[0][0])[tid];
|
||||
|
||||
@@ -155,6 +178,31 @@ __global__ __launch_bounds__(R4D_MXT_NTHR) void r4d_gemm_mxfp4a8_tiled_kernel(
|
||||
const unsigned int* __restrict__ Wp = (const unsigned int*)W;
|
||||
lds_barrier(); // sMag
|
||||
|
||||
// The slab's weight words and exponents, loaded a slab ahead (the first one here).
|
||||
unsigned int wqn[NW][WSLOTS], wswn[NW];
|
||||
auto wload = [&](int k0) {
|
||||
const int kstep0 = k0 / 16;
|
||||
#pragma unroll
|
||||
for (int it = 0; it < NW; ++it) {
|
||||
const unsigned int* src =
|
||||
&Wp[((size_t)(wrc[it] >> 4) * ksteps + kstep0 + wkst[it]) * 32 + wlanec[it]];
|
||||
const int blk = (k0 + wkst[it] * 16) / R4D_MXFP4_GROUP;
|
||||
if constexpr (WSLOTS == 4) {
|
||||
const mxt_u4 v = *(const mxt_u4*)src;
|
||||
wqn[it][0] = v[0]; wqn[it][1] = v[1]; wqn[it][2] = v[2]; wqn[it][3] = v[3];
|
||||
wswn[it] = *(const unsigned int*)(Ws + (size_t)blk * ldws + wrc[it]);
|
||||
} else if constexpr (WSLOTS == 2) {
|
||||
const mxt_u2 v = *(const mxt_u2*)src;
|
||||
wqn[it][0] = v[0]; wqn[it][1] = v[1];
|
||||
wswn[it] = *(const unsigned short*)(Ws + (size_t)blk * ldws + wrc[it]);
|
||||
} else {
|
||||
wqn[it][0] = *src;
|
||||
wswn[it] = Ws[(size_t)blk * ldws + wrc[it]];
|
||||
}
|
||||
}
|
||||
};
|
||||
wload(0);
|
||||
|
||||
for (int k0 = 0; k0 < K; k0 += LBK) {
|
||||
// ABLK: at a 128 boundary each row's ratio goes to LDS, published by the staging barrier below
|
||||
// (sR's previous readers finished before the last slab's closing barrier). A zero block keeps
|
||||
@@ -172,36 +220,20 @@ __global__ __launch_bounds__(R4D_MXT_NTHR) void r4d_gemm_mxfp4a8_tiled_kernel(
|
||||
for (int st = 0; st < NS; ++st)
|
||||
af[i][st] = *(const mxt_v2i*)(abase[i] +
|
||||
(aoff + (unsigned int)(k0 * 16) + (unsigned int)(st * 256)));
|
||||
// W slab -> LDS, folded to e4m3.
|
||||
const int kstep0 = k0 / 16;
|
||||
// W slab -> LDS, folded to e4m3, from the words loaded a slab ago.
|
||||
#pragma unroll
|
||||
for (int it = 0; it < NW; ++it) {
|
||||
const unsigned int* src =
|
||||
&Wp[((size_t)(wrc[it] >> 4) * ksteps + kstep0 + wkst[it]) * 32 + wlanec[it]];
|
||||
const int blk = (k0 + wkst[it] * 16) / R4D_MXFP4_GROUP;
|
||||
unsigned int wq[WSLOTS], wsw;
|
||||
if constexpr (WSLOTS == 4) {
|
||||
const mxt_u4 v = *(const mxt_u4*)src;
|
||||
wq[0] = v[0]; wq[1] = v[1]; wq[2] = v[2]; wq[3] = v[3];
|
||||
wsw = *(const unsigned int*)(Ws + (size_t)blk * ldws + wrc[it]);
|
||||
} else if constexpr (WSLOTS == 2) {
|
||||
const mxt_u2 v = *(const mxt_u2*)src;
|
||||
wq[0] = v[0]; wq[1] = v[1];
|
||||
wsw = *(const unsigned short*)(Ws + (size_t)blk * ldws + wrc[it]);
|
||||
} else {
|
||||
wq[0] = *src;
|
||||
wsw = Ws[(size_t)blk * ldws + wrc[it]];
|
||||
}
|
||||
#pragma unroll
|
||||
for (int q = 0; q < WSLOTS; ++q) {
|
||||
const int d = r4d_mxfp4_fold_d((int)((wref[it] >> (8 * q)) & 0xFF),
|
||||
(int)((wsw >> (8 * q)) & 0xFF));
|
||||
(int)((wswn[it] >> (8 * q)) & 0xFF));
|
||||
unsigned int lo, hi;
|
||||
r4d_mxfp4_unpack8_t(wq[q], sMag[d * 2], sMag[d * 2 + 1], &lo, &hi);
|
||||
r4d_mxfp4_unpack8_t(wqn[it][q], sMag[d * 2], sMag[d * 2 + 1], &lo, &hi);
|
||||
*(mxt_u2*)(&sW[(wrow[it] + q) * STR + wkloc[it]]) = mxt_u2{lo, hi};
|
||||
}
|
||||
}
|
||||
lds_barrier();
|
||||
if (k0 + LBK < K) wload(k0 + LBK); // the next slab's weight flies across this slab's WMMAs
|
||||
|
||||
if constexpr (ABLK) {
|
||||
// Re-express each row's partial sum in the new block's scale: one ratio a lane a row.
|
||||
@@ -235,11 +267,13 @@ __global__ __launch_bounds__(R4D_MXT_NTHR) void r4d_gemm_mxfp4a8_tiled_kernel(
|
||||
// loads above the first WMMA.
|
||||
__builtin_amdgcn_sched_barrier(0);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < TM; ++i)
|
||||
for (int i = 0; i < TM; ++i) {
|
||||
if (PART && i >= live) break; // wave-uniform: padding fragments past M
|
||||
#pragma unroll
|
||||
for (int j = 0; j < TN; ++j)
|
||||
acc[i][j] = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(wf[j], af[i][step],
|
||||
acc[i][j]);
|
||||
}
|
||||
}
|
||||
lds_barrier();
|
||||
}
|
||||
@@ -287,6 +321,37 @@ __global__ __launch_bounds__(R4D_MXT_NTHR) void r4d_gemm_mxfp4a8_tiled_kernel(
|
||||
}
|
||||
}
|
||||
|
||||
// The plain kernel: every block the unguarded body, over the body's own LDS.
|
||||
template <int TN, int LBK, bool ABLK>
|
||||
__global__ __launch_bounds__(R4D_MXT_NTHR) void r4d_gemm_mxfp4a8_tiled_kernel(
|
||||
const unsigned char* __restrict__ AT, const float* __restrict__ As,
|
||||
const unsigned char* __restrict__ W, const unsigned char* __restrict__ Ws,
|
||||
const unsigned char* __restrict__ Wref, __hip_bfloat16* __restrict__ C,
|
||||
int M, int K, int N, int as_rs, int ldws, int ldc) {
|
||||
r4d_gemm_mxfp4a8_tiled_body<TN, LBK, ABLK, false, true>(AT, As, W, Ws, Wref, C, M, K, N, as_rs, ldws,
|
||||
ldc, nullptr, nullptr, nullptr);
|
||||
}
|
||||
|
||||
// The two-body kernel, for a thin last row of blocks: a block of that row takes the guarded body,
|
||||
// every other block the unguarded one, uniform over the block, over one set of LDS arrays.
|
||||
template <int TN, int LBK, bool ABLK>
|
||||
__global__ __launch_bounds__(R4D_MXT_NTHR) void r4d_gemm_mxfp4a8_tiled_part_kernel(
|
||||
const unsigned char* __restrict__ AT, const float* __restrict__ As,
|
||||
const unsigned char* __restrict__ W, const unsigned char* __restrict__ Ws,
|
||||
const unsigned char* __restrict__ Wref, __hip_bfloat16* __restrict__ C,
|
||||
int M, int K, int N, int as_rs, int ldws, int ldc) {
|
||||
constexpr int BNF = R4D_MXT_WN * TN * 16, STR = LBK + R4D_MXT_PAD;
|
||||
__shared__ unsigned char sW[BNF * STR];
|
||||
__shared__ unsigned int sMag[32];
|
||||
__shared__ float sR[ABLK ? R4D_MXT_BMF : 1];
|
||||
if ((int)(blockIdx.y + 1) * R4D_MXT_BMF > M)
|
||||
r4d_gemm_mxfp4a8_tiled_body<TN, LBK, ABLK, true, false>(AT, As, W, Ws, Wref, C, M, K, N, as_rs,
|
||||
ldws, ldc, sW, sMag, sR);
|
||||
else
|
||||
r4d_gemm_mxfp4a8_tiled_body<TN, LBK, ABLK, false, false>(AT, As, W, Ws, Wref, C, M, K, N, as_rs,
|
||||
ldws, ldc, sW, sMag, sR);
|
||||
}
|
||||
|
||||
extern "C" int r4d_gemm_mxfp4a8_tiled_group(void) { return R4D_MXFP4_GROUP; }
|
||||
|
||||
extern "C" long r4d_mxfp4a8_tile_a_bytes(int M, int K) {
|
||||
@@ -332,9 +397,18 @@ extern "C" void r4d_gemm_mxfp4a8_tiled(long at, long ascale, long wq, long ws, l
|
||||
auto Rp = (const unsigned char*)wref;
|
||||
auto Cp = (__hip_bfloat16*)c;
|
||||
hipStream_t st = (hipStream_t)stream;
|
||||
#define MXT_LAUNCH(TNv, LBKv, ABv) \
|
||||
hipLaunchKernelGGL((r4d_gemm_mxfp4a8_tiled_kernel<TNv, LBKv, ABv>), grid, block, 0, st, Ap, Sp, \
|
||||
Wp, Zp, Rp, Cp, M, K, N, as_rs, ldws, ldc)
|
||||
// The two-body kernel only for a thin last row of blocks (1..160 rows) under 1024 rows; see the
|
||||
// header.
|
||||
const int rem = M % R4D_MXT_BMF;
|
||||
const bool part = rem > 0 && rem <= 160 && M < 1024;
|
||||
#define MXT_LAUNCH(TNv, LBKv, ABv) do { \
|
||||
if (part) \
|
||||
hipLaunchKernelGGL((r4d_gemm_mxfp4a8_tiled_part_kernel<TNv, LBKv, ABv>), grid, block, 0, st, \
|
||||
Ap, Sp, Wp, Zp, Rp, Cp, M, K, N, as_rs, ldws, ldc); \
|
||||
else \
|
||||
hipLaunchKernelGGL((r4d_gemm_mxfp4a8_tiled_kernel<TNv, LBKv, ABv>), grid, block, 0, st, Ap, \
|
||||
Sp, Wp, Zp, Rp, Cp, M, K, N, as_rs, ldws, ldc); \
|
||||
} while (0)
|
||||
if (TN == 4) { if (ablk) MXT_LAUNCH(4, 64, true); else MXT_LAUNCH(4, 64, false); }
|
||||
else if (wide_slab) { if (ablk) MXT_LAUNCH(2, 128, true); else MXT_LAUNCH(2, 128, false); }
|
||||
else { if (ablk) MXT_LAUNCH(2, 64, true); else MXT_LAUNCH(2, 64, false); }
|
||||
|
||||
@@ -537,6 +537,178 @@ __global__ __launch_bounds__(128) void attend_matrix(const uint16_t* q, const ui
|
||||
atomicAdd(&g_mla_stats[2], (unsigned long long)(raster + 15) / 16);
|
||||
}
|
||||
}
|
||||
/* ROWS gives a workgroup one query row and HG groups of sixteen heads, four waves a group, so a
|
||||
* key chunk is loaded once for 16*HG heads instead of the head-tiled row's sixteen. GLM's prefill
|
||||
* splits attention by rows, one rank holding all 64 heads of its rows; that is what this serves.
|
||||
*
|
||||
* Scores are S^T = K Q^T: tokens are the matrix rows and heads the columns, so a head is a lane
|
||||
* and its running maximum and denominator stay in registers. Each wave of a group takes a quarter
|
||||
* of the latent dimension; the four partial dot products meet in LDS and every wave of the group
|
||||
* adds them in the same order, so all four hold identical scores and statistics. The
|
||||
* probabilities become the second product's B operand with one permlanex16 swap, and each wave
|
||||
* accumulates O^T for its quarter of the latent columns, the keys read transposed from the tile.
|
||||
* The next chunk's keys are fetched into registers while the current one is multiplied, so a
|
||||
* chunk costs two barriers. */
|
||||
constexpr int kRowsPitch = 520;
|
||||
template <int HG, bool WIDE>
|
||||
__global__ __launch_bounds__(HG * 128) void attend_rows(const uint16_t* q, const uint16_t* kv,
|
||||
const int* bt, const int* len,
|
||||
uint16_t* out, float* scratch, Meta m) {
|
||||
constexpr int NT = HG * 128, LOADS = 16 * 64 / NT;
|
||||
static_assert(LOADS * NT == 16 * 64 && LOADS <= 32, "a chunk's 16-byte pieces over the group");
|
||||
__shared__ __align__(16) uint16_t keys[2][16 * kRowsPitch];
|
||||
__shared__ __align__(32) float partial[HG * 4][32 * 8];
|
||||
__shared__ int visible[2][16];
|
||||
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, quarter = wave & 3,
|
||||
group = wave >> 2, r = blockIdx.x, split = blockIdx.z;
|
||||
const int head = blockIdx.y * 16 * HG + group * 16 + (lane & 15);
|
||||
const int live = min(max(len[(long)r * m.lp], 0), m.blocks * m.bs);
|
||||
const int begin = (int)((long)live * split / m.splits),
|
||||
end = (int)((long)live * (split + 1) / m.splits);
|
||||
v8bf query[8];
|
||||
{
|
||||
const uint16_t* qp =
|
||||
q + (long)r * m.qr + (long)head * m.qh + quarter * 128 + 4 * (lane >> 4);
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s) {
|
||||
union {
|
||||
uint16_t h[8];
|
||||
v8bf f;
|
||||
} u;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 4; ++e) {
|
||||
u.h[e] = qp[s * 16 + e];
|
||||
u.h[4 + e] = qp[s * 16 + 8 + e];
|
||||
}
|
||||
query[s] = u.f;
|
||||
}
|
||||
}
|
||||
uint4 staged[LOADS];
|
||||
unsigned staged_ok = 0;
|
||||
auto fetch = [&](int start) {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < LOADS; ++j) {
|
||||
const int flat = tid + j * NT, token = start + (flat >> 6), piece = flat & 63;
|
||||
const int page = token < end ? bt[(long)r * m.bp + (token / m.bs) * m.bc] : -1;
|
||||
const bool ok = page >= 0 && page < m.pages;
|
||||
const long at = ok ? (long)page * m.kp + (token % m.bs) * m.kt + piece * 8 : 0;
|
||||
if constexpr (WIDE) {
|
||||
staged[j] = *(const uint4*)(kv + at);
|
||||
if (!ok) staged[j] = make_uint4(0, 0, 0, 0);
|
||||
} else {
|
||||
uint32_t w[4];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i)
|
||||
w[i] = ok ? (uint32_t)kv[at + 2 * i] | (uint32_t)kv[at + 2 * i + 1] << 16 : 0u;
|
||||
staged[j] = make_uint4(w[0], w[1], w[2], w[3]);
|
||||
}
|
||||
staged_ok = ok ? staged_ok | 1u << j : staged_ok & ~(1u << j);
|
||||
}
|
||||
};
|
||||
auto stash = [&](int b) {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < LOADS; ++j) {
|
||||
const int flat = tid + j * NT, token = flat >> 6, piece = flat & 63;
|
||||
*(uint4*)(keys[b] + token * kRowsPitch + piece * 8) = staged[j];
|
||||
if (!piece) visible[b][token] = (int)(staged_ok >> j & 1u);
|
||||
}
|
||||
};
|
||||
v8f value[8];
|
||||
#pragma unroll
|
||||
for (int mt = 0; mt < 8; ++mt) value[mt] = (v8f){ 0 };
|
||||
float run_max = -INFINITY, run_den = 0;
|
||||
fetch(begin);
|
||||
stash(0);
|
||||
__syncthreads();
|
||||
int buf = 0;
|
||||
for (int start = begin; start < end; start += 16) {
|
||||
const bool more = start + 16 < end;
|
||||
if (more) fetch(start + 16);
|
||||
const uint16_t* tile = keys[buf];
|
||||
v8f dot = { 0 };
|
||||
#pragma unroll
|
||||
for (int s = 0; s < 8; ++s)
|
||||
dot = mma(frag(tile, kRowsPitch, 0, quarter * 128 + s * 16, lane), query[s], dot);
|
||||
*(v8f*)&partial[wave][lane * 8] = dot;
|
||||
__syncthreads();
|
||||
v8f score = *(const v8f*)&partial[group * 4][lane * 8];
|
||||
#pragma unroll
|
||||
for (int k = 1; k < 4; ++k) score += *(const v8f*)&partial[group * 4 + k][lane * 8];
|
||||
float chunk_max = -INFINITY;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
score[e] = visible[buf][8 * (lane >> 4) + e] ? score[e] * m.scale : -INFINITY;
|
||||
chunk_max = fmaxf(chunk_max, score[e]);
|
||||
}
|
||||
chunk_max = fmaxf(chunk_max, swap16(chunk_max));
|
||||
const float next_max = fmaxf(run_max, chunk_max);
|
||||
const float alpha = isfinite(next_max) ? expf(run_max - next_max) : 1;
|
||||
float p[8], sum = 0;
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
p[e] = isfinite(score[e]) ? expf(score[e] - next_max) : 0;
|
||||
sum += p[e];
|
||||
}
|
||||
/* Both halves add tokens 0-7 before 8-15, so the sixteen lanes of a head agree. */
|
||||
const float other = swap16(sum);
|
||||
run_den = run_den * alpha + (lane < 16 ? sum + other : other + sum);
|
||||
run_max = next_max;
|
||||
/* A B fragment's lane holds its head's tokens 0-3 and 8-11 (lanes 0-15) or 4-7 and 12-15
|
||||
* (16-31); a score lane holds 0-7 or 8-15. Each half sends the other the four it needs. */
|
||||
uint32_t hi[4], lo[4];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const uint16_t h0 = f32_to_bf16(p[2 * i]), h1 = f32_to_bf16(p[2 * i + 1]);
|
||||
hi[i] = (uint32_t)h0 | (uint32_t)h1 << 16;
|
||||
lo[i] = (uint32_t)f32_to_bf16(p[2 * i] - bf16_to_f32(h0)) |
|
||||
(uint32_t)f32_to_bf16(p[2 * i + 1] - bf16_to_f32(h1)) << 16;
|
||||
}
|
||||
const bool low = lane < 16;
|
||||
const uint32_t hx0 = r4d_xor_lane_u32<16>(low ? hi[2] : hi[0]),
|
||||
hx1 = r4d_xor_lane_u32<16>(low ? hi[3] : hi[1]),
|
||||
lx0 = r4d_xor_lane_u32<16>(low ? lo[2] : lo[0]),
|
||||
lx1 = r4d_xor_lane_u32<16>(low ? lo[3] : lo[1]);
|
||||
union {
|
||||
uint32_t w[4];
|
||||
v8bf f;
|
||||
} ph, pl;
|
||||
ph.w[0] = low ? hi[0] : hx0;
|
||||
ph.w[1] = low ? hi[1] : hx1;
|
||||
ph.w[2] = low ? hx0 : hi[2];
|
||||
ph.w[3] = low ? hx1 : hi[3];
|
||||
pl.w[0] = low ? lo[0] : lx0;
|
||||
pl.w[1] = low ? lo[1] : lx1;
|
||||
pl.w[2] = low ? lx0 : lo[2];
|
||||
pl.w[3] = low ? lx1 : lo[3];
|
||||
#pragma unroll
|
||||
for (int mt = 0; mt < 8; ++mt) {
|
||||
const auto v = fragT(tile, kRowsPitch, quarter * 128 + mt * 16, 0, lane);
|
||||
value[mt] *= alpha;
|
||||
value[mt] = mma(v, ph.f, value[mt]);
|
||||
value[mt] = mma(v, pl.f, value[mt]);
|
||||
}
|
||||
if (more) stash(buf ^ 1);
|
||||
__syncthreads();
|
||||
buf ^= 1;
|
||||
}
|
||||
#pragma unroll
|
||||
for (int mt = 0; mt < 8; ++mt)
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; ++e) {
|
||||
const int col = quarter * 128 + mt * 16 + 8 * (lane >> 4) + e;
|
||||
if (m.splits == 1)
|
||||
out[(long)r * m.orr + (long)head * m.oh + col] =
|
||||
f32_to_bf16(run_den > 0 ? value[mt][e] / run_den : 0);
|
||||
else
|
||||
scratch[(((long)r * m.heads + head) * m.splits + split) * PART + col] =
|
||||
value[mt][e];
|
||||
}
|
||||
if (m.splits > 1 && !quarter && lane < 16) {
|
||||
float* dst = scratch + (((long)r * m.heads + head) * m.splits + split) * PART;
|
||||
dst[D] = run_max;
|
||||
dst[D + 1] = run_den;
|
||||
}
|
||||
}
|
||||
/* Read once: the launch path asks on every matrix launch. */
|
||||
bool stats_on() {
|
||||
static const bool on = [] {
|
||||
@@ -647,6 +819,70 @@ int body(const RadArgs* a, RadStream stream) {
|
||||
if (rc == RAD_OK && stats) report_stats(stream);
|
||||
return rc;
|
||||
}
|
||||
/* r4d_mla_rows_pin_form: 0 is the launcher's rule (64 heads a workgroup, vector key loads where
|
||||
* the cache is aligned, the matrix row's kernel under kRowsMin rows), 1 the element-wise key load,
|
||||
* 2 thirty-two heads a workgroup, 3 the rule's rows kernel at any row count, 4 the matrix row's
|
||||
* kernel at every row count. The pin is a test hook (r4d_selftest, r4d_emu); nothing else sets
|
||||
* it. */
|
||||
int g_mla_rows_form = 0;
|
||||
/* Under it, a decode step's rows at one rank, a workgroup a row leaves the card mostly idle, and
|
||||
* the matrix row's four tiles a row and its splits are faster (r4d_selftest --case
|
||||
* mla_attn_rows_bf16, M 7: 0.5x). */
|
||||
constexpr long kRowsMin = 32;
|
||||
int rows_body(const RadArgs* a, RadStream stream) {
|
||||
if (!a || a->n_t < 5) return RAD_E_INVAL;
|
||||
const auto &q = a->t[0], &kv = a->t[1], &bt = a->t[2], &len = a->t[3], &o = a->t[4];
|
||||
if (q.dtype != RAD_BF16 || kv.dtype != RAD_BF16 || o.dtype != RAD_BF16 || bt.dtype != RAD_I32 ||
|
||||
len.dtype != RAD_I32)
|
||||
return RAD_E_DTYPE;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
if (!a->t[i].data) return RAD_E_INVAL;
|
||||
const long heads = r4d_geti_or(a, "heads", 0), bs = r4d_geti_or(a, "block_size", 0);
|
||||
const float scale = (float)r4d_getf(a, "scale", 0);
|
||||
if (r4d_geti_or(a, "latent_dim", 0) != D || heads < 1 || heads > 64 || bs < 1 || bs > 256 ||
|
||||
!std::isfinite(scale) || scale <= 0 || q.rank != 3 || q.shape[0] < 1 ||
|
||||
q.shape[0] > 65535 || q.shape[1] != heads || q.shape[2] != D || q.stride[2] != 1 ||
|
||||
q.stride[1] < D || q.stride[0] < heads * q.stride[1] || kv.rank != 4 || kv.shape[0] < 1 ||
|
||||
kv.shape[0] > INT_MAX || kv.shape[1] != 1 || kv.shape[2] != bs || kv.shape[3] != D ||
|
||||
kv.stride[3] != 1 || kv.stride[2] < D || kv.stride[0] < bs * kv.stride[2] || bt.rank != 2 ||
|
||||
bt.shape[0] != q.shape[0] || bt.shape[1] < 1 || bt.shape[1] > INT_MAX / bs ||
|
||||
bt.stride[1] < 1 || bt.stride[0] < bt.shape[1] * bt.stride[1] || len.rank != 1 ||
|
||||
len.shape[0] != q.shape[0] || len.stride[0] < 1 || o.rank != 3 ||
|
||||
o.shape[0] != q.shape[0] || o.shape[1] != heads || o.shape[2] != D || o.stride[2] != 1 ||
|
||||
o.stride[1] < D || o.stride[0] < heads * o.stride[1])
|
||||
return RAD_E_SHAPE;
|
||||
if (heads % 32) return RAD_E_UNSUPPORTED;
|
||||
const int form = g_mla_rows_form;
|
||||
if (form == 4 || (!form && q.shape[0] < kRowsMin)) return body<true>(a, stream);
|
||||
const int group_heads = heads % 64 || form == 2 ? 32 : 64;
|
||||
const long rows = q.shape[0], groups = heads / group_heads, jobs = rows * groups;
|
||||
/* Short batches split the history until 64 workgroups run, two to each of the R9700's 32
|
||||
* WGPs, as far as 16-token chunks and the scratch's 2048 partials allow. */
|
||||
int splits = (int)std::min(64l, (64 + jobs - 1) / jobs);
|
||||
splits = (int)std::max(1l, std::min((long)splits, (bt.shape[1] * bs + 15) / 16));
|
||||
splits = (int)std::max(1l, std::min((long)splits, 2048 / (rows * heads)));
|
||||
if (!a->scratch || a->scratch_bytes < matrix_scratch_bytes) splits = 1;
|
||||
const bool wide = form != 1 && !((uintptr_t)kv.data & 15) && !(kv.stride[0] % 8) &&
|
||||
!(kv.stride[2] % 8);
|
||||
Meta m{ q.stride[0], q.stride[1], kv.stride[0], kv.stride[2],
|
||||
bt.stride[0], bt.stride[1], len.stride[0], o.stride[0],
|
||||
o.stride[1], (int)q.shape[0], (int)heads, (int)bt.shape[1],
|
||||
(int)bs, (int)kv.shape[0], splits, scale };
|
||||
#define ROWS_EMIT(HG, W) \
|
||||
R4D_EMIT(stream, (attend_rows<HG, W>), dim3(m.rows, (unsigned)groups, m.splits), \
|
||||
dim3(HG * 128), 0, (const uint16_t*)q.data, (const uint16_t*)kv.data, \
|
||||
(const int*)bt.data, (const int*)len.data, (uint16_t*)o.data, (float*)a->scratch, m)
|
||||
int rc;
|
||||
if (group_heads == 64)
|
||||
rc = wide ? ROWS_EMIT(4, true) : ROWS_EMIT(4, false);
|
||||
else
|
||||
rc = wide ? ROWS_EMIT(2, true) : ROWS_EMIT(2, false);
|
||||
#undef ROWS_EMIT
|
||||
if (rc == RAD_OK && splits > 1)
|
||||
rc = R4D_EMIT(stream, merge, dim3(m.rows, (m.heads + 3) / 4), dim3(128), 0,
|
||||
(const float*)a->scratch, (uint16_t*)o.data, m);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
extern "C" int r4d_rad_mla_attn(const RadArgs* a, RadStream stream) {
|
||||
return body<false>(a, stream);
|
||||
@@ -654,12 +890,16 @@ extern "C" int r4d_rad_mla_attn(const RadArgs* a, RadStream stream) {
|
||||
extern "C" int r4d_rad_mla_attn_matrix(const RadArgs* a, RadStream stream) {
|
||||
return body<true>(a, stream);
|
||||
}
|
||||
extern "C" int r4d_rad_mla_attn_rows(const RadArgs* a, RadStream stream) {
|
||||
return rows_body(a, stream);
|
||||
}
|
||||
extern "C" int r4d_rad_mla_project(const RadArgs* a, RadStream stream) {
|
||||
return project_body(a, stream);
|
||||
}
|
||||
extern "C" int64_t r4d_rad_mla_attn_scratch(const RadArgs*) { return scratch_bytes; }
|
||||
extern "C" int64_t r4d_rad_mla_attn_matrix_scratch(const RadArgs*) { return matrix_scratch_bytes; }
|
||||
extern "C" void r4d_mla_matrix_pin_form(int form) { g_mla_matrix_form = form; }
|
||||
extern "C" void r4d_mla_rows_pin_form(int form) { g_mla_rows_form = form; }
|
||||
/* Test hook: the current device's RADIANCE_MLA_CONFLICT_STATS counters since the last call (passes,
|
||||
* second passes, raster chunks), then zeroed. */
|
||||
extern "C" void r4d_mla_matrix_stats(unsigned long long* out) {
|
||||
|
||||
@@ -234,8 +234,10 @@ 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);
|
||||
int r4d_rad_frame_store (const RadArgs*, RadStream);
|
||||
int r4d_rad_resample_nearest (const RadArgs*, RadStream);
|
||||
int r4d_rad_fourier_embed (const RadArgs*, RadStream);
|
||||
@@ -729,6 +731,7 @@ int r4d_rad_mla_attn(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_mla_attn_scratch(const RadArgs*);
|
||||
int r4d_rad_mla_attn_matrix(const RadArgs*, RadStream);
|
||||
int64_t r4d_rad_mla_attn_matrix_scratch(const RadArgs*);
|
||||
int r4d_rad_mla_attn_rows(const RadArgs*, RadStream);
|
||||
int r4d_shape_mla_attn(const RadParam*, int, int, RadOpdDesc*);
|
||||
int r4d_rad_router_sigmoid_topk(const RadArgs*, RadStream);
|
||||
int r4d_shape_router_sigmoid_topk(const RadParam*, int, int, RadOpdDesc*);
|
||||
|
||||
+61
-92
@@ -2497,7 +2497,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 },
|
||||
@@ -2516,7 +2517,8 @@ static const RadOperandSpec oSnake[] = {
|
||||
static const RadParamSpec pCfgTopk[] = { P_INT("M"), P_INT("n_vocab"), P_INT("k"), P_INT("rep"),
|
||||
P_INTO("cond_k"), P_F64("cfg"), P_F64O("temp"),
|
||||
P_INT("stream"), P_STR("dtype"), P_INTO("sup_lo"),
|
||||
P_INTO("sup_hi"), P_F64O("penalty") };
|
||||
P_INTO("sup_hi"), P_F64O("penalty"),
|
||||
P_F64O("top_p") };
|
||||
static const RadOperandSpec oCfgTopk[] = {
|
||||
{ "logits", RAD_OPD_IN, 0 }, { "seed", RAD_OPD_IN, 0 }, { "frame", RAD_OPD_IN, 0 },
|
||||
{ "ids", RAD_OPD_OUT, 0 }, { "cfg", RAD_OPD_IN, 1 }, { "lane", RAD_OPD_IN, 1 },
|
||||
@@ -2524,11 +2526,18 @@ static const RadOperandSpec oCfgTopk[] = {
|
||||
};
|
||||
static const RadParamSpec pRvqEmbed[] = { P_INT("M"), P_INT("n_embd"), P_INT("n_codes"),
|
||||
P_INT("vocab"), P_INT("off0"), P_F64("scale"),
|
||||
P_STR("dtype"), P_INTO("round") };
|
||||
P_STR("dtype"), P_INTO("round"), P_INTO("clamp") };
|
||||
static const RadOperandSpec oRvqEmbed[] = {
|
||||
{ "codes", RAD_OPD_IN, 0 }, { "t0", RAD_OPD_WEIGHT, 0 }, { "t1", RAD_OPD_WEIGHT, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
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[] = {
|
||||
@@ -2540,6 +2549,12 @@ static const RadParamSpec pEndStatus[] = { P_INT("M"), P_INT("end"), P_INT("rep"
|
||||
static const RadOperandSpec oEndStatus[] = {
|
||||
{ "ids", RAD_OPD_IN, 0 }, { "lane", RAD_OPD_IN, 0 }, { "status", RAD_OPD_INOUT, 0 },
|
||||
};
|
||||
static const RadParamSpec pRasPick[] = { P_INT("M"), P_INT("win"), P_INT("lo"), P_INT("hi"),
|
||||
P_INTO("rep"), P_INTO("clear") };
|
||||
static const RadOperandSpec oRasPick[] = {
|
||||
{ "first", RAD_OPD_IN, 0 }, { "alt", RAD_OPD_IN, 0 }, { "lane", RAD_OPD_IN, 0 },
|
||||
{ "hist", RAD_OPD_INOUT, 0 }, { "ids", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
static const RadParamSpec pFrameStore[] = { P_INT("M"), P_INT("n_codes"), P_INT("stride"),
|
||||
P_INTO("rep"), P_INTO("skip") };
|
||||
static const RadOperandSpec oFrameStore[] = {
|
||||
@@ -3507,10 +3522,16 @@ 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,
|
||||
"status[lane[i]] = ids[rep i] == end ? -1 : ids[rep i]."),
|
||||
SCHEMA("ras_pick", pRasPick, oRasPick,
|
||||
"repetition-aware sampling: the alternative draw where the first is in [lo, hi) and in "
|
||||
"the lane's window of its last picks, else the first; the pick enters the window."),
|
||||
SCHEMA("frame_store", pFrameStore, oFrameStore,
|
||||
"a frame's codes into its lane's history: dst[(lane n_codes + k) stride + frame - skip] "
|
||||
"= codes[k, rep i] when frame >= skip and status[lane] >= 0."),
|
||||
@@ -4205,20 +4226,6 @@ static const RadConstraint cArTsExact8[] = {
|
||||
C_LE("numel", R4D_AR_TWOSHOT_WIDE_MAX_ELEMS),
|
||||
C_P2P,
|
||||
};
|
||||
/* The three- and eight-rank two-shots at `overlap` 1, as cArTsExact4Lane: a pipelined prefill's
|
||||
* all-reduce beside the other piece's compute. They launch as the wide rows unless R4D_AR_LANE_3_8=1
|
||||
* (tArTs38Lane); neither launch has been measured on three or eight cards. */
|
||||
static const RadConstraint cArTsExact3Lane[] = {
|
||||
C_EQ("world_size", 3), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"),
|
||||
C_GE("numel", R4D_AR_WIDE_ONESHOT_MAX_ELEMS + 1), C_LE("numel", R4D_AR_TWOSHOT_WIDE_MAX_ELEMS),
|
||||
C_EQ("overlap", 1), C_P2P,
|
||||
};
|
||||
static const RadConstraint cArTsExact8Lane[] = {
|
||||
C_EQ("world_size", 8), C_EQ("exact", 1), C_IN("dtype", "bf16 fp16 fp32"),
|
||||
C_DIV("numel", 64), C_GE("numel", R4D_AR_WIDE_ONESHOT_MAX_ELEMS + 1),
|
||||
C_LE("numel", R4D_AR_TWOSHOT_WIDE_MAX_ELEMS),
|
||||
C_EQ("overlap", 1), C_P2P,
|
||||
};
|
||||
static const RadConstraint cArWht6[] = {
|
||||
C_EQ("world_size", 2), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 64),
|
||||
C_P2P,
|
||||
@@ -4237,11 +4244,6 @@ static const RadConstraint cArTi8Ts3[] = {
|
||||
C_EQ("world_size", 3), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 32),
|
||||
C_P2P,
|
||||
};
|
||||
/* The same at `overlap` 1 (tArTs38Lane: the wide launch unless R4D_AR_LANE_3_8=1). */
|
||||
static const RadConstraint cArTi8Ts3Lane[] = {
|
||||
C_EQ("world_size", 3), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 32),
|
||||
C_EQ("overlap", 1), C_P2P,
|
||||
};
|
||||
static const RadConstraint cArTi8Ts4[] = {
|
||||
C_EQ("world_size", 4), C_EQ("exact", 0), C_IN("dtype", "bf16 fp16"), C_DIV("numel", 128),
|
||||
C_P2P,
|
||||
@@ -4398,6 +4400,16 @@ static const RadConstraint cMlaMatrix[] = { C_GE("M", 1),
|
||||
C_GE("blocks", 1),
|
||||
C_EQ("block_size", 4),
|
||||
C_IN("dtype", "bf16") };
|
||||
/* GLM's prefill splits MLA by rows, a rank holding every head of its rows: the rows row loads a
|
||||
* key chunk once for a workgroup of 64 heads and outranks the matrix row there. Measured on gfx1201
|
||||
* (r4d_selftest --perfmla 512 513 64 20 topk:16:16000), a rank's run of 512 rows takes 3.9 ms
|
||||
* against 12.3 ms on the matrix row, and the 4096 rows x 8 heads it replaces 14.0 ms on the paired
|
||||
* matrix row. Under 32 rows its launcher runs the matrix row's kernel (r4d_mla.hip); a bound on M
|
||||
* here would cut every mla_attn band at 32, the eight-head ones too. */
|
||||
static const RadConstraint cMlaRows[] = { C_GE("M", 1), C_LE("M", 65535),
|
||||
C_EQ("latent_dim", 512), C_EQ("heads", 64),
|
||||
C_GE("blocks", 1), C_EQ("block_size", 4),
|
||||
C_IN("dtype", "bf16") };
|
||||
/* `n` is bounded by LDS: nh (hc*n) and ne (n) are both bf16 and live through the second phase;
|
||||
* 28672 elements is that budget at hc 4. */
|
||||
static const RadConstraint cMtpEnter[] = {
|
||||
@@ -4846,10 +4858,8 @@ static const RadConstraint cGemmMxfp4A8GemvWide[] = {
|
||||
/* gemm_mxfp4a8_decode_shortk: the decode entry at 33..64 rows of a wide, short weight, above
|
||||
* gemv_wide by priority. There gemv_wide's blocks hold three or four row tiles over a short K each,
|
||||
* so the cross-wave reduction of a block weighs more than its WMMAs, and the decode kernel's split
|
||||
* wins (R9700, each row alone, ours' gemv_wide / decode / upstream's decode, us: 19456 x 2560 at 64
|
||||
* rows 131.8 / 93.3 / 104.5, 16384 x 2048 92.8 / 68.6 / 76.1, 9728 x 2560 74.1 / 52.5 / 67.4).
|
||||
* Narrower N stays with gemv_wide (5120 x 2176 at 40 rows 39.2 against the decode kernel's 52.8),
|
||||
* as does K past 2560 (17408 x 5120 at 40 rows 119.3 against 129.0). */
|
||||
* wins on gfx1201 (19456 x 2560 at 64 rows: 93.3 us against gemv_wide's 131.8). Narrower N and K
|
||||
* past 2560 stay with gemv_wide. */
|
||||
static const RadConstraint cGemmMxfp4A8DecodeShortK[] = {
|
||||
C_GE("M", 33), C_LE("M", 64), C_GE("N", 8192), C_LE("N", 36864), C_LE("K", 2560), C_DIV("K", 64),
|
||||
C_DIV("N", 16), C_IN("dtype", "mxfp4a8"),
|
||||
@@ -4956,11 +4966,11 @@ static const RadTunable tGemmPrefill[] = {
|
||||
};
|
||||
|
||||
/* The MXFP4 tiled GEMM's one axis: compiled arms of one template. 0 is the M crossover (TN=4 from
|
||||
* M >= 1024), which needs M. */
|
||||
* M >= 2048), which needs M. */
|
||||
static const int64_t kMxTn[] = { 0, 2, 4 };
|
||||
static const RadTunable tGemmMxfp4Tiled[] = {
|
||||
{ "tn", kMxTn, NELEM(kMxTn), 0,
|
||||
"16-column fragments per wave: 2 = 256x64 block, 4 = 256x128. 0 = TN 4 from M >= 1024" },
|
||||
"16-column fragments per wave: 2 = 256x64 block, 4 = 256x128. 0 = TN 4 from M >= 2048" },
|
||||
};
|
||||
|
||||
/* The MXFP4 decode GEMM's axes. 0 on each is the kernel's own rule (r4d_gemm_mxfp4a8_decode.hip);
|
||||
@@ -5044,25 +5054,6 @@ static const RadTunable tArTs4Lane[] = {
|
||||
{ "acq", kArAcq, NELEM(kArAcq), 0, "acquire fence after the flag read" },
|
||||
{ "ts_min", kArTsMin, NELEM(kArTsMin), 32768, "unused here (tAr says why it is declared)" },
|
||||
};
|
||||
|
||||
/* The three- and eight-rank two-shots at `overlap` 1 (cArTsExact3Lane, cArTsExact8Lane, cArTi8Ts3Lane). The
|
||||
* four-rank lane's narrow launch was measured at four ranks; at three and eight it is not, and a narrow
|
||||
* grid can also lose there: where the all-reduce is the longer of the two (a large message, slow or shared
|
||||
* links, seven peers to feed at eight ranks) the step waits on a slower collective. So by default these
|
||||
* rows launch as the wide rows do (tAr's defaults: the same grid, the same bits, the same time) and the
|
||||
* narrow launch is a deployment's choice, read once at load: R4D_AR_LANE_3_8=1 gives them the four-rank
|
||||
* lane's four blocks of 1024. Any other value, or unset, is the default. */
|
||||
static int64_t ar_lane38_nblocks() {
|
||||
const char* e = std::getenv("R4D_AR_LANE_3_8");
|
||||
return e && e[0] == '1' && !e[1] ? 4 : 0;
|
||||
}
|
||||
static const RadTunable tArTs38Lane[] = {
|
||||
{ "nblocks", kArNblocks, NELEM(kArNblocks), ar_lane38_nblocks(), "blocks in the grid. 0 = scale with the message and clamp (the wide rows' launch, the default); 4 under R4D_AR_LANE_3_8=1" },
|
||||
{ "nthreads", kArNthreads, NELEM(kArNthreads), 1024, "threads per block" },
|
||||
{ "drain", kArDrain, NELEM(kArDrain), 3, "store-drain wait before the flag: 3 = s_wait_storecnt, 1 = vmcnt" },
|
||||
{ "acq", kArAcq, NELEM(kArAcq), 0, "acquire fence after the flag read" },
|
||||
{ "ts_min", kArTsMin, NELEM(kArTsMin), 32768, "unused here (tAr says why it is declared)" },
|
||||
};
|
||||
/* The same axes with narrow defaults, for a collective issued beside compute (`overlap` 1). The
|
||||
* wide launch (up to 24 blocks of 1024 threads spinning on peer flags) suits an all-reduce that has
|
||||
* the card to itself; beside a GEMM it slows the GEMM by 1.45-2.1x. Eight blocks of 256 threads
|
||||
@@ -5201,6 +5192,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) };
|
||||
@@ -5708,6 +5701,12 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr, r4d_rad_scale, nullptr, nullptr, nullptr,
|
||||
r4d_shape_scale },
|
||||
|
||||
{ "mla_attn_rows_bf16", "mla_attn", "attn", "row-tiled WMMA latent attention, one key load for 64 heads",
|
||||
"latent_dim=512, heads=64, block_size=4",
|
||||
"bf16 operands; FP32 probabilities via high/low BF16",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY + 2, ROW(cMlaRows), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_mla_attn_rows, r4d_rad_mla_attn_matrix_scratch, nullptr, nullptr,
|
||||
r4d_shape_mla_attn },
|
||||
{ "mla_attn_matrix_bf16", "mla_attn", "attn", "head-tiled WMMA latent attention with FP32 softmax",
|
||||
"latent_dim=512, heads a multiple of 8 (heads%16==8 pairs two requests a tile), block_size=4",
|
||||
"bf16 operands; FP32 probabilities via high/low BF16",
|
||||
@@ -5866,7 +5865,8 @@ static const RadKernelInfo kKernels[] = {
|
||||
"the lm_head projection over an fp8 head: C[M, n_vocab] f32 = x[M, n_embd] @ dequant(W)^T, "
|
||||
"one wave a vocabulary row, the row's own scales in its tail",
|
||||
"any M -- one pass to 32 rows, instantiated exactly to 8 and rounded up to a multiple of 4 above "
|
||||
"it, then chunked; n_embd a multiple of 128, which is the group the scales are taken over. The "
|
||||
"it, to 64 rows in four tiles a wave, to 128 in two M halves of four that share the staged head "
|
||||
"tile, then chunked by 128; n_embd a multiple of 128, which is the group the scales are taken over. The "
|
||||
"stored row is padded to 16 bytes so the uint4 load stays aligned at any such n_embd",
|
||||
"bf16 x, E4M3 head with a bf16 scale per 128x128 tile stored PER ROW, F32 LOGITS OUT, fp32 "
|
||||
"accumulate",
|
||||
@@ -7131,21 +7131,6 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_exact, nullptr, nullptr, nullptr,
|
||||
r4d_shape_all_reduce },
|
||||
|
||||
/* The row above at `overlap` 1: the same entry point and the same arithmetic; the wide launch unless
|
||||
* R4D_AR_LANE_3_8=1 gives it the four-rank lane's narrow one (tArTs38Lane; not measured on three cards). */
|
||||
{ "ar_twoshot_3rank_exact_lane", "all_reduce", "ar",
|
||||
"all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch, beside compute; the wide "
|
||||
"launch unless R4D_AR_LANE_3_8=1 makes it narrow",
|
||||
"exactly 3 ranks, all peer-accessible, overlap 1; messages above "
|
||||
R4D_XSTR(R4D_AR_WIDE_ONESHOT_MAX_ELEMS) " elements; shards as even as the word count allows; "
|
||||
"in place; cudagraph-safe",
|
||||
"bf16 / fp16 / fp32 payload, fp32 accumulate in ascending rank order, bit-identical across ranks "
|
||||
"and to the wide row",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_AR_TWOSHOT + 1,
|
||||
ROW(cArTsExact3Lane), ROW(tArTs38Lane), nullptr,
|
||||
r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_exact, nullptr, nullptr, nullptr,
|
||||
r4d_shape_all_reduce },
|
||||
|
||||
{ "ar_twoshot_4rank_exact", "all_reduce", "ar",
|
||||
"all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch, per-block pipelined "
|
||||
"handshake",
|
||||
@@ -7185,21 +7170,6 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_8rank_exact, nullptr, nullptr, nullptr,
|
||||
r4d_shape_all_reduce },
|
||||
|
||||
/* The row above at `overlap` 1: the same entry point and the same arithmetic; the wide launch unless
|
||||
* R4D_AR_LANE_3_8=1 gives it the four-rank lane's narrow one (tArTs38Lane; not measured on eight cards). */
|
||||
{ "ar_twoshot_8rank_exact_lane", "all_reduce", "ar",
|
||||
"all-reduce (sum), reduce-scatter + all-gather over P2P IPC scratch, beside compute; the wide "
|
||||
"launch unless R4D_AR_LANE_3_8=1 makes it narrow",
|
||||
"exactly 8 ranks, all peer-accessible, overlap 1; messages above "
|
||||
R4D_XSTR(R4D_AR_WIDE_ONESHOT_MAX_ELEMS) " elements; message must shard evenly over the ranks; "
|
||||
"in place; cudagraph-safe",
|
||||
"bf16 / fp16 / fp32 payload, fp32 accumulate in ascending rank order, bit-identical across ranks "
|
||||
"and to the wide row",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_AR_TWOSHOT + 1,
|
||||
ROW(cArTsExact8Lane), ROW(tArTs38Lane), nullptr,
|
||||
r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_8rank_exact, nullptr, nullptr, nullptr,
|
||||
r4d_shape_all_reduce },
|
||||
|
||||
{ "ar_gather_2rank", "all_gather", "ar",
|
||||
"all-gather, one-shot push over P2P IPC scratch, per-block flag handshake",
|
||||
"exactly 2 ranks, peer-accessible; y is [2 * numel] with rank r's input at r * numel; "
|
||||
@@ -7327,20 +7297,6 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_ti8, nullptr, nullptr, nullptr,
|
||||
r4d_shape_all_reduce },
|
||||
|
||||
/* The row above at `overlap` 1: the same entry point and the same arithmetic; the wide launch unless
|
||||
* R4D_AR_LANE_3_8=1 gives it the four-rank lane's narrow one (tArTs38Lane; not measured on three cards). */
|
||||
{ "ar_twoshot_3rank_ti8_lane", "all_reduce", "ar",
|
||||
"all-reduce (sum), reduce-scatter + all-gather on the tiered-int8 wire -- lossy, beside compute; "
|
||||
"the wide launch unless R4D_AR_LANE_3_8=1 makes it narrow",
|
||||
"exactly 3 ranks, all peer-accessible, overlap 1; groups of 32, split over the shards as evenly as "
|
||||
"they divide; under min_bytes the exact one-shot; cudagraph-safe",
|
||||
"bf16 / fp16 payload, int8 + fp16 scale + u32 record per group on the wire, fp32 accumulate in "
|
||||
"ascending rank order, bit-identical across ranks and to the wide row",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_AR_ONESHOT + 1,
|
||||
ROW(cArTi8Ts3Lane), ROW(tArTs38Lane), nullptr,
|
||||
r4d_rad_ar_init, r4d_rad_ar_fini, r4d_rad_ar_twoshot_3rank_ti8, nullptr, nullptr, nullptr,
|
||||
r4d_shape_all_reduce },
|
||||
|
||||
{ "ar_twoshot_4rank_ti8", "all_reduce", "ar",
|
||||
"all-reduce (sum), reduce-scatter + all-gather; both hops ride the tiered-int8 wire (double "
|
||||
"quant, the owner storing the decode of its own words) -- lossy",
|
||||
@@ -9092,6 +9048,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",
|
||||
@@ -9105,6 +9068,12 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
nullptr, 0, NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_end_status },
|
||||
|
||||
{ "ras_pick", "ras_pick", "elem",
|
||||
"a repetition-aware choice between two draws a lane", "a thread a lane", "i32",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
nullptr, 0, NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_ras_pick },
|
||||
|
||||
{ "frame_store", "frame_store", "elem",
|
||||
"a frame's codes into its lane's history", "a thread a (lane, code); dst contiguous", "i32",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
|
||||
+321
-131
@@ -145,6 +145,7 @@ struct Plugin {
|
||||
void (*pin_qsel)(int) = nullptr;
|
||||
void (*pin_mla)(int) = nullptr;
|
||||
void (*mla_stats)(unsigned long long*) = nullptr;
|
||||
void (*pin_mla_rows)(int) = nullptr;
|
||||
int (*record)(const void**, int) = nullptr;
|
||||
void (*ar8_settings)(const R4DAr8Settings*) = nullptr;
|
||||
};
|
||||
@@ -213,6 +214,7 @@ static bool load(Plugin* pl, const char* path) {
|
||||
okall &= sym(pl->h, "r4d_qsa_select_pin_form", &pl->pin_qsel);
|
||||
okall &= sym(pl->h, "r4d_mla_matrix_pin_form", &pl->pin_mla);
|
||||
okall &= sym(pl->h, "r4d_mla_matrix_stats", &pl->mla_stats);
|
||||
okall &= sym(pl->h, "r4d_mla_rows_pin_form", &pl->pin_mla_rows);
|
||||
okall &= sym(pl->h, "r4d_record_dispatches", &pl->record);
|
||||
okall &= sym(pl->h, "r4d_ar8_test_settings", &pl->ar8_settings);
|
||||
ScopedAr8::apply = pl->ar8_settings;
|
||||
@@ -400,74 +402,6 @@ static void cross_check(const Plugin& pl) {
|
||||
ok("every row that needs two or more ranks requires device.p2p 1 or 0", no_p2p == 0,
|
||||
p2pdetail);
|
||||
|
||||
/* The three- and eight-rank `overlap` rows launch as their wide rows unless R4D_AR_LANE_3_8=1
|
||||
* (r4d_rows.cpp, tArTs38Lane): every axis default the wide row's, nblocks 4 under the variable.
|
||||
* The four-rank lane rows keep their measured narrow launch either way. */
|
||||
{
|
||||
const char* env = std::getenv("R4D_AR_LANE_3_8");
|
||||
const bool narrow = env && env[0] == '1' && !env[1];
|
||||
auto row = [&](const char* n) -> const RadKernelInfo* {
|
||||
for (int k = 0; k < nk; ++k)
|
||||
if (!std::strcmp(pl.kernel_at(k)->name, n)) return pl.kernel_at(k);
|
||||
return nullptr;
|
||||
};
|
||||
auto deflt = [](const RadKernelInfo* r, const char* key) -> long long {
|
||||
for (int v = 0; v < r->n_tunables; ++v)
|
||||
if (!std::strcmp(r->tunables[v].key, key)) return (long long)r->tunables[v].deflt;
|
||||
return -1;
|
||||
};
|
||||
static const char* const pairs[][2] = {
|
||||
{ "ar_twoshot_3rank_exact_lane", "ar_twoshot_3rank_exact" },
|
||||
{ "ar_twoshot_8rank_exact_lane", "ar_twoshot_8rank_exact" },
|
||||
{ "ar_twoshot_3rank_ti8_lane", "ar_twoshot_3rank_ti8" },
|
||||
};
|
||||
int bad = 0;
|
||||
std::string detail;
|
||||
for (const auto& pr : pairs) {
|
||||
const RadKernelInfo* l = row(pr[0]);
|
||||
const RadKernelInfo* w = row(pr[1]);
|
||||
if (!l || !w) { ++bad; detail += std::string(pr[0]) + " or its wide row absent; "; continue; }
|
||||
for (const char* key : { "nblocks", "nthreads", "drain", "acq" }) {
|
||||
const long long want = narrow && !std::strcmp(key, "nblocks") ? 4 : deflt(w, key);
|
||||
if (deflt(l, key) != want) {
|
||||
++bad;
|
||||
detail += std::string(pr[0]) + ":" + key + " " + std::to_string(deflt(l, key)) +
|
||||
" (want " + std::to_string(want) + "); ";
|
||||
}
|
||||
}
|
||||
}
|
||||
ok(narrow ? "the 3/8-rank overlap rows take the narrow launch (R4D_AR_LANE_3_8=1)"
|
||||
: "the 3/8-rank overlap rows launch as their wide rows (R4D_AR_LANE_3_8 unset)",
|
||||
bad == 0, detail);
|
||||
/* And they accept every pin their wide rows accept: a RADIANCE_TUNE all_reduce pin is applied
|
||||
* to every row serving the op, and a key or value a serving row lacks fails the selection. */
|
||||
int bad_axis = 0;
|
||||
std::string axis_detail;
|
||||
for (const auto& pr : pairs) {
|
||||
const RadKernelInfo* l = row(pr[0]);
|
||||
const RadKernelInfo* w = row(pr[1]);
|
||||
if (!l || !w) continue;
|
||||
for (int v = 0; v < w->n_tunables; ++v) {
|
||||
const RadTunable& wt = w->tunables[v];
|
||||
const RadTunable* lt = nullptr;
|
||||
for (int u = 0; u < l->n_tunables; ++u)
|
||||
if (!std::strcmp(l->tunables[u].key, wt.key)) lt = &l->tunables[u];
|
||||
if (!lt) { ++bad_axis; axis_detail += std::string(pr[0]) + " lacks " + wt.key + "; "; continue; }
|
||||
for (int i = 0; i < wt.n_values; ++i) {
|
||||
bool has = false;
|
||||
for (int j = 0; j < lt->n_values; ++j) has = has || lt->values[j] == wt.values[i];
|
||||
if (!has) {
|
||||
++bad_axis;
|
||||
axis_detail += std::string(pr[0]) + ":" + wt.key + " lacks " +
|
||||
std::to_string((long long)wt.values[i]) + "; ";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ok("the 3/8-rank overlap rows accept every tunable pin of their wide rows", bad_axis == 0,
|
||||
axis_detail);
|
||||
}
|
||||
|
||||
/* The libr4d orderings the integer priority has to preserve. */
|
||||
int p_ts = -1, p_os = -1, p_dec = -1, p_pre = -1, p_m16 = -1, p_m64 = -1, p_bt = -1;
|
||||
int p_g2dec = -1, p_g2pre = -1, p_g2win = -1;
|
||||
@@ -6256,18 +6190,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);
|
||||
@@ -6275,27 +6219,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)];
|
||||
@@ -6428,10 +6376,14 @@ void snake_cases(const Plugin& pl) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Picks whose nucleus ended inside the top k, so that a top_p case is known to test the cut. */
|
||||
int64_t g_topp_cuts = 0;
|
||||
|
||||
/* ref_cfg_topk_sample's rule on the host, for one lane, over f32 logits. */
|
||||
int ref_pick(const std::vector<float>& lg, int64_t V, int64_t lane_row, int rep, float cfg,
|
||||
int64_t K, int64_t cond_k, float temp, int64_t sup_lo, int64_t sup_hi, float pen,
|
||||
std::vector<int32_t>* hist, int64_t hb, uint64_t seed, int32_t frame, uint64_t stream) {
|
||||
std::vector<int32_t>* hist, int64_t hb, uint64_t seed, int32_t frame, uint64_t stream,
|
||||
float top_p = 1.0f) {
|
||||
std::vector<float> c((size_t)V), g((size_t)V);
|
||||
for (int64_t j = 0; j < V; ++j) c[(size_t)j] = lg[(size_t)(rep * lane_row * V + j)];
|
||||
if (rep == 2) {
|
||||
@@ -6464,11 +6416,28 @@ int ref_pick(const std::vector<float>& lg, int64_t V, int64_t lane_row, int rep,
|
||||
if (open(j)) idx.push_back(j);
|
||||
}
|
||||
if (idx.empty()) return -1;
|
||||
const int64_t kk = std::min<int64_t>(K, (int64_t)idx.size());
|
||||
double z = 0;
|
||||
if (top_p < 1.0f) {
|
||||
float gm = g[(size_t)idx[0]];
|
||||
for (const int64_t j : idx) gm = std::max(gm, g[(size_t)j]);
|
||||
for (const int64_t j : idx) z += std::exp((double)(g[(size_t)j] / temp - gm / temp));
|
||||
}
|
||||
int64_t kk = std::min<int64_t>(K, (int64_t)idx.size());
|
||||
std::partial_sort(idx.begin(), idx.begin() + kk, idx.end(), [&](int64_t p, int64_t q) {
|
||||
return g[(size_t)p] > g[(size_t)q] || (g[(size_t)p] == g[(size_t)q] && p < q);
|
||||
});
|
||||
const float m = g[(size_t)idx[0]] / temp;
|
||||
if (top_p < 1.0f) {
|
||||
double cum = 0;
|
||||
int64_t keep = 1;
|
||||
for (int64_t t = 0; t < kk; ++t) {
|
||||
cum += std::exp((double)(g[(size_t)idx[(size_t)t]] / temp - m)) / z;
|
||||
if (t > 0 && cum > (double)top_p) break;
|
||||
keep = t + 1;
|
||||
}
|
||||
if (keep < kk) ++g_topp_cuts;
|
||||
kk = keep;
|
||||
}
|
||||
double tot = 0;
|
||||
std::vector<double> e((size_t)kk);
|
||||
for (int64_t t = 0; t < kk; ++t) {
|
||||
@@ -6493,16 +6462,26 @@ int ref_pick(const std::vector<float>& lg, int64_t V, int64_t lane_row, int rep,
|
||||
void cfg_topk_cases(const Plugin& pl) {
|
||||
const RadKernelInfo* r = find(pl, "cfg_topk_sample");
|
||||
if (!r) { ok("cfg_topk_sample", false, "row absent"); return; }
|
||||
struct C { int64_t V; int rep; bool hist, cfgs; int64_t sup_lo, sup_hi, cond_k; float temp; };
|
||||
struct C {
|
||||
int64_t V; int rep; bool hist, cfgs; int64_t sup_lo, sup_hi, cond_k; float temp;
|
||||
float top_p = 1.0f; int64_t K = 50;
|
||||
};
|
||||
/* The last two are Fish Audio's draws (arch/fishomni): its main pick over eos and 4096
|
||||
* semantic ids at temperature 0.7, nucleus 0.7, top 30, and its repetition-aware redraw at 1.0,
|
||||
* 0.95, 30. */
|
||||
for (const C& c : { C{ 2052, 2, true, true, 2048, 2051, 0, 0.9f },
|
||||
C{ 2051, 1, true, false, 2048, 2051, 0, 0.9f },
|
||||
C{ 2051, 2, false, true, 2048, 2051, 0, 0.9f },
|
||||
C{ 16385, 2, false, false, 0, 0, 50, 1.0f } }) {
|
||||
const int64_t n = 3, lanes = 4, K = 50;
|
||||
const std::string nm = fmt("cfg_topk_sample V %lld rep %d%s%s sup [%lld, %lld) cond_k %lld",
|
||||
C{ 16385, 2, false, false, 0, 0, 50, 1.0f },
|
||||
C{ 4097, 1, false, false, 0, 0, 0, 0.7f, 0.7f, 30 },
|
||||
C{ 4097, 1, false, false, 0, 0, 0, 1.0f, 0.95f, 30 } }) {
|
||||
const int64_t n = 3, lanes = 4, K = c.K;
|
||||
const std::string nm = fmt("cfg_topk_sample V %lld rep %d%s%s sup [%lld, %lld) cond_k %lld"
|
||||
" top_p %g k %lld",
|
||||
(long long)c.V, c.rep, c.hist ? " hist" : "",
|
||||
c.cfgs ? " per-lane cfg" : "", (long long)c.sup_lo,
|
||||
(long long)c.sup_hi, (long long)c.cond_k);
|
||||
(long long)c.sup_hi, (long long)c.cond_k, (double)c.top_p,
|
||||
(long long)K);
|
||||
std::vector<int32_t> lane = { 2, 0, 3 };
|
||||
std::vector<float> cfgv = { 1.0f, 3.0f, 4.0f };
|
||||
std::vector<uint32_t> seed = { 7u, 0u, 0x9e3779b9u, 1u, 0xdeadbeefu, 0x12345u };
|
||||
@@ -6525,6 +6504,9 @@ void cfg_topk_cases(const Plugin& pl) {
|
||||
for (size_t i = 0; i < lg.size(); ++i) {
|
||||
const float v = rnd() * 6.0f;
|
||||
lg[i] = (i % 97 == 0) ? 5.95f : v;
|
||||
/* About 19 peaks a row hold most of the mass, so the nucleus ends inside the top
|
||||
* 30 and the cut is what decides the pick. */
|
||||
if (c.top_p < 1.0f && i % 211 == 5) lg[i] = 12.0f + 4.0f * rnd();
|
||||
}
|
||||
void* dlg = up_f32(lg);
|
||||
std::vector<int32_t> frame((size_t)n, f);
|
||||
@@ -6537,7 +6519,7 @@ void cfg_topk_cases(const Plugin& pl) {
|
||||
PI("rep", c.rep), PI("cond_k", c.cond_k), PF("cfg", 1.5),
|
||||
PF("temp", c.temp), PI("stream", 3), PS("dtype", "f32"),
|
||||
PI("sup_lo", c.sup_lo), PI("sup_hi", c.sup_hi),
|
||||
PF("penalty", 1.1) };
|
||||
PF("penalty", 1.1), PF("top_p", c.top_p) };
|
||||
const int rc = launch(r, { T(dlg, RAD_F32, { c.rep * n, c.V }), T(dseed, RAD_U32, { n, 2 }),
|
||||
T(dfr, RAD_I32, { n }), T(dids, RAD_I32, { c.rep * n }), tcfg,
|
||||
tlane, thist },
|
||||
@@ -6550,7 +6532,8 @@ void cfg_topk_cases(const Plugin& pl) {
|
||||
const int want = ref_pick(lg, c.V, i, c.rep, c.cfgs ? cfgv[(size_t)i] : 1.5f, K,
|
||||
c.cond_k, c.temp, c.sup_lo, c.sup_hi, 1.1f,
|
||||
c.hist ? &hist : nullptr,
|
||||
c.hist ? lane[(size_t)i] * c.V : -1, sd, f, 3);
|
||||
c.hist ? lane[(size_t)i] * c.V : -1, sd, f, 3,
|
||||
c.top_p);
|
||||
for (int t = 0; t < c.rep; ++t)
|
||||
if (got[(size_t)(c.rep * i + t)] != want) ++wrong;
|
||||
}
|
||||
@@ -6558,12 +6541,64 @@ void cfg_topk_cases(const Plugin& pl) {
|
||||
}
|
||||
ok((nm + " picks").c_str(), wrong == 0, fmt("%lld of %lld picks differ", (long long)wrong,
|
||||
(long long)(4 * c.rep * n)));
|
||||
if (c.top_p < 1.0f) {
|
||||
ok((nm + " cuts").c_str(), g_topp_cuts > 0, "the nucleus never ended inside the top k");
|
||||
g_topp_cuts = 0;
|
||||
}
|
||||
if (c.hist)
|
||||
ok((nm + " history").c_str(), hist_wrong == 0,
|
||||
fmt("%lld history entries differ", (long long)hist_wrong));
|
||||
}
|
||||
}
|
||||
|
||||
/* ras_pick over four picks of three lanes, the first clearing their windows: the picks and the
|
||||
* windows against libref's rule on the host, exactly. Draws repeat often (a band of 6 ids) so the
|
||||
* redraw is exercised, and one is outside [lo, hi) (never redrawn). */
|
||||
void ras_pick_cases(const Plugin& pl) {
|
||||
const RadKernelInfo* r = find(pl, "ras_pick");
|
||||
if (!r) { ok("ras_pick", false, "row absent"); return; }
|
||||
const int64_t n = 3, lanes = 4, W = 4, lo = 1, hi = 6;
|
||||
std::vector<int32_t> lane = { 2, 0, 3 };
|
||||
std::vector<int32_t> hist((size_t)(lanes * W), 5);
|
||||
void* dlane = up_i32(lane);
|
||||
void* dhist = up_i32(hist);
|
||||
void* dids = dalloc((size_t)n * 4);
|
||||
int64_t wrong = 0;
|
||||
for (int step = 0; step < 4; ++step) {
|
||||
std::vector<int32_t> first((size_t)n), alt((size_t)n);
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
first[(size_t)i] = (int32_t)((step * 2 + i * 3) % 7); /* 0 is outside [lo, hi) */
|
||||
alt[(size_t)i] = (int32_t)(10 + step * 3 + i);
|
||||
}
|
||||
void* df = up_i32(first);
|
||||
void* da = up_i32(alt);
|
||||
const int clear = step == 0 ? 1 : 0;
|
||||
std::vector<RadParam> ps = { PI("M", n), PI("win", W), PI("lo", lo), PI("hi", hi),
|
||||
PI("rep", 1), PI("clear", clear) };
|
||||
const int rc = launch(r, { T(df, RAD_I32, { n }), T(da, RAD_I32, { n }),
|
||||
T(dlane, RAD_I32, { n }), T(dhist, RAD_I32, { lanes, W }),
|
||||
T(dids, RAD_I32, { n }) },
|
||||
ps);
|
||||
if (rc != RAD_OK) { ok("ras_pick", false, "rc " + std::to_string(rc)); return; }
|
||||
const std::vector<int32_t> got = down_i32(dids, (size_t)n);
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
int32_t* h = hist.data() + lane[(size_t)i] * W;
|
||||
if (clear) for (int64_t w = 0; w < W; ++w) h[w] = -1;
|
||||
int32_t pick = first[(size_t)i];
|
||||
if (pick >= lo && pick < hi)
|
||||
for (int64_t w = 0; w < W; ++w)
|
||||
if (h[w] == pick) { pick = alt[(size_t)i]; break; }
|
||||
for (int64_t w = 0; w + 1 < W; ++w) h[w] = h[w + 1];
|
||||
h[W - 1] = pick;
|
||||
if (got[(size_t)i] != pick) ++wrong;
|
||||
}
|
||||
}
|
||||
ok("ras_pick picks", wrong == 0, fmt("%lld of %lld picks differ", (long long)wrong,
|
||||
(long long)(4 * n)));
|
||||
ok("ras_pick windows", differ(down_i32(dhist, hist.size()), hist) == 0,
|
||||
"the windows differ from libref's rule");
|
||||
}
|
||||
|
||||
/* rvq_embed: libref's two roundings, the single rounding over bf16 and f32 tables into a bf16 or
|
||||
* f32 row, codes read as a block of a wider frame-major history (strides other than the dense
|
||||
* plane's), and a first code outside its table leaving the row unwritten. Exact. */
|
||||
@@ -6571,9 +6606,12 @@ void rvq_embed_cases(const Plugin& pl) {
|
||||
const RadKernelInfo* r = find(pl, "rvq_embed_bf16");
|
||||
if (!r) { ok("rvq_embed_bf16", false, "row absent"); return; }
|
||||
const int64_t D = 96, K = 5, voc = 11, off0 = 3, rows = 6, lanes = 2, stride = 9;
|
||||
struct C { int round1; bool f32tab, f32y, block; };
|
||||
/* The last case clamps every code at 8 (`clamp`): codes 8..10 read row 7, and the later code
|
||||
* past t1 reads its codebook's row 7 instead of nothing. */
|
||||
struct C { int round1; bool f32tab, f32y, block; int clamp = 0; };
|
||||
for (const C& c : { C{ 0, false, false, false }, C{ 1, false, false, false },
|
||||
C{ 1, true, true, true }, C{ 1, false, true, true } }) {
|
||||
C{ 1, true, true, true }, C{ 1, false, true, true },
|
||||
C{ 0, false, false, false, 8 } }) {
|
||||
std::vector<float> t0 = randv((size_t)((voc + off0) * D), 1.0f);
|
||||
std::vector<float> t1 = randv((size_t)((K - 1) * voc * D), 1.0f);
|
||||
void* d0 = c.f32tab ? up_f32(t0) : up_bf16(t0);
|
||||
@@ -6607,10 +6645,12 @@ void rvq_embed_cases(const Plugin& pl) {
|
||||
const float scale = 0.5f;
|
||||
std::vector<RadParam> ps = { PI("M", rows), PI("n_embd", D), PI("n_codes", K),
|
||||
PI("vocab", voc), PI("off0", off0), PF("scale", scale),
|
||||
PS("dtype", c.f32y ? "f32" : "bf16"), PI("round", c.round1) };
|
||||
const std::string nm = fmt("rvq_embed_bf16 round %d %s tables -> %s%s", c.round1,
|
||||
PS("dtype", c.f32y ? "f32" : "bf16"), PI("round", c.round1),
|
||||
PI("clamp", c.clamp) };
|
||||
const std::string nm = fmt("rvq_embed_bf16 round %d %s tables -> %s%s clamp %d", c.round1,
|
||||
c.f32tab ? "f32" : "bf16", c.f32y ? "f32" : "bf16",
|
||||
c.block ? ", codes a history block" : "");
|
||||
c.block ? ", codes a history block" : "", c.clamp);
|
||||
auto cl = [&](int64_t v) { return c.clamp > 0 && v > c.clamp - 1 ? c.clamp - 1 : v; };
|
||||
const int rc = launch(r, { tc, T(d0, c.f32tab ? RAD_F32 : RAD_BF16, { voc + off0, D }),
|
||||
T(d1, c.f32tab ? RAD_F32 : RAD_BF16, { (K - 1) * voc, D }),
|
||||
T(dy, ydt, { rows, D }) },
|
||||
@@ -6618,13 +6658,13 @@ void rvq_embed_cases(const Plugin& pl) {
|
||||
if (rc != RAD_OK) { ok(nm.c_str(), false, "rc " + std::to_string(rc)); continue; }
|
||||
std::vector<float> want = sentinel;
|
||||
for (int64_t i = 0; i < rows; ++i) {
|
||||
const int64_t c0 = code[(size_t)i] + off0;
|
||||
const int64_t c0 = cl(code[(size_t)i]) + off0;
|
||||
if (c0 < 0 || c0 >= voc + off0) continue;
|
||||
for (int64_t d = 0; d < D; ++d) {
|
||||
const float e0 = t0[(size_t)(c0 * D + d)];
|
||||
float acc = c.round1 ? e0 : 0.0f;
|
||||
for (int64_t k = 1; k < K; ++k) {
|
||||
const int64_t ck = code[(size_t)(k * rows + i)] + (k - 1) * voc;
|
||||
const int64_t ck = cl(code[(size_t)(k * rows + i)]) + (k - 1) * voc;
|
||||
if (ck >= 0 && ck < (K - 1) * voc) acc += t1[(size_t)(ck * D + d)];
|
||||
}
|
||||
const float s = c.round1 ? acc : bfr(e0 + bfr(acc));
|
||||
@@ -6638,6 +6678,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) {
|
||||
@@ -9894,36 +10032,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);
|
||||
@@ -9932,14 +10077,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)];
|
||||
@@ -12903,6 +13048,8 @@ static void fp8_real(const Plugin& pl, const char* file, long long woff, long lo
|
||||
static void perf_row(const Plugin& pl, const char* name, std::vector<RadTensor> ts,
|
||||
std::vector<RadParam> ps, void* wbase, size_t wbytes, int copies,
|
||||
double bytes, int iters, int widx = 2, const char* point = nullptr) {
|
||||
/* R4D_PERF_ONLY=<row>: time that row alone (a sweep over one row skips the rest). */
|
||||
if (const char* only = std::getenv("R4D_PERF_ONLY"); only && *only && std::strcmp(only, name)) return;
|
||||
const RadKernelInfo* r = find(pl, name);
|
||||
if (!r) { std::printf(" %-22s row absent\n", name); return; }
|
||||
/* The label is the row, or the tuned point in a sweep. `ps` already carries that point; this
|
||||
@@ -14599,25 +14746,28 @@ static void mla_check(const Plugin& pl, int64_t rows, int64_t heads, int64_t blo
|
||||
const int rc = oracle->launch(&args, nullptr);
|
||||
ok("MLA host oracle", rc == RAD_OK);
|
||||
args.t = device.data();
|
||||
/* The head-tiled row serves tiles of 16 heads or of 8 heads from two requests (cMlaMatrix);
|
||||
* other head counts must be refused rather than computed. */
|
||||
if (std::strstr(name, "matrix") && heads % 8) {
|
||||
/* The head-tiled row serves tiles of 16 heads or of 8 heads from two requests (cMlaMatrix),
|
||||
* the rows row groups of 32 or 64 heads; other head counts must be refused rather than
|
||||
* computed. */
|
||||
const bool by_rows = std::strstr(name, "rows") != nullptr;
|
||||
auto* const pin = by_rows ? pl.pin_mla_rows : pl.pin_mla;
|
||||
if ((std::strstr(name, "matrix") && heads % 8) || (by_rows && heads % 32)) {
|
||||
args.scratch = nullptr;
|
||||
args.scratch_bytes = 0;
|
||||
const int grc = row->launch(&args, nullptr);
|
||||
st_stream_sync(nullptr);
|
||||
ok(fmt("MLA %s refuses heads=%lld (not a multiple of 8) with RAD_E_UNSUPPORTED, no launch",
|
||||
name, (long long)heads)
|
||||
ok(fmt("MLA %s refuses heads=%lld (not a multiple of %d) with RAD_E_UNSUPPORTED, no launch",
|
||||
name, (long long)heads, by_rows ? 32 : 8)
|
||||
.c_str(),
|
||||
grc == RAD_E_UNSUPPORTED);
|
||||
dlclose(ref);
|
||||
return;
|
||||
}
|
||||
if (form && pl.pin_mla) pl.pin_mla(form);
|
||||
if (form && pin) pin(form);
|
||||
const int64_t need = row->scratch ? row->scratch(&args) : 0;
|
||||
void* scratch = need > 0 ? dalloc((size_t)need + 256) : nullptr;
|
||||
if (need <= 0 || !scratch) {
|
||||
if (form && pl.pin_mla) pl.pin_mla(0);
|
||||
if (form && pin) pin(0);
|
||||
ok("MLA scratch allocation", false);
|
||||
dlclose(ref);
|
||||
return;
|
||||
@@ -14686,7 +14836,7 @@ static void mla_check(const Plugin& pl, int64_t rows, int64_t heads, int64_t blo
|
||||
versus, base, name, base / us, shape);
|
||||
}
|
||||
}
|
||||
if (form && pl.pin_mla) pl.pin_mla(0);
|
||||
if (form && pin) pin(0);
|
||||
dlclose(ref);
|
||||
}
|
||||
|
||||
@@ -14796,17 +14946,19 @@ static void mla_perf(const Plugin& pl, int64_t M, int64_t blocks, int64_t heads,
|
||||
PS("dtype", "bf16") };
|
||||
std::vector<uint16_t> base, rule;
|
||||
auto run = [&](const char* name, int form, const char* label) {
|
||||
const bool by_rows = std::strstr(name, "rows") != nullptr;
|
||||
auto* const pin = by_rows ? pl.pin_mla_rows : pl.pin_mla;
|
||||
const RadKernelInfo* r = find(pl, name);
|
||||
if (!r) {
|
||||
std::printf(" %-34s row absent\n", label);
|
||||
return;
|
||||
}
|
||||
if (form >= 0) {
|
||||
if (!pl.pin_mla) {
|
||||
if (!pin) {
|
||||
std::printf(" %-34s no pin hook\n", label);
|
||||
return;
|
||||
}
|
||||
pl.pin_mla(form);
|
||||
pin(form);
|
||||
}
|
||||
RadArgs a;
|
||||
std::memset(&a, 0, sizeof(a));
|
||||
@@ -14823,7 +14975,7 @@ static void mla_perf(const Plugin& pl, int64_t M, int64_t blocks, int64_t heads,
|
||||
const int rc = r->launch(&a, nullptr);
|
||||
if (rc != RAD_OK || st_stream_sync(nullptr) != hipSuccess) {
|
||||
std::printf(" %-34s rc=%d (shape not served)\n", label, rc);
|
||||
if (form >= 0) pl.pin_mla(0);
|
||||
if (form >= 0) pin(0);
|
||||
return;
|
||||
}
|
||||
std::vector<uint16_t> got(qh.size());
|
||||
@@ -14844,7 +14996,7 @@ static void mla_perf(const Plugin& pl, int64_t M, int64_t blocks, int64_t heads,
|
||||
st_ev_destroy(w1);
|
||||
}
|
||||
const double us = st_time_us([&](int) { r->launch(&a, nullptr); }, iters, nullptr);
|
||||
if (form >= 0) pl.pin_mla(0);
|
||||
if (form >= 0) pin(0);
|
||||
char diff[192] = "reference";
|
||||
if (base.empty())
|
||||
base = got;
|
||||
@@ -14870,7 +15022,7 @@ static void mla_perf(const Plugin& pl, int64_t M, int64_t blocks, int64_t heads,
|
||||
}
|
||||
if (form == 0) rule = got;
|
||||
std::printf(" %-34s %10.2f us %7.2f TFLOP/s %s\n", label, us, flops / (us * 1e6), diff);
|
||||
if (form >= 0 && pl.mla_stats) {
|
||||
if (form >= 0 && !by_rows && pl.mla_stats) {
|
||||
/* One more launch with the conflict counters on, outside the timing. */
|
||||
ScopedEnv counting("RADIANCE_MLA_CONFLICT_STATS", "1");
|
||||
unsigned long long v[3];
|
||||
@@ -14899,6 +15051,11 @@ static void mla_perf(const Plugin& pl, int64_t M, int64_t blocks, int64_t heads,
|
||||
run("mla_attn_matrix_bf16", 0, "matrix 16 heads, vector load");
|
||||
run("mla_attn_matrix_bf16", 1, "matrix 16 heads, element load");
|
||||
}
|
||||
if (heads == 64) {
|
||||
run("mla_attn_rows_bf16", 0, "rows 64 heads, vector load");
|
||||
run("mla_attn_rows_bf16", 1, "rows 64 heads, element load");
|
||||
run("mla_attn_rows_bf16", 2, "rows 32 heads, vector load");
|
||||
}
|
||||
}
|
||||
|
||||
static void kda_check(const Plugin& pl, const char* mode, bool cold, int accepted_count,
|
||||
@@ -18321,7 +18478,7 @@ static void fp8_perf(const Plugin& pl, int64_t N, int64_t K, int64_t M, int iter
|
||||
/* The lm head, a decode GEMM too. Its weight is [N][K + 2*nkb] E4M3 with each row's bf16
|
||||
* scales in its tail (r4d_fp8_layout.cpp), so it has its own buffer, and the weight is the
|
||||
* second operand. */
|
||||
if (M <= 64) {
|
||||
if (M <= 256) {
|
||||
const int64_t lstride = r4d_fp8::lm_row_bytes(K);
|
||||
const size_t lwb = (size_t)N * (size_t)lstride;
|
||||
int lcopies = (int)(((size_t)768 << 20) / lwb) + 1;
|
||||
@@ -26982,6 +27139,12 @@ static void device_phase(const Plugin& pl) {
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 300, 272, 640, 2, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 300, 400, 640, 4, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 2100, 512, 2560, 0, true);
|
||||
/* The plain kernel at a whole row of blocks (not the part kernel, which takes a last row of
|
||||
* 1..160 under 1024 rows): TN=4 forced and by the rule from 1024 rows, and TN=2 at the
|
||||
* 64-wide slab (K not a multiple of 128). */
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 256, 400, 640, 4, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 1024, 4096, 640, 0, true);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 512, 272, 576, 2, true);
|
||||
/* K not a multiple of 128: the TN=2 kernel takes the 64-wide slab. */
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 300, 272, 576, 2);
|
||||
mxfp4a8_numeric(pl, "gemm_mxfp4a8_tiled", 300, 272, 576, 2, true);
|
||||
@@ -27394,7 +27557,9 @@ static void device_phase(const Plugin& pl) {
|
||||
if (sel("conv_t1d_bf16")) au::conv_t1d_cases(pl);
|
||||
if (sel("snake_bf16")) au::snake_cases(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("sub_i32")) au::sub_i32_cases(pl);
|
||||
@@ -27721,6 +27886,26 @@ static void device_phase(const Plugin& pl) {
|
||||
for (int64_t heads : { 8, 16 }) mla_project_check(pl, rows, heads, mode, true);
|
||||
}
|
||||
}
|
||||
if (sel("mla_attn_rows_bf16")) {
|
||||
/* A rank's run of rows with all 64 heads (GLM's prefill split by rows), against the matrix
|
||||
* row on the same operands. Under 32 rows the rule runs the matrix kernel, so the rows
|
||||
* kernel is pinned there (form 3). */
|
||||
for (int requests : { 1, 2, 7, 33 })
|
||||
for (int64_t blocks : { 17, 513 })
|
||||
mla_check(pl, requests, 64, blocks, "mla_attn_rows_bf16", MlaFill::ragged,
|
||||
"mla_attn_matrix_bf16", requests < 32 ? 3 : 0);
|
||||
mla_check(pl, 7, 64, 513, "mla_attn_rows_bf16", MlaFill::full, "mla_attn_matrix_bf16", 3);
|
||||
mla_check(pl, 2, 64, 513, "mla_attn_rows_bf16", MlaFill::prefill, "mla_attn_matrix_bf16", 3);
|
||||
mla_check(pl, 33, 64, 513, "mla_attn_rows_bf16", MlaFill::prefill, "mla_attn_matrix_bf16");
|
||||
for (int64_t swaps : { 0, 4, 16, 64 })
|
||||
mla_check(pl, 33, 64, 65, "mla_attn_rows_bf16", MlaFill::topk, "mla_attn_matrix_bf16",
|
||||
0, swaps);
|
||||
mla_check(pl, 7, 64, 513, "mla_attn_rows_bf16", MlaFill::prefill, nullptr, 1);
|
||||
mla_check(pl, 33, 64, 65, "mla_attn_rows_bf16", MlaFill::topk, nullptr, 2, 16);
|
||||
mla_check(pl, 7, 64, 65, "mla_attn_rows_bf16", MlaFill::topk, "mla_attn_matrix_bf16", 0, 4);
|
||||
mla_check(pl, 7, 32, 65, "mla_attn_rows_bf16", MlaFill::ragged, nullptr, 3);
|
||||
mla_check(pl, 4, 40, 17, "mla_attn_rows_bf16");
|
||||
}
|
||||
if (sel("mla_attn_matrix_bf16")) {
|
||||
mla_check(pl, 7, 16, 17, "mla_attn_matrix_bf16");
|
||||
mla_check(pl, 33, 16, 17, "mla_attn_matrix_bf16");
|
||||
@@ -28009,6 +28194,11 @@ static void device_phase(const Plugin& pl) {
|
||||
for (const int64_t M : { (int64_t)1, (int64_t)8, (int64_t)13, (int64_t)32, (int64_t)33,
|
||||
(int64_t)64 })
|
||||
fp8_logits_numeric(pl, 5120, 5120, M);
|
||||
/* Past 64 rows a pass is the two-half tile (MH = 2), whose second half may be partial; past
|
||||
* 128 a second pass follows. */
|
||||
for (const int64_t M : { (int64_t)65, (int64_t)80, (int64_t)128, (int64_t)150 })
|
||||
fp8_logits_numeric(pl, 5120, 5120, M);
|
||||
fp8_logits_numeric(pl, 2560, 2560, 100);
|
||||
/* And K = 2560, the padded row: its stored width 2600 is not a multiple of 16, so
|
||||
* r4d_fp8_lm.h rounds it to 2608 (5120's 5200 needs no pad). M = 1 takes the matvec, M = 8
|
||||
* the WMMA tile, which index the row differently. */
|
||||
@@ -28024,7 +28214,7 @@ static void device_phase(const Plugin& pl) {
|
||||
}
|
||||
/* The int8 head over the same stored row: both forms, and the padded K = 2560 row. */
|
||||
if (sel("logits_gemm_i8") || sel("logit_rerank_i8")) {
|
||||
for (const int64_t M : { (int64_t)1, (int64_t)8, (int64_t)33, (int64_t)64 })
|
||||
for (const int64_t M : { (int64_t)1, (int64_t)8, (int64_t)33, (int64_t)64, (int64_t)100 })
|
||||
fp8_logits_numeric(pl, 5120, 5120, M, true);
|
||||
for (const int64_t M : { (int64_t)1, (int64_t)8 })
|
||||
fp8_logits_numeric(pl, 2560, 2560, M, true);
|
||||
|
||||
Reference in new issue
Block a user