fix(server): reserve per-device VRAM for LOADING models (#66)#148
Draft
marksverdhei wants to merge 1 commit into
Draft
fix(server): reserve per-device VRAM for LOADING models (#66)#148marksverdhei wants to merge 1 commit into
marksverdhei wants to merge 1 commit into
Conversation
The in-router admit check (#136) reasons about live cudaMemGetInfo only, so two same-device admits fired close together both read stale-high free memory before either child's cudaMalloc is visible and co-load onto the same GPU -> OOM. This is the exact titan --models-max 2 scenario in #66 that #136's steady-state fix did not close (the under-lock re-check in load() guards the model count, not per-device memory). Track an in-flight reserved_per_device[] (guarded by mutex, indexed in system-GPU order like per_device_bytes): reserve a model's footprint when it enters LOADING (applied past all throw points in load(), so it cannot leak on a failed spawn, and while the load still holds the lock so update_status can't release it early) and release it when it leaves LOADING to LOADED or back to UNLOADED (in update_status, idempotent against the double-UNLOADED crash path). unload_lru subtracts the reserve from live free memory, and LOADING-state models are excluded from eviction so an in-flight load is never thrashed to make room for another.
Author
Reviewer note: reserve-leak audit (no leak found)Verified that the
Net: every removal of a LOADING model is preceded by a LOADING→UNLOADED (or →LOADED) transition through |
This was referenced Jul 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Closes the remaining exposure of #66. #136 landed the in-router per-device admit + targeted LRU eviction (steady state), but the admit reasons about live
cudaMemGetInfoonly. Two same-device admits fired close together both read stale-high free memory before either child'scudaMallocis visible, and co-load onto the same GPU → OOM. This is the exact titan--models-max 2scenario in #66 (the under-lock re-check inload()guards the model count, not per-device memory).The fix is the
reserved[]LOADING-state accounting sketched in @marksverdhei's own #66 comment, and the residual I flagged in #146.How
reserved_per_device[]onserver_models(guarded bymutex, indexed in system-GPU order — identical toper_device_bytesandunload_lru'sprojected_free).load()): applied past all throw points, just before themapping[]commit, so it can't leak on a failed spawn — and whileload()still holds the lock, so the monitoring thread can'tupdate_status()-release it early.update_status()): toLOADEDon success or back toUNLOADEDon a failed load. Guarded onold_status == LOADING, which makes it idempotent against the double-UNLOADEDpath (crash handler + monitoring thread both fire) —update_status()is never called withstatus == LOADING(the child's "loading" state is a no-op inhandle_child_state).unload_lrusubtracts the reserve from live free memory before the per-device fit check, so a later same-device admit sees the in-flight demand.Verification
cmake --build --target llama-server(GGML_CUDA=OFF, server on) —server-models.cpp.obuilds with no errors/warnings,llama-serverlinks. CI will exercise the full matrix.reserved[]is zero, soprojected_freeis unchanged.Open question for @marksverdhei (why this is a draft)
This makes
unload_lruaware of in-flight loads, but it does not add a hard gate: if a device is saturated by in-flight LOADING models (which are now un-evictable),unload_lrustill falls through andload()proceeds to spawn — same as today (could still OOM in that corner). The clean closure is an under-lock per-device re-check inload()(mirroring the existing count re-check at the// Re-check capacity under the lockblock) that throwsmodel_unavailable_error→ 503, client retries. I left that out because it's a policy call — proceed-and-maybe-OOM vs. 503-retry — andmodel_unavailable_error/503 already exists (#41/#42). Say the word and I'll add the gate.Also a narrow residual: two
unload_lrucalls that perfectly interleave before either sets LOADING won't see each other's reserve. Fully closing it means making the check-and-reserve atomic under one lock hold (folding the admit intoload()'s locked region). Out of scope for this draft; happy to follow up.Refs