|
| 1 | +/* |
| 2 | +Copyright 2026 The llm-d 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 gpuutilization |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 25 | + |
| 26 | + logutil "github.com/llm-d/llm-d-router/pkg/common/observability/logging" |
| 27 | + fwkplugin "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/plugin" |
| 28 | + fwksched "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/scheduling" |
| 29 | + attrgpu "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/datalayer/attribute/gpu" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + PluginType = "gpu-utilization-filter" |
| 34 | + DefaultThreshold = 0.90 |
| 35 | +) |
| 36 | + |
| 37 | +var _ fwksched.Filter = &Plugin{} |
| 38 | + |
| 39 | +// Config holds configurable parameters for the GPU utilization filter. |
| 40 | +type Config struct { |
| 41 | + // Threshold is the maximum GPU utilization (0.0-1.0) an endpoint may |
| 42 | + // have before it is filtered out. Default: 0.90 (90%). |
| 43 | + Threshold float64 `json:"threshold,omitempty"` |
| 44 | +} |
| 45 | + |
| 46 | +// Plugin filters endpoints whose GPU utilization exceeds a configurable |
| 47 | +// threshold. If all endpoints exceed the threshold, all are kept (graceful |
| 48 | +// fallback). Endpoints without GPU data pass through unconditionally. |
| 49 | +type Plugin struct { |
| 50 | + typedName fwkplugin.TypedName |
| 51 | + config Config |
| 52 | + gpuUtilDataKey fwkplugin.DataKey |
| 53 | +} |
| 54 | + |
| 55 | +// Factory creates a gpu-utilization-filter plugin from configuration. |
| 56 | +func Factory(name string, rawParameters *json.Decoder, _ fwkplugin.Handle) (fwkplugin.Plugin, error) { |
| 57 | + config := Config{Threshold: DefaultThreshold} |
| 58 | + if rawParameters != nil { |
| 59 | + if err := rawParameters.Decode(&config); err != nil { |
| 60 | + return nil, fmt.Errorf("failed to unmarshal config: %w", err) |
| 61 | + } |
| 62 | + } |
| 63 | + if config.Threshold < 0 || config.Threshold > 1.0 { |
| 64 | + return nil, fmt.Errorf("threshold must be in [0, 1], got %f", config.Threshold) |
| 65 | + } |
| 66 | + return &Plugin{ |
| 67 | + typedName: fwkplugin.TypedName{Type: PluginType, Name: name}, |
| 68 | + config: config, |
| 69 | + gpuUtilDataKey: attrgpu.GPUUtilizationDataKey, |
| 70 | + }, nil |
| 71 | +} |
| 72 | + |
| 73 | +func (p *Plugin) TypedName() fwkplugin.TypedName { |
| 74 | + return p.typedName |
| 75 | +} |
| 76 | + |
| 77 | +// Filter keeps endpoints whose GPU utilization is at or below the configured |
| 78 | +// threshold. Endpoints without GPU utilization data pass through (the DCGM |
| 79 | +// poller may not have scraped yet). If every endpoint with data exceeds the |
| 80 | +// threshold, all original endpoints are returned as a graceful fallback. |
| 81 | +func (p *Plugin) Filter(ctx context.Context, _ *fwksched.InferenceRequest, endpoints []fwksched.Endpoint) []fwksched.Endpoint { |
| 82 | + logger := log.FromContext(ctx) |
| 83 | + |
| 84 | + if len(endpoints) <= 1 { |
| 85 | + return endpoints |
| 86 | + } |
| 87 | + |
| 88 | + var passed []fwksched.Endpoint |
| 89 | + for _, ep := range endpoints { |
| 90 | + raw, ok := ep.Get(p.gpuUtilDataKey.String()) |
| 91 | + if !ok { |
| 92 | + passed = append(passed, ep) |
| 93 | + continue |
| 94 | + } |
| 95 | + util := raw.(attrgpu.GPUUtilization) |
| 96 | + if float64(util) <= p.config.Threshold { |
| 97 | + passed = append(passed, ep) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if len(passed) == 0 { |
| 102 | + logger.V(logutil.DEBUG).Info("GPUUtilizationFilter: all endpoints above threshold, keeping all", |
| 103 | + "threshold", p.config.Threshold, "total", len(endpoints)) |
| 104 | + return endpoints |
| 105 | + } |
| 106 | + |
| 107 | + logger.V(logutil.DEBUG).Info("GPUUtilizationFilter: filtered endpoints", |
| 108 | + "threshold", p.config.Threshold, "passed", len(passed), "total", len(endpoints)) |
| 109 | + return passed |
| 110 | +} |
| 111 | + |
| 112 | +// Consumes declares a Required dependency on GPUUtilization so the framework |
| 113 | +// validates at init that a producer (dcgm-extractor) is configured. |
| 114 | +func (p *Plugin) Consumes() fwkplugin.DataDependencies { |
| 115 | + return fwkplugin.DataDependencies{ |
| 116 | + Required: map[fwkplugin.DataKey]any{p.gpuUtilDataKey: attrgpu.GPUUtilization(0)}, |
| 117 | + } |
| 118 | +} |
0 commit comments