

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

# SageMaker 训练作业预训练教程 (GPU)
<a name="sagemaker-hyperpod-gpu-sagemaker-training-jobs-pretrain-tutorial"></a>

本教程将指导您完成使用带有 GPU 实例的训练作业设置和运行预 SageMaker 训练作业的过程。
+ 设置环境
+ 使用 SageMaker HyperPod 食谱启动训练作业

在开始之前，确保您满足以下先决条件。

**先决条件**  
在开始设置环境之前，请确保您：  
Amazon FSx 文件系统或 Amazon S3 存储桶，您可以在其中加载数据和输出训练项目。
在亚马逊 AI 上申请 1x ml.p4d.24xlarge 和 1x ml.p5.48xlarge 的服务配额。 SageMaker 要请求增加服务配额，请执行以下操作：  
在 S AWS ervice Quotas 控制台上，导航到 AWS 服务，
选择 **Amazon SageMaker AI**。
选择一个 ml.p4d.24xlarge 和一个 ml.p5.48xlarge 实例。
使用以下托管策略创建 AWS Identity and Access Management(IAM) 角色，以授予 SageMaker AI 运行示例的权限。  
AmazonSageMakerFullAccess
Amazon EC2 FullAccess
拥有采用以下格式之一的数据：  
JSON
JSONGZ（压缩 JSON）
ARROW
（可选）如果您使用中的模型权重进行预训练或微调，则必须获得 HuggingFace 代币。 HuggingFace 有关获取令牌的更多信息，请参阅[用户访问令牌](https://huggingface.co/docs/hub/en/security-tokens)。

## GPU SageMaker 训练作业环境设置
<a name="sagemaker-hyperpod-gpu-sagemaker-training-jobs-environment-setup"></a>

在运行 SageMaker 训练作业之前，请运行`aws configure`命令配置您的 AWS 凭证和首选区域。作为配置命令的替代方案，您可以通过环境变量（例如、）提供凭证。有关更多信息 `AWS_ACCESS_KEY_ID``AWS_SECRET_ACCESS_KEY`，请参阅 [SageMaker AI Python SDK](https://github.com/aws/sagemaker-python-sdk)。`AWS_SESSION_TOKEN.`

我们强烈建议在 AI 中 SageMaker 使用 A SageMaker I Jupyter 笔记本 JupyterLab 来启动 SageMaker 训练作业。有关更多信息，请参阅 [SageMaker JupyterLab](studio-updated-jl.md)。
+ （可选）设置虚拟环境和依赖项。如果您在 Amazon SageMaker Studio 中使用 Jupyter 笔记本电脑，则可以跳过此步骤。确保您使用 Python 3.9 或更高版本。

  ```
  # set up a virtual environment
  python3 -m venv ${PWD}/venv
  source venv/bin/activate
  # install dependencies after git clone.
  
  git clone --recursive git@github.com:aws/sagemaker-hyperpod-recipes.git
  cd sagemaker-hyperpod-recipes
  pip3 install -r requirements.txt
  # Set the aws region.
  
  aws configure set <your_region>
  ```
+ 安装 SageMaker AI Python SDK

  ```
  pip3 install --upgrade sagemaker
  ```
+ `Container`：GPU 容器由 SageMaker AI Python SDK 自动设置。您也可以提供自己的容器。
**注意**  
如果您正在运行 Llama 3.2 多模态训练作业，则 `transformers` 版本必须为 `4.45.2 ` 版或更高版本。

  `source_dir`仅当你使用 SageMaker AI Python SDK 时才会追加`transformers==4.45.2`到`requirements.txt`中。例如，如果您在 SageMaker AI JupyterLab 的笔记本中使用它，请将其追加。

  如果您使用 HyperPod 配方使用集群类型启动`sm_jobs`，则此操作将自动完成。

## 使用 Jupyter Notebook 启动训练作业
<a name="sagemaker-hyperpod-gpu-sagemaker-training-jobs-launch-training-job-notebook"></a>

您可以使用以下 Python 代码使用您的配方运行 SageMaker 训练作业。它利用 AI [SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable/) 中的 PyTorch 估算器来提交配方。以下示例在 AI 训练平台上启动 llama3-8b 配方。 SageMaker 

```
import os
import sagemaker,boto3
from sagemaker.debugger import TensorBoardOutputConfig

from sagemaker.pytorch import PyTorch

sagemaker_session = sagemaker.Session()
role = sagemaker.get_execution_role()

bucket = sagemaker_session.default_bucket() 
output = os.path.join(f"s3://{bucket}", "output")
output_path = "<s3-URI>"

overrides = {
    "run": {
        "results_dir": "/opt/ml/model",
    },
    "exp_manager": {
        "exp_dir": "",
        "explicit_log_dir": "/opt/ml/output/tensorboard",
        "checkpoint_dir": "/opt/ml/checkpoints",
    },   
    "model": {
        "data": {
            "train_dir": "/opt/ml/input/data/train",
            "val_dir": "/opt/ml/input/data/val",
        },
    },
}

tensorboard_output_config = TensorBoardOutputConfig(
    s3_output_path=os.path.join(output, 'tensorboard'),
    container_local_output_path=overrides["exp_manager"]["explicit_log_dir"]
)

estimator = PyTorch(
    output_path=output_path,
    base_job_name=f"llama-recipe",
    role=role,
    instance_type="ml.p5.48xlarge",
    training_recipe="training/llama/hf_llama3_8b_seq8k_gpu_p5x16_pretrain",
    recipe_overrides=recipe_overrides,
    sagemaker_session=sagemaker_session,
    tensorboard_output_config=tensorboard_output_config,
)

estimator.fit(inputs={"train": "s3 or fsx input", "val": "s3 or fsx input"}, wait=True)
```

前面的代码使用训练配方创建一个 PyTorch 估计器对象，然后使用该`fit()`方法拟合模型。使用 training\$1recipe 参数指定要用于训练的配方。

**注意**  
如果您正在运行 Llama 3.2 多模态训练作业，则转换器版本必须为 4.45.2 版或更高版本。

`source_dir`只有当你直接使用 SageMaker AI Python SDK 时，才会追加`transformers==4.45.2`到`requirements.txt`中。例如，在使用 Jupyter Notebook 时，必须将版本追加到文本文件。

在为 SageMaker 训练作业部署终端节点时，必须指定正在使用的图像 URI。如果不提供映像 URI，则估算器将使用训练映像作为部署的映像。 SageMaker HyperPod 提供的训练图像不包含推理和部署所需的依赖关系。以下示例说明如何将推理映像用于部署：

```
from sagemaker import image_uris
container=image_uris.retrieve(framework='pytorch',region='us-west-2',version='2.0',py_version='py310',image_scope='inference', instance_type='ml.p4d.24xlarge')
predictor = estimator.deploy(initial_instance_count=1,instance_type='ml.p4d.24xlarge',image_uri=container)
```

**注意**  
在 Sagemaker 笔记本实例上运行上述代码可能需要超过 AI 提供的默认 5GB 存储空间。 SageMaker JupyterLab 如果您遇到空间不足的问题，请创建一个新的笔记本实例，您可在该实例中使用其他笔记本实例，并增加笔记本的存储空间。

## 使用配方启动程序启动训练作业
<a name="sagemaker-hyperpod-gpu-sagemaker-training-jobs-launch-training-job-recipes"></a>

更新 `./recipes_collection/cluster/sm_jobs.yaml` 文件，使其看起来与以下内容类似：

```
sm_jobs_config:
  output_path: <s3_output_path>
  tensorboard_config:
    output_path: <s3_output_path>
    container_logs_path: /opt/ml/output/tensorboard  # Path to logs on the container
  wait: True  # Whether to wait for training job to finish
  inputs:  # Inputs to call fit with. Set either s3 or file_system, not both.
    s3:  # Dictionary of channel names and s3 URIs. For GPUs, use channels for train and validation.
      train: <s3_train_data_path>
      val: null
  additional_estimator_kwargs:  # All other additional args to pass to estimator. Must be int, float or string.
    max_run: 180000
    enable_remote_debug: True
  recipe_overrides:
    exp_manager:
      explicit_log_dir: /opt/ml/output/tensorboard
    data:
      train_dir: /opt/ml/input/data/train
    model:
      model_config: /opt/ml/input/data/train/config.json
    compiler_cache_url: "<compiler_cache_url>"
```

更新 `./recipes_collection/config.yaml` 以在 `cluster` 和 `cluster_type` 中指定 `sm_jobs`。

```
defaults:
  - _self_
  - cluster: sm_jobs  # set to `slurm`, `k8s` or `sm_jobs`, depending on the desired cluster
  - recipes: training/llama/hf_llama3_8b_seq8k_trn1x4_pretrain
cluster_type: sm_jobs  # bcm, bcp, k8s or sm_jobs. If bcm, k8s or sm_jobs, it must match - cluster above.
```

通过以下命令启动作业

```
python3 main.py --config-path recipes_collection --config-name config
```

有关配置 SageMaker 训练作业的更多信息，请参阅在训练作业上运行 SageMaker 训练作业。