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 {