Views
No views yet
TRIM-KV is an efficient and learnable key–value eviction strategy designed to improve the efficiency of large language models (LLMs) in long-horizon inference.
Phi-3-mini-128k-instruct enhanced with TRIM-KV, as presented in the paper Cache What Lasts: Token Retention for Memory-Bounded KV Cache in LLMs.pip install trimkv1import torch
2from trimkv.models.phi3 import TrimKVPhi3ForCausalLM
3from trimkv.cache_utils import TrimKVCache
4from transformers import AutoTokenizer
5
6model_path = "ngocbh/TrimKV-Phi-3-mini-128k-instruct"
7
8model = TrimKVPhi3ForCausalLM.from_pretrained(
9 model_path,
10 torch_dtype=torch.bfloat16,
11 load_trimkv_weights=True,
12 use_cache=True,
13 device_map="cuda",
14 trust_remote_code=True,
15)
16
17# Configure TRIM-KV settings
18model.config._attn_implementation = "flash_attention_2"
19model.config.compress_memory = True
20model.config.memory_size = 512
21model.config.buffer_size = 128
22
23tokenizer = AutoTokenizer.from_pretrained(
24 "microsoft/Phi-3-mini-128k-instruct",
25 use_fast=True,
26 padding_side="left",
27)
28
29# Use model.generate as normal.
30# Note: TRIM-KV uses TrimKVCache under the hood.
31# Please pass a TrimKVCache instance to past_key_values in model.generate.1@article{bui2025cache,
2 title={Cache what lasts: Token retention for memory-bounded kv cache in llms},
3 author={Bui, Ngoc and Sharma, Shubham and Lamba, Simran and Mishra, Saumitra and Ying, Rex},
4 journal={arXiv preprint arXiv:2512.03324},
5 year={2025}
6}
7@article{bui2025make,
8 title={Make Each Token Count: Towards Improving Long-Context Performance with KV Cache Eviction},
9 author={Bui, Ngoc and Nguyen, Hieu Trung and Cohan, Arman and Ying, Rex},
10 journal={arXiv preprint arXiv:2512.03324},
11 year={2025}
12}