Pytorch huggingface transformers gpt2生成多个GPU

Pytorch huggingface transformers gpt2生成多个GPU,pytorch,huggingface-transformers,Pytorch,Huggingface Transformers,我使用huggingface transformer gpt xl模型生成多个响应。我正在尝试在多个gpu上运行它,因为gpu内存最大,有多个较大的响应。我曾尝试使用dataparallel来实现这一点,但从nvidia smi来看,似乎从未使用过第二个gpu。这是我的密码: import numpy as np from transformers import GPT2Tokenizer, GPT2LMHeadModel import torch n_gpu=torch.cuda.device

我使用huggingface transformer gpt xl模型生成多个响应。我正在尝试在多个gpu上运行它,因为gpu内存最大,有多个较大的响应。我曾尝试使用dataparallel来实现这一点,但从nvidia smi来看,似乎从未使用过第二个gpu。这是我的密码:

import numpy as np
from transformers import GPT2Tokenizer, GPT2LMHeadModel
import torch
n_gpu=torch.cuda.device_count()
#device = xm.xla_device()
device=torch.device("cuda:0")
tokenizer = GPT2Tokenizer.from_pretrained('/spell/GPT2Model/GPT2Model/') #downloaded pre-trained model and tokenizer earlier
model = GPT2LMHeadModel.from_pretrained('/spell/GPT2Model/GPT2Model/')
model.to(device)
model = torch.nn.DataParallel(model, device_ids=[0,1])
encoded_prompt=tokenizer.encode(prompt_text, add_special_tokens=True,return_tensors="pt")
encoded_prompt = encoded_prompt.to(device)
outputs = model.module.generate(encoded_prompt,response_length,temperature=.8,num_return_sequences=num_of_responses,repetition_penalty=85,do_sample=True,top_k=80,top_p=.85 )

程序在双T4上获得oom,第二个gpu的内存永远不会超过11M。

DataParallel
跨gpu复制模型,但一个模型完全保留在单个gpu上,只需拆分批量大小以跨可用gpu分发数据。如果要将模型的各个部分拆分到不同的GPU,则需要手动将层和输入/输出放到这些设备上。概述了该方法。
DataParallel
跨GPU复制模型,但一个模型完全保留在单个GPU上,只需将批量大小拆分以跨可用GPU分发数据。如果要将模型的各个部分拆分到不同的GPU,则需要手动将层和输入/输出放到这些设备上。概述了这一方法。