Skip to main content

Visualize HuggingFace Byte-Pair Encoding (BPE) Tokenizer encoding process

Project description

Hugging Face Byte-Pair Encoding tokenizer visualizer library

The library can help you visualize how the encoding process happens in the Byte-Pair Encoding tokenizer algorithm when you pass on your text content for tokenization.

Byte-Pair Encoding (BPE) was initially developed as an algorithm to compress texts, and then used by OpenAI for tokenization when pre-training the GPT model. It’s used by a lot of Transformer models, including GPT, GPT-2, RoBERTa, BART, and DeBERTa.

Byte-Pair Encoding tokenization

BPE training starts by computing the unique set of words used in the corpus (after the normalization and pre-tokenization steps are completed), then building the vocabulary by taking all the symbols used to write those words.

More about the algorithm here

tiktoken-img

Visualizing the Tokenization process

During the tokenization process the input content is compressed into the encoded IDs based on the trained BPE-Tokenizer. During the training process the token-pairs are merged into new token ID based on their frequency of existence in the training corpus.

This library helps in visualizing how the merging process looks like for a given string to be encoded. It generates a graph where the nodes are tokens / characters and if a pair of characters are merged, the nodes are connected via directed edges.

Installing the Library

pip install hf-tokenizer-visualizer

Using the Library

  1. Save your visualization in a PNG or a PDF file.
from hf_tokenizer_visualizer import HfBPETokenizerVisualizer

visualizer = HfBPETokenizerVisualizer(
    pretrained_model_name="gpt2",
    save_visualization=True,
    file_type="png",
    file_name="bpe_tokenization_visualization_2",
    enable_debug=False,
)

visualizer.visualize_encoding('hello world')

Note: The file is saved in your current working directory. Note: You can choose between png and pdf file types.

  1. Get the raw encoding
from hf_tokenizer_visualizer import HfBPETokenizerVisualizer

visualizer = HfBPETokenizerVisualizer(
    pretrained_model_name="gpt2",
    save_visualization=True,
    file_type="png",
    file_name="bpe_tokenization_visualization_2",
    enable_debug=False,
)

visualizer.encode('hello world')

Output: [31373, 995]

Output Graph generated

generated graph

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

hf_tokenizer_visualizer-0.0.5.tar.gz (3.4 MB view details)

Uploaded Source

Built Distribution

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

hf_tokenizer_visualizer-0.0.5-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file hf_tokenizer_visualizer-0.0.5.tar.gz.

File metadata

  • Download URL: hf_tokenizer_visualizer-0.0.5.tar.gz
  • Upload date:
  • Size: 3.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for hf_tokenizer_visualizer-0.0.5.tar.gz
Algorithm Hash digest
SHA256 6f06a4b88d4048e6459080e4178283b60ad44f5dd233682e6256e8221e34e468
MD5 54160e9c318b0d917205d68a777ba24e
BLAKE2b-256 0f366e3deee7af22a9e832dd98386e11cf1f323a6d4eeb66cce7da886e027ff6

See more details on using hashes here.

File details

Details for the file hf_tokenizer_visualizer-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for hf_tokenizer_visualizer-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 5ccc12d44eb6a166b40ccfd155c856d435bda5e64627552100e682b049da2e2b
MD5 b42874eb28bd122a41f117f6413ebcaf
BLAKE2b-256 74b30840d8b02e17748c9a29a4ad0a7ecff75fd47e63096fcce0f7a1a0c0acfa

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