From cca3dd2e33dd71de65c44bca5258b819b0badf8b Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 26 Oct 2026 15:07:37 +0100 Subject: [PATCH] dense GEMM: the chunked prompt's gemm() prefers the engine's own RDNA3 WMMA kernel (src/prefill/wmma_gemm.cu, now linked) over hipBLAS - 4.97 vs 20-40 TFLOPS at our shapes The dense GEMM was 11.1 s of a 4K prompt at hipBLAS's measured 4.97 TFLOPS (M 4096 N 2304 K 5120, the engine's call convention). A blocked WMMA kernel reaches 23.96 TFLOPS at that shape (128x128 tile, BK 32, 2-stage LDS, rocwmma; scripts/wmma2.cu) and the engine's own tuned kernel (BM 128 x BN 256, grouped tile order, raw wmma intrinsics) 20.8-20.9 at the wq_b shape and 38-41 at M 16384. gemm() now calls strata_pf_gemm_f16_ld with hipBLAS as the fallback (rows < 64, N < 512, K % 32, unaligned X: unchanged); STRATA_DENSE_PFGEMM=0 restores hipBLAS for an A/B. Same operands, the kernel's own k order (rounding-level). Measured on the box (eng-028, 4E224E7F -> 467D7320, 4K prompt fixture p4k, chunk 16384): 46.4 -> 38.1 s, 88.30 -> 107.63 tok/s; with DS4_SETTLE_MS=0;DS4_SETTLE_STORE=0 27.5 s = 148.98 tok/s. 8K 134.66 -> 218.31, 16K 158.6 -> 290.97, 32K 178.9 -> 316.92 (settle off). Decode unchanged in an interleaved A/B (old 15.17/16.73, new 17.38/18.48 tok/s) with acceptance 151/164 = 92% (93/98/100/100/90 per position). c.A (the attention slab, the reason each dense matrix is re-read 10-16x a prompt) takes STRATA_DS4_A_MB: 32 gives 107.63 -> 110.67, small because the expert reads now dominate. --- CMakeLists.txt | 6 ++++-- src/dsv41/ds4_model.cu | 23 +++++++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4e6e8a1..6e7a9b67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -822,8 +822,10 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) endif() set_source_files_properties(src/dsv41/ds4_main.cpp PROPERTIES COMPILE_DEFINITIONS "DS4_BUILD_ID=\"${STRATA_BUILD_ID}\"") target_include_directories(strata-dsv41 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories} - ${CMAKE_CURRENT_SOURCE_DIR}/third_party/ggml) - target_link_libraries(strata-dsv41 PRIVATE strata_dsv41_kernels ${_strata_gpu_runtime_target} ${_strata_gpu_blas_target}) + ${CMAKE_CURRENT_SOURCE_DIR}/third_party/ggml ${CMAKE_CURRENT_SOURCE_DIR}/src/prefill) + # strata_prefill: the RDNA3 WMMA dense GEMM (src/prefill/wmma_gemm.cu), which gemm() prefers over hipBLAS on gfx11 (measured + # 4.97 -> 20-40 TFLOPS on this part: the chunked prompt's dense GEMM is 11.1 s of a 4K prompt at the library's rate) + target_link_libraries(strata-dsv41 PRIVATE strata_prefill strata_dsv41_kernels ${_strata_gpu_runtime_target} ${_strata_gpu_blas_target}) find_package(Threads REQUIRED) target_link_libraries(strata-dsv41 PRIVATE Threads::Threads) if(WIN32 AND STRATA_ENABLE_HIP) # cudaMemGetInfo is the DXGI-budgeted hip_compat::mem_get_info (src/core/device.cu): on a Strix Halo the diff --git a/src/dsv41/ds4_model.cu b/src/dsv41/ds4_model.cu index 9e983c6f..807ac003 100644 --- a/src/dsv41/ds4_model.cu +++ b/src/dsv41/ds4_model.cu @@ -30,6 +30,7 @@ #include "strata/kernels/f16_bits.hpp" #include "strata/dsv41/moe_win.hpp" #include "strata/dsv41/sampler.hpp" +#include "wmma_gemm.h" // strata_pf_gemm_f16_ld: the RDNA3 WMMA dense GEMM (gemm() prefers it over hipBLAS on gfx11; -Isrc/prefill) #include #include @@ -2972,7 +2973,13 @@ struct Model::Impl { split128(g.ff, nm, 0, ff0_, FF); // (the widest member's share: member 0 takes the odd blocks) const size_t C = (size_t) chunk; c.C = chunk; - c.A = std::max(64, (int) (((size_t) 8 << 20) / (size_t) HQ) / 64 * 64); + // the attention slab's rows: 8 MB of q by default. Every dense GEMM of a layer is issued once a slab, so the slab + // width divides the prompt's dense weight traffic (the matrices are re-read once a slab): STRATA_DS4_A_MB widens it. + static const size_t a_mb = [] { + const char* v = std::getenv("STRATA_DS4_A_MB"); + return (size_t) (v != nullptr ? std::max(1, std::atoi(v)) : 8); + }(); + c.A = std::max(64, (int) ((a_mb << 20) / (size_t) HQ) / 64 * 64); // (the indexer's slab: its scores rows are the context wide - about 64 MB of them) c.AI = std::max(32, std::min(128, (int) (((size_t) 64 << 20) / (((size_t) o.max_context + 64) * 4)) / 32 * 32)); c.w16_elems = (size_t) 8 << 20; @@ -3041,15 +3048,27 @@ struct Model::Impl { return true; } - // y[r][c] = x16[r] . W[:, c] (+ beta y) for r < rows: W reconstructed to FP16 in whole 128-column blocks, cuBLAS + // y[r][c] = x16[r] . W[:, c] (+ beta y) for r < rows: W reconstructed to FP16 in whole 128-column blocks, then the product. + // The product prefers the engine's own RDNA3 WMMA kernel (src/prefill/wmma_gemm.cu) over hipBLAS: on gfx1151 hipBLAS + // measures 4.97 TFLOPS at this call convention while that kernel measures 20-40 (BM 128 x BN 256, grouped tile order), and + // the chunked prompt's dense GEMM is 11.1 s of a 4K prompt at the library's rate. Same operands and the same values (the + // k order differs, so it is rounding-level: the quality gate and the acceptance gate cover it); STRATA_DENSE_PFGEMM=0 keeps + // hipBLAS for an A/B. bool gemm(Dev& d, Chunk& c, const x3::Mat& m, const uint16_t* x16, int ldx, int rows, float* y, int ldy, float beta, std::string& err) { if (rows <= 0) return true; + static const bool pf = [] { + const char* v = std::getenv("STRATA_DENSE_PFGEMM"); + return v == nullptr || v[0] != '0'; + }(); const int cols = std::max(128, (int) std::min((size_t) m.n, c.w16_elems / (size_t) m.k) / 128 * 128); for (int c0 = 0; c0 < m.n; c0 += cols) { const int nc = std::min(cols, m.n - c0); if (m.fmt != 0) gg::rows(nc == m.n ? m : gg::view(m, c0, nc), c.w16, false, d.st); // (a GGUF's) else x3::reconstruct_rows(nc == m.n ? m : x3::view(m, c0, nc), c.w16, false, d.st); + if (pf && rows >= 64 && ((uintptr_t) x16 & 15u) == 0 && + strata_pf_gemm_f16_ld(x16, ldx, c.w16, m.k, y + c0, rows, nc, m.k, ldy, beta, d.st)) + continue; const float one = 1.0f; const cublasStatus_t st = cublasGemmEx(c.blas, CUBLAS_OP_T, CUBLAS_OP_N, nc, rows, m.k, &one, c.w16, CUDA_R_16F, m.k, x16, CUDA_R_16F, ldx, &beta, y + c0, CUDA_R_32F, ldy,