

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

# 支持 Hugging Face 转换器模型
<a name="model-parallel-extended-features-pytorch-hugging-face"></a>

 SageMaker 模型并行度库的张量并行度为以下 Hugging Face Transformer 模型提供 out-of-the-box支持：
+ GPT-2、BERT 和 RoBERTa （在 SageMaker 模型并行度库 1.7.0 及更高版本中可用）
+ GPT-J（在 SageMaker 模型并行度库 1.8.0 及更高版本中可用）
+ GPT-neo（在 SageMaker 模型并行度库 v1.10.0 及更高版本中可用）

**注意**  
对于任何其他转换器模型，您需要使用 [smdistributed.modelparallel.torch.tp\$1register\$1with\$1module()](https://sagemaker.readthedocs.io/en/v2.199.0/api/training/smp_versions/latest/smd_model_parallel_pytorch_tensor_parallel.html#smdistributed.modelparallel.torch.tp_register_with_module) API 来应用张量并行性。

**注意**  
要使用张量并行度来训练 Hugging Face Transformer 模型，请务必使用 SageMaker具有模型并行度库 v1.7.0 及更高版本的 Hugging Face Deep Lear PyTorch ning Containers。有关更多信息，请参阅[SageMaker 模型并行度库发行说明](https://sagemaker.readthedocs.io/en/v2.199.0/api/training/smd_model_parallel_release_notes/smd_model_parallel_change_log.html)。

## 现成支持的模型
<a name="model-parallel-extended-features-pytorch-hugging-face-out-of-the-box"></a>

对于开箱即用库支持的 Hugging Face 变压器模型，您无需手动实现挂钩即可将 Transformer `smdistributed` 转换为 APIs 变压器层。[你可以使用上下文管理器 smdistributed.modelparallel.torch.tensor\$1parallelism () 激活张量并行性，然后用 [smdistributed.modelparallel.torch 包装模型。](https://sagemaker.readthedocs.io/en/v2.199.0/api/training/smp_versions/latest/smd_model_parallel_pytorch_tensor_parallel.html#smdistributed.modelparallel.torch.tensor_parallelism) DistributedModel()](https://sagemaker.readthedocs.io/en/v2.199.0/api/training/smp_versions/latest/smd_model_parallel_pytorch.html#smdistributed.modelparallel.torch.DistributedModel)。您无需使用 `smp.tp_register` API 手动注册钩子用于张量并行性。

Hugging Face Transformers 与 `smdistributed.modelparallel` 之间的 `state_dict` 转换函数可以如下所示访问。
+  `smdistributed.modelparallel.torch.nn.huggingface.gpt2.translate_state_dict_to_hf_gpt2(state_dict, max_seq_len=None)`
+  `smdistributed.modelparallel.torch.nn.huggingface.gpt2.translate_hf_state_dict_to_smdistributed_gpt2(state_dict)` 
+  `smdistributed.modelparallel.torch.nn.huggingface.bert.translate_state_dict_to_hf_bert(state_dict, max_seq_len=None)` 
+  `smdistributed.modelparallel.torch.nn.huggingface.bert.translate_hf_state_dict_to_smdistributed_bert(state_dict)` 
+  `smdistributed.modelparallel.torch.nn.huggingface.roberta.translate_state_dict_to_hf_roberta(state_dict, max_seq_len=None)` 
+  `smdistributed.modelparallel.torch.nn.huggingface.roberta.translate_hf_state_dict_to_smdistributed_roberta(state_dict)` 
+ `smdistributed.modelparallel.torch.nn.huggingface.gptj.translate_state_dict_to_hf_gptj(state_dict, max_seq_len=None)`（在 SageMaker 模型并行度库 v1.8.0 及更高版本中可用）
+ `smdistributed.modelparallel.torch.nn.huggingface.gptj.translate_hf_gptj_state_dict_to_smdistributed_gptj`（在 SageMaker 模型并行度库 v1.8.0 及更高版本中可用）
+ `smdistributed.modelparallel.torch.nn.huggingface.gptneo.translate_state_dict_to_hf_gptneo(state_dict, max_seq_len=None)`（在 SageMaker 模型并行度库 v1.10.0 及更高版本中可用）
+ `smdistributed.modelparallel.torch.nn.huggingface.gptneo.translate_hf_state_dict_to_smdistributed_gptneo(state_dict)`（在 SageMaker 模型并行度库 v1.10.0 及更高版本中可用）

**GPT-2 转换函数的使用示例**

首先包装模型，如以下代码所示。

```
from transformers import AutoModelForCausalLM

with smp.tensor_parallelism():
    model = AutoModelForCausalLM.from_config(hf_gpt2_config)

model = smp.DistributedModel(model)
```

对于 `DistributedModel` 对象中的 `state_dict`，您可以使用 `translate_state_dict_to_hf_gpt2` 函数将权重加载到原始 Hugging Face GPT-2 模型中，如以下代码所示。

```
from smdistributed.modelparallel.torch.nn.huggingface.gpt2 \
                                      import translate_state_dict_to_hf_gpt2
max_seq_len = 1024

# [... code block for training ...]

if smp.rdp_rank() == 0:
    state_dict = dist_model.state_dict()
    hf_state_dict = translate_state_dict_to_hf_gpt2(state_dict, max_seq_len)

    # can now call model.load_state_dict(hf_state_dict) to the original HF model
```

**Ro BERTa 翻译函数的用法示例**

同样，给定支持的 HuggingFace 模型`state_dict`，您可以使用`translate_hf_state_dict_to_smdistributed`函数将其转换为可读的格式`smp.DistributedModel`。这在迁移学习使用场景中非常有用，此时预训练的模型加载到 `smp.DistributedModel` 中用于模型并行微调：

```
from smdistributed.modelparallel.torch.nn.huggingface.roberta \
                                      import translate_state_dict_to_smdistributed

model = AutoModelForMaskedLM.from_config(roberta_config)
model = smp.DistributedModel(model)

pretrained_model = AutoModelForMaskedLM.from_pretrained("roberta-large")
translated_state_dict =
        translate_state_dict_to_smdistributed(pretrained_model.state_dict())

# load the translated pretrained weights into the smp.DistributedModel
model.load_state_dict(translated_state_dict)

# start fine-tuning...
```