pytorch演示pipeline并行

pytorch演示pipeline并行

单卡内存不够时,可以将网络切分成几段(stage),每个GPU负责一个stage。比如GPU0计算完之后将数据发送给GPU1算后续的stage;以上的方式,会导致GPU的利用率不高,可以将输入的batch切分成多份更小的batch,陆续送给GPU0,这样GPU0处理完micro batch0之后 可以处理micro batch1.如此便能提高GPU的利用率

1.原理

2.复现过程

python 复制代码
tee pp_demo.py <<-'EOF'
import os
import torch
from torch import nn
import torch.nn.functional as F
import numpy as np
import torch.distributed as dist
from torch.distributed import ReduceOp   
import time
import argparse
  
parser = argparse.ArgumentParser(description="")
parser.add_argument('--hidden_size', default=512, type=int, help='')
parser.add_argument('--ffn_size', default=1024, type=int, help='')
parser.add_argument('--seq_len', default=512, type=int, help='')
parser.add_argument('--batch_size', default=8, type=int, help='')
parser.add_argument('--world_size', default=4, type=int, help='')
parser.add_argument('--device', default="cuda", type=str, help='')
parser.add_argument('--chunk_size', default=1, type=int, help='')
 
class FeedForward(nn.Module):
  
    def __init__(self,hidden_size,ffn_size):
        super(FeedForward, self).__init__()
        self.fc1 = nn.Linear(hidden_size, ffn_size,bias=False)
        self.fc2 = nn.Linear(ffn_size, hidden_size,bias=False)
  
    def forward(self, input):
        return self.fc2(self.fc1(input))
 
args = parser.parse_args()
hidden_size = args.hidden_size
ffn_size = args.ffn_size
seq_len = args.seq_len
batch_size = args.batch_size
world_size = args.world_size
device = args.device
chunk_size = args.chunk_size
 
def tp_mode():
  torch.random.manual_seed(1)
  dist.init_process_group(backend='nccl')
      
  world_size = torch.distributed.get_world_size()
  rank=rank = torch.distributed.get_rank()
  local_rank=int(os.environ['LOCAL_RANK'])
    
  torch.cuda.set_device(local_rank)
  device = torch.device("cuda",local_rank)
 
  model = FeedForward(hidden_size,ffn_size)
  model.eval()
  input = torch.rand((batch_size, seq_len, hidden_size),dtype=torch.float32).half().to(device)
  model=model.half().to(device)
 
  index=0
  count=0
  t0=0
 
  chunks=torch.split(input,chunk_size,dim=0)
 
  for epoch in range(32):
    index+=1
    if index>1:
      count+=1
      if t0==0:
        t0=time.time()
      if count%10==0 and rank==0:
        print("qps:{:.2f}".format(count/(time.time()-t0)))
        count=0
        t0=0
 
    all_output=[]
    snd_reqs=[]
 
    for chunk in chunks: 
      if rank==0:
        out=model(chunk)
      else:
        torch.distributed.recv(chunk,rank-1)
        out=model(chunk)
      if rank==world_size-1:
        all_output.append(out.clone())
      else:
        snd_reqs = torch.distributed.send(out,rank+1)
 
    if rank==world_size-1:
      out=torch.cat(all_output,dim=0)
 
if __name__ == "__main__":
  num_gpus = int(os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1
  is_distributed = num_gpus > 1
  if is_distributed:
    tp_mode()
EOF
 
torchrun -m --nnodes=1 --nproc_per_node=4 pp_demo \
		--hidden_size 512 --ffn_size 4096 --seq_len 512 \
		--batch_size 16 --world_size 4 --chunk_size 8
相关推荐
GFCGUO5 分钟前
ubuntu18.04运行OpenPCDet出现的问题
linux·python·学习·ubuntu·conda·pip
985小水博一枚呀2 小时前
【深度学习基础模型】神经图灵机(Neural Turing Machines, NTM)详细理解并附实现代码。
人工智能·python·rnn·深度学习·lstm·ntm
SEU-WYL3 小时前
基于深度学习的任务序列中的快速适应
人工智能·深度学习
OCR_wintone4213 小时前
中安未来 OCR—— 开启高效驾驶证识别新时代
人工智能·汽车·ocr
matlabgoodboy3 小时前
“图像识别技术:重塑生活与工作的未来”
大数据·人工智能·生活
萧鼎3 小时前
Python调试技巧:高效定位与修复问题
服务器·开发语言·python
最近好楠啊3 小时前
Pytorch实现RNN实验
人工智能·pytorch·rnn
OCR_wintone4213 小时前
中安未来 OCR—— 开启文字识别新时代
人工智能·深度学习·ocr
学步_技术3 小时前
自动驾驶系列—全面解析自动驾驶线控制动技术:智能驾驶的关键执行器
人工智能·机器学习·自动驾驶·线控系统·制动系统
IFTICing3 小时前
【文献阅读】Attention Bottlenecks for Multimodal Fusion
人工智能·pytorch·python·神经网络·学习·模态融合