Panduan ini menunjukkan cara melatih model di Google Kubernetes Engine (GKE) menggunakan Ray, PyTorch, dan add-on Ray Operator.
Tentang Ray
Ray adalah framework komputasi skalabel open source untuk aplikasi AI/ML. Ray Train adalah komponen dalam Ray yang dirancang untuk pelatihan dan penyesuaian model terdistribusi. Anda dapat menggunakan Ray Train API untuk menskalakan pelatihan di beberapa mesin dan berintegrasi dengan library machine learning seperti PyTorch.
Anda dapat men-deploy tugas pelatihan Ray menggunakan resource RayCluster atau RayJob. Anda harus menggunakan resource RayJob saat men-deploy tugas Ray dalam produksi karena alasan berikut
- Resource RayJob membuat cluster Ray ephemeral yang dapat dihapus secara otomatis saat tugas selesai.
- Resource RayJob mendukung kebijakan coba lagi untuk eksekusi tugas yang tangguh.
- Anda dapat mengelola tugas Ray menggunakan pola Kubernetes API yang sudah dikenal.
Tujuan
Panduan ini ditujukan bagi pelanggan AI Generatif, pengguna baru atau lama GKE, Engineer ML, engineer MLOps (DevOps), atau administrator platform yang tertarik menggunakan kemampuan orkestrasi container Kubernetes untuk menyalurkan model menggunakan Ray.
- Membuat cluster GKE.
- Buat cluster Ray menggunakan resource kustom RayCluster.
- Latih model menggunakan tugas Ray.
- Men-deploy tugas Ray menggunakan resource kustom RayJob.
Biaya
Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih berikut:
Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda,
gunakan kalkulator harga.
Setelah menyelesaikan tugas yang dijelaskan dalam dokumen ini, Anda dapat menghindari penagihan berkelanjutan dengan menghapus resource yang Anda buat. Untuk mengetahui informasi selengkapnya, lihat Pembersihan.
Sebelum memulai
Cloud Shell telah diinstal dengan software yang Anda perlukan untuk tutorial ini, termasuk kubectl
dan gcloud CLI. Jika tidak menggunakan Cloud Shell, Anda harus menginstal gcloud CLI.
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the GKE API:
gcloud services enable container.googleapis.com
-
Install the Google Cloud CLI.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the GKE API:
gcloud services enable container.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/container.clusterAdmin, roles/container.admin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
Luncurkan sesi Cloud Shell dari Google Cloud konsol, dengan mengklik
Aktifkan Cloud Shell di Google Cloud konsol. Tindakan ini akan meluncurkan sesi di panel bawah konsol Google Cloud .
Menetapkan variabel lingkungan:
export PROJECT_ID=PROJECT_ID export CLUSTER_NAME=ray-cluster export COMPUTE_REGION=us-central1 export COMPUTE_ZONE=us-central1-c export CLUSTER_VERSION=CLUSTER_VERSION export TUTORIAL_HOME=`pwd`
Ganti kode berikut:
PROJECT_ID
: Google Cloud project ID Anda.CLUSTER_VERSION
: versi GKE yang akan digunakan. Harus1.30.1
atau setelahnya.
Buat clone repositori GitHub:
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
Ubah ke direktori kerja:
cd kubernetes-engine-samples/ai-ml/gke-ray/raytrain/pytorch-mnist
Buat lingkungan virtual Python:
python -m venv myenv && \ source myenv/bin/activate
Tinjau manifes berikut:
Manifes ini menjelaskan resource kustom RayCluster.
Terapkan manifes ke cluster GKE Anda:
kubectl apply -f ray-cluster.yaml
Pastikan resource RayCluster sudah siap:
kubectl get raycluster
Outputnya mirip dengan hal berikut ini:
NAME DESIRED WORKERS AVAILABLE WORKERS CPUS MEMORY GPUS STATUS AGE pytorch-mnist-cluster 2 2 6 20Gi 0 ready 63s
Dalam output ini,
ready
di kolomSTATUS
menunjukkan bahwa resource RayCluster sudah siap.Pastikan GKE membuat Layanan RayCluster:
kubectl get svc pytorch-mnist-cluster-head-svc
Outputnya mirip dengan hal berikut ini:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE pytorch-mnist-cluster-head-svc ClusterIP 34.118.238.247 <none> 10001/TCP,8265/TCP,6379/TCP,8080/TCP 109s
Buat sesi penerusan port ke head Ray:
kubectl port-forward svc/pytorch-mnist-cluster-head-svc 8265:8265 2>&1 >/dev/null &
Pastikan klien Ray dapat terhubung ke cluster Ray menggunakan localhost:
ray list nodes --address http://localhost:8265
Outputnya mirip dengan hal berikut ini:
Stats: ------------------------------ Total: 3 Table: ------------------------------ NODE_ID NODE_IP IS_HEAD_NODE STATE NODE_NAME RESOURCES_TOTAL LABELS 0 1d07447d7d124db641052a3443ed882f913510dbe866719ac36667d2 10.28.1.21 False ALIVE 10.28.1.21 CPU: 2.0 ray.io/node_id: 1d07447d7d124db641052a3443ed882f913510dbe866719ac36667d2 # Several lines of output omitted
Kirimkan tugas Ray dan tunggu hingga tugas selesai:
ray job submit --submission-id pytorch-mnist-job --working-dir . --runtime-env-json='{"pip": ["torch", "torchvision"], "excludes": ["myenv"]}' --address http://localhost:8265 -- python train.py
Outputnya mirip dengan hal berikut ini:
Job submission server address: http://localhost:8265 -------------------------------------------- Job 'pytorch-mnist-job' submitted successfully -------------------------------------------- Next steps Query the logs of the job: ray job logs pytorch-mnist-job Query the status of the job: ray job status pytorch-mnist-job Request the job to be stopped: ray job stop pytorch-mnist-job Handling connection for 8265 Tailing logs until the job exits (disable with --no-wait): ... ...
Verifikasi status Job:
ray job status pytorch-mnist
Outputnya mirip dengan hal berikut ini:
Job submission server address: http://localhost:8265 Status for job 'pytorch-mnist-job': RUNNING Status message: Job is currently running.
Tunggu hingga
Status for job
menjadiCOMPLETE
. Proses ini dapat memerlukan waktu 15 menit atau lebih.Melihat log tugas Ray:
ray job logs pytorch-mnist
Outputnya mirip dengan hal berikut ini:
Training started with configuration: ╭─────────────────────────────────────────────────╮ │ Training config │ ├──────────────────────────────────────────────────┤ │ train_loop_config/batch_size_per_worker 8 │ │ train_loop_config/epochs 10 │ │ train_loop_config/lr 0.001 │ ╰─────────────────────────────────────────────────╯ # Several lines omitted Training finished iteration 10 at 2024-06-19 08:29:36. Total running time: 9min 18s ╭───────────────────────────────╮ │ Training result │ ├────────────────────────────────┤ │ checkpoint_dir_name │ │ time_this_iter_s 25.7394 │ │ time_total_s 351.233 │ │ training_iteration 10 │ │ accuracy 0.8656 │ │ loss 0.37827 │ ╰───────────────────────────────╯ # Several lines omitted ------------------------------- Job 'pytorch-mnist' succeeded -------------------------------
Tinjau manifes berikut:
Manifes ini menjelaskan resource kustom RayJob.
Terapkan manifes ke cluster GKE Anda:
kubectl apply -f ray-job.yaml
Pastikan resource RayJob sedang berjalan:
kubectl get rayjob
Outputnya mirip dengan hal berikut ini:
NAME JOB STATUS DEPLOYMENT STATUS START TIME END TIME AGE pytorch-mnist-job RUNNING Running 2024-06-19T15:43:32Z 2m29s
Dalam output ini, kolom
DEPLOYMENT STATUS
menunjukkan bahwa resource RayJob adalahRunning
.Lihat status resource RayJob:
kubectl logs -f -l job-name=pytorch-mnist-job
Outputnya mirip dengan hal berikut ini:
Training started with configuration: ╭─────────────────────────────────────────────────╮ │ Training config │ ├──────────────────────────────────────────────────┤ │ train_loop_config/batch_size_per_worker 8 │ │ train_loop_config/epochs 10 │ │ train_loop_config/lr 0.001 │ ╰─────────────────────────────────────────────────╯ # Several lines omitted Training finished iteration 10 at 2024-06-19 08:29:36. Total running time: 9min 18s ╭───────────────────────────────╮ │ Training result │ ├────────────────────────────────┤ │ checkpoint_dir_name │ │ time_this_iter_s 25.7394 │ │ time_total_s 351.233 │ │ training_iteration 10 │ │ accuracy 0.8656 │ │ loss 0.37827 │ ╰───────────────────────────────╯ # Several lines omitted ------------------------------- Job 'pytorch-mnist' succeeded -------------------------------
Verifikasi bahwa tugas Ray telah selesai:
kubectl get rayjob
Outputnya mirip dengan hal berikut ini:
NAME JOB STATUS DEPLOYMENT STATUS START TIME END TIME AGE pytorch-mnist-job SUCCEEDED Complete 2024-06-19T15:43:32Z 2024-06-19T15:51:12Z 9m6s
Dalam output ini, kolom
DEPLOYMENT STATUS
menunjukkan bahwa resource RayJob adalahComplete
.- Pelajari arsitektur referensi, diagram, dan praktik terbaik tentang Google Cloud. Lihat Cloud Architecture Center kami.
Menyiapkan lingkungan Anda
Untuk menyiapkan lingkungan Anda, ikuti langkah-langkah berikut:
Membuat cluster GKE
Buat cluster GKE Autopilot atau Standard:
Autopilot
Buat cluster Autopilot:
gcloud container clusters create-auto ${CLUSTER_NAME} \
--enable-ray-operator \
--cluster-version=${CLUSTER_VERSION} \
--location=${COMPUTE_REGION}
Standar
Buat cluster Standard:
gcloud container clusters create ${CLUSTER_NAME} \
--addons=RayOperator \
--cluster-version=${CLUSTER_VERSION} \
--machine-type=e2-standard-8 \
--location=${COMPUTE_ZONE} \
--num-nodes=4
Men-deploy resource RayCluster
Deploy resource RayCluster ke cluster Anda:
Menghubungkan ke resource RayCluster
Hubungkan ke resource RayCluster untuk mengirimkan tugas Ray.
Melatih model
Latih model PyTorch menggunakan set data Fashion MNIST:
Men-deploy RayJob
Resource kustom RayJob mengelola siklus proses resource RayCluster selama eksekusi satu tugas Ray.
Pembersihan
Menghapus project
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Menghapus resource satu per satu
Jika Anda telah menggunakan project yang sudah ada dan tidak ingin menghapusnya, Anda dapat menghapus resource individual tersebut. Untuk menghapus cluster, ketik:
gcloud container clusters delete ${CLUSTER_NAME}