Skip to content

fix: return token logprobs for chat completions that set logprobs without top_logprobs - #2484

Open
sohailshk wants to merge 1 commit into
abetlen:mainfrom
sohailshk:fix/chat-logprobs-without-top-logprobs
Open

sohailshk wants to merge 1 commit into
abetlen:mainfrom
sohailshk:fix/chat-logprobs-without-top-logprobs

Conversation

@sohailshk

Copy link
Copy Markdown

create_chat_completion(logprobs=True) without top_logprobs returns "logprobs": null. The OpenAI API returns the logprob of each generated token in that case, with an empty top_logprobs list. Only requests that also set top_logprobs got logprobs here. The server's chat endpoint passes logprobs/top_logprobs through to the same handlers.

llm = Llama("stories260K.gguf", logits_all=True, n_ctx=256)
msgs = [{"role": "user", "content": "hi"}]
llm.create_chat_completion(messages=msgs, max_tokens=3, logprobs=True)["choices"][0]["logprobs"]
# main: None      expected: {"content": [3 entries], "refusal": None}
llm.create_chat_completion(messages=msgs, max_tokens=3, logprobs=True, top_logprobs=0)["choices"][0]["logprobs"]
# main: 3 entries (works)

The chat handlers pass logprobs=top_logprobs if logprobs else None to create_completion, which treats None as "no logprobs". With top_logprobs unset that expression is None. The same line is in the generic formatter handler, Llava15ChatHandler, MTMDChatHandler and chatml_function_calling (two places).

The five call sites now pass (top_logprobs or 0) if logprobs else None, so logprobs=True alone behaves like logprobs=True, top_logprobs=0. logprobs=False and unset behave as before. Two things follow from that, both already true for top_logprobs=0 today:

  • Each token's top_logprobs list has one entry, the sampled token itself, not an empty list (create_completion always adds the sampled token).
  • A model loaded with logits_all=False (the Llama default) now raises ValueError: logprobs is not supported for models created with logits_all=False for logprobs=True, where it used to return None. The server defaults to logits_all=True.

Tests: pytest tests/test_llama_chat_format.py passes (3); the new test fails on main (None == 0). Checked end to end with stories260K.gguf on macOS: logprobs=True gave None before and 3 entries after, also when streaming; top_logprobs=0/2 and logprobs=False are unchanged. The server was not run. ruff check and ruff format --check are clean. A changelog line is added under Unreleased.

The implementation and tests were written with AI assistance.

…hout top_logprobs

The chat handlers passed top_logprobs straight to create_completion as logprobs, so logprobs=true with top_logprobs unset became logprobs=None and the response had logprobs: null. Request zero extra candidates in that case so the sampled token's logprob is returned, as the OpenAI API does.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant