MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT-LLM / load_hf_tokenizer

Function load_hf_tokenizer

tensorrt_llm/tokenizer/tokenizer.py:416–450  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

414
415
416def 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

Callers 4

launch_disaggregated_llmFunction · 0.90
load_hf_tokenizerMethod · 0.85
__init__Method · 0.85

Calls 3

from_pretrainedMethod · 0.45
warningMethod · 0.45