Skip to main content

Library for manipulating the existing tokenizer.

Project description

Tokenizer-Changer

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, space_sign)

Create the object of TokenizerChanger class that optionally requires an existing tokenizer and space sign, which differs from one tokenizer to another. The tokenizer could be PreTrainedTokenizerFast class from рџ¤— tokenizers library.

changer.load_tokenizer(tokenizer)

If you did not load the tokenizer with TokenizerChanger class declaration, you can load it using this function.

changer.set_space_sign(space_sign)

If you did not set the space sign with TokenizerChanger class declaration, you can set it using this function. Default space sign is Д .

Deletion

changer.delete_tokens(list_of_unwanted_tokens, include_substrings)

Deletes the unwanted tokens from the tokenizer. If include_substrings is True, all token occurrences will be deleted even in other tokens. Defaults to True.

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 the least frequent tokens.

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_merges(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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

TokenizerChanger-0.3.2.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

TokenizerChanger-0.3.2-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file TokenizerChanger-0.3.2.tar.gz.

File metadata

  • Download URL: TokenizerChanger-0.3.2.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.11

File hashes

Hashes for TokenizerChanger-0.3.2.tar.gz
Algorithm Hash digest
SHA256 eee3a5a9aebc45b7af0aa9dcebf69e41e6db1339aba453d44d5fce91247a139e
MD5 70c9fb781e67c036a51b1fe0094844c9
BLAKE2b-256 fd083ce34aad86c8d81535ba79bbfaf77741f474fa77d63e52d958241488c0ea

See more details on using hashes here.

File details

Details for the file TokenizerChanger-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for TokenizerChanger-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7b2a4e25bf95f9963361e63c7f14d82d0e251b13d5f677a7889a1a83b0ebb040
MD5 997efb24f15a8f3ff704fabcda8de1f3
BLAKE2b-256 7696bc2cdb81a5bfc9cdc73081c11f9046949cf89cd262c2a9faf8510acd28ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page