[PR #1916] [CLOSED] OpenAI logprobs on /v1/chat/completions for the CUDA/HIP engine #3757

Closed
opened 2026-11-07 15:30:32 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1916
Author: @gt50
Created: 10/11/2026
Status: ❌ Closed

Base: main ← Head: logprobs


📝 Commits (3)

  • 92ebad2 OpenAI logprobs on /v1/chat/completions for the CUDA/HIP engine
  • 7e1a00d serve: logprobs with "parallel" (batch slots) for one-token decisions; 400 for longer replies
  • 4098d58 serve: the logprobs 400 with "parallel" only for real batch slots, not for replicas without them

📊 Changes

5 files changed (+638 additions, -16 deletions)

View changed files

📝 docs/DETAILS.md (+17 -0)
📝 serve/server.py (+216 -16)
➕ serve/test_logprobs.py (+271 -0)
➕ serve/test_logprobs_parallel.py (+101 -0)
📝 src/program/generate.cpp (+33 -0)

📄 Description

Title

OpenAI logprobs on /v1/chat/completions for the CUDA/HIP engine

Summary

"logprobs": true (with "top_logprobs": 0..20) on /v1/chat/completions now returns choices[0].logprobs.content, as OpenAI and llama-server do. Until now only the SYCL port had it (835d30b); the CUDA/HIP engine and serve/ accepted the field and returned nothing. A client that marks values the model was unsure of (the weakest token of a value under 50%) needs it.

What changed

  • Engine (src/program/generate.cpp): the GEN key logprobs=K writes LP <logprob> [id:logprob ...] after each T <id>, from the verify window's head logits (Verifier::copy_logits), before penalties, temperature and sampling. It is the SYCL port's code, placed at both token outputs (the plain and the pipelined path). Without the key, no LP lines and nothing else changes.
  • serve/server.py:
    • sampling_keys sends logprobs=K only when the request asks; an engine without LP lines ignores the key and the reply has no logprobs.
    • StrataEngine.generate reads a token's LP line before it yields the token (the engine writes both in the same flush), so the logprobs belong to the right text chunk. When nothing follows a token within 0.5 s, it is yielded without one. A line read this way is handed to the early-stop drain, so a DONE read after the last token is not waited for again (tested).
    • Each content / reasoning chunk carries the entries of the tokens that spell its text. token is the text the token completes (an incremental UTF-8 decode, as the reply's text is made): a character split across tokens is in the token that finishes it, so the tokens, joined, spell the reply. bytes are the token's own bytes. Tokens that make no text (</think> and the newlines after it) and the stop token are left out.
    • openai_collect merges them into choices[0].logprobs (content, refusal, and reasoning_content with thinking on).
    • response_format json_schema: with logprobs, the answer is returned as the model wrote it (validated as before, the same value) when its tokens spell it, including the JSON inside a ```json fence; otherwise the canonical JSON without logprobs. Without logprobs, the canonical JSON as before.
  • docs/DETAILS.md: a "Logprobs" paragraph after the sampling one.
  • serve/test_logprobs.py: the request keys, the engine line pairing (with and without LP lines, not asked, early stop), the API streamed and not (multi-byte characters, top_logprobs, the stop token), thinking, json_schema with a fenced answer, and the span helper.

Extra Notes

  • Default path: a request without logprobs sends the same GEN line, reads the same lines and returns the same reply (tested). The engine prints LP lines only when asked.
  • Measured on a Radeon 8060S (Ryzen AI Max+ 395, Windows 11, UD-IQ4_XS, --spec 4 --mtp), this branch built with tools/hip/build_windows.bat (gfx1151, engine 0.1.42):
    • A greedy 255-token answer: the same text with and without logprobs, 38.5 and 38.1 tok/s; 254 entries (the stop token left out).
    • {"rent": "$1,250", "quote": "Rent is $1,250 a month."} from a two-field json_schema at temperature 0: 26 tokens that spell the content.
    • Thinking on: content and reasoning_content each spelled by their tokens. A streamed json_object answer and a sentence with café, 東京 and €5: spelled too.
    • The same patch on 0.1.40.2 gave the same results; that build is what I run day to day.
  • Not covered: the Responses and Anthropic APIs, and --parallel batch slots (their BT lines get no LP lines). The legacy completions form "logprobs": N is taken as top_logprobs N.
  • Tests: all serve test files pass on main 61b3fb5d (704 tests).

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/1916 **Author:** [@gt50](https://github.com/gt50) **Created:** 10/11/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `logprobs` --- ### 📝 Commits (3) - [`92ebad2`](https://github.com/Niko1221/Strata/commit/92ebad2006492c10cdb38eb85e7b9de59cd0dd93) OpenAI logprobs on /v1/chat/completions for the CUDA/HIP engine - [`7e1a00d`](https://github.com/Niko1221/Strata/commit/7e1a00d8ac545d304f19fb190a421b3c2ef2d2bb) serve: logprobs with "parallel" (batch slots) for one-token decisions; 400 for longer replies - [`4098d58`](https://github.com/Niko1221/Strata/commit/4098d58571cf75694d9f2cffa2a19e7d54462650) serve: the logprobs 400 with "parallel" only for real batch slots, not for replicas without them ### 📊 Changes **5 files changed** (+638 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+17 -0) 📝 `serve/server.py` (+216 -16) ➕ `serve/test_logprobs.py` (+271 -0) ➕ `serve/test_logprobs_parallel.py` (+101 -0) 📝 `src/program/generate.cpp` (+33 -0) </details> ### 📄 Description ## Title OpenAI logprobs on /v1/chat/completions for the CUDA/HIP engine ## Summary `"logprobs": true` (with `"top_logprobs": 0..20`) on `/v1/chat/completions` now returns `choices[0].logprobs.content`, as OpenAI and llama-server do. Until now only the SYCL port had it (835d30b); the CUDA/HIP engine and `serve/` accepted the field and returned nothing. A client that marks values the model was unsure of (the weakest token of a value under 50%) needs it. ## What changed - **Engine (`src/program/generate.cpp`):** the GEN key `logprobs=K` writes `LP <logprob> [id:logprob ...]` after each `T <id>`, from the verify window's head logits (`Verifier::copy_logits`), before penalties, temperature and sampling. It is the SYCL port's code, placed at both token outputs (the plain and the pipelined path). Without the key, no LP lines and nothing else changes. - **`serve/server.py`:** - `sampling_keys` sends `logprobs=K` only when the request asks; an engine without LP lines ignores the key and the reply has no `logprobs`. - `StrataEngine.generate` reads a token's LP line before it yields the token (the engine writes both in the same flush), so the logprobs belong to the right text chunk. When nothing follows a token within 0.5 s, it is yielded without one. A line read this way is handed to the early-stop drain, so a `DONE` read after the last token is not waited for again (tested). - Each content / reasoning chunk carries the entries of the tokens that spell its text. `token` is the text the token completes (an incremental UTF-8 decode, as the reply's text is made): a character split across tokens is in the token that finishes it, so the tokens, joined, spell the reply. `bytes` are the token's own bytes. Tokens that make no text (`</think>` and the newlines after it) and the stop token are left out. - `openai_collect` merges them into `choices[0].logprobs` (`content`, `refusal`, and `reasoning_content` with thinking on). - `response_format` `json_schema`: with `logprobs`, the answer is returned as the model wrote it (validated as before, the same value) when its tokens spell it, including the JSON inside a ```` ```json ```` fence; otherwise the canonical JSON without `logprobs`. Without `logprobs`, the canonical JSON as before. - **`docs/DETAILS.md`:** a "Logprobs" paragraph after the sampling one. - **`serve/test_logprobs.py`:** the request keys, the engine line pairing (with and without LP lines, not asked, early stop), the API streamed and not (multi-byte characters, `top_logprobs`, the stop token), thinking, `json_schema` with a fenced answer, and the span helper. ## Extra Notes - **Default path:** a request without `logprobs` sends the same GEN line, reads the same lines and returns the same reply (tested). The engine prints LP lines only when asked. - **Measured** on a Radeon 8060S (Ryzen AI Max+ 395, Windows 11, UD-IQ4_XS, `--spec 4 --mtp`), this branch built with `tools/hip/build_windows.bat` (gfx1151, engine 0.1.42): - A greedy 255-token answer: the same text with and without `logprobs`, 38.5 and 38.1 tok/s; 254 entries (the stop token left out). - `{"rent": "$1,250", "quote": "Rent is $1,250 a month."}` from a two-field `json_schema` at temperature 0: 26 tokens that spell the content. - Thinking on: `content` and `reasoning_content` each spelled by their tokens. A streamed `json_object` answer and a sentence with `café`, `東京` and `€5`: spelled too. - The same patch on 0.1.40.2 gave the same results; that build is what I run day to day. - **Not covered:** the Responses and Anthropic APIs, and `--parallel` batch slots (their `BT` lines get no LP lines). The legacy completions form `"logprobs": N` is taken as `top_logprobs` N. - **Tests:** all `serve` test files pass on `main` 61b3fb5d (704 tests). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:30:32 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3757
No description provided.