From 6a5793819ec05113d7044852d7952dca07e5a0ab Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:14:06 +0900 Subject: [PATCH 1/2] poly: Inkling 2's next layer run's experts copied during the current run, opt-in (STRATA_INK_PRESTAGE=1, same bits) In a prompt read of several calls (read_groups()), each layer run's experts that are not cached go through the staging buffer, and main copies them only when the run starts, so the card waits for that copy. With STRATA_INK_PRESTAGE=1 the read's staging buffer gets a second part as large as the first (widen_stage(); the expert cache lends that VRAM for the read and takes it back in narrow_stage(), as it does for the first part), and once a run's first call has queued its own copy, the next run's uncached experts are copied from the pinned mirror into the other part on the upload stream (prestage()). The copy waits for the last call that read that part (the run before's, ev_free_); the next run's calls wait for its event and read their experts there. If the next run's experts are not the ones copied, it copies its own as before. Only with the pinned mirror (build_mirror(), the Linux default): its copies are queued at once, where the pinned buffers' CPU copy would hold the call's launches up. The experts, their layout within a part and the launches are the same, so the results are too: identity of main, this change with STRATA_INK_PRESTAGE=1, and without it, on an RTX 2070 8 GB (302 / 2,048 / 8,192 tokens, IQ4_NL and UD-Q2_K_XL, graphs and --no-graphs, experts frozen), 40 of 40 comparisons byte-identical, prestage engaged in 12 of the 20 opt-in runs. RTX 2070 Super 8 GB, mean of 3 runs: IQ4_NL 2,048-token prompt +12.6% (480.3 -> 540.7 tok/s), UD-Q2_K_XL 2,048 -1.3%, IQ4_NL 32,768 -1.9%; decode unchanged. Off by default. Inkling 2 on CUDA only; HIP compiles the code, never takes it. From our reference PR #200 (commit 802b20df0, "Inkling 2 Mini's prompt reads do not wait for the copies to the card"), whose row-table half is #265; adapted to main's read_groups() and the pinned mirror. Co-Authored-By: Claude Opus 5.5 --- docs/SETTINGS.md | 3 +- docs/models/inkling.md | 7 ++ engine/src/poly/engine.cpp | 144 +++++++++++++++++++++------ engine/src/poly/engine.hpp | 22 +++- engine/src/poly/inkling2.cpp | 8 ++ terminal/data/settings-register.json | 35 +++++++ 6 files changed, 185 insertions(+), 34 deletions(-) diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index bb8fe2806..493325201 100644 --- a/docs/SETTINGS.md +++ b/docs/SETTINGS.md @@ -11,7 +11,7 @@ A number or a word is set like any environment variable, for example `STRATA_KVA (`terminal/strata-.json`), which the server passes to the engine. The Strata project's Terminal writes it there with `strata launch --set NAME=value`. -## Switches you can turn on (347) +## Switches you can turn on (348) Off unless you set them. The last column says where each one is described. @@ -127,6 +127,7 @@ Off unless you set them. The last column says where each one is described. | `STRATA_I8_MT` | number | Intel Arc | [INTEL_INT8_EXPERTS.md](INTEL_INT8_EXPERTS.md) | | `STRATA_I8_WG` | number | Intel Arc | [INTEL_INT8_EXPERTS.md](INTEL_INT8_EXPERTS.md) | | `STRATA_INK_FAST_CTRL` | on / off | NVIDIA | - | +| `STRATA_INK_PRESTAGE` | on / off | NVIDIA | [inkling.md](models/inkling.md) | | `STRATA_IO_PF_AHEAD` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md) | | `STRATA_IO_PF_GATE` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md), [release-notes-0.2.0.md](release-notes-0.2.0.md) | | `STRATA_IO_PF_GATE_LO` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md) | diff --git a/docs/models/inkling.md b/docs/models/inkling.md index 815746520..b8f225410 100644 --- a/docs/models/inkling.md +++ b/docs/models/inkling.md @@ -96,6 +96,13 @@ by a kernel, so they no longer wait behind the experts copied over the bus. The added. On an RTX 2070 Super 8 GB a 2,048-token prompt reads 6.5% faster with IQ4_NL (477 against 448 tokens/s) and 2.7% faster with UD-Q2_K_XL; 32,768 tokens read as fast as before. `STRATA_INK_KERNEL_TABLES=0` turns it off. +`STRATA_INK_PRESTAGE=1` (opt-in, NVIDIA, with the pinned copy of the experts that Linux makes): in a prompt of more +than one batch, the next layer's missing experts are copied while the card works on the current layer. The staging +buffer then holds two layers, and the expert cache gives up that VRAM (about 1 GB on IQ4_NL) for the read only and +takes it back after. The answers are the same. On an RTX 2070 Super 8 GB a 2,048-token IQ4_NL prompt reads 12.6% +faster (541 against 480 tokens/s); UD-Q2_K_XL at 2,048 tokens is 1.3% slower and IQ4_NL at 32,768 tokens 1.9% +slower, as the smaller cache costs more than the copies save. + On a laptop with an RTX 4070 Laptop (8 GB), a Core i7-14700HX and 32 GB of RAM, on Windows, UD-Q3_K_XL has 1,216 of its 3,072 experts on the card and the rest are computed on the CPU. On a card that does not hold every expert, setup gives Inkling 2 Mini three settings that read fewer bytes for each token: `--attn-requant q5_0` (the attention's diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 35005b37e..ad3a04865 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -909,8 +909,8 @@ Engine::~Engine() { for (unsigned long long h : vpool_) cuMemRelease(h); // the pool goes back to the card with the engine vpool_.clear(); for (void* p : host_allocs_) cudaFreeHost(p); - for (void* e : {ev_stage_[0], ev_stage_[1], ev_ahead_, ev_free_, ev_in_[0], ev_in_[1], ev_out_[0], ev_out_[1], ev_hr_, - ev_saved_, ev_bell_}) + for (void* e : {ev_stage_[0], ev_stage_[1], ev_ahead_, ev_free_, ev_pre_[0], ev_pre_[1], ev_in_[0], ev_in_[1], + ev_out_[0], ev_out_[1], ev_hr_, ev_saved_, ev_bell_}) if (e) cudaEventDestroy((cudaEvent_t) e); for (void* e : swap_pin_ev_) if (e) cudaEventDestroy((cudaEvent_t) e); @@ -2536,7 +2536,9 @@ void Engine::ensure_stage() { if (opt_.metal_balanced && all_cached()) return; #endif if (stage_wide_ > stage_narrow_) { - vmm_reserve(stage_map_, stage_wide_ + 256 + kTail); + // prestage_: addresses for a second part as large, which a read maps (widen_stage()) + const size_t one = stage_wide_ + 256 + kTail; + vmm_reserve(stage_map_, prestage_ ? pad(one) + one : one); // with the pool the buffer comes out of it, and out of the expert cache when the pool lacks the room if (pool_on_ && pool_claimed_ && exp_) { const size_t need = up_to(stage_narrow_ + 256 + kTail, pg_); @@ -2746,7 +2748,10 @@ void Engine::widen_stage() { if (stage_lent_ || stage_wide_ <= stage_narrow_) return; if (up_) ck(cudaStreamSynchronize(cs(up_)), "the expert staging buffer"); ck(cudaStreamSynchronize(cs(st_)), "the expert staging buffer"); - const size_t want = up_to(stage_wide_ + 256 + kTail, pool_on_ ? pg_ : grain_) - stage_map_.mapped; + // prestage_: a second part as large, for the next layer run's experts while a run's calls go (prestage(), which + // copies from the pinned mirror only) + const size_t one = stage_wide_ + 256 + kTail, span = prestage_ && !mirror_of_.empty() ? pad(one) + one : one; + const size_t want = up_to(span, pool_on_ ? pg_ : grain_) - stage_map_.mapped; size_t free_b = 0, total_b = 0; ck(cudaMemGetInfo(&free_b, &total_b), "cudaMemGetInfo"); if (pool_on_) free_b = vpool_bytes(); // what the card has is the pool's @@ -2760,10 +2765,22 @@ void Engine::widen_stage() { } stage_lent_ = true; // narrow_stage() gives back what was cut even if the map below fails try { - vmm_map(stage_map_, stage_wide_ + 256 + kTail, SIZE_MAX, "the expert staging buffer"); + vmm_map(stage_map_, span, SIZE_MAX, "the expert staging buffer"); + stage_two_ = span > one ? pad(one) : 0; } catch (const std::runtime_error&) { - narrow_stage(); - return; + // without the second part each run's experts are copied when the run starts, as without prestage_ + bool mapped = false; + if (span > one) { + try { + vmm_map(stage_map_, one, SIZE_MAX, "the expert staging buffer"); + mapped = true; + } catch (const std::runtime_error&) { + } + } + if (!mapped) { + narrow_stage(); + return; + } } stage_bytes_ = stage_wide_; staged_il_ = -1; @@ -2778,6 +2795,7 @@ void Engine::narrow_stage() { vmm_unmap(stage_map_, stage_narrow_ + 256 + kTail); stage_bytes_ = stage_narrow_; staged_il_ = -1; + stage_at_ = stage_two_ = 0; stage_lent_ = false; if (exp_ && stage_cut_ > 0) grow_cache(stage_cut_, true); stage_cut_ = 0; @@ -2931,6 +2949,37 @@ void Engine::plan_ahead(Layer& L, int il) { L.ahead.resize(std::min(L.ahead.size(), stage_bytes_ / (L.gu_step + L.d_bytes))); } +int Engine::uncached(const Layer& L) const { return (int) std::count(L.slot.begin(), L.slot.end(), nullptr); } + +// prestage_: in read_groups(), the experts of the next layer run's layer (next_home_) that are not cached go to the +// staging buffer's other part (stage_two_) while this run's calls read theirs, on the upload stream behind the copy +// of this run's. That part was the run before's, whose last call the copy waits for (ev_free_ marks it until this +// call's experts are read). From the pinned mirror only: its copies are queued at once, where the pinned buffers' +// would hold this call's launches up while the CPU fills them. The next run takes them if its experts are still the +// ones copied (moe_rows()), else copies its own as without this. The experts, their layout and the launches are +// the same either way, so the results are too. +void Engine::prestage(int n) { + const int il = next_home_; + Layer& N = layers_[(size_t) il]; + if (!stage_two_ || mirror_of_.empty() || !batched(N, n)) return; + plan_ahead(N, il); + const int m = (int) N.ahead.size(); + if (m == 0 || m != uncached(N)) return; + pre_.il = il; + pre_.at = stage_at_ ? 0 : stage_two_; + pre_.experts = N.ahead; + pre_.ev ^= 1; + ck(cudaStreamWaitEvent(cs(up_), (cudaEvent_t) ev_free_, 0), "expert staging"); + stage(N, pre_.experts.data(), m, up_, false, stage_ + pre_.at); + ck(cudaEventRecord((cudaEvent_t) ev_pre_[pre_.ev], cs(up_)), "expert staging"); +} + +// A read is over, or given up: no copy of prestage()'s is left for a later call to take. +void Engine::drop_prestage() { + if (pre_.il >= 0 && up_) ck(cudaStreamSynchronize(cs(up_)), "expert staging"); + pre_.il = next_home_ = -1; +} + // The follower: an asked-for expert goes from the mapped file to a pinned buffer, and from there to its slot. // The file may have to be read for it, which is why this is not the engine's thread. Two buffers take turns: an // expert is copied into one while the other uploads, and the upload is the quicker of the two. (A buffer for @@ -4071,7 +4120,7 @@ void Engine::alloc() { if (!opt_.metal_balanced) #endif ensure_stage(); - for (void** e : {&ev_stage_[0], &ev_stage_[1], &ev_ahead_, &ev_free_}) { + for (void** e : {&ev_stage_[0], &ev_stage_[1], &ev_ahead_, &ev_free_, &ev_pre_[0], &ev_pre_[1]}) { cudaEvent_t ev = nullptr; ck(cudaEventCreateWithFlags(&ev, cudaEventDisableTiming), "cudaEventCreate"); *e = ev; @@ -5698,9 +5747,18 @@ void Engine::bell_job() { // m experts from the mapped file into the staging buffer: gate and up of each back to back from the start, the // downs after them. The two pinned buffers take turns, so the workers fill one while the other uploads. `alone`: -// stagers_ fill them, while the workers compute experts. -void Engine::stage(const Layer& L, const int32_t* experts, int m, void* stream, bool alone) { - staged_il_ = -1; // whatever the buffer held +// stagers_ fill them, while the workers compute experts. `to`: the buffer's second part, where prestage() copies +// the next layer run's experts while this run's calls read theirs. +void Engine::stage(const Layer& L, const int32_t* experts, int m, void* stream, bool alone, uint8_t* to) { + if (!to) { + staged_il_ = -1; // whatever the buffer held + if (pre_.il >= 0) { // a copy of prestage()'s may be on its way to this part: it is no one's now + if (stream != up_) ck(cudaStreamWaitEvent(cs(stream), (cudaEvent_t) ev_pre_[pre_.ev], 0), "expert staging"); + pre_.il = -1; + } + stage_at_ = 0; + to = stage_; + } for (Ahead& g : ahead_) { // vram_ahead()'s groups go, once the calls that read them are done if (g.il < 0) continue; ck(cudaStreamWaitEvent(cs(stream), (cudaEvent_t) g.read, 0), "expert staging"); @@ -5727,16 +5785,16 @@ void Engine::stage(const Layer& L, const int32_t* experts, int m, void* stream, stage_rest_.push_back(j); continue; } - piece(src, stage_ + (size_t) j * step, L.gu2()); - piece(src + L.gu2(), stage_ + down_at + (size_t) j * L.d_bytes, L.d_bytes); + piece(src, to + (size_t) j * step, L.gu2()); + piece(src + L.gu2(), to + down_at + (size_t) j * L.d_bytes, L.d_bytes); } if (!dma) { gather_pieces(q, k, stream); ck(cudaGetLastError(), "expert staging"); ck(cudaEventRecord((cudaEvent_t) gather_ev_[gather_at_], cs(stream)), "expert staging"); } - if (!stage_rest_.empty()) stage_rest(L, experts, stage_rest_, m, stream, alone); - ck(cudaMemsetAsync(stage_ + down_at + (size_t) m * L.d_bytes, 0, kTail, cs(stream)), "expert staging"); + if (!stage_rest_.empty()) stage_rest(L, experts, stage_rest_, m, stream, alone, to); + ck(cudaMemsetAsync(to + down_at + (size_t) m * L.d_bytes, 0, kTail, cs(stream)), "expert staging"); return; } #endif @@ -5768,20 +5826,20 @@ void Engine::stage(const Layer& L, const int32_t* experts, int m, void* stream, else get(h + c * step + j * L.d_bytes, L.exp_down + e * L.d_bytes, L.d_bytes, L.gu2()); }; (alone ? *stagers_ : *workers_).run((int) (3 * c), copy); - ck(cudaMemcpyAsync(stage_ + (size_t) j0 * step, h, c * step, cudaMemcpyHostToDevice, cs(stream)), + ck(cudaMemcpyAsync(to + (size_t) j0 * step, h, c * step, cudaMemcpyHostToDevice, cs(stream)), "expert staging"); - ck(cudaMemcpyAsync(stage_ + down_at + (size_t) j0 * L.d_bytes, h + c * step, c * L.d_bytes, + ck(cudaMemcpyAsync(to + down_at + (size_t) j0 * L.d_bytes, h + c * step, c * L.d_bytes, cudaMemcpyHostToDevice, cs(stream)), "expert staging"); ck(cudaEventRecord(left, cs(stream)), "expert staging"); } - ck(cudaMemsetAsync(stage_ + down_at + (size_t) m * L.d_bytes, 0, kTail, cs(stream)), "expert staging"); + ck(cudaMemsetAsync(to + down_at + (size_t) m * L.d_bytes, 0, kTail, cs(stream)), "expert staging"); } // stage()'s experts `which` (their places in experts[0..m)) that the mirror lacks: through the pinned buffers one by // one, each to its place. void Engine::stage_rest(const Layer& L, const int32_t* experts, const std::vector& which, int m, void* stream, - bool alone) { + bool alone, uint8_t* to) { const size_t step = L.gu_step, down_at = pad((size_t) m * step), one = L.gu2() + L.d_bytes; stage_pins(); const int per = (int) std::max(chunk_bytes_ / one, 1); @@ -5799,8 +5857,8 @@ void Engine::stage_rest(const Layer& L, const int32_t* experts, const std::vecto }); for (size_t i = 0; i < c; ++i) { const size_t j = (size_t) which[i0 + i]; - ck(cudaMemcpyAsync(stage_ + j * step, h + i * one, L.gu2(), cudaMemcpyHostToDevice, cs(stream)), "expert staging"); - ck(cudaMemcpyAsync(stage_ + down_at + j * L.d_bytes, h + i * one + L.gu2(), L.d_bytes, cudaMemcpyHostToDevice, + ck(cudaMemcpyAsync(to + j * step, h + i * one, L.gu2(), cudaMemcpyHostToDevice, cs(stream)), "expert staging"); + ck(cudaMemcpyAsync(to + down_at + j * L.d_bytes, h + i * one + L.gu2(), L.d_bytes, cudaMemcpyHostToDevice, cs(stream)), "expert staging"); } @@ -6351,7 +6409,12 @@ void Engine::moe_rows(Layer& L, int il, int n) { // request's: a frozen answer takes the staging as it finds it, which is what the first token saw. const bool mine = ahead && staged_il_ == il && (staged_ == L.ahead || (freeze_experts_ ? true : 2 * route_ms_ < ahead_ms_)); - const bool early = n_ahead > 0 && !mine; + // The run before may have copied them to the buffer's other part (prestage()). + const bool pre = n_ahead > 0 && !mine && pre_.il == il && pre_.experts == L.ahead; + void* const pre_done = ev_pre_[pre_.ev]; // (prestage() below starts the next run's copy) + if (!mine) pre_.il = -1; + if (pre) stage_at_ = pre_.at; + const bool early = n_ahead > 0 && !mine && !pre; if (early) { const Clock::time_point t = Clock::now(); // the buffer is free once the layer before has read it @@ -6365,9 +6428,14 @@ void Engine::moe_rows(Layer& L, int il, int n) { } } + // The next run's experts go to the other part while this run's calls are read, if this layer's that are not + // cached are all in this part (no later round of a call copies to the buffer). + pre_hits_ += pre; + if ((early || pre) && next_home_ >= 0 && n_ahead == uncached(L)) prestage(n); + const Clock::time_point t0 = Clock::now(); ck(cudaEventSynchronize((cudaEvent_t) ev_), "router"); - if (ahead && !early && !freeze_experts_) route_ms_ = ms_since(t0); + if (ahead && !early && !pre && !freeze_experts_) route_ms_ = ms_since(t0); stats_.route_wait_ms += ms_since(t0); const size_t cb_row = (size_t) n_expert_ + 1; @@ -6686,7 +6754,7 @@ void Engine::moe_rows(Layer& L, int il, int n) { if ((size_t) (last - first) == staged_.size() && std::equal(first, last, staged_.begin())) kept = r; } const int from = mine && kept < 0 ? 1 : 0; - holds = early ? 0 : kept; + holds = early || pre ? 0 : kept; for (int turn = 0; turn < n_round; ++turn) { const int r = kept < 0 ? (turn + from) % n_round : turn == 0 ? kept : turn - (turn <= kept ? 1 : 0); const int g0 = round_[(size_t) r], m = round_[(size_t) r + 1] - g0; @@ -6694,8 +6762,8 @@ void Engine::moe_rows(Layer& L, int il, int n) { const int r0 = round_row_[(size_t) r], rows = round_row_[(size_t) r + 1] - r0; if (r == kept) { // the buffer has it - } else if (r == 0 && early) { - ck(cudaStreamWaitEvent(cs(st_), (cudaEvent_t) ev_ahead_, 0), "expert staging"); + } else if (r == 0 && (early || pre)) { + ck(cudaStreamWaitEvent(cs(st_), (cudaEvent_t) (pre ? pre_done : ev_ahead_), 0), "expert staging"); } else if (rows > 0) { stage(L, group_.data() + g0, m, st_, n_cpu > 0); holds = r; @@ -6713,10 +6781,11 @@ void Engine::moe_rows(Layer& L, int il, int n) { if (top > 0) launch(k, top); } }; + uint8_t* const sb = stage_ + stage_at_; // (stage() above sets stage_at_ to 0) each_class([&](int k, int top) { const int32_t* b = n_tile == 1 ? d_bounds + g0 : d_tb + (size_t) k * tile_row + (size_t) (g0 - n_cached + r); - mmq_->mul(stage_, gt, gu_rows, n_embd_, step, m, mq_, b, iota_, q_rows, top, gu_, ff2, st_, tok_of); - up_mul(stage_, step, m, b, top); + mmq_->mul(sb, gt, gu_rows, n_embd_, step, m, mq_, b, iota_, q_rows, top, gu_, ff2, st_, tok_of); + up_mul(sb, step, m, b, top); }); ExpertScales er = es; if (er.ids) er.ids += r0; @@ -6734,7 +6803,7 @@ void Engine::moe_rows(Layer& L, int il, int n) { each_class([&](int k, int top) { const int32_t* b = (n_tile == 1 ? d_rel + (g0 - n_cached + r) : d_tb + (size_t) (n_tile + k) * tile_row + (size_t) (g0 - n_cached + r)); - mmq_->mul(stage_ + down_at, dt, n_embd_, ff, L.d_bytes, m, mq2_, b, d_place + r0, rows, top, dx_, n_embd_, + mmq_->mul(sb + down_at, dt, n_embd_, ff, L.d_bytes, m, mq2_, b, d_place + r0, rows, top, dx_, n_embd_, st_); }); if (guard) scale_rows(dx_, n_embd_, n_embd_, rows, d_place + r0, hf_ + r0, false, st_); @@ -8308,9 +8377,13 @@ bool Engine::read_groups(const Mark* marks, int n_marks, const std::function 1 && !(base = carry_take((size_t) g * per * sizeof(float), most)); g = (g + 1) / 2) { } g = std::max(g, 1); - int visits = 0; - for (int il = lo_; il < hi_; il = next_layer(il)) ++visits; + std::vector order; // the layer of each run, in the order the calls meet them + for (int il = lo_; il < hi_; il = next_layer(il)) order.push_back(il); + const int visits = (int) order.size(); const int groups = (n_calls + g - 1) / g, steps = groups * visits; + // prestage(): the next run's experts are copied while a run's calls go, where the buffer has its second part + const bool pre_on = prestage_ && stage_two_ && !mirror_of_.empty(); + pre_runs_ = pre_hits_ = 0; std::vector 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()) { @@ -8319,6 +8392,7 @@ bool Engine::read_groups(const Mark* marks, int n_marks, const std::function= opt_.stage_ahead; const auto restore = [&] { + drop_prestage(); r_ = r0; carry_at(nullptr); pos_ = pos0; @@ -8344,6 +8418,10 @@ bool Engine::read_groups(const Mark* marks, int n_marks, const std::function= 0 && on < n_layer_ && layers_[(size_t) on].moe ? on : -1; + pre_runs_ += layers_[(size_t) il].moe; for (int c = c0; c < c1; ++c) { const Call& call = calls_[(size_t) c]; r_ = rows_of(c); @@ -8372,6 +8450,10 @@ bool Engine::read_groups(const Mark* marks, int n_marks, const std::function& work); void trim_step_graphs(size_t keep, bool all); void moe_rows(Layer& L, int il, int n); - void stage(const Layer& L, const int32_t* experts, int m, void* stream, bool alone = false); + void stage(const Layer& L, const int32_t* experts, int m, void* stream, bool alone = false, uint8_t* to = nullptr); + void prestage(int n); + void drop_prestage(); + int uncached(const Layer& L) const; void stage_rest(const Layer& L, const int32_t* experts, const std::vector& which, int m, void* stream, - bool alone); + bool alone, uint8_t* to); // The pinned mirror (build_mirror()): the routed experts the card does not hold, in pinned host memory in a // card slot's layout. void build_mirror(); @@ -1029,6 +1032,10 @@ class Engine { /// (about 100 KB) waited behind the layer's experts on their way to the staging buffer. Inkling 2's on CUDA /// unless STRATA_INK_KERNEL_TABLES=0, else off. bool kernel_tables_ = false; + /// read_groups() copies the next layer run's experts that are not cached into a second part of the staging + /// buffer while a run's calls are read (prestage()). The buffer then takes two layers' VRAM from the expert + /// cache during a read (widen_stage()). Inkling 2's on CUDA with STRATA_INK_PRESTAGE=1, else off. + bool prestage_ = false; std::vector layers_; ///< the model's, and after them the draft block Dev out_norm_, output_; @@ -1215,6 +1222,17 @@ class Engine { void* ev_hr_ = nullptr; ///< marks the card's last read of h_r_ by draft_input() int staged_il_ = -1; ///< the layer whose experts the buffer holds for its next call, or -1 std::vector staged_; ///< those experts, in the buffer's order: one round of the layer's + size_t stage_at_ = 0; ///< where in stage_ they are: 0, or stage_two_ (prestage()) + size_t stage_two_ = 0; ///< where the buffer's second part starts while a read maps it, or 0 + struct Prestaged { + int il = -1; ///< the layer whose experts prestage() copies, or -1 + size_t at = 0; ///< where in stage_ + std::vector experts; ///< that layer's L.ahead when the copy started + int ev = 0; ///< which of ev_pre_ marks its end + } pre_; + void* ev_pre_[2] = {}; ///< mark the ends of prestage()'s copies, in turn + int next_home_ = -1; ///< read_groups(): the layer of the next run, for prestage(), or -1 + int pre_runs_ = 0, pre_hits_ = 0; ///< a read's layer runs, and the ones prestage() copied for double ahead_ms_ = 0, route_ms_ = 0; ///< what that layer's copy ahead took, and the card to its routing bool shared_ahead_ = false; ///< read() runs several calls: each copies ahead, whatever its size size_t stage_bytes_ = 0, chunk_bytes_ = 0; diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index c20d37404..4f9933c94 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -216,6 +216,14 @@ void Inkling2::load_geometry() { const char* v = std::getenv("STRATA_INK_KERNEL_TABLES"); kernel_tables_ = !(v && std::string(v) == "0"); } + // STRATA_INK_PRESTAGE=1: a prompt's read copies the next layer run's experts that are not cached while a run's + // calls go, into a second part of the staging buffer that the expert cache lends for the read (prestage()). + // The experts and the launches are the same, so the results are too. Opt-in: the cache holds fewer experts + // during the read. + { + const char* v = std::getenv("STRATA_INK_PRESTAGE"); + prestage_ = v && std::string(v) == "1"; + } #endif clamp_exp_.assign((size_t) n_layer_ + 1, INFINITY); clamp_shexp_ = clamp_exp_; diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 1b115a170..63acf18c9 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -16716,6 +16716,41 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_INK_PRESTAGE", + "backends": [ + "cuda" + ], + "reads": [ + { + "file": "engine/src/poly/inkling2.cpp", + "line": 193, + "backend": "cuda", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": "off", + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [ + "docs/models/inkling.md:93" + ], + "first_commit": null, + "first_date": "2026-11-23", + "since": null, + "last_commit": null, + "last_date": "2026-11-23", + "status": "opt-in", + "owner": "poly", + "note": "off unless =1: Inkling 2 on CUDA, in a prompt read of several calls (read_groups()), the staging buffer gets a second part as large (one layer's experts, about 627 MB of IQ4_NL's), lent by the expert cache for the read only and given back after it (widen_stage()/narrow_stage()), and the next layer run's uncached experts are copied there from the pinned mirror while the current run computes (prestage()). Same bits: the same experts, layout and launches. Opt-in because the cache holds fewer experts during the read. Needs the pinned mirror (Linux default); HIP and Metal never take it (engine/src/poly/inkling2.cpp, engine.cpp prestage())", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_INK_PROJECT_MIXED", "backends": [ From 3c7d85d8beca23318c708ed0815419ef54e6e3a3 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:52:16 +0900 Subject: [PATCH 2/2] poly: say so when STRATA_INK_PRESTAGE=1 cannot engage Prestage runs in read_groups() on CUDA. On HIP and Metal it is not built, and with --draft-model Inkling 2's prompt reads go call by call (carry_rows() is 0), so the switch did nothing without a word. Load now prints a line in both cases. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/inkling2.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index 4f9933c94..adf4ec90b 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -224,6 +224,9 @@ void Inkling2::load_geometry() { const char* v = std::getenv("STRATA_INK_PRESTAGE"); prestage_ = v && std::string(v) == "1"; } +#else + if (const char* v = std::getenv("STRATA_INK_PRESTAGE"); v && std::string(v) == "1") + std::fprintf(stderr, "strata-poly: STRATA_INK_PRESTAGE=1 is CUDA only: off\n"); #endif clamp_exp_.assign((size_t) n_layer_ + 1, INFINITY); clamp_shexp_ = clamp_exp_; @@ -316,6 +319,8 @@ void Inkling2::load_geometry() { "attention.sliding_window_pattern differs between a layer's applications"); } if (!opt_.draft_model.empty()) ds_open(); + if (prestage_ && carry_rows() == 0) // read_groups() prestages, and the drafter's prompt reads go call by call + std::fprintf(stderr, "strata-poly: STRATA_INK_PRESTAGE=1 does nothing with --draft-model: off\n"); if (opt_.window == 0) opt_.window = ds_file_ ? ds_block_ : 1; }