alea-institute commited on
Commit
98fa34d
·
verified ·
1 Parent(s): a08a3b8

Upload KL3M multi-word tokenizer (32K) - Update README

Browse files
Files changed (1) hide show
  1. README.md +216 -117
README.md CHANGED
@@ -1,199 +1,298 @@
1
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  library_name: transformers
3
- tags: []
4
  ---
5
 
6
- # Model Card for Model ID
7
 
8
- <!-- Provide a quick summary of what the model is/does. -->
9
 
 
10
 
 
11
 
12
- ## Model Details
 
 
 
13
 
14
- ### Model Description
15
 
16
- <!-- Provide a longer summary of what this model is. -->
17
 
18
- This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
 
 
 
 
 
 
 
19
 
20
- - **Developed by:** [More Information Needed]
21
- - **Funded by [optional]:** [More Information Needed]
22
- - **Shared by [optional]:** [More Information Needed]
23
- - **Model type:** [More Information Needed]
24
- - **Language(s) (NLP):** [More Information Needed]
25
- - **License:** [More Information Needed]
26
- - **Finetuned from model [optional]:** [More Information Needed]
27
 
28
- ### Model Sources [optional]
29
 
30
- <!-- Provide the basic links for the model. -->
31
 
32
- - **Repository:** [More Information Needed]
33
- - **Paper [optional]:** [More Information Needed]
34
- - **Demo [optional]:** [More Information Needed]
35
 
36
- ## Uses
 
 
 
37
 
38
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
 
 
39
 
40
- ### Direct Use
 
 
 
41
 
42
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
 
 
43
 
44
- [More Information Needed]
 
 
 
45
 
46
- ### Downstream Use [optional]
47
 
48
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
 
50
- [More Information Needed]
 
 
 
 
 
 
 
51
 
52
- ### Out-of-Scope Use
 
 
 
 
53
 
54
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
 
 
 
55
 
56
- [More Information Needed]
57
 
58
- ## Bias, Risks, and Limitations
59
 
60
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
 
 
 
 
 
 
 
 
61
 
62
- [More Information Needed]
63
 
64
- ### Recommendations
65
 
66
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
 
67
 
68
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
 
69
 
70
- ## How to Get Started with the Model
 
 
 
71
 
72
- Use the code below to get started with the model.
 
 
73
 
74
- [More Information Needed]
75
-
76
- ## Training Details
77
-
78
- ### Training Data
79
-
80
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
-
82
- [More Information Needed]
83
-
84
- ### Training Procedure
85
-
86
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
-
88
- #### Preprocessing [optional]
89
-
90
- [More Information Needed]
91
 
 
 
92
 
93
- #### Training Hyperparameters
 
94
 
95
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
 
 
 
 
96
 
97
- #### Speeds, Sizes, Times [optional]
98
 
99
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
 
100
 
101
- [More Information Needed]
102
 
103
- ## Evaluation
 
 
 
 
 
 
 
 
104
 
105
- <!-- This section describes the evaluation protocols and provides the results. -->
106
-
107
- ### Testing Data, Factors & Metrics
108
-
109
- #### Testing Data
110
-
111
- <!-- This should link to a Dataset Card if possible. -->
112
-
113
- [More Information Needed]
114
-
115
- #### Factors
116
-
117
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
-
119
- [More Information Needed]
120
 
121
- #### Metrics
122
 
123
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
 
 
 
 
124
 
125
- [More Information Needed]
126
 
127
- ### Results
128
 
129
- [More Information Needed]
 
 
 
 
 
 
 
 
 
 
 
130
 
131
- #### Summary
 
 
 
132
 
 
133
 
 
134
 
135
- ## Model Examination [optional]
136
 
137
- <!-- Relevant interpretability work for the model goes here -->
 
138
 
139
- [More Information Needed]
 
140
 
141
- ## Environmental Impact
 
 
 
 
142
 
143
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
 
145
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
 
147
- - **Hardware Type:** [More Information Needed]
148
- - **Hours used:** [More Information Needed]
149
- - **Cloud Provider:** [More Information Needed]
150
- - **Compute Region:** [More Information Needed]
151
- - **Carbon Emitted:** [More Information Needed]
 
 
 
152
 
153
- ## Technical Specifications [optional]
154
 
155
- ### Model Architecture and Objective
156
 
157
- [More Information Needed]
 
 
 
158
 
159
- ### Compute Infrastructure
 
 
 
160
 
161
- [More Information Needed]
162
 
163
- #### Hardware
164
 
165
- [More Information Needed]
166
 
167
- #### Software
168
 
169
- [More Information Needed]
170
 
171
- ## Citation [optional]
172
 
173
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
 
175
- **BibTeX:**
176
 
177
- [More Information Needed]
178
 
179
- **APA:**
 
 
 
 
 
 
 
180
 
181
- [More Information Needed]
182
 
183
- ## Glossary [optional]
 
 
 
 
 
 
 
184
 
185
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
 
187
- [More Information Needed]
188
 
189
- ## More Information [optional]
190
 
191
- [More Information Needed]
 
 
 
192
 
193
- ## Model Card Authors [optional]
194
 
195
- [More Information Needed]
196
 
197
- ## Model Card Contact
198
 
199
- [More Information Needed]
 
 
 
 
1
  ---
2
+ language:
3
+ - en
4
+ license: mit
5
+ tags:
6
+ - tokenizer
7
+ - legal
8
+ - bpe
9
+ - byte-pair-encoding
10
+ - multi-word
11
+ - kl3m
12
+ - legal-domain
13
+ - hierarchical
14
+ pipeline_tag: fill-mask
15
  library_name: transformers
 
16
  ---
17
 
18
+ # KL3M Multi-Word Tokenizer - 32K
19
 
20
+ This is the **32,768 token** variant of the KL3M (Kelvin Legal Large Language Model) multi-word tokenizer family, optimized for legal domain text with hierarchical vocabulary nesting.
21
 
22
+ ## Overview
23
 
24
+ The KL3M multi-word tokenizers are a family of byte-pair encoding (BPE) tokenizers trained on ~44GB of legal domain text from the [KL3M dataset](https://aleainstitute.ai/work/kl3m/) (copyright-clean legal corpus from the ALEA Institute). Unlike general-purpose tokenizers, these tokenizers:
25
 
26
+ - **Capture multi-word legal phrases** as single tokens (e.g., "United States", "with respect to", "Supreme Court")
27
+ - **Use hierarchical vocabulary nesting** where smaller vocabularies are proper subsets of larger ones
28
+ - **Enable vocabulary expansion experiments** and transfer learning across vocabulary sizes
29
+ - **Optimize for legal domain text** while maintaining general-purpose capability
30
 
31
+ ## Tokenizer Family
32
 
33
+ This tokenizer is part of a hierarchically nested family. Token IDs in smaller vocabularies are **identical** across all larger vocabularies, enabling seamless vocabulary expansion:
34
 
35
+ | Vocabulary Size | HuggingFace Repository | File Size |
36
+ |----------------|------------------------|-----------|
37
+ | 4,096 (4K) | [alea-institute/kl3m-multi-word-001-4k](https://huggingface.co/alea-institute/kl3m-multi-word-001-4k) | 118 KB |
38
+ | 8,192 (8K) | [alea-institute/kl3m-multi-word-001-8k](https://huggingface.co/alea-institute/kl3m-multi-word-001-8k) | 249 KB |
39
+ | 16,384 (16K) | [alea-institute/kl3m-multi-word-001-16k](https://huggingface.co/alea-institute/kl3m-multi-word-001-16k) | 529 KB |
40
+ | 32,768 (32K) | [alea-institute/kl3m-multi-word-001-32k](https://huggingface.co/alea-institute/kl3m-multi-word-001-32k) | 1.2 MB |
41
+ | 65,536 (65K) | [alea-institute/kl3m-multi-word-001-65k](https://huggingface.co/alea-institute/kl3m-multi-word-001-65k) | 2.4 MB |
42
+ | 131,072 (131K) | [alea-institute/kl3m-multi-word-001-131k](https://huggingface.co/alea-institute/kl3m-multi-word-001-131k) | 5.2 MB |
43
 
44
+ **→ You are viewing: 32,768 (32K)**
 
 
 
 
 
 
45
 
46
+ ## Key Features
47
 
48
+ ### 1. Multi-Word Tokenization
49
 
50
+ Legal text contains frequent multi-word phrases that benefit from being treated as single tokens. The larger vocabularies capture increasingly sophisticated legal terminology:
 
 
51
 
52
+ **Example: "with respect to" (common legal phrase)**
53
+ ```python
54
+ # 4K tokenizer: 3 tokens
55
+ ['with respec', 't ', 'to'] → [2286, 282, 393]
56
 
57
+ # 131K tokenizer: 1 token
58
+ ['with respect to'] → [15878]
59
+ ```
60
 
61
+ **Example: "Supreme Court"**
62
+ ```python
63
+ # 4K tokenizer: 5 tokens
64
+ ['Sup', 'rem', 'e ', 'Cour', 't'] → [4062, 1847, 265, 3032, 123]
65
 
66
+ # 131K tokenizer: 1 token
67
+ ['Supreme Court'] → [81439]
68
+ ```
69
 
70
+ **Other multi-word tokens in this vocabulary:**
71
+ - Common legal phrases: "United States" (→1 token), "in accordance with" (→1 token), "on behalf of" (→1 token)
72
+ - Frequent constructions: "of the " (→1 token), "in the " (→1 token), ", the " (→1 token)
73
+ - Legal terminology: "the defendant", "the Court", "Therefore,", "However,"
74
 
75
+ ### 2. Hierarchical Token ID Nesting
76
 
77
+ Token IDs are **preserved across vocabulary sizes** a token with ID 1846 in the 4K vocabulary has the **same ID** in all larger vocabularies:
78
 
79
+ ```python
80
+ # "of the" tokenizes to ID 1846 in ALL vocabulary sizes
81
+ 4K: [1846]
82
+ 8K: [1846]
83
+ 16K: [1846]
84
+ 32K: [1846]
85
+ 65K: [1846]
86
+ 131K: [1846]
87
 
88
+ # Special tokens are identical across all sizes
89
+ <|start|>: [0]
90
+ <|end|>: [1]
91
+ <|pad|>: [2]
92
+ ```
93
 
94
+ This enables:
95
+ - **Vocabulary expansion during training**: Start with 4K vocab, expand to 8K → 16K → 32K
96
+ - **Embedding transfer**: Initialize larger vocabulary models from smaller ones
97
+ - **Controlled ablation studies**: Isolate the effect of vocabulary size
98
 
99
+ ### 3. Special Tokens
100
 
101
+ All tokenizers include 7 special tokens with consistent IDs:
102
 
103
+ | Token | ID | Purpose |
104
+ |-------|---:|---------|
105
+ | `<\|start\|>` | 0 | Start of sequence (GPT-style) |
106
+ | `<\|end\|>` | 1 | End of sequence |
107
+ | `<\|pad\|>` | 2 | Padding token |
108
+ | `<\|unk\|>` | 3 | Unknown token |
109
+ | `<\|cls\|>` | 4 | Classification token (BERT-style) |
110
+ | `<\|sep\|>` | 5 | Separator token (BERT-style) |
111
+ | `<\|mask\|>` | 6 | Mask token (MLM training) |
112
 
113
+ ## Usage
114
 
115
+ ### Quick Start
116
 
117
+ ```python
118
+ from transformers import PreTrainedTokenizerFast
119
 
120
+ # Load tokenizer
121
+ tokenizer = PreTrainedTokenizerFast.from_pretrained("alea-institute/kl3m-multi-word-001-32k")
122
 
123
+ # Tokenize text
124
+ text = "The Supreme Court held that the defendant violated due process."
125
+ tokens = tokenizer.tokenize(text)
126
+ ids = tokenizer.encode(text)
127
 
128
+ print(f"Tokens: {tokens}")
129
+ print(f"Token IDs: {ids}")
130
+ ```
131
 
132
+ ### Using with 🤗 Tokenizers Library
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
+ ```python
135
+ from tokenizers import Tokenizer
136
 
137
+ # Load tokenizer
138
+ tokenizer = Tokenizer.from_pretrained("alea-institute/kl3m-multi-word-001-32k")
139
 
140
+ # Encode text
141
+ encoding = tokenizer.encode("in accordance with the United States Code")
142
+ print(f"Tokens: {encoding.tokens}")
143
+ print(f"IDs: {encoding.ids}")
144
+ ```
145
 
146
+ ### Configuration for Training
147
 
148
+ ```python
149
+ from transformers import PreTrainedTokenizerFast
150
 
151
+ tokenizer = PreTrainedTokenizerFast.from_pretrained("alea-institute/kl3m-multi-word-001-32k")
152
 
153
+ # Configure special tokens for your model
154
+ tokenizer.pad_token = "<|pad|>"
155
+ tokenizer.eos_token = "<|end|>"
156
+ tokenizer.bos_token = "<|start|>"
157
+ tokenizer.unk_token = "<|unk|>"
158
+ tokenizer.cls_token = "<|cls|>" # For BERT-style models
159
+ tokenizer.sep_token = "<|sep|>" # For BERT-style models
160
+ tokenizer.mask_token = "<|mask|>" # For masked language modeling
161
+ ```
162
 
163
+ ## Training Details
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
 
165
+ ### Training Corpus
166
 
167
+ - **Source**: KL3M (Kelvin Legal Large Language Model) dataset
168
+ - **Size**: ~44.2 GB (44,168,540,153 bytes)
169
+ - **Content**: 1,018,355,750 lines, 5,997,814,602 words
170
+ - **Domain**: Legal text (court opinions, contracts, statutes, legal documents)
171
+ - **License**: Copyright-clean corpus from the ALEA Institute
172
 
173
+ ### Training Method
174
 
175
+ Trained using the `bbpe` (Binary Byte Pair Encoding) Rust crate with multi-word optimization:
176
 
177
+ ```bash
178
+ zcat /nas4/data/kl3m/kl3m-bbpe-sample.txt.gz | \
179
+ bbpe train -v - \
180
+ --max-entropy 7.0 \
181
+ --preprocessor unicode-whitespace \
182
+ --preprocessor-probability 0.1 \
183
+ --vocab-size 131072 \
184
+ --family-size 65536 --family-size 32768 --family-size 16384 \
185
+ --family-size 8192 --family-size 4096 \
186
+ --family-template tokenizer-{size}.json \
187
+ --output tokenizer-131072.json
188
+ ```
189
 
190
+ Parameters:
191
+ - `max-entropy 7.0`: Entropy threshold balancing multi-word phrases with common tokens
192
+ - `family-size`: Creates nested vocabulary families ensuring ID consistency
193
+ - `preprocessor unicode-whitespace`: Whitespace normalization
194
 
195
+ ## Use Cases
196
 
197
+ ### 1. Legal Language Models
198
 
199
+ Train domain-specific language models optimized for legal text:
200
 
201
+ ```python
202
+ from transformers import AutoModelForCausalLM, PreTrainedTokenizerFast
203
 
204
+ tokenizer = PreTrainedTokenizerFast.from_pretrained("alea-institute/kl3m-multi-word-001-32k")
205
+ model = AutoModelForCausalLM.from_pretrained("your-legal-model")
206
 
207
+ # The model will efficiently process legal terminology
208
+ text = "The Court held that the statute of limitations had expired."
209
+ inputs = tokenizer(text, return_tensors="pt")
210
+ outputs = model(**inputs)
211
+ ```
212
 
213
+ ### 2. Vocabulary Ablation Studies
214
 
215
+ Compare model performance across vocabulary sizes:
216
 
217
+ ```python
218
+ # Train models with different vocabulary sizes
219
+ for vocab_size in ["4k", "8k", "16k", "32k", "65k", "131k"]:
220
+ tokenizer = PreTrainedTokenizerFast.from_pretrained(
221
+ f"alea-institute/kl3m-multi-word-001-{vocab_size}"
222
+ )
223
+ # Train model and compare convergence, perplexity, downstream performance
224
+ ```
225
 
226
+ ### 3. Curriculum Learning with Vocabulary Expansion
227
 
228
+ Leverage hierarchical nesting for progressive vocabulary growth:
229
 
230
+ ```python
231
+ # Stage 1: Train with 4K vocabulary
232
+ tokenizer_4k = PreTrainedTokenizerFast.from_pretrained("alea-institute/kl3m-multi-word-001-4k")
233
+ # ... train model ...
234
 
235
+ # Stage 2: Expand to 16K vocabulary (embeddings for IDs 0-4095 are identical!)
236
+ tokenizer_16k = PreTrainedTokenizerFast.from_pretrained("alea-institute/kl3m-multi-word-001-16k")
237
+ # ... expand model embeddings and continue training ...
238
+ ```
239
 
240
+ ## Research Applications
241
 
242
+ These tokenizers enable research into:
243
 
244
+ 1. **Vocabulary Size Effects**: How does vocabulary size affect convergence speed, final perplexity, and downstream task performance?
245
 
246
+ 2. **Domain-Specific Tokenization**: Do legal domain tokenizers outperform general-purpose tokenizers (GPT-4, LLaMA) on legal tasks?
247
 
248
+ 3. **Multi-Word Phrase Modeling**: Does capturing legal phrases as single tokens improve legal reasoning and understanding?
249
 
250
+ 4. **Hierarchical Curriculum Learning**: Can progressive vocabulary expansion improve training efficiency or final performance?
251
 
252
+ 5. **Transfer Learning**: Can models trained on smaller vocabularies transfer knowledge to larger vocabularies?
253
 
254
+ ## Citation
255
 
256
+ If you use these tokenizers in your research, please cite:
257
 
258
+ ```bibtex
259
+ @misc{kl3m-multi-word-tokenizers-2025,
260
+ title={KL3M Multi-Word Tokenizers: Hierarchically Nested BPE for Legal Domain Language Modeling},
261
+ author={ALEA Institute},
262
+ year={2025},
263
+ url={https://huggingface.co/alea-institute/kl3m-multi-word-001-32k}
264
+ }
265
+ ```
266
 
267
+ Also consider citing the KL3M dataset:
268
 
269
+ ```bibtex
270
+ @article{kl3m-data-2025,
271
+ title={The KL3M Data Project: Copyright-Clean Training Resources for Large Language Models},
272
+ author={Bommarito, Michael and others},
273
+ journal={arXiv preprint arXiv:2504.07854},
274
+ year={2025}
275
+ }
276
+ ```
277
 
278
+ ## License
279
 
280
+ These tokenizers are released under the MIT License. The training corpus (KL3M dataset) is copyright-clean and permissively licensed.
281
 
282
+ ## Links
283
 
284
+ - **ALEA Institute**: https://aleainstitute.ai/
285
+ - **KL3M Project**: https://aleainstitute.ai/work/kl3m/
286
+ - **KL3M Dataset Paper**: https://arxiv.org/html/2504.07854
287
+ - **Research Repository**: https://github.com/alea-institute/multi-word-tokenization
288
 
289
+ ## Acknowledgments
290
 
291
+ These tokenizers were created as part of research into vocabulary size effects on legal language model performance. The KL3M dataset and tokenizers are stewarded by the ALEA Institute for public benefit.
292
 
293
+ ---
294
 
295
+ **Version**: 001
296
+ **Created**: November 2025
297
+ **Vocabulary Size**: 32,768 tokens
298
+ **Domain**: Legal text (with general-purpose capability)