Skip to content

Local GGUF: realistic KV-cache memory estimate and a bounded default context - #4

Merged
drzo merged 1 commit into
mainfrom
claude/dte-no-canned-output
Oct 29, 2026
Merged

drzo merged 1 commit into
mainfrom
claude/dte-no-canned-output

Conversation

@drzo

@drzo drzo commented Oct 29, 2026

Copy link
Copy Markdown

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: ProbeModelFile charged 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 TestLocalGGUFProviderGatedRealModelSmoke now 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

…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
@drzo
drzo marked this pull request as ready for review October 29, 2026 12:19
Copilot AI balanced review requested due to automatic review settings October 29, 2026 12:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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
@drzo
drzo merged commit e934c60 into main Oct 29, 2026
22 of 29 checks passed
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.

3 participants