STRATA_ARENA_MMAP=1: the expert arena as a read-only mapped file (Linux), for small-RAM machines
The host expert arena is normally read into pinned (locked) RAM. On a machine whose GPUs already hold most experts, that locks memory the CPU never reads: with the Coder (25 GB arena) on 2x 16 GB GPUs and 32 GB of RAM, ~1 GB stayed available at 128K context, the kernel swapped, and a multi-turn checkpoint took 25-80 s. With STRATA_ARENA_MMAP=1 (opt-in, Linux; off by default, nothing changes without it): - the first start writes the arena to the pack's experts.bin (padded by one blob so a whole-slot copy may start at any expert); later starts map it read-only, not locked, not pinned. The page cache keeps what the CPU pool and the prompt path read and gives the rest back under pressure; a dropped page is re-read from the file, so no result depends on what is resident. The GPUs get no mapped alias of it: run with --pcie-frac 0; - after the VRAM caches are filled, the pages of experts a GPU holds are handed back (MADV_DONTNEED, STRATA_ARENA_RELEASE=0 keeps them) and the rest are prefetched (MADV_WILLNEED); an adaptive swap releases the expert that went to VRAM and prefetches the one that came back; - HIP: ROCclr copies a pageable source of 1 MiB or more by locking its pages in place and keeps them locked, which undid every release. The engine raises GPU_PINNED_MIN_XFER_SIZE (an explicit setting wins) so such copies stage through ROCclr's own buffers, and locks an adaptive swap's source pages itself only until the copy lands (the page ranges are merged first: a copy from a range only part of which is registered fails); - STRATA_RSS_TRACE=1 prints the process's RssFile at the startup steps (madvise reports success on locked pages and frees nothing; RssFile is the honest check). The existing --mmap-experts refuses native packs (the Coder); this path is for them. Measured on 2x AMD Instinct MI50 16 GB + 32 GB DDR4 (gfx906 port, Coder IQ1_M, 128K context, layer split): RAM available while serving ~1 GB -> 25 GB; multi-turn re-read of a checkpoint 25-80 s -> <= 3.2 s; decode unchanged (49.06 vs 49.16 ms per verify window). Not measured on NVIDIA (pin_blobs is a no-op there).
This commit is contained in:
1 parent
464ca57d7f
commit
6149efd3a4
4 files changed
+226
-4
No files matched your search
@@ -2569,7 +2569,7 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
|
||||
std::ifstream f(path, std::ios::binary | std::ios::ate);
|
||||
if (!f) { err = "ArenaExpertSource: cannot open " + path; return false; }
|
||||
const uint64_t got = (uint64_t) f.tellg();
|
||||
if (got != want) {
|
||||
if (got != want && got != want + (uint64_t) blob) { // + one blob: STRATA_ARENA_MMAP's padded file
|
||||
char buf[400];
|
||||
std::snprintf(buf, sizeof buf,
|
||||
"ArenaExpertSource: %s is %llu B but %lld layers x %lld experts (blobs up to %lld B) "
|
||||
@@ -2581,6 +2581,44 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
|
||||
}
|
||||
}
|
||||
|
||||
// STRATA_ARENA_MMAP=1 (a small-RAM machine whose GPUs hold most experts): the arena is the pack's experts.bin
|
||||
// mapped READ-ONLY - not locked, not pinned. The page cache keeps what the CPU pool and the prompt path
|
||||
// actually read (the experts no VRAM cache holds) and gives back the rest under memory pressure; a page
|
||||
// dropped is re-read from the file, so a result never depends on what is resident. The GPUs then get no
|
||||
// mapped alias: run with --pcie-frac 0. The first start writes experts.bin (arena layout, padded by one blob
|
||||
// so a whole-slot copy may start at any expert), later ones map it.
|
||||
#if defined(_WIN32)
|
||||
static const bool arena_mmap = false; // POSIX mmap/madvise: Linux only for now
|
||||
#else
|
||||
static const bool arena_mmap = [] { const char* v = std::getenv("STRATA_ARENA_MMAP"); return v && v[0] == '1'; }();
|
||||
if (arena_mmap) {
|
||||
const uint64_t file_bytes = want + (uint64_t) blob;
|
||||
uint64_t have = 0;
|
||||
{
|
||||
std::ifstream f(path, std::ios::binary | std::ios::ate);
|
||||
if (f) have = (uint64_t) f.tellg();
|
||||
}
|
||||
if (have == file_bytes) {
|
||||
const int fd = ::open(path.c_str(), O_RDONLY);
|
||||
void* v = fd >= 0 ? mmap(nullptr, (size_t) file_bytes, PROT_READ, MAP_SHARED, fd, 0) : MAP_FAILED;
|
||||
if (fd >= 0) ::close(fd);
|
||||
if (v == MAP_FAILED) { err = "ArenaExpertSource: mmap of " + path + " failed"; return false; }
|
||||
map_ = v;
|
||||
map_bytes_ = file_bytes;
|
||||
base_ = (const uint8_t*) v;
|
||||
pinned_bytes_ = 0;
|
||||
dev_slice_.clear();
|
||||
slice_bytes_ = 0;
|
||||
blobs_ = n_layers * n_expert;
|
||||
n_expert_ = n_expert;
|
||||
reads_ = 0;
|
||||
note_ = "mapped read-only from " + path + " (STRATA_ARENA_MMAP: not locked, not pinned)";
|
||||
gib_per_s_ = 0.0;
|
||||
load_seconds_ = load_read_s_ = load_copy_s_ = 0.0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
uint64_t pack_hash = 0;
|
||||
if (!shared_arena_file.empty() &&
|
||||
!shared_arena_pack_hash(pack_dir, path, gguf_, lay, pack_hash, err)) return false;
|
||||
@@ -2639,6 +2677,20 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
|
||||
err = "ArenaExpertSource: the load read " + std::to_string(st.bytes) + " B of " + std::to_string(want);
|
||||
return false;
|
||||
}
|
||||
if (arena_mmap && from_gguf) { // the first start: write experts.bin for the mapped starts after this one
|
||||
const std::string tmp = path + ".tmp";
|
||||
std::FILE* f = std::fopen(tmp.c_str(), "wb");
|
||||
bool ok = f != nullptr && std::fwrite(a->data(), 1, (size_t) want, f) == (size_t) want;
|
||||
if (ok) {
|
||||
std::vector<uint8_t> pad((size_t) blob, 0);
|
||||
ok = std::fwrite(pad.data(), 1, pad.size(), f) == pad.size();
|
||||
}
|
||||
if (f) ok = std::fclose(f) == 0 && ok;
|
||||
if (ok) ok = std::rename(tmp.c_str(), path.c_str()) == 0;
|
||||
if (!ok) std::remove(tmp.c_str());
|
||||
std::fprintf(stderr, "strata generate: STRATA_ARENA_MMAP: %s %s (the next start maps it)\n",
|
||||
ok ? "wrote" : "could NOT write", path.c_str());
|
||||
}
|
||||
arena_ = a;
|
||||
base_ = a->data();
|
||||
pinned_bytes_ = a->registered_bytes;
|
||||
@@ -2669,6 +2721,11 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
|
||||
}
|
||||
|
||||
void ArenaExpertSource::close() {
|
||||
#if !defined(_WIN32)
|
||||
if (map_ != nullptr) munmap(map_, (size_t) map_bytes_);
|
||||
#endif
|
||||
map_ = nullptr;
|
||||
map_bytes_ = 0;
|
||||
if (arena_ != nullptr) {
|
||||
delete (PinnedArena*) arena_;
|
||||
arena_ = nullptr;
|
||||
@@ -2678,6 +2735,39 @@ void ArenaExpertSource::close() {
|
||||
n_expert_ = 0;
|
||||
}
|
||||
|
||||
void ArenaExpertSource::prefetch(int64_t layer, int64_t expert) {
|
||||
#if defined(_WIN32)
|
||||
(void) layer; (void) expert;
|
||||
#else
|
||||
if (map_ == nullptr || layer < 0 || expert < 0 || expert >= n_expert_) return;
|
||||
const auto& lay = strata::kernels::cpu::expert_layout();
|
||||
const uint64_t off = lay.blob_offset(layer, expert) & ~(uint64_t) 4095;
|
||||
const uint64_t end = lay.blob_offset(layer, expert) + lay.blob_bytes(layer);
|
||||
madvise((uint8_t*) map_ + off, (size_t) (end - off), MADV_WILLNEED);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Only the pages wholly inside the blob: a page shared with a neighbour the CPU may still read stays. MADV_DONTNEED
|
||||
// unmaps them from this process; they stay in the page cache as clean, unmapped pages - the first the kernel takes
|
||||
// back under pressure, and a minor fault away when the CPU needs the expert again (an adaptive swap evicts it).
|
||||
// Dropping them from the cache too (POSIX_FADV_DONTNEED) made every eviction a re-read from the disk: ~200 major
|
||||
// faults a second in decode, +8 ms a window. The memory the arena used to hold was never the cache itself but
|
||||
// ROCclr's pin-in-place locks on it (see main) - locked pages are the ones the kernel cannot take back.
|
||||
uint64_t ArenaExpertSource::release(int64_t layer, int64_t expert) {
|
||||
#if defined(_WIN32)
|
||||
(void) layer; (void) expert;
|
||||
return 0;
|
||||
#else
|
||||
if (map_ == nullptr || layer < 0 || expert < 0 || expert >= n_expert_) return 0;
|
||||
const auto& lay = strata::kernels::cpu::expert_layout();
|
||||
const uint64_t off = (lay.blob_offset(layer, expert) + 4095) & ~(uint64_t) 4095;
|
||||
const uint64_t end = (lay.blob_offset(layer, expert) + lay.blob_bytes(layer)) & ~(uint64_t) 4095;
|
||||
if (end <= off) return 0;
|
||||
if (madvise((uint8_t*) map_ + off, (size_t) (end - off), MADV_DONTNEED) != 0) return 0;
|
||||
return end - off;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ArenaExpertSource::pinned(int64_t layer, int64_t expert) const {
|
||||
if (base_ == nullptr || layer < 0 || expert < 0 || expert >= n_expert_) return false;
|
||||
const auto& lay = strata::kernels::cpu::expert_layout();
|
||||
|
||||
Reference in new issue
Block a user