Views
No views yet
pip install -r requirements.txt1git 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-14B-Math"
7download_from = "huggingface" # options: "wandb", "local", "huggingface"
8
9model = TrimKVQwen3ForCausalLM.from_pretrained(
10 model_path,
11 torch_dtype=torch.bfloat16,
12 load_trimkv_weights=True,
13 download_from=download_from,
14 use_cache=True,
15 device_map="cuda",
16)
17
18# Configure TRIM-KV settings
19model.config._attn_implementation = "flash_attention_2"
20model.config.compress_memory = True
21model.config.memory_size = 512
22model.config.buffer_size = 128
23
24tokenizer = AutoTokenizer.from_pretrained(
25 model.config.base_model,
26 use_fast=True,
27 padding_side="left",
28)
29
30# Use model.generate as normal.
31# Note: TRIM-KV uses TrimKVCache under the hood. So please pass TrimKVCache to model.generateexamples/test_qwen3.py.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}