Repository navigation
[Bugfix][Router] loadaware: KV lookup for chat completions, fall back when the lookup fails - #1107
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces chat completions tokenization using the engine's /tokenize endpoint and adds fallback routing logic to handle tokenization or lookup failures gracefully. It also includes comprehensive unit tests for these changes. Feedback on the changes suggests adding defensive checks to handle non-list messages in _flatten_text_content and wrapping response.json() in a try-except block to handle non-JSON responses from the remote endpoint.
… when the lookup fails
LoadAwareRouter.tokenize_prompt only read request_json["prompt"], so every
/v1/chat/completions request was looked up with an empty token list and
loadaware never found a cache match for chat traffic.
- Tokenize chat requests through the engine's /tokenize with "messages"
(plus add_generation_prompt, tools and chat_template_kwargs), so the token
ids match what the engine prefills and caches.
- Flatten text-only list content into one string joined with "\n", as
vLLM's chat_utils does; vLLM's /tokenize rejects list content on text-only
models ("... is not a multimodal model").
- Raise a clear error when /tokenize returns no "tokens".
- route_request: when tokenization or the controller lookup fails, log a
warning and take the session/QPS fallback route instead of failing the
request with an HTTP 500.
Signed-off-by: Muhammad Ibrahim <ibrahimnd2000@gmail.com>
…rate malformed messages Address review feedback: - Wrap response.json() so a non-JSON /tokenize reply (e.g. an HTML error page from a proxy) raises an error naming the status code and body. - Return a non-list `messages` unchanged from _flatten_text_content so the engine's /tokenize validates it instead of the router raising TypeError. Both cases already ended in the fallback route; this makes the logged reason clear and avoids the TypeError. Adds tests for both. Signed-off-by: Muhammad Ibrahim <ibrahimnd2000@gmail.com>
Head branch was pushed to by a user without write access
df33910 to
c6322c1
Compare
|
Rebased onto main (behind by 1 → 0; the new commit, #1099, only touches cc: @ruizhang0101 Note |
Move the live request_stats read from the no-cache branch of route_request into fallback_url, so every fallback scores against the live load: no cache info, no endpoint selected, and the lookup-failure fallback added in vllm-project#1107, in either merge order. Add a cold-burst test (nothing cached anywhere) that fails when the fallback uses the arrival snapshot: 128/0/0 instead of 43/43/42. Signed-off-by: Muhammad Ibrahim <muhammad.ibrahim@multiversecomputing.com>
|
@ruizhang0101 thanks for the approval! The one red check is |
Resolve the conflict with vllm-project#1107, which added chat-completion tokenization to LoadAwareRouter.tokenize_prompt: KvawareRouter keeps the non-blocking prompt tokenizer from this branch, and LoadAwareRouter handles messages and defers plain prompts to it with super(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: David-Wu1119 <133224895+David-Wu1119@users.noreply.github.com>
Problem
LoadAwareRouter.tokenize_promptonly readsrequest_json["prompt"]. A/v1/chat/completionsrequest hasmessagesand noprompt, so it is tokenized as an empty string, the LMCache controller lookup matches nothing, and every chat request takes the session/QPS fallback route. KV-cache-aware placement never applies to chat traffic./tokenizereturns an error (for example while an engine restarts),response.json()["tokens"]raisesKeyError: 'tokens'and the client gets an HTTP 500. A controller lookup error does the same. A lookup is an optimisation, so neither should fail a request that could still be routed.Fix
messages, tokenize through the engine's/tokenizewithmessages,add_generation_prompt(defaultTrue) and, when present,toolsandchat_template_kwargs. The engine applies the model's own chat template, so the token ids match what it prefills and caches. The blocking HTTP call runs in an executor, as the existing remote fallback does.[{"type": "text", "text": ...}, ...]is joined into one string with"\n", the same way vLLM'schat_utilsrenders it before applying the template. vLLM's/tokenizerejects list content on text-only models ("... is not a multimodal model"), and some OpenAI-compatible benchmark clients always send list-style content. Messages containing any non-text part are sent unchanged. The client's request body is not mutated./tokenizereturns a body withouttokens, raise an error that includes the status code and body, instead of a bareKeyError.route_request, tokenization and the controller lookup are wrapped intry/except. On failure, log a warning and take the existingfallback_urlroute (session hash, else lowest QPS).The
prompt(completions) path is unchanged.Tests
New tests in
src/tests/test_loadaware_router.py(/tokenizemocked):/tokenizewithmessages,add_generation_prompt,toolsandchat_template_kwargs;"\n", and the request body is not mutated;tokensraises a clear error;route_requestfalls back (no exception) when/tokenizefails, and when the controller lookup fails;Seven of the new tests fail on
main.pre-commit(black, isort, ruff, codespell) passes.Validation
In a multi-replica deployment (GLM-5.x, 3–5 vLLM replicas with LMCache) serving chat completions:
Note
This PR and "loadaware: score bursts against live load" both add a test section at the same place in
test_loadaware_router.py. Whichever lands second needs a trivial rebase;routing_logic.pymerges cleanly.