[{"data":1,"prerenderedAt":586},["ShallowReactive",2],{"content-query-V1jLsaBNmI":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":580,"_id":581,"_source":582,"_file":583,"_stem":584,"_extension":585},"/technology-blogs/zh/1469","zh",false,"","【MLSys 2022】论文介绍：图算融合Apollo:-基于多层规约的自动算子融合优化框架","图算融合Apollo通过多层规约融合架构，实现了不同融合方式的结合，并获得显著的融合性能收益。这种融合架构设计具有较好的扩展性和弹性。","2022-05-11","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/05/11/2c4dd75ba29547d489adc636312f4e42.png","technology-blogs","大V博文",{"type":15,"children":16,"toc":571},"root",[17,25,31,44,80,93,106,123,131,144,153,161,169,179,187,204,221,229,237,249,257,265,276,284,292,300,308,316,324,332,340,348,356,371,379,390,405,413,430,438,449,457,464,472,489,499,509,517,525,533,541,549,557],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mlsys-2022论文介绍图算融合apollo-基于多层规约的自动算子融合优化框架",[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],{"type":24,"value":35},"文章来源：",{"type":18,"tag":37,"props":38,"children":42},"a",{"href":39,"rel":40},"https://zhuanlan.zhihu.com/p/511821386",[41],"nofollow",[43],{"type":24,"value":39},{"type":18,"tag":26,"props":45,"children":46},{},[47],{"type":18,"tag":48,"props":49,"children":50},"strong",{},[51,53,60,62,69,71,78],{"type":24,"value":52},"MLSys 2022放出的",{"type":18,"tag":37,"props":54,"children":57},{"href":55,"rel":56},"https://link.zhihu.com/?target=https://proceedings.mlsys.org/paper/2022",[41],[58],{"type":24,"value":59},"Pre-proceedings论文列表",{"type":24,"value":61},"中的第一篇论文",{"type":18,"tag":37,"props":63,"children":66},{"href":64,"rel":65},"https://link.zhihu.com/?target=https://proceedings.mlsys.org/paper/2022/hash/069059b7ef840f0c74a814ec9237b6ec-Abstract.html",[41],[67],{"type":24,"value":68},"Apollo",{"type":24,"value":70},"是MindSpore图算融合团队和赵捷老师(",{"type":18,"tag":37,"props":72,"children":75},{"href":73,"rel":74},"https://www.zhihu.com/people/f3edc0b3c8221a4fa224169746ae9ed0",[41],[76],{"type":24,"value":77},"@要术甲杰",{"type":24,"value":79},")、陈雷老师(港科大)合作出品的；同时这篇论文也是MindSpore团队与赵捷老师合作的第三篇论文，前两篇论文分别发布在Micro 2020、PLDI 2021：",{"type":18,"tag":26,"props":81,"children":82},{},[83],{"type":18,"tag":48,"props":84,"children":85},{},[86],{"type":18,"tag":37,"props":87,"children":90},{"href":88,"rel":89},"https://zhuanlan.zhihu.com/p/384191216",[41],[91],{"type":24,"value":92},"金雪锋：PLDI 2021论文分析(一)：AKG-NPU上算子自动生成技术探索",{"type":18,"tag":26,"props":94,"children":95},{},[96],{"type":18,"tag":48,"props":97,"children":98},{},[99],{"type":18,"tag":37,"props":100,"children":103},{"href":101,"rel":102},"https://zhuanlan.zhihu.com/p/333394142",[41],[104],{"type":24,"value":105},"量子位：53年来国内唯三，MindSpore加速昇腾芯片论文获国际顶会MICRO最佳论文提名",{"type":18,"tag":26,"props":107,"children":108},{},[109],{"type":18,"tag":48,"props":110,"children":111},{},[112,114,121],{"type":24,"value":113},"前两篇论文主要**于自动算子编译技术，而Apollo则主要是围绕自动算子融合优化展开。当前，这些对应技术已经全部落地MindSpore，并共同支撑了",{"type":18,"tag":37,"props":115,"children":118},{"href":116,"rel":117},"https://link.zhihu.com/?target=https://www.mindspore.cn/docs/zh-CN/r1.7/design/graph_kernel_fusion_engine.html",[41],[119],{"type":24,"value":120},"图算融合加速引擎",{"type":24,"value":122},"这个重要性能优化特性。当然，我们的所有代码实现也是全部开源，欢迎小伙伴们围观讨论。",{"type":18,"tag":26,"props":124,"children":125},{},[126],{"type":18,"tag":48,"props":127,"children":128},{},[129],{"type":24,"value":130},"从另外一方面，以上论文历程也基本代表了MindSpore图算融合团队近几年所走过的技术历程：先从自动算子调度生成起步，然后逐步延伸到自动算子融合优化领域，最后进行二者的技术整合和整体协同创新。对于一个团队来说，一个技术方向能够持续投入3年以上，特别是这个技术在中间很长一段时间可能都没有产出，还是需要很大决心和耐心的。所以很庆幸能够一路坚持下来，逐步将图算融合打造成为国内首个全自研的自动算子融合加速引擎。并在一些关键性能上大幅超越XLA，具体的论文的介绍参见：",{"type":18,"tag":26,"props":132,"children":133},{},[134],{"type":18,"tag":48,"props":135,"children":136},{},[137],{"type":18,"tag":37,"props":138,"children":141},{"href":139,"rel":140},"https://zhuanlan.zhihu.com/p/350343159",[41],[142],{"type":24,"value":143},"国产深度学习框架MindSpore训练性能评测 —— by 中科大ADSL实验室",{"type":18,"tag":145,"props":146,"children":148},"h2",{"id":147},"算子融合技术发展",[149],{"type":18,"tag":48,"props":150,"children":151},{},[152],{"type":24,"value":147},{"type":18,"tag":26,"props":154,"children":155},{},[156],{"type":18,"tag":48,"props":157,"children":158},{},[159],{"type":24,"value":160},"算子融合优化一直是AI网络最重要的单卡性能优化手段，也是最容易立竿见影的性能优化手段。从硬件性能角度看，算子融合主要是解决AI处理器所面临的两方面的性能难题：内存墙和并行墙问题。这也是不同AI处理器中普遍性存在的两个关键性性能问题。",{"type":18,"tag":26,"props":162,"children":163},{},[164],{"type":18,"tag":48,"props":165,"children":166},{},[167],{"type":24,"value":168},"内存墙问题主要是访存瓶颈引起。算子融合主要通过对计算图上存在数据依赖的“生产者-消费者”算子进行融合，从而提升中间Tensor数据的访存局部性，以此来解决内存墙问题。这种融合技术也统称为“Buffer融合”。在很长一段时间，Buffer融合一直是算子融合的主流技术。",{"type":18,"tag":26,"props":170,"children":171},{},[172,174],{"type":24,"value":173},"**早期的AI框架，主要通过手工方式实现固定Pattern的Buffer融合。首先通过手工编写融合算子实现，然后再手工实现对应的融合Pass，进行融合pattern匹配和替换修改。但随着不同AI模型的快速演进和多样化，大家很快发现这种方式的不足：**无法泛化！",{"type":18,"tag":48,"props":175,"children":176},{},[177],{"type":24,"value":178},"即只能针对特定网络中特定算子组合Pattern进行融合优化。面对网络结构越来越复杂、越来越多样化的快速发展趋势，这种方式只能望洋兴叹了。",{"type":18,"tag":26,"props":180,"children":181},{},[182],{"type":18,"tag":48,"props":183,"children":184},{},[185],{"type":24,"value":186},"为了解决手工融合无法泛化的问题，以XLA、TVM、MLIR等为代表的AI编译技术技术/框架开始转向自动Buffer融合优化技术。从算子生成角度看，早期的自动Buffer融合基本是等价于Loop Fusion，即把相邻的存在数据依赖的算子进行Loop空间深度融合，使得中间tensor数据访问退化为局部变量、甚至寄存器变量，从而大幅减少访存开销。",{"type":18,"tag":26,"props":188,"children":189},{},[190],{"type":18,"tag":48,"props":191,"children":192},{},[193,195,202],{"type":24,"value":194},"Loop Fusion虽然优化效果显著，但它有一个明显的限制：相邻算子节点是否可融合受限于待融合算子的Loop循环是否可以进行有效的循环合并。这个限制条件的存在，使得融合子图规模难以进一步放大。但随着AI芯片计算能力的快速发展，内存墙问题越来越突出，这个限制条件逐渐成为限制融合技术发展的关键因素。为了突破这个限制条件，阿里的小伙伴们提出了非常棒的",{"type":18,"tag":37,"props":196,"children":199},{"href":197,"rel":198},"https://zhuanlan.zhihu.com/p/475898491",[41],[200],{"type":24,"value":201},"AStitch",{"type":24,"value":203},"技术，使用Stitch的方式将相互依赖算子通过层次化的存储媒介进一步“缝合”在一起。而不再依赖融合算子循环空间的深度合并。",{"type":18,"tag":26,"props":205,"children":206},{},[207],{"type":18,"tag":48,"props":208,"children":209},{},[210,212,219],{"type":24,"value":211},"随着",{"type":18,"tag":37,"props":213,"children":216},{"href":214,"rel":215},"https://link.zhihu.com/?target=https://dl.acm.org/doi/abs/10.5555/3488766.3488816",[41],[217],{"type":24,"value":218},"Rammer",{"type":24,"value":220},"的发布，通过算子融合解决并行墙问题进入大家的视野。并行墙问题主要是由于芯片多核增加与单算子多核并行度不匹配引起。随着芯片并行核数的快速堆叠，AI网络单个算子节点的并行度难以有效利用这么多多核资源。在推理场景这个问题更加明显。为此，Rammer提出一种将计算图中的算子节点进行并行编排，从而提升整体计算并行度。特别是对于网络中存在可并行的分支节点，这种方式可以获得较好的并行加速效果。为了区别于Buffer融合，这种融合方式我们姑且称为 “并行融合”吧。",{"type":18,"tag":26,"props":222,"children":223},{},[224],{"type":18,"tag":48,"props":225,"children":226},{},[227],{"type":24,"value":228},"如上所述，当前的自动算子融合优化技术本质上走上了两条有点割裂的技术方向：用于解决内存墙的Buffer融合以及用于解决并行墙的并行融合。那么，我们是否有可能把这些不同的融合优化技术进行有机结合，发挥各自的优势，从而达到更优的融合优化效果呢？ 这个问题正是Apollo（图算融合）试图解决的核心问题。Apollo为此提出了“多层规约融合”优化技术，完美地把Loop Fusion、Stitch Fusion、并行融合等不同优化角度的融合技术纳入统一的框架之下，并实现了非常好的融合优化效果。",{"type":18,"tag":26,"props":230,"children":231},{},[232],{"type":18,"tag":48,"props":233,"children":234},{},[235],{"type":24,"value":236},"将以上所述的算子融合技术演进和发展历程做个简单总结，大概如下图所示：",{"type":18,"tag":26,"props":238,"children":239},{},[240],{"type":18,"tag":48,"props":241,"children":242},{},[243],{"type":18,"tag":244,"props":245,"children":248},"img",{"alt":246,"src":247},"1_看图王.jpg","https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20225/11/1652232326359188387.jpg",[],{"type":18,"tag":145,"props":250,"children":252},{"id":251},"多层规约融合优化",[253],{"type":18,"tag":48,"props":254,"children":255},{},[256],{"type":24,"value":251},{"type":18,"tag":26,"props":258,"children":259},{},[260],{"type":18,"tag":48,"props":261,"children":262},{},[263],{"type":24,"value":264},"为了实现不同算子融合方式的协同组合，Apollo设计了一个开放式多层规约式融合架构（如下图所示）。将不同的融合方式实现为不同的Layer，然后通过对不同Layer进行逐层规约合并，从而得到最终的融合算子子图，并获得最佳的融合性能收益。",{"type":18,"tag":26,"props":266,"children":267},{},[268],{"type":18,"tag":48,"props":269,"children":270},{},[271],{"type":18,"tag":244,"props":272,"children":275},{"alt":273,"src":274},"2.jpg","https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20225/11/1652232056703739796.jpg",[],{"type":18,"tag":26,"props":277,"children":278},{},[279],{"type":18,"tag":48,"props":280,"children":281},{},[282],{"type":24,"value":283},"在分层规约融合之前，我们首先通过Partition将输入的计算图拆为为最基本的micro-graphs。将每个micro-graph作为最基本的算子schedule单位。然后以micro-graph为起点，进行多层规约融合。在Apollo论文中，我们介绍了3层设计，分别对应融合功能：",{"type":18,"tag":26,"props":285,"children":286},{},[287],{"type":18,"tag":48,"props":288,"children":289},{},[290],{"type":24,"value":291},"layer I：通过Polyhedral自动schedule技术对micro-graph进行Loop Fusion和schedule优化；",{"type":18,"tag":26,"props":293,"children":294},{},[295],{"type":18,"tag":48,"props":296,"children":297},{},[298],{"type":24,"value":299},"layer II：在layer I得到的融合算子IR基础上，识别其中存在数据依赖、且可以做进一步Stitch Fusion的融合算子IR，然后进行Stitch Fusion的规约合并；",{"type":18,"tag":26,"props":301,"children":302},{},[303],{"type":18,"tag":48,"props":304,"children":305},{},[306],{"type":24,"value":307},"layer III：在layer II得到的融合算子IR基础上，识别其中的无依赖融合算子IR，然后根据并行规则，然后进行并行融合的规约合并。",{"type":18,"tag":26,"props":309,"children":310},{},[311],{"type":18,"tag":48,"props":312,"children":313},{},[314],{"type":24,"value":315},"通过以上多层次规约合并之后，将最终得到的融合算子IR进行codegen，并生成对应融合算子kernel。",{"type":18,"tag":26,"props":317,"children":318},{},[319],{"type":18,"tag":48,"props":320,"children":321},{},[322],{"type":24,"value":323},"以上多层规约融合依赖于灵活的IR合并以及修改，所以IR选择是一个非常重要的设计考虑。在算子层，考虑到与AKG的对接需求，我们采用了与AKG一致的Halide IR。Halide IR在成熟度以及灵活性还是非常有优势，也有非常丰富的功能基础设施，非常方便我们实现不同融合算子的IR规约合并等修改。在图层IR选择上，我们没有选择类似XLA的独立HLO IR，而是采用了更简单的、直接复用MindSpore IR（MindIR）的设计思路。这种IR原生设计为我们接下来的工作带来了诸多好处，比如：",{"type":18,"tag":26,"props":325,"children":326},{},[327],{"type":18,"tag":48,"props":328,"children":329},{},[330],{"type":24,"value":331},"1）直接消减了MindSpore前端框架向Apollo的IR映射和转换工作；",{"type":18,"tag":26,"props":333,"children":334},{},[335],{"type":18,"tag":48,"props":336,"children":337},{},[338],{"type":24,"value":339},"2）方便白盒化方式不同层的融合策略分析以及子图合并等；",{"type":18,"tag":26,"props":341,"children":342},{},[343],{"type":18,"tag":48,"props":344,"children":345},{},[346],{"type":24,"value":347},"3）直接复用MindSpore的已有公共优化和基础设施，大大减少代码开发量。",{"type":18,"tag":145,"props":349,"children":351},{"id":350},"技术效果与典型应用",[352],{"type":18,"tag":48,"props":353,"children":354},{},[355],{"type":24,"value":350},{"type":18,"tag":26,"props":357,"children":358},{},[359],{"type":18,"tag":48,"props":360,"children":361},{},[362,364,369],{"type":24,"value":363},"如前所述，图算融合Apollo已经完全落地MindSpore，并作为",{"type":18,"tag":37,"props":365,"children":367},{"href":116,"rel":366},[41],[368],{"type":24,"value":120},{"type":24,"value":370},"功能供用户使用。在MindSpore的一些具体网络模型中，Apollo发挥了关键的性能优化作用。",{"type":18,"tag":26,"props":372,"children":373},{},[374],{"type":18,"tag":48,"props":375,"children":376},{},[377],{"type":24,"value":378},"在MindSpore 1.3版本发布在Bert GPU网络中，Apollo实现3.1~3.6倍的性能加速，并实现了对XLA的整体领先。",{"type":18,"tag":26,"props":380,"children":381},{},[382],{"type":18,"tag":48,"props":383,"children":384},{},[385],{"type":18,"tag":244,"props":386,"children":389},{"alt":387,"src":388},"3.jpg","https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20225/11/1652232069324617589.jpg",[],{"type":18,"tag":26,"props":391,"children":392},{},[393],{"type":18,"tag":48,"props":394,"children":395},{},[396,398,403],{"type":24,"value":397},"（图片引自：",{"type":18,"tag":37,"props":399,"children":401},{"href":139,"rel":400},[41],[402],{"type":24,"value":143},{"type":24,"value":404},"）",{"type":18,"tag":26,"props":406,"children":407},{},[408],{"type":18,"tag":48,"props":409,"children":410},{},[411],{"type":24,"value":412},"在MindSpore 1.6版本发布的GNN网络中，Apollo实现了3~4倍的性能加速。值得一提的是，针对GNN网络特点，我们基于Apollo的多层规约融合框架，实现了基于索引的非规则内存访问算子融合优化技术。自动识别GNN模型运行任务特有执行pattern并进行融合和kernel level优化。相较于其他框架对常用算子进行定制优化的方案更加灵活，更具扩展性，能够覆盖现有框架中已有的算子和新组合算子的融合优化。也间接证明的Apollo多层规约融合框架的良好扩展性。",{"type":18,"tag":26,"props":414,"children":415},{},[416],{"type":18,"tag":48,"props":417,"children":418},{},[419,421,428],{"type":24,"value":420},"参考：",{"type":18,"tag":37,"props":422,"children":425},{"href":423,"rel":424},"https://zhuanlan.zhihu.com/p/478547185",[41],[426],{"type":24,"value":427},"跳票了很长时间的MindSpore1.6",{"type":24,"value":429}," 。",{"type":18,"tag":26,"props":431,"children":432},{},[433],{"type":18,"tag":48,"props":434,"children":435},{},[436],{"type":24,"value":437},"另外，我们也对多达50+网络做了广泛的验证。Apollo平均性能提升：NLP类96.4%↑；推荐类136.6%↑; CV类30.7%↑。",{"type":18,"tag":26,"props":439,"children":440},{},[441],{"type":18,"tag":48,"props":442,"children":443},{},[444],{"type":18,"tag":244,"props":445,"children":448},{"alt":446,"src":447},"4.jpg","https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20225/11/1652232135248514502.jpg",[],{"type":18,"tag":145,"props":450,"children":452},{"id":451},"总结和展望",[453],{"type":18,"tag":48,"props":454,"children":455},{},[456],{"type":24,"value":451},{"type":18,"tag":26,"props":458,"children":459},{},[460],{"type":18,"tag":48,"props":461,"children":462},{},[463],{"type":24,"value":9},{"type":18,"tag":26,"props":465,"children":466},{},[467],{"type":18,"tag":48,"props":468,"children":469},{},[470],{"type":24,"value":471},"未来随着算力的快速发展，并受限于摩尔定律等工艺限制，AI芯片的内存墙以及并行墙问题会越加凸显。Apollo的多层规约融合是我们试图解决这个问题所做的系统性技术尝试。当然这个思路还是有很多待改进之处，希望与业界同行以及小伙伴进行更多的交流以及讨论！",{"type":18,"tag":26,"props":473,"children":474},{},[475],{"type":18,"tag":48,"props":476,"children":477},{},[478,480,487],{"type":24,"value":479},"最后也要感谢已经离开团队的淡孝强（ ",{"type":18,"tag":37,"props":481,"children":484},{"href":482,"rel":483},"https://www.zhihu.com/people/f97b503d6d11a2fdb34cad834ec48ed0",[41],[485],{"type":24,"value":486},"@dxq",{"type":24,"value":488}," ）在这里面的贡献！",{"type":18,"tag":26,"props":490,"children":491},{},[492,494],{"type":24,"value":493},"****广告1：",{"type":18,"tag":48,"props":495,"children":496},{},[497],{"type":24,"value":498},"MindSpore图算融合是一支学术研究与商业落地并重的团队，目前还有很多技术问题尚待攻克。欢迎对算子编译融合感兴趣的同学投递简历加入我们（请私信~）！",{"type":18,"tag":26,"props":500,"children":501},{},[502,504],{"type":24,"value":503},"****广告2：",{"type":18,"tag":48,"props":505,"children":506},{},[507],{"type":24,"value":508},"欢迎对MindSpore和图算融合技术感兴趣的团队或个人参与我们的社区开发，与我们一起进步！",{"type":18,"tag":145,"props":510,"children":512},{"id":511},"参考文献",[513],{"type":18,"tag":48,"props":514,"children":515},{},[516],{"type":24,"value":511},{"type":18,"tag":26,"props":518,"children":519},{},[520],{"type":18,"tag":48,"props":521,"children":522},{},[523],{"type":24,"value":524},"[1] Tianqi Chen, Thierry Moreau, Ziheng Jiang, Lianmin Zheng, Eddie Yan, Haichen Shen, Meghan Cowan, Leyuan Wang, Yuwei Hu, Luis Ceze, et al. TVM: An automated end-to-end optimizing compiler for deep learning. OSDI'18.",{"type":18,"tag":26,"props":526,"children":527},{},[528],{"type":18,"tag":48,"props":529,"children":530},{},[531],{"type":24,"value":532},"[2] Zhen Zheng, Xuanda Yang Pengzhan Zhao, Guoping Long，Kai Zhu, Feiwen Zhu, Wenyi Zhao, Xiaoyong Liu, Jun Yang, Jidong Zhai, et al. AStitch: Enabling a New Multi-dimensional Optimization Space for Memory-Intensive ML Training and Inference on Modern SIMT Architectures. ASPLOS 2022.",{"type":18,"tag":26,"props":534,"children":535},{},[536],{"type":18,"tag":48,"props":537,"children":538},{},[539],{"type":24,"value":540},"[3] Lingxiao Ma, Zhiqiang Xie et al. Rammer：Rammer: Enabling Holistic Deep Learning Compiler Optimizations with rTasks. OSDI 2020.",{"type":18,"tag":26,"props":542,"children":543},{},[544],{"type":18,"tag":48,"props":545,"children":546},{},[547],{"type":24,"value":548},"[4] Jie Zhao, Bojie Li, et al. 2021. AKG: Automatic Kernel Generation for Neural Processing Units using Polyhedral Transformations. PLDI 2021.",{"type":18,"tag":26,"props":550,"children":551},{},[552],{"type":18,"tag":48,"props":553,"children":554},{},[555],{"type":24,"value":556},"[5] Jie Zhao,Peng Di. 2020. Optimizing the Memory Hierarchy by Compositing Automatic Transformations on Computations and Data. MICRO-53.",{"type":18,"tag":26,"props":558,"children":559},{},[560],{"type":18,"tag":48,"props":561,"children":562},{},[563,565],{"type":24,"value":564},"[6] 图算融合加速引擎介绍. ",{"type":18,"tag":37,"props":566,"children":569},{"href":567,"rel":568},"https://www.mindspore.cn/docs/zh-CN/r1.8/index.html",[41],[570],{"type":24,"value":567},{"title":7,"searchDepth":572,"depth":572,"links":573},4,[574,576,577,578,579],{"id":147,"depth":575,"text":147},2,{"id":251,"depth":575,"text":251},{"id":350,"depth":575,"text":350},{"id":451,"depth":575,"text":451},{"id":511,"depth":575,"text":511},"markdown","content:technology-blogs:zh:1469.md","content","technology-blogs/zh/1469.md","technology-blogs/zh/1469","md",1776506112613]