Start: register the expert arena per layer on a thread ahead of the readers (Windows; part 2 of #285)

0.1.31 registers the whole arena with CUDA before the first byte is read. With 4 KB pages, which every account
without the Lock Pages privilege gets, that registration is ~1.5 s of the start that nothing hides (IQ2_XS:
33 GiB).

- PinnedArena(bytes, bounds, Deferred) reserves only. register_slices() registers the per-layer slices in order on
  a thread and publishes how many are done.
- Every reader (load_experts_gguf buffered and unbuffered, load_experts_ranges, load_experts_direct) writes layer L
  only once slices L and L+1 are registered. Writing a page while cudaHostRegister runs on it corrupted the arena
  under WDDM, even on large pages. A layer boundary inside a page puts that page in both registrations, so the next
  slice must be done too (page-aligned in effect, without moving the slice bounds an expert must not straddle).
- A slice CUDA refuses ends the registration, and the rest is locked resident as in the sliced fallback.
- Only where the whole arena would be registered anyway: no cap (STRATA_ARENA_PIN_GIB, multi-GPU under WDDM) and no
  shared arena file. Linux keeps #253's whole-arena pin. STRATA_DEFERRED_REGISTER=0 restores the old order.
- STRATA_VERIFY_ARENA=1 prints the loaded arena's FNV-1a checksum (tests).

Checksums: the same with the old order and the deferred one, 4 KB and large pages, the GGUF in place and experts.bin
(IQ2_XS; 13 deferred loads).

IQ2_XS without experts.bin, RTX 5090, 128 GB. Each cell is the time until the arena is loaded / until a 16-token
answer is done. Cold: the files pushed out of the file cache first. Two to three runs each:

| | cold | restart |
|---|---|---|
| 4 KB pages (STRATA_NO_LARGEPAGES=1), 0.1.31 | 12.4-13.0 / 16.3-16.8 s | 7.8-8.5 / 10.9-11.7 s |
| 4 KB pages, parts 1+2 | 11.1-11.5 / 14.9-15.4 s | 5.5-5.7 / 8.5-8.9 s |
| large pages, 0.1.31 | 17.6-18.7 / 21.1-22.4 s | 4.6-5.1 / 7.0-7.6 s |
| large pages, parts 1+2 | 17.6-19.9 / 21.3-23.6 s | 4.1-4.5 / 6.7-7.1 s |

The cold spread with large pages comes from the drive: the same build gave 17.6 and 19.9 s with the deferred
registration and the cache probe switched off as well.
This commit is contained in:
sergiywith authored and Niko1221 committed 2026-10-05 15:58:38 +02:00
1 parent 066094c81f
commit ebe1db1a0b
4 files changed
+116 -18

No files matched your search

+45 -9
View File
@@ -2917,7 +2917,13 @@ bool check_experts_gguf(const std::string& gguf, const strata::kernels::cpu::Exp
// `unbuffered` (Windows, experts_unbuffered): each chunk's 4 KiB-aligned window is read with FILE_FLAG_NO_BUFFERING into
// an aligned buffer and scattered into the blobs - no copy through the file cache when the drive is read anyway.
LoadStats load_experts_gguf(const std::string& gguf, uint8_t* dst, const strata::kernels::cpu::ExpertLayout& lay,
int threads, bool unbuffered) {
int threads, bool unbuffered, const std::atomic<int>* ready) {
// `ready`: layer l is written only once *ready > l + 1 (a PinnedArena registering its slices meanwhile; the next
// slice too, since its registration starts on the page that may hold this layer's tail)
auto wait_ready = [ready](int64_t l) {
if (ready != nullptr)
while (ready->load(std::memory_order_acquire) <= l + 1) std::this_thread::yield();
};
LoadStats st;
st.layers = (uint64_t) lay.n_layers;
const auto t0 = std::chrono::steady_clock::now();
@@ -2947,6 +2953,7 @@ LoadStats load_experts_gguf(const std::string& gguf, uint8_t* dst, const strata:
for (;;) {
const int64_t l = next.fetch_add(1);
if (l >= lay.n_layers || bad) break;
wait_ready(l);
const auto& fm = lay.fmt[(size_t) l];
const uint64_t blob = lay.bytes[(size_t) l];
const uint64_t per[3] = {fm.up_off, fm.up_off, blob - fm.down_off};
@@ -3025,6 +3032,7 @@ LoadStats load_experts_gguf(const std::string& gguf, uint8_t* dst, const strata:
for (;;) {
const int64_t l = next.fetch_add(1);
if (l >= lay.n_layers || bad) break;
wait_ready(l);
const auto& fm = lay.fmt[(size_t) l];
const uint64_t blob = lay.bytes[(size_t) l];
const uint64_t per[3] = {fm.up_off, fm.up_off, blob - fm.down_off};
@@ -3195,8 +3203,20 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
lbytes.push_back(lay.blob_bytes(l) * (uint64_t) n_expert);
}
bounds.push_back(want);
PinnedArena* a = new PinnedArena(want + (uint64_t) blob, bounds, max_pinned_bytes,
shared_arena_file, pack_hash);
// #285: on Windows the per-layer registration runs on a thread ahead of the readers, which wait for their
// layer's slice (the whole arena registered before the load cost ~2.5 s at 33 GiB of 4 KB pages, not hidden).
// Only where the whole arena would be registered: no cap (STRATA_ARENA_PIN_GIB, multi-GPU), no shared arena.
// Linux keeps the whole-arena pin (#253). STRATA_DEFERRED_REGISTER=0: the registration before the load.
#ifdef _WIN32
const char* defer_env = std::getenv("STRATA_DEFERRED_REGISTER");
const bool deferred = max_pinned_bytes == 0 && arena_pin_cap_gib() == -1 && shared_arena_file.empty() &&
(defer_env == nullptr || defer_env[0] != '0');
#else
const bool deferred = false;
#endif
PinnedArena* a = deferred ? new PinnedArena(want + (uint64_t) blob, bounds, PinnedArena::Deferred{})
: new PinnedArena(want + (uint64_t) blob, bounds, max_pinned_bytes,
shared_arena_file, pack_hash);
if (!a->valid()) {
const std::string why = a->note;
delete a;
@@ -3221,13 +3241,25 @@ 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;
if (from_gguf) {
st = load_experts_gguf(gguf_, a->data(), lay, readers, unbuffered);
} else {
if (unbuffered) st = load_experts_direct(path, a->data(), loff, lbytes, readers, /*chunk=*/8u << 20);
if (!unbuffered || (!st.ok && st.error.empty())) // unaligned ranges: the buffered reader
st = load_experts_ranges(path, a->data(), loff, lbytes, threads, /*chunk=*/8u << 20);
std::atomic<int> ready{0};
std::thread reg;
if (deferred) {
int dev = 0;
cudaGetDevice(&dev);
reg = std::thread([a, &ready, dev] {
cudaSetDevice(dev);
a->register_slices(ready);
});
}
const std::atomic<int>* rp = deferred ? &ready : nullptr;
if (from_gguf) {
st = load_experts_gguf(gguf_, a->data(), lay, readers, unbuffered, rp);
} else {
if (unbuffered) st = load_experts_direct(path, a->data(), loff, lbytes, readers, /*chunk=*/8u << 20, rp);
if (!unbuffered || (!st.ok && st.error.empty())) // unaligned ranges: the buffered reader
st = load_experts_ranges(path, a->data(), loff, lbytes, threads, /*chunk=*/8u << 20, rp);
}
if (reg.joinable()) reg.join();
std::fprintf(stderr, "strata generate: expert arena read %s (%s)\n", unbuffered ? "unbuffered" : "through the file cache",
why.c_str());
if (!st.ok) {
@@ -3240,6 +3272,10 @@ bool ArenaExpertSource::open(const std::string& pack_dir, int64_t n_layers, int6
err = "ArenaExpertSource: the load read " + std::to_string(st.bytes) + " B of " + std::to_string(want);
return false;
}
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));
}
// the first start: write experts.bin for the mapped starts after this one - only when the drive has room for it
// and 2 GiB more (a full drive fails other writes too); otherwise this start says so and runs pinned as before
std::error_code space_ec;