GKE Standard モードで GPU を使用してモデルをトレーニングする
このクイックスタート チュートリアルでは、Google Kubernetes Engine(GKE)で GPU を使用するトレーニング モデルをデプロイし、予測を Cloud Storage に保存する方法について説明します。このチュートリアルでは、TensorFlow モデルと GKE Standard クラスタを使用します。こうしたワークロードは、Autopilot クラスタでより少ない設定手順で実行することもできます。 手順については、GKE Autopilot モードで GPU を使用してモデルをトレーニングするをご覧ください。
このドキュメントは、Standard クラスタがすでに存在し、GPU ワークロードを初めて実行する GKE 管理者を対象としています。
始める前に
-
In the Trusted Cloud console, on the project selector page, select or create a Trusted Cloud project.
-
Make sure that billing is enabled for your Trusted Cloud project.
-
Enable the Kubernetes Engine and Cloud Storage APIs.
-
In the Trusted Cloud console, activate Cloud Shell.
At the bottom of the Trusted Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
GKE 用 Workload Identity 連携を使用する Standard クラスタを作成し、Cloud Storage FUSE ドライバをインストールします。
gcloud container clusters create gke-gpu-cluster \ --addons GcsFuseCsiDriver \ --location=us-central1 \ --num-nodes=1 \ --workload-pool=PROJECT_ID.svc.id.goog
PROJECT_ID
は、実際の Trusted Cloudプロジェクト ID に置き換えます。クラスタの作成には数分かかることもあります。
GPU ノードプールを作成します。
gcloud container node-pools create gke-gpu-pool-1 \ --accelerator=type=nvidia-tesla-t4,count=1,gpu-driver-version=default \ --machine-type=n1-standard-16 --num-nodes=1 \ --location=us-central1 \ --cluster=gke-gpu-cluster
Trusted Cloud コンソールで、[バケットの作成] ページに移動します。
[バケットに名前を付ける] フィールドに、次の名前を入力します。
PROJECT_ID-gke-gpu-bucket
[続行] をクリックします。
[ロケーション タイプ] で、[リージョン] を選択します。
[リージョン] リストで
us-central1 (Iowa)
を選択し、[続行] をクリックします。[データのストレージ クラスを選択する] セクションで、[続行] をクリックします。
[オブジェクトへのアクセスを制御する方法を選択する] セクションで、[アクセス制御] には [均一] を選択します。
[作成] をクリックします。
[公開アクセスの防止] ダイアログで、[このバケットに対する公開アクセス禁止を適用する] チェックボックスをオンにして、[確認] をクリックします。
- Trusted Cloud サービス アカウントを作成する。
- クラスタに Kubernetes ServiceAccount を作成する。
- Kubernetes ServiceAccount を Trusted Cloud サービス アカウントにバインドする。
Trusted Cloud コンソールで、[サービス アカウントの作成] ページに移動します。
[サービス アカウント ID] フィールドに「
gke-ai-sa
」と入力します。[作成して続行] をクリックします。
[ロール] リストで、[Cloud Storage] > [Storage Insights コレクタ サービス] ロールを選択します。
[
別のロールを追加] をクリックします。[ロールを選択] リストで、[Cloud Storage] > [Storage Object Admin] ロールを選択します。
[続行] をクリックして [完了] をクリックします。
Kubernetes Namespace を作成します。
kubectl create namespace gke-ai-namespace
名前空間に Kubernetes ServiceAccount を作成します。
kubectl create serviceaccount gpu-k8s-sa --namespace=gke-ai-namespace
Trusted Cloud サービス アカウントに IAM バインディングを追加します。
gcloud iam service-accounts add-iam-policy-binding gke-ai-sa@PROJECT_ID.s3ns-system.iam.gserviceaccount.com \ --role roles/iam.workloadIdentityUser \ --member "serviceAccount:PROJECT_ID.svc.id.goog[gke-ai-namespace/gpu-k8s-sa]"
--member
フラグは、 Trusted Cloud上の Kubernetes ServiceAccount の完全な ID を指定します。Kubernetes ServiceAccount にアノテーションを付けます。
kubectl annotate serviceaccount gpu-k8s-sa \ --namespace gke-ai-namespace \ iam.gke.io/gcp-service-account=gke-ai-sa@PROJECT_ID.s3ns-system.iam.gserviceaccount.com
Cloud Shell で、次の環境変数を作成します。
export K8S_SA_NAME=gpu-k8s-sa export BUCKET_NAME=PROJECT_ID-gke-gpu-bucket
PROJECT_ID
は、実際の Trusted Cloudプロジェクト ID に置き換えます。TensorFlow コンテナを含む Pod を作成します。
envsubst < src/gke-config/standard-tensorflow-bash.yaml | kubectl --namespace=gke-ai-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-ai-namespace --timeout=180s
Pod の準備ができると、出力は次のようになります。
pod/test-tensorflow-pod condition met
TensorFlow コンテナでシェルを開きます。
kubectl -n gke-ai-namespace exec --stdin --tty test-tensorflow-pod --container tensorflow -- /bin/bash
作成したサンプル ファイルを閲覧してみます。
ls /data
出力にサンプル ファイルが表示されます。
ログを確認して Pod に接続されている GPU を特定します。
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
出力には、Pod に接続された GPU が次のように表示されます。
... PhysicalDevice(name='/physical_device:GPU:0',device_type='GPU')
コンテナを終了します。
exit
サンプル Pod を削除します。
kubectl delete -f src/gke-config/standard-tensorflow-bash.yaml \ --namespace=gke-ai-namespace
サンプルデータを 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
トレーニング Job を確認します。
トレーニング Job をデプロイします。
envsubst < src/gke-config/standard-tf-mnist-train.yaml | kubectl -n gke-ai-namespace apply -f -
このコマンドで、作成した環境変数がマニフェスト内の対応する参照に代入されます。マニフェストをテキスト エディタで開いて、
$K8S_SA_NAME
と$BUCKET_NAME
を対応する値に置き換えることもできます。Job のステータスが
Completed
になるまで待ちます。kubectl wait -n gke-ai-namespace --for=condition=Complete job/mnist-training-job --timeout=180s
出力は次のようになります。
job.batch/mnist-training-job condition met
TensorFlow コンテナのログを確認します。
kubectl logs -f jobs/mnist-training-job -c tensorflow -n gke-ai-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-ai-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-ai-namespace apply -f -
このコマンドで、作成した環境変数がマニフェスト内の対応する参照に代入されます。マニフェストをテキスト エディタで開いて、
$K8S_SA_NAME
と$BUCKET_NAME
を対応する値に置き換えることもできます。Job のステータスが
Completed
になるまで待ちます。kubectl wait -n gke-ai-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-ai-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.
- GKE クラスタを保持する場合: クラスタにある Kubernetes リソースと Trusted Cloud リソースを削除する
- Trusted Cloud プロジェクトを保持する場合: GKE クラスタと Trusted Cloud リソースを削除する
- プロジェクトを削除する
Kubernetes Namespace とデプロイしたワークロードを削除します。
kubectl -n gke-ai-namespace delete -f src/gke-config/standard-tf-mnist-batch-predict.yaml kubectl delete namespace gke-ai-namespace
Cloud Storage バケットを削除します。
[バケット] ページに移動します。
PROJECT_ID-gke-gpu-bucket
のチェックボックスをオンにします。[
削除] をクリックします。削除を確定するには、「
DELETE
」と入力して [削除] をクリックします。
Trusted Cloud サービス アカウントを削除します。
[サービス アカウント] ページに移動します。
プロジェクトを選択します。
gke-ai-sa@PROJECT_ID.s3ns-system.iam.gserviceaccount.com
のチェックボックスをオンにします。[
削除] をクリックします。削除を確定するには、[削除] をクリックします。
GKE クラスタを削除します。
[クラスタ] ページに移動します。
gke-gpu-cluster
のチェックボックスをオンにします。[
削除] をクリックします。削除を確定するには、「
gke-gpu-cluster
」と入力して [削除] をクリックします。
Cloud Storage バケットを削除します。
[バケット] ページに移動します。
PROJECT_ID-gke-gpu-bucket
のチェックボックスをオンにします。[
削除] をクリックします。削除を確定するには、「
DELETE
」と入力して [削除] をクリックします。
Trusted Cloud サービス アカウントを削除します。
[サービス アカウント] ページに移動します。
プロジェクトを選択します。
gke-ai-sa@PROJECT_ID.s3ns-system.iam.gserviceaccount.com
のチェックボックスをオンにします。[
削除] をクリックします。削除を確定するには、[削除] をクリックします。
- In the Trusted Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
サンプル リポジトリのクローンを作成する
Cloud Shell で、次のコマンドを実行します。
git clone https://github.com/GoogleCloudPlatform/ai-on-gke/ ai-on-gke
cd ai-on-gke/tutorials-and-examples/gpu-examples/training-single-gpu
Standard モードのクラスタと GPU ノードプールを作成する
Cloud Shell で以下の操作を行います。
Cloud Storage バケットを作成する
GKE 用 Workload Identity 連携を使用してバケットにアクセスするようにクラスタを構成する
クラスタから Cloud Storage バケットにアクセスできるようにする手順は次のとおりです。
Trusted Cloud サービス アカウントを作成する
クラスタに Kubernetes ServiceAccount を作成する
Cloud Shell で、次の操作を行います。
Kubernetes ServiceAccount を Trusted Cloud サービス アカウントにバインドする
Cloud Shell で、次のコマンドを実行します。
Pod が Cloud Storage バケットにアクセスできることを確認する
MNIST
データセットを使用してトレーニングと予測を行う
このセクションでは、サンプル データセット MNIST
でトレーニング ワークロードを実行します。
推論ワークロードをデプロイする
このセクションでは、サンプル データセットを入力として受け取り、予測を返す推論ワークロードをデプロイします。
クリーンアップ
このガイドで作成したリソースについて Trusted Cloud アカウントに課金されないようにするには、次のいずれかを行います。