Antkillerfarm Hacking V7.0

并行 & 框架 & 优化(三)——tf.distribute & MultiDevice, Pipeline Parallel, 其他概念, 大模型训练

2022-09-13

MPI(续)

https://blog.csdn.net/q19149/article/details/102594031

集合通信函数图解

https://zhuanlan.zhihu.com/p/465967735

分布式训练硬核技术——通讯原语

https://zhuanlan.zhihu.com/p/276122469

分布式训练常用技术简介

https://zhuanlan.zhihu.com/p/425830285

最理想的点到点通信库究竟是怎样的?

tf.distribute & MultiDevice

MirroredStrategy:单机多卡训练

MultiWorkerMirroredStrategy:多机训练

CentralStorageStrategy也执行同步训练,但是变量不会被镜像,而是放在CPU上。各操作(operation)在本地GPU之间复制进行。如果只有一个GPU,变量和操作都会放在GPU上。在对CPU上的变量进行更新前,该策略会先将所有 GPU副本的上的变量梯度进行聚合,然后应用到CPU变量更新中。


tensorflow::ProcessFunctionLibraryRuntime::RunMultiDevice

https://www.cnblogs.com/rossiXYZ/p/16142677.html

TensorFlow之分布式变量(该作者写了一系列的TF分布式文章)

示例:

https://github.com/antkillerfarm/antkillerfarm_crazy/tree/master/python/ml/tensorflow/xla/multi_device_lenet_xla.py

Rendezvous

Rendezvous是一个法语单词,发音也比较特殊,一般直译为“约会、相会、会和”,而在TensorFlow中,Rendezvous是用来完成消息传输的通信组件。

消息传输的唯一标识符——ParsedKey

Send和RecvAsync二者的相对顺序是不能保证先后的,经常出现需求比供给在时间片上先到的情况,总是迟到的一方执行waiter函数。

Send方——将Ready的Tensor挂入本地Table

Recv方——向Send方主动发出请求,触发通信过程

所以,真正的通信过程由Recv方触发,而不是Send方。

TensorFlow已经支持包括gRPC,RDMA(Remote Direct Memroy Access),GDR(GPU Direct)和MPI四种通信协议。

BFC(Best-Fit with Coalescing)是dlmalloc的一个简单实现版本。

https://www.cnblogs.com/deep-learning-stacks/p/10354258.html

TensorFlow中的通信机制——Rendezvous(一)本地传输

https://www.cnblogs.com/deep-learning-stacks/p/10355770.html

TensorFlow中的通信机制——Rendezvous(二)gRPC传输

https://blog.csdn.net/gaofeipaopaotang/article/details/80736452

模型优化之分布式执行

https://xieyu.github.io/blog/tensorflow/rendezvous.html

Tensorflow Rendezvous


Host to Device:

SameWorkerRecvDone -> CopyTensor::ViaDMA -> CopyHostToDevice -> XlaDeviceContext::CopyCPUTensorToDevice ->

GenericTransferManager::TransferLiteralToDeviceAsync -> TransferManager::TransferBufferToDevice -> Stream::ThenMemcpy

StreamExecutor

StreamExecutor是Google内部为并行编程模型开发的库。TensorFlow中的StreamExecutor是StreamExecutor的开源简版。

https://www.cnblogs.com/deep-learning-stacks/p/9386188.html

TensorFlow中的并行执行引擎——StreamExecutor框架

TF使用stream_executor::DeviceMemoryBase作为设备内存的抽象。用DeviceMemoryBase::opaque作为对于不可直接访问的设备地址的指针。

class GpuExecutor : public internal::StreamExecutorInterface

所以上面提到的Memcpy的调用路径,还有设备相关的后半部分:

Stream::ThenMemcpy -> StreamExecutor::Memcpy -> GpuExecutor::Memcpy -> GpuDriver::AsynchronousMemcpyH2D -> cuMemcpyHtoDAsync

TransferManager

TransferManager类使后端能够提供特定于平台的机制,用于通过给定的设备内存句柄构造XLA literal data。换言之,它可以帮助封装主机与设备之间的双向数据传输。

TransferManager类已经有了一个通用实现:GenericTransferManager,设备只需要派生该类,做一些定制化的修改。所以xxx_transfer_manager.h是关注的重点。

TPU和GPU的修改主要集中在TransferLiteralToInfeedTransferLiteralFromOutfeed两个函数。

这两个函数的GPU实现在InfeedManager类中。

TransferLiteralToInfeed关键函数调用:

gpu::CopyBufferToDevice -> Stream::ThenMemcpy

如果不对GenericTransferManager做修改,则该类会用Host上的mem buffer,虚拟一个DeviceMemoryBase来处理Feed。

graphcore的实现没有动GenericTransferManager,而是自己单独弄了一套基于TranslatedFeedInfo类的cache机制。

tensorflow/compiler/plugin/poplar/driver/poplar_executable_cache.cc

Pipeline Parallel

右上图一般称为朴素流水线并行,该方案在任意给定时刻,除了一个GPU之外的其他所有GPU都是空闲的。

所以就有了Micro-batch Pipeline Parallel。将Batch数据分成N份,每次算完1份,就将输出交给下一个GPU,同时开始处理下1个Micro-batch的数据。这里的N一般被称为chunks。

注:模型经过拆分后的上一个rank的stage需要长期持续处于空闲状态,等待其他rank的stage计算完成,才可以开始计算,这极大降低了设备的平均使用率。这种现象被称为并行空泡(Parallelism Bubble)。

上图这种最简单的Mini-batch Pipeline Parallel,一般称为F-then-B模式,先进行前向计算,再进行反向计算。

1F1B(One Forward pass followed by One Backward pass)模式,一种前向计算和反向计算交叉进行的方式。在1F1B模式下,前向计算和反向计算交叉进行,可以及时释放不必要的中间变量。

使用1F1B策略时,存在两种调度模式:非交错调度和交错式调度。

所谓交错式调度,就是在第一个batch(上图中包含4个mini-batch)的F和B还没有完成的情况下,就开始计算第二个batch的mini-batch了。

参考:

https://zhuanlan.zhihu.com/p/618590870

大模型训练Pipeline Parallel流水并行性能分析

https://zhuanlan.zhihu.com/p/653860567

流水线并行

https://www.cnblogs.com/rossiXYZ/p/15318574.html

PyTorch流水线并行实现

TF-Replicator, GPipe, Mesh-Tensorflow

TF-Replicator主要侧重于Data parallelism的库。

GPipe是一个纯model-parallelism的库。

Mesh-TensorFlow是一个DSL语言,既支持Data parallelism,又支持Model parallelism,但是侵入性很强。

参考:

https://zhuanlan.zhihu.com/p/113233933

谷歌GPipe训练超大规模神经网络

https://zhuanlan.zhihu.com/p/342223356

Mesh-Tensorflow: 广义分布式

https://zhuanlan.zhihu.com/p/63211072

TF-Replicator, GPipe, Mesh-Tensorflow三个库对比

https://mp.weixin.qq.com/s/9k6PDusoDHjmz58HAZxZcw

GPipe: 小批量流水线带来的大模型训练

https://mp.weixin.qq.com/s/HY2yPZ–Zm5_m3B70baWjQ

谷歌开源效率怪兽GPipe,速度提升25倍,CIFAR-10精度达到99%

pytorch

因为GPipe是基于TensorFlow的库,所以kakaobrain的一些工程师就用PyTorch来实现了GPipe,并且开源出来,这就是torchgpipe。

代码:

https://github.com/kakaobrain/torchgpipe

fairscale.nn.pipefork自torchgpipe,并最终进入torch.distributed.pipeline

代码:

benchmarks/distributed/pipeline/pipe.py

另一个Pipeline Parallelism的库。

https://github.com/pytorch/PiPPy

参考:

https://www.cnblogs.com/rossiXYZ/p/15318574.html

PyTorch流水线并行实现 (1)–基础知识

https://pytorch.org/docs/stable/pipeline.html

Pipeline Parallelism

https://pytorch.org/tutorials/intermediate/pipeline_tutorial.html

Training Transformer models using Pipeline Parallelism

https://pytorch.org/tutorials/advanced/ddp_pipeline.html

Training Transformer models using Distributed Data Parallel and Pipeline Parallelism


torch.distributed.pipeline关键代码:

torch/distributed/pipeline/sync/pipeline.py: _clock_cycles
torch/distributed/pipeline/sync/pipe.py: _split_module

其他概念

分布式数据集

大模型不光模型的训练是分布式的,数据集也是分布式的。

https://www.alanshawn.com/tech/2022/02/27/tensorflow-big-dataset.html

Using Huge, Heterogenous Datasets in TensorFlow

https://tensorflow.google.cn/datasets/beam_datasets?hl=zh-cn

使用Apache Beam生成大型数据集

https://www.tensorflow.org/tutorials/distribute/input?hl=zh-cn

分布式输入

Pathways

https://blog.csdn.net/OneFlow_Official/article/details/124054450

解读谷歌Pathways架构(一):Single-controller与Multi-controller

https://blog.csdn.net/OneFlow_Official/article/details/124113864

解读谷歌Pathways架构(二):向前一步是OneFlow

框架设计

https://zhuanlan.zhihu.com/p/547878945

五谈AI软件栈–无责乱弹AI软件栈研发方法论

数据下沉

数据下沉是将数据一次性传输到端侧,减少频繁的Host-Device数据传输来加速的技术。因为数据在Device上通过通道传输,Host侧和Device侧之间每个epoch进行一次数据交互,所以每个epoch只返回一次结果。

https://zhuanlan.zhihu.com/p/397481167

MindSpore的桎梏和破局

https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/63RC2alpha002/tfmoddevg/tfmigr1/atlasmprtg_13_9048.html

训练迭代循环下沉

大模型训练

目前部分深度学习框架,例如Pytorch和Tensorflow,没有办法满足超大规模模型训练的需求,于是微软基于Pytroch开发了DeepSpeed,腾讯基于Pytroch开发了派大星PatricStar,达摩院基于Tensoflow开发的分布式框架Whale。像是华为昇腾的MindSpore、百度的PaddlePaddle,还有国内的一流科技OneFlow等厂商,对超大模型训练进行了深度的跟进与探索,基于原生的AI框架支持超大模型训练。

https://zhuanlan.zhihu.com/p/432813821

大模型的发展与解决的问题

https://zhuanlan.zhihu.com/p/432289008

从分布式训练到大模型训练

https://www.zhihu.com/question/498271491

为什么说大模型训练很难?

https://zhuanlan.zhihu.com/p/611325149

大型语言模型(LLM)训练指南

https://zhuanlan.zhihu.com/p/629589593

LLM应用开发全栈指南

实战经验

https://pytorch.org/blog/high-performance-llama-2/

https://huggingface.co/blog/zh/bloom-inference-optimization

https://huggingface.co/blog/zh/bloom-megatron-deepspeed

Fork me on GitHub