diff --git a/docs/DETAILS.md b/docs/DETAILS.md index 5405ce29..377ea096 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -1548,6 +1548,58 @@ expert here: none was page-locked): A coding agent's recorded conversation on the two cards (a 100K-token start, then 8 turns of 1-5K tokens of code, 128 tokens written a turn, the same tokens read by both): 137.4 s off, 130.6 s auto (reading 121.1 -> 114.3 s). +## Missed experts in a verify window: the tail is skipped (on by default on CUDA, `STRATA_ROUTE_TAIL_SKIP`) + +When a card cannot hold every routed expert, most of a decode token's waiting time is spent on the experts that are not in VRAM: +their copy over PCIe or their rows on the CPU pool. In a verify window of several tokens, an expert that **every** token of the window +routes at the bottom of its top 10 (rank 7 or lower, the smallest weights) adds little to any of them. **0.1.42: on by default on CUDA builds**: +such a missed expert is neither copied nor computed, its contribution is zero, and the other experts keep the weight the router gave them +(no renormalisation). An expert that any token ranks above 7, and every expert already in VRAM, is served as before. + +**`STRATA_ROUTE_TAIL_SKIP=0` turns it off** and restores the answers of a build without it byte for byte (checked: `mg_norepeat` 10 of 10 and `exact_pp` 4096 and 20000 +tokens identical to 0.1.42 without the change, on the RTX 3060, the RTX A4000 alone and as a 2-GPU split, and the Tesla P100). Another rank +(`STRATA_ROUTE_TAIL_SKIP=8`, `=9`; 1-9 are accepted) is taken as given. The start-up line +`STRATA_ROUTE_TAIL_SKIP=7 is ON (default)` names the opt-out. + +Where it applies: serve on a CUDA build with one GPU or a layer split (every stage skips by itself). Not applied, and no line printed: when every expert is +in VRAM (nothing is missed), with `--batch` / `parallel` slots (a window then mixes requests, and one request's skip would depend on another's +tokens: kept off until measured), with a peer-expert tier or a helper GPU, on HIP and SYCL builds. The request log gets +`route tail skip: N missed experts skipped` (cumulative) while it is active. + +**Answers differ slightly from 0.1.41** (`STRATA_ROUTE_TAIL_SKIP=0` brings them back). Teacher-forced KL of the decode path (the default's own greedy text and the default sampled at T=0.8, 28 chat prompts of code, +reasoning, chat, six languages and long-document summaries, 220 tokens each, about 6,000 scored tokens per set, through the real verify windows with the MTP drafts; +`phaseA-tests/cuda-route-kl.md`): + +| | RTX 3060 12 GB, IQ3_XXS | RTX A4000 16 GB, Q2_0 | +|---|---|---| +| KL(0.1.41 behaviour \|\| tail skip), mean, greedy / sampled text | 0.0058 / 0.0056 | 0.0032 / 0.0030 | +| p99, max (greedy) | 0.061, 0.29 | 0.035, 0.32 | +| top-1 token agreement | 97.7% / 97.5% | 98.2% / 98.2% | +| perplexity of the forced text, skip / no skip | x1.011 / x1.007 | x1.005 / x1.003 | +| KL of the first token after the prompt | 0.024 / 0.033 | 0.015 / 0.016 | +| the same engine run twice (the floor; adaptive tier and CPU/GPU split depend on timing) | 0.0006-0.0007 | 0.0007 | +| `STRATA_PREFILL_CPU_SHARE` on / off (already the default) | 0.0006-0.0007 (first token 0.002-0.0035) | 0.0005-0.0007 (first token 0.002-0.004) | +| Q2_0 against IQ3_XXS, the same text | - | 0.1006 (p99 1.08, top-1 90.6%) | + +The effect is not even: code and reasoning prompts have a KL of 0.002-0.004, chat and non-English prompts 0.004-0.009. +Task check, greedy, thinking off (HumanEval every second problem, 82; 30 generated reasoning tasks), default / tail skip: +RTX 3060 76 and 78 of 82 / 78 of 82, reasoning 27 and 27 / 29; RTX A4000 76 and 76 / 76, reasoning 27 and 30 / 27. The runs do not separate the settings +(one run each; a change of 3 reasoning tasks is noise). + +Decode speed, `STRATA_ROUTE_TAIL_SKIP` default against `=0`, 5 interleaved rounds on fresh engines, a story, a code answer and a 6K-token document, 200 tokens each, +median over the rounds (ratio of tokens/s, default over `=0`; rounds in which the default was faster): + +| machine | story | code | 6K document | all three | +|---|---|---|---|---| +| RTX 3060 12 GB, IQ3_XXS, resident experts | 1.104 (5/5) | 1.197 (5/5) | 1.241 (5/5) | **1.156 (5/5)** | +| RTX A4000 16 GB alone, Q2_0 | 1.151 (5/5) | 1.135 (5/5) | 1.101 (5/5) | **1.129 (5/5)** | +| Tesla P100 16 GB, IQ3_XXS, `--pcie-frac 0.29` | 1.149 (5/5) | 1.182 (5/5) | 1.118 (5/5) | **1.146 (5/5)** | +| two RTX A4000, layer split (hit rate 99.7%) | 0.987 (0/5) | 1.004 (5/5) | 1.002 (3/5) | 0.998 (1/5) | + +The gain follows how much of a token is spent waiting for missed experts (hit rate 75-90% on the single cards in the profile, `phaseA-tests/cuda-profile.md`); a 2-GPU +split that holds nearly every expert (hit rate 99.7%) gains nothing. It does not touch prompt reading. Switches that stay opt-in and are not part of this: +`STRATA_ROUTE_PRIOR` (adds speed, doubles the KL), `STRATA_ROUTE_RESIDENT` and `STRATA_ROUTE_RESIDENT_MTP` (the latter is not faster than the former). + ## More opt-in switches measured for 0.1.41 (all off unless you set them) None of these changes the default answers; each was measured against the default with interleaved off/on pairs of whole diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index 861d9e82..98dae6ce 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -463,6 +463,10 @@ void expert_pool_dispatch(void* user, const float* x_f, const int32_t* ids, cons /// Plan v0.3 P6: the pool for a verify window of `n_tok` tokens. `x_f` is (n_tok, n_embd), `ids` (n_tok, k) and /// `out` (n_tok * k, n_embd). Each distinct missed expert is computed once for all the tokens routed to it; /// resident experts' rows are zeroed (the GPU adds them). Requires `host_res` (the token-graph residency). +/// STRATA_ROUTE_TAIL_SKIP rank (0: off). Unset in the environment: 0 until the serve start-up sets the default. +int tail_skip_rank(); +void set_tail_skip_rank(int r); + void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32_t* ids, int64_t n_tok, int64_t k, float* out); diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index add047d0..8f29affc 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -3370,6 +3370,23 @@ constexpr int64_t kMaxWindowEntries = 128; static_assert(strata::kernels::cpu::MAXT * 10 <= kMaxWindowEntries, "a verify window's entries overflow the tables"); } // namespace +namespace { +// STRATA_ROUTE_TAIL_SKIP: the rank from which a missed expert that every token of the window routes that low is skipped. An +// explicit environment value is read once; the serve start-up (generate.cpp) sets the CUDA default where it applies. +std::atomic g_tail_skip_rank{-1}; // -1: not resolved yet +} // namespace +int tail_skip_rank() { + int r = g_tail_skip_rank.load(std::memory_order_relaxed); + if (r < 0) { + const char* v = std::getenv("STRATA_ROUTE_TAIL_SKIP"); + r = v != nullptr ? std::atoi(v) : 0; + r = r > 0 && r < 10 ? r : 0; + g_tail_skip_rank.store(r, std::memory_order_relaxed); + } + return r; +} +void set_tail_skip_rank(int r) { g_tail_skip_rank.store(r > 0 && r < 10 ? r : 0, std::memory_order_relaxed); } + void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32_t* ids, int64_t n_tok, int64_t k, float* out) { using namespace strata::kernels::cpu; @@ -3431,14 +3448,10 @@ void expert_pool_dispatch_multi(ExpertDispatch& d, const float* x_f, const int32 !(d.peer != nullptr && d.peer->has(d.layers, e)) && !helper_holds(e)) ++nmiss; } } - // STRATA_ROUTE_TAIL_SKIP=R (opt-in, changes the output): a missed expert that EVERY token of the window routes + // STRATA_ROUTE_TAIL_SKIP=R (default 7 on CUDA serve, =0 off; changes the output): a missed expert that EVERY token of the window routes // only at rank >= R (the smallest weights of the top-10) is neither copied nor computed; its contribution is // zero, without renormalisation (the others keep the weight the router gave them). Less PCIe and CPU work. - static const int tail_rank = [] { - const char* v = std::getenv("STRATA_ROUTE_TAIL_SKIP"); - const int r = v != nullptr ? std::atoi(v) : 0; - return r > 0 && r < 10 ? r : 0; - }(); + const int tail_rank = tail_skip_rank(); bool skip_d[kMaxWindowEntries]; for (int q = 0; q < nd; ++q) skip_d[q] = false; if (tail_rank > 0 && d.peer == nullptr && d.remote_count == 0) { diff --git a/src/program/generate.cpp b/src/program/generate.cpp index d3eb73e5..d625cba8 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -8012,6 +8012,25 @@ int main(int argc, char** argv) { drive.d.pcie_num = std::max(0, std::min(256, (int) (o.pcie_frac * 256.0 + 0.5))); const bool all_experts_resident = !host_res.empty() && std::all_of(host_res.begin(), host_res.end(), [](int32_t r) { return r >= 0; }); + // STRATA_ROUTE_TAIL_SKIP: ON BY DEFAULT on CUDA builds (owner decision, 0.1.42): a missed expert that every token of + // a verify window routes at rank 7 or lower is neither copied nor computed (answers differ slightly from 0.1.41, + // KL ~0.003-0.006; docs/DETAILS.md). =0 restores 0.1.41's bytes. Nothing to do, so no default and no line, when every + // expert is in VRAM; off with --batch slots (a window then mixes requests, and one request's skip would depend on + // another's tokens) and, as in the dispatch, with a peer or helper GPU. A layer split is fine: each stage skips by itself. + { + const char* tse = std::getenv("STRATA_ROUTE_TAIL_SKIP"); +#if !defined(STRATA_USE_HIP) + const bool tail_default_ok = tse == nullptr && !all_experts_resident && o.batch == 0 && o.peer_device < 0; +#else + const bool tail_default_ok = false; // HIP and SYCL stay as they were +#endif + if (tail_default_ok) strata::core::set_tail_skip_rank(7); + if (strata::core::tail_skip_rank() > 0 && !all_experts_resident) + std::fprintf(stderr, "strata serve: STRATA_ROUTE_TAIL_SKIP=%d is ON%s: a missed expert that every token of a verify window routes at rank %d or " + "lower is skipped (+10..20%% decode on 12-16 GB cards that miss experts, answers differ slightly from 0.1.41); " + "STRATA_ROUTE_TAIL_SKIP=0 turns it off\n", strata::core::tail_skip_rank(), + tse == nullptr ? " (default)" : "", strata::core::tail_skip_rank()); + } if (o.adapt_every > 0 && o.adapt_swaps > 0 && !all_experts_resident) drive.d.usage.assign((size_t) (g.n_layers * g.n_expert), 0.0f); // #477 --expert-profile-save: what the adaptive tier learned, kept across restarts (opt-in; off: `heat` stays