Linux: read ahead of the mapped loads at start
On Linux every load phase read its files one page fault (one 128 KB readahead) at a time, so a cold start spent most of its time waiting on the drive at queue depth ~2. A new platform::advise_willneed asks the kernel for a range in 128 KiB steps (WILLNEED reads at most one readahead window per call), and is called before the weight load, the native dense upload, the profile fill (256 pairs ahead, STRATA_FILL_AHEAD), the peer tier's fill (--peer-device), the resident RAM copy and the MTP draft files. STRATA_READ_AHEAD=0 turns it off. Each phase logs its time. Gen3 NVMe, RTX 5090, 32 GB, Q2_0 resident at 262K: ready in 70 s, was ~920 s.
This commit is contained in:
1 parent
2de94bca5e
commit
cb33c0bac5
10 files changed
+137
-10
No files matched your search
@@ -194,6 +194,12 @@ GPU nor the RAM budget holds (only pages - the experts computed are the same; `S
|
||||
is what runs [Unsloth's UD-Q4_K_XL](UNSLOTH_Q4.md) (72 GiB of experts) on a 64 GB PC: 7-8.5 tokens/s at N = 40 on an
|
||||
RTX 5070, against ~3 tokens/s before these changes.
|
||||
|
||||
**Read-ahead at start (Linux):** the weights, the native dense matrices, the GPU cache's fill from the profile, the
|
||||
resident RAM copy and the MTP draft files are asked for ahead of their reads (madvise / posix_fadvise WILLNEED in
|
||||
128 KiB steps), so the drive sees a deep queue instead of one page fault at a time. Measured on a Gen3 NVMe (RTX 5090,
|
||||
32 GB, Q2_0 resident at 262K): ready in 70 s instead of ~920 s; the fill went from 39 MB/s to 3.2 GB/s.
|
||||
`STRATA_READ_AHEAD=0` turns it off; `STRATA_FILL_AHEAD=N` sets how many fill pairs are asked for ahead (default 256).
|
||||
|
||||
**How much came from where:** with `--stats` the engine prints the tiers of the decode (`expert tiers`: blobs from the
|
||||
RAM copy, blobs and MB from the files, the time spent reading them; `routing prefetch`: how many of the file reads had
|
||||
been warmed). The server log has the same per request (`expert tiers: GPU ... hits ...; RAM ... blobs, files ...
|
||||
|
||||
@@ -162,6 +162,10 @@ public:
|
||||
/// The blob's bytes into `dst` (blob_bytes(layer) of them). Safe from several threads for a source whose
|
||||
/// `transient` can be true.
|
||||
virtual bool copy_blob(int64_t layer, int64_t expert, uint8_t* dst);
|
||||
/// Asks the OS for these pairs' bytes ahead of the `blob` calls that read them. False (nothing asked) by default.
|
||||
virtual bool advise_pairs(const std::pair<int32_t, int32_t>* pairs, int64_t n) const {
|
||||
(void) pairs; (void) n; return false;
|
||||
}
|
||||
/// The `n` experts of `layer` the CPU is about to ask `blob` for, all at once: a source that reads a file may
|
||||
/// fetch them in parallel. The bytes `blob` then returns are the same. Default: nothing.
|
||||
virtual void prefetch(int64_t layer, const int64_t* experts, int64_t n) { (void) layer; (void) experts; (void) n; }
|
||||
@@ -539,6 +543,9 @@ public:
|
||||
/// #286, unbuffered: assembles the blobs of these pairs ahead of the `blob` calls that will ask for them (the
|
||||
/// GPU cache's fill from the profile) - one batch of reads instead of one blob at a time. At most 64 pairs.
|
||||
void prefetch_pairs(const std::pair<int32_t, int32_t>* pairs, int64_t n);
|
||||
/// Mapped reads: asks the OS for these pairs' blobs ahead (platform::advise_willneed). False when unbuffered,
|
||||
/// when read-ahead is off, or before `open`.
|
||||
bool advise_pairs(const std::pair<int32_t, int32_t>* pairs, int64_t n) const override;
|
||||
|
||||
const uint8_t* blob(int64_t layer, int64_t expert) override;
|
||||
bool pinned(int64_t layer, int64_t expert) const override;
|
||||
|
||||
@@ -43,4 +43,12 @@ inline bool file_cache_keeps(uint64_t avail, uint64_t arena_bytes, uint64_t read
|
||||
return room >= read_bytes;
|
||||
}
|
||||
|
||||
/// Whether `advise_willneed` asks the OS for anything: not on Windows, nor with STRATA_READ_AHEAD=0.
|
||||
bool read_ahead_enabled();
|
||||
/// Asks the OS to start reading [p, p + bytes) of a file mapping, without waiting. Linux reads at most one
|
||||
/// readahead window per request, so the range is asked for in 128 KiB steps.
|
||||
void advise_willneed(const void* p, uint64_t bytes);
|
||||
/// The same for [offset, offset + bytes) of an open file.
|
||||
void advise_willneed(int fd, uint64_t offset, uint64_t bytes);
|
||||
|
||||
} // namespace strata::platform
|
||||
@@ -887,6 +887,23 @@ void FileExpertSource::prefetch_pairs(const std::pair<int32_t, int32_t>* pairs,
|
||||
fill_many(todo);
|
||||
}
|
||||
|
||||
bool FileExpertSource::advise_pairs(const std::pair<int32_t, int32_t>* pairs, int64_t n) const {
|
||||
if (base_ == nullptr || !direct_.empty() || !strata::platform::read_ahead_enabled()) return false;
|
||||
for (int64_t i = 0; pairs != nullptr && i < n; ++i) {
|
||||
const int64_t l = pairs[i].first, e = pairs[i].second;
|
||||
if (l < 0 || e < 0 || l >= n_layers_ || e >= n_expert_) continue;
|
||||
if (role_ptr_.empty()) { // experts.bin: the blob is one contiguous range
|
||||
if (const uint8_t* b = mapped_blob(l, e)) strata::platform::advise_willneed(b, layer_blob_bytes_[(size_t) l]);
|
||||
continue;
|
||||
}
|
||||
for (int r = 0; r < 3; ++r) {
|
||||
const size_t k = (size_t) (3 * l + r);
|
||||
strata::platform::advise_willneed(role_ptr_[k] + (size_t) ((uint64_t) e * role_bytes_[k]), role_bytes_[k]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileExpertSource::fill_many(const std::vector<Fill>& todo) {
|
||||
if (todo.empty()) return;
|
||||
if (!direct_.empty()) {
|
||||
@@ -1647,6 +1664,7 @@ bool FileExpertSource::pin_cache_complement(
|
||||
std::atomic<uint64_t> copied{0};
|
||||
std::atomic<bool> failed{false};
|
||||
std::mutex fail_mu;
|
||||
const auto copy_t0 = std::chrono::steady_clock::now();
|
||||
std::string fail_msg;
|
||||
auto fail = [&](const std::string& m) {
|
||||
std::lock_guard<std::mutex> lock(fail_mu);
|
||||
@@ -1668,6 +1686,13 @@ bool FileExpertSource::pin_cache_complement(
|
||||
batch.clear();
|
||||
return true;
|
||||
};
|
||||
if (direct_.empty()) { // mapped reads: ask for the layer's blobs before copying them
|
||||
std::vector<std::pair<int32_t, int32_t>> ahead;
|
||||
for (int64_t expert = 0; expert < n_expert_; ++expert)
|
||||
if (offsets[(size_t) layer * (size_t) n_expert_ + (size_t) expert] != kNoComplement)
|
||||
ahead.emplace_back((int32_t) layer, (int32_t) expert);
|
||||
(void) advise_pairs(ahead.data(), (int64_t) ahead.size());
|
||||
}
|
||||
for (int64_t expert = 0; expert < n_expert_; ++expert) {
|
||||
const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) expert;
|
||||
const uint64_t offset = offsets[index];
|
||||
@@ -1718,8 +1743,9 @@ bool FileExpertSource::pin_cache_complement(
|
||||
#endif
|
||||
const int64_t done = layers_done.fetch_add(1) + 1;
|
||||
if (done % 8 == 0 || done == n_layers_)
|
||||
std::fprintf(stderr, "FileExpertSource: copied cache complement through layer %lld/%lld (%.2f GiB)\n",
|
||||
(long long) done, (long long) n_layers_, (double) copied.load() / 1073741824.0);
|
||||
std::fprintf(stderr, "FileExpertSource: copied cache complement through layer %lld/%lld (%.2f GiB, %.0f s)\n",
|
||||
(long long) done, (long long) n_layers_, (double) copied.load() / 1073741824.0,
|
||||
std::chrono::duration<double>(std::chrono::steady_clock::now() - copy_t0).count());
|
||||
}
|
||||
};
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "strata/core/native_head.hpp"
|
||||
#include "strata/core/peer_experts.hpp"
|
||||
#include "strata/platform/memory.hpp"
|
||||
#include "strata/kernels/bf16_gemv.hpp"
|
||||
#include "strata/kernels/cpu/expert.hpp"
|
||||
#include "strata/kernels/elementwise.hpp"
|
||||
@@ -101,6 +102,9 @@ bool read_file(const std::string& path, std::vector<uint8_t>& out) {
|
||||
if (STRATA_FILE_SEEK64(f, 0, SEEK_END) != 0) return false;
|
||||
const long long n = (long long) STRATA_FILE_TELL64(f);
|
||||
if (n < 0 || STRATA_FILE_SEEK64(f, 0, SEEK_SET) != 0) return false;
|
||||
#if !defined(_WIN32)
|
||||
strata::platform::advise_willneed(fileno(f), 0, (uint64_t) n);
|
||||
#endif
|
||||
out.resize((size_t) n);
|
||||
return n == 0 || std::fread(out.data(), 1, (size_t) n, f) == (size_t) n;
|
||||
}
|
||||
@@ -212,6 +216,9 @@ bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, Session
|
||||
~Closer() { if (f != nullptr) std::fclose(f); }
|
||||
} closer{f};
|
||||
if (cudaMalloc((void**) &experts_, bytes) != cudaSuccess) { err = "mtp: the 512 experts do not fit in VRAM"; return false; }
|
||||
#if !defined(_WIN32)
|
||||
strata::platform::advise_willneed(fileno(f), 0, bytes);
|
||||
#endif
|
||||
std::vector<uint8_t> chunk(64u << 20);
|
||||
for (uint64_t off = 0; off < bytes;) {
|
||||
const uint64_t n = std::min<uint64_t>(chunk.size(), bytes - off);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "strata/core/weights.hpp"
|
||||
#include "strata/artifact/gguf_reader.hpp"
|
||||
#include "strata/kernels/native_mmvq.hpp"
|
||||
#include "strata/platform/memory.hpp"
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <algorithm>
|
||||
@@ -150,6 +151,10 @@ bool NativeDense::load(const std::vector<std::string>& shards, WeightTable& tabl
|
||||
if (next != offsets.end() && bytes > *next - tensor.offset) {
|
||||
err = "native dense: overlapping payload " + tensor.name; return false;
|
||||
}
|
||||
// the uploads below read these: ask for them now so the reads overlap
|
||||
if (eligible(tensor, include_ple_key) && !outside(tensor.name) &&
|
||||
strata::kernels::native_mmvq_supported(tensor.type))
|
||||
strata::platform::advise_willneed(gguf.tensor_data(tensor), bytes);
|
||||
}
|
||||
for (const auto& tensor : gguf.tensors()) {
|
||||
if (!eligible(tensor, include_ple_key) || outside(tensor.name)) continue;
|
||||
|
||||
@@ -156,7 +156,12 @@ bool PeerExperts::open(int device, const std::vector<std::pair<int32_t, int32_t>
|
||||
// open() zeroes the arena with cudaMemset on the legacy stream, which is not ordered against the non-blocking
|
||||
// refill stream: wait for it, or the zeroing can land on top of the fills
|
||||
if (!ck(cudaDeviceSynchronize(), "arena zeroing", err)) { close(); return false; }
|
||||
for (const auto& pr : pick) {
|
||||
// mapped reads: advise the next kAhead pairs so their reads overlap
|
||||
constexpr size_t kAhead = 256;
|
||||
const bool advise = src.advise_pairs(pick.data(), (int64_t) std::min(kAhead, pick.size()));
|
||||
for (size_t i = 0; i < pick.size(); ++i) {
|
||||
const auto& pr = pick[i];
|
||||
if (advise && i + kAhead < pick.size()) (void) src.advise_pairs(&pick[i + kAhead], 1);
|
||||
const int32_t slot = cache_.admit(pr.first, pr.second);
|
||||
const uint8_t* b = src.blob(pr.first, pr.second);
|
||||
if (slot < 0 || b == nullptr || !cache_.fill_slot(slot, b, refill_, err, (int64_t) lay.blob_bytes(pr.first))) {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "strata/core/weights.hpp"
|
||||
|
||||
#include "strata/kernels/f16_bits.hpp"
|
||||
#include "strata/platform/memory.hpp"
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
@@ -279,6 +280,12 @@ bool WeightTable::load(const std::string& pack_dir, void* arena_base, uint64_t a
|
||||
cur = std::fopen(p.c_str(), "rb");
|
||||
if (!cur) { err = "cannot open " + p; cudaFreeHost(stage_in); cudaFreeHost(stage_out); return false; }
|
||||
cur_file = r.file;
|
||||
#if !defined(_WIN32)
|
||||
// ask for this file's remaining rows up front so their reads overlap
|
||||
for (size_t j = row_i; j < rows.size(); ++j)
|
||||
if (rows[j].file == r.file && !skipped[j])
|
||||
strata::platform::advise_willneed(fileno(cur), rows[j].src_off, rows[j].src_bytes);
|
||||
#endif
|
||||
}
|
||||
|
||||
// ---- the segment list: what this tensor's bytes are, plane by plane, in both forms
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
#include <dxgi1_4.h>
|
||||
#include <cstring>
|
||||
#else
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
@@ -108,6 +111,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;
|
||||
@@ -133,6 +140,32 @@ 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)
|
||||
(void) posix_fadvise(fd, (off_t) (offset + at), (off_t) std::min(kAdviseStep, bytes - at), POSIX_FADV_WILLNEED);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace strata::platform
|
||||
@@ -2422,14 +2422,21 @@ int main(int argc, char** argv) {
|
||||
(unsigned long long) (total_b >> 20));
|
||||
return 1;
|
||||
}
|
||||
auto load_t0 = std::chrono::steady_clock::now();
|
||||
auto load_s = [&] {
|
||||
const auto t = std::chrono::steady_clock::now();
|
||||
const double s = std::chrono::duration<double>(t - load_t0).count();
|
||||
load_t0 = t;
|
||||
return s;
|
||||
};
|
||||
strata::core::WeightTable wt;
|
||||
if (!wt.load(o.pack, arena, pool_bytes, err, skip.empty() ? nullptr : &skip)) {
|
||||
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
|
||||
return 1;
|
||||
}
|
||||
std::fprintf(stderr, "strata generate: %llu MiB of weights loaded from %s (%zu canonical tensors skipped: "
|
||||
"served natively)\n",
|
||||
(unsigned long long) (pool_bytes >> 20), o.pack.c_str(), skip.size());
|
||||
std::fprintf(stderr, "strata generate: %llu MiB of weights loaded from %s in %.1f s (%zu canonical tensors "
|
||||
"skipped: served natively)\n",
|
||||
(unsigned long long) (pool_bytes >> 20), o.pack.c_str(), load_s(), skip.size());
|
||||
|
||||
strata::core::NativeDense native_dense;
|
||||
if (!o.native_dense_gguf.empty()) {
|
||||
@@ -2437,8 +2444,8 @@ int main(int argc, char** argv) {
|
||||
std::fprintf(stderr, "strata generate: native dense projections: %s\n", err.c_str());
|
||||
return 1;
|
||||
}
|
||||
std::fprintf(stderr, "strata generate: %zu native projection matrices, %.2f MiB of weights\n",
|
||||
native_dense.tensor_count(), (double) native_dense.weight_bytes() / (1024.0 * 1024.0));
|
||||
std::fprintf(stderr, "strata generate: %zu native projection matrices, %.2f MiB of weights, in %.1f s\n",
|
||||
native_dense.tensor_count(), (double) native_dense.weight_bytes() / (1024.0 * 1024.0), load_s());
|
||||
}
|
||||
|
||||
strata::kernels::gr_set_fp32_activations(o.gr_fp32_activations);
|
||||
@@ -3743,12 +3750,24 @@ int main(int argc, char** argv) {
|
||||
// #286: an unbuffered file tier reads the pairs in batches of 64, the next batch while this one is copied
|
||||
std::future<void> ahead;
|
||||
auto read_batch = [&](int64_t at) { src.prefetch_pairs(profile.data() + at, std::min<int64_t>(64, want - at)); };
|
||||
// mapped reads: advise the next `fill_ahead` pairs so their reads overlap; STRATA_FILL_AHEAD=0 turns it off
|
||||
int64_t fill_ahead = 0;
|
||||
if (!per_layer && srcp == &src && !src.unbuffered()) {
|
||||
const char* v = std::getenv("STRATA_FILL_AHEAD");
|
||||
fill_ahead = v == nullptr ? 256 : std::max(0, std::atoi(v));
|
||||
if (fill_ahead > 0 && !src.advise_pairs(profile.data(), std::min<int64_t>(fill_ahead, want))) fill_ahead = 0;
|
||||
std::fprintf(stderr, "strata generate: the profile fill asks for %lld pairs ahead (STRATA_FILL_AHEAD)\n",
|
||||
(long long) fill_ahead);
|
||||
}
|
||||
const auto fill_t0 = std::chrono::steady_clock::now();
|
||||
uint64_t fill_bytes = 0;
|
||||
for (int64_t i = 0; i < want; ++i) {
|
||||
if (!per_layer && srcp == &src && src.unbuffered() && i % 64 == 0) {
|
||||
if (ahead.valid()) ahead.get();
|
||||
else read_batch(i);
|
||||
if (i + 64 < want) ahead = std::async(std::launch::async, read_batch, i + 64);
|
||||
}
|
||||
if (fill_ahead > 0 && i + fill_ahead < want) (void) src.advise_pairs(profile.data() + i + fill_ahead, 1);
|
||||
const int32_t slot = xcache.admit(profile[(size_t) i].first, profile[(size_t) i].second);
|
||||
if (slot == strata::core::kNotResident) {
|
||||
if (per_layer) continue;
|
||||
@@ -3761,8 +3780,10 @@ int main(int argc, char** argv) {
|
||||
(long long) i, err.c_str());
|
||||
return 1;
|
||||
}
|
||||
fill_bytes += strata::kernels::cpu::expert_layout().blob_bytes(profile[(size_t) i].first);
|
||||
++prefilled;
|
||||
}
|
||||
const double fill_s = std::chrono::duration<double>(std::chrono::steady_clock::now() - fill_t0).count();
|
||||
// **AND ONE SLOT IS READ BACK AND COMPARED.** A residency table that is right about indices and wrong
|
||||
// about bytes produces a plausible token, which is this project's most expensive failure mode; the
|
||||
// cache's own `verify_slot` is the check and it costs one 1.38 MB D2H at startup.
|
||||
@@ -3773,8 +3794,10 @@ int main(int argc, char** argv) {
|
||||
return 1;
|
||||
}
|
||||
mem_mark("the profile fill");
|
||||
std::fprintf(stderr, "strata generate: pre-filled %lld of %lld slots from the profile; slot 0 verified\n",
|
||||
(long long) prefilled, (long long) (per_layer ? xcache.slots() : want));
|
||||
std::fprintf(stderr, "strata generate: pre-filled %lld of %lld slots from the profile in %.1f s (%.0f MB/s); "
|
||||
"slot 0 verified\n",
|
||||
(long long) prefilled, (long long) (per_layer ? xcache.slots() : want), fill_s,
|
||||
fill_s > 0 ? (double) fill_bytes / 1e6 / fill_s : 0.0);
|
||||
}
|
||||
|
||||
for (auto& stp : stages) {
|
||||
|
||||
Reference in new issue
Block a user