Skip to content

[Serve][LLM][3/3] Token based autoscaling: scale the bound server from router token metrics - #67941

Open
celinaky wants to merge 14 commits into
celina/prefill-token-autoscaling-2-metrics-pathfrom
celina/prefill-token-autoscaling-3-policy
Open

celinaky wants to merge 14 commits into
celina/prefill-token-autoscaling-2-metrics-pathfrom
celina/prefill-token-autoscaling-3-policy

Conversation

@celinaky

@celinaky celinaky commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Token-based autoscaling stack

  1. [Serve][LLM][1/3] Token based autoscaling: track autoscaling workload in KV router #67937 tracks outstanding effective prefill tokens in the KV router.
  2. [Serve][LLM][2/3] Token based autoscaling: report custom autoscaling metrics from fixed deployments #67938 lets a deployment publish replica metrics to an application autoscaling policy.
  3. [Serve][LLM][3/3] Token based autoscaling: scale the bound server from router token metrics #67941 (this PR) uses those token metrics to scale the server bound to the router.

Changes in this PR

  • The KV-aware direct-streaming builder enables replica reporting on LLMRouter. Serve polls record_autoscaling_stats(), which reads the tracker's current snapshot and includes the bound server name in the metric key.
  • Remove PR 1's autoscaling callback, its setter, and its publication calls from request lifecycle and peer-update paths. The tracker continues updating the workload and sharing reservations across peers.
  • The application policy reads contexts[router_id].aggregated_metrics from the router’s AutoscalingMetricsContext. It resolves the bound server and verifies isinstance(contexts[target_id], AutoscalingContext) before using the server’s scaling settings. Router replicas see overlapping views of the same workload, so Serve computes a time-weighted mean token load for each router replica over its reporting window, and the policy uses the largest of those means. It divides by the configured token target and rounds up to obtain the desired server count. The bound server's scaling limits and delays apply.

This updates the existing policy PR to consume #67938's replica reporting path in response to review.

Example

For a cluster configured for KV-aware direct streaming, set autoscaling_policy on the application and autoscaling_config on the LLM server.

applications:
  - name: llm-kv-router
    import_path: ray.serve.llm:build_openai_app
    autoscaling_policy:
      policy_function: ray.llm._internal.serve.routing_policies.kv_aware.autoscaling_policy.LLMTokenAutoscalingPolicy
      policy_kwargs:
        target_prefill_tokens_per_replica: 20000 # example target
    args:
      llm_configs:
        - model_loading_config:
            model_id: qwen3-0.6b
            model_source: Qwen/Qwen3-0.6B
          deployment_config:
            autoscaling_config:
              min_replicas: 1
              max_replicas: 8
            request_router_config:
              request_router_class: ray.serve.llm.request_router.KVAwareRouter

Verification

The mixed-context regression exercises the application-policy wrapper with a readable router and a scalable server, including rejection of a router scaling decision. Across the two PRs, the focused custom-metrics, policy, config, and deployment-state tests yielded 518 passed and 12 skipped.

  • pytest -q python/ray/serve/tests/unit/test_llm_token_autoscaling_policy.py — 4 passed, including a router report reaching the controller and producing a decision for its bound server.
  • pytest -q python/ray/serve/tests/unit/test_autoscaling_policy.py — 47 passed. Repository pre-commit checks passed.
  • The local direct-streaming and tracker integration suites were blocked during collection because the installed vLLM version lacks vllm.entrypoints.common.

Developed with AI assistance.

@celinaky
celinaky force-pushed the celina/prefill-token-autoscaling-3-policy branch from 0f72a20 to 39ef527 Compare November 7, 2026 09:43
@celinaky
celinaky added this pull request to stack #67950 November 8, 2026 00:45
@celinaky
celinaky force-pushed the celina/prefill-token-autoscaling-3-policy branch 2 times, most recently from 8acca59 to 579aa83 Compare November 8, 2026 18:59
@celinaky
celinaky force-pushed the celina/prefill-token-autoscaling-3-policy branch 2 times, most recently from bfcdeaf to f7e066d Compare November 8, 2026 20:58
@celinaky
celinaky force-pushed the celina/prefill-token-autoscaling-3-policy branch 4 times, most recently from 03510d1 to 422aeff Compare November 9, 2026 18:33
@celinaky
celinaky force-pushed the celina/prefill-token-autoscaling-3-policy branch from 422aeff to 5f7a98b Compare November 9, 2026 19:18
@celinaky celinaky added the go add ONLY when ready to merge, run all tests label Nov 9, 2026
@celinaky
celinaky force-pushed the celina/prefill-token-autoscaling-3-policy branch 2 times, most recently from 1bec553 to fa16d11 Compare November 9, 2026 19:44
@celinaky celinaky changed the title [Serve][LLM][3/3] Token based autoscaling: add upscaling-only prefill policy [Serve][LLM][3/3] Token based autoscaling: add prefill policy Nov 9, 2026
@celinaky celinaky changed the title [Serve][LLM][3/3] Token based autoscaling: add prefill policy [Serve][LLM][3/3] Token based autoscaling: add uncached prefill token based autoscaling policy Nov 9, 2026
@celinaky celinaky changed the title [Serve][LLM][3/3] Token based autoscaling: add uncached prefill token based autoscaling policy [Serve][LLM][3/3] Token based autoscaling: add uncached prefill token autoscaling policy Nov 9, 2026
@celinaky
celinaky requested a review from eicherseiji November 11, 2026 19:16
@celinaky
celinaky marked this pull request as ready for review November 11, 2026 19:16
@celinaky
celinaky requested a review from a team as a code owner November 11, 2026 19:16

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the LLMTokenAutoscalingPolicy to scale LLM deployments based on outstanding uncached prefill tokens, along with its unit tests. The feedback highlights a potential TypeError if ctx.total_queued_requests is None at runtime, suggesting a defensive fallback to 0 and a corresponding test case to verify this behavior.

@celinaky
celinaky requested a review from a team as a code owner November 20, 2026 16:43
@celinaky
celinaky force-pushed the celina/prefill-token-autoscaling-3-policy branch from fa21bce to a5fb96d Compare November 20, 2026 16:43
@celinaky celinaky changed the title [Serve][LLM][3/3] Token based autoscaling: add uncached prefill token autoscaling policy [Serve][LLM] Scale the bound server from router token metrics Nov 20, 2026
@celinaky celinaky changed the title [Serve][LLM] Scale the bound server from router token metrics [Serve][LLM][3/3] Token based autoscaling: scale the bound server from router token metrics Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant