すべてのプロダクト
Search
ドキュメントセンター

E-MapReduce:モデル学習に Kubeflow を使用する

最終更新日:Jan 11, 2025

Kubeflow は、TFJob や PyTorchJob などのカスタムリソース定義 (CRD) を提供します。これらの CRD を使用して、Kubernetes クラスターで分散トレーニングジョブを実行できます。このようにして、分散コードロジックやクラスターの O&M を管理する必要なく、モデル開発に集中できます。Data Science クラスターは、TensorFlow や PyTorch など、安定したコンピューティング機能と豊富な機械学習フレームワークを提供します。

前提条件

  • E-MapReduce (EMR) Data Science クラスターが作成され、クラスターの作成時にオプションサービスから Kubeflow が選択されています。詳細については、クラスターの作成をご参照ください。
  • dsdemo コードがダウンロードされています。32497587 の番号の DingTalk グループに参加して、dsdemo コードを入手できます。

背景情報

このトピックでは、モデル学習の例として次の方法を使用します。

準備

  1. SSH モードでクラスターにログオンします。詳細については、クラスターへのログオンをご参照ください。
  2. ダウンロードした dsdemo コードパッケージをクラスターのマスターノードのディレクトリにアップロードし、コードパッケージを解凍します。
    この例では、コードパッケージを root/dsdemo ディレクトリにアップロードし、コードパッケージをそのディレクトリに解凍します。ビジネス要件に基づいてディレクトリを指定できます。

Estimator API の呼び出し

  1. 次のコマンドを実行して、estimator-API ディレクトリにアクセスします。
    cd /root/dsdemo/kubeflow_samples/training/tf/estimator-API
  2. トレーニングイメージを作成します。
    • 次のコマンドを実行して、トレーニングイメージを作成します。
      make
    • 次のコマンドを実行して、トレーニングイメージを作成し、イメージをイメージリポジトリにプッシュします。
      make push
  3. ビジネス要件に基づいて 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
  4. 変更を有効にするには、次のコマンドを実行します。
    kubectl apply -f distributed_tfjob.yaml
  5. ビジネス要件に基づいて 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 に設定できます。
  6. 変更を有効にするには、次のコマンドを実行します。
    kubectl apply -f pvc.yaml
  7. トレーニングステータスを表示します。
    1. ポッドに関する情報をクエリするには、次のコマンドを実行します。
      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 の場合、トレーニングは終了です。
    2. ログ情報を表示するには、次のコマンドを実行します。
      kubectl logs distributed-training-worker-0
  8. チェックポイント形式のモデルエクスポートファイルを表示します。
    1. 次のコマンドを実行して、default_storage_class ディレクトリにアクセスします。
      cd /mnt/disk1/k8s_pv/default_storage_class
    2. 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
                                      
    3. 次のコマンドを実行して、特定のモデルディレクトリにアクセスします。
      cd default-strategy-estimator-volume-pvc-6cd2979e-f925-45b7-8c25-ce67e12e2f03
      説明 default-strategy-estimator-volume-pvc-6cd2979e-f925-45b7-8c25-ce67e12e2f03 を実際のディレクトリに置き換えます。
    4. 次のコマンドを実行して、master ディレクトリにアクセスします。
      cd master
    5. ll コマンドを実行して、前のディレクトリのサブディレクトリを表示します。
      total 4
      drwxr-xr-x 3 root root 4096 Jul  8 16:27 1625732821
    6. 次のコマンドを実行して、master ディレクトリ内の特定のディレクトリにアクセスします。
      cd 1625732821
      説明 1625732821 を実際のディレクトリに置き換えます。
    7. 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

Keras API の呼び出し

重要 この方法を使用する場合は、GPU リソースが必要です。イメージの作成にも GPU リソースが必要です。したがって、Data Science クラスターを作成するときは、GPU 構成のタイプを選択する必要があります。
  1. 次のコマンドを実行して、keras-API ディレクトリにアクセスします。
    cd /root/dsdemo/kubeflow_samples/training/tf/keras-API
  2. トレーニングイメージを作成します。
    • 次のコマンドを実行して、トレーニングイメージを作成します。
      make
    • 次のコマンドを実行して、トレーニングイメージを作成し、イメージをイメージリポジトリにプッシュします。
      make push
  3. 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
  4. 変更を有効にするには、次のコマンドを実行します。
    kubectl apply -f multi_worker_tfjob.yaml
  5. 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 に設定できます。
  6. 変更を有効にするには、次のコマンドを実行します。
    kubectl apply -f pvc.yaml
  7. トレーニングステータスを表示します。
    1. ポッドに関する情報をクエリするには、次のコマンドを実行します。
      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 の場合、トレーニングは終了です。
    2. ログ情報を表示するには、次のコマンドを実行します。
      kubectl logs distributed-training-worker-0
  8. チェックポイント形式のモデルエクスポートファイルを表示するには、次のコマンドを実行します。
    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-daee90ee25a41625553118 を実際のディレクトリに置き換えます。
    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 の使用

  1. 次のコマンドを実行して、mnist ディレクトリにアクセスします。
    cd /root/dsdemo/kubeflow_samples/training/pytorch/mnist
  2. トレーニングイメージを作成します。
    • 次のコマンドを実行して、トレーニングイメージを作成します。
      make
    • 次のコマンドを実行して、トレーニングイメージを作成し、イメージをイメージリポジトリにプッシュします。
      make push
  3. ビジネス要件に基づいて、トレーニング構成ファイルのイメージ名を変更します。
    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
  4. 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 に設定できます。
  5. 永続ボリュームクレーム (PVC) を申請し、トレーニングジョブを送信します。
    kubectl apply -f ./v1/pytorch_job_mnist_gloo.yaml
  6. トレーニングステータスを表示します。
    1. ポッドに関する情報をクエリするには、次のコマンドを実行します。
      kubectl get pods
      次のような情報が出力されます。
      NAME                                      READY   STATUS      RESTARTS   AGE
      pytorch-dist-mnist-gloo-worker-0          0/1     Pending     0          11m
      説明 すべてのポッドのステータスが Completed の場合、トレーニングは終了です。
    2. ログ情報を表示するには、次のコマンドを実行します。
      kubectl logs pytorch-dist-mnist-gloo-master-0
  7. チェックポイント形式のモデルエクスポートファイルを表示するには、次のコマンドを実行します。
    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