Graph Mode加速背景介绍AI编译框架有两种运行模式动态图模式和静态图模式。MindSpore默认情况下是以动态图模式运行但也支持手动切换为静态图模式。两种运行模式的详细介绍如下动态图模式动态图的特点是计算图的构建和计算同时发生Define by run符合Python的解释执行方式。在计算图中定义一个Tensor时其值就已经被计算且确定因此在调试模型时较为方便能够实时得到中间结果的值。但由于所有节点都需要被保存导致难以对整个计算图进行优化。在MindSpore中动态图模式又被称为PyNative模式。由于动态图的解释执行特性在脚本开发和网络流程调试过程中推荐使用动态图模式。如需要手动控制框架采用PyNative模式可以通过以下代码进行网络构建importnumpyasnpimportmindsporeasmsfrommindsporeimportnn,Tensor ms.set_context(modems.PYNATIVE_MODE)# 使用set_context进行动态图模式的配置classNetwork(nn.Cell):def__init__(self):super().__init__()self.flattennn.Flatten()self.dense_relu_sequentialnn.SequentialCell(nn.Dense(28*28,512),nn.ReLU(),nn.Dense(512,512),nn.ReLU(),nn.Dense(512,10))defconstruct(self,x):xself.flatten(x)logitsself.dense_relu_sequential(x)returnlogits modelNetwork()inputTensor(np.ones([64,1,28,28]).astype(np.float32))outputmodel(input)print(output)[[-0.00134926 -0.13563682 -0.02863023 -0.05452826 0.03290743 -0.12423715 -0.0582641 -0.10854103 -0.08558805 0.06099342] [-0.00134926 -0.13563682 -0.02863023 -0.05452826 0.03290743 -0.12423715 -0.0582641 -0.10854103 -0.08558805 0.06099342] [-0.00134926 -0.13563682 -0.02863023 -0.05452826 0.03290743 -0.12423715 -0.0582641 -0.10854103 -0.08558805 0.06099342] [-0.00134926 -0.13563682 -0.02863023 -0.05452826 0.03290743 -0.12423715 -0.0582641 -0.10854103 -0.08558805 0.06099342] [-0.00134926 -0.13563682 -0.02863023 -0.05452826 0.03290743 -0.12423715 -0.0582641 -0.10854103 -0.08558805 0.06099342] ... [-0.00134926 -0.13563682 -0.02863023 -0.05452826 0.03290743 -0.12423715 -0.0582641 -0.10854103 -0.08558805 0.06099342] [-0.00134926 -0.13563682 -0.02863023 -0.05452826 0.03290743 -0.12423715 -0.0582641 -0.10854103 -0.08558805 0.06099342] [-0.00134926 -0.13563682 -0.02863023 -0.05452826 0.03290743 -0.12423715 -0.0582641 -0.10854103 -0.08558805 0.06099342] [-0.00134926 -0.13563682 -0.02863023 -0.05452826 0.03290743 -0.12423715 -0.0582641 -0.10854103 -0.08558805 0.06099342]]静态图模式相较于动态图而言静态图的特点是将计算图的构建和实际计算分开Define and run。有关静态图模式的运行原理可以参考静态图语法支持。在MindSpore中静态图模式又被称为Graph模式。在Graph模式下基于图优化、计算图整图下沉等技术编译器可以针对图进行全局的优化获得较好的性能因此比较适合网络固定且需要高性能的场景。如需手动控制框架采用静态图模式可以通过以下代码进行网络构建importnumpyasnpimportmindsporeasmsfrommindsporeimportnn,Tensor ms.set_context(modems.GRAPH_MODE)# 使用set_context进行运行静态图模式的配置classNetwork(nn.Cell):def__init__(self):super().__init__()self.flattennn.Flatten()self.dense_relu_sequentialnn.SequentialCell(nn.Dense(28*28,512),nn.ReLU(),nn.Dense(512,512),nn.ReLU(),nn.Dense(512,10))defconstruct(self,x):xself.flatten(x)logitsself.dense_relu_sequential(x)returnlogits modelNetwork()inputTensor(np.ones([64,1,28,28]).astype(np.float32))outputmodel(input)print(output)[[ 0.05363735 0.05117104 -0.03343301 0.06347139 0.07546629 0.03263091 0.02790363 0.06269836 0.01838502 0.04387159] [ 0.05363735 0.05117104 -0.03343301 0.06347139 0.07546629 0.03263091 0.02790363 0.06269836 0.01838502 0.04387159] [ 0.05363735 0.05117104 -0.03343301 0.06347139 0.07546629 0.03263091 0.02790363 0.06269836 0.01838502 0.04387159] [ 0.05363735 0.05117104 -0.03343301 0.06347139 0.07546629 0.03263091 0.02790363 0.06269836 0.01838502 0.04387159] ... [ 0.05363735 0.05117104 -0.03343301 0.06347139 0.07546629 0.03263091 0.02790363 0.06269836 0.01838502 0.04387159] [ 0.05363735 0.05117104 -0.03343301 0.06347139 0.07546629 0.03263091 0.02790363 0.06269836 0.01838502 0.04387159] [ 0.05363735 0.05117104 -0.03343301 0.06347139 0.07546629 0.03263091 0.02790363 0.06269836 0.01838502 0.04387159] [ 0.05363735 0.05117104 -0.03343301 0.06347139 0.07546629 0.03263091 0.02790363 0.06269836 0.01838502 0.04387159]]静态图模式的使用场景MindSpore编译器重点面向Tensor数据的计算以及其微分处理。因此使用MindSpore API以及基于Tensor对象的操作更适合使用静态图编译优化。其他操作虽然可以部分入图编译但实际优化作用有限。另外静态图模式采用先编译后执行存在编译耗时。如果函数无需反复执行那么Graph Mode加速也可能没有价值。有关使用静态图来进行网络编译的示例请参考网络构建。静态图模式开启方式通常情况下由于动态图的灵活性我们会选择使用PyNative模式来进行自由的神经网络构建以实现模型的创新和优化。但是当需要进行性能加速时可以对神经网络部分或整体进行加速。MindSpore提供了两种切换为静态图模式的方式基于装饰器的开启方式以及基于全局context的开启方式。基于装饰器的开启方式MindSpore提供了jit装饰器可以通过修饰Python函数或者Python类的成员函数使其被编译成计算图并通过图优化等技术提高运行速度。此时可以对想要进行性能优化的模块进行图编译加速而模型其他部分仍旧使用解释执行方式不丢失动态图的灵活性。无论全局context是设置成静态图模式还是动态图模式被jit修饰的部分始终会以静态图模式进行运行。在需要对Tensor的某些运算进行编译加速时可以在其定义的函数上使用jit修饰器在调用该函数时该模块自动被编译为静态图。需要注意的是jit装饰器只能用来修饰函数无法对类进行修饰。jit的使用示例如下importnumpyasnpimportmindsporeasmsfrommindsporeimportnn,TensorclassNetwork(nn.Cell):def__init__(self):super().__init__()self.flattennn.Flatten()self.dense_relu_sequentialnn.SequentialCell(nn.Dense(28*28,512),nn.ReLU(),nn.Dense(512,512),nn.ReLU(),nn.Dense(512,10))defconstruct(self,x):xself.flatten(x)logitsself.dense_relu_sequential(x)returnlogitsinputTensor(np.ones([64,1,28,28]).astype(np.float32))ms.jit# 使用ms.jit装饰器使被装饰的函数以静态图模式运行defrun(x):modelNetwork()returnmodel(x)outputrun(input)print(output)[[-0.12126954 0.06986676 -0.2230821 -0.07087803 -0.01003947 0.01063392 0.10143848 -0.0200909 -0.09724037 0.0114444 ] [-0.12126954 0.06986676 -0.2230821 -0.07087803 -0.01003947 0.01063392 0.10143848 -0.0200909 -0.09724037 0.0114444 ] [-0.12126954 0.06986676 -0.2230821 -0.07087803 -0.01003947 0.01063392 0.10143848 -0.0200909 -0.09724037 0.0114444 ] [-0.12126954 0.06986676 -0.2230821 -0.07087803 -0.01003947 0.01063392 0.10143848 -0.0200909 -0.09724037 0.0114444 ] ... [-0.12126954 0.06986676 -0.2230821 -0.07087803 -0.01003947 0.01063392 0.10143848 -0.0200909 -0.09724037 0.0114444 ] [-0.12126954 0.06986676 -0.2230821 -0.07087803 -0.01003947 0.01063392 0.10143848 -0.0200909 -0.09724037 0.0114444 ] [-0.12126954 0.06986676 -0.2230821 -0.07087803 -0.01003947 0.01063392 0.10143848 -0.0200909 -0.09724037 0.0114444 ] [-0.12126954 0.06986676 -0.2230821 -0.07087803 -0.01003947 0.01063392 0.10143848 -0.0200909 -0.09724037 0.0114444 ]]除使用修饰器外也可使用函数变换方式调用jit方法示例如下importnumpyasnpimportmindsporeasmsfrommindsporeimportnn,TensorclassNetwork(nn.Cell):def__init__(self):super().__init__()self.flattennn.Flatten()self.dense_relu_sequentialnn.SequentialCell(nn.Dense(28*28,512),nn.ReLU(),nn.Dense(512,512),nn.ReLU(),nn.Dense(512,10))defconstruct(self,x):xself.flatten(x)logitsself.dense_relu_sequential(x)returnlogitsinputTensor(np.ones([64,1,28,28]).astype(np.float32))defrun(x):modelNetwork()returnmodel(x)run_with_jitms.jit(run)# 通过调用jit将函数转换为以静态图方式执行outputrun_with_jit(input)print(output)[[ 0.11027216 -0.09628229 0.0457969 0.05396656 -0.06958974 0.0428197 -0.1572069 -0.14151613 -0.04531277 0.07521383] [ 0.11027216 -0.09628229 0.0457969 0.05396656 -0.06958974 0.0428197 -0.1572069 -0.14151613 -0.04531277 0.07521383] [ 0.11027216 -0.09628229 0.0457969 0.05396656 -0.06958974 0.0428197 -0.1572069 -0.14151613 -0.04531277 0.07521383] [ 0.11027216 -0.09628229 0.0457969 0.05396656 -0.06958974 0.0428197 -0.1572069 -0.14151613 -0.04531277 0.07521383] ... [ 0.11027216 -0.09628229 0.0457969 0.05396656 -0.06958974 0.0428197 -0.1572069 -0.14151613 -0.04531277 0.07521383] [ 0.11027216 -0.09628229 0.0457969 0.05396656 -0.06958974 0.0428197 -0.1572069 -0.14151613 -0.04531277 0.07521383] [ 0.11027216 -0.09628229 0.0457969 0.05396656 -0.06958974 0.0428197 -0.1572069 -0.14151613 -0.04531277 0.07521383] [ 0.11027216 -0.09628229 0.0457969 0.05396656 -0.06958974 0.0428197 -0.1572069 -0.14151613 -0.04531277 0.07521383]]当我们需要对神经网络的某部分进行加速时可以直接在construct方法上使用jit修饰器在调用实例化对象时该模块自动被编译为静态图。示例如下importnumpyasnpimportmindsporeasmsfrommindsporeimportnn,TensorclassNetwork(nn.Cell):def__init__(self):super().__init__()self.flattennn.Flatten()self.dense_relu_sequentialnn.SequentialCell(nn.Dense(28*28,512),nn.ReLU(),nn.Dense(512,512),nn.ReLU(),nn.Dense(512,10))ms.jit# 使用ms.jit装饰器使被装饰的函数以静态图模式运行defconstruct(self,x):xself.flatten(x)logitsself.dense_relu_sequential(x)returnlogitsinputTensor(np.ones([64,1,28,28]).astype(np.float32))modelNetwork()outputmodel(input)print(output)[[ 0.10522258 0.06597593 -0.09440921 -0.04883489 0.07194916 0.1343117 -0.06813788 0.01986085 0.0216996 -0.05345828] [ 0.10522258 0.06597593 -0.09440921 -0.04883489 0.07194916 0.1343117 -0.06813788 0.01986085 0.0216996 -0.05345828] [ 0.10522258 0.06597593 -0.09440921 -0.04883489 0.07194916 0.1343117 -0.06813788 0.01986085 0.0216996 -0.05345828] [ 0.10522258 0.06597593 -0.09440921 -0.04883489 0.07194916 0.1343117 -0.06813788 0.01986085 0.0216996 -0.05345828] ... [ 0.10522258 0.06597593 -0.09440921 -0.04883489 0.07194916 0.1343117 -0.06813788 0.01986085 0.0216996 -0.05345828] [ 0.10522258 0.06597593 -0.09440921 -0.04883489 0.07194916 0.1343117 -0.06813788 0.01986085 0.0216996 -0.05345828] [ 0.10522258 0.06597593 -0.09440921 -0.04883489 0.07194916 0.1343117 -0.06813788 0.01986085 0.0216996 -0.05345828] [ 0.10522258 0.06597593 -0.09440921 -0.04883489 0.07194916 0.1343117 -0.06813788 0.01986085 0.0216996 -0.05345828]]基于context的开启方式context模式是一种全局的设置模式。代码示例如下importnumpyasnpimportmindsporeasmsfrommindsporeimportnn,Tensor ms.set_context(modems.GRAPH_MODE)# 使用set_context进行运行静态图模式的配置classNetwork(nn.Cell):def__init__(self):super().__init__()self.flattennn.Flatten()self.dense_relu_sequentialnn.SequentialCell(nn.Dense(28*28,512),nn.ReLU(),nn.Dense(512,512),nn.ReLU(),nn.Dense(512,10))defconstruct(self,x):xself.flatten(x)logitsself.dense_relu_sequential(x)returnlogits modelNetwork()inputTensor(np.ones([64,1,28,28]).astype(np.float32))outputmodel(input)print(output)[[ 0.08501796 -0.04404321 -0.05165704 0.00357929 0.00051521 0.00946456 0.02748473 -0.19415936 -0.00278988 0.04024826] [ 0.08501796 -0.04404321 -0.05165704 0.00357929 0.00051521 0.00946456 0.02748473 -0.19415936 -0.00278988 0.04024826] [ 0.08501796 -0.04404321 -0.05165704 0.00357929 0.00051521 0.00946456 0.02748473 -0.19415936 -0.00278988 0.04024826] [ 0.08501796 -0.04404321 -0.05165704 0.00357929 0.00051521 0.00946456 0.02748473 -0.19415936 -0.00278988 0.04024826] ... [ 0.08501796 -0.04404321 -0.05165704 0.00357929 0.00051521 0.00946456 0.02748473 -0.19415936 -0.00278988 0.04024826] [ 0.08501796 -0.04404321 -0.05165704 0.00357929 0.00051521 0.00946456 0.02748473 -0.19415936 -0.00278988 0.04024826] [ 0.08501796 -0.04404321 -0.05165704 0.00357929 0.00051521 0.00946456 0.02748473 -0.19415936 -0.00278988 0.04024826] [ 0.08501796 -0.04404321 -0.05165704 0.00357929 0.00051521 0.00946456 0.02748473 -0.19415936 -0.00278988 0.04024826]]静态图的语法约束在Graph模式下Python代码并不会由Python解释器去执行而是先编译成静态计算图再执行该静态计算图。因此编译器无法支持全量的Python语法。MindSpore的静态图编译器支持Python常用语法子集以支持神经网络的构建及训练。详情可参考静态图语法支持。静态图高级编程技巧使用静态图高级编程技巧可以有效地提高编译和执行效率使程序运行更加稳定。详情可参考静态图高级编程技巧。