

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

# Amazon SageMaker AI 如何提供培训信息
<a name="your-algorithms-training-algo-running-container"></a>

本节介绍了 SageMaker AI 如何将训练信息（例如训练数据、超参数和其他配置信息）提供给 Docker 容器。

当您向 A SageMaker I 发送启动模型训练的[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html)请求时，您可以指定包含训练算法的 Docker 镜像的亚马逊弹性容器注册表 (Amazon ECR) Container Registry 路径。您还可以指定存储训练数据的亚马逊简单存储服务 (Amazon S3) 位置以及特定于算法的参数。 SageMaker AI 将这些信息提供给 Docker 容器，以便您的训练算法可以使用这些信息。本节介绍我们如何向您的 Docker 容器提供此信息。有关创建训练作业的信息，请参阅`CreateTrainingJob`。有关 SageMaker AI 容器组织信息的方式的更多信息，请参阅[SageMaker 训练和推理工具包](amazon-sagemaker-toolkits.md)。

**Topics**
+ [

## 超参数
](#your-algorithms-training-algo-running-container-hyperparameters)
+ [

## 环境变量
](#your-algorithms-training-algo-running-container-environment-variables)
+ [

## 输入数据配置
](#your-algorithms-training-algo-running-container-inputdataconfig)
+ [

## 训练数据
](#your-algorithms-training-algo-running-container-trainingdata)
+ [

## 分布式训练配置
](#your-algorithms-training-algo-running-container-dist-training)

## 超参数
<a name="your-algorithms-training-algo-running-container-hyperparameters"></a>

 SageMaker AI 使`CreateTrainingJob`请求中的超参数在文件的 Docker 容器中`/opt/ml/input/config/hyperparameters.json`可用。

以下是中用于在`CreateTrainingJob`操作中`hyperparameters.json`指定`num_round`和超参数的`eta`超参数配置示例。[XGBoost](https://docs.aws.amazon.com/sagemaker/latest/dg/xgboost.html)

```
{
    "num_round": "128",
    "eta": "0.001"
}
```

有关可用于 SageMaker AI 内置 XGBoost 算法的超参数的完整列表，请参阅[XGBoost超](https://docs.aws.amazon.com/sagemaker/latest/dg/xgboost_hyperparameters.html)参数。

您可以调整的超参数取决于所训练的算法。有关可用于 AI 内置算法的超参数列表，请在使用 A [ma SageMaker zon SageMaker AI 内置算法或预](https://docs.aws.amazon.com/sagemaker/latest/dg/algos.html)训练模型中的算法链接下的**超参数**中找到这些超参数。

## 环境变量
<a name="your-algorithms-training-algo-running-container-environment-variables"></a>

SageMaker AI 在您的容器中设置以下环境变量：
+ TRAINING\$1JOB\$1NAME – 在 `CreateTrainingJob` 请求中指定 `TrainingJobName` 参数。
+ TRAINING\$1JOB\$1ARN – 作为 `CreateTrainingJob` 响应中的 `TrainingJobArn` 返回的训练作业的 Amazon 资源名称 (ARN)。
+ 在 `CreateTrainingJob` 请求的 [Environment](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html#sagemaker-CreateTrainingJob-request-Environment) 参数中指定的所有环境变量。

## 输入数据配置
<a name="your-algorithms-training-algo-running-container-inputdataconfig"></a>

SageMaker AI 使`CreateTrainingJob`请求中`InputDataConfig`参数中的数据通道信息在 Docker 容器的`/opt/ml/input/config/inputdataconfig.json`文件中可用。

例如，假设您在请求中指定了三个数据通道（`train``evaluation`、和`validation`）。 SageMaker AI 提供了以下 JSON：

```
{
  "train" : {"ContentType":  "trainingContentType",
             "TrainingInputMode": "File",
             "S3DistributionType": "FullyReplicated",
             "RecordWrapperType": "None"},
  "evaluation" : {"ContentType":  "evalContentType",
                  "TrainingInputMode": "File",
                  "S3DistributionType": "FullyReplicated",
                  "RecordWrapperType": "None"},
  "validation" : {"TrainingInputMode": "File",
                  "S3DistributionType": "FullyReplicated",
                  "RecordWrapperType": "None"}
}
```

**注意**  
SageMaker AI 仅向容器提供有关每个数据通道的相关信息（例如频道名称和内容类型），如前面的示例所示。 `S3DistributionType`将设置为`FullyReplicated`将 EFS 或 FSx Lustre 指定为输入数据源。

## 训练数据
<a name="your-algorithms-training-algo-running-container-trainingdata"></a>

[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html) 请求中 `AlgorithmSpecification` 的参数 `TrainingInputMode` 指定了如何向容器提供训练数据集。有以下输入模式可用。
+ **`File` 模式**

  如果您使用`File`模式作为`TrainingInputMode`值， SageMaker AI 会在您的容器中设置以下参数。
  + 您的 `TrainingInputMode` 参数将写入到 `inputdataconfig.json` 中作为“File”。
  + 您的数据通道目录写入到 `/opt/ml/input/data/channel_name` 中。

  如果您使用`File`模式， SageMaker AI 会为每个频道创建一个目录。例如，如果你有三个名为、和`training``validation``testing`、的频道， SageMaker AI 会在你的 Docker 容器中创建以下三个目录：
  + `/opt/ml/input/data/training`
  + `/opt/ml/input/data/validation`
  + `/opt/ml/input/data/testing`

  `File` 还支持以下数据来源：
  + Amazon Simple Storage Service (Amazon S3)
  + Amazon Elastic File System (Amazon EFS)
  + 亚马逊 f FSx or Lustre
**注意**  
使用 Amazon EFS 和 Amazon 等文件系统数据源的渠道 FSx必须使用`File`模式。在这种情况下，通道中提供的目录路径挂载在 `/opt/ml/input/data/channel_name`。
+ **`FastFile` 模式**

  如果你使用`FastFile`模式作为你的`TrainingInputNodeParameter`， SageMaker AI 会在你的容器中设置以下参数。
  + 与 `File` 模式类似，在 `FastFile` 模式下，您的 `TrainingInputMode` 参数将写入到 `inputdataconfig.json` 中作为“File”。
  + 您的数据通道目录写入到 `/opt/ml/input/data/channel_name` 中。

  `FastFile` 模式支持以下数据来源：
  + Amazon S3

  如果您使用 `FastFile` 模式，则以只读权限挂载通道目录。

  过去，`File` 模式优先于 `FastFile` 模式。为了确保向后兼容性，只要在 `inputdataconfig.json.` 中将 `TrainingInputMode` 参数设置为 `File`，则支持 `File` 模式的算法也可以无缝地与 `FastFile` 模式配合使用。
**注意**  
使用 `FastFile` 模式的通道必须使用`S3DataType`“S3Prefix”。  
`FastFile` 模式显示了文件夹视图，使用正斜杠 (`/`) 作为将 Amazon S3 对象分组到文件夹中的分隔符。`S3Uri` 前缀不能对应于部分文件夹名称。例如，如果 Amazon S3 数据集包含 `s3://amzn-s3-demo-bucket/train-01/data.csv`，则 `s3://amzn-s3-demo-bucket/train` 或 `s3://amzn-s3-demo-bucket/train-01` 均不允许使用 `S3Uri` 前缀。  
建议使用结尾正斜杠来定义与文件夹对应的通道。例如，`train-01` 文件夹的 `s3://amzn-s3-demo-bucket/train-01/` 通道。如果没有结尾处的正斜杠，则如果存在其他文件夹 `s3://amzn-s3-demo-bucket/train-011/` 或文件 `s3://amzn-s3-demo-bucket/train-01.txt/`，就无法确定该通道。
+ **`Pipe` 模式**
  + `TrainingInputMode` 参数写入到 `inputdataconfig.json`：“Pipe”
  + Docker 容器中的数据通道目录：`/opt/ml/input/data/channel_name_epoch_number`
  + 支持的数据来源：Amazon S3

  您需要为每个通道从单独的管道中读取。例如，如果您有三个通道（分别名为 `training`、`validation` 和 `testing`），则需要从以下管道读取：
  + `/opt/ml/input/data/training_0, /opt/ml/input/data/training_1, ...`
  + `/opt/ml/input/data/validation_0, /opt/ml/input/data/validation_1, ...`
  + `/opt/ml/input/data/testing_0, /opt/ml/input/data/testing_1, ...`

  按顺序读取管道。例如，如果您有一个名为 `training` 的通道，请按以下顺序读取管道：

  1. `/opt/ml/input/data/training_0`在读取模式下打开并将其读入 end-of-file (EOF)，或者，如果您完成了第一个纪元，请尽早关闭管道文件。

  1. 关闭第一个管道文件后，请查找 `/opt/ml/input/data/training_1` 并读取它，直到您完成第二个纪元，以此类推。

  如果给定纪元的文件尚不存在，您的代码可能需要重试，直到创建该管道。各个通道类型没有顺序限制。例如，对于 `training` 通道，您可以读取多个纪元，并仅在准备好后才开始读取 `validation` 通道。或者，如果算法要求，您可以同时读取它们。

  有关展示自带容器时如何使用管道模式的 Jupyter 笔记本示例，请参阅将自己的管道模式[算法引入 Amazon AI](https://github.com/aws/amazon-sagemaker-examples/blob/main/advanced_functionality/pipe_bring_your_own/pipe_bring_your_own.ipynb)。 SageMaker 

  

SageMaker AI 模型训练支持高性能 S3 Express One Zone 目录存储桶作为文件模式、快速文件模式和管道模式的数据输入位置。要使用 S3 Express One Zone，请输入 S3 Express One Zone 目录存储桶的位置，而不是 Amazon S3 通用存储桶的位置。为具有所需访问控制和权限策略的 IAM 角色提供 ARN。有关详细信息，请参阅[AmazonSageMakerFullAccesspolicy](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AmazonSageMakerFullAccess.html)。您只能使用 Amazon S3 托管密钥 (SSE-S3) 通过服务器端加密对目录存储桶中的 A SageMaker I 输出数据进行加密。目前不支持使用 AWS KMS 密钥进行服务器端加密 (SSE-KMS)，以将 SageMaker AI 输出数据存储在目录存储桶中。有关更多信息，请参阅 [S3 Express One Zone](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-express-one-zone.html)。

## 分布式训练配置
<a name="your-algorithms-training-algo-running-container-dist-training"></a>

如果您使用多个容器执行分布式训练， SageMaker AI 会在`/opt/ml/input/config/resourceconfig.json`文件中提供有关所有容器的信息。

为了启用容器间通信，此 JSON 文件包含所有容器的信息。 SageMaker AI 使该文件可用于模式算法`File`和`Pipe`模式算法。该文件提供以下信息：
+ `current_host` – 容器网络中的当前容器的名称。例如 `algo-1`。您可以随时更改主机值。不要使用此变量的特定值编写代码。
+ `hosts` – 容器网络中的所有容器的名称列表，按字典顺序排列。例如，`["algo-1", "algo-2", "algo-3"]` 用于三节点集群。容器可以使用这些名称来查找容器网络上的其他容器。您可以随时更改主机值。不要使用这些变量的特定值编写代码。
+ `network_interface_name` – 对您的容器公开的网络接口的名称。例如，运行消息传递接口 (MPI) 的容器可以使用该信息设置网络接口名称。
+ 请勿使用 `/etc/hostname` 或 `/etc/hosts` 中的信息，因为可能不准确。
+ 算法容器可能无法立即获得主机名信息。我们建议当节点在集群中可用时，在主机名解析操作上添加重试策略。

下面是三节点集群的节点 1 上的示例文件：

```
{
    "current_host": "algo-1",
    "hosts": ["algo-1","algo-2","algo-3"],
    "network_interface_name":"eth1"
}
```