Skip to main content

Algorithms for byte-level language modeling

Project description

Logo

Docs Tests

GenLM Bytes is a Python library for byte-level language modeling. It contains algorithms for turning token-level language models into byte-level language models.

See the docs for details and basic usage.

Note: This project is under active development — expect bugs, missing features, and breaking changes. Please report any issues or suggestions in the issue tracker.

Quick Start

This library requires python>=3.11 and can be installed using pip:

pip install genlm-bytes

For faster and less error-prone installs, consider using uv:

uv pip install genlm-bytes

See DEVELOPING.md for details on how to install the project for development.

Usage

from genlm.bytes import ByteBeamState, BeamParams
from genlm.backend import load_model_by_name

# Load a token-level language model from a huggingface model name
# (Note: for fast GPU inference, specify `backend="vllm"`)
llm = load_model_by_name("gpt2-medium")

# Initialize a beam state with a maximum beam width of 5 and a prune threshold of 0.05 (higher threshold values lead to more aggressive pruning).
beam = await ByteBeamState.initial(llm, BeamParams(K=5, prune_threshold=0.05))

# Populate the beam state with byte context.
beam = await beam.prefill(b"An apple a day keeps the ")

# Get the log probability distribution over the next byte.
logp_next = await beam.logp_next()
logp_next.pretty().top(5)
# Example output:
# b'd' -0.5766762743944795
# b'b' -2.8732729803080233
# b's' -2.9816068063730867
# b'w' -3.3758250127787264
# b'm' -3.528177345847574

# Prune the beam and extend it with a new byte
new_beam = await (beam.prune() << 100) # 100 is the byte value of 'd'

See basic usage for a more detailed example.

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

genlm_bytes-0.1.2.tar.gz (3.5 MB view details)

Uploaded Source

Built Distribution

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

genlm_bytes-0.1.2-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

Details for the file genlm_bytes-0.1.2.tar.gz.

File metadata

  • Download URL: genlm_bytes-0.1.2.tar.gz
  • Upload date:
  • Size: 3.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for genlm_bytes-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ee69720e85cb8c8fcf660d2717c9c185d2e2eaf00392ca0fb40216b8397f064c
MD5 3a51f7f17d418a82c52567ecdc3dd526
BLAKE2b-256 0c235bba9b25f8e109ed759ddf4d3168a90d5a878ba9b3fa09bf9de393c68f66

See more details on using hashes here.

File details

Details for the file genlm_bytes-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: genlm_bytes-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 25.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for genlm_bytes-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ed8a0ae4bb3caddb2a4d671b2c972823e57f6bb48af7d23dc2c80ed2a49fa240
MD5 bbb2d3c085651631830e8854f8c258e1
BLAKE2b-256 6d57f2b3a1ec46312726f7ac5d9d9ae0b8b45155aff901eae6d28980b6a3198f

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