Tensor program optimization method and device
By dividing and splicing tensor programs, and using error correction processing and greedy algorithms to optimize tensor programs, the limitations of memory layout and calculation graph optimization in the existing technology are solved, and efficient tensor program execution and verification are achieved.
Patent Information
- Application Number
- CN202110788296.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-13
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2041-07-13
AI Technical Summary
Existing tensor program optimization methods have limitations in memory layout and calculation graph optimization, and cannot effectively explore unknown transformation rules. Moreover, the automatic search algorithm only supports completely equivalent transformations, and the verification time is long and limited to supported layouts.
Generate linear tensor subprograms by dividing them, use a set of preset operators to generate mutation programs, perform error correction to make the mutation programs equivalent, and use a greedy algorithm to select the optimal subprograms and splice them to generate optimized tensor programs to reduce searches. space and guarantee end-to-end correctness.
It expands the optimization space of tensor programs, improves execution efficiency, reduces verification time, enables exploration of unknown transformation rules, and improves the optimization effect of calculation graphs.
Smart Images

Figure CN113703768B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to data processing technology, and in particular to a tensor program optimization method and device. Background Art
[0002] Existing deep learning frameworks represent deep learning programs as tensor programs composed of tensors and operators, and the tensor programs are often expressed in the form of computational graphs. In the prior art, optimization schemes for tensor programs can be divided into three types: graph-level optimization that performs equivalent replacement of operators on the computational graph, data layout optimization that replaces the tensor memory layout, and operator-level optimization that generates more efficient operators.
[0003] Existing operator libraries, such as cuDNN, cuBLAS, Intel MKL, etc., can support different data layouts. By applying the most suitable memory layout to different operators, their computing efficiency can be improved. The current technology has the following limitations for memory layout optimization: 1. The data layout considered by the underlying operator library is just a simple adjustment of the order of different dimensions of the tensor, such as row priority and column priority for two-dimensional tensors, NCHW, NHWC for four-dimensional tensors, etc. 2. Tensor program optimization based on memory layout optimization only considers the relatively limited layout supported by the underlying operator library. For the optimization of computational graphs, many existing works use rule-based optimization methods, that is, for a computational subgraph composed of several operators, it is replaced with another subgraph according to pre-designed specific rules. However, rule-based optimization methods require developers to have sufficient understanding of the transformation of computational subgraphs and design relevant transformation rules accordingly. At the same time, since all rules need to be manually specified by developers, their search space is often greatly limited and cannot explore unknown transformation rules.
[0004] There is also an automatic search algorithm in the prior art that can optimize a computational graph automatically and ensure its mathematical correctness through formal verification. However, the current automatic search algorithm only supports completely equivalent transformations, and its formal verification method needs to be deduced based on some basic rules, which takes a long time to verify. Summary of the invention
[0005] In view of the defects existing in the existing tensor program optimization, the present invention aims to expand the optimization space of tensor programs by using a new optimization method, and provides a tensor program optimization method, including:
[0006] The tensor program to be optimized is divided into linear tensor subprograms;
[0007] Generate a mutation program of the subprogram according to a preset operator set;
[0008] Correcting the inequivalent mutation programs in the mutation programs of the subprograms so that each mutation program is equivalent to the corresponding subprogram;
[0009] The optimal subroutine is selected from the mutated program after error correction and concatenated to generate the optimized tensor program.
[0010] In the embodiment of the present invention, the step of dividing the tensor program to be optimized to generate linear tensor subprograms includes:
[0011] Determining a nonlinear activation function operator in the tensor program to be optimized;
[0012] The tensor program to be optimized is divided according to the nonlinear activation function operator to generate linear tensor subprograms.
[0013] In the embodiment of the present invention, the mutation program of generating the subprogram according to the preset operator set includes:
[0014] Step 1, enumerating the tensors in the subroutine as inputs of each operator in the preset operator set;
[0015] Step 2, adding the output tensor of the operator to the subroutine;
[0016] Step 3, determining whether the size of the subroutine exceeds a preset threshold. If the threshold is not reached, executing steps 1 to 3. If the threshold is reached, terminating.
[0017] In the embodiment of the present invention, the preset operator set includes: computationally intensive operators, element-by-element operators and tensor operation operators.
[0018] In the embodiment of the present invention, the error correction process of the inequivalent mutation programs in the mutation programs of the subprogram so that each mutation program is equivalent to the corresponding subprogram includes:
[0019] Determining the reduction domain of the subprogram and its mutant programs by using reduction domain propagation;
[0020] For the overlapping area of any two reduction domains, identify m+1 positions and perform random tests to determine the inequivalent mutation programs; where m is the dimension of the subprogram output tensor;
[0021] Generate an error correction kernel according to the subroutines corresponding to the non-equivalent mutation programs;
[0022] Correct inequivalent mutation programs using the generated error correction kernel.
[0023] In the embodiment of the present invention, the step of selecting the optimal subprogram from the mutation program after error correction processing and splicing it to generate the optimized tensor program includes:
[0024] A greedy algorithm is used to select K candidate subroutines of each subroutine from the mutation program after error correction processing; K is a preset value;
[0025] Select the candidate subprogram with the smallest conversion cost of the previous and next subprograms after splicing from the K candidate subprograms of each subprogram as the optimal subprogram of each subprogram;
[0026] The determined optimal subprograms are concatenated to generate the optimized tensor program.
[0027] In the embodiment of the present invention, the step of splicing the determined optimal subprograms to generate an optimized tensor program includes:
[0028] splicing the determined optimal subprograms to generate a spliced tensor program;
[0029] Perform reversible operator elimination on the concatenated tensor program;
[0030] Perform operator fusion on the spliced tensor program after reversible operator elimination based on the preprocessed weight tensor to generate an optimized tensor program; wherein the preprocessed weight tensor is determined by deforming the preset weight tensor according to the calculation graph of the spliced tensor program.
[0031] At the same time, the present invention also provides a tensor program optimization device, comprising:
[0032] Program partitioning module, used to partition the tensor program to be optimized into linear tensor subprograms;
[0033] A mutation generation module, used to generate a mutation program of the subprogram according to a preset operator set;
[0034] A mutation error correction module, used for performing error correction processing on inequivalent mutation programs in the mutation programs of the subprogram so that each mutation program is equivalent to the corresponding subprogram;
[0035] The optimization program generation module selects the best subprogram from the mutation program after error correction and splices it to generate the optimized tensor program.
[0036] In the embodiment of the present invention, the program division module includes:
[0037] An operator determination unit, used to determine a nonlinear activation function operator in the tensor program to be optimized;
[0038] A partitioning unit is used to partition the tensor program to be optimized according to the nonlinear activation function operator to generate linear tensor subprograms.
[0039] In the embodiment of the present invention, the step of generating a mutation program of the subprogram according to a preset operator set by the mutation generation module includes:
[0040] Step 1, enumerating the tensors in the subroutine as inputs of each operator in the preset operator set;
[0041] Step 2, adding the output tensor of the operator to the subroutine;
[0042] Step 3, determining whether the size of the subroutine exceeds a preset threshold. If the threshold is not reached, executing steps 1 to 3. If the threshold is reached, terminating.
[0043] In an embodiment of the present invention, the mutation error correction module includes:
[0044] A reduction domain determination unit, used for determining the reduction domain of the subprogram and its mutant program by using reduction domain propagation;
[0045] The identification unit is used to identify m+1 positions in the overlapping area of any two reduction domains for random testing to determine the inequivalent mutation programs; where m is the dimension of the subprogram output tensor;
[0046] A kernel generation unit, used for generating an error correction kernel according to a subroutine corresponding to an inequivalent mutation program;
[0047] The error correction unit is used to correct the inequivalent mutation program using the generated error correction kernel.
[0048] In the embodiment of the present invention, the optimization program generation module includes:
[0049] The candidate program determination unit is used to select K candidate subprograms of each subprogram from the mutant program after error correction processing by using a greedy algorithm; K is a preset value;
[0050] A selection unit, used for selecting, from the K candidate subprograms of each subprogram, a candidate subprogram with the smallest conversion cost of the previous and next subprograms after splicing as the optimal subprogram of each subprogram;
[0051] The optimization program splicing unit is used to splice the determined optimal subprograms to generate optimized tensor programs.
[0052] In the embodiment of the present invention, the optimization program splicing unit includes:
[0053] A splicing unit, used for splicing the determined optimal subprograms to generate a spliced tensor program;
[0054] A reversible operator elimination unit, used for performing reversible operator elimination on the spliced tensor program;
[0055] The optimization unit is used to generate an optimized tensor program by performing operator fusion on the spliced tensor program after reversible operator elimination based on the preprocessed weight tensor; wherein the preprocessed weight tensor is determined by deforming the preset weight tensor according to the calculation graph of the spliced tensor program.
[0056] At the same time, the present invention also provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, and the above method is implemented when the processor executes the computer program.
[0057] At the same time, the present invention also provides a computer-readable storage medium, which stores a computer program for executing the above method.
[0058] The present invention divides the tensor program to be optimized into linear tensor subprograms, reduces the search space that needs to be explored, explores all possible local equivalent deformations based on memory rearrangement for each subprogram, and generates all possible mutations of the subprogram. In order to ensure the end-to-end correctness of the optimized tensor program, the outputs of the tensor subprograms before and after optimization are compared, and the inequivalent positions therein are found and corrected. The mutations that complete the error correction will be further optimized, and the mutations of each subprogram are combined into a complete tensor program in an optimal way, which can make the execution of the tensor program more efficient.
[0059] In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0061] Figure 1 A flowchart of the tensor program optimization method provided by the present invention;
[0062] Figure 2 It is a schematic diagram of an embodiment of the present invention;
[0063] Figure 3 It is a schematic diagram of an embodiment of the present invention;
[0064] Figure 4 It is a schematic diagram of an embodiment of the present invention;
[0065] Figure 5It is a schematic diagram of an embodiment of the present invention;
[0066] Figure 6 It is a schematic diagram of an embodiment of the present invention;
[0067] Figure 7 It is a schematic diagram of an embodiment of the present invention;
[0068] Figure 8 It is a schematic diagram of an embodiment of the present invention;
[0069] Fig. 9 It is a schematic diagram of an embodiment of the present invention;
[0070] Fig.10 It is a schematic diagram of an embodiment of the present invention;
[0071] Fig.11 It is a schematic diagram of an embodiment of the present invention;
[0072] Fig.12 A block diagram of a tensor program optimization device provided by the present invention;
[0073] Fig.13 Schematic diagram of an electronic device in an embodiment of the present invention. DETAILED DESCRIPTION
[0074] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0075] In order to expand the optimization space of tensor programs and make the execution of tensor programs more efficient, the present invention provides a tensor program optimization method, such as Figure 1 As shown, the method of the present invention comprises:
[0076] Step S101, dividing the tensor program to be optimized into linear tensor subprograms;
[0077] Step S102, generating a mutation program of the subprogram according to a preset operator set;
[0078] Step S103, performing error correction processing on the inequivalent mutation programs in the mutation programs of the subprogram so that each mutation program is equivalent to the corresponding subprogram;
[0079] Step S104, selecting the best subroutine from the mutant program after error correction processing, and splicing them to generate an optimized tensor program.
[0080] In the embodiment of the present invention, in step S101, dividing the tensor program to be optimized into linear tensor subprograms includes:
[0081] Determining a nonlinear activation function operator in the tensor program to be optimized;
[0082] The tensor program to be optimized is divided according to the nonlinear activation function operator to generate linear tensor subprograms.
[0083] In an embodiment of the present invention, a nonlinear operator is used as a split point in the tensor program to be optimized. Nonlinear operators such as activation functions in deep neural networks are widely used in tensor programs. Usually, each or several linear operators are followed by a nonlinear activation function operator (such as ReLU or sigmoid). In an embodiment of the present invention, these operators can effectively split the tensor program into subroutines of the desired size. Moreover, for the latest equivalent graph optimization algorithms, almost no nonlinear operators are included in their replacement mode (except for operator fusion, which will be processed in code generation optimization), so leaving the nonlinear operator outside the subroutine will not significantly affect the optimization effect.
[0084] For two multilinear tensor programs and if and The input and output have the same number and shape, then it is called for mutation (their calculations do not need to be equivalent). is a subroutine of a tensor program, then use replace The tensor program is still legal, but may produce different results, so the replacement of mutations can ensure the legality of the program, but cannot guarantee the correctness or mathematical equivalence of the program.
[0085] After the tensor program is partitioned by nonlinear operators, the size of the subprogram can be further adjusted. In order to combine multiple independent operators into larger groups or batches to process operators for more optimization opportunities, several subprograms that do not depend on each other are combined into larger subprograms, such as multiple branches in the Inception network. On the other hand, if the subprogram is too large, only a subset of operators in the subprogram will be mutation optimized at a time.
[0086] For each subprogram, all possible local equivalent deformations based on memory rearrangement are explored and all possible mutations of the subprogram are generated. Each mutation has the same input tensor and output tensor of the same shape as the atomic program.
[0087] Specifically, in the embodiment of the present invention, generating the mutation program of the subprogram according to the preset operator set includes:
[0088] Step 1, enumerating the tensors in the subroutine as inputs of each operator in the preset operator set;
[0089] Step 2, adding the output tensor of the operator to the subroutine;
[0090] Step 3, determining whether the size of the subroutine exceeds a preset threshold. If the threshold is not reached, executing steps 1 to 3. If the threshold is reached, terminating.
[0091] In the embodiment of the present invention, the preset operator set includes: computationally intensive operators, element-by-element operators and tensor operation operators.
[0092] In the embodiment of the present invention, for any given subroutine According to a given set of operators As a basis to generate its possible mutations, where the operator set Covers the most commonly used operators in DNN (Deep Neural Network) and other tensor programs, including: computationally intensive operators (such as conv, matmul, etc.) and element-by-element operators (such as add, mul, etc.), as well as tensor operation operators (such as split, transpose, etc.). It can be easily extended to cover different types of tensor programs according to user needs. To generate potential mutations, we start with a program that has no operators but contains The empty program starts with the original input tensor in and enumerates the set Each operator in , enumerates all available tensors in the current program as inputs of the operator, and adds its output tensor to the current program until the size of the current program reaches a certain threshold (called mutation depth). With the original program If the input and output have the same number and shape, the mutation is considered for An effective mutant.
[0093] In order to ensure the end-to-end correctness of the optimized tensor program, the outputs of the tensor subprograms before and after optimization are compared to find out the inequality and correct them. In this embodiment, the mutation error correction process uses a strict linear algebra theory foundation to simplify this extremely complex task.
[0094] Generate higher performance mutations for subroutines, but do not guarantee computational equivalence between the mutations and the original program. In order to maintain the end-to-end correctness of the tensor program, it is necessary to further identify the locations in the mutations that are not equivalent to the atomic program and correct them. and one of its mutations As input, it automatically finds the inequivalent positions in its output and generates the corresponding error correction kernel to ensure and Functionally equivalent.
[0095] To simplify the analysis, assume that each input subroutine and its mutation have only one output. The results can be easily generalized to subroutines with multiple outputs by analyzing each output sequentially. The computation of the output tensor often involves reductions along some dimensions. For example, the convolution operator conv accumulates the products of the input tensor and the weight tensor in the height and width dimensions over the channel dimension.
[0096] The output positions in the same reduction domain have the same reduction boundaries and similar mathematical properties. The mutation corrector will perform equivalence checks based on this. The embodiments of the present invention are based on the following two linear algebraic theories:
[0097] Theorem 1: For two MLTPs with m-dimensional output tensors and make is a set of m-dimensional basis vectors, that is is a tuple of length m, where the i-th element is 1 and the others are 0.
[0098] make for and A reduction domain of for Any position in . 1≤j≤m. If 0≤i≤m, but
[0099] Theorem 2: For two MLTP and make for and An inequivalent position of Let I be a randomly generated input, then The possibility is at most Where d is the number of all possible values of the input variable I.
[0100] Theorem 1 shows that if and If m+1 specific positions in a reduction domain are equivalent, then all other positions in the reduction domain are also equivalent. The theorem adopted in this embodiment greatly reduces the workload of verification: there is no need to check all positions in the output tensor, but only to test m+1 specific positions in each reduction domain.
[0101] Theorem 2 shows that if two subroutines are unequal at some position, then if they are tested randomly with 32-bit integers, the probability that they will be the same on this random input is at most 2. -32 .
[0102] By combining Theorems 1 and 2, checking all combinations of input values that all output positions depend on is reduced to a lightweight task that only requires testing a few representative positions using a few randomly generated inputs.
[0103] In the embodiment of the present invention, error correction processing is performed on inequivalent mutation programs in the mutation programs of the subprogram so that each mutation program is equivalent to the corresponding subprogram, including:
[0104] Determining the reduction domain of the subprogram and its mutant programs by using reduction domain propagation;
[0105] For the overlapping area of any two reduction domains, identify m+1 positions and perform random tests to determine the inequivalent mutation programs; where m is the dimension of the subprogram output tensor;
[0106] Generate an error correction kernel according to the subroutines corresponding to the non-equivalent mutation programs;
[0107] Correct inequivalent mutation programs using the generated error correction kernel.
[0108] The verification algorithm for mutation error correction in the embodiment of the present invention is as follows:
[0109] Step 1: Reduction domain propagation. First, the reduction domain of a given subroutine is calculated by reduction domain propagation. The concept of reduction domain propagation is similar to forward and backward propagation in deep learning, that is, the reduction domain of the operator output tensor is calculated by analyzing the dependencies of the operator calculations according to the reduction domain of the input tensor. A set of split points is maintained for each dimension of the tensor to identify the boundaries of its reduction domain. For linear operators, the split points of their output tensors can be inferred based on the split points of the input tensor as well as the operator type and hyperparameters.
[0110] Step 2: Perform random tests on each reduction domain. and its mutation After finding all the reduction domains in the output tensor of , we check the intersection of each pair of reduction domains in the two programs using the theorem mentioned above. If the two reduction domains do not have any overlapping regions, they can be skipped. For each overlapping region, we check the equivalence of the two programs at a set of m+1 positions identified by Theorem 1, where m is the output tensor dimension (e.g., m=4 in the subprogram in the figure above because the output of conv is 4-dimensional). At these m+1 positions, we perform random tests using a set of randomly generated inputs.
[0111] Step 3: Error correction kernel. For each reduction domain that fails the random test, an error correction kernel is generated to correct its output to ensure the mathematical equivalence between the original subroutine and its mutation. To correct the output, the error correction kernel needs to perform the same set of operators on the reduction domain that is not equivalent to the original subroutine to make corrections to these positions.
[0112] In order to reduce the additional overhead introduced by the error correction kernel, the generated error correction kernel is optimized through the kernel fusion optimization technology. When the error correction kernel has the same operator as the optimized subroutine, it can be merged into one computing kernel through the kernel fusion optimization method. In the case where the error correction kernel and the optimized subroutine have different operators, since the error correction kernel has the same operator as the atomic program before optimization, and the operator of the optimized subroutine is obtained by transforming the operator of the atomic program, it is always possible to transform the operator of the error correction kernel into the same operator as the optimized subroutine, and further perform fusion optimization.
[0113] In the embodiment of the present invention, the step of selecting the optimal subprogram from the mutation program after error correction processing and splicing it to generate the optimized tensor program includes:
[0114] A greedy algorithm is used to select K candidate subroutines of each subroutine from the mutation program after error correction processing; K is a preset value;
[0115] Select the candidate subprogram with the smallest conversion cost of the previous and next subprograms after splicing from the K candidate subprograms of each subprogram as the optimal subprogram of each subprogram;
[0116] The determined optimal subprograms are concatenated to generate the optimized tensor program.
[0117] In the embodiment of the present invention, the step of splicing the determined optimal subprograms to generate an optimized tensor program includes:
[0118] splicing the determined optimal subprograms to generate a spliced tensor program;
[0119] Perform reversible operator elimination on the concatenated tensor program;
[0120] Perform operator fusion on the spliced tensor program after reversible operator elimination based on the preprocessed weight tensor to generate an optimized tensor program; wherein the preprocessed weight tensor is determined by deforming the preset weight tensor according to the calculation graph of the spliced tensor program.
[0121] After program partitioning, the input program will be converted into a list of subroutines. The global optimizer in code generation optimization traverses each subroutine to generate its mutation list by querying the mutation generator, and keeps the top K candidate programs Cands of the entire program with the best performance so far in a greedy manner. For each subroutine, K candidates will be selected and saved (because saving all mutations will take up a lot of space), and then the most suitable one will be selected in the subsequent splicing process. In this embodiment, the so-called most suitable subroutine is the subroutine with the smallest conversion overhead at the "joint" with the previous and next subroutines after splicing.
[0122] In order to explore a sufficiently large mutation space for each subroutine at a reasonable time and space cost, the mutation process is carefully designed by the global optimizer through several key hyperparameters. First, if a subroutine is too large, it is divided into smaller subsets of operators, and mutation generation is performed only on each subset, keeping the remaining operators unchanged while mutation generation is performed on one subset of operators. Second, by allowing iterative mutation on a subroutine for at most r rounds (r is a preset adjustable parameter value), the search space can be greatly expanded and more complex and potentially better mutation generation can be achieved.
[0123] After completing the optimization of the subroutines, it is also necessary to connect the optimization results of all subroutines together. In addition to connecting their input and output tensors, some post-optimization processing needs to be performed on the boundaries across subroutines to further improve the overall performance. In one embodiment of the present invention, the R / T operator and the nonlinear ReLU operator are first reordered so that all R / T operators at the connection of the two subroutines are continuous. Obviously, since the calculation of nonlinear activation function operators including ReLU is element-by-element, the reordering operation can ensure its correctness. Next, three post-optimization processing will be continued:
[0124] 1. Reversible operator elimination. Any set of R / T operators that can cancel each other (i.e., running these operators on a tensor is equivalent to no operation) is called a reversible transformation. Obviously, reversible transformations can be deleted from the program without affecting correctness.
[0125] 2. Operator fusion. The remaining continuous memory rearrangement operators R / T are fused into a single kernel to reduce kernel launch overhead (R / T-DH). At the same time, for activation operators such as ReLU, they are also fused into their adjacent computational operators (Conv-Relu-CF) like classic optimization.
[0126] 3. Preprocessing. If a tensor is statically known (such as a weight tensor), the post-optimization processor will complete its memory reordering in the preprocessing stage, such as Figure 6 For the weight tensors w1 and w2 of the convolution in (b), the operators R / TB and R / TI corresponding to the memory reordering operations will be executed during the preprocessing of the weights instead of being completed at runtime.
[0127] In deep learning applications, there are some tensors used to store weights, and these weight tensors are statically determined. Preprocessing means that before reasoning, the weight tensors are transformed according to the optimized calculation graph, and then the weight tensors are stored with the transformed results, so that there is no need to execute these transformation operators again when actually reasoning.
[0128] The tensor program optimization method provided by the present invention is an optimization method based on memory rearrangement and local equivalent transformation. It is not currently used by other frameworks. Although most of the transformations can be completed by a combination of classical operators, if these operators are not subjected to special code generation optimization processing, the optimization effect cannot be reflected. Therefore, the system described in the present invention is a complete system, and the existing framework cannot replace most of the work therein. In addition, the present invention can achieve a speedup ratio of up to 2 times or more by combining with different backends (such as cuDNN / cuBLAS, TVM, Ansor).
[0129] An embodiment of the present invention also provides a tensor program optimization system based on memory rearrangement and local equivalent transformation, including several main modules: program partitioning, mutation generator, mutation corrector and code generation optimization.
[0130] The overall architecture of the tensor program optimization system provided in this embodiment is as follows: Figure 2 As shown:
[0131] In the embodiment of the present invention, the input of the tensor program optimization system is a tensor program to be optimized. First, the input program is divided into smaller subprograms to reduce the search space to be explored, and all subprograms are guaranteed to be linear programs (to apply the subsequent verification theory).
[0132] In this embodiment, for each subprogram, the mutation generator explores all possible local equivalent deformations based on memory rearrangement and generates all possible mutations of the subprogram. Each mutation has the same input tensor and output tensor of the same shape as the atomic program.
[0133] In order to ensure the end-to-end correctness of the optimized tensor program, the mutation error corrector compares the output of the tensor program before and after optimization, finds out the inequivalent positions and corrects them. The mutation error corrector uses the rigorous theoretical foundation of linear algebra to simplify this extremely complex task.
[0134] The mutations that have completed error correction will be further passed through the program optimizer, which will combine the mutations of each subprogram into a complete tensor program in the best way and further optimize it to obtain the optimized tensor program.
[0135] The following will introduce several modules including program partitioning, mutation generator, mutation corrector and code generation optimization, and use several typical security examples to introduce the optimization effects that can be achieved by this system.
[0136] (I) Division of procedures:
[0137] In this embodiment, nonlinear operators are used as splitting points in the input program. First, nonlinear operators such as activation functions in deep neural networks are widely used in tensor programs. Usually, each or several linear operators are followed by nonlinear activation function operators (such as ReLU or sigmoid), through which the tensor program can be effectively split into subroutines of the desired size. Secondly, since the theoretical basis of the mutation corrector is only applicable to multilinear tensor programs, any nonlinear operators must be excluded from the subroutines, which makes nonlinear operators a natural choice as splitting points. Third, for the latest equivalent graph optimization algorithms, almost no nonlinear operators are included in their replacement patterns (except for operator fusion, which will be processed in code generation optimization), so keeping them outside the subroutines will not significantly affect the optimization effect.
[0138] After the tensor program is partitioned by nonlinear operators, the size of the subprogram can be further adjusted. In order to combine multiple independent operators into larger groups or batches to process operators for more optimization opportunities, several subprograms that do not depend on each other are combined into larger subprograms, such as multiple branches in the Inception network. On the other hand, if the subprogram is too large, only a subset of operators in the subprogram will be mutation optimized at a time.
[0139] (II) Mutation Generator:
[0140] For two multilinear tensor programs and if and The input and output have the same number and shape, then it is called for mutation (their calculations do not need to be equivalent). is a subroutine of a tensor program, then use replace The tensor program is still legal, but may produce different results, so the replacement of mutations can ensure the legality of the program, but cannot guarantee the correctness or mathematical equivalence of the program.
[0141] For any given The mutation generator generates mutations based on a given set of operators. As a basis to generate its possible mutations, the operator set It covers the most commonly used operators in DNN and other tensor programs, including computationally intensive operators (such as conv, matmul, etc.) and element-wise operators (such as add, mul, etc.), as well as tensor operation operators (such as split, transpose, etc.). This set can be easily extended to cover a variety of different types of tensor programs according to user needs. To generate potential mutations, the mutation generator starts with a set of operators that does not have operators but contains The empty program starts with the original input tensor in and enumerates the set Each operator in the program also enumerates all available tensors in the current program as inputs of the operator, and adds its output tensor to the current program until the size of the current program reaches a certain threshold (called mutation depth, threshold in the algorithm below). With the original program have the same number and shape of inputs and outputs, the mutation generator will consider the mutation for An effective mutant.
[0142] The following is used to generate Code for a depth-first search algorithm for possible mutants.
[0143]
[0144] (III) Mutation Error Corrector:
[0145] The mutation generator can generate higher-performance mutations for a subprogram, but it cannot guarantee the computational equivalence between the mutation and the original program. In order to maintain the end-to-end correctness of the tensor program, it is necessary to further find out the locations in the mutation that are not equivalent to the atomic program and correct them. and one of its mutations As input, it automatically finds the inequivalent positions in its output and generates the corresponding error correction kernel to ensure and Functionally equivalent.
[0146] To simplify the analysis, assume that each input subroutine and its mutation have only one output. The results can be easily generalized to subroutines with multiple outputs by analyzing each output sequentially. The computation of the output tensor often involves reductions along some dimensions. For example, the convolution operator conv accumulates the products of the input tensor and the weight tensor in the height and width dimensions over the channel dimension.
[0147] However, different positions in the output tensor may involve different reduction bounds. For example, for a convolution with a kernel size of 3×3, the computation of the top-left output position involves only the 2×2 position in the kernel, because the computation at that position exceeds the bounds of the input tensor in the leftmost column and topmost row (with the same padding), as Figure 3 As shown in . Output positions with the same reduction bounds can be classified into the same reduction domain, that is, if multiple positions in the output tensor share exactly the same reduction bounds, they belong to the same reduction domain. Figure 3 Nine reduction domains and their corresponding reduction boundaries for conv with a 3×3 kernel are shown.
[0148] The output positions in the same reduction domain have the same reduction bounds and similar mathematical properties, and the mutation corrector performs equivalence checks based on this. The mutation corrector is based on the following two linear algebraic theories:
[0149] Theorem 1: For two subroutines with m-dimensional output tensors and make is a set of m-dimensional basis vectors, that is is a tuple of length m, where the i-th element is 1 and the others are 0.
[0150] make for and A reduction domain of for Any position in . 1≤j≤m. If 0≤i≤m, but
[0151] Theorem 2: For two subroutines and make for and An inequivalent position of Let I be a randomly generated input, then The possibility is at most Where d is the number of all possible values of the input variable I.
[0152] Theorem 1 shows that if and If m+1 specific positions in a reduction domain are equivalent, then all other positions in this reduction domain are also equivalent. This theorem greatly reduces the workload of verification: the mutation corrector does not need to check all positions in the output tensor, but only needs to test m+1 specific positions in each reduction domain.
[0153] Theorem 2 shows that if two subroutines are unequal at some position, then if they are tested randomly with 32-bit integers, the probability that they will be the same on this random input is at most 2. -32 .
[0154] By combining Theorems 1 and 2, the mutation corrector simplifies the task of checking all combinations of input values that all output positions depend on to a lightweight task that only requires testing a few representative positions using a few randomly generated inputs.
[0155] In this embodiment, the verification algorithm of the mutation error corrector is as follows:
[0156] Step 1: Reduction domain propagation. First, the mutation corrector calculates the reduction domain of a given subroutine through reduction domain propagation. The concept of reduction domain propagation is similar to forward and backward propagation in deep learning, that is, the reduction domain of the operator output tensor is calculated by analyzing the dependencies of the operator calculations based on the reduction domain of the input tensor. The mutation corrector maintains a set of split points for each dimension of the tensor to identify the boundaries of its reduction domain. For linear operators, the split points of their output tensors can be inferred based on the split points of the input tensor as well as the operator type and hyperparameters. Figure 4 The reduction domain propagation process is shown.
[0157] Step 2: Perform random tests on each reduction domain. and its mutation After checking all the reduction domains in the output tensor of , the mutation corrector checks the intersection of each pair of reduction domains in the two programs using the theorem mentioned above. If the two reduction domains do not have any overlapping regions, they can be skipped. For each overlapping region, the equivalence of the two programs is checked at a set of m+1 positions identified by Theorem 1, where m is the dimension of the output tensor. At these m+1 positions, the mutation corrector performs random tests using a set of randomly generated inputs.
[0158] Step 3: Error correction kernel. For each reduction domain that fails the random test, the tensor program optimization system generates an error correction kernel to correct its output to ensure the mathematical equivalence between the original subprogram and its mutation. To correct the output, the error correction kernel needs to perform the same set of operators on the reduction domain that is not equivalent to the original subprogram to make corrections to these locations.
[0159] like Figure 5 The process from (a) to (b) shows the generation of the error correction kernel. Figure 5 (b) to (c) are schematic diagrams of the error correction kernel fusion process.
[0160] Fusion optimization of error correction kernel: In order to reduce the additional overhead introduced by the error correction kernel, this work optimizes the generated error correction kernel through kernel fusion optimization technology. Figure 5 As shown in the process from (b) to (c), when the error correction kernel is the same as the operator of the optimized subroutine (i.e., Conv-1 and Conv-2, sharing weights W 1 ), which can be combined into a single computational kernel (i.e. Figure 5 Conv-1-2 in (c) of ). For the case where the error correction kernel and the optimized subroutine have different operators, since the error correction kernel is the same as the atomic program operator before optimization, and the optimized subroutine operator is obtained by transforming the atomic program operator, it is always possible to transform the error correction kernel operator into the same operator as the optimized subroutine, and further perform fusion optimization.
[0161] (IV) Code generation optimization:
[0162] After program partitioning, the input program will be converted into a list of subroutines. The global optimizer in code generation optimization traverses each subroutine to generate its mutation list by querying the mutation generator, and greedily keeps the top K candidates of the entire program with the best performance so far. For each subroutine, K candidates will be selected and saved (because saving all mutations will take up a lot of space, so K candidates are selected), and then the most suitable subroutine will be selected for splicing in the subsequent splicing process. The so-called most suitable subroutine refers to the subroutine with the smallest conversion overhead at the "seam" between the previous and next subroutines after splicing.
[0163] In order to explore a sufficiently large mutation space for each subroutine at a reasonable time and space cost, the mutation process is carefully designed by the global optimizer through several key hyperparameters. First, if a subroutine is too large, it is divided into smaller subsets of operators and mutation generation is performed only on each subset, while mutation generation is performed on one subset of operators, leaving the remaining operators unchanged. Second, by allowing iterative mutation on a subroutine for at most r rounds, the search space can be greatly expanded and more complex and potentially better mutation generation can be achieved.
[0164] The present invention involves two key steps in the program splicing process: one is how to search each subroutine separately (i.e., the process of finding the most suitable mutation), and the other is post-optimization processing during the splicing process, so as to make the generated program more efficient.
[0165] After completing the optimization of the subprograms, it is necessary to connect the optimization results of all subprograms together. The specific process of subprogram connection is the process of splicing several computational graphs, which is a basic operation in the field of deep learning optimization and will not be described here.
[0166] After completing the optimization of the subroutines, it is also necessary to connect the optimization results of all subroutines together. In addition to connecting their input and output tensors, it is also necessary to perform some post-optimization processing on the boundaries across subroutines to further improve the overall performance.
[0167] Figure 6 The results of mutation optimization of two subroutines are given. First, the R / T operator and the nonlinear ReLU operator are reordered, as follows: Figure 6 As shown in (b), all R / T operators at the connection of the two subroutines are continuous. Obviously, since the calculation of nonlinear activation function operators including ReLU is element-by-element, the reordering operation can guarantee its correctness. Next, three post-optimization processes will be carried out:
[0168] 1. Reversible operator elimination. Any set of R / T operators that can cancel each other (i.e., running these operators on a tensor is equivalent to no operation) is called a reversible transformation. Obviously, reversible transformations can be deleted from the program without affecting correctness. Figure 6 In the example of (b), R / TE and R / TG are reversible deformations that can be eliminated.
[0169] 2. Operator fusion. Figure 6 As shown in (c), the post-optimization processor fuses the remaining continuous memory rearrangement operators R / T into a single kernel to reduce the kernel launch overhead (R / T-DH). At the same time, for activation operators such as ReLU, they are also fused into their adjacent computational operators (Conv-Relu-CF) like classical optimization.
[0170] 3. Preprocessing. If a tensor is statically known (such as a weight tensor), the post-optimization processor will complete its memory reordering in the preprocessing stage, such as Figure 6 For the weight tensors w1 and w2 of the convolution in (b), the operators R / TB and R / TI corresponding to the memory reordering operations will be executed during the preprocessing of the weights instead of being completed at runtime.
[0171] In deep learning applications, there are some tensors used to store "weights". These weight tensors can be determined statically (to be precise, they are determined during the training process. The work of this invention is oriented to reasoning, and these weights are determined before reasoning). Preprocessing, that is, before reasoning, the weight tensor is deformed according to the optimized calculation graph (the operators used for deformation are as follows Figure 6 RT-B in , etc.), and then store these weight tensors as the deformed results, so there is no need to execute these deformation operators again during inference.
[0172] Typical optimization cases:
[0173] Case 1: If Figure 7 As shown in the figure, two separate images are connected into a larger image along the width direction, that is, the data of N dimension is transferred to W dimension. Under a specific dimension size, this deformation can provide greater parallelism and improve the locality of calculation, thereby improving performance. The idea of memory reordering provides new opportunities for the optimization of tensor programs. However, after the transformation, elements that are not equal to the original result are generated in the sub-region of the output tensor along the merge boundary (the oblique line shaded position in Figure (b)), so this optimization is a locally equivalent optimization, rather than a completely equivalent optimization.
[0174] Case 2: If Figure 8 A mutation is given, which will transform the calculation of the dilated convolution into the calculation of the standard convolution through memory rearrangement. (The diagonal shaded part in the figure) shows the unequal positions in the mutation, and the tensor program optimization system will further correct it through the mutation corrector. This optimization transforms the inefficient dilated convolution into a standard convolution that is highly optimized by the existing operator library, and can use efficient algorithms such as Winograd and FFT.
[0175] Case 3: Fig. 9 Two graph transformation strategies for optimizing the Inception module are shown. For two parallel conv operators with different output channels, Fig. 9(a) shows a non-equivalent transformation based on memory reordering, which fills W2 with 0 to make it have the same shape as W1, so that the two conv operators can be merged into a groupconv operator. In the error correction process, it is necessary to delete the result of the 0 calculation filled in W2 (the tensor marked as zeros, from which it can be deduced that all elements of the tensor are 0).
[0176] Fig. 9 (b) shows the equivalent transformation found. The basic principle of this transformation is also the rearrangement of memory, but in this process, some tensors need to be redundantly copied. This transformation copies the input tensor I2 and merges the input tensor and weight through the concat operator, thereby fusing the two conv operators into a group conv operator.
[0177] The main body of the present invention is a tensor program optimization system based on memory rearrangement and local equivalent transformation. The system provides a corresponding interface for users to build a tensor program calculation graph, and also supports the import of models in onnx format. Its output is an executable tensor program.
[0178] The present invention can make the execution of tensor programs more efficient.
[0179] In the embodiments of the present invention, the server used in the experiment is equipped with two 28-core Intel Xeon E52680 v4 (with hyperthreading enabled), 256GB DRAM and an NVIDIA Tesla V100 GPU. All experiments use CUDA 10.2 and cuDNN 7.6.5, except for experiments related to TVM and Ansor, which directly use the best kernels generated by these two tensor compilers.
[0180] Five real DNN models were used in the test program experiments:
[0181] Resnet-18, a widely used convolutional neural network for image classification;
[0182] CSRNet, a dilated convolutional network for semantic segmentation, can arbitrarily adjust the sampling rate to expand the receptive field for more accurate prediction results;
[0183] Inception-v3, an improved version of GoogleNet, consists of carefully designed Inception modules to improve accuracy and reduce computational complexity;
[0184] BERT, a network architecture for natural language processing with very high accuracy;
[0185] Resnet18-3D, a neural network for video processing;
[0186] In end-to-end experiments, the present invention can achieve a speedup ratio of up to 2.51 times compared with existing work. Fig.10 shown.
[0187] In operator-level experiments, the present invention can achieve a speedup ratio of more than 2 times by combining with different backends (cuDNN / cuBLAS, TVM, Ansor), such as Fig.11 shown.
[0188] The program optimization method based on memory rearrangement and local equivalent transformation of the present invention is not currently used by other frameworks. Although most transformations can be completed by combining classical operators, the optimization effect cannot be reflected if these operators are not specially optimized for code generation. Therefore, the system described in the present invention is a complete system, and the existing framework cannot replace most of the work in it.
[0189] like Fig.12 As shown, the present invention also provides a tensor program optimization device, comprising:
[0190] A program partitioning module 201 is used to partition the tensor program to be optimized into linear tensor subprograms;
[0191] A mutation generation module 202, used to generate a mutation program of the subprogram according to a preset operator set;
[0192] A mutation error correction module 203 is used to perform error correction on inequivalent mutation programs in the mutation programs of the subprograms so that each mutation program is equivalent to the corresponding subprogram;
[0193] The optimized program generation module 204 selects the best subprogram from the mutant program after error correction and concatenates them to generate the optimized tensor program.
[0194] For those skilled in the art, the implementation method of a tensor program optimization device provided by the present invention can be clearly known through the description of the above embodiments, and will not be repeated here.
[0195] This embodiment also provides an electronic device, which may be a desktop computer, a tablet computer, a mobile terminal, etc., but this embodiment is not limited thereto. In this embodiment, the electronic device may refer to the embodiments of the aforementioned method and device, the contents of which are incorporated herein, and the repeated parts are not repeated.
[0196] Fig.13 FIG. 6 is a schematic block diagram of a system structure of an electronic device 600 according to an embodiment of the present invention. Fig.13As shown, the electronic device 600 may include a central processor 100 and a memory 140; the memory 140 is coupled to the central processor 100. It should be noted that this figure is exemplary; other types of structures may also be used to supplement or replace this structure to implement telecommunication functions or other functions.
[0197] In one embodiment, the tensor program optimization function may be integrated into the central processing unit 100. The central processing unit 100 may be configured to perform the following control:
[0198] The tensor program to be optimized is divided into linear tensor subprograms;
[0199] Generate a mutation program of the subprogram according to a preset operator set;
[0200] Correcting the inequivalent mutation programs in the mutation programs of the subprograms so that each mutation program is equivalent to the corresponding subprogram;
[0201] The optimal subroutine is selected from the mutated program after error correction and concatenated to generate the optimized tensor program.
[0202] In another embodiment, the tensor program optimization device may be configured separately from the central processing unit 100. For example, the tensor program optimization device may be configured as a chip connected to the central processing unit 100, and the tensor program optimization function is implemented under the control of the central processing unit.
[0203] like Fig.13 As shown, the electronic device 600 may further include: a communication module 110, an input unit 120, an audio processing unit 130, a display 160, and a power supply 170. It is worth noting that the electronic device 600 does not necessarily have to include Fig.13 In addition, the electronic device 600 may also include Fig.13 For components not shown, reference may be made to the prior art.
[0204] like Fig.13 As shown, the central processor 100 is sometimes also referred to as a controller or an operation control, and may include a microprocessor or other processor devices and / or logic devices. The central processor 100 receives inputs and controls the operations of various components of the electronic device 600.
[0205] The memory 140 may be, for example, one or more of a cache, a flash memory, a hard drive, a removable medium, a volatile memory, a non-volatile memory or other suitable devices. The above-mentioned information related to the failure may be stored, and a program for executing the relevant information may also be stored. The CPU 100 may execute the program stored in the memory 140 to implement information storage or processing.
[0206] The input unit 120 provides input to the CPU 100. The input unit 120 is, for example, a key or a touch input device. The power supply 170 is used to provide power to the electronic device 600. The display 160 is used to display display objects such as images and text. The display may be, for example, an LCD display, but is not limited thereto.
[0207] The memory 140 may be a solid-state memory, such as a read-only memory (ROM), a random access memory (RAM), a SIM card, etc. It may also be a memory that saves information even when the power is off, can be selectively erased, and is provided with more data, examples of which are sometimes referred to as EPROMs, etc. The memory 140 may also be some other type of device. The memory 140 includes a buffer memory 141 (sometimes referred to as a buffer). The memory 140 may include an application / function storage unit 142, which is used to store application programs and function programs or processes for executing the operation of the electronic device 600 through the central processor 100.
[0208] The memory 140 may also include a data storage unit 143 for storing data, such as contacts, digital data, pictures, sounds, and / or any other data used by the electronic device. The driver storage unit 144 of the memory 140 may include various drivers for communication functions of the electronic device and / or for executing other functions of the electronic device (such as messaging applications, address book applications, etc.).
[0209] The communication module 110 is a transmitter / receiver 110 that transmits and receives signals via an antenna 111. The communication module (transmitter / receiver) 110 is coupled to the central processor 100 to provide input signals and receive output signals, which may be the same as the case of a conventional mobile communication terminal.
[0210] Based on different communication technologies, multiple communication modules 110 may be provided in the same electronic device, such as a cellular network module, a Bluetooth module and / or a wireless LAN module. The communication module (transmitter / receiver) 110 is also coupled to a speaker 131 and a microphone 132 via an audio processor 130 to provide an audio output via the speaker 131 and receive an audio input from the microphone 132, thereby realizing a common telecommunication function. The audio processor 130 may include any suitable buffer, decoder, amplifier, etc. In addition, the audio processor 130 is also coupled to the central processor 100, so that the sound can be recorded on the local machine through the microphone 132, and the sound stored on the local machine can be played through the speaker 131.
[0211] An embodiment of the present invention also provides a computer-readable program, wherein when the program is executed in an electronic device, the program enables a computer to execute the tensor program optimization method as described in the above embodiment in the electronic device.
[0212] An embodiment of the present invention further provides a storage medium storing a computer-readable program, wherein the computer-readable program enables a computer to execute the tensor program optimization described in the above embodiment in an electronic device.
[0213] The preferred embodiments of the present invention have been described above with reference to the accompanying drawings. Many features and advantages of these embodiments are clear from this detailed description, and therefore the appended claims are intended to cover all such features and advantages of these embodiments that fall within their true spirit and scope. In addition, since many modifications and changes are readily apparent to those skilled in the art, the embodiments of the present invention are not intended to be limited to the precise structure and operation illustrated and described, but all suitable modifications and equivalents that fall within their scope may be included.
[0214] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0215] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0216] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1A function specified in one or more boxes.
[0217] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0218] The present invention uses specific embodiments to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A tensor program optimization method, characterized in that: The method includes: The tensor program to be optimized is divided into linear tensor subprograms; Generate a mutation program of the subprogram according to a preset operator set; Correcting the inequivalent mutation programs in the mutation programs of the subprograms so that each mutation program is equivalent to the corresponding subprogram; Select the best subprogram from the mutation program after error correction and concatenate them to generate the optimized tensor program; Wherein, the step of dividing the tensor program to be optimized into linear tensor subprograms comprises: Determining a nonlinear activation function operator in the tensor program to be optimized; The tensor program to be optimized is divided according to the nonlinear activation function operator to generate linear tensor subprograms; Wherein, the mutation program of generating the subprogram according to the preset operator set includes: Step 1, enumerating the tensors in the subroutine as inputs of each operator in the preset operator set; Step 2, adding the output tensor of the operator to the subroutine; Step 3, determining whether the size of the subroutine exceeds a preset threshold value, if it does not reach the threshold value, executing steps 1 to 3, and terminating if it is determined that the threshold value is reached; Among them, the preset operator set includes: computationally intensive operators, element-by-element operators and tensor operation operators.
2. The tensor program optimization method according to claim 1, characterized in that: The error correction process for the inequivalent mutation programs in the mutation programs of the subprogram so that each mutation program is equivalent to the corresponding subprogram includes: Determining the reduction domain of the subprogram and its mutant programs by using reduction domain propagation; For the overlapping area of any two reduction domains, identify m+1 positions and perform random tests to determine the inequivalent mutation programs; where m is the dimension of the subprogram output tensor; Generate an error correction kernel according to the subroutines corresponding to the non-equivalent mutation programs; Correct inequivalent mutation programs using the generated error correction kernel.
3. The tensor program optimization method according to claim 1, characterized in that: The method of selecting the best subprogram from the mutation program after error correction and splicing it to generate the optimized tensor program includes: A greedy algorithm is used to select K candidate subroutines of each subroutine from the mutation program after error correction processing; K is a preset value; Select the candidate subprogram with the smallest conversion cost of the previous and next subprograms after splicing from the K candidate subprograms of each subprogram as the optimal subprogram of each subprogram; The determined optimal subprograms are concatenated to generate the optimized tensor program.
4. The tensor program optimization method according to claim 3, characterized in that: The step of splicing the determined optimal subprograms to generate an optimized tensor program includes: splicing the determined optimal subprograms to generate a spliced tensor program; Perform reversible operator elimination on the concatenated tensor program; Perform operator fusion on the spliced tensor program after reversible operator elimination based on the preprocessed weight tensor to generate an optimized tensor program; wherein the preprocessed weight tensor is determined by deforming the preset weight tensor according to the calculation graph of the spliced tensor program.
5. A tensor program optimization device, characterized in that: The device comprises: Program partitioning module, used to partition the tensor program to be optimized into linear tensor subprograms; A mutation generation module, used to generate a mutation program of the subprogram according to a preset operator set; A mutation error correction module, used for performing error correction processing on inequivalent mutation programs in the mutation programs of the subprogram so that each mutation program is equivalent to the corresponding subprogram; The optimization program generation module selects the best subprogram from the mutation program after error correction and splices it to generate the optimized tensor program; Wherein, the program division module includes: An operator determination unit, used to determine a nonlinear activation function operator in the tensor program to be optimized; A partitioning unit, used for partitioning the tensor program to be optimized according to the nonlinear activation function operator to generate linear tensor subprograms; Wherein, the mutation generation module is specifically used for: Step 1, enumerating the tensors in the subroutine as inputs of each operator in the preset operator set; Step 2, adding the output tensor of the operator to the subroutine; Step 3, determining whether the size of the subroutine exceeds a preset threshold value, if it does not reach the threshold value, executing steps 1 to 3, and terminating if it is determined that the threshold value is reached; Wherein, the mutation error correction module comprises: A reduction domain determination unit, used for determining the reduction domain of the subprogram and its mutant program by using reduction domain propagation; The identification unit is used to identify m+1 positions in the overlapping area of any two reduction domains for random testing to determine the inequivalent mutation programs; where m is the dimension of the subprogram output tensor; A kernel generation unit, used for generating an error correction kernel according to a subroutine corresponding to an inequivalent mutation program; The error correction unit is used to correct the inequivalent mutation program using the generated error correction kernel.
6. The tensor program optimization device according to claim 5, characterized in that: The optimization program generation module includes: The candidate program determination unit is used to select K candidate subprograms of each subprogram from the mutant program after error correction processing by using a greedy algorithm; K is a preset value; A selection unit, used for selecting, from the K candidate subprograms of each subprogram, a candidate subprogram with the smallest conversion cost of the previous and next subprograms after splicing as the optimal subprogram of each subprogram; The optimization program splicing unit is used to splice the determined optimal subprograms to generate optimized tensor programs.
7. The tensor program optimization device according to claim 6, characterized in that: The optimization program splicing unit comprises: A splicing unit, used for splicing the determined optimal subprograms to generate a spliced tensor program; A reversible operator elimination unit, used for performing reversible operator elimination on the spliced tensor program; The optimization unit is used to generate an optimized tensor program by performing operator fusion on the spliced tensor program after reversible operator elimination based on the preprocessed weight tensor; wherein the preprocessed weight tensor is determined by deforming the preset weight tensor according to the calculation graph of the spliced tensor program.
8. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method according to any one of claims 1 to 4 is implemented.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program for executing the method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Tensor calculation code optimization method and device, equipment and medium
CN110968321A
Method and device for optimizing tensor calculation performance
CN112559163A
Homomorphic evaluation of tensor programs
US20200076570A1
