Archived
Prune padded-row work from single-row asymmetric W4/W5 GEMMs
Keep WMMA, group and split arithmetic unchanged, but stage and reduce only the live row. Add byte parity, guard and canonical-arithmetic cases over legal launch geometries.
This commit is contained in:
1 parent
2ef0536051
commit
9f90451599
2 files changed
+99
-8
No files matched your search
@@ -83,7 +83,7 @@ __device__ __forceinline__ v8f mmah(v8h a, v8h b, v8f c) {
|
||||
|
||||
// MT = row tiles per block, as in the bf16 kernel. HI false is the 4-bit asymmetric grid (z-lab's
|
||||
// plain int4), with no fifth-bit plane.
|
||||
template <int MT, int NPW, bool NT, bool HI>
|
||||
template <int MT, int NPW, bool NT, bool HI, bool ONE = false>
|
||||
__global__ __launch_bounds__(1024) void r4d_gemm_w5a16_nt_m64_kernel(
|
||||
const unsigned short* __restrict__ A, const unsigned* __restrict__ Wq,
|
||||
const unsigned* __restrict__ Whi, const unsigned* __restrict__ Wsz,
|
||||
@@ -181,16 +181,46 @@ __global__ __launch_bounds__(1024) void r4d_gemm_w5a16_nt_m64_kernel(
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++)
|
||||
#pragma unroll
|
||||
for (int e = 0; e < 8; e++) acc[j][i][e] = fmaf(g_acc[j][i][e], sc[j], acc[j][i][e]);
|
||||
for (int e = 0; e < (ONE ? 1 : 8); e++)
|
||||
acc[j][i][e] = fmaf(g_acc[j][i][e], sc[j], acc[j][i][e]);
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++) pa[i] += (R4D_GEMM_W4_GROUP / 16) * 4;
|
||||
}
|
||||
}
|
||||
|
||||
// Split-K reduction and store as in r4d_gemm_bf16_nt_m64: one row tile at a time, fixed split
|
||||
// order.
|
||||
const int ncols = WV * NPW;
|
||||
const int nthreads = WV * SK * WAVE32;
|
||||
if constexpr (ONE) {
|
||||
// M=1: only element 0 of lanes 0..15 belongs to the live row. Keep the same WMMA,
|
||||
// per-group FMA and split order, but do not scale/reduce the other fifteen rows.
|
||||
static_assert(MT == 1, "the one-row epilogue needs one row tile");
|
||||
if (SK == 1) {
|
||||
if (lane < 16) {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < NPW; j++) {
|
||||
const int n = (nt0 + j) * 16 + lane;
|
||||
if (n < N) C[n] = (__hip_bfloat16)(0.f + acc[j][0][0]);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (lane < 16) {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < NPW; j++)
|
||||
red[((col_local * NPW + j) * SK + ksplit) * 16 + lane] = acc[j][0][0];
|
||||
}
|
||||
__syncthreads();
|
||||
for (int idx = tid; idx < ncols * 16; idx += nthreads) {
|
||||
const int col = idx / 16, n = (blockIdx.x * ncols + col) * 16 + idx % 16;
|
||||
float v = 0.f;
|
||||
for (int s = 0; s < SK; s++) v += red[(col * SK + s) * 16 + idx % 16];
|
||||
if (n < N) C[n] = (__hip_bfloat16)v;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Split-K reduction and store as in r4d_gemm_bf16_nt_m64: one row tile at a time, fixed split
|
||||
// order.
|
||||
#pragma unroll
|
||||
for (int i = 0; i < MT; i++) {
|
||||
__syncthreads();
|
||||
@@ -229,8 +259,12 @@ void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, i
|
||||
const int ntiles = N / 16, mtiles = (M + 15) / 16;
|
||||
const int ncols = WV * NPW;
|
||||
dim3 grid((ntiles + ncols - 1) / ncols, (mtiles + MB - 1) / MB), block(WV * SK * WAVE32);
|
||||
// Keep the same accepted tuning space as the multi-row form. The single-row specialization
|
||||
// uses 1/16 of its reduction storage (none without a split).
|
||||
size_t smem = (size_t)ncols * SK * 256 * sizeof(float);
|
||||
if (smem > 64 * 1024) throw std::runtime_error("r4d gemm_w5a16_nt_m64: WV*NPW*SK too large");
|
||||
const bool one = M == 1 && MB == 1;
|
||||
if (one) smem = SK == 1 ? 0 : smem / 16;
|
||||
auto Ap = (const unsigned short*)a;
|
||||
auto Wp = (const unsigned*)wq;
|
||||
auto Hp = (const unsigned*)whi;
|
||||
@@ -242,8 +276,13 @@ void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, i
|
||||
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); \
|
||||
else hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<MT, NP, TT, false>), grid, block, smem, \
|
||||
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); } while (0)
|
||||
#define LAUNCH_ONE(NP, TT) do { \
|
||||
if (hi5) hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<1, NP, TT, true, true>), grid, block, smem, \
|
||||
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); \
|
||||
else hipLaunchKernelGGL((r4d_gemm_w5a16_nt_m64_kernel<1, NP, TT, false, true>), grid, block, smem, \
|
||||
st, Ap, Wp, Hp, Sp, Cp, M, K, N, WV, SK, lda, ldc); } while (0)
|
||||
#define LAUNCH_MB(NP, TT) switch (MB) { \
|
||||
case 1: LAUNCH(1, NP, TT); break; \
|
||||
case 1: if (one) { LAUNCH_ONE(NP, TT); } else { LAUNCH(1, NP, TT); } break; \
|
||||
case 2: LAUNCH(2, NP, TT); break; \
|
||||
case 3: LAUNCH(3, NP, TT); break; \
|
||||
case 4: LAUNCH(4, NP, TT); break; \
|
||||
@@ -254,6 +293,7 @@ void r4d_gemm_w5a16_nt_m64(long a, long wq, long whi, long wsz, long c, int M, i
|
||||
if (NT) { LAUNCH_MB(4, true); } else { LAUNCH_MB(4, false); }
|
||||
}
|
||||
#undef LAUNCH_MB
|
||||
#undef LAUNCH_ONE
|
||||
#undef LAUNCH
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user