|
| 1 | +package scorer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 9 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins" |
| 10 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework" |
| 11 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" |
| 12 | + logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging" |
| 13 | + |
| 14 | + "github.com/llm-d/llm-d-inference-scheduler/pkg/plugins/profile" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + // ProximityScorerType is the type of the ProximityScorer |
| 19 | + ProximityScorerType = "proximity-scorer" |
| 20 | +) |
| 21 | + |
| 22 | +// ProximityScorerParams defines configuration parameters for the proximity scorer |
| 23 | +type ProximityScorerParams struct { |
| 24 | + SameNodeScore float64 `json:"sameNodeScore"` // Score for same node (NVLink ~900GB/s) |
| 25 | + DefaultScore float64 `json:"defaultScore"` // Score for different node (InfiniBand ~400GB/s) |
| 26 | +} |
| 27 | + |
| 28 | +// compile-time type assertion |
| 29 | +var _ framework.Scorer = &ProximityScorer{} |
| 30 | + |
| 31 | +// ProximityScorerFactory defines the factory function for the ProximityScorer |
| 32 | +func ProximityScorerFactory(name string, rawParameters json.RawMessage, _ plugins.Handle) (plugins.Plugin, error) { |
| 33 | + params := ProximityScorerParams{ |
| 34 | + SameNodeScore: 1.0, |
| 35 | + DefaultScore: 0.0, |
| 36 | + } |
| 37 | + |
| 38 | + if rawParameters != nil { |
| 39 | + if err := json.Unmarshal(rawParameters, ¶ms); err != nil { |
| 40 | + return nil, fmt.Errorf("failed to parse proximity scorer parameters: %w", err) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return NewProximityScorer(params).WithName(name), nil |
| 45 | +} |
| 46 | + |
| 47 | +// NewProximityScorer creates a new ProximityScorer instance |
| 48 | +func NewProximityScorer(params ProximityScorerParams) *ProximityScorer { |
| 49 | + return &ProximityScorer{ |
| 50 | + typedName: plugins.TypedName{Type: ProximityScorerType}, |
| 51 | + params: params, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// ProximityScorer is a scorer that prefers prefill pods on the same node as the decode pod |
| 56 | +type ProximityScorer struct { |
| 57 | + typedName plugins.TypedName |
| 58 | + params ProximityScorerParams |
| 59 | +} |
| 60 | + |
| 61 | +// TypedName returns the typed name of the plugin |
| 62 | +func (s *ProximityScorer) TypedName() plugins.TypedName { |
| 63 | + return s.typedName |
| 64 | +} |
| 65 | + |
| 66 | +// WithName sets the name of the plugin |
| 67 | +func (s *ProximityScorer) WithName(name string) *ProximityScorer { |
| 68 | + s.typedName.Name = name |
| 69 | + return s |
| 70 | +} |
| 71 | + |
| 72 | +// Category returns the scorer category (Affinity to prefer locality) |
| 73 | +func (s *ProximityScorer) Category() framework.ScorerCategory { |
| 74 | + return framework.Affinity |
| 75 | +} |
| 76 | + |
| 77 | +// Score assigns scores to prefill pods based on their proximity to the decode pod |
| 78 | +func (s *ProximityScorer) Score(ctx context.Context, cycleState *types.CycleState, |
| 79 | + request *types.LLMRequest, pods []types.Pod) map[types.Pod]float64 { |
| 80 | + |
| 81 | + scores := make(map[types.Pod]float64, len(pods)) |
| 82 | + |
| 83 | + // Read decode pod node name from CycleState |
| 84 | + proximityState, err := types.ReadCycleStateKey[*profile.ProximityState]( |
| 85 | + cycleState, |
| 86 | + plugins.StateKey(profile.ProximityStateKey), |
| 87 | + ) |
| 88 | + |
| 89 | + if err != nil { |
| 90 | + // If decode pod node name not available, give all pods default score |
| 91 | + log.FromContext(ctx).V(logutil.DEBUG).Info( |
| 92 | + "Proximity state not found in CycleState, using default scores", |
| 93 | + "error", err, |
| 94 | + ) |
| 95 | + for _, pod := range pods { |
| 96 | + scores[pod] = s.params.DefaultScore |
| 97 | + } |
| 98 | + return scores |
| 99 | + } |
| 100 | + |
| 101 | + // Score each prefill pod based on proximity to decode pod |
| 102 | + for _, pod := range pods { |
| 103 | + metadata := pod.GetPod() |
| 104 | + |
| 105 | + // Get prefill pod's node name |
| 106 | + prefillNodeName := metadata.NodeName |
| 107 | + |
| 108 | + // Simple binary scoring: same node = 1.0, different node = 0.0 |
| 109 | + score := s.params.DefaultScore |
| 110 | + if prefillNodeName != "" && prefillNodeName == proximityState.DecodeNodeName { |
| 111 | + score = s.params.SameNodeScore // Same node - NVLink (~900GB/s) |
| 112 | + } |
| 113 | + // else: Different node - InfiniBand (~400GB/s) |
| 114 | + |
| 115 | + scores[pod] = score |
| 116 | + |
| 117 | + log.FromContext(ctx).V(logutil.DEBUG).Info("Proximity score calculated", |
| 118 | + "pod", metadata.NamespacedName.String(), |
| 119 | + "prefillNode", prefillNodeName, |
| 120 | + "decodeNode", proximityState.DecodeNodeName, |
| 121 | + "score", score, |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + return scores |
| 126 | +} |
0 commit comments