[{"data":1,"prerenderedAt":721},["ShallowReactive",2],{"content-query-5EgL67feUa":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":715,"_id":716,"_source":717,"_file":718,"_stem":719,"_extension":720},"/technology-blogs/zh/1552","zh",false,"","【自动微分原理三】自动微分的具体实现","通过介绍目前比较热门的方法给大家普及一下自动微分的具体实现","2022-06-15","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/06/22/bc3cd790329a4717aa8e5be97489497d.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":703},"root",[17,25,32,58,63,96,102,107,125,130,141,151,161,166,171,179,183,188,193,198,206,211,221,226,234,239,247,252,260,272,280,290,303,308,312,317,322,327,336,344,352,360,368,376,384,392,397,420,430,443,452,465,470,475,480,485,490,495,500,505,510,517,522,527,532,537,547,565,574,592,597,609,614,619,624,629,634,639,644,649,654,659,664,669,680,691],{"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":29},"h2",{"id":28},"自动微分原理03自动微分的具体实现",[30],{"type":24,"value":31},"【自动微分原理】03.自动微分的具体实现",{"type":18,"tag":33,"props":34,"children":35},"p",{},[36,38,47,49,56],{"type":24,"value":37},"第一篇",{"type":18,"tag":39,"props":40,"children":44},"a",{"href":41,"rel":42},"https://zhuanlan.zhihu.com/p/518198564",[43],"nofollow",[45],{"type":24,"value":46},"自动微分原理",{"type":24,"value":48},"文章中我们大概初步谈了谈从手动微分到自动微分的过程，第二篇",{"type":18,"tag":39,"props":50,"children":53},{"href":51,"rel":52},"https://zhuanlan.zhihu.com/p/518296942",[43],[54],{"type":24,"value":55},"自动微分正反模式",{"type":24,"value":57},"中深入了自动微分的正反向模式具体公式和推导。",{"type":18,"tag":33,"props":59,"children":60},{},[61],{"type":24,"value":62},"实际上第二章了解到正反向模式只是自动微分的原理模式，在实际代码实现的过程，正方向模式只是提供一个原理性的指导，在真正编码过程会有很多细节需要打开，例如如何解析表达式，如何记录反向求导表达式的操作等等。这一篇文章中，ZOMI希望通过介绍目前比较热门的方法给大家普及一下自动微分的具体实现。",{"type":18,"tag":64,"props":65,"children":66},"ul",{},[67,77,86],{"type":18,"tag":68,"props":69,"children":70},"li",{},[71],{"type":18,"tag":39,"props":72,"children":74},{"href":41,"rel":73},[43],[75],{"type":24,"value":76},"【自动微分原理】01. 原理介绍",{"type":18,"tag":68,"props":78,"children":79},{},[80],{"type":18,"tag":39,"props":81,"children":83},{"href":51,"rel":82},[43],[84],{"type":24,"value":85},"【自动微分原理】02. 正反模式",{"type":18,"tag":68,"props":87,"children":88},{},[89],{"type":18,"tag":39,"props":90,"children":93},{"href":91,"rel":92},"https://zhuanlan.zhihu.com/p/520065656",[43],[94],{"type":24,"value":95},"【自动微分原理】03. 具体实现",{"type":18,"tag":97,"props":98,"children":100},"h3",{"id":99},"自动微分实现",[101],{"type":24,"value":99},{"type":18,"tag":33,"props":103,"children":104},{},[105],{"type":24,"value":106},"了解自动微分的不同实现方式非常有用。在这里呢，我们将介绍主要的自动微分实现方法。在上一篇的文章中，我们介绍了自动微分的基本数学原理。可以总结自动微分的关键步骤为：",{"type":18,"tag":64,"props":108,"children":109},{},[110,115,120],{"type":18,"tag":68,"props":111,"children":112},{},[113],{"type":24,"value":114},"分解程序为一系列已知微分规则的基础表达式的组合",{"type":18,"tag":68,"props":116,"children":117},{},[118],{"type":24,"value":119},"根据已知微分规则给出各基础表达式的微分结果",{"type":18,"tag":68,"props":121,"children":122},{},[123],{"type":24,"value":124},"根据基础表达式间的数据依赖关系，使用链式法则将微分结果组合完成程序的微分结果",{"type":18,"tag":33,"props":126,"children":127},{},[128],{"type":24,"value":129},"虽然自动微分的数学原理已经明确，包括正向和反向的数学逻辑和模式。但具体的实现方法则可以有很大的差异，2018 年，Siskind 等学者在其综述论文Automatic Differentiation in Machine Learning: a Survey [1] 中对自动微分实现方案划分为三类：",{"type":18,"tag":33,"props":131,"children":132},{},[133,139],{"type":18,"tag":134,"props":135,"children":136},"strong",{},[137],{"type":24,"value":138},"基本表达式",{"type":24,"value":140},"：基本表达式或者称元素库（Elemental Libraries），基于元素库中封装一系列基本的表达式（如：加减乘除等）及其对应的微分结果表达式，作为库函数。用户通过调用库函数构建需要被微分的程序。而封装后的库函数在运行时会记录所有的基本表达式和相应的组合关系，最后使用链式法则对上述基本表达式的微分结果进行组合完成自动微分。",{"type":18,"tag":33,"props":142,"children":143},{},[144,149],{"type":18,"tag":134,"props":145,"children":146},{},[147],{"type":24,"value":148},"操作符重载",{"type":24,"value":150},"：操作符重载或者称运算重载（Operator Overloading，OO），利用现代语言的多态特性（例如C++/JAVA/Python等高级语言），使用操作符重载对语言中基本运算表达式的微分规则进行封装。同样，重载后的操作符在运行时会记录所有的操作符和相应的组合关系，最后使用链式法则对上述基本表达式的微分结果进行组合完成自动微分。",{"type":18,"tag":33,"props":152,"children":153},{},[154,159],{"type":18,"tag":134,"props":155,"children":156},{},[157],{"type":24,"value":158},"源代码变换",{"type":24,"value":160},"：源代码变换或者叫做源码转换（Source Code Transformation，SCT）则是通过对语言预处理器、编译器或解释器的扩展，将其中程序表达（如：源码、AST抽象语法树 或 编译过程中的中间表达 IR）的基本表达式微分规则进行预定义，再对程序表达进行分析得到基本表达式的组合关系，最后使用链式法则对上述基本表达式的微分结果进行组合生成对应微分结果的新程序表达，完成自动微分。",{"type":18,"tag":33,"props":162,"children":163},{},[164],{"type":24,"value":165},"任何 AD 实现中的一个主要考虑因素是 AD 运算时候引入的性能开销。就计算复杂性而言，AD 需要保证算术量增加不超过一个小的常数因子。另一方面，如果不小心管理 AD 算法，可能会带来很大的开销。例如，简单的分配数据结构来保存对偶数（正向运算和反向求导），将涉及每个算术运算的内存访问和分配，这通常比现代计算机上的算术运算更昂贵。同样，使用运算符重载可能会引入伴随成本的方法分派，与原始函数的原始数值计算相比，这很容易导致一个数量级的减速。",{"type":18,"tag":33,"props":167,"children":168},{},[169],{"type":24,"value":170},"下面这个图是论文作者回顾了一些比较通用的 AD 实现。",{"type":18,"tag":33,"props":172,"children":173},{},[174],{"type":18,"tag":175,"props":176,"children":178},"img",{"alt":7,"src":177},"https://pic3.zhimg.com/80/v2-be108c490e05e98efe25c677ed2b2096_720w.jpg",[],{"type":18,"tag":97,"props":180,"children":181},{"id":138},[182],{"type":24,"value":138},{"type":18,"tag":33,"props":184,"children":185},{},[186],{"type":24,"value":187},"基本表达式法也叫做元素库（Elemental Libraries），程序中实现构成自动微分中计算的最基本的类别或者表达式，并通过调用自动微分中的库，来代替数学逻辑运算来工作。然后在函数定义中使用库公开的方法，这意味着在编写代码时，手动将任何函数分解为基本操作。",{"type":18,"tag":33,"props":189,"children":190},{},[191],{"type":24,"value":192},"这个方法呢从自动微分刚出现的时候就已经被广泛地使用，典型的例子是 Lawson (1971) 的 WCOMP 和 UCOMP 库，Neidinger (1989) 的 APL 库，以及 Hinkins (1994) 的工作。同样，Rich 和 Hill (1992) 使用基本表达式法在 MATLAB 中制定了他们的自动微分实现。",{"type":18,"tag":33,"props":194,"children":195},{},[196],{"type":24,"value":197},"以公式为例子：",{"type":18,"tag":33,"props":199,"children":200},{},[201],{"type":18,"tag":175,"props":202,"children":205},{"alt":203,"src":204},"[公式]","https://www.zhihu.com/equation?tex=+f%28x1%2Cx2%29%3Dln%28x1%29%2Bx1%2Ax2%E2%88%92sin%28x2%29+%5Ctag%7B1%7D+",[],{"type":18,"tag":33,"props":207,"children":208},{},[209],{"type":24,"value":210},"用户首先需要手动将公式1中的各个操作，或者叫做子函数，分解为库函数中基本表达式组合：",{"type":18,"tag":212,"props":213,"children":215},"pre",{"code":214},"t1 = log(x)\nt3 = sin(x)\nt2 = x1 * x2\nt4 = x1 + x2\nt5 = x1 - x2\n",[216],{"type":18,"tag":217,"props":218,"children":219},"code",{"__ignoreMap":7},[220],{"type":24,"value":214},{"type":18,"tag":33,"props":222,"children":223},{},[224],{"type":24,"value":225},"使用给定的库函数，完成上述函数的程序设计：",{"type":18,"tag":212,"props":227,"children":229},{"code":228},"// 参数为变量 x,y,t 和对应的导数变量 dx,dy,dt\ndef ADAdd(x, y, dx, dy, t, dt)\n\n// 同理对上面的公式实现对应的函数\ndef ADSub(x, y, dx, dy, t, dt)\ndef ADMul(x, y, dx, dy, t, dt)\ndef ADLog(x, dx, t, dt)\ndef ADSin(x, dx, t, dt)\n",[230],{"type":18,"tag":217,"props":231,"children":232},{"__ignoreMap":7},[233],{"type":24,"value":228},{"type":18,"tag":33,"props":235,"children":236},{},[237],{"type":24,"value":238},"而库函数中则定义了对应表达式的数学微分规则，和对应的链式法则:",{"type":18,"tag":212,"props":240,"children":242},{"code":241},"// 参数为变量 x,y,t 和对应的导数变量 dx,dy,dt\ndef ADAdd(x, y, dx, dy, t, dt):\n    t = x + y\n    dt = dy + dx\n\n// 参数为变量 x,y,t 和对应的导数变量 dx,dy,dt\ndef ADSub(x, y, dx, dy, t, dt):\n    t = x - y\n    dt = dy - dx\n\n// ... 以此类推\n",[243],{"type":18,"tag":217,"props":244,"children":245},{"__ignoreMap":7},[246],{"type":24,"value":241},{"type":18,"tag":33,"props":248,"children":249},{},[250],{"type":24,"value":251},"针对公式1中基本表达式法，可以按照下面示例代码来实现正向的推理功能，反向其实也是一样，不过调用代码更复杂一点：",{"type":18,"tag":212,"props":253,"children":255},{"code":254},"x1 = xxx\nx2 = xxx\nt1 = ADlog(x1)\nt2 = ADSin(x2)\nt3 = ADMul(x1, x2)\nt4 = ADAdd(t1, t3)\nt5 = ADSub(t4, t2)\n",[256],{"type":18,"tag":217,"props":257,"children":258},{"__ignoreMap":7},[259],{"type":24,"value":254},{"type":18,"tag":33,"props":261,"children":262},{},[263,265,270],{"type":24,"value":264},"基本表达式法的",{"type":18,"tag":134,"props":266,"children":267},{},[268],{"type":24,"value":269},"优点",{"type":24,"value":271},"可以总结如下：",{"type":18,"tag":64,"props":273,"children":274},{},[275],{"type":18,"tag":68,"props":276,"children":277},{},[278],{"type":24,"value":279},"实现简单，基本可在任意语言中快速地实现为库",{"type":18,"tag":33,"props":281,"children":282},{},[283,284,289],{"type":24,"value":264},{"type":18,"tag":134,"props":285,"children":286},{},[287],{"type":24,"value":288},"缺点",{"type":24,"value":271},{"type":18,"tag":64,"props":291,"children":292},{},[293,298],{"type":18,"tag":68,"props":294,"children":295},{},[296],{"type":24,"value":297},"用户必须使用库函数进行编程，而无法使用语言原生的运算表达式；",{"type":18,"tag":68,"props":299,"children":300},{},[301],{"type":24,"value":302},"另外实现逻辑和代码也会冗余较长，依赖于开发人员较强的数学背景",{"type":18,"tag":33,"props":304,"children":305},{},[306],{"type":24,"value":307},"基本表达式法在没有操作符重载AD的80到90年代初期，仍然是计算机中实现自动微分功能最简单和快捷的策略啦。",{"type":18,"tag":97,"props":309,"children":310},{"id":148},[311],{"type":24,"value":148},{"type":18,"tag":33,"props":313,"children":314},{},[315],{"type":24,"value":316},"在具有多态特性的现代编程语言中，运算符重载提供了实现自动微分的最直接方式，利用了编程语言的第一特性（first class feature），重新定义了微分基本操作语义的能力。",{"type":18,"tag":33,"props":318,"children":319},{},[320],{"type":24,"value":321},"在 C++ 中使用运算符重载实现的流行工具是 ADOL-C（Walther 和 Griewank，2012）。 ADOL-C 要求对变量使用启用 AD 的类型，并在 Tape 数据结构中记录变量的算术运算，随后可以在反向模式 AD 计算期间“回放”。 Mxyzptlk 库 (Michelotti, 1990) 是 C++ 能够通过前向传播计算任意阶偏导数的另一个例子。 FADBAD++ 库（Bendtsen 和 Stauning，1996 年）使用模板和运算符重载为 C++ 实现自动微分。对于 Python 语言来说，autograd 提供正向和反向模式自动微分，支持高阶导数。",{"type":18,"tag":33,"props":323,"children":324},{},[325],{"type":24,"value":326},"在机器学习 ML 或者深度学习 DL 领域，目前AI框架中使用操作符重载的一个典型代表是 Pytroch，其中使用数据结构 Tape 来记录计算流程，在反向模式求解梯度的过程中进行replay Operator。",{"type":18,"tag":328,"props":329,"children":330},"ol",{},[331],{"type":18,"tag":68,"props":332,"children":333},{},[334],{"type":24,"value":335},"操作符重载来实现自动微分的功能里面，很重要的是利用高级语言的特性。下面简单看看伪代码，这里面我们定义一个特殊的数据结构 Variable，然后基于 Variable 重载一系列的操作如 __mul__ 代替 * 操作。",{"type":18,"tag":212,"props":337,"children":339},{"code":338},"class Variable:\n    def __init__(self, value):\n        self.value = value\n\n    def __mul__(self, other):\n        return ops_mul(self, other)\n\n   # 同样重载各种不同的基础操作\n   def __add__(self, other)\n   def __sub__(self, other)\n   def __div__(self, other)\n",[340],{"type":18,"tag":217,"props":341,"children":342},{"__ignoreMap":7},[343],{"type":24,"value":338},{"type":18,"tag":328,"props":345,"children":346},{},[347],{"type":18,"tag":68,"props":348,"children":349},{},[350],{"type":24,"value":351},"实现操作符重载后的计算。",{"type":18,"tag":212,"props":353,"children":355},{"code":354},"def ops_mul(self, other):\n    x = Variable(self.value * other.value)\n",[356],{"type":18,"tag":217,"props":357,"children":358},{"__ignoreMap":7},[359],{"type":24,"value":354},{"type":18,"tag":328,"props":361,"children":362},{},[363],{"type":18,"tag":68,"props":364,"children":365},{},[366],{"type":24,"value":367},"接着通过一个 Tape 的数据结构，来记录每次 Variable 执行计算的顺序，Tape 这里面主要是记录正向的计算，把输入、输出和执行运算的操作符记录下来。",{"type":18,"tag":212,"props":369,"children":371},{"code":370},"class Tape(NamedTuple):\n    inputs : []\n    outputs : []\n    propagate : (inputs, outpus)\n",[372],{"type":18,"tag":217,"props":373,"children":374},{"__ignoreMap":7},[375],{"type":24,"value":370},{"type":18,"tag":328,"props":377,"children":378},{},[379],{"type":18,"tag":68,"props":380,"children":381},{},[382],{"type":24,"value":383},"因为大部分 ML 系统或者 AI 框架采用的是反向模式，因此最后会逆向遍历 Tape 里面的数据（相当于反向传播或者反向模式的过程），然后累积反向计算的梯度。",{"type":18,"tag":212,"props":385,"children":387},{"code":386},"# 反向求导的过程，类似于 Pytroch 的 backward 接口\ndef grad(l, results):\n\n    # 通过 reversed 操作把带有梯度信息的 tape 逆向遍历\n    for entry in reversed(gradient_tape):\n        # 进行梯度累积，反向传播给上一次的操作计算\n        dl_d[input] += dl_dinput\n",[388],{"type":18,"tag":217,"props":389,"children":390},{"__ignoreMap":7},[391],{"type":24,"value":386},{"type":18,"tag":33,"props":393,"children":394},{},[395],{"type":24,"value":396},"当然啦，我们会在下一节当中带着大家亲自通过操作符重载实现一个前向的自动微分和后向的自动微分。下面总结一下操作符重载的一个基本流程：",{"type":18,"tag":64,"props":398,"children":399},{},[400,405,410,415],{"type":18,"tag":68,"props":401,"children":402},{},[403],{"type":24,"value":404},"预定义了特定的数据结构，并对该数据结构重载了相应的基本运算操作符",{"type":18,"tag":68,"props":406,"children":407},{},[408],{"type":24,"value":409},"程序在实际执行时会将相应表达式的操作类型和输入输出信息记录至特殊数据结构",{"type":18,"tag":68,"props":411,"children":412},{},[413],{"type":24,"value":414},"得到特殊数据结构后，将对数据结构进行遍历并对其中记录的基本运算操作进行微分",{"type":18,"tag":68,"props":416,"children":417},{},[418],{"type":24,"value":419},"把结果通过链式法则进行组合，完成自动微分",{"type":18,"tag":33,"props":421,"children":422},{},[423,425,429],{"type":24,"value":424},"操作符重载法的",{"type":18,"tag":134,"props":426,"children":427},{},[428],{"type":24,"value":269},{"type":24,"value":271},{"type":18,"tag":64,"props":431,"children":432},{},[433,438],{"type":18,"tag":68,"props":434,"children":435},{},[436],{"type":24,"value":437},"实现简单，只要求语言提供多态的特性能力",{"type":18,"tag":68,"props":439,"children":440},{},[441],{"type":24,"value":442},"易用性高，重载操作符后跟使用原生语言的编程方式类似",{"type":18,"tag":33,"props":444,"children":445},{},[446,447,451],{"type":24,"value":424},{"type":18,"tag":134,"props":448,"children":449},{},[450],{"type":24,"value":288},{"type":24,"value":271},{"type":18,"tag":64,"props":453,"children":454},{},[455,460],{"type":18,"tag":68,"props":456,"children":457},{},[458],{"type":24,"value":459},"需要显式的构造特殊数据结构和对特殊数据结构进行大量读写、遍历操作，这些额外数据结构和操作的引入不利于高阶微分的实现",{"type":18,"tag":68,"props":461,"children":462},{},[463],{"type":24,"value":464},"对于一些类似 if，while 等控制流表达式，难以通过操作符重载进行微分规则定义。对于这些操作的处理会退化成基本表达式方法中特定函数封装的方式，难以使用语言原生的控制流表达式",{"type":18,"tag":97,"props":466,"children":468},{"id":467},"源代码转换",[469],{"type":24,"value":467},{"type":18,"tag":33,"props":471,"children":472},{},[473],{"type":24,"value":474},"源码转换（Source Code Transformation，SCT）是最复杂的，实现起来也是非常具有挑战性。",{"type":18,"tag":33,"props":476,"children":477},{},[478],{"type":24,"value":479},"源码转换的实现提供了对编程语言的扩展，可自动将算法分解为支持自动微分的基本操作。通常作为预处理器执行，以将扩展语言的输入转换为原始语言。简单来说就是利用源语言来实现领域扩展语言 DSL 的操作方式。",{"type":18,"tag":33,"props":481,"children":482},{},[483],{"type":24,"value":484},"源代码转换的经典实例包括 Fortran 预处理器 GRESS（Horwedel 等人，1988 年）和 PADRE2（Kubo 和 Iri，1990 年），在编译之前将启用 AD 的 Fortran 变体转换为标准 Fortran。类似地，ADIFOR 工具 (Bischof et al., 1996) 给定一个 Fortran 源代码，生成一个增强代码，其中除了原始结果之外还计算所有指定的偏导数。对于以 ANSI C 编码的过程，ADIC 工具（Bischof 等人，1997）在指定因变量和自变量之后将 AD 实现为源代码转换。 Tapenade（Pascual 和 Hasco¨et，2008 年；Hasco¨et 和 Pascual，2013 年）是过去10年终 SCT 的流行工具，它为 Fortran 和 C 程序实现正向和反向模式 AD。",{"type":18,"tag":33,"props":486,"children":487},{},[488],{"type":24,"value":489},"除了通过源代码转换进行语言扩展外，还有一些实现通过专用编译器或解释器引入了具有紧密集成的 AD 功能的新语言。一些最早的 AD 工具，例如 SLANG (Adamson and Winant, 1969) 和 PROSE (Pfeiffer, 1987) 属于这一类。 NAGWare Fortran 编译器 (Naumann and Riehme, 2005) 是一个较新的示例，其中使用与 AD 相关的扩展会在编译时触发衍生代码的自动生成。",{"type":18,"tag":33,"props":491,"children":492},{},[493],{"type":24,"value":494},"作为基于解释器的实现的一个例子，代数建模语言 AMPL (Fourer et al., 2002) 可以用数学符号表示目标和约束，系统从中推导出活动变量并安排必要的 AD 计算。此类别中的其他示例包括基于类似 Algol 的 DIFALG 语言的 FM/FAD 包 (Mazourik, 1991)，以及类似于 Pascal 的面向对象的 COZY 语言 (Berz et al., 1996)。",{"type":18,"tag":33,"props":496,"children":497},{},[498],{"type":24,"value":499},"而华为全场景AI框架 MindSpore 则是基于 Python 语言使用源代码转换实现 AD 的正反向模式，并采用了函数式编程的风格，该机制可以用控制流表示复杂的组合。函数被转换成函数中间表达（Intermediate Representation，IR），中间表达构造出一个能够在不同设备上解析和执行的计算图。在执行前，计算图上应用了多种软硬件协同优化技术，以提升端、边、云等不同场景下的性能和效率。",{"type":18,"tag":33,"props":501,"children":502},{},[503],{"type":24,"value":504},"其主要流程是：分析获得源程序的 AST 表达形式；然后基于 AST 完成基本表达式的分解和微分操作；再通过遍历 AST 得到基本表达式间的依赖关系，从而应用链式法则完成自动微分。",{"type":18,"tag":33,"props":506,"children":507},{},[508],{"type":24,"value":509},"因为源码转换涉及到底层的抽象语法树、编译执行等细节，因此这里就不给出伪代码了（实在太难了给不出来），我们通过下面这张图来简单了解下 SCT 的一般性过程。",{"type":18,"tag":33,"props":511,"children":512},{},[513],{"type":18,"tag":175,"props":514,"children":516},{"alt":7,"src":515},"https://pic1.zhimg.com/80/v2-db48e558395a4b205fe201728ba46080_720w.jpg",[],{"type":18,"tag":33,"props":518,"children":519},{},[520],{"type":24,"value":521},"从图中可以看到源码转换的整体流程分为编译时间和执行时间，以 MindSpore 为例，其在运行之前的第一个 epoch 会等待一段时间，是因为需要对源码进行编译转换解析等一系列的操作。然后再 run time 运行时则会比较顺畅，直接对数据和代码不断地按照计算机指令来高速执行。",{"type":18,"tag":33,"props":523,"children":524},{},[525],{"type":24,"value":526},"编译阶段呢，在 Initialization 过程中会对源码进行 Parse 转换成为抽象语法树 AST，接着转换为基于图表示的中间表达 IR，这个基于图的IR从概念上理解可以理解为计算图，神经网络层数的表示通过图表示会比较直观。",{"type":18,"tag":33,"props":528,"children":529},{},[530],{"type":24,"value":531},"接着对 Graph base IR进行一些初级的类型推导，特别是针对 Tensor/List/Str 等不同的基础数据表示，然后进行宏展开，还有语言单态化，最后再对变量或者自变量进行类型推导。可以从图中看到，很多地方出现了不同形式的 IR，IR 其实是编译器中常用的一个中间表达概念，在编译的 Pass 中会有很多处理流程，每一步处理流程产生一个 IR，交给下一个Pass进行处理。",{"type":18,"tag":33,"props":533,"children":534},{},[535],{"type":24,"value":536},"最后通过 LLVM 或者其他等不同的底层编译器，最后把 IR 编译成机器码，然后就可以真正地在runtime执行起来。",{"type":18,"tag":33,"props":538,"children":539},{},[540,542,546],{"type":24,"value":541},"源码转换法的",{"type":18,"tag":134,"props":543,"children":544},{},[545],{"type":24,"value":269},{"type":24,"value":271},{"type":18,"tag":64,"props":548,"children":549},{},[550,555,560],{"type":18,"tag":68,"props":551,"children":552},{},[553],{"type":24,"value":554},"支持更多的数据类型（原生和用户自定义的数据类型） + 原生语言操作（基本数学运算操作和控制流操作）",{"type":18,"tag":68,"props":556,"children":557},{},[558],{"type":24,"value":559},"高阶微分中实现容易，不用每次使用 Tape 来记录高阶的微分中产生的大量变量，而是统一通过编译器进行额外变量优化和重计算等优化",{"type":18,"tag":68,"props":561,"children":562},{},[563],{"type":24,"value":564},"进一步提升性能，没有产生额外的 tape 数据结构和 tape 读写操作，除了利于实现高阶微分以外，还能够对计算表达式进行统一的编译优化",{"type":18,"tag":33,"props":566,"children":567},{},[568,569,573],{"type":24,"value":541},{"type":18,"tag":134,"props":570,"children":571},{},[572],{"type":24,"value":288},{"type":24,"value":271},{"type":18,"tag":64,"props":575,"children":576},{},[577,582,587],{"type":18,"tag":68,"props":578,"children":579},{},[580],{"type":24,"value":581},"实现复杂，需要扩展语言的预处理器、编译器或解释器，深入计算机体系和底层编译",{"type":18,"tag":68,"props":583,"children":584},{},[585],{"type":24,"value":586},"支持更多数据类型和操作，用户自由度虽然更高，但同时更容易写出不支持的代码导致错误",{"type":18,"tag":68,"props":588,"children":589},{},[590],{"type":24,"value":591},"微分结果是以代码的形式存在，在执行计算的过程当中，特别是深度学习中大量使用for循环过程中间错误了，或者是数据处理流程中出现错误，并不利于深度调试",{"type":18,"tag":97,"props":593,"children":595},{"id":594},"参考",[596],{"type":24,"value":594},{"type":18,"tag":33,"props":598,"children":599},{},[600,602],{"type":24,"value":601},"[1] Automatic Differentiation in Machine Learning: a Survey: ",{"type":18,"tag":39,"props":603,"children":606},{"href":604,"rel":605},"https://link.zhihu.com/?target=https%3A//arxiv.org/abs/1502.05767",[43],[607],{"type":24,"value":608},"https://arxiv.org/abs/1502.05767",{"type":18,"tag":33,"props":610,"children":611},{},[612],{"type":24,"value":613},"[2] Rirchard L. Burden and J. Douglas Faires. Numerical Analysis. Brooks/Cole, 2001.",{"type":18,"tag":33,"props":615,"children":616},{},[617],{"type":24,"value":618},"[3] Max E. Jerrell. Automatic differentiation and interval arithmetic for estimation of disequilibrium models. Computational Economics, 10(3):295–316, 1997.",{"type":18,"tag":33,"props":620,"children":621},{},[622],{"type":24,"value":623},"[4] Johannes Grabmeier and Erich Kaltofen. Computer Algebra Handbook: Foundations, Applications, Systems. Springer, 2003.",{"type":18,"tag":33,"props":625,"children":626},{},[627],{"type":24,"value":628},"[5] G. W. Leibniz. Machina arithmetica in qua non additio tantum et subtractio sed et multiplicatio nullo, diviso vero paene nullo animi labore peragantur. Hannover, 1685.",{"type":18,"tag":33,"props":630,"children":631},{},[632],{"type":24,"value":633},"[6] George F. Corliss. Application of differentiation arithmetic, volume 19 of Perspectives in Computing, pages 127–48. Academic Press, Boston, 1988.",{"type":18,"tag":33,"props":635,"children":636},{},[637],{"type":24,"value":638},"[7] Arun Verma. An introduction to automatic differentiation. Current Science, 78(7):804–7, 2000.",{"type":18,"tag":33,"props":640,"children":641},{},[642],{"type":24,"value":643},"[8] Andreas Griewank and Andrea Walther. Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation. Society for Industrial and Applied Mathematics, Philadelphia, 2008. doi: 10.1137/1.9780898717761.",{"type":18,"tag":33,"props":645,"children":646},{},[647],{"type":24,"value":648},"[9] John F. Nolan. Analytical differentiation on a digital computer. Master’s thesis, Massachusetts Institute of Technology, 1953.",{"type":18,"tag":33,"props":650,"children":651},{},[652],{"type":24,"value":653},"[10] L. M. Beda, L. N. Korolev, N. V. Sukkikh, and T. S. Frolova. Programs for automatic differentiation for the machine BESM (in Russian). Technical report, Institute for Precise Mechanics and Computation Techniques, Academy of Science, Moscow, USSR, 1959.",{"type":18,"tag":33,"props":655,"children":656},{},[657],{"type":24,"value":658},"[11] Robert E. Wengert. A simple automatic derivative evaluation program. Communications of the ACM, 7:463–4, 1964.",{"type":18,"tag":33,"props":660,"children":661},{},[662],{"type":24,"value":663},"[12] Andreas Griewank. On automatic differentiation. pages 83–108, 1989.",{"type":18,"tag":33,"props":665,"children":666},{},[667],{"type":24,"value":668},"[13] Hascoet, Laurent, and Valérie Pascual. \"The Tapenade automatic differentiation tool: principles, model, and specification.\" ACM Transactions on Mathematical Software (TOMS) 39.3 (2013): 1-43.",{"type":18,"tag":33,"props":670,"children":671},{},[672,674],{"type":24,"value":673},"[14] 知乎专栏：自动微分（Automatic Differentiation）: ",{"type":18,"tag":39,"props":675,"children":678},{"href":676,"rel":677},"https://zhuanlan.zhihu.com/p/61103504",[43],[679],{"type":24,"value":676},{"type":18,"tag":33,"props":681,"children":682},{},[683,685],{"type":24,"value":684},"[15] 知乎专栏：数值计算方法 第六章 数值积分和数值微分: ",{"type":18,"tag":39,"props":686,"children":689},{"href":687,"rel":688},"https://zhuanlan.zhihu.com/p/14",[43],[690],{"type":24,"value":687},{"type":18,"tag":33,"props":692,"children":693},{},[694,696],{"type":24,"value":695},"[16] 知乎专栏：技术分享 | 从自动微分到可微编程语言设计 ",{"type":18,"tag":39,"props":697,"children":700},{"href":698,"rel":699},"https://zhuanlan.zhihu.com/p/395725922",[43],[701],{"type":24,"value":702},"https://zhuanlan.zhihu.com/p/39",{"title":7,"searchDepth":704,"depth":704,"links":705},4,[706],{"id":28,"depth":707,"text":31,"children":708},2,[709,711,712,713,714],{"id":99,"depth":710,"text":99},3,{"id":138,"depth":710,"text":138},{"id":148,"depth":710,"text":148},{"id":467,"depth":710,"text":467},{"id":594,"depth":710,"text":594},"markdown","content:technology-blogs:zh:1552.md","content","technology-blogs/zh/1552.md","technology-blogs/zh/1552","md",1776506113303]