forked from llm-d/llm-d-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler_profile.go
More file actions
294 lines (260 loc) · 12.4 KB
/
Copy pathscheduler_profile.go
File metadata and controls
294 lines (260 loc) · 12.4 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scheduling
import (
"context"
"fmt"
"sort"
"strings"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"sigs.k8s.io/controller-runtime/pkg/log"
errcommon "github.com/llm-d/llm-d-router/pkg/common/error"
logutil "github.com/llm-d/llm-d-router/pkg/common/observability/logging"
"github.com/llm-d/llm-d-router/pkg/common/observability/tracing"
"github.com/llm-d/llm-d-router/pkg/epp/framework/interface/plugin"
fwksched "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/scheduling"
"github.com/llm-d/llm-d-router/pkg/epp/metrics"
)
// NewSchedulerProfile creates a new SchedulerProfile object and returns its pointer.
func NewSchedulerProfile() *SchedulerProfile {
return &SchedulerProfile{
filters: []fwksched.Filter{},
scorers: []*WeightedScorer{},
// picker remains nil since profile doesn't support multiple pickers
}
}
// SchedulerProfile provides a profile configuration for the scheduler which influence routing decisions.
type SchedulerProfile struct {
filters []fwksched.Filter
scorers []*WeightedScorer
picker fwksched.Picker
}
// WithFilters sets the given filter plugins as the Filter plugins.
// if the SchedulerProfile has Filter plugins, this call replaces the existing plugins with the given ones.
func (p *SchedulerProfile) WithFilters(filters ...fwksched.Filter) *SchedulerProfile {
p.filters = filters
return p
}
// WithScorers sets the given scorer plugins as the Scorer plugins.
// if the SchedulerProfile has Scorer plugins, this call replaces the existing plugins with the given ones.
func (p *SchedulerProfile) WithScorers(scorers ...*WeightedScorer) *SchedulerProfile {
p.scorers = scorers
return p
}
// WithPicker sets the given picker plugins as the Picker plugin.
// if the SchedulerProfile has Picker plugin, this call replaces the existing plugin with the given one.
func (p *SchedulerProfile) WithPicker(picker fwksched.Picker) *SchedulerProfile {
p.picker = picker
return p
}
// AddPlugins adds the given plugins to all scheduler plugins according to the interfaces each plugin implements.
// A plugin may implement more than one scheduler plugin interface.
// Special Case: In order to add a scorer, one must use the scorer.NewWeightedScorer function in order to provide a weight.
// if a scorer implements more than one interface, supplying a WeightedScorer is sufficient. The function will take the internal
// scorer object and register it to all interfaces it implements.
func (p *SchedulerProfile) AddPlugins(pluginObjects ...plugin.Plugin) error {
for _, plugin := range pluginObjects {
if weightedScorer, ok := plugin.(*WeightedScorer); ok {
p.scorers = append(p.scorers, weightedScorer)
plugin = weightedScorer.Scorer // if we got WeightedScorer, unwrap the plugin
} else if scorer, ok := plugin.(fwksched.Scorer); ok { // if we got a Scorer instead of WeightedScorer that's an error.
return fmt.Errorf("failed to register scorer '%s' without a weight. follow function documentation to register a scorer", scorer.TypedName())
}
if filter, ok := plugin.(fwksched.Filter); ok {
p.filters = append(p.filters, filter)
}
if picker, ok := plugin.(fwksched.Picker); ok {
if p.picker != nil {
return fmt.Errorf("failed to set '%s' as picker, already have a registered picker plugin '%s'", picker.TypedName(), p.picker.TypedName())
}
p.picker = picker
}
}
return nil
}
func (p *SchedulerProfile) String() string {
filterNames := make([]string, len(p.filters))
for i, filter := range p.filters {
filterNames[i] = filter.TypedName().String()
}
scorerNames := make([]string, len(p.scorers))
for i, scorer := range p.scorers {
scorerNames[i] = fmt.Sprintf("%s: %f", scorer.TypedName(), scorer.Weight())
}
return fmt.Sprintf(
"{Filters: [%s], Scorers: [%s], Picker: %s}",
strings.Join(filterNames, ", "),
strings.Join(scorerNames, ", "),
p.picker.TypedName(),
)
}
// Run runs a SchedulerProfile. It invokes all the SchedulerProfile plugins for the given request in this
// order - Filters, Scorers, Picker. After completing all, it returns the result.
func (p *SchedulerProfile) Run(ctx context.Context, request *fwksched.InferenceRequest, candidateEndpoints []fwksched.Endpoint) (*fwksched.ProfileRunResult, error) {
endpoints := p.runFilterPlugins(ctx, request, candidateEndpoints)
if len(endpoints) == 0 {
return nil, errcommon.Error{Code: errcommon.Internal, Msg: "no endpoints available for the given request"}
}
// if we got here, there is at least one endpoint to score
weightedScorePerEndpoint := p.runScorerPlugins(ctx, request, endpoints)
result := p.runPickerPlugin(ctx, request, weightedScorePerEndpoint)
return result, nil
}
func (p *SchedulerProfile) runFilterPlugins(ctx context.Context, request *fwksched.InferenceRequest, endpoints []fwksched.Endpoint) []fwksched.Endpoint {
logger := log.FromContext(ctx)
filteredEndpoints := endpoints
logger.V(logutil.DEBUG).Info("Before running filter plugins", "endpoints", filteredEndpoints)
ctx, span := tracing.Tracer(TracerScope).Start(ctx, "filter_endpoints",
trace.WithSpanKind(trace.SpanKindInternal),
)
defer span.End()
span.SetAttributes(attribute.Int("llm_d.epp.filter.candidate_endpoints", len(endpoints)))
if request != nil {
if request.TargetModel != "" {
span.SetAttributes(attribute.String("gen_ai.request.model", request.TargetModel))
}
if request.RequestID != "" {
span.SetAttributes(attribute.String("gen_ai.request.id", request.RequestID))
}
}
for _, filter := range p.filters {
logger.V(logutil.VERBOSE).Info("Running filter plugin", "plugin", filter.TypedName())
before := time.Now()
filteredEndpoints = filter.Filter(ctx, request, filteredEndpoints)
metrics.RecordPluginProcessingLatency(filterExtensionPoint, filter.TypedName().Type, filter.TypedName().Name, time.Since(before))
logger.V(logutil.DEBUG).Info("Completed running filter plugin successfully", "plugin", filter.TypedName(), "endpoints", filteredEndpoints)
if len(filteredEndpoints) == 0 {
logger.V(logutil.VERBOSE).Info("Filter eliminated all endpoints", "plugin", filter.TypedName(), "endpointsBefore", len(endpoints))
break
}
}
span.SetAttributes(attribute.Int("llm_d.epp.filter.filtered_endpoints", len(filteredEndpoints)))
logger.V(logutil.VERBOSE).Info("Completed running filter plugins", "remainingEndpoints", len(filteredEndpoints))
return filteredEndpoints
}
func (p *SchedulerProfile) runScorerPlugins(ctx context.Context, request *fwksched.InferenceRequest, endpoints []fwksched.Endpoint) map[fwksched.Endpoint]float64 {
logger := log.FromContext(ctx)
logger.V(logutil.DEBUG).Info("Before running scorer plugins", "endpoints", endpoints)
weightedScorePerEndpoint := make(map[fwksched.Endpoint]float64, len(endpoints))
for _, endpoint := range endpoints {
weightedScorePerEndpoint[endpoint] = float64(0) // initialize weighted score per endpoint with 0 value
}
// Cache the debug logger and its enabled state once. The per-endpoint Info
// call below evaluates and boxes its variadic args even when the verbosity
// gate would suppress output; on a 100-endpoint, 4-scorer fleet that line
// alone accounted for ~80% of total allocations per Scheduler.Schedule call.
// Guarding by Enabled() preserves debugging behavior while removing the
// per-endpoint allocation when DEBUG logging is off (the production default).
debug := logger.V(logutil.DEBUG)
debugEnabled := debug.Enabled()
// Iterate through each scorer in the chain and accumulate the weighted scores.
for _, scorer := range p.scorers {
logger.V(logutil.VERBOSE).Info("Running scorer plugin", "plugin", scorer.TypedName())
before := time.Now()
scores := scorer.Score(ctx, request, endpoints)
metrics.RecordPluginProcessingLatency(scorerExtensionPoint, scorer.TypedName().Type, scorer.TypedName().Name, time.Since(before))
for endpoint, score := range scores { // weight is relative to the sum of weights
if debugEnabled {
debug.Info("Calculated score", "plugin", scorer.TypedName(), "endpoint", endpoint.GetMetadata().NamespacedName, "score", score)
}
weightedScorePerEndpoint[endpoint] += enforceScoreRange(score) * scorer.Weight()
}
debug.Info("Completed running scorer plugin successfully", "plugin", scorer.TypedName())
}
logger.V(logutil.VERBOSE).Info("Completed running scorer plugins successfully")
return weightedScorePerEndpoint
}
func (p *SchedulerProfile) runPickerPlugin(ctx context.Context, request *fwksched.InferenceRequest, weightedScorePerEndpoint map[fwksched.Endpoint]float64) *fwksched.ProfileRunResult {
logger := log.FromContext(ctx)
// Allocate the ScoredEndpoint values as a single contiguous backing array
// and build the picker's pointer slice by indexing into it. Previously each
// per-endpoint &ScoredEndpoint{...} was a separate heap allocation, which
// at production fleet sizes (~100 pods) dominated per-request picker cost.
// Pickers reorder the pointer slice (shuffle/sort) but do not realloc, so
// pointer aliasing into the backing array is safe.
n := len(weightedScorePerEndpoint)
storage := make([]fwksched.ScoredEndpoint, n)
scoredEndpoints := make([]*fwksched.ScoredEndpoint, n)
i := 0
for endpoint, score := range weightedScorePerEndpoint {
storage[i] = fwksched.ScoredEndpoint{Endpoint: endpoint, Score: score}
scoredEndpoints[i] = &storage[i]
i++
}
logger.V(logutil.VERBOSE).Info("Running picker plugin", "plugin", p.picker.TypedName())
logger.V(logutil.DEBUG).Info("Candidate pods for picking", "endpoints-weighted-score", scoredEndpoints)
ctx, span := tracing.Tracer(TracerScope).Start(ctx, "pick_endpoints",
trace.WithSpanKind(trace.SpanKindInternal),
)
defer span.End()
span.SetAttributes(attribute.Int("llm_d.epp.picker.candidate_endpoints", len(scoredEndpoints)))
// The picker almost always returns a single target, so its count carries
// little signal. The score distribution across the strongest candidates is
// what explains why an endpoint was chosen, so record the highest-scoring
// few (names with their weighted scores). Captured before Pick because
// pickers reorder scoredEndpoints in place.
if names, scores := topScoredEndpoints(scoredEndpoints, maxTracedEndpointScores); len(names) > 0 {
span.SetAttributes(
attribute.StringSlice("llm_d.epp.picker.top_endpoints", names),
attribute.Float64Slice("llm_d.epp.picker.top_scores", scores),
)
}
if request != nil {
if request.TargetModel != "" {
span.SetAttributes(attribute.String("gen_ai.request.model", request.TargetModel))
}
if request.RequestID != "" {
span.SetAttributes(attribute.String("gen_ai.request.id", request.RequestID))
}
}
before := time.Now()
result := p.picker.Pick(ctx, scoredEndpoints)
metrics.RecordPluginProcessingLatency(pickerExtensionPoint, p.picker.TypedName().Type, p.picker.TypedName().Name, time.Since(before))
logger.V(logutil.DEBUG).Info("Completed running picker plugin successfully", "plugin", p.picker.TypedName(), "result", result)
return result
}
// topScoredEndpoints returns the names and weighted scores of the highest
// scoring candidates, ordered by descending score with the endpoint name as a
// stable tiebreaker and capped at limit. The returned slices are index-aligned.
func topScoredEndpoints(scored []*fwksched.ScoredEndpoint, limit int) ([]string, []float64) {
ranked := make([]*fwksched.ScoredEndpoint, len(scored))
copy(ranked, scored)
sort.Slice(ranked, func(i, j int) bool {
if ranked[i].Score != ranked[j].Score {
return ranked[i].Score > ranked[j].Score
}
return ranked[i].GetMetadata().NamespacedName.String() <
ranked[j].GetMetadata().NamespacedName.String()
})
if limit < len(ranked) {
ranked = ranked[:limit]
}
names := make([]string, len(ranked))
scores := make([]float64, len(ranked))
for i, se := range ranked {
names[i] = se.GetMetadata().NamespacedName.String()
scores[i] = se.Score
}
return names, scores
}
func enforceScoreRange(score float64) float64 {
if score < 0 {
return 0
}
if score > 1 {
return 1
}
return score
}