From b7ebee021dbd730c584252b8c896a20ec3e912ae Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 29 Oct 2026 12:00:24 +0000 Subject: [PATCH] Local GGUF: realistic KV-cache memory estimate and a bounded default 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 Claude-Session: https://claude.ai/code/session_01RXDZoNoMWjiSYJ4aoUuhym --- core/backendcap/host_model.go | 32 +++++++++++++++++++++++++++++--- core/llm/local_gguf_provider.go | 8 +++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/core/backendcap/host_model.go b/core/backendcap/host_model.go index f8ccc9a6ef9..bfaed3eaf7b 100644 --- a/core/backendcap/host_model.go +++ b/core/backendcap/host_model.go @@ -285,7 +285,7 @@ func ReadGGUFModelMetadata(path string) (ModelFileMetadata, error) { context := intFromMetadata(metadata, arch+".context_length", "llama.context_length", "context_length") quantization := quantizationFromMetadata(metadata) fileSize := uint64(stat.Size()) - estimate := estimateModelMemory(fileSize, context) + estimate := estimateModelMemory(fileSize, context, kvBytesPerToken(metadata, arch)) metadata["gguf.version"] = uint64(version) metadata["gguf.tensor_count"] = tensorCount @@ -439,10 +439,36 @@ func quantizationFromMetadata(metadata map[string]interface{}) string { return "unknown" } -func estimateModelMemory(fileSize uint64, contextLength int) uint64 { +// fallbackKVBytesPerToken is used when the GGUF metadata does not describe the +// attention geometry; it corresponds to a large (~70B class) model so that an +// unknown model is treated conservatively. +const fallbackKVBytesPerToken = 512 * 1024 + +// kvBytesPerToken derives the KV-cache cost of one context token from the +// model's attention geometry: K and V per layer, each head_count_kv × head_dim +// values stored as 16-bit floats. +func kvBytesPerToken(metadata map[string]interface{}, arch string) uint64 { + layers := intFromMetadata(metadata, arch+".block_count", "llama.block_count") + embedding := intFromMetadata(metadata, arch+".embedding_length", "llama.embedding_length") + heads := intFromMetadata(metadata, arch+".attention.head_count", "llama.attention.head_count") + kvHeads := intFromMetadata(metadata, arch+".attention.head_count_kv", "llama.attention.head_count_kv") + if layers <= 0 || embedding <= 0 || heads <= 0 { + return fallbackKVBytesPerToken + } + if kvHeads <= 0 { + kvHeads = heads + } + headDim := embedding / heads + if headDim <= 0 { + return fallbackKVBytesPerToken + } + return uint64(2) * uint64(layers) * uint64(kvHeads) * uint64(headDim) * 2 +} + +func estimateModelMemory(fileSize uint64, contextLength int, kvPerToken uint64) uint64 { estimate := uint64(float64(fileSize) * 1.20) if contextLength > 0 { - estimate += uint64(contextLength) * 512 * 1024 + estimate += uint64(contextLength) * kvPerToken } if estimate < fileSize { return fileSize diff --git a/core/llm/local_gguf_provider.go b/core/llm/local_gguf_provider.go index 0d324584318..8e16ad20507 100644 --- a/core/llm/local_gguf_provider.go +++ b/core/llm/local_gguf_provider.go @@ -314,7 +314,13 @@ func (lgp *LocalGGUFProvider) safeToLoadLocked() error { capability = cap lgp.config.Capability = cap } - if capability.ContextLength > 0 && (lgp.config.ContextSize <= 0 || lgp.config.ContextSize > capability.ContextLength) { + 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 + } + if capability.ContextLength > 0 && lgp.config.ContextSize > capability.ContextLength { lgp.config.ContextSize = capability.ContextLength } if lgp.config.BatchSize <= 0 || lgp.config.BatchSize > lgp.config.ContextSize {