Elastic K/V: VMM access failures roll back, failed growth stops the engine, the trim maps one run

From an audit of this PR, each finding checked against the code:
- VmmRange: a run whose cuMemSetAccess failed stayed counted as mapped (a later map_range skipped it, so the chunks
  were never made accessible); it is unmapped and freed again (commit_run).
- a failed growth or trim could leave host_res, d_res and the slot range half-changed while serve went on to the
  next request; such a failure now stops the engine, as other CUDA failures do. The residency uploads are checked.
- the trim maps the chunks it takes back as one run (one cuMemSetAccess instead of one per 2 MiB chunk) and refills
  the slots with async copies and one sync instead of a blocking cudaMemcpy per expert.

IQ2_XS: the served scenario (grow at a request and mid-decode, trim and refill, regrow, a parked conversation's
restore) runs as before; vmm_test passes.
This commit is contained in:
sergiywith authored and Niko1221 committed 2026-10-05 19:57:35 +02:00
1 parent 9e3dbfe74a
commit 025bf3b2ef
3 files changed
+54 -21

No files matched your search

+41 -18
View File
@@ -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<int64_t>(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<std::pair<size_t, int32_t>> 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) {