Skip to content

[Serve] Align capacity-adjusted replica counts with gang size - #68056

Open
YidaWeng wants to merge 2 commits into
ray-project:masterfrom
YidaWeng:serve-gang-target-capacity
Open

YidaWeng wants to merge 2 commits into
ray-project:masterfrom
YidaWeng:serve-gang-target-capacity

Conversation

@YidaWeng

@YidaWeng YidaWeng commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Why are these changes needed?

With gang scheduling, GangSchedulingAutoscalingPolicy rounds autoscaling decisions up to whole gangs, but target_capacity is still applied to the raw replica counts:

  • AutoscalingState.apply_bounds() clamps to bounds computed from min_replicas/max_replicas, so it can clip an aligned decision back to an unaligned count. E.g. max_replicas=8, gang_size=4, target_capacity=75 -> upper bound 6.
  • For non-autoscaling deployments, the capacity-adjusted target replica count is computed directly, e.g. num_replicas=8, gang_size=4, target_capacity=30 -> 2.

The gang scheduler then rejects the delta (num_replicas_to_add N is not divisible by gang_size M), and the deployment ends up in DEPLOY_FAILED or flaps.

What is the change?

Apply target_capacity in gang units instead of replica units:

capacity_adjust(num_replicas // gang_size) * gang_size

This reuses the existing ROUND_HALF_UP + max(1, ...) semantics, keeps the result aligned by construction, preserves scale-to-zero (0 capacity or 0 replicas still yields 0), and is invariant to whether bounds are eventually expressed in replicas or in gangs (relevant to the API discussion in #67507).

  • New helper get_capacity_adjusted_gang_replicas() in _private/utils.py; get_capacity_adjusted_num_replicas() is unchanged.
  • DeploymentAutoscalingState uses it for the capacity-adjusted lower/upper bounds, and rounds a configured initial_replicas (or a target carried over from a pre-gang config) up to a whole gang.
  • Non-autoscaling DeploymentState uses it for the target replica count.
  • No change when gang scheduling is disabled or target_capacity is None/100.

For unaligned legacy values, lower bounds round the gang count up (so initial_replicas=6, gang_size=4 yields 8, matching the registered target) and upper bounds round down (so a bound is never exceeded). A positive replica count always maps to at least one gang, so the default min_replicas=1 gives 4 rather than 0; only target_capacity=0 or num_replicas=0 yield 0.

Note on the rounding choice: 8 replicas / gang_size=4 / target_capacity=75 is 2 gangs * 75% = 1.5 -> 2 gangs (8 replicas, half-up) rather than 6 or 4. This matches the existing half-up behavior of get_capacity_adjusted_num_replicas. If maintainers prefer to never exceed the capacity target, switching to floor-in-gang-units is a one-line change (still floored at one gang for non-zero capacity).

Related issue number

Part of #67494. This is the runtime half that #67507 leaves as a follow-up; it doesn't depend on that PR's API validation and touches different files.

Tests

  • python/ray/serve/tests/unit/test_autoscaling_policy.py - 62 passed
  • python/ray/serve/tests/unit/test_deployment_state.py -k TargetCapacity - 47 passed
  • python/ray/serve/tests/test_gang_scheduling.py -k TestGangTargetCapacity - 2 passed

The two new gang integration tests fail on master without the fix (verified by reverting the _private/ changes while keeping the tests) and pass with it. Unit tests run in a Linux container with the repo's python/ray/serve overlaid on a nightly Ray install; ruff and black==22.10.0 pass.

Review follow-up

18a66d0 addresses the Cursor Bugbot finding about floored lower bounds: lower bounds now round gangs up (round_gangs_up=True) and always map a positive replica count to at least one gang, so an unaligned min_replicas/initial_replicas can no longer fall to zero or below the registered target. Added regression tests for both cases.

Apply target_capacity to the number of gangs rather than to the raw
replica count when gang scheduling is enabled. Capacity-adjusting the
replica count directly can produce a value that isn't a multiple of
gang_size, which the gang scheduler rejects, leaving the deployment in
DEPLOY_FAILED or flapping.

Two paths are fixed:
- Autoscaling bounds are capacity-adjusted in gang units, so
  apply_bounds() can no longer clip a gang-aligned decision back to an
  unaligned count.
- Non-autoscaling targets are capacity-adjusted in gang units before the
  replica delta is computed.

Scale-to-zero is preserved, and behavior is unchanged when gang
scheduling is disabled or target_capacity is None.

Signed-off-by: YidaWeng <[email protected]>
@YidaWeng
YidaWeng requested a review from a team as a code owner November 11, 2026 06:05
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit ebdc1d3. Configure here.

Comment thread python/ray/serve/_private/utils.py Resolved
@ray-gardener ray-gardener Bot added serve Ray Serve Related Issue community-contribution Contributed by the community labels Nov 11, 2026
@jeffreywang88

Copy link
Copy Markdown
Contributor

Check out #67507 (review).

An unaligned min_replicas or initial_replicas (including the default
min_replicas=1, which is explicitly allowed with gang scheduling) was
floored to zero gangs, and the capacity-up lower bound could sit below
the target that register() rounds up to.

Lower bounds now round the gang count up, upper bounds still round down,
and a positive replica count always maps to at least one gang. Only
target_capacity=0 or num_replicas=0 yield zero, preserving scale-to-zero.

Signed-off-by: YidaWeng <[email protected]>
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Nov 25, 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

community-contribution Contributed by the community serve Ray Serve Related Issue stale The issue is stale. It will be closed within 7 days unless there are further conversation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants