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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for genlm_bytes-0.1.0.tar.gz
Algorithm Hash digest
SHA256 53552177757c151ee9e1c7dbdfe74ef45e85f7e9c75221b70d73bd777c65c806
MD5 99e7b05028f3e76454b6dedd321e7e7f
BLAKE2b-256 2786d90854928bea43a62abdf71164cdca07467a57735e90dd96f8632274dd7c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for genlm_bytes-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 943f0e54f74a11b10e7d550aa531ba7161206c3d6e45c1a432a31cbfd86de855
MD5 3e134eaa1e31bf59285737eed19d08b3
BLAKE2b-256 fe20a2bcb80653833b62397994037352c29c3e06d3f2ebcf210a5d6514b2d5aa

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