tiktoken-fast 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,232 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "aho-corasick"
7
+ version = "1.1.5"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"
10
+ dependencies = [
11
+ "memchr",
12
+ ]
13
+
14
+ [[package]]
15
+ name = "anyhow"
16
+ version = "1.0.104"
17
+ source = "registry+https://github.com/rust-lang/crates.io-index"
18
+ checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
19
+
20
+ [[package]]
21
+ name = "base64"
22
+ version = "0.22.1"
23
+ source = "registry+https://github.com/rust-lang/crates.io-index"
24
+ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
25
+
26
+ [[package]]
27
+ name = "bit-set"
28
+ version = "0.8.0"
29
+ source = "registry+https://github.com/rust-lang/crates.io-index"
30
+ checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3"
31
+ dependencies = [
32
+ "bit-vec",
33
+ ]
34
+
35
+ [[package]]
36
+ name = "bit-vec"
37
+ version = "0.8.0"
38
+ source = "registry+https://github.com/rust-lang/crates.io-index"
39
+ checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7"
40
+
41
+ [[package]]
42
+ name = "bstr"
43
+ version = "1.13.1"
44
+ source = "registry+https://github.com/rust-lang/crates.io-index"
45
+ checksum = "6bb31b46c14244e20ee9984b11bf5c992b91fb6939fea616e3512c8baecdbe5f"
46
+ dependencies = [
47
+ "memchr",
48
+ "regex-automata",
49
+ "serde_core",
50
+ ]
51
+
52
+ [[package]]
53
+ name = "crossbeam-deque"
54
+ version = "0.8.8"
55
+ source = "registry+https://github.com/rust-lang/crates.io-index"
56
+ checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a"
57
+ dependencies = [
58
+ "crossbeam-epoch",
59
+ "crossbeam-utils",
60
+ ]
61
+
62
+ [[package]]
63
+ name = "crossbeam-epoch"
64
+ version = "0.9.21"
65
+ source = "registry+https://github.com/rust-lang/crates.io-index"
66
+ checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d"
67
+ dependencies = [
68
+ "crossbeam-utils",
69
+ ]
70
+
71
+ [[package]]
72
+ name = "crossbeam-utils"
73
+ version = "0.8.23"
74
+ source = "registry+https://github.com/rust-lang/crates.io-index"
75
+ checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
76
+
77
+ [[package]]
78
+ name = "either"
79
+ version = "1.19.0"
80
+ source = "registry+https://github.com/rust-lang/crates.io-index"
81
+ checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
82
+
83
+ [[package]]
84
+ name = "fancy-regex"
85
+ version = "0.19.2"
86
+ source = "registry+https://github.com/rust-lang/crates.io-index"
87
+ checksum = "d301f5bf187b3c295fce6468d3875037a0bccc5f6b151c63cac2f85babf21912"
88
+ dependencies = [
89
+ "bit-set",
90
+ "regex-automata",
91
+ "regex-syntax",
92
+ ]
93
+
94
+ [[package]]
95
+ name = "memchr"
96
+ version = "2.8.3"
97
+ source = "registry+https://github.com/rust-lang/crates.io-index"
98
+ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
99
+
100
+ [[package]]
101
+ name = "proc-macro2"
102
+ version = "1.0.107"
103
+ source = "registry+https://github.com/rust-lang/crates.io-index"
104
+ checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
105
+ dependencies = [
106
+ "unicode-ident",
107
+ ]
108
+
109
+ [[package]]
110
+ name = "quote"
111
+ version = "1.0.47"
112
+ source = "registry+https://github.com/rust-lang/crates.io-index"
113
+ checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
114
+ dependencies = [
115
+ "proc-macro2",
116
+ ]
117
+
118
+ [[package]]
119
+ name = "rayon"
120
+ version = "1.12.0"
121
+ source = "registry+https://github.com/rust-lang/crates.io-index"
122
+ checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
123
+ dependencies = [
124
+ "either",
125
+ "rayon-core",
126
+ ]
127
+
128
+ [[package]]
129
+ name = "rayon-core"
130
+ version = "1.13.0"
131
+ source = "registry+https://github.com/rust-lang/crates.io-index"
132
+ checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
133
+ dependencies = [
134
+ "crossbeam-deque",
135
+ "crossbeam-utils",
136
+ ]
137
+
138
+ [[package]]
139
+ name = "regex"
140
+ version = "1.13.1"
141
+ source = "registry+https://github.com/rust-lang/crates.io-index"
142
+ checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
143
+ dependencies = [
144
+ "aho-corasick",
145
+ "memchr",
146
+ "regex-automata",
147
+ "regex-syntax",
148
+ ]
149
+
150
+ [[package]]
151
+ name = "regex-automata"
152
+ version = "0.4.18"
153
+ source = "registry+https://github.com/rust-lang/crates.io-index"
154
+ checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
155
+ dependencies = [
156
+ "aho-corasick",
157
+ "memchr",
158
+ "regex-syntax",
159
+ ]
160
+
161
+ [[package]]
162
+ name = "regex-syntax"
163
+ version = "0.8.11"
164
+ source = "registry+https://github.com/rust-lang/crates.io-index"
165
+ checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
166
+
167
+ [[package]]
168
+ name = "rustc-hash"
169
+ version = "2.1.3"
170
+ source = "registry+https://github.com/rust-lang/crates.io-index"
171
+ checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d"
172
+
173
+ [[package]]
174
+ name = "serde_core"
175
+ version = "1.0.229"
176
+ source = "registry+https://github.com/rust-lang/crates.io-index"
177
+ checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
178
+ dependencies = [
179
+ "serde_derive",
180
+ ]
181
+
182
+ [[package]]
183
+ name = "serde_derive"
184
+ version = "1.0.229"
185
+ source = "registry+https://github.com/rust-lang/crates.io-index"
186
+ checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
187
+ dependencies = [
188
+ "proc-macro2",
189
+ "quote",
190
+ "syn",
191
+ ]
192
+
193
+ [[package]]
194
+ name = "syn"
195
+ version = "3.0.9"
196
+ source = "registry+https://github.com/rust-lang/crates.io-index"
197
+ checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b"
198
+ dependencies = [
199
+ "proc-macro2",
200
+ "quote",
201
+ "unicode-ident",
202
+ ]
203
+
204
+ [[package]]
205
+ name = "tiktoken-fast"
206
+ version = "0.1.3"
207
+ dependencies = [
208
+ "base64",
209
+ "rayon",
210
+ "regex-syntax",
211
+ "tiktoken-rs",
212
+ ]
213
+
214
+ [[package]]
215
+ name = "tiktoken-rs"
216
+ version = "0.12.1"
217
+ source = "registry+https://github.com/rust-lang/crates.io-index"
218
+ checksum = "2aeff724640cfe13037336ddf35befdffd2909cbdb65cf041cc8a4cf8c584cfa"
219
+ dependencies = [
220
+ "anyhow",
221
+ "base64",
222
+ "bstr",
223
+ "fancy-regex",
224
+ "regex",
225
+ "rustc-hash",
226
+ ]
227
+
228
+ [[package]]
229
+ name = "unicode-ident"
230
+ version = "1.0.27"
231
+ source = "registry+https://github.com/rust-lang/crates.io-index"
232
+ checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
@@ -0,0 +1,28 @@
1
+ [package]
2
+ name = "tiktoken-fast"
3
+ version = "0.1.3"
4
+ edition = "2021"
5
+ license = "MIT"
6
+ description = "Byte-pair encoder for OpenAI's tiktoken vocabularies: the token ids of the reference core, several times faster"
7
+ repository = "https://github.com/s3bastiankoch/tiktoken-fast"
8
+ readme = "README.md"
9
+ keywords = ["tokenizer", "tiktoken", "bpe", "openai", "llm"]
10
+ categories = ["text-processing", "encoding"]
11
+ include = ["/src/**", "/assets/**", "/Cargo.toml", "/README.md", "/LICENSE", "/NOTICE"]
12
+
13
+ [lib]
14
+ name = "tiktoken_fast"
15
+
16
+ [dependencies]
17
+ base64 = "0.22"
18
+ regex-syntax = "0.8"
19
+ rayon = "1.10"
20
+
21
+ [dev-dependencies]
22
+ tiktoken-rs = "0.12.1"
23
+
24
+ [profile.release]
25
+ opt-level = 3
26
+ lto = "fat"
27
+ codegen-units = 1
28
+ panic = "abort"
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sebastian Koch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sebastian Koch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,25 @@
1
+ The vocabulary files under assets/ are OpenAI's tiktoken vocabularies
2
+ (cl100k_base, o200k_base, p50k_base, r50k_base), copied from the tiktoken-rs
3
+ crate (MIT), which redistributes them. OpenAI serves these files for its
4
+ MIT-licensed tiktoken and states no separate licence for the files themselves. The texts under tests/corpora/
5
+ are the public-domain and open-licence corpora of the Go benchmark in go/bench
6
+ (Moby-Dick, Go standard library source under the Go BSD licence, 紅樓夢, and
7
+ the Universal Declaration of Human Rights in three languages); the Go licence
8
+ is in tests/corpora/GO-LICENSE.
9
+
10
+ src/backtrack.rs follows the backtracking byte-pair encoder of the bpe crate
11
+ (github.com/github/rust-gems, MIT licence): the split table, the pair-validity
12
+ test and the backtracking search are its design; the longest-token search here
13
+ uses length masks over the crate's own vocabulary table instead of an
14
+ Aho-Corasick automaton.
15
+
16
+ go/ is a fork of pkoukk/tiktoken-go (MIT licence); its own LICENSE and NOTICE
17
+ list the upstream copyright and every change from the fork point.
18
+
19
+ bench/fonts/ holds Inter (github.com/rsms/inter, SIL Open Font License 1.1),
20
+ used only to render the README figures; its licence is bench/fonts/OFL.txt.
21
+
22
+ The Unicode character classes in src/ are built from the tables of the
23
+ regex-syntax crate (MIT or Apache-2.0), which derive from the Unicode
24
+ Character Database, Copyright (c) 1991-2024 Unicode, Inc., used under the
25
+ Unicode License v3 (https://www.unicode.org/license.txt).
@@ -0,0 +1,25 @@
1
+ The vocabulary files under assets/ are OpenAI's tiktoken vocabularies
2
+ (cl100k_base, o200k_base, p50k_base, r50k_base), copied from the tiktoken-rs
3
+ crate (MIT), which redistributes them. OpenAI serves these files for its
4
+ MIT-licensed tiktoken and states no separate licence for the files themselves. The texts under tests/corpora/
5
+ are the public-domain and open-licence corpora of the Go benchmark in go/bench
6
+ (Moby-Dick, Go standard library source under the Go BSD licence, 紅樓夢, and
7
+ the Universal Declaration of Human Rights in three languages); the Go licence
8
+ is in tests/corpora/GO-LICENSE.
9
+
10
+ src/backtrack.rs follows the backtracking byte-pair encoder of the bpe crate
11
+ (github.com/github/rust-gems, MIT licence): the split table, the pair-validity
12
+ test and the backtracking search are its design; the longest-token search here
13
+ uses length masks over the crate's own vocabulary table instead of an
14
+ Aho-Corasick automaton.
15
+
16
+ go/ is a fork of pkoukk/tiktoken-go (MIT licence); its own LICENSE and NOTICE
17
+ list the upstream copyright and every change from the fork point.
18
+
19
+ bench/fonts/ holds Inter (github.com/rsms/inter, SIL Open Font License 1.1),
20
+ used only to render the README figures; its licence is bench/fonts/OFL.txt.
21
+
22
+ The Unicode character classes in src/ are built from the tables of the
23
+ regex-syntax crate (MIT or Apache-2.0), which derive from the Unicode
24
+ Character Database, Copyright (c) 1991-2024 Unicode, Inc., used under the
25
+ Unicode License v3 (https://www.unicode.org/license.txt).
@@ -0,0 +1,86 @@
1
+ Metadata-Version: 2.4
2
+ Name: tiktoken-fast
3
+ Version: 0.1.3
4
+ Classifier: Programming Language :: Python :: 3
5
+ Classifier: Programming Language :: Rust
6
+ Classifier: License :: OSI Approved :: MIT License
7
+ Requires-Dist: tiktoken>=0.7
8
+ License-File: LICENSE.txt
9
+ License-File: NOTICE.txt
10
+ Summary: A faster drop-in for tiktoken: the same token ids, encoded by a new Rust core
11
+ License-Expression: MIT
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
14
+ Project-URL: Repository, https://github.com/s3bastiankoch/tiktoken-fast
15
+
16
+ # tiktoken-fast for Python
17
+
18
+ A drop-in for [tiktoken](https://github.com/openai/tiktoken) that returns the
19
+ same token ids, faster. It keeps tiktoken's Python layer, its encoding files and
20
+ model names, and replaces only the core that turns text into tokens.
21
+
22
+ ```python
23
+ import tiktoken_fast as tiktoken
24
+
25
+ enc = tiktoken.get_encoding("o200k_base")
26
+ enc.encode("hello world")
27
+ enc.encode_batch(texts, num_threads=8)
28
+ ```
29
+
30
+ Or, for code that already imports tiktoken:
31
+
32
+ ```python
33
+ import tiktoken_fast
34
+ tiktoken_fast.patch() # tiktoken.get_encoding and encoding_for_model now return fast encodings
35
+ ```
36
+
37
+ Two additions that tiktoken does not have:
38
+
39
+ ```python
40
+ enc.count_tokens(text) # len(enc.encode(text)) without building the list
41
+ ```
42
+
43
+ ```sh
44
+ python -m tiktoken_fast.bench # compare with tiktoken on this machine, about 15 s
45
+ ```
46
+
47
+ The bench uses text every Python installation has (the standard library's
48
+ source and docstrings) unless you pass `--file`. It checks that every call
49
+ returns tiktoken's tokens before it times anything.
50
+
51
+ Every encoding tiktoken ships (`r50k_base`, `p50k_base`, `p50k_edit`,
52
+ `cl100k_base`, `o200k_base`, `o200k_harmony`) runs on the new core. An
53
+ `Encoding` built with a split pattern the core does not know keeps tiktoken's
54
+ core. `tiktoken_fast.Encoding` is a subclass of `tiktoken.Encoding`, so type
55
+ checks and pickling work as before.
56
+
57
+ ## Speed
58
+
59
+ Against tiktoken 0.14 through the same Python calls, one core of a Xeon
60
+ Platinum 8481C, Python 3.11, 2026-10-26. Geometric means over Wikipedia in
61
+ eleven languages, 512-byte chat chunks, code and two books, under cl100k_base
62
+ and o200k_base; slowest workload in brackets. No document's tokens differed.
63
+ On AMD EPYC 9B14 and Google Axion `encode_ordinary()` is 3.78x and 3.50x
64
+ ([all three machines](../README.md#numbers)).
65
+
66
+ | call | new text | fresh caches per document |
67
+ |---|---|---|
68
+ | `encode()` | **3.21x** [1.84x] | **2.64x** |
69
+ | `encode_ordinary()` | **3.28x** [1.80x] | **2.72x** |
70
+
71
+ Short texts gain more, because tiktoken spends most of a short call in Python:
72
+ `encode()` on 16-byte strings is 3.3x faster, and a batch of 16-byte strings
73
+ on four vCPUs 20x to 24x faster. Raw output is in
74
+ [`bench/results/2026-10-26-python`](../bench/results/2026-10-26-python).
75
+
76
+ ## Build
77
+
78
+ ```sh
79
+ pip install maturin
80
+ cd python && maturin build --release && pip install target/wheels/*.whl
81
+ python -m pytest tests
82
+ ```
83
+
84
+ Needs Python 3.10 or later and a Rust toolchain. The tests compare every public
85
+ method with tiktoken.
86
+