支援 Hugging Face 轉換器模型 - Amazon SageMaker

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

支援 Hugging Face 轉換器模型

SageMaker 模型平行程式庫的張量平行度可 out-of-the-box支援下列 Hugging Face 變壓器模型:

  • GPT-2、BERT 和羅伯特 (可在 SageMaker 模型平行處理程式庫 v1.7.0 及更新版本中使用)

  • GPT-J (可在 SageMaker 模型平行程式庫 v1.8.0 及更新版本中使用)

  • GPT-NEO (可在 SageMaker 模型平行程式庫 v1.10.0 及更新版本中使用)

注意

對於任何其他轉換器模型,您需要使用 smdistributed.modelparallel.torch.tp_register_with_module() API 來套用張量平行處理。

注意

要使用張量並行性來訓練 Hugging Face 變壓器模型,請確保您使用 Hugging Face Deep Learning Containers 具有 SageMaker模型並行性庫 v1.7.0 及更高版本。 PyTorch 如需詳細資訊,請參閱SageMaker 模型平行程度庫版本說明

開箱即用的支援模型

對於開箱即用程式庫支援的 Hugging Face 轉換器模型,您不需要手動實作勾點,即可將轉換器 API 翻譯為 smdistributed 轉換器層。您可以使用內容管理員中的內容管理員啟動張量平行處理原則。 DistributedModel()。您不需要使用 smp.tp_register API 手動註冊張量平行處理的勾點。

Hugging Face 轉換器之間與 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

RoBERTa 平移函式使用範例

同樣地,在支援的 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...