

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

# 专家并行性
<a name="model-parallel-core-features-v2-expert-parallelism"></a>

*混合专家* (MoE) 模型是一种采用*稀疏*方法的转换器模型，与训练传统的密集模型相比，训练起来更轻松。在此 MoE 神经网络架构中，每个输入只使用模型中称为*专家*的组件子集。这种方法具有多种优点，包括更高效的训练和更快的推理，即使模型规模更大也是如此。换句话说，使用相同的计算预算来训练全密集模型，使用 MoE 可以拟合出更大的模型或数据集。

MoE 模型由多个*专家*组成，每个专家都由一个神经网络组成，通常是一个前馈网络 (FFN)。一个称为*路由器*的网关网络决定将哪些令牌发送给哪些专家。这些专家专门处理输入数据的特定方面，使模型的训练速度更快，计算成本更低，同时达到与其对应的密集模型相同的性能质量。要了解有关混合专家的更多信息，请参阅博客在 [NVIDIA 开发者网站](https://developer.nvidia.com/blog/applying-mixture-of-experts-in-llm-architectures/)上*应用 LLM 架构专家组合*。

*专家并行性*是一种用于处理在 GPU 设备上拆分 MoE 模型的专家并行性。

SMP v2 与 [NVIDIA 威震天](https://github.com/NVIDIA/Megatron-LM)集成，可实现专家并行性以支持训练 MoE 模型，并在 FSDP 的基础上运行。 PyTorch APIs您可以继续按原样使用 PyTorch FSDP 训练代码，并激活 SMP 专家并行度来训练 MoE 模型。

## Hugging Face 转换器模型兼容 SMP 专家并行性
<a name="model-parallel-core-features-v2-expert-parallelism-supported-models"></a>

SMP v2 目前可为以下 Hugging Face 转换器模型提供专家并行性支持。
+ [Mixtral](https://huggingface.co/docs/transformers/en/model_doc/mixtral)

## 配置专家并行性
<a name="model-parallel-core-features-v2-expert-parallelism-configure"></a>

对于 `expert_parallel_degree`，您可以为专家并行性选择一个值。该值必须平均除以集群 GPUs 中的数量。例如，要在使用带有 8 的实例时对模型进行分片 GPUs，请选择 2、4 或 8。我们建议您从一个较小的数字开始，然后逐渐增加，直到模型适合 GPU 内存。

以下代码片段显示了如何在训练脚本中添加 SMP 初始化模块 `torch.sagemaker.init()`，并按照 [使用 SageMaker 模型并行度库 v2](model-parallel-use-api-v2.md) 中介绍的两步流程，为训练作业启动器设置 JSON 格式的 SMP 配置字典。您无需对 PyTorch 模型或 [PyTorch FSDP](https://pytorch.org/docs/stable/fsdp.html#module-torch.distributed.fsdp) 配置进行任何更改。有关 `expert_parallel_degree` 参数的更多信息，请参阅 [SMP v2 核心功能配置参数](distributed-model-parallel-v2-reference.md#distributed-model-parallel-v2-reference-init-config)。

**注意**  
您可以在 [混合分片数据并行性](model-parallel-core-features-v2-sharded-data-parallelism.md) 中使用专家并行性。请注意，专家并行性目前与张量并行不兼容。

**注意**  
此专家并行度训练功能可在以下库 SageMaker 和库组合中使用： PyTorch   
SMP v2.3.0 及更高版本
 SageMaker Python SDK v2.214.4 及更高版本
PyTorch v2.2.0 及更高版本

### 在您的训练脚本中
<a name="model-parallel-core-features-v2-expert-parallelism-configure-in-script"></a>

作为[步骤 1](model-parallel-use-api-v2.md#model-parallel-adapt-pytorch-script-v2) 的一部分，使用 `torch.sagemaker.init()` 初始化脚本以激活 SMP v2，然后使用 [`torch.sagemaker.transform`](distributed-model-parallel-v2-reference.md#model-parallel-v2-torch-sagemaker-reference-transform) API 封装模型，在 API 中添加 `config` 参数以激活 MoE。下面的代码片段显示了如何激活通用模型类 `AutoModelForCausalLM` 的 SMP MoE 使用 `from_config` 方法从头开始训练，或使用 `from_pretrained` 方法进行微调，来拉取 MoE 转换器模型配置。要了解有关 SMP `MoEConfig` 类的更多信息，请参阅 [`torch.sagemaker.moe.moe_config.MoEConfig`](distributed-model-parallel-v2-reference.md#model-parallel-v2-torch-sagemaker-reference-moe)。

```
# Import the torch.sagemaker.transform API and initialize.
import torch.sagemaker as tsm
tsm.init()

# Import transformers AutoModelForCausalLM class.
from transformers import AutoModelForCausalLM

# Import the SMP-implementation of MoE configuration class.
from torch.sagemaker.moe.moe_config import MoEConfig

# Define a transformer model with an MoE model configuration
model = AutoModelForCausalLM.from_config(MoEModelConfig)

# Wrap it by torch.sagemaker.transform with the SMP MoE configuration.
model = tsm.transform(
    model, 
    config=MoEConfig(
        smp_moe=True,
        random_seed=12345,
        moe_load_balancing="sinkhorn",
        global_token_shuffle=False,
        moe_all_to_all_dispatcher=True,
        moe_aux_loss_coeff=0.001,
        moe_z_loss_coeff=0.001
    )
)
```

### SMP 配置
<a name="model-parallel-core-features-v2-expert-parallelism-configure-in-estimator-config"></a>

作为[步骤 2](model-parallel-use-api-v2.md#model-parallel-launch-a-training-job-v2) 的一部分，将以下参数添加到 SageMaker PyTorch 估算器的 SMP 配置字典中。

```
{   
    ..., # other SMP config parameters
    "expert_parallel_degree": 8
}
```