

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

# Trainium Slurm 叢集預先訓練教學課程
<a name="hyperpod-trainium-slurm-cluster-pretrain-tutorial"></a>

下列教學課程會在 Slurm 叢集上設定 Trainium 環境，並在 Llama 80 億參數模型上啟動訓練任務。

**先決條件**  
開始設定環境之前，請確定您具有下列先決條件：  
設定 SageMaker HyperPod Trainium Slurm 叢集。
共用儲存位置。它可以是可從叢集節點存取的 Amazon FSx 檔案系統或 NFS 系統。
採用下列其中一種格式的資料：  
JSON
JSONGZ (壓縮 JSON)
ARROW
(選用) 如果您要使用來自 HuggingFace 的模型權重進行預先訓練或微調，則必須取得 HuggingFace 權杖。如需取得權杖的詳細資訊，請參閱[使用者存取權杖](https://huggingface.co/docs/hub/en/security-tokens)。

## 在 Slurm 叢集上設定 Trainium 環境
<a name="hyperpod-trainium-slurm-cluster-pretrain-setup-trainium-environment"></a>

若要在 Slurm 叢集上啟動訓練任務，請執行下列動作：
+ 對 Slurm 叢集的主節點執行 SSH。
+ 登入後，請設定 Neuron 環境。如需設定 Neuron 的相關資訊，請參閱 [Neuron 設定步驟](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/libraries/nxd-training/tutorials/hf_llama3_8B_SFT.html#setting-up-the-environment)。我們建議依賴預先安裝 Neuron 驅動程式的深度學習 AMI，例如 [Ubuntu 20 搭配 DLAMI Pytorch](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/general/setup/neuron-setup/pytorch/neuronx/ubuntu/torch-neuronx-ubuntu20-pytorch-dlami.html#setup-torch-neuronx-ubuntu20-dlami-pytorch)。
+ 將 SageMaker HyperPod 配方儲存庫複製到叢集中的共用儲存位置。共用儲存位置可以是可從叢集節點存取的 Amazon FSx 檔案系統或 NFS 系統。

  ```
  git clone --recursive https://github.com/aws/sagemaker-hyperpod-recipes.git
  cd sagemaker-hyperpod-recipes
  pip3 install -r requirements.txt
  ```
+ 完成下列教學課程：[HuggingFace Llama3-8B 預先訓練](https://awsdocs-neuron.readthedocs-hosted.com/en/latest/libraries/nxd-training/tutorials/hf_llama3_8B_pretraining.html#)
+ 準備模型組態。Neuron 儲存庫中可用的模型組態。如需本教學課程中使用的模型組態，請參閱 [llama3 8b 模型組態](https://github.com/aws-neuron/neuronx-distributed/blob/main/examples/training/llama/tp_zero1_llama_hf_pretrain/8B_config_llama3/config.json)

## 在 Trainium 中啟動訓練任務
<a name="hyperpod-trainium-slurm-cluster-pretrain-launch-training-job-trainium"></a>

若要在 Trainium 中啟動訓練任務，請指定叢集組態和 Neuron 配方。例如，若要在 Trainium 中啟動 llama3 8b 預先訓練任務，請將啟動指令碼 `launcher_scripts/llama/run_hf_llama3_8b_seq8k_trn1x4_pretrain.sh` 設定為下列項目：
+ `MODEL_CONFIG`：來自環境設定區段的模型組態
+ (選用) 如果您需要來自 HuggingFace 的預先訓練權重，您可以設定下列金鑰/值對，以提供 HuggingFace 權杖：

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

```
#!/bin/bash

#Users should set up their cluster type in /recipes_collection/config.yaml

SAGEMAKER_TRAINING_LAUNCHER_DIR=${SAGEMAKER_TRAINING_LAUNCHER_DIR:-"$(pwd)"}

COMPILE=0
TRAIN_DIR="${TRAIN_DIR}" # Location of training dataset
MODEL_CONFIG="${MODEL_CONFIG}" # Location of config.json for the model

HYDRA_FULL_ERROR=1 python3 "${SAGEMAKER_TRAINING_LAUNCHER_DIR}/main.py" \
    base_results_dir="${SAGEMAKER_TRAINING_LAUNCHER_DIR}/results" \
    instance_type="trn1.32xlarge" \
    recipes.run.compile="$COMPILE" \
    recipes.run.name="hf-llama3-8b" \
    recipes.trainer.num_nodes=4 \
    recipes=training/llama/hf_llama3_8b_seq8k_trn1x4_pretrain \
    recipes.data.train_dir="$TRAIN_DIR" \
    recipes.model.model_config="$MODEL_CONFIG"
```

若要啟動訓練任務，請執行下列命令：

```
bash launcher_scripts/llama/run_hf_llama3_8b_seq8k_trn1x4_pretrain.sh
```

如需 Slurm 叢集組態的詳細資訊，請參閱 [在 HyperPod Slurm 上執行訓練任務](cluster-specific-configurations-run-training-job-hyperpod-slurm.md)。