diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hpp index e10d8ba1..f643918a 100644 --- a/include/strata/prefill/kernels.hpp +++ b/include/strata/prefill/kernels.hpp @@ -25,6 +25,10 @@ void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float* int64_t T, void* stream, uint16_t* mixed_h = nullptr, uint16_t* mixed16_lo = nullptr); /// F-2: gr_write, then gr_norm_rs of the next half (its norm weights) over the rows just written - the same bits as /// the two calls, without reading R back. +/// S23 (opt-in STRATA_HC_UPMIX=1): the up projection (lo16 x w_up^T, BF16, FP32 accumulate) with gr_mix_r as its +/// epilogue - `gated` is never written. False (nothing launched) off gfx11. +bool gr_upmix(const uint16_t* lo16, const uint16_t* w_up, const float* R, const float* rs, const float* w_norm, + float* mixed, uint16_t* mixed16, uint16_t* mixed_h, int64_t T, void* stream); void gr_write_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* w_norm_next, float eps, float* rs, uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo = nullptr); /// lo16[t, k] = bf16(silu(lo[t, k] / hc)) diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu index 6f1ec519..8322b9a8 100644 --- a/src/prefill/kernels.cu +++ b/src/prefill/kernels.cu @@ -11,6 +11,7 @@ #include #include #include +#include namespace strata::prefill { namespace { @@ -798,6 +799,77 @@ __global__ void to_bf16_kernel(const float* __restrict__ x, uint16_t* __restrict } } +// S23 (opt-in STRATA_HC_UPMIX=1, gfx11): the hyper-connection read's up projection with gr_mix_r as its epilogue. The +// up GEMM's FP32 output `gated` (T x 10240: 671 MB per 16K chunk) is never written or read back: a block computes a +// tile of 64 tokens x 64 columns for all 4 streams on the matrix cores (BF16 in, FP32 accumulate over K = 320; the +// lo16 and w_up tiles staged in LDS), and each lane already holds the 4 streams' values of its (token, column) - the +// mix (x_c = R * rs * w, sum of x_c * sigmoid(gated_c), / 4) runs in registers, in gr_mix_r_kernel's order. The +// GEMM sums K in another order than hipBLASLt: rounding-level, hence opt-in and quality-gated. +#if defined(__HIPCC__) +typedef __bf16 um_b16 __attribute__((ext_vector_type(16))); +typedef float um_f8 __attribute__((ext_vector_type(8))); +__device__ __forceinline__ um_f8 um_wmma(um_b16 a, um_b16 b, um_f8 c) { +#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1150__) || defined(__gfx1151__) + return __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a, b, c); +#else + (void) a; (void) b; return c; // not a gfx11 device pass: never launched (runtime gate) +#endif +} +constexpr int UM_T = 128, UM_D = 16, UM_LD = LR + 8; // a block: 128 tokens x 16 columns x the 4 streams +__device__ __forceinline__ um_b16 um_frag(const uint16_t* p) { // 16 BF16 (32 bytes, 16-byte aligned) + const uint4 a = *reinterpret_cast(p), b = *reinterpret_cast(p + 8); + const uint32_t w[8] = {a.x, a.y, a.z, a.w, b.x, b.y, b.z, b.w}; + return __builtin_bit_cast(um_b16, w); +} +// The block's w_up rows (16 columns x 4 streams x all 320 k) are staged in LDS once; each wave streams its own 16 +// tokens' lo16 rows from global memory (every A fragment feeds the 4 streams' WMMAs) - one barrier per block. +__global__ void __launch_bounds__(256) gr_upmix_kernel(const uint16_t* __restrict__ lo16, const uint16_t* __restrict__ wu, + const float* __restrict__ R, const float* __restrict__ rs, + const float* __restrict__ w, float* __restrict__ mixed, + uint16_t* __restrict__ mixed16, uint16_t* __restrict__ mixed_h, + int64_t T) { + __shared__ __align__(16) uint16_t sB[HC * UM_D][UM_LD]; + const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, l16 = lane & 15, hi = lane >> 4; + // blocks ordered column-tile fastest: the blocks in flight share a token tile, so the epilogue's R reads run + // along rows (token-tile fastest scattered them over 512 rows x 40 KB per block: 28 ms instead of 3.7 at 8K) + const int64_t t0 = (int64_t) blockIdx.y * UM_T + 16 * wave; + const int d0 = blockIdx.x * UM_D; + for (int i = tid; i < HC * UM_D * (LR / 8); i += 256) { + const int rr = i / (LR / 8), q = i % (LR / 8); + const int c = rr / UM_D, dd = rr % UM_D; + *reinterpret_cast(&sB[rr][8 * q]) = *reinterpret_cast(wu + (size_t) (c * N + d0 + dd) * LR + 8 * q); + } + __syncthreads(); + if (t0 >= T) return; + const int64_t ta = t0 + l16 < T ? t0 + l16 : T - 1; + const uint16_t* arow = lo16 + ta * LR; + um_f8 acc[HC]; +#pragma unroll + for (int c = 0; c < HC; ++c) acc[c] = um_f8{0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; +#pragma unroll + for (int ks = 0; ks < LR; ks += 16) { + const um_b16 a = um_frag(arow + ks); +#pragma unroll + for (int c = 0; c < HC; ++c) acc[c] = um_wmma(a, um_frag(&sB[c * UM_D + l16][ks]), acc[c]); + } + const int d = d0 + l16; +#pragma unroll + for (int i = 0; i < 8; ++i) { + const int64_t t = t0 + 2 * i + hi; + if (t >= T) continue; + float s = 0.0f; +#pragma unroll + for (int c = 0; c < HC; ++c) { + const float x = R[t * D + c * N + d] * rs[t * HC + c] * w[c * N + d]; // gr_mix_r_kernel's order + s = fmaf(x, sigm(acc[c][i]), s); + } + s /= (float) HC; + mixed[t * N + d] = s; + if (mixed16) mixed16[t * N + d] = bf(s); + if (mixed_h) mixed_h[t * N + d] = hf(s); + } +} +#endif } // namespace void kv_append(const float* K, const float* V, int64_t T, int64_t pos0, const int32_t* page_table, int64_t page_size, @@ -841,6 +913,26 @@ void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float* mixed_h, mixed16_lo); check("gr_mix_r"); } +bool gr_upmix(const uint16_t* lo16, const uint16_t* w_up, const float* R, const float* rs, const float* w_norm, + float* mixed, uint16_t* mixed16, uint16_t* mixed_h, int64_t T, void* stream) { +#if defined(__HIPCC__) + static const bool gfx11 = [] { + int dev = 0; + cudaDeviceProp p; + if (cudaGetDevice(&dev) != cudaSuccess || cudaGetDeviceProperties(&p, dev) != cudaSuccess) return false; + return std::strncmp(p.gcnArchName, "gfx11", 5) == 0; + }(); + if (!gfx11 || T <= 0) return false; + gr_upmix_kernel<<>>( + lo16, w_up, R, rs, w_norm, mixed, mixed16, mixed_h, T); + check("gr_upmix"); + return true; +#else + (void) lo16; (void) w_up; (void) R; (void) rs; (void) w_norm; (void) mixed; (void) mixed16; (void) mixed_h; + (void) T; (void) stream; + return false; +#endif +} void gr_write_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* w_norm_next, float eps, float* rs, uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo) { gr_write_norm_rs_kernel<<<(unsigned) (T * HC), 256, 0, (cudaStream_t) stream>>>(R, bo, inj, inj_ld, w_norm_next, eps, diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 54c9cb7e..79c9351f 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -167,6 +167,12 @@ inline int bf16x2_mode() { return v; } inline bool bf16x2() { return bf16x2_mode() != 0; } +// S23 (opt-in STRATA_HC_UPMIX=1): the hyper-connection read's up projection and gr_mix_r as one kernel (gr_upmix, gfx11); +// STRATA_HC_UPMIX_CHECK=N also runs the default pair on the first N reads and reports the difference of `mixed` +inline bool hc_upmix() { + static const bool v = [] { const char* e = std::getenv("STRATA_HC_UPMIX"); return e != nullptr && e[0] == '1'; }(); + return v; +} inline bool bf16x2_hc() { return bf16x2_mode() == 1; } // F-1: STRATA_GR_UNFUSED=1 keeps the FP32 copy of the normalized rows (gr_norm + gr_mix), the A/B arm @@ -1409,11 +1415,42 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e normed = false; if (!bf16_proj(m.gemm, wd, m.xn16, m.lo, T, sd, err, 0, m.xn16_lo)) return false; gr_silu(m.lo, m.lo16, T, m.cs, m.lo16_lo); - if (!bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err, 0, m.lo16_lo)) return false; + bool upmixed = false; + if (hc_upmix() && !gr_unfused() && !m.lo16_lo && !m.mixed_bf_lo && + wu->kind == core::WeightKind::Bf16InF32 && wu->data && wu->ne0 == LR && wu->ne1 == D) { + static int checks = [] { const char* e = std::getenv("STRATA_HC_UPMIX_CHECK"); return e ? std::atoi(e) : 0; }(); + if (checks > 0) { // the default pair first, kept for the comparison + --checks; + if (!bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err)) return false; + gr_mix_r(m.R, m.grs, (const float*) wn->data, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h); + std::vector ref((size_t) T * N), got((size_t) T * N); + cudaMemcpyAsync(ref.data(), m.mixed, ref.size() * 4, cudaMemcpyDeviceToHost, m.cs); + upmixed = gr_upmix(m.lo16, (const uint16_t*) wu->data, m.R, m.grs, (const float*) wn->data, + m.mixed, m.mixed_bf, m.mixed_h, T, m.cs); + cudaMemcpyAsync(got.data(), m.mixed, got.size() * 4, cudaMemcpyDeviceToHost, m.cs); + cudaStreamSynchronize(m.cs); + double e2 = 0, r2 = 0, emax = 0; + for (size_t i = 0; i < ref.size(); ++i) { + const double dd = (double) got[i] - ref[i]; + e2 += dd * dd; r2 += (double) ref[i] * ref[i]; emax = std::max(emax, std::fabs(dd)); + } + std::fprintf(stderr, "strata: STRATA_HC_UPMIX_CHECK layer %lld half %d T %lld: mixed rel RMS %.3e, " + "max |diff| %.3e (%s)\n", (long long) l, half, (long long) T, + std::sqrt(e2 / std::max(r2, 1e-30)), emax, upmixed ? "upmix" : "upmix refused"); + } else { + upmixed = gr_upmix(m.lo16, (const uint16_t*) wu->data, m.R, m.grs, (const float*) wn->data, + m.mixed, m.mixed_bf, m.mixed_h, T, m.cs); + } + } + if (!upmixed && !bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err, 0, m.lo16_lo)) return false; if (!bf16_proj(m.gemm, wi, m.xn16, m.inj, T, si, err, 0, m.xn16_lo)) return false; - if (gr_unfused()) gr_mix(m.xn, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h, m.mixed_bf_lo); - else gr_mix_r(m.R, m.grs, (const float*) wn->data, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h, - m.mixed_bf_lo); + if (upmixed) { + } else if (gr_unfused()) { + gr_mix(m.xn, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h, m.mixed_bf_lo); + } else { + gr_mix_r(m.R, m.grs, (const float*) wn->data, m.gated, m.mixed, m.mixed_bf, T, m.cs, m.mixed_h, + m.mixed_bf_lo); + } if (half == 0 && !core::is_qsa_layer(g, l)) { // ======================= GDN =======================