mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #1690] [CLOSED] kv-grow: kvg_trim uploads the residency table with res_put, which waits for the copy #3612
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#3612
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/1690
Author: @sergqwer
Created: 10/9/2026
Status: ❌ Closed
Base:
main← Head:pr/kvg-trim-sync📝 Commits (1)
16c6cbfkv-grow: kvg_trim uploads the residency table with res_put, which waits for the copy📊 Changes
1 file changed (+2 additions, -2 deletions)
View changed files
📝
src/program/generate.cpp(+2 -2)📄 Description
With
--kv-grow(opt-in),kvg_trimgives the K/V's surplus back to the expert cache. It refills those slots and then uploads the residency table with a plaincudaMemcpyfrom pageable memory, with no sync after it (generate.cpp:6049 onfb58e0db):CUDA's API synchronization notes say that for a pageable host-to-device
cudaMemcpy, "The function will return once the pageable buffer has been copied to the staging memory for DMA transfer to device memory, but the DMA to final destination may not have completed."res_put's comment says the same (generate.cpp:5558, from #550), and every other residency upload goes throughres_put. These twokvg_*uploads are the exceptions.kvg_ensure's upload (5963) is covered:qsa_kv_elastic_growends incudaDeviceSynchronize(layer.cpp:601) before anything reads the table.kvg_trim's upload is not covered.What reads
d_resafter a trim, in serve onfb58e0db:kv_quiesce()(cudaDeviceSynchronize+apply_pending), thenkvg_trim. The sync comes before the upload, not after it.resume == 0, 9325):session_zeroonmain_cs, thencudaStreamSynchronize(main_stream).main_streamiscudaStreamNonBlocking(2796), so this waits for nothing on stream 0.apply_pending(true)returns at once, becausekv_quiesceemptiedpending(7415).adapt_tick(true)also returns at once:--adapt-asyncneeds the resident mode, and that turns--kv-growoff.--short-read(64) tokens or fewer goes through the verify windows (9396).refill()(9883) has nothing to give back, because the last request refilled its loan at its end (9927). So it makes nores_upload.ver.run(9539) launches the window graph on the verifier'scs_, which iscudaStreamNonBlocking(verify.cpp:605).hits_.d_resinresident_plananddoorbell_publish_res(verify.cpp:1320-1352).lend()instead, whoseres_uploadsyncs stream 0 first. That path is ordered.So nothing orders that first window after the copy. If the window reads the old table, the refilled experts look not resident to the GPU's plan, while the CPU pool, which plans from
host_res, already counts them as resident.The pattern alone (a scratch program, not part of the PR):
cudaMemcpyfrom a pageable buffer;cudaMemcpy, reader on acudaStreamNonBlockingstream (kvg_trimnow)cudaMemcpy+cudaStreamSynchronize(nullptr)(res_put, this PR)cudaMemcpy, reader on a blocking streamWith the host waiting before the launch (1,000 launches each, two runs):
In the engine, more host work than that sits between the two:
session_zero's memsets, which the host waits for, and theRESUMEline. So the window is tens of µs wide here. This is a latent ordering bug: I have not seen it change tokens. A copy queued behind other DMA, or a faster path to the first window, would widen or reach it.Change:
kvg_trimuploads withres_put: the same copy, thencudaStreamSynchronize(nullptr). One line.Default identity:
kvg_trimreturns at its first line unless--kv-growis on (kvg.on), so without the flag the build runs fb58e0db's code. The extra sync costs one wait for a 96 KiB copy per trim, and a trim happens at most once per request.Run with the change. Settings:
--kv-grow --expert-cache 12000 --spec 4 --kv int8 --max-context 262144 --vram-reserve-mib 1500;STRATA_KV_GROW_INIT=4096 STRATA_KV_GROW_STEP=2048, so a short session grows and trims.Requests, all greedy:
The same four requests on a
fb58e0dbbuild with the same settings gave the same replies (content and token counts), and the same grow, trim and per-requestdecode expert cache hit ratelines, to the hit. No errors.Not measured: the engine's actual gap between the trim and the first window; HIP (
res_putis already used there).🤖 Generated with Claude Code
https://claude.ai/code/session_01VZy1yKaDDiA8a7svdwaHio
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.