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.

Using the Library

from hf_tokenizer import HfBPETokenizerVisualizer

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

encoded_ids = visualizer.encode("hello world")
print(encoded_ids)

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.2.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.2-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hf_tokenizer_visualizer-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 900d89abb304ae4fe8d4e5718952ff2858f8a04d2fb48813307b2dd4c9e2095a
MD5 cb342a489324b7c3ba52f8a137028cdc
BLAKE2b-256 0ecdd468ec78f8fa493128bb1655827836068c93bbaaf4c1227dbbb1f3af5810

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hf_tokenizer_visualizer-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e090447295c6982f0d5043681ad866282b149cd21107a0036ef3d75c9012e6c4
MD5 a4f912d13cca1dd0709de73a85edc66f
BLAKE2b-256 863c8b67c625d47f64a269ba098ffbb4f35ceb5881c82bf37052651ca9b36c37

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