real-wordpiece
This repository provides a Python implementation of the WordPiece tokenizer, which is compatible with the 🤗
tokenizers.
Motivation
🤗 tokenizers provides state-of-the-art text tokenizer implementations. They are also used in the 🤗 transformers, helping to convert text into a format that might be fed into LLMs or embedding models. 🤗 tokenizers are broadly adopted in the NLP community, and became the de-facto standard for tokenization, providing models such as:
Surprisingly, the WordPiece tokenizer described in the brilliant Hugging Face NLP
Course is not the same as the one implemented in the 🤗 tokenizers library.
That fact is not well-known and vaguely documented. Instead of using the original WordPiece algorithm, it uses a
## prefix to indicate the continuation of the word and then uses the BPE algorithm to merge the tokens. This process
is mentioned in the NLP Course, but might be surprising for
those who haven't read it.
This library fills the gap by providing a Python implementation of the original WordPiece tokenizer, in a way that is
described in the course.
Installation
real-wordpiece can be installed from PyPI using pip or the package manager of your choice:
pip install real-wordpiece
Usage
Since the 🤗 tokenizers library is written in Rust, it is not possible to directly extend its interfaces with Python.
Thus, the real-wordpiece package provides a Python implementation of the WordPiece tokenizer, which can produce a
compatible model, but its interface is slightly different.
from tokenizers.models import WordPiece
from tokenizers.normalizers import Lowercase
from tokenizers.pre_tokenizers import Whitespace
from tokenizers.tokenizers import Tokenizer
from real_wordpiece.trainer import RealWordPieceTrainer
# Create the Tokenizer, in the same way as you would with the 🤗 tokenizers
tokenizer = Tokenizer(WordPiece(unk_token="[UNK]"))
tokenizer.normalizer = Lowercase()
tokenizer.pre_tokenizer = Whitespace()
# Define the training data
training_data = [
"walker walked a long walk",
]
# Finally, train the tokenizer using the RealWordPieceTrainer
trainer = RealWordPieceTrainer(vocab_size=28, special_tokens=["[UNK]"])
trainer.train_tokenizer(training_data, tokenizer)
# The tokenizer.model will be now an instance of WordPiece trained above
print(tokenizer.encode("walker walked a long walk").tokens)
# Out: ['walk', '##er', 'walk', '##ed', 'a', 'long', 'walk']
In real-world applications the training corpus should be much larger, and the vocab_size should be set to a higher
value.
WordPiece basics
WordPiece and Byte-Pair Encoding (BPE) are two of the most popular subword tokenization algorithms, and they have much in common. Let's consider and example and assume we have just a single word in our corpus.
word = "reappear"
The training process of the BPE algorithm starts with a vocabulary that contains all the characters.
vocab = {"r", "e", "a", "p"}
The algorithm iteratively merges the most frequent pair of tokens in the vocabulary and adds it to the vocabulary,
until the vocabulary reaches the desired size. In the case of the word "reappear", the BPE algorithm would merge the
pair ("e", "a") to create the token "ea".
vocab = {"r", "e", "a", "p", "ea"}
The process would continue until the vocabulary reaches the desired size or there are no more pairs to merge.
The WordPiece algorithm is similar to BPE, but it distinguishes first letters of words from the middle letters. The
middle letters are prefixed with ##. The WordPiece algorithm starts with a vocabulary that contains all the characters
and also the middle letters.
vocab = {"r", "e", "a", "p", "##r", "##e", "##a", "##p"}
WordPieces also uses a different heuristic to select the pair of tokens to merge. Instead of merging the most frequent pair, WordPiece merges the pair that maximizes the score function that is defined as:
$$ score(u, v) = \frac{frequency(u, v)}{frequency(u) \dot frequency(v)} $$
Where $u$ and $v$ are tokens, $frequency(u, v)$ is the frequency of the pair in the corpus, and $frequency(u)$ and $frequency(v)$ are the frequencies of the tokens $u$ and $v$ alone. BPE merges are a bit more intuitive, and both algorithms may lead to different tokenization.
Why does that matter?
Choosing the tokenization is another hyperparameter that can significantly affect the performance of the model. Large Language Models' ability to solve different tasks might be limited by the tokenization algorithm used.
References
The importance of the tokenization model is becoming more and more apparent. The following resources provide more information on the topic:
Metadata
Release files for real-wordpiece 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| real_wordpiece-0.1.7.tar.gz | 11.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| real_wordpiece-0.1.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.0 kB
Release files / real_wordpiece-0.1.7.tar.gz
| Download URL | real_wordpiece-0.1.7.tar.gz |
|---|---|
| Size | 11.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
394efa30a7f906ee5eb5575949cd972fa4d501f62752e5594cc67bf265975ca7
|
|
BLAKE2b-256 checksum How to use checksums |
06dfb55aaee861e418e441805d062d6ce875fd1bd832a1591bf118e57e765f56
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / real_wordpiece-0.1.7-py3-none-any.whl
| Download URL | real_wordpiece-0.1.7-py3-none-any.whl |
|---|---|
| Size | 11.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8e79ca54fb72ebf13e8e8b386510b6c3bbe2fa195cb7937eedf2a9b3a1612403
|
|
BLAKE2b-256 checksum How to use checksums |
8ffb08f5c5fa672cc8eedd107fb3bab92fe56317eae9b4b1bb9f5301fd6f1a6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Security for real-wordpiece 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.