

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# HyperPod 提供的基本生命週期指令碼
<a name="sagemaker-hyperpod-lifecycle-best-practices-slurm-slurm-base-config"></a>

本節將以***由上而下***方式地引導您完成在 HyperPod 上設定 Slurm 的基本流程的每個元件 它從準備 HyperPod 叢集建立請求以執行 `CreateCluster` API 開始，並深入探討階層結構到生命週期指令碼。使用 [Awsome 分散式訓練 GitHub 儲存庫](https://github.com/aws-samples/awsome-distributed-training/)中提供的範例生命週期指令碼。執行下列命令來複製儲存庫。

```
git clone https://github.com/aws-samples/awsome-distributed-training/
```

您可以在 [https://github.com/aws-samples/awsome-distributed-training/tree/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config](https://github.com/aws-samples/awsome-distributed-training/tree/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config) 取得在 SageMaker HyperPod 上設定 Slurm 叢集的基本生命週期指令碼。

```
cd awsome-distributed-training/1.architectures/5.sagemaker_hyperpods/LifecycleScripts/base-config
```

下列流程圖顯示如何設計基本生命週期指令碼的詳細概觀。圖表下方的描述和程序指南說明它們在 HyperPod `CreateCluster` API 呼叫期間的運作方式。

![\[HyperPod 叢集建立的詳細流程圖和生命週期指令碼的結構。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/hyperpod-lifecycle-structure.png)


***圖：**HyperPod 叢集建立的詳細流程圖和生命週期指令碼的結構。(1) 虛線箭頭導向「呼叫至」方塊的位置，並顯示組態檔案和生命週期指令碼準備的流程。它從準備 `provisioning_parameters.json` 和生命週期指令碼開始。這些接著會依序在 `lifecycle_script.py` 中編碼以進行集體執行。`lifecycle_script.py` 指令碼的執行是由 `on_create.sh` Shell 指令碼完成，該指令碼將要在 HyperPod 執行個體終端機中執行。(2) 實心箭頭顯示主要 HyperPod 叢集建立流程，以及如何「呼叫至」或「提交至」方塊。`on_create.sh` 是叢集建立請求的必要項目，可以在 `create_cluster.json` 中或在主控台 UI 的**建立叢集**請求表單中建立。在您提交請求之後，HyperPod 會根據來自請求和生命週期指令碼的指定組態資訊執行 `CreateCluster` API。(3) 虛線箭頭表示 HyperPod 平台在叢集資源佈建期間於叢集執行個體中建立 `resource_config.json`。`resource_config.json` 包含 HyperPod 叢集資源資訊，例如叢集 ARN、執行個體類型和 IP 位址。請務必注意，您應該準備生命週期指令碼，以在叢集建立期間預期 `resource_config.json` 檔案。如需詳細資訊，請參閱下方的程序指南。*

下列程序指南說明 HyperPod 叢集建立期間發生的情況，以及基本生命週期指令碼的設計方式。

1. `create_cluster.json` - 若要提交 HyperPod 叢集建立請求，您需要準備 JSON 格式的 `CreateCluster`。請求檔案。在此最佳實務範例中，我們假設請求檔案名為 `create_cluster.json`。撰寫 `create_cluster.json` 以使用執行個體群組佈建 HyperPod 叢集。最佳實務是新增的執行個體群組數量與您計劃在 HyperPod 叢集上設定的 Slurm 節點數量相同。確定您將獨特的名稱提供給執行個體群組，而您會將這些執行個體群組指派給您計劃設定的 Slurm 節點。

   此外，您必須指定 S3 儲存貯體路徑，將整組組態檔案和生命週期指令碼存放至 `CreateCluster` 請求表單中的欄位名稱 `InstanceGroups.LifeCycleConfig.SourceS3Uri`，並將進入點 Shell 指令碼的檔案名稱 (假設名為 `on_create.sh`) 指定至 `InstanceGroups.LifeCycleConfig.OnCreate`。
**注意**  
如果您在 HyperPod 主控台 UI 中使用**建立叢集**提交表單，主控台會代表您管理填入和提交 `CreateCluster` 請求，並在後端執行 `CreateCluster` API。在此情況下，您不需要建立 `create_cluster.json`；反之，確定您將正確的叢集組態資訊指定至**建立叢集**提交表單。

1. `on_create.sh` - 對於每個執行個體群組，您需要提供進入點 Shell 指令碼 `on_create.sh`，以執行命令、執行指令碼來安裝軟體套件，以及使用 Slurm 設定 HyperPod 叢集環境。需要您準備的兩個項目是 HyperPod 需要用於設定 Slurm 的 `provisioning_parameters.json`，以及一組用於安裝軟體套件的生命週期指令碼。應撰寫此指令碼以尋找並執行下列檔案，如 [https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/on_create.sh](https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/on_create.sh) 的範例指令碼所示。
**注意**  
確定您將整組生命週期指令碼上傳至您在 `create_cluster.json` 中指定的 S3 位置。您也應該將 `provisioning_parameters.json` 放置在相同的位置中。

   1. `provisioning_parameters.json` - 這是一個 [provisioning\$1parameters.json 的組態表單](sagemaker-hyperpod-ref.md#sagemaker-hyperpod-ref-provisioning-forms-slurm)。`on_create.sh` 指令碼會尋找此 JSON 檔案，並定義用於識別其路徑的環境變數。透過此 JSON 檔案，您可以設定 Slurm 節點和儲存選項，例如 Slurm 要與其通訊的 Amazon FSx for Lustre。在 `provisioning_parameters.json` 中，請確定根據您計劃的設定方式，使用在 `create_cluster.json` 中指定的名稱，將 HyperPod 叢集執行個體群組適當地指派給 Slurm 節點。

      下圖顯示如何撰寫兩個 JSON 組態檔案 `create_cluster.json` 和 `provisioning_parameters.json`，以將 HyperPod 執行個體群組指派給 Slurm 節點的範例。在此範例中，我們假設一種設定三個 Slurm 節點的情況：控制器 (管理) 節點、登入節點 (此為選用) 和運算 (工作者) 節點。
**提示**  
為了協助您驗證這兩個 JSON 檔案，HyperPod 服務團隊提供一個驗證指令碼，即 [https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/validate-config.py](https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/validate-config.py)。如需詳細資訊，請參閱 [在 HyperPod 上建立 Slurm 叢集之前驗證 JSON 組態檔案](sagemaker-hyperpod-lifecycle-best-practices-slurm-slurm-validate-json-files.md)。  
![\[.json 檔案之間的直接比較。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/hyperpod-lifecycle-slurm-config.png)

      ***圖：**`create_cluster.json` 進行 HyperPod 叢集建立與 `provisiong_params.json` 進行 Slurm 設定之間的直接比較。`create_cluster.json` 中的執行個體群組數量應與您要設定為 Slurm 節點的節點數量相符。以圖中的範例為例，將在三個執行個體群組的 HyperPod 叢集上設定三個 Slurm 節點。您應該透過相應地指定執行個體群組名稱，將 HyperPod 叢集執行個體群組指派給 Slurm 節點。*

   1. `resource_config.json` - 在叢集建立期間，會撰寫 `lifecycle_script.py` 以預期來自 HyperPod 的 `resource_config.json` 檔案。此檔案包含叢集的相關資訊，例如執行個體類型和 IP 位址。

      當您執行 `CreateCluster` API 時，HyperPod 會根據 `create_cluster.json` 檔案在 `/opt/ml/config/resource_config.json` 建立資源組態檔案。檔案路徑會儲存至名為 `SAGEMAKER_RESOURCE_CONFIG_PATH` 的環境變數。
**重要**  
`resource_config.json` 檔案是由 HyperPod 平台自動產生，您不需要建立該檔案。下列程式碼展示了將根據上一個步驟中的 `create_cluster.json` 從叢集建立中建立的 `resource_config.json` 範例，並協助您了解後端中發生的情況，以及自動產生的 `resource_config.json` 外觀。

      ```
      {
      
          "ClusterConfig": {
              "ClusterArn": "arn:aws:sagemaker:us-west-2:111122223333:cluster/abcde01234yz",
              "ClusterName": "your-hyperpod-cluster"
          },
          "InstanceGroups": [
              {
                  "Name": "controller-machine",
                  "InstanceType": "ml.c5.xlarge",
                  "Instances": [
                      {
                          "InstanceName": "controller-machine-1",
                          "AgentIpAddress": "111.222.333.444",
                          "CustomerIpAddress": "111.222.333.444",
                          "InstanceId": "i-12345abcedfg67890"
                      }
                  ]
              },
              {
                  "Name": "login-group",
                  "InstanceType": "ml.m5.xlarge",
                  "Instances": [
                      {
                          "InstanceName": "login-group-1",
                          "AgentIpAddress": "111.222.333.444",
                          "CustomerIpAddress": "111.222.333.444",
                          "InstanceId": "i-12345abcedfg67890"
                      }
                  ]
              },
              {
                  "Name": "compute-nodes",
                  "InstanceType": "ml.trn1.32xlarge",
                  "Instances": [
                      {
                          "InstanceName": "compute-nodes-1",
                          "AgentIpAddress": "111.222.333.444",
                          "CustomerIpAddress": "111.222.333.444",
                          "InstanceId": "i-12345abcedfg67890"
                      },
                      {
                          "InstanceName": "compute-nodes-2",
                          "AgentIpAddress": "111.222.333.444",
                          "CustomerIpAddress": "111.222.333.444",
                          "InstanceId": "i-12345abcedfg67890"
                      },
                      {
                          "InstanceName": "compute-nodes-3",
                          "AgentIpAddress": "111.222.333.444",
                          "CustomerIpAddress": "111.222.333.444",
                          "InstanceId": "i-12345abcedfg67890"
                      },
                      {
                          "InstanceName": "compute-nodes-4",
                          "AgentIpAddress": "111.222.333.444",
                          "CustomerIpAddress": "111.222.333.444",
                          "InstanceId": "i-12345abcedfg67890"
                      }
                  ]
              }
          ]
      }
      ```

   1. `lifecycle_script.py` - 這是主要 Python 指令碼，其會在佈建時集體執行在 HyperPod 叢集上設定 Slurm 的生命週期指令碼。此指令碼會在 `provisioning_parameters.json` 和 `resource_config.json` 從 `on_create.sh` 中指定或識別的路徑讀取、將相關資訊傳遞至每個生命週期指令碼，然後依序執行生命週期指令碼。

      生命週期指令碼是一組指令碼，您可以完全靈活地自訂這些指令碼來安裝軟體套件，並在叢集建立期間設定必要或自訂組態，例如設定 Slurm、建立使用者、安裝 Conda 或 Docker。範例 [https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/lifecycle_script.py](https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/lifecycle_script.py) 指令碼已準備好在儲存庫中執行其他基本生命週期指令碼，例如啟動 Slurm 常駐程式 ([https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/start_slurm.sh](https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/start_slurm.sh))、掛載 Amazon FSx for Lustre ([https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/mount_fsx.sh](https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/mount_fsx.sh))，以及設定 MariaDB 會計 ([https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/setup_mariadb_accounting.sh](https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/setup_mariadb_accounting.sh)) 和 RDS 會計 ([https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/setup_rds_accounting.sh](https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/setup_rds_accounting.sh))。您也可以新增更多指令碼、將其封裝在相同的目錄下，並將程式碼行新增至 `lifecycle_script.py`，讓 HyperPod 執行指令碼。如需基本生命週期指令碼的詳細資訊，另請參閱 *Awsome 分散式訓練 GitHub 儲存庫*中的 [3.1 生命週期指令碼](https://github.com/aws-samples/awsome-distributed-training/tree/main/1.architectures/5.sagemaker-hyperpod#31-lifecycle-scripts)。
**注意**  
HyperPod 會在叢集的每個執行個體上執行 [SageMaker HyperPod DLAMI](sagemaker-hyperpod-ref.md#sagemaker-hyperpod-ref-hyperpod-ami)，而 AMI 已預先安裝符合其與 HyperPod 功能之間相容性的軟體套件。請注意，如果您重新安裝任何預先安裝的套件，您必須負責安裝相容的套件，並注意部分 HyperPod 功能可能無法如預期運作。

      除了預設設定之外，[https://github.com/aws-samples/awsome-distributed-training/tree/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/utils](https://github.com/aws-samples/awsome-distributed-training/tree/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/utils) 資料夾下還提供更多用於安裝下列軟體的指令碼。`lifecycle_script.py` 檔案已準備好包含用於執行安裝指令碼的程式碼行，因此請查看下列項目以搜尋這些行，並取消註解以啟用它們。

      1. 下列程式碼行用於安裝 [Docker](https://www.docker.com/)、[Enroot](https://github.com/NVIDIA/enroot) 和 [Pyxis](https://github.com/NVIDIA/pyxis)。在 Slurm 叢集上執行 Docker 容器需要這些套件。

         若要啟用此安裝步驟，請在 [https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/config.py](https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/config.py) 檔案中將 `enable_docker_enroot_pyxis` 參數設定為 `True`。

         ```
         # Install Docker/Enroot/Pyxis
         if Config.enable_docker_enroot_pyxis:
             ExecuteBashScript("./utils/install_docker.sh").run()
             ExecuteBashScript("./utils/install_enroot_pyxis.sh").run(node_type)
         ```

      1. 您可以將 HyperPod 叢集與 [Amazon Managed Service for Prometheus](https://docs.aws.amazon.com/prometheus/latest/userguide/what-is-Amazon-Managed-Service-Prometheus.html) 和 [Amazon Managed Grafana](https://docs.aws.amazon.com/grafana/latest/userguide/what-is-Amazon-Managed-Service-Grafana.html) 整合，以將有關 HyperPod 叢集和叢集節點的指標匯出至 Amazon Managed Grafana 儀表板。若要匯出指標並使用 Amazon Managed Grafana 上的 [Slurm 儀表板](https://grafana.com/grafana/dashboards/4323-slurm-dashboard/)、[NVIDIA DCGM Exporter 儀表板](https://grafana.com/grafana/dashboards/12239-nvidia-dcgm-exporter-dashboard/)和 [EFA 指標儀表板](https://grafana.com/grafana/dashboards/20579-efa-metrics-dev/)，您需要安裝[適用於 Prometheus 的 Slurm 匯出工具](https://github.com/vpenso/prometheus-slurm-exporter)、[NVIDIA DCGM 匯出工具](https://github.com/NVIDIA/dcgm-exporter)和 [EFA 節點匯出工具](https://github.com/aws-samples/awsome-distributed-training/blob/main/4.validation_and_observability/3.efa-node-exporter/README.md)。如需在 Amazon Managed Grafana 工作區上安裝匯出工具套件和使用 Grafana 儀表板的詳細資訊，請參閱[SageMaker HyperPod 叢集資源監控](sagemaker-hyperpod-cluster-observability-slurm.md)。

         若要啟用此安裝步驟，請在 [https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/config.py](https://github.com/aws-samples/awsome-distributed-training/blob/main/1.architectures/5.sagemaker-hyperpod/LifecycleScripts/base-config/config.py) 檔案中將 `enable_observability` 參數設定為 `True`。

         ```
         # Install metric exporting software and Prometheus for observability
         
         if Config.enable_observability:
             if node_type == SlurmNodeType.COMPUTE_NODE:
                 ExecuteBashScript("./utils/install_docker.sh").run()
                 ExecuteBashScript("./utils/install_dcgm_exporter.sh").run()
                 ExecuteBashScript("./utils/install_efa_node_exporter.sh").run()
             
             if node_type == SlurmNodeType.HEAD_NODE:
                 wait_for_scontrol()
                 ExecuteBashScript("./utils/install_docker.sh").run()
                 ExecuteBashScript("./utils/install_slurm_exporter.sh").run()
                 ExecuteBashScript("./utils/install_prometheus.sh").run()
         ```

1. 確定您將所有組態檔案和設定指令碼從**步驟 2** 上傳至您在**步驟 1** 的 `CreateCluster` 請求中提供的 S3 儲存貯體。例如，假設您的 `create_cluster.json` 具有以下項目。

   ```
   "LifeCycleConfig": { 
   
       "SourceS3URI": "s3://sagemaker-hyperpod-lifecycle/src",
       "OnCreate": "on_create.sh"
   }
   ```

   然後，您的 `"s3://sagemaker-hyperpod-lifecycle/src"` 應該包含 `on_create.sh`、`provisioning_parameters.json`、`lifecycle_script.py` 和所有其他設定指令碼。假設您已準備本機資料夾中的檔案，如下所示。

   ```
   └── lifecycle_files // your local folder
       ├── provisioning_parameters.json
       ├── on_create.sh
       ├── lifecycle_script.py
       └── ... // more setup scrips to be fed into lifecycle_script.py
   ```

   若要上傳檔案，請使用 S3 命令，如下所示。

   ```
   aws s3 cp --recursive ./lifecycle_scripts s3://sagemaker-hyperpod-lifecycle/src
   ```