-
Notifications
You must be signed in to change notification settings - Fork 260
feature/prefix-aware-routing #546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
YuhanLiu11
merged 14 commits into
vllm-project:main
from
BrianPark314:feature/prefix-aware-routing
Aug 6, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e273d6b
feat: add vllmruntime
e7edcbd
Merge branch 'feature/gateway-inference-extension'
53500d6
feat: add prefix aware picker
bca73e5
feat: add prefix aware picker
9cb5ea8
Merge branch 'main' into feature/prefix-aware-routing
d6f4f7d
feat: update prefix aware logic
81123e5
chore: revert roundrobin picker
e6633bd
chore: fix pa picker logic
6fdbf68
Merge branch 'main' into feature/prefix-aware-routing
BrianPark314 9dcc93a
chore: add newline
f7857fd
Merge remote-tracking branch 'origin/feature/prefix-aware-routing' in…
912ed36
Merge branch 'main' into feature/prefix-aware-routing
BrianPark314 7f11560
Merge branch 'main' into feature/prefix-aware-routing
YuhanLiu11 af817d7
Merge branch 'main' into feature/prefix-aware-routing
BrianPark314 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
/* | ||
Copyright 2025 The vLLM Production Stack 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 | ||
*/ | ||
|
||
package picker | ||
|
||
import ( | ||
"math/rand" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/cespare/xxhash/v2" | ||
|
||
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins" | ||
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" | ||
) | ||
|
||
const chunkSize = 128 | ||
|
||
var _ plugins.Picker = &PrefixMatchPicker{} | ||
|
||
// PrefixMatchPicker selects the engine whose URL was returned by the | ||
// longest-prefix match against previously-seen prompts (same idea as the | ||
// Python `route_request`). Ties are broken at random. | ||
type PrefixMatchPicker struct { | ||
trie *hashTrie | ||
rnd *rand.Rand | ||
} | ||
|
||
// NewPrefixMatchPicker returns a ready-to-use picker instance. | ||
func NewPrefixMatchPicker() *PrefixMatchPicker { | ||
return &PrefixMatchPicker{ | ||
trie: newHashTrie(), | ||
rnd: rand.New(rand.NewSource(time.Now().UnixNano())), | ||
} | ||
} | ||
|
||
func (p *PrefixMatchPicker) Name() string { return "prefixmatch" } | ||
|
||
// Pick implements plugins.Picker. | ||
// | ||
// SchedulingContext is assumed to carry the inference request body in | ||
// ctx.RequestBody (map[string]any) with the prompt at key "prompt". Adjust | ||
// the accessor if your integration differs. | ||
func (p *PrefixMatchPicker) Pick( | ||
ctx *types.SchedulingContext, | ||
scoredPods []*types.ScoredPod, | ||
) *types.Result { | ||
if len(scoredPods) == 0 { | ||
return &types.Result{} | ||
} | ||
|
||
var prompt string | ||
|
||
if msgs, ok := ctx.RequestBody["messages"]; ok { | ||
if arr, ok := msgs.([]any); ok { | ||
var parts []string | ||
for _, m := range arr { | ||
mm, ok := m.(map[string]any) | ||
if !ok { | ||
continue | ||
} | ||
switch c := mm["content"].(type) { | ||
case string: | ||
parts = append(parts, c) | ||
case []any: | ||
for _, part := range c { | ||
mp, ok := part.(map[string]any) | ||
if !ok { | ||
continue | ||
} | ||
if mp["type"] == "text" { | ||
if txt, ok := mp["text"].(string); ok { | ||
parts = append(parts, txt) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
prompt = strings.Join(parts, "\n") | ||
} | ||
} | ||
|
||
if prompt == "" { | ||
prompt, _ = ctx.RequestBody["prompt"].(string) | ||
} | ||
|
||
// 1. Build the set of available endpoints. | ||
available := make(map[string]struct{}, len(scoredPods)) | ||
for _, sp := range scoredPods { | ||
ep := sp.GetPod().EndpointURL // <-- adapt this accessor | ||
available[ep] = struct{}{} | ||
} | ||
|
||
// 2. Longest-prefix match within the trie. | ||
matched := p.trie.longestPrefixMatch(prompt, available) | ||
|
||
// 3. Fallback: no match --> all endpoints are candidates. | ||
if len(matched) == 0 { | ||
for ep := range available { | ||
matched[ep] = struct{}{} | ||
} | ||
} | ||
|
||
// 4. Convert the matched set to a slice and pick randomly. | ||
endpoints := make([]string, 0, len(matched)) | ||
for ep := range matched { | ||
endpoints = append(endpoints, ep) | ||
} | ||
selected := endpoints[p.rnd.Intn(len(endpoints))] | ||
|
||
// 5. Cache the decision for future prefix look-ups. | ||
p.trie.insert(prompt, selected) | ||
|
||
// 6. Return the pod whose URL matches `selected`. | ||
for _, sp := range scoredPods { | ||
if sp.GetPod().EndpointURL == selected { // same accessor as above | ||
return &types.Result{TargetPod: sp} | ||
} | ||
} | ||
// Should never hit; safe fallback. | ||
return &types.Result{TargetPod: scoredPods[0]} | ||
} | ||
|
||
/*---------------------------- trie implementation ---------------------------*/ | ||
|
||
BrianPark314 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type hashTrie struct { | ||
mu sync.RWMutex | ||
children map[uint64]*hashTrie | ||
endpoints map[string]struct{} | ||
} | ||
|
||
func newHashTrie() *hashTrie { | ||
return &hashTrie{children: make(map[uint64]*hashTrie)} | ||
} | ||
|
||
func intersection(a, b map[string]struct{}) map[string]struct{} { | ||
res := make(map[string]struct{}) | ||
for k := range a { | ||
if _, ok := b[k]; ok { | ||
res[k] = struct{}{} | ||
} | ||
} | ||
return res | ||
} | ||
|
||
func chunkAndHash(s string) []uint64 { | ||
hashes := make([]uint64, 0, (len(s)+chunkSize-1)/chunkSize) | ||
for i := 0; i < len(s); i += chunkSize { | ||
end := i + chunkSize | ||
if end > len(s) { | ||
end = len(s) | ||
} | ||
hashes = append(hashes, xxhash.Sum64([]byte(s[i:end]))) | ||
} | ||
return hashes | ||
} | ||
|
||
func (t *hashTrie) insert(key, endpoint string) { | ||
BrianPark314 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
|
||
node := t | ||
if node.endpoints == nil { | ||
node.endpoints = make(map[string]struct{}) | ||
} | ||
node.endpoints[endpoint] = struct{}{} | ||
|
||
for _, h := range chunkAndHash(key) { | ||
child, ok := node.children[h] | ||
if !ok { | ||
child = newHashTrie() | ||
node.children[h] = child | ||
} | ||
node = child | ||
if node.endpoints == nil { | ||
node.endpoints = make(map[string]struct{}) | ||
} | ||
node.endpoints[endpoint] = struct{}{} | ||
} | ||
} | ||
|
||
func (t *hashTrie) longestPrefixMatch( | ||
key string, | ||
available map[string]struct{}, | ||
) map[string]struct{} { | ||
t.mu.RLock() | ||
defer t.mu.RUnlock() | ||
|
||
node := t | ||
matched := intersection(node.endpoints, available) | ||
|
||
for _, h := range chunkAndHash(key) { | ||
child, ok := node.children[h] | ||
if !ok { | ||
break | ||
} | ||
node = child | ||
cand := intersection(node.endpoints, available) | ||
if len(cand) == 0 { | ||
break | ||
} | ||
matched = cand | ||
} | ||
return matched | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.