Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions core/backendcap/host_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Comment on lines +461 to +465
}

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
Expand Down
8 changes: 7 additions & 1 deletion core/llm/local_gguf_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +317 to +321
}
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 {
Expand Down
Loading