|
| 1 | +/* |
| 2 | +Copyright 2023. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package odh |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + . "github.com/project-codeflare/codeflare-common/support" |
| 26 | + rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + "k8s.io/apimachinery/pkg/api/resource" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "sigs.k8s.io/kueue/apis/kueue/v1beta1" |
| 31 | +) |
| 32 | + |
| 33 | +func TestMnistRayTuneHpoCpu(t *testing.T) { |
| 34 | + mnistRayTuneHpo(t, 0) |
| 35 | +} |
| 36 | + |
| 37 | +func TestMnistRayTuneHpoGpu(t *testing.T) { |
| 38 | + mnistRayTuneHpo(t, 1) |
| 39 | +} |
| 40 | + |
| 41 | +func mnistRayTuneHpo(t *testing.T, numGpus int) { |
| 42 | + test := With(t) |
| 43 | + |
| 44 | + // Creating a namespace |
| 45 | + namespace := test.NewTestNamespace() |
| 46 | + |
| 47 | + // Create Kueue resources |
| 48 | + resourceFlavor := CreateKueueResourceFlavor(test, v1beta1.ResourceFlavorSpec{}) |
| 49 | + defer test.Client().Kueue().KueueV1beta1().ResourceFlavors().Delete(test.Ctx(), resourceFlavor.Name, metav1.DeleteOptions{}) |
| 50 | + cqSpec := v1beta1.ClusterQueueSpec{ |
| 51 | + NamespaceSelector: &metav1.LabelSelector{}, |
| 52 | + ResourceGroups: []v1beta1.ResourceGroup{ |
| 53 | + { |
| 54 | + CoveredResources: []corev1.ResourceName{corev1.ResourceName("cpu"), corev1.ResourceName("memory"), corev1.ResourceName("nvidia.com/gpu")}, |
| 55 | + Flavors: []v1beta1.FlavorQuotas{ |
| 56 | + { |
| 57 | + Name: v1beta1.ResourceFlavorReference(resourceFlavor.Name), |
| 58 | + Resources: []v1beta1.ResourceQuota{ |
| 59 | + { |
| 60 | + Name: corev1.ResourceCPU, |
| 61 | + NominalQuota: resource.MustParse("8"), |
| 62 | + }, |
| 63 | + { |
| 64 | + Name: corev1.ResourceMemory, |
| 65 | + NominalQuota: resource.MustParse("12Gi"), |
| 66 | + }, |
| 67 | + { |
| 68 | + Name: corev1.ResourceName("nvidia.com/gpu"), |
| 69 | + NominalQuota: resource.MustParse(fmt.Sprint(numGpus)), |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + clusterQueue := CreateKueueClusterQueue(test, cqSpec) |
| 78 | + defer test.Client().Kueue().KueueV1beta1().ClusterQueues().Delete(test.Ctx(), clusterQueue.Name, metav1.DeleteOptions{}) |
| 79 | + localQueue := CreateKueueLocalQueue(test, namespace.Name, clusterQueue.Name) |
| 80 | + |
| 81 | + // Test configuration |
| 82 | + jupyterNotebookConfigMapFileName := "mnist_hpo_raytune.ipynb" |
| 83 | + mnist_hpo := ReadFile(test, "resources/mnist_hpo.py") |
| 84 | + |
| 85 | + if numGpus > 0 { |
| 86 | + mnist_hpo = bytes.Replace(mnist_hpo, []byte("gpu_value=\"has to be specified\""), []byte("gpu_value=\"1\""), 1) |
| 87 | + } else { |
| 88 | + mnist_hpo = bytes.Replace(mnist_hpo, []byte("gpu_value=\"has to be specified\""), []byte("gpu_value=\"0\""), 1) |
| 89 | + } |
| 90 | + |
| 91 | + config := CreateConfigMap(test, namespace.Name, map[string][]byte{ |
| 92 | + // MNIST Raytune HPO Notebook |
| 93 | + jupyterNotebookConfigMapFileName: ReadFile(test, "resources/mnist_hpo_raytune.ipynb"), |
| 94 | + "mnist_hpo.py": mnist_hpo, |
| 95 | + "hpo_raytune_requirements.txt": ReadFile(test, "resources/hpo_raytune_requirements.txt"), |
| 96 | + }) |
| 97 | + |
| 98 | + // Define the regular(non-admin) user |
| 99 | + userName := GetNotebookUserName(test) |
| 100 | + userToken := GetNotebookUserToken(test) |
| 101 | + |
| 102 | + // Create role binding with Namespace specific admin cluster role |
| 103 | + CreateUserRoleBindingWithClusterRole(test, userName, namespace.Name, "admin") |
| 104 | + |
| 105 | + // Create Notebook CR |
| 106 | + createNotebook(test, namespace, userToken, localQueue.Name, config.Name, jupyterNotebookConfigMapFileName, numGpus) |
| 107 | + |
| 108 | + // Gracefully cleanup Notebook |
| 109 | + defer func() { |
| 110 | + deleteNotebook(test, namespace) |
| 111 | + test.Eventually(listNotebooks(test, namespace), TestTimeoutMedium).Should(HaveLen(0)) |
| 112 | + }() |
| 113 | + |
| 114 | + // Make sure the RayCluster is created and running |
| 115 | + test.Eventually(rayClusters(test, namespace), TestTimeoutLong). |
| 116 | + Should( |
| 117 | + And( |
| 118 | + HaveLen(1), |
| 119 | + ContainElement(WithTransform(RayClusterState, Equal(rayv1.Ready))), |
| 120 | + ), |
| 121 | + ) |
| 122 | + |
| 123 | + // Make sure the Workload is created and running |
| 124 | + test.Eventually(GetKueueWorkloads(test, namespace.Name), TestTimeoutMedium). |
| 125 | + Should( |
| 126 | + And( |
| 127 | + HaveLen(1), |
| 128 | + ContainElement(WithTransform(KueueWorkloadAdmitted, BeTrueBecause("Workload failed to be admitted"))), |
| 129 | + ), |
| 130 | + ) |
| 131 | + |
| 132 | + // Make sure the RayCluster finishes and is deleted |
| 133 | + test.Eventually(rayClusters(test, namespace), TestTimeoutLong). |
| 134 | + Should(HaveLen(0)) |
| 135 | +} |
0 commit comments