Repository navigation
feat(server): add /v1/completions to the disaggregated router - #386
Merged
Merged
Conversation
The disaggregated router (`src/server/router_front.rs`) only served `POST /v1/chat/completions` and `GET /health`; the text-completion endpoint `POST /v1/completions` (served single-node by `routes::completions`) was unreachable through the router. This adds it (issue #200, follow-up from epic #116 / #126). Factor the shared tokenize -> select_prefill -> send `PrefillRequestFrame` body out of `route_chat` into a new `start_handoff(state, prompt, request_id, response_id, opts) -> Result<(prompt_tokens, rx)>`. Both the chat and the new completion handlers call it; the chat id scheme (`chatcmpl-<n>`) and routing are preserved unchanged, so the existing chat endpoint stays byte-identical to before. `start_handoff` applies the same `add_special` rule the worker uses (`!prompt.starts_with("<bos>") && !prompt.starts_with("<s>")`) and removes the registered `pending` channel on any pre-send failure. The new `router_completions` / `route_completion` handlers reuse the EXISTING single-node serializers so the output is structurally byte-identical to single-node `/v1/completions` for the same prompt and sampling: non-streaming builds `CompletionResponse::new_with_logprobs` (object `text_completion`, choices[index, text, finish_reason, logprobs], usage), and streaming emits `CompletionChunk::content` per token, then `CompletionChunk::finish`, an optional `CompletionChunk::usage` chunk when `stream_options.include_usage` is set, and the `[DONE]` sentinel. `finish_reason` mirrors the worker exactly (`completion_tokens >= max_tokens` -> "length" else "stop", per `model_worker.rs`), and `prompt_tokens` comes from the router's own tokenization. Per-token logprobs are rejected with a 400 because the disaggregated result frames carry detokenized text only and cannot reproduce them; `n` and `echo` are silently ignored exactly as the single-node path does. Register `POST /v1/completions` in `create_router_app`. Add `disaggregated_router_completions_match_single_node` to `tests/disaggregated_router_e2e.rs`: it captures single-node `/v1/completions` output (the baseline is started with `--alias` so its model id matches the router's echoed `model`), then drives the three-process router stack and asserts the non-streaming JSON body and the streaming SSE chunk sequence are byte-identical after normalizing only the volatile `id` / `created` fields. Run with `cargo test --test disaggregated_router_e2e --release --features metal,accelerate disaggregated_router_completions_match_single_node -- --ignored --nocapture`.
…age chunk on success Reject response_format (structured output) and explicit reasoning/thinking budgets on the disaggregated /v1/completions, returning a 400 like the existing logprobs guard. The PrefillRequestFrame carries only sampling and max_tokens, so the worker-side controls these options drive (a structured-output constraint, reasoning-budget enforcement) cannot be reproduced on the router path. Returning 200 with unconstrained or un-budgeted output would silently diverge from single-node and break the byte-identity criterion, so the router rejects the request instead. Requests with no budget alias set are unaffected and the default unbounded path keeps working. Gate the streaming usage chunk on success: only send the CompletionChunk::usage chunk when include_usage is true and the handoff result is Ok, matching single-node stream_completion which guards on `if include_usage && let Ok(ref r) = result`. The finish chunk and the [DONE] sentinel are left exactly as they were. Document the byte-fallback completion_tokens limitation: completion_tokens is counted from emitted detokenized text pieces (one on_token call per ResultFrame text entry), which equals the worker's generated-token count for byte-level-BPE tokenizers (e.g. Qwen) but may under-count for byte-fallback tokenizers (e.g. Gemma <0xXX> byte sequences), and can consequently flip finish_reason between length and stop. A precise fix requires the disaggregated wire protocol to carry the worker's token count and is deferred to a follow-up.
Add POST /v1/completions to the router's scope in CONTINUOUS_BATCHING.md, alongside the pre-existing POST /v1/chat/completions entry. Document the two wire-protocol limitations specific to the text-completion path: stop sequences are not forwarded (pre-existing, shared with the chat path), and completion_tokens is counted from detokenized text frames, which can under-count for byte-fallback tokenizers (Gemma). Also document the three unsupported option groups that return HTTP 400 (logprobs, response_format, reasoning/thinking budgets) and correct the outdated note about the stream filter, which now actively suppresses structural markers and routes reasoning_content.
Member
Author
Finalization completeDocumentation (
Tests: no new unit tests added. The 400-guard conditions reduce to Lint/format: Commit: |
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…#386) * feat(server): add /v1/completions to the disaggregated router The disaggregated router (`src/server/router_front.rs`) only served `POST /v1/chat/completions` and `GET /health`; the text-completion endpoint `POST /v1/completions` (served single-node by `routes::completions`) was unreachable through the router. This adds it (issue lablup#200, follow-up from epic lablup#116 / lablup#126). Factor the shared tokenize -> select_prefill -> send `PrefillRequestFrame` body out of `route_chat` into a new `start_handoff(state, prompt, request_id, response_id, opts) -> Result<(prompt_tokens, rx)>`. Both the chat and the new completion handlers call it; the chat id scheme (`chatcmpl-<n>`) and routing are preserved unchanged, so the existing chat endpoint stays byte-identical to before. `start_handoff` applies the same `add_special` rule the worker uses (`!prompt.starts_with("<bos>") && !prompt.starts_with("<s>")`) and removes the registered `pending` channel on any pre-send failure. The new `router_completions` / `route_completion` handlers reuse the EXISTING single-node serializers so the output is structurally byte-identical to single-node `/v1/completions` for the same prompt and sampling: non-streaming builds `CompletionResponse::new_with_logprobs` (object `text_completion`, choices[index, text, finish_reason, logprobs], usage), and streaming emits `CompletionChunk::content` per token, then `CompletionChunk::finish`, an optional `CompletionChunk::usage` chunk when `stream_options.include_usage` is set, and the `[DONE]` sentinel. `finish_reason` mirrors the worker exactly (`completion_tokens >= max_tokens` -> "length" else "stop", per `model_worker.rs`), and `prompt_tokens` comes from the router's own tokenization. Per-token logprobs are rejected with a 400 because the disaggregated result frames carry detokenized text only and cannot reproduce them; `n` and `echo` are silently ignored exactly as the single-node path does. Register `POST /v1/completions` in `create_router_app`. Add `disaggregated_router_completions_match_single_node` to `tests/disaggregated_router_e2e.rs`: it captures single-node `/v1/completions` output (the baseline is started with `--alias` so its model id matches the router's echoed `model`), then drives the three-process router stack and asserts the non-streaming JSON body and the streaming SSE chunk sequence are byte-identical after normalizing only the volatile `id` / `created` fields. Run with `cargo test --test disaggregated_router_e2e --release --features metal,accelerate disaggregated_router_completions_match_single_node -- --ignored --nocapture`. * fix(server): reject unsupported completion options and gate router usage chunk on success Reject response_format (structured output) and explicit reasoning/thinking budgets on the disaggregated /v1/completions, returning a 400 like the existing logprobs guard. The PrefillRequestFrame carries only sampling and max_tokens, so the worker-side controls these options drive (a structured-output constraint, reasoning-budget enforcement) cannot be reproduced on the router path. Returning 200 with unconstrained or un-budgeted output would silently diverge from single-node and break the byte-identity criterion, so the router rejects the request instead. Requests with no budget alias set are unaffected and the default unbounded path keeps working. Gate the streaming usage chunk on success: only send the CompletionChunk::usage chunk when include_usage is true and the handoff result is Ok, matching single-node stream_completion which guards on `if include_usage && let Ok(ref r) = result`. The finish chunk and the [DONE] sentinel are left exactly as they were. Document the byte-fallback completion_tokens limitation: completion_tokens is counted from emitted detokenized text pieces (one on_token call per ResultFrame text entry), which equals the worker's generated-token count for byte-level-BPE tokenizers (e.g. Qwen) but may under-count for byte-fallback tokenizers (e.g. Gemma <0xXX> byte sequences), and can consequently flip finish_reason between length and stop. A precise fix requires the disaggregated wire protocol to carry the worker's token count and is deferred to a follow-up. * docs(server): document /v1/completions on the disaggregated router Add POST /v1/completions to the router's scope in CONTINUOUS_BATCHING.md, alongside the pre-existing POST /v1/chat/completions entry. Document the two wire-protocol limitations specific to the text-completion path: stop sequences are not forwarded (pre-existing, shared with the chat path), and completion_tokens is counted from detokenized text frames, which can under-count for byte-fallback tokenizers (Gemma). Also document the three unsupported option groups that return HTTP 400 (logprobs, response_format, reasoning/thinking budgets) and correct the outdated note about the stream filter, which now actively suppresses structural markers and routes reasoning_content.
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.
Summary
Add
POST /v1/completionsto the disaggregated router (src/server/router_front.rs), which previously served onlyPOST /v1/chat/completionsandGET /health. The new path reuses the single-node completion serializers so its output is byte-identical to single-node/v1/completionsfor the same prompt and sampling. Follow-up from epic #116 / #126.What changed
PrefillRequestFrame) out ofroute_chatintostart_handoff(state, prompt, request_id, response_id, opts) -> Result<(prompt_tokens, rx)>. Both the chat and the completion handlers call it. The chat id scheme (chatcmpl-<n>), routing, and tokenization are unchanged, so the existing chat endpoint stays byte-identical to before this PR.start_handoffapplies the sameadd_specialrule the worker uses (!prompt.starts_with("<bos>") && !prompt.starts_with("<s>")) and removes the registeredpendingchannel on any pre-send failure.router_completions/route_completion. They reuse the EXISTING single-node serializers (no hand-rolled completion shape): non-streaming returnsCompletionResponse::new_with_logprobs(objecttext_completion,choices[index, text, finish_reason, logprobs],usage); streaming emitsCompletionChunk::contentper token, thenCompletionChunk::finish, an optionalCompletionChunk::usagechunk whenstream_options.include_usageis set, then the[DONE]sentinel, matchingroutes::completionschunk-for-chunk.finish_reasonmirrors the worker exactly:completion_tokens >= max_tokens-> "length" else "stop" (same logic assrc/server/model_worker.rs).prompt_tokensfor theusageblock comes from the router's own tokenization (equal to the worker's count under the sharedadd_specialrule).400 invalid_request_errorbecause the disaggregatedResultFrames carry detokenized text only and cannot reproduce them;nandechoare not inCompletionRequest, so they are silently ignored exactly as the single-node path does.POST /v1/completionsincreate_router_appand update the module / factory docs.disaggregated_router_completions_match_single_nodeintests/disaggregated_router_e2e.rscovering both the non-streaming JSON body and the streaming SSE chunk sequence.Parity notes
CompletionResponse/CompletionChunktypes, andserde_jsonis built withpreserve_order, so field order matches. The only inherently volatile fields areid(cmpl-<uuid>, same format as single-node) andcreated(timestamp); the test normalizes just these two before comparing.PrefillRequestFramedoes not carry stop sequences, sostopis not honored on the disaggregated path. The byte-identity claim holds for requests without stop sequences.Test plan
cargo check -p mlxcel --lib --testscargo clippy -p mlxcel --lib --tests -- -D warningscargo fmt --check -p mlxcelcargo test --test disaggregated_router_e2e --release --features metal,accelerate disaggregated_router_completions_match_single_node -- --ignored --nocapture(heavy; spawns four realmlxcel-serverprocesses loading qwen3-0.6b-4bit, run by the orchestrator)Closes #200