Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions engine/cmake/strata_gpu_engine.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions engine/include/strata/core/verify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#include <string>
#include <vector>

#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
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions engine/include/strata/kernels/bf16_gemv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 5 additions & 7 deletions engine/include/strata/kernels/qsa_decode_attn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 58 additions & 14 deletions engine/src/core/verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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_})
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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_,
Expand Down Expand Up @@ -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)
Expand Down
Loading