Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions llama_cpp/llama_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4575,14 +4575,14 @@ def llama_sampler_init_greedy() -> llama_sampler_p: ...

# LLAMA_API struct llama_sampler * llama_sampler_init_dist (uint32_t seed);
@ctypes_function("llama_sampler_init_dist", [ctypes.c_uint32], llama_sampler_p_ctypes)
def llama_sampler_init_dist(seed: int) -> llama_sampler_p: ...
def llama_sampler_init_dist(seed: int, /) -> llama_sampler_p: ...


# /// @details Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
# /// Setting k <= 0 makes this a noop
# LLAMA_API struct llama_sampler * llama_sampler_init_top_k (int32_t k);
@ctypes_function("llama_sampler_init_top_k", [ctypes.c_int32], llama_sampler_p_ctypes)
def llama_sampler_init_top_k(k: int) -> llama_sampler_p: ...
def llama_sampler_init_top_k(k: int, /) -> llama_sampler_p: ...


# /// @details Nucleus sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
Expand All @@ -4592,7 +4592,7 @@ def llama_sampler_init_top_k(k: int) -> llama_sampler_p: ...
[ctypes.c_float, ctypes.c_size_t],
llama_sampler_p_ctypes,
)
def llama_sampler_init_top_p(p: float, min_keep: int) -> llama_sampler_p: ...
def llama_sampler_init_top_p(p: float, min_keep: int, /) -> llama_sampler_p: ...


# /// @details Minimum P sampling as described in https://github.com/ggml-org/llama.cpp/pull/3841
Expand All @@ -4602,7 +4602,7 @@ def llama_sampler_init_top_p(p: float, min_keep: int) -> llama_sampler_p: ...
[ctypes.c_float, ctypes.c_size_t],
llama_sampler_p_ctypes,
)
def llama_sampler_init_min_p(p: float, min_keep: int) -> llama_sampler_p: ...
def llama_sampler_init_min_p(p: float, min_keep: int, /) -> llama_sampler_p: ...


# /// @details Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
Expand All @@ -4612,13 +4612,13 @@ def llama_sampler_init_min_p(p: float, min_keep: int) -> llama_sampler_p: ...
[ctypes.c_float, ctypes.c_size_t],
llama_sampler_p_ctypes,
)
def llama_sampler_init_typical(p: float, min_keep: int) -> llama_sampler_p: ...
def llama_sampler_init_typical(p: float, min_keep: int, /) -> llama_sampler_p: ...


# /// #details Updates the logits l_i` = l_i/t. When t <= 0.0f, the maximum logit is kept at it's original value, the rest are set to -inf
# LLAMA_API struct llama_sampler * llama_sampler_init_temp (float t);
@ctypes_function("llama_sampler_init_temp", [ctypes.c_float], llama_sampler_p_ctypes)
def llama_sampler_init_temp(t: float) -> llama_sampler_p: ...
def llama_sampler_init_temp(t: float, /) -> llama_sampler_p: ...


# /// @details Dynamic temperature implementation (a.k.a. entropy) described in the paper https://arxiv.org/abs/2309.02772.
Expand All @@ -4629,7 +4629,7 @@ def llama_sampler_init_temp(t: float) -> llama_sampler_p: ...
llama_sampler_p_ctypes,
)
def llama_sampler_init_temp_ext(
t: float, delta: float, exponent: float
t: float, delta: float, exponent: float, /
) -> llama_sampler_p: ...


Expand Down
51 changes: 51 additions & 0 deletions tests/test_llama_cpp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest

import llama_cpp

# ctypes function objects built without paramflags bind arguments
# positionally only; the trailing `/` on these bindings just keeps the
# type stub honest about that (#2371).


def test_llama_sampler_init_dist_rejects_keyword_args():
with pytest.raises(TypeError):
llama_cpp.llama_sampler_init_dist(seed=0)


def test_llama_sampler_init_top_k_rejects_keyword_args():
with pytest.raises(TypeError):
llama_cpp.llama_sampler_init_top_k(k=40)


def test_llama_sampler_init_top_p_rejects_keyword_args():
with pytest.raises(TypeError):
llama_cpp.llama_sampler_init_top_p(0.9, min_keep=1)


def test_llama_sampler_init_min_p_rejects_keyword_args():
with pytest.raises(TypeError):
llama_cpp.llama_sampler_init_min_p(0.1, min_keep=1)


def test_llama_sampler_init_typical_rejects_keyword_args():
with pytest.raises(TypeError):
llama_cpp.llama_sampler_init_typical(1.0, min_keep=1)


def test_llama_sampler_init_temp_rejects_keyword_args():
with pytest.raises(TypeError):
llama_cpp.llama_sampler_init_temp(t=0.8)


def test_llama_sampler_init_temp_ext_rejects_keyword_args():
with pytest.raises(TypeError):
llama_cpp.llama_sampler_init_temp_ext(0.8, 0.0, exponent=1.0)


def test_llama_sampler_init_top_p_silently_ignores_extra_keyword():
# Known limitation, not fixed here: with all positional slots filled,
# ctypes has no parameter names to match an extra keyword against, so
# it's silently ignored instead of raising.
sampler = llama_cpp.llama_sampler_init_top_p(0.9, 1, bogus=999)
assert sampler
llama_cpp.llama_sampler_free(sampler)