[{"data":1,"prerenderedAt":463},["ShallowReactive",2],{"content-query-diOWqhjKTa":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":457,"_id":458,"_source":459,"_file":460,"_stem":461,"_extension":462},"/technology-blogs/zh/454","zh",false,"","动态路由条件计算-万亿级参数超大模型关键技术与MindSpore支持（1）","万亿级参数超大模型关键技术与MindSpore支持-技术介绍","2021-04-29","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/04/29/7e92941da8f1485ba10e47c7679af6d7.jpg","technology-blogs","大V博文",{"type":15,"children":16,"toc":454},"root",[17,25,31,56,67,72,77,93,101,105,113,118,123,128,133,138,143,148,153,158,174,179,184,192,197,202,210,223,228,233,238,243,251,256,261,269,274,279,284,292,297,302,307,312,317,322,331,344,349,354,359,364,369,374,379,384,389,394,399,404,409,414,419,424,429,434,439,444,449],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"动态路由条件计算-万亿级参数超大模型关键技术与mindspore支持1",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":24,"value":30},"作者：金雪锋",{"type":18,"tag":26,"props":32,"children":33},{},[34,36,45,47,54],{"type":24,"value":35},"作者主页：[https : //",{"type":18,"tag":37,"props":38,"children":42},"a",{"href":39,"rel":40},"http://www.zhihu.com/people/jin-xue-feng%5D(https",[41],"nofollow",[43],{"type":24,"value":44},"www.zhihu.com/people/jin-xue-feng](https",{"type":24,"value":46}," : //",{"type":18,"tag":37,"props":48,"children":51},{"href":49,"rel":50},"http://www.zhihu.com/people/jin-xue-feng",[41],[52],{"type":24,"value":53},"www.zhihu.com/people/jin-xue-feng",{"type":24,"value":55},")",{"type":18,"tag":26,"props":57,"children":58},{},[59,61],{"type":24,"value":60},"文章来源：",{"type":18,"tag":37,"props":62,"children":65},{"href":63,"rel":64},"https://zhuanlan.zhihu.com/p/363702998",[41],[66],{"type":24,"value":63},{"type":18,"tag":26,"props":68,"children":69},{},[70],{"type":24,"value":71},"最近项目组分析了类Switch Transformer的动态路由条件计算的模型，特分享出来，供大家参考。",{"type":18,"tag":26,"props":73,"children":74},{},[75],{"type":24,"value":76},"主要分两部分，本次先介绍第一部分：",{"type":18,"tag":26,"props":78,"children":79},{},[80,86,88],{"type":18,"tag":81,"props":82,"children":83},"strong",{},[84],{"type":24,"value":85},"1",{"type":24,"value":87},".",{"type":18,"tag":81,"props":89,"children":90},{},[91],{"type":24,"value":92},"动态路由条件计算-技术介绍",{"type":18,"tag":26,"props":94,"children":95},{},[96],{"type":18,"tag":81,"props":97,"children":98},{},[99],{"type":24,"value":100},"2.动态路由条件计算-算法实现",{"type":18,"tag":26,"props":102,"children":103},{},[104],{"type":24,"value":92},{"type":18,"tag":19,"props":106,"children":108},{"id":107},"前言",[109],{"type":18,"tag":81,"props":110,"children":111},{},[112],{"type":24,"value":107},{"type":18,"tag":26,"props":114,"children":115},{},[116],{"type":24,"value":117},"近来，增大模型规模成为了提升模型性能的主要手段。特别是NLP领域的自监督预训练语言模型，规模越来越大，从GPT3的1750亿参数，到Switch Transformer的16000亿参数，又是一个数量级的增加。",{"type":18,"tag":26,"props":119,"children":120},{},[121],{"type":24,"value":122},"模型规模的数量级的增大，虽然取得了一定程度的性能提升，甚至产生了某些意想不到的“神奇”效果（如GPT3），但其背后的计算开销成了最大的问题，比如GPT3训练使用了万级的GPU和数周的训练时间。",{"type":18,"tag":26,"props":124,"children":125},{},[126],{"type":24,"value":127},"如何既能利用超大规模的参数来提升模型表达和性能，又能控制计算量的较小的增加，成为了最主要的挑战之一。",{"type":18,"tag":26,"props":129,"children":130},{},[131],{"type":24,"value":132},"以MoE为代表的动态神经网络技术被重点引入。",{"type":18,"tag":26,"props":134,"children":135},{},[136],{"type":24,"value":137},"大脑是典型的低能耗高效率的计算模式，稀疏激活是最重要的特性。",{"type":18,"tag":26,"props":139,"children":140},{},[141],{"type":24,"value":142},"除了巨型模型在训练推理特别是训练时的计算效能挑战外，当前巨型模型的训练优化算法另一个更大的挑战是（不在此处讨论），BP算法是当前最为可用的深度网络优化，但更理想的优化算法需要高并行、优化过程非对称、并能够在时空维度通过局部持续优化完成整体优化。",{"type":18,"tag":26,"props":144,"children":145},{},[146],{"type":24,"value":147},"1、传统的神经网络模型，前馈的时候，输入的batch中，每一个样本的处理，都将激活网络中的每一个参数参与计算。",{"type":18,"tag":26,"props":149,"children":150},{},[151],{"type":24,"value":152},"2、条件计算最宽松的定义，指仅激活网络中某些部分的一类算法。Conditional Computation refers to a class of algorithms that activate only some of the different parts in a network. 在具体某类条件计算实现中，条件选择模式，可能按照输入的batch中每sample独立激活网络不同部分，可能按照输入数据空间上不同的部分（比如image不同区域或者channel），可能按照输入数据时间上不同的部分（比如time series的不同slide window或者video的不同的frame。），可能按照目标任务的不同每task独立的，可能按照非可学习的固定的随机分配不同的子网独立计算。",{"type":18,"tag":26,"props":154,"children":155},{},[156],{"type":24,"value":157},"3、对不同的输入（原始或者前层），按照一定条件，选择性的执行后续部分网络的计算，这个技术下，有一些近似或相关的技术，如：dynamic neural network(s), conditional computing, conditional activation, sparse activating, selective execution, mixture of experts (MoE), dynamic routing, …；强相关的一些模型比如 Switch Transformer等。",{"type":18,"tag":19,"props":159,"children":161},{"id":160},"条件计算的分类广义",[162,167,169],{"type":18,"tag":81,"props":163,"children":164},{},[165],{"type":24,"value":166},"条件计算的分类",{"type":24,"value":168},"（广义）",{"type":18,"tag":81,"props":170,"children":171},{},[172],{"type":24,"value":173},"：",{"type":18,"tag":26,"props":175,"children":176},{},[177],{"type":24,"value":178},"1、按照routing是否可学习可以分为：learnable routing conditional computation和 unlearnable routing conditional computation.",{"type":18,"tag":26,"props":180,"children":181},{},[182],{"type":24,"value":183},"2、按照activation是否不执行non-activation计算，可以分为：hard conditional computation和soft conditional computation。对于hard-mode的条件计算，通过tensor挑选切分等操作，无论何种条件选择模式，不需要激活的数据将完全不参与不激活的网络部分的计算；soft-mode的条件计算，可能仅采取将相关数据置零等方式来避免产生计算效果，但还是和不需要激活网路部分实际执行计算过程。",{"type":18,"tag":19,"props":185,"children":187},{"id":186},"条件计算的主要优势",[188],{"type":18,"tag":81,"props":189,"children":190},{},[191],{"type":24,"value":186},{"type":18,"tag":26,"props":193,"children":194},{},[195],{"type":24,"value":196},"1.计算有效，降低能耗：通过部分激活部分计算，以每样本条件激活的条件计算为例，单个样本只需要经过整个SuperNet的一部分参与计算。",{"type":18,"tag":26,"props":198,"children":199},{},[200],{"type":24,"value":201},"2.更大网络，表达更强：由于一处到多处的Route，各处（层）的Input被路由到不同的子网独立计算，不同的输入的相互在各层的表达相对独立没有影响，表达能力更强，网络可以更大，但表达效率降低了。",{"type":18,"tag":19,"props":203,"children":205},{"id":204},"条件计算的网络和计算形式",[206],{"type":18,"tag":81,"props":207,"children":208},{},[209],{"type":24,"value":204},{"type":18,"tag":26,"props":211,"children":212},{},[213,215,221],{"type":24,"value":214},"条件计算的网络和计算形式比较灵活，部分构建形式如：（此处省略具体模型和论文引用，参见",{"type":18,"tag":37,"props":216,"children":219},{"href":217,"rel":218},"https://intellabs.github.io/distiller/conditional_computation.html",[41],[220],{"type":24,"value":217},{"type":24,"value":222},"）",{"type":18,"tag":26,"props":224,"children":225},{},[226],{"type":24,"value":227},"1.按照CV等task的特点，用多个独立的CNN作为expert网络，按照task来独立路由，尾部组合后给一个大网络。",{"type":18,"tag":26,"props":229,"children":230},{},[231],{"type":24,"value":232},"2.使用更复杂的cascading等形式组合不同层级的不同的expert网络。",{"type":18,"tag":26,"props":234,"children":235},{},[236],{"type":24,"value":237},"3.通过决策树等方法做数据变换实现路由。",{"type":18,"tag":26,"props":239,"children":240},{},[241],{"type":24,"value":242},"4.通过可学习的网络来选择路由。其中策略学习的损失有多种构建形式：直接使用分类等任务的主损失，对不同专家的重要性和负载构建损失作为辅助损失等等。",{"type":18,"tag":19,"props":244,"children":246},{"id":245},"条件计算的路由策略",[247],{"type":18,"tag":81,"props":248,"children":249},{},[250],{"type":24,"value":245},{"type":18,"tag":26,"props":252,"children":253},{},[254],{"type":24,"value":255},"1.non-learnable/hard-mode，通过某种确定性策略，如LSH等方式计算路由。",{"type":18,"tag":26,"props":257,"children":258},{},[259],{"type":24,"value":260},"2.learnable-mode，通过可学习网络计算路由。 网络规模可大可小，简单的可学习路由为单层权重：G(x) = P(X*W)，G(x)为路由Gate函数，X为输入， W为通损失函数来度量的可学习路由权重，P为某种挑选函数（如topk, sort等），在实际实现中，X*W的输入与权重计算结果可能作为后续网络的输入信息的一部分，不仅仅利用G(x)来选择路由，则需要对X*W的结果做归一化，更典型的形式则为：G(x)=P(N(X*W))，其中N为表达Normalization函数，如Softmax。",{"type":18,"tag":19,"props":262,"children":264},{"id":263},"条件计算的冗余策略",[265],{"type":18,"tag":81,"props":266,"children":267},{},[268],{"type":24,"value":263},{"type":18,"tag":26,"props":270,"children":271},{},[272],{"type":24,"value":273},"条件计算的冗余策略，可分为无冗余条件计算和冗余条件计算：",{"type":18,"tag":26,"props":275,"children":276},{},[277],{"type":24,"value":278},"1、无冗余条件计算可通过P(.)函数的实现如topk(k=1,…)来实现；",{"type":18,"tag":26,"props":280,"children":281},{},[282],{"type":24,"value":283},"2、冗余条件计算，可以多种实现形式，可以通过P(.)函数的实现如topk(k=n,…)，n>=2来实现，也可以通过硬冗余模式，整个网络中支持输入的复制和多路计算实现。",{"type":18,"tag":19,"props":285,"children":287},{"id":286},"条件计算的挑战",[288],{"type":18,"tag":81,"props":289,"children":290},{},[291],{"type":24,"value":286},{"type":18,"tag":26,"props":293,"children":294},{},[295],{"type":24,"value":296},"1、路由算法对模型质量的影响",{"type":18,"tag":26,"props":298,"children":299},{},[300],{"type":24,"value":301},"无论输入和路由权重作用的信息（X*W），是仅作为路由选择并作为后续网络单元的输入，还是直接作为后续网络单元的输入的一部分，路由算法决定了输入信息的处理流向，对模型的整体质量都有很大影响。",{"type":18,"tag":26,"props":303,"children":304},{},[305],{"type":24,"value":306},"2、路由(routing)/门(gate)的稳定性",{"type":18,"tag":26,"props":308,"children":309},{},[310],{"type":24,"value":311},"随机初始化的路由/门的权重，权重自身在不断被训练调整；在前后层的网络持续训练变化，同一样本在训练的不同阶段会被分派到不同的后续网络单元中，这种动态变化过于剧烈，将严重影响整个网络训练过程的稳定性和收敛速度。",{"type":18,"tag":26,"props":313,"children":314},{},[315],{"type":24,"value":316},"3、路由的专家样本重要性和负载的平衡性",{"type":18,"tag":26,"props":318,"children":319},{},[320],{"type":24,"value":321},"训练阶段，每专家和样本批次中样本的关联度重要性，和每批次中样本被均衡分派到不同专家的负载平衡性，这两个指标既相关又冲突。需要分别构建损失函数作为辅助损失，来优化这两个指标。在arxiv:1701.06538《Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer》做了相关讨论。",{"type":18,"tag":19,"props":323,"children":325},{"id":324},"关于条件计算动态神经网络更多的信息",[326],{"type":18,"tag":81,"props":327,"children":328},{},[329],{"type":24,"value":330},"关于条件计算/动态神经网络，更多的信息。",{"type":18,"tag":26,"props":332,"children":333},{},[334,336,342],{"type":24,"value":335},"在《Dynamic Neural Networks: A Survey》arxiv:2102.04906 (",{"type":18,"tag":37,"props":337,"children":340},{"href":338,"rel":339},"https://arxiv.org/abs/2102.04906",[41],[341],{"type":24,"value":338},{"type":24,"value":343},")一文中，作者对广义的动态神经网络，将各种动态网络相关的技术按照实例级、时间级、空间级做了分类。",{"type":18,"tag":26,"props":345,"children":346},{},[347],{"type":24,"value":348},"1、Instance-wise Dynamic NN： 逐实例动态，每样本独立激活不同的网络和参数（MoE为这个方向）。",{"type":18,"tag":26,"props":350,"children":351},{},[352],{"type":24,"value":353},"Dynamic Architecture：Dynamic Depth、Dynamic Width、Dynamic Routing/MoE；",{"type":18,"tag":26,"props":355,"children":356},{},[357],{"type":24,"value":358},"Dynamic Parameter：Parameter Adjustment、Parameter Prediction、Dynamic Feature(s)",{"type":18,"tag":26,"props":360,"children":361},{},[362],{"type":24,"value":363},"2、Spatial-wise Dynamic NN：空间级动态：图像等不同空间位置激活后续不同网络和参数。",{"type":18,"tag":26,"props":365,"children":366},{},[367],{"type":24,"value":368},"(CNN等)：Pixel Level、Region Level、Resolution Level",{"type":18,"tag":26,"props":370,"children":371},{},[372],{"type":24,"value":373},"3、Temporal-wise Dynamic NN：时间级动态：时序数据按时序维切分激活后续不同网络和参数。（video-frames, text-sequence, time-series, stream, ...)",{"type":18,"tag":26,"props":375,"children":376},{},[377],{"type":24,"value":378},"Text-Sequence",{"type":18,"tag":26,"props":380,"children":381},{},[382],{"type":24,"value":383},"Video-Frames",{"type":18,"tag":26,"props":385,"children":386},{},[387],{"type":24,"value":388},"上述为该综述论文对Dynamic NN的总体分类。",{"type":18,"tag":26,"props":390,"children":391},{},[392],{"type":24,"value":393},"从超大规模网络动态网络技术支撑角度，高表达能力，低计算代价为主的来考虑分类，从两个维度对动态网络技术分类:",{"type":18,"tag":26,"props":395,"children":396},{},[397],{"type":24,"value":398},"1、按照在前馈计算时是否部分激活:",{"type":18,"tag":26,"props":400,"children":401},{},[402],{"type":24,"value":403},"Hard-Dynamic：在前馈的时候，部分网络绝对不激活参与计算",{"type":18,"tag":26,"props":405,"children":406},{},[407],{"type":24,"value":408},"Soft-Dynamic：在前馈的时候，部分网络经过softmax等gate/route后，通过张量元素置零等方式，失去表达能力，但会参与计算。",{"type":18,"tag":26,"props":410,"children":411},{},[412],{"type":24,"value":413},"2、按照动态激活判定算法的输入：",{"type":18,"tag":26,"props":415,"children":416},{},[417],{"type":24,"value":418},"逐样本级：（在输入层）按照每样本的实例来决定动态网络的后续激活。",{"type":18,"tag":26,"props":420,"children":421},{},[422],{"type":24,"value":423},"亚样本级：（在输入层）样本内时间/空间级激活不同的后续网络单元。一般深度网络，不仅在输入层会被选择性激活执行，在中间层也类似。",{"type":18,"tag":26,"props":425,"children":426},{},[427],{"type":24,"value":428},"其中，智能平台支持Hard-Dynamic逐样本级的动态神经网络，能比较自然的获得网络结构大颗粒的稀疏激活，在超大模型中能实现训练和推理的搞能效。",{"type":18,"tag":26,"props":430,"children":431},{},[432],{"type":24,"value":433},"动态神经网络相比与静态结构的神经网络，在相关研究中，从效能，表达，泛化、鲁棒，可解释等方面做了大量对比研究。从智能平台通过计算成本尽量低的支持超大规模网络来提升模型性能的角度看，Efficiency和Representation最为重要：",{"type":18,"tag":26,"props":435,"children":436},{},[437],{"type":24,"value":438},"1、Efficiency：静态网络“牵一发而动全身”，每一个样本输入整个网络/所有参数都要响应，这对超大网络来取得领先效果的模型能耗挑战太大。",{"type":18,"tag":26,"props":440,"children":441},{},[442],{"type":24,"value":443},"2、Representation: 参数量更大，表达容量更大；但MoE等结构在深度网络的各层特征的表达上，复用降低，每参数的表达效率更低。",{"type":18,"tag":26,"props":445,"children":446},{},[447],{"type":24,"value":448},"后续，将继续分享探讨算法实现。",{"type":18,"tag":26,"props":450,"children":451},{},[452],{"type":24,"value":453},"最后，感谢岳大师的支持",{"title":7,"searchDepth":455,"depth":455,"links":456},4,[],"markdown","content:technology-blogs:zh:454.md","content","technology-blogs/zh/454.md","technology-blogs/zh/454","md",1776506137556]