[HIP] P1 b6: BF16 dense projection WMMA tensor-core GEMM
k_gemm_bf16_wmma: drop-in for the tiled-LDS scalar k_gemm_bf16 (same bf16 X[M,K]/ W[N,K], f32 Y[M,N]). BM16/BN128(8 waves)/BK64 LDS; bf16 operands feed __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32 directly (no dequant). gemm_bf16 binding dispatches WMMA for prefill (m>=16, k%64==0); decode m<16 keeps scalar; VETTA_NO_BF16_WMMA=1 forces scalar (A/B baseline). Parity vs serial-K CPU oracle (global-scale): 1.0e-7..3.0e-7 (bf16 products exact in f32, only accumulation order differs). gw serve_gen 4/4; ld forward_batch argmax 111==111 (greedy gate), max_abs unchanged 7.25e-2. MEASURED prefill 2048 synthetic A/B: scalar 36.2s -> WMMA 27.8s (-23%). Cumulative prefill 60 -> 27.8s (-54%); reference ~4.5s, gap ~6x. Suite GREEN: hip35 ld23 gw4 GPU serial, CPU -D warnings 23, Lean 6/6 0sorryAx.
This commit is contained in:
1 parent
99cd458d3b
commit
216983abae
2 files changed
+145
No files matched your search
@@ -932,6 +932,18 @@ __device__ __forceinline__ v_f8 wmma_f16_g11(v_h16 a, v_h16 b, v_f8 c) {
|
||||
// gfx11 accumulator lane mapping: lane l holds col (l&15), rows {2r+(l>>4)}.
|
||||
__device__ __forceinline__ int wmma_acc_m11(int r, int lane) { return 2*r + (lane>>4); }
|
||||
__device__ __forceinline__ int wmma_acc_n11(int lane) { return lane & 15; }
|
||||
// BF16 WMMA operand (gfx11): same 16x16-bit lane layout as f16, bf16 bit pattern.
|
||||
typedef short v_bf16 __attribute__((ext_vector_type(16)));
|
||||
__device__ __forceinline__ v_bf16 wmma_ld_bf16(const uint16_t* p) {
|
||||
const uint4 a = *reinterpret_cast<const uint4*>(p);
|
||||
const uint4 b = *reinterpret_cast<const uint4*>(p + 8);
|
||||
typedef unsigned u32x8 __attribute__((ext_vector_type(8)));
|
||||
const u32x8 v = {a.x,a.y,a.z,a.w,b.x,b.y,b.z,b.w};
|
||||
return __builtin_bit_cast(v_bf16, v);
|
||||
}
|
||||
__device__ __forceinline__ v_f8 wmma_bf16_g11(v_bf16 a, v_bf16 b, v_f8 c) {
|
||||
return __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a, b, c);
|
||||
}
|
||||
|
||||
// ---- k_nvfp4_gemv (M=1 decode): y[N] = dequant(W)[N,K] @ x[K] --------------
|
||||
// One block per output row; threads stride over K/16 groups, f32 accumulate.
|
||||
@@ -2413,4 +2425,83 @@ extern "C" int vetta_moe_reduce_buckets_masked(const float* bucket_y,
|
||||
k_moe_reduce_buckets_masked<<<grid, 256, 0, s>>>(bucket_y, slotk_to_bidx, tk_w, out, B, top_k, D);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_gemm_bf16_wmma: BF16 WMMA tensor-core dense GEMM (P1 b6) -------------
|
||||
// Drop-in for k_gemm_bf16 (same bf16 X[M,K]/W[N,K], f32 Y[M,N]). BM16/BN128
|
||||
// (8 waves, one 16x16 N tile each)/BK64 K-slab staged in LDS. bf16 operands feed
|
||||
// the matrix core directly (no dequant); f32 accumulation order differs from the
|
||||
// serial-K scalar kernel => ~1e-3 (P1 budget; greedy-vs-reference is the gate).
|
||||
#define GB_BM 16
|
||||
#define GB_BN 128
|
||||
#define GB_BK 64
|
||||
#define GB_LD (GB_BK + 8)
|
||||
__global__ void __launch_bounds__(256) k_gemm_bf16_wmma(
|
||||
const uint16_t* __restrict__ x,
|
||||
const uint16_t* __restrict__ w,
|
||||
float* __restrict__ y, int M, int N, int K) {
|
||||
__shared__ uint16_t sX[GB_BM * GB_LD];
|
||||
__shared__ uint16_t sW[GB_BN * GB_LD];
|
||||
const int tid = threadIdx.x;
|
||||
const int wave = tid >> 5; // 0..7 -> one 16-wide N tile
|
||||
const int lane = tid & 31;
|
||||
const int l16 = lane & 15;
|
||||
const int m0 = blockIdx.y * GB_BM;
|
||||
const int n0 = blockIdx.x * GB_BN;
|
||||
const int tm = (M - m0 < GB_BM) ? (M - m0) : GB_BM;
|
||||
v_f8 acc = v_f8_zero();
|
||||
for (int k0 = 0; k0 < K; k0 += GB_BK) {
|
||||
// X [16, BK] bf16: 128 threads x uint4
|
||||
if (tid < 128) {
|
||||
const int xr = tid >> 3, xc = tid & 7;
|
||||
const int gm = m0 + xr, gk = k0 + xc * 8;
|
||||
uint16_t* dst = &sX[xr * GB_LD + xc * 8];
|
||||
if (xr < tm && gk + 8 <= K) {
|
||||
*reinterpret_cast<uint4*>(dst) = *reinterpret_cast<const uint4*>(x + (size_t)gm * K + gk);
|
||||
} else {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j)
|
||||
dst[j] = (xr < tm && gk + j < K) ? x[(size_t)gm * K + gk + j] : (uint16_t)0;
|
||||
}
|
||||
}
|
||||
// W [BN, BK] bf16: 256 threads x 4 uint4
|
||||
#pragma unroll
|
||||
for (int it = 0; it < 4; ++it) {
|
||||
const int idx = it * 256 + tid;
|
||||
const int r = idx >> 3, c = (idx & 7) * 8;
|
||||
const int gn = n0 + r, gk = k0 + c;
|
||||
uint16_t* dst = &sW[r * GB_LD + c];
|
||||
if (gn < N && gk + 8 <= K) {
|
||||
*reinterpret_cast<uint4*>(dst) = *reinterpret_cast<const uint4*>(w + (size_t)gn * K + gk);
|
||||
} else {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < 8; ++j)
|
||||
dst[j] = (gn < N && gk + j < K) ? w[(size_t)gn * K + gk + j] : (uint16_t)0;
|
||||
}
|
||||
}
|
||||
__syncthreads();
|
||||
#pragma unroll
|
||||
for (int kk = 0; kk < 4; ++kk) {
|
||||
v_bf16 a = wmma_ld_bf16(&sX[l16 * GB_LD + kk * 16]);
|
||||
v_bf16 b = wmma_ld_bf16(&sW[(wave * 16 + l16) * GB_LD + kk * 16]);
|
||||
acc = wmma_bf16_g11(a, b, acc);
|
||||
}
|
||||
__syncthreads();
|
||||
}
|
||||
const int gn = n0 + wave * 16 + wmma_acc_n11(lane);
|
||||
#pragma unroll
|
||||
for (int r = 0; r < 8; ++r) {
|
||||
const int lm = wmma_acc_m11(r, lane);
|
||||
if (lm < tm && gn < N) y[(size_t)(m0 + lm) * N + gn] = acc[r];
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_gemm_bf16_wmma(const uint16_t* x, const uint16_t* w, float* y,
|
||||
int M, int N, int K, vetta_stream_t stream) {
|
||||
if (M <= 0 || N <= 0 || K <= 0 || (K % GB_BK) != 0) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
dim3 grid((N + GB_BN - 1) / GB_BN, (M + GB_BM - 1) / GB_BM);
|
||||
k_gemm_bf16_wmma<<<grid, 256, 0, s>>>(x, w, y, M, N, K);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user