Inkling HIP: the VRAM guard drives the elastic pool (shed experts when other programs take memory, take them back when it is given back); K/V growth asks the guard for spare memory

This commit is contained in:
Niko1221 committed 2026-12-03 01:39:36 +01:00
1 parent dd3ff7d98a
commit 47c22697cc
1 file changed
+61 -3
@@ -31,6 +31,8 @@
#include "strata/quant/sk_codecs.hpp"
#include "strata/core/vmm.hpp"
#include "../ink_elastic_sec4.hpp"
#include "strata/platform/vram_guard.hpp"
#include "strata/platform/vram_guard_driver.hpp"
#if defined(__linux__)
#include "strata/platform/numa_balancing.hpp"
#endif
@@ -7903,6 +7905,7 @@ private:
// Pre-ensure KV pages exist for all tokens in batch across all 40 SWA & 8 Global pools
// Keep >= 768 MiB free headroom on R9700 so amdgpu TTM never crosses the 31.8 GiB eviction cliff!
const size_t kv_headroom = r9700_ ? (768ULL << 20) : 0u;
if (elastic_.active() && max_slots_ == 1) elastic_guard_poll(); // other programs' memory: shed or take back experts (one clock read between samples)
for (int b = 0; b < B; ++b) {
const int s = batch.slot_ids[b];
const int p = batch.pos[b];
@@ -14710,6 +14713,11 @@ private:
size_t fr = 0, tot = 0;
if (hipMemGetInfo(&fr, &tot) == hipSuccess && fr > floor_b) {
int64_t spare = static_cast<int64_t>((fr - floor_b) / gran);
if (elastic_.active()) {
// the guard sees what the driver's number does not (other programs' memory on WDDM) and keeps its own low watermark
if (!elastic_guard_started_) { elastic_guard_started_ = true; elastic_guard_init(); }
if (vram_guard_.enabled()) spare = std::min<int64_t>(spare, static_cast<int64_t>(vram_guard_.spare_for_growth() / gran));
}
while (need > 0 && spare > 0) {
const strata::core::VmmChunk h = strata::core::vmm_chunk_new();
if (h == 0) break;
@@ -14929,8 +14937,8 @@ private:
}
// Chunks the K/V gives back (the conversation ended): the experts come back into them, read from the host copy.
void elastic_regrow(int64_t add_chunks) {
if (!elastic_.active() || elastic_n_evicted_ == 0 || add_chunks <= 0) return;
int64_t elastic_regrow(int64_t add_chunks, bool allow_os = false) {
if (!elastic_.active() || elastic_n_evicted_ == 0 || add_chunks <= 0) return 0;
const auto& dir = adj_dir_;
uint8_t* arena = const_cast<uint8_t*>(expert_gpu_arena_.dev_ptr);
auto reload = [&](int key, uint64_t off, uint32_t bytes) -> bool {
@@ -14958,9 +14966,56 @@ private:
elastic_upload_layer(il);
}
};
const int64_t got = elastic_.regrow_chunks(add_chunks, /*allow_os_alloc=*/false, reload, sync_fn, publish_fn);
const int64_t got = elastic_.regrow_chunks(add_chunks, /*allow_os_alloc=*/allow_os, reload, sync_fn, publish_fn);
if (got > 0) invalidate_graph();
if (got > 0) std::fprintf(stderr, "[ink-hip] elastic experts: %lld chunks back to the experts; %zu experts are off the card\n", static_cast<long long>(got), elastic_n_evicted_);
return got;
}
// ---- live VRAM pressure (strata/platform/vram_guard.hpp): another program took memory, or gave it back ----
// Shed: idle chunks first, then the coldest experts (copied to the host first); the chunks go back to the OS. MayReclaim (only for what was shed): the experts come
// back into chunks taken from the OS again. Polled once per batch (one clock read between samples).
void elastic_guard_init() {
if (!elastic_.active()) return;
strata::platform::VramGuardConfig c = strata::platform::make_hip_config(gpu0_id_ >= 0 ? gpu0_id_ : 0);
c.chunk_bytes = strata::core::vmm_granularity();
if (vram_guard_.init(c)) {
std::fprintf(stderr, "[ink-hip] VRAM guard on: %s (low %llu MiB, target %llu MiB, high %llu MiB)\n", vram_guard_.status(),
static_cast<unsigned long long>(vram_guard_.low_bytes() >> 20), static_cast<unsigned long long>(vram_guard_.target_bytes() >> 20),
static_cast<unsigned long long>(vram_guard_.high_bytes() >> 20));
}
}
void elastic_guard_poll() {
if (!elastic_guard_started_) { elastic_guard_started_ = true; elastic_guard_init(); }
if (!vram_guard_.enabled() || !elastic_.active() || elastic_off_) return;
const uint64_t g = strata::core::vmm_granularity();
if (g == 0) return;
const strata::platform::VramAdvice adv = vram_guard_.poll(elastic_shed_bytes_);
if (adv.kind == strata::platform::VramAdvice::Shed) {
const int64_t want = static_cast<int64_t>(adv.bytes / g);
int64_t done = 0;
while (done < want && !kv_spare_.empty()) { strata::core::vmm_chunk_free(kv_take_chunk()); ++done; } // idle chunks first
if (done < want) {
const int64_t got = shed_experts_for_chunks(want - done, stream_);
kv_collect_donated();
for (int64_t i = 0; i < got && !kv_spare_.empty(); ++i) strata::core::vmm_chunk_free(kv_take_chunk());
done += got;
}
elastic_shed_bytes_ += static_cast<uint64_t>(done) * g;
vram_guard_.report_applied(static_cast<uint64_t>(done) * g, 0);
std::fprintf(stderr, "[ink-hip] VRAM pressure (headroom %.0f MiB): %.0f MiB of experts / idle chunks went back to the OS (%.0f MiB shed in all)\n",
static_cast<double>(adv.snap.headroom_bytes) / 1048576.0, static_cast<double>(done * static_cast<int64_t>(g)) / 1048576.0,
static_cast<double>(elastic_shed_bytes_) / 1048576.0);
} else if (adv.kind == strata::platform::VramAdvice::MayReclaim && elastic_shed_bytes_ > 0) {
const int64_t want = static_cast<int64_t>(std::min<uint64_t>(adv.bytes, elastic_shed_bytes_) / g);
const int64_t got = elastic_regrow(want, /*allow_os=*/true);
elastic_shed_bytes_ -= std::min<uint64_t>(elastic_shed_bytes_, static_cast<uint64_t>(got) * g);
vram_guard_.report_applied(0, static_cast<uint64_t>(got) * g);
if (got > 0) std::fprintf(stderr, "[ink-hip] memory is free again (headroom %.0f MiB): %.0f MiB of experts are back on the card (%.0f MiB still shed)\n",
static_cast<double>(adv.snap.headroom_bytes) / 1048576.0, static_cast<double>(got * static_cast<int64_t>(g)) / 1048576.0,
static_cast<double>(elastic_shed_bytes_) / 1048576.0);
}
}
// A conversation ended and no slot holds a K/V page any more: the global K/V returns to its start size and the freed chunks go back to the experts.
@@ -15267,6 +15322,9 @@ private:
std::vector<uint8_t> elastic_cold_layer_; // per layer: some expert is off the card
std::vector<int> elastic_layer_cold_n_;
GpuBuffer d_elastic_stage_, d_elastic_step_, d_elastic_plan_;
strata::platform::VramGuard vram_guard_;
uint64_t elastic_shed_bytes_ = 0; // bytes of experts / chunks given back to the OS because other programs needed the memory
bool elastic_guard_started_ = false;
// The host copy of an expert that is not on the card (RAM bodies of the hybrid tier, or the pinned copy of an expert that left); nullptr when none.
const uint8_t* host_body_of(int dir_idx) const {