Text Generation
qwen3
math
trimkv
KV
Cache
Compression

Update model card: add pipeline tag, paper and code links

#1
by nielsr HF Staff - opened
Files changed (1) hide show
  1. README.md +30 -56
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-8B
 
 
 
 
7
  tags:
8
  - math
9
  - trimkv
@@ -12,61 +13,42 @@ 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
 
22
  <a href="https://arxiv.org/pdf/2512.03324"><img src="https://img.shields.io/badge/arxiv-2512.03324-red?style=for-the-badge"></a>
23
 
 
24
 
25
- ### Why TRIM-KV?
26
-
27
- It's fast
28
-
29
- <div align="center">
30
- <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/speed.png?raw=true"/>
31
- </div>
32
-
33
- It's smart
34
 
35
- <div align="center">
36
- <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/performance.png?raw=true"/>
37
- </div>
38
 
 
39
 
40
- And it's interpretable
41
 
42
  <div align="center">
43
- <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/eviction.png?raw=true"/>
44
  </div>
45
 
46
  <div align="center">
47
- <img width="1000" alt="teaser" src="https://github.com/ngocbh/trimkv/blob/main/assets/vis.png?raw=true"/>
48
  </div>
49
 
50
  ---
51
 
52
- ## Getting Started
53
-
54
- ### Requirements
55
-
56
- - Python 3.11 or higher (tested with 3.12)
57
- - PyTorch 2.7.0 or higher (tested with 2.8.0)
58
- - FlashAttention 2.7.2.post1 or higher (tested with 2.8.0)
59
- - Transformers 4.57.1
60
-
61
- ```sh
62
- pip install -r requirements.txt
63
- ```
64
-
65
- 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).
66
 
67
  ### Installation
68
 
69
- From the root of the repo:
70
 
71
  ```sh
72
  git clone https://github.com/ngocbh/trimkv.git
@@ -74,9 +56,7 @@ cd trimkv
74
  pip install -e .
75
  ````
76
 
77
- ---
78
-
79
- ## Quick Start
80
 
81
  ```python
82
  import torch
@@ -84,14 +64,13 @@ from trimkv.models.qwen3 import TrimKVQwen3ForCausalLM
84
  from trimkv.cache_utils import TrimKVCache
85
  from transformers import AutoTokenizer
86
 
87
- model_path = "<TrimKV model_path here>"
88
- download_from = "huggingface" # options: "wandb", "local", "huggingface"
89
 
90
  model = TrimKVQwen3ForCausalLM.from_pretrained(
91
  model_path,
92
  torch_dtype=torch.bfloat16,
93
  load_trimkv_weights=True,
94
- download_from=download_from,
95
  use_cache=True,
96
  device_map="cuda",
97
  )
@@ -112,18 +91,13 @@ tokenizer = AutoTokenizer.from_pretrained(
112
  # Note: TRIM-KV uses TrimKVCache under the hood. So please pass TrimKVCache to model.generate
113
  ```
114
 
115
- For a runnable end-to-end example, see [`examples/test_qwen3.py`](examples/test_qwen3.py).
116
-
117
- ## Released Models
118
-
119
- | Base Model | TRIM-KV Checkpoints | Training Datasets | Training Context Len | Training $M$ |
120
- |------------------------------|-----------------------------------------------|--------------------------|-------------------------|--------------|
121
- | Qwen3-1.7B | [TRIM-KV-Qwen3-1.7B-Math](https://huggingface.co/ngocbh/TrimKV-Qwen3-1.7B-Math) | OpenR1-Math-220k | 16K | 256 |
122
- | Qwen3-4B | [TRIM-KV-Qwen3-4B-Math](https://huggingface.co/ngocbh/TrimKV-Qwen3-4B-Math) | OpenR1-Math-220k | 16K | 256 |
123
- | Qwen3-8B | [TRIM-KV-Qwen3-8B-Math](https://huggingface.co/ngocbh/TrimKV-Qwen3-8B-Math) | OpenR1-Math-220k | 16K | 256 |
124
- | Qwen3-14B | [TRIM-KV-Qwen3-14B-Math](https://huggingface.co/ngocbh/TrimKV-Qwen3-14B-Math) | OpenR1-Math-220k | 16K | 256 |
125
- | Qwen3-4B-Instruct-2507 | [TrimKV-Qwen3-4B-Instruct-2507](https://huggingface.co/ngocbh/TrimKV-Qwen3-4B-Instruct-2507) | Synth-Long, BookSum, Buddhi | 128K | 1024 |
126
- | Phi-3-mini-128k-instruct | [TrimKV-Phi-3-mini-128k-instruct](https://huggingface.co/ngocbh/TrimKV-Phi-3-mini-128k-instruct) | LongAlpaca | 128K | 512 |
127
- | 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 |
128
 
129
- ---
 
 
 
 
 
 
 
 
1
  ---
 
 
 
2
  base_model:
3
  - Qwen/Qwen3-8B
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-Qwen3-8B-Math
17
 
18
+ 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.
19
 
20
+ This model is a Qwen3-8B checkpoint trained with TRIM-KV on math reasoning tasks, as presented in the paper [Make Each Token Count: Towards Improving Long-Context Performance with KV Cache Eviction](https://huggingface.co/papers/2605.09649).
21
 
22
+ - **Paper:** [https://huggingface.co/papers/2512.03324](https://huggingface.co/papers/2512.03324)
23
+ - **GitHub:** [https://github.com/ngocbh/trimkv](https://github.com/ngocbh/trimkv)
24
 
25
  <a href="https://arxiv.org/pdf/2512.03324"><img src="https://img.shields.io/badge/arxiv-2512.03324-red?style=for-the-badge"></a>
26
 
27
+ ### About TRIM-KV
28
 
29
+ 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.
 
 
 
 
 
 
 
 
30
 
31
+ 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.
 
 
32
 
33
+ ### Why TRIM-KV?
34
 
35
+ It's fast, smart, and interpretable.
36
 
37
  <div align="center">
38
+ <img width="1000" alt="speed" src="https://github.com/ngocbh/trimkv/blob/main/assets/speed.png?raw=true"/>
39
  </div>
40
 
41
  <div align="center">
42
+ <img width="1000" alt="performance" src="https://github.com/ngocbh/trimkv/blob/main/assets/performance.png?raw=true"/>
43
  </div>
44
 
45
  ---
46
 
47
+ ## Quick Start
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  ### Installation
50
 
51
+ To use this model, you need to install the `trimkv` library:
52
 
53
  ```sh
54
  git clone https://github.com/ngocbh/trimkv.git
 
56
  pip install -e .
57
  ````
58
 
59
+ ### Inference
 
 
60
 
61
  ```python
62
  import torch
 
64
  from trimkv.cache_utils import TrimKVCache
65
  from transformers import AutoTokenizer
66
 
67
+ model_path = "ngocbh/TrimKV-Qwen3-8B-Math"
 
68
 
69
  model = TrimKVQwen3ForCausalLM.from_pretrained(
70
  model_path,
71
  torch_dtype=torch.bfloat16,
72
  load_trimkv_weights=True,
73
+ download_from="huggingface",
74
  use_cache=True,
75
  device_map="cuda",
76
  )
 
91
  # Note: TRIM-KV uses TrimKVCache under the hood. So please pass TrimKVCache to model.generate
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
+ ```