From ec1e830c43cd11b6f20ff06413f2964ae28a5717 Mon Sep 17 00:00:00 2001 From: baKon Date: Fri, 16 Oct 2026 11:15:24 -0500 Subject: [PATCH] libr4d: tiled runtime-LoRA rows over row tiles, preferred over the scalar rows The v1 rows grid by row: shrink is one block a row walking K serially, and expand one block a (row, 256 columns) that re-reads all of B[s] for every row, so a prefill chunk reads it M times and a chunk of base rows still launches M * ceil(N/256) blocks that read a slot and leave. lora_shrink_wmma (bf16 a) and lora_shrink_wmma_q (fp8 / i8 codes whose scale group is whole 32-wide steps) run WMMA bf16 over 16-row x 16-j tiles: K in 32-wide steps over 4 waves, the waves summed in fixed order in LDS, and a K split fixed by K and R alone (sh_plan) whose partials live in the op's scratch and are summed in split order by the last block to arrive; the arrival counters are a small block keyed on (device, t). Codes are exact in bf16 and each step's partial is scaled by its group's f32 scale; f16 / f32 activations and other groups stay on the scalar kernel. lora_expand_tiled stages B[s]'s [128 columns, 32 j] slab in LDS as f32 for a 32-row tile and keeps v1's per-output sequence (j ascending, no FMA), so its bytes are libref's. Both grid by row tiles; a tile of base rows leaves on one ballot before any barrier or operand load, a mixed tile runs one pass per distinct slot, and a small grid spreads a tile's passes over blockIdx.z. No choice depends on M but which block runs a pass. The scalar rows stay, by name and as the f16 / f32 shrink, below R4D_PRIO_LORA_TILED. --- docs/OPS.md | 5 +- libr4d/README.md | 2 +- libr4d/r4d_lora_bf16.hip | 650 +++++++++++++++++++++++++++++++++++++-- libr4d/r4d_plugin.h | 14 +- libr4d/r4d_rows.cpp | 48 ++- tools/kbench_core.cpp | 7 +- 6 files changed, 690 insertions(+), 36 deletions(-) diff --git a/docs/OPS.md b/docs/OPS.md index 70836c43..7100a23b 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -654,7 +654,10 @@ tensor's. The pools are ordinary inputs and not weights: the engine owns them as and copies an adapter into a slot between steps, so a recorded pass replays the same pointers and reads whatever adapters the slots hold. A row's result does not depend on `M` or on any other row (no split of a reduction that moves with the batch), which is what lets a request's bytes be the -same alone and in a mixed batch. Implementations: libref, libavx, libr4d (scalar f32 rows). +same alone and in a mixed batch. Implementations: libref, libavx, libr4d (scalar f32 rows, and +tiled rows over row tiles that outrank them: a WMMA shrink whose summation order is fixed by `K` +and `R`, and an expand that stages `lora_b[s]` in LDS for a tile of rows and computes libref's +bytes). ## Attention diff --git a/libr4d/README.md b/libr4d/README.md index a922c365..e5976422 100644 --- a/libr4d/README.md +++ b/libr4d/README.md @@ -59,7 +59,7 @@ The table lists a selection; `r4d_rows.cpp` and `r4d.h` hold the full set. | `dflash_conv_t2_g16_bf16` | the drafter's causal conv, two taps, 16 groups | | `dflash_select_bf16` | the drafter's candidate selector: edge scores and the greedy path walk | | `dequant_w4_bf16` | unpacks a 4-bit fragment-ordered weight back to bf16 | -| `lora_shrink` / `lora_expand` | runtime LoRA's two halves beside a trunk GEMM, each row's adapter read from a per-row slot on the device (base rows untouched): `t = x A[slot]^T` one block a row, `y += scale ⊙ (t B[slot]^T)` one thread an output; scalar f32, a row's bytes independent of M | +| `lora_shrink` / `lora_expand` | runtime LoRA's two halves beside a trunk GEMM, each row's adapter read from a per-row slot on the device (base rows untouched). Tiled rows (preferred): `t = x A[slot]^T` WMMA bf16 over 16-row x 16-j tiles with a K split fixed by K and R (`lora_shrink_wmma`, `lora_shrink_wmma_q` for fp8/i8 codes), `y += scale ⊙ (t B[slot]^T)` over 32-row x 128-column tiles with B[slot] staged in LDS, libref's bytes (`lora_expand_tiled`); a tile of base rows leaves on one ballot, a mixed tile runs one pass a slot. Scalar v1 rows (`_scalar`): one block a row / one thread an output, and the f16/f32-activation shrink. A row's bytes are independent of M in every row | | `quant_act_i8` / `quant_act_i8_asum` | per-row int8 activation quantiser, in the r4d GEMMs' fragment byte order; the `_asum` form also emits the per-group row sums the asymmetric GEMMs need | | `had_quant_act_i8` | the same with a block Walsh-Hadamard rotation first, which is what makes one scale per row viable | | `rmsnorm_had_quant_i8` | residual add, RMS norm, rotate and quantise in one kernel | diff --git a/libr4d/r4d_lora_bf16.hip b/libr4d/r4d_lora_bf16.hip index f5984e01..802156b4 100644 --- a/libr4d/r4d_lora_bf16.hip +++ b/libr4d/r4d_lora_bf16.hip @@ -33,11 +33,43 @@ * twice (-ffp-contract=off, RadAddPlugin.cmake), so expand computes libref's bytes and shrink * differs from them only by its summation order. * + * V2, THE TILED ROWS (lora_shrink_wmma, lora_shrink_wmma_q, lora_expand_tiled), below the v1 + * kernels. v1's grid is a ROW: shrink is one block a row walking K serially, expand one block a + * (row, 256 columns) re-reading all of B[s] for every row, and a chunk of base rows still launches + * M * ceil(N/256) blocks that read a slot and leave. On the card that is a prefill at a third of + * the base speed and base requests 7-8 % slower. The tiled rows keep every promise above and + * change only who computes what: + * + * * THE GRID'S UNIT IS A ROW TILE (16 rows shrink, 32 expand). Every wave reads the tile's slots + * and a ballot decides; a tile of base rows leaves before any barrier or operand load, so a + * base step costs one small block a tile. A tile holding rows of several adapters runs ONE + * PASS PER DISTINCT SLOT, ascending, each pass with its own slot's pools; a row's value comes + * out of its own slot's pass alone and other rows' results are never stored. The passes of a + * tile may be spread over blockIdx.z when the grid is small (decode with several adapters); + * that decides which block runs a pass, never the pass's arithmetic. + * * SHRINK IS WMMA bf16 (16 x 16 x 16), with the pool fragment as the A operand and the x + * fragment as B, so a lane's accumulator is one row. Its order is fixed by K and R alone: K + * in 32-wide steps, a block's run of steps split over its 4 waves (wave w takes every 4th), + * the waves summed 0..3 in LDS, and -- for a K long enough that one tile would leave the card + * idle at decode -- the run split over KS blocks whose f32 partials the LAST to arrive sums + * 0..KS-1 (sh_plan). bf16 activations feed the WMMA as they are; fp8 and i8 codes are exact + * in bf16 and feed it as codes, and each step's partial is scaled by its group's f32 scale, + * which needs the group to be whole steps (a multiple of 32, or one group across K) and is + * otherwise the v1 kernel. Every one of those roundings is a sum or product of the terms the + * contract names, so the result is inside v1's (K + 2) 2^-24 sum|terms| bound; no code times + * scale is ever rounded on its own. f16 and f32 activations have no exact WMMA and stay v1. + * * EXPAND IS v1's ARITHMETIC IN A TILE. B[s]'s [128 columns, 32 j] slab is staged in LDS as f32 + * (exact) and reused by the tile's 32 rows; each output keeps v1's sequence (acc = 0, j + * ascending acc = acc + t*B, d = scale*acc, one rounding into y), so its bytes are libref's. + * * Kernels are written against r4d_launch.h so tests/r4d_emu runs this source on the host. */ #include #include +#include +#include +#include #include "r4d_plugin.h" #include "r4d_launch.h" @@ -216,6 +248,365 @@ __global__ __launch_bounds__(256) void lora_expand_kernel(ExpandArgs A) { } } +/* ================================================================ v2: the tiled rows */ + +typedef short lr_v8s __attribute__((ext_vector_type(8))); /* 8 bf16, as the WMMA takes them */ +typedef float lr_v8f __attribute__((ext_vector_type(8))); +typedef unsigned lr_v4u __attribute__((ext_vector_type(4))); /* 16 codes */ + +enum { + kShMT = 16, /* shrink: rows a tile -- the WMMA's N side */ + kShJT = 16, /* shrink: columns j a tile -- the WMMA's M side */ + kShStep = 32, /* k a step: two WMMAs, 16 contiguous k a lane */ + kShWaves = 4, + kShThreads = kShWaves * 32, + kShU = 4, /* steps a wave keeps in flight */ + kShSplitMin = 16, /* no K split leaves a block fewer than 16 steps (512 k) */ + kShFill = 24, /* the blocks one row tile is spread over, j tiles x K splits */ + kExMT = 32, /* expand: rows a tile, four a wave */ + kExNT = 128, /* expand: columns a tile, four a lane */ + kExJC = 32, /* expand: j a staged slab */ + kExThreads = 256, + kZMax = 16, /* the passes of one tile spread over at most this many blocks */ + kZGrid = 256, /* ... and only while the grid is smaller than this */ +}; + +struct ShrinkTArgs { + ShrinkArgs b; + float* part; /* [ks][M][R] split partials, the op's scratch; null when ks == 1 */ + unsigned* ctr; /* arrival counters, one a (row tile, j tile, z); null when ks == 1 */ + int M, jt, ks, spb, nsteps, Z; +}; + +struct ExpandTArgs { + ExpandArgs b; + int M, Z; +}; + +__device__ __forceinline__ int imin(int a, int b) { return a < b ? a : b; } + +/* The tile's distinct adapter slots, ascending; -1 entries are base rows. One thread, sixteen or + * thirty-two values: an insertion sort is the whole cost. */ +__device__ inline int distinct_slots(const int* s, int* out, int n) { + int k = 0; + for (int i = 0; i < n; ++i) { + const int v = s[i]; + if (v < 0) continue; + int p = 0; + while (p < k && out[p] < v) ++p; + if (p < k && out[p] == v) continue; + for (int q = k; q > p; --q) out[q] = out[q - 1]; + out[p] = v; + ++k; + } + return k; +} + +/* A code as the bf16 that holds it exactly: E4M3 (3 mantissa bits, exponents 2^-9..2^8), E5M2 (2, + * 2^-16..2^15) and i8 (|v| <= 128, eight significant bits) all fit bf16's 8-bit significand and + * exponent. Bit for bit the upper half of the ABI's f32 decode above (e4m3_to_f32, f16_to_f32 of + * the E5M2 byte, the i8 value), NaN payloads included; tests/r4d_emu walks every code. */ +template +__device__ __forceinline__ uint16_t code_bf16(unsigned b) { + if (ADT == kE4M3) { + const unsigned s = (b & 0x80u) << 8, e = (b >> 3) & 0xfu, m = b & 7u; + if (e == 0u) return (uint16_t)(s | (__float_as_uint((float)m * 0.001953125f) >> 16)); + if (e == 0xfu && m == 7u) return (uint16_t)(s | 0x7fc0u); + return (uint16_t)(s | ((e + 120u) << 7) | (m << 4)); + } + if (ADT == kE5M2) { + const unsigned s = (b & 0x80u) << 8, e = (b >> 2) & 0x1fu, m = b & 3u; + if (e == 0u) return (uint16_t)(s | (__float_as_uint((float)m * 1.52587890625e-05f) >> 16)); + if (e == 0x1fu) return (uint16_t)(s | 0x7f80u | (m << 5)); + return (uint16_t)(s | ((e + 112u) << 7) | (m << 5)); + } + return (uint16_t)(__float_as_uint((float)(int8_t)(uint8_t)b) >> 16); +} + +/* Sixteen bf16 of a pool row from k on, as two WMMA fragments; zeros past K and for a lane that is + * off (j >= R). Zeros and not whatever follows: the next row's values times a zero x could be a + * NaN times zero. V: the row is dense and 16-byte aligned, so two 16-byte loads. */ +template +__device__ __forceinline__ void ld_pool16(const uint16_t* p, int k, int K, long long cs, bool on, + lr_v8s* f) { + f[0] = (lr_v8s)(short)0; + f[1] = (lr_v8s)(short)0; + if (!on) return; + if (V && k + 16 <= K) { + f[0] = *(const lr_v8s*)(p + k); + f[1] = *(const lr_v8s*)(p + k + 8); + return; + } + for (int e = 0; e < 16; ++e) + if (k + e < K) f[e >> 3][e & 7] = (short)p[(long long)(k + e) * cs]; +} + +/* Sixteen activation elements of a row from k on, as bf16 fragments: bf16 as it is, a code as the + * bf16 that holds it. Off (the row is another slot's, or past M): zeros, and nothing is read. */ +template +__device__ __forceinline__ void ld_act16(const void* a, long long row, int k, int K, long long cs, + bool on, lr_v8s* f) { + f[0] = (lr_v8s)(short)0; + f[1] = (lr_v8s)(short)0; + if (!on) return; + if (ADT == kBF16) { + ld_pool16((const uint16_t*)a + row, k, K, cs, true, f); + return; + } + const uint8_t* p = (const uint8_t*)a + row; + if (V && k + 16 <= K) { + const lr_v4u q = *(const lr_v4u*)(p + k); + for (int e = 0; e < 16; ++e) + f[e >> 3][e & 7] = (short)code_bf16((q[e >> 2] >> (8 * (e & 3))) & 0xffu); + return; + } + for (int e = 0; e < 16; ++e) + if (k + e < K) f[e >> 3][e & 7] = (short)code_bf16(p[(long long)(k + e) * cs]); +} + +__device__ __forceinline__ lr_v8f wmma_bf16(lr_v8s a, lr_v8s b, lr_v8f c) { + return __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12(a, b, c); +} + +/* SHRINK, one block a (j tile x K split, row tile, z), 4 waves. The pool fragment is the WMMA's A + * operand (16 j x 16 k) and the x fragment its B (16 k x 16 rows), so lane l's eight outputs are + * row l % 16 at j = 8 (l / 16) .. +7: one row, one group scale a lane. A lane holds 16 contiguous + * k of its row in both operands, the first 8 in the step's first WMMA and the next 8 in its + * second; any assignment of k to WMMA positions is a sum of the same products, and this one is + * fixed. Per element the order is, all of it a function of K, R and the group alone: + * + * wave w: acc = 0; for its steps i ascending (i = w, w + 4, ... of the block's run) + * bf16: acc = wmma(A1, X1, wmma(A0, X0, acc)) + * coded: acc = acc + scale(m, group of i) * wmma(A1, X1, wmma(A0, X0, 0)) + * block: v = ((w0 + w1) + w2) + w3 + * t: v of the single block, or ((p0 + p1) + ...) + p[ks-1] over the K splits + * + * x of a row that is not in the pass is a zero fragment and its column of the WMMA is never + * stored; a WMMA's output column depends on that column of B alone, so it cannot reach the rows + * that are stored. */ +template +__global__ __launch_bounds__(kShThreads) void lora_shrink_wmma_kernel(ShrinkTArgs T) { + __shared__ float red[kShWaves][kShJT * kShMT]; /* [wave][j][m] */ + __shared__ int sh_slot[kShMT], sh_list[kShMT]; + __shared__ int sh_n; + __shared__ unsigned sh_arr; + const ShrinkArgs& A = T.b; + const int tid = (int)threadIdx.x, lane = tid & 31, wave = tid >> 5; + const int col = lane & 15, half = lane >> 4; + const int jt = (int)blockIdx.x / T.ks, ks = (int)blockIdx.x - jt * T.ks; + const int m0 = (int)blockIdx.y * kShMT, z = (int)blockIdx.z; + + /* The tile's slots, read by every wave, so a tile of base rows leaves here: no barrier, no + * operand load. */ + int my = -1; + if (lane < kShMT && m0 + lane < T.M) { + const int s = A.slot[(long long)(m0 + lane) * A.slot_st]; + if (s >= 0 && s < A.S) my = s; + } + if (__builtin_amdgcn_ballot_w32(my >= 0) == 0u) return; + if (wave == 0 && lane < kShMT) sh_slot[lane] = my; + __syncthreads(); + if (tid == 0) sh_n = distinct_slots(sh_slot, sh_list, kShMT); + __syncthreads(); + const int npass = sh_n; + if (z >= npass) return; + + const int st0 = ks * T.spb, st1 = imin(st0 + T.spb, T.nsteps); + const int j = jt * kShJT + col; /* this lane's pool row */ + const bool aon = j < A.R; + const long long xrow = (long long)(m0 + col) * A.a_rs; + const long long srow = (long long)(m0 + col) * A.s_rs; + for (int p = z; p < npass; p += T.Z) { + const int s = sh_list[p]; + const bool xon = sh_slot[col] == s; /* this lane's row is in the pass */ + const uint16_t* ap = A.la + (long long)s * A.la_s0 + (long long)j * A.la_s1; + lr_v8f acc = (lr_v8f)(0.0f); + for (int st = st0 + wave; st < st1; st += kShWaves * kShU) { + lr_v8s af[kShU][2], xf[kShU][2]; + float sc[kShU]; +#pragma unroll + for (int u = 0; u < kShU; ++u) { + const int su = st + u * kShWaves; + const bool in = su < st1; + const int k = su * kShStep + half * 16; + ld_pool16(ap, k, A.K, A.la_s2, aon && in, af[u]); + ld_act16(A.a, xrow, k, A.K, A.a_cs, xon && in, xf[u]); + sc[u] = 0.0f; + if (ADT != kBF16 && xon && in) + sc[u] = A.as[srow + (long long)((su * kShStep) / A.group) * A.s_cs]; + } +#pragma unroll + for (int u = 0; u < kShU; ++u) { + if (st + u * kShWaves >= st1) break; /* wave-uniform */ + if (ADT == kBF16) { + acc = wmma_bf16(af[u][0], xf[u][0], acc); + acc = wmma_bf16(af[u][1], xf[u][1], acc); + } else { + lr_v8f d = wmma_bf16(af[u][0], xf[u][0], (lr_v8f)(0.0f)); + d = wmma_bf16(af[u][1], xf[u][1], d); + acc = acc + d * sc[u]; + } + } + } + for (int e = 0; e < 8; ++e) red[wave][(half * 8 + e) * kShMT + col] = acc[e]; + __syncthreads(); + for (int i = tid; i < kShJT * kShMT; i += kShThreads) { + const int jj = i / kShMT, mm = i - jj * kShMT, jg = jt * kShJT + jj; + float v = red[0][i]; + for (int w = 1; w < kShWaves; ++w) v = v + red[w][i]; + if (sh_slot[mm] != s || jg >= A.R) continue; + if (T.ks == 1) + A.t[(long long)(m0 + mm) * A.t_rs + (long long)jg * A.t_cs] = v; + else + T.part[((long long)ks * T.M + m0 + mm) * A.R + jg] = v; + } + __syncthreads(); + } + if (T.ks == 1) return; + + /* THE K SPLIT'S SUM, in the last block to arrive. Every thread's partial stores are fenced + * before the arrival, and the counter is left zero for the next launch (they are ordered: the + * lora rows are not concurrent, and each lane's `t` keys its own counter block). */ + __threadfence(); + __syncthreads(); + const int cidx = ((int)blockIdx.y * T.jt + jt) * kZMax + z; + if (tid == 0) + sh_arr = __hip_atomic_fetch_add(&T.ctr[cidx], 1u, __ATOMIC_ACQ_REL, + __HIP_MEMORY_SCOPE_AGENT); + __syncthreads(); + if (sh_arr != (unsigned)(T.ks - 1)) return; + if (tid == 0) T.ctr[cidx] = 0u; + __threadfence(); + for (int i = tid; i < kShJT * kShMT; i += kShThreads) { + const int jj = i / kShMT, mm = i - jj * kShMT, jg = jt * kShJT + jj; + const int s = sh_slot[mm]; + if (s < 0 || jg >= A.R) continue; + int r = 0; + while (sh_list[r] != s) ++r; + if (r % T.Z != z) continue; /* another block's pass */ + const long long o = (long long)(m0 + mm) * A.R + jg, pl = (long long)T.M * A.R; + float v = T.part[o]; + for (int q = 1; q < T.ks; ++q) v = v + T.part[(long long)q * pl + o]; + A.t[(long long)(m0 + mm) * A.t_rs + (long long)jg * A.t_cs] = v; + } +} + +/* EXPAND, one block a (128 columns, 32 rows, z), 8 waves: wave w owns rows 4w..4w+3 and lane l + * columns 4l..4l+3, sixteen outputs a thread. B[s]'s slab [128 columns][32 j] is staged as f32 + * (a bf16 widens exactly) and t's [32 rows][32 j] beside it; then every output runs v1's sequence + * over the slab and the next, so the bytes are v1's and libref's. A pass's accumulators start at + * zero and only its own rows are written back: a row of another slot accumulates against zeros it + * never stores. V: B is dense, 16-byte aligned and R a multiple of 8, so 16-byte loads. */ +template +__global__ __launch_bounds__(kExThreads) void lora_expand_tiled_kernel(ExpandTArgs T) { + __shared__ float bs[kExJC][kExNT]; + __shared__ float ts[kExJC][kExMT]; + __shared__ int ex_slot[kExMT], ex_list[kExMT]; + __shared__ int ex_n; + const ExpandArgs& A = T.b; + const int tid = (int)threadIdx.x, lane = tid & 31, wave = tid >> 5; + const long long n0 = (long long)blockIdx.x * kExNT; + const int m0 = (int)blockIdx.y * kExMT, z = (int)blockIdx.z; + + int my = -1; + if (m0 + lane < T.M) { + const int s = A.slot[(long long)(m0 + lane) * A.slot_st]; + if (s >= 0 && s < A.S) my = s; + } + if (__builtin_amdgcn_ballot_w32(my >= 0) == 0u) return; /* a tile of base rows */ + if (wave == 0) ex_slot[lane] = my; + __syncthreads(); + if (tid == 0) ex_n = distinct_slots(ex_slot, ex_list, kExMT); + __syncthreads(); + const int npass = ex_n; + if (z >= npass) return; + + for (int p = z; p < npass; p += T.Z) { + const int s = ex_list[p]; + const uint16_t* bp = A.lb + (long long)s * A.lb_s0; + /* A wave none of whose four rows is in the pass stages with the others and computes + * nothing: at decode that is seven of the eight. Wave-uniform, outside every barrier. */ + const bool live = ex_slot[4 * wave] == s || ex_slot[4 * wave + 1] == s || + ex_slot[4 * wave + 2] == s || ex_slot[4 * wave + 3] == s; + float acc[4][4]; + for (int r = 0; r < 4; ++r) + for (int c = 0; c < 4; ++c) acc[r][c] = 0.0f; + for (int j0 = 0; j0 < A.R; j0 += kExJC) { + const int jc = imin(kExJC, A.R - j0); + __syncthreads(); /* the last slab's readers are done */ + if (V) { + const int nv = jc >> 3; /* four lanes cover a column's 32 j */ + for (int v = tid; v < kExNT * nv; v += kExThreads) { + const int nl = v / nv, jv = v - nl * nv; + const long long n = n0 + nl; + lr_v8s q = (lr_v8s)(short)0; + if (n < A.N) q = *(const lr_v8s*)(bp + n * A.lb_s1 + j0 + jv * 8); + for (int e = 0; e < 8; ++e) bs[jv * 8 + e][nl] = bf2f((uint16_t)q[e]); + } + } else { + for (int v = tid; v < kExNT * jc; v += kExThreads) { + const int jj = v / kExNT, nl = v - jj * kExNT; + const long long n = n0 + nl; + bs[jj][nl] = n < A.N ? bf2f(bp[n * A.lb_s1 + (long long)(j0 + jj) * A.lb_s2]) + : 0.0f; + } + } + for (int v = tid; v < kExMT * jc; v += kExThreads) { + const int jj = v / kExMT, r = v - jj * kExMT; + ts[jj][r] = ex_slot[r] == s + ? A.t[(long long)(m0 + r) * A.t_rs + (long long)(j0 + jj) * A.t_cs] + : 0.0f; + } + __syncthreads(); + if (!live) continue; + /* Four j a round, their LDS reads issued together: one round's wait instead of four. + * The adds stay in j order for every output. */ + int jj = 0; + for (; jj + 4 <= jc; jj += 4) { + float tv[4][4], bv[4][4]; +#pragma unroll + for (int q = 0; q < 4; ++q) { + for (int r = 0; r < 4; ++r) tv[q][r] = ts[jj + q][4 * wave + r]; + for (int c = 0; c < 4; ++c) bv[q][c] = bs[jj + q][4 * lane + c]; + } +#pragma unroll + for (int q = 0; q < 4; ++q) + for (int r = 0; r < 4; ++r) + for (int c = 0; c < 4; ++c) + acc[r][c] = acc[r][c] + tv[q][r] * bv[q][c]; + } + for (; jj < jc; ++jj) { + float tv[4], bv[4]; + for (int r = 0; r < 4; ++r) tv[r] = ts[jj][4 * wave + r]; + for (int c = 0; c < 4; ++c) bv[c] = bs[jj][4 * lane + c]; + for (int r = 0; r < 4; ++r) + for (int c = 0; c < 4; ++c) acc[r][c] = acc[r][c] + tv[r] * bv[c]; + } + } + for (int r = 0; r < 4; ++r) { + const int row = 4 * wave + r; + if (ex_slot[row] != s) continue; /* another pass's row, or a base row */ + const long long m = m0 + row; + for (int c = 0; c < 4; ++c) { + const long long n = n0 + 4 * lane + c; + if (n >= A.N) continue; + const float d = A.sc[(long long)s * A.sc_rs + n * A.sc_cs] * acc[r][c]; + const long long o = m * A.y_rs + n * A.y_cs; + if (YDT == kBF16) { + uint16_t* y = (uint16_t*)A.y + o; + *y = f2bf(bf2f(*y) + d); + } else if (YDT == kF16) { + uint16_t* y = (uint16_t*)A.y + o; + *y = f32_to_f16(f16_to_f32(*y) + d); + } else { + float* y = (float*)A.y + o; + *y = *y + d; + } + } + } + } +} + /* ---------------------------------------------------------------- operand reading (host) */ /* `t` read as [rows, cols]: the trailing axes that span `cols` must be one dense run, the leading @@ -276,8 +667,8 @@ static bool dtype_agrees(const RadArgs* a, uint32_t dt) { return !d || rad_dtype_parse(d) == dt; } -/* The launches, one per dtype instantiation: the grid is M blocks for shrink and M strips of 256 - * columns for expand, 256 threads each, whatever the slots hold. */ +/* The launches, one per dtype instantiation: the v1 grid is M blocks for shrink and M strips of + * 256 columns for expand, 256 threads each, whatever the slots hold. */ template static void launch_shrink(const ShrinkArgs& P, int64_t blocks, RadStream stream) { (void)stream; /* the host emulator's launch takes none */ @@ -291,13 +682,10 @@ static void launch_expand(const ExpandArgs& P, int64_t blocks, RadStream stream) (hipStream_t)stream, P); } -} /* namespace r4d_lora */ - -extern "C" { - -/* operands: a, a_scale?, slot, lora_a, t */ -int r4d_rad_lora_shrink(const RadArgs* a, RadStream stream) { - using namespace r4d_lora; +/* lora_shrink's operands, read and checked once for every row: on RAD_OK, `P` and `adt` describe + * the launch over *M > 0 rows. Zero rows is refused, as every op refuses it (docs/OPS.md). */ +static int shrink_operands(const RadArgs* a, ShrinkArgs* out, int* adt_out, int64_t* M_out) { + *M_out = 0; if (!a || a->n_t < 5) return RAD_E_INVAL; const RadTensor *X = rad_arg_in(a, 0), *XS = rad_arg_in(a, 1), *SL = rad_arg_in(a, 2); const RadTensor *LA = rad_arg_in(a, 3), *T = rad_arg_in(a, 4); @@ -364,21 +752,15 @@ int r4d_rad_lora_shrink(const RadArgs* a, RadStream stream) { P.t = (float*)T->data; P.t_rs = t_rs; P.t_cs = t_cs; P.K = (int)K; P.R = (int)R; P.S = (int)S; if (!P.as) P.group = (int)K; - - switch (adt) { - case kBF16: launch_shrink(P, M, stream); break; - case kF16: launch_shrink(P, M, stream); break; - case kF32: launch_shrink(P, M, stream); break; - case kE4M3: launch_shrink(P, M, stream); break; - case kE5M2: launch_shrink(P, M, stream); break; - default: launch_shrink(P, M, stream); break; - } - return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE; + *out = P; + *adt_out = adt; + *M_out = M; + return RAD_OK; } -/* operands: t, slot, lora_b, scale, y (inout) */ -int r4d_rad_lora_expand(const RadArgs* a, RadStream stream) { - using namespace r4d_lora; +/* lora_expand's, the same way. */ +static int expand_operands(const RadArgs* a, ExpandArgs* out, int* ydt_out, int64_t* M_out) { + *M_out = 0; if (!a || a->n_t < 5 || !rad_args_have(a, 5)) return RAD_E_INVAL; const RadTensor *T = rad_arg_in(a, 0), *SL = rad_arg_in(a, 1), *LB = rad_arg_in(a, 2); const RadTensor *SC = rad_arg_in(a, 3), *Y = rad_arg_in(a, 4); @@ -405,8 +787,7 @@ int r4d_rad_lora_expand(const RadArgs* a, RadStream stream) { if (!as_rows(T, R, &tr, &t_rs, &t_cs) || tr < M) return RAD_E_SHAPE; if (!as_vec(SL, &sr, &sl_st) || sr < M) return RAD_E_SHAPE; if (rad_tensor_numel(LB) < S * N * R || rad_tensor_numel(SC) < S * N) return RAD_E_SHAPE; - const int64_t ntiles = (N + 255) / 256; - if (M * ntiles > 0x7fffffff) return RAD_E_SHAPE; + if (M > 0x7fffffff) return RAD_E_SHAPE; ExpandArgs P{}; P.t = (const float*)T->data; P.t_rs = t_rs; P.t_cs = t_cs; @@ -417,12 +798,225 @@ int r4d_rad_lora_expand(const RadArgs* a, RadStream stream) { P.sc_rs = SC->rank >= 2 ? SC->stride[SC->rank - 2] : N; P.sc_cs = SC->rank >= 2 ? SC->stride[SC->rank - 1] : 1; P.y = Y->data; P.y_rs = y_rs; P.y_cs = y_cs; - P.N = N; P.ntiles = ntiles; P.R = (int)R; P.S = (int)S; + P.N = N; P.ntiles = (N + 255) / 256; P.R = (int)R; P.S = (int)S; + *out = P; + *ydt_out = ydt; + *M_out = M; + return RAD_OK; +} +static int shrink_scalar(const ShrinkArgs& P, int adt, int64_t M, RadStream stream) { + switch (adt) { + case kBF16: launch_shrink(P, M, stream); break; + case kF16: launch_shrink(P, M, stream); break; + case kF32: launch_shrink(P, M, stream); break; + case kE4M3: launch_shrink(P, M, stream); break; + case kE5M2: launch_shrink(P, M, stream); break; + default: launch_shrink(P, M, stream); break; + } + return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE; +} + +/* ---------------------------------------------------------------- the tiled rows' plans (host) */ + +/* THE SHRINK SPLIT, from K and R alone -- never M, because it is the summation order. A row tile + * covers ceil(R / 16) j tiles; at decode that tile is the whole grid, so K is split until j tiles + * times splits reach kShFill blocks, but never below 16 steps (512 k) a block, past which the + * split's partial traffic and its arrival outweigh what the extra blocks read. The split is then + * rounded so no block is left without a step. MiniCPM5-2B's four sites at TP1, for scale: K 2048 + * R 48 -> 3 x 4 blocks; K 2048 R 32 -> 2 x 4; K 2048 R 16 -> 1 x 4; K 6144 R 16 -> 1 x 12. */ +struct ShPlan { int jt, ks, spb, nsteps; }; +static ShPlan sh_plan(int64_t K, int64_t R) { + ShPlan p; + p.nsteps = (int)((K + kShStep - 1) / kShStep); + p.jt = (int)((R + kShJT - 1) / kShJT); + int ks = (kShFill + p.jt - 1) / p.jt; + const int kmax = (p.nsteps + kShSplitMin - 1) / kShSplitMin; + if (ks > kmax) ks = kmax; + if (ks < 1) ks = 1; + p.spb = (p.nsteps + ks - 1) / ks; + p.ks = (p.nsteps + p.spb - 1) / p.spb; + return p; +} +static int64_t sh_part_bytes(int64_t M, int64_t R, const ShPlan& p) { + return p.ks > 1 ? (int64_t)p.ks * M * R * (int64_t)sizeof(float) : 0; +} + +/* HOW MANY BLOCKS ONE TILE'S PASSES SPREAD OVER. Decode with several adapters is a handful of + * blocks a pass, run one after another inside a block they would add up; spread over blockIdx.z + * they run at once. Only while the grid is small -- every z block of a tile that has fewer passes + * leaves after reading the slots, and on a prefill grid those would be thousands -- and never more + * than the passes a tile can have. Which block runs a pass is all this decides. */ +static int z_fan(int64_t base, int64_t S, int64_t M, int64_t tile) { + int64_t z = base >= kZGrid ? 1 : kZGrid / base; + if (z > S) z = S; + if (z > kZMax) z = kZMax; + if (z > M) z = M; + if (z > tile) z = tile; + return z < 1 ? 1 : (int)z; +} + +/* THE SPLIT'S ARRIVAL COUNTERS, one block a (device, `t`). They cannot live in the scratch: other + * ops write the arena between two of ours and the counters must be zero at entry. Each lane's `t` + * is its own buffer and the lora rows are ordered on their lane (not concurrent), so two launches + * that could overlap never share a block; the reducer leaves every counter zero. A block too + * small for a launch is replaced by a larger one and the old one is kept -- a recorded pass may + * still name it, and its own launches still fit it. */ +static unsigned* shrink_ctr(const void* t, int64_t n, RadStream stream) { + static std::mutex mu; + static std::map, std::pair> blocks; + int dev = 0; + if (hipGetDevice(&dev) != hipSuccess) return nullptr; + std::lock_guard lk(mu); + const auto key = std::make_pair(dev, t); + auto it = blocks.find(key); + if (it != blocks.end() && it->second.second >= n) return it->second.first; + int64_t cap = it != blocks.end() ? 2 * it->second.second : 4096; + while (cap < n) cap *= 2; + void* p = nullptr; + if (hipMalloc(&p, (size_t)cap * sizeof(unsigned)) != hipSuccess) return nullptr; + /* On the launch's own stream, so the zeros land before the kernel that counts on them. */ + if (hipMemsetAsync(p, 0, (size_t)cap * sizeof(unsigned), (hipStream_t)stream) != hipSuccess) + return nullptr; + blocks[key] = std::make_pair((unsigned*)p, cap); + return (unsigned*)p; +} + +template +static void launch_shrink_wmma(const ShrinkTArgs& P, dim3 grid, RadStream stream) { + (void)stream; + R4D_KLAUNCH(R4D_KNAME(lora_shrink_wmma_kernel), grid, dim3(kShThreads), + (hipStream_t)stream, P); +} +template +static void launch_shrink_wmma_v(const ShrinkTArgs& P, dim3 grid, bool vx, bool va, + RadStream stream) { + if (vx && va) launch_shrink_wmma(P, grid, stream); + else if (vx) launch_shrink_wmma(P, grid, stream); + else if (va) launch_shrink_wmma(P, grid, stream); + else launch_shrink_wmma(P, grid, stream); +} +template +static void launch_expand_tiled(const ExpandTArgs& P, dim3 grid, RadStream stream) { + (void)stream; + R4D_KLAUNCH(R4D_KNAME(lora_expand_tiled_kernel), grid, dim3(kExThreads), + (hipStream_t)stream, P); +} + +} /* namespace r4d_lora */ + +extern "C" { + +/* operands: a, a_scale?, slot, lora_a, t */ +int r4d_rad_lora_shrink(const RadArgs* a, RadStream stream) { + using namespace r4d_lora; + ShrinkArgs P{}; + int adt = 0; + int64_t M = 0; + const int rc = shrink_operands(a, &P, &adt, &M); + if (rc != RAD_OK || M <= 0) return rc; + return shrink_scalar(P, adt, M, stream); +} + +/* operands: t, slot, lora_b, scale, y (inout) */ +int r4d_rad_lora_expand(const RadArgs* a, RadStream stream) { + using namespace r4d_lora; + ExpandArgs P{}; + int ydt = 0; + int64_t M = 0; + const int rc = expand_operands(a, &P, &ydt, &M); + if (rc != RAD_OK || M <= 0) return rc; + if (M * P.ntiles > 0x7fffffff) return RAD_E_SHAPE; switch (ydt) { - case kBF16: launch_expand(P, M * ntiles, stream); break; - case kF16: launch_expand(P, M * ntiles, stream); break; - default: launch_expand(P, M * ntiles, stream); break; + case kBF16: launch_expand(P, M * P.ntiles, stream); break; + case kF16: launch_expand(P, M * P.ntiles, stream); break; + default: launch_expand(P, M * P.ntiles, stream); break; + } + return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE; +} + +/* The tiled shrink: lora_shrink_wmma (bf16 a) and lora_shrink_wmma_q (fp8 / i8 codes whose group + * is whole 32-wide steps) share this entry point. What the WMMA cannot hold exactly -- f16 and f32 + * activations, a scale group that ends inside a step -- goes to the v1 kernel, decided by the + * dtype and the stream's grid and never by M; the rows' bands do not send either here, and a + * direct caller still gets the exact answer. */ +int r4d_rad_lora_shrink_wmma(const RadArgs* a, RadStream stream) { + using namespace r4d_lora; + ShrinkArgs P{}; + int adt = 0; + int64_t M = 0; + const int rc = shrink_operands(a, &P, &adt, &M); + if (rc != RAD_OK || M <= 0) return rc; + if (adt == kF16 || adt == kF32) return shrink_scalar(P, adt, M, stream); + if (adt != kBF16 && P.group % kShStep != 0 && P.group < P.K) + return shrink_scalar(P, adt, M, stream); + + const ShPlan pl = sh_plan(P.K, P.R); + const int64_t rt = (M + kShMT - 1) / kShMT; + if (rt > 65535 || (int64_t)pl.jt * pl.ks > 0x7fffffff) return RAD_E_SHAPE; + ShrinkTArgs T{}; + T.b = P; + T.M = (int)M; T.jt = pl.jt; T.ks = pl.ks; T.spb = pl.spb; T.nsteps = pl.nsteps; + T.Z = z_fan(rt * pl.jt * pl.ks, P.S, M, kShMT); + if (pl.ks > 1) { + const int64_t need = sh_part_bytes(M, P.R, pl); + if (!a->scratch || a->scratch_bytes < need) return RAD_E_SCRATCH; + const int64_t band = rad_args_geti_or(a, "M", 0); + const int64_t rows = band > M ? band : M; + T.part = (float*)a->scratch; + T.ctr = shrink_ctr(P.t, ((rows + kShMT - 1) / kShMT) * pl.jt * kZMax, stream); + if (!T.ctr) return RAD_E_DEVICE; + } + /* 16-byte loads where the rows allow them; the arithmetic is the same either way. */ + const uintptr_t xa = (uintptr_t)P.a, pa = (uintptr_t)P.la; + const bool vx = P.a_cs == 1 && xa % 16 == 0 && P.a_rs % (adt == kBF16 ? 8 : 16) == 0; + const bool va = P.la_s2 == 1 && pa % 16 == 0 && P.la_s1 % 8 == 0 && P.la_s0 % 8 == 0; + const dim3 grid((unsigned)(pl.jt * pl.ks), (unsigned)rt, (unsigned)T.Z); + switch (adt) { + case kBF16: launch_shrink_wmma_v(T, grid, vx, va, stream); break; + case kE4M3: launch_shrink_wmma_v(T, grid, vx, va, stream); break; + case kE5M2: launch_shrink_wmma_v(T, grid, vx, va, stream); break; + default: launch_shrink_wmma_v(T, grid, vx, va, stream); break; + } + return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE; +} + +/* The K split's partials, sized at declare for the band's widest M (the core calls this with it). + * f16 / f32 activations run the v1 kernel and need none. */ +int64_t r4d_rad_lora_shrink_wmma_scratch(const RadArgs* a) { + using namespace r4d_lora; + const int64_t M = rad_args_geti_or(a, "M", 0), K = rad_args_geti_or(a, "K", 0); + const int64_t R = rad_args_geti_or(a, "R", 0); + if (M <= 0 || K <= 0 || R <= 0 || K > 0x7fffffff || R > (1 << 16)) return 0; + const char* d = rad_args_gets(a, "dtype"); + if (d && (rad_dtype_parse(d) == RAD_F16 || rad_dtype_parse(d) == RAD_F32)) return 0; + return sh_part_bytes(M, R, sh_plan(K, R)); +} + +/* The tiled expand: one block a (128 columns, 32 rows), the grid a function of M's tiles. */ +int r4d_rad_lora_expand_tiled(const RadArgs* a, RadStream stream) { + using namespace r4d_lora; + ExpandArgs P{}; + int ydt = 0; + int64_t M = 0; + const int rc = expand_operands(a, &P, &ydt, &M); + if (rc != RAD_OK || M <= 0) return rc; + const int64_t rt = (M + kExMT - 1) / kExMT, nt = (P.N + kExNT - 1) / kExNT; + if (rt > 65535 || nt > 0x7fffffff) return RAD_E_SHAPE; + ExpandTArgs T{}; + T.b = P; + T.M = (int)M; + T.Z = z_fan(rt * nt, P.S, M, kExMT); + const bool v = P.lb_s2 == 1 && (uintptr_t)P.lb % 16 == 0 && P.lb_s1 % 8 == 0 && + P.lb_s0 % 8 == 0 && P.R % 8 == 0; + const dim3 grid((unsigned)nt, (unsigned)rt, (unsigned)T.Z); + switch (ydt) { + case kBF16: v ? launch_expand_tiled(T, grid, stream) + : launch_expand_tiled(T, grid, stream); break; + case kF16: v ? launch_expand_tiled(T, grid, stream) + : launch_expand_tiled(T, grid, stream); break; + default: v ? launch_expand_tiled(T, grid, stream) + : launch_expand_tiled(T, grid, stream); break; } return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE; } diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index bc49dafa..45fba47c 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -68,6 +68,13 @@ enum { * one. */ R4D_PRIO_LOGITS_FP8 = 60, + /* Runtime LoRA: the tiled rows serve a subset of the scalar rows' dtypes at every M, so they + * must outrank them to be reached; the scalar rows keep f16 / f32 activations (no exact WMMA) + * and a coded activation whose scale group is not whole 32-wide steps, and stay reachable by + * name. The choice never depends on M -- a row's bytes would then depend on its batch. */ + R4D_PRIO_LORA_TILED = 60, + R4D_PRIO_LORA_SCALAR = 50, + /* Exactly one candidate row per (op, geometry). The value carries no ordering; it leaves a * user's override plugin room above and below. */ R4D_PRIO_ONLY = 50, @@ -128,9 +135,14 @@ int r4d_rad_norm_add_norm_bf16 (const RadArgs*, RadStream); int r4d_rad_norm_add_norm_route_bf16(const RadArgs*, RadStream); int r4d_rad_join_add_scale_bf16 (const RadArgs*, RadStream); int r4d_rad_expert_scale_bf16 (const RadArgs*, RadStream); -/* Runtime LoRA's two halves, each row's adapter named by its slot (r4d_lora_bf16.hip). */ +/* Runtime LoRA's two halves, each row's adapter named by its slot (r4d_lora_bf16.hip): the v1 + * scalar rows, and the tiled rows -- WMMA shrink over row tiles with a K split fixed by K and R + * (its partials in the op's scratch), and an expand that stages B[s] in LDS for a tile of rows. */ int r4d_rad_lora_shrink (const RadArgs*, RadStream); int r4d_rad_lora_expand (const RadArgs*, RadStream); +int r4d_rad_lora_shrink_wmma (const RadArgs*, RadStream); +int64_t r4d_rad_lora_shrink_wmma_scratch(const RadArgs*); +int r4d_rad_lora_expand_tiled (const RadArgs*, RadStream); int r4d_rad_attn_vit (const RadArgs*, RadStream); int r4d_rad_attn_dense (const RadArgs*, RadStream); int r4d_rad_seg_sinusoid (const RadArgs*, RadStream); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index f7d1d8e1..25ae530c 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -3195,6 +3195,17 @@ static const RadConstraint cLoraExpand[] = { C_GE("R", 1), C_LE("R", 65536), C_GE("S", 1), C_LE("S", 1048576), C_IN("dtype", "bf16 f16 f32"), }; +/* The tiled rows (r4d_lora_bf16.hip, V2). Shrink is two rows because a constraint on an absent + * parameter does not hold: a bf16 activation has no `group`, and a coded one is the WMMA's only + * when its group is whole 32-wide steps (the scale is applied a step at a time). Anything else -- + * f16, f32, a coded activation with another group or none declared -- is the scalar row's. */ +static const RadConstraint cLoraShrinkWmma[] = { + C_GE("R", 1), C_LE("R", 65536), C_GE("S", 1), C_LE("S", 1048576), C_IN("dtype", "bf16"), +}; +static const RadConstraint cLoraShrinkWmmaQ[] = { + C_GE("R", 1), C_LE("R", 65536), C_GE("S", 1), C_LE("S", 1048576), + C_IN("dtype", "fp8_e4m3 fp8_e5m2 i8"), C_GE("group", 32), C_DIV("group", 32), +}; /* ---- the routed mixture of experts ---------------------------------------------------------- * @@ -6310,7 +6321,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, /* Runtime LoRA, v1: scalar f32, correct first (r4d_lora_bf16.hip). NO opd_shape: libref describes * both ops, so rad-kbench draws the same operands for this row as for the oracle -- `slot` in - * [0, S], S itself a base row. NOT CONCURRENT: see rad_kernel_concurrent below. */ + * [0, S], S itself a base row. NOT CONCURRENT: see rad_kernel_concurrent below. Below the tiled + * rows, which serve the same ops for every dtype but f16/f32 activations (R4D_PRIO_LORA_TILED). */ { "lora_shrink_scalar", "lora_shrink", "gemm", "t = x A[slot]^T per row, base rows untouched: one block a row, one wave a column, the k of a " "lane in order and a fixed xor butterfly across the wave", @@ -6318,7 +6330,7 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, "honoured", "bf16 / f16 / f32 a, or fp8_e4m3 / fp8_e5m2 / i8 a with its f32 scale stream; bf16 lora_a; " "i32 slot; f32 t", - RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, + RAD_DOMAIN_DEVICE, R4D_PRIO_LORA_SCALAR, ROW(cLoraShrink), NOTUNE, nullptr, nullptr, r4d_rad_lora_shrink }, { "lora_expand_scalar", "lora_expand", "gemm", @@ -6326,9 +6338,39 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, "output, j in order", "any M and N, 1 <= R <= 65536, 1 <= S <= 2^20; row and column strides of t and y honoured", "f32 t and scale; bf16 lora_b; i32 slot; bf16 / f16 / f32 y", - RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, + RAD_DOMAIN_DEVICE, R4D_PRIO_LORA_SCALAR, ROW(cLoraExpand), NOTUNE, nullptr, nullptr, r4d_rad_lora_expand }, +/* Runtime LoRA, v2: the tiled rows. The grid is row TILES, a tile of base rows leaves on one + * ballot, and a mixed tile runs one pass per distinct slot; shrink's summation order is fixed by K + * and R (sh_plan), expand's bytes are v1's. The shrink split keeps its partials in the op's + * scratch (the hook sizes it for the band) and its arrival counters in a block keyed on `t`. */ +{ "lora_shrink_wmma", "lora_shrink", "gemm", + "t = x A[slot]^T per row, base rows untouched: WMMA bf16 over 16-row x 16-j tiles, K in 32-wide " + "steps over 4 waves and a K split fixed by K and R, one pass a distinct slot of a tile", + "any M and K, 1 <= R <= 65536, 1 <= S <= 2^20; row and column strides of a and t honoured, " + "16-byte loads where the rows are aligned", + "bf16 a; bf16 lora_a; i32 slot; f32 t", + RAD_DOMAIN_DEVICE, R4D_PRIO_LORA_TILED, + ROW(cLoraShrinkWmma), NOTUNE, + nullptr, nullptr, r4d_rad_lora_shrink_wmma, r4d_rad_lora_shrink_wmma_scratch }, +{ "lora_shrink_wmma_q", "lora_shrink", "gemm", + "lora_shrink_wmma over fp8 / i8 codes: each code is exact in bf16, and each 32-wide step's " + "partial is scaled by its group's f32 scale", + "any M and K, a scale group that is a multiple of 32 (or one group across K), 1 <= R <= 65536, " + "1 <= S <= 2^20; strides of a, a_scale and t honoured", + "fp8_e4m3 / fp8_e5m2 / i8 a with its f32 scale stream; bf16 lora_a; i32 slot; f32 t", + RAD_DOMAIN_DEVICE, R4D_PRIO_LORA_TILED, + ROW(cLoraShrinkWmmaQ), NOTUNE, + nullptr, nullptr, r4d_rad_lora_shrink_wmma, r4d_rad_lora_shrink_wmma_scratch }, +{ "lora_expand_tiled", "lora_expand", "gemm", + "y += scale * (t B[slot]^T) per row, base rows neither read nor written: 32-row x 128-column " + "tiles, B[slot]'s slab staged in LDS once a tile, v1's j-ascending order a output", + "any M and N, 1 <= R <= 65536, 1 <= S <= 2^20; row and column strides of t and y honoured", + "f32 t and scale; bf16 lora_b; i32 slot; bf16 / f16 / f32 y", + RAD_DOMAIN_DEVICE, R4D_PRIO_LORA_TILED, + ROW(cLoraExpand), NOTUNE, + nullptr, nullptr, r4d_rad_lora_expand_tiled }, { "attn_prefix_self_h128_bf16", "attn_prefix_self", "attn", "attention over a paged prefix and the pass's own rows, causal over the own rows or not", "head_dim 128, 1/2/4/8 query heads a KV head, a flat grid over the sequences' query blocks; " diff --git a/tools/kbench_core.cpp b/tools/kbench_core.cpp index 6397f0cd..337fb419 100644 --- a/tools/kbench_core.cpp +++ b/tools/kbench_core.cpp @@ -72,8 +72,11 @@ const std::vector g_tol = { { "logit_rerank", "*", 8e-3 }, { "moe_gemm", "*", 3e-2 }, /* Runtime LoRA's shrink is a K-long reduction into an f32 `t` whatever `a` is -- the codes are - * decoded the same way on both sides, so only the summation order differs, and that is what - * gemm_nt's f32 row allows. Expand's R-long sum lands in `y` and takes the dtype rows below. */ + * decoded the same way on both sides, so only the summation order differs (and, in libr4d's + * WMMA row, a code's group scale is applied to each 32-wide step's partial rather than to each + * element: a different rounding of the same terms), and that is what gemm_nt's f32 row + * allows. Expand's R-long sum lands in `y` and takes the dtype rows below; libr4d's tiled + * expand computes libref's bytes. */ { "lora_shrink", "*", 2e-5 }, /* Attention: a softmax over a long context plus a second reduction. */ { "attn_", "bf16", 2e-2 },