openai/tiktoken

Public

mirrored fromhttps://github.com/openai/tiktokenAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.11.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/test_encoding.py

263lines · modecode

1# Note that there are more actual tests, they're just not currently public :-)
2
3from typing import Callable
4
5import hypothesis
6import hypothesis.strategies as st
7import pytest
8
9import tiktoken
10
11from .test_helpers import ENCODING_FACTORIES, MAX_EXAMPLES
12
13
14def test_simple():
15 enc = tiktoken.get_encoding("gpt2")
16 assert enc.encode("hello world") == [31373, 995]
17 assert enc.decode([31373, 995]) == "hello world"
18 assert enc.encode("hello <|endoftext|>", allowed_special="all") == [31373, 220, 50256]
19
20 enc = tiktoken.get_encoding("cl100k_base")
21 assert enc.encode("hello world") == [15339, 1917]
22 assert enc.decode([15339, 1917]) == "hello world"
23 assert enc.encode("hello <|endoftext|>", allowed_special="all") == [15339, 220, 100257]
24
25 for enc_name in tiktoken.list_encoding_names():
26 enc = tiktoken.get_encoding(enc_name)
27 for token in range(min(10_000, enc.max_token_value - 1)):
28 assert enc.encode_single_token(enc.decode_single_token_bytes(token)) == token
29
30
31def test_simple_repeated():
32 enc = tiktoken.get_encoding("gpt2")
33 assert enc.encode("0") == [15]
34 assert enc.encode("00") == [405]
35 assert enc.encode("000") == [830]
36 assert enc.encode("0000") == [2388]
37 assert enc.encode("00000") == [20483]
38 assert enc.encode("000000") == [10535]
39 assert enc.encode("0000000") == [24598]
40 assert enc.encode("00000000") == [8269]
41 assert enc.encode("000000000") == [10535, 830]
42 assert enc.encode("0000000000") == [8269, 405]
43 assert enc.encode("00000000000") == [8269, 830]
44 assert enc.encode("000000000000") == [8269, 2388]
45 assert enc.encode("0000000000000") == [8269, 20483]
46 assert enc.encode("00000000000000") == [8269, 10535]
47 assert enc.encode("000000000000000") == [8269, 24598]
48 assert enc.encode("0000000000000000") == [25645]
49 assert enc.encode("00000000000000000") == [8269, 10535, 830]
50
51
52def test_large_repeated():
53 enc = tiktoken.get_encoding("o200k_base")
54
55 with pytest.raises(ValueError):
56 enc.encode("x" * 1_000_000)
57
58
59def test_simple_regex():
60 enc = tiktoken.get_encoding("cl100k_base")
61 assert enc.encode("rer") == [38149]
62 assert enc.encode("'rer") == [2351, 81]
63 assert enc.encode("today\n ") == [31213, 198, 220]
64 assert enc.encode("today\n \n") == [31213, 27907]
65 assert enc.encode("today\n \n") == [31213, 14211]
66
67
68def test_basic_encode():
69 enc = tiktoken.get_encoding("r50k_base")
70 assert enc.encode("hello world") == [31373, 995]
71
72 enc = tiktoken.get_encoding("p50k_base")
73 assert enc.encode("hello world") == [31373, 995]
74
75 enc = tiktoken.get_encoding("cl100k_base")
76 assert enc.encode("hello world") == [15339, 1917]
77 assert enc.encode(" \x850") == [220, 126, 227, 15]
78
79
80def test_encode_empty():
81 enc = tiktoken.get_encoding("r50k_base")
82 assert enc.encode("") == []
83
84
85def test_encode_bytes():
86 enc = tiktoken.get_encoding("cl100k_base")
87 assert enc._encode_bytes(b" \xec\x8b\xa4\xed") == [62085]
88 for i in range(10):
89 bytestring = b"\x80" * i
90 assert enc.decode_bytes(enc._encode_bytes(bytestring)) == bytestring
91
92
93@pytest.mark.parametrize("make_enc", ENCODING_FACTORIES)
94@hypothesis.given(bytestring=st.binary())
95@hypothesis.settings(deadline=None, max_examples=MAX_EXAMPLES)
96def test_hyp_encode_bytes(make_enc: Callable[[], tiktoken.Encoding], bytestring: bytes):
97 enc = make_enc()
98 assert enc.decode_bytes(enc._encode_bytes(bytestring)) == bytestring
99
100
101def test_encode_surrogate_pairs():
102 enc = tiktoken.get_encoding("cl100k_base")
103
104 assert enc.encode("👍") == [9468, 239, 235]
105 # surrogate pair gets converted to codepoint
106 assert enc.encode("\ud83d\udc4d") == [9468, 239, 235]
107
108 # lone surrogate just gets replaced
109 assert enc.encode("\ud83d") == enc.encode("�")
110
111
112@pytest.mark.parametrize("make_enc", ENCODING_FACTORIES)
113def test_catastrophically_repetitive(make_enc: Callable[[], tiktoken.Encoding]):
114 enc = make_enc()
115 for c in ["^", "0", "a", "'s", " ", "\n"]:
116 big_value = c * 10_000
117 assert big_value == enc.decode(enc.encode(big_value))
118
119 big_value = " " + big_value
120 assert big_value == enc.decode(enc.encode(big_value))
121
122 big_value = big_value + "\n"
123 assert big_value == enc.decode(enc.encode(big_value))
124
125
126# ====================
127# Roundtrip
128# ====================
129
130
131@pytest.mark.parametrize("make_enc", ENCODING_FACTORIES)
132def test_basic_roundtrip(make_enc):
133 enc = make_enc()
134 for value in (
135 "hello",
136 "hello ",
137 "hello ",
138 " hello",
139 " hello ",
140 " hello ",
141 "hello world",
142 "请考试我的软件!12345",
143 ):
144 assert value == enc.decode(enc.encode(value))
145 assert value == enc.decode(enc.encode_ordinary(value))
146
147
148@pytest.mark.parametrize("make_enc", ENCODING_FACTORIES)
149@hypothesis.given(text=st.text())
150@hypothesis.settings(deadline=None, max_examples=MAX_EXAMPLES)
151def test_hyp_roundtrip(make_enc: Callable[[], tiktoken.Encoding], text):
152 enc = make_enc()
153
154 assert text == enc.decode(enc.encode(text))
155
156
157@pytest.mark.parametrize("make_enc", ENCODING_FACTORIES)
158def test_single_token_roundtrip(make_enc: Callable[[], tiktoken.Encoding]):
159 enc = make_enc()
160
161 for token in range(enc.n_vocab):
162 try:
163 token_bytes = enc.decode_single_token_bytes(token)
164 except KeyError:
165 continue
166 assert enc.encode_single_token(token_bytes) == token
167
168
169# ====================
170# Special tokens
171# ====================
172
173
174def test_special_token():
175 enc = tiktoken.get_encoding("cl100k_base")
176
177 eot = enc.encode_single_token("<|endoftext|>")
178 assert eot == enc.eot_token
179 fip = enc.encode_single_token("<|fim_prefix|>")
180 fim = enc.encode_single_token("<|fim_middle|>")
181
182 text = "<|endoftext|> hello <|fim_prefix|>"
183 assert eot not in enc.encode(text, disallowed_special=())
184 with pytest.raises(ValueError):
185 enc.encode(text)
186 with pytest.raises(ValueError):
187 enc.encode(text, disallowed_special="all")
188 with pytest.raises(ValueError):
189 enc.encode(text, disallowed_special={"<|endoftext|>"})
190 with pytest.raises(ValueError):
191 enc.encode(text, disallowed_special={"<|fim_prefix|>"})
192
193 text = "<|endoftext|> hello <|fim_prefix|> there <|fim_middle|>"
194 tokens = enc.encode(text, disallowed_special=())
195 assert eot not in tokens
196 assert fip not in tokens
197 assert fim not in tokens
198
199 tokens = enc.encode(text, allowed_special="all", disallowed_special=())
200 assert eot in tokens
201 assert fip in tokens
202 assert fim in tokens
203
204 tokens = enc.encode(text, allowed_special="all", disallowed_special="all")
205 assert eot in tokens
206 assert fip in tokens
207 assert fim in tokens
208
209 tokens = enc.encode(text, allowed_special={"<|fim_prefix|>"}, disallowed_special=())
210 assert eot not in tokens
211 assert fip in tokens
212 assert fim not in tokens
213
214 tokens = enc.encode(text, allowed_special={"<|endoftext|>"}, disallowed_special=())
215 assert eot in tokens
216 assert fip not in tokens
217 assert fim not in tokens
218
219 tokens = enc.encode(text, allowed_special={"<|fim_middle|>"}, disallowed_special=())
220 assert eot not in tokens
221 assert fip not in tokens
222 assert fim in tokens
223
224
225@pytest.mark.parametrize("make_enc", ENCODING_FACTORIES)
226@hypothesis.given(text=st.text())
227@hypothesis.settings(deadline=None, max_examples=MAX_EXAMPLES)
228def test_hyp_special_ordinary(make_enc, text: str):
229 enc = make_enc()
230 assert enc.encode_ordinary(text) == enc.encode(text, disallowed_special=())
231
232
233# ====================
234# Batch encoding
235# ====================
236
237
238@pytest.mark.parametrize("make_enc", ENCODING_FACTORIES)
239def test_batch_encode(make_enc: Callable[[], tiktoken.Encoding]):
240 enc = make_enc()
241 text1 = "hello world"
242 text2 = "goodbye world"
243
244 assert enc.encode_batch([text1]) == [enc.encode(text1)]
245 assert enc.encode_batch([text1, text2]) == [enc.encode(text1), enc.encode(text2)]
246
247 assert enc.encode_ordinary_batch([text1]) == [enc.encode_ordinary(text1)]
248 assert enc.encode_ordinary_batch([text1, text2]) == [
249 enc.encode_ordinary(text1),
250 enc.encode_ordinary(text2),
251 ]
252
253
254@pytest.mark.parametrize("make_enc", ENCODING_FACTORIES)
255@hypothesis.given(batch=st.lists(st.text()))
256@hypothesis.settings(deadline=None, max_examples=MAX_EXAMPLES)
257def test_hyp_batch_roundtrip(make_enc: Callable[[], tiktoken.Encoding], batch):
258 enc = make_enc()
259
260 encoded = enc.encode_batch(batch, allowed_special="all")
261 assert encoded == [enc.encode(t, allowed_special="all") for t in batch]
262 decoded = enc.decode_batch(encoded)
263 assert decoded == batch
264