Repository navigation
[llm][kv][12/N] Transmit tokens out-of-band and skip tokenization at the engine - #65095
Conversation
…returning calls _svc.delete_worker's Rust binding returns a future, not a coroutine, so passing it straight to create_task raised TypeError. _schedule now wraps whatever it is given in a coroutine, keeping the call sites direct. Signed-off-by: Jeffrey Wang <[email protected]>
Pure relocation of the tokenizer module (and its unit test) from core/ingress to routing_policies/kv_aware, updating import paths. No logic change; the vLLM-specific rewrite follows in the next commit. Signed-off-by: Jeffrey Wang <[email protected]>
…s vLLM renderer Replace the per-request /tokenize RPC with an in-process tokenizer that drives the engine's own vLLM OnlineRenderer: render_chat for chat (the engine's own entry point, covering the HF chat template, Harmony for gpt_oss, and Mistral) and preprocess_completion for text prompts. Routing token ids are therefore byte-identical to the replica's prefill tokens without contending with inference on the replica processes. The vLLM renderer imports live at module top (OnlineRenderer needs vLLM >= 0.25); the ingress router imports the tokenizer lazily so non-KV paths never pull the renderer in. Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
…to KVTokenTracker The KVTokenTracker (formerly KVRouterActor) is built as a plain object by the LLMRouter ingress replica and shared via a module global, so KVAwareRouter.choose_replicas selects with a local call rather than an actor RPC hop. Engine replicas book request lifecycle events through the LLMRouter handle's on_lifecycle_events method instead of a named deployment actor, so the DeploymentActorConfig attachment is dropped. Selection-service tests (KV-event ingestion, scoring, lifecycle tracking) are kept and adapted to the new hosting. Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
…gress replicas are supported Signed-off-by: Jeffrey Wang <[email protected]>
…eplicas to share token load Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
# Conflicts: # python/ray/llm/_internal/serve/core/ingress/router.py # python/ray/llm/_internal/serve/routing_policies/kv_aware/kv_token_tracker.py # python/ray/llm/_internal/serve/routing_policies/kv_aware/vllm/token_tracking.py # python/ray/llm/tests/serve/cpu/deployments/routers/kv/test_kv_token_tracker.py # python/ray/llm/tests/serve/cpu/deployments/routers/kv/test_request_lifecycle_tracking.py # python/ray/llm/tests/serve/cpu/deployments/routers/kv/test_tokenizer.py # python/ray/llm/tests/serve/cpu/deployments/routers/kv/test_tokenizer_exactness.py # release/llm_tests/kv_router_test/test_request_lifecycle_tracking.py # release/llm_tests/kv_router_test/utils.py # release/ray_release/byod/byod_llm_kv_router_test.sh
Signed-off-by: Jeffrey Wang <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces a best-effort, replica-local ZMQ side channel to forward pre-routing prompt token IDs from the LLMRouter to the selected LLMServer replica, avoiding redundant tokenization. The changes include implementing a TokenSender, TokenStore, and TokenReceiver, integrating them into the router and vLLM engine, and updating HAProxy templates to securely forward the token key header. The review feedback highlights several critical robustness and performance improvements in the new token channel implementation: handling general exceptions in the sender to prevent router crashes, wrapping token decoding in try-except blocks to avoid 500 errors on corrupt payloads, handling ZMQ context termination in the receiver loop to prevent CPU thrashing, and simplifying the TokenStore by removing redundant asyncio locks and async overhead.
Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
…e completion races Signed-off-by: Jeffrey Wang <[email protected]>
dbaeb54 to
55e4e24
Compare
Signed-off-by: Jeffrey Wang <[email protected]>
# Conflicts: # python/ray/llm/_internal/serve/routing_policies/kv_aware/kv_token_tracker.py # python/ray/llm/_internal/serve/routing_policies/kv_aware/vllm/token_tracking.py # python/ray/llm/tests/serve/cpu/deployments/routers/kv/test_kv_token_tracker.py # python/ray/llm/tests/serve/cpu/deployments/routers/kv/test_request_lifecycle_tracking.py # release/llm_tests/kv_router_test/test_request_lifecycle_tracking.py # release/llm_tests/kv_router_test/test_token_load_state.py # release/llm_tests/kv_router_test/utils.py
Signed-off-by: Jeffrey Wang <[email protected]>
…te cross-node Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
…elivery validation Signed-off-by: Jeffrey Wang <[email protected]>
kouroshHakha
left a comment
There was a problem hiding this comment.
nit but otherwise good.
93e248d to
a684bba
Compare
Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
1f36120 to
af49ebc
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit af49ebc. Configure here.
…the engine (ray-project#65095) Signed-off-by: Jeffrey Wang <[email protected]> Signed-off-by: kv-benchmark <[email protected]>
…the engine (ray-project#65095) Signed-off-by: Jeffrey Wang <[email protected]> Signed-off-by: kv-benchmark <[email protected]>
…the engine (ray-project#65095) Signed-off-by: Jeffrey Wang <[email protected]> Signed-off-by: kv-benchmark <[email protected]> Signed-off-by: 400Ping <[email protected]>
## Why The current 256 KiB cap truncates the router copy of large LLM requests, causing body-aware routing to lose information. Several recent routing changes depend on this body: - [#64224](#64224), [#64328](#64328), and [#65095](#65095) use it for KV-aware routing. - [#65680](#65680) uses it for LoRA model selection. - [#66258](#66258) and [#66315](#66315) use it for deployment-aware, multi-model direct streaming. - [#63183](#63183) introduced the body-forwarding gate shared by these paths. This PR raises the default buffer to 8 MiB and lowers the default `maxconn` to 4,096. The sizing and behavior were verified with a real HAProxy 2.8.25 test and against the bundled HAProxy source code. Both settings retain their existing environment-variable overrides. | Consideration | Result | |---|---| | Request size | A one-million-token English request is commonly estimated at roughly 4 MB. The test request is 5.72 MiB, so 8 MiB leaves 2.28 MiB (~40%) headroom. | | Latency | Requests below 256 KiB do not wait longer. Requests from 256 KiB to 8 MiB now wait for the complete body before routing. | | Memory | Buffers are allocated dynamically. A live stream can use roughly 16 MiB across request and response buffers. Lowering `maxconn` from 20,000 to 4,096 reduces the theoretical buffer ceiling from 320 GiB to 64 GiB. | ## Changes - Raise `RAY_SERVE_HAPROXY_INGRESS_REQUEST_ROUTER_BUFSIZE` from 256 KiB to 8 MiB. - Lower `RAY_SERVE_HAPROXY_MAXCONN` from 20,000 to 4,096. - Test a 6,000,014-byte, million-token-sized request end to end. - Derive rendered-config assertions from their constants so environment overrides remain testable. - Update the KV-aware routing guide. ## Tests - Pre-commit hooks, Python compilation, Serve mypy, and Serve pyrefly passed. - HAProxy end-to-end test passed: 1 passed, 37 deselected in 2.85s using bundled HAProxy 2.8.25. This does not duplicate the related PRs: they introduce or consume body-aware routing, while this PR increases the shared buffer default and tests a million-token-sized body. AI assistance was used to implement and validate this change; the submitter reviewed every changed line. --------- Signed-off-by: Seiji Eicher <[email protected]> Signed-off-by: Seiji Eicher <[email protected]>
…t#66360) ## Why The current 256 KiB cap truncates the router copy of large LLM requests, causing body-aware routing to lose information. Several recent routing changes depend on this body: - [ray-project#64224](ray-project#64224), [ray-project#64328](ray-project#64328), and [ray-project#65095](ray-project#65095) use it for KV-aware routing. - [ray-project#65680](ray-project#65680) uses it for LoRA model selection. - [ray-project#66258](ray-project#66258) and [ray-project#66315](ray-project#66315) use it for deployment-aware, multi-model direct streaming. - [ray-project#63183](ray-project#63183) introduced the body-forwarding gate shared by these paths. This PR raises the default buffer to 8 MiB and lowers the default `maxconn` to 4,096. The sizing and behavior were verified with a real HAProxy 2.8.25 test and against the bundled HAProxy source code. Both settings retain their existing environment-variable overrides. | Consideration | Result | |---|---| | Request size | A one-million-token English request is commonly estimated at roughly 4 MB. The test request is 5.72 MiB, so 8 MiB leaves 2.28 MiB (~40%) headroom. | | Latency | Requests below 256 KiB do not wait longer. Requests from 256 KiB to 8 MiB now wait for the complete body before routing. | | Memory | Buffers are allocated dynamically. A live stream can use roughly 16 MiB across request and response buffers. Lowering `maxconn` from 20,000 to 4,096 reduces the theoretical buffer ceiling from 320 GiB to 64 GiB. | ## Changes - Raise `RAY_SERVE_HAPROXY_INGRESS_REQUEST_ROUTER_BUFSIZE` from 256 KiB to 8 MiB. - Lower `RAY_SERVE_HAPROXY_MAXCONN` from 20,000 to 4,096. - Test a 6,000,014-byte, million-token-sized request end to end. - Derive rendered-config assertions from their constants so environment overrides remain testable. - Update the KV-aware routing guide. ## Tests - Pre-commit hooks, Python compilation, Serve mypy, and Serve pyrefly passed. - HAProxy end-to-end test passed: 1 passed, 37 deselected in 2.85s using bundled HAProxy 2.8.25. This does not duplicate the related PRs: they introduce or consume body-aware routing, while this PR increases the shared buffer default and tests a million-token-sized body. AI assistance was used to implement and validate this change; the submitter reviewed every changed line. --------- Signed-off-by: Seiji Eicher <[email protected]> Signed-off-by: Seiji Eicher <[email protected]>
…the engine (ray-project#65095) Signed-off-by: Jeffrey Wang <[email protected]> Signed-off-by: kv-benchmark <[email protected]>
…t#66360) ## Why The current 256 KiB cap truncates the router copy of large LLM requests, causing body-aware routing to lose information. Several recent routing changes depend on this body: - [ray-project#64224](ray-project#64224), [ray-project#64328](ray-project#64328), and [ray-project#65095](ray-project#65095) use it for KV-aware routing. - [ray-project#65680](ray-project#65680) uses it for LoRA model selection. - [ray-project#66258](ray-project#66258) and [ray-project#66315](ray-project#66315) use it for deployment-aware, multi-model direct streaming. - [ray-project#63183](ray-project#63183) introduced the body-forwarding gate shared by these paths. This PR raises the default buffer to 8 MiB and lowers the default `maxconn` to 4,096. The sizing and behavior were verified with a real HAProxy 2.8.25 test and against the bundled HAProxy source code. Both settings retain their existing environment-variable overrides. | Consideration | Result | |---|---| | Request size | A one-million-token English request is commonly estimated at roughly 4 MB. The test request is 5.72 MiB, so 8 MiB leaves 2.28 MiB (~40%) headroom. | | Latency | Requests below 256 KiB do not wait longer. Requests from 256 KiB to 8 MiB now wait for the complete body before routing. | | Memory | Buffers are allocated dynamically. A live stream can use roughly 16 MiB across request and response buffers. Lowering `maxconn` from 20,000 to 4,096 reduces the theoretical buffer ceiling from 320 GiB to 64 GiB. | ## Changes - Raise `RAY_SERVE_HAPROXY_INGRESS_REQUEST_ROUTER_BUFSIZE` from 256 KiB to 8 MiB. - Lower `RAY_SERVE_HAPROXY_MAXCONN` from 20,000 to 4,096. - Test a 6,000,014-byte, million-token-sized request end to end. - Derive rendered-config assertions from their constants so environment overrides remain testable. - Update the KV-aware routing guide. ## Tests - Pre-commit hooks, Python compilation, Serve mypy, and Serve pyrefly passed. - HAProxy end-to-end test passed: 1 passed, 37 deselected in 2.85s using bundled HAProxy 2.8.25. This does not duplicate the related PRs: they introduce or consume body-aware routing, while this PR increases the shared buffer default and tests a million-token-sized body. AI assistance was used to implement and validate this change; the submitter reviewed every changed line. --------- Signed-off-by: Seiji Eicher <[email protected]> Signed-off-by: Seiji Eicher <[email protected]>
## Why are these changes needed? When `KVAwareRouter` is enabled, `LLMRouter` tokenizes the prompt and sends the token IDs to the chosen `LLMServer` replica, so the engine can skip tokenization (#65095). This is best effort: if the tokens don't arrive in time, the engine tokenizes the prompt itself. Nothing currently measures how often the bypass works. This PR adds the counter `ray_serve_llm_tokenization_bypass_requests_total`. Serve adds the `application`, `deployment` and `replica` tags, and an `outcome` tag says what happened: | `outcome` | Recorded by | Meaning | |---|---|---| | `success` | LLMServer | The engine used the forwarded tokens | | `send_failed` | LLMRouter | The router couldn't send the tokens | | `miss` | LLMServer | No staged tokens for the request (arrived late, expired, or evicted) | | `invalid` | LLMServer | The staged tokens couldn't be decoded, or the list was empty | ### Implementation details - Each request that tries the bypass is counted exactly once, by either the router or the server that receives it. Every outcome except `success` falls back to engine tokenization. Requests the router never tries (multimodal requests, bodies it can't parse) aren't counted. - Each replica exports every outcome at 0 when it starts. Without this, a series only appears on its first event, and prometheus `rate()` drops everything recorded before that series' first scrape. That undercounts rare outcomes and new replicas. `Counter.inc` rejects 0, so this uses `Counter._record(0, ...)`. ### Example query Share of skipping tokenization attempts that didn't apply, over 1h: ```promql sum by (application) (rate(ray_serve_llm_tokenization_bypass_requests_total{outcome!="success"}[1h])) / sum by (application) (rate(ray_serve_llm_tokenization_bypass_requests_total[1h])) ``` Filter by `application`, not `deployment`, because `send_failed` comes from the router deployment and the other outcomes come from the server deployment. 🤖 Generated with [Claude Code](https://claude.com/claude-code). I reviewed every line of code. Signed-off-by: Jeffrey Wang <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>

Description
Adds skip-tokenization using a per-replica ZMQ token side channel plus a HAProxy token-key header.
API shape
Mechanics
LLMServer
PULLsocket on a replica-rank-offset port.LLMRouter.[token_key, token_bytes]fromLLMRouterin a bounded TTL store.x-kv-token-key, pops staged tokens and passes them to vLLM viakv_transfer_params(requires: [Frontend] Reuse prefill token ids on the decode chat path for disaggregated serving vllm-project/vllm#48145).LLMRouter
uint32bytes.PUSHes[token_key, payload]to the selected LLMServer endpoint.x-kv-token-keyinto the backend request.Semantics
Best effort, no ACK. If ZMQ is unavailable, queued out, or tokens arrive late, vLLM re-tokenizes normally.
Alternatives
HAProxy headers/body mutation: 200k tokens exceed practical header limits. Body splicing forces HAProxy/Lua to buffer, copy, rewrite
Content-Length, and burn proxy CPU on every hot-path request.Ray RPC/object paths: Per-request RPC adds scheduling, serialization, and possible large-object transport costs. Slow/restarting replicas can backpressure
LLMRouter, while token forwarding only needs best-effort delivery.Ablation study
FAQ
Related issues
Additional information