Text Generation
qwen3
math
trimkv
KV
Cache
Compression

Add pipeline tag and link to paper

#1
by nielsr HF Staff - opened
Files changed (1) hide show
  1. README.md +40 -64
README.md CHANGED
@@ -1,9 +1,10 @@
1
  ---
2
- license: apache-2.0
3
- datasets:
4
- - open-r1/OpenR1-Math-220k
5
  base_model:
6
  - Qwen/Qwen3-1.7B
 
 
 
 
7
  tags:
8
  - math
9
  - trimkv
@@ -12,116 +13,91 @@ tags:
12
  - Compression
13
  ---
14
 
 
 
 
 
 
 
15
  > 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.
16
 
17
  The core idea behind TRIM-KV is to learn the intrinsic importance of each key–value pair at creation time, which we call *token retention*, and then decay this importance exponentially over time to mimic the standard inference running with eviction.
18
 
19
  The retention score is query-agnostic and captures the long-term utility of tokens. This is different from attention scores, which are query-dependent: they capture the short-term utility for predicting the next token and are recomputed at every step, making them local, myopic, and highly dependent on the transient decoding state.
20
 
21
- <a href="https://arxiv.org/pdf/2512.03324"><img src="https://img.shields.io/badge/arxiv-2512.03324-red?style=for-the-badge"></a>
22
-
23
  ### Why TRIM-KV?
24
 
25
  It's fast
26
-
27
  <div align="center">
28
  <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/speed.png?raw=true"/>
29
  </div>
30
 
31
  It's smart
32
-
33
  <div align="center">
34
  <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/performance.png?raw=true"/>
35
  </div>
36
 
37
-
38
  And it's interpretable
39
-
40
  <div align="center">
41
  <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/eviction.png?raw=true"/>
42
  </div>
43
 
44
- <div align="center">
45
- <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/vis.png?raw=true"/>
46
- </div>
47
-
48
  ---
49
 
50
- ## Getting Started
51
-
52
- ### Requirements
53
-
54
- - Python 3.11 or higher (tested with 3.12)
55
- - PyTorch 2.7.0 or higher (tested with 2.8.0)
56
- - FlashAttention 2.7.2.post1 or higher (tested with 2.8.0)
57
- - Transformers 4.57.1
58
-
59
- ```sh
60
- pip install -r requirements.txt
61
- ```
62
-
63
- This is a minimal set of requirements for training purposes. Additional dependencies may be needed for running specific experiments. We provided a full example of the environment used in our experiments in [`examples/env.yaml`](examples/env.yaml).
64
 
65
  ### Installation
66
 
67
- From the root of the repo:
68
-
69
  ```sh
70
- git clone https://github.com/ngocbh/trimkv.git
71
- cd trimkv
72
- pip install -e .
73
- ````
74
-
75
- ---
76
 
77
- ## Quick Start
78
 
79
  ```python
80
  import torch
81
  from trimkv.models.qwen3 import TrimKVQwen3ForCausalLM
82
- from trimkv.cache_utils import TrimKVCache
83
  from transformers import AutoTokenizer
84
 
85
- model_path = "<TrimKV model_path here>"
86
- download_from = "huggingface" # options: "wandb", "local", "huggingface"
87
 
88
  model = TrimKVQwen3ForCausalLM.from_pretrained(
89
  model_path,
90
  torch_dtype=torch.bfloat16,
91
  load_trimkv_weights=True,
92
- download_from=download_from,
93
  use_cache=True,
94
  device_map="cuda",
95
  )
96
-
97
- # Configure TRIM-KV settings
98
  model.config._attn_implementation = "flash_attention_2"
99
- model.config.compress_memory = True
100
- model.config.memory_size = 512
101
- model.config.buffer_size = 128
102
 
103
  tokenizer = AutoTokenizer.from_pretrained(
104
- model.config.base_model,
105
- use_fast=True,
106
- padding_side="left",
107
  )
108
 
109
- # Use model.generate as normal.
110
- # Note: TRIM-KV uses TrimKVCache under the hood. So please pass TrimKVCache to model.generate
111
- ```
112
-
113
- For a runnable end-to-end example, see [`examples/test_qwen3.py`](examples/test_qwen3.py).
 
 
 
 
 
 
 
114
 
115
- ## Released Models
 
116
 
117
- | Base Model | TRIM-KV Checkpoints | Training Datasets | Training Context Len | Training $M$ |
118
- |------------------------------|-----------------------------------------------|--------------------------|-------------------------|--------------|
119
- | Qwen3-1.7B | [TRIM-KV-Qwen3-1.7B-Math](https://huggingface.co/ngocbh/TrimKV-Qwen3-1.7B-Math) | OpenR1-Math-220k | 16K | 256 |
120
- | Qwen3-4B | [TRIM-KV-Qwen3-4B-Math](https://huggingface.co/ngocbh/TrimKV-Qwen3-4B-Math) | OpenR1-Math-220k | 16K | 256 |
121
- | Qwen3-8B | [TRIM-KV-Qwen3-8B-Math](https://huggingface.co/ngocbh/TrimKV-Qwen3-8B-Math) | OpenR1-Math-220k | 16K | 256 |
122
- | Qwen3-14B | [TRIM-KV-Qwen3-14B-Math](https://huggingface.co/ngocbh/TrimKV-Qwen3-14B-Math) | OpenR1-Math-220k | 16K | 256 |
123
- | Qwen3-4B-Instruct-2507 | [TrimKV-Qwen3-4B-Instruct-2507](https://huggingface.co/ngocbh/TrimKV-Qwen3-4B-Instruct-2507) | Synth-Long, BookSum, Buddhi | 128K | 1024 |
124
- | Phi-3-mini-128k-instruct | [TrimKV-Phi-3-mini-128k-instruct](https://huggingface.co/ngocbh/TrimKV-Phi-3-mini-128k-instruct) | LongAlpaca | 128K | 512 |
125
- | DeepSeek-R1-Distill-Llama-8B | [TrimKV-DeepSeek-R1-Distill-Llama-8B](https://huggingface.co/ngocbh/TrimKV-DeepSeek-R1-Distill-Llama-8B) | OpenR1-Math-220k | 32K | 256 |
126
 
127
- ---
 
 
 
 
 
 
 
 
1
  ---
 
 
 
2
  base_model:
3
  - Qwen/Qwen3-1.7B
4
+ datasets:
5
+ - open-r1/OpenR1-Math-220k
6
+ license: apache-2.0
7
+ pipeline_tag: text-generation
8
  tags:
9
  - math
10
  - trimkv
 
13
  - Compression
14
  ---
15
 
16
+ # TrimKV: Token Retention for Memory-Bounded Key-Value Eviction
17
+
18
+ This repository contains the weights for **TRIM-KV-Qwen3-1.7B-Math**, presented in the paper [Make Each Token Count: Towards Improving Long-Context Performance with KV Cache Eviction](https://huggingface.co/papers/2605.09649).
19
+
20
+ The official implementation and training code can be found at [https://github.com/ngocbh/trimkv](https://github.com/ngocbh/trimkv).
21
+
22
  > 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.
23
 
24
  The core idea behind TRIM-KV is to learn the intrinsic importance of each key–value pair at creation time, which we call *token retention*, and then decay this importance exponentially over time to mimic the standard inference running with eviction.
25
 
26
  The retention score is query-agnostic and captures the long-term utility of tokens. This is different from attention scores, which are query-dependent: they capture the short-term utility for predicting the next token and are recomputed at every step, making them local, myopic, and highly dependent on the transient decoding state.
27
 
 
 
28
  ### Why TRIM-KV?
29
 
30
  It's fast
 
31
  <div align="center">
32
  <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/speed.png?raw=true"/>
33
  </div>
34
 
35
  It's smart
 
36
  <div align="center">
37
  <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/performance.png?raw=true"/>
38
  </div>
39
 
 
40
  And it's interpretable
 
41
  <div align="center">
42
  <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/eviction.png?raw=true"/>
43
  </div>
44
 
 
 
 
 
45
  ---
46
 
47
+ ## Quick Start
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  ### Installation
50
 
 
 
51
  ```sh
52
+ pip install trimkv
53
+ ```
 
 
 
 
54
 
55
+ ### Usage
56
 
57
  ```python
58
  import torch
59
  from trimkv.models.qwen3 import TrimKVQwen3ForCausalLM
60
+ from trimkv.cache_utils import PagedTrimKVCache
61
  from transformers import AutoTokenizer
62
 
63
+ model_path = "ngocbh/TrimKV-Qwen3-1.7B-Math"
 
64
 
65
  model = TrimKVQwen3ForCausalLM.from_pretrained(
66
  model_path,
67
  torch_dtype=torch.bfloat16,
68
  load_trimkv_weights=True,
 
69
  use_cache=True,
70
  device_map="cuda",
71
  )
 
 
72
  model.config._attn_implementation = "flash_attention_2"
 
 
 
73
 
74
  tokenizer = AutoTokenizer.from_pretrained(
75
+ model.config.base_model, use_fast=True, padding_side="left"
 
 
76
  )
77
 
78
+ # PagedTrimKVCache is the inference-time cache used by TRIM-KV.
79
+ # It allocates a global pool of blocks and reassigns them to heads on the fly.
80
+ past_key_values = PagedTrimKVCache(
81
+ num_layers=model.config.num_hidden_layers,
82
+ num_heads=model.config.num_key_value_heads,
83
+ max_seq_len=32768,
84
+ memory_size=128,
85
+ num_blocks_ratio=1.0,
86
+ buffer_size=32,
87
+ strategy="fixed_budget",
88
+ device="cuda",
89
+ )
90
 
91
+ # Use model.generate as normal — pass past_key_values to enable TrimKV eviction.
92
+ ```
93
 
94
+ ## Citation
 
 
 
 
 
 
 
 
95
 
96
+ ```bibtex
97
+ @article{bui2025make,
98
+ title={Make Each Token Count: Towards Improving Long-Context Performance with KV Cache Eviction},
99
+ author={Bui, Ngoc and Nguyen, Hieu Trung and Cohan, Arman and Ying, Rex},
100
+ journal={arXiv preprint arXiv:2512.03324},
101
+ year={2025}
102
+ }
103
+ ```