Panduan ini menunjukkan cara men-deploy dan menyajikan model Stable Diffusion di Google Kubernetes Engine (GKE) menggunakan TPU, Ray Serve, dan add-on Ray Operator.
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.
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 dalam lingkungan terdistribusi. Untuk mengetahui informasi selengkapnya, lihat Ray Serve dalam dokumentasi Ray.
Tentang TPU
Tensor Processing Unit (TPU) adalah akselerator hardware khusus yang dirancang untuk mempercepat pelatihan dan inferensi model machine learning berskala besar secara signifikan. Dengan menggunakan Ray bersama TPU, Anda dapat menskalakan aplikasi ML berperforma tinggi dengan lancar. Untuk mengetahui informasi selengkapnya tentang TPU, lihat Pengantar Cloud TPU dalam dokumentasi Cloud TPU.
Tentang webhook inisialisasi TPU KubeRay
Sebagai bagian dari add-on Ray Operator, GKE menyediakan
webhook
validasi dan mutasi
yang menangani penjadwalan Pod TPU dan variabel lingkungan TPU tertentu yang diperlukan
oleh framework seperti
JAX untuk inisialisasi penampung. Webhook TPU KubeRay
mengubah Pod dengan label app.kubernetes.io/name: kuberay yang
meminta TPU dengan properti berikut:
TPU_WORKER_ID: Bilangan bulat unik untuk setiap Pod pekerja dalam slice TPU.TPU_WORKER_HOSTNAMES: Daftar nama host DNS untuk semua pekerja TPU yang perlu berkomunikasi satu sama lain dalam slice. Variabel ini hanya disisipkan untuk Pod TPU dalam grup multi-host.replicaIndex: Label Pod yang berisi ID unik untuk replika grup pekerja tempat Pod berada. Hal ini berguna untuk grup pekerja multi-host, di mana beberapa Pod pekerja mungkin termasuk dalam replika yang sama, dan digunakan oleh Ray untuk mengaktifkan penskalaan otomatis multi-host.TPU_NAME: String yang merepresentasikan PodSlice TPU GKE tempat Pod ini berada, ditetapkan ke nilai yang sama dengan labelreplicaIndex.podAffinity: Memastikan GKE menjadwalkan Pod TPU dengan labelreplicaIndexyang cocok di node pool yang sama. Hal ini memungkinkan GKE menskalakan TPU multi-host secara atomik berdasarkan node pool, bukan node tunggal.
Tujuan
- Buat cluster GKE dengan node pool TPU.
- Deploy cluster Ray dengan TPU.
- Deploy resource kustom RayService.
- Berinteraksi dengan server model Stable Diffusion.
Biaya
Di 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, 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, instal 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.
- SSD Persistent Disk (GB)
- Alamat IP yang digunakan
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-central2-b export CLUSTER_VERSION=CLUSTER_VERSIONGanti kode berikut:
PROJECT_ID: Project ID Google CloudAnda.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 cluster mode Standard dengan Ray Operator diaktifkan:
gcloud container clusters create ${CLUSTER_NAME} \ --addons=RayOperator \ --machine-type=n1-standard-8 \ --cluster-version=${CLUSTER_VERSION} \ --location=${COMPUTE_REGION}Buat node pool TPU host tunggal:
gcloud container node-pools create tpu-pool \ --location=${COMPUTE_REGION} \ --cluster=${CLUSTER_NAME} \ --machine-type=ct4p-hightpu-4t \ --num-nodes=1- Lokasi Compute Engine dengan kapasitas untuk akselerator TPU
- Jenis mesin yang kompatibel untuk TPU dan
- Topologi fisik PodSlice TPU
Tinjau manifes berikut:
Manifes ini menjelaskan resource kustom RayService yang membuat resource RayCluster dengan 1 node head dan grup pekerja TPU dengan topologi 2x2x1, yang berarti setiap node pekerja akan memiliki 4 chip TPU v4.
Node TPU termasuk dalam satu podslice TPU v4 dengan topologi 2x2x1. Untuk membuat grup pekerja multi-host, ganti nilai
gke-tpu nodeSelector, batas dan permintaan penampunggoogle.com/tpu, dan nilainumOfHostsdengan konfigurasi multi-host Anda. Untuk mengetahui informasi selengkapnya tentang topologi multi-host TPU, lihat Arsitektur sistem dalam dokumentasi Cloud TPU.Terapkan manifes ke cluster Anda:
kubectl apply -f ray-service-tpu.yamlPastikan resource RayService sedang berjalan:
kubectl get rayservicesOutputnya mirip dengan hal berikut ini:
NAME SERVICE STATUS NUM SERVE ENDPOINTS stable-diffusion-tpu Running 2Dalam output ini,
Runningdi kolomSERVICE STATUSmenunjukkan bahwa resource RayService sudah siap.Buat sesi penerusan port ke dasbor Ray dari layanan head Ray:
kubectl port-forward svc/stable-diffusion-tpu-head-svc 8265:8265Di browser web, buka
http://localhost:8265/.Klik tab Serve.
Buat sesi penerusan port ke endpoint Serve dari layanan head Ray:
kubectl port-forward svc/stable-diffusion-tpu-serve-svc 8000Buka sesi Cloud Shell baru.
Kirim perintah teks ke gambar ke server model Stable Diffusion:
python stable_diffusion_tpu_req.py --save_picturesHasil inferensi stable diffusion disimpan ke file bernama
diffusion_results.png.
- Pelajari Ray di Kubernetes.
- Pelajari dokumentasi KubeRay.
- Pelajari arsitektur referensi, diagram, dan praktik terbaik tentang Google Cloud. Lihat Cloud Architecture Center kami.
Pastikan kuota mencukupi
Pastikan project Anda memiliki kuota TPU yang cukup di region atau zona Compute Engine Anda. Google Cloud Untuk mengetahui informasi selengkapnya, lihat Memastikan kuota TPU dan GKE yang memadai dalam dokumentasi Cloud TPU. Anda mungkin juga perlu meningkatkan kuota untuk:
Menyiapkan lingkungan Anda
Untuk menyiapkan lingkungan Anda, ikuti langkah-langkah berikut:
Membuat cluster dengan node pool TPU
Buat cluster GKE Standard dengan node pool TPU:
Untuk menggunakan TPU dengan mode Standar, Anda harus memilih:
Mengonfigurasi resource RayCluster dengan TPU
Konfigurasi manifes RayCluster untuk menyiapkan workload TPU:
Mengonfigurasi TPU nodeSelector
GKE menggunakan nodeSelectors Kubernetes untuk memastikan bahwa workload TPU dijadwalkan pada topologi dan akselerator TPU yang sesuai. Untuk mengetahui informasi selengkapnya tentang memilih nodeSelector TPU, lihat Men-deploy workload TPU di GKE Standard.
Perbarui manifes ray-cluster.yaml untuk menjadwalkan Pod Anda di podslice TPU v4
dengan topologi 2x2x1:
nodeSelector:
cloud.google.com/gke-tpu-accelerator: tpu-v4-podslice
cloud.google.com/gke-tpu-topology: 2x2x1
Mengonfigurasi resource container TPU
Untuk menggunakan akselerator TPU, Anda harus menentukan jumlah chip TPU yang harus dialokasikan GKE ke setiap Pod dengan mengonfigurasi google.com/tpuresource limits dan requests di kolom container TPU pada manifes RayCluster workerGroupSpecs.
Perbarui manifes ray-cluster.yaml dengan batas dan permintaan resource:
resources:
limits:
cpu: "1"
ephemeral-storage: 10Gi
google.com/tpu: "4"
memory: "2G"
requests:
cpu: "1"
ephemeral-storage: 10Gi
google.com/tpu: "4"
memory: "2G"
Mengonfigurasi grup pekerja numOfHosts
KubeRay v1.1.0 menambahkan kolom numOfHosts ke resource kustom RayCluster,
yang menentukan jumlah host TPU yang akan dibuat per replika grup pekerja.
Untuk grup pekerja multi-host, replika diperlakukan sebagai PodSlice, bukan pekerja individual, dengan node pekerja numOfHosts dibuat per replika.
Perbarui manifes ray-cluster.yaml dengan kode berikut:
workerGroupSpecs:
# Several lines omitted
numOfHosts: 1 # the number of "hosts" or workers per replica
Buat resource kustom RayService
Buat resource kustom RayService:
(Opsional) Melihat Dasbor Ray
Anda dapat melihat deployment Ray Serve dan log yang relevan dari Dasbor Ray.
Mengirim perintah ke server model
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}