Skip to content

[Bug] Scale KPA on the total load, not the per-pod mean - #2884

Merged
varungup90 merged 2 commits into
vllm-project:mainfrom
ankit373:fix/kpa-total-not-mean-2879
Oct 1, 2026
Merged

varungup90 merged 2 commits into
vllm-project:mainfrom
ankit373:fix/kpa-total-not-mean-2879

Conversation

@ankit373

@ankit373 ankit373 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request Description

Problem. KPA's replica count ignores how many pods carry the load. Four pods each at 90 against a target of 50 (80% over) scale down to 2. The shipped samples/autoscaling/kpa.yaml (gpu_cache_usage_perc, target 0.5) can never recommend more than ceil(1/0.5) = 2 replicas.

Root cause. MetricsClient.UpdateMetrics records the mean of the per-pod values for pod, resource and custom sources. KPAAlgorithm.computeTargetReplicas then computed ceil(observed / target), which is Knative's formula for a total across ready pods. APA already multiplies the mean by the pod count.

Fix. For pod, resource and custom sources, multiply the windowed value by max(1, readyPods) before dividing by the target. External and domain sources report one total, so they keep ceil(total / target). The tolerance gate still compares the mean with the per-pod target, so its behavior is unchanged. This adds GetMetricSourceTypeForMetric to ScalingContext to read the source type already stored in MetricTarget.

Pending-replica guard. applyPendingReplicaGuard recomputed the KPA scale-up with ceil(adjusted / target) and could override the corrected result. It now uses ceil(currentReplicas * adjusted / target) for per-pod sources.

Tests.

  • per_pod_mean_over_target_scales_up: 4 pods at 90 vs 50 gives 8 (2 before the fix).
  • ratio_metric_can_exceed_two_replicas: 4 pods at 0.9 vs 0.5, max 8, gives 8 (2 before the fix).
  • external_source_value_is_already_a_total: an external total of 90 vs 50 still gives 2.
  • Two existing cases assumed the total semantics with an unset source type; their inputs are now per-pod means.

Checks run:

  • go test ./pkg/controller/podautoscaler/... passes. On macOS I needed -ldflags=-linkmode=external to avoid a missing LC_UUID abort.
  • golangci-lint run ./pkg/controller/podautoscaler/... reports 0 issues.
  • The two new per-pod cases fail on main and pass here.

Related Issues

Resolves: #2879

The metrics client records the per-pod mean for pod, resource and custom sources, but KPA divided that mean by the per-pod target as if it were a total. Four pods at 90 against a target of 50 scaled down to 2. Multiply by the pod count for those sources (external and domain sources already report one total), as APA does.

Signed-off-by: Ankit Jha <[email protected]>

@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 updates the KPA scaling algorithm to correctly distinguish between per-pod mean metrics and total metrics. For per-pod, resource, and custom metric sources, the target replica calculation now scales the observed value by the number of ready pods to compute the total load, while external and domain sources continue to use the total value directly. The ScalingContext interface and its implementations have been updated to support retrieving the metric source type, and comprehensive unit tests have been added to verify the new scaling behavior. There are no review comments, and I have no feedback to provide.

@varungup90 varungup90 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed commit a31e12ac8a95d1d30073c303696e352f562ac75b. Found one P2 correctness gap: the pending-replica guard still assumes a total metric on KPA scale-up and can override the corrected recommendation.

Validation:

  • go test ./pkg/controller/podautoscaler/algorithm ./pkg/controller/podautoscaler/context -count=1 passed.
  • go test ./pkg/controller/podautoscaler/... -count=1 passed on the unmodified PR head.
  • An additional full-pipeline regression case (4 current replicas, 3 ready, 1 pending, per-pod metric 90, target 50) failed for KPA with both POD and RESOURCE sources: expected 6 replicas, actual 4. The same case passed for APA.
  • Temporarily multiplying the KPA guard's adjusted mean by currentReplicas made those regression cases pass. All temporary changes were confined to an isolated checkout and removed afterward.

The algorithm-level fix is correct for the covered cases, but the pending-replica path needs the same normalization and integration coverage.

var dspc, dppc float64
if observedStableValue > scaleUpThreshold || observedStableValue < scaleDownThreshold {
dspc = math.Ceil(observedStableValue / targetValue)
dspc = math.Ceil(load * observedStableValue / targetValue)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] Apply the per-pod formula in the pending-replica guard too

The corrected recommendation is still overwritten by applyPendingReplicaGuard: its KPA scale-up branch in autoscaler.go:323 uses ceil(adjustedMetricValue / targetValue), even though the adjusted value is a per-pod mean. I reproduced this through ComputeDesiredReplicas with 4 current replicas, 3 ready pods each at 90, 1 pending pod, and target 50. This calculation recommends 8, but the guard computes ceil((90*3/4)/50)=2 and clamps it to 4, suppressing scale-up even though the ready pods alone need ceil(3*90/50)=6. Both POD and RESOURCE sources are affected; APA returns 6 for the same input. Please update the KPA guard to ceil(currentReplicas * adjustedMetricValue / targetValue) and add a full-pipeline pending-replica test, updating the existing total-based guard fixtures as needed. This pending-replica inconsistency is also called out in #2879.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks. The guard's scale-up branch now uses ceil(currentReplicas * adjusted / target) for per-pod sources (autoscaler.go), as APA does. External and domain sources keep the old formula. Your case (4 current, 3 ready, 1 pending, 90 vs 50) now gives 6 for KPA with POD and RESOURCE sources, and the same for APA. I updated the two total-based fixtures (kpa scale-up adjusted is now 10, dampened uses 100 so the guard still bites) and added the POD, RESOURCE and APA rows. Without the change the new rows fail. Tip 78a7b32.

The guard recomputed the scale-up with ceil(adjusted / target), which assumed a total and could override the corrected KPA recommendation. Use ceil(currentReplicas * adjusted / target) for per-pod sources, as APA does.

Signed-off-by: Ankit Jha <[email protected]>
@varungup90
varungup90 merged commit 6f07dd1 into vllm-project:main Oct 1, 2026
25 of 26 checks passed
bolubo added a commit to bolubo/aibrix that referenced this pull request Oct 3, 2026
The projection mirrors the reactive formula. KPA scales the windowed
per-pod mean by the pod count, external and domain sources keep the
total formula, and APA is unchanged. Mirrors the reactive fix in vllm-project#2884.
bolubo added a commit to bolubo/aibrix that referenced this pull request Oct 3, 2026
The projection mirrors the reactive formula. KPA scales the windowed
per-pod mean by the pod count, external and domain sources keep the
total formula, and APA is unchanged. Mirrors the reactive fix in vllm-project#2884.

Signed-off-by: bolubo <[email protected]>
bolubo added a commit to bolubo/aibrix that referenced this pull request Oct 18, 2026
The projection mirrors the reactive formula. KPA scales the windowed
per-pod mean by the pod count, external and domain sources keep the
total formula, and APA is unchanged. Mirrors the reactive fix in vllm-project#2884.

Signed-off-by: bolubo <[email protected]>
bolubo added a commit to bolubo/aibrix that referenced this pull request Nov 9, 2026
The projection mirrors the reactive formula. KPA scales the windowed
per-pod mean by the pod count, external and domain sources keep the
total formula, and APA is unchanged. Mirrors the reactive fix in vllm-project#2884.

Signed-off-by: bolubo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] KPA treats the per-pod mean as a total, so its replica count ignores the pod count

2 participants