Library for manipulating the existing tokenizer.
Project description
Tokens-Deletion
Python script for manipulating the existing tokenizer.
The solution was tested on Llama3-8B tokenizer.
Installation
Installation from PyPI:
pip install tokenizerchanger
Usage:
changer = TokenizerChanger(tokenizer)
Create the object of TokenizerChanger class that requires an existing tokenizer that will be changed, e.g. PreTrainedTokenizerFast class from рџ¤— Tokenizers library.
Deletion:
changer.delete_k_least_frequent_tokens(k=1000)
changer.delete_k_least_frequent_tokens(k=1000, exclude=list_of_tokens)
Deletes k most frequent tokens. The exclude argument stands for tokens that will be ignored during the deletion of least frequent tokens.
changer.delete_unwanted_tokens(list_of_unwanted_tokens)
Deletes all tokens from list_of_unwanted_tokens from the tokenizer.
changer.delete_tokens(list_of_unwanted_tokens)
Now, you can delete exactly the list of unwanted tokens, in contrast to the delete_unwanted_tokens function, which deletes all tokens from the list and tokens that contain unwanted tokens as a substring.
changer.delete_overlaps(vocab)
Finds and deletes all intersections of the tokenizer's vocabulary and the vocab variable from the tokenizer. Notice that vocab should be a dict variable.
changer.delete_inappropriate_merges(vocab)
Deletes all merges from tokenizer which contradict the vocab variable. Notice that vocab should be a list[str] variable.
Addition:
The idea of creating such functions arose due to the fact that the built-in functions do not add tokens/merges properly, when some tokens are deleted. That is why you can get more tokens after encoding the same text, even if the necessary tokens have been added.
changer.add_tokens(list_of_tokens)
Adds the tokens from the list. The indexes will be filled automatically.
changer.add_merges(list_of_merges)
Adds the merges from the list.
"Get" functions:
changer.get_overlapping_tokens(vocab)
Returns the intersection between the tokenizer's vocabulary and the vocab variable. Notice that vocab should be a dict variable.
changer.get_overlapping_megres(merges)
Returns the intersection between the tokenizer's merges and the merges variable. Notice that merges should be a list variable.
Saving:
changer.save_tokenizer(path)
Saves the current state of the changed tokenizer. Additionally, it saves tokenizer configs into path folder (./updated_tokenizer by default).
tokenizer = ch.updated_tokenizer()
Return the changed tokenizer.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file TokenizerChanger-0.0.2.tar.gz.
File metadata
- Download URL: TokenizerChanger-0.0.2.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
786b3ccef2cd496b53b46835835b2a7d2d97eb2b9a3ce7cb75d32ddd9724a941
|
|
| MD5 |
2babfef26ae91a03d1c2ca4b19d0e111
|
|
| BLAKE2b-256 |
4ee4d6af3d4c1126164eb6613c85607653cb791c4e03b15c7abe902678cb4169
|
File details
Details for the file TokenizerChanger-0.0.2-py3-none-any.whl.
File metadata
- Download URL: TokenizerChanger-0.0.2-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d912499144fa32207ea766385e31d598a6e505b2fa9a2be04454f9b7bddeeb17
|
|
| MD5 |
20fa44f986bd4c8429417c03fa97cccc
|
|
| BLAKE2b-256 |
c7d910c738943d6f67619d9c22c8780c04df24fcf2ce86e7824fe35e885fc4a4
|