Load from slow or remote stores: retrying reads, n-gram table in its own memory, container thread default
A RunPod network volume (S3-backed FUSE) refuses reads now and then with ENOMEM or EIO, which made the expert load fail at start. The new strata/platform/robust_read.hpp retries such reads, halves the number of parallel readers while the store refuses and raises it again afterwards. The expert loader reads through it, prints progress, and names the file, offset and error on failure. --ple-io ram now reads the n-gram table into anonymous memory with 16 readers instead of locking the file mapping, which needs ulimit -l and faulted pages back from the store. The front end's default thread count comes from the container's CPU quota when there is one: a pod showed 256 host CPUs with a 32-CPU quota. Verified on a 3090 pod: the model loads in normal mode straight from the volume in 12 minutes, through two refused reads; from local disk in 19 s. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
d0a44ea7e7
commit
9a53e42de4
4 files changed
+274
-26
No files matched your search
+38
-2
@@ -14,6 +14,7 @@
|
||||
|
||||
#include <cpp-httplib/httplib.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
@@ -55,8 +56,8 @@ void usage() {
|
||||
" --pack DIR the prepared pack for that model\n"
|
||||
" --mtp DIR the prepared draft head\n"
|
||||
" --ctx N context length in tokens (default 32768)\n"
|
||||
" --threads N CPU threads for the experts the GPU does not hold (default: every physical core;\n"
|
||||
" set it inside a container, where the host's core count is not yours)\n"
|
||||
" --threads N CPU threads for the experts the GPU does not hold (default: every physical core,\n"
|
||||
" or half the CPU quota inside a container that has one)\n"
|
||||
" --host ADDR address to listen on (default 127.0.0.1)\n"
|
||||
" --port N port to listen on (default 8080)\n\n"
|
||||
"Serves POST /v1/chat/completions (OpenAI style, with streaming), GET /v1/models and GET /health.\n");
|
||||
@@ -516,6 +517,35 @@ void quiet_log(ggml_log_level level, const char* text, void*) {
|
||||
if (level == GGML_LOG_LEVEL_ERROR || level == GGML_LOG_LEVEL_WARN) std::fputs(text, stderr);
|
||||
}
|
||||
|
||||
// The CPU expert workers to ask the engine for when --threads is not given: 0 lets the engine use every physical
|
||||
// core it sees. Inside a container it sees the host's cores, not its own share, and that many workers on a small
|
||||
// quota throttle one another; so with a cgroup CPU quota below the visible CPUs the quota decides. The quota
|
||||
// counts hardware threads and the engine wants cores, hence the halving when SMT is on.
|
||||
int default_threads() {
|
||||
long long quota = 0, period = 0;
|
||||
char q[32] = "";
|
||||
if (FILE* f = std::fopen("/sys/fs/cgroup/cpu.max", "r")) { // cgroup v2
|
||||
if (std::fscanf(f, "%31s %lld", q, &period) == 2 && std::strcmp(q, "max") != 0) quota = std::atoll(q);
|
||||
std::fclose(f);
|
||||
} else if (FILE* f1 = std::fopen("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", "r")) { // cgroup v1
|
||||
if (std::fscanf(f1, "%lld", "a) != 1) quota = 0;
|
||||
std::fclose(f1);
|
||||
if (FILE* f2 = std::fopen("/sys/fs/cgroup/cpu/cpu.cfs_period_us", "r")) {
|
||||
if (std::fscanf(f2, "%lld", &period) != 1) period = 0;
|
||||
std::fclose(f2);
|
||||
}
|
||||
}
|
||||
if (quota <= 0 || period <= 0) return 0;
|
||||
const long long cpus = (quota + period / 2) / period;
|
||||
if (cpus < 1 || cpus >= sysconf(_SC_NPROCESSORS_ONLN)) return 0;
|
||||
int smt = 0;
|
||||
if (FILE* f = std::fopen("/sys/devices/system/cpu/smt/active", "r")) {
|
||||
if (std::fscanf(f, "%d", &smt) != 1) smt = 0;
|
||||
std::fclose(f);
|
||||
}
|
||||
return (int) std::max<long long>(smt ? cpus / 2 : cpus, 1);
|
||||
}
|
||||
|
||||
bool parse_args(int argc, char** argv, Options& o) {
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
const std::string a = argv[i];
|
||||
@@ -578,6 +608,12 @@ int main(int argc, char** argv) {
|
||||
"--expert-profile", profile, "--expert-cache", "auto", "--prefill", "auto", "--spec", "4", "--spec-min-p", "0.5",
|
||||
"--mtp", o.mtp, "--max-context", std::to_string(o.ctx), "--ple-io", "ram"};
|
||||
if (o.ctx > 8192) { eargs.push_back("--kv"); eargs.push_back("int8"); }
|
||||
if (o.threads <= 0) {
|
||||
o.threads = default_threads();
|
||||
if (o.threads > 0)
|
||||
std::fprintf(stderr, "unbiased-strata: this container's CPU quota gives %d expert threads (--threads N overrides)\n",
|
||||
o.threads);
|
||||
}
|
||||
if (o.threads > 0) { eargs.push_back("--pool-workers"); eargs.push_back(std::to_string(o.threads)); }
|
||||
for (const std::string& e : o.extra) eargs.push_back(e);
|
||||
std::signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
// strata/platform/robust_read.hpp - positional reads that survive a slow or remote file system.
|
||||
//
|
||||
// A model on a network store (an S3-backed FUSE mount, NFS) can refuse a read for a moment where a local SSD never
|
||||
// does: ENOMEM from the mount's own buffer limit when many readers are open at once, EIO or a time-out from the
|
||||
// link. `read_at` retries those with a growing pause, and through a shared `ReadGate` halves how many readers run
|
||||
// at once each time one of them is refused. `read_range` loads one contiguous range with several readers, each
|
||||
// on a handle of its own, because such a store's speed is per stream.
|
||||
#pragma once
|
||||
|
||||
#if !defined(_WIN32)
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace strata::platform {
|
||||
|
||||
/// `pread`'s signature; the tests pass a function that fails on purpose.
|
||||
using PreadFn = ssize_t (*)(int, void*, size_t, off_t);
|
||||
|
||||
/// How many readers may be inside a read at once. It starts at the reader count, is halved when the store refuses
|
||||
/// a read, and climbs back by one after every `kRecover` reads in a row that worked.
|
||||
class ReadGate {
|
||||
public:
|
||||
explicit ReadGate(int readers) : limit_(std::max(readers, 1)), most_(limit_) {}
|
||||
|
||||
/// Waits for a place and returns the limit in force at that moment (what `refused` wants back).
|
||||
int enter() {
|
||||
std::unique_lock<std::mutex> lk(mu_);
|
||||
cv_.wait(lk, [&] { return active_ < limit_; });
|
||||
++active_;
|
||||
return limit_;
|
||||
}
|
||||
/// `ok`: the read returned bytes.
|
||||
void leave(bool ok = true) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
--active_;
|
||||
if (!ok) good_ = 0;
|
||||
else if (limit_ < most_ && ++good_ >= kRecover) {
|
||||
++limit_;
|
||||
good_ = 0;
|
||||
}
|
||||
}
|
||||
cv_.notify_all();
|
||||
}
|
||||
/// A read that entered under `seen` was refused. Halves the limit once per burst: readers refused together
|
||||
/// all saw the same limit, and only the first of them lowers it. Returns the new limit, 0 if unchanged.
|
||||
int refused(int seen) {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
if (seen != limit_ || limit_ == 1) return 0;
|
||||
limit_ = std::max(limit_ / 2, 1);
|
||||
good_ = 0;
|
||||
return limit_;
|
||||
}
|
||||
int limit() {
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
return limit_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex mu_;
|
||||
std::condition_variable cv_;
|
||||
static constexpr int kRecover = 32;
|
||||
int limit_;
|
||||
int most_;
|
||||
int active_ = 0;
|
||||
int good_ = 0;
|
||||
};
|
||||
|
||||
/// The errors a remote store gives for a read that works when asked again.
|
||||
inline bool read_error_is_transient(int e) {
|
||||
switch (e) {
|
||||
case ENOMEM: case EAGAIN: case EIO: case ETIMEDOUT: case ENOBUFS: case EBUSY: case ESTALE:
|
||||
case ECONNRESET: case ECONNABORTED: case ENETDOWN: case ENETUNREACH: case EHOSTUNREACH: case ENOTCONN:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
inline std::string read_error(const std::string& path, uint64_t off, int e) {
|
||||
return "cannot read " + path + " at offset " + std::to_string(off) + ": " +
|
||||
(e == ENODATA ? std::string("the file ends before that") : std::string(std::strerror(e)));
|
||||
}
|
||||
|
||||
/// Reads exactly `n` bytes at `off`. Returns 0, or the errno of the read that could not be completed: ENODATA if
|
||||
/// the file ends early, a transient error only after `max_wait_s` seconds of retries without one byte arriving.
|
||||
inline int read_at(int fd, void* buf, size_t n, uint64_t off, ReadGate& gate, PreadFn pread_fn = ::pread,
|
||||
double max_wait_s = 120.0) {
|
||||
size_t done = 0;
|
||||
double pause = 0.05, waited = 0.0;
|
||||
while (done < n) {
|
||||
const int seen = gate.enter();
|
||||
const ssize_t got = pread_fn(fd, (uint8_t*) buf + done, n - done, (off_t) (off + done));
|
||||
const int e = got < 0 ? errno : 0;
|
||||
gate.leave(got > 0);
|
||||
if (got > 0) {
|
||||
done += (size_t) got;
|
||||
pause = 0.05;
|
||||
waited = 0.0;
|
||||
continue;
|
||||
}
|
||||
if (got == 0) return ENODATA;
|
||||
if (e == EINTR) continue;
|
||||
if (!read_error_is_transient(e) || waited >= max_wait_s) return e;
|
||||
const int now = gate.refused(seen);
|
||||
if (now > 0)
|
||||
std::fprintf(stderr, "strata: a read was refused (%s); retrying with at most %d reader%s at once for now\n",
|
||||
std::strerror(e), now, now == 1 ? "" : "s");
|
||||
std::this_thread::sleep_for(std::chrono::duration<double>(pause));
|
||||
waited += pause;
|
||||
pause = std::min(pause * 2.0, 5.0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// Loads `bytes` at `off` of `path` into `dst` with up to `readers` readers. With `what`, progress goes to stderr
|
||||
/// every 4 GiB. False with `err` set on failure.
|
||||
inline bool read_range(const std::string& path, uint64_t off, uint8_t* dst, uint64_t bytes, int readers,
|
||||
std::string& err, const char* what = nullptr, PreadFn pread_fn = ::pread,
|
||||
double max_wait_s = 120.0) {
|
||||
constexpr uint64_t kChunk = 16ull << 20;
|
||||
const uint64_t n_chunks = (bytes + kChunk - 1) / kChunk;
|
||||
readers = (int) std::clamp<uint64_t>((uint64_t) std::max(readers, 1), 1, std::max<uint64_t>(n_chunks, 1));
|
||||
ReadGate gate(readers);
|
||||
std::atomic<uint64_t> next{0}, loaded{0};
|
||||
std::atomic<bool> bad{false};
|
||||
std::mutex err_mu;
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
auto fail = [&](const std::string& m) {
|
||||
std::lock_guard<std::mutex> lk(err_mu);
|
||||
if (!bad.exchange(true)) err = m;
|
||||
};
|
||||
auto worker = [&]() {
|
||||
const int fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
fail("cannot open " + path + ": " + std::strerror(errno));
|
||||
return;
|
||||
}
|
||||
for (;;) {
|
||||
const uint64_t c = next.fetch_add(1);
|
||||
if (c >= n_chunks || bad) break;
|
||||
const uint64_t at = c * kChunk, n = std::min(kChunk, bytes - at);
|
||||
const int e = read_at(fd, dst + at, (size_t) n, off + at, gate, pread_fn, max_wait_s);
|
||||
if (e != 0) {
|
||||
fail(read_error(path, off + at, e));
|
||||
break;
|
||||
}
|
||||
const uint64_t was = loaded.fetch_add(n);
|
||||
if (what != nullptr && (was + n) >> 32 != was >> 32) {
|
||||
const double s = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
|
||||
std::fprintf(stderr, "strata: %s: %.0f of %.1f GiB read (%.0f MB/s)\n", what,
|
||||
(double) (was + n) / 1073741824.0, (double) bytes / 1073741824.0,
|
||||
s > 0 ? (double) (was + n) / 1e6 / s : 0.0);
|
||||
}
|
||||
}
|
||||
::close(fd);
|
||||
};
|
||||
std::vector<std::thread> pool;
|
||||
for (int i = 1; i < readers; ++i) pool.emplace_back(worker);
|
||||
worker();
|
||||
for (auto& t : pool) t.join();
|
||||
return !bad;
|
||||
}
|
||||
|
||||
} // namespace strata::platform
|
||||
|
||||
#endif // !_WIN32
|
||||
+28
-12
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "strata/core/pinned.hpp"
|
||||
#include "strata/platform/memory.hpp"
|
||||
#include "strata/platform/robust_read.hpp"
|
||||
#include "strata/kernels/elementwise.hpp"
|
||||
#include "strata/kernels/quantize_act.hpp"
|
||||
#include "strata/kernels/s2_expert_grouped.hpp"
|
||||
@@ -2627,13 +2628,22 @@ LoadStats load_experts_gguf(const std::string& gguf, uint8_t* dst, const strata:
|
||||
#else
|
||||
(void) unbuffered;
|
||||
#endif
|
||||
// UnbiasedStrata: positional reads through strata/platform/robust_read.hpp, so a network store that refuses a
|
||||
// read for a moment (too many readers at once, a slow link) is waited for and retried instead of failing the
|
||||
// start, and a failure names the file, the offset and the error.
|
||||
strata::platform::ReadGate gate(threads);
|
||||
std::mutex err_mu;
|
||||
std::string err;
|
||||
std::atomic<uint64_t> loaded{0};
|
||||
auto fail = [&](const std::string& m) {
|
||||
std::lock_guard<std::mutex> lk(err_mu);
|
||||
if (!bad.exchange(true)) err = m;
|
||||
};
|
||||
auto worker = [&]() {
|
||||
// #230: `fread` on a `FILE*`, as load_experts_ranges (#89): MSVC's `std::ifstream::read` splits a request
|
||||
// into 4095-byte freads, which took this path to 0.02 GiB/s on a Windows install without experts.bin.
|
||||
// The guard closes the handle on every return.
|
||||
struct Closer {
|
||||
FILE* f = nullptr;
|
||||
~Closer() { if (f != nullptr) std::fclose(f); }
|
||||
int fd = -1;
|
||||
~Closer() { if (fd >= 0) ::close(fd); }
|
||||
} file;
|
||||
std::string open_name;
|
||||
std::vector<uint8_t> buf;
|
||||
@@ -2648,25 +2658,31 @@ LoadStats load_experts_gguf(const std::string& gguf, uint8_t* dst, const strata:
|
||||
// the handle is kept while consecutive roles share a file (every layer of a v3 pack)
|
||||
const std::string name = expert_gguf_file(gguf, lay, l, r);
|
||||
if (name != open_name) {
|
||||
if (file.f != nullptr) std::fclose(file.f);
|
||||
file.f = std::fopen(name.c_str(), "rb");
|
||||
if (file.f == nullptr) { bad = true; return; }
|
||||
if (file.fd >= 0) ::close(file.fd);
|
||||
file.fd = ::open(name.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (file.fd < 0) { fail("cannot open " + name + ": " + std::strerror(errno)); return; }
|
||||
open_name = name;
|
||||
}
|
||||
FILE* const f = file.f;
|
||||
const uint64_t src = lay.gguf_off[(size_t) (3 * l + r)];
|
||||
const uint64_t total = per[r] * (uint64_t) lay.n_expert;
|
||||
const uint64_t chunk = per[r] * 16; // 16 experts per read
|
||||
buf.resize((size_t) chunk);
|
||||
for (uint64_t done = 0; done < total; done += chunk) {
|
||||
if (bad) return;
|
||||
const uint64_t n = std::min<uint64_t>(chunk, total - done);
|
||||
// 64-bit seek: a shard is tens of GB
|
||||
if (STRATA_FSEEK64(f, src + done) != 0) { bad = true; return; }
|
||||
if (std::fread(buf.data(), 1, (size_t) n, f) != (size_t) n) { bad = true; return; }
|
||||
const int rc = strata::platform::read_at(file.fd, buf.data(), (size_t) n, src + done, gate);
|
||||
if (rc != 0) { fail(strata::platform::read_error(name, src + done, rc)); return; }
|
||||
for (uint64_t k = 0; k < n / per[r]; ++k) {
|
||||
const uint64_t e = done / per[r] + k;
|
||||
std::memcpy(dst + lay.blob_offset(l, (int64_t) e) + at[r], buf.data() + k * per[r], (size_t) per[r]);
|
||||
}
|
||||
const uint64_t was = loaded.fetch_add(n);
|
||||
if ((was + n) >> 32 != was >> 32) {
|
||||
const double s = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
|
||||
std::fprintf(stderr, "strata generate: experts: %.0f of %.1f GiB read (%.0f MB/s)\n",
|
||||
(double) (was + n) / 1073741824.0, (double) lay.total / 1073741824.0,
|
||||
s > 0 ? (double) (was + n) / 1e6 / s : 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2678,7 +2694,7 @@ LoadStats load_experts_gguf(const std::string& gguf, uint8_t* dst, const strata:
|
||||
if (bad) {
|
||||
st.seconds = -1.0;
|
||||
st.ok = false;
|
||||
st.error = "short read or unreadable shard while reading the experts from the GGUF";
|
||||
st.error = err.empty() ? "unreadable shard while reading the experts from the GGUF" : err;
|
||||
return st;
|
||||
}
|
||||
st.bytes = lay.total;
|
||||
|
||||
+27
-12
@@ -6,6 +6,7 @@
|
||||
#include "strata/artifact/dequant.hpp"
|
||||
#include "strata/kernels/f16_bits.hpp"
|
||||
#include "strata/ngram/ple_reader.hpp"
|
||||
#include "strata/platform/robust_read.hpp"
|
||||
|
||||
#include <cerrno>
|
||||
#include <cmath>
|
||||
@@ -138,6 +139,8 @@ void fp8_e4m3_dequant_row(const uint8_t* row, float scale, float* out160) {
|
||||
struct PleTable::Impl {
|
||||
GgufFile* file = nullptr;
|
||||
const uint8_t* data = nullptr;
|
||||
void* own = nullptr; // `--ple-io ram`: the table in memory of its own (`data` points into it)
|
||||
uint64_t own_bytes = 0;
|
||||
uint64_t n_rows = 0;
|
||||
bool q5_0 = false; // #296: Q5_0 rows (110 B), the mapped reader only
|
||||
mutable uint64_t bytes_read = 0;
|
||||
@@ -261,19 +264,26 @@ bool PleTable::open(const std::string& gguf_path, std::string& err, const PleIoO
|
||||
}
|
||||
if (io.mode == PleIo::Mmap && io.lock && impl_->data != nullptr) {
|
||||
#if !defined(_WIN32)
|
||||
const uint64_t page = 4096;
|
||||
const uintptr_t a0 = (uintptr_t) impl_->data & ~(uintptr_t) (page - 1);
|
||||
const uintptr_t a1 = (uintptr_t) impl_->data + (uintptr_t) need;
|
||||
madvise((void*) a0, a1 - a0, MADV_WILLNEED);
|
||||
if (mlock((const void*) a0, a1 - a0) == 0) {
|
||||
impl_->locked = true;
|
||||
} else {
|
||||
std::fprintf(stderr, "strata: PLE table mlock failed (%s; raise `ulimit -l`): touching its pages instead\n",
|
||||
std::strerror(errno));
|
||||
volatile uint8_t sink = 0;
|
||||
for (uintptr_t p = a0; p < a1; p += page) sink = sink + *(const volatile uint8_t*) p;
|
||||
(void) sink;
|
||||
// UnbiasedStrata: the table is read into memory of its own, not locked inside the file mapping. Several
|
||||
// readers load it (a network store's speed is per stream, and a refused read is retried), no later page
|
||||
// fault can go back to the file, and it does not depend on `ulimit -l` (8 MB in a container).
|
||||
const uint64_t table_offset = impl_->file->data_start() + t->offset;
|
||||
void* mem = mmap(nullptr, (size_t) need, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (mem == MAP_FAILED) {
|
||||
err = "cannot allocate " + std::to_string(need >> 20) + " MiB of RAM for the n-gram table: " + std::strerror(errno);
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
impl_->own = mem;
|
||||
impl_->own_bytes = need;
|
||||
if (!strata::platform::read_range(gguf_path, table_offset, (uint8_t*) mem, need, 16, err, "n-gram table")) {
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
delete impl_->file;
|
||||
impl_->file = nullptr;
|
||||
impl_->data = (const uint8_t*) mem;
|
||||
impl_->locked = mlock(mem, (size_t) need) == 0; // extra: the memory is already resident either way
|
||||
#endif
|
||||
}
|
||||
impl_->mode = io.mode;
|
||||
@@ -321,6 +331,11 @@ void PleTable::close() {
|
||||
delete impl_->file;
|
||||
impl_->file = nullptr;
|
||||
impl_->data = nullptr;
|
||||
#if !defined(_WIN32)
|
||||
if (impl_->own != nullptr) munmap(impl_->own, (size_t) impl_->own_bytes);
|
||||
#endif
|
||||
impl_->own = nullptr;
|
||||
impl_->own_bytes = 0;
|
||||
impl_->n_rows = 0;
|
||||
impl_->rb = PLE_ROW_BYTES;
|
||||
impl_->q5_0 = false;
|
||||
|
||||
Reference in new issue
Block a user