אימון מודל עם מעבדי GPU במצב GKE Autopilot
במדריך למתחילים הזה נסביר איך פורסים מודל אימון עם מעבדי GPU ב-Google Kubernetes Engine (GKE) ומאחסנים את התחזיות ב-Cloud Storage. המסמך הזה מיועד לאדמינים של GKE שיש להם אשכולות קיימים במצב Autopilot ורוצים להריץ עומסי עבודה של GPU בפעם הראשונה.
אפשר גם להריץ את עומסי העבודה האלה באשכולות Standard אם יוצרים מאגרי צמתים נפרדים של GPU באשכולות. הוראות מפורטות זמינות במאמר אימון מודל באמצעות GPU במצב GKE Standard.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the GKE and Cloud Storage APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the GKE and Cloud Storage APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
במסוף Google Cloud , מפעילים את Cloud Shell.
בחלק התחתון של Google Cloud המסוף יתחיל סשן של Cloud Shell ותופיע הודעה של שורת הפקודה. Cloud Shell היא סביבת מעטפת שבה ה-CLI של Google Cloud מותקן ומוגדרים ערכים לפרויקט הקיים. הסשן יופעל תוך כמה שניות.
שכפול המאגר לדוגמה
ב-Cloud Shell, מריצים את הפקודה הבאה:
git clone https://github.com/GoogleCloudPlatform/ai-on-gke && \
cd ai-on-gke/tutorials-and-examples/gpu-examples/training-single-gpu
יצירת אשכול
נכנסים לדף Create an Autopilot cluster במסוף Google Cloud :
בשדה Name, מזינים
gke-gpu-cluster.ברשימה Region בוחרים באזור
us-central1.לוחצים על יצירה.
יצירת קטגוריה של Cloud Storage
נכנסים לדף Create a bucket במסוף Google Cloud :
בקטע Get started, מזינים שם ייחודי לקטגוריה:
PROJECT_ID-gke-gpu-bucketמחליפים את
PROJECT_IDבמזהה הפרויקט ב- Google Cloud.לוחצים על Continue.
בשדה Location type, בוחרים באפשרות Region.
ברשימה Region, בוחרים באפשרות
us-central1 (Iowa)ולוחצים על Continue.בקטע בחירת סוג אחסון לנתונים, לוחצים על המשך.
בקטע Choose how to control access to objects, בוחרים באפשרות Uniform בשדה בקרת גישה.
לוחצים על יצירה.
בתיבת הדו-שיח Public access will be prevented, מוודאים שתיבת הסימון Enforce public access prevention on this bucket מסומנת, ולוחצים על Confirm.
הגדרת גישה לדלי באשכול באמצעות איחוד זהויות של עומסי עבודה ל-GKE
כדי לאפשר לאשכול לגשת לקטגוריה של Cloud Storage:
- יוצרים Kubernetes ServiceAccount באשכול.
- יוצרים מדיניות הרשאה ב-IAM שמאפשרת לחשבון השירות לגשת לקטגוריה.
יצירת חשבון שירות של Kubernetes באשכול
ב-Cloud Shell, מבצעים את הפעולות הבאות:
מתחברים לאשכול:
gcloud container clusters get-credentials gke-gpu-cluster \ --location=us-central1יוצרים מרחב שמות של Kubernetes:
kubectl create namespace gke-gpu-namespaceיוצרים ServiceAccount של Kubernetes במרחב השמות:
kubectl create serviceaccount gpu-k8s-sa --namespace=gke-gpu-namespace
יצירת מדיניות הרשאות IAM בקטגוריה
מקצים את התפקיד Storage Object Admin (roles/storage.objectAdmin) בקטגוריה לחשבון השירות של Kubernetes:
gcloud storage buckets add-iam-policy-binding gs://PROJECT_ID-gke-gpu-bucket \
--member=principal://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/PROJECT_ID.svc.id.goog/subject/ns/gke-gpu-namespace/sa/gpu-k8s-sa \
--role=roles/storage.objectAdmin \
--condition=None
מחליפים את PROJECT_NUMBER במספר הפרויקט ב- Google Cloud.
בדיקה אם ל-Pods יש גישה לקטגוריה של Cloud Storage
ב-Cloud Shell, יוצרים את משתני הסביבה הבאים:
export K8S_SA_NAME=gpu-k8s-sa export BUCKET_NAME=PROJECT_ID-gke-gpu-bucketמחליפים את
PROJECT_IDבמזהה הפרויקט ב- Google Cloud.יוצרים Pod עם קונטיינר של TensorFlow:
envsubst < src/gke-config/standard-tensorflow-bash.yaml | kubectl --namespace=gke-gpu-namespace apply -f -הפקודה הזו מוסיפה את משתני הסביבה שיצרתם להפניות המתאימות במניפסט. אפשר גם לפתוח את המניפסט בכלי לעריכת טקסט ולהחליף את
$K8S_SA_NAMEואת$BUCKET_NAMEבערכים המתאימים.יוצרים קובץ לדוגמה בקטגוריה:
touch sample-file gcloud storage cp sample-file gs://PROJECT_ID-gke-gpu-bucketמחכים שה-Pod יהיה מוכן:
kubectl wait --for=condition=Ready pod/test-tensorflow-pod -n=gke-gpu-namespace --timeout=180sכשה-Pod מוכן, הפלט הוא:
pod/test-tensorflow-pod condition metאם הזמן הקצוב לפקודה יסתיים, יכול להיות ש-GKE עדיין ייצור צמתים חדשים להרצת הפודים. מריצים שוב את הפקודה ומחכים שה-Pod יהיה מוכן.
פותחים מעטפת בקונטיינר של TensorFlow:
kubectl -n gke-gpu-namespace exec --stdin --tty test-tensorflow-pod --container tensorflow -- /bin/bashמנסים לקרוא את הקובץ לדוגמה שיצרתם:
ls /dataהפלט מציג את קובץ הדוגמה.
בודקים את היומנים כדי לזהות את ה-GPU שמצורף ל-Pod:
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"בפלט מוצג ה-GPU שמחובר ל-Pod, בדומה לדוגמה הבאה:
... PhysicalDevice(name='/physical_device:GPU:0',device_type='GPU')יוצאים מהקונטיינר:
exitמוחקים את ה-Pod לדוגמה:
kubectl delete -f src/gke-config/standard-tensorflow-bash.yaml \ --namespace=gke-gpu-namespace
אימון וחיזוי באמצעות מערך הנתונים MNIST
בקטע הזה מריצים עומס עבודה של אימון על מערך הנתונים לדוגמה MNIST.
מעתיקים את נתוני הדוגמה לקטגוריה של Cloud Storage:
gcloud storage cp src/tensorflow-mnist-example gs://PROJECT_ID-gke-gpu-bucket/ --recursiveיוצרים את משתני הסביבה הבאים:
export K8S_SA_NAME=gpu-k8s-sa export BUCKET_NAME=PROJECT_ID-gke-gpu-bucketבודקים את משימת האימון:
פורסים את משימת האימון:
envsubst < src/gke-config/standard-tf-mnist-train.yaml | kubectl -n gke-gpu-namespace apply -f -הפקודה הזו מחליפה את משתני הסביבה שיצרתם בהפניות המתאימות במניפסט. אפשר גם לפתוח את המניפסט בכלי לעריכת טקסט ולהחליף את
$K8S_SA_NAMEו-$BUCKET_NAMEבערכים המתאימים.מחכים עד שהמשימה תהיה בסטטוס
Completed:kubectl wait -n gke-gpu-namespace --for=condition=Complete job/mnist-training-job --timeout=180sכשהמשימה מוכנה, הפלט דומה לפלט הבא:
job.batch/mnist-training-job condition metאם פסק הזמן של הפקודה יחלוף, יכול להיות ש-GKE עדיין ייצור צמתים חדשים להפעלת ה-Pods. מריצים שוב את הפקודה וממתינים עד שהמשימה תהיה מוכנה.
בודקים את היומנים מהקונטיינר של TensorFlow:
kubectl logs -f jobs/mnist-training-job -c tensorflow -n gke-gpu-namespaceבפלט מוצגים האירועים הבאים:
- התקנת חבילות Python הנדרשות
- הורדת מערך הנתונים של MNIST
- אימון המודל באמצעות GPU
- שמירת המודל
- הערכת המודל
... Epoch 12/12 927/938 [============================>.] - ETA: 0s - loss: 0.0188 - accuracy: 0.9954 Learning rate for epoch 12 is 9.999999747378752e-06 938/938 [==============================] - 5s 6ms/step - loss: 0.0187 - accuracy: 0.9954 - lr: 1.0000e-05 157/157 [==============================] - 1s 4ms/step - loss: 0.0424 - accuracy: 0.9861 Eval loss: 0.04236088693141937, Eval accuracy: 0.9861000180244446 Training finished. Model savedמחיקת עומס העבודה של האימון:
kubectl -n gke-gpu-namespace delete -f src/gke-config/standard-tf-mnist-train.yaml
פריסת עומס עבודה של הסקה
בקטע הזה תפרסו עומס עבודה של היקשים שמקבל מערך נתונים לדוגמה כקלט ומחזיר חיזויים.
מעתיקים את התמונות לחיזוי לקטגוריה:
gcloud storage cp data/mnist_predict gs://PROJECT_ID-gke-gpu-bucket/ --recursiveבודקים את עומס העבודה של ההסקות:
פריסת עומס העבודה של ההסקה:
envsubst < src/gke-config/standard-tf-mnist-batch-predict.yaml | kubectl -n gke-gpu-namespace apply -f -הפקודה הזו מחליפה את משתני הסביבה שיצרתם בהפניות המתאימות במניפסט. אפשר גם לפתוח את המניפסט בכלי לעריכת טקסט ולהחליף את
$K8S_SA_NAMEו-$BUCKET_NAMEבערכים המתאימים.מחכים עד שהסטטוס של המשימה יהיה
Completed:kubectl wait -n gke-gpu-namespace --for=condition=Complete job/mnist-batch-prediction-job --timeout=180sהפלט אמור להיראות כך:
job.batch/mnist-batch-prediction-job condition metבודקים את היומנים מהקונטיינר של TensorFlow:
kubectl logs -f jobs/mnist-batch-prediction-job -c tensorflow -n gke-gpu-namespaceהפלט הוא התחזית לכל תמונה ורמת הביטחון של המודל בתחזית, בדומה לזה:
Found 10 files belonging to 1 classes. 1/1 [==============================] - 2s 2s/step The image /data/mnist_predict/0.png is the number 0 with a 100.00 percent confidence. The image /data/mnist_predict/1.png is the number 1 with a 99.99 percent confidence. The image /data/mnist_predict/2.png is the number 2 with a 100.00 percent confidence. The image /data/mnist_predict/3.png is the number 3 with a 99.95 percent confidence. The image /data/mnist_predict/4.png is the number 4 with a 100.00 percent confidence. The image /data/mnist_predict/5.png is the number 5 with a 100.00 percent confidence. The image /data/mnist_predict/6.png is the number 6 with a 99.97 percent confidence. The image /data/mnist_predict/7.png is the number 7 with a 100.00 percent confidence. The image /data/mnist_predict/8.png is the number 8 with a 100.00 percent confidence. The image /data/mnist_predict/9.png is the number 9 with a 99.65 percent confidence.
הסרת המשאבים
כדי להימנע מחיובים בחשבון Google Cloud על המשאבים שיצרתם במדריך הזה, אפשר לבצע אחת מהפעולות הבאות:
- משאירים את אשכול GKE: מוחקים את משאבי Kubernetes באשכול ואת משאבי Google Cloud
- שמירת הפרויקט Google Cloud : מחיקת אשכול GKE והמשאבים Google Cloud
- מחיקת הפרויקט
מחיקת משאבי Kubernetes באשכול ו Google Cloud המשאבים
מוחקים את מרחב השמות של Kubernetes ואת עומסי העבודה שפרסתם:
kubectl -n gke-gpu-namespace delete -f src/gke-config/standard-tf-mnist-batch-predict.yaml kubectl delete namespace gke-gpu-namespaceמוחקים את הקטגוריה של Cloud Storage:
נכנסים לדף Buckets:
מסמנים את התיבה
PROJECT_ID-gke-gpu-bucket.לוחצים על מחיקה.
כדי לאשר את המחיקה, מקלידים
DELETEולוחצים על מחיקה.
מוחקים את Google Cloud חשבון השירות:
עוברים לדף Service accounts:
בוחרים את הפרויקט הרצוי.
מסמנים את התיבה
gke-gpu-sa@PROJECT_ID.iam.gserviceaccount.com.לוחצים על מחיקה.
לוחצים על מחיקה כדי לאשר.
מחיקת אשכול GKE ו Google Cloud המשאבים
מוחקים את אשכול GKE:
עוברים לדף Clusters:
מסמנים את התיבה של
gke-gpu-cluster.לוחצים על מחיקה.
כדי לאשר את המחיקה, מקלידים
gke-gpu-clusterולוחצים על מחיקה.
מוחקים את הקטגוריה של Cloud Storage:
נכנסים לדף Buckets:
מסמנים את התיבה
PROJECT_ID-gke-gpu-bucket.לוחצים על מחיקה.
כדי לאשר את המחיקה, מקלידים
DELETEולוחצים על מחיקה.
מוחקים את חשבון השירות Google Cloud :
עוברים לדף Service accounts:
בוחרים את הפרויקט הרצוי.
מסמנים את התיבה
gke-gpu-sa@PROJECT_ID.iam.gserviceaccount.com.לוחצים על מחיקה.
לוחצים על מחיקה כדי לאשר.
מחיקת הפרויקט
- במסוף Google Cloud , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.