Skip to content

Size a GPU model's context from free VRAM - #280

Merged
mehfuzh merged 3 commits into
mainfrom
feat/size-gpu-context-from-vram
Nov 30, 2026
Merged

mehfuzh merged 3 commits into
mainfrom
feat/size-gpu-context-from-vram

Conversation

@mehfuzh

@mehfuzh mehfuzh commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Problem

On CUDA and Vulkan, a local model's context was a fixed 8192:

  • LlmFactory started at n_ctx = 8192 and only ever lowered it (the large-model cap is applied as if n_ctx > cap).
  • ModelService::device_free_total_gb() returned None off Apple Silicon, so widened_ctx_cap never ran and the large-model cap stayed at its 16384 floor.

Seen on an A100 40GB: qwen3.8-27b ran with 8192, and every docent request that read a file failed with Requested tokens (12337) exceed context window of 8192.

Change

  • gpu_memory_gb(): free/total VRAM summed over the discrete GPUs llama.cpp reports (list_llama_ggml_backend_devices; CUDA and Vulkan both fill it in).
  • LlmFactory::ctx_for_memory(): the Apple Silicon sizing (free memory − weights − 2GB, at 0.75–2GB of KV per 8k, up to the device ceiling), now shared. The GPU branch uses it with the default 8192 as its floor; VRAM not reported, 8192 stays.
  • device_free_total_gb() reports GPU VRAM, so the large-model cap widens with it.

For the A100 case: ~16GB weights, ~38GB free → n_ctx = 81920.

Behavior to watch

  • The large-model fit guard now runs on CUDA/Vulkan (it always passed before), so a model that barely fit may now be refused with the existing message.
  • The KV cost is the existing estimate. If a load over-allocates, the existing in-process fallback halves the context, then falls back to CPU.
  • Not run on real GPU hardware here; on the box, the log should show GPU: …GB total, …GB free … — using n_ctx=….

Also: run-debug takes slp-speech from a release

make run-debug no longer builds slp-speech (whisper.cpp). A new fetch-speech target downloads <os>-<arch>-slp.tar.gz for the release install.sh installs (or SPEECH_RELEASE=x.y.z) from dl.smartloop.ai, checks it against checksums-sha256.txt, and keeps only slp/slp-speech and slp/lib/ under ~/.smartloop/cache/slp-speech/<version>/. Windows (zip archive) still builds it. Tried on Linux: 1.2.42 fetched, checksum matched, slp-speech resolves its bundled libvulkan.so.1, a second run is a no-op.

Also: fontconfig in the build requirements

The README's Linux build requirements now list pkg-config libfontconfig1-dev (fontconfig-devel on Fedora/RHEL), as CI installs: mupdf's system fonts (font-kit → yeslogic-fontconfig-sys) need it, and a fresh box's make run-debug failed in that crate's build script.

Tests

New tests/unit/test_gpu_context_from_vram.rs: the A100 case, the ceilings, a tight GPU keeping the floor, the large-model cap widening. cargo test: all pass.

Related: #279 (stream the generation error instead of nothing), smartloop-labs/docent#106.

🤖 Generated with Claude Code

On CUDA and Vulkan the context was a fixed 8192: VRAM was never read, the
factory only lowered its default, and device_free_total_gb() returned
None, so the large-model cap stayed at its 16384 floor too. A 27B model
on a 40GB A100 ran with 8192 and failed every request that read a file
("Requested tokens (12337) exceed context window of 8192").

llama.cpp reports each GPU's free and total memory; the context is now
sized from it as Apple Silicon's is from unified memory (ctx_for_memory,
shared), never under the default, and the fit guard and large-model cap
read it too.
make run-debug built slp-speech (whisper.cpp) every time. A new
fetch-speech target downloads the release install.sh installs (or
SPEECH_RELEASE=x.y.z), checks it against the release's checksums, and
keeps only slp-speech and its lib/ under ~/.smartloop/cache/slp-speech/.
Windows, whose archive is a zip, still builds it.
mupdf's system-fonts feature (font-kit) links fontconfig; CI installs
libfontconfig1-dev, the README didn't say so, and a fresh box failed in
yeslogic-fontconfig-sys's build script.
@mehfuzh
mehfuzh merged commit b2b32ea into main Nov 30, 2026
5 checks passed
@mehfuzh
mehfuzh deleted the feat/size-gpu-context-from-vram branch November 30, 2026 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant