Skip to main content

A Metropolis-Hastings based sampling framework for LLMs.

Project description

A Metropolis-Hastings sampler for LLMs.

Implements the sampling algorithm described in Reasoning with Sampling: Your Base Model is Smarter Than You Think in vLLM. This cuts the evaluation time for Qwen/Qwen2.5-Math-7B from 30hrs to <1hr on the MATH500 dataset on a B200 GPU compared to the official implementation.

This package patches the vLLM LLMEngine object and adds the alpha parameter to SamplingParams in order to sample from the power distribuion.

Installation

pip install mh-llm

or from the source:

pip install git+https://github.com/maxzuo/mh-llm.git

Example Usage

from mh_llm import MHLLM
from mh_llm.vllm import SamplingParams

# Initialize MH LLM with your model
mh_llm = MHLLM(model='Qwen/Qwen2.5-Math-7B')
# Define sampling parameters with alpha
sampling_params = SamplingParams(temperature=0.25, alpha=0.4)

# Generate samples, without metropolis-hastings or power distribution
output = mh_llm.generate("What is 1234 + 5678?", sampling_params=sampling_params)

# Sample with Metropolis-Hastings against the power distribution
mh_output = mh_llm.mh_sample(
    "What is 1234 + 5678?",
    sampling_params=sampling_params,
    block_size=192,
    max_new_tokens=3_072,
    num_mcmc_steps=10,
    use_tqdm=True,
)

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

mh_llm-0.0.1.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

mh_llm-0.0.1-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file mh_llm-0.0.1.tar.gz.

File metadata

  • Download URL: mh_llm-0.0.1.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mh_llm-0.0.1.tar.gz
Algorithm Hash digest
SHA256 785ae37a78e259d96f0de87c3d6326b400fb6c755e35aa45d32e8b0d54f7fbfc
MD5 e4aecb6962112d4503c72a6a77e94652
BLAKE2b-256 bc6940fbf90c9cacd58d26b635b5d90d23f6abec7dc1a827f245075182692820

See more details on using hashes here.

Provenance

The following attestation bundles were made for mh_llm-0.0.1.tar.gz:

Publisher: publish.yml on maxzuo/mh-llm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mh_llm-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: mh_llm-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 19.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mh_llm-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c09507ffae6f83146cfff9cca1460c63adf01a337e3b5827f3825c28742bd4fa
MD5 4551cf3dacab48e9d6cc30026809ac90
BLAKE2b-256 5563f0d3a3bd6c97c78ce59e0227c439588455cd1726a9900e50f07517749dfc

See more details on using hashes here.

Provenance

The following attestation bundles were made for mh_llm-0.0.1-py3-none-any.whl:

Publisher: publish.yml on maxzuo/mh-llm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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