CPU prefill share: skip the default when host memory is short, and never fail a chat on its buffers

0.1.41 report (RTX 5090, tight RAM): chats failed and the engine restarted until STRATA_PREFILL_CPU_SHARE=0.
- share_rules: default_share_memory_verdict. The default is not armed when the RAM this process can get
  (cgroup-aware) is below the share's buffers + 3 GiB, when the Windows commit left is below buffers + 4 GiB,
  or when the experts it would read are file pages the free RAM cannot keep. One log line says why and that
  STRATA_PREFILL_CPU_SHARE=auto turns it on anyway. An explicit setting is kept and only warns.
- prefill: the share's two pinned buffers are taken once at their largest (an expert takes at most MAXT
  tokens' rows); a failed allocation ends the share for the run instead of failing the request.
- unit test, docs/DETAILS.md.
This commit is contained in:
Niko1221 committed 2026-10-09 15:10:32 +02:00
1 parent 65d97b0ab6
commit 04dbe56145
5 files changed
+147 -8

No files matched your search

+34 -6
View File
@@ -746,6 +746,7 @@ struct Prefill::Impl {
// writes (Dm's tail, in Dm's row order), both pinned; and the pool's per-token activations and jobs.
float *cpu_x = nullptr, *cpu_rows = nullptr;
size_t cpu_x_n = 0, cpu_rows_n = 0;
bool cpu_dead = false; // a pinned buffer of the share could not be had (host memory is short): the GPU does it all
std::vector<uint8_t> cpu_nact;
std::vector<kernels::cpu::ActQ> cpu_actq; // a Q2_0 layer's activations (the pool's Q2_0 kernels read ActQ)
std::vector<kernels::cpu::ExpertJobMulti> cpu_jobs;
@@ -3005,8 +3006,18 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
int32_t* src_h = grp_mapped ? m.grp_host + 2 * m.grp_tk : m.src_host.data();
if (grp_mapped) copy_i32(m.grp_dev, m.ids, T * K, m.cs);
else cudaMemcpyAsync(m.ids_host.data(), m.ids, (size_t) T * K * 4, cudaMemcpyDeviceToHost, m.cs);
const bool cpu_maybe = pool_hold.held && !stream_all && m.src != nullptr &&
lay.native && !m.pp && !lay.fmt.empty();
bool cpu_maybe = pool_hold.held && !stream_all && m.src != nullptr && !m.cpu_dead &&
lay.native && !m.pp && !lay.fmt.empty();
// The share's two pinned buffers are small (~50 MB) but are had mid-request, when the host may have
// nothing left (0.1.41 report: tight RAM, chats failing). A buffer that cannot be had ends the share
// for this engine - the GPU takes every expert, as with STRATA_PREFILL_CPU_SHARE=0 - instead of
// failing the request.
auto cpu_share_off = [&](const char* what) {
m.cpu_dead = true;
cpu_maybe = false;
std::fprintf(stderr, "prefill: the CPU share is off for this run: the host could not give %s "
"(RAM is short); the GPU streams every expert\n", what);
};
// auto shares only while the layers that share cost less per non-resident expert than their
// neighbours that do not. #1282 (RX 7900 GRE + 5700X3D): every share lost, auto's 0.47 by
// 4.5%, the host's issue time per streamed expert rising 107 -> 200 us with the CPU busy -
@@ -3034,12 +3045,14 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
m.cpu_x = nullptr;
m.cpu_x_n = 0;
if (cudaHostAlloc((void**) &m.cpu_x, want * sizeof(float), cudaHostAllocDefault) != cudaSuccess) {
err = "prefill: cannot allocate the CPU experts' activations";
return false;
(void) cudaGetLastError();
m.cpu_x = nullptr;
cpu_share_off("the activation buffer");
} else {
m.cpu_x_n = want;
}
m.cpu_x_n = want;
}
cudaMemcpyAsync(m.cpu_x, m.mixed, want * sizeof(float), cudaMemcpyDeviceToHost, m.cs);
if (cpu_maybe) cudaMemcpyAsync(m.cpu_x, m.mixed, want * sizeof(float), cudaMemcpyDeviceToHost, m.cs);
}
// #579: a stall here is the GPU (this layer's attention and router, or the previous layer's
// work), not the host: the watchdog's report says so (only its text changes)
@@ -3047,6 +3060,21 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
l, p0);
cudaStreamSynchronize(m.cs);
core::progress_at("reading the prompt (batched): layer", l, p0);
if (cpu_maybe) { // the rows buffer, once, at its largest (an expert takes at most MAXT tokens' rows)
const size_t cap = (size_t) std::min<int64_t>(T * K, (int64_t) strata::kernels::cpu::MAXT * m.g->n_expert) * N;
if (m.cpu_rows_n < cap) {
if (m.cpu_rows) cudaFreeHost(m.cpu_rows);
m.cpu_rows = nullptr;
m.cpu_rows_n = 0;
if (cudaHostAlloc((void**) &m.cpu_rows, cap * sizeof(float), cudaHostAllocDefault) != cudaSuccess) {
(void) cudaGetLastError();
m.cpu_rows = nullptr;
cpu_share_off("the rows buffer");
} else {
m.cpu_rows_n = cap;
}
}
}
if (m.cpu_pend) { // the measured share: the last CPU-sharing layer's GPU time is final now
m.cpu_pend = false;
float g_ms = 0;