Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

prefill: STRATA_PF_HCDOWN=1 - the HC read's down and inject projections as one WMMA GEMM over a padded xn16

Niko12215eecc5dee6c0
diff --git a/include/strata/prefill/kernels.hpp b/include/strata/prefill/kernels.hpp
index 8ce3f8edc..da22f98b4 100644
--- a/include/strata/prefill/kernels.hpp
+++ b/include/strata/prefill/kernels.hpp
@@ -19,7 +19,7 @@ void gr_norm(const float* R, const float* w_norm, float eps, float* xn, uint16_t
uint16_t* xn16_lo = nullptr);
/// F-1: gr_norm without its FP32 output: the row scales rs[t*4 + c] and the BF16 image; gr_mix_r then reads R.
void gr_norm_rs(const float* R, const float* w_norm, float eps, float* rs, uint16_t* xn16, int64_t T, void* stream,
- uint16_t* xn16_lo = nullptr);
+ uint16_t* xn16_lo = nullptr, int64_t ldx = 0); // ldx: xn16's token stride (0 = 10240)
/// gr_mix with xn recomputed from R, rs and w_norm exactly as gr_norm computes it (the same bits).
void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float* gated, float* mixed, uint16_t* mixed16,
int64_t T, void* stream, uint16_t* mixed_h = nullptr, uint16_t* mixed16_lo = nullptr);
@@ -30,13 +30,14 @@ void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float*
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);
+ float* rs, uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo = nullptr,
+ int64_t ldx = 0);
/// S23 (STRATA_CVEC_FUSE=1): gr_write, then the control vector (v_l = layer l's direction row, *s_l its scale,
/// *on the request flag, mode 0 project / 1 add; cvec_kernel's arithmetic), then gr_norm_rs with the next half's
/// norm - one pass over R, the same bits as the three kernels.
void gr_write_cvec_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* v_l,
const float* s_l, const int* on, int mode, const float* w_norm_next, float eps, float* rs,
- uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo = nullptr);
+ uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo = nullptr, int64_t ldx = 0);
/// lo16[t, k] = bf16(silu(lo[t, k] / hc))
void gr_silu(const float* lo, uint16_t* lo16, int64_t T, void* stream, uint16_t* lo16_lo = nullptr);
/// mixed[t, d] = mean_c xn[t, c, d] * sigmoid(gated[t, c, d]); FP32, BF16 and FP16 (either image may be null).
diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu
index 1a09cd6f9..6ee42ef3e 100644
--- a/src/prefill/kernels.cu
+++ b/src/prefill/kernels.cu
@@ -85,11 +85,12 @@ __global__ void gr_norm_kernel(const float* __restrict__ R, const float* __restr
// so the FP32 copy of the normalized rows (T x 10240 floats) is neither written nor read
__global__ void gr_norm_rs_kernel(const float* __restrict__ R, const float* __restrict__ w, float eps,
float* __restrict__ rs_out, uint16_t* __restrict__ xn16,
- uint16_t* __restrict__ xn16_lo) {
+ uint16_t* __restrict__ xn16_lo, int64_t ldx) {
__shared__ float sh[32];
const int64_t row = blockIdx.x; // t * 4 + c
const int c = (int) (row % HC);
const float* r = R + row * N;
+ const int64_t xo = (row / HC) * ldx + (int64_t) c * N; // the BF16 image's row (token stride ldx)
float ss = 0.0f;
for (int d = threadIdx.x; d < N; d += blockDim.x) ss += r[d] * r[d];
const float rs = rsqrtf(block_sum(ss, sh) / (float) N + eps);
@@ -97,8 +98,8 @@ __global__ void gr_norm_rs_kernel(const float* __restrict__ R, const float* __re
for (int d = threadIdx.x; d < N; d += blockDim.x) {
const float v = r[d] * rs * w[c * N + d];
const uint16_t h = bf(v);
- xn16[row * N + d] = h;
- if (xn16_lo) xn16_lo[row * N + d] = bf_lo(v, h);
+ xn16[xo + d] = h;
+ if (xn16_lo) xn16_lo[xo + d] = bf_lo(v, h);
}
}
__global__ void gr_mix_r_kernel(const float* __restrict__ R, const float* __restrict__ rs, const float* __restrict__ w,
@@ -131,12 +132,13 @@ __global__ void __launch_bounds__(256) gr_write_norm_rs_kernel(float* __restrict
const float* __restrict__ inj, int64_t inj_ld,
const float* __restrict__ w, float eps,
float* __restrict__ rs_out, uint16_t* __restrict__ xn16,
- uint16_t* __restrict__ xn16_lo) {
+ uint16_t* __restrict__ xn16_lo, int64_t ldx) {
__shared__ float sh[32];
const int64_t row = blockIdx.x; // t * 4 + c
const int64_t t = row / HC;
const int c = (int) (row % HC);
float* r = R + row * N;
+ const int64_t xo = t * ldx + (int64_t) c * N;
const float sc = 2.0f * sigm(inj[t * inj_ld + c] / (float) HC);
float v[GRW_PER];
float ss = 0.0f;
@@ -155,8 +157,8 @@ __global__ void __launch_bounds__(256) gr_write_norm_rs_kernel(float* __restrict
for (int d = threadIdx.x; d < N; d += 256, ++k) {
const float x = v[k] * rs * w[c * N + d];
const uint16_t h = bf(x);
- xn16[row * N + d] = h;
- if (xn16_lo) xn16_lo[row * N + d] = bf_lo(x, h);
+ xn16[xo + d] = h;
+ if (xn16_lo) xn16_lo[xo + d] = bf_lo(x, h);
}
}
// S23 (opt-in STRATA_CVEC_FUSE=1): a steered layer's FFN write, its control vector and the next half's norm in one
@@ -167,7 +169,7 @@ __global__ void __launch_bounds__(256) gr_write_cvec_norm_rs_kernel(
float* __restrict__ R, const float* __restrict__ bo, const float* __restrict__ inj, int64_t inj_ld,
const float* __restrict__ v_l, const float* __restrict__ s_l, const int* __restrict__ on, int mode,
const float* __restrict__ w, float eps, float* __restrict__ rs_out, uint16_t* __restrict__ xn16,
- uint16_t* __restrict__ xn16_lo) {
+ uint16_t* __restrict__ xn16_lo, int64_t ldx) {
__shared__ float sh[32];
__shared__ float part[8];
const int64_t row = blockIdx.x; // t * 4 + c
@@ -217,8 +219,8 @@ __global__ void __launch_bounds__(256) gr_write_cvec_norm_rs_kernel(
for (int d = threadIdx.x; d < N; d += 256, ++k) {
const float y = x[k] * rs * w[c * N + d];
const uint16_t h = bf(y);
- xn16[row * N + d] = h;
- if (xn16_lo) xn16_lo[row * N + d] = bf_lo(y, h);
+ xn16[t * ldx + (int64_t) c * N + d] = h;
+ if (xn16_lo) xn16_lo[t * ldx + (int64_t) c * N + d] = bf_lo(y, h);
}
}
__global__ void gr_silu_kernel(const float* __restrict__ lo, uint16_t* __restrict__ lo16, uint16_t* __restrict__ lo16_lo,
@@ -967,8 +969,9 @@ void gr_norm(const float* R, const float* w_norm, float eps, float* xn, uint16_t
check("gr_norm");
}
void gr_norm_rs(const float* R, const float* w_norm, float eps, float* rs, uint16_t* xn16, int64_t T, void* stream,
- uint16_t* xn16_lo) {
- gr_norm_rs_kernel<<<(unsigned) (T * HC), 256, 0, (cudaStream_t) stream>>>(R, w_norm, eps, rs, xn16, xn16_lo);
+ uint16_t* xn16_lo, int64_t ldx) {
+ gr_norm_rs_kernel<<<(unsigned) (T * HC), 256, 0, (cudaStream_t) stream>>>(R, w_norm, eps, rs, xn16, xn16_lo,
+ ldx > 0 ? ldx : D);
check("gr_norm_rs");
}
void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float* gated, float* mixed, uint16_t* mixed16,
@@ -998,16 +1001,17 @@ bool gr_upmix(const uint16_t* lo16, const uint16_t* w_up, const float* R, const
#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) {
+ float* rs, uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo, int64_t ldx) {
gr_write_norm_rs_kernel<<<(unsigned) (T * HC), 256, 0, (cudaStream_t) stream>>>(R, bo, inj, inj_ld, w_norm_next, eps,
- rs, xn16, xn16_lo);
+ rs, xn16, xn16_lo,
+ ldx > 0 ? ldx : D);
check("gr_write_norm_rs");
}
void gr_write_cvec_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* v_l,
const float* s_l, const int* on, int mode, const float* w_norm_next, float eps, float* rs,
- uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo) {
+ uint16_t* xn16, int64_t T, void* stream, uint16_t* xn16_lo, int64_t ldx) {
gr_write_cvec_norm_rs_kernel<<<(unsigned) (T * HC), 256, 0, (cudaStream_t) stream>>>(
- R, bo, inj, inj_ld, v_l, s_l, on, mode, w_norm_next, eps, rs, xn16, xn16_lo);
+ R, bo, inj, inj_ld, v_l, s_l, on, mode, w_norm_next, eps, rs, xn16, xn16_lo, ldx > 0 ? ldx : D);
check("gr_write_cvec_norm_rs");
}
void gr_silu(const float* lo, uint16_t* lo16, int64_t T, void* stream, uint16_t* lo16_lo) {
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 67997d565..26a860a57 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -1,6 +1,7 @@
// src/prefill/prefill.cpp - see include/strata/prefill/prefill.hpp.
#include "strata/prefill/prefill.hpp"
#include "mmq_resident_sort.hpp"
+#include "wmma_gemm.h"
#include "strata/core/mtp.hpp"
#include "strata/core/progress.hpp"
#include "strata/core/on_device.hpp"
@@ -182,6 +183,14 @@ inline bool hc_upmix() {
inline bool bf16x2_hc() { return bf16x2_mode() == 1; }
// S23 (opt-in STRATA_CVEC_FUSE=1): a steered layer's FFN write + control vector + the next half's norm in one pass
// over R (gr_write_cvec_norm_rs; bitwise the gr_write + cvec_apply + gr_norm_rs it replaces)
+// S23 (opt-in STRATA_PF_HCDOWN=1, on chunks of STRATA_PF_SWITCH_MIN_T+ tokens): the hyper-connection read's down and
+// inject projections as one WMMA GEMM over xn16 (strata_pf_hcdown_bf16), xn16 written with token stride
+// 10240 + 64. Rounding-level (another k order than hipBLASLt): quality-gated.
+inline bool pf_hcdown() {
+ static const bool v = [] { const char* e = std::getenv("STRATA_PF_HCDOWN"); return e && e[0] == '1'; }();
+ return v;
+}
+constexpr int64_t XN_PAD = 64;
inline bool cvec_fuse() {
static const bool v = [] { const char* e = std::getenv("STRATA_CVEC_FUSE"); return e && e[0] == '1'; }();
return v;
@@ -738,7 +747,7 @@ bool Prefill::carve(size_t T, void* alloc) {
m.emb = o.take<float>(T * N, ok); m.R = o.take<float>(T * D, ok);
m.xn = gr_unfused() ? o.take<float>(T * D, ok) : nullptr; // F-1: not needed (gr_mix_r reads R)
m.grs = o.take<float>(T * HC, ok);
- m.xn16 = o.take<uint16_t>(T * D, ok); m.lo = o.take<float>(T * LR, ok); m.lo16 = o.take<uint16_t>(T * LR, ok);
+ m.xn16 = o.take<uint16_t>(T * (D + (pf_hcdown() ? XN_PAD : 0)), ok); m.lo = o.take<float>(T * LR, ok); m.lo16 = o.take<uint16_t>(T * LR, ok);
m.gated = o.take<float>(T * D, ok); m.inj = o.take<float>(T * HC, ok);
m.mixed = o.take<float>(T * N, ok); m.mixed_bf = o.take<uint16_t>(T * N, ok);
m.mixed_h = o.take<uint16_t>(T * N, ok); m.bo = o.take<float>(T * N, ok);
@@ -1003,7 +1012,8 @@ uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::Session
o.take<uint16_t>((size_t) GEMM_SCRATCH, ok);
o.take<uint8_t>(GEMM_WS, ok);
auto f = [&](size_t n) { o.take<float>(n, ok); };
- f(T * N); f(T * D); f(T * D); o.take<uint16_t>(T * D, ok); f(T * LR); o.take<uint16_t>(T * LR, ok);
+ f(T * N); f(T * D); f(T * D); o.take<uint16_t>(T * (D + (pf_hcdown() ? XN_PAD : 0)), ok); f(T * LR);
+ o.take<uint16_t>(T * LR, ok);
f(T * D); f(T * HC); f(T * N); o.take<uint16_t>(T * N, ok); o.take<uint16_t>(T * N, ok); f(T * N);
if (bf16x2_hc()) { o.take<uint16_t>(T * D, ok); o.take<uint16_t>(T * LR, ok); }
if (bf16x2()) o.take<uint16_t>(T * N, ok);
@@ -1438,10 +1448,24 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
*wu = need(v, su.c_str(), err), *wi = need(v, si.c_str(), err);
if (!wn || !wd || !wu || !wi) return false;
pt.mark(kPfHc, cs);
+ // the xn16 token stride of this chunk (the writers of both halves and of the fused write-backs use it)
+ const int64_t ldx = pf_hcdown() && T >= std::max<int64_t>(pf_switch_min_t(), 64) && !gr_unfused() &&
+ !m.xn16_lo ? D + XN_PAD : D;
if (gr_unfused()) gr_norm(m.R, (const float*) wn->data, EPS, m.xn, m.xn16, T, m.cs, m.xn16_lo);
- else if (!normed) gr_norm_rs(m.R, (const float*) wn->data, EPS, m.grs, m.xn16, T, m.cs, m.xn16_lo);
+ else if (!normed) gr_norm_rs(m.R, (const float*) wn->data, EPS, m.grs, m.xn16, T, m.cs, m.xn16_lo, ldx);
normed = false;
- if (!bf16_proj(m.gemm, wd, m.xn16, m.lo, T, sd, err, 0, m.xn16_lo)) return false;
+ bool hcd = false;
+ if (ldx != D) {
+ if (wd->kind != core::WeightKind::Bf16InF32 || !wd->data || wd->ne0 != D || wd->ne1 != LR ||
+ wi->kind != core::WeightKind::Bf16InF32 || !wi->data || wi->ne0 != D || wi->ne1 != HC ||
+ !strata_pf_hcdown_bf16(m.xn16, ldx, (const uint16_t*) wd->data, (const uint16_t*) wi->data, LR,
+ HC, m.lo, m.inj, T, D, m.cs)) {
+ err = "prefill: STRATA_PF_HCDOWN could not run the hyper-connection projections";
+ return false;
+ }
+ hcd = true;
+ }
+ if (!hcd && !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);
bool upmixed = false;
if (hc_upmix() && T >= pf_switch_min_t() && !gr_unfused() && !m.lo16_lo && !m.mixed_bf_lo &&
@@ -1471,7 +1495,7 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
}
}
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 (!hcd && !bf16_proj(m.gemm, wi, m.xn16, m.inj, T, si, err, 0, m.xn16_lo)) return false;
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);
@@ -2141,10 +2165,10 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
cudaMemcpyAsync(Rc, m.R, (size_t) T * D * 4, cudaMemcpyDeviceToDevice, m.cs);
gr_write(Rc, m.bo, m.inj, HC, T, m.cs);
strata::kernels::cvec_apply(Rc, l, T, D, nullptr, 0, nullptr, 0, false, m.cs);
- gr_norm_rs(Rc, (const float*) wnn->data, EPS, rc, xc, T, m.cs);
+ gr_norm_rs(Rc, (const float*) wnn->data, EPS, rc, xc, T, m.cs, nullptr, D);
gr_write_cvec_norm_rs(m.R, m.bo, m.inj, HC, cv_dir + l * N, cv_s + l, cv_on,
strata::kernels::cvec().mode, (const float*) wnn->data, EPS, m.grs,
- m.xn16, T, m.cs, m.xn16_lo);
+ m.xn16, T, m.cs, m.xn16_lo, ldx);
std::vector<float> a((size_t) T * D), b((size_t) T * D), ra((size_t) T * HC), rb((size_t) T * HC);
std::vector<uint16_t> xa((size_t) T * D), xb((size_t) T * D);
cudaMemcpyAsync(a.data(), m.R, a.size() * 4, cudaMemcpyDeviceToHost, m.cs);
@@ -2163,11 +2187,11 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
} else
gr_write_cvec_norm_rs(m.R, m.bo, m.inj, HC, cv_dir + l * N, cv_s + l, cv_on,
strata::kernels::cvec().mode, (const float*) wnn->data, EPS, m.grs, m.xn16,
- T, m.cs, m.xn16_lo);
+ T, m.cs, m.xn16_lo, ldx);
normed = true;
} else if (wnn) {
gr_write_norm_rs(m.R, m.bo, m.inj, HC, (const float*) wnn->data, EPS, m.grs, m.xn16, T, m.cs,
- m.xn16_lo);
+ m.xn16_lo, ldx);
normed = true;
} else {
gr_write(m.R, m.bo, m.inj, HC, T, m.cs);
diff --git a/src/prefill/wmma_gemm.cu b/src/prefill/wmma_gemm.cu
index e029f47c4..5c88fb3c0 100644
--- a/src/prefill/wmma_gemm.cu
+++ b/src/prefill/wmma_gemm.cu
@@ -459,8 +459,121 @@ __global__ void __launch_bounds__(256) kernel_bk64(const _Float16* __restrict__
}
#endif
}
+// S23 (STRATA_PF_HCDOWN=1): the hyper-connection read's down (N nd = 320) and inject (N ni = 4) projections of xn16
+// (BF16, T x 10240, token stride ldx: 10240 + 64 avoids the 4 KB-multiple stride) as ONE GEMM - weight rows
+// [w_down; w_inject] read from the two tensors, the epilogue splitting the columns into lo (T x nd) and inj (T x ni),
+// so xn16 is read once. 128 x 128 blocks, the BK 64 single-buffer scheme of kernel_bk64. s23/hcdown_probe.hip,
+// 16K tokens: 3.60 / 3.77 ms (hipBLASLt in the engine: down 6.78 + inject 1.81). FP32 accumulation in another k
+// order than hipBLASLt: rounding-level.
+typedef __bf16 hb16 __attribute__((ext_vector_type(16)));
+__device__ __forceinline__ hb16 bfrag(const uint16_t* p) {
+ const uint4 a = *reinterpret_cast<const uint4*>(p), b = *reinterpret_cast<const uint4*>(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(hb16, w);
+}
+__global__ void __launch_bounds__(256) kernel_hcdown(const uint16_t* __restrict__ X, int ldx,
+ const uint16_t* __restrict__ Wd, const uint16_t* __restrict__ Wi,
+ int nd, int ni, float* __restrict__ lo, float* __restrict__ inj,
+ int M, int K) {
+#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1150__) || defined(__gfx1151__)
+ constexpr int BN = 128, WN = 32, TN = 2, BK64 = 64, LDK64 = BK64 + 8;
+ __shared__ __align__(16) uint16_t sA[BM][LDK64];
+ __shared__ __align__(16) uint16_t sB[BN][LDK64];
+ const int N = nd + ni;
+ const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, l16 = lane & 15, hi = lane >> 4;
+ const int wm = wave & 1, wn = wave >> 1;
+ const int num_m = (M + BM - 1) / BM, num_n = (N + BN - 1) / BN, b = blockIdx.x;
+ const int group = b / (GM * num_n), first_m = group * GM, gsize = min(GM, num_m - first_m);
+ const int m0 = (first_m + (b % (GM * num_n)) % gsize) * BM, n0 = ((b % (GM * num_n)) / gsize) * BN;
+ const int sr = tid >> 3, sq = tid & 7;
+ auto ldA = [&](int k0, int r) -> uint4 {
+ return *reinterpret_cast<const uint4*>(X + (size_t) min(m0 + r, M - 1) * ldx + k0 + 8 * sq);
+ };
+ auto ldB = [&](int k0, int r) -> uint4 {
+ const int n = min(n0 + r, N - 1);
+ const uint16_t* row = n < nd ? Wd + (size_t) n * K : Wi + (size_t) (n - nd) * K;
+ return *reinterpret_cast<const uint4*>(row + k0 + 8 * sq);
+ };
+ uint4 ra0, ra1, ra2, ra3, rb0, rb1, rb2, rb3;
+ auto load = [&](int k0) {
+ ra0 = ldA(k0, sr); ra1 = ldA(k0, sr + 32); ra2 = ldA(k0, sr + 64); ra3 = ldA(k0, sr + 96);
+ rb0 = ldB(k0, sr); rb1 = ldB(k0, sr + 32); rb2 = ldB(k0, sr + 64); rb3 = ldB(k0, sr + 96);
+ };
+ auto store = [&]() {
+ *reinterpret_cast<uint4*>(&sA[sr][8 * sq]) = ra0; *reinterpret_cast<uint4*>(&sA[sr + 32][8 * sq]) = ra1;
+ *reinterpret_cast<uint4*>(&sA[sr + 64][8 * sq]) = ra2; *reinterpret_cast<uint4*>(&sA[sr + 96][8 * sq]) = ra3;
+ *reinterpret_cast<uint4*>(&sB[sr][8 * sq]) = rb0; *reinterpret_cast<uint4*>(&sB[sr + 32][8 * sq]) = rb1;
+ *reinterpret_cast<uint4*>(&sB[sr + 64][8 * sq]) = rb2; *reinterpret_cast<uint4*>(&sB[sr + 96][8 * sq]) = rb3;
+ };
+ f8 acc[4][TN];
+#pragma unroll
+ for (int i = 0; i < 4; ++i)
+#pragma unroll
+ for (int j = 0; j < TN; ++j) acc[i][j] = f8{0, 0, 0, 0, 0, 0, 0, 0};
+ load(0);
+ store();
+ __syncthreads();
+ const int ar = 64 * wm + l16, br = WN * wn + l16;
+ for (int k0 = 0; k0 < K; k0 += BK64) {
+ const bool more = k0 + BK64 < K;
+ if (more) load(k0 + BK64);
+#pragma unroll
+ for (int ks = 0; ks < BK64; ks += 16) {
+ const hb16 a0 = bfrag(&sA[ar][ks]), a1 = bfrag(&sA[ar + 16][ks]), a2 = bfrag(&sA[ar + 32][ks]),
+ a3 = bfrag(&sA[ar + 48][ks]);
+ const hb16 b0 = bfrag(&sB[br][ks]), b1 = bfrag(&sB[br + 16][ks]);
+ acc[0][0] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a0, b0, acc[0][0]);
+ acc[1][0] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a1, b0, acc[1][0]);
+ acc[2][0] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a2, b0, acc[2][0]);
+ acc[3][0] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a3, b0, acc[3][0]);
+ acc[0][1] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a0, b1, acc[0][1]);
+ acc[1][1] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a1, b1, acc[1][1]);
+ acc[2][1] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a2, b1, acc[2][1]);
+ acc[3][1] = __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a3, b1, acc[3][1]);
+ }
+ if (more) {
+ __syncthreads();
+ store();
+ __syncthreads();
+ }
+ }
+#pragma unroll
+ for (int i = 0; i < 4; ++i)
+#pragma unroll
+ for (int j = 0; j < TN; ++j) {
+ const int n = n0 + WN * wn + 16 * j + l16;
+ if (n >= N) continue;
+#pragma unroll
+ for (int e = 0; e < 8; ++e) {
+ const int m = m0 + 64 * wm + 16 * i + 2 * e + hi;
+ if (m < M) {
+ if (n < nd) lo[(size_t) m * nd + n] = acc[i][j][e];
+ else inj[(size_t) m * ni + (n - nd)] = acc[i][j][e];
+ }
+ }
+ }
+#endif
+}
} // namespace pfg
+bool strata_pf_hcdown_bf16(const uint16_t* X, int64_t ldx, const uint16_t* Wd, const uint16_t* Wi, int64_t nd,
+ int64_t ni, float* lo, float* inj, int64_t T, int64_t K, void* stream) {
+ if (!X || !Wd || !Wi || !lo || !inj || T < 64 || nd < 1 || ni < 0 || K % 64 != 0 || ldx < K || ldx % 8 != 0) return false;
+ if (T > (1LL << 30) || K > (1LL << 30) || ldx > (1LL << 30)) return false;
+ static const bool gfx11 = [] {
+ int dev = 0;
+ hipDeviceProp_t prop{};
+ if (hipGetDevice(&dev) != hipSuccess || hipGetDeviceProperties(&prop, dev) != hipSuccess) return false;
+ return std::strncmp(prop.gcnArchName, "gfx11", 5) == 0;
+ }();
+ if (!gfx11) return false;
+ const int64_t mt = (T + pfg::BM - 1) / pfg::BM;
+ const unsigned grid = (unsigned) (mt * ((nd + ni + 127) / 128));
+ pfg::kernel_hcdown<<<grid, 256, 0, static_cast<hipStream_t>(stream)>>>(X, (int) ldx, Wd, Wi, (int) nd, (int) ni,
+ lo, inj, (int) T, (int) K);
+ return hipGetLastError() == hipSuccess;
+}
+
bool strata_pf_gemm_f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t T, int64_t N, int64_t K, int64_t ldy,
float beta, void* stream) {
return strata_pf_gemm_f16_ld(X, K, W, K, Y, T, N, K, ldy, beta, stream);
@@ -510,6 +623,10 @@ bool strata_pf_gemm_f16_ld(const uint16_t*, int64_t, const uint16_t*, int64_t, f
int64_t, float, void*) {
return false;
}
+bool strata_pf_hcdown_bf16(const uint16_t*, int64_t, const uint16_t*, const uint16_t*, int64_t, int64_t, float*, float*,
+ int64_t, int64_t, void*) {
+ return false;
+}
bool strata_wmma_gemm_f16(const uint16_t*, const uint16_t*, float*,
int64_t, int64_t, int64_t, int64_t, float,
void*) {
diff --git a/src/prefill/wmma_gemm.h b/src/prefill/wmma_gemm.h
index f8e56c91c..cfe087a09 100644
--- a/src/prefill/wmma_gemm.h
+++ b/src/prefill/wmma_gemm.h
@@ -23,6 +23,11 @@ bool strata_pf_gemm_f16(const uint16_t* X, const uint16_t* W, float* Y, int64_t
float beta, void* stream = nullptr);
/// The same with row strides ldx >= K and ldw >= K (multiples of 8): a 4 KB-multiple row stride (K 2048 / 4096 /
/// 6144) camps on the memory channels, K + 64 does not (S23 gemm_probe7/8: K 6144 19 -> 35 TFLOPS).
+/// S23 (STRATA_PF_HCDOWN=1): lo[t, n] = sum_k X[t, k] Wd[n, k] (n < nd) and inj[t, n] = sum_k X[t, k] Wi[n, k]
+/// (n < ni) in one BF16 WMMA GEMM over X (T x K, token stride ldx >= K, a multiple of 8; Wd / Wi row-major, stride
+/// K); lo T x nd and inj T x ni, FP32. K a multiple of 64, T >= 64. False (nothing launched) otherwise / off gfx11.
+bool strata_pf_hcdown_bf16(const uint16_t* X, int64_t ldx, const uint16_t* Wd, const uint16_t* Wi, int64_t nd,
+ int64_t ni, float* lo, float* inj, int64_t T, int64_t K, void* stream = nullptr);
bool strata_pf_gemm_f16_ld(const uint16_t* X, int64_t ldx, const uint16_t* W, int64_t ldw, float* Y, int64_t T,
int64_t N, int64_t K, int64_t ldy, float beta, void* stream = nullptr);