Repository navigation
Native llama.cpp path allocates a fixed 32K context at load time; Ollama's per-request negotiation avoids the same OOM - #1643
Conversation
…2K (closes #1638) The native llama.cpp path allocated a fixed 32K-token KV cache at model load regardless of free memory, so any model whose weights + full KV cache didn't comfortably fit hard-OOM'd on the second conversational turn (kIOGPUCommandBufferCallbackErrorOutOfMemory). Gemma 4 12B reproduced this on a 16GB machine; the same model over Ollama never OOM'd because Ollama negotiates context per request rather than reserving the full window up front. The catalog context_window is now a ceiling, not a fixed allocation. When the chat context is created we compute the GQA-aware KV-cache cost per token from model geometry, probe memory free after weights are resident (sysinfo), and cap the KV cache at the smaller of (available − 2GiB headroom) and 40% of available memory — the fraction reserve leaves proportional room for the batch compute buffer, which scales with the window and is what tipped the fixed-headroom-only budget back into OOM at a 27K window during testing. The result is clamped to [4096, configured] and the effective window feeds the overflow/stop guards so they measure the real allocation. Verified end-to-end on a 16GB machine: gemma-4-12b-unsloth-q4km now survives multiple turns without OOM (window reduced to fit; warns on reduction), while Gemma 4 E4B keeps the full 32K ("context fits configured window"). Unit tests cover the sizing arithmetic (tight-memory reduction, roomy no-op, floor clamp, degenerate-geometry fallback). Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01DMPgbnyNk6bDFnpqtTPbKr
Pragmatic Code Review — PR #1643Review Type: Initial review (no prior reviews on the PR) SummaryThis is a well-executed, tightly-scoped fix. It converts the hardcoded 32K KV-cache allocation into a memory-fitted It is a clear net improvement and directly resolves the OOM. My one substantive finding is a cross-module consistency gap (the agent's history budget doesn't learn about the reduced window), which is arguably out of this PR's declared scope but materially affects acceptance criterion 4 for large models in long conversations. Requirements Check (issue #1638)
Findings🟡 1. Effective window is invisible to the agent's history-budgeting layer — long conversations on a shrunk window fail hard instead of summarizing
Consequence: when the 12B model is capped to ~8960 tokens, the agent will still assemble up to ~32K tokens of history before summarizing, then This is the flip side of the PR's own note that
I would not block merge on this if it's captured as a follow-up, but it should not be left implicit. 🟢 2.
|
malibio
left a comment
There was a problem hiding this comment.
Pragmatic code review posted as a PR comment. Verdict: APPROVE with one recommended follow-up (agent history budget doesn't learn the reduced effective window — long conversations on a shrunk 12B window fail hard with ContextOverflow instead of summarizing). Allocation fix itself is correct, well-tested, and standards-compliant. See the review comment for full detail.
…d 32K Addresses the code-review finding on the adaptive-n_ctx change: sizing the KV cache down to fit memory only prevents the OOM if the agent's history-budgeting layer also respects the reduced window. It didn't — model_info() reported the configured ceiling and the summarization gate used a hardcoded 32K budget, so a 12B model capped at ~7K tokens would pack up to 32K of history and then be rejected with ContextOverflow. That trades an OOM crash for a different crash on any moderately long conversation with the very model this unblocks. - Decide the effective n_ctx eagerly at model load (geometry + free memory are both known then) instead of lazily at first generation, so model_info() reports the real window immediately. Context creation stays lazy but reuses the stored value. - LlamaChatInferenceEngine::model_info() now reports the engine's effective context_size rather than the cached configured value. - The agent loop derives its total/history token budgets from model_info().context_window (reserving MAX_RESPONSE_TOKENS so summarization fires before the prompt fills the window), falling back to the static 32K budget only when the window is unknown (model not loaded / remote backend). - MockEngine gains a configurable window; new test summarization_triggers_on_reduced_effective_window pins the closed gap (10K-token history under the old 32K constant but over a 4096 window now summarizes instead of erroring). Re-verified E2E: gemma-4-12b-unsloth-q4km reduces at load (effective=6912, logged) and runs multiple turns with no OOM; the reduced window is now the budget the agent plans against. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01DMPgbnyNk6bDFnpqtTPbKr
Pragmatic Code Review — PR #1643 (Re-Review)Review Type: Re-Review — of the new commit Previous Review Summary
Finding 1 was: the effective (memory-reduced) window was invisible to the agent's history-budgeting layer — Verification of the fix1. Finding 1 fully resolved — yes. The effective window now flows end-to-end on the native path:
2. Eager-at-load sizing move — no regression found.
3. Budget arithmetic at very small windows — degenerate-but-safe, no thrash. At the 4. Blast radius correctly contained. The Findings (this pass)No new findings. No standards violations (no lint suppression, no 🟢 (Informational, optional — carried context, not a blocker): my prior 🟢 Findings 2 ( RecommendationAPPROVE. The one recommended finding from the initial review is fully and correctly addressed, with a targeted regression test pinning the closed gap and E2E confirmation ( |
malibio
left a comment
There was a problem hiding this comment.
Re-review of commit 09cd301 (addresses the one 🟡 finding from the initial review). Verdict: APPROVE (recorded as a comment — GitHub blocks self-approval on an own PR). The effective context window now flows end-to-end into the agent's history budget; sizing is moved to load time and is sound across lazy-context / unload-reload / pre-first-generation paths; the new regression test pins the closed gap; no new issues. Full re-review posted as a PR comment.
Closes #1638