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

Uploaded Python 3

File details

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

File metadata

  • Download URL: genlm_bytes-0.2.0.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.2.0.tar.gz
Algorithm Hash digest
SHA256 05690861b623aba1dbb0fedd636b376f8509adbfdb5960e5f64a90e497c980c1
MD5 6a55e2292ca9904514a0c7edaf3d9263
BLAKE2b-256 097db766357371633e082e3e87446f0d6b06322eaefda244bba37eca0437be8d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: genlm_bytes-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 27.4 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0b7ed7d82e5f8168d265a53576d6c7287f770120c04afe5eb56c66628eedb370
MD5 a935638e968847e5daf08106cbe173b5
BLAKE2b-256 f59035c0afccc5481baf2ea45184229f9b9d942520e573ab0aa6263ccdc62eb6

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