-
Notifications
You must be signed in to change notification settings - Fork 266
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
add request routers - least kv cache, least expected latency #543
Changes from all commits
e9f0870
1da1323
20a750a
4db3894
3e1f5ad
6da3e01
ec0e302
2140ba1
490f153
1616aaa
63e289d
77ed626
f933884
c91443f
6b671be
ec2cff9
22e0fa6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright 2024 The Aibrix Team. | ||
|
||
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 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package routingalgorithms | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math" | ||
"math/rand" | ||
|
||
"github.com/aibrix/aibrix/pkg/cache" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type leastBusyTimeRouter struct { | ||
cache *cache.Cache | ||
} | ||
|
||
func NewLeastBusyTimeRouter() Router { | ||
cacheFetched, err := cache.GetCache() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return leastBusyTimeRouter{ | ||
cache: cacheFetched, | ||
} | ||
} | ||
|
||
func (r leastBusyTimeRouter) Route(ctx context.Context, pods map[string]*v1.Pod, model string) (string, error) { | ||
var targetPodIP string | ||
minBusyTimeRatio := math.MaxFloat64 // <= 1 in general | ||
|
||
if len(pods) == 0 { | ||
return "", fmt.Errorf("no pods to forward request") | ||
} | ||
|
||
for _, pod := range pods { | ||
if pod.Status.PodIP == "" { | ||
continue | ||
} | ||
|
||
busyTimeRatio, err := r.cache.GetPodMetric(pod.Name, "gpu_busy_time_ratio") // todo: replace mock | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
klog.V(4).Infof("pod: %v, podIP: %v, GPU busy time ratio: %v", pod.Name, pod.Status.PodIP, busyTimeRatio.GetSimpleValue()) | ||
|
||
if busyTimeRatio.GetSimpleValue() < minBusyTimeRatio { | ||
minBusyTimeRatio = busyTimeRatio.GetSimpleValue() | ||
targetPodIP = pod.Status.PodIP | ||
} | ||
} | ||
|
||
// Use fallback if no valid metrics | ||
if targetPodIP == "" { | ||
klog.Warning("No pods with valid metrics found; selecting a pod randomly as fallback") | ||
var err error | ||
targetPodIP, err = selectRandomPod(pods, rand.Intn) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
if targetPodIP == "" { | ||
return "", fmt.Errorf("no pods to forward request") | ||
} | ||
|
||
return targetPodIP + ":" + podMetricPort, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2024 The Aibrix Team. | ||
|
||
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 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package routingalgorithms | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math" | ||
"math/rand" | ||
|
||
"github.com/aibrix/aibrix/pkg/cache" | ||
metrics "github.com/aibrix/aibrix/pkg/metrics" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type leastKvCacheRouter struct { | ||
cache *cache.Cache | ||
} | ||
|
||
func NewLeastKvCacheRouter() Router { | ||
cache, err := cache.GetCache() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return leastKvCacheRouter{ | ||
cache: cache, | ||
} | ||
} | ||
|
||
func (r leastKvCacheRouter) Route(ctx context.Context, pods map[string]*v1.Pod, model string) (string, error) { | ||
var targetPodIP string | ||
minKvCache := math.MaxFloat64 | ||
|
||
if len(pods) == 0 { | ||
return "", fmt.Errorf("no pods to forward request") | ||
} | ||
|
||
for _, pod := range pods { | ||
if pod.Status.PodIP == "" { | ||
continue | ||
} | ||
|
||
gpuCache, err := r.cache.GetPodMetric(pod.Name, metrics.GPUCacheUsagePerc) | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
cpuCache, err := r.cache.GetPodMetric(pod.Name, metrics.CPUCacheUsagePerc) | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
totalCache := gpuCache.GetSimpleValue() + cpuCache.GetSimpleValue() | ||
|
||
klog.V(4).Infof("pod: %v, podIP: %v, gpuCache: %v, cpuCache: %v, kaCache: %v", | ||
pod.Name, pod.Status.PodIP, gpuCache.GetSimpleValue(), cpuCache.GetSimpleValue(), totalCache) | ||
|
||
if totalCache <= minKvCache { | ||
minKvCache = totalCache | ||
targetPodIP = pod.Status.PodIP | ||
} | ||
} | ||
|
||
// Use fallback if no valid metrics | ||
if targetPodIP == "" { | ||
klog.Warning("No pods with valid metrics found; selecting a pod randomly as fallback") | ||
var err error | ||
targetPodIP, err = selectRandomPod(pods, rand.Intn) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
if targetPodIP == "" { | ||
return "", fmt.Errorf("no pods to forward request") | ||
} | ||
|
||
klog.V(4).Infof("targetPodIP: %v", targetPodIP) | ||
return targetPodIP + ":" + podMetricPort, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
Copyright 2024 The Aibrix Team. | ||
|
||
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 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package routingalgorithms | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math" | ||
"math/rand" | ||
|
||
"github.com/aibrix/aibrix/pkg/cache" | ||
"github.com/aibrix/aibrix/pkg/metrics" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type leastExpectedLatencyRouter struct { | ||
cache *cache.Cache | ||
} | ||
|
||
func NewLeastExpectedLatencyRouter() Router { | ||
cache, err := cache.GetCache() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return leastExpectedLatencyRouter{ | ||
cache: cache, | ||
} | ||
} | ||
|
||
func (r leastExpectedLatencyRouter) Route(ctx context.Context, pods map[string]*v1.Pod, model string) (string, error) { | ||
var targetPodIP string | ||
minExpectedLatency := math.MaxFloat64 | ||
|
||
if len(pods) == 0 { | ||
return "", fmt.Errorf("no pods to forward request") | ||
} | ||
|
||
sumPromptTokens := 0.0 | ||
sumGenerationTokens := 0.0 | ||
cntPromt := 0 | ||
cntGeneration := 0 | ||
for _, pod := range pods { | ||
avgPromptTokens, err := r.cache.GetPodModelMetric(pod.Name, model, metrics.AvgPromptToksPerReq) | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
avgGenerationTokens, err := r.cache.GetPodModelMetric(pod.Name, model, metrics.AvgGenerationToksPerReq) | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
if avgPromptTokens.GetSimpleValue() > 0 { | ||
sumPromptTokens += avgPromptTokens.GetSimpleValue() | ||
cntPromt += 1 | ||
} | ||
if avgGenerationTokens.GetSimpleValue() > 0 { | ||
sumGenerationTokens += avgGenerationTokens.GetSimpleValue() | ||
cntGeneration += 1 | ||
} | ||
} | ||
guessPromptTokens := 10.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am curious about the 10.0 and following 100.0. are these numbers meaningful or just for initializaiton? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just for initialization, need experiments to be tuned. |
||
if cntPromt > 0 { | ||
guessPromptTokens = sumPromptTokens / float64(cntPromt) | ||
} | ||
guessGenerationTokens := 100.0 | ||
if cntGeneration > 0 { | ||
guessGenerationTokens = sumGenerationTokens / float64(cntGeneration) | ||
} | ||
|
||
for _, pod := range pods { | ||
if pod.Status.PodIP == "" { | ||
continue | ||
} | ||
|
||
// expected queuing latency | ||
queuingLatency, err := r.cache.GetPodModelMetric(pod.Name, model, metrics.RequestQueueTimeSeconds) | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
|
||
// expected prefill latency | ||
avgPromptTokens, err := r.cache.GetPodModelMetric(pod.Name, model, metrics.AvgPromptToksPerReq) | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
PrefillTime, err := r.cache.GetPodModelMetric(pod.Name, model, metrics.RequestPrefillTimeSeconds) | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
prefillLatency := PrefillTime.GetHistogramValue().GetMean() / avgPromptTokens.GetSimpleValue() * guessPromptTokens | ||
|
||
// expected decode latency | ||
avgGenerationTokens, err := r.cache.GetPodModelMetric(pod.Name, model, metrics.AvgGenerationToksPerReq) | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
DecodeTime, err := r.cache.GetPodModelMetric(pod.Name, model, metrics.RequestDecodeTimeSeconds) | ||
if err != nil { | ||
klog.Error(err) | ||
continue | ||
} | ||
decodeLatency := DecodeTime.GetHistogramValue().GetMean() / avgGenerationTokens.GetSimpleValue() * guessGenerationTokens | ||
|
||
totalExpectedLatency := queuingLatency.GetSimpleValue() + prefillLatency + decodeLatency | ||
klog.V(4).Infof("pod: %v, podIP: %v, queuingLatency: %v, prefillLatency: %v, decodeLatency: %v, totalExpectedLatency: %v", | ||
pod.Name, pod.Status.PodIP, queuingLatency.GetSimpleValue(), prefillLatency, decodeLatency, totalExpectedLatency) | ||
|
||
if totalExpectedLatency <= minExpectedLatency { | ||
minExpectedLatency = totalExpectedLatency | ||
targetPodIP = pod.Status.PodIP | ||
} | ||
} | ||
|
||
// Use fallback if no valid metrics | ||
if targetPodIP == "" { | ||
klog.Warning("No pods with valid metrics found; selecting a pod randomly as fallback") | ||
var err error | ||
targetPodIP, err = selectRandomPod(pods, rand.Intn) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
if targetPodIP == "" { | ||
return "", fmt.Errorf("no pods to forward request") | ||
} | ||
|
||
return targetPodIP + ":" + podMetricPort, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what metric do you expect here? GPU utilization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, GPU utilization measured by busy time ratio. We've discussed with @brosoul and this metric will be added later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good. for metrics not that mature, we can add some
// TODOs
and do not enable the policy in the initialization, which means we can still merge the PR without waiting.