Skip to main content

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

llama_cpp_beam_search-0.1.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

llama_cpp_beam_search-0.1.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

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

Hashes for llama_cpp_beam_search-0.1.1.tar.gz
Algorithm Hash digest
SHA256 15548a242a9c4468234b35f3c51d18426d43c47426ed635d9cefa4e5d3cc843c
MD5 c5cf5cd27570798d1746e0ec8ba10264
BLAKE2b-256 3dab93c55223f4f37daefd51c52822ea1730ffffa78e2b3d2fc7f7709ef0e933

See more details on using hashes here.

Provenance

The following attestation bundles were made for llama_cpp_beam_search-0.1.1.tar.gz:

Publisher: python-publish.yml on Dakantz/CHASTE

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

File details

Details for the file llama_cpp_beam_search-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_cpp_beam_search-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0c3ecccb5e354f6824b71e516ac66b54aac76088614ab80d023df0f473b834c8
MD5 e246172d3263d6d422ee908faf7b2bd0
BLAKE2b-256 0206d20666106dffea2de197eafa35a17805d5105e40125fd9c6d95971a17d6f

See more details on using hashes here.

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

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

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page