Repository navigation
[llm][kv][7/N] Prefill/decode token load aware request routing - #64400
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements request lifecycle tracking and active load booking for KV-aware routing in Ray LLM. It introduces a RequestLifecycle dataclass to track in-flight request states and updates KVRouterActor to book reservations, update decode progress, and free reservations in the selection service. Additionally, token_tracking.py is updated to pass the expected output tokens, and extensive tests are added to validate these behaviors. The review feedback highlights three key areas for improvement: addressing a potential memory leak in _effective_prefill_tokens_by_request when requests are cancelled or fail prematurely, improving robustness in token tracking to prevent exceptions from disrupting the core engine stream, and ensuring _get_decay_fraction safely handles negative or zero values for expected output tokens.
Signed-off-by: Jeffrey Wang <[email protected]>
… token load Signed-off-by: Jeffrey Wang <[email protected]>
8ef5eab to
6a154c0
Compare
Signed-off-by: Jeffrey Wang <[email protected]>
Signed-off-by: Jeffrey Wang <[email protected]>
| "worker_id": worker_id, | ||
| "token_ids": token_ids, | ||
| "expected_output_tokens": expected_output_tokens, | ||
| "effective_prefill_tokens": effective_prefill_tokens, |
There was a problem hiding this comment.
Routing and lifecycle IDs differ
Medium Severity
select_worker stores effective_prefill_tokens under Serve’s routing request_id, but lifecycle hooks admit load under get_serve_request_id() or the vLLM engine id. When those differ—as with separate direct-streaming hops or missing X-Request-Id—create_reservation gets effective_prefill_tokens as None, so booked prefill load can disagree with the overlap-aware routing score.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit a58e516. Configure here.
kouroshHakha
left a comment
There was a problem hiding this comment.
leaving manual review
| # Actors defined in a test module are pickled by reference, so a worker on | ||
| # another node would have to import this module to load them, which fails. | ||
| # Pickle it by value so the actor classes ship in full and run on any node. | ||
| ray.cloudpickle.register_pickle_by_value(sys.modules[__name__]) |
There was a problem hiding this comment.
why is this needed? I am not sure you need this?
There was a problem hiding this comment.
updated the comment with the reasoning, but yeah I'm not sure how to get around this 🤔
# Actors defined in a test module are pickled by reference, so a worker on
# another node would have to import this module to load them, which fails.
# Pickle it by value so the actor classes ship in full and run on any node.
kouroshHakha
left a comment
There was a problem hiding this comment.
Approach is sound — and the three-level test coverage (mock service, e2e, real Dynamo on GPU) is great. Just a few important questions that need to be resolved or addressed:
-
One failing hook can drop other requests' events. Now that the lifecycle hooks call into the live selection service (and do arithmetic on
self._block_size), they can raise — e.g.create_reservationfor a worker that hasn't registered yet at cold start, or_block_sizestillNonewhen a token-less request gets randomly routed before the first LongPoll snapshot.on_lifecycle_eventsapplies the batch in a plain loop, so one failure aborts the rest of the batch and the forwarder drops it wholesale — and a droppedon_request_completedfor an unrelated request in the same batch leaks its reservation in the selection service permanently, biasing all future routing away from that worker. Interestingly the release-test subclass already wraps each event in its own try/except to keep one failure from poisoning the rest — production should probably be at least as defensive. Can we do per-event try/except (log + continue), plus skipping the hooks when_svc/_block_sizeare unset? -
_effective_prefill_tokens_by_requestcan grow unbounded until the follow-up lands. I saw the reply on the Gemini thread — agreed the map goes away once the selection service cachesselect()results, but in the interim: a request that's routed but never startsgenerate()on the replica (client disconnects while queued, replica dies, backpressure timeout) never fireson_request_addedoron_request_completed, so its entry lives forever in the long-running router actor. If the follow-up is imminent, punting is fine — otherwise a cheap cap + FIFO evict (or purging onremove_worker) is a few lines.
Note
This review was co-written with AI assistance (Claude Code).
Signed-off-by: Jeffrey Wang <[email protected]>
addressed in f3a3579 with per-event try/except and skipped the hooks when
the removal of this will come 2 PRs later as we rely on dynamo's selection service to track booking metadata such as effective prefill tokens between |
Signed-off-by: Jeffrey Wang <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.
There are 3 total unresolved issues (including 1 from previous review).
❌ 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 9c3dea2. Configure here.
…roject#64400) Signed-off-by: Jeffrey Wang <[email protected]> Signed-off-by: 400Ping <[email protected]>


Description
Follow-up to #64327, the KV-aware router previously scored candidates on KV-cache overlap only; this PR feeds each request's lifecycle into Dynamo's
SelectionServiceso scoring also accounts for per-worker token load.KVRouterActorbooks a reservation per routed request (create_reservation) and applies engine lifecycle hooks:prefill_completeshifts load prefill→decodeadd_output_blockbooks decode blocks as generation crosses block boundaries (weighted by a decay fraction fromexpected_output_tokens)free_reservationreleases on completion.RequestTokenTrackerforwardssampling_params.max_tokensas the admission-time expected output length.How the token-load score works
For each candidate worker the selection service computes a cost and routes to the lowest.
prefill_blocks:(worker's in-flight prefill tokens + this request's prompt tokens) / block_size, booked bycreate_reservationand dropped byprefill_completewhen decode begins. overlap_credit subtracts the blocks already KV-cached on that worker, so a cache hit cheapens prefill.prefill_load_scale(default 1.0) sets how hard prefill backlog is penalized.decode_blocks: the worker's active decode-block count. One added per crossed KV-block boundary byadd_output_block, each weighted by its request's decay fractionmax(0, 1 − output/expected), so requests near completion barely count.free_reservationreleases both terms on completion.Related issues
Additional information