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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- fix: load cached pretrained models in offline mode by @emme1t in #2396
- fix(ci): install matching NVML headers for CUDA wheel builds
- fix: return token logprobs for chat completions that set logprobs without top_logprobs

## [0.3.38]

Expand Down
10 changes: 5 additions & 5 deletions llama_cpp/llama_chat_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ def chat_completion_handler(
top_k=top_k,
min_p=min_p,
typical_p=typical_p,
logprobs=top_logprobs if logprobs else None,
logprobs=(top_logprobs or 0) if logprobs else None,
stream=stream,
stop=stop,
seed=seed,
Expand Down Expand Up @@ -3096,7 +3096,7 @@ def __call__(
top_k=top_k,
min_p=min_p,
typical_p=typical_p,
logprobs=top_logprobs if logprobs else None,
logprobs=(top_logprobs or 0) if logprobs else None,
stream=stream,
stop=stop,
seed=seed,
Expand Down Expand Up @@ -3631,7 +3631,7 @@ def raise_exception(message: str):
top_k=top_k,
min_p=min_p,
typical_p=typical_p,
logprobs=top_logprobs if logprobs else None,
logprobs=(top_logprobs or 0) if logprobs else None,
stream=stream,
stop=stop,
seed=seed,
Expand Down Expand Up @@ -4288,7 +4288,7 @@ def chatml_function_calling(
model=model,
logits_processor=logits_processor,
grammar=grammar,
logprobs=top_logprobs if logprobs else None,
logprobs=(top_logprobs or 0) if logprobs else None,
),
stream=stream,
)
Expand Down Expand Up @@ -4401,7 +4401,7 @@ def chatml_function_calling(
typical_p=typical_p,
stream=stream,
stop=["<|im_end|>"],
logprobs=top_logprobs if logprobs else None,
logprobs=(top_logprobs or 0) if logprobs else None,
max_tokens=None,
presence_penalty=presence_penalty,
frequency_penalty=frequency_penalty,
Expand Down
45 changes: 45 additions & 0 deletions tests/test_llama_chat_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,48 @@ def test_hf_tokenizer_config_str_to_chat_formatter():
)

assert chat_formatter_respoonse.prompt == ("<s>[INST] Hello, world! [/INST]</s>")


class _RecordingLlama:
verbose = False

def __init__(self):
self.kwargs = {}

def tokenize(self, text, add_bos=True, special=False):
return [1]

def create_completion(self, **kwargs):
self.kwargs = kwargs
return {
"id": "cmpl-1",
"object": "text_completion",
"created": 0,
"model": "m",
"choices": [
{"text": "a", "index": 0, "logprobs": None, "finish_reason": "stop"}
],
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
}


def test_chat_logprobs_without_top_logprobs_requests_token_logprobs():
handler = llama_chat_format.Jinja2ChatFormatter(
template="{% for m in messages %}{{ m['content'] }}{% endfor %}",
eos_token="</s>",
bos_token="<s>",
).to_chat_handler()
messages = [ChatCompletionRequestUserMessage(role="user", content="hi")]

llama = _RecordingLlama()
handler(llama=llama, messages=messages, logprobs=True)
assert llama.kwargs["logprobs"] == 0

handler(llama=llama, messages=messages, logprobs=True, top_logprobs=3)
assert llama.kwargs["logprobs"] == 3

handler(llama=llama, messages=messages, logprobs=False, top_logprobs=3)
assert llama.kwargs["logprobs"] is None

handler(llama=llama, messages=messages)
assert llama.kwargs["logprobs"] is None