mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #1181] [CLOSED] perf(expert): the verify window's GPU plan in O(n) instead of O(n^2) #3289
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#3289
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/1181
Author: @ZhongUncle
Created: 10/6/2026
Status: ❌ Closed
Base:
main← Head:perf/expert-plan-linear📝 Commits (1)
42d72acperf(plan): the verify window's GPU plan in O(n) instead of O(n^2)📊 Changes
4 files changed (+551 additions, -88 deletions)
View changed files
📝
CMakeLists.txt(+8 -0)📝
include/strata/core/expert_source.hpp(+30 -0)📝
src/core/expert_source.cpp(+175 -88)➕
tests/core/expert_window_plan_test.cpp(+338 -0)📄 Description
The verify window's GPU plan is built with O(n²) scans, and the GPU spin-waits on flag A while it is built: the plan is "decided and published FIRST so the GPU starts while the CPU works", and flag A rises only after it, at
P.publish. Plan time is therefore GPU idle time on the flag-A critical path, even where its share of the round is too small to show end-to-end (see Limits). The quadratic parts: a pairwise first-occurrence scan (expert_source.cpp:2462), three full re-scans of the window per distinct expert (kind assignment, VRAM and PCIe group entries), and astd::finddedup of the CPU-miss prefetch (expert_source.cpp:2598).This PR rewrites all of it as three linear passes with output-identical results. It deliberately does not change what the plan decides — the tiers, the PCIe share and the group order are exactly the old ones. The case for taking it is structural: no behavior change (pinned by a 4,020-case differential test, no GPU needed), and the quadratic term it removes grows with the window, so a bigger
--specwidens the saving.TL;DR
--stats: the dispatchplanfigure halves in both rig states seen here (four interleaved pairs); decode unchanged, as expected — the ≈0.3–0.4% gain is below the ±0.7% run-to-run spreadWhat changes
The planner is extracted as
detail::window_gpu_plan(the header's existingdetailtest-seam pattern) and rewritten:first_ofscanstd::findBounds are the old ones: the call site already guards
n <= kMaxWindowEntries(128) and falls back to the simple no-plan loop beyond it, so the 256-slot table never exceeds load ½; out-of-range ids group by the same equality the pairwise scan used and stay kind −1, refused later by the pool loop with the same message. Unchanged by construction: group order (routing order of first occurrence), entry order within a group, counts, kinds.Measured results
Measured on:
--spec 4 --spec-min-p 0.5+ the MTP drafter (verify window up to 6 tokens)Standalone planner, ns per window-layer (512 experts, ~70% resident, 1,000 pre-generated windows cycled through, 200,000 rounds per arm):
(Per 48-layer window that is 86 → 38 µs at 3×10 up to 734 → 154 µs at 12×10 of GPU wait. The window grows with
--spec: 6×10 is this config's maximum, 12×10 is near the 128-entry cap. The gap widens with window size because the old cost grows with distinct × n.)Engine A/B —
dispatchline sections in ms per window-round, mean of two replicated pairs per rig state (four interleaved pairs total; raw lines in the appendix):(decode: baseline 95.7–97.1, patched 95.8–96.8 tok/s over four runs each — ranges overlap; durations in the appendix.)
Findings
The new planner's output is identical to the old one's.
tests/core/expert_window_plan_test.cppdrivesdetail::window_gpu_planand a reference copy of the O(n^2) planner with the same inputs and compares every positional field — kinds, groupptr/start/start2/dst/tok, the group counts,nmiss, the DMA source list — on 20 targeted corners (the PCIe share's last-m boundary, the staging cap, peer/helper-held experts, out-of-range ids,pcie_mode0/1/2, per-slot cache offsets) and 4,000 randomized windows: identical in all 4,020. The reference is the replaced block with only the mechanical adaptation to the injected predicates, diff-verified against the base commit; the production predicates are the old inline expressions verbatim.Standalone plan time drops 3.2× at this config's max window, 4.8× near the entry cap. 5,103 → 1,576 ns per window-layer at 6×10 (245 → 76 µs of GPU wait per 48-layer window); the old cost grows with distinct × n, the new one with n — measured across six window sizes from 2.3× at 3×10 to 4.8× at 12×10 (table above; the in-engine figure is finding 3). (6 tokens is the cap at
--spec 4because the default suffix drafter may lengthen a window by 2 —generate.cpp:2091; the log's "rounds of 6" in the appendix confirms it is reached.)In the engine, the dispatch
planfigure halves and nothing else moves. 0.19 → 0.09 and 0.14 → 0.07 ms per window-round, replicated in both states; activation-quantize, jobs and run are unchanged within noise state-for-state (the run section's pair deltas are −0.01 to +0.22 ms).The engine's absolute numbers sit below the micro-benchmark's 48-layer extrapolation for two reasons. The
plansegment times more than the planner —begin_layer, usage accounting, the flag-raisingP.publishand the PCIeP.fetchissue share the segment and are untouched, so the ~0.07 ms that remains is those parts plus the now-small planner. And the A/B windows are smaller than the benchmark's headline arms: the logged lookup counts put the mean window at ≈3.3×10 slow-state and ≈2.4×10 fast-state (308,226 lookups ÷ 195 rounds ÷ 48 layers ≈ 33 and 283,774 ÷ 244 ÷ 48 ≈ 24 entries per window-layer — PCIe-served misses excluded; the draft counts agree: 1 + 458/195 ≈ 3.3 and 1 + 355/244 ≈ 2.4 tokens). The per-layer saving (105 µs/round ÷ 48 ≈ 2.2 µs slow-state, 67 ÷ 48 ≈ 1.4 fast-state) is 1.4–2.2× the benchmark's ≈1.0 µs at 3×10 — the expected direction, since the benchmark runs warm (L3-resident working set, regular branches, fixed residency — see Limits) while the engine is cold and irregular, and plan cost is convex in window size, so a spread of real windows costs more than the mean window. The micro-benchmark is the scaling story; the engine figure is the realistic one.Decode tok/s is unchanged, and that is the expected outcome, not hidden work. Within each pair the two arms produce identical progress lines (greedy sampling; every logged counter matches — same rounds, same acceptance, same expert lookups, see the appendix): 195 rounds in the slow state, 244 in the fast, so a round is 27.3 ms slow-state and 21.8 fast-state. The plan saving (105 µs slow-state, 67 fast-state) is an expected ≈0.4% / ≈0.3% decode gain, below the ±0.7% run-to-run spread of the four runs (95.7–97.1 tok/s). The removed plan time is real dead time on the flag-A critical path; it becomes visible end-to-end where its share is larger (bigger
--specwindows, slower CPUs — see Limits).Validation
--statsdispatch lines on four interleaved baseline/patched pairs, 512 generated tokens after an 873-token prompt (finding 3)82f46a8c) — see finding 1ctest -R expert_window_planpasses in ~0.6 s with no GPUctestgreen exceptple_parityandexpert_multi_test, which fail identically on the base commit on this host (missing model files, no AVX512)Limits
1735d647, still fetchable by SHA on GitHub); its build inputs are hash-identical to this PR's base —src9f4e26be,included4bf5e4b,cmake478c0fb6,CMakeLists.txtcd605177in both commits (the rewrite touched no C++ or build files)Reproduce
The standalone micro-benchmark is not part of this PR — it is the test's driver with a timing loop; its parameters are in the appendix.
Raw `--stats` lines and the micro-benchmark
Engine A/B, all eight runs — 873-token task prompt, 512 generated tokens, interleaved baseline/patched pairs, slow/fast = the two rig states seen across the four pairs (clock and trajectory both differ between them; supports findings 3 and 4):
(r1 and r2 baseline differ by 0.3 ms of decode duration — 512/5.3171 = 96.294 and 512/5.3174 = 96.288, both 96.29 at two decimals.)
Two things worth knowing about those lines. The section timer is
steady_clockaccumulated in double ms (expert_source.cpp:2676), nanosecond-resolution — not a coarse timer. Sampling is greedy and every run within a state is identical in every logged counter (slow: 195 rounds, drafts accepted 318 of 458, lookups 277,710 of 308,226 with 30,516 refused; fast: 244, 268 of 355, 260,078 of 283,774 with 23,696 — each patched run matches its baseline partner), so the pure host segmentsplanandjobsrepeat to three decimals whilequantizeandrun, containing GPU work, jitter normally. Refusals are timing-dependent yet reproduce exactly within a state — apparently the state's timing margins are wide enough that the same experts miss their window in every replica; between states the refusal pattern shifts the logits, hence the different trajectories and round counts.Standalone planner micro-benchmark — Xeon E5-2680 v4, 512 experts, ~70% resident, 1,000 pre-generated windows cycled through, 200,000 rounds per arm, quiet machine (supports finding 2). Run single-threaded on the otherwise-idle rig with the benchmark core at turbo (~3.2 GHz; frequency polled during a re-run, all six arms reproduced within noise):
Work with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.