diff --git a/include/strata/core/vmm.hpp b/include/strata/core/vmm.hpp index ffd85e7c..0c9f816b 100644 --- a/include/strata/core/vmm.hpp +++ b/include/strata/core/vmm.hpp @@ -44,6 +44,9 @@ public: private: bool map_one(int64_t i, VmmChunk h); bool set_access(int64_t lo, int64_t hi); + /// Readable and writable [lo, hi) (all newly mapped); when that fails they are unmapped and freed again, so no + /// chunk counts as mapped without access. + bool commit_run(int64_t lo, int64_t hi); unsigned long long base_ = 0; std::vector h_; // per chunk: its physical chunk, 0 = unmapped }; @@ -54,7 +57,7 @@ template bool VmmRange::map_range(int64_t lo, int64_t hi, Take take int64_t run = -1; // access is set once per run of newly mapped chunks (it costs as much as the map) for (int64_t i = lo; i < hi; ++i) { if (mapped(i)) { - if (run >= 0 && !set_access(run, i)) return false; + if (run >= 0 && !commit_run(run, i)) return false; run = -1; continue; } @@ -62,12 +65,12 @@ template bool VmmRange::map_range(int64_t lo, int64_t hi, Take take if (h == 0) h = vmm_chunk_new(); if (h == 0 || !map_one(i, h)) { if (h != 0) vmm_chunk_free(h); - if (run >= 0) set_access(run, i); + if (run >= 0) commit_run(run, i); return false; } if (run < 0) run = i; } - return run < 0 || set_access(run, hi); + return run < 0 || commit_run(run, hi); } } // namespace strata::core diff --git a/src/core/vmm.cpp b/src/core/vmm.cpp index 24a71d70..d66f17b5 100644 --- a/src/core/vmm.cpp +++ b/src/core/vmm.cpp @@ -128,6 +128,12 @@ bool VmmRange::set_access(int64_t lo, int64_t hi) { CUDA_SUCCESS; } +bool VmmRange::commit_run(int64_t lo, int64_t hi) { + if (set_access(lo, hi)) return true; + for (int64_t c = lo; c < hi; ++c) vmm_chunk_free(unmap(c)); + return false; +} + VmmChunk VmmRange::unmap(int64_t i) { if (!mapped(i)) return 0; const Api& a = api(); @@ -151,6 +157,7 @@ void VmmRange::release() {} int64_t VmmRange::mapped_count() const { return 0; } bool VmmRange::map_one(int64_t, VmmChunk) { return false; } bool VmmRange::set_access(int64_t, int64_t) { return false; } +bool VmmRange::commit_run(int64_t, int64_t) { return false; } VmmChunk VmmRange::unmap(int64_t) { return 0; } } // namespace strata::core diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 43734a1c..3a51eb8b 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -5114,7 +5114,11 @@ int main(int argc, char** argv) { if (r.mapped(c)) if (const strata::core::VmmChunk h = r.unmap(c)) kvg.spare.push_back(h); } - if (gave > 0) cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); + if (gave > 0 && + cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice) != cudaSuccess) { + std::fprintf(stderr, "strata: the residency table upload failed: %s\n", cudaGetErrorString(cudaGetLastError())); + return false; + } int64_t fresh = 0; const bool ok = strata::core::qsa_kv_elastic_grow(target, [&]() -> strata::core::VmmChunk { if (kvg.spare.empty()) { ++fresh; return 0; } // the cache is at its floor: new memory @@ -5138,20 +5142,26 @@ int main(int argc, char** argv) { }; // A request that needs far fewer cells than the K/V holds gives the rest back: the slots refill with the // profile's hottest experts the GPU does not hold. Run on a quiet device (as kvg_ensure's `quiesce`). - auto kvg_trim = [&](int64_t cells) { - if (!kvg.on) return; + auto kvg_trim = [&](int64_t cells) -> bool { + if (!kvg.on) return true; const int64_t target = std::max(kvg.step, (cells + kvg.step - 1) / kvg.step * kvg.step); - if (kvg.cells < target + 2 * kvg.step || kvg.lo >= kvg.top) return; + if (kvg.cells < target + 2 * kvg.step || kvg.lo >= kvg.top) return true; strata::core::qsa_kv_elastic_shrink(target, [&](strata::core::VmmChunk h) { kvg.spare.push_back(h); }); kvg.cells = strata::core::qsa_kv_elastic_cells(); strata::core::VmmRange& r = *xcache.vmm_range(); const uint64_t G = strata::core::vmm_granularity(); const int64_t c1 = (int64_t) (xcache.slot_offset(kvg.top) / G); - for (int64_t c = (int64_t) (xcache.slot_offset(kvg.lo) / G); c < c1 && !kvg.spare.empty(); ++c) { - if (r.mapped(c)) continue; - const strata::core::VmmChunk h = kvg.spare.back(); - kvg.spare.pop_back(); - if (!r.map_range(c, c + 1, [h] { return h; })) break; // map_range frees h when it fails + { // one run from the lowest unmapped chunk, as long as the chunks handed back last (one access call) + const int64_t c0 = (int64_t) (xcache.slot_offset(kvg.lo) / G); + int64_t cend = c0, need = (int64_t) kvg.spare.size(); + while (cend < c1 && need > 0) need -= r.mapped(cend++) ? 0 : 1; + if (!r.map_range(c0, cend, [&]() -> strata::core::VmmChunk { + if (kvg.spare.empty()) return 0; + const strata::core::VmmChunk h = kvg.spare.back(); + kvg.spare.pop_back(); + return h; + })) + std::fprintf(stderr, "strata: the expert cache could not take its VRAM back from the K/V\n"); } const int64_t lo0 = kvg.lo; while (kvg.lo < kvg.top) { // a slot whose chunks are all mapped again holds an expert again @@ -5162,7 +5172,9 @@ int main(int argc, char** argv) { if (!all) break; ++kvg.lo; } + // the refills on one stream and one sync (a blocking copy each was a driver round trip per expert) const auto& lay = strata::kernels::cpu::expert_layout(); + std::vector> filled; // (residency index, slot), resident once the copies landed size_t pi = 0; for (int64_t s = lo0; s < kvg.lo; ++s) { const uint64_t room = xcache.slot_offset(s + 1) - xcache.slot_offset(s); @@ -5171,22 +5183,28 @@ int main(int argc, char** argv) { const size_t i = (size_t) l * (size_t) g.n_expert + (size_t) e; if (host_res[i] >= 0 || lay.blob_bytes(l) > room) continue; const uint8_t* b = srcp->blob(l, e); - if (b == nullptr || cudaMemcpy(xcache.device_slot((int32_t) s), b, (size_t) lay.blob_bytes(l), - cudaMemcpyHostToDevice) != cudaSuccess) - continue; - host_res[i] = (int32_t) s; - ++kvg.refilled; + if (b == nullptr) continue; + cudaMemcpyAsync(xcache.device_slot((int32_t) s), b, (size_t) lay.blob_bytes(l), cudaMemcpyHostToDevice, + nullptr); + filled.emplace_back(i, (int32_t) s); ++pi; break; } } - cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice); - cudaDeviceSynchronize(); + if (cudaDeviceSynchronize() != cudaSuccess) { + std::fprintf(stderr, "strata: refilling the expert cache failed: %s\n", cudaGetErrorString(cudaGetLastError())); + return false; + } + for (const auto& [i, s] : filled) host_res[i] = s; + kvg.refilled += (int64_t) filled.size(); + if (cudaMemcpy(d_res, host_res.data(), host_res.size() * sizeof(int32_t), cudaMemcpyHostToDevice) != cudaSuccess) + return false; for (const strata::core::VmmChunk h : kvg.spare) strata::core::vmm_chunk_free(h); // new ones, if any kvg.spare.clear(); ++kvg.trims; std::fprintf(stderr, "strata: K/V trimmed to %lld cells; %lld slots back to the expert cache, refilled from " "the profile\n", (long long) kvg.cells, (long long) (kvg.lo - lo0)); + return true; }; // ---- the resident RAM mode (--resident-experts / --resident-cpu-experts): the experts the GPU cache does not // hold are copied from experts.bin into RAM once, so no decode or prompt step reads the file (the plain mmap @@ -7659,10 +7677,11 @@ int main(int argc, char** argv) { } auto kv_quiesce = [&] { cudaDeviceSynchronize(); apply_pending(true); }; if (kvg.on) { // the elastic K/V: this prompt's cells (it gives back what it does not need below) + // a failed growth may leave the tier half-changed: the engine stops (as for any CUDA failure) if (!kvg_ensure(n + 256, kv_quiesce)) { std::printf("ERR the K/V cannot grow to this prompt: no VRAM is left\n"); std::fflush(stdout); - continue; + return 1; } } int64_t resume = 0; @@ -7847,7 +7866,11 @@ int main(int argc, char** argv) { // so cells past this prompt's are no longer anyone's - far more than it needs go back to the cache if (kvg.on) { kv_quiesce(); - kvg_trim(n + 256); + if (!kvg_trim(n + 256)) { + std::printf("ERR the K/V could not give its VRAM back to the expert cache\n"); + std::fflush(stdout); + return 1; + } } int64_t reread_to = -1; // STRATA_CKPT_REREAD only: read [0, reread_to) again instead of restoring if (resume == 0) {