Files
strata/tests/core/vmm_test.cpp
T
sergiywith 9e3dbfe74a Elastic K/V (--kv-grow): the K/V takes VRAM as the context grows, the expert cache holds the rest
At --max-context 262144 the K/V is allocated whole at start (3.35 GiB at int8 with the drafter's) and the expert
cache is sized around it. With --kv-grow (or STRATA_KV_GROW=1) the K/V pools and the cache arena are CUDA VMM ranges
(core/vmm.hpp; the driver calls through the runtime's entry-point query, nothing links the driver library). The K/V
maps 2 MiB chunks for the cells the requests reach (16384 at start); when a request needs more, slots just below the
prompt path's loan give theirs up, a hotter expert in them first moving into the slot of the coldest one in the loan
region. A short request after a long one hands the chunks back and the slots refill from the profile. No address
moves, so no graph is captured again, and --max-context cells always fit.

- serve: grows at a request (its prompt, and a parked conversation before its restore) and in the decode rounds;
  trims once the outgoing session is parked
- generate: grows once for the prompt and max_new, before the loan
- one GPU with a profile, the whole K/V in VRAM (no --kv-resident), every expert in RAM (not the low-RAM tier);
  STRATA_KV_GROW_INIT / _STEP / _FLOOR for tests. Off by default: more slots move experts between GPU and CPU.
- tests/core/vmm_test: map, a chunk moved between ranges keeps its bytes, release

IQ2_XS, RTX 5090, 262K, fixed cache: first-token logits and 64 tokens bit-identical to 0.1.31 off, mapped whole,
grown into new VRAM, and grown by giving up 266 slots. Cache auto: 15,119-15,207 -> 17,300-17,503 slots, short chat
15.06-15.33 -> 14.71-14.83 ms a round; an emulated 16 GB card: 3,396 -> 5,719 slots, short chat 100 -> 115 tok/s,
a 32K prompt 7.44 -> 6.36 s and 24.6 -> 22.2 ms a round after it.
2026-10-05 19:57:35 +02:00

63 lines
2.9 KiB
C++

// The elastic K/V's VMM ranges (vmm.hpp): chunks map readable and writable, a chunk unmapped from one range and mapped
// into another keeps its bytes (what the expert cache hands the K/V), and a range releases what it holds.
#include "strata/core/vmm.hpp"
#include <cuda_runtime.h>
#include <cstdio>
#include <vector>
using strata::core::VmmChunk;
using strata::core::VmmRange;
static int fails = 0;
#define CHECK(c) do { if (!(c)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); ++fails; } } while (0)
int main() {
if (cudaFree(nullptr) != cudaSuccess || !strata::core::vmm_available()) {
std::printf("vmm_test: no CUDA device with virtual memory - skipped\n");
return 0;
}
const uint64_t G = strata::core::vmm_granularity();
CHECK(G >= 4096);
VmmRange a, b;
CHECK(a.reserve(8 * G + 1)); // rounds up
CHECK(a.chunks() == 9);
CHECK(b.reserve(4 * G));
CHECK(a.mapped_count() == 0);
CHECK(a.map_range(0, 6, [] { return (VmmChunk) 0; }));
CHECK(a.mapped_count() == 6 && a.mapped(5) && !a.mapped(6));
std::vector<uint8_t> h(6 * G), back(6 * G);
for (size_t i = 0; i < h.size(); ++i) h[i] = (uint8_t) (i * 2654435761u >> 13);
CHECK(cudaMemcpy(a.base(), h.data(), h.size(), cudaMemcpyHostToDevice) == cudaSuccess);
// chunks 4 and 5 move to b's chunks 1 and 0
const VmmChunk c4 = a.unmap(4), c5 = a.unmap(5);
CHECK(c4 != 0 && c5 != 0 && !a.mapped(4) && a.unmap(4) == 0);
std::vector<VmmChunk> give = {c4, c5}; // map_range takes them from the back: chunk 0 <- c5, chunk 1 <- c4
CHECK(b.map_range(0, 2, [&] { VmmChunk x = give.back(); give.pop_back(); return x; }));
CHECK(cudaMemcpy(back.data(), b.base(), G, cudaMemcpyDeviceToHost) == cudaSuccess);
bool same = true;
for (uint64_t i = 0; i < G; ++i) same = same && back[i] == h[5 * G + i];
CHECK(same);
CHECK(cudaMemcpy(back.data(), b.base() + G, G, cudaMemcpyDeviceToHost) == cudaSuccess);
same = true;
for (uint64_t i = 0; i < G; ++i) same = same && back[i] == h[4 * G + i];
CHECK(same);
// a's first chunks are untouched, and map_range fills a hole with new memory, skipping mapped chunks
CHECK(cudaMemcpy(back.data(), a.base(), 4 * G, cudaMemcpyDeviceToHost) == cudaSuccess);
same = true;
for (uint64_t i = 0; i < 4 * G; ++i) same = same && back[i] == h[i];
CHECK(same);
CHECK(a.map_range(3, 7, [] { return (VmmChunk) 0; }) && a.mapped_count() == 7);
CHECK(cudaMemset(a.base() + 4 * G, 7, 3 * G) == cudaSuccess && cudaDeviceSynchronize() == cudaSuccess);
size_t f0 = 0, f1 = 0, t = 0;
cudaMemGetInfo(&f0, &t);
a.release();
b.release();
cudaMemGetInfo(&f1, &t);
CHECK(f1 >= f0 + 8 * G); // 7 + 2 chunks went back (the driver may round)
CHECK(cudaGetLastError() == cudaSuccess);
std::printf("vmm_test: %s\n", fails ? "FAILED" : "ok");
return fails ? 1 : 0;
}