TokenizerBase
TheTokenizerBase class defines the tokenizer interface used by TensorRT-LLM. It extends the Hugging Face PreTrainedTokenizerBase protocol.
Overview
TensorRT-LLM uses tokenizers to convert text to token IDs (encoding) and token IDs back to text (decoding). The default implementation wraps Hugging Face transformers tokenizers.Loading Tokenizers
Automatic Loading
Tokenizers are automatically loaded when creating anLLM instance:
Skip Tokenizer Initialization
If you plan to work with token IDs directly:Using a Pre-loaded Tokenizer
Core Methods
encode()
Convert text to token IDs.Parameters
str
required
Input text to encode.
bool
default:"True"
Whether to add special tokens (BOS, EOS) during encoding.
Returns
List[int]
List of token IDs.
decode()
Convert token IDs back to text.Parameters
List[int]
required
Token IDs to decode.
bool
default:"False"
Whether to remove special tokens (BOS, EOS, PAD) from output.
bool
default:"True"
Whether to add spaces between special tokens in output.
Returns
str
Decoded text.
batch_encode_plus()
Encode multiple texts in a batch.Parameters
List[str]
required
List of texts to encode.
Returns
dict
Dictionary containing:
input_ids: Token IDsattention_mask: Attention mask- Other tokenizer-specific outputs
decode_incrementally()
Incremental decoding for streaming generation. This method is optimized for streaming scenarios where tokens are generated one at a time.Parameters
List[int]
required
Incremental token IDs to decode.
str
default:"None"
Previously decoded text.
None for first iteration.dict
default:"None"
Internal decoding state from previous iteration.
None for first iteration.bool
default:"False"
Force flush pending tokens to output.
bool
default:"False"
Whether to skip special tokens in output.
bool
default:"True"
Whether to add spaces between special tokens.
int
default:"1"
Iteration interval for streaming updates.
Returns
Tuple[str, dict]
Tuple of:
text: Current decoded textstates: Updated decoding state (pass to next iteration)
Properties
int
End-of-sequence token ID.
int
Padding token ID.
str
Model name or path the tokenizer was loaded from.
bool
Whether this is a fast (Rust-based) tokenizer.
Chat Templates
apply_chat_template()
Format a conversation using the model’s chat template.Parameters
List[Dict[str, str]]
required
List of message dictionaries with
"role" and "content" keys.bool
default:"True"
If
True, return token IDs. If False, return formatted string.bool
default:"False"
Add prompt for the next assistant message.
Returns
str | List[int]
Formatted prompt as string or token IDs (depending on
tokenize parameter).Custom Tokenizers
You can implement custom tokenizers by inheriting fromTokenizerBase:
TransformersTokenizer
The default tokenizer implementation that wraps Hugging Face transformers tokenizers:Utility Functions
load_hf_tokenizer()
Load a Hugging Face tokenizer directly:Environment Variables
str
default:"'HF'"
Backend for incremental detokenization:
'HF': Use Hugging Face tokenizers backend (faster for small stream intervals)'TRTLLM': Use TensorRT-LLM backend
int
default:"24"
Threshold for switching between HF and TRTLLM incremental detokenization backends.
Usage Examples
Basic Encoding and Decoding
Streaming with Incremental Decoding
Chat Template Formatting
Batch Encoding
See Also
- LLM - Main LLM class
- SamplingParams - Generation parameters
- RequestOutput - Output structure