Repository navigation
[serve][LLM] Add pick-only fast path to AsyncioRouter for LLM ingress - #63517
Merged
kouroshHakha merged 6 commits intoMay 20, 2026
Merged
kouroshHakha merged 6 commits into
kouroshHakha merged 6 commits into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces the choose_replica and dispatch APIs to DeploymentHandle, allowing for manual replica selection and slot reservation. The LLM ingress router has been refactored to use these APIs, delegating routing decisions to the deployment's request router. Feedback indicates that experimental debugging code in replica_wrapper.py which bypasses slot reservation and release logic needs to be reverted. Furthermore, a bug was identified in router.py where a ReplicaWrapper object is incorrectly indexed, potentially causing a TypeError.
Introduce an internal ``_reserve=False`` opt-out on ``AsyncioRouter.choose_replica`` that short-circuits the replica-side ``reserve_slot`` actor RPC and the rejection-retry loop. The configured ``RequestRouter`` still drives replica ordering; only the per-pick capacity reservation is skipped. Use the new fast path from ``LLMRouter._pick_replica`` so the LLM ingress router (which delegates the actual request to HAProxy and never dispatches via the Serve handle) avoids the extra RPC + retry that was introducing burstiness compared to the prior local round-robin implementation. ``ReplicaSelection._slot_token`` is now ``Optional[str]``; ``dispatch`` explicitly rejects a selection that was created without a reservation, and ``release_slot`` is a no-op in that case. Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
kouroshHakha
force-pushed
the
kh/upstream-decouple-primitives
branch
from
May 19, 2026 18:41
c4364bf to
2560c17
Compare
Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
…pick Avoids positional indexing on the inner rank sequence; short-circuits at the first replica found across all ranks. Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
Covers: selection has no slot token, reserve_slot is never called on the replica, and dispatch on a pick-only selection raises. Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
jeffreywang88
approved these changes
May 19, 2026
…plica _pick_replica now passes _reserve=False to handle.choose_replica; the test's captured-kwargs assertion needs to match. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> Signed-off-by: Kourosh Hakhamaneshi <[email protected]>
Contributor
|
Heads up that |
alexandrplashchinsky
pushed a commit
to alexandrplashchinsky/ray-alex
that referenced
this pull request
May 29, 2026
…ray-project#63517) Signed-off-by: Kourosh Hakhamaneshi <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]> Co-authored-by: Jeffrey Wang <[email protected]> Signed-off-by: Alexandr Plashchinsky <[email protected]>
3 tasks done
400Ping
pushed a commit
to 400Ping/ray
that referenced
this pull request
Aug 18, 2026
…ray-project#63517) Signed-off-by: Kourosh Hakhamaneshi <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]> Co-authored-by: Jeffrey Wang <[email protected]> Signed-off-by: 400Ping <[email protected]>
jeffreywang88
added a commit
that referenced
this pull request
Oct 3, 2026
…66489) ## Problem `choose_replica(_reserve=False)` picks a replica and returns without calling the request router's `on_request_routed` hook. Pick-only callers send the request themselves. For example, the Serve LLM ingress router [returns the picked replica](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/core/ingress/router.py#L314-L319) to HAProxy, and HAProxy forwards the request. For these callers the pick is the routing decision, but stateful routing policies never see it. ## Solution Call `on_request_routed(pending_request, replica_id, None)` on the pick-only path. There is no `ReplicaResult` on this path, so the base class now types `result` as `Optional[ReplicaResult]` and documents when it is `None`. Update the `CapacityQueueRouter` docstring, which said that the hook runs only for the rejection protocol. The reserved path does not change. Add a unit test: a `_reserve=False` selection calls `on_request_routed` once, with the request args and `result=None`, and exiting the context does not call it again. Add a Serve-only HAProxy test in the same shape as `test_multiplexed_routing_retry` (#66170): an ingress router picks a backend replica with `_reserve=False`, and HAProxy forwards the request to it. The backend policy keeps its state only in `on_request_routed` (it ranks the last routed replica last). Four requests must reach both replicas. Without this change, all four go to the same replica. Add a manual GPU release test (`release/llm_tests/prefix_router_test`, mirrors `llm_tests/kv_router_test`) that reproduces this bug on a real cluster: 4 direct-streaming `LLMServer` replicas routed by `PrefixCacheAffinityRouter`, asserting that unrelated prompts spread across every replica and repeated prompts pin to one. Both checks call `choose_replica(_reserve=False)` directly, so they exercise the real routing decision without spending GPU time on inference. ## Implication for LLM applications With direct streaming (`RAY_SERVE_LLM_ENABLE_DIRECT_STREAMING=1`), the LLM ingress router picks replicas with `_reserve=False`. `PrefixCacheAffinityRouter` inserts prompts into its prefix tree only in [`on_request_routed`](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/routing_policies/prefix_aware/prefix_aware_router.py#L414-L418), so on this path the tree stays empty. No prompt matches, so every request falls back to [`get_smallest_tenants()`](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/routing_policies/prefix_aware/prefix_tree.py#L546-L562). That call returns all replicas tied at 0 characters, in the same order each time, and the pick-only path takes the first one. All traffic goes to one replica, and there is no prefix-cache affinity. We saw this on a 5-replica deployment with the same image and the same prompts: | Ingress | Load | Requests per replica | Prefix tree | |---|---|---|---| | Default | 100 unique prompts + 1 smoke test, concurrency 1 | 20 / 20 / 20 / 20 / 21 | Filled | | Direct streaming | 100 unique prompts + 1 smoke test, concurrency 1 | 101 / 0 / 0 / 0 / 0 | Empty | | Direct streaming | 20 shared prefixes, concurrency 16 | 1,000 / 0 / 0 / 0 / 0 | Empty | ## Additional information - `PrefixCacheAffinityRouter.on_request_routed` waits for the tree actor with `ray.get`. The reserved path already calls it. The pick-only path now calls it once per selection. - Related: #63517 added the pick-only path, and #66170 added its selection retry loop. - The GPU release test is `frequency: manual`, matching `llm_serve_kv_router`; it has not been run on a real cluster from this environment. --------- Signed-off-by: Dhakshin Suriakannu <[email protected]> Signed-off-by: Jeffrey Wang <[email protected]> Co-authored-by: Jeffrey Wang <[email protected]>
nadongjun
pushed a commit
to nadongjun/ray
that referenced
this pull request
Oct 6, 2026
…ay-project#66489) ## Problem `choose_replica(_reserve=False)` picks a replica and returns without calling the request router's `on_request_routed` hook. Pick-only callers send the request themselves. For example, the Serve LLM ingress router [returns the picked replica](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/core/ingress/router.py#L314-L319) to HAProxy, and HAProxy forwards the request. For these callers the pick is the routing decision, but stateful routing policies never see it. ## Solution Call `on_request_routed(pending_request, replica_id, None)` on the pick-only path. There is no `ReplicaResult` on this path, so the base class now types `result` as `Optional[ReplicaResult]` and documents when it is `None`. Update the `CapacityQueueRouter` docstring, which said that the hook runs only for the rejection protocol. The reserved path does not change. Add a unit test: a `_reserve=False` selection calls `on_request_routed` once, with the request args and `result=None`, and exiting the context does not call it again. Add a Serve-only HAProxy test in the same shape as `test_multiplexed_routing_retry` (ray-project#66170): an ingress router picks a backend replica with `_reserve=False`, and HAProxy forwards the request to it. The backend policy keeps its state only in `on_request_routed` (it ranks the last routed replica last). Four requests must reach both replicas. Without this change, all four go to the same replica. Add a manual GPU release test (`release/llm_tests/prefix_router_test`, mirrors `llm_tests/kv_router_test`) that reproduces this bug on a real cluster: 4 direct-streaming `LLMServer` replicas routed by `PrefixCacheAffinityRouter`, asserting that unrelated prompts spread across every replica and repeated prompts pin to one. Both checks call `choose_replica(_reserve=False)` directly, so they exercise the real routing decision without spending GPU time on inference. ## Implication for LLM applications With direct streaming (`RAY_SERVE_LLM_ENABLE_DIRECT_STREAMING=1`), the LLM ingress router picks replicas with `_reserve=False`. `PrefixCacheAffinityRouter` inserts prompts into its prefix tree only in [`on_request_routed`](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/routing_policies/prefix_aware/prefix_aware_router.py#L414-L418), so on this path the tree stays empty. No prompt matches, so every request falls back to [`get_smallest_tenants()`](https://github.com/ray-project/ray/blob/743f0d31866a05a221ccb605e2ae8331cdff304a/python/ray/llm/_internal/serve/routing_policies/prefix_aware/prefix_tree.py#L546-L562). That call returns all replicas tied at 0 characters, in the same order each time, and the pick-only path takes the first one. All traffic goes to one replica, and there is no prefix-cache affinity. We saw this on a 5-replica deployment with the same image and the same prompts: | Ingress | Load | Requests per replica | Prefix tree | |---|---|---|---| | Default | 100 unique prompts + 1 smoke test, concurrency 1 | 20 / 20 / 20 / 20 / 21 | Filled | | Direct streaming | 100 unique prompts + 1 smoke test, concurrency 1 | 101 / 0 / 0 / 0 / 0 | Empty | | Direct streaming | 20 shared prefixes, concurrency 16 | 1,000 / 0 / 0 / 0 / 0 | Empty | ## Additional information - `PrefixCacheAffinityRouter.on_request_routed` waits for the tree actor with `ray.get`. The reserved path already calls it. The pick-only path now calls it once per selection. - Related: ray-project#63517 added the pick-only path, and ray-project#66170 added its selection retry loop. - The GPU release test is `frequency: manual`, matching `llm_serve_kv_router`; it has not been run on a real cluster from this environment. --------- Signed-off-by: Dhakshin Suriakannu <[email protected]> Signed-off-by: Jeffrey Wang <[email protected]> Co-authored-by: Jeffrey Wang <[email protected]>
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.
In LLMs direct streaming, we can take a shortcut and make a conscious trade off for using the latest known state of available replicas to do the routing and do not do the least loaded check via rpc calls and slot reservation in the application code. This will be slow. instead we can assume the replica has capacity and have a graceful fallback to least loaded at the HAProxy layer. In other words, with this pr, we only make the best effort to pick the optimal replica according to the router, not a guaranteed one.