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