From 02833a75ac041d9c566289e42ed00fcc064dcf9e Mon Sep 17 00:00:00 2001 From: cobra91 Date: Sat, 5 Dec 2026 12:09:09 +0100 Subject: [PATCH 1/2] cuda: enable IQ2_XXS/IQ2_XS/IQ2_S (16/17/22) in STRATA_D_FMTS --- engine/src/kernels/cuda/iq_kernels.cu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engine/src/kernels/cuda/iq_kernels.cu b/engine/src/kernels/cuda/iq_kernels.cu index c4e835aaf6..7bad20a657 100644 --- a/engine/src/kernels/cuda/iq_kernels.cu +++ b/engine/src/kernels/cuda/iq_kernels.cu @@ -958,9 +958,9 @@ STRATA_TQP_FMT(149) // Q5_K / Q6_K downs: Qwen3.6-35B-A3B (FF 512, whole 256-blocks per row); Flash-Next's FF 640 never selects them // Q4_K / IQ3_XXS / IQ3_S / Q3_K / Q2_K as well: Unsloth UD-* and heretic down experts #ifdef STRATA_Q6K_EXPERTS -#define STRATA_D_FMTS(X) X(18) X(20) X(21) X(23) X(42) X(19) X(7) X(6) X(10) X(11) X(12) X(2) X(3) X(8) X(13) X(14) X(40) X(144) X(146) X(147) X(148) X(149) +#define STRATA_D_FMTS(X) X(16) X(17) X(18) X(20) X(21) X(22) X(23) X(42) X(19) X(7) X(6) X(10) X(11) X(12) X(2) X(3) X(8) X(13) X(14) X(40) X(144) X(146) X(147) X(148) X(149) #else -#define STRATA_D_FMTS(X) X(18) X(20) X(21) X(23) X(42) X(19) X(7) X(6) X(10) X(11) X(12) X(2) X(3) X(8) X(13) X(40) X(144) X(146) X(147) X(148) X(149) +#define STRATA_D_FMTS(X) X(16) X(17) X(18) X(20) X(21) X(22) X(23) X(42) X(19) X(7) X(6) X(10) X(11) X(12) X(2) X(3) X(8) X(13) X(40) X(144) X(146) X(147) X(148) X(149) #endif #define STRATA_MMVQ_FMTS(X) X(16) X(17) X(18) X(20) X(21) X(22) X(23) X(29) X(19) X(42) X(10) X(11) X(12) X(13) X(7) X(6) X(2) X(3) X(8) X(40) X(144) X(145) X(146) X(147) X(148) X(149) template<> struct Fmt<40> { static constexpr int qk = 64, ipb = 2, step = 4; // NVFP4: 2 calls of 32 values From 85c82bffa40dac67992ffdb3c7b19815a5ffa822 Mon Sep 17 00:00:00 2001 From: cobra91 Date: Sat, 5 Dec 2026 12:52:00 +0100 Subject: [PATCH 2/2] cuda: port Qwen4 sm_120 optimizations (#7865, #7878, #7879) and IQ2/IQ3 lds_down_ok --- engine/cmake/strata_gpu_engine.cmake | 6 + engine/include/strata/core/verify.hpp | 7 + engine/include/strata/kernels/bf16_gemv.hpp | 14 + .../strata/kernels/qsa_decode_attn.hpp | 12 +- engine/src/core/verify.cpp | 72 +- engine/src/kernels/cuda/iq_kernels.cu | 366 +++++- engine/src/kernels/cuda/native_bf16.cu | 1137 +++++++++-------- engine/src/kernels/cuda/qsa_decode_attn.cu | 92 +- engine/src/kernels/iq_multi_parity.cpp | 15 + engine/src/kernels/mmvf_rows_parity.cpp | 143 ++- engine/src/kernels/qsa_slots_parity.cpp | 88 +- terminal/data/settings-register.json | 140 ++ 12 files changed, 1507 insertions(+), 585 deletions(-) diff --git a/engine/cmake/strata_gpu_engine.cmake b/engine/cmake/strata_gpu_engine.cmake index 8415f94cb4..4943cd6f58 100644 --- a/engine/cmake/strata_gpu_engine.cmake +++ b/engine/cmake/strata_gpu_engine.cmake @@ -120,6 +120,9 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) endif() endif() target_link_libraries(strata_kernels PUBLIC strata_core strata_artifact) + if(STRATA_ENABLE_CUDA) + target_link_libraries(strata_kernels PUBLIC CUDA::cublas) + endif() # Exact gfx1100 prompt scorer (PR1825 method port): optional standalone test, # also available where the published source omits tests/. option(STRATA_PARITY_GFX1100_QSA_SCORES "Build the exact gfx1100 QSA score/ID check" OFF) @@ -865,6 +868,9 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) # the prompt path's DeltaNet recurrence: gdn_rec_kh_kernel against gdn_rec_cols_pipe_kernel, bitwise (GPU, # synthetic, no model; --bench adds timings). NVIDIA only: cp.async and the CUDA occupancy API (not on gfx906) if(STRATA_ENABLE_CUDA AND NOT STRATA_HIP_GFX906) + # Mode 8 fused gate/up + LDS down with the n_ff == 512 sub-warp / 512-thread paths enabled explicitly (so non-sm_120 CI GPUs cover them too) + add_test(NAME iq_multi_parity_exp8 COMMAND iq_multi_parity) + set_tests_properties(iq_multi_parity_exp8 PROPERTIES ENVIRONMENT "STRATA_KQ_SPLIT=1;STRATA_EXP_MODE=8;STRATA_GGUF_512_OPT=1") # Independent pinned expert uploads: byte parity and stream/event ordering. add_executable(dma_batch_parity tests/core/dma_batch_parity.cu) target_include_directories(dma_batch_parity PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/engine/include/strata/core/verify.hpp b/engine/include/strata/core/verify.hpp index b142101ecb..8b227fc392 100644 --- a/engine/include/strata/core/verify.hpp +++ b/engine/include/strata/core/verify.hpp @@ -41,6 +41,10 @@ #include #include +#if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL) +namespace strata::kernels { struct Bf16CublasCtx; } +#endif + namespace strata::core { /// 0.1.43 (STRATA_ADAPT_OVERLAP): the adaptive tier's copies still landing while a decode window runs. The window's @@ -671,6 +675,9 @@ class Verifier { int64_t last_pos0_ = 0; int32_t last_tokens_[strata::kernels::kVerifyLongT] = {}; int64_t n_vocab_ = 0; +#if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL) + strata::kernels::Bf16CublasCtx* bf16_cublas_ = nullptr; // main stream only; serialized graph replays +#endif cudaStream_t cs_ = nullptr; cudaStream_t sh_cs_ = nullptr; cudaEvent_t ev_fork_ = nullptr, ev_join_ = nullptr; diff --git a/engine/include/strata/kernels/bf16_gemv.hpp b/engine/include/strata/kernels/bf16_gemv.hpp index 19c4207f35..6fe2340cb7 100644 --- a/engine/include/strata/kernels/bf16_gemv.hpp +++ b/engine/include/strata/kernels/bf16_gemv.hpp @@ -67,4 +67,18 @@ bool bf16_gemv_fp32_mmvf_multi_aux(const float* x, int64_t ldx, const uint16_t* int64_t n_in, int64_t n_out, int n_tok, const uint16_t* w_aux, float* y_aux, int64_t ldy_aux, void* stream); +#if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL) +// Each context is bound to one stream for its lifetime. Calls and graph replays +// using the context must be serialized on that stream; destroy after replays finish. +struct Bf16CublasCtx; +Bf16CublasCtx* bf16_gemm_cublas_init(int device, void* stream, int max_tokens, int max_k); +void bf16_gemm_cublas_destroy(Bf16CublasCtx* ctx); +// Stage rounds FP32 inputs to BF16. GEMM uses FP32 accumulation, so results +// need not match the FP32-activation MMVF and can change router decisions. +// The staged input is shared by subsequent GEMMs and overwritten by the next stage call. +void bf16_gemm_cublas_stage(Bf16CublasCtx* ctx, const float* x, int64_t ldx, int n_in, int n_tok); +void bf16_gemm_cublas_f32in(Bf16CublasCtx* ctx, const uint16_t* w, float* y, int64_t ldy, + int n_in, int n_out, int n_tok); +#endif + } // namespace strata::kernels diff --git a/engine/include/strata/kernels/qsa_decode_attn.hpp b/engine/include/strata/kernels/qsa_decode_attn.hpp index d7747ba487..819eb09062 100644 --- a/engine/include/strata/kernels/qsa_decode_attn.hpp +++ b/engine/include/strata/kernels/qsa_decode_attn.hpp @@ -51,19 +51,17 @@ void qsa_decode_attn_step(const float* q, const QsaAttnPools& pools, const int32 void qsa_decode_attn_batch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap, const QsaShapes& s, float* scratch, float* attn, int64_t n_q, void* stream); -/// Up to this many queries of different slots in one qsa_decode_attn_slots call. -constexpr int kQsaAttnMaxSlots = 8; +/// Host-side descriptor capacity; individual device kernels use compact or smaller by-value descriptors. +constexpr int kQsaAttnMaxSlots = 64; -/// One KV pool set per query of a qsa_decode_attn_slots call (passed by value as a kernel argument). +/// One KV pool set per query of a qsa_decode_attn_slots call (host-side descriptor). struct QsaAttnSlotPools { QsaAttnPools p[kQsaAttnMaxSlots]; }; -static_assert(sizeof(QsaAttnSlotPools) <= 2048, "sm_86 4KB kernel param limit"); /// qsa_decode_attn_batch for `n_q` queries that each attend over their OWN pools (`pools.p[i]`, one KV cache per batch -/// slot): one launch instead of one call per query, every block computing exactly what the per-query call computes. -/// The pools must all have one KV format. False (nothing launched) where the per-query call would run another kernel -/// (n_q > kQsaAttnMaxSlots, mixed formats, the pre-sm_75 / gfx103x or gfx906 variants): the caller loops instead. +/// slot): the same per-query arithmetic. STRATA_QSA_ATTN_BATCH64=1 enables up to 64 homogeneous slots; +/// otherwise calls split into groups of at most 8. Unsupported combinations return false for caller fallback. bool qsa_decode_attn_slots(const float* q, const QsaAttnSlotPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap, const QsaShapes& s, float* scratch, float* attn, int64_t n_q, void* stream); /// For src/kernels/qsa_merge_daily_parity.cpp only: qsa_decode_attn_batch's two passes apart, whatever the environment diff --git a/engine/src/core/verify.cpp b/engine/src/core/verify.cpp index 200ee638da..97f7d7ce93 100644 --- a/engine/src/core/verify.cpp +++ b/engine/src/core/verify.cpp @@ -43,6 +43,7 @@ #include "strata/kernels/bf16_gemv.hpp" #include "strata/kernels/native_router.hpp" #include "strata/kernels/native_moe.hpp" +#include "strata/kernels/router_top10.hpp" #include "strata/kernels/cpu/expert.hpp" #include "strata/kernels/elementwise.hpp" #include "strata/kernels/qsa_prompt_attn.hpp" @@ -791,6 +792,10 @@ Verifier::~Verifier() { if (kv.second) cudaGraphExecDestroy(kv.second); for (auto& kv : commit_bm_) if (kv.second) cudaGraphExecDestroy(kv.second); +#if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL) + strata::kernels::bf16_gemm_cublas_destroy(bf16_cublas_); + bf16_cublas_ = nullptr; +#endif if (arena_b_) cudaFree(arena_b_); if (h_commitb_) cudaFreeHost(h_commitb_); for (auto* m : {&save_bm_, &dcommit_bm_}) @@ -1355,6 +1360,14 @@ bool Verifier::init(const WeightTable& wt, const ModelGeometry& g, SessionState& err = "verify: stream create failed"; return false; } +#if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL) + const char* ple_tc = std::getenv("STRATA_PLE_CUBLAS_BF16"); + const char* router_tc = std::getenv("STRATA_ROUTER_CUBLAS_BF16"); + if (max_t >= 16 && ((ple_tc && std::atoi(ple_tc) == 1) || (router_tc && std::atoi(router_tc) == 1))) { + bf16_cublas_ = strata::kernels::bf16_gemm_cublas_init(device_, cs_, max_t, 8192); + if (!bf16_cublas_) std::fprintf(stderr, "verify: BF16 cuBLAS unavailable; using existing projections\n"); + } +#endif if (cudaEventCreateWithFlags(&commit_done_, cudaEventDisableTiming) != cudaSuccess || cudaEventCreateWithFlags(&ev_fork_, cudaEventDisableTiming) != cudaSuccess || cudaEventCreateWithFlags(&ev_join_, cudaEventDisableTiming) != cudaSuccess) { @@ -1717,10 +1730,20 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { const bool batched = !ple_batch_kv && ple_batch && n > 1 && (pw.key_bf16 != nullptr || nkey) && ple_native_bf16_enabled(); if (ple_batch_kv) { try { - bf16_gemv_fp32_mmvf_multi(ple_ + (size_t) tb * N, N, ss.ple.w.key_bf16, - xn_ + (size_t) tb * HC * N, HC * N, N, HC * N, n, cs); - bf16_gemv_fp32_mmvf_multi(ple_ + (size_t) tb * N, N, ss.ple.w.value_bf16, - z_ + (size_t) tb * N, N, N, N, n, cs); +#if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL) + static const bool ple_tc_on = [] { const char* v = std::getenv("STRATA_PLE_CUBLAS_BF16"); return v && std::atoi(v) == 1; }(); + if (ple_tc_on && bf16_cublas_ && cs == cs_ && n >= 16 && N <= 8192) { + bf16_gemm_cublas_stage(bf16_cublas_, ple_ + (size_t)tb * N, N, (int)N, n); + bf16_gemm_cublas_f32in(bf16_cublas_, ss.ple.w.key_bf16, xn_ + (size_t)tb * HC * N, HC * N, (int)N, (int)(HC * N), n); + bf16_gemm_cublas_f32in(bf16_cublas_, ss.ple.w.value_bf16, z_ + (size_t)tb * N, N, (int)N, (int)N, n); + } else +#endif + { + bf16_gemv_fp32_mmvf_multi(ple_ + (size_t) tb * N, N, ss.ple.w.key_bf16, + xn_ + (size_t) tb * HC * N, HC * N, N, HC * N, n, cs); + bf16_gemv_fp32_mmvf_multi(ple_ + (size_t) tb * N, N, ss.ple.w.value_bf16, + z_ + (size_t) tb * N, N, N, N, n, cs); + } } catch (const std::exception& e) { err = std::string("verify PLE multi: ") + e.what(); return false; @@ -2230,8 +2253,8 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { return; } if (g_qsa_slots && !streamed) { - for (int t0 = tb; t0 < te; t0 += kQsaAttnMaxSlots) { - const int m = std::min(te - t0, kQsaAttnMaxSlots); + for (int t0 = tb; t0 < te; t0 += 8) { + const int m = std::min(te - t0, 8); const float* pooled[kQsaAttnMaxSlots]; const float* dead[kQsaAttnMaxSlots]; for (int i = 0; i < m; ++i) { @@ -2501,8 +2524,10 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { err = "verify: the queries' branch could not join"; return false; } - for (int t0 = tb; t0 < te; t0 += kQsaAttnMaxSlots) { - const int m = std::min(te - t0, kQsaAttnMaxSlots); + const char* wide_attn = std::getenv("STRATA_QSA_ATTN_BATCH64"); + const int attn_group = wide_attn && wide_attn[0] == '1' && wide_attn[1] == '\0' ? kQsaAttnMaxSlots : 8; + for (int t0 = tb; t0 < te; t0 += attn_group) { + const int m = std::min(te - t0, attn_group); const float* pooled[kQsaAttnMaxSlots]; const float* dead[kQsaAttnMaxSlots]; QsaAttnSlotPools px; @@ -2512,12 +2537,18 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { dead[i] = sx.idx_dead; px.p[i] = qsa_attn_pools(sx); } - // a launcher that would not run the per-row call's kernel launches nothing: per-row calls - if (!qsa_block_scores_slots(pooled, dead, qidx_ + t0 * IQ * ID, step_ + t0 * kStepCount, m, - max_blocks_, s, scores_ + (size_t) t0 * max_blocks_, cs)) - for (int t = t0; t < t0 + m; ++t) - qsa_block_scores(pooled[t - t0], dead[t - t0], qidx_ + t * IQ * ID, step_ + t * kStepCount, - 1, max_blocks_, s, scores_ + (size_t) t * max_blocks_, cs); + // The score kernel has its own 8-row descriptor limit. + for (int j = 0; j < m; j += 8) { + const int count = std::min(m - j, 8); + const int first = t0 + j; + if (!qsa_block_scores_slots(pooled + j, dead + j, qidx_ + first * IQ * ID, + step_ + first * kStepCount, count, max_blocks_, s, + scores_ + (size_t) first * max_blocks_, cs)) + for (int t = first; t < first + count; ++t) + qsa_block_scores(pooled[t - t0], dead[t - t0], qidx_ + t * IQ * ID, + step_ + t * kStepCount, 1, max_blocks_, s, + scores_ + (size_t) t * max_blocks_, cs); + } qsa_block_topk(scores_ + (size_t) t0 * max_blocks_, step_ + t0 * kStepCount, m, max_blocks_, cap_, s, sel_ + (size_t) t0 * cap_, cs); if (!qsa_decode_attn_slots(qcur_ + t0 * NH * HD, px, sel_ + (size_t) t0 * cap_, @@ -2633,6 +2664,19 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) { // move into the combine, gate + up share one launch (all bitwise; resident combine path, 2-8 rows only) const WeightRef* w_sgi = lfuse_on(n) && g_lfuse_gate() ? v.get("ffn_gate_inp_shexp.weight") : nullptr; bool sg_ready = false; +#if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL) + static const bool router_tc_on = [] { const char* v = std::getenv("STRATA_ROUTER_CUBLAS_BF16"); return v && std::atoi(v) == 1; }(); + const bool router_tc = router_tc_on && bf16_cublas_ && cs == cs_ && dec_batch && n >= 16 && N <= 8192 && + NE == 256 && K == 8 && w_router && w_router->kind == WeightKind::Bf16InF32 && w_router->data && + route_window_on(); + if (router_tc) { + try { + bf16_gemm_cublas_stage(bf16_cublas_, mixed_ + (size_t)tb * N, N, (int)N, n); + bf16_gemm_cublas_f32in(bf16_cublas_, (const uint16_t*)w_router->data, logits_ + (size_t)tb * NE, NE, (int)N, (int)NE, n); + router_top10(logits_ + tb * NE, n, (int)NE, (int)K, ids_ + tb * K, w_ + tb * K, cs); + } catch (const std::exception& e) { err = "verify router: " + std::string(e.what()); return false; } + } else +#endif if (dec_batch && n > 1 && w_router != nullptr && native_router_enabled() && NE == 512 && K == 10) { try { if (w_sgi != nullptr) diff --git a/engine/src/kernels/cuda/iq_kernels.cu b/engine/src/kernels/cuda/iq_kernels.cu index 7bad20a657..d393fccf18 100644 --- a/engine/src/kernels/cuda/iq_kernels.cu +++ b/engine/src/kernels/cuda/iq_kernels.cu @@ -1825,16 +1825,16 @@ __device__ __forceinline__ void row_dot_40_sub8(const uint8_t* row, const block_ // 8-lane sub-warp row dot for K = nb * ipb == 20 (TD == 40, NVFP4 with n_ff == 640): 4 rows per warp (32 rows per // 256-thread block). Bitwise row_dot_multi's tree: warp_sum adds lane i + 16 (terms k and k + 16; lanes 20-31 hold // +0) then i + 8, then 4, 2, 1 - so lane t here takes k = t (+ k = t + 16 for t < 4, else + 0) and k = t + 8 (+ 0). -template +template __device__ __forceinline__ void row_dot_20_sub8(const uint8_t* row, const block_q8_1* x, const int (&off)[NC], int n, - int t, float (&s)[NC]) { + int t, float (&s)[NC], const uint32_t* __restrict__ s_grid = nullptr) { using F = Fmt; using S = Split; #pragma unroll for (int c = 0; c < NC; ++c) s[c] = 0.0f; { const int k = t, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); - const typename S::W w = S::load(row, kbx, iqs); + const typename S::W w = S::template load(row, kbx, iqs, s_grid); #pragma unroll for (int c = 0; c < NC; ++c) if (EXACT_N || c < n) s[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); @@ -1842,19 +1842,21 @@ __device__ __forceinline__ void row_dot_20_sub8(const uint8_t* row, const block_ float s16[NC]; #pragma unroll for (int c = 0; c < NC; ++c) s16[c] = 0.0f; - if (t < 4) { - const int k = t + 16, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); - const typename S::W w = S::load(row, kbx, iqs); + if constexpr (NITEMS > 16) { + if (t < NITEMS - 16) { + const int k = t + 16, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); + const typename S::W w = S::template load(row, kbx, iqs, s_grid); #pragma unroll - for (int c = 0; c < NC; ++c) - if (EXACT_N || c < n) s16[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); + for (int c = 0; c < NC; ++c) + if (EXACT_N || c < n) s16[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); + } } float s8[NC]; #pragma unroll for (int c = 0; c < NC; ++c) s8[c] = 0.0f; { const int k = t + 8, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); - const typename S::W w = S::load(row, kbx, iqs); + const typename S::W w = S::template load(row, kbx, iqs, s_grid); #pragma unroll for (int c = 0; c < NC; ++c) if (EXACT_N || c < n) s8[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); @@ -1869,6 +1871,68 @@ __device__ __forceinline__ void row_dot_20_sub8(const uint8_t* row, const block_ } } +#if !defined(__HIPCC__) && !defined(__HIP_PLATFORM_AMD__) +// 8-lane sub-warp row dot for K = nb * ipb == 32 (Q4_K / Q5_K / IQ4_NL with n_ff == 512): +// bitwise identical addition tree to warp_sum. +template +__device__ __forceinline__ void row_dot_32_sub8(const uint8_t* row, const block_q8_1* x, const int (&off)[NC], int n, + int t, float (&s)[NC], const uint32_t* __restrict__ s_grid = nullptr) { + using F = Fmt; + using S = Split; +#pragma unroll + for (int c = 0; c < NC; ++c) s[c] = 0.0f; + { + const int k = t, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); + const typename S::W w = S::template load(row, kbx, iqs, s_grid); +#pragma unroll + for (int c = 0; c < NC; ++c) + if (EXACT_N || c < n) s[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); + } + float s8[NC]; +#pragma unroll + for (int c = 0; c < NC; ++c) s8[c] = 0.0f; + { + const int k = t + 8, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); + const typename S::W w = S::template load(row, kbx, iqs, s_grid); +#pragma unroll + for (int c = 0; c < NC; ++c) + if (EXACT_N || c < n) s8[c] += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); + } + { + const int k = t + 16, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); + const typename S::W w = S::template load(row, kbx, iqs, s_grid); +#pragma unroll + for (int c = 0; c < NC; ++c) { + if (EXACT_N || c < n) { + float s16 = 0.0f; + s16 += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); + s[c] = __fadd_rn(s[c], s16); + } + } + } + { + const int k = t + 24, kbx = k / F::ipb, iqs = F::step * (k % F::ipb); + const typename S::W w = S::template load(row, kbx, iqs, s_grid); +#pragma unroll + for (int c = 0; c < NC; ++c) { + if (EXACT_N || c < n) { + float s24 = 0.0f; + s24 += S::apply(w, x + off[c] + kbx * (F::qk / 32), iqs); + s[c] = __fadd_rn(s[c], __fadd_rn(s8[c], s24)); + } + } + } + const unsigned mask = 0xffu << (threadIdx.x & 24); +#pragma unroll + for (int c = 0; c < NC; ++c) { + if (EXACT_N || c < n) { +#pragma unroll + for (int o = 4; o > 0; o >>= 1) s[c] += __shfl_xor_sync(mask, s[c], o); + } + } +} +#endif + // 16-lane sub-warp row dot for K = nb * ipb == 20 (TD == 42, Q2_0, and TD == 40, NVFP4, with n_ff == 640): // 2 rows per warp (16 rows per 256-thread block), bitwise identical addition tree to row_dot_multi. template @@ -7215,7 +7279,7 @@ __global__ void __launch_bounds__(256) native_gu_lds_kernel(const unsigned long // ---- 7 (down): the group's q8_1 h rows in LDS, the tokens inside the k loop, the R2 row pairs of mode 2 - // the same per-(row, entry) sums in the same order: bitwise the mode-2 output. -template +template __global__ void __launch_bounds__(256) native_down_lds_kernel(const unsigned long long* __restrict__ grp_ptr, const int32_t* __restrict__ grp_start, const int32_t* __restrict__ n_groups, @@ -7259,6 +7323,90 @@ __global__ void __launch_bounds__(256) native_down_lds_kernel(const unsigned lon for (int i = tid; i < cn * hb * 9; i += 256) sh_raw[i] = src[i]; } __syncthreads(); +#if !defined(__HIPCC__) && !defined(__HIP_PLATFORM_AMD__) + if constexpr (SUB_MODE != 0 && kSplit) { + constexpr int kN512 = (512 / F::qk) * F::ipb; + static_assert((SUB_MODE == 1 && (kN512 == 16 || kN512 == 32) && (RB % 32 == 0)) || + (SUB_MODE == 2 && kN512 == 64 && (RB % 16 == 0)), + "unsupported SUB_MODE / RB / TD combination"); + if constexpr (SUB_MODE == 1 && (kN512 == 16 || kN512 == 32) && (RB % 32 == 0)) { + constexpr int kPasses = RB / 32; + const int sub_row = (tid >> 3); + const int slane = tid & 7; + for (int p = 0; p < kPasses; ++p) { + const int r = blockIdx.x * RB + p * 32 + sub_row; + if (r >= nrow) break; + const uint8_t* w = blob + L.down_off + (size_t) r * L.d_row; + auto pass8 = [&](auto ntc) { + constexpr int NTC = decltype(ntc)::value; + int off[NTC]; +#pragma unroll + for (int j = 0; j < NTC; ++j) off[j] = j * hb; + float s[NTC]; + if constexpr (kN512 == 16) + row_dot_20_sub8(w, sh, off, NTC, slane, s, sgrid); + else + row_dot_32_sub8(w, sh, off, NTC, slane, s, sgrid); + if (slane == 0) { +#pragma unroll + for (int j = 0; j < NTC; ++j) { + float v = s[j]; + if constexpr (TD == 40) v *= sd; + out[(size_t) ent_dst[c0 + j] * L.n_embd + r] = v; + } + } + }; + switch (cn) { + case 1: pass8(std::integral_constant{}); break; + case 2: pass8(std::integral_constant{}); break; + case 3: pass8(std::integral_constant{}); break; + default: pass8(std::integral_constant{}); break; + } + } + } else if constexpr (SUB_MODE == 2 && kN512 == 64 && (RB % 16 == 0)) { + constexpr int kPasses = RB / 16; + for (int p = 0; p < kPasses; ++p) { + const int r0 = blockIdx.x * RB + p * 16 + warp, r1 = r0 + 8; + if (r0 >= nrow) break; + const bool two = r1 < nrow; + const uint8_t* w0 = blob + L.down_off + (size_t) r0 * L.d_row; + const uint8_t* w1 = blob + L.down_off + (size_t) (two ? r1 : r0) * L.d_row; + auto pass = [&](auto ntc) { + constexpr int NTC = decltype(ntc)::value; + float s0[NTC], s1[NTC]; +#pragma unroll + for (int j = 0; j < NTC; ++j) { s0[j] = 0.0f; s1[j] = 0.0f; } + for (int k = lane; k < kN512; k += 32) { + const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb); + const auto rw0 = Split::template load(w0, kbx, iqs, sgrid); + const auto rw1 = Split::template load(w1, kbx, iqs, sgrid); +#pragma unroll + for (int j = 0; j < NTC; ++j) { + const block_q8_1* xk = sh + (size_t) j * hb + kbx * (F::qk / 32); + s0[j] += Split::apply(rw0, xk, iqs); + s1[j] += Split::apply(rw1, xk, iqs); + } + } +#pragma unroll + for (int j = 0; j < NTC; ++j) { + const float a = warp_sum(s0[j]) * sd, b = warp_sum(s1[j]) * sd; + if (lane == 0) { + out[(size_t) ent_dst[c0 + j] * L.n_embd + r0] = a; + if (two) out[(size_t) ent_dst[c0 + j] * L.n_embd + r1] = b; + } + } + }; + switch (cn) { + case 1: pass(std::integral_constant{}); break; + case 2: pass(std::integral_constant{}); break; + case 3: pass(std::integral_constant{}); break; + default: pass(std::integral_constant{}); break; + } + } + } + } else +#endif + { if constexpr (kCanSub8) if (use_sub8) { // 8-lane sub-warp (4 rows/warp = 32 rows/block in 2 passes of 32 = 64 rows), 100% active lanes + hoisted load! const int sub_row = (tid >> 3); // 0..31 within block @@ -7448,6 +7596,7 @@ __global__ void __launch_bounds__(256) native_down_lds_kernel(const unsigned lon default: pass(std::integral_constant{}); break; } } + } } } @@ -7609,6 +7758,97 @@ __global__ void __launch_bounds__(256) native_gu_fused_kernel(const unsigned lon } } +#if !defined(__HIPCC__) && !defined(__HIP_PLATFORM_AMD__) +// 512-thread (16-warp) fused gate/up + SwiGLU + q8_1 kernel for q4_lite (n_embd == 2048, K-quants/Q8_0/IQ4_NL with +// kN >= 128 and no SMEM codebook): 32-lane R2 (2 passes x 32 rows), doubling SM warp occupancy on low-grid calls. +template +__global__ void __launch_bounds__(512) native_gu_fused_512_kernel(const unsigned long long* __restrict__ grp_ptr, + const int32_t* __restrict__ grp_start, + const int32_t* __restrict__ n_groups, + const int32_t* __restrict__ ent_tok, + const block_q8_1* __restrict__ xq, NativeExpertLayout L, + block_q8_1* __restrict__ hq) { + using F = Fmt; + static_assert(kSplit && TG != 40 && LDS_NT <= 4, + "native_gu_fused_512_kernel assumes split non-NVFP4 format and LDS_NT <= 4"); + __shared__ float res[LDS_NT][64]; + extern __shared__ int sx_raw[]; + const int g = blockIdx.y; + if (g >= *n_groups) return; + const int e0 = grp_start[g], e1 = grp_start[g + 1]; + if (e0 >= e1) return; + const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5; + const uint8_t* blob = (const uint8_t*) grp_ptr[g]; + const int xb = (int) (L.n_embd / 32), hb = (int) (L.n_ff / 32); + constexpr int kN2048 = (2048 / F::qk) * F::ipb; + const int r0 = blockIdx.x * 32; + const block_q8_1* sx = (const block_q8_1*) sx_raw; + auto wrow = [&](int i) -> const uint8_t* { + return blob + (i < 32 ? (size_t) 0 : L.up_off) + (size_t) (r0 + (i & 31)) * L.gu_row; + }; + for (int c0 = e0; c0 < e1; c0 += LDS_NT) { + const int cn = min(LDS_NT, e1 - c0); + __syncthreads(); + for (int j = 0; j < cn; ++j) { + const int* src = (const int*) (xq + (size_t) ent_tok[c0 + j] * xb); + for (int i = tid; i < xb * 9; i += 512) sx_raw[j * xb * 9 + i] = src[i]; + } + __syncthreads(); + if constexpr (kSplit) { + for (int p = 0; p < 2; ++p) { + const int i0 = p * 32 + warp, i1 = i0 + 16; + const uint8_t* w0 = wrow(i0); + const uint8_t* w1 = wrow(i1); + auto pass = [&](auto ntc) { + constexpr int NTC = decltype(ntc)::value; + float s0[NTC], s1[NTC]; +#pragma unroll + for (int j = 0; j < NTC; ++j) { s0[j] = 0.0f; s1[j] = 0.0f; } + for (int k = lane; k < kN2048; k += 32) { + const int kbx = k / F::ipb, iqs = F::step * (k % F::ipb); + const auto rw0 = Split::template load(w0, kbx, iqs); + const auto rw1 = Split::template load(w1, kbx, iqs); +#pragma unroll + for (int j = 0; j < NTC; ++j) { + const block_q8_1* xk = sx + (size_t) j * xb + kbx * (F::qk / 32); + s0[j] += Split::apply(rw0, xk, iqs); + s1[j] += Split::apply(rw1, xk, iqs); + } + } +#pragma unroll + for (int j = 0; j < NTC; ++j) { + const float a = warp_sum(s0[j]), b = warp_sum(s1[j]); + if (lane == 0) { res[j][i0] = a; res[j][i1] = b; } + } + }; + switch (cn) { + case 1: pass(std::integral_constant{}); break; + case 2: pass(std::integral_constant{}); break; + case 3: pass(std::integral_constant{}); break; + default: pass(std::integral_constant{}); break; + } + } + } + __syncthreads(); + if (warp < cn) { + const float gg = res[warp][lane], uu = res[warp][32 + lane]; + const float xi = __fmul_rn(gg / (1.0f + __expf(-gg)), uu); + float amax = fabsf(xi), sum = xi; +#pragma unroll + for (int o = 16; o > 0; o >>= 1) { + amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o)); + sum += __shfl_xor_sync(0xffffffffu, sum, o); + } + const float d = q8_1_finite(amax / 127.0f); + const int8_t q = q8_1_quant(xi, d, amax); + block_q8_1* y = hq + (size_t) (c0 + warp) * hb + blockIdx.x; + y->qs[lane] = q; + if (lane == 0) y->ds = q8_1_ds(d, sum); + } + } +} +#endif + // dflash2-kern: Strix Halo (gfx1151) & discrete RDNA3 (gfx1100..gfx1102) take mode 8 (gate/up + SwiGLU + q8_1 in one launch, // the group's activations + codebook grid in LDS) for the IQ/NVFP4 packs' gate/up: // - gfx1151 IQ3_S: gate/up -25% (IQ2_S), -8% (IQ3_XXS), -6% (IQ3_S), bitwise equal to mode 0. @@ -7944,6 +8184,33 @@ bool native_expert_expdec_takes(const NativeExpertLayout& L, bool& gu, bool& dow return gu || down; } +#if !defined(__HIPCC__) && !defined(__HIP_PLATFORM_AMD__) +// Bit-exact sm_120 default for n_ff == 512 mode-8 GGUF decode kernels (down RB=32 and gate/up 512T R2 for kN >= 128). +// Unset: ON on sm_120 unless STRATA_SM120_PERF=0 or STRATA_DEFAULTS=026, OFF on other CUDA architectures (where lower SM +// counts already saturate waves with RB=64 / 256T). Explicit STRATA_GGUF_512_OPT=0/1 overrides on any CUDA card. +static bool gguf_512_opt_on() { + static const int env = [] { + const char* v = std::getenv("STRATA_GGUF_512_OPT"); + return v != nullptr && v[0] != '\0' ? (std::atoi(v) != 0 ? 1 : 0) : -1; + }(); + if (env >= 0) return env != 0; + if (strata::qwen_defaults_026()) return false; + static std::atomic per_dev[64]{}; // 0 unknown, 1 no, 2 yes + int dev = 0; + if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return false; } + int cached = per_dev[dev].load(std::memory_order_relaxed); + if (!cached) { + int maj = 0, mnr = 0; + const bool ok = cudaDeviceGetAttribute(&maj, cudaDevAttrComputeCapabilityMajor, dev) == cudaSuccess && + cudaDeviceGetAttribute(&mnr, cudaDevAttrComputeCapabilityMinor, dev) == cudaSuccess; + if (!ok) cudaGetLastError(); + cached = ok && strata::sm120_default(maj, mnr) ? 2 : 1; + per_dev[dev].store(cached, std::memory_order_relaxed); + } + return cached == 2; +} +#endif + void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long* grp_ptr, const int32_t* grp_start, const int32_t* n_groups, const int32_t* ent_dst, const int32_t* ent_tok, int64_t cap_groups, int64_t cap_entries, const void* x_q8_1, void* scratch, float* out, void* stream, @@ -8074,22 +8341,40 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long #endif L.gu_type == 40) && L.n_ff % 32 == 0; +#if !defined(__HIPCC__) && !defined(__HIP_PLATFORM_AMD__) + const bool gguf_512_opt = gguf_512_opt_on(); +#endif if (fused_gu && g_exp_phase != 2) { const dim3 gl((unsigned) (L.n_ff / 32), (unsigned) cap_groups); const size_t sh = (size_t) LDS_NT * (size_t) (L.n_embd / 32) * sizeof(block_q8_1); +#if !defined(__HIPCC__) && !defined(__HIP_PLATFORM_AMD__) + const bool gu512 = gguf_512_opt && !g_old_kernels && L.n_embd == 2048 && L.n_ff == 512; +#endif + auto launch_gu_fused = [&](auto tg) { + constexpr int TG = decltype(tg)::value; +#if !defined(__HIPCC__) && !defined(__HIP_PLATFORM_AMD__) + if constexpr ((2048 / Fmt::qk) * Fmt::ipb >= 128 && TG != 40) { + if (gu512) { + native_gu_fused_512_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); + return; + } + } +#endif + native_gu_fused_kernel<<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); + }; switch (L.gu_type) { - case 8: native_gu_fused_kernel<8><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; + case 8: launch_gu_fused(std::integral_constant{}); break; case 10: native_gu_fused_kernel<10><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; case 11: native_gu_fused_kernel<11><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; - case 12: native_gu_fused_kernel<12><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; - case 13: native_gu_fused_kernel<13><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; + case 12: launch_gu_fused(std::integral_constant{}); break; + case 13: launch_gu_fused(std::integral_constant{}); break; #ifdef STRATA_Q6K_EXPERTS - case 14: native_gu_fused_kernel<14><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; + case 14: launch_gu_fused(std::integral_constant{}); break; #endif case 16: native_gu_fused_kernel<16><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; case 17: native_gu_fused_kernel<17><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; case 18: native_gu_fused_kernel<18><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; - case 20: native_gu_fused_kernel<20><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; + case 20: launch_gu_fused(std::integral_constant{}); break; case 21: native_gu_fused_kernel<21><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; case 23: native_gu_fused_kernel<23><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; case 29: native_gu_fused_kernel<29><<>>(grp_ptr, grp_start, n_groups, ent_tok, X, L, hq); break; @@ -8220,28 +8505,57 @@ void native_expert_grouped(const NativeExpertLayout& L, const unsigned long long cudaGetDevice(&dev_d); // gfx1151: the LDS down kernel is slower than the CUDA layout for IQ4_NL (86 vs 63 us, 24 groups) and faster for Q2_0 (-18%) const bool lds_down_ok = L.n_ff <= 4096 && - (L.d_type == 8 || L.d_type == 12 || L.d_type == 13 || + (L.d_type == 8 || L.d_type == 10 || L.d_type == 11 || + L.d_type == 12 || L.d_type == 13 || #ifdef STRATA_Q6K_EXPERTS L.d_type == 14 || #endif - L.d_type == 18 || L.d_type == 21 || L.d_type == 23 || + L.d_type == 16 || L.d_type == 17 || L.d_type == 18 || + L.d_type == 21 || L.d_type == 22 || L.d_type == 23 || L.d_type == 42 || L.d_type == 40 || (L.d_type == 20 && !gfx1151_exp8(dev_d))); if ((em0d == 7 || em0d == 8) && lds_down_ok) { const dim3 gl((unsigned) ((L.n_embd + LDS_RB - 1) / LDS_RB), (unsigned) cap_groups); const size_t sh = (size_t) LDS_NT * (size_t) (L.n_ff / 32) * sizeof(block_q8_1); +#if !defined(__HIPCC__) && !defined(__HIP_PLATFORM_AMD__) + const dim3 gl32((unsigned) ((L.n_embd + 31) / 32), (unsigned) cap_groups); + const bool down512 = gguf_512_opt && !g_old_kernels && L.n_ff == 512 && L.n_embd % 32 == 0; +#endif + auto launch_down_lds = [&](auto td) { + constexpr int TD = decltype(td)::value; +#if !defined(__HIPCC__) && !defined(__HIP_PLATFORM_AMD__) + constexpr int kN = (512 / Fmt::qk) * Fmt::ipb; + if constexpr (kN == 16 || kN == 32) { + if (down512) { + native_down_lds_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); + return; + } + } else if constexpr (kN == 64) { + if (down512) { + native_down_lds_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); + return; + } + } +#endif + native_down_lds_kernel<<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); + }; switch (L.d_type) { - case 8: native_down_lds_kernel<8><<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break; - case 12: native_down_lds_kernel<12><<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break; - case 13: native_down_lds_kernel<13><<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break; + case 8: launch_down_lds(std::integral_constant{}); break; + case 10: launch_down_lds(std::integral_constant{}); break; + case 11: launch_down_lds(std::integral_constant{}); break; + case 12: launch_down_lds(std::integral_constant{}); break; + case 13: launch_down_lds(std::integral_constant{}); break; #ifdef STRATA_Q6K_EXPERTS - case 14: native_down_lds_kernel<14><<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break; + case 14: launch_down_lds(std::integral_constant{}); break; #endif - case 18: native_down_lds_kernel<18><<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break; - case 20: native_down_lds_kernel<20><<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break; - case 21: native_down_lds_kernel<21><<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break; - case 23: native_down_lds_kernel<23><<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break; + case 16: launch_down_lds(std::integral_constant{}); break; + case 17: launch_down_lds(std::integral_constant{}); break; + case 18: launch_down_lds(std::integral_constant{}); break; + case 20: launch_down_lds(std::integral_constant{}); break; + case 21: launch_down_lds(std::integral_constant{}); break; + case 22: launch_down_lds(std::integral_constant{}); break; + case 23: launch_down_lds(std::integral_constant{}); break; case 40: native_down_lds_kernel<40><<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break; - default: native_down_lds_kernel<42><<>>(grp_ptr, grp_start, n_groups, ent_dst, hq, L, out); break; + default: launch_down_lds(std::integral_constant{}); break; } check("native_expert_grouped/down"); return; diff --git a/engine/src/kernels/cuda/native_bf16.cu b/engine/src/kernels/cuda/native_bf16.cu index 14fbaf6489..aafc8d196a 100644 --- a/engine/src/kernels/cuda/native_bf16.cu +++ b/engine/src/kernels/cuda/native_bf16.cu @@ -1,521 +1,618 @@ -#include "strata/core/bw_dense_defaults.hpp" -#include "strata/kernels/bf16_gemv.hpp" -#include "strata/kernels/window_limits.hpp" -#include "s26_tsum.cuh" -#include "strata/kernels/bf16_bits.hpp" -#include "strata/kernels/pdl.hpp" - -#include +#include "strata/core/bw_dense_defaults.hpp" +#include "strata/kernels/bf16_gemv.hpp" +#include "strata/kernels/window_limits.hpp" +#include "s26_tsum.cuh" +#include "strata/kernels/bf16_bits.hpp" +#include "strata/kernels/pdl.hpp" + +#include +#if !defined(__HIPCC__) +#include +#include +#include +#endif +#include #include -#include -#include -#include -#include - -namespace strata::kernels { -namespace { - -// The FP32-activation MMVF implementation below is adapted from llama.cpp -// 3cf03257f219afbe7334045ff7c6a06ac68c627d, ggml/src/ggml-cuda/{mmvf.cu,common.cuh}. -// Scope: ordinary contiguous BF16 matrix, one FP32 activation vector, no fusion/ids/channels. -// -// MIT License -// Copyright (c) 2023-2026 The ggml authors -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -__device__ __forceinline__ float mmvf_warp_sum(float value) { -#pragma unroll - for (int offset = 16; offset > 0; offset >>= 1) - value += __shfl_xor_sync(0xffffffffu, value, offset, 32); - return value; -} - -// STRATA_MMVF_CHECK=1 (diagnostic): the constant-trip fast path also runs the plain loop with volatile loads (so the loads happen again, later) and -// compares each thread's accumulator bit for bit: g_mmvf_chk[0] checks, [1] differing accumulators, [2..4] block / thread / n_in of the first one. -// A difference means the activations or weights changed between the early and the late loads (no arithmetic differs between the two loops). -__device__ unsigned long long g_mmvf_chk[8]; -__device__ __forceinline__ void mmvf_chk_note(float a, float b, int blk, int t, int n_in) { - atomicAdd(&g_mmvf_chk[0], 1ull); - if (__float_as_uint(a) != __float_as_uint(b)) { - if (atomicAdd(&g_mmvf_chk[1], 1ull) == 0ull) { g_mmvf_chk[2] = (unsigned long long) blk; g_mmvf_chk[3] = (unsigned long long) t; g_mmvf_chk[4] = (unsigned long long) n_in; - g_mmvf_chk[5] = __float_as_uint(a); g_mmvf_chk[6] = __float_as_uint(b); } - } -} - -template -__global__ void bf16_f32_mmvf_kernel(const float* __restrict__ x, const uint16_t* __restrict__ w, - float* __restrict__ y, int n_in, int fast) { - const int t = threadIdx.x; - const uint16_t* row = w + (size_t) blockIdx.x * n_in; - const uint32_t* weights2 = reinterpret_cast(row); - const float2* inputs2 = reinterpret_cast(x); - __shared__ float partials[32]; - if (t < 32) partials[t] = 0.0f; - __syncthreads(); - float acc = 0.0f; - if (BLOCK_SIZE == 256 && fast != 0 && n_in == 2560) { - // STRATA_SMALL_UNR bit 0: the router-shaped single-column read, five passes of one weight pair each (all in range), constant - // trip count so the weight and activation loads of the five passes are in flight together. The same FMAs in the same order. - uint32_t wv[5]; - float2 xv[5]; -#pragma unroll - for (int qq = 0; qq < 5; ++qq) { wv[qq] = weights2[t + qq * BLOCK_SIZE]; xv[qq] = inputs2[t + qq * BLOCK_SIZE]; } -#pragma unroll - for (int qq = 0; qq < 5; ++qq) { - acc = __fmaf_rn(f32_from_bf16((uint16_t) wv[qq]), xv[qq].x, acc); - acc = __fmaf_rn(f32_from_bf16((uint16_t) (wv[qq] >> 16)), xv[qq].y, acc); - } - if (fast == 2) { - float acc2 = 0.0f; - for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { - const uint32_t weight = *(const volatile uint32_t*) (weights2 + pair); - const volatile float* ip = reinterpret_cast(inputs2 + pair); - const float2 input = make_float2(ip[0], ip[1]); - acc2 = __fmaf_rn(f32_from_bf16((uint16_t) weight), input.x, acc2); - acc2 = __fmaf_rn(f32_from_bf16((uint16_t) (weight >> 16)), input.y, acc2); - } - mmvf_chk_note(acc, acc2, (int) blockIdx.x, t, n_in); - } - } else - for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { - const uint32_t weight = weights2[pair]; - const float2 input = inputs2[pair]; - // Match the two ordered multiply-adds in ggml_cuda_mad, not a pair sum followed by one add. - acc = __fmaf_rn(f32_from_bf16((uint16_t) weight), input.x, acc); - acc = __fmaf_rn(f32_from_bf16((uint16_t) (weight >> 16)), input.y, acc); - } - acc = mmvf_warp_sum(acc); - if constexpr (BLOCK_SIZE > 32) { - // All lanes have the same reduced value; one store avoids a same-value shared-memory race. - if ((t & 31) == 0) partials[t / 32] = acc; - __syncthreads(); - if (t < 32) acc = mmvf_warp_sum(partials[t]); - } - if (t == 0) y[blockIdx.x] = acc; -} - -// the same kernel for up to 8 activation rows - the weight row is read ONCE and every -// token keeps its own accumulator with exactly the single-row kernel's order (pairs, two ordered FMAs, the same warp -// and block reductions), so each output is bit-identical to a bf16_f32_mmvf_kernel launch of its own. -template -__global__ void bf16_f32_mmvf_multi_kernel(const float* x_, int64_t ldx, const uint16_t* __restrict__ w, - float* y_, int64_t ldy, int n_in, int n_tok, int fast) { - const float* STRATA_PDL_RESTRICT x = x_; // __restrict__ below sm_70 only (pdl.hpp, #1469) - float* STRATA_PDL_RESTRICT y = y_; - const int t = threadIdx.x; - const uint16_t* row = w + (size_t) blockIdx.x * n_in; - const uint32_t* weights2 = reinterpret_cast(row); - __shared__ float partials[NT][32]; - if (t < 32) { -#pragma unroll - for (int k = 0; k < NT; ++k) partials[k][t] = 0.0f; - } - __syncthreads(); - float acc[NT]; -#pragma unroll - for (int k = 0; k < NT; ++k) acc[k] = 0.0f; - // PDL (pdl.hpp): this thread's first PRE weight pairs are loaded before waiting for the activations - constexpr int PRE = kPdlPrefetch ? 8 : 0; - uint32_t wpre[PRE > 0 ? PRE : 1]; -#pragma unroll - for (int q = 0; q < PRE; ++q) { - const int pair = t + q * BLOCK_SIZE; - wpre[q] = pair < n_in / 2 ? __ldg(weights2 + pair) : 0u; - } - pdl_wait(); - if (BLOCK_SIZE == 256 && fast != 0 && n_in == 2560) { - // BW-DENSE (STRATA_SMALL_UNR bit 0): the router-shaped read (2560 inputs, 256 threads: five passes of one weight pair each, all - // in range) with constant trip counts, so the activation loads of every pass are in flight together instead of one memory - // latency a pass. The same FMAs in the same order (pass ascending, w0 then w1): bitwise the loop below. - float2 xin[NT][5]; -#pragma unroll - for (int qq = 0; qq < 5; ++qq) -#pragma unroll - for (int k = 0; k < NT; ++k) - if (EXACT_T || k < n_tok) xin[k][qq] = __ldg(reinterpret_cast(x + (size_t) k * ldx) + (t + qq * BLOCK_SIZE)); -#pragma unroll - for (int qq = 0; qq < 5; ++qq) { - uint32_t weight; - if constexpr (PRE >= 5) weight = wpre[qq]; - else weight = __ldg(weights2 + t + qq * BLOCK_SIZE); - const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16)); -#pragma unroll - for (int k = 0; k < NT; ++k) { - if (EXACT_T || k < n_tok) { - acc[k] = __fmaf_rn(w0, xin[k][qq].x, acc[k]); - acc[k] = __fmaf_rn(w1, xin[k][qq].y, acc[k]); - } - } - } - if (fast == 2) { -#pragma unroll - for (int k = 0; k < NT; ++k) { - if (EXACT_T || k < n_tok) { - float acc2 = 0.0f; - for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { - const uint32_t weight = *(const volatile uint32_t*) (weights2 + pair); - const volatile float* ip = reinterpret_cast(reinterpret_cast(x + (size_t) k * ldx) + pair); - const float2 input = make_float2(ip[0], ip[1]); - acc2 = __fmaf_rn(f32_from_bf16((uint16_t) weight), input.x, acc2); - acc2 = __fmaf_rn(f32_from_bf16((uint16_t) (weight >> 16)), input.y, acc2); - } - mmvf_chk_note(acc[k], acc2, (int) blockIdx.x, t, n_in); - } - } - } - } else { - int q = 0; - for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE, ++q) { - const uint32_t weight = q < PRE ? wpre[q] : __ldg(weights2 + pair); - const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16)); -#pragma unroll - for (int k = 0; k < NT; ++k) { - if (EXACT_T || k < n_tok) { - const float2 input = __ldg(reinterpret_cast(x + (size_t) k * ldx) + pair); - acc[k] = __fmaf_rn(w0, input.x, acc[k]); - acc[k] = __fmaf_rn(w1, input.y, acc[k]); - } - } - } - } -#pragma unroll - for (int k = 0; k < NT; ++k) - if (EXACT_T || k < n_tok) acc[k] = mmvf_warp_sum(acc[k]); - if constexpr (BLOCK_SIZE > 32) { - if ((t & 31) == 0) -#pragma unroll - for (int k = 0; k < NT; ++k) - if (EXACT_T || k < n_tok) partials[k][t / 32] = acc[k]; - __syncthreads(); - if (t < 32) -#pragma unroll - for (int k = 0; k < NT; ++k) - if (EXACT_T || k < n_tok) acc[k] = mmvf_warp_sum(partials[k][t]); - } - if (t == 0) -#pragma unroll - for (int k = 0; k < NT; ++k) - if (EXACT_T || k < n_tok) y[(size_t) k * ldy + blockIdx.x] = acc[k]; -} - -// S25 (STRATA_MMVF_ROWS=1): RPB output rows per block. Each block read its row's weights once but all T activation -// rows again (n_out blocks x T x n_in floats through L2 - the part that grew with T); here a block reads the -// activations once for RPB rows. Per output, thread t still walks pairs t, t + BLOCK_SIZE, ... with the same two -// ordered FMAs and the same warp and block reductions: bit-identical to bf16_f32_mmvf_multi_kernel. -// S26 STRATA_LFUSE=1 (AUX): one more block computes row 0 of w_aux into y_aux[k * ldy_aux] - the same per-output -// code, so each aux output is bitwise what its own bf16_f32_mmvf_multi_kernel launch gave (the shared expert's gate) -template -__global__ void bf16_f32_mmvf_rows_kernel(const float* __restrict__ x, int64_t ldx, const uint16_t* __restrict__ w, - float* __restrict__ y, int64_t ldy, int n_in, int n_out, int n_tok, - const uint16_t* __restrict__ w_aux = nullptr, float* __restrict__ y_aux = nullptr, - int64_t ldy_aux = 0) { - const int t = threadIdx.x; - int o0 = blockIdx.x * RPB; - if constexpr (AUX) { - if (blockIdx.x == gridDim.x - 1) { w = w_aux; y = y_aux; ldy = ldy_aux; n_out = 1; o0 = 0; } - } - __shared__ float partials[RPB][NT][32]; - if constexpr (BLOCK_SIZE > 32) { - if (t < 32) -#pragma unroll - for (int r = 0; r < RPB; ++r) -#pragma unroll - for (int k = 0; k < NT; ++k) partials[r][k][t] = 0.0f; - __syncthreads(); - } - float acc[RPB][NT]; -#pragma unroll - for (int r = 0; r < RPB; ++r) -#pragma unroll - for (int k = 0; k < NT; ++k) acc[r][k] = 0.0f; - for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { - float2 in[NT]; -#pragma unroll - for (int k = 0; k < NT; ++k) - if (k < n_tok) in[k] = reinterpret_cast(x + (size_t) k * ldx)[pair]; -#pragma unroll - for (int r = 0; r < RPB; ++r) { - if (o0 + r >= n_out) break; - const uint32_t weight = reinterpret_cast(w + (size_t) (o0 + r) * n_in)[pair]; - const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16)); -#pragma unroll - for (int k = 0; k < NT; ++k) { - if (k < n_tok) { - acc[r][k] = __fmaf_rn(w0, in[k].x, acc[r][k]); - acc[r][k] = __fmaf_rn(w1, in[k].y, acc[r][k]); - } - } - } - } - if constexpr (TS) { // S26 STRATA_TSUM=1: the RPB x NT warp sums (both stages) as transposed butterflies, bitwise the same - constexpr int V = RPB * NT, P = s26ts::pow2_ceil(V); - const int lane = t & 31, j = s26ts::tsum_token

(lane); - const bool own = lane == s26ts::tsum_lane

(j) && j < V; - float v[P]; -#pragma unroll - for (int q = 0; q < P; ++q) v[q] = q < V ? acc[q / NT][q % NT] : 0.0f; - float sum = s26ts::tsum

(v, lane); - if constexpr (BLOCK_SIZE > 32) { - if (own) partials[j / NT][j % NT][t / 32] = sum; - __syncthreads(); - if (t >= 32) return; -#pragma unroll - for (int q = 0; q < P; ++q) v[q] = q < V ? partials[q / NT][q % NT][t] : 0.0f; - sum = s26ts::tsum

(v, lane); - } - const int r = j / NT, k = j % NT; - if (own && o0 + r < n_out && k < n_tok) y[(size_t) k * ldy + o0 + r] = sum; - return; - } -#pragma unroll - for (int r = 0; r < RPB; ++r) -#pragma unroll - for (int k = 0; k < NT; ++k) acc[r][k] = mmvf_warp_sum(acc[r][k]); - if constexpr (BLOCK_SIZE > 32) { - if ((t & 31) == 0) -#pragma unroll - for (int r = 0; r < RPB; ++r) -#pragma unroll - for (int k = 0; k < NT; ++k) partials[r][k][t / 32] = acc[r][k]; - __syncthreads(); - if (t < 32) -#pragma unroll - for (int r = 0; r < RPB; ++r) -#pragma unroll - for (int k = 0; k < NT; ++k) acc[r][k] = mmvf_warp_sum(partials[r][k][t]); - } - if (t == 0) -#pragma unroll - for (int r = 0; r < RPB; ++r) - if (o0 + r < n_out) -#pragma unroll - for (int k = 0; k < NT; ++k) - if (k < n_tok) y[(size_t) k * ldy + o0 + r] = acc[r][k]; -} - -template -void launch_rows(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy, int n_in, int n_out, int n_tok, - cudaStream_t st, const uint16_t* w_aux = nullptr, float* y_aux = nullptr, int64_t ldy_aux = 0) { - constexpr int RPB = 4; - const unsigned nb = (unsigned) ((n_out + RPB - 1) / RPB); - static const bool ts = [] { const char* v = std::getenv("STRATA_TSUM"); return v && v[0] == '1'; }(); - if (w_aux != nullptr) { // S26 STRATA_LFUSE: + one aux row block -#define S26_AUX(TSV) \ - if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok, w_aux, y_aux, ldy_aux); \ - else bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok, w_aux, y_aux, ldy_aux); - if (ts) { S26_AUX(true) } else { S26_AUX(false) } -#undef S26_AUX - return; - } - if (ts) { - if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); - else bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); - return; - } - if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); - else bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); -} - -void mmvf_chk_report() { - unsigned long long h[8] = {}; - if (cudaMemcpyFromSymbol(h, g_mmvf_chk, sizeof(h)) != cudaSuccess) return; - std::fprintf(stderr, "strata mmvf check: %llu accumulators compared, %llu differ; first: block %llu thread %llu n_in %llu fast %08llx plain %08llx\n", h[0], h[1], h[2], h[3], h[4], h[5], h[6]); -} -// BW-DENSE STRATA_SMALL_UNR bit 0: see bf16_f32_mmvf_multi_kernel (bw_dense_defaults.hpp: on for a 12.0 card, else off; the variable wins) -int mmvf_fast_bit() { - static int per_dev[64]; // 0 unknown, else bit + 1 - const char* e = std::getenv("STRATA_SMALL_UNR"); - if (e != nullptr) return std::atoi(e) & 1; -#if defined(__HIPCC__) - return 0; -#else - int dev = 0; - if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return 0; } - if (!per_dev[dev]) { - int ma = 0, mi = 0; - const bool ok = cudaDeviceGetAttribute(&ma, cudaDevAttrComputeCapabilityMajor, dev) == cudaSuccess && - cudaDeviceGetAttribute(&mi, cudaDevAttrComputeCapabilityMinor, dev) == cudaSuccess; - if (!ok) cudaGetLastError(); - per_dev[dev] = 1 + (ok ? (strata::small_unr_default(ma, mi) & 1) : 0); - } - return per_dev[dev] - 1; -#endif -} -// AMD-Q STRATA_MMVF_CHECK=1 (with the switch on): the kernel also computes the plain read and counts the accumulators that differ (2 = check mode) -int mmvf_fast() { - const int b = mmvf_fast_bit(); - if (b == 0) return 0; - static const bool chk = [] { - const char* c = std::getenv("STRATA_MMVF_CHECK"); - const bool on = c != nullptr && c[0] == '1'; - if (on) std::atexit(mmvf_chk_report); - return on; - }(); - return chk ? 2 : b; -} -int mmvf_block_size(int64_t n_in) { - int best = 32; - int64_t best_iterations = (n_in + 63) / 64; - for (int candidate = 64; candidate <= 256; candidate += 32) { - const int64_t iterations = (n_in + 2 * candidate - 1) / (2 * candidate); - if (iterations < best_iterations) { - best_iterations = iterations; - best = candidate; - } - } - return best; -} - -} // namespace - -bool bf16_gemv_fp32_mmvf_multi_aux(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy, - int64_t n_in, int64_t n_out, int n_tok, const uint16_t* w_aux, float* y_aux, - int64_t ldy_aux, void* stream) { - static const bool rows = [] { const char* v = std::getenv("STRATA_MMVF_ROWS"); return v && v[0] == '1'; }(); - // exactly the conditions under which both calls would take the rows / multi kernels at the same block size - if (!rows || n_out < 64 || n_tok < 2 || n_tok > 8 || n_in <= 0 || (n_in & 1) != 0 || (ldx & 1) != 0 || x == nullptr || - w == nullptr || y == nullptr || w_aux == nullptr || y_aux == nullptr || (reinterpret_cast(x) & 7u) != 0) - return false; - const cudaStream_t st = (cudaStream_t) stream; - const int ni = (int) n_in, no = (int) n_out; - switch (mmvf_block_size(n_in)) { - case 32: launch_rows<32>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; - case 64: launch_rows<64>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; - case 96: launch_rows<96>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; - case 128: launch_rows<128>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; - case 160: launch_rows<160>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; - case 192: launch_rows<192>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; - case 224: launch_rows<224>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; - default: launch_rows<256>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; - } - const cudaError_t result = cudaGetLastError(); - if (result != cudaSuccess) - throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf_multi_aux launch: ") + cudaGetErrorString(result)); - return true; -} - -void bf16_gemv_fp32_mmvf_multi(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy, - int64_t n_in, int64_t n_out, int n_tok, void* stream) { - if (n_tok == 1 && ldy >= n_out) { bf16_gemv_fp32_mmvf(x, w, y, n_in, n_out, stream); return; } - if (n_tok < 1 || n_tok > STRATA_VERIFY_MAX_T || n_in <= 0 || (n_in & 1) != 0 || n_out <= 0 || (ldx & 1) != 0 || - x == nullptr || w == nullptr || y == nullptr || (reinterpret_cast(x) & 7u) != 0) - throw std::invalid_argument("bf16_gemv_fp32_mmvf_multi: 1..8 rows (16 in a wide build), even n_in/ldx, aligned pointers"); - const cudaStream_t st = (cudaStream_t) stream; - static const bool rows = [] { const char* v = std::getenv("STRATA_MMVF_ROWS"); return v && v[0] == '1'; }(); - if (rows && n_out >= 64 && n_tok <= 8) { // (the opt-in rows kernels hold 8 columns: a wider window takes the switch below) - const int ni = (int) n_in, no = (int) n_out; - switch (mmvf_block_size(n_in)) { - case 32: launch_rows<32>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; - case 64: launch_rows<64>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; - case 96: launch_rows<96>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; - case 128: launch_rows<128>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; - case 160: launch_rows<160>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; - case 192: launch_rows<192>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; - case 224: launch_rows<224>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; - default: launch_rows<256>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; - } - const cudaError_t result = cudaGetLastError(); - if (result != cudaSuccess) - throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf_multi launch: ") + cudaGetErrorString(result)); - return; - } - // 9..16 rows (wide builds only): one exact instantiation per row count, each row's dot independent of the others. 7 and 8 - // rows stay with the default case below, the instantiation they always had. -#if STRATA_VERIFY_MAX_T > 8 -#define STRATA_MMVF_W(N, T) case T: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; -#if STRATA_VERIFY_MAX_T > 12 -#define STRATA_MMVF_W13(N) STRATA_MMVF_W(N, 13) STRATA_MMVF_W(N, 14) STRATA_MMVF_W(N, 15) STRATA_MMVF_W(N, 16) -#else -#define STRATA_MMVF_W13(N) -#endif -#define STRATA_MMVF_WIDE(N) \ - STRATA_MMVF_W(N, 9) STRATA_MMVF_W(N, 10) STRATA_MMVF_W(N, 11) STRATA_MMVF_W(N, 12) \ - STRATA_MMVF_W13(N) -#else -#define STRATA_MMVF_WIDE(N) -#endif -#define STRATA_MMVF_M(N) case N: \ - switch (n_tok) { \ - STRATA_MMVF_WIDE(N) \ - case 1: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ - case 2: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ - case 3: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ - case 4: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ - case 5: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ - case 6: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ - default: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ - } break - switch (mmvf_block_size(n_in)) { - STRATA_MMVF_M(32); STRATA_MMVF_M(64); STRATA_MMVF_M(96); STRATA_MMVF_M(128); - STRATA_MMVF_M(160); STRATA_MMVF_M(192); STRATA_MMVF_M(224); STRATA_MMVF_M(256); - } -#undef STRATA_MMVF_M -#undef STRATA_MMVF_WIDE -#ifdef STRATA_MMVF_W -#undef STRATA_MMVF_W -#endif - const cudaError_t result = cudaGetLastError(); - if (result != cudaSuccess) - throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf_multi launch: ") + cudaGetErrorString(result)); -} - -void bf16_gemv_fp32_mmvf(const float* x, const uint16_t* w, float* y, - int64_t n_in, int64_t n_out, void* stream) { - if (n_in <= 0 || (n_in & 1) != 0 || n_in > std::numeric_limits::max() || - n_out <= 0 || n_out > std::numeric_limits::max()) - throw std::invalid_argument("bf16_gemv_fp32_mmvf: require positive even n_in and positive n_out <= INT_MAX"); - if (x == nullptr || w == nullptr || y == nullptr || - (reinterpret_cast(x) & 7u) != 0 || - (reinterpret_cast(w) & 3u) != 0 || - (reinterpret_cast(y) & 3u) != 0) - throw std::invalid_argument("bf16_gemv_fp32_mmvf: null or misaligned pointer"); - const cudaStream_t st = (cudaStream_t) stream; -#define STRATA_MMVF_CASE(N) case N: \ - bf16_f32_mmvf_kernel<<<(unsigned) n_out, N, 0, st>>>(x, w, y, (int) n_in, mmvf_fast()); break - switch (mmvf_block_size(n_in)) { - STRATA_MMVF_CASE(32); - STRATA_MMVF_CASE(64); - STRATA_MMVF_CASE(96); - STRATA_MMVF_CASE(128); - STRATA_MMVF_CASE(160); - STRATA_MMVF_CASE(192); - STRATA_MMVF_CASE(224); - STRATA_MMVF_CASE(256); - } -#undef STRATA_MMVF_CASE - const cudaError_t result = cudaGetLastError(); - if (result != cudaSuccess) - throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf launch: ") + cudaGetErrorString(result)); -} - - - -void bf16_gemv_fp32_mmvf_cols(const float* x, const uint16_t* w, float* y, int64_t n_in, int64_t n_out, int ncols, - void* stream) { - // columns contiguous: the existing multi-row kernel (weight read once, each output bitwise its one-row call), - // up to 8 rows per launch - for (int c0 = 0; c0 < ncols; c0 += 8) { - const int nc = ncols - c0 < 8 ? ncols - c0 : 8; - if (nc == 1) bf16_gemv_fp32_mmvf(x + (size_t) c0 * n_in, w, y + (size_t) c0 * n_out, n_in, n_out, stream); - else bf16_gemv_fp32_mmvf_multi(x + (size_t) c0 * n_in, n_in, w, y + (size_t) c0 * n_out, n_out, n_in, n_out, nc, - stream); - } -} - -} // namespace strata::kernels +#include +#include +#include +#include + +namespace strata::kernels { +#if !defined(__HIPCC__) +struct Bf16CublasCtx { + int device = -1, max_tokens = 0, max_k = 0, staged_k = 0, staged_tokens = 0; + cudaStream_t stream = nullptr; + cublasHandle_t handle = nullptr; + void* workspace = nullptr; + __nv_bfloat16* x = nullptr; +}; + +__global__ void bf16_gemm_stage_kernel(const float* x, int64_t ldx, __nv_bfloat16* out, int k, int n) { + const int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i < n * k) out[i] = __float2bfloat16_rn(x[(int64_t)(i / k) * ldx + i % k]); +} + +__global__ void bf16_gemm_stage2_kernel(const float* x, int64_t ldx, __nv_bfloat162* out, int k, int n) { + const int i = blockIdx.x * blockDim.x + threadIdx.x; + const int kp = k / 2; + if (i < n * kp) { + const float2 v = *reinterpret_cast(x + (int64_t)(i / kp) * ldx + 2 * (i % kp)); + out[i] = __floats2bfloat162_rn(v.x, v.y); + } +} + +void bf16_gemm_cublas_destroy(Bf16CublasCtx* ctx) { + if (!ctx) return; + int previous = -1; + (void)cudaGetDevice(&previous); + if (ctx->device >= 0 && previous != ctx->device) (void)cudaSetDevice(ctx->device); + if (ctx->handle) (void)cublasDestroy(ctx->handle); + if (ctx->workspace) (void)cudaFree(ctx->workspace); + if (ctx->x) (void)cudaFree(ctx->x); + if (previous >= 0 && previous != ctx->device) (void)cudaSetDevice(previous); + delete ctx; +} + +Bf16CublasCtx* bf16_gemm_cublas_init(int device, void* stream, int max_tokens, int max_k) { + int current = -1; + cudaDeviceProp prop{}; + cudaStreamCaptureStatus capture = cudaStreamCaptureStatusNone; + if (!stream || max_tokens <= 0 || max_k <= 0 || max_tokens > std::numeric_limits::max() / max_k || + cudaGetDevice(¤t) != cudaSuccess || current != device || + cudaGetDeviceProperties(&prop, device) != cudaSuccess || prop.major < 8 || + cudaStreamIsCapturing(static_cast(stream), &capture) != cudaSuccess || + capture != cudaStreamCaptureStatusNone) + return nullptr; + auto* ctx = new (std::nothrow) Bf16CublasCtx; + if (!ctx) return nullptr; + ctx->device = device; ctx->stream = static_cast(stream); + ctx->max_tokens = max_tokens; ctx->max_k = max_k; + constexpr size_t workspace_bytes = 4u << 20; + if (cublasCreate(&ctx->handle) != CUBLAS_STATUS_SUCCESS || + cublasSetPointerMode(ctx->handle, CUBLAS_POINTER_MODE_HOST) != CUBLAS_STATUS_SUCCESS || + cublasSetMathMode(ctx->handle, CUBLAS_DEFAULT_MATH) != CUBLAS_STATUS_SUCCESS || + cublasSetStream(ctx->handle, ctx->stream) != CUBLAS_STATUS_SUCCESS || + cudaMalloc(&ctx->workspace, workspace_bytes) != cudaSuccess || + cudaMalloc(&ctx->x, (size_t)max_tokens * max_k * sizeof(__nv_bfloat16)) != cudaSuccess || + cublasSetWorkspace(ctx->handle, ctx->workspace, workspace_bytes) != CUBLAS_STATUS_SUCCESS) { + bf16_gemm_cublas_destroy(ctx); + (void)cudaGetLastError(); + return nullptr; + } + return ctx; +} + +void bf16_gemm_cublas_stage(Bf16CublasCtx* ctx, const float* x, int64_t ldx, int n_in, int n_tok) { + if (!ctx || !x || n_in <= 0 || n_in > ctx->max_k || n_tok <= 0 || n_tok > ctx->max_tokens || ldx < n_in) + throw std::invalid_argument("bf16_gemm_cublas_stage: invalid input"); + ctx->staged_k = n_in; ctx->staged_tokens = n_tok; + const int count = n_in * n_tok; + if ((n_in & 1) == 0 && (ldx & 1) == 0 && (reinterpret_cast(x) & 7u) == 0) + bf16_gemm_stage2_kernel<<<(count / 2 + 255) / 256, 256, 0, ctx->stream>>>( + x, ldx, reinterpret_cast<__nv_bfloat162*>(ctx->x), n_in, n_tok); + else + bf16_gemm_stage_kernel<<<(count + 255) / 256, 256, 0, ctx->stream>>>(x, ldx, ctx->x, n_in, n_tok); + const cudaError_t error = cudaGetLastError(); + if (error != cudaSuccess) throw std::runtime_error(std::string("bf16_gemm_cublas_stage: ") + cudaGetErrorString(error)); +} + +void bf16_gemm_cublas_f32in(Bf16CublasCtx* ctx, const uint16_t* w, float* y, int64_t ldy, + int n_in, int n_out, int n_tok) { + if (!ctx || !w || !y || n_in != ctx->staged_k || n_tok != ctx->staged_tokens || n_out <= 0 || + ldy < n_out || ldy > std::numeric_limits::max()) + throw std::invalid_argument("bf16_gemm_cublas_f32in: invalid input"); + const float one = 1.0f, zero = 0.0f; + const cublasStatus_t st = cublasGemmEx(ctx->handle, CUBLAS_OP_T, CUBLAS_OP_N, n_out, n_tok, n_in, + &one, w, CUDA_R_16BF, n_in, ctx->x, CUDA_R_16BF, n_in, &zero, y, CUDA_R_32F, (int)ldy, + CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT_TENSOR_OP); + if (st != CUBLAS_STATUS_SUCCESS) throw std::runtime_error("bf16_gemm_cublas_f32in: cuBLAS status " + std::to_string((int)st)); +} +#endif + +namespace { + +// The FP32-activation MMVF implementation below is adapted from llama.cpp +// 3cf03257f219afbe7334045ff7c6a06ac68c627d, ggml/src/ggml-cuda/{mmvf.cu,common.cuh}. +// Scope: ordinary contiguous BF16 matrix, one FP32 activation vector, no fusion/ids/channels. +// +// MIT License +// Copyright (c) 2023-2026 The ggml authors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +__device__ __forceinline__ float mmvf_warp_sum(float value) { +#pragma unroll + for (int offset = 16; offset > 0; offset >>= 1) + value += __shfl_xor_sync(0xffffffffu, value, offset, 32); + return value; +} + +// STRATA_MMVF_CHECK=1 (diagnostic): the constant-trip fast path also runs the plain loop with volatile loads (so the loads happen again, later) and +// compares each thread's accumulator bit for bit: g_mmvf_chk[0] checks, [1] differing accumulators, [2..4] block / thread / n_in of the first one. +// A difference means the activations or weights changed between the early and the late loads (no arithmetic differs between the two loops). +__device__ unsigned long long g_mmvf_chk[8]; +__device__ __forceinline__ void mmvf_chk_note(float a, float b, int blk, int t, int n_in) { + atomicAdd(&g_mmvf_chk[0], 1ull); + if (__float_as_uint(a) != __float_as_uint(b)) { + if (atomicAdd(&g_mmvf_chk[1], 1ull) == 0ull) { g_mmvf_chk[2] = (unsigned long long) blk; g_mmvf_chk[3] = (unsigned long long) t; g_mmvf_chk[4] = (unsigned long long) n_in; + g_mmvf_chk[5] = __float_as_uint(a); g_mmvf_chk[6] = __float_as_uint(b); } + } +} + +template +__global__ void bf16_f32_mmvf_kernel(const float* __restrict__ x, const uint16_t* __restrict__ w, + float* __restrict__ y, int n_in, int fast) { + const int t = threadIdx.x; + const uint16_t* row = w + (size_t) blockIdx.x * n_in; + const uint32_t* weights2 = reinterpret_cast(row); + const float2* inputs2 = reinterpret_cast(x); + __shared__ float partials[32]; + if (t < 32) partials[t] = 0.0f; + __syncthreads(); + float acc = 0.0f; + if (BLOCK_SIZE == 256 && fast != 0 && n_in == 2560) { + // STRATA_SMALL_UNR bit 0: the router-shaped single-column read, five passes of one weight pair each (all in range), constant + // trip count so the weight and activation loads of the five passes are in flight together. The same FMAs in the same order. + uint32_t wv[5]; + float2 xv[5]; +#pragma unroll + for (int qq = 0; qq < 5; ++qq) { wv[qq] = weights2[t + qq * BLOCK_SIZE]; xv[qq] = inputs2[t + qq * BLOCK_SIZE]; } +#pragma unroll + for (int qq = 0; qq < 5; ++qq) { + acc = __fmaf_rn(f32_from_bf16((uint16_t) wv[qq]), xv[qq].x, acc); + acc = __fmaf_rn(f32_from_bf16((uint16_t) (wv[qq] >> 16)), xv[qq].y, acc); + } + if (fast == 2) { + float acc2 = 0.0f; + for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { + const uint32_t weight = *(const volatile uint32_t*) (weights2 + pair); + const volatile float* ip = reinterpret_cast(inputs2 + pair); + const float2 input = make_float2(ip[0], ip[1]); + acc2 = __fmaf_rn(f32_from_bf16((uint16_t) weight), input.x, acc2); + acc2 = __fmaf_rn(f32_from_bf16((uint16_t) (weight >> 16)), input.y, acc2); + } + mmvf_chk_note(acc, acc2, (int) blockIdx.x, t, n_in); + } + } else + for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { + const uint32_t weight = weights2[pair]; + const float2 input = inputs2[pair]; + // Match the two ordered multiply-adds in ggml_cuda_mad, not a pair sum followed by one add. + acc = __fmaf_rn(f32_from_bf16((uint16_t) weight), input.x, acc); + acc = __fmaf_rn(f32_from_bf16((uint16_t) (weight >> 16)), input.y, acc); + } + acc = mmvf_warp_sum(acc); + if constexpr (BLOCK_SIZE > 32) { + // All lanes have the same reduced value; one store avoids a same-value shared-memory race. + if ((t & 31) == 0) partials[t / 32] = acc; + __syncthreads(); + if (t < 32) acc = mmvf_warp_sum(partials[t]); + } + if (t == 0) y[blockIdx.x] = acc; +} + +// the same kernel for up to 8 activation rows - the weight row is read ONCE and every +// token keeps its own accumulator with exactly the single-row kernel's order (pairs, two ordered FMAs, the same warp +// and block reductions), so each output is bit-identical to a bf16_f32_mmvf_kernel launch of its own. +template +__global__ void bf16_f32_mmvf_multi_kernel(const float* x_, int64_t ldx, const uint16_t* __restrict__ w, + float* y_, int64_t ldy, int n_in, int n_tok, int fast) { + const float* STRATA_PDL_RESTRICT x = x_; // __restrict__ below sm_70 only (pdl.hpp, #1469) + float* STRATA_PDL_RESTRICT y = y_; + const int t = threadIdx.x; + const uint16_t* row = w + (size_t) blockIdx.x * n_in; + const uint32_t* weights2 = reinterpret_cast(row); + __shared__ float partials[NT][32]; + if (t < 32) { +#pragma unroll + for (int k = 0; k < NT; ++k) partials[k][t] = 0.0f; + } + __syncthreads(); + float acc[NT]; +#pragma unroll + for (int k = 0; k < NT; ++k) acc[k] = 0.0f; + // PDL (pdl.hpp): this thread's first PRE weight pairs are loaded before waiting for the activations + constexpr int PRE = kPdlPrefetch ? 8 : 0; + uint32_t wpre[PRE > 0 ? PRE : 1]; +#pragma unroll + for (int q = 0; q < PRE; ++q) { + const int pair = t + q * BLOCK_SIZE; + wpre[q] = pair < n_in / 2 ? __ldg(weights2 + pair) : 0u; + } + pdl_wait(); + if (BLOCK_SIZE == 256 && fast != 0 && n_in == 2560) { + // BW-DENSE (STRATA_SMALL_UNR bit 0): the router-shaped read (2560 inputs, 256 threads: five passes of one weight pair each, all + // in range) with constant trip counts, so the activation loads of every pass are in flight together instead of one memory + // latency a pass. The same FMAs in the same order (pass ascending, w0 then w1): bitwise the loop below. + float2 xin[NT][5]; +#pragma unroll + for (int qq = 0; qq < 5; ++qq) +#pragma unroll + for (int k = 0; k < NT; ++k) + if (EXACT_T || k < n_tok) xin[k][qq] = __ldg(reinterpret_cast(x + (size_t) k * ldx) + (t + qq * BLOCK_SIZE)); +#pragma unroll + for (int qq = 0; qq < 5; ++qq) { + uint32_t weight; + if constexpr (PRE >= 5) weight = wpre[qq]; + else weight = __ldg(weights2 + t + qq * BLOCK_SIZE); + const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16)); +#pragma unroll + for (int k = 0; k < NT; ++k) { + if (EXACT_T || k < n_tok) { + acc[k] = __fmaf_rn(w0, xin[k][qq].x, acc[k]); + acc[k] = __fmaf_rn(w1, xin[k][qq].y, acc[k]); + } + } + } + if (fast == 2) { +#pragma unroll + for (int k = 0; k < NT; ++k) { + if (EXACT_T || k < n_tok) { + float acc2 = 0.0f; + for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { + const uint32_t weight = *(const volatile uint32_t*) (weights2 + pair); + const volatile float* ip = reinterpret_cast(reinterpret_cast(x + (size_t) k * ldx) + pair); + const float2 input = make_float2(ip[0], ip[1]); + acc2 = __fmaf_rn(f32_from_bf16((uint16_t) weight), input.x, acc2); + acc2 = __fmaf_rn(f32_from_bf16((uint16_t) (weight >> 16)), input.y, acc2); + } + mmvf_chk_note(acc[k], acc2, (int) blockIdx.x, t, n_in); + } + } + } + } else { + int q = 0; + for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE, ++q) { + const uint32_t weight = q < PRE ? wpre[q] : __ldg(weights2 + pair); + const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16)); +#pragma unroll + for (int k = 0; k < NT; ++k) { + if (EXACT_T || k < n_tok) { + const float2 input = __ldg(reinterpret_cast(x + (size_t) k * ldx) + pair); + acc[k] = __fmaf_rn(w0, input.x, acc[k]); + acc[k] = __fmaf_rn(w1, input.y, acc[k]); + } + } + } + } +#pragma unroll + for (int k = 0; k < NT; ++k) + if (EXACT_T || k < n_tok) acc[k] = mmvf_warp_sum(acc[k]); + if constexpr (BLOCK_SIZE > 32) { + if ((t & 31) == 0) +#pragma unroll + for (int k = 0; k < NT; ++k) + if (EXACT_T || k < n_tok) partials[k][t / 32] = acc[k]; + __syncthreads(); + if (t < 32) +#pragma unroll + for (int k = 0; k < NT; ++k) + if (EXACT_T || k < n_tok) acc[k] = mmvf_warp_sum(partials[k][t]); + } + if (t == 0) +#pragma unroll + for (int k = 0; k < NT; ++k) + if (EXACT_T || k < n_tok) y[(size_t) k * ldy + blockIdx.x] = acc[k]; +} + +// S25 (STRATA_MMVF_ROWS=1): RPB output rows per block. Each block read its row's weights once but all T activation +// rows again (n_out blocks x T x n_in floats through L2 - the part that grew with T); here a block reads the +// activations once for RPB rows. Per output, thread t still walks pairs t, t + BLOCK_SIZE, ... with the same two +// ordered FMAs and the same warp and block reductions: bit-identical to bf16_f32_mmvf_multi_kernel. +// S26 STRATA_LFUSE=1 (AUX): one more block computes row 0 of w_aux into y_aux[k * ldy_aux] - the same per-output +// code, so each aux output is bitwise what its own bf16_f32_mmvf_multi_kernel launch gave (the shared expert's gate) +template +__global__ void bf16_f32_mmvf_rows_kernel(const float* __restrict__ x, int64_t ldx, const uint16_t* __restrict__ w, + float* __restrict__ y, int64_t ldy, int n_in, int n_out, int n_tok, + const uint16_t* __restrict__ w_aux = nullptr, float* __restrict__ y_aux = nullptr, + int64_t ldy_aux = 0) { + const int t = threadIdx.x; + int o0 = blockIdx.x * RPB; + if constexpr (AUX) { + if (blockIdx.x == gridDim.x - 1) { w = w_aux; y = y_aux; ldy = ldy_aux; n_out = 1; o0 = 0; } + } + __shared__ float partials[RPB][NT][32]; + if constexpr (BLOCK_SIZE > 32) { + if (t < 32) +#pragma unroll + for (int r = 0; r < RPB; ++r) +#pragma unroll + for (int k = 0; k < NT; ++k) partials[r][k][t] = 0.0f; + __syncthreads(); + } + float acc[RPB][NT]; +#pragma unroll + for (int r = 0; r < RPB; ++r) +#pragma unroll + for (int k = 0; k < NT; ++k) acc[r][k] = 0.0f; + for (int pair = t; pair < n_in / 2; pair += BLOCK_SIZE) { + float2 in[NT]; +#pragma unroll + for (int k = 0; k < NT; ++k) + if (k < n_tok) in[k] = reinterpret_cast(x + (size_t) k * ldx)[pair]; +#pragma unroll + for (int r = 0; r < RPB; ++r) { + if (o0 + r >= n_out) break; + const uint32_t weight = reinterpret_cast(w + (size_t) (o0 + r) * n_in)[pair]; + const float w0 = f32_from_bf16((uint16_t) weight), w1 = f32_from_bf16((uint16_t) (weight >> 16)); +#pragma unroll + for (int k = 0; k < NT; ++k) { + if (k < n_tok) { + acc[r][k] = __fmaf_rn(w0, in[k].x, acc[r][k]); + acc[r][k] = __fmaf_rn(w1, in[k].y, acc[r][k]); + } + } + } + } + if constexpr (TS) { // S26 STRATA_TSUM=1: the RPB x NT warp sums (both stages) as transposed butterflies, bitwise the same + constexpr int V = RPB * NT, P = s26ts::pow2_ceil(V); + const int lane = t & 31, j = s26ts::tsum_token

(lane); + const bool own = lane == s26ts::tsum_lane

(j) && j < V; + float v[P]; +#pragma unroll + for (int q = 0; q < P; ++q) v[q] = q < V ? acc[q / NT][q % NT] : 0.0f; + float sum = s26ts::tsum

(v, lane); + if constexpr (BLOCK_SIZE > 32) { + if (own) partials[j / NT][j % NT][t / 32] = sum; + __syncthreads(); + if (t >= 32) return; +#pragma unroll + for (int q = 0; q < P; ++q) v[q] = q < V ? partials[q / NT][q % NT][t] : 0.0f; + sum = s26ts::tsum

(v, lane); + } + const int r = j / NT, k = j % NT; + if (own && o0 + r < n_out && k < n_tok) y[(size_t) k * ldy + o0 + r] = sum; + return; + } +#pragma unroll + for (int r = 0; r < RPB; ++r) +#pragma unroll + for (int k = 0; k < NT; ++k) acc[r][k] = mmvf_warp_sum(acc[r][k]); + if constexpr (BLOCK_SIZE > 32) { + if ((t & 31) == 0) +#pragma unroll + for (int r = 0; r < RPB; ++r) +#pragma unroll + for (int k = 0; k < NT; ++k) partials[r][k][t / 32] = acc[r][k]; + __syncthreads(); + if (t < 32) +#pragma unroll + for (int r = 0; r < RPB; ++r) +#pragma unroll + for (int k = 0; k < NT; ++k) acc[r][k] = mmvf_warp_sum(partials[r][k][t]); + } + if (t == 0) +#pragma unroll + for (int r = 0; r < RPB; ++r) + if (o0 + r < n_out) +#pragma unroll + for (int k = 0; k < NT; ++k) + if (k < n_tok) y[(size_t) k * ldy + o0 + r] = acc[r][k]; +} + +template +void launch_rows(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy, int n_in, int n_out, int n_tok, + cudaStream_t st, const uint16_t* w_aux = nullptr, float* y_aux = nullptr, int64_t ldy_aux = 0) { + constexpr int RPB = 4; + const unsigned nb = (unsigned) ((n_out + RPB - 1) / RPB); + static const bool ts = [] { const char* v = std::getenv("STRATA_TSUM"); return v && v[0] == '1'; }(); + if (w_aux != nullptr) { // S26 STRATA_LFUSE: + one aux row block +#define S26_AUX(TSV) \ + if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok, w_aux, y_aux, ldy_aux); \ + else bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok, w_aux, y_aux, ldy_aux); + if (ts) { S26_AUX(true) } else { S26_AUX(false) } +#undef S26_AUX + return; + } + if (ts) { + if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); + else bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); + return; + } + if (n_tok <= 4) bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); + else bf16_f32_mmvf_rows_kernel<<>>(x, ldx, w, y, ldy, n_in, n_out, n_tok); +} + +void mmvf_chk_report() { + unsigned long long h[8] = {}; + if (cudaMemcpyFromSymbol(h, g_mmvf_chk, sizeof(h)) != cudaSuccess) return; + std::fprintf(stderr, "strata mmvf check: %llu accumulators compared, %llu differ; first: block %llu thread %llu n_in %llu fast %08llx plain %08llx\n", h[0], h[1], h[2], h[3], h[4], h[5], h[6]); +} +// BW-DENSE STRATA_SMALL_UNR bit 0: see bf16_f32_mmvf_multi_kernel (bw_dense_defaults.hpp: on for a 12.0 card, else off; the variable wins) +int mmvf_fast_bit() { + static int per_dev[64]; // 0 unknown, else bit + 1 + const char* e = std::getenv("STRATA_SMALL_UNR"); + if (e != nullptr) return std::atoi(e) & 1; +#if defined(__HIPCC__) + return 0; +#else + int dev = 0; + if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) { cudaGetLastError(); return 0; } + if (!per_dev[dev]) { + int ma = 0, mi = 0; + const bool ok = cudaDeviceGetAttribute(&ma, cudaDevAttrComputeCapabilityMajor, dev) == cudaSuccess && + cudaDeviceGetAttribute(&mi, cudaDevAttrComputeCapabilityMinor, dev) == cudaSuccess; + if (!ok) cudaGetLastError(); + per_dev[dev] = 1 + (ok ? (strata::small_unr_default(ma, mi) & 1) : 0); + } + return per_dev[dev] - 1; +#endif +} +// AMD-Q STRATA_MMVF_CHECK=1 (with the switch on): the kernel also computes the plain read and counts the accumulators that differ (2 = check mode) +int mmvf_fast() { + const int b = mmvf_fast_bit(); + if (b == 0) return 0; + static const bool chk = [] { + const char* c = std::getenv("STRATA_MMVF_CHECK"); + const bool on = c != nullptr && c[0] == '1'; + if (on) std::atexit(mmvf_chk_report); + return on; + }(); + return chk ? 2 : b; +} +int mmvf_block_size(int64_t n_in) { + int best = 32; + int64_t best_iterations = (n_in + 63) / 64; + for (int candidate = 64; candidate <= 256; candidate += 32) { + const int64_t iterations = (n_in + 2 * candidate - 1) / (2 * candidate); + if (iterations < best_iterations) { + best_iterations = iterations; + best = candidate; + } + } + return best; +} + +} // namespace + +bool bf16_gemv_fp32_mmvf_multi_aux(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy, + int64_t n_in, int64_t n_out, int n_tok, const uint16_t* w_aux, float* y_aux, + int64_t ldy_aux, void* stream) { + static const bool rows = [] { const char* v = std::getenv("STRATA_MMVF_ROWS"); return v && v[0] == '1'; }(); + // exactly the conditions under which both calls would take the rows / multi kernels at the same block size + if (!rows || n_out < 64 || n_tok < 2 || n_tok > 8 || n_in <= 0 || (n_in & 1) != 0 || (ldx & 1) != 0 || x == nullptr || + w == nullptr || y == nullptr || w_aux == nullptr || y_aux == nullptr || (reinterpret_cast(x) & 7u) != 0) + return false; + const cudaStream_t st = (cudaStream_t) stream; + const int ni = (int) n_in, no = (int) n_out; + switch (mmvf_block_size(n_in)) { + case 32: launch_rows<32>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; + case 64: launch_rows<64>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; + case 96: launch_rows<96>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; + case 128: launch_rows<128>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; + case 160: launch_rows<160>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; + case 192: launch_rows<192>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; + case 224: launch_rows<224>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; + default: launch_rows<256>(x, ldx, w, y, ldy, ni, no, n_tok, st, w_aux, y_aux, ldy_aux); break; + } + const cudaError_t result = cudaGetLastError(); + if (result != cudaSuccess) + throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf_multi_aux launch: ") + cudaGetErrorString(result)); + return true; +} + +void bf16_gemv_fp32_mmvf_multi(const float* x, int64_t ldx, const uint16_t* w, float* y, int64_t ldy, + int64_t n_in, int64_t n_out, int n_tok, void* stream) { + if (n_tok == 1 && ldy >= n_out) { bf16_gemv_fp32_mmvf(x, w, y, n_in, n_out, stream); return; } + if (n_tok < 1 || n_tok > STRATA_VERIFY_MAX_T || n_in <= 0 || (n_in & 1) != 0 || n_out <= 0 || (ldx & 1) != 0 || + x == nullptr || w == nullptr || y == nullptr || (reinterpret_cast(x) & 7u) != 0) + throw std::invalid_argument("bf16_gemv_fp32_mmvf_multi: 1..8 rows (16 in a wide build), even n_in/ldx, aligned pointers"); + const cudaStream_t st = (cudaStream_t) stream; + static const bool rows = [] { const char* v = std::getenv("STRATA_MMVF_ROWS"); return v && v[0] == '1'; }(); + if (rows && n_out >= 64 && n_tok <= 8) { // (the opt-in rows kernels hold 8 columns: a wider window takes the switch below) + const int ni = (int) n_in, no = (int) n_out; + switch (mmvf_block_size(n_in)) { + case 32: launch_rows<32>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; + case 64: launch_rows<64>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; + case 96: launch_rows<96>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; + case 128: launch_rows<128>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; + case 160: launch_rows<160>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; + case 192: launch_rows<192>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; + case 224: launch_rows<224>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; + default: launch_rows<256>(x, ldx, w, y, ldy, ni, no, n_tok, st); break; + } + const cudaError_t result = cudaGetLastError(); + if (result != cudaSuccess) + throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf_multi launch: ") + cudaGetErrorString(result)); + return; + } + // 9..16 rows (wide builds only): one exact instantiation per row count, each row's dot independent of the others. 7 and 8 + // rows stay with the default case below, the instantiation they always had. +#if STRATA_VERIFY_MAX_T > 8 +#define STRATA_MMVF_W(N, T) case T: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; +#if STRATA_VERIFY_MAX_T > 12 +#define STRATA_MMVF_W13(N) STRATA_MMVF_W(N, 13) STRATA_MMVF_W(N, 14) STRATA_MMVF_W(N, 15) STRATA_MMVF_W(N, 16) +#else +#define STRATA_MMVF_W13(N) +#endif +#define STRATA_MMVF_WIDE(N) \ + STRATA_MMVF_W(N, 9) STRATA_MMVF_W(N, 10) STRATA_MMVF_W(N, 11) STRATA_MMVF_W(N, 12) \ + STRATA_MMVF_W13(N) +#else +#define STRATA_MMVF_WIDE(N) +#endif +#define STRATA_MMVF_M(N) case N: \ + switch (n_tok) { \ + STRATA_MMVF_WIDE(N) \ + case 1: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ + case 2: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ + case 3: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ + case 4: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ + case 5: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ + case 6: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ + default: launch_pdl(bf16_f32_mmvf_multi_kernel, dim3((unsigned) n_out), dim3(N), 0, st, x, ldx, w, y, ldy, (int) n_in, n_tok, mmvf_fast()); break; \ + } break + switch (mmvf_block_size(n_in)) { + STRATA_MMVF_M(32); STRATA_MMVF_M(64); STRATA_MMVF_M(96); STRATA_MMVF_M(128); + STRATA_MMVF_M(160); STRATA_MMVF_M(192); STRATA_MMVF_M(224); STRATA_MMVF_M(256); + } +#undef STRATA_MMVF_M +#undef STRATA_MMVF_WIDE +#ifdef STRATA_MMVF_W +#undef STRATA_MMVF_W +#endif + const cudaError_t result = cudaGetLastError(); + if (result != cudaSuccess) + throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf_multi launch: ") + cudaGetErrorString(result)); +} + +void bf16_gemv_fp32_mmvf(const float* x, const uint16_t* w, float* y, + int64_t n_in, int64_t n_out, void* stream) { + if (n_in <= 0 || (n_in & 1) != 0 || n_in > std::numeric_limits::max() || + n_out <= 0 || n_out > std::numeric_limits::max()) + throw std::invalid_argument("bf16_gemv_fp32_mmvf: require positive even n_in and positive n_out <= INT_MAX"); + if (x == nullptr || w == nullptr || y == nullptr || + (reinterpret_cast(x) & 7u) != 0 || + (reinterpret_cast(w) & 3u) != 0 || + (reinterpret_cast(y) & 3u) != 0) + throw std::invalid_argument("bf16_gemv_fp32_mmvf: null or misaligned pointer"); + const cudaStream_t st = (cudaStream_t) stream; +#define STRATA_MMVF_CASE(N) case N: \ + bf16_f32_mmvf_kernel<<<(unsigned) n_out, N, 0, st>>>(x, w, y, (int) n_in, mmvf_fast()); break + switch (mmvf_block_size(n_in)) { + STRATA_MMVF_CASE(32); + STRATA_MMVF_CASE(64); + STRATA_MMVF_CASE(96); + STRATA_MMVF_CASE(128); + STRATA_MMVF_CASE(160); + STRATA_MMVF_CASE(192); + STRATA_MMVF_CASE(224); + STRATA_MMVF_CASE(256); + } +#undef STRATA_MMVF_CASE + const cudaError_t result = cudaGetLastError(); + if (result != cudaSuccess) + throw std::runtime_error(std::string("bf16_gemv_fp32_mmvf launch: ") + cudaGetErrorString(result)); +} + + + +void bf16_gemv_fp32_mmvf_cols(const float* x, const uint16_t* w, float* y, int64_t n_in, int64_t n_out, int ncols, + void* stream) { + // columns contiguous: the existing multi-row kernel (weight read once, each output bitwise its one-row call), + // up to 8 rows per launch + for (int c0 = 0; c0 < ncols; c0 += 8) { + const int nc = ncols - c0 < 8 ? ncols - c0 : 8; + if (nc == 1) bf16_gemv_fp32_mmvf(x + (size_t) c0 * n_in, w, y + (size_t) c0 * n_out, n_in, n_out, stream); + else bf16_gemv_fp32_mmvf_multi(x + (size_t) c0 * n_in, n_in, w, y + (size_t) c0 * n_out, n_out, n_in, n_out, nc, + stream); + } +} + +} // namespace strata::kernels diff --git a/engine/src/kernels/cuda/qsa_decode_attn.cu b/engine/src/kernels/cuda/qsa_decode_attn.cu index 07116248f2..bae3bcfef0 100644 --- a/engine/src/kernels/cuda/qsa_decode_attn.cu +++ b/engine/src/kernels/cuda/qsa_decode_attn.cu @@ -340,10 +340,39 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel(const float* __rest scratch_stride); } +constexpr int kLegacyAttnSlots = 8; +struct QsaAttnLegacySlots { QsaAttnPools p[kLegacyAttnSlots]; }; +static_assert(sizeof(QsaAttnLegacySlots) + 256 <= 4096, "kernel parameter budget"); + +// Each homogeneous KV mode uses at most five pointers, including its page table. +struct QsaAttnSlotPtrs { const void* k; const void* v; const uint16_t* ks; const uint16_t* vs; const int32_t* pages; }; +struct QsaAttnSlotPtrs64 { QsaAttnSlotPtrs p[kQsaAttnMaxSlots]; }; +static_assert(sizeof(QsaAttnSlotPtrs) == 5 * sizeof(void*), "unexpected pointer layout"); +static_assert(sizeof(QsaAttnSlotPtrs64) + 256 <= 3840, "kernel parameter budget"); + +template +__device__ __forceinline__ QsaAttnPools unpack_attn_slot(const QsaAttnSlotPtrs& x) { + QsaAttnPools p{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, x.pages +#if defined(STRATA_KV_FP8) + , false +#endif + }; + if constexpr (MODE == 0) { p.k_pool = (const uint16_t*) x.k; p.v_pool = (const uint16_t*) x.v; } + else if constexpr (MODE == 1) { p.k_q = (const int8_t*) x.k; p.v_q = (const int8_t*) x.v; p.k_scale = x.ks; p.v_scale = x.vs; } + else if constexpr (MODE == 2) { p.k_q4 = (const uint8_t*) x.k; p.v_q4 = (const uint8_t*) x.v; } + else { p.k_q = (const int8_t*) x.k; p.v_q4 = (const uint8_t*) x.v; p.k_scale = x.ks; } + return p; +} +#if defined(__HIPCC__) +#define STRATA_ATTN_GRID_CONSTANT const +#else +#define STRATA_ATTN_GRID_CONSTANT const __grid_constant__ +#endif + // The same blocks for queries of different slots: query blockIdx.z reads its own slot's pools, everything else is // attn_chunk_kernel's batched form (qsa_decode_attn_slots). template -__global__ void __launch_bounds__(THREADS) attn_chunk_slots_kernel(const float* __restrict__ q, QsaAttnSlotPools ps, +__global__ void __launch_bounds__(THREADS) attn_chunk_slots_kernel(const float* __restrict__ q, QsaAttnLegacySlots ps, const int32_t* __restrict__ ids, const int32_t* __restrict__ step, int n_kv_heads, int page_size, float scale, @@ -355,6 +384,16 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_slots_kernel(const float* part_acc, part_m, part_l, n_chunks, cap, scratch_stride); } +// CUDA graph captures the compact descriptor by value, not a borrowed host pointer. +template +__global__ void __launch_bounds__(THREADS) attn_chunk_slots64_kernel(const float* __restrict__ q, STRATA_ATTN_GRID_CONSTANT QsaAttnSlotPtrs64 ps, + const int32_t* __restrict__ ids, const int32_t* __restrict__ step, int n_kv_heads, int page_size, float scale, + float* __restrict__ part_acc, float* __restrict__ part_m, float* __restrict__ part_l, int n_chunks, int cap, long long stride) { + const QsaAttnPools p = unpack_attn_slot(ps.p[blockIdx.z]); + attn_chunk_body(q, p, ids, step, n_kv_heads, page_size, scale, + part_acc, part_m, part_l, n_chunks, cap, stride); +} + // Batch windows: query blockIdx.z attends over its own slot's pools (rows.p[z]); the default kernel otherwise. template __global__ void __launch_bounds__(THREADS) attn_chunk_rows_kernel(const float* __restrict__ q, QsaAttnPoolsRows rows, @@ -742,13 +781,23 @@ __global__ void __launch_bounds__(THREADS) attn_chunk_kernel_pre75(const float* // with the exact pre75 arithmetic and accumulation order (bitwise identical to per-query attn_chunk_kernel_pre75). template __global__ void __launch_bounds__(THREADS) attn_chunk_slots_pre75_kernel( - const float* __restrict__ q, QsaAttnSlotPools ps, const int32_t* __restrict__ ids, + const float* __restrict__ q, QsaAttnLegacySlots ps, const int32_t* __restrict__ ids, const int32_t* __restrict__ step, int n_kv_heads, int page_size, float scale, float* __restrict__ part_acc, float* __restrict__ part_m, float* __restrict__ part_l, int n_chunks, int cap, long long scratch_stride) { attn_chunk_pre75_body(q, ps.p[blockIdx.z], ids, step, n_kv_heads, page_size, scale, part_acc, part_m, part_l, n_chunks, cap, scratch_stride); } +template +__global__ void __launch_bounds__(THREADS) attn_chunk_slots64_pre75_kernel( + const float* __restrict__ q, STRATA_ATTN_GRID_CONSTANT QsaAttnSlotPtrs64 ps, const int32_t* __restrict__ ids, + const int32_t* __restrict__ step, int n_kv_heads, int page_size, float scale, float* __restrict__ part_acc, + float* __restrict__ part_m, float* __restrict__ part_l, int n_chunks, int cap, long long stride) { + const QsaAttnPools p = unpack_attn_slot(ps.p[blockIdx.z]); + attn_chunk_pre75_body(q, p, ids, step, n_kv_heads, page_size, scale, part_acc, part_m, part_l, + n_chunks, cap, stride); +} + // Per-row batched form of attn_chunk_kernel_pre75 (STRATA_BATCH_ATTN_ROWS=1 with STRATA_ATTN_DAILY=1). template __global__ void __launch_bounds__(THREADS) attn_chunk_rows_pre75_kernel( @@ -1155,6 +1204,28 @@ bool qsa_decode_attn_slots(const float* q, const QsaAttnSlotPools& pools, const std::fprintf(stderr, "qsa_decode_attn_slots: unsupported geometry or missing buffers\n"); std::exit(1); } + static const bool batch64 = [] { + const char* v = std::getenv("STRATA_QSA_ATTN_BATCH64"); return v && v[0] == '1' && v[1] == '\0'; + }(); + const int group = batch64 ? 64 : kLegacyAttnSlots; + if (n_q > group) { + const size_t stride = (size_t) qsa_decode_attn_scratch_floats(cap, s); + for (int64_t base = 0; base < n_q; base += group) { + const int count = (int) std::min(group, n_q - base); + QsaAttnSlotPools sub{}; + for (int i = 0; i < count; ++i) sub.p[i] = pools.p[base + i]; + if (!qsa_decode_attn_slots(q + (size_t)base * s.n_head * HD, sub, ids + (size_t)base * cap, + steps + (size_t)base * kStepCount, cap, s, scratch + (size_t)base * stride, + attn + (size_t)base * s.n_head * HD, count, stream)) { + for (int i = 0; i < count; ++i) { + const size_t row = (size_t)base + i; + qsa_decode_attn_batch(q + row * s.n_head * HD, pools.p[row], ids + row * cap, + steps + row * kStepCount, cap, s, scratch + row * stride, attn + row * s.n_head * HD, 1, stream); + } + } + } + return true; + } const int kv_mode = attn_kv_mode(pools.p[0]); for (int64_t z = 0; z < n_q; ++z) { if (!pools.p[z].page_table) { @@ -1183,14 +1254,25 @@ bool qsa_decode_attn_slots(const float* q, const QsaAttnSlotPools& pools, const float* part_m = scratch + (size_t) n_chunks * s.n_head * HD; float* part_l = part_m + (size_t) n_chunks * s.n_head; const float scale = 1.0f / sqrtf((float) HD); + const bool use64 = batch64 && n_q > kLegacyAttnSlots; + QsaAttnLegacySlots legacy{}; + QsaAttnSlotPtrs64 compact{}; + for (int64_t z = 0; z < n_q; ++z) { + const auto& p = pools.p[z]; + if (!use64) { legacy.p[z] = p; continue; } + auto& d = compact.p[z]; + d.k = kv_mode == 0 ? (const void*)p.k_pool : kv_mode == 2 ? (const void*)p.k_q4 : (const void*)p.k_q; + d.v = kv_mode == 0 ? (const void*)p.v_pool : kv_mode == 1 ? (const void*)p.v_q : (const void*)p.v_q4; + d.ks = p.k_scale; d.vs = p.v_scale; d.pages = p.page_table; + } const dim3 grid((unsigned) n_chunks, (unsigned) s.n_head_kv, (unsigned) n_q); cudaStream_t st = (cudaStream_t) stream; #if defined(STRATA_ATTN_PRE75_BUILT) if (use_pre75) { #if defined(__HIPCC__) -#define STRATA_ATTN_SLOTS_PRE75(M) attn_chunk_slots_pre75_kernel<<>>(q, pools, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride) +#define STRATA_ATTN_SLOTS_PRE75(M) do { if (use64) attn_chunk_slots64_pre75_kernel<<>>(q, compact, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); else attn_chunk_slots_pre75_kernel<<>>(q, legacy, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); } while (0) #else -#define STRATA_ATTN_SLOTS_PRE75(M) attn_chunk_slots_pre75_kernel<<>>(q, pools, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride) +#define STRATA_ATTN_SLOTS_PRE75(M) do { if (use64) attn_chunk_slots64_pre75_kernel<<>>(q, compact, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); else attn_chunk_slots_pre75_kernel<<>>(q, legacy, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); } while (0) #endif if (kv_mode == 3) STRATA_ATTN_SLOTS_PRE75(3); else if (kv_mode == 2) STRATA_ATTN_SLOTS_PRE75(2); @@ -1200,7 +1282,7 @@ bool qsa_decode_attn_slots(const float* q, const QsaAttnSlotPools& pools, const } else #endif { -#define STRATA_ATTN_SLOTS(M, LC) attn_chunk_slots_kernel<<>>(q, pools, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride) +#define STRATA_ATTN_SLOTS(M, LC) do { if (use64) attn_chunk_slots64_kernel<<>>(q, compact, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); else attn_chunk_slots_kernel<<>>(q, legacy, ids, steps, (int) s.n_head_kv, (int) s.page_size, scale, part_acc, part_m, part_l, n_chunks, (int) cap, stride); } while (0) if (lane_cell) { if (kv_mode == 3) STRATA_ATTN_SLOTS(3, true); else if (kv_mode == 2) STRATA_ATTN_SLOTS(2, true); diff --git a/engine/src/kernels/iq_multi_parity.cpp b/engine/src/kernels/iq_multi_parity.cpp index 64b6e437cc..86a0cf2175 100644 --- a/engine/src/kernels/iq_multi_parity.cpp +++ b/engine/src/kernels/iq_multi_parity.cpp @@ -65,6 +65,7 @@ const char* name_of(int t) { case 42: return "Q2_0"; case 12: return "Q4_K"; case 13: return "Q5_K"; + case 14: return "Q6_K"; case 7: return "Q5_1"; case 8: return "Q8_0"; default: return "?"; @@ -84,6 +85,10 @@ std::vector random_rows(int t, int64_t rows, int64_t n, std::mt19937& r // sign and the exponent's top bits: 0x1 / 0x2 (or 0x9 / 0xA) keeps it in 2^-11 .. 2^-3 const uint8_t nib = (uint8_t) ((sgn(rng) == 0 ? 0x8 : 0x0) | (1 + (byte(rng) & 1))); w[o + 55] = (uint8_t) ((w[o + 55] & 0x0F) | (nib << 4)); + } else if (t == 14) { + const int e = 1 + ex(rng) % 4; + const uint16_t h = (uint16_t) ((sgn(rng) == 0 ? 0x8000 : 0) | (e << 10) | man(rng)); // Q6_K: d at block end + std::memcpy(&w[o + 208], &h, 2); } else { const uint16_t h = (uint16_t) ((sgn(rng) == 0 ? 0x8000 : 0) | (ex(rng) << 10) | man(rng)); // 2^-13 .. 2^-6 std::memcpy(&w[o], &h, 2); @@ -448,6 +453,16 @@ int main(int argc, char** argv) { for (int gu : {12, 13}) for (int dt : {7, 8}) check_grouped(gu, dt, 2560, 640, s, rng); for (int gu : {12, 13}) check_grouped(gu, 13, 2048, 512, s, rng); // Qwen3.6-35B-A3B's geometry + check_grouped(12, 12, 2048, 512, s, rng); +#ifdef STRATA_Q6K_EXPERTS + for (int gu : {12, 13, 14}) check_grouped(gu, 14, 2048, 512, s, rng); +#endif + check_grouped(8, 8, 2048, 512, s, rng); + check_grouped(18, 21, 2048, 512, s, rng); + check_grouped(21, 23, 2048, 512, s, rng); + check_grouped(23, 12, 2048, 512, s, rng); + check_grouped(20, 20, 2048, 512, s, rng); + check_grouped(42, 42, 2048, 512, s, rng); // Q8_0 gate/up (UD-Q6_K_XL's layer 2, the rt-q8 drafter's experts): 320 calls a row at 2560, the full-warp rows - with // STRATA_KQ_SPLIT=1 a 16-row grid left the up rows unwritten (native_expert_grouped's gu_sub16_rows) check_grouped(8, 8, 2560, 640, s, rng); diff --git a/engine/src/kernels/mmvf_rows_parity.cpp b/engine/src/kernels/mmvf_rows_parity.cpp index 189f56109c..5bf3493d03 100644 --- a/engine/src/kernels/mmvf_rows_parity.cpp +++ b/engine/src/kernels/mmvf_rows_parity.cpp @@ -1,7 +1,10 @@ // Exact multi-row BF16/F32 GEMV checks against independent single-row calls. // Run with STRATA_MMVF_ROWS=1; synthetic data, no model or private prompts. #include "strata/kernels/bf16_gemv.hpp" +#include "strata/kernels/window_limits.hpp" #include +#include +#include #include #include #include @@ -16,10 +19,146 @@ static void ck(cudaError_t e) { template static T* alloc(size_t n) { T* p = nullptr; ck(cudaMalloc(&p, n * sizeof(T))); return p; } -int main() { +#if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL) +static int test_cublas(cudaStream_t s, bool bench) { + if (k::kWindowMaxT < 16) { std::puts("SKIP: build window < 16"); return 77; } + int device = 0; ck(cudaGetDevice(&device)); + auto* ctx = k::bf16_gemm_cublas_init(device, s, k::kWindowMaxT, 8192); + if (!ctx) { std::puts("SKIP: BF16 cuBLAS unavailable"); return 77; } + cudaGraph_t rejected = nullptr; + ck(cudaStreamBeginCapture(s, cudaStreamCaptureModeThreadLocal)); + auto* invalid = k::bf16_gemm_cublas_init(device, s, k::kWindowMaxT, 8192); + ck(cudaStreamEndCapture(s, &rejected)); + if (invalid) { std::fprintf(stderr, "cuBLAS context accepted during capture\n"); return 1; } + ck(cudaGraphDestroy(rejected)); + int failures = 0; + constexpr int K = 2048; + const int LDX = bench ? K : K + 2; + for (int T : {16, 32, 64}) { + if (T > k::kWindowMaxT || (bench && T != std::min(64, k::kWindowMaxT)) || (!bench && T == 32)) continue; + std::mt19937 rng(50124); std::uniform_real_distribution dist(-1.f, 1.f); + std::vector hx((size_t)T * LDX); + for (float& v : hx) v = dist(rng); + float* x = alloc(hx.size()); ck(cudaMemcpy(x, hx.data(), hx.size()*4, cudaMemcpyHostToDevice)); + for (int N : {8192, 2048, 256}) { + const int LDY = bench ? N : N + 3; + std::vector hw((size_t)N * K); + for (auto& v : hw) { float f = dist(rng); uint32_t bits; std::memcpy(&bits, &f, 4); v = uint16_t(bits >> 16); } + uint16_t* w = alloc(hw.size()); ck(cudaMemcpy(w, hw.data(), hw.size()*2, cudaMemcpyHostToDevice)); + float* ref = alloc((size_t)T*LDY); float* got = alloc((size_t)T*LDY); + float* got2 = N == 8192 && !bench ? alloc((size_t)T*LDY) : nullptr; + for (int t=0; t a((size_t)T*LDY), b(a.size()), replay(a.size()); + ck(cudaMemcpy(a.data(), ref, a.size()*4, cudaMemcpyDeviceToHost)); + ck(cudaMemcpy(b.data(), got, b.size()*4, cudaMemcpyDeviceToHost)); + double err = 0, norm = 0; + for (int t=0; t left(N), right(N); + for (int r=0;ra[off+q]; }); + std::stable_sort(right.begin(),right.end(),[&](int p,int q){ return b[off+p]>b[off+q]; }); + if (!std::equal(left.begin(),left.begin()+8,right.begin())) ++changed; + } + std::printf("cuBLAS router top8 order differences: %d/%d\n",changed,T); + } + if (!std::isfinite(rel) || rel > 0.01) ++failures; + if (N == 256) { + // Odd stride and a four-byte offset exercise the scalar conversion fallback. + const int odd_stride = LDX + 1; + float* odd = alloc(1u + (size_t)T * odd_stride); + ck(cudaMemcpy2DAsync(odd + 1, (size_t)odd_stride * 4, x, (size_t)LDX * 4, + (size_t)K * 4, T, cudaMemcpyDeviceToDevice, s)); + k::bf16_gemm_cublas_stage(ctx, odd + 1, odd_stride, K, T); + k::bf16_gemm_cublas_f32in(ctx, w, got, LDY, K, N, T); + ck(cudaStreamSynchronize(s)); + ck(cudaMemcpy(replay.data(), got, replay.size()*4, cudaMemcpyDeviceToHost)); + for (int t=0;t nodes(count); + ck(cudaGraphGetNodes(graph,nodes.data(),&count)); + for (auto node : nodes) { cudaGraphNodeType type; ck(cudaGraphNodeGetType(node,&type)); + if (type == cudaGraphNodeTypeMemAlloc || type == cudaGraphNodeTypeMemFree) ++failures; } + ck(cudaGraphInstantiate(&exec,graph,0)); + for (int rep=0; rep<2; ++rep) { + ck(cudaGraphLaunch(exec,s)); ck(cudaStreamSynchronize(s)); + ck(cudaMemcpy(replay.data(),got,replay.size()*4,cudaMemcpyDeviceToHost)); + for (int t=0;t((size_t)T*LDY); + cudaGraph_t graph2; cudaGraphExec_t exec2; + ck(cudaStreamBeginCapture(s2,cudaStreamCaptureModeThreadLocal)); + k::bf16_gemm_cublas_stage(ctx2,x,LDX,K,T); + k::bf16_gemm_cublas_f32in(ctx2,w,other,LDY,K,N,T); + ck(cudaStreamEndCapture(s2,&graph2)); ck(cudaGraphInstantiate(&exec2,graph2,0)); + ck(cudaGraphLaunch(exec,s)); ck(cudaGraphLaunch(exec2,s2)); + ck(cudaStreamSynchronize(s)); ck(cudaStreamSynchronize(s2)); + ck(cudaMemcpy(replay.data(),other,replay.size()*4,cudaMemcpyDeviceToHost)); + for (int t=0;t> 1) % 7u) << 10) | ((x >> 4) & 0x3ffu)); } -constexpr int64_t kMaxCells = 32768; - struct Slot { float *pooled = nullptr, *dead = nullptr; int32_t* page_table = nullptr; @@ -57,7 +55,7 @@ struct Slot { k::QsaAttnPools pools; }; -Slot make_slot(int fmt, const k::QsaShapes& s, int64_t max_blocks, std::mt19937& r) { +Slot make_slot(int fmt, const k::QsaShapes& s, int64_t max_blocks, int64_t max_cells, std::mt19937& r) { Slot sl; std::normal_distribution nd(0.f, 1.f); std::vector pooled((size_t) (max_blocks * s.idx_dim)), dead((size_t) s.idx_dim); @@ -65,7 +63,7 @@ Slot make_slot(int fmt, const k::QsaShapes& s, int64_t max_blocks, std::mt19937& for (auto& v : dead) v = nd(r); sl.pooled = up(pooled); sl.dead = up(dead); - const int64_t pages = kMaxCells / s.page_size; + const int64_t pages = (max_cells + s.page_size - 1) / s.page_size; std::vector pt((size_t) pages); std::iota(pt.begin(), pt.end(), 0); std::shuffle(pt.begin(), pt.end(), r); @@ -114,13 +112,19 @@ enum Result { kPass, kFail, kSkip }; Result check(int fmt, int m, const int64_t* ctx, uint32_t seed) { const k::QsaShapes s = k::qsa_real_shapes(); const int64_t cap = k::qsa_selection_width(k::kTopkMaxCells, s); - const int64_t max_blocks = kMaxCells / s.idx_block + 2; + const int64_t max_cells = *std::max_element(ctx, ctx + m); + const int64_t max_blocks = max_cells / s.idx_block + 2; const int64_t NH = s.n_head, HD = s.head_dim, QI = s.idx_n_head * s.idx_dim; const int64_t sf = (int64_t) k::qsa_decode_attn_scratch_floats(cap, s); std::mt19937 r(seed); std::normal_distribution nd(0.f, 1.f); std::vector slots; - for (int i = 0; i < m; ++i) slots.push_back(make_slot(fmt, s, max_blocks, r)); + for (int i = 0; i < m; ++i) { + slots.push_back(make_slot(fmt == 4 ? 1 : fmt, s, max_blocks, max_cells, r)); +#if defined(STRATA_KV_FP8) + if (fmt == 4) slots.back().pools.fp8 = true; +#endif + } std::vector qidx((size_t) (m * QI)), qcur((size_t) (m * NH * HD)); for (auto& v : qidx) v = nd(r); for (auto& v : qcur) v = nd(r); @@ -157,10 +161,14 @@ Result check(int fmt, int m, const int64_t* ctx, uint32_t seed) { // a declined launch is no failure: the engine runs the per-row calls there (STRATA_SCORES_MULTI=0, the pre-sm_75 // attention, the gfx906 query swizzle) Result res = kPass; - if (!k::qsa_block_scores_slots(pooled, dead, d_qidx, d_steps, m, max_blocks, s, sc[1], nullptr)) { - std::puts("SKIP: qsa_block_scores_slots declined (STRATA_SCORES_MULTI=0); the engine keeps the per-row calls"); - res = kSkip; - } else { + for (int base = 0; base < m; base += 8) { + const int count = std::min(8, m - base); + if (!k::qsa_block_scores_slots(pooled + base, dead + base, d_qidx + base * QI, + d_steps + base * k::kStepCount, count, max_blocks, s, sc[1] + base * max_blocks, nullptr)) { + std::puts("SKIP: qsa_block_scores_slots declined"); res = kSkip; break; + } + } + if (res == kPass) { k::qsa_block_topk(sc[1], d_steps, m, max_blocks, cap, s, sel[1], nullptr); if (!k::qsa_decode_attn_slots(d_qcur, px, sel[1], d_steps, cap, s, scratch[1], attn[1], m, nullptr)) { std::puts("SKIP: qsa_decode_attn_slots declined (the pre-sm_75 or gfx906 query-swizzle attention); the " @@ -170,6 +178,16 @@ Result check(int fmt, int m, const int64_t* ctx, uint32_t seed) { } ck(cudaDeviceSynchronize()); bool ok = res == kPass; + if (ok && m > 8 && fmt == 1) { + const char* flag = std::getenv("STRATA_QSA_ATTN_BATCH64"); + if (flag && std::strcmp(flag, "1") == 0) { + auto mixed = px; + mixed.p[1].k_q4 = reinterpret_cast(mixed.p[1].k_q); + if (k::qsa_decode_attn_slots(d_qcur, mixed, sel[1], d_steps, cap, s, scratch[1], attn[1], m, nullptr)) { + std::fprintf(stderr, "FAIL: mixed KV modes were not declined\n"); ok = false; + } + } + } const auto s0 = down(sc[0], (size_t) (m * max_blocks)), s1 = down(sc[1], (size_t) (m * max_blocks)); const auto i0 = down(sel[0], (size_t) (m * cap)), i1 = down(sel[1], (size_t) (m * cap)); const auto a0 = down(attn[0], (size_t) (m * NH * HD)), a1 = down(attn[1], (size_t) (m * NH * HD)); @@ -186,6 +204,27 @@ Result check(int fmt, int m, const int64_t* ctx, uint32_t seed) { ok = false; } } + if (ok && m > 8) { + cudaStream_t stream; cudaGraph_t graph; cudaGraphExec_t exec; + ck(cudaStreamCreate(&stream)); ck(cudaStreamBeginCapture(stream, cudaStreamCaptureModeGlobal)); + if (!k::qsa_decode_attn_slots(d_qcur, px, sel[1], d_steps, cap, s, scratch[1], attn[1], m, stream)) { + std::fprintf(stderr, "FAIL: attention declined during graph capture\n"); std::exit(1); + } + ck(cudaStreamEndCapture(stream, &graph)); + const char* flag = std::getenv("STRATA_QSA_ATTN_BATCH64"); + const bool wide = flag && std::strcmp(flag, "1") == 0; + size_t nodes = 0; ck(cudaGraphGetNodes(graph, nullptr, &nodes)); + const size_t expected = wide && fmt != 4 ? 2 : (size_t) (2 * ((m + 7) / 8)); + if (nodes != expected) { std::fprintf(stderr, "FAIL: expected %zu nodes, got %zu\n", expected, nodes); ok = false; } + ck(cudaGraphInstantiate(&exec, graph, nullptr, nullptr, 0)); + for (int i = 0; i < 10; ++i) ck(cudaGraphLaunch(exec, stream)); + ck(cudaStreamSynchronize(stream)); + const auto replay = down(attn[1], (size_t) (m * NH * HD)); + if (std::memcmp(a0.data(), replay.data(), replay.size() * sizeof(float))) { + std::fprintf(stderr, "FAIL: captured attention replay differs\n"); ok = false; + } + cudaGraphExecDestroy(exec); cudaGraphDestroy(graph); cudaStreamDestroy(stream); + } double mag = 0; // the attention output is not trivially zero for (float v : a0) mag += std::fabs(v); if (ok && !(mag > 0)) { std::fprintf(stderr, "FAIL fmt=%d rows=%d: attention output is zero\n", fmt, m); ok = false; } @@ -198,7 +237,15 @@ Result check(int fmt, int m, const int64_t* ctx, uint32_t seed) { } // namespace -int main() { +int main(int argc, char** argv) { + const bool wide = argc == 2 && std::strcmp(argv[1], "--wide64") == 0; + const bool wide_default = argc == 2 && std::strcmp(argv[1], "--wide-default") == 0; + if (argc > 1 && !wide && !wide_default) { std::fprintf(stderr, "usage: qsa_slots_parity [--wide64|--wide-default]\n"); return 2; } +#ifdef _WIN32 + _putenv_s("STRATA_QSA_ATTN_BATCH64", wide ? "1" : "0"); +#else + setenv("STRATA_QSA_ATTN_BATCH64", wide ? "1" : "0", 1); +#endif int devices = 0; if (cudaGetDeviceCount(&devices) != cudaSuccess || devices == 0) { std::puts("SKIP: no CUDA device"); @@ -208,6 +255,25 @@ int main() { std::puts("SKIP: STRATA_SCORES_MULTI=0 (the slots launch is off; the engine keeps the per-row calls)"); return 77; } + if (wide || wide_default) { + std::vector contexts(64, 320); + for (int m : {8, 24, 64}) { + const Result res = check(1, m, contexts.data(), 4242u + (uint32_t)m); + if (res != kPass) return res == kSkip ? 77 : 1; + } + if (wide) for (int fmt : {0, 2, 3}) { + const Result res = check(fmt, 25, contexts.data(), 9000u + (uint32_t)fmt); + if (res != kPass) return res == kSkip ? 77 : 1; + } +#if defined(STRATA_KV_FP8) + if (wide) { + const Result res = check(4, 25, contexts.data(), 9044u); + if (res != kPass) return res == kSkip ? 77 : 1; + } +#endif + std::puts("PASS: wide QSA attention matches per-row and captured replay"); + return 0; + } // contexts per row: under and over the selection width (2048 cells), partial and complete indexer blocks const int64_t ctx[8] = {1500, 37, 9000, 2049, 32768, 300, 20001, 4096}; int cases = 0, skipped = 0; // the gfx906 query swizzle declines only the int8 K/V format diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 7397636882..6cd08c612d 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -12577,6 +12577,39 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_GGUF_512_OPT", + "backends": [ + "cuda" + ], + "reads": [ + { + "file": "engine/src/kernels/cuda/iq_kernels.cu", + "line": 7331, + "backend": "cuda", + "fn": "getenv" + } + ], + "parse": "int", + "default_state": null, + "code_default": "-1", + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "", + "first_date": "", + "since": "", + "last_commit": "", + "last_date": "", + "status": "default", + "owner": "kernels/cuda", + "note": "Mode-8 n_ff=512 GGUF decode fast paths (down RB=32 sub-warp / 32-lane R2 and 512-thread gate/up for kN>=128); bit-exact; on by default on sm_120 (off via STRATA_SM120_PERF=0, STRATA_DEFAULTS=026, or =0), opt-in (=1) on other CUDA cards.", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_GGUF_PLAN_READ", "backends": [ @@ -34832,6 +34865,40 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_PLE_CUBLAS_BF16", + "backends": [ + "cuda" + ], + "reads": [ + { + "file": "engine/src/core/verify.cpp", + "line": 1469, + "backend": "cuda", + "fn": "getenv" + }, + { + "file": "engine/src/core/verify.cpp", + "line": 1995, + "backend": "cuda", + "fn": "getenv" + } + ], + "parse": "string", + "default_state": null, + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "status": "research", + "owner": "core", + "note": "Opt-in BF16 cuBLAS PLE projections; changes activation precision.", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_PLE_GGUF", "backends": [ @@ -41800,6 +41867,45 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_QSA_ATTN_BATCH64", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/core/verify.cpp", + "line": 2874, + "backend": "cuda-hip", + "fn": "env_on" + }, + { + "file": "engine/src/kernels/cuda/qsa_decode_attn.cu", + "line": 1221, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": "off", + "code_default": "0", + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "", + "first_date": "", + "since": "", + "last_commit": "", + "last_date": "", + "status": "opt-in", + "owner": "kernels/cuda", + "note": "Opt-in compact 64-slot QSA decode attention batching; off by default. FP8 pools retain the 8-slot path.", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_QSA_CLUSTER", "backends": [ @@ -43514,6 +43620,40 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_ROUTER_CUBLAS_BF16", + "backends": [ + "cuda" + ], + "reads": [ + { + "file": "engine/src/core/verify.cpp", + "line": 1470, + "backend": "cuda", + "fn": "getenv" + }, + { + "file": "engine/src/core/verify.cpp", + "line": 3050, + "backend": "cuda", + "fn": "getenv" + } + ], + "parse": "int", + "default_state": null, + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "status": "research", + "owner": "core", + "note": "Opt-in BF16 cuBLAS router projection; can change routing decisions.", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_ROUTE_DUMP", "backends": [