CN-115841145-B - Compiling method, readable storage medium and compiler for expression in normalized sentence
Abstract
The present invention relates to a compiling method, a readable storage medium and a compiler for an expression in a normalized sentence, wherein the computing device of the present invention is comprised in an integrated circuit device comprising a universal interconnect interface and other processing means. The computing device interacts with other processing devices to collectively complete a user-specified computing operation. The integrated circuit device may further comprise a storage device coupled to the computing device and the other processing device, respectively, for data storage by the computing device and the other processing device.
Inventors
- Request for anonymity
- Request for anonymity
- Request for anonymity
Assignees
- 中科寒武纪科技股份有限公司
Dates
- Publication Date
- 20260505
- Application Date
- 20210906
Claims (16)
- 1. A compilation method of normalizing expressions in a statement describing a neural network operator, comprising: Merging the expressions in the sentences describing the neural network operator; Setting a level of each expression according to the type of the expression, wherein the level of the expression includes a first level for a constant expression, a second level for an expression representing a nested loop, and a third level for representing other expressions, wherein when the level of the expression of the nested loop is the second level, a sub-level is set according to the level of the loop, and Ranking each expression based on the level of all expressions in the sentence to concatenate expressions having the same level or sub-level with operators to generate a peer expression; All peer expressions are connected in an operator based order of levels or sub-levels to return updated statements that have an equivalent effect to the original statements in the neural network operator, which updated statements are used to find the loop-invariant expressions.
- 2. The compiling method of claim 1, further comprising: Judging whether the statement meets a combination law or not; And if the statement meets the combination rule, executing the setting step.
- 3. The compiling method of claim 1, further comprising: Judging whether the statement meets a combination law or not; and executing the sorting step if the statement meets the combination rule.
- 4. The compiling method of claim 1, further comprising: judging whether the statement is a conditional statement and whether the condition is satisfied corresponds to the same return value, and If the statement is a conditional statement and whether the condition is satisfied corresponds to the same return value, combining judgment conditions with the same return value to update the conditional statement; wherein the setting step sets the level according to the type of the updated conditional statement.
- 5. A compiling method according to claim 2 or 3, wherein the setting step comprises: Judging whether said expression is constant or not, and If the expression is constant, the level is set to be the first level.
- 6. The compiling method according to claim 5, wherein the setting step includes: determining whether the expression includes a nested loop, and The level is set to the second level as the expression includes a nested loop.
- 7. The compiling method according to claim 6, wherein the nested loops comprise N loops, and the step of setting the rank to be a second rank comprises: Setting the sub-level of the outermost circulation as a first sub-level; setting the sub-level of the secondary outer loop to be the second sub-level, and The sub-levels of the other layer loops are set in this way until the sub-level of the innermost layer loop is set as the nth sub-level.
- 8. The compiling method according to claim 7, wherein the setting step includes: the level is set to the third level as the expression is not constant and does not include nested loops.
- 9. The compiling method according to claim 8, wherein the expression includes a plurality of sub-expressions, the setting step includes: If either sub-expression is not constant and does not include nested loops, the level is set to the third level.
- 10. The compiling method according to claim 8, wherein the expression includes a plurality of sub-expressions, the setting step includes: If all the sub-expressions are one of constant and including nested loops, and any one of the sub-expressions includes nested loops, the level is set to be the second level.
- 11. The compiling method according to claim 8, wherein in the sorting step, all expressions in the sentence are arranged in the order of the level of the first level, the second level, and the third level.
- 12. The compiling method according to claim 11, wherein in the sorting step, all the expressions of the level two are arranged in the order of the sub-level first sub-level, the second sub-level to the nth sub-level.
- 13. The compiling method of claim 12, further comprising: recursively judging whether the expression is kept unchanged in the loop based on the updated statement, and if so, determining the expression as the expression with unchanged loop.
- 14. The compiling method of claim 13, further comprising: When the expression satisfies any one of the following conditions, the expression is determined as a loop-invariant expression: the initial variable of the expression is a constant; assignment of variables in the expression is outside of the loop; The use of the arrival variable has only one constant value and the expression of the constant value is also loop-invariant.
- 15. A computer readable storage medium having stored thereon computer program code for normalizing expressions in a sentence describing a neural network operator, which when executed by a processing device, performs the compiling method of any one of claims 1 to 14.
- 16. A compiler that normalizes expressions in a statement, comprising: The judging module is used for judging whether the statement can be normalized or not so as to carry out merging operation on the expressions in the statement describing the neural network operator; A setting module for setting the level of each expression according to the type of the expression, wherein the level of the expression comprises a first level for a constant expression, a second level for an expression representing a nested loop, and a third level for representing other expressions, wherein when the level of the expression of the nested loop is the second level, the sub-level is set according to the level of the loop, and The sorting module is used for sorting each expression based on the grades of all expressions in the statement, so as to connect the expressions with the same grade or sub-grade with operators to generate the same-level expression, and connecting all the same-level expressions with operators based on the order of the grades or sub-grades to return an updated statement which has an equivalent effect to the original statement in the neural network operator, wherein the updated statement is used for finding the expression with unchanged circulation.
Description
Compiling method, readable storage medium and compiler for expression in normalized sentence Technical Field The present invention relates generally to the field of neural networks. More particularly, the present invention relates to a compiling method, a readable storage medium, and a compiler for an expression in a normalized sentence. Background When the algorithm of the neural network is realized, a developer can achieve the same calculation result by utilizing a plurality of calculation methods, and the difference of locality and parallelism of codes can be extended by different calculation methods. TVM is an open source deep learning compiler that generates efficient machine code for different hardware backend by providing both graph-level optimization and operator-level optimization. The TVM defines operators based on tensor expressions and provides a series of scheduling primitives to cycle the operator code. The operator described by the tensor expression is translated by the TVM into a TVM intermediate representation (INTERMEDIATE REPRESENTATION, IR), which is a tree-like, high-level expression that easily describes the loop computation. The TVM implements a series of analysis and transformation optimization passes on the intermediate representation, adjusts the operators, and finally generates efficient code on the target hardware platform. Important operators in deep learning applications, such as convolution, matrix multiplication, vector addition, and the like, are compiled by the TVM to generate multiple layers of nested loops on the intermediate representation, and such programs undergo loop transformation to generate complex expressions associated with loop iteration variables. The loop invariant external optimization mode of the existing non-TVM compiler is to store the calculation result in a physical register so as to avoid repeated calculation when in use. However, the existing high-level intermediate representation of TVM compilers cannot sense the existence of physical registers, and blindly referencing non-TVM compilers to externally lift all loop invariants can bring unnecessary register pressure, rather degrading operator performance. Therefore, the ideal optimizing effect cannot be obtained by directly applying the loop invariant extrapolation algorithm in the existing non-TVM compiler to the TVM compiler. Secondly, the order of operands in the expression during the detection of loop invariance can affect the finding of invariance by the optimization algorithm. The existing optimization algorithm has limited detection capability on complex nested conditional expressions frequently occurring in the deep learning operator, in other words, the optimization degree is poor. Finally, the operator code generated by the TVM compiler also needs to undergo a compilation optimization process by the target platform compiler to generate the executable code of the target hardware. The target platform compiler may make other optimizations on the lower level intermediate representation, and these two optimizations at different levels often conflict, resulting in a reduction in the effectiveness of the loop-invariant boosting algorithm. Therefore, a solution for normalizing expressions in statements at the time of compiling a program is urgent. Disclosure of Invention In order to solve at least partially the technical problems mentioned in the background art, the scheme of the invention provides a compiling method, a readable storage medium and a compiler for an expression in a normalized sentence. In one aspect, the present invention discloses a compiling method for normalizing expressions in a sentence describing a neural network operator, comprising setting a level of the expression according to a type of each expression, and ordering each expression based on the levels of all the expressions in the sentence. In another aspect, the present invention discloses a computer readable storage medium having stored thereon computer program code for normalizing expressions in statements describing a neural network operator, which, when executed by a processing device, performs the foregoing compiling method. In another aspect, the invention discloses a compiler for normalizing expressions in sentences, which comprises a setting module and a sequencing module. The ranking module is used for ranking each expression based on the ranks of all the expressions in the sentence. The invention provides a method for normalizing expression by adjusting operand combination sequence and conversion conditional expression before detecting the circulation invariant, which provides a better foundation for circulation invariant external optimization and subsequent other optimization. Finally, the invention combines the specific characteristics of the TVM intermediate representation and the target platform compiler, and performs special treatment on the inner loop with smaller branch and iteration number, thereby solving