llm.tokenization — Tokenizer Implementations¶
Support for HuggingFace tokenizers and a built-in character-level tokenizer for simple experiments.
Overview¶
| Module | Purpose |
|---|---|
tokenizer |
Base tokenizer interface |
simple_tokenizer |
Character-level tokenizer |
bpe_tokenizer |
Byte-Pair Encoding tokenizer |
train_bpe |
BPE training utilities |
Base Tokenizer¶
tokenizer
¶
BaseTokenizer
¶
Bases: Protocol
Abstract base class for all tokenizers.
源代码位于: src/llm/tokenization/tokenizer.py
encode
¶
HFTokenizer
¶
Wrapper for HuggingFace Transformers Tokenizers.
源代码位于: src/llm/tokenization/tokenizer.py
Simple Character Tokenizer¶
simple_tokenizer
¶
SimpleCharacterTokenizer
¶
A simple character-level tokenizer.
This tokenizer builds a vocabulary from a given corpus and provides methods to encode text into a sequence of integer tokens and decode a sequence of tokens back into text.
源代码位于: src/llm/tokenization/simple_tokenizer.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
encode
¶
Encodes a string of text into a list of integer tokens.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
text
|
str
|
The input string to encode. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
list[int]
|
list[int]: A list of integer tokens representing the input text. |
引发:
| 类型 | 描述 |
|---|---|
KeyError
|
If the text contains characters not present in the tokenizer's vocabulary (i.e., not found in the initial corpus). |
源代码位于: src/llm/tokenization/simple_tokenizer.py
decode
¶
Decodes a list of integer tokens back into a string of text.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
tokens
|
list[int]
|
A list of integer tokens to decode. |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
str |
str
|
The decoded string. |
引发:
| 类型 | 描述 |
|---|---|
KeyError
|
If the list contains token IDs not present in the tokenizer's vocabulary. |
源代码位于: src/llm/tokenization/simple_tokenizer.py
BPE Tokenizer¶
bpe_tokenizer
¶
BPETokenizer
¶
A Byte Pair Encoding (BPE) tokenizer using the tokenizers library.
源代码位于: src/llm/tokenization/bpe_tokenizer.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
pad_token_id
property
¶
Returns the ID of the [PAD] token.
token_to_id("[PAD]") is None whenever the trained/loaded
vocab lacks [PAD] (custom special_tokens, or a foreign
tokenizer.json). Callers fall back with getattr(tokenizer,
"pad_token_id", 0) — but the attribute exists as None, so the
default never fires and padding builds [None] * n, crashing
torch.tensor in batch_generate / TextDataset (RIL
ISS-155). Fall back to the UNK token's id then the documented 0,
so the property never returns None and never collides with
content.
Both lookups use explicit is not None checks, NOT an or
chain: [PAD] (or <unk>) is legitimately vocab id 0 in
some tokenizers, and 0 or ... falls through to the WRONG token —
padding every sample with the real content token <unk> instead of
[PAD] (silent data corruption; deep-dive finding).
train
classmethod
¶
Trains a BPE tokenizer on the given files.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
files
|
list[str]
|
List of paths to text files for training. |
必需 |
vocab_size
|
int
|
The desired vocabulary size. |
5000
|
min_frequency
|
int
|
The minimum frequency for a pair to be merged. |
2
|
special_tokens
|
list[str]
|
List of special tokens to include. |
None
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
BPETokenizer |
BPETokenizer
|
A trained tokenizer instance. |
源代码位于: src/llm/tokenization/bpe_tokenizer.py
encode
¶
Encodes a string into a list of token IDs.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
text
|
str
|
The input text. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
list[int]
|
list[int]: The list of token IDs. |
decode
¶
Decodes a list of token IDs back into a string.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
ids
|
list[int]
|
The list of token IDs. |
必需 |
skip_special_tokens
|
bool
|
Whether to skip special tokens in the output. |
True
|
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
str |
str
|
The decoded string. |
源代码位于: src/llm/tokenization/bpe_tokenizer.py
save
¶
Saves the tokenizer to a file.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
path
|
str
|
The path to save the tokenizer to. |
必需 |
源代码位于: src/llm/tokenization/bpe_tokenizer.py
load
classmethod
¶
Loads a tokenizer from a file.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
path
|
str
|
The path to the tokenizer file. |
必需 |
返回:
| 名称 | 类型 | 描述 |
|---|---|---|
BPETokenizer |
BPETokenizer
|
The loaded tokenizer instance. |