From 63b4bf0c1e72c8e2ef2089227b96fb3f6a7d751a Mon Sep 17 00:00:00 2001 From: Constantin Date: Thu, 1 Oct 2026 23:10:39 -0400 Subject: [PATCH] decode: wait for an adaptive expert swap before reading the residency table Greedy decode was not reproducible on this box. Repeated identical requests diverged from about the 28th generated character, and the drafter, the expert routing and the first verify window were all byte-stable, which left the adaptive tier as the only thing left that could move a result. It is. `apply_pending(false)` polls the swap's event with a NON-BLOCKING cudaEventQuery, so whether the experts an adapt round just copied in were resident for the next window depended on whether their PCIe copies had landed. Resident runs the GPU kernel; non-resident is computed on the CPU, and on a machine without AVX-512 those two kernels round differently enough to move a top logit by 0.26 - measured at the window right after the swap was issued. From there the trajectory is a different one, permanently. Waiting is the fix. The copies were issued during the previous window's drafting, so the wait is short, and in exchange the residency table is a function of what was actually copied rather than of how fast the disk was. With the default --adapt-every 4 left in place, five runs are now byte-identical across all 24 verify windows and emit the identical 40-token stream; before the fix three of five agreed and two forked at window 16. --adapt-every 0 (frozen placement) was already deterministic, which is what identified the tier. The STRATA_TRACE_ADAPT lines added with the probe are env-gated and off by default: they report whether a round considered, skipped, or swapped, and whether the previous round's copies had landed. No cost measurement is claimed here: this says the trajectory is a function of the inputs again, not what the wait does to decode throughput. (cherry picked from commit 1e3888a73b69a332585056af5b86643ff8367242) --- src/program/generate.cpp | 48 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index fa84081a..a02b3e3d 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -5518,7 +5518,13 @@ int main(int argc, char** argv) { drive.d.layers = 0; drive.d.experts = 0; drive.d.failed = false; - apply_pending(false); + // THE WAIT IS THE FIX. A non-blocking query here left the next window's residency table to + // depend on whether the previous round's expert copies had landed: an expert that is resident + // runs the GPU kernel and one that is not is computed on the CPU, and the two round + // differently enough to move a logit by ~0.26. Waiting costs the copies' remaining time on a + // window that follows one that issued them, and buys a residency table that is a function of + // what was actually copied - which is what makes greedy decode reproducible. + apply_pending(true); if (hist_n > 0) { // the tails the penalties count over, ONE PER ROW: the tokens the state has consumed, the // fed-back head `x` (it joins `consumed` only after this window commits), then the drafts @@ -6215,10 +6221,24 @@ int main(int argc, char** argv) { std::vector> pending; cudaEvent_t adapt_ev = nullptr; cudaEventCreateWithFlags(&adapt_ev, cudaEventDisableTiming); + int64_t adapt_rounds = 0; // counted here: `rounds` is declared below the adapt lambda auto apply_pending = [&](bool wait) { if (pending.empty()) return; if (wait) cudaEventSynchronize(adapt_ev); - else if (cudaEventQuery(adapt_ev) != cudaSuccess) return; + // STRATA_TRACE_ADAPT: a non-blocking query means the next window reads the residency table + // with whatever the copy has achieved. Whether the incoming experts are RESIDENT for that + // window depends on copy latency, and a resident expert runs a different kernel than a + // CPU-computed one - a far bigger numerical difference than a rounding wobble. + static const bool trace_pending = std::getenv("STRATA_TRACE_ADAPT") != nullptr; + if (wait) cudaEventSynchronize(adapt_ev); + else if (cudaEventQuery(adapt_ev) != cudaSuccess) { + if (trace_pending) + std::fprintf(stderr, "strata: PENDING not landed, %zu stay non-resident this window\n", + pending.size()); + return; + } + if (trace_pending) + std::fprintf(stderr, "strata: PENDING landed, %zu experts become resident\n", pending.size()); src.commit_exchanges(); // the resident RAM mode: the evicted experts take their places in RAM for (const auto& [i, slot] : pending) host_res[(size_t) i] = slot; pending.clear(); @@ -6230,7 +6250,18 @@ int main(int argc, char** argv) { // routed clearly more often. Copies run between rounds, when the GPU is idle. auto adapt = [&]() -> bool { const Clock::time_point ta = Clock::now(); - if (!pending.empty()) return true; // the previous swaps are still in flight + ++adapt_rounds; + // STRATA_TRACE_ADAPT: why an adapt round did or did not swap. Default off, one getenv, and it + // reports the only thing that can make an adapt round a coin flip: whether the PREVIOUS round's + // asynchronous expert copies had landed by the time this round started. + static const bool trace_adapt = std::getenv("STRATA_TRACE_ADAPT") != nullptr; + if (!pending.empty()) { + if (trace_adapt) + std::fprintf(stderr, "strata: ADAPT round=%lld SKIPPED, %zu swaps still in flight\n", + (long long) adapt_rounds, pending.size()); + return true; // the previous swaps are still in flight + } + if (trace_adapt) std::fprintf(stderr, "strata: ADAPT round=%lld considering\n", (long long) adapt_rounds); struct Swap { float gain; int32_t layer, in, out; }; std::vector swaps; std::vector> cand, vict; @@ -6274,6 +6305,9 @@ int main(int argc, char** argv) { pending.emplace_back((int32_t) in, slot); // resident once the copy has landed } if (!swaps.empty()) cudaEventRecord(adapt_ev, adapt_stream); + if (trace_adapt) + std::fprintf(stderr, "strata: ADAPT round=%lld swapped %zu of %d slots, usage decayed\n", + (long long) adapt_rounds, swaps.size(), o.adapt_swaps); for (float& v : drive.d.usage) v *= 0.7f; swaps_total += (int64_t) swaps.size(); ms_adapt += std::chrono::duration(Clock::now() - ta).count(); @@ -6342,7 +6376,13 @@ int main(int argc, char** argv) { drive.d.layers = 0; drive.d.experts = 0; drive.d.failed = false; - apply_pending(false); + // THE WAIT IS THE FIX. A non-blocking query here left the next window's residency table to + // depend on whether the previous round's expert copies had landed: an expert that is resident + // runs the GPU kernel and one that is not is computed on the CPU, and the two round + // differently enough to move a logit by ~0.26. Waiting costs the copies' remaining time on a + // window that follows one that issued them, and buys a residency table that is a function of + // what was actually copied - which is what makes greedy decode reproducible. + apply_pending(true); if (!ver.run(T, window.data(), p, &drive_pool_multi, &drive, outv.data(), err)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1;