-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
333 additions
and
205 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
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
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
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,57 @@ | ||
import os | ||
from typing import Any | ||
|
||
from composer.utils import dist | ||
from transformers import AutoTokenizer, PreTrainedTokenizerBase | ||
|
||
from llmfoundry.tokenizers.tiktoken import TiktokenTokenizerWrapper | ||
|
||
|
||
def build_tokenizer( | ||
tokenizer_name: str, | ||
tokenizer_kwargs: dict[str, Any], | ||
) -> PreTrainedTokenizerBase: | ||
os.environ['TRANSFORMERS_NO_ADVISORY_WARNINGS'] = '1' | ||
os.environ['TOKENIZERS_PARALLELISM'] = 'false' | ||
|
||
signal_file_path = f'.node_{dist.get_node_rank()}_local_rank0_completed_tokenizer_setup' | ||
|
||
if dist.is_available() and dist.is_initialized() and dist.get_world_size( | ||
) > 1: | ||
# Make sure the tokenizer files are downloaded and cached first by local rank 0 | ||
with dist.local_rank_zero_download_and_wait(signal_file_path): | ||
pass | ||
|
||
if tokenizer_name.startswith('tiktoken'): | ||
tokenizer = TiktokenTokenizerWrapper(**tokenizer_kwargs) | ||
else: | ||
tokenizer = AutoTokenizer.from_pretrained( | ||
tokenizer_name, | ||
**tokenizer_kwargs, | ||
) | ||
|
||
# HuggingFace does not respect the model_max_length kwarg, and overrides it with | ||
# min(kwargs['model_max_length'], original_config['model_max_length']), so we | ||
# explicitly set it here | ||
tokenizer.model_max_length = tokenizer_kwargs.get( | ||
'model_max_length', | ||
int(1e30), | ||
) | ||
|
||
if not hasattr(tokenizer, 'eos_token') or tokenizer.eos_token is None: | ||
raise ValueError( | ||
f'The tokenizer {tokenizer_name} must have an eos_token.', | ||
) | ||
|
||
if dist.is_available() and dist.is_initialized() and dist.get_world_size( | ||
) > 1: | ||
if dist.get_local_rank() == 0: | ||
with open(signal_file_path, 'wb') as f: | ||
f.write(b'local_rank0_completed_tokenizer_setup') | ||
|
||
dist.barrier() | ||
|
||
if dist.get_local_rank() == 0: | ||
os.remove(signal_file_path) | ||
|
||
return tokenizer |
Oops, something went wrong.