forked from karpathy/minbpe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ViswanathaReddyGajjala-master'
- Loading branch information
Showing
5 changed files
with
106 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import pytest | ||
import tiktoken | ||
import os | ||
|
||
from bpe_basic import BasicTokenizer | ||
from bpe_gpt4 import GPT4Tokenizer | ||
from bpe_regex import RegexTokenizer | ||
|
||
# a few strings to test the tokenizers on | ||
test_strings = [ | ||
"", # empty string | ||
"?", # single character | ||
"hello world!!!? (안녕하세요!) lol123 😉", # fun small string | ||
open("taylorswift.txt", "r", encoding="utf-8").read(), # big string | ||
] | ||
|
||
# test encode/decode identity for a few different strings | ||
@pytest.mark.parametrize("tokenizer_factory", [BasicTokenizer, RegexTokenizer, GPT4Tokenizer]) | ||
@pytest.mark.parametrize("text", test_strings) | ||
def test_encode_decode_identity(tokenizer_factory, text): | ||
tokenizer = tokenizer_factory() | ||
ids = tokenizer.encode(text) | ||
decoded = tokenizer.decode(ids) | ||
assert text == decoded | ||
|
||
# test that our tokenizer matches the official GPT-4 tokenizer | ||
@pytest.mark.parametrize("text", test_strings) | ||
def test_gpt4_tiktoken_equality(text): | ||
tokenizer = GPT4Tokenizer() | ||
enc = tiktoken.get_encoding("cl100k_base") | ||
tiktoken_ids = enc.encode(text) | ||
gpt4_tokenizer_ids = tokenizer.encode(text) | ||
assert gpt4_tokenizer_ids == tiktoken_ids | ||
|
||
# reference test to add more tests in the future | ||
@pytest.mark.parametrize("tokenizer_factory", [BasicTokenizer, RegexTokenizer]) | ||
def test_wikipedia_example(tokenizer_factory): | ||
""" | ||
Quick unit test, following along the Wikipedia example: | ||
https://en.wikipedia.org/wiki/Byte_pair_encoding | ||
According to Wikipedia, running bpe on the the input string: | ||
"aaabdaaabac" | ||
for 3 merges will result in string: | ||
"XdXac" | ||
where: | ||
X=ZY | ||
Y=ab | ||
Z=aa | ||
Keep in mind that for us a=97, b=98, c=99, d=100 (ASCII values) | ||
so Z will be 256, Y will be 257, X will be 258. | ||
So we expect the output list of ids to be [258, 100, 258, 97, 99] | ||
""" | ||
tokenizer = tokenizer_factory() | ||
text = "aaabdaaabac" | ||
tokenizer.train(text, 256 + 3) | ||
ids = tokenizer.encode(text) | ||
assert ids == [258, 100, 258, 97, 99] | ||
assert tokenizer.decode(tokenizer.encode(text)) == text | ||
|
||
def test_save_load(): | ||
# take a bit more complex piece of text and train the tokenizer, chosen at random | ||
text = """ | ||
The llama (/ˈlɑːmə/; Spanish pronunciation: [ˈʎama] or [ˈʝama]) (Lama glama) is a domesticated South American camelid, widely used as a meat and pack animal by Andean cultures since the pre-Columbian era. | ||
Llamas are social animals and live with others as a herd. Their wool is soft and contains only a small amount of lanolin.[2] Llamas can learn simple tasks after a few repetitions. When using a pack, they can carry about 25 to 30% of their body weight for 8 to 13 km (5–8 miles).[3] The name llama (in the past also spelled "lama" or "glama") was adopted by European settlers from native Peruvians.[4] | ||
The ancestors of llamas are thought to have originated from the Great Plains of North America about 40 million years ago, and subsequently migrated to South America about three million years ago during the Great American Interchange. By the end of the last ice age (10,000–12,000 years ago), camelids were extinct in North America.[3] As of 2007, there were over seven million llamas and alpacas in South America and over 158,000 llamas and 100,000 alpacas, descended from progenitors imported late in the 20th century, in the United States and Canada.[5] | ||
In Aymara mythology, llamas are important beings. The Heavenly Llama is said to drink water from the ocean and urinates as it rains.[6] According to Aymara eschatology, llamas will return to the water springs and ponds where they come from at the end of time.[6] | ||
""".strip() | ||
|
||
tokenizer = RegexTokenizer() | ||
|
||
# do 64 merges | ||
tokenizer.train(text, 256 + 64) | ||
|
||
# verify that decode(encode(x)) == x | ||
assert tokenizer.decode(tokenizer.encode(text)) == text | ||
|
||
# verify that save/load work as expected | ||
ids = tokenizer.encode(text) | ||
|
||
# save the tokenizer | ||
tokenizer.save("test_tokenizer_tmp") | ||
|
||
# re-load the tokenizer | ||
tokenizer = RegexTokenizer() | ||
tokenizer.load("test_tokenizer_tmp.model") | ||
|
||
# verify that decode(encode(x)) == x | ||
assert tokenizer.decode(ids) == text | ||
assert tokenizer.decode(tokenizer.encode(text)) == text | ||
assert tokenizer.encode(text) == ids | ||
|
||
# delete the temporary files | ||
for file in ["test_tokenizer_tmp.model", "test_tokenizer_tmp.vocab"]: | ||
os.remove(file) | ||
|
||
if __name__ == "__main__": | ||
pytest.main() |