How do I determine what the bos/eos/unk/pad tokens are for a particular LLM model?
Assistant
There are a few ways to determine the special tokens (bos/eos/unk/pad) used by a particular language model.
Check the model's documentation: The model's documentation or the website of the organization that created the model (like OpenAI, Hugging Face, etc.) often contain this information.
Look at the model's configuration file: If you have access to the model's configuration file, it usually contains the definitions for these special tokens.
Using the model's API: Some models provide APIs or functions that allow you to get these special tokens. For example, if you're using Hugging Face's transformers library, you can use the get_special_tokens_mask method to get these special tokens.
Keep in mind that not all models use all of these special tokens. For example, the GPT-2 model used in the example above does not use a bos_token or a pad_token, and if you try to print them, you'll get None.
Please note that the bos/eos/unk/pad tokens stand for:
bos_token: Beginning of Sentence token
eos_token: End of Sentence token
unk_token: Unknown token (used for words that are not in the model's vocabulary)
pad_token: Padding token (used to fill up sequences to a certain length)
In the context of the Hugging Face's transformers library, a tokenizer object typically contains various fields that define its behavior. The specific fields may differ based on the type of tokenizer and the model architecture it is intended for (BERT, GPT-2, etc.). However, there are some common fields you may encounter:
Common Fields
tokenizer.vocab: The vocabulary that the tokenizer uses, usually as a dictionary where keys are words/subwords and values are their integer identifiers.
tokenizer.vocab_size: The size of the tokenizer's vocabulary.
tokenizer.model_max_length: The maximum length of the tokenized output. Input sequences longer than this will be truncated.
tokenizer.padding_side: Which side to pad the input sequence ("right" or "left").
tokenizer.pad_token: The token used for padding when the input sequence is shorter than model_max_length.
tokenizer.pad_token_id: The ID corresponding to the pad token.
tokenizer.eos_token: End of Sentence token.
tokenizer.eos_token_id: The ID corresponding to the End of Sentence token.
tokenizer.bos_token: Beginning of Sentence token.
tokenizer.bos_token_id: The ID corresponding to the Beginning of Sentence token.
tokenizer.unk_token: Unknown token for out-of-vocabulary words.
tokenizer.unk_token_id: The ID corresponding to the Unknown token.
tokenizer.special_tokens_map: A map of special tokens.
tokenizer.backend_tokenizer: The backend used by the tokenizer, which actually performs the tokenization. Could be implemented in Rust or Python, depending on the tokenizer.
tokenizer.pre_tokenizer: Pre-tokenization operations, if any.
tokenizer.post_processor: Post-processing steps for the tokenized output, if any.
Methods
Apart from these fields, tokenizers also have various methods like encode, decode, __call__, batch_encode_plus, etc., for performing tokenization and related tasks.
Code Example
Here's a code snippet in Python to give you a list of attributes of the Hugging Face tokenizer object for GPT-2.
python
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('gpt-2')
attributes = [attr for attr in dir(tokenizer) if not callable(getattr(tokenizer, attr)) and not attr.startswith("__")]
print(attributes)
Please note that the actual fields can vary depending on the specific tokenizer and its configuration, so it might be helpful to look at the documentation or source code for the specific tokenizer you are interested in.