engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied

Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-25 12:49:15 +01:00
co-authored by Claude Sonnet 5.5
parent 570201a0dc
commit 87e2633afe
2498 changed files with 641871 additions and 7323 deletions
+122
View File
@@ -8,6 +8,11 @@
#include <dxgi1_4.h>
#include <cstring>
#else
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#endif
@@ -54,6 +59,49 @@ LockResult lock_resident(void* p, uint64_t bytes) {
return r;
}
LockResult lock_resident_ranges(const std::vector<std::pair<void*, uint64_t>>& regions) {
LockResult r;
uint64_t total = 0;
for (const auto& g : regions) total += g.second;
if (total == 0) { r.note = "nothing to lock"; return r; }
HANDLE self = GetCurrentProcess();
SIZE_T min_ws = 0, max_ws = 0;
DWORD flags = 0;
if (!GetProcessWorkingSetSizeEx(self, &min_ws, &max_ws, &flags)) {
r.note = "GetProcessWorkingSetSizeEx failed (error " + std::to_string(GetLastError()) + ")";
return r;
}
// same growth as lock_resident, but for the sum of the regions and with ONE margin
const SIZE_T margin = (SIZE_T) 512 << 20;
const SIZE_T new_min = min_ws + (SIZE_T) total + margin;
const SIZE_T new_max = max_ws > new_min + margin ? max_ws : new_min + margin;
if (!SetProcessWorkingSetSizeEx(self, new_min, new_max,
QUOTA_LIMITS_HARDWS_MIN_DISABLE | QUOTA_LIMITS_HARDWS_MAX_DISABLE)) {
r.note = "SetProcessWorkingSetSizeEx(" + std::to_string((unsigned long long) (new_min >> 20)) +
" MiB) failed (error " + std::to_string(GetLastError()) + ")";
return r;
}
const uint64_t chunk = 1ull << 30;
for (const auto& g : regions) {
uint8_t* base = (uint8_t*) g.first;
for (uint64_t off = 0; off < g.second; off += chunk) {
const uint64_t n = g.second - off < chunk ? g.second - off : chunk;
if (!VirtualLock(base + off, (SIZE_T) n)) {
r.note = "VirtualLock stopped after " + std::to_string((unsigned long long) (r.locked_bytes >> 20)) +
" of " + std::to_string((unsigned long long) (total >> 20)) + " MiB (error " +
std::to_string(GetLastError()) + ")";
r.ok = r.locked_bytes > 0;
return r;
}
r.locked_bytes += n;
}
}
r.ok = true;
r.note = "locked " + std::to_string((unsigned long long) (total >> 20)) + " MiB in " +
std::to_string(regions.size()) + " regions via working-set minimum + VirtualLock";
return r;
}
void unlock_resident(void* p, uint64_t bytes) {
if (p == nullptr || bytes == 0) return;
const uint64_t chunk = 1ull << 30;
@@ -108,6 +156,10 @@ uint64_t total_physical_memory() {
ms.dwLength = sizeof ms;
return GlobalMemoryStatusEx(&ms) ? (uint64_t) ms.ullTotalPhys : 0;
}
bool read_ahead_enabled() { return false; }
void advise_willneed(const void*, uint64_t) {}
void advise_willneed(int, uint64_t, uint64_t) {}
#else
LockResult lock_resident(void* p, uint64_t bytes) {
LockResult r;
@@ -119,6 +171,18 @@ LockResult lock_resident(void* p, uint64_t bytes) {
return r;
}
LockResult lock_resident_ranges(const std::vector<std::pair<void*, uint64_t>>& regions) {
LockResult r;
for (const auto& g : regions) {
if (g.first == nullptr || g.second == 0) continue;
if (mlock(g.first, g.second) != 0) { r.note = "mlock failed (raise ulimit -l)"; r.ok = r.locked_bytes > 0; return r; }
r.locked_bytes += g.second;
}
r.ok = r.locked_bytes > 0;
r.note = r.ok ? "mlock of " + std::to_string(regions.size()) + " regions" : "nothing to lock";
return r;
}
void unlock_resident(void* p, uint64_t bytes) {
if (p != nullptr && bytes != 0) munlock(p, bytes);
}
@@ -133,6 +197,64 @@ uint64_t total_physical_memory() {
const long pages = sysconf(_SC_PHYS_PAGES), page = sysconf(_SC_PAGE_SIZE);
return pages > 0 && page > 0 ? (uint64_t) pages * (uint64_t) page : 0;
}
namespace {
constexpr uint64_t kAdviseStep = 128ull << 10;
}
bool read_ahead_enabled() {
static const bool on = [] {
const char* v = std::getenv("STRATA_READ_AHEAD");
return v == nullptr || std::atoi(v) != 0;
}();
return on;
}
void advise_willneed(const void* p, uint64_t bytes) {
if (p == nullptr || bytes == 0 || !read_ahead_enabled()) return;
const long ps = sysconf(_SC_PAGE_SIZE);
const uintptr_t pg = ps > 0 ? (uintptr_t) ps : 4096, end = (uintptr_t) p + bytes;
for (uintptr_t a = (uintptr_t) p & ~(pg - 1); a < end; a += kAdviseStep)
(void) madvise((void*) a, (size_t) std::min<uintptr_t>(kAdviseStep, end - a), MADV_WILLNEED);
}
void advise_willneed(int fd, uint64_t offset, uint64_t bytes) {
if (fd < 0 || !read_ahead_enabled()) return;
for (uint64_t at = 0; at < bytes; at += kAdviseStep)
#if defined(__APPLE__) // macOS: F_RDADVISE is the read-ahead hint
{
radvisory ra{(off_t) (offset + at), (int) std::min(kAdviseStep, bytes - at)};
(void) fcntl(fd, F_RDADVISE, &ra);
}
#else
(void) posix_fadvise(fd, (off_t) (offset + at), (off_t) std::min(kAdviseStep, bytes - at), POSIX_FADV_WILLNEED);
#endif
}
#endif
ProcIo proc_io_sample() {
ProcIo r;
#if defined(__linux__)
if (std::FILE* f = std::fopen("/proc/self/io", "r")) {
char key[64];
unsigned long long v;
while (std::fscanf(f, "%63[^:]: %llu\n", key, &v) == 2)
if (std::strcmp(key, "read_bytes") == 0) { r.read_bytes = v; r.valid = true; }
std::fclose(f);
}
if (std::FILE* f = std::fopen("/proc/self/stat", "r")) {
char buf[1024];
const size_t n = std::fread(buf, 1, sizeof buf - 1, f);
buf[n] = 0;
std::fclose(f);
if (const char* p = std::strrchr(buf, ')')) { // fields after the command: state ppid ... minflt cminflt majflt
unsigned long long minflt = 0, cminflt = 0, majflt = 0;
if (std::sscanf(p + 2, "%*c %*d %*d %*d %*d %*d %*u %llu %llu %llu", &minflt, &cminflt, &majflt) == 3)
r.major_faults = majflt;
}
}
#endif
return r;
}
} // namespace strata::platform