Kubeflow は、TFJob や PyTorchJob などのカスタムリソース定義 (CRD) を提供します。これらの CRD を使用して、Kubernetes クラスターで分散トレーニングジョブを実行できます。このようにして、分散コードロジックやクラスターの O&M を管理する必要なく、モデル開発に集中できます。Data Science クラスターは、TensorFlow や PyTorch など、安定したコンピューティング機能と豊富な機械学習フレームワークを提供します。
前提条件
- E-MapReduce (EMR) Data Science クラスターが作成され、クラスターの作成時にオプションサービスから Kubeflow が選択されています。詳細については、クラスターの作成をご参照ください。
- dsdemo コードがダウンロードされています。32497587 の番号の DingTalk グループに参加して、dsdemo コードを入手できます。
背景情報
このトピックでは、モデル学習の例として次の方法を使用します。
準備
- SSH モードでクラスターにログオンします。詳細については、クラスターへのログオンをご参照ください。
- ダウンロードした dsdemo コードパッケージをクラスターのマスターノードのディレクトリにアップロードし、コードパッケージを解凍します。この例では、コードパッケージを root/dsdemo ディレクトリにアップロードし、コードパッケージをそのディレクトリに解凍します。ビジネス要件に基づいてディレクトリを指定できます。
Estimator API の呼び出し
- 次のコマンドを実行して、estimator-API ディレクトリにアクセスします。
cd /root/dsdemo/kubeflow_samples/training/tf/estimator-API - トレーニングイメージを作成します。
- 次のコマンドを実行して、トレーニングイメージを作成します。
make - 次のコマンドを実行して、トレーニングイメージを作成し、イメージをイメージリポジトリにプッシュします。
make push
- 次のコマンドを実行して、トレーニングイメージを作成します。
- ビジネス要件に基づいて distributed_tfjob.yaml ファイルを変更します。次のコードは、distributed_tfjob.yaml ファイルの内容を示しています。コード内の
imageの値を変更する必要があります。apiVersion: "kubeflow.org/v1" kind: "TFJob" metadata: name: "distributed-training" spec: cleanPodPolicy: None tfReplicaSpecs: Worker: replicas: 3 restartPolicy: Never template: metadata: annotations: scheduling.k8s.io/group-name: "distributed-training" spec: containers: - name: tensorflow image: datascience-registry.cn-beijing.cr.aliyuncs.com/kubeflow-examples/distributed_worker:0.1.4 volumeMounts: - mountPath: /train name: training volumes: - name: training persistentVolumeClaim: claimName: strategy-estimator-volume - 変更を有効にするには、次のコマンドを実行します。
kubectl apply -f distributed_tfjob.yaml - ビジネス要件に基づいて pvc.yaml 構成ファイルを変更します。次のコードは、pvc.yaml 構成ファイルの内容を示しています。
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: strategy-estimator-volume labels: app: strategy-estimator-volume spec: storageClassName: "nfs-client" accessModes: - ReadWriteMany resources: requests: storage: 10Gi説明storageパラメーターの値を増やすことができます。たとえば、このパラメーターを 50Gi に設定できます。 - 変更を有効にするには、次のコマンドを実行します。
kubectl apply -f pvc.yaml - トレーニングステータスを表示します。
- ポッドに関する情報をクエリするには、次のコマンドを実行します。
kubectl get pods次のような情報が出力されます。NAME READY STATUS RESTARTS AGE distributed-training-worker-0 0/1 Completed 0 22h distributed-training-worker-1 0/1 Completed 0 22h distributed-training-worker-2 0/1 Completed 0 22h nfs-client-provisioner-5cb8b7cf76-k2z4d 1/1 Running 0 25h説明 すべてのポッドのステータスが Completed の場合、トレーニングは終了です。 - ログ情報を表示するには、次のコマンドを実行します。
kubectl logs distributed-training-worker-0
- ポッドに関する情報をクエリするには、次のコマンドを実行します。
- チェックポイント形式のモデルエクスポートファイルを表示します。
- 次のコマンドを実行して、default_storage_class ディレクトリにアクセスします。
cd /mnt/disk1/k8s_pv/default_storage_class llコマンドを実行して、default_storage_class ディレクトリからモデルディレクトリを取得します。total 28 drwxrwxrwx 2 root root 4096 Jul 8 16:56 anonymous-workspace-ds-notebook2-pvc-09789bbd-aa40-4381-af63-ebcab57c584f drwxrwxrwx 7 root root 4096 Jul 8 18:09 anonymous-workspace-ds-notebook-pvc-ae4db112-9491-4449-854a-7b0e67264703 drwxrwxrwx 5 root root 4096 Jul 8 16:27 default-strategy-estimator-volume-pvc-6cd2979e-f925-45b7-8c25-ce67e12e2f03 drwxrwxrwx 3 root root 4096 Jul 8 13:47 kubeflow-katib-mysql-pvc-9a3ebbfe-2952-4eaa-8a83-36e42d620cac drwxrwxrwx 3 root root 4096 Jul 8 13:47 kubeflow-metadata-mysql-pvc-5778e165-8a08-4536-8e36-9185144d3d6d drwxrwxrwx 3 root root 4096 Jul 8 13:47 kubeflow-minio-pvc-pvc-10015211-fc40-460e-b723-a5c220112d08 drwxrwxrwx 6 polkitd ssh_keys 4096 Jul 8 13:54 kubeflow-mysql-pv-claim-pvc-5136b864-c868-4951-9b34-f6c5f06c2d6f- 次のコマンドを実行して、特定のモデルディレクトリにアクセスします。
cd default-strategy-estimator-volume-pvc-6cd2979e-f925-45b7-8c25-ce67e12e2f03説明default-strategy-estimator-volume-pvc-6cd2979e-f925-45b7-8c25-ce67e12e2f03を実際のディレクトリに置き換えます。 - 次のコマンドを実行して、master ディレクトリにアクセスします。
cd master llコマンドを実行して、前のディレクトリのサブディレクトリを表示します。total 4 drwxr-xr-x 3 root root 4096 Jul 8 16:27 1625732821- 次のコマンドを実行して、master ディレクトリ内の特定のディレクトリにアクセスします。
cd 1625732821説明1625732821を実際のディレクトリに置き換えます。 llコマンドを実行して、前のディレクトリのサブディレクトリを表示します。total 1788 -rw-r--r-- 1 root root 130 Jul 8 16:27 checkpoint -rw-r--r-- 1 root root 838550 Jul 8 16:27 events.out.tfevents.1625732823.distributed-training-worker-0 -rw-r--r-- 1 root root 523304 Jul 8 16:27 graph.pbtxt drwxr-xr-x 2 root root 4096 Jul 8 16:27 keras -rw-r--r-- 1 root root 780 Jul 8 16:27 model.ckpt-0.data-00000-of-00001 -rw-r--r-- 1 root root 249 Jul 8 16:27 model.ckpt-0.index -rw-r--r-- 1 root root 218354 Jul 8 16:27 model.ckpt-0.meta -rw-r--r-- 1 root root 780 Jul 8 16:27 model.ckpt-3200.data-00000-of-00001 -rw-r--r-- 1 root root 249 Jul 8 16:27 model.ckpt-3200.index -rw-r--r-- 1 root root 218354 Jul 8 16:27 model.ckpt-3200.meta
- 次のコマンドを実行して、default_storage_class ディレクトリにアクセスします。
Keras API の呼び出し
重要 この方法を使用する場合は、GPU リソースが必要です。イメージの作成にも GPU リソースが必要です。したがって、Data Science クラスターを作成するときは、GPU 構成のタイプを選択する必要があります。
- 次のコマンドを実行して、keras-API ディレクトリにアクセスします。
cd /root/dsdemo/kubeflow_samples/training/tf/keras-API - トレーニングイメージを作成します。
- 次のコマンドを実行して、トレーニングイメージを作成します。
make - 次のコマンドを実行して、トレーニングイメージを作成し、イメージをイメージリポジトリにプッシュします。
make push
- 次のコマンドを実行して、トレーニングイメージを作成します。
- keras-API ディレクトリに保存されているトレーニング構成ファイル multi_worker_tfjob.yaml のイメージ名を、ビジネス要件に基づいて変更します。次のコードは、multi_worker_tfjob.yaml ファイルの内容を示しています。
apiVersion: kubeflow.org/v1 kind: TFJob metadata: name: multi-worker spec: cleanPodPolicy: None tfReplicaSpecs: Worker: replicas: 3 restartPolicy: Never template: spec: containers: - name: tensorflow image: datascience-registry.cn-beijing.cr.aliyuncs.com/kubeflow-examples/multi_worker_strategy:0.1.1 volumeMounts: - mountPath: /train name: training resources: limits: nvidia.com/gpu: 1 volumes: - name: training persistentVolumeClaim: claimName: strategy-volume - 変更を有効にするには、次のコマンドを実行します。
kubectl apply -f multi_worker_tfjob.yaml - pvc.yaml 構成ファイルを変更します。次のコードは、pvc.yaml 構成ファイルの内容を示しています。
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: strategy-volume labels: app: strategy-volume spec: storageClassName: "nfs-client" accessModes: - ReadWriteMany resources: requests: storage: 10Gi説明storageパラメーターの値を増やすことができます。たとえば、このパラメーターを 50Gi に設定できます。 - 変更を有効にするには、次のコマンドを実行します。
kubectl apply -f pvc.yaml - トレーニングステータスを表示します。
- ポッドに関する情報をクエリするには、次のコマンドを実行します。
kubectl get pods次のような情報が出力されます。NAME READY STATUS RESTARTS AGE multi-worker-worker-0 0/1 Pending 0 5m33s multi-worker-worker-1 0/1 Pending 0 5m33s multi-worker-worker-2 0/1 Pending 0 5m33s nfs-client-provisioner-5cb8b7cf76-k2z4d 1/1 Running 0 25h説明 すべてのポッドのステータスが Completed の場合、トレーニングは終了です。 - ログ情報を表示するには、次のコマンドを実行します。
kubectl logs distributed-training-worker-0
- ポッドに関する情報をクエリするには、次のコマンドを実行します。
- チェックポイント形式のモデルエクスポートファイルを表示するには、次のコマンドを実行します。
ll /mnt/disk1/k8s_pv/default_storage_class/default-strategy-estimator-volume-pvc-aa16c081-cd94-4565-bfde-daee90ee25a4/master/1625553118/説明default-strategy-estimator-volume-pvc-aa16c081-cd94-4565-bfde-daee90ee25a4と1625553118を実際のディレクトリに置き換えます。total 1788 -rw-r--r-- 1 root root 130 Jul 6 14:32 checkpoint -rw-r--r-- 1 root root 838550 Jul 6 14:32 events.out.tfevents.1625553120.distributed-training-worker-0 -rw-r--r-- 1 root root 523405 Jul 6 14:32 graph.pbtxt drwxr-xr-x 2 root root 4096 Jul 6 14:31 keras -rw-r--r-- 1 root root 780 Jul 6 14:32 model.ckpt-0.data-00000-of-00001 -rw-r--r-- 1 root root 249 Jul 6 14:32 model.ckpt-0.index -rw-r--r-- 1 root root 218354 Jul 6 14:32 model.ckpt-0.meta -rw-r--r-- 1 root root 780 Jul 6 14:32 model.ckpt-3200.data-00000-of-00001 -rw-r--r-- 1 root root 249 Jul 6 14:32 model.ckpt-3200.index -rw-r--r-- 1 root root 218354 Jul 6 14:32 model.ckpt-3200.meta
PyTorch の使用
- 次のコマンドを実行して、mnist ディレクトリにアクセスします。
cd /root/dsdemo/kubeflow_samples/training/pytorch/mnist - トレーニングイメージを作成します。
- 次のコマンドを実行して、トレーニングイメージを作成します。
make - 次のコマンドを実行して、トレーニングイメージを作成し、イメージをイメージリポジトリにプッシュします。
make push
- 次のコマンドを実行して、トレーニングイメージを作成します。
- ビジネス要件に基づいて、トレーニング構成ファイルのイメージ名を変更します。
apiVersion: "kubeflow.org/v1" kind: "PyTorchJob" metadata: name: "pytorch-dist-mnist-gloo" spec: pytorchReplicaSpecs: Master: replicas: 1 restartPolicy: OnFailure template: metadata: annotations: sidecar.istio.io/inject: "false" spec: containers: - name: pytorch image: datascience-registry.cn-beijing.cr.aliyuncs.com/kubeflow-examples/pytorch-dist-mnist-test:0.1.5 args: ["--backend", "gloo"] volumeMounts: - mountPath: /train name: training # CPU を使用するには、以下のリソースをコメントアウトします。 resources: limits: nvidia.com/gpu: 1 volumes: - name: training persistentVolumeClaim: claimName: strategy-pytorch-volume Worker: replicas: 1 restartPolicy: OnFailure template: metadata: annotations: sidecar.istio.io/inject: "false" spec: containers: - name: pytorch image: datascience-registry.cn-beijing.cr.aliyuncs.com/kubeflow-examples/pytorch-dist-mnist-test:0.1.5 args: ["--backend", "gloo"] volumeMounts: - mountPath: /train name: training # CPU を使用するには、以下のリソースをコメントアウトします。 resources: limits: nvidia.com/gpu: 1 volumes: - name: training persistentVolumeClaim: claimName: strategy-pytorch-volume - pvc.yaml 構成ファイルを変更します。
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: strategy-pytorch-volume labels: app: strategy-pytorch-volume spec: storageClassName: "nfs-client" accessModes: - ReadWriteMany resources: requests: storage: 10Gi説明storageパラメーターの値を増やすことができます。たとえば、このパラメーターを 50Gi に設定できます。 - 永続ボリュームクレーム (PVC) を申請し、トレーニングジョブを送信します。
kubectl apply -f ./v1/pytorch_job_mnist_gloo.yaml - トレーニングステータスを表示します。
- ポッドに関する情報をクエリするには、次のコマンドを実行します。
kubectl get pods次のような情報が出力されます。NAME READY STATUS RESTARTS AGE pytorch-dist-mnist-gloo-worker-0 0/1 Pending 0 11m説明 すべてのポッドのステータスが Completed の場合、トレーニングは終了です。 - ログ情報を表示するには、次のコマンドを実行します。
kubectl logs pytorch-dist-mnist-gloo-master-0
- ポッドに関する情報をクエリするには、次のコマンドを実行します。
- チェックポイント形式のモデルエクスポートファイルを表示するには、次のコマンドを実行します。
cd /mnt/disk1/k8s_pv/default_storage_class/default-strategy-pytorch-volume-pvc-633b12ab-5f63-4e63-97b9-24c2b3de733c/説明default-strategy-pytorch-volume-pvc-633b12ab-5f63-4e63-97b9-24c2b3de733cを実際のディレクトリに置き換えます。次のような情報が出力されます。total 1688 -rw-r--r-- 1 root root 1725813 Jul 6 15:35 mnist_cnn.pt