本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
支援 Hugging Face 轉換器模型
SageMaker 模型平行處理程式庫的張量平行處理針對下列 Hugging Face 轉換器模型提供立即可用支援:
-
GPT-2、BERT 與 RoBERTa (適用 SageMaker 模型平行處理程式庫 v1.7.0 及更高版本)
-
GPT-J (適用 SageMaker 模型平行處理程度程式庫 v1.8.0 及更高版本)
-
GPT-NEO (適用 SageMaker 模型平行處理程式庫 v1.10.0 及更高版本)
注意
對於任何其他轉換器模型,您需要使用 smdistributed.modelparallel.torch.tp_register_with_module()
注意
若要使用張量平行處理來訓練 Hugging Face 轉換器模型,請確定您使用 Hugging Face Deep Learning Containers (適用於具 SageMaker 模型平行處理程式庫 v1.7.0 及更高版本的 PyTorch)。如需更多資訊,請參閱 SageMaker 模型平行處理程式庫版本備註
開箱即用的支援模型
對於開箱即用程式庫支援的 Hugging Face 轉換器模型,您不需要手動實作勾點,即可將轉換器 API 翻譯為 smdistributed
轉換器層。您可以透過使用內容管理器 smdistributed.modelparallel.torch.tensor_parallelism()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...