This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(decoder): add unit tests for decoder
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Test stateful tokenizer for stream decoding. | ||
""" | ||
from transformers import AutoTokenizer | ||
|
||
from basaran.decoder import StreamDecoder | ||
|
||
|
||
class TestDecoder: | ||
"""Test stateful tokenizer for stream decoding.""" | ||
|
||
def test_byte_pair_encoding(self): | ||
"""Test with Byte-Pair-Encoding.""" | ||
tokenizer = AutoTokenizer.from_pretrained( | ||
pretrained_model_name_or_path="./tests/data/tiny-random-bloom", | ||
local_files_only=True, | ||
) | ||
decoder = StreamDecoder(tokenizer) | ||
|
||
expected = "hello world ABC \n 你好" | ||
tokens = tokenizer.encode(expected) | ||
|
||
actual = "" | ||
for token in tokens: | ||
actual += decoder.decode(token) | ||
|
||
assert actual == expected | ||
assert decoder.end == len(expected) | ||
|
||
def test_sentence_piece(self): | ||
"""Test with SentencePiece.""" | ||
tokenizer = AutoTokenizer.from_pretrained( | ||
pretrained_model_name_or_path="./tests/data/tiny-random-t5", | ||
local_files_only=True, | ||
) | ||
decoder = StreamDecoder(tokenizer) | ||
|
||
expected = "hello world ABC" | ||
tokens = tokenizer.encode(expected) | ||
|
||
actual = "" | ||
for token in tokens: | ||
actual += decoder.decode(token) | ||
|
||
assert actual == expected | ||
assert decoder.end == len(expected) |