KV-Cache Eviction
Definition
KV-Cache Eviction is a set of methods that reduce the size of the KV-cache by removing the least important keys and values. This is necessary for handling contexts that exceed available memory and for reducing inference latency.
Main Strategies
| Strategy | Principle |
|---|---|
| Sliding Window | Only the last N tokens are stored |
| Token Dropping | Low-importance tokens are dropped |
| H2O | High-attention tokens are retained |
| StreamingLLM | The first tokens and a sliding window are fixed |
Implementation
# Pseudocode for the H2O strategy
def evict(kv_cache, scores, budget):
# scores — importance of each token
important = top_k(scores, budget)
return kv_cache[important]
Trade-offs
- Reduced accuracy with aggressive eviction.
- Increased speed and reduced memory consumption.
- Need to balance between quality and performance.