

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

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

本节将引导你了解以自上而***下的***方法开启 Slurm 的基本流程 HyperPod 的每个组成部分。它从准备用于运行 `CreateCluster` API 的 HyperPod 集群创建请求开始，然后深入研究生命周期脚本的层次结构。使用 [Awsome 分布式训练 GitHub 存储库](https://github.com/aws-samples/awsome-distributed-training/)中提供的示例生命周期脚本。运行以下命令克隆版本存储库。

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

用于在上设置 Slurm 集群的基本生命周期脚本 SageMaker HyperPod 可在中找到。[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)

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

下面的流程图详细概述了如何设计基本生命周期脚本。图表下方的描述和程序指南解释了它们在 HyperPod `CreateCluster` API 调用期间的工作原理。

![\[HyperPod 集群创建和生命周期脚本结构的详细流程图。\]](http://docs.aws.amazon.com/zh_cn/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`提交请求后，根据请求和生命周期脚本中的给定配置信息 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 群配置实例组。最佳做法是添加与您计划在集群上配置的 Slurm 节点数量相同的实例组。 HyperPod 确保为将分配给计划建立的 Slurm 节点的实例组赋予独特的名称。

   此外，您还需要在 `CreateCluster` 请求表中的字段名 `InstanceGroups.LifeCycleConfig.SourceS3Uri` 中指定一个 S3 存储桶路径，用于存储整套配置文件和生命周期脚本，并在 `InstanceGroups.LifeCycleConfig.OnCreate` 中指定入口点 shell 脚本的文件名（假设其名为 `on_create.sh`）。
**注意**  
如果您在 HyperPod 控制台用户界面中使用**创建集群**提交表单，则控制台会代表您管理填写和提交`CreateCluster`请求，并在后端运行 `CreateCluster` API。在这种情况下，您无需创建 `create_cluster.json`；相反，请确保在**创建集群**提交表中指定正确的集群配置信息。

1. `on_create.sh`— 对于每个实例组，您需要提供一个入口点 shell 脚本，以运行命令`on_create.sh`、运行脚本来安装软件包，以及使用 Slurm 设置 HyperPod集群环境。您需要准备的两件事是设置 Slurm 所`provisioning_parameters.json` HyperPod 必需的，以及一组用于安装软件包的生命周期脚本。编写此脚本时，应按照 [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`：这是 [用于配置\$1parameters.json 的配置表单](sagemaker-hyperpod-ref.md#sagemaker-hyperpod-ref-provisioning-forms-slurm)。`on_create.sh` 脚本会找到此 JSON 文件，并定义环境变量以确定文件路径。通过这个 JSON 文件，你可以配置 Slurm 节点和要与之通信的存储选项，例如 Ama FSx zon for Lustre for Slurm。在中`provisioning_parameters.json`，请确保根据计划设置方式，使用您在中`create_cluster.json`指定的名称将 HyperPod 集群实例组适当地分配给 Slurm 节点。

      下图显示了如何编写两个 JSON 配置文件`create_cluster.json`和`provisioning_parameters.json`以将 HyperPod 实例组分配给 Slurm 节点的示例。在本例中，我们假设设置了三个 Slurm 节点：控制器（管理）节点、登录节点（可选）和计算（Worker）节点。
**提示**  
为了帮助您验证这两个 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)。要了解更多信息，请参阅[在上创建 Slurm 集群之前验证 JSON 配置文件 HyperPod](sagemaker-hyperpod-lifecycle-best-practices-slurm-slurm-validate-json-files.md)。  
![\[在 .json 文件之间进行直接比较。\]](http://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/images/hyperpod-lifecycle-slurm-config.png)

      ***图：** HyperPod集群创建和 Slurm 配置之间的`create_cluster.json`直接比较。`provisiong_params.json``create_cluster.json` 中的实例组数量应与要配置为 Slurm 节点的节点数量一致。对于图中的示例，将在由三个实例组组组成的 HyperPod 集群上配置三个 Slurm 节点。您应通过相应地指定实例组名称将 HyperPod 集群实例组分配给 Slurm 节点。*

   1. `resource_config.json`— 在创建集群期间，编写`lifecycle_script.py`脚本是为了期望有来自的`resource_config.json`文件 HyperPod。该文件包含有关集群的信息，如实例类型和 IP 地址。

      运行 `CreateCluster` API 时， HyperPod会`/opt/ml/config/resource_config.json`根据该文件在上创建资源配置`create_cluster.json`文件。文件路径会保存到名为 `SAGEMAKER_RESOURCE_CONFIG_PATH` 的环境变量中。
**重要**  
该`resource_config.json`文件由 HyperPod 平台自动生成，您无需创建。下面的代码展示了一个 `resource_config.json` 示例，该示例将根据上一步中的 `create_cluster.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 的生命周期脚本。该脚本从 `on_create.sh` 中指定或标识的路径读入 `provisioning_parameters.json` 和 `resource_config.json`，将相关信息传递给每个生命周期脚本，然后按顺序运行生命周期脚本。

      生命周期脚本是一组脚本，您可以完全灵活地自定义这些脚本，以便在集群创建过程中安装软件包和设置必要或自定义配置，例如设置 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 deamons () [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)、安装 Ama FSx zon 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 会计 () 和 RDS 会计 ([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))。[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 Distributed Training 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 群与[适用于 Prometheus 的亚马逊托管服务](https://docs.aws.amazon.com/prometheus/latest/userguide/what-is-Amazon-Managed-Service-Prometheus.html)和 Amazon Managed Grafana 集成，将有关 HyperPod集群和集群节点的指标导出到[亚马逊托管](https://docs.aws.amazon.com/grafana/latest/userguide/what-is-Amazon-Managed-Service-Grafana.html) Grafana 控制面板。要在 Amazon Managed Grafana 上导出指标并使用 [Slurm 控制面板](https://grafana.com/grafana/dashboards/4323-slurm-dashboard/)、[NVIDIA DCGM 导出器控制面板](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`、`lifecycle_script.py`、`provisioning_parameters.json` 和所有其他设置脚本。假设您在本地文件夹中准备了如下文件。

   ```
   └── 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
   ```