-
Notifications
You must be signed in to change notification settings - Fork 67
Automated test for LLM Finetune Deepspeed demo #197
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
openshift-merge-bot
merged 5 commits into
opendatahub-io:main
from
abhijeet-dhumal:automate-llm-finetune-deepspeed-demo
Aug 28, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0dd3e29
Automate LLM Finetune Deepspeed demo
abhijeet-dhumal 49f0cdf
Updated ray-finetune-test flow to reuse existing demo files from exam…
abhijeet-dhumal 03e3492
Updated ray-finetune-demo test with latest changes
abhijeet-dhumal 34de932
Added test for llama-3.1-8b model finetuning demo
abhijeet-dhumal 9d022c1
Added modelName parameter to be considered for testing finetune demo
abhijeet-dhumal 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
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
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
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
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
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
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,169 @@ | ||
/* | ||
Copyright 2023. | ||
|
||
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 odh | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/gomega" | ||
. "github.com/project-codeflare/codeflare-common/support" | ||
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestRayFinetuneLlmDeepspeedDemoLlama_2_7b(t *testing.T) { | ||
rayFinetuneLlmDeepspeed(t, 1, "meta-llama/Llama-2-7b-chat-hf", "zero_3_llama_2_7b.json") | ||
} | ||
func TestRayFinetuneLlmDeepspeedDemoLlama_31_8b(t *testing.T) { | ||
rayFinetuneLlmDeepspeed(t, 1, "meta-llama/Meta-Llama-3.1-8B", "zero_3_offload_optim_param.json") | ||
} | ||
|
||
func rayFinetuneLlmDeepspeed(t *testing.T, numGpus int, modelName string, modelConfigFile string) { | ||
test := With(t) | ||
|
||
// Create a namespace | ||
namespace := test.NewTestNamespace() | ||
var workingDirectory, err = os.Getwd() | ||
test.Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Define the regular(non-admin) user | ||
userName := GetNotebookUserName(test) | ||
userToken := GetNotebookUserToken(test) | ||
|
||
// Create role binding with Namespace specific admin cluster role | ||
CreateUserRoleBindingWithClusterRole(test, userName, namespace.Name, "admin") | ||
|
||
// list changes required in llm-deepspeed-finetune-demo.ipynb file and update those | ||
requiredChangesInNotebook := map[string]string{ | ||
"import os": "import os,time,sys", | ||
"import sys": "!cp /opt/app-root/notebooks/* ./\\n\",\n\t\"!ls", | ||
"from codeflare_sdk.cluster.auth import TokenAuthentication": "from codeflare_sdk.cluster.auth import TokenAuthentication\\n\",\n\t\"from codeflare_sdk.job import RayJobClient", | ||
"token = ''": fmt.Sprintf("token = '%s'", userToken), | ||
"server = ''": fmt.Sprintf("server = '%s'", GetOpenShiftApiUrl(test)), | ||
"namespace='ray-finetune-llm-deepspeed'": fmt.Sprintf("namespace='%s'", namespace.Name), | ||
"head_cpus=16": "head_cpus=2", | ||
"head_extended_resource_requests=1": "head_extended_resource_requests=0", | ||
"num_workers=7": "num_workers=1", | ||
"worker_cpu_requests=16": "worker_cpu_requests=4", | ||
"worker_cpu_limits=16": "worker_cpu_limits=4", | ||
"worker_memory_requests=128": "worker_memory_requests=64", | ||
"worker_memory_limits=256": "worker_memory_limits=128", | ||
"head_memory=128": "head_memory=48", | ||
"client = cluster.job_client": "ray_dashboard = cluster.cluster_dashboard_uri()\\n\",\n\t\"header = {\\\"Authorization\\\": \\\"Bearer " + userToken + "\\\"}\\n\",\n\t\"client = RayJobClient(address=ray_dashboard, headers=header, verify=False)\\n", | ||
"--num-devices=8": fmt.Sprintf("--num-devices=%d", numGpus), | ||
"--num-epochs=3": fmt.Sprintf("--num-epochs=%d", 1), | ||
"--model-name=meta-llama/Meta-Llama-3.1-8B": fmt.Sprintf("--model-name=%s", modelName), | ||
"--ds-config=./deepspeed_configs/zero_3_offload_optim_param.json": fmt.Sprintf("--ds-config=./%s \\\"\\n\",\n\t\" \\\"--lora-config=./lora.json \\\"\\n\",\n\t\" \\\"--as-test", modelConfigFile), | ||
"--batch-size-per-device=32": "--batch-size-per-device=6", | ||
"--eval-batch-size-per-device=32": "--eval-batch-size-per-device=6", | ||
"'pip': 'requirements.txt'": "'pip': '/opt/app-root/src/requirements.txt'", | ||
"'working_dir': './'": "'working_dir': '/opt/app-root/src'", | ||
"client.stop_job(submission_id)": "finished = False\\n\",\n\t\"while not finished:\\n\",\n\t\" time.sleep(1)\\n\",\n\t\" status = client.get_job_status(submission_id)\\n\",\n\t\" finished = (status == \\\"SUCCEEDED\\\")\\n\",\n\t\"if finished:\\n\",\n\t\" print(\\\"Job completed Successfully !\\\")\\n\",\n\t\"else:\\n\",\n\t\" print(\\\"Job failed !\\\")\\n\",\n\t\"time.sleep(10)\\n", | ||
} | ||
|
||
updatedNotebookContent := string(ReadFileExt(test, workingDirectory+"/../../examples/ray-finetune-llm-deepspeed/ray_finetune_llm_deepspeed.ipynb")) | ||
for oldValue, newValue := range requiredChangesInNotebook { | ||
updatedNotebookContent = strings.Replace(updatedNotebookContent, oldValue, newValue, -1) | ||
} | ||
updatedNotebook := []byte(updatedNotebookContent) | ||
|
||
// Test configuration | ||
jupyterNotebookConfigMapFileName := "ray_finetune_llm_deepspeed.ipynb" | ||
configMap := map[string][]byte{ | ||
jupyterNotebookConfigMapFileName: updatedNotebook, | ||
"ray_finetune_llm_deepspeed.py": ReadFileExt(test, workingDirectory+"/../../examples/ray-finetune-llm-deepspeed/ray_finetune_llm_deepspeed.py"), | ||
"requirements.txt": ReadFileExt(test, workingDirectory+"/../../examples/ray-finetune-llm-deepspeed/requirements.txt"), | ||
"create_dataset.py": ReadFileExt(test, workingDirectory+"/../../examples/ray-finetune-llm-deepspeed/create_dataset.py"), | ||
"lora.json": ReadFileExt(test, workingDirectory+"/../../examples/ray-finetune-llm-deepspeed/lora_configs/lora.json"), | ||
modelConfigFile: ReadFileExt(test, fmt.Sprintf(workingDirectory+"/../../examples/ray-finetune-llm-deepspeed/deepspeed_configs/%s", modelConfigFile)), | ||
"utils.py": ReadFileExt(test, workingDirectory+"/../../examples/ray-finetune-llm-deepspeed/utils.py"), | ||
} | ||
|
||
config := CreateConfigMap(test, namespace.Name, configMap) | ||
|
||
// Create Notebook CR | ||
createNotebook(test, namespace, userToken, config.Name, jupyterNotebookConfigMapFileName, numGpus) | ||
|
||
// Gracefully cleanup Notebook | ||
defer func() { | ||
deleteNotebook(test, namespace) | ||
test.Eventually(listNotebooks(test, namespace), TestTimeoutGpuProvisioning).Should(HaveLen(0)) | ||
}() | ||
|
||
// Make sure the RayCluster is created and running | ||
test.Eventually(RayClusters(test, namespace.Name), TestTimeoutGpuProvisioning). | ||
Should( | ||
And( | ||
HaveLen(1), | ||
ContainElement(WithTransform(RayClusterState, Equal(rayv1.Ready))), | ||
), | ||
) | ||
|
||
// Fetch created raycluster | ||
rayClusterName := "ray" | ||
rayCluster, err := test.Client().Ray().RayV1().RayClusters(namespace.Name).Get(test.Ctx(), rayClusterName, metav1.GetOptions{}) | ||
test.Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Initialise raycluster client to interact with raycluster to get rayjob details using REST-API | ||
dashboardUrl := GetDashboardUrl(test, namespace, rayCluster) | ||
rayClusterClientConfig := RayClusterClientConfig{Address: dashboardUrl.String(), Client: nil, InsecureSkipVerify: true} | ||
rayClient, err := NewRayClusterClient(rayClusterClientConfig, test.Config().BearerToken) | ||
test.Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("Failed to create new raycluster client: %s", err)) | ||
|
||
// wait until rayjob exists | ||
test.Eventually(func() []RayJobDetailsResponse { | ||
rayJobs, err := rayClient.GetJobs() | ||
test.Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("Failed to fetch ray-jobs : %s", err)) | ||
return *rayJobs | ||
}, TestTimeoutMedium, 1*time.Second).Should(HaveLen(1), "Ray job not found") | ||
|
||
// Get test job-id | ||
jobID := GetTestJobId(test, rayClient, dashboardUrl.Host) | ||
test.Expect(jobID).ToNot(BeEmpty()) | ||
|
||
// Wait for the job to be succeeded or failed | ||
var rayJobStatus string | ||
test.T().Logf("Waiting for job to be Succeeded...\n") | ||
test.Eventually(func() string { | ||
resp, err := rayClient.GetJobDetails(jobID) | ||
test.Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("Failed to get job details :%s", err)) | ||
rayJobStatusVal := resp.Status | ||
if rayJobStatusVal == "SUCCEEDED" || rayJobStatusVal == "FAILED" { | ||
test.T().Logf("JobStatus - %s\n", rayJobStatusVal) | ||
rayJobStatus = rayJobStatusVal | ||
return rayJobStatus | ||
} | ||
if rayJobStatus != rayJobStatusVal && rayJobStatusVal != "SUCCEEDED" { | ||
test.T().Logf("JobStatus - %s...\n", rayJobStatusVal) | ||
rayJobStatus = rayJobStatusVal | ||
} | ||
return rayJobStatus | ||
}, TestTimeoutDouble, 1*time.Second).Should(Or(Equal("SUCCEEDED"), Equal("FAILED")), "Job did not complete within the expected time") | ||
// Store job logs in output directory | ||
WriteRayJobAPILogs(test, rayClient, jobID) | ||
|
||
// Assert ray-job status after job execution | ||
test.Expect(rayJobStatus).To(Equal("SUCCEEDED"), "RayJob failed !") | ||
|
||
// Make sure the RayCluster finishes and is deleted | ||
test.Eventually(RayClusters(test, namespace.Name), TestTimeoutLong). | ||
Should(BeEmpty()) | ||
} |
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.