Load a tokenizer from a Hugging Face model directory. Args: model_dir (str): The model directory. trust_remote_code (bool): Whether to trust the remote code. use_fast (bool): Whether to use the fast tokenizer. Returns: A TransformersTokenizer object if the
(model_dir: str,
trust_remote_code: bool = True,
use_fast: bool = True,
**kwargs)
| 414 | |
| 415 | |
| 416 | def load_hf_tokenizer(model_dir: str, |
| 417 | trust_remote_code: bool = True, |
| 418 | use_fast: bool = True, |
| 419 | **kwargs) -> Optional[TransformersTokenizer]: |
| 420 | ''' Load a tokenizer from a Hugging Face model directory. |
| 421 | |
| 422 | Args: |
| 423 | model_dir (str): The model directory. |
| 424 | trust_remote_code (bool): Whether to trust the remote code. |
| 425 | use_fast (bool): Whether to use the fast tokenizer. |
| 426 | |
| 427 | Returns: |
| 428 | A TransformersTokenizer object if the tokenizer is loaded successfully. |
| 429 | ''' |
| 430 | |
| 431 | try: |
| 432 | tokenizer = TransformersTokenizer.from_pretrained( |
| 433 | model_dir, |
| 434 | legacy=False, |
| 435 | padding_side='left', |
| 436 | truncation_side='left', |
| 437 | trust_remote_code=trust_remote_code, |
| 438 | use_fast=use_fast, |
| 439 | **kwargs) |
| 440 | |
| 441 | if trust_remote_code: |
| 442 | maybe_register_transformers_modules_by_value() |
| 443 | |
| 444 | return tokenizer |
| 445 | |
| 446 | except Exception as e: |
| 447 | logger.warning( |
| 448 | f"Failed to load hf tokenizer from {model_dir}, encounter error: {e}" |
| 449 | ) |
| 450 | return None |