Repository navigation
[serve][llm] Fix LoRA routing in PrefixCacheAffinityRouter - #66660
Conversation
…AffinityRouter choose_replicas called apply_multiplex_routing or apply_locality_routing twice per attempt: once inside PowerOfTwoChoicesRequestRouter's choose_replicas for the fallback, then again for prefix matching. Both advance per-request state, so the second call returned the next tier's candidates. A multiplexed request, such as one for a LoRA adapter, was prefix-matched among the replicas with the fewest models loaded instead of the replicas that have the adapter. Compute the candidates once, so prefix matching and the power of two choices fallback use the same candidates. The fallback is only drawn when prefix matching finds no replica. Behavior change: with availability zone locality on, the default for DeploymentHandles, the first attempt now prefix-matches among replicas in the caller's zone instead of all replicas. Signed-off-by: Artur Niederfahrenhorst <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…AffinityRouter With Serve LLM's default unbounded max_ongoing_requests, replicas never reject requests, so Serve never loads a multiplexed model, such as a LoRA adapter, onto more replicas, however busy the replicas that have it get. - When the replicas chosen for a multiplexed request have more than multiplex_spill_threshold (default 64) more requests than the least busy candidate or replica with the fewest models loaded, route the request to the least busy of those instead, which loads the model there. - Match a multiplexed request's prefix only against text cached for the same model, since vLLM's prefix cache doesn't share KV across adapters. - On a low match rate, pick the smallest tenant among this attempt's candidates instead of among all replicas. Signed-off-by: Artur Niederfahrenhorst <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
It is 100% Claude generated and I'm attaching it only for completeness. |
The prefix-aware routing docs stopped including snippets of this file when they were reorganized in ray-project#57787, so no literalinclude uses these tags. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request introduces support for multiplexed models (such as LoRA adapters) in the prefix-aware router. It ensures that prefix matching is scoped per model ID to prevent cross-adapter KV cache sharing issues, and adds a multiplex_spill_threshold configuration to route requests to less busy replicas when the chosen replicas are overloaded. The feedback highlights two critical safety issues in the new code: a potential ZeroDivisionError when calculating the prefix match rate for empty inputs, and a potential AttributeError when accessing metadata on an optional pending_request without a None check.
…refix_aware_router.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
When the spill triggered, the router offered the busy replicas along with the idle ones. Replica selection prefers replicas with a cached queue length, so if the idle replicas had none, the request stayed on a busy replica. Offer only the replicas within multiplex_spill_threshold of the least busy one. The test fixture can now enable the queue length cache, which Serve enables for handles by default. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Wrap the empty-prompt guard to the line length and say when the text is empty. Add a test with an image-only request, which divided by zero before the guard. Also check that the request isn't None before reading its multiplexed model ID. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
…outer Send LoRA traffic through PrefixCacheAffinityRouter to fake LLM replicas on a CPU node, for uniform, skewed (zipf) and shared-prompt adapter traffic, and report where the router places adapters and requests: the share of requests whose adapter is already loaded, prefix cache hits, adapter evictions, the busiest replica's share, latency and routing time. Runs weekly, and fails if a request never completes. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit e611bdf. Configure here.
Requests that failed while traffic was still being sent left the in-flight set without anyone reading their result, so the test only reported them as unfinished, without the error. Keep every request and check them all. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
…mallest_tenants On a weak match, prefix_match_or_smallest_tenants returned the smallest tenants in the whole tree, so the router could pick a replica outside the request's candidates, e.g. one without the requested LoRA adapter. Give get_smallest_tenants an available_tenants argument and pass it through. The helper and its test are copied unchanged from ray-project#66660, so the two PRs make the same change there. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
| ): | ||
| input_text = self._extract_text_from_request(pending_request) | ||
| if input_text is not None: | ||
| # Start Sphinx tag: __begin_load_balance_component__ |
There was a problem hiding this comment.
I noticed that this tag is not used anywhere anymore so instead of moving it, we can just get rid of it.
| multiplexed request goes under its model ID. | ||
| """ | ||
| model_id = pending_request.metadata.multiplexed_model_id | ||
| return f"\0{model_id}\0" if model_id else "" |
There was a problem hiding this comment.
This means that we route traffic for LoRA adapters to where the model AND the adapter is loaded and spill to where there is little load. A more refined version would be one where we spill to where the model and adapter and low load are but I'm not even sure that would be generally better.
There was a problem hiding this comment.
Made this a CPU test even though this is a release test so it is quick and easy to run.
The component under test is the router here so it's ok to fake vLLM.
We fake prefix hits and the prefill lag and evict adapters and I'd argue that that is a good enough target to track since vLLM's behavior will also vary across configurations.
Let me know if you disagree @jeffreywang88
| pending_request=pending_request, | ||
| ) | ||
| if pending_request is None or not fallback_replicas: | ||
| return fallback_replicas |
There was a problem hiding this comment.
With this PR, we stop using PowerOfTwoChoicesRequestRouter.choose_replicas in this router entirely.
There was a problem hiding this comment.
If you call PowerOfTwoChoicesRequestRouter.choose_replicas, it ends up calling apply_multiplex_routing and which sets tried_first_multiplexed_models = True on the pending requests. A few lines below, we then also call apply_multiplex_routing. At that time, the "fried first multiplexed models" flag is already set so apply_multiplex_routing just returns the _get_replica_ids_with_fewest_multiplexed_models(). So that's why, in any case, we just load the adapters on all the replicas and do load balance somehow even though the code does not work as intended I think.
jeffreywang88
left a comment
There was a problem hiding this comment.
Does this work with direct streaming?
| pending_request=pending_request, | ||
| ) | ||
| if pending_request is None or not fallback_replicas: | ||
| return fallback_replicas |
I think this is the same comment that I answer here but it's somehow displayed twice in the github interface. Please let me know if these are two separate comments and there is some question unanswered. |
Resolves the conflicts with ray-project#66671, which moved the router's tree lookup into one non-blocking prefix_match_or_smallest_tenants call: - The router passes the request's key prefix to that call. The tree counts only text matched after the key prefix toward the match rate, which this PR did in the router before. - The prefix tree insert stays fire-and-forget and keeps the key prefix. - Drops the Sphinx tags ray-project#66671 moved: no docs include them since ray-project#57787. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
Covers the router passing the key prefix to the tree lookup, and the tree counting only text matched after it toward the match rate. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Artur Niederfahrenhorst <[email protected]>
…ree key Fixes ray-project#68592. vLLM does not share KV cache blocks across cache salts, but the router built its prefix-tree key from the prompt/messages text only, so a request with salt B counted as a prefix hit on the replica that served the same text under salt A. Include the request's cache salt in the key prefix, mirroring how multiplexed (LoRA) requests are already keyed by model ID (ray-project#66660), so requests only match prefixes cached under the same salt (and model). Adds regression tests: same-salt prefix match, per-salt isolation, and model+salt combined isolation. Signed-off-by: TEJA P C <[email protected]>


Before this PR:
choose_replicas called apply_multiplex_routing or apply_locality_routing twice per attempt: once inside PowerOfTwoChoicesRequestRouter's choose_replicas for the fallback, then again for prefix matching. Both advance per-request state, so the second call returned the next tier's candidates. A multiplexed request, such as one for a LoRA adapter, was prefix-matched among the replicas with the fewest models loaded instead of the replicas that have the adapter.
That spread adapters across replicas by accident and hid two more problems. Serve loads a multiplexed model onto more replicas only when the replicas that have it reject requests, and with Serve LLM's default max_ongoing_requests they never do, so a popular adapter's traffic queues on the replicas that loaded it first. And the prefix tree keyed on prompt text only, so a LoRA request could match text cached under another adapter, which saves nothing because vLLM's prefix cache keys blocks by LoRA ID.
After this PR:
Compute the candidates once, so prefix matching and the power of two choices fallback use the same candidates. The fallback is only drawn when prefix matching finds no replica.
With availability zone locality on, the default for DeploymentHandles, the first attempt now prefix-matches among replicas in the caller's zone instead of all replicas.
When the replicas chosen for a multiplexed request have more than
multiplex_spill_threshold(default 64) more ongoing requests than the least busy candidate or replica with the fewest models loaded, the request is routed among those by load, so a busy adapter spreads to more replicas. A multiplexed request's text goes into the prefix tree under its model ID, and only text matched under the model counts toward the match rate. On a low match rate, the fallback picks the smallest tenant among this attempt's candidates instead of among all replicas. Since #66671 moved prefix matching into the tree's singleprefix_match_or_smallest_tenantscall, that call takes the model ID prefix askey_prefix: it's matched before the text and left out of the match rate.Also removes the router's unused Sphinx tags: the docs stopped including this file in #57787.
Benchmark
This PR adds a weekly release test,
llm_serve_prefix_router_lora_benchmark(release/llm_tests/serve/prefix_router_lora_benchmark.py), that sends LoRA traffic through the router to fake Ray Serve LLM replicas on Serve's real@serve.multiplexed, on a CPU node. 4 replicas, 32 adapters, at most 16 loaded per replica, 2 s to download an adapter (a replica loads one at a time), vLLM-stylemax_loras=4and a prefix cache keyed by adapter, replicas never reject. Open-loop Poisson traffic at 40 req/s for 180 s, identical for both routers;coldis the first 60 s,last-60sthe final 60 s. Workloads:uniform,zipf(the top adapter gets 28% of traffic) andshared-prompt(all adapters share 2 system prompts). Below is the release test on its setup (Anyscale m5.2xlarge,anyscale/ray-llm:nightly-py312-cu130at master0fe2388b9d) with master's router and with this PR's (itsprefix_aware_router.pyandprefix_tree.pycopied over the installed ones), 3 seeds; placement metrics (after the cold start) are means, latencies medians. Master → this PR:The cold start is 6% slower on uniform traffic: the spill downloads adapters onto more replicas while queues build.
multiplex_spill_threshold(the release test's--multiplex-spill-threshold), as uniform / zipf / shared-prompt:64 is the lowest tested threshold that evicts no adapters in any workload. Lower thresholds keep spilling after the cold start, which costs evictions for a 2–4 s faster cold start. Without the spill, the hot zipf adapter stays on one replica: the busiest replica takes 70% of requests and the last-60s p99 is 42.4 s (12.2–67.3 s across seeds).
Both tables were measured before #66671 landed (this PR at
2eadba05dc, image at master0fe2388b9d) and haven't been rerun on the merged branch.Tests
Run locally on macOS (
--noconftestbecause the llmconftest.pyimports vLLM). New tests that fail on master:TestPrefixAwareLogic::test_multiplexed_request_matches_replicas_with_model,TestMultiplexedRouting::test_prefix_match_is_per_model,TestMultiplexedRouting::test_spills_from_busy_replica(2 cases) andTestPrefixTreePrefixMatchOrSmallestTenants::test_key_prefix_does_not_count_toward_match_rate. New tests that pass on master too:TestPrefixAwareLogic::test_image_only_request_uses_smallest_tenantandTestMultiplexedRouting::test_low_match_rate_uses_smallest_candidate(#66671 fixed these as well),TestMultiplexedRouting::test_spill_reaches_replica_without_cached_queue_len(with Serve's queue length cache on) andTestMultiplexedRouting::test_prefix_match_within_model, which fails if the router leaves the model ID out of the tree lookup or insert. The prefix-aware routing guide documentsmultiplex_spill_threshold.The release test passes on its setup with both routers and every threshold above, in about 10 of its 60 minutes, and
ray_releasevalidates the newrelease_tests.yamlentry.No existing issue or PR covers this (searched open PRs for
PrefixCacheAffinityRouter,prefix_aware_router, multiplex,get_smallest_tenants). #66671, which overlapped, landed first and is merged into this branch (d2dcaaf00c). Written with AI assistance (Claude Code).🤖 Generated with Claude Code