Beam Search over Efficient llama.cpp generations
Install
pip install llama-cpp-beam-search
pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
# OR
uv add llama-cpp-beam-search
uv pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
Multi-Platform Compatibility
You can prepend the variable to trigger the llama-cpp-python build for your specific platform/accelerator (see here).
# MacOS + PIP
CMAKE_ARGS="-DGGML_METAL=on" pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
# MacOS + UV
CMAKE_ARGS="-DGGML_METAL=on" uv pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
# CUDA + PIP
CMAKE_ARGS="-DGGML_CUDA=on" pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
# CUDA + UV
CMAKE_ARGS="-DGGML_CUDA=on" uv pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
Theory
Our rough idea was to get a complete (holistic) probabilistic view of the tokens and prune accordingly. To save on the search space, we only take $f$ expansions, or go depth first.
Example Usage
from llama_cpp_beamsearch.completion import BeamSearchCompletion
from llama_cpp_beamsearch.config import BeamSearchConfig
from llama_cpp import Llama, LlamaGrammar, ChatCompletionRequestMessage
import re
model = Llama.from_pretrained(
repo_id="unsloth/Qwen3-0.6B-GGUF",
filename="*Q4_0.gguf",
verbose=False,
logits_all=True,
)
allowed_tokens = ["Wrench", "Screwdriver", "Ornament"]
allowed_tokens_str = " | ".join(f'"{t}"' for t in allowed_tokens)
grammar = LlamaGrammar(
_grammar=f"""
root ::= allowed_tokens
start ::= ( allowed_tokens " " )*
allowed_tokens ::= {allowed_tokens_str}
"""
)
config = BeamSearchConfig(
max_depth=1,
end_token=None,
k_progress=[2, 3],
)
completion = BeamSearchCompletion(model, grammar, config)
message = "Hello?"
result = completion.completion_beam_search(message, max_tokens=5)
assert re.match(r"^((Wrench|Screwdriver|Ornament) ?)*$", result)
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 llama_cpp_beam_search-0.1.1.tar.gz.
File metadata
- Download URL: llama_cpp_beam_search-0.1.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15548a242a9c4468234b35f3c51d18426d43c47426ed635d9cefa4e5d3cc843c
|
|
| MD5 |
c5cf5cd27570798d1746e0ec8ba10264
|
|
| BLAKE2b-256 |
3dab93c55223f4f37daefd51c52822ea1730ffffa78e2b3d2fc7f7709ef0e933
|
Provenance
The following attestation bundles were made for llama_cpp_beam_search-0.1.1.tar.gz:
Publisher:
python-publish.yml on Dakantz/CHASTE
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llama_cpp_beam_search-0.1.1.tar.gz -
Subject digest:
15548a242a9c4468234b35f3c51d18426d43c47426ed635d9cefa4e5d3cc843c - Sigstore transparency entry: 2725205422
- Sigstore integration time:
-
Permalink:
Dakantz/CHASTE@265d53177719a1d17ff46d7fc92ae76d9bc47fb3 -
Branch / Tag:
refs/tags/beams-v0.1.1 - Owner: https://github.com/Dakantz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@265d53177719a1d17ff46d7fc92ae76d9bc47fb3 -
Trigger Event:
release
-
Statement type:
File details
Details for the file llama_cpp_beam_search-0.1.1-py3-none-any.whl.
File metadata
- Download URL: llama_cpp_beam_search-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c3ecccb5e354f6824b71e516ac66b54aac76088614ab80d023df0f473b834c8
|
|
| MD5 |
e246172d3263d6d422ee908faf7b2bd0
|
|
| BLAKE2b-256 |
0206d20666106dffea2de197eafa35a17805d5105e40125fd9c6d95971a17d6f
|
Provenance
The following attestation bundles were made for llama_cpp_beam_search-0.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on Dakantz/CHASTE
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llama_cpp_beam_search-0.1.1-py3-none-any.whl -
Subject digest:
0c3ecccb5e354f6824b71e516ac66b54aac76088614ab80d023df0f473b834c8 - Sigstore transparency entry: 2725206303
- Sigstore integration time:
-
Permalink:
Dakantz/CHASTE@265d53177719a1d17ff46d7fc92ae76d9bc47fb3 -
Branch / Tag:
refs/tags/beams-v0.1.1 - Owner: https://github.com/Dakantz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@265d53177719a1d17ff46d7fc92ae76d9bc47fb3 -
Trigger Event:
release
-
Statement type: