mirror of
https://github.com/ollama/ollama.git
synced 2026-04-18 03:54:12 +02:00
mlxrunner: Simplify KV cache to single-entry prefix matching
The KV cache previously used a tree structure which could store multiple divergent sequences, which is good for cache reuse. However, this is typically used in conjunction with paged attention so each node in the tree can store just a chunk of the KV cache and they can be stitched together later. We don't currently do this, so the cache was storing copies of the full cache for each past sequence. This redundancy plus the lack of resource limits, caused significant memory use as a conversation grew. Instead, this changes to store a single entry for the cache, which can be prefix matched. Although it is less ideal for multiple users, it largely matches Ollama's current behavior. It can be improved as additional pieces are fleshed out.
This commit is contained in:
6
x/mlxrunner/cache/cache.go
vendored
6
x/mlxrunner/cache/cache.go
vendored
@@ -13,6 +13,7 @@ type Cache interface {
|
||||
State() (keys, values *mlx.Array)
|
||||
Trim(int) int
|
||||
Clone() Cache
|
||||
Free()
|
||||
Offset() int
|
||||
Len() int
|
||||
}
|
||||
@@ -84,6 +85,11 @@ func (c *KVCache) Clone() Cache {
|
||||
return clone
|
||||
}
|
||||
|
||||
func (c *KVCache) Free() {
|
||||
mlx.Unpin(c.keys, c.values)
|
||||
c.keys, c.values = nil, nil
|
||||
}
|
||||
|
||||
func (c *KVCache) Offset() int { return c.offset }
|
||||
func (c *KVCache) Len() int { return c.offset }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user