From 45d039dcb445e2de1cec01fb216c312a8d5abad9 Mon Sep 17 00:00:00 2001 From: iz0eyj Date: Wed, 2 Dec 2026 01:43:54 +0100 Subject: [PATCH] [HIP] tiled-LDS gemm_bf16 (16x16x16): ~16x fewer global reads, BIT-EXACT Replace 1-thread-per-output scalar GEMM with a 16x16 output-tile block staging 16-wide K slabs of X/W in LDS (loaded once, reused across 256 outputs). Serial-K ascending accumulation preserved => bit-exact vs scalar (hip gemm parity, ld 23 batched bit-exact, gw 4 serve greedy vs reference all EXACT). Prefill 2048: 52.3 -> 44.4 s (-15%). WMMA tensor-core is the further pass (changes f32 accum order => needs greedy re-verification). --- kernels/hip/vetta_kernels.hip | 41 +++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index f67840d..718c3ff 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -194,20 +194,43 @@ extern "C" int vetta_embed_bf16_gather(const uint16_t* table, const int32_t* tok } // ---- k_gemm_bf16: y[M,N] = x[M,K] @ w[N,K]^T, f32 accumulate --------------- -// x BF16 row-major [M,K], w BF16 row-major [N,K], y f32 [M,N]. One thread per -// output element (correctness-first; WMMA tiling is the performance pass). +// x BF16 row-major [M,K], w BF16 row-major [N,K], y f32 [M,N]. Tiled LDS GEMM: +// each block computes a 16x16 output tile, staging 16-wide K slabs of X and W in +// shared memory (loaded once, reused across the 256 outputs => ~16x fewer global +// reads than 1-thread-per-output). Accumulation stays serial-K ascending per +// output (k0 tiles outer, kk inner) => BIT-EXACT vs the naive scalar kernel. +// (WMMA tensor-core tiling is the further performance pass; it changes the f32 +// accumulation order so it requires greedy-parity re-verification, not bit-exact.) +#define GEMM_TM 16 +#define GEMM_TN 16 +#define GEMM_TK 16 __global__ void k_gemm_bf16(const uint16_t* __restrict__ x, const uint16_t* __restrict__ w, float* __restrict__ y, int M, int N, int K) { - const int n = blockIdx.x * blockDim.x + threadIdx.x; - const int m = blockIdx.y * blockDim.y + threadIdx.y; - if (m >= M || n >= N) return; - const uint16_t* xr = x + (size_t)m * K; - const uint16_t* wr = w + (size_t)n * K; + __shared__ uint16_t xs[GEMM_TM][GEMM_TK]; + __shared__ uint16_t ws[GEMM_TN][GEMM_TK]; + const int ty = threadIdx.y; // m-local 0..15 + const int tx = threadIdx.x; // n-local 0..15 + const int m = blockIdx.y * GEMM_TM + ty; + const int n = blockIdx.x * GEMM_TN + tx; + const int tid = ty * GEMM_TN + tx; // 0..255 + const int lr = tid / GEMM_TK; // cooperative load row 0..15 + const int lc = tid % GEMM_TK; // cooperative load col 0..15 + const int gm = blockIdx.y * GEMM_TM + lr; + const int gn = blockIdx.x * GEMM_TN + lr; float acc = 0.f; - for (int kk = 0; kk < K; ++kk) acc += bf16_to_f32(xr[kk]) * bf16_to_f32(wr[kk]); - y[(size_t)m * N + n] = acc; + for (int k0 = 0; k0 < K; k0 += GEMM_TK) { + const int gk = k0 + lc; + xs[lr][lc] = (gm < M && gk < K) ? x[(size_t)gm * K + gk] : (uint16_t)0; + ws[lr][lc] = (gn < N && gk < K) ? w[(size_t)gn * K + gk] : (uint16_t)0; + __syncthreads(); + #pragma unroll + for (int kk = 0; kk < GEMM_TK; ++kk) + acc += bf16_to_f32(xs[ty][kk]) * bf16_to_f32(ws[tx][kk]); + __syncthreads(); + } + if (m < M && n < N) y[(size_t)m * N + n] = acc; } extern "C" int vetta_gemm_bf16(const uint16_t* x, const uint16_t* w, float* y,