Repository navigation
[Engine] fix(strata): keep the prefill head at a fixed address - #346
Conversation
--prefill-head-alias unloads the output head while each fresh prompt is read (its VRAM is lent to the prompt path) and reloads it before the first decode window. The reload was a cudaMalloc, which does not promise the address the block had before: the verify windows' CUDA graphs are captured during the first decode and hold that address, so a reload that landed elsewhere ended the request with ERR --prefill-head-alias: the output head was reloaded at another address, so the captured verify graphs would read freed memory I saw it on a 524288-token engine, where an elastic K/V growth took the freed range between the unload and the reload. The head now lives in a VmmBuffer, a virtual-memory range reserved once for the process: unload unmaps the physical memory and keeps the addresses, load maps new physical memory at them. A captured graph or a stored pointer survives the pair, so the reload cannot move. Devices without virtual memory keep the cudaMalloc path, and the start line now says that a reload there may land elsewhere. The address check stays as a backstop. The restore line reports the address, so a log shows where the head went. Fixes Edge0-AI#345
linyubupa
left a comment
There was a problem hiding this comment.
Read through it — the approach is right (it is exactly what the "Open questions" section of docs/PREFILL_HEAD_ALIAS.md suggested), the docs and the address backstop are consistent, and the 303-reload log is the evidence this needed. Five things, roughly in priority order:
1. VmmBuffer::ensure() can silently invalidate a mapped buffer (the one I would fix before merging).
VmmRange::reserve() starts with release() (vmm.cpp:91), and ensure() calls it whenever bytes grows — so ensure() on an already mapped buffer frees the reserved range and moves base(). Today this is unreachable because NativeHead::load() refuses when the head is loaded, but VmmBuffer is now a general class (the K/V pools use VmmRange too) and that is a footgun with no diagnostic. Please add if (mapped()) return false; (or an assert) and say so in the comment.
2. map() maps every reserved chunk, not the bytes asked for.
range_.map_range(0, range_.chunks(), …) and unmap() reverse it, so a buffer reserved once at a larger size keeps paying for the whole range — the lend would return less VRAM than the head's bytes suggests, and bytes() reports the reserved size, which reads as the head's. Fine while one process holds one fixed-size head; I would still make it map(uint64_t bytes) and map only ceil(bytes / gran).
3. vmm_available() pins the device on first call.
api() resolves a.dev once through cudaGetDevice (vmm.cpp), so the first caller decides which card every later cuMemCreate/cuMemSetAccess targets. The new call site (generate.cpp:6509) is safe only because line 4240 already switched the thread to primary_device. Please either note "call only after the device is chosen" there, or resolve dev on the first map() instead. This one will bite a future call from argument parsing.
4. The test covers VmmBuffer, not NativeHead.
native_head_alias_test drives the class directly, so load → unload → load through the real head (and the cudaMalloc fallback branch) has no automated test — that path is only covered by your 4.7 h run. Not a blocker, but a case asserting "two loads land on the same weights()" (and, where VMM is absent, that they may not) would close it.
5. scratch_ (nit). It is cudaMalloc'd again on every reload, and nothing in the tree calls scratch() — the verify head kernel only takes weights() — so it is not in the captured graphs and does not need the fixed address. Worth a comment saying so, or dropping the allocation, so the next reader does not assume it needs the same treatment.
Unrelated to the fix itself: native_head_alias_test exits 0 rather than using SKIP_RETURN_CODE on a device without virtual memory, so CI cannot tell "skipped" from "passed" (vmm_test does the same, so this is only a note). HIP/Metal/Windows untested is acknowledged in the description; I do not see an interaction problem with --kv-grow, since both sides reserve their own virtual range.
VmmBuffer: - a mapped buffer refuses to change size: VmmRange::reserve gives the addresses back first, which would move base() under a captured graph, so ensure() now says so and keeps them; - map() maps the chunks the last ensure() asked for, so a buffer reserved at a larger size maps only what it uses, and bytes() reports that size instead of the reservation; - unmap() reports a chunk the driver leaves mapped: the lend then gave back less VRAM than it should. vmm: the device the ranges live on is picked by the first call that touches device memory, not by vmm_available(): a query from argument parsing used to pin the process to whichever card was current, and its granularity with it. A mapping call from another card is refused, with one line. NativeHead: fixed_backed is derived from weights_ == fixed_.base() rather than a second flag that unload, load and both error paths had to keep in step; unload() states that the caller drains the streams that read the head (cudaFree waits for the block's uses, unmapping and releasing do not); scratch_ is documented as run()'s buffer, outside the captured graphs, so it needs no fixed address. tests: native_head_reload_test drives load, unload, load through NativeHead on a synthetic GGUF (same weights(), the file's bytes again); native_head_alias_test covers the resize refusal and map-what-was-asked. vmm_test, native_head_alias_test, native_head_reload_test: ok on an RTX 4000 Ada.
|
Thanks for this — all five points are addressed in 29f1b01, each with a test or a stated reason. Full disclosure: implemented by my coding agent, maximum-effort pass, then built and run here (RTX 4000 Ada).
Three things the review did not cover, also in the patch:
|
|
All five points are addressed and verified in 29f1b01 — the |
What & why
Problem.
--prefill-head-aliasunloads the output head while each fresh prompt is read (its VRAM is lent to theprompt path), then reloads it before the first decode window. The reload was a
cudaMalloc, which does not promise theaddress the block had before. The verify windows' CUDA graphs are captured during the first decode and hold that
address, so a reload that landed elsewhere ended the request with
and the server restarted the engine. I saw it on a 524288-token engine: an elastic K/V growth (
--kv-grow) took thefreed range between the unload and the reload, so the head came back somewhere else.
Result. The head keeps its addresses across an unload/load pair, so a reload cannot move and the request
completes. The full report, with the log of the failure and the four-request run that followed, is in #345.
Approach.
edge0-strata/include/strata/core/vmm.hppgainsVmmBuffer, a virtual-memory range reserved once forthe process:
ensure(bytes)reserves whole chunks of addresses the first time, and only reserves again when a larger range isneeded;
map()takes new physical memory at those addresses;unmap()frees the physical memory and keeps the addresses;release()gives both back.NativeHeaduses one for its weights:loadmaps the head there,unloadunmaps it (that is the lend, and the VRAMreally returns to the pool),
loadmaps it again at the same address.VmmBufferis the existingVmmRangeAPI withthe unmap/map pair the alias path needs, so the CUDA path and the HIP path (ROCm 6, virtual memory management) share it.
Compatibility: a device without virtual memory keeps the
cudaMallocblock, and the--prefill-head-aliasstart linenow says so instead of failing silently later. The address check in
generate.cppstays as a backstop. The restoreline reports the address, so a log answers "did it move?" without a rebuild.
Limits. This removes the move, not the cost of the lend: reading a prompt still pays the reload (about 0.1 s here)
and the workspace rebuild (about 40 ms). A request that reads a few dozen fresh tokens keeps paying for a 2048-token
chunk; the "skip the swap for short reads" item in
docs/PREFILL_HEAD_ALIAS.mdis untouched. The range is reservedfor the life of the process, so its addresses are not available to another allocation: that is the point, and it costs
nothing when the engine runs with the flag.
Fixes #345
Area
edge0-strata/How was it tested?
Hardware and software: NVIDIA RTX 4000 Ada Generation Laptop GPU, 12282 MiB, driver 580.173.02 (CUDA 13.0), Linux,
strata0.1.55, commit 05119ae on top of 2e5d284.Commands and results:
tests/core/native_head_alias_test.cppis new and registered inCMakeLists.txtnext tovmm_test. It performs a reallend: it uploads a pattern, unmaps the head, has another allocation take VRAM from the freed range, maps the head again,
and checks the pointer, the read-back and a kernel write. It skips cleanly (exit 0) on a device without virtual memory.
End to end, with the bug's own workload: a
--max-context 524288engine (the recipe of #345:--kv int8 --kv-resident 20480 --prefill-head-alias --mtp, yarn x2) served the agent sessions on this machine for 4.7 hours.The log holds 303 consecutive restore lines, all at
0x302000000, and zeroERR --prefill-head-aliaslines:Passed: build of the three targets,
vmm_test,native_head_alias_test, 303 reloads under a live 524288 engine.Failed: none observed.
Not run: HIP (no AMD device here;
VmmBufferuses the sameVmmRangeAPI the HIP path already implements) and Metal(no virtual memory there; the code takes the
cudaMallocbranch and the start line says so). Windows was not tested.The memory-contract test covers the device calls, not a full engine run on those backends.
Checklist
Fixes #345, the report that reproduces the failurectest --preset cpu -L "L0|L1"passes locally inedge0-infer/(engine changes) — not affected: this PR touchesedge0-strata/only, and does not buildedge0-inferlocal-onlyanswer suites — not applicable (memory management only, nochange to the computation or to the numerics)
native_head_alias_testandvmm_testare device tests, plusthe 4.7 h engine run above
tests/core/native_head_alias_test.cppdocs/PREFILL_HEAD_ALIAS.md: the graph-safety section now describes the fixedaddress, the start line's warning where there is no virtual memory, and the open question about a fixed address is
closed