Files
Strata/engine/tests/core/mapped_pages_test.cpp
T
Niko1221 8c2afbc2bf fix(inkling): the GGUF loader gives the mapped pages of a tensor back once it is copied, halving peak RSS (UD-IQ4_XS 28 -> 14 GiB, UD-Q4_K_XL 32 -> 15 GiB)
The GGUF is mmapped read-only (strata::GgufFile) and every tensor is copied out of the mapping: the trunk into the arena, each expert into a
VRAM upload buffer or the host expert arena. The mapped pages stayed in the resident set after the copy, so the file (15.7 GB UD-IQ4_XS,
17.6 GB UD-Q4_K_XL) sat next to the copies. Peak RSS was 28-36 GiB with 7-13 GiB of experts in RAM, which does not fit a 32 GB machine.

- strata/core/mapped_pages.hpp: release_mapped_pages(p, n) drops the whole pages inside [p, p + n) from the process (madvise MADV_DONTNEED on
  Linux, VirtualUnlock to trim the working set on Windows; rounded inward so a neighbour's page is never dropped). The pages stay in the OS
  page cache, reclaimable, and fault back in on a later read, so only the double count goes. STRATA_INK_KEEP_MAPPED_PAGES=1 turns it off.
- ink_gguf_loader.h: read_expert_body releases the gate/up/down source ranges after copying them (both the NVFP4 -> SF4 repack and the GGUF_RAW
  copy); the trunk, drafter and vision tensors (every tensor without _exps) are released once sec1_arena holds them.
- tests/core/mapped_pages_test.cpp: a 64 MB mapping touched in full drops from 67 MB to 3.5 MB resident, unaligned ends round inward, and the
  data reads back unchanged (Linux, -O0 and -O2).
- The .strata and safetensors loaders read with fread / ifstream and never map the file: SQ-10 / SQ-20 peaked at 3.3 / 13.5 GiB on the 12 GB
  card, which is the plan's RAM share plus the trunk and the CUDA runtime.

Measured on an RTX 3060 12 GB (planner-sized plans, ctx 32768, drafter off, same plan before and after): UD-IQ4_XS peak RSS 28.4 -> 13.8 GiB,
UD-Q4_K_XL 31.8 -> 15.5 GiB; first-prompt, 128-token, 8000-token tokens identical on every step; decode 11.5 / 7.7 -> 11.4 / 7.9 tok/s and
8.2 / 6.2 -> 8.3 / 6.2 tok/s (unchanged), prefill 217 -> 216 and 195 -> 195 tok/s.
2026-11-23 14:38:15 +01:00

79 lines
3.0 KiB
C++

// strata/core/mapped_pages.hpp: releasing a copied-out range of a read-only file mapping drops it from the process's resident set, keeps the
// neighbouring pages, and the data reads back unchanged afterwards. The RSS check is Linux only (/proc/self/status); elsewhere only the data checks run.
#include "strata/core/mapped_pages.hpp"
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#ifndef _WIN32
#include <fcntl.h>
#include <unistd.h>
#endif
namespace {
int failed = 0;
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
long rss_kb() {
std::FILE* f = std::fopen("/proc/self/status", "r");
if (!f) return -1;
char line[256];
long v = -1;
while (std::fgets(line, sizeof line, f))
if (std::strncmp(line, "VmRSS:", 6) == 0) { std::sscanf(line + 6, "%ld", &v); break; }
std::fclose(f);
return v;
}
} // namespace
int main() {
using strata::core::release_mapped_pages;
// pure range rules need no file
char probe[16384];
CHECK(release_mapped_pages(nullptr, 100) == 0);
CHECK(release_mapped_pages(probe, 0) == 0);
CHECK(release_mapped_pages(probe + 1, 100) == 0); // inside one page: nothing whole to release
#ifndef _WIN32
const size_t kSize = 64u << 20;
const std::string path = "mapped_pages_test.bin";
{
std::vector<unsigned char> buf(kSize);
for (size_t i = 0; i < kSize; ++i) buf[i] = static_cast<unsigned char>((i * 2654435761u) >> 24);
std::FILE* f = std::fopen(path.c_str(), "wb");
CHECK(f != nullptr);
if (!f) return 1;
std::fwrite(buf.data(), 1, kSize, f);
std::fclose(f);
}
int fd = ::open(path.c_str(), O_RDONLY);
CHECK(fd >= 0);
void* m = mmap(nullptr, kSize, PROT_READ, MAP_PRIVATE, fd, 0);
CHECK(m != MAP_FAILED);
const unsigned char* d = static_cast<const unsigned char*>(m);
volatile unsigned sum = 0; // volatile: an optimised build must really touch every page
for (size_t i = 0; i < kSize; i += 4096) sum = sum + d[i];
const long touched = rss_kb();
const size_t got = release_mapped_pages(d + 4096 + 100, kSize - 8192); // unaligned both ends: rounded inward
CHECK(got >= kSize - 3 * 4096 && got <= kSize);
const long after = rss_kb();
if (touched > 0 && after > 0) {
std::printf("RSS touched %ld kB, after release %ld kB\n", touched, after);
CHECK(touched - after > static_cast<long>(kSize / 1024) * 8 / 10); // at least 80% of the 64 MB came back
}
// the data is unchanged when read again (the pages fault back in from the page cache)
bool same = true;
for (size_t i = 0; i < kSize; i += 4097) if (d[i] != static_cast<unsigned char>((i * 2654435761u) >> 24)) { same = false; break; }
CHECK(same);
munmap(m, kSize);
::close(fd);
std::remove(path.c_str());
#endif
if (failed) { std::fprintf(stderr, "%d check(s) failed\n", failed); return 1; }
std::puts("mapped_pages_test: ok");
return 0;
}