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
相关推荐
Fuly10242 分钟前
多模态大模型应用技术栈
人工智能·深度学习·计算机视觉
Brduino脑机接口技术答疑8 分钟前
TDCA 算法在 SSVEP 场景中的训练必要性
人工智能·算法·机器学习·脑机接口
悟道心18 分钟前
1.自然语言处理NLP - 入门
人工智能·自然语言处理
superman超哥21 分钟前
仓颉借用检查器工作原理深度解析
c语言·开发语言·c++·python·仓颉
雪花desu26 分钟前
深度解析RAG(检索增强生成)技术
人工智能·深度学习·语言模型·chatgpt·langchain
张彦峰ZYF36 分钟前
Python 项目文件组织与工程化实践
python·项目文件组织与工程化实践
咚咚王者1 小时前
人工智能之数学基础 离散数学:第四章 离散概率
人工智能
webbodys1 小时前
Python文件操作与异常处理:构建健壮的应用程序
java·服务器·python
阿标在干嘛1 小时前
科力辰平台:作为一个科技查新平台,其核心能力边界在哪里?
人工智能·科技
徽4401 小时前
农田植被目标检测数据标注与模型训练总结3
人工智能·目标检测·目标跟踪