mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 23:08:36 +00:00
[Engine] retain benefit closure reason for periodic prefetch retry
This commit is contained in:
1 parent
5e8a4cca7d
commit
48f6fd4f68
3 files changed
+162
-1
No files matched your search
@@ -272,6 +272,8 @@ When experts are read from GGUF files in place, Linux runs three things by defau
|
||||
| io prefetch | `STRATA_IO_PREFETCH=1` (`0` = off) | I/O threads read the layer's uncached experts and the router-predicted ones of the next layers with whole-blob `pread`s into the page cache. Tunables: `STRATA_LOOKAHEAD_K`, `STRATA_IO_PREFETCH_DEPTH` (2), `STRATA_IO_PF_THREADS` (8), `STRATA_IO_PF_STAGE=1`, `STRATA_IO_PF_AHEAD=0`. |
|
||||
| Decode gate | `STRATA_IO_PF_GATE=1` (`0` = off) | Every 16 decode windows the read-ahead is judged. It runs only while the page cache has room for the experts (off below 0.55, on above 0.65: `STRATA_IO_PF_GATE_LO` and `_HI`) and while it pays (`STRATA_IO_PF_GATE_USEFUL`). |
|
||||
|
||||
A closure for low prefetch benefit retries after 256 decode windows while capacity stays at or above the low watermark. A capacity drop below that watermark cancels the retry; reopening then requires the high watermark to be exceeded.
|
||||
|
||||
For an `experts.bin` pack, io prefetch and the gate switch on when the page cache has room for at least 55% of the experts
|
||||
(the tightest cgroup limit or `MemAvailable`, asked once at start). Below that the pack reads as before and the log says so;
|
||||
`STRATA_IO_PREFETCH=1` forces it. The container RAM check counts a cgroup's clean page cache as available. The startup log
|
||||
|
||||
@@ -1988,7 +1988,9 @@ void FileExpertSource::gate_capacity() {
|
||||
gate_prev_used_ = used;
|
||||
if (dpf >= (64ull << 20)) gate_useful_ = (double) dused / (double) dpf;
|
||||
const bool open = gate_open_.load(std::memory_order_relaxed);
|
||||
const bool room_ok = open ? gate_ratio_ >= gate_lo_ : gate_ratio_ > gate_hi_;
|
||||
// A benefit-only closure must still get its periodic probe in the hysteresis band.
|
||||
// Only a real capacity closure needs the high watermark before reopening.
|
||||
const bool room_ok = (open || gate_useless_) ? gate_ratio_ >= gate_lo_ : gate_ratio_ > gate_hi_;
|
||||
bool want = room_ok;
|
||||
// too little to read ahead (< 128 MB in 16 windows: the RAM and VRAM tiers hold what is predicted) costs the look-ahead's
|
||||
// second layer and the I/O threads for nothing (an 18 GiB RAM tier: 2.5 GB read ahead in a run, -9..-16% decode)
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Exercise the real gate decision with synthetic inputs, without a GPU.
|
||||
|
||||
The method is extracted verbatim from expert_source.cpp and compiled with a
|
||||
minimal class scaffold. This checks controller transitions, not GPU throughput.
|
||||
Run with Python 3 and a C++17 compiler (CXX, default c++).
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
|
||||
def method(text):
|
||||
start = text.index('void FileExpertSource::gate_capacity() {')
|
||||
depth = 0
|
||||
for end in range(text.index('{', start), len(text)):
|
||||
depth += (text[end] == '{') - (text[end] == '}')
|
||||
if depth == 0:
|
||||
return text[start:end + 1]
|
||||
raise ValueError('Unclosed gate method')
|
||||
|
||||
|
||||
SCAFFOLD = r'''
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
static uint64_t room_bytes = 0;
|
||||
static uint64_t gate_cache_room() { return room_bytes; }
|
||||
class FileExpertSource {
|
||||
public:
|
||||
bool gate_on_ = true;
|
||||
double gate_lo_ = .55, gate_hi_ = .65, gate_ratio_ = -1;
|
||||
int64_t gate_win_ = 0, gate_useless_since_ = 0;
|
||||
uint64_t gate_prev_pf_ = 0, gate_prev_used_ = 0;
|
||||
double gate_useful_ = 1, gate_min_useful_ = .10;
|
||||
bool gate_useless_ = false;
|
||||
std::atomic<bool> gate_open_{true};
|
||||
std::atomic<uint64_t> io_pf_bytes_{0}, io_pf_used_bytes_{0};
|
||||
int64_t gate_flips_ = 0;
|
||||
uint64_t expert_bytes() const { return 1000000000; }
|
||||
void gate_capacity();
|
||||
};
|
||||
'''
|
||||
|
||||
CASES = r'''
|
||||
int failures = 0, checks = 0;
|
||||
void check(bool passed, const char* name) {
|
||||
++checks;
|
||||
std::cout << (passed ? "PASS " : "FAIL ") << name << '\n';
|
||||
failures += !passed;
|
||||
}
|
||||
void trajectory(const char* name, double ratio, bool useful, bool capacity, bool low_volume,
|
||||
bool expect_retry, bool expect_open = false) {
|
||||
FileExpertSource g;
|
||||
int closed = -1, reopened = -1;
|
||||
for (int w = 0; w < 3000; ++w) {
|
||||
room_bytes = uint64_t((capacity && w < 32 ? .50 : ratio) * g.expert_bytes());
|
||||
if (g.gate_open_) {
|
||||
const uint64_t n = low_volume ? 1ull << 20 : 16ull << 20;
|
||||
g.io_pf_bytes_ += n;
|
||||
g.io_pf_used_bytes_ += useful ? n / 4 : n / 32;
|
||||
}
|
||||
const bool before = g.gate_open_;
|
||||
g.gate_capacity();
|
||||
if (before && !g.gate_open_ && closed < 0) closed = w;
|
||||
if (!before && g.gate_open_ && reopened < 0) reopened = w;
|
||||
}
|
||||
check(expect_open ? closed < 0 : expect_retry ? reopened >= 0 : closed >= 0 && reopened < 0, name);
|
||||
if (expect_retry && !capacity) check(reopened == 272, "benefit retry occurs after 256 windows");
|
||||
}
|
||||
void decision(FileExpertSource& g, uint64_t room, int64_t window) {
|
||||
room_bytes = room;
|
||||
g.gate_win_ = window;
|
||||
g.io_pf_bytes_ += 256ull << 20;
|
||||
g.io_pf_used_bytes_ += 64ull << 20;
|
||||
g.gate_capacity();
|
||||
}
|
||||
int main() {
|
||||
trajectory("low usefulness at .64", .64, false, false, false, true);
|
||||
trajectory("low usefulness at .62", .62, false, false, false, true);
|
||||
trajectory("low usefulness at .70", .70, false, false, false, true);
|
||||
trajectory("low volume at .64", .64, true, false, true, true);
|
||||
trajectory("low volume at .70", .70, true, false, true, true);
|
||||
trajectory("useful work at .64", .64, true, false, false, false, true);
|
||||
trajectory("capacity recovery at .62", .62, true, true, false, false);
|
||||
trajectory("capacity recovery at .70", .70, true, true, false, true);
|
||||
trajectory("benefit retry at low boundary", .55, false, false, false, true);
|
||||
trajectory("benefit retry at high boundary", .65, false, false, false, true);
|
||||
trajectory("capacity recovery exactly at high boundary", .65, true, true, false, false);
|
||||
|
||||
FileExpertSource g;
|
||||
decision(g, 550000000, 32);
|
||||
check(g.gate_open_, "exact low watermark remains open");
|
||||
decision(g, 549999999, 48);
|
||||
check(!g.gate_open_ && !g.gate_useless_, "below low watermark is capacity closure");
|
||||
decision(g, 650000000, 64);
|
||||
check(!g.gate_open_, "capacity closure needs more than high watermark");
|
||||
decision(g, 650000001, 80);
|
||||
check(g.gate_open_, "above high watermark recovers capacity closure");
|
||||
|
||||
FileExpertSource pending;
|
||||
pending.gate_open_ = false;
|
||||
pending.gate_useless_ = true;
|
||||
pending.gate_useless_since_ = 17;
|
||||
decision(pending, 620000000, 256);
|
||||
check(!pending.gate_open_ && pending.gate_useless_, "benefit closure retains reason before retry");
|
||||
decision(pending, 540000000, 272);
|
||||
check(!pending.gate_open_ && !pending.gate_useless_, "capacity dip cancels pending benefit retry");
|
||||
decision(pending, 620000000, 544);
|
||||
check(!pending.gate_open_, "capacity dip cannot be bypassed by elapsed retry time");
|
||||
decision(pending, 700000000, 560);
|
||||
check(pending.gate_open_, "capacity dip eventually recovers above high watermark");
|
||||
|
||||
FileExpertSource repeated;
|
||||
repeated.gate_open_ = false;
|
||||
repeated.gate_useless_ = true;
|
||||
repeated.gate_useless_since_ = 17;
|
||||
decision(repeated, 620000000, 272);
|
||||
check(repeated.gate_open_ && !repeated.gate_useless_, "retry clears benefit closure reason");
|
||||
repeated.gate_win_ = 288;
|
||||
repeated.gate_capacity(); // no new prefetch: low-volume close again
|
||||
check(!repeated.gate_open_ && repeated.gate_useless_, "unproductive probe can close again");
|
||||
decision(repeated, 620000000, 544);
|
||||
check(repeated.gate_open_, "a second benefit closure gets a second retry");
|
||||
|
||||
FileExpertSource unknown;
|
||||
decision(unknown, 0, 32);
|
||||
check(unknown.gate_open_ && unknown.gate_flips_ == 0, "unknown room leaves state unchanged");
|
||||
unknown.gate_on_ = false;
|
||||
decision(unknown, 100000000, 48);
|
||||
check(unknown.gate_open_ && unknown.gate_flips_ == 0, "disabled gate does not change state");
|
||||
std::cout << checks << " checks, " << failures << " failures\n";
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--source', type=Path, default=Path(__file__).resolve().parents[1] / 'src/core/expert_source.cpp')
|
||||
args = parser.parse_args()
|
||||
code = method(args.source.read_text())
|
||||
print('decision SHA-256:', hashlib.sha256(code.encode()).hexdigest(), flush=True)
|
||||
with tempfile.TemporaryDirectory(prefix='strata-prefetch-gate-') as temporary:
|
||||
root = Path(temporary)
|
||||
cpp, binary = root / 'gate.cpp', root / 'gate'
|
||||
cpp.write_text(SCAFFOLD + '\n' + code + '\n' + CASES)
|
||||
subprocess.run([os.environ.get('CXX', 'c++'), '-std=c++17', '-O2', '-Wall', '-Wextra',
|
||||
str(cpp), '-o', str(binary)], check=True)
|
||||
return subprocess.run([str(binary)]).returncode
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user