|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package endpointattribute |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "math" |
| 25 | + |
| 26 | + fwkplugin "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/plugin" |
| 27 | + fwksched "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/scheduling" |
| 28 | + attrmetrics "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/datalayer/attribute/metrics" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + EndpointAttributeScorerType = "endpoint-attribute-scorer" |
| 33 | + |
| 34 | + algorithmLinearLowerIsBetter = "linear_lower_is_better" |
| 35 | + algorithmLinearHigherIsBetter = "linear_higher_is_better" |
| 36 | +) |
| 37 | + |
| 38 | +// compile-time type assertion |
| 39 | +var _ fwksched.Scorer = &EndpointAttributeScorer{} |
| 40 | + |
| 41 | +// fixedRangeParameters normalizes the attribute value against a fixed |
| 42 | +// [min, max] range (e.g. kv-cache utilization, which is always in [0, 1]). |
| 43 | +type fixedRangeParameters struct { |
| 44 | + Min float64 `json:"min"` |
| 45 | + Max float64 `json:"max"` |
| 46 | +} |
| 47 | + |
| 48 | +// adaptiveRangeParameters normalizes the attribute value against an adaptive |
| 49 | +// [min, max] range computed across the candidate endpoints (e.g. queue depth). |
| 50 | +type adaptiveRangeParameters struct{} |
| 51 | + |
| 52 | +// normalizationParameters selects the normalization strategy. At most one |
| 53 | +// strategy may be set; adaptiveRange is the default when none is set. |
| 54 | +type normalizationParameters struct { |
| 55 | + FixedRange *fixedRangeParameters `json:"fixedRange,omitempty"` |
| 56 | + AdaptiveRange *adaptiveRangeParameters `json:"adaptiveRange,omitempty"` |
| 57 | +} |
| 58 | + |
| 59 | +type algorithmParameters struct { |
| 60 | + Type string `json:"type"` |
| 61 | + Normalization normalizationParameters `json:"normalization"` |
| 62 | +} |
| 63 | + |
| 64 | +type parameters struct { |
| 65 | + AttributeKey string `json:"attributeKey"` |
| 66 | + Algorithm algorithmParameters `json:"algorithm"` |
| 67 | +} |
| 68 | + |
| 69 | +// EndpointAttributeScorerFactory defines the factory function for EndpointAttributeScorer. |
| 70 | +func EndpointAttributeScorerFactory(name string, decoder *json.Decoder, _ fwkplugin.Handle) (fwkplugin.Plugin, error) { |
| 71 | + var params parameters |
| 72 | + if decoder != nil { |
| 73 | + if err := decoder.Decode(¶ms); err != nil { |
| 74 | + return nil, fmt.Errorf("failed to decode endpoint attribute scorer parameters: %w", err) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return NewEndpointAttributeScorer(name, params) |
| 79 | +} |
| 80 | + |
| 81 | +// NewEndpointAttributeScorer validates the given parameters and returns a new |
| 82 | +// EndpointAttributeScorer with the given name. |
| 83 | +func NewEndpointAttributeScorer(name string, params parameters) (*EndpointAttributeScorer, error) { |
| 84 | + if name == "" { |
| 85 | + name = EndpointAttributeScorerType |
| 86 | + } |
| 87 | + if params.AttributeKey == "" { |
| 88 | + return nil, errors.New("endpoint attribute scorer requires a non-empty attributeKey") |
| 89 | + } |
| 90 | + switch params.Algorithm.Type { |
| 91 | + case algorithmLinearLowerIsBetter, algorithmLinearHigherIsBetter: |
| 92 | + default: |
| 93 | + return nil, fmt.Errorf("endpoint attribute scorer algorithm.type must be %q or %q, got %q", |
| 94 | + algorithmLinearLowerIsBetter, algorithmLinearHigherIsBetter, params.Algorithm.Type) |
| 95 | + } |
| 96 | + |
| 97 | + normalization := params.Algorithm.Normalization |
| 98 | + if normalization.FixedRange != nil && normalization.AdaptiveRange != nil { |
| 99 | + return nil, errors.New("endpoint attribute scorer allows at most one normalization strategy, got both fixedRange and adaptiveRange") |
| 100 | + } |
| 101 | + if fixed := normalization.FixedRange; fixed != nil && fixed.Min >= fixed.Max { |
| 102 | + return nil, fmt.Errorf("endpoint attribute scorer normalization.fixedRange requires min < max, got min %v, max %v", |
| 103 | + fixed.Min, fixed.Max) |
| 104 | + } |
| 105 | + |
| 106 | + return &EndpointAttributeScorer{ |
| 107 | + typedName: fwkplugin.TypedName{Type: EndpointAttributeScorerType, Name: name}, |
| 108 | + attributeKey: params.AttributeKey, |
| 109 | + lowerIsBetter: params.Algorithm.Type == algorithmLinearLowerIsBetter, |
| 110 | + fixedRange: normalization.FixedRange, |
| 111 | + }, nil |
| 112 | +} |
| 113 | + |
| 114 | +// EndpointAttributeScorer scores candidate endpoints by a single configured |
| 115 | +// numeric endpoint attribute (produced by the custom metrics extraction layer), |
| 116 | +// linearly normalized against either a fixed [min, max] range or an adaptive |
| 117 | +// range computed across the candidates. |
| 118 | +type EndpointAttributeScorer struct { |
| 119 | + typedName fwkplugin.TypedName |
| 120 | + attributeKey string |
| 121 | + lowerIsBetter bool |
| 122 | + // fixedRange selects fixed-range normalization when set; adaptive-range |
| 123 | + // normalization is used otherwise. |
| 124 | + fixedRange *fixedRangeParameters |
| 125 | +} |
| 126 | + |
| 127 | +// TypedName returns the type and name tuple of this plugin instance. |
| 128 | +func (s *EndpointAttributeScorer) TypedName() fwkplugin.TypedName { |
| 129 | + return s.typedName |
| 130 | +} |
| 131 | + |
| 132 | +// Category returns the preference the scorer applies when scoring candidate endpoints. |
| 133 | +func (s *EndpointAttributeScorer) Category() fwksched.ScorerCategory { |
| 134 | + return fwksched.Distribution |
| 135 | +} |
| 136 | + |
| 137 | +// Consumes returns the list of data that is consumed by the plugin. |
| 138 | +func (s *EndpointAttributeScorer) Consumes() map[string]any { |
| 139 | + return map[string]any{ |
| 140 | + s.attributeKey: attrmetrics.ScalarMetricValue(0), |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// Score returns the scoring result for the given list of endpoints based on the |
| 145 | +// configured endpoint attribute. Endpoints missing the attribute score 0. |
| 146 | +func (s *EndpointAttributeScorer) Score(_ context.Context, _ *fwksched.InferenceRequest, endpoints []fwksched.Endpoint) map[fwksched.Endpoint]float64 { |
| 147 | + if s.fixedRange != nil { |
| 148 | + return s.scoreFixedRange(endpoints) |
| 149 | + } |
| 150 | + return s.scoreAdaptiveRange(endpoints) |
| 151 | +} |
| 152 | + |
| 153 | +// scoreFixedRange normalizes each endpoint's attribute value against the |
| 154 | +// configured [min, max] range, clamping values outside the range. |
| 155 | +func (s *EndpointAttributeScorer) scoreFixedRange(endpoints []fwksched.Endpoint) map[fwksched.Endpoint]float64 { |
| 156 | + scores := make(map[fwksched.Endpoint]float64, len(endpoints)) |
| 157 | + for _, endpoint := range endpoints { |
| 158 | + value, ok := attrmetrics.ReadScalarMetricValue(endpoint, s.attributeKey) |
| 159 | + if !ok { |
| 160 | + scores[endpoint] = 0.0 |
| 161 | + continue |
| 162 | + } |
| 163 | + normalized := (float64(value) - s.fixedRange.Min) / (s.fixedRange.Max - s.fixedRange.Min) |
| 164 | + normalized = math.Max(0.0, math.Min(1.0, normalized)) |
| 165 | + if s.lowerIsBetter { |
| 166 | + normalized = 1.0 - normalized |
| 167 | + } |
| 168 | + scores[endpoint] = normalized |
| 169 | + } |
| 170 | + return scores |
| 171 | +} |
| 172 | + |
| 173 | +// scoreAdaptiveRange normalizes each endpoint's attribute value against the |
| 174 | +// [min, max] range observed across the candidates. Endpoints missing the |
| 175 | +// attribute do not participate in the range. If all endpoints that have the |
| 176 | +// attribute share the same value, they all receive a neutral score of 1.0. |
| 177 | +func (s *EndpointAttributeScorer) scoreAdaptiveRange(endpoints []fwksched.Endpoint) map[fwksched.Endpoint]float64 { |
| 178 | + values := make(map[fwksched.Endpoint]float64, len(endpoints)) |
| 179 | + minValue := math.Inf(1) |
| 180 | + maxValue := math.Inf(-1) |
| 181 | + |
| 182 | + for _, endpoint := range endpoints { |
| 183 | + value, ok := attrmetrics.ReadScalarMetricValue(endpoint, s.attributeKey) |
| 184 | + if !ok { |
| 185 | + continue |
| 186 | + } |
| 187 | + floatValue := float64(value) |
| 188 | + values[endpoint] = floatValue |
| 189 | + if floatValue < minValue { |
| 190 | + minValue = floatValue |
| 191 | + } |
| 192 | + if floatValue > maxValue { |
| 193 | + maxValue = floatValue |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + scores := make(map[fwksched.Endpoint]float64, len(endpoints)) |
| 198 | + for _, endpoint := range endpoints { |
| 199 | + value, ok := values[endpoint] |
| 200 | + if !ok { |
| 201 | + scores[endpoint] = 0.0 |
| 202 | + continue |
| 203 | + } |
| 204 | + if maxValue == minValue { |
| 205 | + // All endpoints with the attribute have the same value, return a neutral score. |
| 206 | + scores[endpoint] = 1.0 |
| 207 | + continue |
| 208 | + } |
| 209 | + normalized := (value - minValue) / (maxValue - minValue) |
| 210 | + if s.lowerIsBetter { |
| 211 | + normalized = 1.0 - normalized |
| 212 | + } |
| 213 | + scores[endpoint] = normalized |
| 214 | + } |
| 215 | + return scores |
| 216 | +} |
0 commit comments