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.
TrimKV-Qwen3-8B-Math enhanced with TRIM-KV, as presented in the paper Cache What Lasts: Token Retention for Memory-Bounded KV Cache in LLMs.trimkv library:1git clone https://github.com/ngocbh/trimkv.git
2cd trimkv
3pip install -e .1import torch
2from trimkv.models.qwen3 import TrimKVQwen3ForCausalLM
3from trimkv.cache_utils import TrimKVCache
4from transformers import AutoTokenizer
5
6model_path = "ngocbh/TrimKV-Qwen3-8B-Math"
7
8model = TrimKVQwen3ForCausalLM.from_pretrained(
9 model_path,
10 torch_dtype=torch.bfloat16,
11 load_trimkv_weights=True,
12 download_from="huggingface",
13 use_cache=True,
14 device_map="cuda",
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 model.config.base_model,
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. So please pass TrimKVCache to model.generate1@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}