Merge f-140 into the Aurora port; verify: halo's STRATA_VERIFY_RESIDENT is 0.1.39's all-resident graph

0.1.39 already skips the host round trip when every expert is in VRAM (all_resident_, on by default, #871's
refresh_ar), so the port keeps that graph and adds only what halo built on top of it: the LFUSE gated combine.
This commit is contained in:
Niko1221 committed 2026-10-05 11:08:25 +02:00
commit 1fb37cf810
17 files changed
+459 -147

No files matched your search

+111 -41
View File
@@ -210,13 +210,23 @@ using Clock = std::chrono::steady_clock;
// reads the file. Swaps that need no exchange (`out` in the lend region is held in RAM already; or `in` is not) go
// on as before; ones beyond the buffers' room wait for a later round. Runs on the adaptive tier's thread while the
// GPU commits and drafts: the copies back are on its stream, and waited for before the refills are queued.
template <class Swap>
bool resident_stage_swaps(strata::core::FileExpertSource& src, strata::core::ExpertCache& cache,
const std::vector<int32_t>& host_res, int64_t n_expert, std::vector<Swap>& swaps,
cudaStream_t stream) {
//
// With a layer split, `host_res` holds slot numbers of WHICHEVER cache owns the layer, so the copy back must read the
// owning card's cache on that card's stream: `locate(layer)` names them (cache, stream, device; -1 = CUDA0). Reading
// a later stage's slot number out of CUDA0's cache copied another expert's bytes into RAM - the resident mode with a
// split then computed some experts from the wrong weights after the first adaptive swaps (garbage output).
struct SwapHome {
strata::core::ExpertCache* cache;
cudaStream_t stream;
int dev;
};
template <class Swap, class Locate>
bool resident_stage_swaps(strata::core::FileExpertSource& src, const std::vector<int32_t>& host_res,
int64_t n_expert, std::vector<Swap>& swaps, Locate locate) {
if (!src.complement_ready() || swaps.empty()) return true;
struct Staged { int32_t layer, in, out; int64_t q; };
std::vector<Staged> staged;
std::vector<SwapHome> used;
std::vector<Swap> kept;
kept.reserve(swaps.size());
for (const Swap& s : swaps) {
@@ -225,17 +235,35 @@ bool resident_stage_swaps(strata::core::FileExpertSource& src, strata::core::Exp
if (q >= src.exchange_capacity()) continue;
const int32_t slot = host_res[(size_t) s.layer * (size_t) n_expert + (size_t) s.out];
if (slot < 0) continue;
if (cudaMemcpyAsync(src.exchange_buffer(q), cache.device_slot(slot),
const SwapHome home = locate(s.layer);
const strata::core::OnDevice on(home.dev);
if (const cudaError_t e = cudaMemcpyAsync(src.exchange_buffer(q), home.cache->device_slot(slot),
(size_t) strata::kernels::cpu::expert_layout().blob_bytes(s.layer), cudaMemcpyDeviceToHost,
stream) != cudaSuccess)
home.stream); e != cudaSuccess) {
std::fprintf(stderr, "strata: copy back of layer %d slot %d (device %d) failed: %s\n", (int) s.layer,
(int) slot, home.dev, cudaGetErrorString(e));
return false;
}
bool seen = false;
for (const SwapHome& u : used) seen = seen || u.stream == home.stream;
if (!seen) used.push_back(home);
staged.push_back({s.layer, s.in, s.out, q});
kept.push_back(s);
}
if (!staged.empty()) {
if (cudaStreamSynchronize(stream) != cudaSuccess) return false;
for (const SwapHome& u : used) {
const strata::core::OnDevice on(u.dev);
if (const cudaError_t e = cudaStreamSynchronize(u.stream); e != cudaSuccess) {
std::fprintf(stderr, "strata: copy back sync (device %d) failed: %s\n", u.dev, cudaGetErrorString(e));
return false;
}
}
for (const Staged& x : staged)
if (!src.stage_exchange(x.layer, x.in, x.out, x.q)) return false;
if (!src.stage_exchange(x.layer, x.in, x.out, x.q)) {
std::fprintf(stderr, "strata: stage_exchange refused layer %d in %d out %d\n", (int) x.layer,
(int) x.in, (int) x.out);
return false;
}
}
swaps.swap(kept);
return true;
@@ -428,6 +456,9 @@ struct Options {
/// `VRAM <reserve_mib>` command can give part of it back to other programs and take it again. Off: one cudaMalloc.
bool vram_elastic = false;
int64_t vram_segment_mib = 512;
/// `--vram-reserve-later-mib N`: the reserve on a layer split's later cards (default: the same as the first).
/// A card that drives no display needs less than the one the monitors are on.
int vram_reserve_later_mib = -1;
/// Plan v0.3 P5: batched prompt processing in chunks of this many tokens (0 = the token path).
int64_t prefill_chunk = 0;
/// `--prefill auto`: the largest chunk (up to 8192) whose buffers the expert cache can lend. Every expert a chunk
@@ -1413,6 +1444,8 @@ int main(int argc, char** argv) {
else if (a == "--expert-cache-remote-placement")
o.expert_cache_remote_placement = next("--expert-cache-remote-placement");
else if (a == "--vram-reserve-mib") { o.vram_reserve_mib = std::atoi(next("--vram-reserve-mib")); o.vram_reserve_given = true; }
else if (a == "--vram-reserve-later-mib")
o.vram_reserve_later_mib = std::atoi(next("--vram-reserve-later-mib"));
else if (a == "--prefill") {
const std::string v = next("--prefill");
o.prefill_auto = v == "auto" || v.rfind("auto:", 0) == 0;
@@ -1662,19 +1695,10 @@ int main(int argc, char** argv) {
}
const bool remote_caches = o.expert_cache_remote[0] > 0 || o.expert_cache_remote[1] > 0 ||
o.expert_cache_remote[2] > 0;
if (o.resident_cpu_experts && !o.layer_split.empty() && !remote_caches && o.resident_soft &&
!o.resident_cpu_explicit && o.resident_budget == 0) {
// #364 #384: setup's --resident-experts with a layer split (--gpus at start, or a config edited by hand) runs
// as the plain mmap mode - the placement those users measured 1.3-1.6x faster than one GPU - instead of
// refusing. Exactly --mmap-experts: nothing else reads these flags (the headroom only sizes the copy).
std::fprintf(stderr, "strata generate: WARNING: the resident RAM mode (--resident-experts) does not support a "
"layer split yet: the experts the GPUs do not hold are read through the OS file cache "
"(--mmap-experts), and RAM may fill up during long prompts\n");
o.resident_cpu_experts = o.resident_pin = o.resident_soft = false;
o.resident_headroom = 8ull << 30;
}
if (o.resident_cpu_experts && (!o.layer_split.empty() || remote_caches)) {
std::fprintf(stderr, "strata generate: --resident-cpu-experts does not support layer splits or remote expert caches\n");
// A layer split keeps the resident RAM mode: every stage's GPU cache is left out of the RAM copy, and an adaptive
// swap copies an evicted expert back from the card that owns its layer (resident_stage_swaps).
if (o.resident_cpu_experts && remote_caches) {
std::fprintf(stderr, "strata generate: --resident-cpu-experts does not support remote expert caches\n");
return 2;
}
// the helper-GPU expert caches (--expert-cache-remote, docs/SECOND_GPU.md): CUDA1..3 on one GPU; with a layer
@@ -2803,7 +2827,9 @@ int main(int argc, char** argv) {
std::fprintf(stderr, "strata generate: layer split: CUDA%d free memory: %s\n", dev < 0 ? 0 : dev,
cudaGetErrorString(e));
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) +
const int64_t base_reserve =
later && o.vram_reserve_later_mib >= 0 ? o.vram_reserve_later_mib : o.vram_reserve_mib;
const int64_t reserve = (base_reserve + pf + (later ? kWindowMib : 0) +
(drafter ? kDrafterMib : 0)) << 20;
return std::max<int64_t>((int64_t) fb - reserve, 0);
};
@@ -2910,7 +2936,12 @@ int main(int argc, char** argv) {
while (st < (int) split_at.size() && l >= split_at[(size_t) st]) ++st;
return st;
};
// the whole ranking, hottest first, before each stage takes its own layers' share: the resident RAM copy ranks
// the experts no GPU holds over ALL the layers (with CUDA0's share alone, a copy that did not fit kept only layers
// 0..K-1's experts and read every later layer's from the files)
std::vector<std::pair<int32_t, int32_t>> profile_all;
if (multi_gpu) {
profile_all = profile;
std::vector<std::pair<int32_t, int32_t>> mine;
for (const auto& pr : profile) {
const int st = stage_of(pr.first);
@@ -4324,6 +4355,13 @@ int main(int argc, char** argv) {
// Plan v0.3 P4: with a PROFILE-filled cache the residency is static, so the hit decision moves onto the
// device and the token graph keeps it. (A cache filled on demand still needs the per-layer host path.)
std::vector<int32_t> host_res;
// The residency table is read by kernels on non-blocking streams, which do not wait for the legacy stream a plain
// cudaMemcpy runs on, and a pageable copy can return before its DMA has landed: each upload waits for its own copy
// (the current device's legacy stream, stream 0 - nothing else; HIP has no cudaStreamLegacy name, #550).
auto res_put = [&host_res](int32_t* dst) -> cudaError_t {
const cudaError_t e = cudaMemcpy(dst, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice);
return e != cudaSuccess ? e : cudaStreamSynchronize(nullptr);
};
int32_t* d_res = nullptr;
int32_t* d_hit_count = nullptr;
strata::core::TokenHits thits;
@@ -4340,7 +4378,7 @@ int main(int argc, char** argv) {
}
if (cudaMalloc((void**) &d_res, host_res.size() * sizeof(int32_t)) != cudaSuccess ||
cudaMalloc((void**) &d_hit_count, sizeof(int32_t)) != cudaSuccess ||
cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice) != cudaSuccess) {
res_put(d_res) != cudaSuccess) {
std::fprintf(stderr, "strata generate: the device residency table could not be staged\n");
return 1;
}
@@ -4374,7 +4412,7 @@ int main(int argc, char** argv) {
for (auto& st : stages) { // layer split across GPUs: the same table on every device
const strata::core::OnDevice on(st->dev);
if (cudaMalloc((void**) &st->d_res, host_res.size() * sizeof(int32_t)) != cudaSuccess ||
cudaMemcpy(st->d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice) !=
res_put(st->d_res) !=
cudaSuccess) {
std::fprintf(stderr, "strata generate: layer split: CUDA%d residency table failed\n", st->dev);
return 1;
@@ -4607,16 +4645,24 @@ int main(int argc, char** argv) {
const int64_t k = plan_lend(chunk);
if (k > 0) lend_from = xcache.slots() - k;
}
bool resident_ok = src.pin_cache_complement(xcache, err, o.resident_pin, {}, lend_from, o.resident_headroom,
o.resident_budget, &profile);
// a layer split: the experts every later stage's cache holds are left out of the RAM copy, as CUDA0's are
// (with them the copy keeps no lend region: pin_cache_complement turns the loan off)
std::vector<std::pair<int32_t, int32_t>> stage_pairs;
for (auto& st : stages)
for (int64_t l = st->lb; l < st->le; ++l)
for (int64_t e = 0; e < g.n_expert; ++e)
if (st->cache.slot_of(l, e) >= 0) stage_pairs.emplace_back((int32_t) l, (int32_t) e);
const std::vector<std::pair<int32_t, int32_t>>& rank_all = profile_all.empty() ? profile : profile_all;
bool resident_ok = src.pin_cache_complement(xcache, err, o.resident_pin, stage_pairs, lend_from,
o.resident_headroom, o.resident_budget, &rank_all);
std::string whole_err;
if (!resident_ok && o.resident_soft) {
// #467: the whole complement does not fit - keep what does, the hottest by the profile, through the #403
// budget path (sized by the RAM alone) instead of none: the misses outside it read the same file bytes
// the mmap fallback reads, so the answers are unchanged. Nothing pinned: the old fallback below.
whole_err = err;
resident_ok = src.pin_cache_complement(xcache, err, o.resident_pin, {}, -1, o.resident_headroom,
strata::core::FileExpertSource::kResidentWhatFits, &profile);
resident_ok = src.pin_cache_complement(xcache, err, o.resident_pin, stage_pairs, -1, o.resident_headroom,
strata::core::FileExpertSource::kResidentWhatFits, &rank_all);
if (resident_ok)
std::fprintf(stderr, "strata generate: WARNING: the whole resident RAM mode does not fit (%s); %.2f "
"GiB of the experts the GPU does not hold, the hottest by the expert profile, are "
@@ -5100,10 +5146,10 @@ int main(int argc, char** argv) {
}
if (evicted > 0) {
if (d_res != nullptr)
cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice);
res_put(d_res);
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);
res_put(st->d_res);
}
}
}
@@ -5248,7 +5294,7 @@ int main(int argc, char** argv) {
(size_t) o.conversation_cache_slots);
// Save only on a switch/rewind, not on each continuing request. No graph
// addresses change: all parked images live in ordinary host vectors.
auto park_current = [&](size_t held) -> bool {
auto park_current_body = [&](size_t held) -> bool {
if (!conversations.enabled() || !live_ok || live.empty()) return true;
// #342: before make_room evicts oldest-first, the copies of this conversation a turn back go (they hold
// nothing the outgoing chain does not, apart from the tail this conversation rewrote)
@@ -5264,7 +5310,7 @@ int main(int argc, char** argv) {
const strata::core::QsaState* draft0 = n_st > 0 ? nullptr : &mtp.kv_state();
auto draft_of = [&](size_t k) -> const strata::core::QsaState* { return k + 1 == n_st ? &mtp.kv_state() : nullptr; };
strata::core::ConversationCheckpointSplit cs;
if (n_st > 0) cs = strata::core::conversation_checkpoints_split(std::move(checks), n_st);
if (n_st > 0) cs = strata::core::conversation_checkpoints_split(std::move(checks), n_st); // #752: two-phase; a bad_alloc leaves `checks` whole
struct MergeBack {
strata::core::ConversationCheckpointSplit& cs; std::vector<ConvCheckpoint>& checks; bool on;
~MergeBack() { if (on && !strata::core::conversation_checkpoints_merge(std::move(cs), checks)) checks.clear(); }
@@ -5368,6 +5414,15 @@ int main(int argc, char** argv) {
}
return true;
};
// #752: the whole park (the checkpoint split included) is under one try, so a host OOM skips parking instead of
// ending the server
auto park_current = [&](size_t held) -> bool {
try { return park_current_body(held); }
catch (const std::bad_alloc&) {
std::fprintf(stderr, "strata serve: conversation cache: allocation failed; skip parking\n");
return true;
}
};
int64_t pp_total = 0, pp_from = 0, pp_next_check = 0;
// #471: the position the prompt pass has read up to (a chunk's or a window's end): what a request cancelled
// mid-read reports as read, instead of the whole prompt
@@ -5524,10 +5579,10 @@ int main(int argc, char** argv) {
// a layer split's later stages keep a copy of the residency table on their devices, and swap on their own
auto res_upload = [&]() {
if (d_res != nullptr)
cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice);
res_put(d_res);
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);
res_put(st->d_res);
}
};
auto apply_pending = [&](bool wait) {
@@ -5578,7 +5633,16 @@ int main(int argc, char** argv) {
}
std::sort(swaps.begin(), swaps.end(), [](const Swap& a, const Swap& b) { return a.gain > b.gain; });
if ((int) swaps.size() > o.adapt_swaps) swaps.resize((size_t) o.adapt_swaps);
if (!resident_stage_swaps(src, xcache, host_res, g.n_expert, swaps, adapt_stream)) return false;
// the copies back read the cache of the card that owns each layer (see resident_stage_swaps)
auto home_of = [&](int64_t layer) -> SwapHome {
const int stn = multi_gpu ? stage_of(layer) : 0;
if (stn > 0) {
GpuStage* gs = stages[(size_t) stn - 1].get();
return {&gs->cache, gs->adapt_stream, gs->dev};
}
return {&xcache, adapt_stream, -1};
};
if (!resident_stage_swaps(src, host_res, g.n_expert, swaps, home_of)) return false;
if (pin_blobs_on()) { // lock the batch's source pages (a file-backed arena on AMD; see pin_blobs)
std::vector<std::pair<uintptr_t, uintptr_t>> spans;
for (const Swap& s : swaps)
@@ -7308,8 +7372,12 @@ int main(int argc, char** argv) {
(d1.actq - ds0.actq) / w, (d1.jobs - ds0.jobs) / w, (d1.run - ds0.run) / w,
(d1.host - ds0.host) / w, dt_commit / w, dt_draft / w, (d1.misses - ds0.misses) / (w * L),
(d1.entries - ds0.entries) / (w * L), (d1.hits - ds0.hits) / (w * L), (d1.pcie - ds0.pcie) / (w * L));
const std::string pr = ver.profile_report();
if (!pr.empty()) std::fprintf(stderr, "strata decode GPU stages (ms/window):%s\n", pr.c_str());
for (int st = 0; st < n_stages; ++st) { // every stage's GPU profile, not only the first card's
const std::string pr = stage_ver(st).profile_report();
if (pr.empty()) continue;
if (st == 0) std::fprintf(stderr, "strata decode GPU stages (ms/window):%s\n", pr.c_str()); // text as before
else std::fprintf(stderr, "strata decode GPU stages, stage %d (ms/window):%s\n", st, pr.c_str());
}
}
if (!cancelled) {
// a prompt stopped halfway leaves the session somewhere between two chunks: nothing to continue from
@@ -7620,7 +7688,7 @@ int main(int argc, char** argv) {
lent.emplace_back((int32_t) i, host_res[i]);
host_res[i] = strata::core::kNotResident;
}
cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice);
res_put(d_res);
borrow = xcache.device_slot(first);
borrow_bytes = xcache.slot_offsets() ? (uint64_t) (xcache.bytes() - (int64_t) xcache.slot_offsets()[first])
: (uint64_t) k * (uint64_t) blob;
@@ -7671,7 +7739,7 @@ int main(int argc, char** argv) {
std::fprintf(stderr, "strata generate: refilling the lent slots failed: %s\n", err.c_str());
return 1;
}
cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice);
res_put(d_res);
std::fprintf(stderr, "strata generate: %zu lent slots refilled in %.1f ms\n", lent.size(),
std::chrono::duration<double, std::milli>(Clock::now() - tr).count());
}
@@ -8006,7 +8074,7 @@ int main(int argc, char** argv) {
}
pending.clear();
if (d_res != nullptr)
cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice);
res_put(d_res);
};
// Plan v0.3 P6: the VRAM tier follows the conversation. Candidates are missing experts routed at least
// twice (decayed); each is paired with its layer's least-routed resident expert and swapped when it was
@@ -8049,7 +8117,9 @@ int main(int argc, char** argv) {
}
std::sort(swaps.begin(), swaps.end(), [](const Swap& a, const Swap& b) { return a.gain > b.gain; });
if ((int) swaps.size() > o.adapt_swaps) swaps.resize((size_t) o.adapt_swaps);
if (!resident_stage_swaps(src, xcache, host_res, g.n_expert, swaps, adapt_stream)) {
// this (non-serve) path refills CUDA0's cache only, so its copies back read CUDA0's cache too
if (!resident_stage_swaps(src, host_res, g.n_expert, swaps,
[&](int64_t) -> SwapHome { return {&xcache, adapt_stream, -1}; })) {
std::fprintf(stderr, "strata generate: an adaptive refill failed (copying evicted experts back)\n");
return false;
}