Merge branch 'f-132-amd' into rel-0.1.32

This commit is contained in:
Niko1221 committed 2026-10-01 15:09:45 +02:00
commit aebfd69a2f
11 files changed
+846 -22

No files matched your search

+204 -19
View File
@@ -1272,6 +1272,7 @@ int main(int argc, char** argv) {
}
}
bool multi_gpu = !split_devs.empty() && !split_same; // cleared by --split-skip-if-fits before any stage loads
bool split_own_auto = false; // #340: the split keeps own prompt buffers by its rule (not --no-prefill-borrow)
if (o.mmap_experts && !o.shared_expert_arena.empty()) {
std::fprintf(stderr, "strata generate: --shared-expert-arena backs the resident arena and cannot be used with --mmap-experts\n");
return 2;
@@ -1303,6 +1304,51 @@ int main(int argc, char** argv) {
// more than either 8 GB card had - which is how adding two GPUs to the two 12 GB ones lost 260K.
// The loan is the tail of the stage's own cache (see `PfPart` in the serve block); outside the prompt
// that tail is expert cache, so a large chunk costs a stage nothing permanent.
// #340 - BUT A LOAN IS NOT FREE PER REQUEST: every stage streams the lent experts during the prompt and
// copies them back after it, so on cards that hold (nearly) every expert of their layers a 2K prompt read
// 37% slower than 0.1.29's own buffers (2x RX 9070 XT / R9700: 1570 -> 990 tok/s; 2x A5000 in #340: -35%).
// So a split keeps 0.1.29's own buffers (2048-token chunks, priced into the split search) when they are a
// small part of every card - at most 12% of its VRAM (16 GB and larger cards) - and borrows on smaller cards,
// where the reserve would cost the cache (and the context) the paragraph above is about. Measured with own
// buffers on the 9070 XT + R9700: 2K 1588 tok/s, 16K 2006 (0.1.29 1568 / 1935, 0.1.31 993 / 1852).
// OPT-IN (the owner's choice for 0.1.32): own buffers change which experts a full card keeps resident, so the
// split's output differs from 0.1.31's; the default borrows as 0.1.31 did - with the concurrent refill and the
// 96-blob split ring that alone gave 2K +27%, 16K +5% over 0.1.31, decode unchanged, output identical.
// STRATA_SPLIT_OWN=auto: the 12% rule above; 1: own buffers on any cards; unset/0: borrow.
// --split-skip-if-fits keeps the loans (it can fall back to one GPU, which must stay as it is).
const char* own_env = std::getenv("STRATA_SPLIT_OWN");
if (!o.no_prefill_borrow && !o.split_skip_if_fits && own_env != nullptr && own_env[0] != '0') {
const char* v = std::string(own_env) == "auto" ? nullptr : own_env;
bool own = v ? v[0] == '1' : true;
// the buffers of a 2048-token chunk (or the --prefill one) + a 96-blob ring (as split_pf_mib prices them)
const int64_t own_chunk = o.prefill_auto || o.prefill_chunk <= 0 ? 2048 : o.prefill_chunk;
const int64_t reserve_mib = 160 + (own_chunk * 680) / 1024 + 96 * 4;
std::string why;
if (!v) {
std::vector<int> devs = {0};
for (const int d : split_devs) devs.push_back(d);
for (const int d : devs) {
cudaDeviceProp prop{};
if (cudaGetDeviceProperties(&prop, d) != cudaSuccess) { cudaGetLastError(); own = false; break; }
const int64_t total_mib = (int64_t) (prop.totalGlobalMem >> 20);
if (reserve_mib * 100 > 12 * total_mib) {
own = false;
why = "CUDA" + std::to_string(d) + " has " + std::to_string(total_mib) + " MiB";
break;
}
}
}
if (own) {
o.no_prefill_borrow = true;
split_own_auto = true;
std::fprintf(stderr, "strata generate: layer split: every stage keeps its own prompt buffers (~%lld MiB "
"each, %lld-token chunks)%s\n", (long long) reserve_mib, (long long) own_chunk,
v ? " (STRATA_SPLIT_OWN=1)" : "");
} else if (!why.empty()) {
std::fprintf(stderr, "strata generate: layer split: the prompt paths borrow from the caches (%s)\n",
why.c_str());
}
}
int n_vis = 1;
if (cudaGetDeviceCount(&n_vis) != cudaSuccess || n_vis < 1) n_vis = 1;
cudaGetLastError();
@@ -2149,7 +2195,16 @@ int main(int argc, char** argv) {
// for buffers that cannot be priced exactly yet because the sessions do not exist. `plan_lend` uses the
// exact `Prefill::bytes_needed` as soon as it can.
const bool pf_borrow = !o.no_prefill_borrow && !o.expert_profile.empty();
const int64_t split_pf_mib = (o.prefill_chunk > 0 && !pf_borrow) ? 160 + (o.prefill_chunk * 680) / 1024 : 0;
// (#340: the estimate predates the streamed ring: from 1024-token chunks the prompt path also holds a ring of
// whole expert blobs, which a split without borrowing sizes at 96 (Prefill::set_ring_override below) and books
// here - without it a `--no-prefill-borrow` split filled the cards and the draft head no longer fit)
const int64_t split_ring_mib =
(multi_gpu && !pf_borrow && o.prefill_chunk >= 1024)
? (int64_t) ((96ull * (uint64_t) strata::kernels::cpu::expert_layout().max_blob + (1ull << 20) - 1) >> 20)
: 0;
if (split_ring_mib > 0) strata::prefill::Prefill::set_ring_override(96);
const int64_t split_pf_mib =
(o.prefill_chunk > 0 && !pf_borrow) ? 160 + (o.prefill_chunk * 680) / 1024 + split_ring_mib : 0;
// ---- WHAT A STAGE RESERVES, AND ON WHICH STAGE. The flat 1 GiB this used to withhold from EVERY stage
// after the first was booked "for its windows and the drafter", but the windows measure 75 MiB ("window up
// to 6 tokens, 74.1 MiB of device buffers", on every boot) and the drafter is loaded on ONE stage - the
@@ -2159,13 +2214,23 @@ int main(int argc, char** argv) {
// the windows and - only on the stage that carries them - the drafter and the head.
const int64_t kWindowMib = 96; // the verify windows; 75 MiB measured, rounded up
const int64_t kDrafterMib = 1000; // the MTP drafter (839 MiB) + the head, on the last stage only
auto stage_room = [&](int dev, bool later, bool drafter) -> int64_t {
// #340: with the own prompt buffers chosen by the split's rule (not asked for with --no-prefill-borrow) the
// boundary is searched as the borrowing configuration would (no reserve): the reserve then only makes the caches
// smaller, which measured cost no decode (K=28 on 9070 XT + R9700: 58.4 tok/s own vs 58.5 borrowing), while a
// search with the reserve moved the boundary to K=32 and decode to 54.8. STRATA_SPLIT_OWN_PLACE=reserve: the
// search sees the reserve.
static const bool place_with_reserve = [] {
const char* v = std::getenv("STRATA_SPLIT_OWN_PLACE");
return v != nullptr && std::string(v) == "reserve";
}();
auto stage_room = [&](int dev, bool later, bool drafter, bool search = false) -> int64_t {
const strata::core::OnDevice on(dev);
size_t fb = 0, tb = 0;
if (const cudaError_t e = cudaMemGetInfo(&fb, &tb); e != cudaSuccess)
std::fprintf(stderr, "strata generate: layer split: CUDA%d free memory: %s\n", dev < 0 ? 0 : dev,
cudaGetErrorString(e));
const int64_t reserve = ((int64_t) o.vram_reserve_mib + split_pf_mib + (later ? kWindowMib : 0) +
const int64_t pf = search && split_own_auto && !place_with_reserve ? 0 : split_pf_mib;
const int64_t reserve = ((int64_t) o.vram_reserve_mib + pf + (later ? kWindowMib : 0) +
(drafter ? kDrafterMib : 0)) << 20;
return std::max<int64_t>((int64_t) fb - reserve, 0);
};
@@ -2176,7 +2241,7 @@ int main(int argc, char** argv) {
std::vector<double> layer_ms((size_t) ns);
for (int i = 0; i < ns; ++i) {
const int dev = i == 0 ? 0 : stages[(size_t) i - 1]->dev;
cap[(size_t) i] = stage_room(i == 0 ? -1 : dev, i > 0, i + 1 == ns);
cap[(size_t) i] = stage_room(i == 0 ? -1 : dev, i > 0, i + 1 == ns, true);
int sms = 0, khz = 0;
cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, dev);
if (cudaDeviceGetAttribute(&khz, cudaDevAttrClockRate, dev) != cudaSuccess || khz <= 0) khz = 1800000;
@@ -3662,6 +3727,22 @@ int main(int argc, char** argv) {
: (uint64_t) (xc.slots() - first) *
(uint64_t) strata::kernels::cpu::expert_layout().max_blob;
};
// #340: a layer split whose caches already hold most experts streams few of them through the prompt path, so
// the 384-slot ring (sized for a card that streams nearly every expert of a chunk) only makes every stage's
// loan bigger: 96 slots (0.1.30's ring here) when >= 75% of the (layer, expert) pairs are resident. One GPU
// keeps the pinned-share rule. STRATA_SPLIT_RING=N: N slots on a split; 0: the pinned-share rule.
if (multi_gpu && !host_res.empty()) {
int64_t res_n = 0;
for (const int32_t r : host_res) res_n += r >= 0;
const double res_share = (double) res_n / (double) host_res.size();
const char* v = std::getenv("STRATA_SPLIT_RING");
const int ring = v ? std::atoi(v) : (res_share >= 0.75 ? 96 : 0);
if (ring > 0) {
strata::prefill::Prefill::set_ring_override(ring);
std::fprintf(stderr, "strata serve: layer split: %.0f%% of the experts resident, the prompt path's "
"streamed ring %d slots\n", 100.0 * res_share, ring);
}
}
std::vector<PfPart> pf_parts;
// a cache too small to lend the prompt path its buffers would make it allocate them on top - on a card
// whose cache already filled its reserve, that is the over-subscription the auto sizing avoids - so the
@@ -3702,9 +3783,34 @@ int main(int argc, char** argv) {
p.first = (int32_t) (p.cache->slots() - part_slots(p, chunk));
p.first_now = p.first;
}
// #340: a split stage whose card still has room for the chunk's buffers (its cache already holds
// every expert of its layers, so auto stopped short of its VRAM) keeps them as its own instead of
// borrowing cache slots: nothing of it is lent, streamed during the prompt or refilled after it.
// Room = the buffers + 1.5 GiB (the verify windows, the draft head, hipBLAS/cuBLAS workspaces made
// after this). Layer splits only - one GPU keeps its loan exactly as before.
// STRATA_SPLIT_OWN_BUFFERS=0: every stage borrows (0.1.30/0.1.31).
static const bool own_ok = [] {
const char* v = std::getenv("STRATA_SPLIT_OWN_BUFFERS");
return v == nullptr || v[0] != '0';
}();
if (pf_parts.size() > 1 && own_ok) {
for (PfPart& p : pf_parts) {
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);
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,
(double) need / 1073741824.0, (double) fb / 1073741824.0);
p.first = -1;
p.first_now = -1;
}
}
}
lend_first = pf_parts[0].first;
borrow = xcache.device_slot(lend_first);
borrow_bytes = part_bytes(pf_parts[0], lend_first);
borrow = lend_first >= 0 ? xcache.device_slot(lend_first) : nullptr;
borrow_bytes = lend_first >= 0 ? part_bytes(pf_parts[0], lend_first) : 0;
} else if (o.prefill_auto) {
o.prefill_chunk = 1024; // nothing lendable: small buffers of its own
} else if (pf_parts.size() > 1) {
@@ -3723,11 +3829,14 @@ int main(int argc, char** argv) {
} else if (o.prefill_auto && d_res == nullptr) {
o.prefill_chunk = 1024; // #85: no expert cache at all (a full 8 GB card): small buffers of its own
}
if (borrow != nullptr) {
std::fprintf(stderr, "strata serve: the prompt path borrows %lld CUDA0 cache slots (%.2f GiB)\n",
(long long) (xcache.slots() - lend_first), (double) borrow_bytes / 1073741824.0);
bool any_loan = borrow != nullptr;
for (size_t i = 1; i < pf_parts.size(); ++i) any_loan = any_loan || pf_parts[i].first >= 0;
if (any_loan) {
if (borrow != nullptr)
std::fprintf(stderr, "strata serve: the prompt path borrows %lld CUDA0 cache slots (%.2f GiB)\n",
(long long) (xcache.slots() - lend_first), (double) borrow_bytes / 1073741824.0);
for (size_t i = 1; i < pf_parts.size(); ++i) // one loan per stage, from that stage's own cache
std::fprintf(stderr, "strata serve: CUDA%d prompt path borrows %lld of its %lld slots (%.2f GiB)\n",
if (pf_parts[i].first >= 0) std::fprintf(stderr, "strata serve: CUDA%d prompt path borrows %lld of its %lld slots (%.2f GiB)\n",
pf_parts[i].dev, (long long) (pf_parts[i].cache->slots() - pf_parts[i].first),
(long long) pf_parts[i].cache->slots(),
(double) part_bytes(pf_parts[i], pf_parts[i].first) / 1073741824.0);
@@ -3819,15 +3928,15 @@ int main(int argc, char** argv) {
sp.reset();
(void) cudaGetLastError();
o.prefill_chunk = next;
if (borrow != nullptr) { // smaller loans for the smaller chunk
if (any_loan) { // smaller loans for the smaller chunk
for (PfPart& p : pf_parts)
if (p.first >= 0) {
p.first = (int32_t) (p.cache->slots() - part_slots(p, next));
p.first_now = p.first;
}
lend_first = pf_parts[0].first;
borrow = xcache.device_slot(lend_first);
borrow_bytes = part_bytes(pf_parts[0], lend_first);
borrow = lend_first >= 0 ? xcache.device_slot(lend_first) : nullptr;
borrow_bytes = lend_first >= 0 ? part_bytes(pf_parts[0], lend_first) : 0;
}
err.clear();
continue;
@@ -3840,6 +3949,43 @@ int main(int argc, char** argv) {
return 1;
}
mem_mark("the head and the prompt path");
// #340: STRATA_SPLIT_SMALL_OWN=S (tokens): on a layer split, every stage that borrows keeps the slots for an
// S-token chunk's buffers for the whole session (0.1.29's own buffers, carved from the tail of its cache):
// a request of at most S prompt tokens then lends, streams and refills nothing, a longer one lends (and
// refills) only the slots above them. Their experts stay non-resident (the CPU / PCIe path computes them).
// STRATA_SPLIT_SMALL_MAX=M: a request of at most M prompt tokens reads in S-token chunks on those stages
// (several chunks, still nothing lent) instead of borrowing for a bigger one.
int64_t split_small = 0, split_small_max = 0;
if (multi_gpu && !pf_parts.empty() && !host_res.empty()) {
const char* v = std::getenv("STRATA_SPLIT_SMALL_OWN");
const int64_t S = v ? request_chunk(std::atoll(v), o.prefill_chunk) : 0;
const char* vm = std::getenv("STRATA_SPLIT_SMALL_MAX");
split_small = S;
split_small_max = S > 0 ? std::max<int64_t>(S, vm ? std::atoll(vm) : S) : 0;
int64_t evicted = 0;
for (PfPart& p : pf_parts) {
if (S <= 0 || p.first < 0) continue;
const int32_t first_s = std::max<int32_t>(p.first, (int32_t) (p.cache->slots() - part_slots(p, S)));
int64_t n = 0;
for (int64_t l = p.lb; l < p.le; ++l)
for (int64_t ex = 0; ex < g.n_expert; ++ex) {
int32_t& r = host_res[(size_t) (l * g.n_expert + ex)];
if (r >= first_s) { r = strata::core::kNotResident; ++n; }
}
evicted += n;
std::fprintf(stderr, "strata serve: CUDA%d keeps %lld slots for %lld-token prompts (%lld experts "
"no longer resident)\n", p.dev < 0 ? 0 : p.dev,
(long long) (p.cache->slots() - first_s), (long long) S, (long long) n);
}
if (evicted > 0) {
if (d_res != nullptr)
cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice);
for (auto& st : stages) {
const strata::core::OnDevice on(st->dev);
cudaMemcpy(st->d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice);
}
}
}
// the penalty-history buffer: one row per verify-window row (`penalty_rows`), each the last
// `penalty_last_n` tokens that row's pick follows, -1 padded in front. Allocated once at the cap for
// the widest window; a request without penalties gets a null buffer and takes the byte-for-byte
@@ -4790,8 +4936,8 @@ int main(int argc, char** argv) {
// the arena, the residency table is restored, and one upload puts it on every device. A stage refills
// through its own cache and its own device - a slot refilled into the wrong cache would leave that
// stage's cache holding an expert it does not own, which is silent and produces plausible tokens.
auto refill_one = [&](PfPart& p, std::string& e) -> bool {
if (p.lent.empty()) return true;
// (split in two halves so a layer split can queue every stage's copies before it waits for any: #340)
auto refill_issue = [&](PfPart& p, std::string& e) -> bool {
tr("refill start", (long long) p.lent.size());
const strata::core::OnDevice on(p.dev);
for (const auto& [i, slot] : p.lent) { // D-4: queued, one wait (STRATA_REFILL_BLOCKING=1: each)
@@ -4802,16 +4948,44 @@ int main(int argc, char** argv) {
return false;
host_res[(size_t) i] = slot;
}
return true;
};
auto refill_wait = [&](PfPart& p, std::string& e) -> bool {
const strata::core::OnDevice on(p.dev);
if (!p.cache->sync_queued(e)) return false;
p.lent.clear();
p.lent_chunk = 0;
return true;
};
auto refill_one = [&](PfPart& p, std::string& e) -> bool {
if (p.lent.empty()) return true;
return refill_issue(p, e) && refill_wait(p, e);
};
// #340: with several stages every stage's copies go out first (each card on its own link), then each is
// waited for - the same copies into the same slots, so the same cache; one stage: refill_one exactly.
// STRATA_REFILL_SERIAL=1: stage after stage, as 0.1.30/0.1.31.
static const bool refill_serial = std::getenv("STRATA_REFILL_SERIAL") != nullptr;
auto refill = [&](std::string& e) -> bool {
bool any = false;
const auto t_rf = Clock::now();
int64_t n_lent = 0, n_parts = 0;
for (PfPart& p : pf_parts)
if (!p.lent.empty()) { any = true; if (!refill_one(p, e)) return false; }
if (any) res_upload();
if (!p.lent.empty()) { n_lent += (int64_t) p.lent.size(); ++n_parts; }
if (n_parts > 1 && !refill_serial) {
for (PfPart& p : pf_parts)
if (!p.lent.empty() && !refill_issue(p, e)) return false;
for (PfPart& p : pf_parts)
if (!p.lent.empty() && !refill_wait(p, e)) return false;
} else {
for (PfPart& p : pf_parts)
if (!p.lent.empty() && !refill_one(p, e)) return false;
}
if (n_parts > 0) res_upload();
if (trace && n_parts > 0) {
std::fprintf(stderr, "strata trace: refilled %lld slots on %lld stage(s) in %.1f ms\n",
(long long) n_lent, (long long) n_parts,
std::chrono::duration<double, std::milli>(Clock::now() - t_rf).count());
std::fflush(stderr);
}
return true;
};
// lend the slots `tokens` batched prompt tokens need: the prompt path's buffers for min(chunk, tokens
@@ -4819,9 +4993,13 @@ int main(int argc, char** argv) {
// participant's own cache, and marking only that participant's own layers
auto lend = [&](int64_t tokens, std::string& e) -> bool {
if (pf_parts.empty()) return true; // its own buffers: nothing to lend
const auto t_ln = Clock::now();
// what this segment needs, capped by the configured chunk: a request lends only what its own
// prompt needs, so a large chunk costs a short prompt nothing
const int64_t want = request_chunk(tokens, o.prefill_chunk);
const int64_t want_full = request_chunk(tokens, o.prefill_chunk);
// #340: a short enough request reads in the stages' own S-token chunks (nothing lent)
const int64_t want = split_small > 0 && tokens <= split_small_max ? std::min(want_full, split_small)
: want_full;
if (want <= 0) {
e = "prefill: cannot lend buffers for an empty request segment";
return false;
@@ -4854,6 +5032,13 @@ int main(int argc, char** argv) {
p.lent_chunk = want;
}
if (any) res_upload();
if (trace) {
int64_t n_lent = 0;
for (const PfPart& p : pf_parts) n_lent += (int64_t) p.lent.size();
std::fprintf(stderr, "strata trace: lent %lld slots for %lld tokens in %.1f ms\n", (long long) n_lent,
(long long) want, std::chrono::duration<double, std::milli>(Clock::now() - t_ln).count());
std::fflush(stderr);
}
return true;
};
apply_pending(true);