mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #1237] [CLOSED] perf(expert): the cache fill's stage buffers pinned with cudaHostAlloc #3328
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#3328
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/1237
Author: @ZhongUncle
Created: 10/6/2026
Status: ❌ Closed
Base:
main← Head:perf/stage-buf-pinned📝 Commits (1)
f3b78a0perf(expert): the stage buffers the cache fill copies from, pinned with cudaHostAlloc📊 Changes
2 files changed (+33 additions, -3 deletions)
View changed files
📝
include/strata/core/expert_source.hpp(+10 -1)📝
src/core/expert_source.cpp(+23 -2)📄 Description
blob()serves experts from the stage buffers whenever the resident expert arena is opted out of (--mmap-experts) or too small for the expert set. The stage buffers are the pool that assembles each blob from the GGUF's three role slices.They are allocated with a bare
new[], so they are pageable. AcudaMemcpyAsyncfrom pageable memory first copies the data into a driver-internal bounce buffer, on the calling thread, before the DMA can start. Every consumer pays this per expert: the profile prefill's blocking fill (generate.cpp:4114), the lent-slot refills, the adaptive tier's swaps, and the admit path where it runs.This PR allocates the stage buffers with
cudaHostAllocinstead, falling back to pageable per buffer when the driver refuses (pinned memory is scarce on exactly the small-RAM machines this path serves). Nothing else changes: same pool, same claim/reuse policy, same bytes copied — a buffer whose only job is to be the source of an async H2D copy should be pinned.TL;DR — pinning the stage buffers makes every staged H2D copy cheaper. The cost is ≈0.3 s slower startup (profile prefill ≈7%; ≈0.2 s of it measured pinning cost, the rest unexplained) and ~386 MiB of pinned RAM. It only matters when
blob()serves from the stage buffers; otherwise they sit idle. Decode tok/s is roughly flat.cudaMemcpyAsyncfrom pageable vs 2–3 µs from pinned; end-to-end 219–256 → 144–145 µs (1.5–1.8×), the pinned copy at 10.8 of the card's probed 11.1 GB/s--mmap-expertsin both arms: the adaptive tier's swap round 4.20–4.30 → 0.90–1.18 ms (3.6–4.8×, four interleaved pairs); the lent-slot refill 165–169 → 104–105 ms for 530 slots (1.6×)blob()directly and the stage buffers sit idle — this PR is for the configs where they don'tWhat changes
claim_stage:new uint8_t[stage_blob_]→cudaHostAlloc(..., cudaHostAllocDefault), with a per-buffer pageable fallbackunique_ptrgains a small deleter that frees each buffer the way it was allocatedcudaHostAllocDefault, notMapped: a stage buffer is only ever the source of an H2D copy, never addressed by the devicestage_mu_exactly where thenew[]was; a failedcudaHostAlloc's sticky error is cleared on the spotcudaHostAllocserialization reaches decodeclose()path that alreadycudaFreeHosts the complement and exchange buffers — no new lifetime questionsMeasured results
Measured on:
--mmap-expertsin both arms (see the scope note),--spec 4 --spec-min-p 0.5+ the MTP drafterMicro-benchmark — per-copy cost of one 1.5 MiB blob, 64-buffer rotating ring (no buffer is re-read within 64 copies), 512 copies per arm per variant, quiet machine:
cudaMemcpyAsyncnew[]new[]fill_stageassembles the blob and the cache fill copies it right awayEngine A/B — four interleaved baseline/patched pairs, 512 generated tokens after an 873-token prompt (raw lines in the appendix):
Findings
generate.cpp:10610) issues one async H2D per swapped expert fromblob()'s stage buffer, on its own thread; the logged ms/round times that loop. From pageable memory each issue costs the ~110–170 µs bounce copy measured in the micro-benchmark, from pinned memory ~3 µs. Decode tok/s does not move (finding 4): at this swap cadence (every 4 rounds) the copies are already off the critical path. The gain is headroom on the swap thread, which should matter at a higher cadence or on a slower CPU; the copies are also expected to overlap the MTP drafting that follows (the loop's own comment,generate.cpp:10615).generate.cpp:10180) queues 530 staged copies and waits once: pageable pays ~117 µs more per slot than pinned (313 → 197 µs), the bounce-staging term.blob()(three memcpys out of the mapped GGUF) then a blockingcudaMemcpy. The sweep of 10,244 distinct experts grows the stage pool to its ~257-buffer steady state before recycling kicks in; a buffer becomes reusable 256 assemblies after its last use.cudaHostAllocof 1.5 MiB measures 831 µs here vs 0.2 µs fornew[], so pinning the ~257 buffers accounts for ≈0.21 s — about two-thirds of the measured mean slowdown (4.5 → 4.8 s, +0.3 s); the remaining ~0.1 s is unexplained. Per-copy costs are otherwise strictly lower when pinned (findings 1–2), the fill loop is dominated by the mmap read (~440 µs per slot), and the loss is paid once at startup.Validation
82f46a8c) vs patched binaries on four interleaved pairs of identical--mmap-expertsruns; the micro-benchmark isolates the mechanismFileExpertSource: the stage buffers are pinned (cudaHostAlloc)exactly once; no baseline run doesslot 0 verifiedpasses in every run (the prefill reads one slot back and compares bytes)ctest87/89 —ple_parityandexpert_multi_testfail identically on the base commit on this host (the first needs the Q2_0 pack's GGUF, not present here; the second needs AVX512, which this Xeon lacks)Limits
blob()— the stage buffers are idle, and nothing measurable changes. The measured--mmap-expertsconfig stands in for any machine whose arena is smaller than its expert set.--prefilloff, that growth — and its ~0.2 s — moves out of startup and into the first stage-buffer users: the lent-slot refill alone queues 530 slots, more than the pool's steady state, so the refill would pay it inside its own ~0.1 s window. That configuration was not measured.expert_source.cpp:2721) is not measurable in this pack'sgenerateconfigs:--specrequires the token graph, and the graph decides hits on the device with residency frozen per token — "nothing is admitted during a token" (expert_source.cpp:2391; all four runs logged0 admitted). That path consumes the same buffers through the samefill_slot, so it stands to gain what findings 1–2 measure; it is cited by code inspection, not measured here.Reproduce
The micro-benchmark is not part of this PR; its parameters and build line are in the appendix, and its ~100-line source can be posted as a comment here if anyone wants to rerun it.
Raw `--stats` lines and the micro-benchmark
Engine A/B, all eight runs — 873-token task prompt, 512 generated tokens, interleaved baseline/patched pairs; both arms
--mmap-experts(supports findings 1–4):The trajectory families above are discussed in finding 4: replicas within an arm mostly agree in every logged counter (baseline r2–r4, patched r1/r2/r4); baseline r1's slow decode is the cold first run of the session (14.2 s; every later run 12.5–13.1 s), and its lookup total matches the patched family — so the families' arm correlation is weaker than it first looks.
Micro-benchmark — RTX 3080, 1.5 MiB blobs, 64-buffer rotating ring, 8 reps (512 copies per arm per variant), quiet machine.
host µsis wall time insidecudaMemcpyAsyncon the calling thread;end-to-end µsis stream time between events bracketing the copy, one synchronize per copy so nothing pipelines (supports the TL;DR and finding 2). Build and run:One-time allocation cost, 1.5 MiB buffers, same rig (supports finding 3):
new[]0.2 µs;cudaHostAlloc831 µs per buffer when accumulating as the pool does — ×257 buffers ≈ 0.2 s once at startup.Work with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.