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

Uploaded Python 3

File details

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

File metadata

  • Download URL: hf_tokenizer_visualizer-0.0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 85c876bea0f5531243c301a351ee023cffc0f4a103ca260a2b1b9de877d02b30
MD5 250ccd581f97e8f5820c4c204c921f49
BLAKE2b-256 b0fe39659f64d94adfb0b1d81da34bef18756e5cddf0bfdae4411fa1bcb48417

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hf_tokenizer_visualizer-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 dc5e985a3fe947dc92cb44089e632458c5fe0bfbcdf4ec5e408a7916432b607c
MD5 48f9eb254c28eabd9da16564e9e5e418
BLAKE2b-256 f30564bc3f1a3e06b3063f7d6762a2476a99af70ab99a977437b095720a225ea

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