-
Notifications
You must be signed in to change notification settings - Fork 743
Python hugging face tokenizer #8354
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
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8cbe6cd
hf_tokenizer.py generated
jackzhxng 88b3394
Add hugging face tokenizer
jackzhxng 70fd1fe
Qwen runs with HF tokenizer
jackzhxng 5834d14
Fix encode, remove generated python tokenizer
jackzhxng 421288b
Comment / lint
jackzhxng e3352fa
Move into class
jackzhxng 01ed13c
Lint
jackzhxng b0990da
Mengwei pr rev
jackzhxng 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
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,56 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import json | ||
| import os | ||
| from typing import List, Optional | ||
|
|
||
| from tokenizers import Tokenizer | ||
|
|
||
|
|
||
| class HuggingFaceTokenizer: | ||
| """ | ||
| Tokenizing and encoding/decoding text using the Hugging face tokenizer. | ||
| """ | ||
|
|
||
| def __init__(self, model_path: str, config_path: Optional[str] = None): | ||
| """ | ||
| Initializes the Tokenizer with a tokenizer.json from HuggingFace. | ||
|
|
||
| Args: | ||
| model_path (str): The path to the Tiktoken model file. | ||
| """ | ||
| assert os.path.isfile(model_path), model_path | ||
|
|
||
| self.model = tokenizer = Tokenizer.from_file(model_path) | ||
|
|
||
| self.n_words: int = tokenizer.get_vocab_size() | ||
| if config_path: | ||
| with open(config_path) as f: | ||
| tokenizer_config = json.load(f) | ||
| self.bos_id = ( | ||
| self.model.token_to_id(tokenizer_config["bos_token"]) | ||
| if tokenizer_config["bos_token"] | ||
| else None | ||
| ) | ||
| self.eos_id = self.model.token_to_id(tokenizer_config["eos_token"]) | ||
| else: # Fallback guess. | ||
| self.bos_id = self.model.token_to_id("<|begin_of_text|>") | ||
| self.eos_id = self.model.token_to_id("<|endoftext|>") | ||
|
|
||
| self.stop_tokens = [ | ||
| self.eos_id, | ||
| ] | ||
|
|
||
| def encode(self, s: str, *, bos: bool, eos: bool) -> List[int]: | ||
| assert type(s) is str | ||
| return self.model.encode(s).ids | ||
|
|
||
| def decode(self, t: List[int]) -> str: | ||
| return self.model.decode(t) | ||
|
|
||
| def decode_token(self, t: int) -> str: | ||
| return self.model.decode([t]) |
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
Oops, something went wrong.
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.
Please add
helpto clarify how to use this argumentThere 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.
Also this should be mutually exclusive with tokenizer_path? We should make sure only one is passed in.
Uh oh!
There was an error while loading. Please reload this page.
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 tokenizer.json is always the source of truth, the config is just for metadata like this, e.g. it tells you what the eos_token is, which isn't info that is in the
tokenizer.jsonitselfThere 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.
Can you add this info to the help string?