Neural network model calculation method and device

By representing the dynamic dimension tensor in the neural network model as a dimension calculation expression related to the operator's calculation logic and data dependence, the problem of low efficiency in obtaining dynamic dimension information is solved and the computational efficiency of the neural network model is improved.

WO2024222527A1PCT designated stage expired Publication Date: 2024-10-31ALIBABA CLOUD COMPUTING CO LTD

Patent Information

Application Number
PCT/CN2024/088075
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-04-27
Filing Date
2024-04-16
Publication Date
2024-10-31

AI Technical Summary

Technical Problem

The dimensional information acquisition efficiency of dynamic dimension tensors in existing neural network models is low, resulting in a reduction in overall computing efficiency.

Method used

By representing the information of dynamic dimension tensors as dimension calculation expressions related to the calculation logic and data dependence of operators, each tensor is allowed to calculate its dimension information independently, avoiding the need to derive the dimensions of each tensor from front to back.

Benefits of technology

It improves the efficiency of obtaining tensor dimension information and improves the overall computing efficiency of the neural network model.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024088075_31102024_PF_FP_ABST
    Figure CN2024088075_31102024_PF_FP_ABST
Patent Text Reader

Abstract

One or more embodiments of the present description provide a neural network model calculation method and device. The method comprises: obtaining a computational graph corresponding to a neural network model, wherein nodes in the computational graph represent operators in the neural network model, and edges in the computational graph represent data dependencies of the operators; in the case that dimension information of any tensor participating in the operation in the neural network model is dynamic, using a dimension calculation expression related to the computational logic and / or the data dependencies of the operators to represent the dimension information of said tensor.
Need to check novelty before this filing date? Find Prior Art

Description

A calculation method and device for a neural network model

[0001] This application claims priority to the Chinese patent application filed with the China Patent Office on April 27, 2023, with application number 202310501548.3 and application name “A calculation method and device for a neural network model”, the entire contents of which are incorporated by reference into this application. Technical Field

[0002] One or more embodiments of this specification relate to the field of neural networks, and more particularly, to a method and apparatus for calculating a neural network model. Background Art

[0003] Neural network models are composed of computational units called operators (OPs). Operators compute data in containers called tensors, including input and output tensors. In neural network models, tensors can be understood as multidimensional arrays, so they contain dimensional information. Depending on whether the dimensional information changes, dimensional information can be divided into static and dynamic dimensions. Static dimensions refer to dimensional information that remains unchanged, while dynamic dimensions refer to dimensional information that changes as the neural network model runs.

[0004] In related technologies, since the dimensional information of each tensor is related to each other, for tensors with dynamic dimensional information, it is necessary to derive the dimensional information of each tensor from the front to the back according to the running order of the neural network model to obtain the corresponding calculation data. This method requires determining the dimensional information of the next tensor after determining the dimensional information of the previous tensor, which makes the efficiency of obtaining dynamic dimensional information low, thereby reducing the overall computational efficiency of the neural network model. Therefore, a calculation method for a neural network model is needed to improve the overall computational efficiency of the neural network model.

[0005] Summary of the Invention

[0006] In view of this, one or more embodiments of this specification provide a method and device for calculating a neural network model, which can address the deficiencies in the related art.

[0007] To achieve the above objectives, one or more embodiments of this specification provide the following technical solutions:

[0008] According to a first aspect of one or more embodiments of this specification, a method for calculating a neural network model is proposed, the method comprising:

[0009] Obtaining a computational graph corresponding to a neural network model, where nodes in the computational graph represent operators in the neural network model, and edges in the computational graph represent data dependencies between operators;

[0010] When the dimension information of any tensor involved in the operation in the neural network model is a dynamic dimension, the dimension information of any tensor is represented as a dimension calculation expression related to the calculation logic of the operator and / or the data dependency.

[0011] According to a second aspect of one or more embodiments of this specification, a computing device for a neural network model is provided, the device comprising:

[0012] An acquisition unit: acquiring a computational graph corresponding to a neural network model, wherein nodes in the computational graph represent operators in the neural network model, and edges in the computational graph represent data dependencies between operators;

[0013] Representation unit: When the dimensional information of any tensor participating in the operation in the neural network model is a dynamic dimension, the dimensional information of any tensor is represented as a dimensional calculation expression related to the calculation logic of the operator and / or the data dependency.

[0014] According to a third aspect of one or more embodiments of this specification, an electronic device is provided, including:

[0015] processor;

[0016] a memory for storing processor-executable instructions;

[0017] The processor implements the steps of the method described in the first aspect by running the executable instructions.

[0018] According to a fourth aspect of one or more embodiments of this specification, a computer-readable storage medium is provided, on which computer instructions are stored. When the instructions are executed by a processor, the steps of the method described in the first aspect are implemented.

[0019] According to a fifth aspect of one or more embodiments of this specification, a computer program is proposed, which, when executed by a processor, implements the steps of the method described in the first aspect.

[0020] As can be seen from the above technical solutions, the calculation method of the neural network model provided by one or more embodiments of this specification, by representing the dynamic dimension of the tensor as a dimension calculation expression related to the calculation logic of the operator and / or the data dependency between operators, can make the dimension calculation expression include the data dependency relationship between any tensor and other tensors, and / or, include the operator calculation logic corresponding to the node related to any tensor. Therefore, in the subsequent compilation process, it is only necessary to calculate the dimension information of each tensor separately according to the dimension calculation expression of each tensor, without having to derive the dimension information of each tensor from front to back, thereby effectively improving the efficiency of obtaining the dimension information of each tensor, and thus improving the calculation efficiency of the neural network model. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] FIG1 is an architecture diagram of a computing system of a neural network model provided by an exemplary embodiment.

[0022] FIG2 is a flowchart of a method for calculating a neural network model provided by an exemplary embodiment.

[0023] FIG3 is a schematic diagram of a simple calculation graph provided by an exemplary embodiment.

[0024] FIG4 is a schematic diagram of decomposing a complex operator provided by an exemplary embodiment.

[0025] FIG5 is a schematic diagram of a representation dimension calculation expression provided by an exemplary embodiment.

[0026] FIG6 is a schematic diagram of a fusion type conversion operator provided by an exemplary embodiment.

[0027] FIG7 is a schematic diagram of a mobile non-computation-intensive operator provided by an exemplary embodiment.

[0028] FIG8 is a schematic diagram of a code generation method provided by an exemplary embodiment.

[0029] FIG9 is a schematic structural diagram of a device provided by an exemplary embodiment.

[0030] FIG10 is a block diagram of a computing device for a neural network model provided by an exemplary embodiment. DETAILED DESCRIPTION

[0031] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The implementations described in the following exemplary embodiments are not intended to represent all implementations consistent with one or more embodiments of this specification. Rather, they are merely examples of apparatuses and methods consistent with certain aspects of one or more embodiments of this specification, as detailed in the appended claims.

[0032] It should be noted that in other embodiments, the steps of the corresponding method are not necessarily performed in the order shown and described in this specification. In some other embodiments, the method may include more or fewer steps than those described in this specification. In addition, a single step described in this specification may be broken down into multiple steps for description in other embodiments, and multiple steps described in this specification may be combined into a single step for description in other embodiments.

[0033] The user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.

[0034] To further illustrate one or more embodiments of this specification, the following examples are provided:

[0035] Figure 1 is an architectural diagram of a computing system of a neural network model provided by an exemplary embodiment. As shown in Figure 1, the architectural diagram includes: a neural network model 11 and a compiler 12. The neural network model 11 is the computing object of this specification. In fact, any neural network model involved in the field of machine learning can use the computing method of this specification. Machine learning refers to a method in which a computer uses existing data or experience to train a certain model and uses the model to make predictions. The research of machine learning includes many directions, for example, deep learning is an important branch. Deep learning is the use of deep neural networks to characterize and learn data so that the model has a deeper understanding of the data. Taking deep learning as an example, its commonly used neural network algorithms include RNN (Recurrent Neural Network), CNN (Convolution Neural Network), etc. The corresponding neural network models can be calculated through the technical solutions of this specification.

[0036] The compiler 12 is a tool that compiles the neural network model 11 into optimized code for the deep learning hardware platform, and plays an important role in the design of the machine learning framework. In machine learning, the compiler must not only adapt to the changes in the model algorithm to meet the continuous research needs of developers, but also meet the rigid demands of diversity in the final output and meet the resource requirements of different deployment environments. Therefore, an excellent compiler can achieve efficient deployment of neural network models on deep learning hardware. In related technologies, compilers such as TVM or XLA are usually used to calculate neural network models.

[0037] The two modules, the neural network model 11 and the compiler 12, are logically independent of each other and can be physically deployed on the same electronic device or on multiple electronic devices. An electronic device refers to a device with data processing capabilities, such as a server or a terminal. The server can be a physical server comprising an independent host, or the server can be a virtual server hosted by a host cluster, or the server can be a cloud server. Terminals include but are not limited to smartphones, tablet computers, PDAs (Personal Digital Assistants), wearable devices (such as smart glasses, smart watches, etc.), etc.

[0038] In neural network models, tensors are containers for data. Tensors are actually multidimensional arrays, so they contain dimensional information. This dimensional information can be categorized as static or dynamic, depending on whether it changes. Because dynamic tensors increase compiler compilation complexity, related technologies such as the TVM compiler or the XLA compiler only compile tensors with static dimensions when compiling neural network models, failing to compile tensors with dynamic dimensions.

[0039] The technical solution of this specification addresses the aforementioned technical issues in related technologies and proposes a computational solution for a neural network model. This solution significantly improves the computational efficiency of the neural network model by enabling the calculation of the dynamic dimensions of tensors. The technical solution of this specification will be described in detail below with reference to the accompanying drawings.

[0040] FIG2 is a flow chart of a method for calculating a neural network model provided by an exemplary embodiment. As shown in FIG2 , the method may include the following steps:

[0041] Step 202: Obtain a computational graph corresponding to the neural network model, where the nodes in the computational graph represent operators in the neural network model, and the edges in the computational graph represent data dependencies between operators.

[0042] With the development of deep learning, the topological structure of neural network models has become increasingly complex. In order to facilitate the understanding, expression and execution of neural network models, computational graphs have emerged. Generally, a neural network model can be converted into a computational graph represented by an intermediate representation (IR) defined inside the compiler. The intermediate representation can be understood as a language. Different compilers have different languages, and the computational graphs obtained by their conversion are also different. Those skilled in the art can select a compiler according to actual needs to obtain the corresponding computational graph based on the intermediate representation defined inside the compiler, and this specification does not limit this.

[0043] The nodes in the computation graph represent operators in the neural network model, and the edges in the computation graph represent data dependencies between operators. The dependencies between operators affect the execution order and parallelism of the operators. In other words, a computation graph is a computation function expressed by a directed graph with operators as nodes. This computation function sequentially calls and executes the operator nodes in the directed graph on the input data (e.g., input tensors) and obtains the final output data (e.g., output tensors). Figure 3 is a schematic diagram of a simple computation graph shown in an exemplary embodiment. As shown in Figure 3, the computation graph contains two nodes, corresponding to the Matmul (normal matrix multiplication) operator and the ReLU (activation) operator, respectively. The input tensors are X and Y, and the output tensor is Z. Based on the data dependencies represented by the edges in the computation graph, it can be seen that the computation function reflected by the computation graph is: first, a matrix multiplication operation is performed on the input tensors X and Y, and then the result of the matrix multiplication operation is mapped to the output tensor Z through the activation function, that is, Z = ReLU (X × Y).

[0044] In one embodiment, if a neural network model includes a complex operator composed of multiple basic operators, that is, if a computation graph includes a node corresponding to the load operator, the complex operator can be decomposed into corresponding basic operators. Subsequent computation operations are then performed based on the computational logic of the basic operators and / or the data dependencies between the basic operators.

[0045] FIG4 is a schematic diagram of a disassembly of a complex operator shown in an exemplary embodiment. In FIG4 , the calculation graph on the left is the calculation graph corresponding to the neural network model obtained. The calculation graph contains 6 nodes. According to the forward running order of the calculation graph, these 6 nodes correspond to the Mul (multiplication) operator, Matmul operator, Add (addition) operator, Cast (type conversion) operator, Matmul operator, and Mean (average value) operator. Among them, the Mean operator is a complex operator composed of the Sum (sum) operator and the Div (division) operator. Therefore, the Mean operator can be disassembled into the Sum operator and the Div operator to obtain the calculation graph after the complex operator is disassembled on the right.

[0046] In this embodiment, complex operators are decomposed into corresponding basic operators, and subsequent calculation operations are performed based on the calculation logic of the basic operators and / or the data dependencies between the basic operators. Because the calculation logic of basic operators is simpler than that of complex operators, and the data dependencies between basic operators are more intuitive and clear than those of complex operators, this helps to clearly and easily perform subsequent calculation operations, thereby improving the execution efficiency of subsequent calculation operations.

[0047] Step 204: When the dimension information of any tensor involved in the operation in the neural network model is a dynamic dimension, the dimension information of any tensor is represented as a dimension calculation expression related to the calculation logic of the operator and / or the data dependency.

[0048] In the computational graph, a tensor can be understood as a multidimensional array. The dimensional information of a tensor may include the number of dimensions of the tensor, the size of each dimension, etc. For example, for a tensor (N-dimensional array), you can use X n =(x1,x2,…x i ,…,x n ) to represent its dimension information, where x i (i∈1,2,…,n) represents the size of a dimension. Dimension information can be divided into static and dynamic dimensions based on whether it changes. Static dimensions refer to dimensions that do not change during the computational network model's execution. Dynamic dimensions refer to tensor dimensions that change as the neural network model runs. The starting input tensors of a computation graph can include tensors with static or dynamic dimensions.

[0049] When the dimensional information of any tensor participating in a computation in a computation graph is a dynamic dimension, the dynamic dimension is typically affected by the computational logic of the operator and / or the data dependencies between operators, and thus changes accordingly. Therefore, when the dimensional information of any tensor participating in a computation is a dynamic dimension, the dimensional information of the tensor can be represented as a dimensional computation expression related to the computational logic of the operator and / or the data dependencies between operators, so that the dimensional computation expression can include the data dependencies between the tensor and other tensors, and / or include the operator computation logic corresponding to the nodes related to the tensor.

[0050] Through the technical solution of this specification, when the dimensional information of any tensor involved in the operation is a dynamic dimension, the dimensional information of any tensor is represented as a dimensional calculation expression related to the calculation logic of the operator and / or the data dependency between operators, so that each dimensional calculation expression can accurately represent the dimensional information of the corresponding tensor. Moreover, since any tensor has a separate dimensional calculation expression, the dimensional information of any tensor can be derived separately according to the dimensional calculation expression during the subsequent compilation process. There is no need to derive the dimensional information of each tensor in sequence according to the order of operations from front to back, thereby realizing decoupling in the tensor dimensional information derivation process, thereby effectively improving the efficiency of obtaining tensor dimensional information, and then helping to improve the efficiency of obtaining each calculation data in the calculation graph, and realizing the improvement of the overall computational efficiency of the neural network model.

[0051] In one embodiment, dynamic parameters related to the dimension information of the tensor can be determined according to the calculation logic of the operator and / or the data dependency between operators, and then a dimension calculation expression is generated based on the dynamic parameters.

[0052] Figure 5 is a schematic diagram of a representation dimension calculation expression shown in an exemplary embodiment. As shown in Figure 5, assume that the dimension calculation expression of input tensor A is (a, b, c), and the dimension calculation expression of another input tensor B is (d). Tensor A first undergoes a transpose operator operation to output tensor C. The transpose operator, as the name suggests, changes the position of parameters in the dimension information. In this embodiment, after the transpose operation, the positions of parameters a and b are swapped, and the position of parameter c remains unchanged. Therefore, the dimension calculation expression of tensor C can be (b, a, c). Among them, parameters a, b, and c represent the data dependency relationship between tensor C and tensor A, and the positions of these three parameters represent the calculation logic of the transpose operator. Another tensor B first undergoes a reshape operator operation to output tensor D. The reshape operator is used to change the shape of a tensor. The so-called shape of a tensor refers to the number of dimensions of the tensor and the number of elements in each dimension. For example, the shape of a tensor (3, 4) means that the tensor has two dimensions, where the first dimension has 3 elements and the second dimension has 4 elements. The reshape operator can be used to reshape a tensor into the desired shape. In this embodiment, the computational logic of the reshape operator is to reshape a one-dimensional vector into a two-dimensional matrix. Specifically, tensor B is divided into 8 parts, each of which is one-eighth the length of tensor B. Therefore, the dimension calculation expression of tensor D can be (8, d / 8). Among them, the parameter d represents the data dependency relationship between tensor D and tensor B, and the shape of the dimension calculation expression represents the computational logic of the reshape operator.

[0053] Then, a matrix multiplication operation is performed on tensors C and D to output tensor E. The dimension calculation expression of tensor E is (b, a, d / 8). Among them, the parameters a, b, and d represent the data dependency relationship between tensor E and tensor A and tensor B, and the position and size of the three parameters in the dimension calculation expression represent the calculation logic of all related operators in the entire operation process from tensor A, tensor B to tensor E. Therefore, in the subsequent compilation process, it is only necessary to obtain the specific values ​​of the four parameters a, b, c, and d, and then substitute the specific values ​​into the dimension calculation expressions of tensors A, B, C, D, and E respectively to obtain the dimension information of tensors A, B, C, D, and E respectively.

[0054] In this embodiment, for tensors of dynamic dimensions, their dimension information is represented as a dimension calculation expression based on dynamic parameters, so that during the compilation process, the dimension information of the tensor can be obtained by simply substituting the specific values ​​of the dynamic parameters into the dimension calculation expression of the tensor itself. And because the dynamic parameters are determined based on the calculation logic of the operator and / or the data dependencies between operators, the same dynamic parameters can be applied to the dimension calculation expressions of multiple tensors, that is, the same dynamic parameters can be included in multiple dimension calculation expressions, effectively reducing the number of dynamic parameters, so that the dimension information of multiple tensors can be obtained by obtaining a small number of specific values, without having to derive the dimension information separately for each dimension calculation formula, thereby improving the efficiency of obtaining dimension information.

[0055] In one embodiment, if a computation graph contains a node corresponding to a type conversion operator, the type conversion operator and the adjacent compute-intensive operator can be fused. First, the compute-intensive operators and type conversion operators are explained: according to the operator's resource requirements, they can be divided into two categories: compute-intensive operators and memory-intensive operators (also known as non-compute-intensive operators). Among them, the vast majority of the time of compute-intensive operators is spent on computation, such as GEMM (general matrix multiplication) and Convolution. Memory-intensive operators can refer to operators whose access and storage operations are more time-consuming than computation, such as Elementwise (element-wise operation), BN (batch normalization), etc. Generally, all operators other than compute-intensive operators such as GEMM and Convolution in the computation graph can be regarded as memory-intensive operators. The computation logic of the type conversion operator is to convert any type of value into a value of a specified type. For example, if the input data is of type int and the required target data is of type character, the input data can be processed through the operation of the type conversion operator to output the target data.

[0056] Since neural network models are usually applied on GPUs (Graphic Processing Units), and the data involved in the calculations are usually obtained from CPUs (Central Processing Units), in the process of running the neural network model, the input data of each operator needs to be transferred from the CPU to the GPU, and the output data needs to be moved back to the CPU after the calculation is completed in the GPU. Then, when performing the calculation of the next operator, the output data of the previous operator needs to be transferred from the CPU to the GPU again, and so on. The data transfer overhead of the entire calculation process is very large, and the overall calculation efficiency of the GPU is low. To address this problem, the technical means of operator fusion is proposed. Operator fusion is to fuse multiple operators into one computing core, that is, to merge multiple nodes corresponding to multiple operators into one node. By reducing the number of nodes in the calculation graph, the cost of moving intermediate data is reduced, which saves calculation time and can effectively improve the calculation and reasoning performance of the neural network model.

[0057] The compiler can assist in completing the compilation process based on the operator library in the relevant technology to improve the efficiency of code generation. For example, Cutlass (CUDA Templates for Linear Algebra Subroutines) is a linear algebra operator library based on CUDA code, which defines a series of highly optimized operator components. For example, when it is necessary to generate corresponding code for the GEMM operator contained in the neural network model in this specification, the corresponding code can be quickly generated based on the operator library. Taking the computationally intensive operator GEMM as an example, since the GEMM operator defined in Cutlass can perform type conversion on data in addition to matrix multiplication operations, when the computation graph contains a node corresponding to a type conversion operator, if it is determined that the operator component corresponding to the GEMM operator provided by Cutlass is used during the compilation process, the type conversion operator can be fused with the adjacent GEMM operator, thereby eliminating the need to use a separate type conversion operator to implement type conversion.

[0058] Figure 6 is a schematic diagram of a fused type conversion operator shown in an exemplary embodiment. As shown in Figure 6, there are four input tensors in the left calculation graph, namely tensors a, b, c, and d. Assume that the dimensional information of tensor a is (x, y), the dimensional information of tensor b is (z, w), the dimensional information of tensor c is (p), and the dimensional information of tensor d is (q, m). According to the method of obtaining dimensional information in the aforementioned embodiment, according to the calculation logic of each operator and the data dependency between each operator, the dimensional information of the output tensor e can be obtained as (x, 1). Because there is a cast operator in the left calculation graph, the cast operator in the left calculation graph can be fused with the adjacent MatMul operator, thereby merging the two nodes corresponding to the two operators into one node to obtain the right calculation graph. In the right calculation graph, the merged GEMM operator can be used to perform matrix multiplication and type conversion operations on the input data at the same time, reducing the overhead of data handling and helping to improve the overall calculation and reasoning performance of the neural network model.

[0059] In one embodiment, if the computation graph contains a node corresponding to a transposition operator, it is necessary to determine the dependency relationship between the node's subsequent node and the node's preceding node. As previously mentioned, the transposition operator changes the dimensional information of the tensor, but does not change the data carried by the tensor. Therefore, it can be considered that the node corresponding to the transposition operator performs a transport operation on the data carried by the tensor. In order to reduce the data transport overhead, the node corresponding to the transposition operator can be eliminated so that the data does not need to be transported. Since the transposition operator transports the output tensor of its preceding node to its subsequent node, so that the output tensor of the preceding node is used as the input tensor of the subsequent node, it is necessary to determine the dependency relationship between the subsequent node and the preceding node, so that after eliminating the node corresponding to the transposition operator, the subsequent node can know its corresponding preceding node based on the determined dependency relationship and obtain the output tensor of the corresponding preceding node. Specifically, the dependency relationship refers to the dependency relationship between the layout information of each tensor. Starting from the post-order node corresponding to a transposition operator, the layout information of each tensor can be deduced according to the reverse order of the computational graph execution order to determine the dependency relationship between the post-order node and its corresponding pre-order node.

[0060] In conjunction with the embodiment shown in Figure 5, the node corresponding to the Gemm operator is the subsequent node of the node corresponding to the Transpose operator, and can be deduced in reverse order starting from the node corresponding to the Gemm operator. Assuming that the layout information of tensor E is (M, K), since the Gemm operator itself has two attributes: transpose_a and transpose_b, these two attributes are used to perform a transpose operation on the layout information of the input tensor of the Gemm operator. Assuming that in this embodiment, transpose_a=false, transpose_b=true, it means that the Gemm operator does not perform a transpose operation on the layout information of tensor C, but only performs a transpose operation on the layout information of tensor D. According to the calculation logic of the Gemm operator, it can be deduced that the layout information of tensor C is (M, unknown parameters), and the layout information of tensor D is (K, unknown parameters). Since tensor C is output by tensor A through the Transpose operator, according to the calculation logic of the Transpose operator, it can be deduced that the layout information of tensor A is (unknown parameters, M). Tensor A is the output of the predecessor node of the node corresponding to the Transpose operator. Therefore, the dependency relationship between the node corresponding to the Gemm operator and the predecessor node of the node corresponding to the Transpose operator can be obtained based on the layout information of tensor A (unknown parameters, M) and the layout information of tensor C (M, unknown parameters). After determining the dependency relationship, the node corresponding to the Transpose operator can be eliminated. After eliminating the node corresponding to the Transpose operator, the node corresponding to the Gemm operator can determine its corresponding predecessor node based on the previously derived dependency relationship and receive the output tensor A of the predecessor node, thereby ensuring the correct execution order of each operator in the calculation graph and avoiding errors in the calculation graph operation.

[0061] In this embodiment, by determining the dependencies between the preceding and succeeding nodes of the node corresponding to the transposition operator, the execution order of the computation graph remains unchanged after eliminating the transposition operator. Eliminating the transposition operator can further reduce the number of nodes in the computation graph, lowering data handling overhead and helping to improve the overall computational performance of the neural network model.

[0062] In one embodiment, if the computation graph contains a node corresponding to a reshape operator, since the reshape operator changes the shape of the tensor and does not change the data carried by the tensor, that is, the reshape operator does not perform substantial operations on the data, it is a non-computational operator. Therefore, the node corresponding to the reshape operator can be eliminated to reduce the number of nodes in the computation graph. In conjunction with the embodiment shown in Figure 5, the Reshape operator can be directly eliminated, and the tensor B can be used as one of the inputs of the Gemm operator. In this embodiment, by eliminating the reshape operator, the reshape operator can be prevented from interfering with the code generation process during the subsequent compilation process, which helps to improve the overall computing performance of the neural network model.

[0063] In one embodiment, when performing operator fusion on compute-intensive operators, each compute-intensive operator and its subsequent operators are considered as an operator group, with each compute-intensive operator as the center. Then, based on the fusion rule, one or more operator groups are fused into a kernel function. Therefore, the nodes corresponding to non-compute-intensive operators in the computation graph can be moved after the nodes corresponding to their adjacent compute-intensive operators, thereby facilitating the fusion of the non-compute-intensive and compute-intensive operators.

[0064] Figure 7 is a schematic diagram illustrating an exemplary embodiment of moving non-computationally intensive operators. As shown in Figure 7, a Mul operator exists before the first computationally intensive operator (MatMul) in the computational graph on the left. This Mul operator can be moved to the MatMul operator to fuse the first MatMul operator with the Mul operator and the Add operator, and the second MatMul operator with the Sum operator and the Div operator, as shown in the computational graph on the right. In this way, the three partitions in the left calculation graph (Mul operator is the first partition, MatMul operator and Add operator are the second partition, MatMul operator and Sum operator, Div operator are the third partition) can be reintegrated into two partitions in the right calculation graph (MatMul operator and Mul operator, Add operator are one partition, MatMul operator and Sum operator, Div operator are another partition), so that subsequent fusion can fuse all operators in the left calculation graph, avoid the situation where the Mul operator cannot be fused, achieve further optimized compilation of the neural network model, and improve the overall computing efficiency of the neural network model.

[0065] In one embodiment, the neural network model can be compiled into a compiler to generate corresponding code. Specifically, a pre-generated operator fusion code template is first obtained. The operator fusion code template corresponds to a fused operator obtained by fusing one or more operator groups, where each operator group includes a computationally intensive operator and at least one non-computationally intensive operator.

[0066] The operator fusion code template records the generated codes corresponding to the computationally intensive operators contained in the fusion operator and the code gaps corresponding to the non-computationally intensive operators. Of course, the code gaps corresponding to the computationally intensive operators can also be left out in the code template, and then the codes corresponding to the computationally intensive operators can be temporarily generated after the neural network model is acquired, and then the temporarily generated codes can be filled into the corresponding gaps in the code template. For non-computationally intensive operators, the mapping relationship between the non-computationally intensive operators and their corresponding codes can be preset, and then after the neural network model is acquired, the codes corresponding to the non-computationally intensive operators contained in the model can be queried according to the preset mapping relationship, and then the queried codes can be filled into the corresponding code gaps in the operator fusion code template.

[0067] The code composition of the operator fusion code template can be set according to the corresponding operator fusion rule. For example, in the operator fusion rule of GEMM / Conv+Elementwise(*)+GEMM+Elementwise(*)+GEMM, the calculation logic of the Elementwise type operator is to perform element-by-element operations on the two input tensors. This type of operator can include operators such as sin, cos, sigmoid (activation). (*) is used to indicate that the number of Elementwise operators can be one or more. For this operator fusion rule, an operator fusion code template containing codes corresponding to three computationally intensive operators and two code vacancies corresponding to two non-computationally intensive operators can be pre-generated. Moreover, according to the execution order of the non-computationally intensive operators and the computationally intensive operators, the middle part of the code corresponding to every two computationally intensive operators in the code template includes a code vacancy.

[0068] In addition, operator fusion rules can also include ①GEMM / Conv+Elementwise(*), ②GEMM / Conv+Elementwise(*)+Reshpae+Elementwise(*), ③GEMM / Conv+Elementwise(*)+Reduce+Elementwise(*), and so on. (*) indicates that there can be one or more Elementwise operators. The computational logic of the Reduce operator in ③ is to reduce the dimension of the tensor. When generating code templates based on different fusion rules, the generated code corresponding to the compute-intensive operators recorded in the code templates does not need to be changed. Instead, the number of generated code corresponding to the compute-intensive operators needs to be increased or decreased based on the fusion rule. For example, ①, ②, and ③ each require only one generated code corresponding to a compute-intensive operator. Similarly, the number of code gaps corresponding to non-compute-intensive operators is also related to the number of compute-intensive operators. Generally, each compute-intensive operator requires a corresponding code gap. For example, ①, ②, and ③ each require one code gap to fill in the code corresponding to the non-compute-intensive operator. The pre-generated operator fusion template in this specification can be applied to the generation of the code corresponding to the fusion operator after any non-computation-intensive operator is fused with a computation-intensive operator, which effectively improves the versatility of the code template.

[0069] Figure 8 is a schematic diagram of a generated code shown in an exemplary embodiment. As shown in Figure 8, the box on the right is a pre-generated operator fusion code template provided by this embodiment. The gemm series function in the code template represents the code corresponding to the matrix multiplication operator. The code has been pre-generated, and the specific code is omitted in the figure. The epilogue series function represents the code corresponding to the non-computational intensive operator. This part of the code needs to be filled with the corresponding code according to the non-computational intensive operators contained in the actual neural network model. And it is necessary to combine the relationship between each non-computational intensive operator and its corresponding computationally intensive operator to fill the non-computational intensive operator into the correct code vacancy position. The operator fusion code template can be pre-generated based on CUTLASS. For example, some functions in CUTLASS (such as the overhead function launch, the matrix multiplication function gemm, etc.) can be called to generate the relevant code contained in the operator fusion code template.

[0070] Taking the calculation graph on the right side of Figure 7 as an example, according to the fusion rules of the Gemm operator, each Gemm operator is taken as the center, and each Gemm operator and its subsequent operators are regarded as an operator group. Therefore, the calculation graph on the right side of Figure 7 can be divided into two operator groups, wherein the first MatMul operator, Mul operator, and Add operator are one operator group, and the second MatMul operator, Sum operator, and Div operator are another operator group. Then, based on the operator fusion code template shown in Figure 8, according to the filling rules of the code template, the code corresponding to the Mul operator and the Add operator can be filled in the epilogue1 function. Similarly, the code corresponding to the Sum operator and the Div operator is filled in the epilogue2 function. The compiler then compiles the filled operator fusion code template to generate the corresponding code for the neural network model.

[0071] It should be pointed out that the generated code corresponding to the computationally intensive operators contained in the operator fusion code template specifically includes the code content corresponding to the computational logic and configuration parameters of the computationally intensive operators. The computational logic used by different computationally intensive operators is often the same, but the configuration parameters may be different. Therefore, in addition to filling the code of the non-computationally intensive operators into the corresponding code vacancies in the operator fusion code template, it may also be necessary to write the correct configuration parameters into the generated code corresponding to the computationally intensive operators. For example, as shown in Figure 8, the configuration parameters corresponding to the first MatMul operator need to be filled into the gemm1 function in the code template, and the configuration parameters corresponding to the second MatMul operator need to be filled into the gemm2 function in the code template. Of course, the code for the non-computationally intensive operators also needs to be filled in, which will not be repeated here. Then, the operator fusion code template filled with the above-mentioned configuration parameters and the code corresponding to the non-computationally intensive operators can be compiled by the compiler to generate the corresponding code for the neural network model.

[0072] In this embodiment, by pre-generating an operator fusion code template, on the one hand, after the compiler obtains the neural network model, the code corresponding to the operators contained in the neural network model can be directly filled into the corresponding vacancies in the code template, greatly improving the efficiency of code compilation and generation. On the other hand, because the operator fusion code template does not restrict the code corresponding to the operator type, that is, any operator can fill its corresponding code into the corresponding position in the code template, the operator fusion code template is applicable to a variety of neural network models, improving the versatility of the operator fusion template, and eliminating the need for developers to develop a dedicated template for each neural network model, effectively reducing development costs.

[0073] In one embodiment, multiple operator groups in a neural network model can be cascaded. Cascading multiple operator groups is complex. In related art, when code corresponding to a neural network model is directly generated by a compiler, code for a single operator group is typically only generated; it is difficult to generate code for multiple cascaded operator groups. The solution of this specification pre-generates code templates. Based on the existing code for operator components provided by operator libraries in related art, the code templates are optimized in conjunction with expert experience to compile and generate code for multiple cascaded operator groups. The embodiment shown in FIG8 illustrates the cascading of two operator groups. By filling in the corresponding sections of the code template with non-computationally intensive operators, the complete code for the two cascaded operator groups can be obtained. Of course, the above embodiment is for illustrative purposes only, and the technical solution of this specification does not limit the number of operator groups that can be cascaded. In practice, based on expert experience, operator fusion code templates formed by cascading two or more operator groups can be pre-generated. In subsequent use, by filling in these operator fusion code templates, the corresponding code for the cascaded two or more operator groups can be obtained.

[0074] In this embodiment, a code template is pre-generated using the existing code of operator components provided by an operator library in related technologies and expert experience. During the actual compilation process, the complete code for the cascaded operator group is obtained by simply filling in the corresponding code in the code template. This allows the fusion of multiple cascaded operator groups into a single kernel function, further reducing the overhead of moving data between operator groups and improving the overall computational efficiency of the neural network model. Because the code template is optimized based on expert experience, the code generated based on the code template has better performance, that is, the generated code executes faster and has less overhead.

[0075] FIG9 is a schematic diagram of a device provided by an exemplary embodiment. Referring to FIG9 , at the hardware level, the device includes a processor 902, an internal bus 904, a network interface 906, a memory 908, and a non-volatile memory 910. Of course, it may also include hardware required for other functions. One or more embodiments of this specification may be implemented in software, such as by the processor 902 reading the corresponding computer program from the non-volatile memory 910 into the memory 908 and then executing it. Of course, in addition to software implementation, one or more embodiments of this specification do not exclude other implementation methods, such as logic devices or a combination of software and hardware. In other words, the execution subject of the following processing flow is not limited to each logic unit, but may also be hardware or logic devices. The device represents the dynamic dimension of a tensor as a dimensional calculation expression related to the computational logic of the operator and / or the data dependencies between operators. This allows the dimensional information of each tensor to be calculated separately according to the dimensional calculation expression, without having to derive the dimensional information of each tensor from the front to the back. This effectively improves the efficiency of obtaining the dimensional information of each tensor, thereby improving the computational efficiency of the neural network model.

[0076] Referring to FIG10 , a computing device for a neural network model may be applied to the device shown in FIG9 to implement the technical solution of this specification. The device may include:

[0077] An acquisition unit 1002 is configured to acquire a computation graph corresponding to the neural network model, where nodes in the computation graph represent operators in the neural network model, and edges in the computation graph represent data dependencies between operators;

[0078] The computing unit 1004 is configured to represent the dimensional information of any tensor participating in the operation in the neural network model as a dimensional calculation expression related to the calculation logic of the operator and / or the data dependency when the dimensional information of the any tensor is a dynamic dimension.

[0079] Optionally, the computing unit 1004 is specifically used to determine dynamic parameters related to the dimensional information of any tensor based on the computing logic of the operator and / or the data dependency; and represent the dimensional information of any tensor as a dimensional calculation expression based on the dynamic parameters.

[0080] Optionally, the device further includes:

[0081] Determination unit 1006 is configured to determine, for a node corresponding to a transposition operator in the computational graph, a dependency relationship between its subsequent node and its preceding node, wherein the dependency relationship is used to indicate that the subsequent node determines and obtains the output tensor of its corresponding preceding node; and eliminate the node corresponding to the transposition operator.

[0082] Optionally, the device further includes:

[0083] The eliminating unit 1008 is configured to eliminate the node corresponding to the reshaping operator when the computation graph includes the node corresponding to the reshaping operator.

[0084] Optionally, the device further includes:

[0085] The fusion unit 1010 is configured to fuse the type conversion operator with the adjacent computationally intensive operator when the computation graph includes a node corresponding to the type conversion operator.

[0086] Optionally, the device further includes:

[0087] The code generation unit 1012 is configured to obtain a pre-generated operator fusion code template, where the operator fusion code template corresponds to a fusion operator obtained by fusing one or more operator groups, each operator group includes a computationally intensive operator and at least one non-computationally intensive operator, and the operator fusion code template records the generated code corresponding to the computationally intensive operator contained in the fusion operator and the code gap corresponding to the non-computationally intensive operator; according to the computationally intensive operators and non-computationally intensive operators contained in the neural network model, the code corresponding to the non-computationally intensive operator contained in the neural network model is filled into the corresponding code gap in the operator fusion code template; the filled operator fusion code template is compiled to generate the corresponding code for the neural network model.

[0088] Optionally, the multiple operator groups are in a cascade relationship.

[0089] Optionally, the device further includes:

[0090] The disassembly unit 1014 is configured to disassemble the complex operator into corresponding basic operators when the computation graph contains a node corresponding to a complex operator composed of multiple basic operators; wherein the computation logic is the computation logic between the basic operators, and / or the data dependency is the data dependency between the basic operators.

[0091] Optionally, the device further includes:

[0092] The moving unit 1016 is configured to move the node corresponding to the non-computation-intensive operator to behind the node corresponding to the adjacent computation-intensive operator, so as to merge the non-computation-intensive operator with the computation-intensive operator.

[0093] In the above embodiment, by representing the dynamic dimension of the tensor as a dimension calculation expression related to the calculation logic of the operator and / or the data dependency between operators, it is only necessary to calculate the dimension information of each tensor separately according to the dimension calculation expression of each tensor, without having to derive the dimension information of each tensor from front to back, thereby effectively improving the efficiency of obtaining the dimension information of each tensor, and thereby improving the computational efficiency of the neural network model.

[0094] The systems, devices, modules, or units described in the above embodiments may be implemented by computer chips or entities, or by products having certain functions. A typical implementation device is a computer, which may be in the form of a personal computer, laptop computer, cellular phone, camera phone, smartphone, personal digital assistant, media player, navigation device, email transceiver, game console, tablet computer, wearable device, or any combination of these devices.

[0095] In a typical configuration, a computer includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0096] Memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. Memory is an example of a computer-readable medium.

[0097] Computer-readable media include permanent and non-permanent, removable and non-removable media that can be used to store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, disk storage, quantum memory, graphene-based storage media or other magnetic storage devices, or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory media such as modulated data signals and carrier waves.

[0098] For the computer-readable medium (or computer-readable storage medium) as described above or in any other form, computer instructions may be stored thereon, and when the instructions are executed by the processor, one or more of the above embodiments are implemented, thereby realizing the technical solution of this specification. The computer-readable medium represents the dynamic dimension of the tensor as a dimension calculation expression related to the calculation logic of the operator and / or the data dependency between operators, so that it is only necessary to calculate the dimension information of each tensor separately according to the dimension calculation expression of each tensor, without having to derive the dimension information of each tensor from the front to the back, thereby effectively improving the efficiency of obtaining the dimension information of each tensor, thereby improving the computational efficiency of the neural network model.

[0099] This specification also proposes a computer program, which, when executed by a processor, implements one or more of the above-mentioned embodiments, thereby realizing the technical solution of this specification. The computer program can be specifically recorded in a computer-readable medium as described above or in any other form, and this specification does not limit this. The computer program represents the dynamic dimension of the tensor as a dimensional calculation expression related to the calculation logic of the operator and / or the data dependency between operators, so that it is only necessary to calculate the dimensional information of each tensor separately according to the dimensional calculation expression of each tensor, without having to derive the dimensional information of each tensor from front to back, thereby effectively improving the efficiency of obtaining the dimensional information of each tensor, thereby improving the computational efficiency of the neural network model.

[0100] It should also be noted that the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus that includes a series of elements includes not only those elements but also other elements not explicitly listed, or includes elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity, or apparatus that includes the element.

[0101] The foregoing description of this specification describes specific embodiments. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that described in the embodiments and still achieve the desired results. Furthermore, the processes depicted in the accompanying drawings do not necessarily require the specific order shown or the sequential order to achieve the desired results. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0102] The terms used in one or more embodiments of this specification are for the purpose of describing specific embodiments only and are not intended to limit one or more embodiments of this specification. The singular forms "a," "an," "the," and "the" used in one or more embodiments of this specification and the appended claims are also intended to include plural forms unless the context clearly indicates otherwise. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.

[0103] It should be understood that although the terms first, second, third, etc. may be used to describe various information in one or more embodiments of this specification, such information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of this specification, first information may also be referred to as second information, and similarly, second information may also be referred to as first information. Depending on the context, the word "if" as used herein may be interpreted as "when..." or "when..." or "in response to determining."

[0104] The above description is merely a preferred embodiment of one or more embodiments of this specification and is not intended to limit one or more embodiments of this specification. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of one or more embodiments of this specification shall be included in the scope of protection of one or more embodiments of this specification.

Claims

1. A method for calculating a neural network model, characterized in that: The method comprises: Obtain a computational graph corresponding to the neural network model, where nodes in the computational graph represent operators in the neural network model, and edges in the computational graph represent data dependencies between operators; When the dimension information of any tensor involved in the operation in the neural network model is a dynamic dimension, the dimension information of any tensor is represented as a dimension calculation expression related to the calculation logic of the operator and / or the data dependency.

2. The method according to claim 1, characterized in that The characterizing the dimension information of any tensor as a dimension calculation expression related to the calculation logic of the operator and / or the data dependency includes: Determine a dynamic parameter related to the dimension information of any tensor according to the calculation logic of the operator and / or the data dependency; The dimension information of any tensor is represented as a dimension calculation expression based on the dynamic parameter.

3. The method according to claim 1, characterized in that Also includes: For a node corresponding to a transposition operator in the computation graph, determining a dependency relationship between a subsequent node and a preceding node, wherein the dependency relationship is used to instruct the subsequent node to determine and obtain an output tensor of a corresponding preceding node; The node corresponding to the transposition operator is eliminated.

4. The method according to claim 1, characterized in that Also includes: When the computation graph includes a node corresponding to a reshaping operator, the node corresponding to the reshaping operator is eliminated.

5. The method according to claim 1, characterized in that Also includes: When the computation graph includes a node corresponding to a type conversion operator, the type conversion operator is fused with a computationally intensive operator adjacent to the type conversion operator.

6. The method according to claim 1, characterized in that Also includes: Obtaining a pre-generated operator fusion code template, the operator fusion code template corresponding to a fusion operator obtained by fusing one or more operator groups, each operator group including a computationally intensive operator and at least one non-computationally intensive operator, and the operator fusion code template records the generated code corresponding to the computationally intensive operator contained in the fusion operator and the code vacancy corresponding to the non-computationally intensive operator; According to the computationally intensive operators and non-computationally intensive operators contained in the neural network model, the codes corresponding to the non-computationally intensive operators contained in the neural network model are filled into the corresponding code gaps in the operator fusion code template; The filled operator fusion code template is compiled to generate the code corresponding to the neural network model.

7. The method according to claim 6, characterized in that The multiple operator groups are in a cascade relationship.

8. The method according to claim 1, characterized in that Also includes: When the computation graph includes a node corresponding to a complex operator composed of a plurality of basic operators, the complex operator is decomposed into corresponding basic operators; The calculation logic is the calculation logic between the basic operators, and / or the data dependency is the calculation logic between the basic operators. Data dependency between operators.

9. The method according to claim 1, characterized in that: Also includes: The node corresponding to the non-computation-intensive operator is moved to behind the node corresponding to the adjacent computation-intensive operator, so that the non-computation-intensive operator is merged with the computation-intensive operator.

10. An electronic device, characterized in that: include: processor; a memory for storing processor-executable instructions; The processor implements the steps of the method according to any one of claims 1 to 9 by running the executable instructions.

11. A computer-readable storage medium having computer instructions stored thereon, characterized in that: When the instruction is executed by a processor, the steps of the method according to any one of claims 1 to 9 are implemented.

12. A computer program, characterized in that When the program is executed by a processor, the steps of the method according to any one of claims 1 to 9 are implemented.

Citation Information

Patent Citations

  • Processing unit, calculation device and calculation graph optimization method of deep learning model

    CN113705798A

  • Neural network computational graph processing method and processing device

    CN114881214A

  • Dynamic neural network compiling method and device, electronic equipment and storage medium

    CN115658331A

  • Calculation method and device of neural network model

    CN116796793A

  • Method, apparatus, and device for compiling neural network, storage medium, and program product

    WO2020093304A1

Cited By

  • Model optimization method and device, electronic equipment and storage medium

    CN121257629A