-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Test/tokenizer unit tests #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ansschh
wants to merge
3
commits into
openai:main
Choose a base branch
from
ansschh:test/tokenizer-unit-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,212 @@ | ||
"""Unit tests for tokenizer encoding/decoding functionality.""" | ||
|
||
import pytest | ||
from gpt_oss.tokenizer import get_tokenizer | ||
|
||
|
||
class TestTokenizerBasics: | ||
"""Test basic tokenizer functionality.""" | ||
|
||
def test_get_tokenizer_returns_encoding(self): | ||
"""Test that get_tokenizer returns a valid encoding.""" | ||
tokenizer = get_tokenizer() | ||
assert tokenizer is not None | ||
assert tokenizer.name == "o200k_harmony" | ||
|
||
def test_tokenizer_has_harmony_special_tokens(self): | ||
"""Test that tokenizer includes Harmony special tokens.""" | ||
tokenizer = get_tokenizer() | ||
special_tokens = tokenizer._special_tokens | ||
|
||
# Verify key Harmony tokens are present | ||
assert "<|channel|>" in special_tokens | ||
assert special_tokens["<|channel|>"] == 200005 | ||
assert "<|start|>" in special_tokens | ||
assert special_tokens["<|start|>"] == 200006 | ||
assert "<|end|>" in special_tokens | ||
assert special_tokens["<|end|>"] == 200007 | ||
assert "<|message|>" in special_tokens | ||
assert special_tokens["<|message|>"] == 200008 | ||
assert "<|call|>" in special_tokens | ||
assert special_tokens["<|call|>"] == 200012 | ||
assert "<|return|>" in special_tokens | ||
assert special_tokens["<|return|>"] == 200002 | ||
|
||
def test_tokenizer_has_reserved_tokens(self): | ||
"""Test that tokenizer includes reserved token range.""" | ||
tokenizer = get_tokenizer() | ||
special_tokens = tokenizer._special_tokens | ||
|
||
# Check reserved tokens exist in range | ||
assert "<|reserved_200013|>" in special_tokens | ||
assert special_tokens["<|reserved_200013|>"] == 200013 | ||
assert "<|reserved_201087|>" in special_tokens | ||
assert special_tokens["<|reserved_201087|>"] == 201087 | ||
|
||
|
||
class TestTokenizerEncoding: | ||
"""Test tokenizer encoding functionality.""" | ||
|
||
def test_encode_simple_text(self): | ||
"""Test encoding simple text.""" | ||
tokenizer = get_tokenizer() | ||
text = "Hello, world!" | ||
tokens = tokenizer.encode(text) | ||
|
||
assert isinstance(tokens, list) | ||
assert len(tokens) > 0 | ||
assert all(isinstance(t, int) for t in tokens) | ||
|
||
def test_encode_special_tokens(self): | ||
"""Test encoding text with special tokens.""" | ||
tokenizer = get_tokenizer() | ||
text = "<|channel|>final<|message|>Hello<|return|>" | ||
tokens = tokenizer.encode(text, allowed_special="all") | ||
|
||
assert 200005 in tokens # <|channel|> | ||
assert 200008 in tokens # <|message|> | ||
assert 200002 in tokens # <|return|> | ||
|
||
def test_encode_without_special_allowed_raises(self): | ||
"""Test that encoding special tokens without permission raises error.""" | ||
tokenizer = get_tokenizer() | ||
text = "<|channel|>test" | ||
|
||
with pytest.raises(ValueError): | ||
tokenizer.encode(text) | ||
|
||
def test_encode_empty_string(self): | ||
"""Test encoding empty string.""" | ||
tokenizer = get_tokenizer() | ||
tokens = tokenizer.encode("") | ||
|
||
assert isinstance(tokens, list) | ||
assert len(tokens) == 0 | ||
|
||
def test_encode_unicode_text(self): | ||
"""Test encoding unicode text.""" | ||
tokenizer = get_tokenizer() | ||
text = "Hello 世界 🌍" | ||
tokens = tokenizer.encode(text) | ||
|
||
assert isinstance(tokens, list) | ||
assert len(tokens) > 0 | ||
|
||
|
||
class TestTokenizerDecoding: | ||
"""Test tokenizer decoding functionality.""" | ||
|
||
def test_decode_simple_tokens(self): | ||
"""Test decoding simple tokens.""" | ||
tokenizer = get_tokenizer() | ||
text = "Hello, world!" | ||
tokens = tokenizer.encode(text) | ||
decoded = tokenizer.decode(tokens) | ||
|
||
assert decoded == text | ||
|
||
def test_decode_with_special_tokens(self): | ||
"""Test decoding tokens including special tokens.""" | ||
tokenizer = get_tokenizer() | ||
text = "<|channel|>final<|message|>Hello<|return|>" | ||
tokens = tokenizer.encode(text, allowed_special="all") | ||
decoded = tokenizer.decode(tokens) | ||
|
||
assert decoded == text | ||
|
||
def test_decode_empty_list(self): | ||
"""Test decoding empty token list.""" | ||
tokenizer = get_tokenizer() | ||
decoded = tokenizer.decode([]) | ||
|
||
assert decoded == "" | ||
|
||
def test_decode_single_token(self): | ||
"""Test decoding single token.""" | ||
tokenizer = get_tokenizer() | ||
tokens = tokenizer.encode("a") | ||
decoded = tokenizer.decode(tokens) | ||
|
||
assert decoded == "a" | ||
|
||
def test_decode_unicode(self): | ||
"""Test decoding unicode tokens.""" | ||
tokenizer = get_tokenizer() | ||
text = "Hello 世界 🌍" | ||
tokens = tokenizer.encode(text) | ||
decoded = tokenizer.decode(tokens) | ||
|
||
assert decoded == text | ||
|
||
|
||
class TestTokenizerRoundTrip: | ||
"""Test encode/decode round-trip consistency.""" | ||
|
||
@pytest.mark.parametrize("text", [ | ||
"Simple text", | ||
"Text with numbers: 123456", | ||
"Special chars: !@#$%^&*()", | ||
"Unicode: 你好世界", | ||
"Emoji: 🚀🌟💡", | ||
"Mixed: Hello 世界 123 🎉", | ||
"Newlines:\nand\ttabs", | ||
]) | ||
def test_roundtrip_consistency(self, text): | ||
"""Test that encode->decode returns original text.""" | ||
tokenizer = get_tokenizer() | ||
tokens = tokenizer.encode(text) | ||
decoded = tokenizer.decode(tokens) | ||
|
||
assert decoded == text | ||
|
||
def test_roundtrip_with_harmony_format(self): | ||
"""Test round-trip with Harmony message format.""" | ||
tokenizer = get_tokenizer() | ||
text = "<|channel|>analysis<|start|><|message|>Thinking...<|end|><|channel|>final<|message|>Answer<|return|>" | ||
tokens = tokenizer.encode(text, allowed_special="all") | ||
decoded = tokenizer.decode(tokens) | ||
|
||
assert decoded == text | ||
|
||
|
||
class TestTokenizerEdgeCases: | ||
"""Test edge cases and error handling.""" | ||
|
||
def test_encode_very_long_text(self): | ||
"""Test encoding very long text.""" | ||
tokenizer = get_tokenizer() | ||
text = "a" * 10000 | ||
tokens = tokenizer.encode(text) | ||
|
||
assert isinstance(tokens, list) | ||
assert len(tokens) > 0 | ||
|
||
def test_decode_invalid_token_ids(self): | ||
"""Test decoding with potentially invalid token IDs.""" | ||
tokenizer = get_tokenizer() | ||
# Use valid token IDs from the special tokens | ||
tokens = [200005, 200006, 200007] | ||
decoded = tokenizer.decode(tokens) | ||
|
||
assert isinstance(decoded, str) | ||
|
||
def test_multiple_tokenizer_instances_consistent(self): | ||
"""Test that multiple tokenizer instances behave consistently.""" | ||
tokenizer1 = get_tokenizer() | ||
tokenizer2 = get_tokenizer() | ||
|
||
text = "Test consistency" | ||
tokens1 = tokenizer1.encode(text) | ||
tokens2 = tokenizer2.encode(text) | ||
|
||
assert tokens1 == tokens2 | ||
|
||
def test_special_token_ids_immutable(self): | ||
"""Test that special token IDs are consistent.""" | ||
tokenizer = get_tokenizer() | ||
|
||
# Get special tokens multiple times | ||
channel_id_1 = tokenizer.encode("<|channel|>", allowed_special="all")[0] | ||
channel_id_2 = tokenizer.encode("<|channel|>", allowed_special="all")[0] | ||
|
||
assert channel_id_1 == channel_id_2 == 200005 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new preflight check unconditionally wraps
args.checkpoint
inPath
and raisesFileNotFoundError
when the path does not exist. This happens before the backend switch, so it also triggers when using the vLLM backend, which previously accepted HuggingFace model identifiers or other non-local URIs and downloaded weights on demand. With the change, any string that is not an existing filesystem path now fails before vLLM can handle it, breaking valid use cases such as--backend vllm --checkpoint mistralai/Mixtral-8x7B
. Consider moving the existence check inside the torch/triton branches or restricting it to local backends only.Useful? React with 👍 / 👎.