-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Misc] Make cached tokenizer pickle-compatible #17048
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
Merged
DarkLight1337
merged 13 commits into
vllm-project:main
from
DarkLight1337:pickle-cached-tokenizer
Apr 27, 2025
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2614d63
[Misc] Make cached tokenizer pickle-compatible
DarkLight1337 aa38396
Use a list
DarkLight1337 b8f7598
Update type annotations
DarkLight1337 1b5393c
Fix chatglm tokenizer
DarkLight1337 1fcebb1
Rename
DarkLight1337 84769d2
Merge branch 'main' into pickle-cached-tokenizer
DarkLight1337 fbd688b
Update attribute access
DarkLight1337 385b4f4
Merge branch 'main' into pickle-cached-tokenizer
DarkLight1337 8e0c95d
Simplify the code
DarkLight1337 9aeeabc
Rename
DarkLight1337 3d2007c
Update
DarkLight1337 1239e1c
Use copy instead of deepcopy
DarkLight1337 af6b11e
Remove unnecessary parenthesis
DarkLight1337 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,43 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pickle | ||
from copy import deepcopy | ||
|
||
import pytest | ||
from transformers import AutoTokenizer | ||
|
||
from vllm.transformers_utils.tokenizer import get_cached_tokenizer | ||
from vllm.transformers_utils.tokenizer import (AnyTokenizer, | ||
get_cached_tokenizer) | ||
|
||
|
||
def test_cached_tokenizer(): | ||
reference_tokenizer = AutoTokenizer.from_pretrained("gpt2") | ||
@pytest.mark.parametrize("model_id", ["gpt2", "THUDM/chatglm3-6b"]) | ||
def test_cached_tokenizer(model_id: str): | ||
reference_tokenizer = AutoTokenizer.from_pretrained(model_id, | ||
trust_remote_code=True) | ||
reference_tokenizer.add_special_tokens({"cls_token": "<CLS>"}) | ||
reference_tokenizer.add_special_tokens( | ||
{"additional_special_tokens": ["<SEP>"]}) | ||
|
||
cached_tokenizer = get_cached_tokenizer(deepcopy(reference_tokenizer)) | ||
_check_consistency(cached_tokenizer, reference_tokenizer) | ||
|
||
pickled_tokenizer = pickle.dumps(cached_tokenizer) | ||
unpickled_tokenizer = pickle.loads(pickled_tokenizer) | ||
_check_consistency(unpickled_tokenizer, reference_tokenizer) | ||
|
||
|
||
def _check_consistency(target: AnyTokenizer, expected: AnyTokenizer): | ||
assert isinstance(target, type(expected)) | ||
|
||
# Cached attributes | ||
assert target.all_special_ids == expected.all_special_ids | ||
assert target.all_special_tokens == expected.all_special_tokens | ||
assert (target.all_special_tokens_extended == | ||
expected.all_special_tokens_extended) | ||
assert target.get_vocab() == expected.get_vocab() | ||
assert len(target) == len(expected) | ||
|
||
# Other attributes | ||
assert getattr(target, "padding_side", | ||
None) == getattr(expected, "padding_side", None) | ||
|
||
assert reference_tokenizer.encode("prompt") == cached_tokenizer.encode( | ||
"prompt") | ||
assert set(reference_tokenizer.all_special_ids) == set( | ||
cached_tokenizer.all_special_ids) | ||
assert set(reference_tokenizer.all_special_tokens) == set( | ||
cached_tokenizer.all_special_tokens) | ||
assert set(reference_tokenizer.all_special_tokens_extended) == set( | ||
cached_tokenizer.all_special_tokens_extended) | ||
assert target.encode("prompt") == expected.encode("prompt") |
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
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.
Uh oh!
There was an error while loading. Please reload this page.