Merge f-029-par-amd (4772f0cd9)
This commit is contained in:
commit
16e08b50cd
12 files changed
+1025
-10
No files matched your search
@@ -20,6 +20,10 @@
|
||||
#include "strata/kernels/cpu/pool.hpp"
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#ifndef _WIN32
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
@@ -72,6 +76,27 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// The pid of this process and whether a pid still exists (the shared arena's reader election, both OSes)
|
||||
static uint64_t arena_this_pid() {
|
||||
#ifdef _WIN32
|
||||
return (uint64_t) GetCurrentProcessId();
|
||||
#else
|
||||
return (uint64_t) getpid();
|
||||
#endif
|
||||
}
|
||||
static bool arena_pid_alive(uint64_t pid) {
|
||||
#ifdef _WIN32
|
||||
HANDLE h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, (DWORD) pid);
|
||||
if (h == nullptr) return GetLastError() == ERROR_ACCESS_DENIED; // (denied: it exists; invalid parameter: it does not)
|
||||
DWORD code = 0;
|
||||
const bool alive = GetExitCodeProcess(h, &code) && code == STILL_ACTIVE;
|
||||
CloseHandle(h);
|
||||
return alive;
|
||||
#else
|
||||
return !(kill((pid_t) pid, 0) != 0 && errno == ESRCH);
|
||||
#endif
|
||||
}
|
||||
|
||||
namespace strata::core {
|
||||
|
||||
namespace detail {
|
||||
@@ -6000,6 +6025,43 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
|
||||
const bool unbuffered = experts_unbuffered(files, want + (uint64_t) blob, why);
|
||||
const int readers = unbuffered ? std::max(threads, 16) : threads; // 16 keep a PCIe 5 drive's queue full
|
||||
LoadStats st;
|
||||
// A shared arena (--shared-expert-arena, the replicas): ONE engine reads the experts into it, the others wait and skip the
|
||||
// read. The state word in the file's header says who: 0 nobody, a pid loading, ~0 filled. An engine that finds a
|
||||
// pid that is gone takes the load over (the previous one died mid-read: the arena is read again from the start), so a
|
||||
// dead loader never leaves the others waiting, and an engine that waits too long loads it itself.
|
||||
constexpr uint64_t kArenaFilled = ~uint64_t{0};
|
||||
std::atomic<uint64_t>* arena_state = padded_ ? nullptr : a->shared_state;
|
||||
bool arena_leader = false, arena_skip = false;
|
||||
if (arena_state != nullptr) {
|
||||
const uint64_t me = arena_this_pid();
|
||||
const auto t_begin = std::chrono::steady_clock::now();
|
||||
bool said = false;
|
||||
for (;;) {
|
||||
uint64_t cur = arena_state->load(std::memory_order_acquire);
|
||||
if (cur == kArenaFilled) { arena_skip = true; break; }
|
||||
if (cur == 0 || cur == me) {
|
||||
if (arena_state->compare_exchange_strong(cur, me, std::memory_order_acq_rel)) { arena_leader = true; break; }
|
||||
continue;
|
||||
}
|
||||
const bool alive = arena_pid_alive(cur);
|
||||
if (!alive) {
|
||||
if (arena_state->compare_exchange_strong(cur, me, std::memory_order_acq_rel)) {
|
||||
std::fprintf(stderr, "strata generate: shared expert arena: engine %llu died while reading it; reading it again\n",
|
||||
(unsigned long long) cur);
|
||||
arena_leader = true;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (std::chrono::steady_clock::now() - t_begin > std::chrono::minutes(45)) break; // (loads it on its own)
|
||||
if (!said) {
|
||||
said = true;
|
||||
std::fprintf(stderr, "strata generate: shared expert arena: engine %llu is reading the experts; waiting for it "
|
||||
"instead of reading them again\n", (unsigned long long) cur);
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
}
|
||||
}
|
||||
std::atomic<int> ready{0};
|
||||
std::thread reg;
|
||||
if (deferred) {
|
||||
@@ -6011,7 +6073,12 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
|
||||
});
|
||||
}
|
||||
const std::atomic<int>* rp = deferred ? &ready : nullptr;
|
||||
if (from_gguf) {
|
||||
if (arena_skip) {
|
||||
st.ok = true;
|
||||
st.bytes = want;
|
||||
st.seconds = 0.0;
|
||||
std::fprintf(stderr, "strata generate: shared expert arena: already filled by another engine; the read is skipped\n");
|
||||
} else if (from_gguf) {
|
||||
st = load_experts_gguf(gguf_, a->data(), lay, readers, unbuffered, rp, padded_ ? &pad_ : nullptr);
|
||||
} else {
|
||||
if (unbuffered && !padded_) st = load_experts_direct(path, a->data(), loff, lbytes, readers, /*chunk=*/8u << 20, rp);
|
||||
@@ -6022,15 +6089,18 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
|
||||
std::fprintf(stderr, "strata generate: expert arena read %s (%s)\n", unbuffered ? "unbuffered" : "through the file cache",
|
||||
why.c_str());
|
||||
if (!st.ok) {
|
||||
if (arena_leader) arena_state->store(0, std::memory_order_release); // (somebody else may try)
|
||||
delete a;
|
||||
err = "ArenaExpertSource: the expert load was refused: " + (st.error.empty() ? std::string("unknown") : st.error);
|
||||
return false;
|
||||
}
|
||||
if (st.bytes != want) {
|
||||
if (arena_leader) arena_state->store(0, std::memory_order_release);
|
||||
delete a;
|
||||
err = "ArenaExpertSource: the load read " + std::to_string(st.bytes) + " B of " + std::to_string(want);
|
||||
return false;
|
||||
}
|
||||
if (arena_leader) arena_state->store(kArenaFilled, std::memory_order_release); // the others may use (and skip reading) it
|
||||
if (std::getenv("STRATA_VERIFY_ARENA") != nullptr) { // tests: the loaded arena's checksum, any loader
|
||||
std::fprintf(stderr, "strata generate: expert arena checksum %016llx (%.2f GiB)\n",
|
||||
(unsigned long long) fnv1a64(a->data(), want), (double) want / (1ull << 30));
|
||||
|
||||
+214
-3
@@ -2,6 +2,7 @@
|
||||
#include "strata/core/pinned.hpp"
|
||||
#include "strata/platform/memory.hpp"
|
||||
#include "strata/core/pin_budget.hpp"
|
||||
#include <cstddef>
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
@@ -32,6 +33,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/vfs.h>
|
||||
#include <unistd.h>
|
||||
#include <linux/mman.h>
|
||||
#ifndef MAP_HUGE_2MB
|
||||
@@ -56,6 +58,51 @@ struct SharedArenaHeader {
|
||||
};
|
||||
static_assert(sizeof(SharedArenaHeader) <= kSharedArenaHeaderBytes);
|
||||
|
||||
#ifdef _WIN32
|
||||
// Shared-section views: MapViewOfFile address -> the section handle, so release() unmaps and closes instead of VirtualFree.
|
||||
std::mutex g_views_mu;
|
||||
std::map<void*, HANDLE> g_views;
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
// MemAvailable and, in a cgroup v2 group with a limit, the room left under it; ~0 when neither can be read
|
||||
uint64_t free_ram_room() {
|
||||
uint64_t room = ~uint64_t{0};
|
||||
if (std::FILE* f = std::fopen("/proc/meminfo", "r")) {
|
||||
char line[256];
|
||||
while (std::fgets(line, sizeof line, f) != nullptr) {
|
||||
unsigned long long kb = 0;
|
||||
if (std::sscanf(line, "MemAvailable: %llu kB", &kb) == 1) { room = (uint64_t) kb * 1024u; break; }
|
||||
}
|
||||
std::fclose(f);
|
||||
}
|
||||
std::string cg;
|
||||
if (std::FILE* f = std::fopen("/proc/self/cgroup", "r")) {
|
||||
char line[512];
|
||||
while (std::fgets(line, sizeof line, f) != nullptr)
|
||||
if (std::strncmp(line, "0::", 3) == 0) { cg = line + 3; break; }
|
||||
std::fclose(f);
|
||||
}
|
||||
while (!cg.empty() && (cg.back() == '\n' || cg.back() == '\r')) cg.pop_back();
|
||||
if (!cg.empty()) {
|
||||
unsigned long long lim = 0, cur = 0;
|
||||
bool have_lim = false;
|
||||
if (std::FILE* f = std::fopen(("/sys/fs/cgroup" + cg + "/memory.max").c_str(), "r")) {
|
||||
have_lim = std::fscanf(f, "%llu", &lim) == 1; // ("max" does not parse: no limit)
|
||||
std::fclose(f);
|
||||
}
|
||||
if (have_lim) {
|
||||
if (std::FILE* f = std::fopen(("/sys/fs/cgroup" + cg + "/memory.current").c_str(), "r")) {
|
||||
if (std::fscanf(f, "%llu", &cur) != 1) cur = 0;
|
||||
std::fclose(f);
|
||||
}
|
||||
room = std::min<uint64_t>(room, lim > cur ? (uint64_t) (lim - cur) : 0);
|
||||
}
|
||||
}
|
||||
return room;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
// bytes in free blocks of 2 MiB or more (/proc/buddyinfo, order >= 9): what MADV_HUGEPAGE can get without compaction
|
||||
uint64_t free_huge_bytes() {
|
||||
@@ -96,8 +143,102 @@ void* reserve(uint64_t bytes, PageBacking& got, std::string& note, const std::st
|
||||
|
||||
#ifdef _WIN32
|
||||
if (!shared_file.empty()) {
|
||||
note = "shared-file arena backing is not implemented on Windows";
|
||||
return nullptr;
|
||||
// The shared arena on Windows is a named, pagefile-backed section (CreateFileMappingW) that every engine of the
|
||||
// replicas opens by name: "win:Local\\strata-replicas-<server pid>" (a name without the prefix is used as it is).
|
||||
// The first engine creates it at the full size, the others open it; it lives while any process holds a handle or
|
||||
// a view, so a replica that restarts joins the same pages. The first 4 KiB are the same header as on Linux (the
|
||||
// pack hash, the size, the load state word); a new section is all zeros, so "no magic" means "nobody has been
|
||||
// here" - two engines that both find it empty write the same header.
|
||||
if (shared_pack_hash == 0) {
|
||||
note = "shared arena requires a nonzero pack hash";
|
||||
return nullptr;
|
||||
}
|
||||
if (bytes > UINT64_MAX - kSharedArenaHeaderBytes) {
|
||||
note = "shared arena size overflows its header";
|
||||
return nullptr;
|
||||
}
|
||||
const uint64_t file_bytes = bytes + kSharedArenaHeaderBytes;
|
||||
std::string nm = shared_file.rfind("win:", 0) == 0 ? shared_file.substr(4) : shared_file;
|
||||
std::wstring wname(nm.begin(), nm.end());
|
||||
// the section is committed memory (RAM plus pagefile) from the moment it exists: say so before taking it
|
||||
MEMORYSTATUSEX ms{};
|
||||
ms.dwLength = sizeof ms;
|
||||
if (GlobalMemoryStatusEx(&ms) && file_bytes + (2ull << 30) > ms.ullAvailPageFile) {
|
||||
HANDLE probe = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, wname.c_str()); // (already there: nothing new is committed)
|
||||
if (probe == nullptr) {
|
||||
note = "not enough free memory (RAM + pagefile) for the shared arena " + nm + ": it needs " +
|
||||
std::to_string((unsigned long long) (file_bytes >> 20)) + " MiB and " +
|
||||
std::to_string((unsigned long long) (ms.ullAvailPageFile >> 20)) + " MiB are available (2 GiB are kept back)";
|
||||
return nullptr;
|
||||
}
|
||||
CloseHandle(probe);
|
||||
}
|
||||
HANDLE h = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, (DWORD) (file_bytes >> 32),
|
||||
(DWORD) (file_bytes & 0xffffffffu), wname.c_str());
|
||||
if (h == nullptr) {
|
||||
note = "cannot create shared arena section " + nm + ": Windows error " + std::to_string((unsigned long long) GetLastError());
|
||||
return nullptr;
|
||||
}
|
||||
void* view = MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, 0); // (0: the whole section, as big as it was created)
|
||||
if (view == nullptr) {
|
||||
const DWORD e = GetLastError();
|
||||
CloseHandle(h);
|
||||
note = "cannot map shared arena section " + nm + ": Windows error " + std::to_string((unsigned long long) e);
|
||||
return nullptr;
|
||||
}
|
||||
MEMORY_BASIC_INFORMATION mbi{};
|
||||
if (VirtualQuery(view, &mbi, sizeof mbi) == 0 || (uint64_t) mbi.RegionSize < file_bytes) {
|
||||
UnmapViewOfFile(view);
|
||||
CloseHandle(h);
|
||||
note = "shared arena section " + nm + " is smaller than the " + std::to_string((unsigned long long) file_bytes) +
|
||||
" B this engine needs (including its header)";
|
||||
return nullptr;
|
||||
}
|
||||
auto* hdr = static_cast<SharedArenaHeader*>(view);
|
||||
bool empty = true;
|
||||
for (char c : hdr->magic) empty = empty && c == 0;
|
||||
if (empty) {
|
||||
SharedArenaHeader fresh{};
|
||||
std::memcpy(fresh.magic, kSharedArenaMagic, sizeof fresh.magic);
|
||||
fresh.version = 1;
|
||||
fresh.header_bytes = (uint32_t) kSharedArenaHeaderBytes;
|
||||
fresh.arena_bytes = bytes;
|
||||
fresh.pack_hash = shared_pack_hash;
|
||||
// fields after the magic first, the magic last: an engine that sees the magic sees a complete header (the
|
||||
// state word, reserved[0], stays zero)
|
||||
std::memcpy(reinterpret_cast<char*>(hdr) + sizeof fresh.magic, reinterpret_cast<char*>(&fresh) + sizeof fresh.magic,
|
||||
sizeof fresh - sizeof fresh.magic);
|
||||
MemoryBarrier();
|
||||
std::memcpy(hdr->magic, fresh.magic, sizeof fresh.magic);
|
||||
} else {
|
||||
// (an engine that arrives in the instant between another one's header fields and its magic never sees a
|
||||
// half header: the magic is written last)
|
||||
if (std::memcmp(hdr->magic, kSharedArenaMagic, sizeof hdr->magic) != 0 || hdr->version != 1 ||
|
||||
hdr->header_bytes != kSharedArenaHeaderBytes || hdr->arena_bytes != bytes) {
|
||||
UnmapViewOfFile(view);
|
||||
CloseHandle(h);
|
||||
note = "shared arena section " + nm + " has an incompatible or missing header";
|
||||
return nullptr;
|
||||
}
|
||||
if (hdr->pack_hash != shared_pack_hash) {
|
||||
char b[256];
|
||||
std::snprintf(b, sizeof b, "shared arena %s was written for pack hash %016llx, expected %016llx", nm.c_str(),
|
||||
(unsigned long long) hdr->pack_hash, (unsigned long long) shared_pack_hash);
|
||||
UnmapViewOfFile(view);
|
||||
CloseHandle(h);
|
||||
note = b;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_views_mu);
|
||||
g_views[view] = h;
|
||||
}
|
||||
mapping_base = view;
|
||||
mapping_bytes = file_bytes;
|
||||
got = PageBacking::NormalPages;
|
||||
note = "shared section " + nm + " (pack hash checked)";
|
||||
return static_cast<uint8_t*>(view) + kSharedArenaHeaderBytes;
|
||||
}
|
||||
// MEM_LARGE_PAGES needs SeLockMemoryPrivilege. Having it assigned to the account is not enough: the
|
||||
// PROCESS must enable it in its own token (AdjustTokenPrivileges) before VirtualAlloc, or the call fails.
|
||||
@@ -200,7 +341,27 @@ void* reserve(uint64_t bytes, PageBacking& got, std::string& note, const std::st
|
||||
return nullptr;
|
||||
}
|
||||
SharedArenaHeader hdr{};
|
||||
const ssize_t got_header = pread(fd, &hdr, sizeof hdr, 0);
|
||||
ssize_t got_header = pread(fd, &hdr, sizeof hdr, 0);
|
||||
// the creator sizes the file first and writes the header right after: an engine that starts in that gap waits
|
||||
// for it (up to 10 s) instead of calling the file broken
|
||||
for (int w = 0; w < 100 && got_header == (ssize_t) sizeof hdr &&
|
||||
std::memcmp(hdr.magic, kSharedArenaMagic, sizeof hdr.magic) != 0 &&
|
||||
hdr.magic[0] == 0 && hdr.magic[1] == 0; ++w) {
|
||||
usleep(100 * 1000);
|
||||
got_header = pread(fd, &hdr, sizeof hdr, 0);
|
||||
}
|
||||
if (got_header == (ssize_t) sizeof hdr && std::memcmp(hdr.magic, kSharedArenaMagic, sizeof hdr.magic) != 0 &&
|
||||
hdr.magic[0] == 0 && hdr.magic[1] == 0) {
|
||||
// still no header after 10 s: the engine that sized the file died before writing it. Every engine
|
||||
// fills the arena itself, so nothing in the file is worth keeping: write the header as the creator would
|
||||
std::memset(&hdr, 0, sizeof hdr);
|
||||
std::memcpy(hdr.magic, kSharedArenaMagic, sizeof hdr.magic);
|
||||
hdr.version = 1;
|
||||
hdr.header_bytes = (uint32_t) kSharedArenaHeaderBytes;
|
||||
hdr.arena_bytes = bytes;
|
||||
hdr.pack_hash = shared_pack_hash;
|
||||
got_header = pwrite(fd, &hdr, sizeof hdr, 0);
|
||||
}
|
||||
if (got_header != (ssize_t) sizeof hdr ||
|
||||
std::memcmp(hdr.magic, kSharedArenaMagic, sizeof hdr.magic) != 0 ||
|
||||
hdr.version != 1 || hdr.header_bytes != kSharedArenaHeaderBytes || hdr.arena_bytes != bytes) {
|
||||
@@ -220,6 +381,36 @@ void* reserve(uint64_t bytes, PageBacking& got, std::string& note, const std::st
|
||||
}
|
||||
}
|
||||
|
||||
// tmpfs: take the pages now (ftruncate only sets the length). With /dev/shm too small the load would die with a bus
|
||||
// error part-way; here a full tmpfs is a plain error at start, for every engine that maps the file (pages already
|
||||
// taken cost nothing), and the server then starts its replicas with private copies. A file on a disk stays sparse.
|
||||
{
|
||||
struct statfs sf{};
|
||||
if (fstatfs(fd, &sf) == 0 && (unsigned long) sf.f_type == 0x01021994ul) {
|
||||
// a memfd (or any tmpfs file not capped by a mount size) would take the RAM and then the OOM killer: check the
|
||||
// free RAM and the cgroup's room for the pages that are not there yet before taking them
|
||||
struct stat st2{};
|
||||
const uint64_t have = fstat(fd, &st2) == 0 ? (uint64_t) st2.st_blocks * 512u : 0;
|
||||
const uint64_t need = file_bytes > have ? file_bytes - have : 0;
|
||||
if (need > 0) {
|
||||
const uint64_t room = free_ram_room();
|
||||
if (room != ~uint64_t{0} && need + (2ull << 30) > room) {
|
||||
close(fd);
|
||||
note = "not enough free RAM for the shared arena " + shared_file + ": it needs " +
|
||||
std::to_string((unsigned long long) (need >> 20)) + " MiB more and " +
|
||||
std::to_string((unsigned long long) (room >> 20)) + " MiB are free (2 GiB are kept back)";
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
const int fe = posix_fallocate(fd, 0, (off_t) file_bytes);
|
||||
if (fe != 0 && fe != EOPNOTSUPP && fe != EINVAL) {
|
||||
close(fd);
|
||||
note = "not enough room for the shared arena " + shared_file + " (" +
|
||||
std::to_string((unsigned long long) (file_bytes >> 20)) + " MiB): " + std::strerror(fe);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
void* map = mmap(nullptr, (size_t) file_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
const int e = errno;
|
||||
close(fd);
|
||||
@@ -322,6 +513,20 @@ void release(void* p, uint64_t bytes) {
|
||||
if (!p) return;
|
||||
#ifdef _WIN32
|
||||
(void) bytes;
|
||||
HANDLE section = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_views_mu);
|
||||
auto it = g_views.find(p);
|
||||
if (it != g_views.end()) {
|
||||
section = it->second;
|
||||
g_views.erase(it);
|
||||
}
|
||||
}
|
||||
if (section != nullptr) { // a shared-section view: the pages stay while another engine has the section
|
||||
UnmapViewOfFile(p);
|
||||
CloseHandle(section);
|
||||
return;
|
||||
}
|
||||
VirtualFree(p, 0, MEM_RELEASE);
|
||||
#else
|
||||
munmap(p, bytes);
|
||||
@@ -410,6 +615,9 @@ PinnedArena::PinnedArena(uint64_t bytes, const std::vector<uint64_t>& bounds,
|
||||
uint64_t shared_pack_hash) : capacity(bytes) {
|
||||
if (bytes == 0) return;
|
||||
base = reserve(bytes, backing, note, shared_file, shared_pack_hash, mapping_base, mapping_bytes);
|
||||
if (!shared_file.empty() && base != nullptr && mapping_base != nullptr)
|
||||
shared_state = reinterpret_cast<std::atomic<uint64_t>*>(static_cast<char*>(mapping_base) +
|
||||
offsetof(SharedArenaHeader, reserved));
|
||||
if (base != nullptr && mapping_base == nullptr) {
|
||||
mapping_base = base;
|
||||
mapping_bytes = bytes;
|
||||
@@ -529,6 +737,9 @@ PinnedArena::PinnedArena(uint64_t bytes, ByNeed, const std::vector<std::pair<uin
|
||||
by_need = true;
|
||||
if (bytes == 0) return;
|
||||
base = reserve(bytes, backing, note, shared_file, shared_pack_hash, mapping_base, mapping_bytes);
|
||||
if (!shared_file.empty() && base != nullptr && mapping_base != nullptr)
|
||||
shared_state = reinterpret_cast<std::atomic<uint64_t>*>(static_cast<char*>(mapping_base) +
|
||||
offsetof(SharedArenaHeader, reserved));
|
||||
if (base != nullptr && mapping_base == nullptr) {
|
||||
mapping_base = base;
|
||||
mapping_bytes = bytes;
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
// Windows selftest for the named-section backing of PinnedArena (the replicas' shared expert arena).
|
||||
// Parent: two arenas on one section name (same bytes, header refused for another pack / a bigger size, the load state word),
|
||||
// then a CHILD PROCESS that opens the same section, reads what the parent wrote and writes back; the parent closes its first
|
||||
// arena while the second still holds the section (the pages must stay), and a third arena joins the live section. After the
|
||||
// last close, a new arena with the same name starts empty (the section is gone with its last handle).
|
||||
#ifdef _WIN32
|
||||
#include "strata/core/pinned.hpp"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
constexpr uint64_t kBytes = 64u << 10;
|
||||
constexpr uint64_t kPackHash = 0x0123456789abcdefull;
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
const std::vector<uint64_t> no_bounds;
|
||||
if (argc >= 3 && std::strcmp(argv[1], "child") == 0) {
|
||||
const std::string name = argv[2];
|
||||
strata::core::PinnedArena a(kBytes, no_bounds, 0, name, kPackHash);
|
||||
if (!a.valid()) { std::fprintf(stderr, "child: cannot open: %s\n", a.note.c_str()); return 2; }
|
||||
if (a.data()[123] != 0x5a) { std::fprintf(stderr, "child: did not see the parent's byte\n"); return 3; }
|
||||
if (a.shared_state == nullptr || a.shared_state->load() != 4242u) { std::fprintf(stderr, "child: state word is not the parent's\n"); return 4; }
|
||||
a.data()[456] = 0xa5;
|
||||
a.shared_state->store(~uint64_t{0}); // (filled)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fail = 0;
|
||||
const std::string name = "win:Local\\strata-pinned-test-" + std::to_string((unsigned long long) GetCurrentProcessId());
|
||||
{
|
||||
auto* first = new strata::core::PinnedArena(kBytes, no_bounds, 0, name, kPackHash);
|
||||
if (!first->valid()) {
|
||||
std::fprintf(stderr, "first section failed: %s\n", first->note.c_str());
|
||||
return 1;
|
||||
}
|
||||
first->data()[123] = 0x5a;
|
||||
if (first->shared_state == nullptr) { std::fprintf(stderr, "no state word\n"); fail = 1; }
|
||||
else if (first->shared_state->load() != 0) { std::fprintf(stderr, "a new section's state word is not 0\n"); fail = 1; }
|
||||
else first->shared_state->store(4242u);
|
||||
|
||||
auto* second = new strata::core::PinnedArena(kBytes, no_bounds, 0, name, kPackHash);
|
||||
if (!second->valid()) { std::fprintf(stderr, "second section failed: %s\n", second->note.c_str()); fail = 1; }
|
||||
else {
|
||||
if (second->data()[123] != 0x5a) { std::fprintf(stderr, "same name did not expose the same bytes\n"); fail = 1; }
|
||||
if (second->shared_state == nullptr || second->shared_state->load() != 4242u) { std::fprintf(stderr, "state word not shared\n"); fail = 1; }
|
||||
}
|
||||
{
|
||||
strata::core::PinnedArena wrong_pack(kBytes, no_bounds, 0, name, kPackHash + 1);
|
||||
if (wrong_pack.valid() || wrong_pack.note.find("pack hash") == std::string::npos) {
|
||||
std::fprintf(stderr, "pack mismatch was not refused: %s\n", wrong_pack.note.c_str());
|
||||
fail = 1;
|
||||
}
|
||||
strata::core::PinnedArena wrong_size(kBytes * 2, no_bounds, 0, name, kPackHash);
|
||||
if (wrong_size.valid()) { std::fprintf(stderr, "a bigger size was not refused\n"); fail = 1; }
|
||||
}
|
||||
|
||||
// a child process opens the live section
|
||||
char exe[MAX_PATH];
|
||||
GetModuleFileNameA(nullptr, exe, MAX_PATH);
|
||||
std::string cmd = std::string("\"") + exe + "\" child \"" + name + "\"";
|
||||
STARTUPINFOA si{};
|
||||
si.cb = sizeof si;
|
||||
PROCESS_INFORMATION pi{};
|
||||
std::vector<char> cl(cmd.begin(), cmd.end());
|
||||
cl.push_back('\0');
|
||||
if (!CreateProcessA(nullptr, cl.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi)) {
|
||||
std::fprintf(stderr, "cannot start the child (error %lu)\n", GetLastError());
|
||||
fail = 1;
|
||||
} else {
|
||||
WaitForSingleObject(pi.hProcess, 120000);
|
||||
DWORD code = 99;
|
||||
GetExitCodeProcess(pi.hProcess, &code);
|
||||
CloseHandle(pi.hProcess);
|
||||
CloseHandle(pi.hThread);
|
||||
if (code != 0) { std::fprintf(stderr, "the child failed with %lu\n", code); fail = 1; }
|
||||
if (first->data()[456] != 0xa5) { std::fprintf(stderr, "the child's byte did not arrive\n"); fail = 1; }
|
||||
if (first->shared_state->load() != ~uint64_t{0}) { std::fprintf(stderr, "the child's state word did not arrive\n"); fail = 1; }
|
||||
}
|
||||
|
||||
delete first; // the creator is gone; the second still holds the section
|
||||
if (second != nullptr && second->valid()) {
|
||||
if (second->data()[123] != 0x5a || second->data()[456] != 0xa5) { std::fprintf(stderr, "bytes lost when the first arena closed\n"); fail = 1; }
|
||||
strata::core::PinnedArena third(kBytes, no_bounds, 0, name, kPackHash);
|
||||
if (!third.valid() || third.data()[123] != 0x5a) { std::fprintf(stderr, "a late arena did not join the live section\n"); fail = 1; }
|
||||
}
|
||||
delete second;
|
||||
}
|
||||
{
|
||||
strata::core::PinnedArena again(kBytes, no_bounds, 0, name, kPackHash); // (every handle was closed: a new, empty section)
|
||||
if (!again.valid() || again.data()[123] != 0 || again.shared_state->load() != 0) {
|
||||
std::fprintf(stderr, "the section survived its last handle\n");
|
||||
fail = 1;
|
||||
}
|
||||
}
|
||||
std::printf("pinned_shared_win_test: %s\n", fail ? "FAILED" : "OK");
|
||||
return fail;
|
||||
}
|
||||
#else
|
||||
int main() { return 77; } // (skipped: the Linux build has pinned_shared_test)
|
||||
#endif
|
||||
@@ -5949,9 +5949,15 @@ int64_t Prefill::lm_prepare(int64_t n, std::string& why, int64_t cap) {
|
||||
Impl& m = *impl_;
|
||||
if (m.g == nullptr) { why = "not initialized"; return 0; }
|
||||
#if defined(STRATA_USE_HIP) || defined(STRATA_HIP_GFX906)
|
||||
// only NVIDIA CUDA has run this path; on an AMD build the prompt stays on the chunk-major path
|
||||
why = "layer-major prefill has only been run on NVIDIA CUDA";
|
||||
return 0;
|
||||
// only NVIDIA CUDA has run this path by default; on an AMD build the prompt stays on the chunk-major path unless
|
||||
// STRATA_LM_HIP=1 (opt-in, PAR-A 2026-10-30: gfx1201 is being qualified)
|
||||
{
|
||||
static const bool lm_hip = [] { const char* v = std::getenv("STRATA_LM_HIP"); return v != nullptr && v[0] == '1'; }();
|
||||
if (!lm_hip) {
|
||||
why = "layer-major prefill has only been run on NVIDIA CUDA";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
const core::ModelGeometry& g = *m.g;
|
||||
const core::OnDevice on_device(m.device); // a layer split's later stage is sized on its own GPU
|
||||
|
||||
Reference in new issue
Block a user