From 2d47caa44ae837d5830967e1c5bb39295326d6fc Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 12 Oct 2026 00:45:07 +0200 Subject: [PATCH] STRATA_HC_Q8: the prompt path's scratch is ordered by an event across streams (batch serving), the session read has its own pair --- include/strata/core/native_dense.hpp | 6 +++- src/core/layer.cpp | 12 ++++---- src/core/native_dense.cpp | 44 ++++++++++++++++++++-------- src/prefill/prefill.cpp | 8 +++-- 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/include/strata/core/native_dense.hpp b/include/strata/core/native_dense.hpp index 4a261068..5e3907b4 100644 --- a/include/strata/core/native_dense.hpp +++ b/include/strata/core/native_dense.hpp @@ -47,7 +47,11 @@ public: /// A projection's bf16 values for a path that has no Q8 kernel (the prompt path's GEMMs, the single-token read): the /// resident copy, or - when the row was skipped - the Q8_0 copy dequantized into a per-stream scratch (`slot` 0 / 1: /// two matrices alive at once). nullptr if there is neither. - static const uint16_t* hc_bf16_view(const WeightRef& w, void* stream, int slot); + /// `cls` 0 = the prompt path (a scratch pair shared by its streams, ordered by events: `hc_bf16_release` after the + /// last GEMM that reads the slot), 1 = the layer / session read (its own pair: it is captured into graphs, where an + /// event wait on an outside event is illegal). The scratches are allocated by `load`, never on first use. + static const uint16_t* hc_bf16_view(const WeightRef& w, void* stream, int slot, int cls); + static void hc_bf16_release(void* stream, int slot, int cls); uint64_t weight_bytes() const { return bytes_; } /// What the device lost to `load`: each matrix rounded up to the granule `cudaMalloc` backs it with. uint64_t allocated_bytes() const { return allocated_; } diff --git a/src/core/layer.cpp b/src/core/layer.cpp index c6dc2080..45f19c08 100644 --- a/src/core/layer.cpp +++ b/src/core/layer.cpp @@ -1409,12 +1409,12 @@ st_begin(layer, 0, stream); if (fused) { strata::kernels::FusedGrArgs fa; fa.R = R; fa.R_out = R; fa.apply = pending_ffn; fa.bo_prev = bb.block_out; fa.inj_prev = bb.inject2; - fa.w_norm = (const float*) w_norm[0]->data; fa.w_down = NativeDense::hc_bf16_view(*w_down[0], stream, 0); - fa.w_up = NativeDense::hc_bf16_view(*w_up[0], stream, 1); fa.w_inject = (const uint16_t*) w_inject[0]->data; + fa.w_norm = (const float*) w_norm[0]->data; fa.w_down = NativeDense::hc_bf16_view(*w_down[0], stream, 0, 1); + fa.w_up = NativeDense::hc_bf16_view(*w_up[0], stream, 1, 1); fa.w_inject = (const uint16_t*) w_inject[0]->data; fa.eps = RMS_EPS; fa.lo = bb.gr.lo; fa.rs = bb.gr_rs; fa.inject_out = bb.inject; fa.mixed = bb.mixed; strata::kernels::fused_gr_read(fa, stream); } else { - gr_read(R, (const float*) w_norm[0]->data, NativeDense::hc_bf16_view(*w_down[0], stream, 0), NativeDense::hc_bf16_view(*w_up[0], stream, 1), (const uint16_t*) w_inject[0]->data, RMS_EPS, gs, bb.gr, bb.mixed, bb.inject, stream); + gr_read(R, (const float*) w_norm[0]->data, NativeDense::hc_bf16_view(*w_down[0], stream, 0, 1), NativeDense::hc_bf16_view(*w_up[0], stream, 1, 1), (const uint16_t*) w_inject[0]->data, RMS_EPS, gs, bb.gr, bb.mixed, bb.inject, stream); } st_end(layer, 0, stream); dump_half(bb, g, layer, bb.inject, 2 * g.n_embd, g.hc, stream); } if (run1) { @@ -1432,12 +1432,12 @@ st_begin(layer, 3, stream); if (fused) { strata::kernels::FusedGrArgs fa; fa.R = R; fa.R_out = R; fa.apply = true; fa.bo_prev = bb.block_out; fa.inj_prev = bb.inject; - fa.w_norm = (const float*) w_norm[1]->data; fa.w_down = NativeDense::hc_bf16_view(*w_down[1], stream, 0); - fa.w_up = NativeDense::hc_bf16_view(*w_up[1], stream, 1); fa.w_inject = (const uint16_t*) w_inject[1]->data; + fa.w_norm = (const float*) w_norm[1]->data; fa.w_down = NativeDense::hc_bf16_view(*w_down[1], stream, 0, 1); + fa.w_up = NativeDense::hc_bf16_view(*w_up[1], stream, 1, 1); fa.w_inject = (const uint16_t*) w_inject[1]->data; fa.eps = RMS_EPS; fa.lo = bb.gr.lo; fa.rs = bb.gr_rs; fa.inject_out = bb.inject2; fa.mixed = bb.mixed; strata::kernels::fused_gr_read(fa, stream); } else { - gr_read(R, (const float*) w_norm[1]->data, NativeDense::hc_bf16_view(*w_down[1], stream, 0), NativeDense::hc_bf16_view(*w_up[1], stream, 1), (const uint16_t*) w_inject[1]->data, RMS_EPS, gs, bb.gr, bb.mixed, bb.inject, stream); + gr_read(R, (const float*) w_norm[1]->data, NativeDense::hc_bf16_view(*w_down[1], stream, 0, 1), NativeDense::hc_bf16_view(*w_up[1], stream, 1, 1), (const uint16_t*) w_inject[1]->data, RMS_EPS, gs, bb.gr, bb.mixed, bb.inject, stream); } st_end(layer, 3, stream); } if (run4) { diff --git a/src/core/native_dense.cpp b/src/core/native_dense.cpp index ef56c06e..b1b8ebf4 100644 --- a/src/core/native_dense.cpp +++ b/src/core/native_dense.cpp @@ -212,12 +212,15 @@ bool NativeDense::keep_unquantized_ple_key(const std::string& pack_dir, std::set // STRATA_HC_Q8 without a bf16 copy: the two dequantization scratches (one bf16 matrix each) of `hc_bf16_view`, per device, // allocated by `load` - never on first use, so a graph capture cannot meet the allocation -static void* g_hc_scratch[64][2] = {}; +static void* g_hc_scratch[64][2][2] = {}; // [device][class][slot] +static cudaEvent_t g_hc_ev[64][2] = {}; // [device][slot]: the prompt path's last reader of the slot +static bool g_hc_ev_set[64][2] = {}; NativeDense::~NativeDense() { for (auto& dev : g_hc_scratch) - for (void*& b : dev) - if (b != nullptr && std::find(weights_.begin(), weights_.end(), b) != weights_.end()) b = nullptr; + for (auto& cl : dev) + for (void*& b : cl) + if (b != nullptr && std::find(weights_.begin(), weights_.end(), b) != weights_.end()) b = nullptr; for (const void* p : packed_keys_) strata::kernels::native_q8_0_packed_unregister(p); if (scratch_) cudaFree(scratch_); for (void* p : weights_) cudaFree(p); @@ -425,13 +428,19 @@ bool NativeDense::load(const std::vector& shards, WeightTable& tabl for (const auto& [name, ref] : table.table_) if (ref.hc_q8 != nullptr && ref.data == nullptr) elems = (std::max)(elems, (uint64_t) ref.elements); int dev = 0; - if (elems != 0 && cudaGetDevice(&dev) == cudaSuccess && dev >= 0 && dev < 64 && g_hc_scratch[dev][0] == nullptr) - for (int i = 0; i < 2; ++i) { - void* b = nullptr; - if (cudaMalloc(&b, (size_t) elems * 2) != cudaSuccess) { err = "native dense (STRATA_HC_Q8): scratch alloc"; return false; } - weights_.push_back(b); - g_hc_scratch[dev][i] = b; - } + if (elems != 0 && cudaGetDevice(&dev) == cudaSuccess && dev >= 0 && dev < 64 && g_hc_scratch[dev][0][0] == nullptr) { + for (int c = 0; c < 2; ++c) + for (int i = 0; i < 2; ++i) { + void* b = nullptr; + if (cudaMalloc(&b, (size_t) elems * 2) != cudaSuccess) { err = "native dense (STRATA_HC_Q8): scratch alloc"; return false; } + weights_.push_back(b); + g_hc_scratch[dev][c][i] = b; + } + for (int i = 0; i < 2; ++i) + if (g_hc_ev[dev][i] == nullptr && cudaEventCreateWithFlags(&g_hc_ev[dev][i], cudaEventDisableTiming) != cudaSuccess) { + err = "native dense (STRATA_HC_Q8): scratch event"; return false; + } + } } if (pending.empty()) { err = "native dense: no supported GDN/QSA matrices in supplied shards"; return false; } void* allocation = nullptr; @@ -486,16 +495,25 @@ bool NativeDense::add_hc_skip(const std::string& pack_dir, std::set return true; } -const uint16_t* NativeDense::hc_bf16_view(const WeightRef& w, void* stream, int slot) { +const uint16_t* NativeDense::hc_bf16_view(const WeightRef& w, void* stream, int slot, int cls) { if (w.data != nullptr) return (const uint16_t*) w.data; if (w.hc_q8 == nullptr) return nullptr; // the device's scratch (allocated at load, one pair per device): the prompt path and the single-token read run one // after the other on a device, and each use is stream-ordered after the previous GEMM that read the slot int dev = 0; - if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64 || slot < 0 || slot > 1) return nullptr; - void* buf = g_hc_scratch[dev][slot]; + if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64 || slot < 0 || slot > 1 || cls < 0 || cls > 1) return nullptr; + void* buf = g_hc_scratch[dev][cls][slot]; if (buf == nullptr) return nullptr; + // the prompt path: the scratch may still be read by a GEMM another stream enqueued (batch serving: several slots' prompts) + if (cls == 0 && g_hc_ev_set[dev][slot]) cudaStreamWaitEvent((cudaStream_t) stream, g_hc_ev[dev][slot], 0); strata::kernels::fused_gr_dequant_q8_0((const uint8_t*) w.hc_q8, (uint16_t*) buf, w.elements / 32, stream); return (const uint16_t*) buf; } + +void NativeDense::hc_bf16_release(void* stream, int slot, int cls) { + int dev = 0; + if (cls != 0 || slot < 0 || slot > 1 || cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64 || g_hc_ev[dev][slot] == nullptr) return; + cudaEventRecord(g_hc_ev[dev][slot], (cudaStream_t) stream); + g_hc_ev_set[dev][slot] = true; +} } // namespace strata::core diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index 91c8141b..d13fdd19 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -2570,8 +2570,8 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri core::WeightRef wd_q8, wu_q8; if (!wd->data && wd->hc_q8 && !wu->data && wu->hc_q8) { wd_q8 = *wd; wu_q8 = *wu; - wd_q8.data = core::NativeDense::hc_bf16_view(*wd, m.cs, 0); - wu_q8.data = core::NativeDense::hc_bf16_view(*wu, m.cs, 1); + wd_q8.data = core::NativeDense::hc_bf16_view(*wd, m.cs, 0, 0); + wu_q8.data = core::NativeDense::hc_bf16_view(*wu, m.cs, 1, 0); if (!wd_q8.data || !wu_q8.data) { err = "prefill: the Q8_0 hyper-connection projections could not be dequantized"; return false; } wd = &wd_q8; wu = &wu_q8; } @@ -2636,6 +2636,10 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri m.mixed_bf_lo); } + if (wd_q8.data) { // the HC GEMMs that read the scratch are enqueued: the next user waits for them + core::NativeDense::hc_bf16_release(m.cs, 0, 0); + core::NativeDense::hc_bf16_release(m.cs, 1, 0); + } if (half == 0 && !core::is_qsa_layer(g, l)) { // ======================= GDN ======================= const core::WeightRef *wqkv = need(v, "attn_qkv.weight", err), *wg = need(v, "attn_gate.weight", err),