Skip to content

Commit c6b69f5

Browse files
committed
Fix the scoring function to use matchBlocks instead of totalBlocks
1 parent 33f74a1 commit c6b69f5

3 files changed

Lines changed: 19 additions & 17 deletions

File tree

pkg/epp/framework/plugins/scheduling/scorer/prefix/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ score = prefixLengthWeight * matchLengthRatio + (1.0 - prefixLengthWeight) * mat
1616

1717
Where:
1818
- `matchRatio = matchBlocks / totalBlocks` (the fraction of the request prefix matched)
19-
- `matchLengthRatio = min(1.0, (totalBlocks * blockSize / maxModelLen) ^ 2)` (normalized square of the total request prefix length relative to `maxModelLen`)
19+
- `matchLengthRatio = min(1.0, matchedBlocks * blockSize / maxModelLen) ^ 2` (square of normalized matched blocks relative to `maxModelLen`)
2020

2121
If `prefixLengthWeight` is `0.0` (the default), the score simplifies to just the `matchRatio`.
2222

pkg/epp/framework/plugins/scheduling/scorer/prefix/plugin.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,17 @@ func (p *Plugin) Score(ctx context.Context, _ *fwksched.InferenceRequest, endpoi
138138
}
139139

140140
if prefixMatchInfo, ok := info.(*attrprefix.PrefixCacheMatchInfo); ok {
141+
matchBlocks := prefixMatchInfo.MatchBlocks()
141142
totalBlocks := prefixMatchInfo.TotalBlocks()
142143
if totalBlocks != 0 {
143-
matchRatio := float64(prefixMatchInfo.MatchBlocks()) / float64(totalBlocks)
144+
matchRatio := float64(matchBlocks) / float64(totalBlocks)
144145
matchLengthRatio := 0.0
145146
blockSize := prefixMatchInfo.BlockSizeTokens()
146147
if blockSize > 0 && p.maxModelLen > 0 {
147-
// (TotalBlocks * BlockSize / maxModelLen) ^ 2
148+
// (MatchBlocks * BlockSize / maxModelLen) ^ 2
148149
// Capped at 1.0 as the normalized score term cannot be greater than 1.
149-
matchLengthRatio = float64(totalBlocks) * float64(blockSize) / float64(p.maxModelLen)
150-
matchLengthRatio = math.Min(1.0, matchLengthRatio*matchLengthRatio)
150+
matchLengthRatio = math.Min(1.0, float64(matchBlocks)*float64(blockSize)/float64(p.maxModelLen))
151+
matchLengthRatio *= matchLengthRatio
151152
}
152153
scores[endpoint] = p.prefixLengthWeight*matchLengthRatio + (1.0-p.prefixLengthWeight)*matchRatio
153154
}

pkg/epp/framework/plugins/scheduling/scorer/prefix/plugin_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,26 @@ func TestPrefixPluginScoreWithWeights(t *testing.T) {
5959

6060
key := attrprefix.PrefixCacheMatchInfoDataKey.WithNonEmptyProducerName(producerName).String()
6161

62-
// Endpoint 1: match 50, total 80, block size 1
63-
// matchRatio = 50/80 = 0.625
64-
// matchLengthRatio = 80*1/100 = 0.8 -> squared = 0.64
65-
// score = 0.5 * 0.64 + 0.5 * 0.625 = 0.6325
62+
// Endpoint 1: match 5, total 10, block size 1
63+
// matchRatio = 5/10 = 0.5
64+
// matchLengthRatio = min(1.0, 5*1/100) = 0.05 -> squared = 0.0025
65+
// score = 0.5 * 0.0025 + 0.5 * 0.5 = 0.25125
6666
endpoint1 := fwksched.NewEndpoint(&fwkdl.EndpointMetadata{NamespacedName: k8stypes.NamespacedName{Name: "pod1"}}, fwkdl.NewMetrics(), nil)
67-
endpoint1.Put(key, attrprefix.NewPrefixCacheMatchInfo(50, 80, 1))
67+
endpoint1.Put(key, attrprefix.NewPrefixCacheMatchInfo(5, 10, 1))
6868

69-
// Endpoint 2: match 10, total 200, block size 1 (exceeds maxModelLen)
70-
// matchRatio = 10/200 = 0.05
71-
// matchLengthRatio = 200*1/100 = 2.0 -> squared = 4.0 -> capped at 1.0
72-
// score = 0.5 * 1.0 + 0.5 * 0.05 = 0.525
69+
// Endpoint 2: match 50, total 100, block size 1
70+
// matchRatio = 50/100 = 0.5
71+
// matchLengthRatio = min(1.0, 50*1/100) = 0.5 -> squared = 0.25
72+
// score = 0.5 * 0.25 + 0.5 * 0.5 = 0.375
7373
endpoint2 := fwksched.NewEndpoint(&fwkdl.EndpointMetadata{NamespacedName: k8stypes.NamespacedName{Name: "pod2"}}, fwkdl.NewMetrics(), nil)
74-
endpoint2.Put(key, attrprefix.NewPrefixCacheMatchInfo(10, 200, 1))
74+
endpoint2.Put(key, attrprefix.NewPrefixCacheMatchInfo(50, 100, 1))
7575

7676
endpoints := []fwksched.Endpoint{endpoint1, endpoint2}
7777
scores := p.Score(context.Background(), nil, endpoints)
7878

79-
assert.InDelta(t, 0.6325, scores[endpoint1], 1e-6)
80-
assert.InDelta(t, 0.525, scores[endpoint2], 1e-6)
79+
// matchRatio is the same but we still give longer request higher score
80+
assert.InDelta(t, 0.25125, scores[endpoint1], 1e-6)
81+
assert.InDelta(t, 0.375, scores[endpoint2], 1e-6)
8182
}
8283

8384
func TestPrefixPluginFactoryValidation(t *testing.T) {

0 commit comments

Comments
 (0)