WO2023082901A1 - 一种用于计算图编译的优化方法及装置 - Google Patents

一种用于计算图编译的优化方法及装置 Download PDF

Info

Publication number
WO2023082901A1
WO2023082901A1 PCT/CN2022/124001 CN2022124001W WO2023082901A1 WO 2023082901 A1 WO2023082901 A1 WO 2023082901A1 CN 2022124001 W CN2022124001 W CN 2022124001W WO 2023082901 A1 WO2023082901 A1 WO 2023082901A1
Authority
WO
WIPO (PCT)
Prior art keywords
node
nodes
stack
tensor
graph
Prior art date
Application number
PCT/CN2022/124001
Other languages
English (en)
French (fr)
Inventor
王宏升
何水兵
陈�光
Original Assignee
之江实验室
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 之江实验室 filed Critical 之江实验室
Priority to US17/992,814 priority Critical patent/US20240127027A1/en
Publication of WO2023082901A1 publication Critical patent/WO2023082901A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/06Physical realisation, i.e. hardware implementation of neural networks, neurons or parts of neurons
    • G06N3/063Physical realisation, i.e. hardware implementation of neural networks, neurons or parts of neurons using electronic means
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/04Architecture, e.g. interconnection topology
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F5/00Methods or arrangements for data conversion without changing the order or content of the data handled
    • G06F5/06Methods or arrangements for data conversion without changing the order or content of the data handled for changing the speed of data flow, i.e. speed regularising or timing, e.g. delay lines, FIFO buffers; over- or underrun control therefor
    • G06F5/08Methods or arrangements for data conversion without changing the order or content of the data handled for changing the speed of data flow, i.e. speed regularising or timing, e.g. delay lines, FIFO buffers; over- or underrun control therefor having a sequence of storage locations, the intermediate ones not being accessible for either enqueue or dequeue operations, e.g. using a shift register
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/43Checking; Contextual analysis
    • G06F8/433Dependency analysis; Data or control flow analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/04Architecture, e.g. interconnection topology
    • G06N3/045Combinations of networks
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/10Interfaces, programming languages or software development kits, e.g. for simulating neural networks
    • G06N3/105Shells for specifying net layout

Definitions

  • the present invention relates to the technical field of a computer system based on a specific calculation model, in particular to an optimization method and device for compiling a calculation graph.
  • the present invention proposes to abstract the dynamic change process of the node state during the execution of the calculation graph into a constraint-based set representation method, and provides an intermediate representation technology based on a node set containing effective tensor variables.
  • the present invention provides an optimization method and device for compiling computation graphs.
  • An optimization method for computing graph compilation comprising the following steps:
  • Step S1 Convert the computational graph to an intermediate representation based on a collection of nodes containing valid tensor variables
  • Step S2 Analyzing dependencies between nodes in the computation graph
  • Step S3 constructing a work stack for storing nodes to be processed
  • Step S4 initializing the node elements included in the work stack to be in an inactive state
  • Step S5 the working stack pops up the stack top node element, uses the dependency relationship in step S2 to deduce the input node set of the stack top node element, and updates the input node set of the stack top node element obtained by the current round of iteration;
  • Step S6 adding the elements of the top node of the stack dependent on the step S5 to the top position of the stack in sequence, and updating the current working stack, repeating the step S5 until the working stack is cleared;
  • Step S7 using a bit vector to realize the intermediate representation of the fixed point state
  • Step S8 Allocate registers for valid tensor variables contained in nodes of the intermediate representation reaching the fixed point state.
  • step S1 specifically includes the following sub-steps:
  • Step S11 Express the nodes of the computational graph containing valid tensor variables as: an equation consisting of the definition of the tensor variable and the expression using the tensor variable ;
  • Step S12 defining that the nodes of the computation graph relate to a set of input nodes containing valid tensor variable nodes, the set of input nodes being the union of all predecessor nodes of the nodes of the computation graph;
  • Step S13 Define the nodes of the calculation graph about the output node set containing valid tensor variable nodes, the output node set removes and redefines the node set of valid tensor variables for the input node set, and then combines with the valid tensor variable nodes
  • the node position of the tensor variable contains the node set and union of the valid tensor variable;
  • Step S14 Obtaining the set of nodes containing valid tensor variables in the intermediate representation needs to iteratively deduce the nodes containing valid tensor variables for each node until the set of input nodes and the set of output nodes of all nodes no longer change, reaching fixed-nodes, defines the set up to fixed-nodes as said intermediate representation based on the set of nodes containing valid tensor variables.
  • step S2 is specifically: analyzing and deriving the relationship between the input node sets among the nodes of the computation graph.
  • step S3 specifically includes: traversing the computation graph according to the topological order, and sequentially pushing the nodes in the computation graph into the work stack.
  • step S4 specifically includes: initializing each node of the computation graph that has not started to be executed to an inactive state.
  • step S5 specifically includes the following sub-steps:
  • Step S51 popping the top node element from the work stack, that is, popping the top node element of the work stack from the stack;
  • Step S52 Using the dependencies in step S2, add the input node set of the popped top node element to the work stack, and update the input node set of the stack top node element obtained in the current round of iteration.
  • step S7 is specifically: mapping the node elements included when the input node set of each node in the intermediate representation of the calculation graph reaches the fixed point state to 1, and mapping other node elements to 0.
  • step S8 specifically includes: allocating free registers for the tensor variables contained in the nodes whose node elements are mapped to 1 when the input node set reaches the fixed point state in step 7.
  • the present invention also provides an optimization device for computing graph compilation, including a memory and one or more processors, executable codes are stored in the memory, and when the one or more processors execute the executable codes , for implementing an optimization method for compiling a computation graph described in any one of the above embodiments.
  • the present invention also provides a computer-readable storage medium, on which a program is stored.
  • the program is executed by a processor, the optimization method for compiling a computation graph described in any one of the above embodiments is implemented.
  • the present invention discloses an optimization method and device for compiling a computation graph
  • the method is an optimization method for compiling a computation graph.
  • the present invention proposes a way to convert the calculation graph into an intermediate representation based on a set of effective variable nodes, provides a method for parsing the intermediate representation nodes to dynamically execute to a fixed point state, and optimizes the The implementation method of allocating free registers for the tensor variables contained in each node in the moving point state.
  • the optimization method for computing graph compilation proposed by the present invention not only improves the execution efficiency of the computing graph at runtime. In the process of developing the algorithm model, researchers and engineering users use the optimization method and device optimization model for computing graph compilation to optimize the compilation efficiency of the computing graph and promote the neural network in the relationship graph. The development of model landing application.
  • Fig. 1 is a structure diagram of an optimization method for computing graph compilation of the present invention
  • Fig. 2 is the calculation diagram generated by the neural network compilation of the embodiment of the present invention.
  • FIG. 3 is a definition of a set-based intermediate representation according to an embodiment of the present invention.
  • Fig. 4 is a first round of iterative derivation of the intermediate representation in the embodiment of the present invention, including a set of effective variable nodes;
  • Fig. 5 is a second round of iterative derivation of the intermediate representation in the embodiment of the present invention, including a set of effective variable nodes;
  • FIG. 6 is a diagram of constraint relationships between input sets of various nodes in the calculation graph according to an embodiment of the present invention.
  • FIG. 7 is a schematic structural diagram of an optimization device for compiling a computation graph according to the present invention.
  • Step S1 Convert the computational graph to an intermediate representation based on a collection of nodes containing valid tensor variables
  • Step S11 Express the nodes of the computation graph containing valid tensor variables as: an equation consisting of the definition of the tensor variable and the expression using the tensor variable;
  • Step S12 defining that the nodes of the computation graph relate to a set of input nodes containing valid tensor variable nodes, the set of input nodes being the union of all predecessor nodes of the nodes of the computation graph;
  • Step S13 Define the nodes of the calculation graph about the output node set containing valid tensor variable nodes, the output node set removes and redefines the node set of valid tensor variables for the input node set, and then combines with the valid tensor variable nodes
  • the node position of the tensor variable contains the node set and union of the valid tensor variable;
  • Step S14 Obtaining the set of nodes containing valid tensor variables in the intermediate representation needs to iteratively deduce the nodes containing valid tensor variables for each node until the set of input nodes and the set of output nodes of all nodes no longer change, reaching fixed-nodes, defines the set up to fixed-nodes as said intermediate representation based on the set of nodes containing valid tensor variables.
  • Step S2 Analyzing dependencies between nodes in the computation graph
  • Step S3 constructing a work stack for storing nodes to be processed
  • the computation graph is traversed according to the topological order, and the nodes in the computation graph are sequentially pushed into the work stack.
  • Step S4 initializing the node elements included in the work stack to be in an inactive state
  • Step S5 the working stack pops up the stack top node element, uses the dependency relationship in step S2 to deduce the input node set of the stack top node element, and updates the input node set of the stack top node element obtained by the current round of iteration;
  • Step S51 popping the top node element from the work stack, that is, popping the top node element of the work stack from the stack;
  • Step S52 Using the dependencies in step S2, add the input node set of the popped top node element to the work stack, and update the input node set of the stack top node element obtained in the current round of iteration.
  • Step S6 adding the elements of the top node of the stack dependent on the step S5 to the top position of the stack in sequence, and updating the current working stack, repeating the step S5 until the working stack is cleared;
  • Step S7 using a bit vector to realize the intermediate representation of the fixed point state
  • Step S8 Allocating registers for valid tensor variables contained in nodes of the intermediate representation reaching the fixed point state;
  • Free registers will be allocated for tensor variables contained in nodes whose elements are mapped to 1 when the input node set reaches the fixed point state in step 7.
  • ⁇ ⁇ in this embodiment represents an empty set, and those marked with ⁇ are inactive;
  • tf.matmul(x, y) Indicates the matrix multiplication operation between tensor x and tensor y;
  • tf.ones(a i .shape) means to create a tensor with the same shape as tensor a i and all elements are 1;
  • tf.nn.relu(x) indicates that the tensor x is input to the rectified linear unit
  • An optimization method for computing graph compilation comprising the following steps:
  • step S1 convert the calculation graph into an intermediate representation based on a node set containing valid tensor variables
  • Step S11 Express the nodes of the computational graph containing valid tensor variables as: an equation consisting of the definition of the tensor variable and the expression using the tensor variable ;
  • FIG. 3 shows the definition process of the intermediate representation of the set of nodes containing valid variables.
  • a node V of a computation graph containing a valid tensor variable v as an equation consisting of a definition of the tensor variable v and an expression E using the tensor variable v.
  • Step S12 defining that the nodes of the computation graph relate to a set of input nodes containing valid tensor variable nodes, the set of input nodes being the union of all predecessor nodes of the nodes of the computation graph;
  • the set of input nodes about the nodes containing the effective tensor variable v is defined as the union of all the predecessor nodes V pred of the node V.
  • Step S13 Define the nodes of the calculation graph about the output node set containing the effective tensor variable node, the output node set removes the node set that redefines the effective tensor variable v for the input node set, and then combines with the effective
  • the node position of the tensor variable contains the node set and union of the valid tensor variable;
  • the set of nodes containing and defining a valid tensor variable v is expressed as: ⁇ (node V, variable v) ⁇ .
  • the set of nodes containing a redefined effective tensor variable v is expressed as: ⁇ redefinition (variable v) ⁇ , wherein the set element of the node containing a valid tensor variable v in the intermediate representation is the set of nodes and effective tensor variables
  • Two-dimensional information, such as V (effective variable v) which contains node information V and information about the effective tensor variable v contained in the node.
  • Step S14 Obtaining the set of nodes containing valid tensor variables in the intermediate representation needs to iteratively deduce the nodes containing valid tensor variables for each node until the set of input nodes and the set of output nodes of all nodes no longer change, reaching fixed-nodes, defines the set up to fixed-nodes as said intermediate representation based on the set of nodes containing valid tensor variables.
  • FIG. 4 it shows the process of deriving the node set of the intermediate representation containing valid variables in the first round of iteration.
  • V 2 _OUT ⁇ 0 x , 2 y ⁇ .
  • V 5 _OUT ⁇ 0 x , 2 y , 4 x , 5 z ⁇ .
  • FIG. 5 it shows the process of deriving the node set of the intermediate representation containing valid tensor variables in the second round of iteration.
  • V 1 _IN V 0 _OUT U
  • V 8 _OUT ⁇ 0 x , 2 y , 4 x , 7 x , 8 z ⁇ ;
  • the set of nodes on V2 is expressed as:
  • the set of nodes on V3 is expressed as:
  • the set of 4 nodes on V is expressed as:
  • the set of nodes on V5 is expressed as:
  • V 5 _IN V 3 _OUT U
  • V 4 _OUT ⁇ 0 x , 2 y , 4 x , 7 x , 8 z ⁇ ,
  • the set of nodes on V6 is expressed as:
  • a collection of V8 nodes is expressed as:
  • V 8 _IN V 6 _OUT U
  • V 7 _OUT ⁇ 0 x , 2 y , 4 x , 7 x , 5 z ⁇ ,
  • the node elements contained in the set of valid variable nodes contained in the intermediate representation will not change and reach a fixed node.
  • the set of reaching fixed points is defined as the intermediate representation based on the set of valid variable nodes.
  • step S2 analyze the dependency relationship between the nodes in the calculation graph
  • V 1 _IN ⁇ ⁇
  • V 2 _IN V 1 _IN U(V 3 _IN ⁇ V 3 ,V 5 ,V 6 ⁇ )U ⁇ V 3 ⁇ ;
  • V 1 _IN V 2 _IN;
  • V 4 _IN V 1 _IN U(V 5 _IN ⁇ V 3 ,V 5 ,V 6 ⁇ )U ⁇ V 5 ⁇ ;
  • V 5 _IN V 4 _IN;
  • V 6 _IN V 2 _IN U V 4 _IN.
  • Step S3 constructing a work stack for storing nodes to be processed
  • Step S4 initializing the node elements included in the work stack to be in an inactive state
  • Step S5 the working stack pops up the stack top node element, uses the dependency relationship in step S2 to deduce the input node set of the stack top node element, and updates the input node set of the stack top node element obtained by the current round of iteration;
  • Step S51 popping the top node element from the work stack, that is, popping the top node element of the work stack from the stack;
  • Step S52 Using the dependencies in step S2, add the input node set of the popped top node element to the work stack, and update the input node set of the stack top node element obtained in the current round of iteration.
  • Step S6 adding the elements of the top node of the stack dependent on the step S5 to the top position of the stack in sequence, and updating the current working stack, repeating the step S5 until the working stack is cleared;
  • Steps S3-S6 iteratively deduce the set of fixed points based on valid tensor variable nodes and include the following process:
  • the first step is to build a work stack that saves the nodes to be processed. Construct the work stack storing the nodes to be processed as [V 1 _IN, V 2 _IN, V 3 _IN, V 4 _IN, V 5 _IN, V 6 _IN].
  • the second step is to initialize the node elements contained in the work stack to the inactive state. Initialize the elements in the work stack to an inactive state marked with ⁇ . Table 1 shows the state of the input node set of each node in the work stack.
  • the third step is to process the element at the top of the work stack.
  • the process of processing the elements at the top of the work stack includes the following processes:
  • the working stack pops the V 1 _IN node element at the top of the stack.
  • Popping the top node element of the work stack refers to popping the V 1 _IN node element at the top of the work stack from the stack. Since the input node set of the V 1 _IN node is an empty set, the V 1 _IN node has never been activated. Update is the empty set state ⁇ ⁇ .
  • the set of nodes that depend on the V 1 _IN node being popped is added to the working stack.
  • the process of adding the node set that depends on the popped V 1 _IN node to the work stack is as follows: since the set that depends on the V 1 _IN node includes the V 2 _IN node and the V 4 _IN node, so add the dependent node set ⁇ V 2_IN , V 4_IN ⁇ .
  • the working stack pops the V 2 _IN node element at the top of the stack.
  • the working stack pops the V 3 _IN node element at the top of the stack.
  • the working stack pops the V 2 _IN node element at the top of the stack.
  • the working stack pops the V 6 _IN node element at the top of the stack.
  • the working stack pops the V 4 _IN node element at the top of the stack.
  • the working stack pops the V 5 _IN node element at the top of the stack.
  • Popping the top node element of the working stack refers to popping the V 5 _IN node element at the top of the working stack from the stack.
  • the working stack pops the V 4 _IN node element at the top of the stack.
  • the working stack pops the V 6 _IN node element at the top of the stack.
  • Popping the top node element of the working stack refers to popping the V 6 _IN node element at the top of the working stack from the stack.
  • V 4 _IN node element at the top of the stack is popped from the working stack.
  • the working stack pops the V 2 _IN node element at the top of the stack.
  • the working stack pops the V 3 _IN node element at the top of the stack.
  • the working stack pops the V 4 _IN node element at the top of the stack.
  • the working stack pops the V 5 _IN node element at the top of the stack.
  • Step S7 using a bit vector to realize the intermediate representation of the fixed point state
  • Step S8 Allocating registers for valid tensor variables contained in nodes of the intermediate representation reaching the fixed point state;
  • Free registers will be allocated for tensor variables contained in nodes whose elements map to 1 when the input node set reaches the fixed point state in step 7.
  • the present invention also provides an embodiment of an optimization device for compiling a computation graph.
  • an optimization device for computing graph compilation provided by an embodiment of the present invention includes a memory and one or more processors, executable codes are stored in the memory, and the one or more processors execute The executable code is used to implement an optimization method for compiling computation graphs in the above embodiments.
  • An embodiment of an optimization device for computing graph compilation in the present invention can be applied to any device with data processing capability, and any device with data processing capability can be a device or device such as a computer.
  • the device embodiments can be implemented by software, or by hardware or a combination of software and hardware. Taking software implementation as an example, as a device in a logical sense, it is formed by reading the corresponding computer program instructions in the non-volatile memory into the memory for operation by the processor of any device capable of data processing.
  • any device with data processing capabilities where the optimization device for computing graph compilation in the present invention is located, except for the processor and memory shown in Figure 7
  • any device with data processing capability where the device in the embodiment is usually based on the actual function of any device with data processing capability may also include other hardware. repeat.
  • the device embodiment since it basically corresponds to the method embodiment, for related parts, please refer to the part description of the method embodiment.
  • the device embodiments described above are only illustrative, and the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in One place, or it can be distributed to multiple network elements. Part or all of the modules can be selected according to actual needs to achieve the purpose of the solution of the present invention. It can be understood and implemented by those skilled in the art without creative effort.
  • An embodiment of the present invention also provides a computer-readable storage medium, on which a program is stored.
  • a program is stored.
  • an optimization method for compiling a computation graph in the above-mentioned embodiments is implemented.
  • the computer-readable storage medium may be an internal storage unit of any device capable of data processing described in any of the foregoing embodiments, such as a hard disk or a memory.
  • the computer-readable storage medium can also be an external storage device of any device with data processing capabilities, such as a plug-in hard disk, a smart media card (Smart Media Card, SMC), an SD card, and a flash memory card equipped on the device. (Flash Card) etc.
  • the computer-readable storage medium may also include both an internal storage unit of any device capable of data processing and an external storage device.
  • the computer-readable storage medium is used to store the computer program and other programs and data required by any device capable of data processing, and may also be used to temporarily store data that has been output or will be output.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Software Systems (AREA)
  • Biophysics (AREA)
  • Computing Systems (AREA)
  • Life Sciences & Earth Sciences (AREA)
  • Biomedical Technology (AREA)
  • Health & Medical Sciences (AREA)
  • Computational Linguistics (AREA)
  • Data Mining & Analysis (AREA)
  • Evolutionary Computation (AREA)
  • General Health & Medical Sciences (AREA)
  • Molecular Biology (AREA)
  • Artificial Intelligence (AREA)
  • Mathematical Physics (AREA)
  • Neurology (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

一种用于计算图编译的优化方法及装置,包括以下步骤:步骤S1:将计算图转换为中间表示;步骤S2:分析依赖关系;步骤S3:构建工作栈;步骤S4:初始化为未激活状态;步骤S5:弹出栈顶节点元素,并更新当前轮迭代输入节点集合;步骤S6:将依赖所述步骤S5的栈顶节点的元素依次添加到栈顶位置,直到工作栈清空为止;步骤S7:采用位向量实现达到不动点状态的中间表示;步骤S8:为达到不动点状态的中间表示的节点包含的有效张量变量分配寄存器。由此提供了一种解析所述中间表示节点动态地执行至不动点状态的方法,并且优化了为所述中间表示不动点状态下各节点所包含的张量变量分配空闲寄存器的实现方法,提升计算图在运行时的执行效率。

Description

一种用于计算图编译的优化方法及装置
本申请要求于2022年9月27日向中国国家知识产权局提交的发明专利申请号为202211177796.9,发明名称为“一种用于计算图编译的优化方法及装置”的中国专利申请的优先权权益,其全部内容通过引用合并于本申请。
技术领域
本发明涉及一种基于特定计算模型的计算机系统技术领域,尤其涉及一种用于计算图编译的优化方法及装置。
背景技术
随着近几年神经网络模型的落地,面向神经网络编译的技术变得越来越重要。已有的计算图编译技术仍未从全局角度分析计算图执行过程中节点之间的约束关系以及基于约束关系分析计算图节点在执行过程中不同状态下所包含的张量变量的生命周期的动态变化情况。
为此,本发明提出将计算图执行过程中节点状态的动态变化过程抽象为基于约束的集合表示方法,提供一种基于包含有效张量变量的节点集合的中间表示技术。
发明内容
本发明为了解决上述技术问题,在此提供一种用于计算图编译的优化方法及装置。
本发明采用的技术方案如下:
一种用于计算图编译的优化方法,包括以下步骤:
步骤S1:将计算图转换为基于包含有效张量变量的节点集合的中间表示;
步骤S2:分析所述计算图中节点之间的依赖关系;
步骤S3:构建保存待处理节点的工作栈;
步骤S4:初始化所述工作栈中包含的节点元素为未激活状态;
步骤S5:所述工作栈弹出栈顶节点元素,利用所述步骤S2的依赖关系推导栈顶节点元素的输入节点集合,并更新当前轮迭代所得的栈顶节点元素的输入节点集合;
步骤S6:将依赖所述步骤S5的栈顶节点的元素依次添加到栈顶位置,并更新当前工作栈,重复所述步骤S5,直到工作栈清空为止;
步骤S7:采用位向量实现达到不动点状态的中间表示;
步骤S8:为达到不动点状态的中间表示的节点包含的有效张量变量分配寄存器。
进一步地,所述步骤S1具体包括以下子步骤:
步骤S11:将包含有效张量变量的计算图的节点表示为:由张量变量的定义和使用张量变量 的表达式组成的等式;
步骤S12:定义所述计算图的节点关于包含有效张量变量节点的输入节点集合,所述输入节点集合为所述计算图的节点的所有前驱节点的并集;
步骤S13:定义所述计算图的节点关于包含有效张量变量节点的输出节点集合,所述输出节点集合为所述输入节点集合移除重定义有效张量变量的节点集合,然后再与有效张量变量的节点位置包含有效张量变量的节点集合取并集;
步骤S14:获取中间表示的包含有效张量变量节点的集合需要迭代地推导每个节点包含有效张量变量的节点,直至所有节点的所述输入节点集合和所述输出节点集合不再变化,达到固定节点,将达到固定节点的集合定义为所述基于包含有效张量变量的节点集合的中间表示。
进一步地,所述步骤S2具体为:分析推导所述计算图的各节点之间的输入节点集合之间的关系。
进一步地,所述步骤S3具体为:按照拓扑顺序遍历所述计算图,依次将所述计算图中的节点压入工作栈中。
进一步地,所述步骤S4具体为:将还未开始执行的所述计算图的各个节点初始化为未激活状态。
进一步地,所述步骤S5具体包括以下子步骤:
步骤S51:工作栈弹出栈顶节点元素,即将工作栈的栈顶节点元素弹出栈;
步骤S52:利用所述步骤S2的依赖关系,将被弹出所述栈顶节点元素的输入节点集合添加到所述工作栈,并更新当前轮迭代所得的栈顶节点元素的输入节点集合。
进一步地,所述步骤S7具体为:将所述计算图的中间表示中各个节点的输入节点集合达到不动点状态时所包含的节点元素映射为1,其他节点元素映射为0。
进一步地,所述步骤S8具体为:将为步骤7中输入节点集合达到不动点状态时所包含的节点元素映射为1的节点所包含的张量变量分配空闲寄存器。
本发明还提供一种用于计算图编译的优化装置,包括存储器和一个或多个处理器,所述存储器中存储有可执行代码,所述一个或多个处理器执行所述可执行代码时,用于实现上述实施例中任一项所述的一种用于计算图编译的优化方法。
本发明还提供一种计算机可读存储介质,其上存储有程序,该程序被处理器执行时,实现上述实施例中任一项所述的一种用于计算图编译的优化方法。
本发明的有益效果是:本发明公开了一种用于计算图编译的优化方法及装置,所述的方法是一种用于计算图编译的优化方法。本发明提出一种将计算图转换为基于包含有效变量 节点集合的中间表示,提供了一种解析所述中间表示节点动态地执行至不动点状态的方法,并且优化了为所述中间表示不动点状态下各节点所包含的张量变量分配空闲寄存器的实现方法。本发明提出的用于计算图编译的优化方法既提升了计算图在运行时的执行效率。研究人员和工程应用者开发算法模型的过程中,利用所述的一种用于计算图编译的优化方法及装置优化模型,优化了计算图的编译效率,推动了所述关系图中度神经网络模型落地应用的发展。
附图说明
图1为本发明一种用于计算图编译的优化方法的架构图;
图2为本发明实施例神经网络编译生成的计算图;
图3为本发明实施例基于集合的中间表示的定义;
图4为本发明实施例第一轮迭代推导所述中间表示的包含有效变量节点集合;
图5为本发明实施例第二轮迭代推导所述中间表示的包含有效变量节点集合;
图6为本发明实施例计算图的各个节点输入集合之间的约束关系图;
图7为本发明一种用于计算图编译的优化装置的结构示意图。
具体实施方式
以下对至少一个示范性实施例的描述实际上仅仅是说明性的,其并不作为对本发明及其应用或使用的任何限制。基于本发明中的实施例,本领域普通技术人员在没有付出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。
参见图1,一种用于计算图编译的优化方法,包括以下步骤:
步骤S1:将计算图转换为基于包含有效张量变量的节点集合的中间表示;
步骤S11:将包含有效张量变量的计算图的节点表示为:由张量变量的定义和使用张量变量的表达式组成的等式;
步骤S12:定义所述计算图的节点关于包含有效张量变量节点的输入节点集合,所述输入节点集合为所述计算图的节点的所有前驱节点的并集;
步骤S13:定义所述计算图的节点关于包含有效张量变量节点的输出节点集合,所述输出节点集合为所述输入节点集合移除重定义有效张量变量的节点集合,然后再与有效张量变量的节点位置包含有效张量变量的节点集合取并集;
步骤S14:获取中间表示的包含有效张量变量节点的集合需要迭代地推导每个节点包含有效张量变量的节点,直至所有节点的所述输入节点集合和所述输出节点集合不再变化,达到固定节点,将达到固定节点的集合定义为所述基于包含有效张量变量的节点集合的中间表示。
步骤S2:分析所述计算图中节点之间的依赖关系;
分析推导所述计算图的各节点之间的输入节点集合之间的关系。
步骤S3:构建保存待处理节点的工作栈;
按照拓扑顺序遍历所述计算图,依次将所述计算图中的节点压入工作栈中。
步骤S4:初始化所述工作栈中包含的节点元素为未激活状态;
将还未开始执行的所述计算图的各个节点初始化为未激活状态。
步骤S5:所述工作栈弹出栈顶节点元素,利用所述步骤S2的依赖关系推导栈顶节点元素的输入节点集合,并更新当前轮迭代所得的栈顶节点元素的输入节点集合;
步骤S51:工作栈弹出栈顶节点元素,即将工作栈的栈顶节点元素弹出栈;
步骤S52:利用所述步骤S2的依赖关系,将被弹出所述栈顶节点元素的输入节点集合添加到所述工作栈,并更新当前轮迭代所得的栈顶节点元素的输入节点集合。
步骤S6:将依赖所述步骤S5的栈顶节点的元素依次添加到栈顶位置,并更新当前工作栈,重复所述步骤S5,直到工作栈清空为止;
步骤S7:采用位向量实现达到不动点状态的中间表示;
将所述计算图的中间表示中各个节点的输入节点集合达到不动点状态时所包含的节点元素映射为1,其他节点元素映射为0。
步骤S8:为达到不动点状态的中间表示的节点包含的有效张量变量分配寄存器;
将为步骤7中输入节点集合达到不动点状态时所包含的节点元素映射为1的节点所包含的张量变量分配空闲寄存器。
实施例:
本实施例中的{ }表示为空集,标记为■的为未激活状态;
tf.matmul(x,y):表示张量x与张量y进行矩阵乘法操作;
tf.ones(a i.shape):表示创建一个与张量a i形状相同且所有元素都为1的张量;
tf.nn.relu(x):表示将张量x输入整流线性单元;
|x|:表示张量变量的x的模。
一种用于计算图编译的优化方法,包括以下步骤:
参见图2,其展示了神经网络编译生成的计算图,步骤S1:将计算图转换为基于包含有效张量变量的节点集合的中间表示;
步骤S11:将包含有效张量变量的计算图的节点表示为:由张量变量的定义和使用张量变量 的表达式组成的等式;
参见图3,其展示了包含有效变量的节点集合的中间表示的定义过程。将包含有效张量变量v的计算图的节点V表示为:由张量变量v的定义和使用张量变量v的表达式E组成的等式。
步骤S12:定义所述计算图的节点关于包含有效张量变量节点的输入节点集合,所述输入节点集合为所述计算图的节点的所有前驱节点的并集;
所述关于包含有效张量变量v的节点的输入节点集合定义为节点V的所有前驱节点V pred的并集。
步骤S13:定义所述计算图的节点关于包含有效张量变量节点的输出节点集合,所述输出节点集合为所述输入节点集合移除重定义有效张量变量v的节点集合,然后再与有效张量变量的节点位置包含有效张量变量的节点集合取并集;
其中所述包含定义有效张量变量v的节点集合表示为:{(节点V,变量v)}。其中包含重定义有效张量变量v的节点集合表示为:{重定义(变量v)},其中所述中间表示的包含有效张量变量v的节点的集合元素是包含节点和有效张量变量的二维信息,如V(有效变量v),其中包含了节点信息V和节点所包含的有效张量变量v的信息。
步骤S14:获取中间表示的包含有效张量变量节点的集合需要迭代地推导每个节点包含有效张量变量的节点,直至所有节点的所述输入节点集合和所述输出节点集合不再变化,达到固定节点,将达到固定节点的集合定义为所述基于包含有效张量变量的节点集合的中间表示。
获取所述中间表示的包含有效变量的节点的集合需要迭代地推导每个节点包含有效张量变量的节点,直至所有节点的所述输入节点集合和所述输出节点集合不再变化为止,也就是说直至所有集合所包含节点元素达到不动点为止。所述迭代过程如下:
参见图4,其展示了第一轮迭代推导所述中间表示的包含有效变量的节点集合的过程。
第一轮迭代,所述各节点的输入节点集合和输出节点集合的变化如下:
(1)关于V 0节点的集合表示:
1.1关于V 0节点包含张量变量的输入节点集合:V 0节点的输入节点集合为空集,表示为V 0_IN={ };
1.2关于V 0节点包含张量变量的输出节点集合:因为V 0节点定义了张量变量x,所以V 0节点的输出节点集合为包含张量变量x的V 0节点,表示为V 0_OUT={0 x}。
(2)关于V 1节点的集合表示:
2.1关于V 1节点包含张量变量的输入节点集合:按照节点顺序访问计算图的各节点,开始访 问V 1节点,V 1节点的输入节点集合等于V 0节点的输出节点集合,表示为V 1_IN={0 x};
2.2关于V 1节点包含张量变量的输出节点集合:V 1节点的输出节点集合等于V 1节点的输入节点集合,表示为V 1_OUT={0 x}。
(3)关于V 2节点的集合表示:
3.1关于V 2节点包含张量变量的输入节点集合:V 2节点的输入节点集合等于V 1节点的输出节点集合,表示为V 2_IN={0 x};
3.2关于V 2节点包含张量变量的输出节点集合:由于V 2节点定义了张量变量y,所以V 2节点的输出节点集合是取V 2节点的输入节点集合与包含定义张量变量y的V 2节点集合的并集,表示为V 2_OUT={0 x,2 y}。
(4)关于V 3节点的集合表示:
4.1关于V 3节点包含张量变量的输入节点集合:V 3节点的输入节点集合等于V 3节点的输出节点集合,表示为V 3_IN={0 x,2 y};
4.2关于V 3节点包含张量变量的输出节点集合:V 3节点的输出节点集合等于V 3节点的输入节点集合,表示为V 3_OUT={0 x,2 y}。
(5)关于V 4节点的集合表示:
5.1关于V 4节点包含张量变量的输入节点集合:V 4节点的输入节点集合等于V 3节点的输出节点集合,表示为V 4_OUT={2 y,4 x};
5.2关于V 4节点包含张量变量的输出节点集合:由于V 4节点重新定义了张量变量x,所以V 4节点的输出节点集合为V 4节点的输入节点集合移除包含张量变量x的V 0节点,然后再取与包含张量变量x定义的V 4节点集合的并集,表示为:V 4OUT={2 y,4 x}。
(6)关于V 5节点的集合表示:
6.1关于V 5节点包含张量变量的输入节点集合:V 5节点的输入节点集合是取其前驱V 3节点和V 4节点的输出节点集合的并集,表示为V 5_IN=V 3_OUT U V 4OUT={0 x,2 y,4 x};
6.2关于V 5节点包含张量变量的输出节点集合:由于V 5节点定义了张量变量z,所以V 5节点的输出节点集合为取V 5节点的输入节点集合与包含张量变量z定义的V 5节点集合的并集,表示为:V 5_OUT={0 x,2 y,4 x,5 z}。
(7)关于V 6节点的集合表示:
7.1关于V 6节点包含张量变量的输入节点集合:V 6节点的输入节点集合等于V 5节点的输出 节点集合,表示为V 6_IN={0 x,2 y,4 x,5 z};
7.2关于V 6节点包含张量变量的输出节点集合:V 6节点的输出节点集合等于V 6节点的输入节点集合,表示为V 6_OUT={0 x,2 y,4 x,5 z}。
(8)关于V 7节点的集合表示:
8.1关于V 7节点包含张量变量的输入节点集合:V 7_IN=V 6_OUT={0 x,2 y,4 x,5 z};
8.2关于V 7节点包含张量变量的输出节点集合:由于V 7节点重新定义了张量变量x,所以V 7节点的输出节点集合为V 7节点的输入节点集合移除包含张量变量x的V 0节点和V 4节点,然后再取与包含张量变量x定义的V 7节点集合的并集,表示为:V 7_OUT={2 y,5 z,7 x}。
(9)关于V 8节点的集合表示:
9.1关于V 8节点包含张量变量的输入集合:V 8节点的输入节点集合是取其前驱V 6节点和V 7节点的输出节点集合的并集,表示为V 8_IN=V 6_OUT U V 7_OUT={0 x,2 y,4 x,5 z,7 x}。
9.2关于V 8节点包含张量变量的输出集合:由于V 8节点重新定义了张量变量z,所以V 8节点的输出节点集合为V 8节点的输入节点集合移除包含张量变量z的V 5节点,然后再取与包含张量变量z定义的V 8节点集合的并集,表示为:V 8_OUT={0 x,2 y,4 x,7 x,8 z}。
(10)关于V 9节点的集合表示:
10.1关于V 9节点包含张量变量的输入节点集合:V 9节点的输入节点集合是取其前驱V 1节点的输出节点集合,表示为V 9_IN=V 1_OUT={0 x}。
参见图5,其展示了第二轮迭代推导所述中间表示的包含有效张量变量的节点集合的过程。
第二轮迭代,所述各节点的输入节点集合和输出节点集合的变化如下:
(1)关于V 0节点的集合表示:
1.1关于V 0节点包含张量变量的输入节点集合:V 0节点的输入节点集合为空集,表示为V 0_IN={ }。
1.2关于V 0节点包含张量变量的输出节点集合:因为V 0节点定义了张量变量x,所以V 0节点的输出节点集合为包含张量变量x的V 0节点,表示为V 0_OUT={0 x};
(2)关于V 1节点的集合表示:
2.1关于V 1节点包含张量变量的输入节点集合:按照节点顺序访问计算图的各节点,开始访 问V 1节点,V 1节点的输入节点集合是取其前驱V 0节点和V 8节点的输出节点集合的并集,表示为V 1_IN=V 0_OUT U V 8_OUT={0 x,2 y,4 x,7 x,8 z};
2.2关于V 1节点包含张量变量的输出节点集合:V 1节点的输出节点集合等于V 1节点的输入节点集合,表示为V 1_OUT={0 x,2 y,4 x,7 x,8 z};
关于V 2节点的集合表示为:
V 2_IN=V 1_OUT={0 x,2 y,4 x,7 x,8 z},
V 2_OUT=V 2_IN={0 x,2 y,4 x,7 x,8 z};
关于V 3节点的集合表示为:
V 3_IN=V 2_OUT={0 x,2 y,4 x,7 x,8 z},
V 3_OUT=V 3_IN={0 x,2 y,4 x,7 x,8 z};
关于V 4节点的集合表示为:
V 4_IN=V 3_OUT={0 x,2 y,4 x,7 x,8 z},,
V 4_OUT=(V 3_IN\{0 x,4 x,7 x})U{4 x}={2 y,4 x,8 z};
关于V 5节点的集合表示为:
V 5_IN=V 3_OUT U V 4_OUT={0 x,2 y,4 x,7 x,8 z},
V 5_OUT=(V 5_IN\{8 z})U{5 z}={0 x,2 y,4 x,7 x,5 z};;
关于V 6节点的集合表示为:
V 6_IN=V 5_OUT={0 x,2 y,4 x,7 x,5 z},
V 6_OUT=V 6_IN={0 x,2 y,4 x,7 x,5 z};
关于V 7节点的集合表示为:
V 7_IN=V 6_OUT={0 x,2 y,4 x,7 x,5 z},
V 7_OUT=(V 7_IN\{0 x,4 x,7 x})U{7 x}={2 y,5 z,7 x};
关于V 8节点的集合表示为:
V 8_IN=V 6_OUT U V 7_OUT={0 x,2 y,4 x,7 x,5 z},
V 8_OUT=(V 8_IN\{5 z})U{8 z}={0 x,2 y,4 x,7 x,8 z};
关于V 9节点的集合表示为:V 9IN=V 1_OUT={0 x,2 y,4 x,7 x,8 z}。
经过上述两轮迭代,所述中间表示的包含有效变量节点集合所包含节点元素不再变化,达到固定节点。将所述达到不动点的集合定义为所述基于包含有效变量节点集合的中间表示。
参见图6,其展示了计算图的各个节点的输入节点集合之间的依赖关系图,步骤S2:分析所述计算图中节点之间的依赖关系;
分析推导所述计算图的各节点之间的输入节点集合之间的关系;
因为各节点的输出节点集合可以由输入节点集合表示,所以只需要推导各节点的输入节点集合之间的关系。
所述推导图6所示的计算图的各节点的输入节点集合之间的关系包含如下过程:
V 1_IN={ };
V 2_IN=V 1_IN U(V 3_IN\{V 3,V 5,V 6})U{V 3};
V 1_IN=V 2_IN;
V 4_IN=V 1_IN U(V 5_IN\{V 3,V 5,V 6})U{V 5};
V 5_IN=V 4_IN;
V 6_IN=V 2_IN U V 4_IN。
步骤S3:构建保存待处理节点的工作栈;
按照拓扑顺序遍历所述计算图,依次将所述计算图中的节点压入工作栈中;
步骤S4:初始化所述工作栈中包含的节点元素为未激活状态;
将还未开始执行的所述计算图的各个节点初始化为未激活状态;
步骤S5:所述工作栈弹出栈顶节点元素,利用所述步骤S2的依赖关系推导栈顶节点元素的输入节点集合,并更新当前轮迭代所得的栈顶节点元素的输入节点集合;
步骤S51:工作栈弹出栈顶节点元素,即将工作栈的栈顶节点元素弹出栈;
步骤S52:利用所述步骤S2的依赖关系,将被弹出所述栈顶节点元素的输入节点集合添加到所述工作栈,并更新当前轮迭代所得的栈顶节点元素的输入节点集合。
步骤S6:将依赖所述步骤S5的栈顶节点的元素依次添加到栈顶位置,并更新当前工作栈,重复所述步骤S5,直到工作栈清空为止;
步骤S3-步骤S6迭代地推导基于包含有效张量变量节点的不动点集合包含如下过程:
第一步,构建保存待处理节点的工作栈。将所述保存待处理节点的工作栈构建为[V 1_IN,V 2_IN,V 3_IN,V 4_IN,V 5_IN,V 6_IN]。
第二步,初始化工作栈中包含的节点元素为未激活状态。将所述工作栈中元素初始化为标记为■的未激活状态。如表1所示工作栈中各个节点的输入节点集合的状态。
表1
Figure PCTCN2022124001-appb-000001
第三步,处理位于工作栈栈顶的元素。所述处理位于工作栈栈顶的元素过程包含如下过程:
第一,工作栈弹出栈顶V 1_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 1_IN节点元素弹出栈,由于V 1_IN节点的输入节点集合是空集,所以V 1_IN节点从未激活状态■更新为空集状态{ }。
第二,将依赖被弹出V 1_IN节点的节点集合添加到工作栈。所述将依赖被弹出V 1_IN节点的节点集合添加到工作栈的过程如下:由于依赖V 1_IN节点的集合包含V 2_IN节点和V 4_IN节点,所以向栈顶添加依赖节点集合{V 2_IN,V 4_IN}。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表2所示。
表2
Figure PCTCN2022124001-appb-000002
第三,工作栈弹出栈顶V 2_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 2_IN节点元素弹出栈,根据V 2_IN=V 1_IN U(V 3_IN\{V 3,V 5,V 6})U{V 3},而且V 1_IN={ },推导出V 2_IN={V 3}。所以V 2_IN节点从未激活状态■更新为{V 3}状态。
第四,将依赖被弹出V 2_IN节点的节点集合添加到工作栈。所述将依赖被弹出V 2_IN节点的节点集合添加到工作栈的过程如下:由于依赖V 2_IN节点的集合包含V 3_IN节点和V 6_IN节点,所以向栈顶添加依赖节点集合{V 3_IN,V 6_IN}。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表3所示。
表3
Figure PCTCN2022124001-appb-000003
第五,工作栈弹出栈顶V 3_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 3_IN节点元素弹出栈,根据V 3_IN=V 2_IN=V 1_IN U(V 3_IN\{V 3,V 5,V 6})U{V 3},而且V 1_IN={ },推导出V 3_IN={V 3}。所以V 3_IN节点从未激活状态■更新为{V 3}状态。
第六,将依赖被弹出V 3_IN节点的节点集合添加到工作栈。所述将依赖被弹出V 3_IN节点的节点集合添加到工作栈的过程如下:由于依赖V 3_IN节点的集合包含V 2_IN节点,所以向栈顶添加依赖节点集合{V 2_IN}。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表4所示。
表4
Figure PCTCN2022124001-appb-000004
第七,工作栈弹出栈顶V 2_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 2_IN节点元素弹出栈,根据V 2_IN=V 1_IN U(V 3_IN\{V 3,V 5,V 6})U{V 3},而且V 1_IN={ },推导出V 2_IN={V 3}。由于V 2_IN节点的集合元素没变,所以V 2_IN节点保持激活状态为{V 3}状态不变。
第八,将依赖被弹出V 2_IN节点的节点集合添加到工作栈。由于V 2_IN节点的集合元素没变,所以没有依赖V 2_IN节点的节点集合被添加到工作栈中。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表5所示。
表5
Figure PCTCN2022124001-appb-000005
第九,工作栈弹出栈顶V 6_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 6_IN节点元素弹出栈,根据V 6_IN=V 2_IN U V 4_IN,而且V 2_IN={V 3},推导出V 6_IN={V 3}。所以V 6_IN节点从未激活状态■更新为{V 3}状态。
第十,将依赖被弹出V 6_IN节点的节点集合添加到工作栈。由于没有依赖V 6_IN节点的其他节点,所以没有依赖V 6_IN节点的节点集合被添加到工作栈中。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表6所示。
表6
Figure PCTCN2022124001-appb-000006
第十一,工作栈弹出栈顶V 4_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 4_IN节点元素弹出栈,根据V 4_IN=V 1_IN U(V 5_IN\{V 3,V 5,V 6})U{V 5},而且V 1_IN={ },推导出V 4_IN={V 5}。所以V 4_IN节点从未激活状态■更新为{V 5}状态。
第十二,将依赖被弹出V 4_IN节点的节点集合添加到工作栈。由于依赖V 4_IN节点的集合包含V 5_IN节点和V 6_IN节点,所以向栈顶添加依赖节点集合{V 5_IN,V 6_IN}。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表7所示。
表7
Figure PCTCN2022124001-appb-000007
第十三,工作栈弹出栈顶V 5_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 5_IN节点元素弹出栈,根据V 5_IN=V 4_IN={V 5},推导出V 5_IN={V 5}。所以V 5_IN节点从未激活状态■更新为{V 5}状态。
第十四,将依赖被弹出V 5_IN节点的节点集合添加到工作栈。由于依赖V 5_IN节点的集合包含V 4_IN节点,所以向栈顶添加依赖节点集合{V 4_IN}。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表8所示。
表8
Figure PCTCN2022124001-appb-000008
第十五,工作栈弹出栈顶V 4_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 4_IN节点元素弹出栈,根据V 4_IN=V 1_IN U(V 5_IN\{V 3,V 5,V 6})U{V 5},而且V 1_IN={ },推导出V 4_IN={V 5}。所以V 4_IN节点保持激活状态为{V 5}状态。
第十六,将依赖被弹出V 4_IN节点的节点集合添加到工作栈。由于V 4_IN节点的集合元素没变,所以没有依赖V 4_IN节点的节点集合被添加到工作栈中。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表9所示。
表9
Figure PCTCN2022124001-appb-000009
第十七,工作栈弹出栈顶V 6_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 6_IN节点元素弹出栈,根据V 6_IN=V 2_IN U V 4_IN,推导出V 6_IN={V 3,V 5}。所以V 6_IN节点的激活状态更新为{V 3,V 5}状态。
第十八,将依赖被弹出V 6_IN节点的节点集合添加到工作栈。由于没有依赖V 6_IN节点的其他节点,所以没有依赖V 6_IN节点的节点集合被添加到工作栈中。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表10所示。
表10
Figure PCTCN2022124001-appb-000010
第十九,工作栈弹出栈顶V 4_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 4_IN节点元素弹出栈,根据V 4_IN=V 1_IN U(V 5_IN\{V 3,V 5,V 6})U{V 5},而且V 1_IN={ },推导出V 4_IN={V 5}。所以V 4_IN节点保持激活状态为{V 5}状态。
第二十,将依赖被弹出V 4_IN节点的节点集合添加到工作栈。由于V 4_IN节点的集 合元素没变,所以没有依赖V 4_IN节点的节点集合被添加到工作栈中。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表11所示。
表11
Figure PCTCN2022124001-appb-000011
第二十一,工作栈弹出栈顶V 2_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 2_IN节点元素弹出栈,根据V 2_IN=V 1_IN U(V 3_IN\{V 3,V 5,V 6})U{V 3},而且V 1_IN={ },推导出V 2_IN={V 3}。由于V 2_IN节点的集合元素没变,所以V 2_IN节点保持激活状态为{V 3}状态不变。
第二十二,将依赖被弹出V 2_IN节点的节点集合添加到工作栈。由于V 2_IN节点的集合元素没变,所以没有依赖V 2_IN节点的节点集合被添加到工作栈中。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表12所示。
表12
Figure PCTCN2022124001-appb-000012
第二十三,工作栈弹出栈顶V 3_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 3_IN节点元素弹出栈,根据V 3_IN=V 2_IN=V 1_IN U(V 3_IN\{V 3,V 5,V 6})U{V 3},而且V 1_IN={ },推导出V 3_IN。所以V 3_IN节点的激活状态保持为{V 3}状态。
第二十四,将依赖被弹出V 3_IN节点的节点集合添加到工作栈。所述将依赖被弹出V 3_IN节点的节点集合添加到工作栈的过程如下:由于V 3_IN节点的集合元素没变,所以没有依赖V 3_IN节点的节点集合被添加到工作栈中。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表13所示。
表13
Figure PCTCN2022124001-appb-000013
第二十五,工作栈弹出栈顶V 4_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 4_IN节点元素弹出栈,根据V 4_IN=V 1_IN U(V 5_IN\{V 3,V 5,V 6})U{V 5},而且V 1_IN={ },推导出V 4_IN={V 5}。所以V 4_IN节点保持激活状态为{V 5}状态。
第二十六,将依赖被弹出V 4_IN节点的节点集合添加到工作栈。由于V 4_IN节点的集合元素没变,所以没有依赖V 4_IN节点的节点集合被添加到工作栈中。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表14所示。
表14
Figure PCTCN2022124001-appb-000014
第二十七,工作栈弹出栈顶V 5_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 5_IN节点元素弹出栈,根据V 5_IN=V 4_IN={V 5},推导出
Figure PCTCN2022124001-appb-000015
所以V 5_IN点的激活状态保持为{V 5}状态。
第二十八,将依赖被弹出V 5_IN节点的节点集合添加到工作栈。由于V 5_IN节点的集合元素没变,所以没有依赖V 5IN节点的节点集合被添加到工作栈中。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表15所示。
表15
Figure PCTCN2022124001-appb-000016
第二十九,工作栈弹出栈顶V 6_IN节点元素。所述工作栈弹出栈顶节点元素是指将工作栈的栈顶的V 6_IN节点元素弹出栈,根据V 6_IN=V 2_IN U V 4_IN,而且V 2_IN={V 3},V 4_IN={V 5},推导出V 6_IN={V 3,V 5}。所以V 6_IN节点的激活状态保持为{V 3,V 5}状态。
第三十,将依赖被弹出V 6_IN节点的节点集合添加到工作栈。由于没有依赖V 6_IN节点的其他节点,所以没有依赖V 6_IN节点的节点集合被添加到工作栈中。经过上述步骤,工作栈中各个节点的输入节点集合的状态更新为表16所示。
表16
Figure PCTCN2022124001-appb-000017
步骤S7:采用位向量实现达到不动点状态的中间表示;
将所述计算图的中间表示中各个节点的输入节点集合达到不动点状态时所包含的节点元素映射为1,其他节点元素映射为0。
参见表17,其展示了达到不动点状态的中间表示的位向量表示。
表17
Figure PCTCN2022124001-appb-000018
步骤S8:为达到不动点状态的中间表示的节点包含的有效张量变量分配寄存器;
将为步骤7中输入节点集合达到不动点状态时所包含的节点元素映射为1的节点所包含的张 量变量分配空闲寄存器。
与前述一种用于计算图编译的优化方法的实施例相对应,本发明还提供了一种用于计算图编译的优化装置的实施例。
参见图7,本发明实施例提供的一种用于计算图编译的优化装置,包括存储器和一个或多个处理器,所述存储器中存储有可执行代码,所述一个或多个处理器执行所述可执行代码时,用于实现上述实施例中的一种用于计算图编译的优化方法。
本发明一种用于计算图编译的优化装置的实施例可以应用在任意具备数据处理能力的设备上,该任意具备数据处理能力的设备可以为诸如计算机等设备或装置。装置实施例可以通过软件实现,也可以通过硬件或者软硬件结合的方式实现。以软件实现为例,作为一个逻辑意义上的装置,是通过其所在任意具备数据处理能力的设备的处理器将非易失性存储器中对应的计算机程序指令读取到内存中运行形成的。从硬件层面而言,如图7所示,为本发明一种用于计算图编译的优化装置所在任意具备数据处理能力的设备的一种硬件结构图,除了图7所示的处理器、内存、网络接口、以及非易失性存储器之外,实施例中装置所在的任意具备数据处理能力的设备通常根据该任意具备数据处理能力的设备的实际功能,还可以包括其他硬件,对此不再赘述。
上述装置中各个单元的功能和作用的实现过程具体详见上述方法中对应步骤的实现过程,在此不再赘述。
对于装置实施例而言,由于其基本对应于方法实施例,所以相关之处参见方法实施例的部分说明即可。以上所描述的装置实施例仅仅是示意性的,其中所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部模块来实现本发明方案的目的。本领域普通技术人员在不付出创造性劳动的情况下,即可以理解并实施。
本发明实施例还提供一种计算机可读存储介质,其上存储有程序,该程序被处理器执行时,实现上述实施例中的一种用于计算图编译的优化方法。
所述计算机可读存储介质可以是前述任一实施例所述的任意具备数据处理能力的设备的内部存储单元,例如硬盘或内存。所述计算机可读存储介质也可以是任意具备数据处理能力的设备的外部存储设备,例如所述设备上配备的插接式硬盘、智能存储卡(Smart Media Card,SMC)、SD卡、闪存卡(Flash Card)等。进一步的,所述计算机可读存储介质还可以既包括任意具备数据处理能力的设备的内部存储单元也包括外部存储设备。所述计算机可读 存储介质用于存储所述计算机程序以及所述任意具备数据处理能力的设备所需的其他程序和数据,还可以用于暂时地存储已经输出或者将要输出的数据。
以上所述仅为本发明的优选实施例而已,并不用于限制本发明,对于本领域的技术人员来说,本发明可以有各种更改和变化。凡在本发明的精神和原则之内,所作的任何修改、等同替换、改进等,均应包含在本发明的保护范围之内。

Claims (10)

  1. 一种用于计算图编译的优化方法,其特征在于,包括以下步骤:
    步骤S1:将计算图转换为基于包含有效张量变量的节点集合的中间表示;
    步骤S2:分析所述计算图中节点之间的依赖关系;
    步骤S3:构建保存待处理节点的工作栈;
    步骤S4:初始化所述工作栈中包含的节点元素为未激活状态;
    步骤S5:所述工作栈弹出栈顶节点元素,利用所述步骤S2的依赖关系推导栈顶节点元素的输入节点集合,并更新当前轮迭代所得的栈顶节点元素的输入节点集合;
    步骤S6:将依赖所述步骤S5的栈顶节点的元素依次添加到栈顶位置,并更新当前工作栈,重复所述步骤S5,直到工作栈清空为止;
    步骤S7:采用位向量实现达到不动点状态的中间表示;
    步骤S8:为达到不动点状态的中间表示的节点包含的有效张量变量分配寄存器。
  2. 如权利要求1所述的用于计算图编译的优化方法,其特征在于,所述步骤S1具体包括以下子步骤:
    步骤S11:将包含有效张量变量的计算图的节点表示为:由张量变量的定义和使用张量变量的表达式组成的等式;
    步骤S12:定义所述计算图的节点关于包含有效张量变量节点的输入节点集合,所述输入节点集合为所述计算图的节点的所有前驱节点的并集;
    步骤S13:定义所述计算图的节点关于包含有效张量变量节点的输出节点集合,所述输出节点集合为所述输入节点集合移除重定义有效张量变量的节点集合,然后再与有效张量变量的节点位置包含有效张量变量的节点集合取并集;
    步骤S14:获取中间表示的包含有效张量变量节点的集合需要迭代地推导每个节点包含有效张量变量的节点,直至所有节点的所述输入节点集合和所述输出节点集合不再变化,达到固定节点,将达到固定节点的集合定义为所述基于包含有效张量变量的节点集合的中间表示。
  3. 如权利要求2所述的用于计算图编译的优化方法,其特征在于,所述步骤S2具体为:分析推导所述计算图的各节点之间的输入节点集合之间的关系。
  4. 如权利要求1所述的用于计算图编译的优化方法,其特征在于,所述步骤S3具体为:按照拓扑顺序遍历所述计算图,依次将所述计算图中的节点压入工作栈中。
  5. 如权利要求1所述的用于计算图编译的优化方法,其特征在于,所述步骤S4具体为:将还未开始执行的所述计算图的各个节点初始化为未激活状态。
  6. 如权利要求2所述的用于计算图编译的优化方法,其特征在于,所述步骤S5具体包括以 下子步骤:
    步骤S51:工作栈弹出栈顶节点元素,即将工作栈的栈顶节点元素弹出栈;
    步骤S52:利用所述步骤S2的依赖关系,将被弹出所述栈顶节点元素的输入节点集合添加到所述工作栈,并更新当前轮迭代所得的栈顶节点元素的输入节点集合。
  7. 如权利要求1所述的用于计算图编译的优化方法,其特征在于,所述步骤S7具体为:将所述计算图的中间表示中各个节点的输入节点集合达到不动点状态时所包含的节点元素映射为1,其他节点元素映射为0。
  8. 如权利要求7所述的用于计算图编译的优化方法,其特征在于,所述步骤S8具体为:将为步骤7中输入节点集合达到不动点状态时所包含的节点元素映射为1的节点所包含的张量变量分配空闲寄存器。
  9. 一种用于计算图编译的优化装置,其特征在于,包括存储器和一个或多个处理器,所述存储器中存储有可执行代码,所述一个或多个处理器执行所述可执行代码时,用于实现权利要求1-8中任一项所述的用于计算图编译的优化方法。
  10. 一种计算机可读存储介质,其特征在于,其上存储有程序,该程序被处理器执行时,实现权利要求1-8中任一项所述的用于计算图编译的优化方法。
PCT/CN2022/124001 2022-09-27 2022-10-09 一种用于计算图编译的优化方法及装置 WO2023082901A1 (zh)

Priority Applications (1)

Application Number Priority Date Filing Date Title
US17/992,814 US20240127027A1 (en) 2022-09-27 2022-11-22 Optimization method and apparatus for compiling computation graph

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202211177796.9A CN115268936B (zh) 2022-09-27 2022-09-27 一种用于计算图编译的优化方法及装置
CN202211177796.9 2022-09-27

Related Child Applications (1)

Application Number Title Priority Date Filing Date
US17/992,814 Continuation US20240127027A1 (en) 2022-09-27 2022-11-22 Optimization method and apparatus for compiling computation graph

Publications (1)

Publication Number Publication Date
WO2023082901A1 true WO2023082901A1 (zh) 2023-05-19

Family

ID=83756194

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2022/124001 WO2023082901A1 (zh) 2022-09-27 2022-10-09 一种用于计算图编译的优化方法及装置

Country Status (3)

Country Link
US (1) US20240127027A1 (zh)
CN (1) CN115268936B (zh)
WO (1) WO2023082901A1 (zh)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116167461B (zh) * 2023-04-21 2023-07-11 之江实验室 一种模型训练的方法、装置、存储介质及电子设备

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US5107418A (en) * 1990-06-11 1992-04-21 Supercomputer Systems Limited Partnership Method for representing scalar data dependences for an optimizing compiler
CN110766147A (zh) * 2018-07-25 2020-02-07 赛灵思公司 神经网络编译器架构及编译方法
CN112465108A (zh) * 2020-11-11 2021-03-09 上海交通大学 一种面向存算一体平台的神经网络编译方法
CN114186687A (zh) * 2022-02-17 2022-03-15 之江实验室 一种面向神经网络模型计算的中间表示方法和装置

Family Cites Families (30)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US5659754A (en) * 1995-03-31 1997-08-19 Sun Microsystems, Inc. Method and apparatus for an improved optimizing compiler
WO2009107416A1 (ja) * 2008-02-27 2009-09-03 日本電気株式会社 グラフ構造変化検出装置、グラフ構造変化検出方法およびプログラム
CN102012833A (zh) * 2010-11-09 2011-04-13 北京神舟航天软件技术有限公司 基于编译过程中间结果的静态堆栈检测方法
US9081583B2 (en) * 2012-08-23 2015-07-14 National Instruments Corporation Compile time execution
US11151446B2 (en) * 2015-10-28 2021-10-19 Google Llc Stream-based accelerator processing of computational graphs
CN108292374B (zh) * 2015-11-09 2022-04-15 谷歌有限责任公司 训练表示为计算图的神经网络
US10956500B2 (en) * 2017-01-19 2021-03-23 Google Llc Dynamic-length stateful tensor array
WO2019191578A1 (en) * 2018-03-30 2019-10-03 Wave Computing, Inc. Data flow graph computation for machine learning
CN110321999B (zh) * 2018-03-30 2021-10-01 赛灵思电子科技(北京)有限公司 神经网络计算图优化方法
GB2580348A (en) * 2019-01-03 2020-07-22 Graphcore Ltd Compilation method
WO2020182989A1 (en) * 2019-03-13 2020-09-17 Deepmind Technologies Limited Scheduling computation graphs using neural networks
GB2582785A (en) * 2019-04-02 2020-10-07 Graphcore Ltd Compiling a program from a graph
US11100167B2 (en) * 2019-05-06 2021-08-24 Advanced New Technologies Co., Ltd. Obtaining dynamic embedding vectors of nodes in relationship graphs
CN110245269B (zh) * 2019-05-06 2021-01-05 创新先进技术有限公司 获取关系网络图中节点的动态嵌入向量的方法和装置
US10963301B2 (en) * 2019-07-17 2021-03-30 Google Llc Scheduling operations on a computation graph
CN110941494A (zh) * 2019-12-02 2020-03-31 哈尔滨工程大学 一种面向深度学习的gpu并行计算的数据处理方法
CN111338635B (zh) * 2020-02-20 2023-09-12 腾讯科技(深圳)有限公司 计算图的图编译方法、装置、设备及存储介质
CN111460815B (zh) * 2020-03-16 2022-04-01 平安科技(深圳)有限公司 规则处理方法、装置、介质及电子设备
WO2021248138A1 (en) * 2020-06-05 2021-12-09 Google Llc Learned graph optimizations for compilers
US11698779B2 (en) * 2020-09-01 2023-07-11 Ansys, Inc. Systems using computation graphs for flow solvers
CN112200297B (zh) * 2020-09-04 2024-03-01 星宸科技股份有限公司 神经网络优化方法、装置及处理器
CN114327844A (zh) * 2020-09-29 2022-04-12 华为技术有限公司 内存分配方法、相关设备及计算机可读存储介质
WO2022087788A1 (zh) * 2020-10-26 2022-05-05 华为技术有限公司 一种神经网络编译优化方法和相关装置
CN112711422B (zh) * 2020-12-31 2024-01-19 北京清微智能科技有限公司 一种神经网络编译的优化方法及系统
CN113050951A (zh) * 2021-03-31 2021-06-29 上海天旦网络科技发展有限公司 基于计算图的协议描述和解码方法
US11494321B1 (en) * 2021-09-30 2022-11-08 Amazon Technologies, Inc. State buffer memloc reshaping
US20230123811A1 (en) * 2021-10-15 2023-04-20 Nvidia Corporation Techniques for inferring information
CN114035916A (zh) * 2021-11-01 2022-02-11 寒武纪行歌(南京)科技有限公司 计算图的编译、调度方法及相关产品
CN114461351B (zh) * 2022-04-13 2022-06-17 之江实验室 一种用于神经网络计算的动态图执行方法及装置
CN115034358A (zh) * 2022-05-17 2022-09-09 北京灵汐科技有限公司 神经网络计算图的处理方法及处理装置

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US5107418A (en) * 1990-06-11 1992-04-21 Supercomputer Systems Limited Partnership Method for representing scalar data dependences for an optimizing compiler
CN110766147A (zh) * 2018-07-25 2020-02-07 赛灵思公司 神经网络编译器架构及编译方法
CN112465108A (zh) * 2020-11-11 2021-03-09 上海交通大学 一种面向存算一体平台的神经网络编译方法
CN114186687A (zh) * 2022-02-17 2022-03-15 之江实验室 一种面向神经网络模型计算的中间表示方法和装置

Also Published As

Publication number Publication date
US20240127027A1 (en) 2024-04-18
CN115268936B (zh) 2022-12-27
CN115268936A (zh) 2022-11-01

Similar Documents

Publication Publication Date Title
Lee et al. {PRETZEL}: Opening the black box of machine learning prediction serving systems
US8365142B2 (en) Hypergraph implementation
WO2021000970A1 (zh) 深度学习算法的编译方法、装置及相关产品
US6832369B1 (en) Object oriented method and apparatus for class variable initialization
Deumens et al. Software design of ACES III with the super instruction architecture
US20230177312A1 (en) Method for distributed type training adaptation and apparatus in deep learning framework and ai accelerator card
Seinstra et al. A software architecture for user transparent parallel image processing
WO2024021192A1 (zh) 一种用于神经网络计算的图优化方法和装置
Brock et al. BCL: A cross-platform distributed data structures library
US7386843B2 (en) Method and system for register allocation
WO2023093185A1 (zh) 一种用于神经网络计算的数据流动方法和装置
CN109154908B (zh) 具有精确垃圾收集报告的动态大小的局部
US20220114429A1 (en) Method and device for generating operation data and related product
WO2023082901A1 (zh) 一种用于计算图编译的优化方法及装置
WO2024065867A1 (zh) 一种用于神经网络编译的内存优化方法及装置
US11461142B2 (en) Automatic memory management method, corresponding micro-controller unit and computer program product
CN114330735A (zh) 处理机器学习模型的方法、电子设备和计算机程序产品
US11861331B1 (en) Scaling high-level statistical languages to large, distributed datasets
Williams et al. Libfork: portable continuation-stealing with stackless coroutines
EP2972837B1 (en) Dynamic memory management for a virtual supercomputer
WO2024065866A1 (zh) 一种用于计算图编译的中间表示方法及装置
Chawdhary et al. Compact difference bound matrices
Thoman et al. Command Horizons: Coalescing Data Dependencies While Maintaining Asynchronicity
Jamieson et al. Compact native code generation for dynamic languages on micro-core architectures
CN115269205B (zh) 一种面向神经网络计算的内存优化方法和装置

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 22891702

Country of ref document: EP

Kind code of ref document: A1