mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
integrated gfx11 on Linux: the engine gives the model files' cached pages back while and after it reads the expert arena
On a Radeon 780M (gfx1103) under Linux the expert arena (31.6 GiB of the Q2_0 pack) is filled with buffered reads of experts.bin and registered with the driver (a KFD userptr). The GPU's own buffers take about 12 GiB of the same RAM, and the file cache of the model files the start read fills most of the rest: MemFree 1.6-2.4 GiB and Cached 6.9-7.6 GiB at READY. With that little free memory, broken into small blocks, the kernel's proactive compaction (kcompactd, fragmentation score above its high mark) scans and migrates the node's pages in passes: one right after the fill and, when a pass made no progress, the next one 32 s later (500 ms << COMPACT_MAX_DEFER_SHIFT). A pass that moves pages of the registered arena makes KFD evict the process's queues, and the gc 11 MES firmware never answers the eviction while a dispatch sits in a barrier: the late stalls of an 8K prompt (30-50 s after READY) that the wait before READY does not reach. Passive starts (no request) under a g++ load, settle build, 100 s of idle after READY, the daemon's scan counters read once a second: late compaction passes in 3 of 8 starts (one of them evicted the idle engine's queues, evicted_ms +434 ms); the three have compact_daemon_migrate_scanned moving with compact_stall, compact_daemon_wake and the kswapd counters flat (proactive compaction). In the failing starts of the 8K prompt arm (b) the first late pass came 31-34 s after the end of the fill, in all four. The engine now asks the kernel to drop the cached pages of the model files: posix_fadvise(DONTNEED) on each 8 MiB chunk of experts.bin right after it has been copied into the arena (drop behind), and once per file after the load: the arena's expert files, the pack's dense.bin and embd.bin, the GGUF shards of --native except the PLE table's (its rows are read directly). It is advice for clean pages: a file that is still mapped keeps its mapped pages, a failed call is ignored, nothing is written or deleted. Nothing reads these files after the load but the session fingerprint (the first and last 2 MiB of each model file) and an engine restart, which reads them from the drive again. Automatic on the class of the huge-page and wait-before-READY rules (igpu_cache_drop_default: integrated gfx1103 / gfx1150 / gfx1151 under Linux); Windows, discrete cards and every other device are unchanged. STRATA_NO_ARENA_CACHE_DROP=1 turns it off (research / A-B only, registered as research). One [DRAFT] log line after the load says how much was given back and how long the final call took. The harness version of the final call (posix_fadvise DONTNEED on experts.bin, dense.bin, embd.bin and the native shard right after the line "expert arena read"), same passive starts: late passes 0 of 8, MemFree at READY 8.4-9.3 GiB (1.6-2.4 GiB without), free 2-4 MiB blocks at READY 0.8-3.3 GiB (0.01-0.25 GiB without), READY 23.8-24.6 s (23.7-26.4 s without), the call 0.51-0.55 s. Loaded starts of the finished change (g++ load beside the start, first request at READY + 0 s, 2K and 8K prompts, early in a boot, one Radeon 780M box): 9 of 9 passed with the give-back, 10 of 14 with STRATA_NO_ARENA_CACHE_DROP=1. Tests: tests/core/page_cache_drop_test.cpp (the flag and the switch, a range and a whole temporary file, every error path ignored, the final call over a mixed list, the meminfo reader, the draft line) and the device rule in src/core/arch_defaults_test.cpp; settings register: one hand-added entry (STRATA_NO_ARENA_CACHE_DROP, research).
This commit is contained in:
1 parent
8f6393a373
commit
0ee2639926
11 files changed
+425
-3
No files matched your search
@@ -318,6 +318,10 @@ if(STRATA_BUILD_TESTS)
|
||||
add_executable(kfd_settle_test tests/core/kfd_settle_test.cpp)
|
||||
target_include_directories(kfd_settle_test PRIVATE include)
|
||||
add_test(NAME kfd_settle_test COMMAND kfd_settle_test)
|
||||
# the file-cache give-back during and after the expert load on an integrated AMD GPU (a temporary file; no GPU)
|
||||
add_executable(page_cache_drop_test tests/core/page_cache_drop_test.cpp)
|
||||
target_include_directories(page_cache_drop_test PRIVATE include)
|
||||
add_test(NAME page_cache_drop_test COMMAND page_cache_drop_test)
|
||||
add_executable(logit_bias_test src/core/logit_bias_test.cpp)
|
||||
target_include_directories(logit_bias_test PRIVATE include)
|
||||
add_test(NAME logit_bias_test COMMAND logit_bias_test)
|
||||
|
||||
@@ -20947,6 +20947,39 @@
|
||||
"retire_after": "",
|
||||
"retired_in": ""
|
||||
},
|
||||
{
|
||||
"name": "STRATA_NO_ARENA_CACHE_DROP",
|
||||
"backends": [
|
||||
"cuda-hip"
|
||||
],
|
||||
"reads": [
|
||||
{
|
||||
"file": "include/strata/core/page_cache_drop.hpp",
|
||||
"line": 49,
|
||||
"backend": "cuda-hip",
|
||||
"fn": "getenv"
|
||||
}
|
||||
],
|
||||
"parse": "flag",
|
||||
"default_state": null,
|
||||
"code_default": null,
|
||||
"engine_sets": [],
|
||||
"engine_defaults": [],
|
||||
"launch_files": [],
|
||||
"launch_sets": [],
|
||||
"launch_reads": [],
|
||||
"doc_mentions": [],
|
||||
"first_commit": null,
|
||||
"first_date": null,
|
||||
"since": null,
|
||||
"last_commit": null,
|
||||
"last_date": null,
|
||||
"status": "research",
|
||||
"owner": "core",
|
||||
"note": "turns off the file-cache give-back during and after the expert load on an integrated gfx11 GPU under Linux (posix_fadvise DONTNEED behind the arena read and once per model file; automatic on that class, nothing on other devices); for A/B runs only, not for users",
|
||||
"retire_after": "",
|
||||
"retired_in": ""
|
||||
},
|
||||
{
|
||||
"name": "STRATA_NO_ARENA_THP",
|
||||
"backends": [
|
||||
|
||||
@@ -67,6 +67,9 @@ bool igpu_arena_no_thp_default(const char* gcn_arch, bool integrated, bool windo
|
||||
/// gfx1150, gfx1151), where the registered expert arena is a KFD userptr and the kernel's page moves right after the load evict the
|
||||
/// process's queues. Never Windows, never a discrete card. STRATA_NO_KFD_SETTLE=1 turns the wait off.
|
||||
bool igpu_kfd_settle_default(const char* gcn_arch, bool integrated, bool windows);
|
||||
/// the file-cache give-back during and after the expert load (strata/core/page_cache_drop.hpp) applies to the same class: an integrated
|
||||
/// gfx11 GPU under Linux (gfx1103, gfx1150, gfx1151). Never Windows, never a discrete card. STRATA_NO_ARENA_CACHE_DROP=1 turns it off.
|
||||
bool igpu_cache_drop_default(const char* gcn_arch, bool integrated, bool windows);
|
||||
/// Whether the expert arena skips its MADV_HUGEPAGE request: `no_arena_thp` is STRATA_NO_ARENA_THP (nullptr when unset): "0" asks for the huge
|
||||
/// pages, any other value skips the request; unset leaves it to `igpu_rule` (igpu_arena_no_thp_default for this device). `thp_always`: the
|
||||
/// kernel's THP mode is "always" (the request would add nothing, #771). Inline: src/core/pinned.cu (strata_core) uses it without
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
// give the page cache of the model files back while and after the expert arena is read, on an integrated AMD GPU under Linux.
|
||||
//
|
||||
// Radeon 780M (gfx1103), Linux. The expert arena (31.6 GiB of the Q2_0 pack) is filled with buffered reads of experts.bin and registered with the
|
||||
// driver (a KFD userptr); the GPU's own buffers take about 12 GiB of the same RAM, and the file cache of the model files the start read fills most
|
||||
// of what is left: MemFree 1.6-2.4 GiB, Cached 6.9-7.6 GiB at READY. With free memory that small and that broken up, the kernel's proactive
|
||||
// compaction (kcompactd, fragmentation score above its high mark) scans and migrates the node's pages in passes: one right after the fill and,
|
||||
// when a pass made no progress, the next 32 s later (500 ms << COMPACT_MAX_DEFER_SHIFT). A pass that moves pages of the registered arena makes
|
||||
// KFD evict the process's queues, and the gc 11 MES firmware never answers the eviction while a dispatch sits in a barrier (
|
||||
// passive starts under a g++ load, late passes in 3 of 8; with the file cache given back by the harness at this point, 0 of 8, MemFree at READY
|
||||
// 8.4-9.3 GiB, order 9 / 10 free blocks 0.8-3.3 GiB against 0.01-0.25 GiB).
|
||||
//
|
||||
// So the engine gives the cache back: posix_fadvise(POSIX_FADV_DONTNEED) behind the read position of each chunk of experts.bin as it is copied
|
||||
// into the arena, and once per model file after the load (experts.bin, the pack's dense.bin and embd.bin, the GGUF shards of --native except the
|
||||
// PLE table's, whose rows are read directly). It is advice for clean pages: a file still mapped keeps its mapped pages, a failure is ignored, no
|
||||
// file is written or deleted, and what a later reader needs is read from the drive again. Automatic on the device class (arch_defaults.hpp,
|
||||
// igpu_cache_drop_default); STRATA_NO_ARENA_CACHE_DROP=1 turns it off (research / A-B only).
|
||||
//
|
||||
// Pure pieces, so they can be tested without a GPU: drop_range (one range of an open file), drop_file (a whole file by path), enabled, the
|
||||
// meminfo reader, the draft line.
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace strata::core {
|
||||
|
||||
/// Set by the engine on the device class this is for (arch_defaults.hpp, igpu_cache_drop_default); enabled() is false while it is false.
|
||||
inline std::atomic<bool>& arena_cache_drop_flag() {
|
||||
static std::atomic<bool> f{false};
|
||||
return f;
|
||||
}
|
||||
|
||||
namespace page_cache_drop {
|
||||
|
||||
/// STRATA_NO_ARENA_CACHE_DROP: anything but empty or 0 turns the give-back off.
|
||||
inline bool off_by_env() {
|
||||
const char* e = std::getenv("STRATA_NO_ARENA_CACHE_DROP");
|
||||
return e != nullptr && e[0] != '\0' && e[0] != '0';
|
||||
}
|
||||
|
||||
/// Whether the engine gives the cache back: Linux, the device class set the flag, the switch is not set.
|
||||
inline bool enabled() {
|
||||
#if defined(__linux__)
|
||||
return arena_cache_drop_flag().load(std::memory_order_relaxed) && !off_by_env();
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Bytes the engine has asked the kernel to drop behind its reads so far (drop_range's successful calls).
|
||||
inline std::atomic<uint64_t>& advised_bytes() {
|
||||
static std::atomic<uint64_t> n{0};
|
||||
return n;
|
||||
}
|
||||
|
||||
/// Advice for one range of an open file: its cached clean pages may go (pages that are mapped, dirty or in use stay). False when the range
|
||||
/// is empty, the descriptor is not valid or the kernel refused; never an error for the caller.
|
||||
inline bool drop_range(int fd, uint64_t off, uint64_t len) {
|
||||
#if defined(__linux__)
|
||||
if (fd < 0 || len == 0) return false;
|
||||
if (::posix_fadvise(fd, (off_t) off, (off_t) len, POSIX_FADV_DONTNEED) != 0) return false;
|
||||
advised_bytes().fetch_add(len, std::memory_order_relaxed);
|
||||
return true;
|
||||
#else
|
||||
(void) fd; (void) off; (void) len;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// The same for a whole file by path (opened read-only for this call only). False when it cannot be opened or the kernel refused.
|
||||
inline bool drop_file(const std::string& path) {
|
||||
#if defined(__linux__)
|
||||
if (path.empty()) return false;
|
||||
const int fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) return false;
|
||||
const bool ok = ::posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) == 0; // length 0 = to the end of the file
|
||||
::close(fd);
|
||||
return ok;
|
||||
#else
|
||||
(void) path;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// One value of /proc/meminfo in bytes ("Cached", "MemFree" ...); 0 where it cannot be read.
|
||||
inline uint64_t meminfo_bytes(const char* key) {
|
||||
#if defined(__linux__)
|
||||
std::FILE* f = std::fopen("/proc/meminfo", "r");
|
||||
if (f == nullptr) return 0;
|
||||
char line[256];
|
||||
const size_t n = std::strlen(key);
|
||||
uint64_t v = 0;
|
||||
while (std::fgets(line, sizeof line, f) != nullptr) {
|
||||
if (std::strncmp(line, key, n) == 0 && line[n] == ':') {
|
||||
v = (uint64_t) std::strtoull(line + n + 1, nullptr, 10) * 1024ull;
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::fclose(f);
|
||||
return v;
|
||||
#else
|
||||
(void) key;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
struct Outcome {
|
||||
size_t files = 0; ///< the files the final call went to (empty and repeated paths left out)
|
||||
size_t failed = 0; ///< of them, the calls that did not work (ignored)
|
||||
uint64_t advised_behind = 0; ///< bytes asked back behind the reads before the final call
|
||||
uint64_t cached_before = 0; ///< /proc/meminfo Cached before the final call, bytes
|
||||
uint64_t cached_after = 0; ///< and after it
|
||||
double ms = 0.0; ///< the final call's time
|
||||
};
|
||||
|
||||
/// The final call: every file once. Empty and repeated paths are skipped, a failure is counted and ignored.
|
||||
inline Outcome give_back(const std::vector<std::string>& files) {
|
||||
Outcome o;
|
||||
o.advised_behind = advised_bytes().load(std::memory_order_relaxed);
|
||||
o.cached_before = meminfo_bytes("Cached");
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
std::vector<std::string> seen;
|
||||
for (const std::string& p : files) {
|
||||
if (p.empty()) continue;
|
||||
bool dup = false;
|
||||
for (const std::string& s : seen) dup = dup || s == p;
|
||||
if (dup) continue;
|
||||
seen.push_back(p);
|
||||
++o.files;
|
||||
if (!drop_file(p)) ++o.failed;
|
||||
}
|
||||
o.ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
|
||||
o.cached_after = meminfo_bytes("Cached");
|
||||
return o;
|
||||
}
|
||||
|
||||
/// The one line the engine prints (a [DRAFT] until the owner words it, par/OWNER-WORDING-1111.md): empty when nothing was given back.
|
||||
inline std::string draft_line(const Outcome& o) {
|
||||
const double gib = 1073741824.0;
|
||||
const uint64_t after_call = o.cached_before > o.cached_after ? o.cached_before - o.cached_after : 0;
|
||||
if (o.advised_behind == 0 && after_call == 0) return std::string();
|
||||
char buf[512];
|
||||
std::snprintf(buf, sizeof buf,
|
||||
"[DRAFT] the model files' cached pages were given back after the expert load: %.1f GiB asked back during the read, %.1f GiB "
|
||||
"by the call after it (%.0f ms); the file cache is %.1f GiB now (STRATA_NO_ARENA_CACHE_DROP=1 keeps it)",
|
||||
(double) o.advised_behind / gib, (double) after_call / gib, o.ms, (double) o.cached_after / gib);
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
} // namespace page_cache_drop
|
||||
} // namespace strata::core
|
||||
@@ -175,7 +175,9 @@ LoadStats load_experts_ranges(const std::string& path, uint8_t* dst, const std::
|
||||
/// (an overload, not a default argument: the SYCL build has its own pinned.dp.cpp that defines the signature above only)
|
||||
LoadStats load_experts_ranges(const std::string& path, uint8_t* dst, const std::vector<uint64_t>& layer_off,
|
||||
const std::vector<uint64_t>& layer_bytes, int threads, uint64_t chunk,
|
||||
const std::atomic<int>* ready, const PadMap* pad);
|
||||
const std::atomic<int>* ready, const PadMap* pad, bool drop_behind = false);
|
||||
/// `drop_behind` (Linux only; page_cache_drop.hpp): right after each chunk is copied into the arena, the file cache of that chunk is
|
||||
/// given back (posix_fadvise DONTNEED, advice only: a failure is ignored), so the cache of the file never grows to the arena's size.
|
||||
|
||||
/// The same ranges read UNBUFFERED straight into `dst` (Windows): no staging buffer and no file-cache copy - the
|
||||
/// drive's DMA lands where the experts live. Every range, `dst` and `chunk` must be 4 KiB aligned; returns ok =
|
||||
|
||||
@@ -158,6 +158,10 @@ bool igpu_kfd_settle_default(const char* gcn_arch, bool integrated, bool windows
|
||||
return igpu_arena_no_thp_default(gcn_arch, integrated, windows); // the same device class
|
||||
}
|
||||
|
||||
bool igpu_cache_drop_default(const char* gcn_arch, bool integrated, bool windows) {
|
||||
return igpu_arena_no_thp_default(gcn_arch, integrated, windows); // the same device class
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, std::string>> igpu_default_env(const char* gcn_arch, bool integrated) {
|
||||
std::vector<std::pair<std::string, std::string>> t;
|
||||
if (!integrated || !igpu_prompt_defaults_arch(gcn_arch)) return t;
|
||||
|
||||
@@ -424,6 +424,13 @@ int main() {
|
||||
CHECK(!igpu_kfd_settle_default(g, false, false)); // not integrated
|
||||
}
|
||||
for (const char* g : {"gfx1100", "gfx1201", "gfx90a", "gfx1030", "gfx1152", ""}) CHECK(!igpu_kfd_settle_default(g, true, false));
|
||||
// the file-cache give-back during and after the expert load (page_cache_drop.hpp): the same class
|
||||
for (const char* g : {"gfx1103", "gfx1150", "gfx1151", "gfx1103:sramecc-:xnack-"}) {
|
||||
CHECK(igpu_cache_drop_default(g, true, false));
|
||||
CHECK(!igpu_cache_drop_default(g, true, true)); // Windows
|
||||
CHECK(!igpu_cache_drop_default(g, false, false)); // not integrated
|
||||
}
|
||||
for (const char* g : {"gfx1100", "gfx1201", "gfx90a", "gfx1030", "gfx1152", ""}) CHECK(!igpu_cache_drop_default(g, true, false));
|
||||
// STRATA_NO_ARENA_THP: unset = the device rule decides; "0" = ask for the huge pages (also on such a device); any other value = skip them
|
||||
CHECK(arena_skip_thp_request(nullptr, false, true)); // integrated gfx11, nothing set: skipped
|
||||
CHECK(!arena_skip_thp_request(nullptr, false, false)); // any other device, nothing set: requested (as before)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "strata/core/conversation_memory.hpp"
|
||||
#include "strata/core/foresight_swap.hpp"
|
||||
#include "strata/core/on_device.hpp"
|
||||
#include "strata/core/page_cache_drop.hpp"
|
||||
#include "strata/core/ple_residency.hpp"
|
||||
#include "strata/core/resident_ram_copy.hpp"
|
||||
#include "strata/platform/aux_cpus.hpp"
|
||||
@@ -6313,7 +6314,8 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
|
||||
} else {
|
||||
if (unbuffered && !padded_) st = load_experts_direct(path, a->data(), loff, lbytes, readers, /*chunk=*/8u << 20, rp);
|
||||
if (!unbuffered || padded_ || (!st.ok && st.error.empty())) // unaligned ranges / the padded layout: the buffered reader
|
||||
st = load_experts_ranges(path, a->data(), loff, lbytes, threads, /*chunk=*/8u << 20, rp, padded_ ? &pad_ : nullptr);
|
||||
st = load_experts_ranges(path, a->data(), loff, lbytes, threads, /*chunk=*/8u << 20, rp, padded_ ? &pad_ : nullptr,
|
||||
/*drop_behind=*/strata::core::page_cache_drop::enabled()); // integrated gfx11 under Linux
|
||||
}
|
||||
if (reg.joinable()) reg.join();
|
||||
std::fprintf(stderr, "strata generate: expert arena read %s (%s)\n", unbuffered ? "unbuffered" : "through the file cache",
|
||||
|
||||
+6
-1
@@ -3,6 +3,7 @@
|
||||
#include "strata/platform/memory.hpp"
|
||||
#include "strata/core/pin_budget.hpp"
|
||||
#include "strata/core/arch_defaults.hpp" // arena_skip_thp_request
|
||||
#include "strata/core/page_cache_drop.hpp" // drop_range (the drop behind the arena read, Linux)
|
||||
#include <cstddef>
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
@@ -1181,7 +1182,7 @@ LoadStats load_experts_ranges(const std::string& path, uint8_t* dst, const std::
|
||||
|
||||
LoadStats load_experts_ranges(const std::string& path, uint8_t* dst, const std::vector<uint64_t>& layer_off,
|
||||
const std::vector<uint64_t>& layer_bytes, int threads, uint64_t chunk,
|
||||
const std::atomic<int>* ready, const PadMap* pad) {
|
||||
const std::atomic<int>* ready, const PadMap* pad, bool drop_behind) {
|
||||
LoadStats st;
|
||||
const uint64_t layers = (uint64_t) layer_off.size();
|
||||
st.layers = layers;
|
||||
@@ -1264,6 +1265,10 @@ LoadStats load_experts_ranges(const std::string& path, uint8_t* dst, const std::
|
||||
h = fnv1a64(buf.data(), n, h);
|
||||
copy_ns += (uint64_t) std::chrono::duration_cast<std::chrono::nanoseconds>(
|
||||
std::chrono::steady_clock::now() - t_copy).count();
|
||||
#if defined(__linux__)
|
||||
// this chunk is in the arena now: its file-cache pages are only a second copy. Advice; a failure is ignored.
|
||||
if (drop_behind) (void) strata::core::page_cache_drop::drop_range(::fileno(f), off + pos, n);
|
||||
#endif
|
||||
pos += n;
|
||||
remaining -= n;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "strata/core/arch_defaults.hpp"
|
||||
#include "strata/core/reader_room.hpp"
|
||||
#include "strata/core/kfd_settle.hpp"
|
||||
#include "strata/core/page_cache_drop.hpp"
|
||||
#include "strata/core/gfx12_chunk.hpp"
|
||||
#include "strata/core/nv_chunk.hpp"
|
||||
#include "strata/core/nv_defaults.hpp"
|
||||
@@ -3988,6 +3989,10 @@ int main(int argc, char** argv) {
|
||||
// and before READY it waits until its own KFD eviction counter has been quiet for 2 s (kfd_settle.hpp)
|
||||
if (strata::core::igpu_kfd_settle_default(ad.gcnArchName, all_igpu, win_host))
|
||||
strata::core::kfd_settle_flag().store(true);
|
||||
// and the file cache of the model files is given back while the expert arena is read and after it (page_cache_drop.hpp):
|
||||
// the little free memory left beside the arena, the GPU's buffers and that cache is what makes the kernel's proactive compaction run
|
||||
if (strata::core::igpu_cache_drop_default(ad.gcnArchName, all_igpu, win_host))
|
||||
strata::core::arena_cache_drop_flag().store(true);
|
||||
// gfx1151 on Windows (#1630 capped a bare `--prefill auto` at 6144 for a 7900 XTX's commit charge): the Strix Halo has its
|
||||
// 8192 chunk back. IQ3_S, 8K prompt: 691 -> 787 tok/s (one chunk streams 1,785 experts instead of 2,782), 64K 713 -> 748;
|
||||
// 4K is one chunk already. STRATA_GFX1151_CHUNK=0 (or STRATA_GFX1151_DEFAULTS=0) keeps 6144.
|
||||
@@ -7979,6 +7984,23 @@ int main(int argc, char** argv) {
|
||||
std::fprintf(stderr, "strata generate: loaded %.2f GiB at %.2f GiB/s\n",
|
||||
(double) strata::kernels::cpu::expert_layout().total / (1024.0 * 1024 * 1024),
|
||||
arena_src.load_gib_per_second());
|
||||
#if defined(__linux__)
|
||||
// integrated gfx11 under Linux: the experts are in the arena now; the model files' cached pages are a second copy. The reads of
|
||||
// experts.bin gave theirs back as they went (drop behind); this is the call after it, for every file the start read: the arena's expert files,
|
||||
// the pack's dense.bin and embd.bin, the GGUF shards of --native (not the PLE table's: its rows are read directly). The weights, the native
|
||||
// projections and the head were loaded before the arena (engine log order); the PLE table is read from the drive on request. Advice only.
|
||||
if (strata::core::page_cache_drop::enabled()) {
|
||||
std::vector<std::string> cached_files;
|
||||
for (const auto& kv : arena_src.model_inputs()) cached_files.push_back(kv.second);
|
||||
if (!o.pack.empty())
|
||||
for (const char* n : {"dense.bin", "embd.bin"}) cached_files.push_back(o.pack + "/" + n);
|
||||
for (const std::string& s : o.native_shards)
|
||||
if (s != o.ple_gguf) cached_files.push_back(s);
|
||||
const auto outcome = strata::core::page_cache_drop::give_back(cached_files);
|
||||
const std::string line = strata::core::page_cache_drop::draft_line(outcome);
|
||||
if (!line.empty()) std::fprintf(stderr, "strata generate: %s\n", line.c_str());
|
||||
}
|
||||
#endif
|
||||
// A rate under ~0.2 GiB/s is not the hardware. Task Scheduler / service contexts throttle this
|
||||
// read+fill about 24x (measured 0.05 vs 1.42 GiB/s for the same binary, args and cache state; the
|
||||
// scheduler's defaults - Below normal priority and a least-privilege token - were the only
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
// the file-cache give-back during and after the expert load (include/strata/core/page_cache_drop.hpp). No GPU. Cases: the flag and the
|
||||
// switch, one range and one whole file of a temporary file, every error path (empty path, missing file, bad descriptor, empty range) ignored,
|
||||
// the final call over a mixed list (a missing file and a repeated path among good ones), the meminfo reader, the draft line. (The device rule
|
||||
// is checked in src/core/arch_defaults_test.cpp.)
|
||||
#include "strata/core/page_cache_drop.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
using namespace strata::core;
|
||||
namespace pcd = strata::core::page_cache_drop;
|
||||
|
||||
namespace {
|
||||
int failed = 0;
|
||||
#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
|
||||
|
||||
#if defined(__linux__)
|
||||
// a temporary file of `bytes` bytes of a pattern, written through, flushed and read back once (so its pages are clean and cached); "" on failure
|
||||
std::string make_file(size_t bytes) {
|
||||
char path[] = "/tmp/page_cache_drop_test_XXXXXX";
|
||||
const int fd = ::mkstemp(path);
|
||||
if (fd < 0) return std::string();
|
||||
std::vector<unsigned char> buf(bytes);
|
||||
for (size_t i = 0; i < bytes; ++i) buf[i] = (unsigned char) (i * 131 + 7);
|
||||
bool ok = ::write(fd, buf.data(), bytes) == (ssize_t) bytes && ::fdatasync(fd) == 0;
|
||||
std::vector<unsigned char> back(bytes);
|
||||
ok = ok && ::pread(fd, back.data(), bytes, 0) == (ssize_t) bytes;
|
||||
::close(fd);
|
||||
if (!ok) { ::unlink(path); return std::string(); }
|
||||
return std::string(path);
|
||||
}
|
||||
// how many pages of the file are resident (mincore); -1 when it cannot be told
|
||||
long resident_pages(const std::string& path) {
|
||||
const int fd = ::open(path.c_str(), O_RDONLY);
|
||||
if (fd < 0) return -1;
|
||||
struct stat st {};
|
||||
if (::fstat(fd, &st) != 0 || st.st_size == 0) { ::close(fd); return -1; }
|
||||
void* m = ::mmap(nullptr, (size_t) st.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (m == MAP_FAILED) { ::close(fd); return -1; }
|
||||
const size_t page = (size_t) ::sysconf(_SC_PAGESIZE);
|
||||
const size_t n = ((size_t) st.st_size + page - 1) / page;
|
||||
std::vector<unsigned char> vec(n);
|
||||
long r = ::mincore(m, (size_t) st.st_size, vec.data()) == 0 ? 0 : -1;
|
||||
if (r == 0) for (unsigned char c : vec) r += c & 1;
|
||||
::munmap(m, (size_t) st.st_size);
|
||||
::close(fd);
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
// 1. the flag and the switch
|
||||
{
|
||||
arena_cache_drop_flag().store(false);
|
||||
CHECK(!pcd::enabled());
|
||||
#if defined(__linux__)
|
||||
::unsetenv("STRATA_NO_ARENA_CACHE_DROP");
|
||||
arena_cache_drop_flag().store(true);
|
||||
CHECK(pcd::enabled());
|
||||
::setenv("STRATA_NO_ARENA_CACHE_DROP", "1", 1);
|
||||
CHECK(!pcd::enabled() && pcd::off_by_env());
|
||||
::setenv("STRATA_NO_ARENA_CACHE_DROP", "0", 1);
|
||||
CHECK(pcd::enabled() && !pcd::off_by_env());
|
||||
::setenv("STRATA_NO_ARENA_CACHE_DROP", "", 1);
|
||||
CHECK(pcd::enabled() && !pcd::off_by_env());
|
||||
::unsetenv("STRATA_NO_ARENA_CACHE_DROP");
|
||||
#else
|
||||
arena_cache_drop_flag().store(true);
|
||||
CHECK(!pcd::enabled()); // never off Linux
|
||||
#endif
|
||||
arena_cache_drop_flag().store(false);
|
||||
}
|
||||
#if defined(__linux__)
|
||||
// 2. one range and one whole file of a real file: the calls work, the advised bytes add up, the file is unchanged
|
||||
{
|
||||
const size_t sz = 4u << 20;
|
||||
const std::string p = make_file(sz);
|
||||
CHECK(!p.empty());
|
||||
if (!p.empty()) {
|
||||
const int fd = ::open(p.c_str(), O_RDONLY);
|
||||
CHECK(fd >= 0);
|
||||
const uint64_t a0 = pcd::advised_bytes().load();
|
||||
CHECK(pcd::drop_range(fd, 0, 1u << 20));
|
||||
CHECK(pcd::advised_bytes().load() == a0 + (1u << 20));
|
||||
CHECK(pcd::drop_range(fd, 1u << 20, sz - (1u << 20)));
|
||||
CHECK(pcd::advised_bytes().load() == a0 + sz);
|
||||
CHECK(pcd::drop_range(fd, 100u << 20, 4096)); // beyond the end: advice, not an error
|
||||
const long before = resident_pages(p);
|
||||
CHECK(pcd::drop_file(p));
|
||||
const long after = resident_pages(p);
|
||||
CHECK(before < 0 || after < 0 || after <= before); // (tmpfs keeps its pages: only "not more" is required)
|
||||
std::vector<unsigned char> back(sz);
|
||||
CHECK(::pread(fd, back.data(), sz, 0) == (ssize_t) sz);
|
||||
bool same = true;
|
||||
for (size_t i = 0; i < sz && same; ++i) same = back[i] == (unsigned char) (i * 131 + 7);
|
||||
CHECK(same); // what a later reader needs is read again, unchanged
|
||||
::close(fd);
|
||||
::unlink(p.c_str());
|
||||
}
|
||||
}
|
||||
// 3. every error path is ignored and returns false; nothing is added to the advised bytes
|
||||
{
|
||||
const uint64_t a0 = pcd::advised_bytes().load();
|
||||
CHECK(!pcd::drop_range(-1, 0, 4096));
|
||||
CHECK(!pcd::drop_range(0x7fffffff, 0, 4096)); // not an open descriptor
|
||||
const std::string p = make_file(8192);
|
||||
const int fd = p.empty() ? -1 : ::open(p.c_str(), O_RDONLY);
|
||||
CHECK(fd >= 0);
|
||||
CHECK(!pcd::drop_range(fd, 0, 0)); // an empty range
|
||||
::close(fd);
|
||||
CHECK(!pcd::drop_range(fd, 0, 4096)); // closed
|
||||
if (!p.empty()) ::unlink(p.c_str());
|
||||
CHECK(pcd::advised_bytes().load() == a0);
|
||||
CHECK(!pcd::drop_file(""));
|
||||
CHECK(!pcd::drop_file("/nonexistent/page_cache_drop_test/file.bin"));
|
||||
CHECK(!pcd::drop_file("/proc/self/mem/x")); // a path that cannot be opened
|
||||
}
|
||||
// 4. the final call over a mixed list: a missing file, an empty path and a repeated path among good ones
|
||||
{
|
||||
const std::string a = make_file(1u << 20), b = make_file(1u << 20);
|
||||
CHECK(!a.empty() && !b.empty());
|
||||
const pcd::Outcome o = pcd::give_back({a, "/nonexistent/page_cache_drop_test/x.bin", "", b, a});
|
||||
CHECK(o.files == 3); // a, the missing one, b (the empty path and the repeat are left out)
|
||||
CHECK(o.failed == 1); // the missing one, ignored
|
||||
CHECK(o.ms >= 0.0);
|
||||
CHECK(o.cached_before > 0 && o.cached_after > 0); // /proc/meminfo is readable
|
||||
const pcd::Outcome none = pcd::give_back({});
|
||||
CHECK(none.files == 0 && none.failed == 0);
|
||||
if (!a.empty()) ::unlink(a.c_str());
|
||||
if (!b.empty()) ::unlink(b.c_str());
|
||||
}
|
||||
// 5. the meminfo reader
|
||||
{
|
||||
CHECK(pcd::meminfo_bytes("Cached") > 0);
|
||||
CHECK(pcd::meminfo_bytes("MemFree") > 0);
|
||||
CHECK(pcd::meminfo_bytes("NoSuchKey") == 0);
|
||||
CHECK(pcd::meminfo_bytes("Cach") == 0); // a prefix is not a key
|
||||
}
|
||||
#endif
|
||||
// 6. the draft line: nothing when nothing was given back, a [DRAFT] otherwise, with the numbers and the switch
|
||||
{
|
||||
pcd::Outcome none;
|
||||
CHECK(pcd::draft_line(none).empty());
|
||||
pcd::Outcome o;
|
||||
o.advised_behind = 9ull << 30;
|
||||
o.cached_before = 7ull << 30;
|
||||
o.cached_after = 1ull << 30;
|
||||
o.ms = 520.0;
|
||||
const std::string s = pcd::draft_line(o);
|
||||
CHECK(s.rfind("[DRAFT] ", 0) == 0);
|
||||
CHECK(s.find("9.0 GiB") != std::string::npos);
|
||||
CHECK(s.find("6.0 GiB") != std::string::npos);
|
||||
CHECK(s.find("520 ms") != std::string::npos);
|
||||
CHECK(s.find("STRATA_NO_ARENA_CACHE_DROP=1") != std::string::npos);
|
||||
pcd::Outcome grew; // the cache grew meanwhile and nothing was advised behind: nothing to say
|
||||
grew.cached_before = 1ull << 30;
|
||||
grew.cached_after = 2ull << 30;
|
||||
CHECK(pcd::draft_line(grew).empty());
|
||||
}
|
||||
if (failed != 0) {
|
||||
std::fprintf(stderr, "page_cache_drop_test: %d check(s) failed\n", failed);
|
||||
return 1;
|
||||
}
|
||||
std::printf("page_cache_drop_test: ok\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user