diff --git a/src/core/native_dense.cpp b/src/core/native_dense.cpp index 1eaee7a1..ef56c06e 100644 --- a/src/core/native_dense.cpp +++ b/src/core/native_dense.cpp @@ -210,7 +210,14 @@ bool NativeDense::keep_unquantized_ple_key(const std::string& pack_dir, std::set return true; } +// 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] = {}; + 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 (const void* p : packed_keys_) strata::kernels::native_q8_0_packed_unregister(p); if (scratch_) cudaFree(scratch_); for (void* p : weights_) cudaFree(p); @@ -413,6 +420,19 @@ bool NativeDense::load(const std::vector& shards, WeightTable& tabl err = "native dense (STRATA_HC_Q8): quantizing the projections failed"; return false; } } + if (hc_q8_env()) { // the scratches of hc_bf16_view (rows without a bf16 copy), here and not on first use + uint64_t elems = 0; + 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 (pending.empty()) { err = "native dense: no supported GDN/QSA matrices in supplied shards"; return false; } void* allocation = nullptr; const auto status = cudaMalloc(&allocation, strata::kernels::native_q8_1_bytes(max_in)); @@ -469,15 +489,12 @@ bool NativeDense::add_hc_skip(const std::string& pack_dir, std::set const uint16_t* NativeDense::hc_bf16_view(const WeightRef& w, void* stream, int slot) { if (w.data != nullptr) return (const uint16_t*) w.data; if (w.hc_q8 == nullptr) return nullptr; - static std::mutex mu; - static std::map, void*> scratch; // per stream and slot: one matrix of bf16 (6.5 MB) - void* buf = nullptr; - { - std::lock_guard lock(mu); - void*& b = scratch[{stream, slot}]; - if (b == nullptr && cudaMalloc(&b, (size_t) w.elements * 2) != cudaSuccess) { cudaGetLastError(); b = nullptr; return nullptr; } - buf = b; - } + // 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 (buf == nullptr) return nullptr; 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; }