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.3.tar.gz (3.5 kB 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.3-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hf_tokenizer_visualizer-0.0.3.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • 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.3.tar.gz
Algorithm Hash digest
SHA256 fb3f1798dc85a28b5f2f28e3a1207d64be6e93aa3e6cdb588e21205a33c0efb2
MD5 903380969d137da13ee8f3637f6e18af
BLAKE2b-256 8dd1ce2a0c7ea2c3a8c2ec60cd091d35f8bc0b13f01fa5fc5b2fb9f09571725e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hf_tokenizer_visualizer-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c2b4b42c43f83d8456bd12e9222622a6d6a557c228d08b36ee5294c37ff523e7
MD5 6dcd9f7350b821e77368bd68bb1d659f
BLAKE2b-256 ebcb21629993dd39e25d8074d2282d7c26def9e17f8d3ce895ebd71361d09dd0

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