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++
HIP (S6): RDNA4 matrix-core prompt attention for int8 KV, opt-in (STRATA_HIP_WMMA=1, gfx12 only)
Niko122173ba449679a8
diff --git a/CMakeLists.txt b/CMakeLists.txtindex a47c32e6d..3bc403e50 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -414,6 +414,12 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)add_test(NAME hip_prefill_native_batch COMMAND hip_prefill_native_batch)list(APPEND _strata_hip_tests hip_prefill_native_batch)endif()+ # S6: the RDNA4 matrix-core prompt attention (STRATA_HIP_WMMA) against the FP32 kernel and FP64; skips off gfx12+ add_executable(hip_prompt_attn_wmma src/kernels/qsa_prompt_attn_parity.cpp)+ target_link_libraries(hip_prompt_attn_wmma PRIVATE strata_kernels strata_hip_runtime)+ add_test(NAME hip_prompt_attn_wmma COMMAND hip_prompt_attn_wmma 32768 2048 3)+ set_tests_properties(hip_prompt_attn_wmma PROPERTIES TIMEOUT 600 SKIP_RETURN_CODE 77)+ list(APPEND _strata_hip_tests hip_prompt_attn_wmma)if(STRATA_HIPBLASLT_AVAILABLE AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/hip/prefill_hipblaslt_gemm.cpp")add_executable(hip_prefill_hipblaslt_gemm tests/hip/prefill_hipblaslt_gemm.cpp)target_include_directories(hip_prefill_hipblaslt_gemm PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/prefill")diff --git a/src/kernels/cuda/qsa_prompt_attn.cu b/src/kernels/cuda/qsa_prompt_attn.cuindex e87ec02c1..7273eb2f3 100644--- a/src/kernels/cuda/qsa_prompt_attn.cu+++ b/src/kernels/cuda/qsa_prompt_attn.cu@@ -9,6 +9,7 @@#include <cstdio>#include <cstdlib>+#include <cstring>#include <type_traits>namespace strata::kernels {@@ -685,6 +686,287 @@ bool launch(const float* q, const QsaAttnPools& pools, const int32_t* ids, constreturn true;}+#if defined(__HIPCC__)+// ---- S6: the int8-KV prompt attention on RDNA4 matrix cores (opt-in: STRATA_HIP_WMMA=1, gfx12 only). The design of+// the v2 kernel above with gfx12's v_wmma_f32_16x16x16_f16 (wave32) in place of m16n8k16: wave w owns dims+// [64w, 64w+64) (int8 scale group w) for q.k and p.v; q and p are split into FP16 hi + lo parts, the int8 codes enter+// exactly as FP16, the scales are applied in FP32 and the four groups' q.k partials are added in a fixed order.+// FP32-level accuracy, deterministic, but not bitwise equal to qsa_decode_attn_batch (another summation order).+// Fragment layout (16x16x16, wave32, checked on gfx1201): A lane l holds A[l % 16][(l / 16) * 8 + i], B lane l holds+// B[(l / 16) * 8 + i][l % 16], C/D lane l holds D[(l / 16) * 8 + i][l % 16], i = 0..7.+#if defined(__gfx1200__) || defined(__gfx1201__)+#define STRATA_PA_WMMA 1+#else+#define STRATA_PA_WMMA 0+#endif+typedef _Float16 wh8 __attribute__((ext_vector_type(8)));+typedef float wf8 __attribute__((ext_vector_type(8)));+constexpr int WCH = 32; // cells per chunk (two 16-cell tiles)+constexpr int WVS = 80; // staged V row stride in bytes (64 codes, 16-byte aligned, banks spread)+struct alignas(16) SmemW {+ float part[4][16][WCH + 1]; // q.k per dim group+ float p[16][WCH + 1];+ float vs[4][WCH]; // V scale per (group, cell)+ int valid[WCH]; // the cell has a resident pool row+ uint8_t v[4][WCH][WVS]; // per wave: the chunk's V codes of its group, one row per cell+ float qmax[4];+ float alpha[16];+ float lsum[16];+ float mrow[16];+};++__device__ __forceinline__ wf8 wmma_f16(wh8 a, wh8 b, wf8 c) {+#if STRATA_PA_WMMA+ return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(a, b, c);+#else+ __builtin_trap();+ return c;+#endif+}++__device__ __forceinline__ wh8 i8x8_to_h8(uint2 x) { // exact: |code| <= 128+ wh8 h;+#pragma unroll+ for (int i = 0; i < 4; ++i) {+ h[i] = (_Float16) (int) (int8_t) (x.x >> (8 * i));+ h[4 + i] = (_Float16) (int) (int8_t) (x.y >> (8 * i));+ }+ return h;+}++__global__ void __launch_bounds__(THREADS) prompt_attn_wmma_kernel(const float* __restrict__ q, QsaAttnPools p,+ const int32_t* __restrict__ ids,+ const int32_t* __restrict__ steps, int n_kv_heads,+ int page_size, float scale_log2,+ float* __restrict__ attn, int cap) {+#if STRATA_PA_WMMA+ __shared__ SmemW S;+ const int qi = blockIdx.x, kvh = blockIdx.y;+ const int n_head = n_kv_heads * G;+ q += (size_t) qi * n_head * HD + (size_t) kvh * G * HD;+ attn += (size_t) qi * n_head * HD + (size_t) kvh * G * HD;+ ids += (size_t) qi * cap;+ const int n = __ldg(steps + (size_t) qi * kStepCount + kStepWidth);+ const int t = threadIdx.x, lane = t & 31, warp = t >> 5;+ const int col = lane & 15, half = lane >> 4; // fragment column / k half+ const int dim0 = warp * 64;++ float qm = 0.0f;+ for (int i = t; i < G * HD; i += THREADS) qm = fmaxf(qm, fabsf(q[i]));+#pragma unroll+ for (int o = 16; o > 0; o >>= 1) qm = fmaxf(qm, __shfl_xor_sync(0xffffffffu, qm, o));+ if (lane == 0) S.qmax[warp] = qm;+ if (t < 16) { S.mrow[t] = -INFINITY; S.lsum[t] = 0.0f; }+ __syncthreads();+ qm = fmaxf(fmaxf(S.qmax[0], S.qmax[1]), fmaxf(S.qmax[2], S.qmax[3]));+ int qe = 0;+ if (qm > 0.0f) frexpf(qm, &qe);+ const float qup = ldexpf(1.0f, 14 - qe), qdown = ldexpf(scale_log2, qe - 14);+ // q A fragments of this wave's 64 dims: row = col (heads 12..15 zero), k = dim0 + kk*16 + half*8 + i+ wh8 qh[4], ql[4];+#pragma unroll+ for (int kk = 0; kk < 4; ++kk) {+ float x[8];+ if (col < G) {+ const float4* src = reinterpret_cast<const float4*>(q + (size_t) col * HD + dim0 + kk * 16 + half * 8);+ const float4 a = src[0], b = src[1];+ x[0] = a.x; x[1] = a.y; x[2] = a.z; x[3] = a.w; x[4] = b.x; x[5] = b.y; x[6] = b.z; x[7] = b.w;+ } else {+#pragma unroll+ for (int i = 0; i < 8; ++i) x[i] = 0.0f;+ }+#pragma unroll+ for (int i = 0; i < 8; ++i) {+ const float v = x[i] * qup;+ const _Float16 hi = (_Float16) v;+ qh[kk][i] = hi;+ ql[kk][i] = (_Float16) (v - (float) hi);+ }+ }++ wf8 acc[4];+#pragma unroll+ for (int j = 0; j < 4; ++j) acc[j] = wf8{0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};++ const int n_chunks = (n + WCH - 1) / WCH;+ for (int ci = 0; ci < n_chunks; ++ci) {+ const int c0 = ci * WCH;+ // this lane's cell of the chunk (lane = cell index): its pool row, or -1 (past the selection, or a block the+ // KV streaming left non-resident: masked as in the decode kernel)+ long long myrow = -1;+ if (c0 + lane < n) {+ const int cell = __ldg(ids + c0 + lane);+ const long long page = (long long) __ldg(p.page_table + cell / page_size);+ if (page >= 0) myrow = (page * n_kv_heads + kvh) * page_size + (cell % page_size);+ }+ // stage this wave's V slice of the chunk (64 bytes of each row) and the V scale; the K fragments come straight+ // from global memory (8 consecutive codes per lane)+ {+ uint4 v0 = make_uint4(0, 0, 0, 0), v1 = v0, v2 = v0, v3 = v0;+ float vsc = 0.0f;+ if (myrow >= 0) {+ const uint4* src = reinterpret_cast<const uint4*>(p.v_q + myrow * HD + dim0);+ v0 = __ldg(src); v1 = __ldg(src + 1); v2 = __ldg(src + 2); v3 = __ldg(src + 3);+ vsc = __half2float(__ushort_as_half(__ldg(p.v_scale + myrow * (HD / KV_Q8_GROUP) + warp)));+ }+ uint4* dst = reinterpret_cast<uint4*>(&S.v[warp][lane][0]);+ dst[0] = v0; dst[1] = v1; dst[2] = v2; dst[3] = v3;+ S.vs[warp][lane] = vsc;+ if (warp == 0) S.valid[lane] = myrow >= 0;+ }+ // q.k over this wave's 64 dims for two 16-cell tiles, times the cell's K scale for this group+#pragma unroll+ for (int nt = 0; nt < 2; ++nt) {+ const long long rr = __shfl(myrow, nt * 16 + col);+ wf8 s = wf8{0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};+ float ksc = 0.0f;+ if (rr >= 0) ksc = __half2float(__ushort_as_half(__ldg(p.k_scale + rr * (HD / KV_Q8_GROUP) + warp)));+#pragma unroll+ for (int kk = 0; kk < 4; ++kk) {+ uint2 kx = make_uint2(0, 0);+ if (rr >= 0) kx = __ldg(reinterpret_cast<const uint2*>(p.k_q + rr * HD + dim0 + kk * 16 + half * 8));+ const wh8 b = i8x8_to_h8(kx);+ s = wmma_f16(qh[kk], b, s);+ s = wmma_f16(ql[kk], b, s);+ }+#pragma unroll+ for (int i = 0; i < 8; ++i) S.part[warp][half * 8 + i][nt * 16 + col] = s[i] * ksc;+ }+ __syncthreads();+ // online softmax over the four groups' sum (fixed order): row t/8, 4 cells per thread+ {+ const int r = t >> 3, sub = t & 7;+ float x[4], mx = -INFINITY;+#pragma unroll+ for (int j = 0; j < 4; ++j) {+ const int c = sub * 4 + j;+ // past the selection, or a masked cell (non-resident page, as in the decode kernel): no weight+ x[j] = S.valid[c] ? (((S.part[0][r][c] + S.part[1][r][c]) + S.part[2][r][c]) + S.part[3][r][c]) * qdown+ : -INFINITY;+ mx = fmaxf(mx, x[j]);+ }+#pragma unroll+ for (int o = 1; o < 8; o <<= 1) mx = fmaxf(mx, __shfl_xor_sync(0xffffffffu, mx, o));+ const float m_old = S.mrow[r];+ const float m_new = fmaxf(m_old, mx);+ float sum = 0.0f;+#pragma unroll+ for (int j = 0; j < 4; ++j) {+ const float e = x[j] == -INFINITY ? 0.0f : exp2f(x[j] - m_new);+ S.p[r][sub * 4 + j] = e;+ sum += e;+ }+#pragma unroll+ for (int o = 1; o < 8; o <<= 1) sum += __shfl_xor_sync(0xffffffffu, sum, o);+ if (sub == 0) {+ const float a = m_old == -INFINITY ? 0.0f : exp2f(m_old - m_new);+ S.alpha[r] = a;+ S.lsum[r] = fmaf(S.lsum[r], a, sum);+ S.mrow[r] = m_new;+ }+ }+ __syncthreads();+ // p.v over this wave's 64 dims: 4 tiles of 16 dims, 2 k-steps of 16 cells+ {+ float vmax = S.vs[warp][lane];+#pragma unroll+ for (int o = 16; o > 0; o >>= 1) vmax = fmaxf(vmax, __shfl_xor_sync(0xffffffffu, vmax, o));+ const float vup = vmax > 0.0f ? 16384.0f / vmax : 0.0f, vdown = vmax * (1.0f / 16384.0f);+ wf8 tmp[4];+#pragma unroll+ for (int j = 0; j < 4; ++j) tmp[j] = wf8{0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};+#pragma unroll+ for (int ks = 0; ks < 2; ++ks) {+ const int cb = ks * 16 + half * 8; // this lane's 8 cells+ wh8 ah, al;+#pragma unroll+ for (int i = 0; i < 8; ++i) {+ const float pv = S.p[col][cb + i] * (S.vs[warp][cb + i] * vup);+ const _Float16 hi = (_Float16) pv;+ ah[i] = hi;+ al[i] = (_Float16) (pv - (float) hi);+ }+#pragma unroll+ for (int j = 0; j < 4; ++j) {+ const int d = j * 16 + col;+ wh8 b;+#pragma unroll+ for (int i = 0; i < 8; ++i) b[i] = (_Float16) (int) (int8_t) S.v[warp][cb + i][d];+ tmp[j] = wmma_f16(ah, b, tmp[j]);+ tmp[j] = wmma_f16(al, b, tmp[j]);+ }+ }+ float a[8];+#pragma unroll+ for (int i = 0; i < 8; ++i) a[i] = S.alpha[half * 8 + i];+#pragma unroll+ for (int j = 0; j < 4; ++j)+#pragma unroll+ for (int i = 0; i < 8; ++i) acc[j][i] = fmaf(acc[j][i], a[i], tmp[j][i] * vdown);+ }+ // no barrier here: `v`/`vs` are this wave's own, and `p`/`alpha`/`valid` are rewritten only after the next+ // chunk's first barrier (which every wave reaches after its p.v)+ }+ float inv[8];+#pragma unroll+ for (int i = 0; i < 8; ++i) {+ const float l = S.lsum[half * 8 + i];+ inv[i] = l > 0.0f ? 1.0f / l : 0.0f;+ }+#pragma unroll+ for (int j = 0; j < 4; ++j)+#pragma unroll+ for (int i = 0; i < 8; ++i) {+ const int row = half * 8 + i;+ if (row < G) attn[(size_t) row * HD + dim0 + j * 16 + col] = acc[j][i] * inv[i];+ }+#else+ __builtin_trap();+#endif+}++// gfx12 (RDNA4) only, and only on request: the output differs from the default kernel's in its last bits+bool hip_wmma_usable() {+ static const bool want = [] {+ const char* e = std::getenv("STRATA_HIP_WMMA");+ return e != nullptr && e[0] == '1';+ }();+ if (!want) return false;+ static int arch[64] = {}; // 0 unknown, 1 gfx12, 2 other+ int dev = 0;+ if (hipGetDevice(&dev) != hipSuccess || dev < 0 || dev >= 64) { (void) hipGetLastError(); return false; }+ if (arch[dev] == 0) {+ hipDeviceProp_t prop{};+ if (hipGetDeviceProperties(&prop, dev) != hipSuccess) { (void) hipGetLastError(); return false; }+ arch[dev] = std::strncmp(prop.gcnArchName, "gfx12", 5) == 0 ? 1 : 2;+ static bool told = false;+ if (!told) {+ told = true;+ std::fprintf(stderr, "strata: STRATA_HIP_WMMA: the prompt attention on matrix cores %s (%s)\n",+ arch[dev] == 1 ? "on" : "unavailable", prop.gcnArchName);+ }+ }+ return arch[dev] == 1;+}++bool launch_wmma(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap,+ const QsaShapes& s, float* attn, int64_t n_q, cudaStream_t st) {+ const float scale_log2 = 1.4426950408889634f / sqrtf((float) HD);+ for (int64_t q0 = 0; q0 < n_q; q0 += 65535) {+ const int64_t nb = n_q - q0 < 65535 ? n_q - q0 : 65535;+ prompt_attn_wmma_kernel<<<dim3((unsigned) nb, (unsigned) s.n_head_kv), THREADS, 0, st>>>(+ q + q0 * s.n_head * HD, pools, ids + q0 * cap, steps + q0 * kStepCount, (int) s.n_head_kv,+ (int) s.page_size, scale_log2, attn + q0 * s.n_head * HD, (int) cap);+ }+ const cudaError_t e = cudaGetLastError();+ if (e != cudaSuccess) {+ std::fprintf(stderr, "qsa_prompt_attn_batch (wmma): %s\n", cudaGetErrorString(e));+ std::exit(1);+ }+ return true;+}+#endif+} // namespacebool qsa_prompt_attn_batch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps,@@ -709,7 +991,14 @@ bool qsa_prompt_attn_batch(const float* q, const QsaAttnPools& pools, const int3turing = cc_major[dev] < 8;}#if defined(__HIPCC__)- return false; // the tensor-core kernel is compiled out on AMD (its major version is not a CUDA sm)+ // the tensor-core kernels are compiled out on AMD (its major version is not a CUDA sm); RDNA4 has its own int8-KV+ // matrix-core kernel, opt-in (STRATA_HIP_WMMA=1); everything else keeps the old kernel+ (void) turing;+ if (pools.k_q != nullptr && pools.v_q != nullptr && pools.k_scale != nullptr && pools.v_scale != nullptr &&+ pools.k_q4 == nullptr && pools.v_q4 == nullptr && s.head_dim == HD && s.n_head == (int64_t) G * s.n_head_kv &&+ cap > 0 && ids && steps && pools.page_table && hip_wmma_usable())+ return launch_wmma(q, pools, ids, steps, cap, s, attn, n_q, (cudaStream_t) stream);+ return false;#endifif (pools.k_q4 != nullptr || s.head_dim != HD || s.n_head != (int64_t) G * s.n_head_kv || cap <= 0 || !ids ||!steps || !pools.page_table)diff --git a/src/kernels/qsa_prompt_attn_parity.cpp b/src/kernels/qsa_prompt_attn_parity.cppindex 3b3fe1945..9525d0cfc 100644--- a/src/kernels/qsa_prompt_attn_parity.cpp+++ b/src/kernels/qsa_prompt_attn_parity.cpp@@ -7,6 +7,7 @@// 1. against FP64, the new kernel's error is no larger than a small multiple of the old kernel's (both FP32 math);// 2. the new and old outputs agree to a relative 1e-4 of the output scale;// then times both over a prompt chunk (the old one in batches of 32, as prefill.cpp calls it).+// HIP builds (S6): the same checks for the RDNA4 matrix-core kernel (STRATA_HIP_WMMA), skipped (77) off gfx12.// Usage: qsa_prompt_attn_parity [context=32768] [queries=2048] [reps=5]#include "strata/kernels/qsa.hpp"#include "strata/kernels/qsa_decode_attn.hpp"@@ -19,6 +20,7 @@#include <cmath>#include <cstdio>#include <cstdlib>+#include <cstring>#include <random>#include <vector>@@ -196,6 +198,23 @@ int run(int fmt, int64_t ctx, int64_t nq, int reps) { // fmt 1 int8, 0 fp16} // namespaceint main(int argc, char** argv) {+#if defined(__HIP_PLATFORM_AMD__)+ // S6: on AMD the kernel under test is the RDNA4 matrix-core one (opt-in in the engine); other cards skip+ {+ int dev = 0;+ hipDeviceProp_t prop{};+ if (hipGetDevice(&dev) != hipSuccess || hipGetDeviceProperties(&prop, dev) != hipSuccess) return 2;+ if (std::strncmp(prop.gcnArchName, "gfx12", 5) != 0) {+ std::printf("SKIP: %s is not gfx12 (the matrix-core prompt attention is RDNA4 only)\n", prop.gcnArchName);+ return 77;+ }+#if defined(_WIN32)+ _putenv_s("STRATA_HIP_WMMA", "1");+#else+ setenv("STRATA_HIP_WMMA", "1", 1);+#endif+ }+#endifconst int64_t ctx = argc > 1 ? std::atoll(argv[1]) : 32768;const int64_t nq = argc > 2 ? std::atoll(argv[2]) : 2048;const int reps = argc > 3 ? std::atoi(argv[3]) : 5;