forked from kaito-project/kaito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preset_test.go
424 lines (350 loc) · 13.9 KB
/
preset_test.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package e2e
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
kaitov1alpha1 "github.com/azure/kaito/api/v1alpha1"
"github.com/azure/kaito/test/e2e/utils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/samber/lo"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"knative.dev/pkg/apis"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
PresetLlama2AChat = "llama-2-7b-chat"
PresetLlama2BChat = "llama-2-13b-chat"
PresetFalcon7BModel = "falcon-7b"
PresetFalcon40BModel = "falcon-40b"
)
func createFalconWorkspaceWithPresetPublicMode(numOfNode int) *kaitov1alpha1.Workspace {
workspaceObj := &kaitov1alpha1.Workspace{}
By("Creating a workspace CR with Falcon 7B preset public mode", func() {
uniqueID := fmt.Sprint("preset-", rand.Intn(1000))
workspaceObj = utils.GenerateWorkspaceManifest(uniqueID, namespaceName, "", numOfNode, "Standard_NC12s_v3",
&metav1.LabelSelector{
MatchLabels: map[string]string{"kaito-workspace": "public-preset-e2e-test"},
}, nil, PresetFalcon7BModel, kaitov1alpha1.ModelImageAccessModePublic, nil, nil)
createAndValidateWorkspace(workspaceObj)
})
return workspaceObj
}
func createLlama7BWorkspaceWithPresetPrivateMode(registry, registrySecret, imageVersion string, numOfNode int) *kaitov1alpha1.Workspace {
workspaceObj := &kaitov1alpha1.Workspace{}
By("Creating a workspace CR with Llama 7B Chat preset private mode", func() {
uniqueID := fmt.Sprint("preset-", rand.Intn(1000))
workspaceObj = utils.GenerateWorkspaceManifest(uniqueID, namespaceName, fmt.Sprintf("%s/%s:%s", registry, PresetLlama2AChat, imageVersion),
numOfNode, "Standard_NC12s_v3", &metav1.LabelSelector{
MatchLabels: map[string]string{"kaito-workspace": "private-preset-e2e-test"},
}, nil, PresetLlama2AChat, kaitov1alpha1.ModelImageAccessModePrivate, []string{registrySecret}, nil)
createAndValidateWorkspace(workspaceObj)
})
return workspaceObj
}
func createLlama13BWorkspaceWithPresetPrivateMode(registry, registrySecret, imageVersion string, numOfNode int) *kaitov1alpha1.Workspace {
workspaceObj := &kaitov1alpha1.Workspace{}
By("Creating a workspace CR with Llama 13B Chat preset private mode", func() {
uniqueID := fmt.Sprint("preset-", rand.Intn(1000))
workspaceObj = utils.GenerateWorkspaceManifest(uniqueID, namespaceName, fmt.Sprintf("%s/%s:%s", registry, PresetLlama2BChat, imageVersion),
numOfNode, "Standard_NC12s_v3", &metav1.LabelSelector{
MatchLabels: map[string]string{"kaito-workspace": "private-preset-e2e-test"},
}, nil, PresetLlama2BChat, kaitov1alpha1.ModelImageAccessModePrivate, []string{registrySecret}, nil)
createAndValidateWorkspace(workspaceObj)
})
return workspaceObj
}
func createCustomWorkspaceWithPresetCustomMode(imageName string, numOfNode int) *kaitov1alpha1.Workspace {
workspaceObj := &kaitov1alpha1.Workspace{}
By("Creating a workspace CR with custom workspace mode", func() {
uniqueID := fmt.Sprint("preset-", rand.Intn(1000))
workspaceObj = utils.GenerateWorkspaceManifest(uniqueID, namespaceName, "",
numOfNode, "Standard_D4s_v3", &metav1.LabelSelector{
MatchLabels: map[string]string{"kaito-workspace": "private-preset-e2e-test"},
}, nil, "", utils.InferenceModeCustomTemplate, nil, utils.GeneratePodTemplate(uniqueID, namespaceName, imageName, nil))
createAndValidateWorkspace(workspaceObj)
})
return workspaceObj
}
func createAndValidateWorkspace(workspaceObj *kaitov1alpha1.Workspace) {
By("Creating workspace", func() {
Eventually(func() error {
return TestingCluster.KubeClient.Create(ctx, workspaceObj, &client.CreateOptions{})
}, utils.PollTimeout, utils.PollInterval).
Should(Succeed(), "Failed to create workspace %s", workspaceObj.Name)
By("Validating workspace creation", func() {
err := TestingCluster.KubeClient.Get(ctx, client.ObjectKey{
Namespace: workspaceObj.Namespace,
Name: workspaceObj.Name,
}, workspaceObj, &client.GetOptions{})
Expect(err).NotTo(HaveOccurred())
})
})
}
func getAllValidMachines(workspaceObj *kaitov1alpha1.Workspace) (*v1alpha5.MachineList, error) {
machineList := &v1alpha5.MachineList{}
ls := labels.Set{
kaitov1alpha1.LabelWorkspaceName: workspaceObj.Name,
kaitov1alpha1.LabelWorkspaceNamespace: workspaceObj.Namespace,
}
err := TestingCluster.KubeClient.List(ctx, machineList, &client.MatchingLabelsSelector{Selector: ls.AsSelector()})
if err != nil {
return nil, err
}
return machineList, nil
}
// Logic to validate machine creation
func validateMachineCreation(workspaceObj *kaitov1alpha1.Workspace, expectedCount int) {
By("Checking machine created by the workspace CR", func() {
Eventually(func() bool {
machineList, err := getAllValidMachines(workspaceObj)
if err != nil {
fmt.Printf("Failed to get all valid machines: %v", err)
return false
}
if len(machineList.Items) != expectedCount {
return false
}
for _, machine := range machineList.Items {
_, conditionFound := lo.Find(machine.GetConditions(), func(condition apis.Condition) bool {
return condition.Type == apis.ConditionReady && condition.Status == v1.ConditionTrue
})
if !conditionFound {
return false
}
}
return true
}, 20*time.Minute, utils.PollInterval).Should(BeTrue(), "Failed to wait for machine to be ready")
})
}
// Logic to validate resource status
func validateResourceStatus(workspaceObj *kaitov1alpha1.Workspace) {
By("Checking the resource status", func() {
Eventually(func() bool {
err := TestingCluster.KubeClient.Get(ctx, client.ObjectKey{
Namespace: workspaceObj.Namespace,
Name: workspaceObj.Name,
}, workspaceObj, &client.GetOptions{})
if err != nil {
return false
}
_, conditionFound := lo.Find(workspaceObj.Status.Conditions, func(condition metav1.Condition) bool {
return condition.Type == string(kaitov1alpha1.WorkspaceConditionTypeResourceStatus) &&
condition.Status == metav1.ConditionTrue
})
return conditionFound
}, 10*time.Minute, utils.PollInterval).Should(BeTrue(), "Failed to wait for resource status to be ready")
})
}
func validateAssociatedService(workspaceObj *kaitov1alpha1.Workspace) {
serviceName := workspaceObj.Name
serviceNamespace := workspaceObj.Namespace
By(fmt.Sprintf("Checking for service %s in namespace %s", serviceName, serviceNamespace), func() {
service := &v1.Service{}
Eventually(func() bool {
err := TestingCluster.KubeClient.Get(ctx, client.ObjectKey{
Namespace: serviceNamespace,
Name: serviceName,
}, service)
if err != nil {
if errors.IsNotFound(err) {
GinkgoWriter.Printf("Service %s not found in namespace %s\n", serviceName, serviceNamespace)
} else {
GinkgoWriter.Printf("Error fetching service %s in namespace %s: %v\n", serviceName, serviceNamespace, err)
}
return false
}
GinkgoWriter.Printf("Found service: %s in namespace %s\n", serviceName, serviceNamespace)
return true
}, 10*time.Minute, utils.PollInterval).Should(BeTrue(), "Failed to wait for service to be created")
})
}
// Logic to validate inference deployment
func validateInferenceResource(workspaceObj *kaitov1alpha1.Workspace, expectedReplicas int32, isStatefulSet bool) {
By("Checking the inference resource", func() {
Eventually(func() bool {
var err error
var readyReplicas int32
if isStatefulSet {
sts := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: workspaceObj.Name,
Namespace: workspaceObj.Namespace,
},
}
err = TestingCluster.KubeClient.Get(ctx, client.ObjectKey{
Namespace: workspaceObj.Namespace,
Name: workspaceObj.Name,
}, sts)
readyReplicas = sts.Status.ReadyReplicas
} else {
dep := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: workspaceObj.Name,
Namespace: workspaceObj.Namespace,
},
}
err = TestingCluster.KubeClient.Get(ctx, client.ObjectKey{
Namespace: workspaceObj.Namespace,
Name: workspaceObj.Name,
}, dep)
readyReplicas = dep.Status.ReadyReplicas
}
if err != nil {
GinkgoWriter.Printf("Error fetching resource: %v\n", err)
return false
}
if readyReplicas == expectedReplicas {
return true
}
// GinkgoWriter.Printf("Resource '%s' not ready. Ready replicas: %d\n", workspaceObj.Name, readyReplicas)
return false
}, 20*time.Minute, utils.PollInterval).Should(BeTrue(), "Failed to wait for inference resource to be ready")
})
}
// Logic to validate workspace readiness
func validateWorkspaceReadiness(workspaceObj *kaitov1alpha1.Workspace) {
By("Checking the workspace status is ready", func() {
Eventually(func() bool {
err := TestingCluster.KubeClient.Get(ctx, client.ObjectKey{
Namespace: workspaceObj.Namespace,
Name: workspaceObj.Name,
}, workspaceObj, &client.GetOptions{})
if err != nil {
return false
}
_, conditionFound := lo.Find(workspaceObj.Status.Conditions, func(condition metav1.Condition) bool {
return condition.Type == string(kaitov1alpha1.WorkspaceConditionTypeReady) &&
condition.Status == metav1.ConditionTrue
})
return conditionFound
}, 10*time.Minute, utils.PollInterval).Should(BeTrue(), "Failed to wait for workspace to be ready")
})
}
func cleanupResources(workspaceObj *kaitov1alpha1.Workspace) {
By("Cleaning up resources", func() {
// delete workspace
err := deleteWorkspace(workspaceObj)
Expect(err).NotTo(HaveOccurred(), "Failed to delete workspace")
})
}
func deleteWorkspace(workspaceObj *kaitov1alpha1.Workspace) error {
By("Deleting workspace", func() {
Eventually(func() error {
// Check if the workspace exists
err := TestingCluster.KubeClient.Get(ctx, client.ObjectKey{
Namespace: workspaceObj.Namespace,
Name: workspaceObj.Name,
}, workspaceObj)
if errors.IsNotFound(err) {
GinkgoWriter.Printf("Workspace %s does not exist, no need to delete\n", workspaceObj.Name)
return nil
}
if err != nil {
return fmt.Errorf("error checking if workspace %s exists: %v", workspaceObj.Name, err)
}
err = TestingCluster.KubeClient.Delete(ctx, workspaceObj, &client.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete workspace %s: %v", workspaceObj.Name, err)
}
return nil
}, utils.PollTimeout, utils.PollInterval).Should(Succeed(), "Failed to delete workspace")
})
return nil
}
var runLlama13B bool
var aiModelsRegistry string
var aiModelsRegistrySecret string
var modelInfo map[string]string
var _ = Describe("Workspace Preset", func() {
BeforeEach(func() {
var err error
runLlama13B, err = strconv.ParseBool(os.Getenv("RUN_LLAMA_13B"))
if err != nil {
// Handle error or set a default value
fmt.Print("Error: RUN_LLAMA_13B ENV Variable not set")
runLlama13B = false
}
aiModelsRegistry = utils.GetEnv("AI_MODELS_REGISTRY")
aiModelsRegistrySecret = utils.GetEnv("AI_MODELS_REGISTRY_SECRET")
// Load stable model versions
configs, err := utils.GetModelConfigInfo("/home/runner/work/kaito/kaito/presets/models/supported_models.yaml")
if err != nil {
fmt.Printf("Failed to load model configs: %v\n", err)
os.Exit(1)
}
modelInfo, err = utils.ExtractModelVersion(configs)
if err != nil {
fmt.Printf("Failed to extract stable model versions: %v\n", err)
os.Exit(1)
}
})
It("should create a workspace with preset public mode successfully", func() {
numOfNode := 1
workspaceObj := createFalconWorkspaceWithPresetPublicMode(numOfNode)
defer cleanupResources(workspaceObj)
time.Sleep(30 * time.Second)
validateMachineCreation(workspaceObj, numOfNode)
validateResourceStatus(workspaceObj)
time.Sleep(30 * time.Second)
validateAssociatedService(workspaceObj)
validateInferenceResource(workspaceObj, int32(numOfNode), false)
validateWorkspaceReadiness(workspaceObj)
})
It("should create a llama 7b workspace with preset private mode successfully", func() {
numOfNode := 1
modelVersion, ok := modelInfo[PresetLlama2AChat]
if !ok {
Fail(fmt.Sprintf("Model version for %s not found", PresetLlama2AChat))
}
workspaceObj := createLlama7BWorkspaceWithPresetPrivateMode(aiModelsRegistry, aiModelsRegistrySecret, modelVersion, numOfNode)
defer cleanupResources(workspaceObj)
time.Sleep(30 * time.Second)
validateMachineCreation(workspaceObj, numOfNode)
validateResourceStatus(workspaceObj)
time.Sleep(30 * time.Second)
validateAssociatedService(workspaceObj)
validateInferenceResource(workspaceObj, int32(numOfNode), false)
validateWorkspaceReadiness(workspaceObj)
})
It("should create a llama 13b workspace with preset private mode successfully", func() {
if !runLlama13B {
Skip("Skipping llama 13b workspace test")
}
numOfNode := 2
modelVersion, ok := modelInfo[PresetLlama2BChat]
if !ok {
Fail(fmt.Sprintf("Model version for %s not found", PresetLlama2AChat))
}
workspaceObj := createLlama13BWorkspaceWithPresetPrivateMode(aiModelsRegistry, aiModelsRegistrySecret, modelVersion, numOfNode)
defer cleanupResources(workspaceObj)
time.Sleep(30 * time.Second)
validateMachineCreation(workspaceObj, numOfNode)
validateResourceStatus(workspaceObj)
time.Sleep(30 * time.Second)
validateAssociatedService(workspaceObj)
validateInferenceResource(workspaceObj, int32(numOfNode), true)
validateWorkspaceReadiness(workspaceObj)
})
It("should create a custom template workspace successfully", func() {
numOfNode := 1
imageName := "nginx:latest"
workspaceObj := createCustomWorkspaceWithPresetCustomMode(imageName, numOfNode)
defer cleanupResources(workspaceObj)
time.Sleep(30 * time.Second)
validateMachineCreation(workspaceObj, numOfNode)
validateResourceStatus(workspaceObj)
time.Sleep(30 * time.Second)
validateInferenceResource(workspaceObj, int32(numOfNode), false)
validateWorkspaceReadiness(workspaceObj)
})
})