KV-Cache Policy
Definition
KV-Cache Policy — a set of rules and algorithms that determine how to manage memory allocated for key-value cache (KV-cache) during text generation. The policy includes the choice of memory allocation timing, eviction strategies when memory is scarce, and cache compression methods.
Key Decisions
| Decision | Options | Impact |
|---|---|---|
| Allocation Timing | Static at start, dynamic as it grows | Memory usage, latency |
| Eviction | LRU, by position, by token importance | Generation quality |
| Compression | Quantization, removal of unimportant tokens | Accuracy, speed |
| Sharing | Between requests, between users | Efficiency |
Example Eviction Policy
class LRUPolicy:
def evict(self, cache, new_tokens):
# Evict least recently used tokens
while cache.size + len(new_tokens) > cache.limit:
cache.remove_least_recent()