Repository navigation
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #682
/v1/completions serves n from 1 to 128: each prompt is fanned out into n ordinary engine requests, and their outputs come back as n choices, streamed or not. The engine is unchanged, and chat still refuses n != 1. Builds on #842, which this replaces the refusal of; until that merges, the diff here also shows its commit, and the change in this PR is the last one. Opened as a draft: it has not been run on a GPU yet.
Summary
After #842, completions refuse n != 1. This PR makes them serve it, following OpenAI's conventions: choices are ordered prompt-major (index = prompt_index * n + j); usage.prompt_tokens counts each prompt once and completion_tokens is summed over all choices; when streaming, every chunk carries its choice index, each choice gets exactly one finish_reason chunk, then one usage chunk (with include_usage) and a single [DONE]; n < 1 or n > 128 (OpenAI's own ceiling) is a 400 with param "n".
Open choices I would like a maintainer's view on: the cap of 128 (an alternative is tying it to --max-running-requests); cached_tokens is reported from choice 0 only, so it does not show the reuse by choices 1..n-1; prompts in a list still run one after another, as before, and only the n choices of one prompt run concurrently.
Known limitations: Starlette skips a streaming response's background task on a client disconnect under ASGI spec >= 2.4, so a stream that never starts leaves choice 0 running there (uvicorn, which FreeToken runs on, reports 2.3; the same gap exists for n = 1 on main). A non-streaming client that disconnects does not cancel its generations, also unchanged from main. Each abort of a never-submitted uid leaves a scheduler tombstone, at most n - 1 per request that ends early; the scheduler already caps them at 65,536.
Why now: clients that pass n to completions (best-of selection, self-consistency, eval harnesses) otherwise have to send n separate requests, each prefilling the same prompt. Fanning out on the server keeps the OpenAI request shape and prefills the prompt once.
Tests
In tests/server/test_openai_api.py, with a small fake state (MultiUserState) that hands out one reply list per uid and logs sends, replies and aborts. test_completion_refuses_n_other_than_one from #842 becomes test_completion_refuses_n_out_of_range, for n in {0, 129}, streamed and not. New:
Each of these fails if the behaviour it covers is removed.
Test environment
Unit tests
This branch: 20 passed, 17 failed, 1 error. Main: 11 passed, 17 failed, 1 error. The 9 extra passes are the tests above. The 17 failures and the error are all "ModuleNotFoundError: No module named 'flashlib'", which needs CUDA, and are the same on main.
End-to-end A/B against main
Not run yet: it needs a GPU, which I don't have access to yet. Once I do, I will run these and add the results here before taking this out of draft:
/v1/completionsacceptsnand silently returns one choice;/v1/chat/completionsrefusesn != 1with 400 #682 (python repro_completions_n_ignored.py --base-url http://127.0.0.1:1941): on this branch every row should PASS, with choices [0, 1] / [0, 1, 2];