From c5d7a9eadfac9604434f09c0a3485efab69d08d2 Mon Sep 17 00:00:00 2001 From: Sarah Date: Fri, 4 Dec 2026 23:57:29 +0100 Subject: [PATCH] [Engine] poly: opt-in --stage-next: with the staging ring, a layer's call copies the next layer's experts A prompt read on a card that holds few of the experts copies a layer's experts outside VRAM ahead of the routing (moe_rows()), into the staging buffer, which is at most one layer wide; with --stage-ring its two halves take turns. The next layer's copy goes out only when the host reaches that layer, after waiting for this layer's router, so on an RTX 4060 Laptop with Qwen4-35B-A3B UD-IQ4_XS the upload copy engine was idle for 11-12 ms of every 38-47 ms layer of a 2,048-token prompt, and neither the kernels (60%) nor the copies (45%) kept the card busy. --stage-next 1 (STRATA_POLY_STAGE_NEXT=1), with --stage-ring 1, in a read of one or two calls through the layers in turn (not read_groups(), and not with three or more calls, whose packs' rows come from pinned memory through the same copy engine) and on an architecture whose call row tables go to the card by a kernel (kernel_tables_; a copy of them would wait behind the next layer's experts): - widen_stage() maps two layers' experts for the read instead of one (Options::stage_next, stage_wide1_ the one- layer width it falls back to when two do not fit, as with a frozen cache), so a layer's experts are one round in one half; - moe_rows()'s card() plans the next MoE layer's rounds (plan_ahead() does not depend on the routing) and copies each into the half this call's round two before it leaves, or at once when that half is free; the next layer's call finds them (ring_next_mine) and copies nothing; - the call's end waits for the halves it used, not the next layer's; anything that drops the ring's halves drops the next layer's copy too (drop_ring_next(), which lets the upload stream finish first). Off by default; nothing changes without it. RTX 4060 Laptop 8 GB (PCIe 4.0 x8), CUDA 13.4, Qwen4-35B-A3B UD-IQ4_XS on this branch plus PR #440 and the Qwen4 row-table kernel (separate change), --kv q8_0, 2,048-token prompt, 2 runs each: 1,224.8 / 1,225.2 tokens/s against 1,039.7 / 1,040.4 with --stage-ring 1 alone (main and PR #440 without either setting: 948.6 +- 9.6, 3 runs); the copies now run back to back. A 6,000-token prompt 1,136.9 / 1,125.1 against 1,124.9 / 1,114.4 (inactive there by design). Inkling 2 Mini UD-Q4_K_XL (read_groups(), so inactive), 2,047 tokens: 674.8 / 674.8 against 664.7 / 669.9. The same tokens in every pair. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018j62UGHq9crCFFy7fHrCcW --- docs/SETTINGS.md | 3 +- docs/models/qwen4.md | 13 +++ engine/src/poly/engine.cpp | 144 ++++++++++++++++++++++++--- engine/src/poly/engine.hpp | 16 ++- engine/src/poly/main.cpp | 9 +- terminal/data/settings-register.json | 35 +++++++ 6 files changed, 201 insertions(+), 19 deletions(-) diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index d653eace9..2350b998e 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 (365) +## Switches you can turn on (366) Off unless you set them. The last column says where each one is described. @@ -267,6 +267,7 @@ Off unless you set them. The last column says where each one is described. | `STRATA_PIPE_BYIELD_NODRAIN` | on / off | NVIDIA and AMD | [BATCHING.md](BATCHING.md) | | `STRATA_PLE_COMPACT` | on / off | NVIDIA and AMD | [DETAILS.md](DETAILS.md) | | `STRATA_PLE_COPY_STREAM` | on / off | NVIDIA and AMD | [DETAILS.md](DETAILS.md) | +| `STRATA_POLY_STAGE_NEXT` | on / off | NVIDIA and AMD | [qwen4.md](models/qwen4.md) | | `STRATA_POLY_STAGE_RING` | on / off | NVIDIA and AMD | [inkling.md](models/inkling.md) | | `STRATA_POLY_STREAM_STAGE` | number | NVIDIA and AMD | [DETAILS.md](DETAILS.md) | | `STRATA_POOL_CORES` | on / off | NVIDIA and AMD, Intel Arc | [DETAILS.md](DETAILS.md), [release-notes-0.2.2.md](release-notes-0.2.2.md) | diff --git a/docs/models/qwen4.md b/docs/models/qwen4.md index d1ae2bd0d..38c3a3905 100644 --- a/docs/models/qwen4.md +++ b/docs/models/qwen4.md @@ -185,3 +185,16 @@ All numbers below were measured directly on `strata-poly` with greedy decoding u | Apple M5 Ultra (256 GB, macOS 27.2.1) | Metal | UD-Q4_K_XL | 8K → 256K NIAH | Embedded MTP (FP16 KV) | 2,206.5 → 1,228.8 | 120.8 → 105.0 | 100% PASS (8K/32K/64K/128K/256K), ~5.5 GiB KV growth | | Apple M5 Ultra (256 GB, macOS 27.2.1) | Metal | UD-Q4_K_XL | 256K NIAH | `--no-draft` (FP16 KV) | 1,235.0 | 75.3 | 100% PASS (`AMBER-4921`) | | AMD HIP (not yet measured on hardware) | HIP | — | — | ROCm `10.2.0a20260930` build verified | — | — | Compiled and linked cleanly; not yet run on physical AMD GPU | + +## Prompt reads on a card that holds few of the experts + +On an 8 GB card a prompt of 2,048 tokens copies almost every routed expert the card does not hold to the card once a +layer (13.6 GB with UD-IQ4_XS on an RTX 4060 Laptop), and the copies, not the multiplications, set the pace. Two +opt-in settings keep the card's one upload copy engine busy: `--stage-ring 1` splits the staging buffer into two halves +so a round's copy runs while the round before it is multiplied, and `--stage-next 1` (with the ring, CUDA, a read of +one or two `--prompt-batch` calls) widens the buffer to two layers while the read lasts and copies the next layer's +experts into the half the current layer does not use. Measured on that laptop (UD-IQ4_XS, `--kv q8_0`, 2,048 tokens, +three runs each, the same tokens in every run): 1,040 tokens/s with `--stage-ring 1` and 1,225 with +`--stage-ring 1 --stage-next 1`, where the row tables of a prompt call go to the card by a kernel +(`STRATA_QWEN4_KERNEL_TABLES`, on by default); without that, the tables' own copy waits behind the next layer's +experts and `--stage-next` stays off. A 6,000-token prompt (six calls) reads as with the ring alone. diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 746492db8..1f9134af6 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -1448,6 +1448,7 @@ bool Engine::ensure_vram_for_kv(size_t more, bool allow_reclaim) { stage_bytes_ = stage_narrow_; staged_il_ = -1; ring_il_ = -1; + drop_ring_next(); stage_lent_ = false; const size_t freed = vpool_bytes() > before ? vpool_bytes() - before : 0; const size_t used = more > before ? std::min(more - before, freed) : 0; @@ -1517,6 +1518,7 @@ bool Engine::ensure_vram_for_kv(size_t more, bool allow_reclaim) { stage_bytes_ = stage_narrow_; staged_il_ = -1; ring_il_ = -1; + drop_ring_next(); stage_lent_ = false; budget_ += stage_cut_; stage_cut_ -= std::min(stage_cut_, more); @@ -2283,6 +2285,7 @@ void Engine::shrink_cache(size_t bytes) { exp_at_ = std::min(exp_at_, end); staged_il_ = -1; ring_il_ = -1; + drop_ring_next(); } float* Engine::fbuf(size_t n) { @@ -3174,6 +3177,7 @@ void Engine::resize_card_cache(int mib) { refill_.clear(); staged_il_ = -1; ring_il_ = -1; + drop_ring_next(); cached_ = 0; exp_at_ = 0; if (exp_ && vmm_) { @@ -3372,11 +3376,13 @@ void Engine::lend_prompt() { grow_cache(bytes, true); } -void Engine::widen_stage() { - if (stage_lent_ || stage_wide_ <= stage_narrow_) return; +void Engine::widen_stage(bool two_layers) { + // STRATA_POLY_STAGE_NEXT: two layers' width only for a read whose layers copy the next one's experts (read()) + size_t wide = two_layers || !stage_wide1_ ? stage_wide_ : std::min(stage_wide_, stage_wide1_); + if (stage_lent_ || 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; + const size_t want = up_to(wide + 256 + kTail, 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 @@ -3397,14 +3403,28 @@ 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_, wide + 256 + kTail, SIZE_MAX, "the expert staging buffer"); } catch (const std::runtime_error&) { - narrow_stage(); - return; + // STRATA_POLY_STAGE_NEXT's two layers did not fit (a frozen cache gives none back): one layer, as without it + bool one = false; + if (stage_wide1_ > stage_narrow_ && stage_wide1_ < wide) { + try { + vmm_map(stage_map_, stage_wide1_ + 256 + kTail, SIZE_MAX, "the expert staging buffer"); + wide = stage_wide1_; + one = true; + } catch (const std::runtime_error&) { + } + } + if (!one) { + narrow_stage(); + return; + } } - stage_bytes_ = stage_wide_; + stage_bytes_ = wide; + stage_two_ = wide > stage_wide1_ && stage_wide1_ > 0; staged_il_ = -1; ring_il_ = -1; + drop_ring_next(); } // The read is done: the staging buffer goes back to stage_narrow_, and the expert cache takes back what it gave @@ -3416,8 +3436,10 @@ void Engine::narrow_stage() { ck(cudaStreamSynchronize(cs(st_)), "the expert staging buffer"); vmm_unmap(stage_map_, stage_narrow_ + 256 + kTail); stage_bytes_ = stage_narrow_; + stage_two_ = false; staged_il_ = -1; ring_il_ = -1; + drop_ring_next(); stage_lent_ = false; } if (exp_ && stage_cut_ > 0) grow_cache(stage_cut_, true); @@ -3475,6 +3497,7 @@ void Engine::trim_stage() { stage_bytes_ = stage_narrow_ = stage_wide_ = std::min(was, stage_map_.mapped - 256 - kTail); // whole pieces' worth staged_il_ = -1; ring_il_ = -1; + drop_ring_next(); const int before = cached_; // STRATA_GPU_EXPERTS holds the cache to its N: the buffer is cut all the same, and the VRAM stays free. A freeze // (STRATA_FREEZE_EXPERTS) holds the cache still from the first request on, not during the load. @@ -3598,6 +3621,7 @@ int Engine::grow_cache(size_t bytes, bool later) { } staged_il_ = -1; ring_il_ = -1; + drop_ring_next(); held_is_budget(); return (int) taken.size(); } @@ -3639,6 +3663,13 @@ void Engine::plan_ahead(Layer& L, int il) { L.ahead.resize(std::min(L.ahead.size(), cap)); } +// STRATA_POLY_STAGE_NEXT: the next layer's experts on their way into a half are not wanted any more: their copy is +// done before the buffer is anything else's. +void Engine::drop_ring_next() { + if (ring_next_il_ >= 0 && up_) ck(cudaStreamSynchronize(cs(up_)), "expert staging"); + ring_next_il_ = -1; +} + // STRATA_POLY_STAGE_RING: the staging buffer as two halves, each holding `*half` experts of the largest layer's size, // `ring_stride_` bytes apart. False where the buffer holds too few experts for it, or there is no upload stream. bool Engine::ring_plan(int* half) { @@ -3820,6 +3851,7 @@ void Engine::follow(int written) { ++stats_.swapped_experts; staged_il_ = -1; // the staging buffer holds what was not cached ring_il_ = -1; + drop_ring_next(); } n_free += s.at.load() == Swap::kFree; } @@ -3932,6 +3964,7 @@ void Engine::follow(int written) { given = nullptr; staged_il_ = -1; ring_il_ = -1; + drop_ring_next(); coming_[(size_t) in.il * ne + (size_t) in.e] = 1; from_pool(*s); s->mirror = mirror(in.il, in.e); @@ -4047,6 +4080,8 @@ void Engine::load(const Options& o) { // STRATA_POLY_STAGE_RING=1: --stage-ring 1 if (const char* v = std::getenv("STRATA_POLY_STAGE_RING"); v && *v) opt_.stage_ring = v[0] != '0'; + // STRATA_POLY_STAGE_NEXT=1: --stage-next 1 + if (const char* v = std::getenv("STRATA_POLY_STAGE_NEXT"); v && *v) opt_.stage_next = v[0] != '0'; // STRATA_POLY_STAGED_TILES=8,16,32: a prompt batch's staged experts go to llama.cpp's MMQ in size classes, the // experts of up to 8 rows in one launch, up to 16 in the next, up to 32 in the next, the rest in the last. One // launch takes its tile width from its largest expert, and a tile computes that many columns whatever rows @@ -4960,8 +4995,14 @@ void Engine::alloc() { chunk_bytes_ = std::max(kChunkBytes, one_expert); // With stage_read_mib the buffer's addresses are reserved for the larger size, and a read maps the rest. stage_narrow_ = stage_wide_ = stage_bytes_; - if (widens && read_mib > 0) - stage_wide_ = std::max(stage_bytes_, std::min((size_t) read_mib << 20, (size_t) n_expert_ * one_expert)); + // STRATA_POLY_STAGE_NEXT with the ring: two layers' worth, a half a layer, so that the next layer's experts go + // into the half this layer does not use while its calls multiply theirs (moe_rows()) + const size_t layers_wide = opt_.stage_ring && opt_.stage_next ? 2 : 1; + if (widens && read_mib > 0) { + stage_wide_ = std::max(stage_bytes_, + std::min((size_t) read_mib << 20, layers_wide * (size_t) n_expert_ * one_expert)); + stage_wide1_ = std::max(stage_bytes_, std::min((size_t) read_mib << 20, (size_t) n_expert_ * one_expert)); + } #if defined(STRATA_VMM_RELEASE_WITH_ADDRESSES) && !defined(STRATA_GPU_METAL) // trim_stage(): mapped in pieces, which it gives back once the cache is filled. Not a cudaMalloc it frees and // makes again smaller: on ROCm 7.2.4 (an RX 9070 XT) the expert cache's addresses then could not be reserved @@ -5913,6 +5954,7 @@ void Engine::vram_ahead(int j, int n, size_t part) { ahead_turn_ = (ahead_turn_ + 1) % kAheadGroups; staged_il_ = -1; // the staging buffer holds other experts now ring_il_ = -1; + drop_ring_next(); ck(cudaStreamWaitEvent(cs(up_), (cudaEvent_t) g.read, 0), "upload ahead"); uint8_t* at = stage_ + (size_t) me * ahead_slots_ * ahead_stride_; for (int i = 0; i < g.n; ++i, at += ahead_stride_) { @@ -7549,7 +7591,13 @@ bool Engine::bell_job(uint32_t seq) { void Engine::stage(const Layer& L, const int32_t* experts, int m, void* stream, bool alone, size_t at) { uint8_t* const sb = stage_ + at; // stage_ring: the half of the buffer the round goes to staged_il_ = -1; // whatever the buffer held - if (!ring_staging_) ring_il_ = -1; // (the ring's own copies keep what the halves hold) + if (!ring_staging_) { // (the ring's own copies keep what the halves hold) + ring_il_ = -1; + if (ring_next_il_ >= 0) { // STRATA_POLY_STAGE_NEXT: the next layer's copy is in before this one overwrites it + ck(cudaStreamWaitEvent(cs(stream), (cudaEvent_t) ev_slot_ready_[ring_next_base_ & 1u], 0), "expert staging"); + ring_next_il_ = -1; + } + } 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"); @@ -8441,13 +8489,21 @@ void Engine::moe_rows(Layer& L, int il, int n) { const bool ring_mine = ring && n_ahead > 0 && ring_il_ == il && ring_ahead_.size() == (size_t) n_ahead && std::equal(L.ahead.begin(), L.ahead.begin() + n_ahead, ring_ahead_.begin()); if (ring_mine) ring_base = ring_held_base_; + // STRATA_POLY_STAGE_NEXT: the last call of the layer before copied this layer's rounds into the halves as its own + // rounds left them (card() below); they are this call's when they are the same + const bool ring_next_mine = ring && !ring_mine && n_ahead > 0 && ring_next_il_ == il && + ring_next_ahead_.size() == (size_t) n_ahead && + std::equal(L.ahead.begin(), L.ahead.begin() + n_ahead, ring_next_ahead_.begin()); + if (ring_next_mine) ring_base = ring_next_base_; + if (ring_next_il_ == il) ring_next_il_ = -1; // taken, or a copy the up stream orders before this call's own + const bool ring_have = ring_mine || ring_next_mine; const bool mine = !ring && ahead && staged_il_ == il && (staged_ == L.ahead || (freeze_experts_ ? true : 2 * route_ms_ < ahead_ms_)); const bool early = n_ahead > 0 && !mine; int n_early = 0; // the ring's rounds copied already if (early && ring) { for (int c = 0; c * ring_n < n_ahead; ++c, ++n_early) - if (!ring_mine) + if (!ring_have) ring_copy(L, L.ahead.data() + (size_t) c * ring_n, std::min(ring_n, n_ahead - c * ring_n), (ring_base + (unsigned) c) & 1u); } else if (early) { @@ -8805,6 +8861,37 @@ void Engine::moe_rows(Layer& L, int il, int n) { // by the copy of the round two on, into the half it has just left. std::vector ring_sent; const auto round_rows = [&](int r) { return round_row_[(size_t) r + 1] - round_row_[(size_t) r]; }; + // STRATA_POLY_STAGE_NEXT: the last call of this layer in a read goes on into the next MoE layer's rounds, the + // ones copied ahead of its routing (plan_ahead() does not depend on it): round k of that layer is "round + // n_round + k" of this one, copied into the half this call's round n_round + k - 2 leaves, or at once where + // that half is free already. The bus then copies the next layer's experts while this one multiplies and + // the next one's attention runs, where it waited for the host to reach the next layer. Only where every + // round of both layers is one copied ahead, so no later round of either takes a half the other holds. + Layer* LN = nullptr; + int nx_il = -1, nx_n = 0, nx_rounds = 0; + if (ring && opt_.stage_next && ring_next_read_ && ring_next_il_ < 0 && n_early == n_round && n_round <= 2) { + int j = il + 1; + while (j < hi_ && !layers_[(size_t) j].moe) ++j; + if (j < hi_) { + Layer& LJ = layers_[(size_t) j]; + plan_ahead(LJ, j); + int outside = 0; + for (int e = 0; e < n_expert_; ++e) outside += LJ.slot[(size_t) e] ? 0 : 1; + const int nj = (int) LJ.ahead.size(), rj = (nj + ring_n - 1) / ring_n; + // a round of the next layer into a half this layer's later calls read only after the last of them + const bool safe = ring_last_call_ || (n_round == 1 && rj == 1); + if (nj > 0 && nj == outside && rj <= 2 && safe) { + LN = &LJ; + nx_il = j; + nx_n = nj; + nx_rounds = rj; + } + } + } + const auto next_copy = [&](int k) { // the next layer's round k + const int a = k * ring_n; + ring_copy(*LN, LN->ahead.data() + a, std::min(ring_n, nx_n - a), (ring_base + (unsigned) (n_round + k)) & 1u); + }; const auto ring_after = [&](int r) { if (round_rows(r) > 0) ck(cudaEventRecord((cudaEvent_t) ev_slot_free_[(ring_base + (unsigned) r) & 1u], cs(st_)), "expert staging"); @@ -8814,7 +8901,10 @@ void Engine::moe_rows(Layer& L, int il, int n) { (ring_base + (unsigned) nx) & 1u); ring_sent[(size_t) nx] = 1; } + if (LN && nx >= n_round && nx - n_round < nx_rounds) next_copy(nx - n_round); }; + if (LN) // the next layer's rounds whose half no round of this call takes + for (int k = 0; k < nx_rounds && n_round + k - 2 < 0; ++k) next_copy(k); if (ring) { ring_sent.assign((size_t) n_round, 0); for (int c = 0; c < n_early && c < n_round; ++c) ring_sent[(size_t) c] = 1; @@ -8896,13 +8986,20 @@ void Engine::moe_rows(Layer& L, int il, int n) { if (guard) scale_rows(dx_, n_embd_, n_embd_, rows, d_place + r0, hf_ + r0, false, st_); if (ring) ring_after(r); } + if (LN) { // the next layer's call finds its rounds in the halves (moe_rows(): ring_next_mine) + ring_next_il_ = nx_il; + ring_next_ahead_.assign(LN->ahead.begin(), LN->ahead.end()); + ring_next_base_ = ring_base + (unsigned) n_round; + ring_next_rounds_ = nx_rounds; + } ck(cudaEventRecord((cudaEvent_t) ev_free_, cs(st_)), "expert staging"); }; if (n_cpu == 0) { card(); if (ring) { // whatever the call copied is in before anything else reads or writes the buffer const bool all_ahead = n_ahead > 0 && n_round == n_early; - if (all_ahead) { + // (with the next layer's rounds on their way into a half of this one's, the halves are not this layer's) + if (all_ahead && (ring_next_il_ < 0 || n_round + ring_next_rounds_ <= 2)) { ring_il_ = il; ring_ahead_.assign(L.ahead.begin(), L.ahead.begin() + n_ahead); ring_held_base_ = ring_base; @@ -8910,8 +9007,12 @@ void Engine::moe_rows(Layer& L, int il, int n) { ring_il_ = -1; } ring_seq_ = ring_base + (unsigned) n_round; - for (int h = 0; h < 2; ++h) - ck(cudaStreamWaitEvent(cs(st_), (cudaEvent_t) ev_slot_ready_[h], 0), "expert staging"); + for (int h = 0; h < 2; ++h) { // (not the next layer's halves: its call waits for them, STRATA_POLY_STAGE_NEXT) + bool next = false; + for (int k = 0; k < (ring_next_il_ >= 0 ? ring_next_rounds_ : 0); ++k) + next = next || (unsigned) h == ((ring_next_base_ + (unsigned) k) & 1u); + if (!next) ck(cudaStreamWaitEvent(cs(st_), (cudaEvent_t) ev_slot_ready_[h], 0), "expert staging"); + } } } else { // the card's part on a thread of its own, and the CPU's on the workers if (!stagers_) stagers_ = std::make_unique(4); // one thread copied slower than the bus @@ -10714,7 +10815,9 @@ bool Engine::read(const int32_t* tokens, int n, const Mark* marks, int n_marks, settle.now(); return true; } - widen_stage(); + // STRATA_POLY_STAGE_NEXT: where a call's row tables go to the card by a kernel (kernel_tables_): a copy of them + // would wait behind the next layer's experts on the card's one upload copy engine + widen_stage(opt_.stage_ring && opt_.stage_next && kernel_tables_ && !groups && n_packs <= 2); turn = share_to(hi_); if (pos_ + n > opt_.max_context) throw std::runtime_error("the context is full (" + std::to_string(opt_.max_context) + " tokens)"); @@ -10777,6 +10880,13 @@ bool Engine::read(const int32_t* tokens, int n, const Mark* marks, int n_marks, // 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) + // STRATA_POLY_STAGE_NEXT: not where the packs' rows come from pinned memory (host): those copies would wait behind + // the next layer's experts on the card's one copy engine for uploads (6,000 tokens read 35% slower) + ring_next_read_ = !host && stage_two_; + struct NextOff { + bool& b; + ~NextOff() { b = false; } + } next_off{ring_next_read_}; try { for (int k = 0; k < turns; ++k) { const int il = lo_ + k; @@ -10795,7 +10905,9 @@ bool Engine::read(const int32_t* tokens, int n, const Mark* marks, int n_marks, const Pack& pk = packs_[(size_t) p]; if (host) ck(cudaStreamWaitEvent(cs(st_), (cudaEvent_t) ev_in_[p % 2], 0), "read"); if (il < n_layer_ && pk.n > 1) { + ring_last_call_ = p == n_packs - 1; // (STRATA_POLY_STAGE_NEXT: the layer's last call) layer_pack(il, pk, half(p)); + ring_last_call_ = false; } else { for (int c = pk.first; c < pk.first + pk.n; ++c) { const Call& call = calls_[(size_t) c]; @@ -10804,7 +10916,9 @@ bool Engine::read(const int32_t* tokens, int n, const Mark* marks, int n_marks, pos_ = call.pos; tok_ = call.tok; if (il < n_layer_) { + ring_last_call_ = p == n_packs - 1 && c == pk.first + pk.n - 1; layer(il, call.m); + ring_last_call_ = false; } else { if (draft_h_from_residual()) { const size_t dh = (size_t) draft_h_floats(); diff --git a/engine/src/poly/engine.hpp b/engine/src/poly/engine.hpp index c36a571ca..1b0a4fe67 100644 --- a/engine/src/poly/engine.hpp +++ b/engine/src/poly/engine.hpp @@ -89,6 +89,10 @@ struct Options { ///< the copy of a round (and of the next layer's first) goes on while the round before ///< it is multiplied, where one buffer holds the card until a layer's rounds are done ///< (STRATA_POLY_STAGE_RING=1, --stage-ring 1; off: as before) + bool stage_next = false; ///< with stage_ring, in a read of one or two calls: the staging buffer is two layers + ///< wide while the read lasts, and a layer's experts go to one half while the next + ///< layer's are copied into the other, so the bus does not wait for the host to + ///< reach the next layer (STRATA_POLY_STAGE_NEXT=1, --stage-next 1; off: as before) int stage_ahead = 1024; ///< from this many tokens, a call routes to most of a layer's experts, and the ///< ones that are not cached start their copy before the layer has routed; in a ///< read of several calls and this many tokens every call does; 0: never. Under @@ -889,6 +893,7 @@ class Engine { void stage_rest(const Layer& L, const int32_t* experts, const std::vector& which, int m, void* stream, bool alone, size_t at = 0); bool ring_plan(int* half); + void drop_ring_next(); // 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(); @@ -991,7 +996,7 @@ class Engine { float* scratch_batch_ = nullptr; void use_scratch(bool batch); void fill_now(); - void widen_stage(); + void widen_stage(bool two_layers = false); void narrow_stage(); void trim_stage(); void resize_card_cache(int mib); ///< resize_cache() and mind_vram() for this engine's card @@ -1413,6 +1418,15 @@ class Engine { ///< copied them all ahead and nothing else, so the next call of it finds them std::vector ring_ahead_; ///< stage_ring: those experts, in the halves' order unsigned ring_held_base_ = 0; ///< stage_ring: the half the first of them is in, by parity + int ring_next_il_ = -1; ///< stage_ring: the next layer whose experts are on their way into a + ///< half (Options::stage_next), or -1 + std::vector ring_next_ahead_; ///< stage_ring: those experts, in their order + unsigned ring_next_base_ = 0; ///< stage_ring: the half their first round goes to, by parity + int ring_next_rounds_ = 0; ///< stage_ring: their rounds (1 or 2) + bool ring_last_call_ = false; ///< stage_ring: this call is the layer's last in the read + size_t stage_wide1_ = 0; ///< stage_ring: a read's width for one layer (STRATA_POLY_STAGE_NEXT's fallback) + bool stage_two_ = false; ///< stage_ring: the read widened the buffer to two layers (widen_stage()) + bool ring_next_read_ = false; ///< stage_ring: a read whose layers may copy the next one's experts 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 diff --git a/engine/src/poly/main.cpp b/engine/src/poly/main.cpp index d3fbff614..444164ba7 100644 --- a/engine/src/poly/main.cpp +++ b/engine/src/poly/main.cpp @@ -2343,9 +2343,13 @@ void usage() { " --stage-ring 0|1 a batch's staged experts go through the staging buffer's two halves in turn,\n" " each round copied while the one before it is multiplied (default 0; also\n" " STRATA_POLY_STAGE_RING=1); no more VRAM, needs --stage-ahead\n" + " --stage-next 0|1 with --stage-ring 1, a read of one or two batches widens the staging buffer\n" + " to two layers' experts, and the next layer's are copied while a layer is\n" + " multiplied (default 0; also STRATA_POLY_STAGE_NEXT=1)\n" " --stage-read-mib N a prompt read of several batches grows the staging buffer to N MiB (at\n" - " most a layer's experts) with VRAM the expert cache lends it until the\n" - " answer starts (default 2048 with the 128 MiB --stage-mib default; 0 = never)\n" + " most a layer's experts, two with --stage-next 1) with VRAM the expert cache\n" + " lends it until the answer starts (default 2048 with the 128 MiB --stage-mib\n" + " default; 0 = never)\n" " --cpu-share F under that, the share of those experts the CPU computes meanwhile, the\n" " ones the fewest tokens chose (default: as the times say; 0 = none)\n" " --prompt-cache N checkpoints kept of a conversation, about 150 MB of RAM each (default 6;\n" @@ -2605,6 +2609,7 @@ int main(int argc, char** argv) { else if (a == "--stage-mib") o.stage_mib = std::atoi(val()); else if (a == "--stage-ahead") o.stage_ahead = std::atoi(val()); else if (a == "--stage-ring") o.stage_ring = std::atoi(val()) != 0; + else if (a == "--stage-next") o.stage_next = std::atoi(val()) != 0; else if (a == "--stage-read-mib") o.stage_read_mib = std::atoi(val()); else if (a == "--cpu-share") o.cpu_share = (float) std::atof(val()); else if (a == "--prompt-cache") cache.o.checkpoints = std::atoi(val()); diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 739763688..018d525bd 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -35915,6 +35915,41 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_POLY_STAGE_NEXT", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/poly/engine.cpp", + "line": 4081, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": null, + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [ + "docs/models/qwen4.md" + ], + "first_commit": null, + "first_date": null, + "since": null, + "last_commit": null, + "last_date": null, + "status": "opt-in", + "owner": "poly", + "note": "1 (--stage-next 1), with the staging ring (--stage-ring 1) and an architecture whose prompt row tables go to the card by a kernel (kernel_tables_: Inkling 2, Qwen4 with STRATA_QWEN4_KERNEL_TABLES): a prompt read of one or two calls through the layers in turn widens the staging buffer to two layers' experts, and each layer's call copies the next MoE layer's experts into the half it does not use while it multiplies (moe_rows()), so the uploads do not wait for the host to reach the next layer. Not in reads of three or more calls (their packs' rows come from pinned memory through the same upload copy engine) nor in read_groups(); one layer's width when two do not fit (a frozen cache). Off by default", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_POLY_STAGE_RING", "backends": [