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: bytes_needed sizes the MoE buffers for the layout init picks (with or without an expert source)

sergiywitha26fcbcdba7e
diff --git a/include/strata/prefill/prefill.hpp b/include/strata/prefill/prefill.hpp
index c1b0a9118..5be63ac3c 100644
--- a/include/strata/prefill/prefill.hpp
+++ b/include/strata/prefill/prefill.hpp
@@ -88,15 +88,19 @@ public:
/// fits it keeps 0.1.39's ring). 0 slots = none. A layer split's set_ring_override and STRATA_PREFILL_RING win.
static void set_ring_budget(int slots, int64_t small_max);
- /// Device bytes `init` needs for a chunk of `chunk` tokens (what a borrowed region must hold).
- static uint64_t bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk);
+ /// Device bytes `init` needs for a chunk of `chunk` tokens (what a borrowed region must hold). `src`: init gets an
+ /// ExpertSource (carve picks the fused MoE layout only then).
+ static uint64_t bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,
+ bool src = true);
/// 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);
+ static uint64_t bytes_needed_owned(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,
+ bool src = true);
+ static uint64_t bytes_needed_no_ring(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,
+ bool src = true);
/// The streamed ring's byte budget as a slot count for this pack (the measured slot count x Q2_0's blob, over
/// max_blob, never past ring_cap()):
@@ -183,7 +187,7 @@ public:
private:
static uint64_t bytes_needed_impl(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,
- bool owned_pages);
+ bool owned_pages, bool src);
kernels::cpu::ExpertPool* cpu_pool_ = nullptr; ///< set_cpu_pool
// Stage-1 pipeline: intermediate stages return after handing their chunk to
// the direct successor. The public run() drains the chain once at prompt end.
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 4e03dc8b6..89e0b1f06 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -1743,19 +1743,20 @@ void Prefill::set_ring_budget(int slots, int64_t small_max) {
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);
+uint64_t Prefill::bytes_needed(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk, bool src) {
+ return bytes_needed_impl(g, ss, chunk, false, src);
}
// 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_owned(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,
+ bool src) {
+ return bytes_needed_impl(g, ss, chunk, true, src);
}
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
+ bool owned_pages, bool src) {
+ // the same allocation sequence as `init` (`carve`), counted - with the MoE layout `carve` picks for the same source
const size_t T = (size_t) chunk;
bool ok = true;
Alloc o;
@@ -1789,7 +1790,7 @@ uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::Se
const int64_t cap = strata::kernels::qsa_selection_width(strata::kernels::kTopkMaxCells, s);
const int64_t max_blocks = ss.qsa_states[ss.qsa_primary()].max_cells / s.idx_block + 2;
o.take<uint8_t>((size_t) std::max({gdn_set_bytes(T), qsa_set_bytes(T, cap, max_blocks, 256, 32, s),
- moe_set_bytes(T, g.n_expert, fused_layout(T, true))}), ok);
+ moe_set_bytes(T, g.n_expert, fused_layout(T, src))}), ok);
for (int i = 0; i < DQ; ++i) { o.take<uint16_t>(1280 * 2560, ok); o.take<uint16_t>(2560 * 640, ok); }
if (mmq_plan().any) {
const MmqPlan& mp = mmq_plan();
@@ -1810,8 +1811,9 @@ uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::Se
return o.used + (8u << 20); // alignment slack
}
-uint64_t Prefill::bytes_needed_no_ring(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk) {
- return bytes_needed(g, ss, chunk) - (uint64_t) ring_slots((size_t) chunk) * (uint64_t) MAXBLOB();
+uint64_t Prefill::bytes_needed_no_ring(const core::ModelGeometry& g, const core::SessionState& ss, int64_t chunk,
+ bool src) {
+ return bytes_needed(g, ss, chunk, src) - (uint64_t) ring_slots((size_t) chunk) * (uint64_t) MAXBLOB();
}
int64_t Prefill::ring_default_slots() {
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index ff425cfd6..dd88845db 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -4336,7 +4336,7 @@ int main(int argc, char** argv) {
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;
+ const int64_t mib = ((int64_t) strata::prefill::Prefill::bytes_needed_owned(g, ss, o.prefill_chunk, srcp != nullptr) + (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
@@ -5749,7 +5749,7 @@ int main(int argc, char** argv) {
return (uint64_t) k * (uint64_t) strata::kernels::cpu::expert_layout().max_blob;
};
auto lend_slots = [&](int64_t c) -> int64_t {
- return slots_from_bytes(strata::prefill::Prefill::bytes_needed(g, ss, c));
+ return slots_from_bytes(strata::prefill::Prefill::bytes_needed(g, ss, c, srcp != nullptr));
};
// `lend_bytes` went with the single-cache serve loan: a participant's loan is priced by `part_bytes` from its
// OWN cache, and the only other user of the old helper was the serve path's own relayout.
@@ -5854,7 +5854,7 @@ int main(int argc, char** argv) {
const int64_t blob = (int64_t) strata::kernels::cpu::expert_layout().max_blob;
const uint64_t avail = bytes_from_slots(budget);
auto room_of = [&](int64_t t) -> int64_t {
- const uint64_t nonring = strata::prefill::Prefill::bytes_needed_no_ring(g, ss, t);
+ const uint64_t nonring = strata::prefill::Prefill::bytes_needed_no_ring(g, ss, t, srcp != nullptr);
return std::min((int64_t) ((avail - std::min(avail, nonring)) / (uint64_t) blob), ring_max);
};
// `room_of` is capped at ring_max, so "the ring is full" is exactly room == ring_max, and both that
@@ -6286,7 +6286,7 @@ int main(int argc, char** argv) {
return (int64_t) ((need + (uint64_t) blob - 1) / (uint64_t) blob);
};
auto part_slots = [&](const PfPart& p, int64_t c) -> int64_t {
- return cache_slots_for(*p.cache, strata::prefill::Prefill::bytes_needed(g, *p.ses, c));
+ return cache_slots_for(*p.cache, strata::prefill::Prefill::bytes_needed(g, *p.ses, c, srcp != nullptr));
};
auto part_bytes = [&](const PfPart& p, int32_t first) -> uint64_t {
strata::core::ExpertCache& xc = *p.cache;
@@ -6359,7 +6359,7 @@ int main(int argc, char** argv) {
const int64_t budget = std::max<int64_t>(0, std::min(p.cache->slots() - 128,
kAutoLendPct * p.cache->slots() / 100));
const uint64_t avail = part_bytes(p, (int32_t) (p.cache->slots() - budget));
- const uint64_t nonring = strata::prefill::Prefill::bytes_needed_no_ring(g, *p.ses, c);
+ const uint64_t nonring = strata::prefill::Prefill::bytes_needed_no_ring(g, *p.ses, c, srcp != nullptr);
room = std::min(room, (int64_t) ((avail - std::min(avail, nonring)) / (uint64_t) kBlob));
}
return room;
@@ -6472,7 +6472,7 @@ int main(int argc, char** argv) {
const strata::core::OnDevice on(p.dev);
size_t fb = 0, tb = 0;
if (cudaMemGetInfo(&fb, &tb) != cudaSuccess) { (void) cudaGetLastError(); continue; }
- const uint64_t need = strata::prefill::Prefill::bytes_needed(g, *p.ses, chunk);
+ const uint64_t need = strata::prefill::Prefill::bytes_needed(g, *p.ses, chunk, srcp != nullptr);
if ((uint64_t) fb >= need + (3ull << 29)) {
std::fprintf(stderr, "strata serve: CUDA%d keeps its own prompt buffers (%.2f GiB of "
"%.2f GiB free): no loan\n", p.dev < 0 ? 0 : p.dev,
@@ -6562,7 +6562,7 @@ int main(int argc, char** argv) {
const strata::core::OnDevice on(dev);
size_t fb = 0, tb = 0;
cudaMemGetInfo(&fb, &tb);
- const int64_t need = (int64_t) strata::prefill::Prefill::bytes_needed(g, i == 0 ? ss : stages[i - 1]->ss, c);
+ const int64_t need = (int64_t) strata::prefill::Prefill::bytes_needed(g, i == 0 ? ss : stages[i - 1]->ss, c, srcp != nullptr);
if (need + kHeadroom > (int64_t) fb) {
dev_out = dev < 0 ? 0 : dev; need_out = need; free_out = (int64_t) fb;
return false;