Repository navigation
Local GGUF: realistic KV-cache memory estimate and a bounded default context - #4
Merged
Merged
Conversation
…context ProbeModelFile charged every model 512 KiB per context token, so a 0.5B Q4 model with a 32k declared context was estimated at 16.5 GiB and refused on a 16 GiB host, leaving the character with no local model. The estimate now derives the per-token KV cost from the model's attention geometry (block_count, head_count_kv, head_dim, 16-bit K and V), falling back to the old conservative constant only when the metadata lacks it. The provider's context window defaults to 2048 (clamped to the model's maximum) instead of the full declared context. With this, TestLocalGGUFProviderGatedRealModelSmoke passes against a real Qwen2.5-0.5B GGUF: the provider loads the model in-process and generates. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RXDZoNoMWjiSYJ4aoUuhym
There was a problem hiding this comment.
🟡 Changes recommended
The registry constructor still bypasses the 2048-token context default, and the KV-cache estimate ignores explicit key/value widths, potentially admitting models that exceed the memory safety limit.
2 open issues
What changed in this PR
This follow-up to #3 improves memory handling for the in-process GGUF provider, now the character’s only offline generation path after removal of the fallback provider.
Changes:
- Derives KV-cache memory estimates from model attention geometry, retaining a conservative fallback when metadata is missing.
- Adds a 2048-token context fallback during load validation, clamped to the model’s declared maximum.
| File | Description |
|---|---|
core/llm/local_gguf_provider.go |
Adds bounded context defaulting during load validation. |
core/backendcap/host_model.go |
Replaces the fixed per-token KV estimate with an attention-geometry calculation. |
Review effort: Balanced
Comment on lines
+461
to
+465
| headDim := embedding / heads | ||
| if headDim <= 0 { | ||
| return fallbackKVBytesPerToken | ||
| } | ||
| return uint64(2) * uint64(layers) * uint64(kvHeads) * uint64(headDim) * 2 |
Comment on lines
+317
to
+321
| if lgp.config.ContextSize <= 0 { | ||
| // Default to a modest window rather than the model's full declared | ||
| // context, which for long-context models would allocate a KV cache | ||
| // far larger than any single thought needs. | ||
| lgp.config.ContextSize = defaultLocalGGUFContext |
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.

Summary
Follow-up to #3. With the fallback provider gone, the character's only offline voice is the in-process GGUF provider, and its memory gate refused every realistic model:
ProbeModelFilecharged 512 KiB per context token regardless of the model, so a 0.5B Q4 model with a 32k declared context was estimated at 16.5 GiB and marked unavailable on a 16 GiB host.Changes
core/backendcap/host_model.go: the per-token KV-cache cost is derived from the model's attention geometry (block_count,head_count_kv,head_dim, 16-bit K and V); the old constant remains only as a conservative fallback when the metadata lacks those keys.core/llm/local_gguf_provider.go: the context window defaults to 2048 tokens (clamped to the model's maximum) instead of the full declared context, so loading does not allocate a KV cache far larger than any single thought needs.Verification
go test ./core/backendcap/ ./core/llm/pass.ECHO_TEST_GGUF_MODEL=<qwen2.5-0.5b-instruct-q4_k_m.gguf> go test ./core/llm/ -run TestLocalGGUFProviderGatedRealModelSmokenow passes: the provider loads the real model in-process and generates. Before this change the same test skipped with "unavailable in this build or host memory envelope".Generated by Claude Code