Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

prefill: owned buffers take the ring in one allocation, exact owned pricing (STRATA_OWNED_PRICE=exact), OOM diagnostics (#796 part A)

Niko12214a9b9041c270
diff --git a/include/strata/prefill/prefill.hpp b/include/strata/prefill/prefill.hpp
index cd5477cb1..037385a65 100644
--- a/include/strata/prefill/prefill.hpp
+++ b/include/strata/prefill/prefill.hpp
@@ -90,6 +90,9 @@ public:
/// The same without the streamed ring: what the chunk's own buffers cost. The auto chunk scan sizes the chunk
/// first and hands the ring what the chunk leaves over, so it needs the chunk priced on its own.
+ /// What `init` allocates when the prompt path OWNS its buffers: each cudaMalloc rounded up to a 2 MiB page and the
+ /// ring as one allocation (the startup sizing of a cache without a loan).
+ static uint64_t bytes_needed_owned(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk);
static uint64_t bytes_needed_no_ring(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk);
/// The streamed ring's byte budget as a slot count for this pack (the measured slot count x Q2_0's blob, over
@@ -160,6 +163,8 @@ public:
bool set_stage_helper(Prefill* helper, std::string& err);
private:
+ static uint64_t bytes_needed_impl(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,
+ bool owned_pages);
// Stage-1 pipeline: intermediate stages return after handing their chunk to
// the direct successor. The public run() drains the chain once at prompt end.
bool run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::string& err);
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 518b2b69e..7b53ac9a4 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -282,19 +282,25 @@ double ms_since(Clock::time_point t) { return std::chrono::duration<double, std:
struct Alloc {
uint8_t* base = nullptr;
uint64_t cap = 0, used = 0;
+ uint64_t failed_bytes = 0; ///< the take that could not be allocated; the failure message names it
+ uint64_t granule = 0; ///< count_only: each take rounds up to this (cudaMalloc's 2 MiB pages: owned buffers)
bool count_only = false;
std::vector<void*>* owned = nullptr;
template <typename T> T* take(size_t n, bool& ok) {
- const uint64_t bytes = ((uint64_t) n * sizeof(T) + 256 + 255) & ~255ull;
- if (count_only) { used += bytes; return nullptr; }
+ uint64_t bytes = ((uint64_t) n * sizeof(T) + 256 + 255) & ~255ull;
+ if (count_only) {
+ if (granule > 0) bytes = (bytes + granule - 1) / granule * granule;
+ used += bytes;
+ return nullptr;
+ }
if (base != nullptr) {
- if (used + bytes > cap) { ok = false; return nullptr; }
+ if (used + bytes > cap) { ok = false; failed_bytes = bytes; return nullptr; }
T* p = (T*) (base + used);
used += bytes;
return p;
}
void* p = nullptr;
- if (cudaMalloc(&p, bytes) != cudaSuccess) { ok = false; return nullptr; }
+ if (cudaMalloc(&p, bytes) != cudaSuccess) { ok = false; failed_bytes = bytes; return nullptr; }
owned->push_back(p);
used += bytes;
return (T*) p;
@@ -918,7 +924,13 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
set_act_f16(m.f16_io);
}
if (!carve(T, &o)) {
- err = "prefill: device buffers for a chunk of " + std::to_string(chunk) + " tokens do not fit";
+ size_t fb = 0, tb = 0;
+ cudaMemGetInfo(&fb, &tb);
+ (void) cudaGetLastError();
+ err = "prefill: device buffers for a chunk of " + std::to_string(chunk) + " tokens do not fit (" +
+ std::to_string(fb >> 20) + " of " + std::to_string(tb >> 20) + " MiB free at the failure; " +
+ std::to_string(o.used >> 20) + " MiB taken, the failing buffer wanted " +
+ std::to_string(o.failed_bytes >> 20) + " MiB)";
return false;
}
return true;
@@ -999,10 +1011,22 @@ bool Prefill::carve(size_t T, void* alloc) {
if (!m.mmq_ctx) m.mmq_ctx = std::make_unique<mmq::Context>();
}
m.ring = ring_slots(T);
- for (int i = 0; i < m.ring; ++i) {
- m.stage_dev[i] = o.take<uint8_t>((size_t) MAXBLOB(), ok);
- m.stage_live[i] = false; // a new buffer: nothing of an earlier layout to wait for
- m.used_of[i] = i;
+ if (o.base == nullptr && m.ring > 0) {
+ // OWNED buffers: the ring in ONE allocation. 384 separate 2.7 MiB cudaMallocs each round up to a 2 MiB page
+ // (~1.3 MiB a slot, ~0.5 GiB in all) that no count ever saw. A borrowed region keeps its per-slot layout (and
+ // so its price, `bytes_needed`: the loans' slot counts do not move).
+ uint8_t* ring_base = o.take<uint8_t>((size_t) m.ring * (size_t) MAXBLOB(), ok);
+ for (int i = 0; ok && i < m.ring; ++i) {
+ m.stage_dev[i] = ring_base + (size_t) i * (size_t) MAXBLOB();
+ m.stage_live[i] = false;
+ m.used_of[i] = i;
+ }
+ } else {
+ for (int i = 0; i < m.ring; ++i) {
+ m.stage_dev[i] = o.take<uint8_t>((size_t) MAXBLOB(), ok);
+ m.stage_live[i] = false; // a new buffer: nothing of an earlier layout to wait for
+ m.used_of[i] = i;
+ }
}
m.ple_emb = o.take<float>(T * N, ok);
m.ple_norm = o.take<float>((size_t) strata::kernels::NG_HC_DIM, ok);
@@ -1031,7 +1055,13 @@ bool Prefill::relayout(int64_t chunk, void* borrow, uint64_t borrow_bytes, std::
void* ws = o.take<uint8_t>(GEMM_WS, ok);
if (ok) m.gemm.rebind(gs, GEMM_SCRATCH, ws, GEMM_WS);
if (!ok || !carve((size_t) chunk, &o)) {
- err = "prefill: device buffers for a chunk of " + std::to_string(chunk) + " tokens do not fit";
+ size_t fb = 0, tb = 0;
+ cudaMemGetInfo(&fb, &tb);
+ (void) cudaGetLastError();
+ err = "prefill: device buffers for a chunk of " + std::to_string(chunk) + " tokens do not fit (" +
+ std::to_string(fb >> 20) + " of " + std::to_string(tb >> 20) + " MiB free at the failure; " +
+ std::to_string(o.used >> 20) + " MiB taken, the failing buffer wanted " +
+ std::to_string(o.failed_bytes >> 20) + " MiB)";
return false;
}
return true;
@@ -1444,11 +1474,23 @@ double Prefill::pinned_share() { return g_pinned_share; }
int64_t Prefill::stream_all_min_tokens() { return stream_all_min(); }
uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk) {
+ return bytes_needed_impl(g, ss, chunk, false);
+}
+
+// What `init` really allocates when the prompt path owns its buffers (no loan): every cudaMalloc rounds up to a 2 MiB
+// page, and the ring is one allocation (carve). `bytes_needed` stays the borrowed region's sum.
+uint64_t Prefill::bytes_needed_owned(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk) {
+ return bytes_needed_impl(g, ss, chunk, true);
+}
+
+uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,
+ bool owned_pages) {
// the same allocation sequence as `init`, counted
const size_t T = (size_t) chunk;
bool ok = true;
Alloc o;
o.count_only = true;
+ if (owned_pages) o.granule = 2ull << 20;
o.take<uint16_t>((size_t) GEMM_SCRATCH, ok);
o.take<uint8_t>(GEMM_WS, ok);
auto f = [&](size_t n) { o.take<float>(n, ok); };
@@ -1485,7 +1527,11 @@ uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::Session
o.take<uint8_t>(MMQ_GROUP * mp.gu_max + MMQ_TAIL, ok);
o.take<uint8_t>(MMQ_GROUP * mp.d_max + MMQ_TAIL, ok);
}
- for (int i = 0; i < ring_slots(T); ++i) o.take<uint8_t>((size_t) MAXBLOB(), ok);
+ if (owned_pages) {
+ if (ring_slots(T) > 0) o.take<uint8_t>((size_t) ring_slots(T) * (size_t) MAXBLOB(), ok); // one allocation
+ } else {
+ for (int i = 0; i < ring_slots(T); ++i) o.take<uint8_t>((size_t) MAXBLOB(), ok);
+ }
f(T * N);
f((size_t) strata::kernels::NG_HC_DIM);
strata::kernels::KvHostPools stage;
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 16495a8f7..acb0b39ea 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -3751,6 +3751,19 @@ int main(int argc, char** argv) {
std::fprintf(stderr, "strata generate: --pipeline-windows %d: %lld MiB of CUDA0 kept out of the expert cache "
"(the second verifier%s)\n", o.pipeline_windows, (long long) (pipe_first >> 20),
o.pipeline_windows >= 2 ? ", the GDN snapshots" : "");
+ // The MiB the prompt path's OWN buffers (no loan) take, which the cache sizing leaves out. 0.1.39's rule is
+ // 160 + chunk * 680 / 1024 (~16 GiB at 24576 against ~2.4 GiB really allocated); STRATA_OWNED_PRICE=exact prices the
+ // real allocation instead (Prefill::bytes_needed_owned: 2 MiB pages, the ring in one piece, as PR #796 measured)
+ // and gives that cache the difference. A recommendation to try, so it is not the default.
+ auto owned_prefill_mib = [&]() -> int64_t {
+ if (!(o.prefill_chunk > 0 && !pf_borrow)) return 0;
+ static const bool exact = [] { const char* v = std::getenv("STRATA_OWNED_PRICE"); return v != nullptr && std::string(v) == "exact"; }();
+ if (!exact) return 160 + (o.prefill_chunk * 680) / 1024;
+ const int64_t mib = ((int64_t) strata::prefill::Prefill::bytes_needed_owned(g, ss, o.prefill_chunk) + (1 << 20) - 1) >> 20;
+ std::fprintf(stderr, "strata generate: STRATA_OWNED_PRICE=exact: the prompt path's own buffers for a %lld-token chunk: %lld MiB (the 0.1.39 rule: %lld MiB)\n",
+ (long long) o.prefill_chunk, (long long) mib, (long long) (160 + (o.prefill_chunk * 680) / 1024));
+ return mib + 64; // a margin for the allocator
+ };
if (o.expert_cache < 0) {
size_t free_b = 0, total_b = 0;
free_b = strata::core::device_free_bytes(); (void) total_b;
@@ -3758,7 +3771,7 @@ int main(int argc, char** argv) {
// under WDDM an over-subscribed allocation does not fail, it pages to system memory and crawls.
// (with borrowing - the default with a profile - the prompt path lends cache slots instead; `pf_borrow` is
// the predicate a local `borrow` was here, hoisted above so both cache-size branches read the same one)
- const int64_t prefill_mib = (o.prefill_chunk > 0 && !pf_borrow) ? 160 + (o.prefill_chunk * 680) / 1024 : 0;
+ const int64_t prefill_mib = owned_prefill_mib();
// the draft layer's head and logits are allocated when it binds, after this: 0.1.27's CJK subset made them
// ~110-180 MiB larger, and out of the reserve they left 16 GB cards below the stall line (#199)
int64_t mtp_bind = (!o.mtp.empty() && native_head.loaded())
@@ -3829,7 +3842,7 @@ int main(int argc, char** argv) {
// slots instead and `prefill_mib` is 0, so only the reserve is checked)
size_t free_b = 0, total_b = 0;
free_b = strata::core::device_free_bytes(); (void) total_b;
- const int64_t prefill_mib = (o.prefill_chunk > 0 && !pf_borrow) ? 160 + (o.prefill_chunk * 680) / 1024 : 0;
+ const int64_t prefill_mib = owned_prefill_mib();
const int64_t reserve = (((int64_t) o.vram_reserve_mib + prefill_mib) << 20) + pipe_first;
const int64_t fit = std::max<int64_t>(((int64_t) free_b - reserve) / (int64_t) strata::kernels::cpu::expert_layout().max_blob, 0);
if (o.expert_cache > fit) {