/
runner.go
377 lines (329 loc) · 12.5 KB
/
runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
Copyright 2023 KubeAGI.
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 worker
import (
"context"
"errors"
"fmt"
"strconv"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
arcadiav1alpha1 "github.com/kubeagi/arcadia/api/base/v1alpha1"
"github.com/kubeagi/arcadia/pkg/config"
)
const (
// tag is the same version as fastchat
defaultFastChatImage = "kubeagi/arcadia-fastchat-worker:vllm-v0.4.0-hotfix"
// For ease of maintenance and stability, VLLM module is now included in standard image as a default feature.
defaultFastchatVLLMImage = "kubeagi/arcadia-fastchat-worker:vllm-v0.4.0-hotfix"
// defaultKubeAGIImage for RunnerKubeAGI
defaultKubeAGIImage = "kubeagi/core-library-cli:v0.0.1"
// mount path in runner
defaultModelMountPath = "/data/models"
defaultShmMountPath = "/dev/shm"
)
// ModelRunner run a model service
type ModelRunner interface {
// Device used when running model
Device() Device
// NumberOfGPUs used when running model
NumberOfGPUs() string
// Build a model runner instance
Build(ctx context.Context, model *arcadiav1alpha1.TypedObjectReference) (any, error)
}
var _ ModelRunner = (*RunnerFastchat)(nil)
// RunnerFastchat use fastchat to run a model
type RunnerFastchat struct {
c client.Client
w *arcadiav1alpha1.Worker
modelFileFromRemote bool
}
func NewRunnerFastchat(c client.Client, w *arcadiav1alpha1.Worker, modelFileFromRemote bool) (ModelRunner, error) {
return &RunnerFastchat{
c: c,
w: w,
modelFileFromRemote: modelFileFromRemote,
}, nil
}
// Device utilized by this runner
func (runner *RunnerFastchat) Device() Device {
return DeviceBasedOnResource(runner.w.Spec.Resources.Limits)
}
// NumberOfGPUs utilized by this runner
func (runner *RunnerFastchat) NumberOfGPUs() string {
return NumberOfGPUs(runner.w.Spec.Resources.Limits)
}
// Build a runner instance
func (runner *RunnerFastchat) Build(ctx context.Context, model *arcadiav1alpha1.TypedObjectReference) (any, error) {
if model == nil {
return nil, errors.New("nil model")
}
gw, err := config.GetGateway(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get arcadia config with %w", err)
}
modelFileDir := fmt.Sprintf("%s/%s", defaultModelMountPath, model.Name)
additionalEnvs := []corev1.EnvVar{}
systemArgs := fmt.Sprintf("--device %s", runner.Device().String())
if runner.modelFileFromRemote {
m := arcadiav1alpha1.Model{}
if err := runner.c.Get(ctx, types.NamespacedName{Namespace: *model.Namespace, Name: model.Name}, &m); err != nil {
return nil, err
}
if m.Spec.Revision != "" {
systemArgs += fmt.Sprintf(" --revision %s ", m.Spec.Revision)
}
if m.Spec.ModelSource == modelSourceFromHugginfFace {
modelFileDir = m.Spec.HuggingFaceRepo
}
if m.Spec.ModelSource == modelSourceFromModelScope {
modelFileDir = m.Spec.ModelScopeRepo
additionalEnvs = append(additionalEnvs, corev1.EnvVar{Name: "FASTCHAT_USE_MODELSCOPE", Value: "True"})
}
}
additionalEnvs = append(additionalEnvs, corev1.EnvVar{Name: "FASTCHAT_MODEL_NAME_PATH", Value: modelFileDir})
img := defaultFastChatImage
if runner.w.Spec.Runner.Image != "" {
img = runner.w.Spec.Runner.Image
}
// read worker address
container := &corev1.Container{
Name: "runner",
Image: img,
ImagePullPolicy: runner.w.Spec.Runner.ImagePullPolicy,
Env: []corev1.EnvVar{
{Name: "FASTCHAT_WORKER_NAME", Value: "fastchat.serve.model_worker"},
{Name: "FASTCHAT_WORKER_NAMESPACE", Value: runner.w.Namespace},
{Name: "FASTCHAT_REGISTRATION_MODEL_NAME", Value: runner.w.MakeRegistrationModelName()},
{Name: "FASTCHAT_MODEL_NAME", Value: model.Name},
{Name: "FASTCHAT_WORKER_ADDRESS", Value: fmt.Sprintf("http://%s.%s:%d", runner.w.Name+WokerCommonSuffix, runner.w.Namespace, arcadiav1alpha1.DefaultWorkerPort)},
{Name: "FASTCHAT_CONTROLLER_ADDRESS", Value: gw.Controller},
{Name: "NUMBER_GPUS", Value: runner.NumberOfGPUs()},
},
Ports: []corev1.ContainerPort{
{Name: "http", ContainerPort: arcadiav1alpha1.DefaultWorkerPort},
},
VolumeMounts: []corev1.VolumeMount{
{Name: "models", MountPath: defaultModelMountPath},
},
Resources: runner.w.Spec.Resources,
}
additionalEnvs = append(additionalEnvs, corev1.EnvVar{Name: "SYSTEM_ARGS", Value: systemArgs})
container.Env = append(container.Env, additionalEnvs...)
return container, nil
}
var _ ModelRunner = (*RunnerFastchatVLLM)(nil)
// RunnerFastchatVLLM use fastchat with vllm to run a model
type RunnerFastchatVLLM struct {
c client.Client
w *arcadiav1alpha1.Worker
modelFileFromRemote bool
}
func NewRunnerFastchatVLLM(c client.Client, w *arcadiav1alpha1.Worker, modelFileFromRemote bool) (ModelRunner, error) {
return &RunnerFastchatVLLM{
c: c,
w: w,
modelFileFromRemote: modelFileFromRemote,
}, nil
}
// Device used by this runner
func (runner *RunnerFastchatVLLM) Device() Device {
return DeviceBasedOnResource(runner.w.Spec.Resources.Limits)
}
// NumberOfGPUs utilized by this runner
func (runner *RunnerFastchatVLLM) NumberOfGPUs() string {
return NumberOfGPUs(runner.w.Spec.Resources.Limits)
}
// Build a runner instance
func (runner *RunnerFastchatVLLM) Build(ctx context.Context, model *arcadiav1alpha1.TypedObjectReference) (any, error) {
if model == nil {
return nil, errors.New("nil model")
}
gw, err := config.GetGateway(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get arcadia config with %w", err)
}
systemArgs := ""
additionalEnvs := []corev1.EnvVar{}
// configure ray cluster
resources := runner.w.Spec.Resources
gpuEnvExist := false
// default ray cluster which can only utilize gpus on single nodes
rayCluster := config.DefaultRayCluster()
for _, envItem := range runner.w.Spec.AdditionalEnvs {
// using existing ray cluster
if envItem.Name == "RAY_CLUSTER_INDEX" {
externalRayClusterIndex, _ := strconv.Atoi(envItem.Value)
rayClusters, err := config.GetRayClusters(ctx)
if err != nil || len(rayClusters) == 0 {
return nil, fmt.Errorf("failed to find ray clusters: %s", err.Error())
}
if len(rayClusters) == 0 {
return nil, fmt.Errorf("no ray clusters configured")
}
rayCluster = rayClusters[externalRayClusterIndex]
// Hardcoded directly requested gpu to 1 if using existing ray cluster
resources.Limits[ResourceNvidiaGPU] = resource.MustParse("1")
}
// set gpu memory utilization
// The ratio (between 0 and 1) of GPU memory to reserve for the model weights, activations, and KV cache. Higher values will increase the KV cache size and thus improve the model's throughput.
// However, if the value is too high, it may cause out-of-memory (OOM) errors.
// By default, gpu_memory_utilization will be 0.9
if envItem.Name == "GPU_MEMORY_UTILIZATION" {
gpuMemoryUtilization, _ := strconv.ParseFloat(envItem.Value, 64)
systemArgs += fmt.Sprintf(" --gpu_memory_utilization %f", gpuMemoryUtilization)
}
if envItem.Name == "NUMBER_GPUS" {
gpuEnvExist = true
}
}
klog.V(5).Infof("run worker with raycluster:\n %s", rayCluster.String())
// set ray configurations into additional environments
additionalEnvs = append(additionalEnvs,
corev1.EnvVar{
Name: "RAY_ADDRESS",
Value: rayCluster.HeadAddress,
}, corev1.EnvVar{
Name: "RAY_VERSION",
Value: rayCluster.GetRayVersion(),
}, corev1.EnvVar{
Name: "PYTHON_VERSION",
Value: rayCluster.GetPythonVersion(),
})
modelFileDir := fmt.Sprintf("%s/%s", defaultModelMountPath, model.Name)
systemArgs = fmt.Sprintf("%s --trust-remote-code", systemArgs)
if runner.modelFileFromRemote {
m := arcadiav1alpha1.Model{}
if err := runner.c.Get(ctx, types.NamespacedName{Namespace: *model.Namespace, Name: model.Name}, &m); err != nil {
return nil, err
}
if m.Spec.Revision != "" {
systemArgs += fmt.Sprintf(" --revision %s", m.Spec.Revision)
}
if m.Spec.ModelSource == modelSourceFromHugginfFace {
modelFileDir = m.Spec.HuggingFaceRepo
}
if m.Spec.ModelSource == modelSourceFromModelScope {
modelFileDir = m.Spec.ModelScopeRepo
additionalEnvs = append(additionalEnvs, corev1.EnvVar{Name: "FASTCHAT_USE_MODELSCOPE", Value: "True"})
}
}
additionalEnvs = append(additionalEnvs, corev1.EnvVar{Name: "FASTCHAT_MODEL_NAME_PATH", Value: modelFileDir})
img := defaultFastchatVLLMImage
if runner.w.Spec.Runner.Image != "" {
img = runner.w.Spec.Runner.Image
}
container := &corev1.Container{
Name: "runner",
Image: img,
ImagePullPolicy: runner.w.Spec.Runner.ImagePullPolicy,
Env: []corev1.EnvVar{
{Name: "FASTCHAT_WORKER_NAME", Value: "fastchat.serve.vllm_worker"},
{Name: "FASTCHAT_WORKER_NAMESPACE", Value: runner.w.Namespace},
{Name: "FASTCHAT_REGISTRATION_MODEL_NAME", Value: runner.w.MakeRegistrationModelName()},
{Name: "FASTCHAT_MODEL_NAME", Value: model.Name},
{Name: "FASTCHAT_WORKER_ADDRESS", Value: fmt.Sprintf("http://%s.%s:%d", runner.w.Name+WokerCommonSuffix, runner.w.Namespace, arcadiav1alpha1.DefaultWorkerPort)},
{Name: "FASTCHAT_CONTROLLER_ADDRESS", Value: gw.Controller},
},
Ports: []corev1.ContainerPort{
{Name: "http", ContainerPort: arcadiav1alpha1.DefaultWorkerPort},
},
VolumeMounts: []corev1.VolumeMount{
{Name: "models", MountPath: defaultModelMountPath},
// mount volume to /dev/shm to avoid Bus error
{Name: "models", MountPath: defaultShmMountPath},
},
Resources: resources,
}
if !gpuEnvExist {
// if env doesn't exist, set gpu number to the number of GPUs in the worker's resource
additionalEnvs = append(additionalEnvs, corev1.EnvVar{Name: "NUMBER_GPUS", Value: runner.NumberOfGPUs()})
}
additionalEnvs = append(additionalEnvs, corev1.EnvVar{Name: "SYSTEM_ARGS", Value: systemArgs})
container.Env = append(container.Env, additionalEnvs...)
return container, nil
}
var _ ModelRunner = (*KubeAGIRunner)(nil)
// KubeAGIRunner utilizes core-library-cli(https://github.com/kubeagi/core-library/tree/main/libs/cli) to run model services
// Mainly for reranking,whisper,etc..
type KubeAGIRunner struct {
c client.Client
w *arcadiav1alpha1.Worker
modelFileFromRemote bool
}
func NewKubeAGIRunner(c client.Client, w *arcadiav1alpha1.Worker, modelFileFromRemote bool) (ModelRunner, error) {
return &KubeAGIRunner{
c: c,
w: w,
modelFileFromRemote: modelFileFromRemote,
}, nil
}
// Device used when running model
func (runner *KubeAGIRunner) Device() Device {
return DeviceBasedOnResource(runner.w.Spec.Resources.Limits)
}
// NumberOfGPUs utilized by this runner
func (runner *KubeAGIRunner) NumberOfGPUs() string {
return NumberOfGPUs(runner.w.Spec.Resources.Limits)
}
// Build a model runner instance
func (runner *KubeAGIRunner) Build(ctx context.Context, model *arcadiav1alpha1.TypedObjectReference) (any, error) {
if model == nil {
return nil, errors.New("nil model")
}
img := defaultKubeAGIImage
if runner.w.Spec.Runner.Image != "" {
img = runner.w.Spec.Runner.Image
}
// read worker address
modelMountPath := "/data/models"
rerankModelPath := fmt.Sprintf("%s/%s", modelMountPath, model.Name)
if runner.modelFileFromRemote {
m := arcadiav1alpha1.Model{}
if err := runner.c.Get(ctx, types.NamespacedName{Namespace: *model.Namespace, Name: model.Name}, &m); err != nil {
return nil, err
}
if m.Spec.HuggingFaceRepo != "" {
rerankModelPath = m.Spec.HuggingFaceRepo
}
/*
TODO support modelscope
if m.Spec.ModelScopeRepo != "" {
rerankModelPath = m.Spec.ModelScopeRepo
}
*/
}
container := &corev1.Container{
Name: "runner",
Image: img,
ImagePullPolicy: runner.w.Spec.Runner.ImagePullPolicy,
Command: []string{
"python", "kubeagi_cli/cli.py", "serve", "--host", "0.0.0.0", "--port", fmt.Sprintf("%d", arcadiav1alpha1.DefaultWorkerPort),
},
Env: []corev1.EnvVar{
// Only reranking supported for now
{Name: "RERANKING_MODEL_PATH", Value: rerankModelPath},
},
Ports: []corev1.ContainerPort{
{Name: "http", ContainerPort: arcadiav1alpha1.DefaultWorkerPort},
},
VolumeMounts: []corev1.VolumeMount{
{Name: "models", MountPath: defaultModelMountPath},
},
Resources: runner.w.Spec.Resources,
}
return container, nil
}