[{"data":1,"prerenderedAt":811},["ShallowReactive",2],{"content-query-NnXdsC7qzT":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":805,"_id":806,"_source":807,"_file":808,"_stem":809,"_extension":810},"/technology-blogs/zh/1040","zh",false,"","用这拌元宵，一个字：香！| 分布式训练硬核技术——通讯原语","分布式的通信一般有两大类：  1、集合通信 2、点对点通信","2022-02-15","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/29f45b14b9af489dab11f15e5eb3651a.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":799},"root",[17,25,34,55,62,74,93,110,121,138,155,160,178,185,193,198,206,211,216,221,228,233,238,243,248,253,261,266,271,276,281,286,291,298,303,308,315,320,328,336,341,349,354,359,366,371,378,386,391,398,403,408,415,423,428,435,440,447,455,460,467,472,479,487,492,499,504,511,519,524,531,536,543,551,556,563,571,576,581,586,591,598,606,611,619,624,629,634,642,647,652,660,665,670,675,680,685,690,695,703,743,750,757,760,772,781,790],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"用这拌元宵一个字香-分布式训练硬核技术通讯原语",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":18,"tag":30,"props":31,"children":33},"img",{"alt":7,"src":32},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/14bbfa817b94494a8be55a06e5063631.gif",[],{"type":18,"tag":26,"props":35,"children":36},{},[37,48,50],{"type":18,"tag":38,"props":39,"children":40},"strong",{},[41,43],{"type":24,"value":42},"作者：",{"type":18,"tag":38,"props":44,"children":45},{},[46],{"type":24,"value":47},"ZOMI酱",{"type":24,"value":49}," ｜",{"type":18,"tag":38,"props":51,"children":52},{},[53],{"type":24,"value":54},"来源：知乎",{"type":18,"tag":26,"props":56,"children":57},{},[58],{"type":18,"tag":30,"props":59,"children":61},{"alt":7,"src":60},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/825fcdb6d7a64ed98b92a535db1417d3.jpg",[],{"type":18,"tag":26,"props":63,"children":64},{},[65,67,72],{"type":24,"value":66},"针对分布式训练服务器的集群进行架构设计，是为了更好地解决机器学习中分布式训练中的通讯问题。目前机器学习中主要由两种",{"type":18,"tag":38,"props":68,"children":69},{},[70],{"type":24,"value":71},"分布式架构",{"type":24,"value":73},"：",{"type":18,"tag":26,"props":75,"children":76},{},[77,79,84,86,91],{"type":24,"value":78},"1、",{"type":18,"tag":38,"props":80,"children":81},{},[82],{"type":24,"value":83},"参数服务器架构",{"type":24,"value":85},"（",{"type":18,"tag":38,"props":87,"children":88},{},[89],{"type":24,"value":90},"Parameter Server，PS",{"type":24,"value":92},"）",{"type":18,"tag":26,"props":94,"children":95},{},[96,98,103,104,109],{"type":24,"value":97},"2、",{"type":18,"tag":38,"props":99,"children":100},{},[101],{"type":24,"value":102},"去中心化架构",{"type":24,"value":85},{"type":18,"tag":38,"props":105,"children":106},{},[107],{"type":24,"value":108},"Decentralized Network",{"type":24,"value":92},{"type":18,"tag":26,"props":111,"children":112},{},[113,115,120],{"type":24,"value":114},"其中，分布式训练通常在计算集群上进行，集群的每个节点分别执行一部分计算。不同节点的计算之间有数据依赖和共享，需要将数据在不同节点间传输，这就是通信。",{"type":18,"tag":38,"props":116,"children":117},{},[118],{"type":24,"value":119},"分布式的通信一般有两大类",{"type":24,"value":73},{"type":18,"tag":26,"props":122,"children":123},{},[124,125,130,131,136],{"type":24,"value":78},{"type":18,"tag":38,"props":126,"children":127},{},[128],{"type":24,"value":129},"集合通信",{"type":24,"value":85},{"type":18,"tag":38,"props":132,"children":133},{},[134],{"type":24,"value":135},"Collective communication，CC",{"type":24,"value":137},"）：在一组节点内进行通信",{"type":18,"tag":26,"props":139,"children":140},{},[141,142,147,148,153],{"type":24,"value":97},{"type":18,"tag":38,"props":143,"children":144},{},[145],{"type":24,"value":146},"点对点通信",{"type":24,"value":85},{"type":18,"tag":38,"props":149,"children":150},{},[151],{"type":24,"value":152},"Point to point communication，P2P",{"type":24,"value":154},"）：在两个节点之间进行通信",{"type":18,"tag":26,"props":156,"children":157},{},[158],{"type":24,"value":159},"深度学习训练过程中因为需要传输大量的网络模型权重参数和训练过程中产生的大量临时变量等，因此主要使用集合通信的方式。可以理解为，机器学习/深度学习的分布式训练，主要是采用在PS架构下的集合通讯模式；而在大模型的分布式训练中，因为减少跟单点参数服务器统一更新，更多直接采用纯集合通讯模式。",{"type":18,"tag":26,"props":161,"children":162},{},[163,165,170,171,176],{"type":24,"value":164},"在深度学习框架中，分布式训练的通讯原语和通讯实现方式对AI框架分布式训练起着非常重要的作用，如果想要训练",{"type":18,"tag":38,"props":166,"children":167},{},[168],{"type":24,"value":169},"大模型",{"type":24,"value":85},{"type":18,"tag":38,"props":172,"children":173},{},[174],{"type":24,"value":175},"Foundation Model",{"type":24,"value":177},"）肯定离不开进行通讯操作，下面橙色标签的是分布式训练中通讯原语在AI框架中的的位置。",{"type":18,"tag":26,"props":179,"children":180},{},[181],{"type":18,"tag":30,"props":182,"children":184},{"alt":7,"src":183},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/6d29e3453819452199ca042296eccfe0.jpg",[],{"type":18,"tag":26,"props":186,"children":187},{},[188],{"type":18,"tag":38,"props":189,"children":190},{},[191],{"type":24,"value":192},"为什么需要对通讯操作",{"type":18,"tag":26,"props":194,"children":195},{},[196],{"type":24,"value":197},"当将神经网络的训练并行化到集群中不同的节点时，必须选择如何将不同的计算操作分配到集群中可用的节点，这就离不开对分布式集群的通讯进行操作。",{"type":18,"tag":26,"props":199,"children":200},{},[201],{"type":18,"tag":38,"props":202,"children":203},{},[204],{"type":24,"value":205},"数据并行通讯问题",{"type":18,"tag":26,"props":207,"children":208},{},[209],{"type":24,"value":210},"通常神经网络的训练学习过程中，使用随机梯度下降算法SGD进行训练。",{"type":18,"tag":26,"props":212,"children":213},{},[214],{"type":24,"value":215},"以分布式数据并行为例，在数据并行训练中，每块NPU都有整个神经网络模型的完整副本，每次进行迭代的时候，只分配了小批次Batch中数据样本的子集，每块NPU利用该Batch样本数据进行网络模型的前向计算，然后计算损失误差进行反向传播，以计算损耗相对于网络参数的梯度。",{"type":18,"tag":26,"props":217,"children":218},{},[219],{"type":24,"value":220},"最后，NPU 之间进行相互通信，计算由不同 NPU 得到的梯度的平均值，将平均梯度应用于总体权重以获得网络模型中本次迭代的最新权重。任何一块 NPU 都在锁定步骤的迭代中前进，一旦某块 NPU 完成了本轮迭代，必须等待集群中所有 NPU 完成各自的迭代，这样神经网络的总体权重才能被正确更新。简单地理解，相当于在单个 NPU 上执行SGD，但是通过在多个 NPU 之间分发数据，通过并行执行计算来获得加速。这就是简单的分布式**数据并行（DP）**方式。",{"type":18,"tag":26,"props":222,"children":223},{},[224],{"type":18,"tag":30,"props":225,"children":227},{"alt":7,"src":226},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/da67b9c653524a59a76a96f355672d28.jpg",[],{"type":18,"tag":26,"props":229,"children":230},{},[231],{"type":24,"value":232},"当只有两个 NPU 和以兆字节数据衡量网络模型参数的时候，NPU 的通信方式可能并不重要。然而，当网络模型的参数量去到十亿甚至万亿个参数时，仅仅是计算梯度所产生的临时变量就需要数十亿字节的 NPU 内存空间，并且需要协调数十个 NPU 共同工作，这个时候通信机制变得至关重要。",{"type":18,"tag":26,"props":234,"children":235},{},[236],{"type":24,"value":237},"例如，考虑上图中最直接的数据并行通信方式：每一个 NPU 都计算其小Batch上的梯度，然后每个 NPU 将其梯度发送到 CPU 上，该 CPU 取所有梯度的平均值，并将平均值发送回所有其他 NPU。",{"type":18,"tag":26,"props":239,"children":240},{},[241],{"type":24,"value":242},"直接从单个 CPU 发送和接收数据的机制中，CPU 必须从所有 NPU 中接收参数，并将所有参数逐一发送到所有 NPU中，当分布式集群系统中的 NPU 节点越多，通信成本就越大。",{"type":18,"tag":26,"props":244,"children":245},{},[246],{"type":24,"value":247},"现在来以上述的通信策略评估在真实网络模型上运行的带宽效果。以三亿个可训练参数的网络模型为例：假设每个参数四个字节，三亿个参数约1.2千兆字节，分布式系统的网络硬件可以支持每秒1千兆字节的带宽。在这种情况下，将系统并行化到两个 NPU 上将使每次迭代减慢1.2秒，将训练并行化到10个 NPU 将使每次迭代减慢10.8秒。随着 NPU 数量的增长，每次迭代所需的时间将会呈线性增长。",{"type":18,"tag":26,"props":249,"children":250},{},[251],{"type":24,"value":252},"需要发送的数据越多，通讯的时间就越长。每个通信通道都有一个最大的吞吐量，即网络带宽。例如，一个Internet连接可以提供每秒15兆字节的带宽，而千兆以太网连接可以提供每秒125兆字节的带宽，HPC集群上的专用网络硬件Infiniband可以在节点之间提供每秒GB级的带宽。即使每次迭代花费几秒钟，通信成本的线性增长也会使得分布式并行，并没有带来实际的优化，反而会降低训练效率。",{"type":18,"tag":26,"props":254,"children":255},{},[256],{"type":18,"tag":38,"props":257,"children":258},{},[259],{"type":24,"value":260},"异步 vs 同步通讯",{"type":18,"tag":26,"props":262,"children":263},{},[264],{"type":24,"value":265},"异步 vs 同步通信属于网络模型参数更新的策略。",{"type":18,"tag":26,"props":267,"children":268},{},[269],{"type":24,"value":270},"数据并行中，在各个 NPU 分别根据各自获得的batch，前向计算获得损失，进而反向传播计算梯度。计算好梯度后，就涉及到一个梯度同步的问题：每个节点都有根据自己的数据计算的梯度，如何在不同节点之间维护模型的不同副本之间的一致性？",{"type":18,"tag":26,"props":272,"children":273},{},[274],{"type":24,"value":275},"如果不同的模型以某种方式最终获得不同的权重，则权重更新将变得不一致，最终导致网络模型参数收敛迭代收敛情况前后不一致，这称为模型一致性问题（consistency of model），如何同步更新网络模型参数，这是分布式机器学习系统的一个核心问题。",{"type":18,"tag":26,"props":277,"children":278},{},[279],{"type":24,"value":280},"为了解决模型一致性问题，分布式训练的梯度同步策略可分为异步（Asynchronous）梯度更新和同步（Synchronous）梯度更新机制。",{"type":18,"tag":26,"props":282,"children":283},{},[284],{"type":24,"value":285},"同步指的是所有的节点都采用相同的模型参数来训练，等待所有设备的batch训练完成后，收集它们的梯度然后取均值，最后所有网络模型的参数一次性更新。",{"type":18,"tag":26,"props":287,"children":288},{},[289],{"type":24,"value":290},"由于每一轮迭代结束时，计算的比较快的节点都需等待算得慢的节点信息进行同步，再进行下一轮迭代。类似于木桶效应，所以同步方式的性能取决于集群通讯最慢的节点。",{"type":18,"tag":26,"props":292,"children":293},{},[294],{"type":18,"tag":30,"props":295,"children":297},{"alt":7,"src":296},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/0a234550bd2f4dcaafc8e752d2db0c42.jpg",[],{"type":18,"tag":26,"props":299,"children":300},{},[301],{"type":24,"value":302},"异步通讯中，各节点完成一个batch数据的训练之后，不需要等待其它节点，直接更新Server中的网络模型参数，因为是异步更新参数的因此不需要阻塞性等待单节点的数据，使得总体会训练速度会快很多。",{"type":18,"tag":26,"props":304,"children":305},{},[306],{"type":24,"value":307},"异步通讯的问题是梯度失效（stale gradients），刚开始所有节点采用相同的参数来训练，但是异步情况下，某节点完成 T 步迭代训练后，可能发现模型参数其实已经被其它设备在 T+n 步更新过了，如果当前节点把 T 步迭代训练参数更新到 Server中，会造成 n 步内的梯度更新无效。由于梯度失效问题，异步训练虽然速度快，但是可能陷入次优解（sub-optimal training performance）。",{"type":18,"tag":26,"props":309,"children":310},{},[311],{"type":18,"tag":30,"props":312,"children":314},{"alt":7,"src":313},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/9b4980a037df495e9abb717015076d91.jpg",[],{"type":18,"tag":26,"props":316,"children":317},{},[318],{"type":24,"value":319},"回到上面三亿个可训练参数的网络模型案例中，另一种参数更新的选择是异步通讯，虽然异步更新会使得数据同步不需要阻塞性地等待，减少通讯的延时，但是消除了参数的同步更新后会使得SGD很难调试和收敛，因此不考虑异步更新的策略。",{"type":18,"tag":26,"props":321,"children":322},{},[323],{"type":18,"tag":38,"props":324,"children":325},{},[326],{"type":24,"value":327},"总的来说，在训练大模型的时候离不开分布式并行操作，需要利用分布式集群中的带宽通讯优化操作，优化深度学习参数同步策略来解决通信瓶颈。",{"type":18,"tag":26,"props":329,"children":330},{},[331],{"type":18,"tag":38,"props":332,"children":333},{},[334],{"type":24,"value":335},"通讯原语操作",{"type":18,"tag":26,"props":337,"children":338},{},[339],{"type":24,"value":340},"集合通讯中包含多个sender和多个receiver，一般的通信原语包括broadcast、gather、all-gather、scatter、reduce、all-reduce、reduce-scatter、all-to-all等通信操作进行数据传输，下面将会分别介绍其具体含义。",{"type":18,"tag":26,"props":342,"children":343},{},[344],{"type":18,"tag":38,"props":345,"children":346},{},[347],{"type":24,"value":348},"Broadcast",{"type":18,"tag":26,"props":350,"children":351},{},[352],{"type":24,"value":353},"在集合通信中，如果某个节点想把自身的数据发送到集群中的其他节点，那么就可以使用广播Broadcast的操作。",{"type":18,"tag":26,"props":355,"children":356},{},[357],{"type":24,"value":358},"如图所示，圆圈表示分布式系统中的独立节点，一共4个节点，小方块则代表了数据，颜色相同表示数据一样。Broadcast代表广播行为，执行Broadcast时，数据从主节点0广播至其他各个指定的节点（0~3）。",{"type":18,"tag":26,"props":360,"children":361},{},[362],{"type":18,"tag":30,"props":363,"children":365},{"alt":7,"src":364},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/45c3fbcc1d5141ec89b9ebfac2c55ac0.jpg",[],{"type":18,"tag":26,"props":367,"children":368},{},[369],{"type":24,"value":370},"Broadcast操作是将某节点的输入广播到其他节点上，分布式机器学习中常用于网络参数的初始化。如图中，从单个sender数据发送到其他节点上，将0卡大小为1xN的Tensor进行广播，最终每张卡输出均为[1xN]的矩阵。",{"type":18,"tag":26,"props":372,"children":373},{},[374],{"type":18,"tag":30,"props":375,"children":377},{"alt":7,"src":376},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/87bdf2659b7447958f2bed0de9e06c1a.jpg",[],{"type":18,"tag":26,"props":379,"children":380},{},[381],{"type":18,"tag":38,"props":382,"children":383},{},[384],{"type":24,"value":385},"Scatter",{"type":18,"tag":26,"props":387,"children":388},{},[389],{"type":24,"value":390},"Scatter操作表示一种散播行为，将主节点的数据进行划分并散布至其他指定的节点。",{"type":18,"tag":26,"props":392,"children":393},{},[394],{"type":18,"tag":30,"props":395,"children":397},{"alt":7,"src":396},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/e68ffc590c084f209957f4d7e943b392.jpg",[],{"type":18,"tag":26,"props":399,"children":400},{},[401],{"type":24,"value":402},"实际上，Scatter与Broadcast非常相似，都是一对多的通信方式，不同的是Broadcast的0号节点将相同的信息发送给所有的节点，而Scatter则是将数据的不同部分，按需发送给所有的节点。",{"type":18,"tag":26,"props":404,"children":405},{},[406],{"type":24,"value":407},"如图所示，从单个sender数据发送到其他节点上。",{"type":18,"tag":26,"props":409,"children":410},{},[411],{"type":18,"tag":30,"props":412,"children":414},{"alt":7,"src":413},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/d38c3911575c4bcea53e4063f1a1de8f.jpg",[],{"type":18,"tag":26,"props":416,"children":417},{},[418],{"type":18,"tag":38,"props":419,"children":420},{},[421],{"type":24,"value":422},"Reuduce",{"type":18,"tag":26,"props":424,"children":425},{},[426],{"type":24,"value":427},"Reuduce称为规约运算，是一系列简单运算操作的统称，细分可以包括：SUM、MIN、MAX、PROD、LOR等类型的规约操作。Reuduce意为减少/精简，因为其操作在每个节点上获取一个输入元素数组，通过执行操作后，将得到精简的更少的元素。下面以Reduce sum为例子。",{"type":18,"tag":26,"props":429,"children":430},{},[431],{"type":18,"tag":30,"props":432,"children":434},{"alt":7,"src":433},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/66ec554cd09441ab91a82de4f3b19c9f.jpg",[],{"type":18,"tag":26,"props":436,"children":437},{},[438],{"type":24,"value":439},"在NCCL中的Reduce，从多个sender那里接收数据，最终combine到一个节点上。",{"type":18,"tag":26,"props":441,"children":442},{},[443],{"type":18,"tag":30,"props":444,"children":446},{"alt":7,"src":445},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/eda144948a064835a31b7d4c1e16d7f7.jpg",[],{"type":18,"tag":26,"props":448,"children":449},{},[450],{"type":18,"tag":38,"props":451,"children":452},{},[453],{"type":24,"value":454},"All Reduce",{"type":18,"tag":26,"props":456,"children":457},{},[458],{"type":24,"value":459},"Reduce是一系列简单运算操作的统称，All Reduce则是在所有的节点上都应用同样的Reduce操作。以All Reduce Sum为例。",{"type":18,"tag":26,"props":461,"children":462},{},[463],{"type":18,"tag":30,"props":464,"children":466},{"alt":7,"src":465},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/42dab5e0ba8542e49403f651987c3021.jpg",[],{"type":18,"tag":26,"props":468,"children":469},{},[470],{"type":24,"value":471},"All Reduce操作可通过单节点上Reduce + Broadcast操作完成。在NCCL中的All Reduce中，则是从多个sender那里接收数据，最终合并和分发到每一个节点上。",{"type":18,"tag":26,"props":473,"children":474},{},[475],{"type":18,"tag":30,"props":476,"children":478},{"alt":7,"src":477},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/542719f1835a4a87abfdc3cde3fee999.jpg",[],{"type":18,"tag":26,"props":480,"children":481},{},[482],{"type":18,"tag":38,"props":483,"children":484},{},[485],{"type":24,"value":486},"Gather",{"type":18,"tag":26,"props":488,"children":489},{},[490],{"type":24,"value":491},"Gather操作将多个sender上的数据收集到单个节点上，Gather可以理解为反向的Scatter。",{"type":18,"tag":26,"props":493,"children":494},{},[495],{"type":18,"tag":30,"props":496,"children":498},{"alt":7,"src":497},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/f0a4e871d4594dfbb7ea12ff8f9bb1cb.jpg",[],{"type":18,"tag":26,"props":500,"children":501},{},[502],{"type":24,"value":503},"Gather操作会从多个节点里面收集数据到一个节点上面，而不是从一个节点分发数据到多个节点。这个机制对很多平行算法很有用，比如并行的排序和搜索。",{"type":18,"tag":26,"props":505,"children":506},{},[507],{"type":18,"tag":30,"props":508,"children":510},{"alt":7,"src":509},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/4380e8198c794ccb884b0b318361dc37.jpg",[],{"type":18,"tag":26,"props":512,"children":513},{},[514],{"type":18,"tag":38,"props":515,"children":516},{},[517],{"type":24,"value":518},"All Gather",{"type":18,"tag":26,"props":520,"children":521},{},[522],{"type":24,"value":523},"很多时候发送多个元素到多个节点也很有用，即在多对多通信模式的场景。这个时候就需要 All Gather操作。",{"type":18,"tag":26,"props":525,"children":526},{},[527],{"type":18,"tag":30,"props":528,"children":530},{"alt":7,"src":529},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/b18f58ca8cdf437fa56882ad92835dc9.jpg",[],{"type":18,"tag":26,"props":532,"children":533},{},[534],{"type":24,"value":535},"对于分发在所有节点上的一组数据来说，All Gather会收集所有数据到所有节点上。从最基础的角度来看，All Gather相当于一个Gather操作之后跟着一个Bcast操作。下面的示意图显示了All Gather调用之后数据是如何分布的。",{"type":18,"tag":26,"props":537,"children":538},{},[539],{"type":18,"tag":30,"props":540,"children":542},{"alt":7,"src":541},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/5668443081284de6a962f4db09d6555b.jpg",[],{"type":18,"tag":26,"props":544,"children":545},{},[546],{"type":18,"tag":38,"props":547,"children":548},{},[549],{"type":24,"value":550},"Reduce Scatter",{"type":18,"tag":26,"props":552,"children":553},{},[554],{"type":24,"value":555},"Reduce Scatter操作会将个节点的输入先进行求和，然后在第0维度按卡数切分，将数据分发到对应的卡上。例如上图所示，每卡的输入均为4x1的Tensor。Reduce Scatter先对输入求和得到[0, 4, 8, 12]的Tensor，然后进行分发，每卡获得1x1大小的Tensor。例如卡0对应的输出结果为[[0.0]]，卡1对应的输出结果为[[4.0]]。",{"type":18,"tag":26,"props":557,"children":558},{},[559],{"type":18,"tag":30,"props":560,"children":562},{"alt":7,"src":561},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/9a80ffd363aa41179dfb3741d1e0c1e1.jpg",[],{"type":18,"tag":26,"props":564,"children":565},{},[566],{"type":18,"tag":38,"props":567,"children":568},{},[569],{"type":24,"value":570},"All to All",{"type":18,"tag":26,"props":572,"children":573},{},[574],{"type":24,"value":575},"All to All作为全交换操作，通过All to All通信，可以让每个节点都获取其他节点的值。",{"type":18,"tag":26,"props":577,"children":578},{},[579],{"type":24,"value":580},"在使用 All to All 时，每一个节点都会向任意一个节点发送消息，每一个节点也都会接收到任意一个节点的消息。每个节点的接收缓冲区和发送缓冲区都是一个分为若干个数据块的数组。",{"type":18,"tag":26,"props":582,"children":583},{},[584],{"type":24,"value":585},"All to All 的具体操作是：将节点i的发送缓冲区中的第j块数据发送给节点j，节点j将接收到的来自节点i的数据块放在自身接收缓冲区的第i块位置。",{"type":18,"tag":26,"props":587,"children":588},{},[589],{"type":24,"value":590},"All to All 与 All Gather 相比较，区别在于：All Gather 操作中，不同节点向某一节点收集到的数据是完全相同的，而在 All to All 中，不同的节点向某一节点收集到的数据是不同的。在每个节点的发送缓冲区中，为每个节点都单独准备了一块数据。",{"type":18,"tag":26,"props":592,"children":593},{},[594],{"type":18,"tag":30,"props":595,"children":597},{"alt":7,"src":596},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/18/02ec75d5c07a4828bc2c65e1698fa2cb.jpg",[],{"type":18,"tag":26,"props":599,"children":600},{},[601],{"type":18,"tag":38,"props":602,"children":603},{},[604],{"type":24,"value":605},"AI框架中的通信实现",{"type":18,"tag":26,"props":607,"children":608},{},[609],{"type":24,"value":610},"分布式集群的网络硬件多种多样，可以是Ethernet、InfiniBand 等，深度学习框架通常不直接操作硬件，而是使用通信库。之所以采用通信库屏，是因为其蔽了底层硬件细节，提供了统一封装的通信接口。其中MPI和NCCL是最常用的通讯库，MPI专注于CPU的并行通讯，NCCL则专注于GPU的通讯。",{"type":18,"tag":26,"props":612,"children":613},{},[614],{"type":18,"tag":38,"props":615,"children":616},{},[617],{"type":24,"value":618},"Message Passing Interface (MPI)",{"type":18,"tag":26,"props":620,"children":621},{},[622],{"type":24,"value":623},"MPI 信息传递接口，是一个用于编写并行计算程序的编程接口。它提供了丰富全面的通信功能。",{"type":18,"tag":26,"props":625,"children":626},{},[627],{"type":24,"value":628},"MPI 常用于在计算集群、超算上编写程序，比如很多传统科学计算的并行程序。MPI 接口的兼容性好，通信功能丰富，在深度学习框架中主要用于 CPU 数据的通信。",{"type":18,"tag":26,"props":630,"children":631},{},[632],{"type":24,"value":633},"MPI是一个开放接口，有多种实现的库，一种广泛使用的开源实现是 Open MPI。一些硬件厂商也提供针对硬件优化的实现。",{"type":18,"tag":26,"props":635,"children":636},{},[637],{"type":18,"tag":38,"props":638,"children":639},{},[640],{"type":24,"value":641},"NVIDIA Collective Communication Library (NCCL)",{"type":18,"tag":26,"props":643,"children":644},{},[645],{"type":24,"value":646},"NCCL 英伟达集合通信库，是一个专用于多个 GPU 乃至多个节点间通信的实现。它专为英伟达的计算卡和网络优化，能带来更低的延迟和更高的带宽。",{"type":18,"tag":26,"props":648,"children":649},{},[650],{"type":24,"value":651},"NCCL 也提供了较丰富的通信功能，接口形式上与 MPI 相似，可满足大多数深度学习任务的通信需求。它在深度学习框架中专用于 GPU 数据的通信。因为NCCL则是NVIDIA基于自身硬件定制的，能做到更有针对性且更方便优化，故在英伟达硬件上，NCCL的效果往往比其它的通信库更好。",{"type":18,"tag":26,"props":653,"children":654},{},[655],{"type":18,"tag":38,"props":656,"children":657},{},[658],{"type":24,"value":659},"MPI和NCCL的关系",{"type":18,"tag":26,"props":661,"children":662},{},[663],{"type":24,"value":664},"openMPI的通讯算法和通讯操作原语最晚在2009年就都已经成熟并开源了，而Nvidia在2015年下半年首次公开发布NCCL。",{"type":18,"tag":26,"props":666,"children":667},{},[668],{"type":24,"value":669},"既然openMPI已经实现了这么多All Reduce算法，为什么英伟达还要开发NCCL？是不是从此只要NCCL，不再需要MPI了呢？",{"type":18,"tag":26,"props":671,"children":672},{},[673],{"type":24,"value":674},"NO！",{"type":18,"tag":26,"props":676,"children":677},{},[678],{"type":24,"value":679},"从openMPI的源码里能看到，其完全没有考虑过深度学习的场景，基本没有考虑过GPU系统架构。很明显的一点，MPI中各个工作节点基本视为等同，并没有考虑节点间latency和带宽的不同，所以并不能充分发挥异构场景下的硬件性能。",{"type":18,"tag":26,"props":681,"children":682},{},[683],{"type":24,"value":684},"Nvidia的策略还是比较聪明，不和MPI竞争，只结合硬件做MPI没做好的通信性能优化。在多机多卡分布式训练中，MPI还是广泛用来做节点管理，NCCL只做GPU的实际规约通信。NCCL可以轻松与MPI结合使用，将MPI用于CPU到CPU的通信，将NCCL用于GPU到GPU的通信。",{"type":18,"tag":26,"props":686,"children":687},{},[688],{"type":24,"value":689},"而NCCL的优势就在于完全贴合英伟达自己的硬件，能充分发挥性能。但是基本的算法原理其实相比openMPI里实现的算法是没有太大变化。",{"type":18,"tag":26,"props":691,"children":692},{},[693],{"type":24,"value":694},"NCCL1.x只能在单机内部进行通信，NCCL2.0开始支持多节点（2017年Q2）。所以在NCCL2之前大家还会依赖MPI来进行集合通信。",{"type":18,"tag":26,"props":696,"children":697},{},[698],{"type":18,"tag":38,"props":699,"children":700},{},[701],{"type":24,"value":702},"引用",{"type":18,"tag":704,"props":705,"children":706},"ol",{},[707,713,718,723,728,733,738],{"type":18,"tag":708,"props":709,"children":710},"li",{},[711],{"type":24,"value":712},"分布式集合通信原语 - MindSpore master documentation",{"type":18,"tag":708,"props":714,"children":715},{},[716],{"type":24,"value":717},"Huring：分布式训练与NCCL",{"type":18,"tag":708,"props":719,"children":720},{},[721],{"type":24,"value":722},"兰瑞Frank：腾讯机智团队分享--AllReduce算法的前世今生",{"type":18,"tag":708,"props":724,"children":725},{},[726],{"type":24,"value":727},"OpenMMLab：AI框架基础技术之深度学习中的通信优化",{"type":18,"tag":708,"props":729,"children":730},{},[731],{"type":24,"value":732},"NVIDIA Collective Communications Library (NCCL)",{"type":18,"tag":708,"props":734,"children":735},{},[736],{"type":24,"value":737},"Overview of NCCL",{"type":18,"tag":708,"props":739,"children":740},{},[741],{"type":24,"value":742},"Open MPI: Open Source High Performance Computing",{"type":18,"tag":744,"props":745,"children":747},"h3",{"id":746},"扫描下方二维码加入mindspore项目",[748],{"type":24,"value":749},"扫描下方二维码加入MindSpore项目",{"type":18,"tag":26,"props":751,"children":752},{},[753],{"type":18,"tag":30,"props":754,"children":756},{"alt":7,"src":755},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/09/984c702452434b10817b9084c6a2cee6.jpg",[],{"type":18,"tag":744,"props":758,"children":759},{"id":7},[],{"type":18,"tag":26,"props":761,"children":762},{},[763],{"type":18,"tag":764,"props":765,"children":769},"a",{"href":766,"rel":767},"http://mp.weixin.qq.com/s?__biz=MzAxMDA1MDM0NQ==&mid=2247541446&idx=1&sn=f397c4aadf6313efe8c16d15cec8db3d&chksm=9b542c80ac23a596935839754dc4690c6c0a20319c1bb23c67bc3bd7b527c8cf6d3c1efcc9a9&scene=21#wechat_redirect",[768],"nofollow",[770],{"type":24,"value":771},"MindSpore官方资料",{"type":18,"tag":26,"props":773,"children":774},{},[775],{"type":18,"tag":764,"props":776,"children":778},{"href":766,"rel":777},[768],[779],{"type":24,"value":780},"GitHub : https://github.com/mindspore-ai/mindspore",{"type":18,"tag":26,"props":782,"children":783},{},[784],{"type":18,"tag":764,"props":785,"children":787},{"href":766,"rel":786},[768],[788],{"type":24,"value":789},"Gitee : https : //gitee.com/mindspore/mindspore",{"type":18,"tag":26,"props":791,"children":792},{},[793],{"type":18,"tag":764,"props":794,"children":796},{"href":766,"rel":795},[768],[797],{"type":24,"value":798},"官方QQ群 : 486831414",{"title":7,"searchDepth":800,"depth":800,"links":801},4,[802,804],{"id":746,"depth":803,"text":749},3,{"id":7,"depth":803,"text":7},"markdown","content:technology-blogs:zh:1040.md","content","technology-blogs/zh/1040.md","technology-blogs/zh/1040","md",1776506111518]