-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathstabilization.go
More file actions
111 lines (97 loc) · 3.61 KB
/
Copy pathstabilization.go
File metadata and controls
111 lines (97 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package saturation
import (
"context"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"github.com/llm-d/llm-d-workload-variant-autoscaler/internal/interfaces"
"github.com/llm-d/llm-d-workload-variant-autoscaler/internal/stabilization"
)
// applyStabilization damps the optimizer's raw per-variant targets with
// HPA-style scaling behavior before the enforcer and emit path consume them.
// It mutates decisions in place and is a no-op unless EnableStabilization is set
// in the global ("default") saturation config — mirroring how the GPU limiter is
// gated. Each variant (per role, when disaggregated) is stabilized independently
// against its own trailing recommendation window and per-period rate budget,
// retained on the Engine's long-lived stabilizer.
func (e *Engine) applyStabilization(ctx context.Context, decisions []interfaces.VariantDecision) {
if e.stabilizer == nil || len(decisions) == 0 {
return
}
cfg, ok := e.Config.SaturationConfig()["default"]
if !ok || !cfg.EnableStabilization {
return
}
logger := ctrl.LoggerFrom(ctx)
behavior := stabilization.DefaultBehavior()
// Bound the stabilizer's per-key history to the variants live this cycle, so
// keys for deleted variants do not accumulate.
active := make(map[string]struct{}, len(decisions))
for i := range decisions {
active[stabilizationKey(&decisions[i])] = struct{}{}
}
e.stabilizer.Retain(active)
type stabilizationEntry struct {
Name string `json:"name"`
Role string `json:"role,omitempty"`
Curr int `json:"curr"`
Raw int `json:"raw"` // optimizer recommendation
Final int `json:"final"` // after stabilization
}
type modelKey struct{ ns, modelID string }
grouped := make(map[modelKey][]stabilizationEntry)
for i := range decisions {
d := &decisions[i]
res := e.stabilizer.Stabilize(stabilization.Args{
Key: stabilizationKey(d),
CurrentReplicas: int32(d.CurrentReplicas),
DesiredReplicas: int32(d.TargetReplicas),
Behavior: behavior,
// 0 means no floor / no cap: scale-to-zero and minimum-replica
// enforcement remain the enforcer's job, not the stabilizer's.
MinReplicas: int32(ptr.Deref(d.MinReplicas, 0)),
MaxReplicas: int32(ptr.Deref(d.MaxReplicas, 0)),
})
raw := d.TargetReplicas
final := int(res.Replicas)
if final != raw {
d.TargetReplicas = final
retargetDecision(d)
}
k := modelKey{d.Namespace, d.ModelID}
grouped[k] = append(grouped[k], stabilizationEntry{
Name: d.VariantName,
Role: d.Role,
Curr: d.CurrentReplicas,
Raw: raw,
Final: final,
})
}
for k, entries := range grouped {
logger.Info("stabilization-decision",
"modelID", k.modelID,
"namespace", k.ns,
"decisions", entries,
)
}
}
// stabilizationKey identifies a scale target across cycles. It includes the
// model so that, even if two models in a namespace ever expose a same-named
// variant, their histories never collide. Disaggregated P/D variants have one
// scale target per role, so the role is part of the key too.
func stabilizationKey(d *interfaces.VariantDecision) string {
key := d.Namespace + "/" + d.ModelID + "/" + d.VariantName
if d.Role != "" {
key += "/" + d.Role
}
return key
}
// retargetDecision recomputes a decision's Action after stabilization changed
// its TargetReplicas, preserving the original reason category so the decision is
// not mis-attributed in the reason metric label.
func retargetDecision(d *interfaces.VariantDecision) {
category := d.ReasonCategory()
if category == "" {
category = interfaces.DecisionReasonV2
}
d.SetDecisionReason(d.ActionForTarget(), category, string(category)+" (stabilized)")
}