Panduan ini memberikan contoh cara men-deploy dan menayangkan model Stable Diffusion di Google Kubernetes Engine (GKE) menggunakan Ray Serve dan add-on Ray Operator sebagai contoh penerapan.
Tentang Ray dan Ray Serve
Ray adalah framework komputasi skalabel open source untuk aplikasi AI/ML. Ray Serve adalah library inferensi model untuk Ray yang digunakan untuk menskalakan dan menyajikan model di lingkungan terdistribusi. Untuk mengetahui informasi selengkapnya, lihat Ray Serve dalam dokumentasi Ray.
Anda dapat menggunakan resource RayCluster atau RayService untuk men-deploy aplikasi Ray Serve. Anda harus menggunakan resource RayService dalam produksi karena alasan berikut:
- Update di tempat untuk aplikasi RayService
- Upgrade tanpa periode nonaktif untuk resource RayCluster
- Aplikasi Ray Serve dengan ketersediaan tinggi
Tujuan
Panduan ini ditujukan untuk 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.
- Buat cluster GKE dengan node pool GPU.
- Buat cluster Ray menggunakan resource kustom RayCluster.
- Jalankan aplikasi Ray Serve.
- Deploy resource kustom RayService.
Biaya
Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih sebagai 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, baca bagian Pembersihan.
Sebelum memulai
Cloud Shell telah diinstal dengan software yang Anda perlukan untuk tutorial ini, termasuk kubectl dan gcloud CLI. Jika Anda 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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith 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_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the GKE API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith 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_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the GKE API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.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.admingcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
Luncurkan sesi Cloud Shell dari Google Cloud konsol, dengan mengklik
Activate 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=rayserve-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: Project ID Google Cloud Anda.CLUSTER_VERSION: versi GKE yang akan digunakan. Harus1.30.1atau setelahnya.
Buat clone repositori GitHub:
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samplesUbah ke direktori kerja:
cd kubernetes-engine-samples/ai-ml/gke-ray/rayserve/stable-diffusionBuat lingkungan virtual Python:
venv
python -m venv myenv && \ source myenv/bin/activateConda
Jalankan perintah berikut:
conda create -c conda-forge python=3.9.19 -n myenv && \ conda activate myenv
Saat Anda men-deploy aplikasi Serve dengan
serve run, Ray mengharapkan versi Python klien lokal cocok dengan versi yang digunakan di cluster Ray. Imagerayproject/ray:2.37.0menggunakan Python 3.9. Jika Anda menjalankan versi klien yang berbeda, pilih image Ray yang sesuai.Instal dependensi yang diperlukan untuk menjalankan aplikasi Serve:
pip install ray[serve]==2.37.0 pip install torch pip install requestsBuat cluster Standard:
gcloud container clusters create ${CLUSTER_NAME} \ --addons=RayOperator \ --cluster-version=${CLUSTER_VERSION} \ --machine-type=c3d-standard-8 \ --location=${COMPUTE_ZONE} \ --num-nodes=1Buat node pool GPU:
gcloud container node-pools create gpu-pool \ --cluster=${CLUSTER_NAME} \ --machine-type=g2-standard-8 \ --location=${COMPUTE_ZONE} \ --num-nodes=1 \ --accelerator type=nvidia-l4,count=1,gpu-driver-version=latestTinjau manifes berikut:
Manifes ini menjelaskan resource RayCluster.
Terapkan manifes ke cluster Anda:
kubectl apply -f ray-cluster.yamlPastikan resource RayCluster sudah siap:
kubectl get rayclusterOutputnya mirip dengan hal berikut ini:
NAME DESIRED WORKERS AVAILABLE WORKERS CPUS MEMORY GPUS STATUS AGE stable-diffusion-cluster 2 2 6 20Gi 0 ready 33sDalam output ini,
readydi kolomSTATUSmenunjukkan bahwa resource RayCluster sudah siap.Pastikan GKE membuat layanan RayCluster:
kubectl get svc stable-diffusion-cluster-head-svcOutputnya 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 109sBuat sesi penerusan port ke head Ray:
kubectl port-forward svc/stable-diffusion-cluster-head-svc 8265:8265 2>&1 >/dev/null & kubectl port-forward svc/stable-diffusion-cluster-head-svc 10001:10001 2>&1 >/dev/null &Pastikan klien Ray dapat terhubung ke cluster Ray menggunakan localhost:
ray list nodes --address http://localhost:8265Outputnya mirip dengan hal berikut ini:
======== List: 2024-06-19 15:15:15.707336 ======== 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 omittedJalankan aplikasi Stable Diffusion Ray Serve:
serve run stable_diffusion:entrypoint --working-dir=. --runtime-env-json='{"pip": ["torch", "torchvision", "diffusers==0.12.1", "huggingface_hub==0.25.2", "transformers", "fastapi==0.113.0"], "excludes": ["myenv"]}' --address ray://localhost:10001Outputnya mirip dengan hal berikut ini:
2024-06-19 18:20:58,444 INFO scripts.py:499 -- Running import path: 'stable_diffusion:entrypoint'. 2024-06-19 18:20:59,730 INFO packaging.py:530 -- Creating a file package for local directory '.'. 2024-06-19 18:21:04,833 INFO handle.py:126 -- Created DeploymentHandle 'hyil6u9f' for Deployment(name='StableDiffusionV2', app='default'). 2024-06-19 18:21:04,834 INFO handle.py:126 -- Created DeploymentHandle 'xo25rl4k' for Deployment(name='StableDiffusionV2', app='default'). 2024-06-19 18:21:04,836 INFO handle.py:126 -- Created DeploymentHandle '57x9u4fp' for Deployment(name='APIIngress', app='default'). 2024-06-19 18:21:04,836 INFO handle.py:126 -- Created DeploymentHandle 'xr6kt85t' for Deployment(name='StableDiffusionV2', app='default'). 2024-06-19 18:21:04,836 INFO handle.py:126 -- Created DeploymentHandle 'g54qagbz' for Deployment(name='APIIngress', app='default'). 2024-06-19 18:21:19,139 INFO handle.py:126 -- Created DeploymentHandle 'iwuz00mv' for Deployment(name='APIIngress', app='default'). 2024-06-19 18:21:19,139 INFO api.py:583 -- Deployed app 'default' successfully.Buat sesi penerusan port ke port Ray Serve (8000):
kubectl port-forward svc/stable-diffusion-cluster-head-svc 8000:8000 2>&1 >/dev/null &Jalankan skrip Python:
python generate_image.pySkrip menghasilkan gambar ke file bernama
output.png. Gambar tersebut mirip dengan berikut:
Tinjau manifes berikut:
Manifes ini menjelaskan resource kustom RayService.
Terapkan manifes ke cluster Anda:
kubectl apply -f ray-service.yamlPastikan bahwa Service sudah siap:
kubectl get svc stable-diffusion-serve-svcOutputnya mirip dengan hal berikut ini:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE stable-diffusion-serve-svc ClusterIP 34.118.236.0 <none> 8000/TCP 31mKonfigurasi penerusan port ke Layanan Ray Serve:
kubectl port-forward svc/stable-diffusion-serve-svc 8000:8000 2>&1 >/dev/null &Jalankan skrip Python dari bagian sebelumnya:
python generate_image.pySkrip ini menghasilkan gambar yang mirip dengan gambar yang dihasilkan di bagian sebelumnya.
- 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 dengan node pool GPU
Buat cluster GKE Autopilot atau Standard dengan node pool GPU:
Autopilot
Buat cluster Autopilot:
gcloud container clusters create-auto ${CLUSTER_NAME} \
--enable-ray-operator \
--cluster-version=${CLUSTER_VERSION} \
--location=${COMPUTE_REGION}
Standar
Men-deploy resource RayCluster
Untuk men-deploy resource RayCluster:
Menghubungkan ke resource RayCluster
Untuk terhubung ke resource RayCluster:
Menjalankan aplikasi Ray Serve
Untuk menjalankan aplikasi Ray Serve:
Men-deploy RayService
Resource kustom RayService mengelola siklus proses resource RayCluster dan aplikasi Ray Serve.
Untuk mengetahui informasi selengkapnya tentang RayService, lihat Deploy Ray Serve Applications dan Production Guide dalam dokumentasi Ray.
Untuk men-deploy resource RayService, ikuti langkah-langkah berikut:
Mengamati workload Ray
Untuk melihat detail RayJob, Anda dapat membuka bagian Kubernetes Engine > AI/ML > Jobs di konsol. Google Cloud
Pembersihan
Menghapus project
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Menghapus resource satu per satu
Untuk menghapus cluster, ketik:
gcloud container clusters delete ${CLUSTER_NAME}