From a56441f868722d673a401b5aae7b3dc14e742058 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:39:48 +0900 Subject: [PATCH] poly: every checkpoint of a read through pinned memory read() and read_groups() copy a checkpoint's layers in the stream behind the call that ends at it. Only the first checkpoint went to pinned memory (h_state_); the others went straight to their pageable states. A D2H copy into pageable memory returns only after the stream has done the work before it, so the host stopped issuing launches at every layer of every checkpoint after the first, and the card waited: 257 such calls held the host 1.0 s in an 8,192-token Inkling 2 Mini prompt with the conversation cache on (RTX 2070 Super, nsys). Each checkpoint after the first now goes to a pinned buffer too (h_marks_, stage_marks()), and from there to its state after the read, as the first one did (state_out()). The buffers are made between requests (trim() -> mark_buffers()), as many as a read has had checkpoints after the first, and kept: cudaMallocHost() of a 330 MiB state took 210 ms, and made on a thread of its own during the read it held the read's CUDA calls up as long. So the first long read of a process goes as before, and the reads after it go through the buffers. A buffer is made only while the RAM free holds the buffers still to make, the pageable states the next read fills and two states more; a checkpoint without one goes straight to its state as before. read() and read_groups() serve every family, so any model whose reads cross more than one checkpoint keeps up to (checkpoints - 1) x the card's state size of extra pinned RAM per card (Inkling 2 Mini with the default 6 checkpoints: 5 x 330 MiB). Same bits: the same copies of the same bytes, only the host memory they land in first differs. This extends VecSzn's pinned staging of a read's first checkpoint. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/engine.cpp | 68 +++++++++++++++++++++++++++++--------- engine/src/poly/engine.hpp | 5 +++ 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 35005b37e..9e647633c 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -4177,6 +4177,54 @@ void Engine::state_out(const uint8_t* from, uint8_t* to) { for (int i = 0; i < pieces; ++i) piece(i); } +// Pinned buffers for the states of a read's checkpoints, where the layers' copies go in the stream: h_state_ for +// the first, h_marks_ for the others. A copy into pageable memory returns only once the card has done the work +// queued before it, so the host stopped issuing launches at each layer of each checkpoint after the first and the +// card waited for them. The buffers are made between requests (trim()), as many as a read has had checkpoints +// after the first: cudaMallocHost() of Inkling 2 Mini's 330 MiB state took 210 ms, and on a thread of its own it +// held the read's CUDA calls up as long. They stay pinned, so one is made only while the RAM free holds the ones +// still to make, the checkpoints' own (pageable) states the next read fills, and two states more; a checkpoint +// without one goes straight to its state, as before. +void Engine::mark_buffers() { + const size_t bytes = card_state_bytes(); + if (h_mark_bytes_ < bytes) { // the states grew: the buffers made for the smaller ones go + for (uint8_t* p : h_marks_) hfree(p); + h_marks_.clear(); + h_mark_bytes_ = bytes; + } + if (h_marks_.size() >= marks_wanted_) return; + OnCard here(device_, split_); + while (h_marks_.size() < marks_wanted_ && ram_available() >= (2 * (marks_wanted_ - h_marks_.size()) + 2) * bytes) { + void* p = nullptr; + if (cudaMallocHost(&p, bytes) != cudaSuccess) { + cudaGetLastError(); + break; + } + host_allocs_.push_back(p); + h_marks_.push_back((uint8_t*) p); + } +} + +// Null for a mark without a state or a buffer (it goes straight to its state). +std::vector Engine::stage_marks(const Mark* marks, int n_marks) { + std::vector staged((size_t) n_marks, nullptr); + const size_t bytes = card_state_bytes(); + bool first = h_state_bytes_ >= bytes; + size_t k = 0, with_state = 0; + for (int m = 0; m < n_marks; ++m) { + if (!marks[m].state) continue; + ++with_state; + if (std::exchange(first, false)) { + staged[(size_t) m] = h_state_; + continue; + } + if (h_mark_bytes_ >= bytes && k < h_marks_.size()) staged[(size_t) m] = h_marks_[k++]; + } + // the first goes through h_state_ once save_state() has made it the state's size + marks_wanted_ = std::max(marks_wanted_, with_state > 0 ? with_state - 1 : 0); + return staged; +} + // What the state has that is no layer's: the model's output for the last token, which the draft block pairs the // next one with. It lies behind the layers' state. void Engine::save_last(uint8_t* dst) { @@ -4340,6 +4388,7 @@ size_t Engine::covering(int pct) const { void Engine::trim() { relock(); // first: under Linux the pages dropped below must not be locked + mark_buffers(); // between requests: the next read's checkpoints all go through pinned memory struct Held { float recent; int64_t used; @@ -8171,15 +8220,9 @@ bool Engine::read(const int32_t* tokens, int n, const Mark* marks, int n_marks, // a turn for each layer, and one for the model's output, which the draft block keeps a row of const int turns = hi_ - lo_ + (draft_ ? 1 : 0); // A checkpoint's layers are copied in the stream, behind the call before it and ahead of the next one's turn - // at the layer, so the card goes on. The first goes to pinned memory, at the card's speed, and to its state - // after the read; the others straight to their states, which are pageable: 2 MB of Qwen3.6's took the card - // 1.3 ms a layer. - std::vector staged((size_t) n_marks, nullptr); - for (int m = 0; m < n_marks; ++m) - if (marks[m].state && h_state_bytes_ >= card_state_bytes()) { - staged[(size_t) m] = h_state_; - break; - } + // at the layer, so the card goes on. They go to pinned memory, at the card's speed, and to their states after + // the read (stage_marks()). + const std::vector staged = stage_marks(marks, n_marks); shared_ahead_ = all >= opt_.stage_ahead; // (co-reads of fewer tokens: one pack, its own experts) try { for (int k = 0; k < turns; ++k) { @@ -8311,12 +8354,7 @@ bool Engine::read_groups(const Mark* marks, int n_marks, const std::function staged((size_t) n_marks, nullptr); // the first mark's state goes through h_state_ - for (int m = 0; m < n_marks; ++m) - if (marks[m].state && h_state_bytes_ >= card_state_bytes()) { - staged[(size_t) m] = h_state_; - break; - } + const std::vector staged = stage_marks(marks, n_marks); // pinned buffers of the marks' states shared_ahead_ = n >= opt_.stage_ahead; const auto restore = [&] { r_ = r0; diff --git a/engine/src/poly/engine.hpp b/engine/src/poly/engine.hpp index ab0e27454..b64fa2f36 100644 --- a/engine/src/poly/engine.hpp +++ b/engine/src/poly/engine.hpp @@ -1147,7 +1147,12 @@ class Engine { uint8_t* h_state_ = nullptr; ///< pinned: save_state()'s way to its caller's memory, and read()'s checkpoints' void* ev_saved_ = nullptr; ///< marks read()'s copy of a checkpoint's state into one of them size_t h_state_bytes_ = 0; + std::vector h_marks_; ///< pinned: read()'s checkpoints' states after the first (mark_buffers()) + size_t h_mark_bytes_ = 0; ///< the size of each of them + size_t marks_wanted_ = 0; ///< the most checkpoints after the first that a read has had void state_out(const uint8_t* from, uint8_t* to); + std::vector stage_marks(const Mark* marks, int n_marks); + void mark_buffers(); int cached_ = 0; // device scratch: a layer's input streams, one stream, the normed input, the attention's and the FFN's output