chopratejas commited on
Commit
490f973
Β·
verified Β·
1 Parent(s): ee0536c

Update model card: v9 evals, architecture details, comparison with LLMLingua-2

Browse files
Files changed (1) hide show
  1. README.md +125 -117
README.md CHANGED
@@ -2,28 +2,24 @@
2
  license: apache-2.0
3
  language:
4
  - en
5
- library_name: transformers
6
  tags:
7
  - token-compression
8
- - prompt-compression
9
- - context-compression
10
  - agentic
11
  - modernbert
12
- - llmlingua
13
- - headroom
14
- - tool-outputs
15
- - structured-data
16
- pipeline_tag: token-classification
17
- base_model: answerdotai/ModernBERT-base
18
  datasets:
19
- - SWE-bench/SWE-smith-trajectories
20
- - glaiveai/glaive-function-calling-v2
21
- - nebius/SWE-agent-trajectories
22
- - Agent-Ark/Toucan-1.5M
23
- - tuandunghcmut/toolbench-v1
24
- - JetBrains-Research/diff-xyz
25
- - code_search_net
26
- - b-mc2/sql-create-context
 
 
 
27
  model-index:
28
  - name: kompress-base
29
  results:
@@ -31,142 +27,154 @@ model-index:
31
  type: token-classification
32
  name: Token Compression
33
  metrics:
34
- - type: f1
35
- value: 0.9956
36
- name: F1
37
- - type: accuracy
38
- value: 0.9926
39
- name: Accuracy
 
 
 
 
 
 
40
  ---
41
 
42
- # Kompress: Token Compression for Structured Tool Outputs & Agentic Contexts
43
-
44
- **Kompress** is a ModernBERT-based token compressor trained on **330K examples** of structured tool outputs β€” JSON API responses, git diffs, error logs, source code, CLI output, database results, and agentic conversation traces. It is a drop-in replacement for [LLMLingua-2](https://arxiv.org/abs/2403.12968).
45
 
46
- ## Key Results
47
 
48
- ### On Agentic / Structured Data (our target domain)
49
 
50
  | Metric | Kompress | LLMLingua-2 |
51
  |--------|----------|-------------|
52
- | Entity Preservation | **82.1%** | 36.0% |
53
- | Compression Ratio | **48.1%** | 206.0% (expands!) |
54
- | Model Size | **600 MB** | 1,400 MB |
55
- | Context Window | **8,192** | 512 |
56
- | Parameters | **149M** | 355M |
 
57
 
58
- ### On LLMLingua-2's Benchmarks
59
 
60
- | Dataset | Kompress | LLMLingua-2 | Note |
61
- |---------|----------|-------------|------|
62
- | MeetingBank | 46.3% | **57.4%** | LLMLingua's training domain |
63
- | GSM8K | 97.8% | **98.9%** | Both excellent; LLMLingua keeps 88% vs Kompress 50% |
 
64
 
65
- ### Cross-Agent Generalization (Cursor IDE β€” never seen in training)
66
 
67
- | Metric | Kompress | LLMLingua-2 |
68
- |--------|----------|-------------|
69
- | Entity Preservation | **91.1%** | 13.5% |
70
- | Compression Ratio | **49.9%** | 85.8% |
71
 
72
- ## Why Kompress?
73
 
74
- LLMLingua-2 was trained on meeting transcripts. When applied to structured tool outputs, it:
 
75
 
76
- - **Destroys file paths**: `/Users/foo/.claude/tasks/abc-123` β†’ `abc - 123 abc 123`
77
- - **Expands instead of compressing**: 206% average ratio on agentic data
78
- - **Fragments UUIDs**: `4e149fea-6eb8-4feb` β†’ `4e149fea - 6eb8 - 4feb`
79
- - **Has no cross-chunk awareness**: 512-token limit
80
 
81
- Kompress fixes these with:
82
 
83
- 1. **Trained on structured data** β€” 330K examples of real tool outputs: JSON, diffs, logs, code, CLI output, SQL
84
- 2. **Dual-head architecture** β€” token classification + span importance CNN prevents entity splitting
85
- 3. **ModernBERT backbone** β€” 8K context window, code-pretrained, RoPE attention
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
- ## Training Data (330K examples)
88
 
89
- | Source | Examples | Type |
90
- |--------|----------|------|
91
- | Toucan-1.5M (MCP tool outputs) | ~80K | Real MCP server tool responses |
92
- | SWE-agent trajectories | ~60K | Bash output, file reads, git diffs |
93
- | ToolBench | ~50K | REST API JSON responses |
94
- | Glaive Function Calling | ~40K | Function call/response pairs |
95
- | CodeSearchNet | ~40K | Source code (Python, JS, Java, Go, Ruby, PHP) |
96
- | JetBrains diff-xyz | ~10K | Git unified diffs |
97
- | SQL create-context | ~10K | Database schemas + queries |
98
- | Claude Code sessions | ~15K | Real agentic coding traces (API-labeled) |
99
- | SWE-bench trajectories | ~15K | Open-source coding agent traces |
100
- | Glaive + SWE (API-labeled) | ~10K | Function calling + coding (API-labeled) |
101
 
102
- Labeling: Heuristic rules for structured data (JSON→keep keys, diffs→keep +/- lines, logs→keep errors) + Claude Sonnet distillation for natural language segments.
 
103
 
104
- ## Architecture
 
105
 
 
 
 
 
106
  ```
107
- Input tokens β†’ ModernBERT-base encoder (149M params, 8K context) β†’
108
- Head 1: Token-level keep/discard (Linear β†’ Softmax)
109
- Head 2: Span importance (Conv1d β†’ GELU β†’ Conv1d β†’ Sigmoid)
110
 
111
- Final score = token_prob Γ— (0.5 + 0.5 Γ— span_score)
 
 
 
112
  ```
113
 
114
- The span head (~200K extra params) learns contiguous importance regions, preventing entity splitting and maintaining coherence.
115
 
116
- ## Quick Start
117
 
118
- ```python
119
- pip install kompress
 
 
 
120
 
121
- from kompress.inference.pytorch_runner import KompressRunner
 
122
 
123
- runner = KompressRunner(checkpoint_path="chopratejas/kompress-base")
124
- result = runner.compress(
125
- '{"users": [{"id": 1, "name": "Alice", "email": "alice@example.com"}, '
126
- '{"id": 2, "name": "Bob", "email": "bob@example.com"}, '
127
- '{"id": 3, "name": "Charlie", "email": "charlie@example.com"}]}',
128
- target_ratio=0.5,
129
- )
130
- print(result.compressed)
131
- # Keeps: keys, structure, unique values β€” discards repetitive patterns
132
- ```
133
 
134
- ## Use with Headroom
135
 
136
- ```python
137
- from kompress.integration.headroom_bridge import patch_content_router
138
- from headroom.transforms import ContentRouter
139
-
140
- router = ContentRouter()
141
- patch_content_router(router) # Swaps LLMLingua β†’ Kompress
142
- ```
143
 
144
- ## Training Details
145
 
146
- - **Base model**: [answerdotai/ModernBERT-base](https://huggingface.co/answerdotai/ModernBERT-base) (149M params)
147
- - **Training**: 3 epochs, batch=64, lr=2e-5, AdamW + torch.compile on NVIDIA H100
148
- - **Loss**: CrossEntropy (token head) + 0.3 Γ— BCE (span head)
149
- - **Final metrics**: F1=0.9956, Precision=0.9959, Recall=0.9953, train_loss=0.068
150
- - **Training time**: 2h39m on H100 (330K examples, 3 epochs)
151
 
152
- ## License
153
 
154
- Apache 2.0
 
 
 
 
 
 
 
 
155
 
156
- ## Citation
157
 
158
- ```bibtex
159
- @software{kompress2025,
160
- title={Kompress: Token Compression for Structured Tool Outputs and Agentic Contexts},
161
- author={Tejas Chopra},
162
- year={2025},
163
- url={https://huggingface.co/chopratejas/kompress-base},
164
- }
165
- ```
166
 
167
- ## Links
168
 
169
- - [GitHub](https://github.com/chopratejas/kompress) β€” Source code, training pipeline, eval scripts
170
- - [Headroom](https://github.com/chopratejas/headroom) β€” Context compression framework
171
- - [LLMLingua-2](https://arxiv.org/abs/2403.12968) β€” The model Kompress replaces
172
- - [ModernBERT](https://huggingface.co/answerdotai/ModernBERT-base) β€” Base encoder
 
2
  license: apache-2.0
3
  language:
4
  - en
 
5
  tags:
6
  - token-compression
7
+ - context-optimization
8
+ - llm
9
  - agentic
10
  - modernbert
 
 
 
 
 
 
11
  datasets:
12
+ - lmsys/lmsys-chat-1m
13
+ - cnn_dailymail
14
+ - EdinburghNLP/xsum
15
+ - ccdv/govreport-summarization
16
+ - ccdv/arxiv-summarization
17
+ - huuuyeah/meetingbank
18
+ - knkarthick/samsum
19
+ metrics:
20
+ - f1
21
+ - accuracy
22
+ pipeline_tag: token-classification
23
  model-index:
24
  - name: kompress-base
25
  results:
 
27
  type: token-classification
28
  name: Token Compression
29
  metrics:
30
+ - name: Quality Score (Claude-judged)
31
+ type: custom
32
+ value: 6.9
33
+ - name: LLMLingua-2 Quality Score
34
+ type: custom
35
+ value: 6.2
36
+ - name: Latency (median, Apple Silicon)
37
+ type: latency
38
+ value: 49ms
39
+ - name: LLMLingua-2 Latency
40
+ type: latency
41
+ value: 113ms
42
  ---
43
 
44
+ # Kompress: ModernBERT Token Compressor for LLM Context Windows
 
 
45
 
46
+ **Kompress compresses text in LLM context windows so agents can do more with less.** It's a drop-in replacement for LLMLingua-2 that's higher quality and 2.3x faster.
47
 
48
+ ## Results
49
 
50
  | Metric | Kompress | LLMLingua-2 |
51
  |--------|----------|-------------|
52
+ | **Quality (Claude-judged, 1-10)** | **6.9** | 6.2 |
53
+ | **Latency (median)** | **49ms** | 113ms |
54
+ | Speed | **2.3x faster** | baseline |
55
+ | Model size | 600MB (150M params) | 710MB (179M params) |
56
+ | Max sequence length | **8,192 tokens** | 512 tokens |
57
+ | Architecture | ModernBERT-base (2024) | mBERT (2018) |
58
 
59
+ ### Quality on Real Agent Data
60
 
61
+ | Eval Set | Kompress | LLMLingua-2 |
62
+ |----------|----------|-------------|
63
+ | Unstructured NL text | **6.9/10** | 6.2/10 |
64
+ | Claude Code sessions (text) | **6.7/10** | 5.2/10 |
65
+ | Claude Code sessions (raw) | **6.6/10** | 5.2/10 |
66
 
67
+ Quality scores are judged by Claude Sonnet 4.6: "Can an LLM fully understand and act on the compressed version?" (1-10 scale).
68
 
69
+ ## How It Works
 
 
 
70
 
71
+ Kompress is a **dual-head ModernBERT** model trained to classify each token as keep or discard:
72
 
73
+ - **Token head**: Binary classifier (keep/discard per token via argmax)
74
+ - **Span head**: 1D CNN that identifies important regions, boosts borderline tokens in critical spans
75
 
76
+ The model decides how much to compress based on content density β€” no fixed compression ratio.
 
 
 
77
 
78
+ ### Example
79
 
80
+ ```
81
+ ORIGINAL (98 words):
82
+ After investigating the memory leak, I traced it to the event listener
83
+ registration in the WebSocket handler. Every time a client connects, we
84
+ register a new listener on the global event bus, but when the client
85
+ disconnects, the cleanup function only removes the WebSocket connection
86
+ from the pool β€” it doesn't unregister the event listener. Over time,
87
+ these orphaned listeners accumulate and each one holds a reference to
88
+ the connection's closure, which in turn holds the entire request context.
89
+ The fix is straightforward: store the listener reference at connection
90
+ time and explicitly remove it in the disconnect handler.
91
+
92
+ COMPRESSED (59 words, 60% kept):
93
+ investigating memory leak, traced event listener registration WebSocket
94
+ handler. Every time client connects, register new listener global event
95
+ bus, client disconnects, cleanup function only removes WebSocket
96
+ connection pool β€” doesn't unregister event listener. Over time, orphaned
97
+ listeners accumulate each one holds reference connection's closure, holds
98
+ entire request context. fix straightforward: store listener reference
99
+ connection time explicitly remove disconnect handler.
100
+ ```
101
 
102
+ An LLM can fully understand and act on the compressed version.
103
 
104
+ ## Usage
 
 
 
 
 
 
 
 
 
 
 
105
 
106
+ ```python
107
+ from kompress.inference.pytorch_runner import KompressRunner
108
 
109
+ # Auto-downloads from HuggingFace on first use
110
+ runner = KompressRunner()
111
 
112
+ result = runner.compress("Your long text here...")
113
+ print(result.compressed) # Compressed text
114
+ print(result.compression_ratio) # e.g., 0.62
115
+ print(result.tokens_saved) # Number of tokens saved
116
  ```
 
 
 
117
 
118
+ ### With Headroom (LLM Proxy)
119
+
120
+ ```bash
121
+ pip install headroom-ai
122
  ```
123
 
124
+ Kompress is built into [Headroom](https://github.com/headroom-ai/headroom) as the default text compressor. It auto-downloads and runs on every API request that passes through the proxy.
125
 
126
+ ## Training
127
 
128
+ ### Architecture
129
+ - **Base**: `answerdotai/ModernBERT-base` (149M params, 8192 token context)
130
+ - **Token head**: Linear(768, 2) β€” binary keep/discard classifier
131
+ - **Span head**: Conv1d(768β†’256, k=5) β†’ GELU β†’ Conv1d(256β†’1, k=3) β†’ Sigmoid
132
+ - **Total**: 150M params
133
 
134
+ ### Data
135
+ 215K extractive compression labels from 8 diverse datasets, labeled by Claude Sonnet 4.6:
136
 
137
+ | Dataset | Count | Type |
138
+ |---------|-------|------|
139
+ | LMSYS-Chat-1M | 57K | LLM conversations |
140
+ | CNN/DailyMail | 50K | News articles |
141
+ | WikiHow | 50K | How-to guides |
142
+ | MeetingBank | 50K | Meeting transcripts |
143
+ | XSum | 47K | News articles |
144
+ | GovReport | 25K | Government reports |
145
+ | ArXiv | 25K | Academic papers |
146
+ | SAMSum | 14K | Dialogues |
147
 
148
+ ### Labeling Approach
149
 
150
+ Key insight: the labels must be **strictly extractive** β€” a subset of original words in original order. Previous versions failed because the labeling LLM rephrased text, causing alignment failures (5-12% keep ratio instead of the intended 40-60%).
 
 
 
 
 
 
151
 
152
+ The fix: prompt Claude to "select words like highlighting with a marker" rather than "compress this text." This ensures every word in the compressed output exists in the original, and the greedy alignment recovers 95%+ of the intended labels.
153
 
154
+ ### Training Details
155
+ - 3 epochs, batch size 32, learning rate 2e-5
156
+ - BF16 mixed precision on NVIDIA H100
157
+ - HuggingFace Trainer with warmup + cosine schedule
158
+ - ~3 hours training time
159
 
160
+ ## Comparison with LLMLingua-2
161
 
162
+ | | Kompress | LLMLingua-2 |
163
+ |---|---|---|
164
+ | Architecture | ModernBERT (2024) | mBERT (2018) |
165
+ | Max context | 8,192 tokens | 512 tokens |
166
+ | Training data | 215K from 8 datasets | 41K from MeetingBank |
167
+ | Labeling model | Claude Sonnet 4.6 | GPT-4 |
168
+ | Compression style | Content-adaptive | Fixed ratio |
169
+ | Quality | **6.9/10** | 6.2/10 |
170
+ | Latency | **49ms** | 113ms |
171
 
172
+ ## Limitations
173
 
174
+ - English only (ModernBERT is English-focused)
175
+ - Best on natural language text; structured data (JSON, code, logs) should use specialized compressors
176
+ - Compression ratio varies by content (60-80% kept for dense text, 40-60% for verbose text)
 
 
 
 
 
177
 
178
+ ## License
179
 
180
+ Apache 2.0