Melatih model dengan Ray dan PyTorch di Google Kubernetes Engine (GKE)


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.

Pengguna Google Cloud baru mungkin memenuhi syarat untuk mendapatkan uji coba gratis.

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.

  1. 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.
  2. Install the Google Cloud CLI.

  3. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  4. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  5. 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.

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the GKE API:

    gcloud services enable container.googleapis.com
  8. Install the Google Cloud CLI.

  9. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  10. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  11. 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.

  12. Make sure that billing is enabled for your Google Cloud project.

  13. Enable the GKE API:

    gcloud services enable container.googleapis.com
  14. 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.
  15. Menyiapkan lingkungan Anda

    Untuk menyiapkan lingkungan Anda, ikuti langkah-langkah berikut:

    1. Luncurkan sesi Cloud Shell dari Google Cloud konsol, dengan mengklik Ikon aktivasi Cloud Shell Aktifkan Cloud Shell di Google Cloud konsol. Tindakan ini akan meluncurkan sesi di panel bawah konsol Google Cloud .

    2. 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. Harus 1.30.1 atau setelahnya.
    3. Buat clone repositori GitHub:

      git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
      
    4. Ubah ke direktori kerja:

      cd kubernetes-engine-samples/ai-ml/gke-ray/raytrain/pytorch-mnist
      
    5. Buat lingkungan virtual Python:

      python -m venv myenv && \
      source myenv/bin/activate
      
    6. Instal Ray.

    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:

    1. Tinjau manifes berikut:

      apiVersion: ray.io/v1
      kind: RayCluster
      metadata:
        name: pytorch-mnist-cluster
      spec:
        rayVersion: '2.37.0'
        headGroupSpec:
          rayStartParams:
            dashboard-host: '0.0.0.0'
          template:
            metadata:
            spec:
              containers:
              - name: ray-head
                image: rayproject/ray:2.37.0
                ports:
                - containerPort: 6379
                  name: gcs
                - containerPort: 8265
                  name: dashboard
                - containerPort: 10001
                  name: client
                resources:
                  limits:
                    cpu: "2"
                    ephemeral-storage: "9Gi"
                    memory: "4Gi"
                  requests:
                    cpu: "2"
                    ephemeral-storage: "9Gi"
                    memory: "4Gi"
        workerGroupSpecs:
        - replicas: 4
          minReplicas: 1
          maxReplicas: 5
          groupName: worker-group
          rayStartParams: {}
          template:
            spec:
              containers:
              - name: ray-worker
                image: rayproject/ray:2.37.0
                resources:
                  limits:
                    cpu: "4"
                    ephemeral-storage: "9Gi"
                    memory: "8Gi"
                  requests:
                    cpu: "4"
                    ephemeral-storage: "9Gi"
                    memory: "8Gi"

      Manifes ini menjelaskan resource kustom RayCluster.

    2. Terapkan manifes ke cluster GKE Anda:

      kubectl apply -f ray-cluster.yaml
      
    3. 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 kolom STATUS menunjukkan bahwa resource RayCluster sudah siap.

    Menghubungkan ke resource RayCluster

    Hubungkan ke resource RayCluster untuk mengirimkan tugas Ray.

    1. 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
      
    2. Buat sesi penerusan port ke head Ray:

      kubectl port-forward svc/pytorch-mnist-cluster-head-svc 8265:8265 2>&1 >/dev/null &
      
    3. 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:

    1. 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):
      ...
      ...
      
    2. 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 menjadi COMPLETE. Proses ini dapat memerlukan waktu 15 menit atau lebih.

    3. 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.

    1. Tinjau manifes berikut:

      apiVersion: ray.io/v1
      kind: RayJob
      metadata:
        name: pytorch-mnist-job
      spec:
        shutdownAfterJobFinishes: true
        entrypoint: python ai-ml/gke-ray/raytrain/pytorch-mnist/train.py
        runtimeEnvYAML: |
          pip:
            - torch
            - torchvision
          working_dir: "https://github.com/GoogleCloudPlatform/kubernetes-engine-samples/archive/main.zip"
          env_vars:
            NUM_WORKERS: "4"
            CPUS_PER_WORKER: "2"
        rayClusterSpec:
          rayVersion: '2.37.0'
          headGroupSpec:
            rayStartParams: {}
            template:
              spec:
                containers:
                  - name: ray-head
                    image: rayproject/ray:2.37.0
                    ports:
                      - containerPort: 6379
                        name: gcs-server
                      - containerPort: 8265
                        name: dashboard
                      - containerPort: 10001
                        name: client
                    resources:
                      limits:
                        cpu: "2"
                        ephemeral-storage: "9Gi"
                        memory: "4Gi"
                      requests:
                        cpu: "2"
                        ephemeral-storage: "9Gi"
                        memory: "4Gi"
          workerGroupSpecs:
            - replicas: 4
              minReplicas: 1
              maxReplicas: 5
              groupName: small-group
              rayStartParams: {}
              template:
                spec:
                  containers:
                    - name: ray-worker
                      image: rayproject/ray:2.37.0
                      resources:
                        limits:
                          cpu: "4"
                          ephemeral-storage: "9Gi"
                          memory: "8Gi"
                        requests:
                          cpu: "4"
                          ephemeral-storage: "9Gi"
                          memory: "8Gi"

      Manifes ini menjelaskan resource kustom RayJob.

    2. Terapkan manifes ke cluster GKE Anda:

      kubectl apply -f ray-job.yaml
      
    3. 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 adalah Running.

    4. 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
      -------------------------------
      
    5. 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 adalah Complete.

    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}
    

    Langkah berikutnya