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.
Menyiapkan lingkungan Anda
Untuk menyiapkan lingkungan Anda, ikuti langkah-langkah berikut:
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
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:
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.
Menghubungkan ke resource RayCluster
Hubungkan ke resource RayCluster untuk mengirimkan tugas Ray.
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
Melatih model
Latih model PyTorch menggunakan set data Fashion MNIST:
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 -------------------------------
Men-deploy RayJob
Resource kustom RayJob mengelola siklus proses resource RayCluster selama eksekusi satu tugas Ray.
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
.