BPE¶
trimbed.backends.bpe
¶
Selection constraints for byte-pair-encoding tokenizers (GPT-2, Qwen, Llama, ...).
BpeBackend
¶
Bases: VocabBackend
Keeps a BPE vocabulary encodable and its merge chains intact.
structural_tokens
¶
structural_tokens(spec: TokenizerSpec) -> set[str]
Return the unk token plus, for byte-level BPE, the whole byte alphabet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
TokenizerSpec
|
The tokenizer being trimmed. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Token strings that may never be dropped. For codefuse-ai/F2LLM-v2-160M that is
the 256 alphabet characters ( |
View source on GitHub: src/trimbed/backends/bpe.py lines 22–37
dependencies
¶
dependencies(
spec: TokenizerSpec,
) -> dict[int, tuple[int, ...]]
Map each merged token to the pair it is assembled from.
codefuse-ai/F2LLM-v2-160M builds "Ġthe" by merging "Ġth" with "e", so
dropping "Ġth" leaves "Ġthe" in the vocabulary but unreachable and the text
quietly tokenizes into characters instead. The selector uses this mapping to pull
in whatever a kept token is built from.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
TokenizerSpec
|
The tokenizer being trimmed. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, tuple[int, ...]]
|
A mapping of merged token id to its two parent ids, e.g. |
View source on GitHub: src/trimbed/backends/bpe.py lines 39–63