

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

# HyperPod Slurm 集群预训练教程 (GPU)
<a name="hyperpod-gpu-slurm-pretrain-tutorial"></a>

按照以下教程操作，设置 Slurm 环境并在 Llama 80 亿参数模型上开始训练作业。

**先决条件**  
在开始设置环境以运行配方之前，请确保您：  
设置 HyperPod GPU Slurm 集群。  
你的 HyperPod Slurm 集群必须启用 Nvidia Enroot 和 Pyxis（默认情况下它们处于启用状态）。
拥有一个共享存储位置。它可以是 Amazon FSx 文件系统或可从群集节点访问的 NFS 系统。
拥有采用以下格式之一的数据：  
JSON
JSONGZ（压缩 JSON）
ARROW
（可选）如果您使用中的模型权重进行预训练或微调，则必须获得 HuggingFace 代币。 HuggingFace 有关获取令牌的更多信息，请参阅[用户访问令牌](https://huggingface.co/docs/hub/en/security-tokens)。

## HyperPod GPU Slurm 环境设置
<a name="hyperpod-gpu-slurm-environment-setup"></a>

要在 HyperPod GPU Slurm 集群上启动训练作业，请执行以下操作：

1. 通过 SSH 连接到 Slurm 集群的头节点。

1. 登录后，设置虚拟环境。确保您使用 Python 3.9 或更高版本。

   ```
   #set up a virtual environment
   python3 -m venv ${PWD}/venv
   source venv/bin/activate
   ```

1. 将 SageMaker HyperPod 配方和 SageMaker HyperPod 适配器存储库克隆到共享存储位置。

   ```
   git clone https://github.com/aws/sagemaker-hyperpod-training-adapter-for-nemo.git
   git clone --recursive https://github.com/aws/sagemaker-hyperpod-recipes.git
   cd sagemaker-hyperpod-recipes
   pip3 install -r requirements.txt
   ```

1. 使用 Enroot 创建一个 squash 文件。要查找最新版本的 SMP 容器，请参阅 [SageMaker 模型并行度库的发行说明](model-parallel-release-notes.md)。要更深入地了解如何使用 Enroot 文件，请参阅[构建 Amazon经过优化的 Nemo-](https://github.com/aws-samples/awsome-distributed-training/tree/main/3.test_cases/2.nemo-launcher#2-build-aws-optimized-nemo-launcher-image) Launcher 镜像。

   ```
   REGION="<region>"
   IMAGE="658645717510.dkr.ecr.${REGION}.amazonaws.com/smdistributed-modelparallel:2.4.1-gpu-py311-cu121"
   aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin 658645717510.dkr.ecr.${REGION}.amazonaws.com
   enroot import -o $PWD/smdistributed-modelparallel.sqsh dockerd://${IMAGE}
   mv $PWD/smdistributed-modelparallel.sqsh "/fsx/<any-path-in-the-shared-filesystem>"
   ```

1. 要使用 Enroot squash 文件开始训练，请使用以下示例来修改 `recipes_collection/config.yaml` 文件。

   ```
   container: /fsx/path/to/your/smdistributed-modelparallel.sqsh
   ```

## 启动训练作业
<a name="hyperpod-gpu-slurm-launch-training-job"></a>

安装依赖项后，从 `sagemaker-hyperpod-recipes/launcher_scripts` 目录中启动训练作业。你可以通过克隆[SageMaker HyperPod 配方存储库](https://github.com/aws/sagemaker-hyperpod-recipes)来获得依赖关系：

首先，从 Github 中选择训练配方，模型名称将被指定为配方的一部分。在以下示例中，我们使用 `launcher_scripts/llama/run_hf_llama3_8b_seq16k_gpu_p5x16_pretrain.sh` 脚本启动 Llama 8b，带序列长度 8192 的预训练配方 `llama/hf_llama3_8b_seq16k_gpu_p5x16_pretrain`。
+ `IMAGE`：环境设置部分中的容器。
+ （可选）如果您需要预先训练的权重，则 HuggingFace 可以通过设置以下键值对来提供 HuggingFace 标记：

  ```
  recipes.model.hf_access_token=<your_hf_token>
  ```

```
#!/bin/bash
IMAGE="${YOUR_IMAGE}"
SAGEMAKER_TRAINING_LAUNCHER_DIR="${SAGEMAKER_TRAINING_LAUNCHER_DIR:-${PWD}}"

TRAIN_DIR="${YOUR_TRAIN_DIR}" # Location of training dataset
VAL_DIR="${YOUR_VAL_DIR}" # Location of validation dataset

# experiment ouput directory
EXP_DIR="${YOUR_EXP_DIR}"

HYDRA_FULL_ERROR=1 python3 "${SAGEMAKER_TRAINING_LAUNCHER_DIR}/main.py" \
  recipes=training/llama/hf_llama3_8b_seq16k_gpu_p5x16_pretrain \
  base_results_dir="${SAGEMAKER_TRAINING_LAUNCHER_DIR}/results" \
  recipes.run.name="hf_llama3_8b" \
  recipes.exp_manager.exp_dir="$EXP_DIR" \
  recipes.model.data.train_dir="$TRAIN_DIR" \
  recipes.model.data.val_dir="$VAL_DIR" \
  container="${IMAGE}" \
  +cluster.container_mounts.0="/fsx:/fsx"
```

在启动程序脚本中配置所有必需参数后，可使用以下命令运行脚本。

```
bash launcher_scripts/llama/run_hf_llama3_8b_seq16k_gpu_p5x16_pretrain.sh
```

有关 Slurm 集群配置的更多信息，请参阅[在 HyperPod Slurm 上运行训练作业](cluster-specific-configurations-run-training-job-hyperpod-slurm.md)。