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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
785ae37a78e259d96f0de87c3d6326b400fb6c755e35aa45d32e8b0d54f7fbfc
|
|
| MD5 |
e4aecb6962112d4503c72a6a77e94652
|
|
| BLAKE2b-256 |
bc6940fbf90c9cacd58d26b635b5d90d23f6abec7dc1a827f245075182692820
|
Provenance
The following attestation bundles were made for mh_llm-0.0.1.tar.gz:
Publisher:
publish.yml on maxzuo/mh-llm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mh_llm-0.0.1.tar.gz -
Subject digest:
785ae37a78e259d96f0de87c3d6326b400fb6c755e35aa45d32e8b0d54f7fbfc - Sigstore transparency entry: 766572589
- Sigstore integration time:
-
Permalink:
maxzuo/mh-llm@2880e90bacf29fdb926877df2d0cbf2e358cbb28 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/maxzuo
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2880e90bacf29fdb926877df2d0cbf2e358cbb28 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c09507ffae6f83146cfff9cca1460c63adf01a337e3b5827f3825c28742bd4fa
|
|
| MD5 |
4551cf3dacab48e9d6cc30026809ac90
|
|
| BLAKE2b-256 |
5563f0d3a3bd6c97c78ce59e0227c439588455cd1726a9900e50f07517749dfc
|
Provenance
The following attestation bundles were made for mh_llm-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on maxzuo/mh-llm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mh_llm-0.0.1-py3-none-any.whl -
Subject digest:
c09507ffae6f83146cfff9cca1460c63adf01a337e3b5827f3825c28742bd4fa - Sigstore transparency entry: 766572596
- Sigstore integration time:
-
Permalink:
maxzuo/mh-llm@2880e90bacf29fdb926877df2d0cbf2e358cbb28 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/maxzuo
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2880e90bacf29fdb926877df2d0cbf2e358cbb28 -
Trigger Event:
release
-
Statement type: