From a34078ab2f11208dd4b58a01eceacdd05011c812 Mon Sep 17 00:00:00 2001 From: sergiywith Date: Thu, 1 Oct 2026 17:21:22 +0300 Subject: [PATCH] Prompt path: the first chunk's n-gram (PLE) rows read beside layer 0, 256 at a time Layer 1 adds the per-layer embedding to every token of a chunk, so its rows must be in. A later chunk's rows were already read on a thread during the chunk before; the first chunk's were read synchronously before layer 0, so the GPU idled while the SSD read them (an nsys trace: ~245 ms of a single-chunk 32K prompt). Now the first chunk's gather starts on a thread too and lands just before layer 1 (ple_land), which also starts the next chunk's. The reads themselves went 64 at a time: a 32K prompt is 512,720 rows in 213,358 4 KiB reads, ~620K IOPS at queue depth 64. --ple-inflight now defaults to 256, where the drive saturates (1024 is no faster). Decode reads 16 rows a token and is unchanged. The same rows: the same results. IQ2_XS, RTX 5090: a 32K prompt 5986 / 6013 -> 5843 / 5808 ms, 8K 1714 -> 1624 ms; first-token logits bit-identical. --- include/strata/kernels/ngram.hpp | 2 +- src/prefill/prefill.cpp | 23 +++++++++++++++++------ src/program/generate.cpp | 4 ++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/include/strata/kernels/ngram.hpp b/include/strata/kernels/ngram.hpp index 3c6f8fab..9aae9c08 100644 --- a/include/strata/kernels/ngram.hpp +++ b/include/strata/kernels/ngram.hpp @@ -114,7 +114,7 @@ enum class PleIo { Direct, Mmap }; struct PleIoOptions { PleIo mode = PleIo::Direct; - uint32_t max_inflight = 64; ///< outstanding SSD reads (decode needs 16; prefill chunks use more) + uint32_t max_inflight = 256; ///< outstanding SSD reads (decode needs 16; a prompt chunk saturates the SSD at 256) uint64_t cache_rows = 1u << 20; ///< bounded row cache: 1,048,576 rows x 90 B ~ 95 MB; 0 disables bool io_thread = true; ///< reads submitted by a worker thread, not the caller /// Mmap mode only (`--ple-io ram`): lock the whole mapped table in RAM at open, so no SSD read ever sits on diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp index afbacc4a..d7b9a33c 100644 --- a/src/prefill/prefill.cpp +++ b/src/prefill/prefill.cpp @@ -1180,12 +1180,20 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e } } if (hand_in_ == nullptr) gr_broadcast(m.emb, m.R, T, m.cs); - // ---- the PLE rows of the whole chunk, one batched SSD request (read ahead on a thread, see ple_gather) - if (ple_on) { + // ---- the PLE rows of the whole chunk, one batched SSD request on a thread (see ple_gather). A later chunk's + // were read ahead during the previous chunk; the first chunk's are read beside layer 0 - they are needed from + // layer 1 on, and gathering them here first left the GPU idle for the whole read (~0.4 s of a 32K prompt) + if (ple_on && !ple_next.valid()) + ple_next = std::async(std::launch::async, [&ple_gather, &ple_next_err, c0, b = ple_buf] { + return ple_gather(c0, b, ple_next_err); + }); + bool ple_pending = ple_on; + // the chunk's rows onto the device just before layer 1 reads them, and the next chunk's gather started + auto ple_land = [&]() -> bool { + if (!ple_pending) return true; + ple_pending = false; const auto tp = Clock::now(); - if (!ple_next.valid()) { - if (!ple_gather(c0, ple_buf, err)) return false; - } else if (!ple_next.get()) { + if (!ple_next.get()) { err = ple_next_err; return false; } @@ -1199,7 +1207,8 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e } ple_buf ^= 1; stats_.ms_ple += ms_since(tp); - } + return true; + }; for (int64_t t = 0; t < T; ++t) { prev[0] = prev[1]; prev[1] = (int32_t) tokens[c0 + t]; } // ---- the QSA step records of every position in the chunk for (int64_t t = 0; t < T; ++t) strata::kernels::qsa_step_fill(m.steps_host.data() + t * strata::kernels::kStepCount, p0 + t, s); @@ -1335,6 +1344,7 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e core::progress_beat(); // the serve watchdog: a prompt chunk of 8192 tokens is still moving core::progress_at("reading the prompt (batched): layer", l, p0); // #251: a stall names layer and chunk const core::LayerView v(*m.wt, l); + if (l == std::max(LB, 1) && !ple_land()) return false; // the PLE rows, read from layer 1 on // ---- the PLE block at layer 1, token by token (its conv reads the previous tokens' rows) if (l == 1 && ple_on && ple_batch) { // the whole chunk at once, in sub-batches carved from the idle scratch region: the key and value @@ -2053,6 +2063,7 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e strata::kernels::cvec_apply(m.R, l, T, D, nullptr, 0, nullptr, 0, false, m.cs); } } + if (!ple_land()) return false; // a stage that ends before layer 1: the rows land anyway, the next gather starts if (issuer.joinable()) { issuer.join(); stats_.ms_experts_host += iss_ms; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index f2df1d4c..fa84081a 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -221,7 +221,7 @@ struct Options { /// Plan v0.3 P2: how the n-gram table is read. Direct (default) = unbuffered SSD reads, table never in RAM. std::string ple_io = "direct"; int64_t ple_row_cache = 1 << 20; ///< bounded row cache (rows of 90 B); 0 disables - int ple_inflight = 64; + int ple_inflight = 256; // the prompt path reads a chunk's rows at once: 64 left the SSD half idle (32K: 303 -> 189 ms) double ple_delay_us = 0; ///< fault injection: every row read completes no earlier than this bool ple_sync_submit = false; ///< A/B arm: submit reads on the token thread, no I/O worker std::string kv = "fp16"; ///< plan v0.3 P7: KV storage, fp16 (default) or int8 (half the VRAM) @@ -433,7 +433,7 @@ void usage() { " reads, the table never enters RAM or the file cache; mmap: A/B arm;\n" " ram: mmap with the whole table locked in RAM at start (Linux/macOS)\n" " --ple-row-cache N bounded cache of fetched rows, 90 B each (default 1048576; 0 = off)\n" - " --ple-inflight N outstanding SSD reads (default 64)\n" + " --ple-inflight N outstanding SSD reads (default 256)\n" " --ple-delay-us U fault injection: each row read completes no earlier than U us\n" " --ple-sync-submit A/B arm: submit table reads on the token thread (default: an I/O thread)\n" " --kv fp16|int8 KV storage (plan v0.3 P7): int8 codes + fp16 scale per 64 values, half the\n"