This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
Simplify TokenizerArgs.__post_init__ with Enum Tokenizer Type
#1535
Merged
+29
−27
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
98eaf8f
Simplify `TokenizerArgs.__post_init__` with Enum Tokenizer Type
63be93a
Simplify `TokenizerArgs.__post_init__` with Enum Tokenizer Type
379c07b
Add check no tokenizer
zhenyan-zhang-meta b896262
Rollback to 98eaf8f
zhenyan-zhang-meta c846de9
Add No Tokenizer Checker
zhenyan-zhang-meta c752a40
Reply to nits
zhenyan-zhang-meta 03e2019
Reply to nits
zhenyan-zhang-meta 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import os | ||
| import sys | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from pathlib import Path | ||
| from typing import Any, Dict, Optional, Tuple, Union | ||
|
|
||
|
|
@@ -237,23 +238,24 @@ def from_speculative_args(cls, args: argparse.Namespace) -> "BuilderArgs": | |
| speculative_builder_args.pte_path = None | ||
| return speculative_builder_args | ||
|
|
||
| class TokenizerType(Enum): | ||
| NONE = 0 | ||
| TIKTOKEN = 1 | ||
| SENTENCEPIECE = 2 | ||
| HF_TOKENIZER = 3 | ||
|
|
||
| @dataclass | ||
| class TokenizerArgs: | ||
| tokenizer_path: Optional[Union[Path, str]] = None | ||
| is_sentencepiece: bool = False | ||
| is_tiktoken: bool = False | ||
| is_hf_tokenizer: bool = False | ||
| tokenizer_type: TokenizerType = TokenizerType.NONE | ||
| t: Optional[Any] = None | ||
|
|
||
| def __post_init__(self): | ||
| try: | ||
| from tokenizer.tiktoken import Tokenizer as TiktokenTokenizer | ||
|
|
||
| self.t = TiktokenTokenizer(model_path=str(self.tokenizer_path)) | ||
| self.is_tiktoken = True | ||
| self.is_sentencepiece = False | ||
| self.is_hf_tokenizer = False | ||
| self.tokenizer_type = TokenizerType.TIKTOKEN | ||
| return | ||
| except: | ||
| pass | ||
|
|
@@ -262,9 +264,7 @@ def __post_init__(self): | |
| from sentencepiece import SentencePieceProcessor | ||
|
|
||
| self.t = SentencePieceProcessor(model_file=str(self.tokenizer_path)) | ||
| self.is_tiktoken = False | ||
| self.is_sentencepiece = True | ||
| self.is_hf_tokenizer = False | ||
| self.tokenizer_type = TokenizerType.SENTENCEPIECE | ||
| return | ||
| except: | ||
| pass | ||
|
|
@@ -273,19 +273,24 @@ def __post_init__(self): | |
| from tokenizer.hf_tokenizer import HFTokenizer | ||
|
|
||
| self.t = HFTokenizer(str(self.tokenizer_path)) | ||
| self.is_tiktoken = False | ||
| self.is_sentencepiece = False | ||
| self.is_hf_tokenizer = True | ||
| self.tokenizer_type = TokenizerType.HF_TOKENIZER | ||
| return | ||
| except: | ||
| pass | ||
|
|
||
| self.is_tiktoken = False | ||
| self.is_sentencepiece = False | ||
| self.is_hf_tokenizer = False | ||
| self.tokenizer_type = TokenizerType.NONE | ||
| self.t = None | ||
| return | ||
|
||
|
|
||
| def is_tiktoken(self) -> bool: | ||
| return self.tokenizer_type == TokenizerType.TIKTOKEN | ||
|
|
||
| def is_sentencepiece(self) -> bool: | ||
| return self.tokenizer_type == TokenizerType.SENTENCEPIECE | ||
|
|
||
| def is_hf_tokenizer(self) -> bool: | ||
| return self.tokenizer_type == TokenizerType.HF_TOKENIZER | ||
|
|
||
| def validate_model( | ||
| self, | ||
| model: Optional[Model], | ||
|
|
@@ -294,12 +299,14 @@ def validate_model( | |
| if model is None: | ||
| return | ||
|
|
||
| if sum([self.is_tiktoken, self.is_hf_tokenizer, self.is_sentencepiece]) != 1: | ||
|
|
||
| is_tiktoken = self.is_tiktoken() | ||
| is_sentencepiece = self.is_sentencepiece() | ||
| is_hf_tokenizer = self.is_hf_tokenizer() | ||
|
|
||
| if sum([is_tiktoken, is_hf_tokenizer, is_sentencepiece]) != 1: | ||
|
||
| raise RuntimeError(f"no tokenizer was found at {self.tokenizer_path}") | ||
|
|
||
| is_tiktoken = self.is_tiktoken | ||
| is_sentencepiece = self.is_sentencepiece | ||
| is_hf_tokenizer = self.is_hf_tokenizer | ||
| use_tiktoken = model.config.use_tiktoken | ||
| use_hf_tokenizer = model.config.use_hf_tokenizer | ||
| use_sentencepiece = not (use_tiktoken or use_hf_tokenizer) | ||
|
|
@@ -651,13 +658,13 @@ def do_nothing(max_batch_size, max_seq_length): | |
| model = torch.load(builder_args.snapshot_path, weights_only=False) | ||
| except Exception: | ||
| raise RuntimeError(f"Failed to load torchchat snapshot {builder_args.snapshot_path}") | ||
| # _active_backend() does not allow DSO & AOTI to be true. | ||
| # _active_backend() does not allow DSO & AOTI to be true. | ||
| # Choose either. | ||
| from torchchat.utils.build_utils import set_backend | ||
| set_backend (dso=True, pte=False, aoti_package=False) | ||
| if (model.config != config): | ||
| raise RuntimeError("loaded model architecture mismatch") | ||
| ## | ||
| ## | ||
| ## import all libraries with custom kernels ans custom operators | ||
| ## that quantize may be pulling in | ||
| ## | ||
|
|
@@ -792,4 +799,4 @@ def tokenizer_setting_to_name(tiktoken: bool, tokenizers: bool) -> str: | |
| return "TikToken" | ||
| if tokenizers: | ||
| return "Tokenizers" | ||
| return "SentencePiece" | ||
| return "SentencePiece" | ||
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.
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.
Do we really have to set these as none again since we already set them at the very top.
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.
We can actually drop all the logic here after the HF tokenizer check, tokenizer_type and .t are already set to these by default