mistral_common.tokens.tokenizers.sentencepiece
SentencePieceTokenizer(model_path, tokenizer_version=None)
Bases: Tokenizer
SentencePiece tokenizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str | Path
|
The path to the SentencePiece model file. |
required |
tokenizer_version
|
TokenizerVersion | None
|
The version of the tokenizer. If |
None
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the model file does not exist or its internal vocab size is inconsistent. |
TokenizerException
|
If the version inferred from the filename is unrecognized. |
Source code in src/mistral_common/tokens/tokenizers/sentencepiece.py
bos_id
cached
property
The beginning of sentence token id.
eos_id
cached
property
The end of sentence token id.
file_path
property
The path to the tokenizer model.
model_settings_builder
property
Always returns None as SentencePiece does not support model_settings_builder.
n_words
property
Vocabulary size of the tokenizer.
num_special_tokens
property
The number of special tokens of the tokenizer.
pad_id
property
The padding token id.
special_ids
cached
property
Ids of the special tokens.
unk_id
property
The unknown token id.
version
property
The version of the tokenizer.
decode(tokens, special_token_policy=SpecialTokenPolicy.IGNORE)
Decode the given list of token ids into a string.
Note
Using special_token_policy=SpecialTokenPolicy.KEEP will keep the special tokens and the normal tokens as
SentencePiece pieces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
list[int]
|
The list of token ids. |
required |
special_token_policy
|
SpecialTokenPolicy
|
The policy to use for special tokens. |
IGNORE
|
Returns:
| Type | Description |
|---|---|
str
|
The decoded string. |
Source code in src/mistral_common/tokens/tokenizers/sentencepiece.py
encode(s, bos, eos)
Encode the given string into a list of token ids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The string to encode. |
required |
bos
|
bool
|
Whether to add the beginning of sentence token. |
required |
eos
|
bool
|
Whether to add the end of sentence token. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
The list of token ids. |
Source code in src/mistral_common/tokens/tokenizers/sentencepiece.py
get_control_token(s)
Get the token ID of a control token. Deprecated: use get_special_token() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The special token string. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The token ID for the special token. |
Source code in src/mistral_common/tokens/tokenizers/sentencepiece.py
get_special_token(s)
Get the token ID for a special token string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The special token string (e.g., " |
required |
Returns:
| Type | Description |
|---|---|
int
|
The token ID for the special token. Unknown strings map to the |
int
|
unknown token ID (sentencepiece behavior). |
Source code in src/mistral_common/tokens/tokenizers/sentencepiece.py
id_to_piece(token_id)
is_special(token)
Check if a token is a special (control) token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
int | integer | str
|
Token ID (int or numpy integer) or token string to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If token is not an int, numpy integer, or str. |
Source code in src/mistral_common/tokens/tokenizers/sentencepiece.py
get_image_config(tokenizer_filename)
Get the image config from the tokenizer filename.
The multimodal version suffix (e.g., "m1" in "tokenizer.model.v7m1") selects the image configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer_filename
|
str | Path
|
The tokenizer model path to parse. |
required |
Returns:
| Type | Description |
|---|---|
ImageConfig | None
|
The ImageConfig for the multimodal version in the filename, or |
ImageConfig | None
|
if the filename carries no multimodal version. |
Raises:
| Type | Description |
|---|---|
TokenizerException
|
If the multimodal version in the filename is unrecognized. |
Source code in src/mistral_common/tokens/tokenizers/sentencepiece.py
get_spm_version(tokenizer_filename, raise_deprecated=False)
Get the version of the tokenizer from the filename.
Expects filenames like "tokenizer.model.v3" or "tokenizer.model.v7m1".
A bare tokenizer.model is treated as v1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer_filename
|
str | Path
|
The tokenizer model path to parse. |
required |
raise_deprecated
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
TokenizerVersion
|
The TokenizerVersion parsed from the filename. |
Raises:
| Type | Description |
|---|---|
TokenizerException
|
If |
Source code in src/mistral_common/tokens/tokenizers/sentencepiece.py
is_sentencepiece(path)
Check if the given path is a SentencePiece model.
Recognizes files ending in .model or .model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The path to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
suffix, |
Source code in src/mistral_common/tokens/tokenizers/sentencepiece.py
is_sentencepiece_tokenizer(tokenizer)
Return whether the tokenizer is a SentencePieceTokenizer.