bytes
Functions to get the byte vocabulary from a HuggingFace tokenizer
get_byte_vocab(tokenizer)
Extract byte vocabulary from a tokenizer.
The byte encoding is chosen by the tokenizer's decoder: ByteLevel uses the GPT-2
byte alphabet, ByteFallback or Metaspace uses SentencePiece pieces. Any other
declared decoder is rejected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
A Hugging Face tokenizer instance. |
required |
Returns:
| Type | Description |
|---|---|
list[bytes]
|
List of byte representations of tokens. |
Raises:
| Type | Description |
|---|---|
ByteVocabError
|
If vocabulary cannot be decoded. |
Source code in genlm/backend/tokenization/bytes.py
get_byte_tokens_from_byte_decoder(tokenizer, byte_decoder)
Convert tokens to bytes using a byte decoder mapping.
Special tokens are handled by directly encoding their string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
A Hugging Face tokenizer instance |
required | |
byte_decoder
|
dict
|
Dictionary mapping characters to bytes |
required |
Returns:
| Name | Type | Description |
|---|---|---|
byte_tokens |
list[byte]
|
List of byte representations for each token |
Source code in genlm/backend/tokenization/bytes.py
get_byte_tokens_from_sp(tokenizer)
Convert SentencePiece token strings to bytes.
<0xXX> escapes become the byte they name and ▁ becomes a space; special
tokens are encoded directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
A Hugging Face tokenizer instance |
required |
Returns:
| Name | Type | Description |
|---|---|---|
byte_tokens |
list[bytes]
|
List of byte representations for each token in the vocabulary |
Source code in genlm/backend/tokenization/bytes.py
check_byte_decoder(tokenizer, byte_decoder)
Verify that a byte decoder can properly handle all tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
A Hugging Face tokenizer instance |
required | |
byte_decoder
|
dict
|
Dictionary mapping characters to bytes |
required |
Raises:
| Type | Description |
|---|---|
ByteDecoderError
|
If byte decoder fails validation checks |