Program compilation method and device

By updating the operation types of variables in the program to handle operation type conflicts between expressions consistently, and using chain rules to combine derivative expressions, the problem of redundant code in the compilation system is solved, and the program operation performance and compilation efficiency are improved.

CN115543330BActive Publication Date: 2025-08-22HUAWEI TECH CO LTD

Patent Information

Application Number
CN202110742873.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-06-30
Publication Date
2025-08-22
Estimated Expiration
2041-06-30

AI Technical Summary

Technical Problem

When the existing compilation system handles operation type conflicts in variables in program expressions, it causes redundant code to be included in the compiled program, which affects the running performance and requires users to spend a lot of time debugging.

Method used

By obtaining the first and second expressions in the program, updating the variable operation type therein so that it is consistent in both, and indicating that there is no differential result or the result is zero after the differential operation, the derivative function expression is combined based on the chain law, and redundant differential operation is omitted.

Benefits of technology

Simplifies compiled program expressions, improves running performance, reduces redundant code, and improves compilation efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115543330B_ABST
    Figure CN115543330B_ABST
Patent Text Reader

Abstract

The present application discloses a program compilation method and device, including: obtaining a first expression and a second expression of a program, the first expression and the second expression generating a dependency relationship based on a first variable, and the operation type of the first variable in the first expression is different from the operation type of the first variable in the second expression; updating the operation type of the first variable in the first expression and / or the operation type of the first variable in the second expression, the updated operation type of the first variable indicating that the first variable has no differential result or the result is zero after encountering a differential operation; processing the first expression and the second expression respectively according to the updated operation type of the first variable to obtain corresponding derivative function expressions; combining the derivative function expressions corresponding to the first expression and the second expression respectively based on the chain rule to obtain the compilation result of the program. By implementing the present application, the compilation result of the program can be simplified and the running performance of the compiled program can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of information technology, and in particular to a program compilation method and device. Background Art

[0002] Derivatives are an important and fundamental concept in mathematics. The derivative of a function at a point describes the rate of change of the function around that point. Automatic Differentiation (AD) is a method for automatically performing function differentiation using a computer program. However, the mathematical expression of a function to be differentiated cannot be understood or read by a computer. Therefore, the user must first define the function using a programming language to obtain a program expression that can be understood by the computer. A compilation system is then used to derive the function expressed in the programming language (this process is also called compilation). During the differentiation process, the compilation system can correctly handle the differences between the program expression and the mathematical expression.

[0003] However, existing compiler systems cannot handle scenarios where variables within program expressions have conflicting operation types when differentiating them. This means that a variable is an input in one expression and an output in another, and that the variable is differentiated in one expression but not in the other. This conflict can lead to redundant code in the compiled program, severely impacting performance and consuming significant debugging time. Summary of the Invention

[0004] The embodiments of the present application provide a program compilation method and device, which can simplify the compiled program expression and improve the running performance of the compiled program.

[0005] In a first aspect, the present application provides a program compilation method, the method comprising: obtaining a first expression and a second expression of a program, the first expression and the second expression both including a first variable, the first variable being an input variable in the first expression, and the first variable being an output variable in the second expression, and the operation type of the first variable in the first expression is different from the operation type of the first variable in the second expression; updating the operation type of the first variable in the first expression and / or the operation type of the first variable in the second expression so that the operation type of the updated first variable is consistent in the first expression and the second expression, and the operation type of the updated first variable indicates that the first variable has no differential result or the result is zero after encountering a differential operation; processing the first expression and the second expression according to the operation type of the updated first variable to obtain a derivative function expression corresponding to the first expression and a derivative function expression corresponding to the second expression; combining the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression based on the chain rule to obtain a compilation result of the program, and the compilation result of the program is related to the combination result of the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression.

[0006] Effectively, when a first variable encounters a differential operation with no differential result, the program's compiled results do not include the item corresponding to the first variable. In other words, when a variable's operation type indicates that the variable encounters a differential operation with no differential result, if the variable is differentiated, the variable is deleted, or the differential result is represented by an identifier. When the variable is differentiated and represented by this identifier, the identifier has no compiled result, meaning the program's compiled results do not include the item corresponding to this character. This means that when a first variable encounters a differential operation with no differential result, the program's compiled results do not include the item corresponding to the first variable. This ensures that the program's compiled results do not contain redundant code, effectively improving the performance of the program's compiled results.

[0007] Optionally, the program may further include multiple groups of expressions, wherein the relationship between the two expressions in each group of expressions is the same as the relationship between the first expression and the second expression described above. The relationship is: in each group of expressions, one expression and the other expression contain the same variable, which is an input variable in one expression and an output variable in the other expression. In the embodiments of the present application, this relationship is referred to as a dependency relationship, i.e., the first expression and the second expression have a dependency relationship or the first expression and the second expression have a dependency relationship based on the first variable.

[0008] Optionally, the first expression and the second expression may be any of multiple groups of dependent expressions in a program. The program compilation result is obtained by combining the derivative expressions corresponding to each expression in the program. In this case, the program compilation result is related to the combination of the derivative expression corresponding to the first expression and the derivative expression corresponding to the second expression. The processing process for each group of dependent expressions is the same as the processing process for the first expression and the second expression.

[0009] Optionally, the compiler / compilation system can use any identifier to represent the operation type of the first variable, for example, a character, a string or other feasible identifier. Furthermore, when the operation type of the first variable in the first expression conflicts with the operation type of the first variable in the second expression (i.e., they are not the same), the operation type of the first variable in the first expression can be represented by the first character, and the operation type of the first variable in the second expression can be represented by the second character. The operation type of the updated first variable can be represented by the third character, that is, the first character and the second character need to be updated at the same time at this time; when the operation type of the updated first variable is represented by the first character or the second character, the first character or the second character is updated. The updated first character, the updated second character or the updated third character indicates that the first variable has no differential result or the result is zero after encountering the differential operation.

[0010] Optionally, when the first variable has no differential result after encountering a differential operation, the differential result may be represented by an identifier, for example, a certain character, which may be a null character or other characters.

[0011] It should be understood that when the first variable is an output variable in the first expression and the first variable is an input variable in the second expression, the compilation process of the program is the same as the compilation process of the program in the above embodiment.

[0012] It can be seen that in the embodiment of the present application, since the first variable is the input variable in the first expression and the first variable is the output variable in the second expression, during the compilation of the program, the differential result of the first variable in the first expression and the differential result of the first variable in the second expression will be combined based on the chain rule. At this time, when the first variable has no differential result or the differential result is zero in one expression, for the compilation process of the entire program, it is not necessary to perform a differential operation on the first variable in the other expression. The embodiment of the present application can check the operation type of the first variable in the first expression and the second expression in advance. When the operation type of the first variable in the first expression and the operation type of the first variable in the second expression are different, the operation type of the first variable in the first expression and / or the operation type of the first variable in the second expression are updated. Since the operation type of the updated first variable indicates that the first variable has no differential result or the result is zero after encountering the differential operation, when the first expression and the second expression are processed separately based on the operation type of the updated first variable, the first variable will not be differentiated, that is, the differential result of the first variable in the first expression and the second expression is omitted, thereby simplifying the derivative function expressions corresponding to the first expression and the second expression respectively. Then, based on the processing of the first expression and the second expression, other groups of expressions with dependencies in the program are processed to simplify the derivative expressions corresponding to other expressions; and after combining the derivative expressions corresponding to each expression in the program based on the chain rule, the compiled result of the program does not contain redundant code, which can effectively improve the running performance of the program compilation result.

[0013] In a feasible implementation manner, when the operation type of the updated first variable is differentiation, the result of the first variable encountering the differentiation operation is zero.

[0014] Optionally, the operation type of the first variable is differentiation or non-differentiation. Differentiation means that the compiler / compilation system running this method performs a differentiation operation on the first variable; non-differentiation means that the compiler / compilation system running this method does not perform a differentiation operation on the first variable.

[0015] It can be seen that in the embodiment of the present application, after combining the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression based on the chain rule, the differential result of the first variable in the compiled result of the program is zero. Therefore, when processing the first expression and the second expression, the differential result of the first variable in the first expression and the second expression is omitted, thereby simplifying the derivative function expressions corresponding to the first expression and the second expression, thereby simplifying the compiled result of the program and effectively improving the running performance of the compiled result of the program.

[0016] In a feasible implementation manner, when the operation type of the updated first variable is non-differentiation, the first variable has no differentiation result after encountering a differentiation operation.

[0017] It can be seen that in the embodiment of the present application, after combining the derivative expression corresponding to the first expression and the derivative expression corresponding to the second expression based on the chain rule, the first variable in the compiled result of the program does not have a differential result. Therefore, when processing the first expression and the second expression, no differential operation is performed on the first variable in the first expression and the second expression. In other words, the derivative expressions corresponding to the first expression and the second expression can be simplified, thereby simplifying the compiled result of the program and effectively improving the running performance of the compiled result of the program.

[0018] In a feasible embodiment, when a user requests to output the derivative of the first output variable of the program to the first input variable of the program, and the derivative is obtained based on the combination of differential results of the first variable, a warning message is generated; wherein the warning message is used to indicate the position of the first expression and the second expression in the program, as well as the first variable.

[0019] Optionally, the program may include at least one input variable and / or at least one output variable, wherein the at least one input variable includes a first input variable and the at least one output variable includes a first output variable.

[0020] As can be seen in the embodiment of the present application, when the first variable encounters a differential operation and no differential result is obtained, and the derivative of the first output variable to the first input variable is obtained by combining the differential results of the first variable, since the derivative also has no result, it conflicts with the user's requirements. A warning message is generated to indicate the location of the first expression and the second expression in the program, as well as the first variable, so that the user can quickly locate the problematic code and make corresponding modifications.

[0021] In a feasible implementation, the operation type of the first variable in the above-mentioned first expression is determined by the mathematical calculation rules indicated by the semantics of the first expression, and the operation type of the first variable in the second expression is determined by the mathematical calculation rules indicated by the semantics of the second expression.

[0022] Optionally, the mathematical calculation rule represents the order of operations included in the expression and the operation type of the operations. For example, the operation type may be conditional judgment, addition, subtraction, multiplication or division.

[0023] In a feasible implementation, when the first expression or the second expression is a conditional statement or a loop statement, and the first variable is located in the conditional selection expression of the conditional statement or the loop control expression of the loop statement, the operation type of the first variable is non-differentiation.

[0024] It can be seen that in the embodiment of the present application, after the operation type of the first variable is determined by the mathematical calculation rules contained in the semantics of the expression, when the operation type of the first variable in the first expression is different from the operation type of the first variable in the second expression, the operation type of the first variable is updated, and then when the expression is processed based on the updated first variable operation, the derivative function expression corresponding to the expression is simplified, thereby simplifying the compilation result of the program.

[0025] In a second aspect, the present application provides a compilation device, which includes: an acquisition unit, configured to acquire a first expression and a second expression of a program, wherein both the first expression and the second expression include a first variable, the first variable being an input variable in the first expression, and the first variable being an output variable in the second expression, and the operation type of the first variable in the first expression is different from the operation type of the first variable in the second expression; an updating unit, configured to update the operation type of the first variable in the first expression and / or the operation type of the first variable in the second expression, so that the operation type of the updated first variable is consistent in the first expression and the second expression, and the operation type of the updated first variable indicates that the first variable has no differential result or the result is zero after encountering a differential operation; a processing unit, configured to process the first expression and the second expression according to the updated operation type of the first variable to obtain a derivative function expression corresponding to the first expression and a derivative function expression corresponding to the second expression; a combining unit, configured to combine the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression based on the chain rule to obtain a compilation result of the program, wherein the compilation result of the program is related to the combination result of the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression.

[0026] In a feasible implementation manner, when the operation type of the updated first variable is differentiation, the result of the first variable encountering the differentiation operation is zero.

[0027] In a feasible implementation manner, when the operation type of the updated first variable is non-differentiation, the first variable has no differentiation result after encountering a differentiation operation.

[0028] In a feasible embodiment, when a user requests to output the derivative of the first output variable of the program to the first input variable of the program, and the derivative is obtained based on the combination of differential results of the first variable, a warning message is generated; wherein the warning message is used to indicate the position of the first expression and the second expression in the program, as well as the first variable.

[0029] In a feasible implementation, the operation type of the first variable in the first expression is determined by the mathematical calculation rules indicated by the semantics of the first expression, and the operation type of the first variable in the second expression is determined by the mathematical calculation rules indicated by the semantics of the second expression.

[0030] In a feasible implementation, when the first expression or the second expression is a conditional statement or a loop statement, and the first variable is located in the conditional selection expression of the conditional statement or the loop control expression of the loop statement, the operation type of the first variable is non-differentiation.

[0031] In a third aspect, the present application provides a chip system, which includes at least one processor, a memory and an interface circuit, wherein the memory, the interface circuit and at least one processor are interconnected through lines, and instructions are stored in at least one memory; when the instruction is executed by the processor, the method described in any one of the above first aspects is implemented.

[0032] In a fourth aspect, the present application provides a compilation device, which includes the chip system as described in the third aspect above, and a discrete device coupled to the chip system.

[0033] In a fifth aspect, the present application provides a computer-readable storage medium storing program code for execution by a device, wherein the program code includes code for executing any one of the methods described in the first aspect.

[0034] In a sixth aspect, the present application provides a computer program product, which includes program instructions. When the program instructions are run on a computer, the method described in any one of the first aspects is implemented. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] The following is an introduction to the drawings used in the embodiments of this application.

[0036] Figure 1 It is a schematic diagram of the derivation process corresponding to a mathematical expression and a program expression in the prior art;

[0037] Figure 2 This is a schematic diagram of a system architecture in an embodiment of the present application;

[0038] Figure 3 This is a schematic diagram of an application scenario in an embodiment of the present application;

[0039] Figure 4 is a schematic flow chart of a program compilation method in an embodiment of the present application;

[0040] Figure 5 This is a schematic diagram of the execution process of a program compilation method in an embodiment of the present application;

[0041] Figure 6 This is a schematic diagram of the structure of a compiling device in an embodiment of the present application;

[0042] Figure 7 It is a schematic diagram of the hardware structure of a compiling device in an embodiment of the present application. DETAILED DESCRIPTION

[0043] The embodiments of the present application are described below in conjunction with the drawings in the embodiments of the present application.

[0044] It should be understood that the technical solutions of the embodiments of the present application can be applied to various computer systems, such as personal computers (PCs), computer cluster systems, large computer systems, or various supercomputers, etc., and the present application does not limit this. In addition, the present application can also be applied to various compilers, such as the GNU Compiler Collection (GCC) and the Low Level Virtual Machine (LLVM), etc., and the embodiments of the present application do not limit this.

[0045] The following is an introduction to the relevant terms in this application:

[0046] (1) Chain rule: A rule for finding the derivative of a composite function. For example, let f and g be two differentiable functions with respect to x. Then the derivative of the composite function f(g(x)) is f'(g(x))*g'(x).

[0047] (2) Expression: An independent element in a programming language that represents some actions to be performed by the program.

[0048] (3) Recursion: In mathematics, the use of a function's own operations in its definition.

[0049] (4) Loop: An expression that appears only once in a program but may be executed multiple times in succession.

[0050] (5) Compilation: The process of converting one source code into another.

[0051] First, we will introduce the mathematical function derivation and automatic differentiation (AD) technology involved in this application. In the embodiments of this application, the letter d is used to represent the differentiation of variables. For example, the differential of variable x can be expressed as dx, and the derivative of variable A to variable E can be expressed as dA / dE.

[0052] In mathematics, the process of finding the derivative of a given function at a certain point, or its derivative function, is called differentiation. For example, the derivative of the function f(x) at the independent variable x = x0 can be written as f'(x0). Therefore, for a differentiable function f(x), x → f'(x) is also a function, which is called the derivative function of f(x). The basic process of mathematical function differentiation is as follows:

[0053] 1) Given a mathematical expression of a function, it is continuously decomposed into a combination of sub-mathematical operations (such as addition, subtraction, multiplication, division, etc.) with well-defined derivation rules.

[0054] 2) Derivative each sub-mathematical operation according to the basic mathematical derivation rules.

[0055] 3) Combine the derivative results of the sub-mathematical operations according to the chain rule to obtain the derivative function of the function.

[0056] Automatic differentiation (AD) is a method for calculating function derivatives by automatically performing the above process using a computer program. However, the mathematical representation of the function to be differentiated cannot be understood or read by a computer. Therefore, the user must first define the function using a programming language to obtain a program representation that can be understood by the computer. It is important to note that there is a difference between the mathematical representation of a function and the program representation in a programming language.

[0057] See Figure 1 , Figure 1 It is a schematic diagram of the derivation process corresponding to a mathematical expression and a program expression in the prior art.

[0058] The following will be based on Figure 1 The examples in the following are used to describe the process of differentiating mathematical expressions and program expressions:

[0059] exist Figure 1In the example, program expression 1 is the programming language expression corresponding to mathematical expression 1. In mathematical expression 1, recursion is implicitly present in the expression, so no special processing is required during derivation. However, when applied to the program expression, the recursion is mapped to the parameter n that is explicitly present in program expression 1, and the loop expression "for" that implements the recursion. In the process of deriving program expression 1, what actually needs to be derived is the multiplication operation within the loop expression. The parameter n and the control part "for(i in 0 to n-1)" in the loop expression used to determine whether the loop continues should be ignored and not derived. Otherwise, the compilation system will produce an incorrect derivation result.

[0060] In program expression 1, the main function is f, which contains two input variables x and n, and the return value of the main function f is res.

[0061] In the derivative function expression corresponding to program expression 1, the main function is f_grad, which takes two input variables, x and n. The derivative of the expression res = x with respect to x is res_grad = 1. The control portion of the loop expression, for(i in 0 to n-1), is ignored and no derivative is calculated. The derivative of the output of the main function f_grad with respect to the input variable x is res_grad = res_grad * x + res. The character grad indicates the derivative. The return value of the main function f_grad is res_grad.

[0062] Because prior art compilers only decompose the current expression when compiling (deriving) a program, for example, when deriving program expression 1, it is first decomposed into two expressions: res = x and the loop expression, and then these two expressions are differentiated separately. Therefore, the derivative function expression res_grad corresponding to the main function contains a portion that can be simplified (i.e., the res_grad portion in res_grad*x, whose value is 1).

[0063] In summary, the existing technology only processes the decomposed single expression during the program compilation process, and then combines the derivative results of each expression based on the chain derivation rule. The resulting derivative function expression contains redundant code, resulting in poor running performance of the compiled program.

[0064] The system architecture and application scenarios of the embodiments of the present application are introduced below.

[0065] See Figure 2 , Figure 2This is a schematic diagram of a system architecture in an embodiment of the present application. As shown in system architecture 200, data acquisition device 260 is used to collect data. In the embodiment of the present application, the data may include image data in the field of artificial intelligence, speech semantic data in natural language processing, or data used to calculate the differential of source program 250 in other fields (such as scientific computing or physical modeling), but this application is not limited to this.

[0066] After collecting the data, the data collection device 260 stores the data in the database 230 .

[0067] Compiler 220 compiles source program 250 (i.e., the program in the embodiment of the present application) to obtain a derivative program 201 (i.e., the compilation result of the program in the embodiment of the present application). The data used by the derivative program is maintained based on database 230. Compiler 220 can be any device that includes a compiler (such as a compiler such as GCC or LLVM).

[0068] The following will be based on Figure 4 The illustrated embodiment describes in detail the process of compiling the source program 250 by the compiling device 220 .

[0069] The differential program 201 compiled by the compiling device 220 can be applied to different systems or devices, such as Figure 2 The execution device 210 shown in the figure can be a terminal, such as a mobile phone terminal, a tablet computer, a laptop computer, an augmented reality (AR) / virtual reality (VR), a vehicle terminal, etc. It can also be a server or a cloud. Figure 2 In the embodiment, the execution device 210 is configured with an input / output (I / O) interface 212 for data exchange with external devices.

[0070] The execution device 210 can receive data from the database 230 or input from the client device 240, and use the calculation module 211 to execute the relevant calculation process in the differential program to process the input data and obtain the processing results corresponding to the input data.

[0071] Finally, the I / O interface 212 returns the processing result (for example, the category label of the image in image recognition, the target identified from the image in target detection scenario, etc.) to the client device 240 to provide it to the user.

[0072] It is worth noting that the compilation device 220 can compile source programs for achieving different goals or different tasks to obtain corresponding differential programs 201, and then use the differential programs to process the data used in different goals or different tasks, thereby providing the user with the required processing results.

[0073] exist Figure 2 In the case shown in , the user can manually give input data, and the manual giving can be operated through the interface provided by the I / O interface 212. In another case, the client device 240 can automatically send input data to the I / O interface 212. If the client device 240 is required to automatically send input data and requires the user's authorization, the user can set the corresponding permissions in the client device 240. The user can view the results output by the execution device 210 on the client device 240, and the specific presentation form can be a specific method such as display, sound, action, etc. The client device 240 can also serve as a data acquisition terminal, collecting the input data input to the I / O interface 212 and the output results of the output I / O interface 212 as shown in the figure as new sample data, and storing them in the database 230.

[0074] It is worth noting that Figure 2 This is only a schematic diagram of a system architecture provided by an embodiment of the present invention. The positional relationship between the devices, components, modules, etc. shown in the figure does not constitute any limitation. For example, Figure 2 In the embodiment, the compilation device 220 is an external device relative to the execution device 210. In other cases, the compilation device 220 can also be placed in the execution device 210. The execution device 210 is an external device relative to the client device 240. In other cases, the execution device 210 and the client device 240 can be the same device.

[0075] See Figure 3 , Figure 3 It should be understood that the compilation method in the embodiment of the present application can be applied to scenarios requiring differential calculations in fields including artificial intelligence, scientific computing, and physical modeling. Figure 3 Taking the image processing scenario based on deep learning in the field of artificial intelligence (for example, image recognition, target detection, and image segmentation, etc.) as an example, the application process of the compilation method in the embodiment of the present application is described.

[0076] First, the user designs a network model source program 320 based on deep learning based on a specific image processing scenario (corresponding to Figure 2 The source program 250 in the embodiment of the present application is then used to train the network model to obtain a trained network model. Specifically:

[0077] The network model source program is compiled by executing the compilation method in the embodiment of the present application through the compilation device 220 to obtain the network model differentiation program 340 (corresponding to Figure 2 The network model is then trained using a training device 350 to execute the corresponding computational process in the network model differentiation program 340 on the image samples in the training set, obtaining a gradient result 360 for a single training process. The network model is then updated using the gradient result 360. The network model is trained multiple times until the model converges, resulting in a trained network model.

[0078] After obtaining the trained network model, the trained network model is run using the execution device 210 to process the image to be processed 310 to obtain the corresponding image processing result 330 (for example, in image recognition, the processing result is the category label of the image, in target detection, the processing result is the target identified from the image, and in image segmentation, the processing result is the segmentation result of the image).

[0079] It should be understood that Figure 3 In the embodiment, the compiling device 220 is an external device relative to the executing device 210 and the training device 350. In other cases, the compiling device 220 can also be placed in the executing device 210, and the compiling device 220 can also be placed in the training device 350.

[0080] See Figure 4 , Figure 4 4 is a schematic flow chart of a program compiling method 400 in an embodiment of the present application. The method 400 includes steps S410, S420, S430 and S440.

[0081] Step S410: Obtain a first expression and a second expression of a program, where both the first expression and the second expression include a first variable, the first variable is an input variable in the first expression, and the first variable is an output variable in the second expression, and an operation type of the first variable in the first expression is different from an operation type of the first variable in the second expression.

[0082] Optionally, expressions in a program can be divided into basic mathematical expressions, control flow expressions, and function expressions.

[0083] Among them, basic mathematical expressions can be basic operation expressions such as addition, subtraction, multiplication, and division, or expressions obtained by combining at least two of the basic operation expressions. Control flow expressions are used to control whether a program executes a certain operation. For example, control expressions can be conditional branch expressions or loop expressions. Function expressions can be library functions defined in various programming languages ​​or user-defined functions, and can be ordinary functions or anonymous functions. It should be understood that those skilled in the art can use other methods to divide expressions in programs, and this application does not limit this.

[0084] Specifically, the first expression and the second expression may be the basic mathematical expression, the control flow expression or the function expression. The first expression and the second expression include at least one input variable and / or at least one output variable.

[0085] Optionally, the program may further include multiple groups of expressions, wherein the relationship between the two expressions in each group of expressions is the same as the relationship between the first expression and the second expression described above. The relationship is: in each group of expressions, one expression and the other expression contain the same variable, which is an input variable in one expression and an output variable in the other expression. In the embodiments of the present application, this relationship is referred to as a dependency relationship, i.e., the first expression and the second expression have a dependency relationship or the first expression and the second expression have a dependency relationship based on the first variable.

[0086] Optionally, the first expression and the second expression may be any of multiple groups of dependent expressions in a program. The program compilation result is obtained by combining the derivative expressions corresponding to each expression in the program. In this case, the program compilation result is related to the combination of the derivative expression corresponding to the first expression and the derivative expression corresponding to the second expression. The processing process for each group of dependent expressions is the same as the processing process for the first expression and the second expression.

[0087] It should be understood that when the first variable is an output variable in the first expression and the first variable is an input variable in the second expression, the compilation process of the program is the same as the compilation process of the program in the above embodiment.

[0088] Step S420: Update the operation type of the first variable in the first expression and / or the operation type of the first variable in the second expression, so that the updated operation type of the first variable is consistent in the first expression and the second expression, and the updated operation type of the first variable indicates that the first variable has no differential result or the result is zero after encountering a differential operation.

[0089] Optionally, the compiler / compilation system can use any character, string or other feasible symbol to represent the operation type of the first variable. Furthermore, when the operation type of the first variable in the first expression conflicts with the operation type of the first variable in the second expression (i.e., they are not the same), the operation type of the first variable in the first expression can be represented by the first character, and the operation type of the first variable in the second expression can be represented by the second character. The operation type of the updated first variable can be represented by the third character, i.e., the first character and the second character need to be updated at the same time at this time; when the operation type of the updated first variable is represented by the first character or the second character, the first character or the second character is updated. The updated first character, the updated second character or the updated third character indicates that the first variable has no differential result or the result is zero after encountering the differential operation.

[0090] Optionally, when the first variable has no differential result after encountering a differential operation, the differential result may be represented by an identifier, for example, a certain character, which may be a null character or other characters.

[0091] The following describes in detail four scenarios in which the operation type of the first variable in the first expression conflicts with the operation type of the first variable in the second expression, as shown in Table 1:

[0092] Conflict 1: The first variable is an input variable in the first expression, and its operation type is differential; the first variable is an output variable in the second expression, and its operation type is non-differential.

[0093] Conflict 2: The first variable is the input variable in the first expression, and its operation type is non-differential; the first variable is the output variable in the second expression, and its operation type is differential.

[0094] Conflict three: The first variable is the output variable in the first expression, and its operation type is differential; the first variable is the input variable in the second expression, and its operation type is non-differential.

[0095] Conflict 4: The first variable is the output variable in the first expression, and its operation type is non-differential; the first variable is the input variable in the second expression, and its operation type is differential.

[0096] In a feasible implementation manner, when the operation type of the updated first variable is differentiation, the result of the first variable encountering the differentiation operation is zero.

[0097] Optionally, the operation type of the first variable is differentiation or non-differentiation. Differentiation means that the compiler / compilation system running this method performs a differentiation operation on the first variable; non-differentiation means that the compiler / compilation system running this method does not perform a differentiation operation on the first variable.

[0098] It can be seen that in the embodiment of the present application, after combining the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression based on the chain rule, the differential result of the first variable in the compiled result of the program is zero. Therefore, when processing the first expression and the second expression, the differential result of the first variable in the first expression and the second expression is omitted, thereby simplifying the derivative function expressions corresponding to the first expression and the second expression, thereby simplifying the compiled result of the program and effectively improving the running performance of the compiled result of the program.

[0099]

[0100] Table 1. Conflicts in the operation types of the first variable in the first expression and the second expression with dependencies

[0101] In a feasible implementation manner, when the operation type of the updated first variable is non-differentiation, the first variable has no differentiation result after encountering a differentiation operation.

[0102] Specifically, when the first variable has no differential result after encountering a differential operation, the compiled result of the program does not include an item corresponding to the first variable.

[0103] The following will take the first variable as an example to describe in detail how to update the operation type of the first variable.

[0104] Specifically, for a first expression and a second expression having a dependency relationship, when the operation type of the first variable in the first expression conflicts with the operation type of the first variable in the second expression, two methods can be used to update the operation type of the first variable, as shown in Table 1:

[0105] Method 1: Update the operation type of the first variable to non-differentiable. In this case, if the operation type of the first variable in the first expression is differential, the derivative of the first variable in the derivative of the first expression will be zero when the first expression is subsequently processed, that is, when the first variable is differentiated according to the updated operation type of the first variable. If the operation type of the first variable in the second expression is differential, the derivative of the first variable in the derivative of the second expression will be zero when the first expression is subsequently processed, that is, when the first variable is differentiated according to the updated operation type of the first variable.

[0106] Method 2: Update the operation type of the first variable to differential. In this case, if the operation type of the first variable in the first expression is non-differentiable, then when the first expression is subsequently processed, that is, when the first variable is differentiated according to the updated operation type of the first variable, the derivative of the first expression will not have any differential result for the first variable. If the initial operation type of the first variable in the second expression is non-differentiable, then when the first expression is subsequently processed, that is, when the first variable is differentiated according to the updated operation type of the first variable, the derivative of the second expression will not have any differential result for the first variable.

[0107] It can be seen that in the embodiment of the present application, after combining the derivative expression corresponding to the first expression and the derivative expression corresponding to the second expression based on the chain rule, the first variable in the compiled result of the program does not have a differential result. Therefore, when processing the first expression and the second expression, no differential operation is performed on the first variable in the first expression and the second expression. In other words, the derivative expressions corresponding to the first expression and the second expression can be simplified, thereby simplifying the compiled result of the program and effectively improving the running performance of the compiled result of the program.

[0108] In a feasible embodiment, when a user requests to output the derivative of the first output variable of the program to the first input variable of the program, and the derivative is obtained based on the combination of differential results of the first variable, a warning message is generated; wherein the warning message is used to indicate the position of the first expression and the second expression in the program, as well as the first variable.

[0109] Optionally, the above program may include at least one input variable and / or at least one output variable. The at least one input variable includes a first input variable, and the at least one output variable includes a first output variable. During the compilation process, the derivative of each output variable to each input variable will be generated, that is, when the output variables of the program are A and B, and the input variables are E and F, the derivatives of each output variable to each input variable will be generated during the compilation process: dA / dE, dA / dF, dB / dE, and dB / dF. The user can choose which derivative results to retain according to the specific scenario. For example, if dA / dE, dA / dF, and dB / dE are retained, then the derivative function expression corresponding to the first program is res_grad=dA / dE+dA / dF+dB / dE; where res_grad is the output of the first program.

[0110] Specifically, when obtaining the derivative dA / dF of the first output variable A of the program to the first input variable F, it is necessary to combine the differential results of the first variable in the first expression and the second expression. When the operation type of the first variable in the first expression is different from the operation type of the first variable in the second expression, and the operation type of the updated first variable is non-differential, it can be seen from the above embodiment that there is no differential result for the first variable. Therefore, after combining the differential result of the first variable in the first expression and the differential result of the first variable in the second expression based on the chain rule, there is no dA / dF. At this time, the compilation result of the program conflicts with the user's requirements, and the compiler / compilation system generates a warning message. The warning message indicates the location of the first expression and the second expression in the program, and indicates that the operation type of the first variable in the first expression conflicts (i.e., is different) with the operation type of the first variable in the second expression, so that the user can make corresponding modifications to the first expression and the second expression based on this warning message.

[0111] As can be seen in the embodiment of the present application, when the first variable encounters a differential operation and no differential result is obtained, and the derivative of the first output variable to the first input variable is obtained by combining the differential results of the first variable, since the derivative also has no result, it conflicts with the user's requirements. A warning message is generated to indicate the location of the first expression and the second expression in the program, as well as the first variable, so that the user can quickly locate the problematic code and make corresponding modifications.

[0112] In a feasible implementation, the operation type of the first variable in the above-mentioned first expression is determined by the mathematical calculation rules indicated by the semantics of the first expression, and the operation type of the first variable in the second expression is determined by the mathematical calculation rules indicated by the semantics of the second expression.

[0113] Specifically, the semantics of the first expression are identified based on the syntax of the first expression; wherein the syntax of the first expression is a predefined syntax or a syntax in an existing programming language; the operation type of the first variable in the first expression is determined based on the mathematical calculation rules contained in the semantics of the first expression; the semantics of the second expression are identified based on the syntax of the second expression; wherein the syntax of the second expression is a predefined syntax or a syntax in an existing programming language; the operation type of the first variable in the second expression is determined based on the mathematical calculation rules contained in the semantics of the second expression.

[0114] The above-mentioned syntax is the expression form of program expressions in programming languages, that is, the format and specifications of expressions; the above-mentioned syntax can be the syntax of existing programming languages, such as the syntax in languages ​​such as Python, C, C++, or user-defined syntax, for example, the user can define the syntax x@y. The above-mentioned semantics refers to the meaning of the expression in the programming language, that is, what specific operation the expression performs. For example, the compiler / compilation system can recognize that the semantics corresponding to the above-mentioned syntax x@y is x+y*x+y. Different semantics correspond to different mathematical calculation rules, and the operation types of the input variables and output variables contained in the expression are obtained through the mathematical calculation rules contained in the semantics.

[0115] Optionally, the mathematical calculation rule represents the order of operations included in the expression and the operation type of the operations. For example, the operation type may be conditional judgment, addition, subtraction, multiplication or division.

[0116] The following two examples describe the process of obtaining the operation types of output variables and input variables in an expression.

[0117] Example 1: For the expression z = (+, x, y), the syntax of this expression is its representation. The compiler / compilation system recognizes the corresponding semantics through the above syntax. The semantics of this expression can be to add the input variables x and the input variables y to obtain the output variable z, that is, z = x + y. Through the mathematical calculation rules contained in the semantics corresponding to this expression, the operation type of the input variables and output variables is determined to be differentiation.

[0118] Example 2: For the expression z = if(m){x}else{y}, the syntax of this expression is its expression form if(){}else{}. The compiler / compilation system recognizes the corresponding semantics through the above syntax: it determines which content in the curly braces {} to execute through the condition in the round brackets (); and determines the operation type of the input variables and output variables through the mathematical calculation rules contained in the semantics corresponding to the expression, that is, the input variable m is not differentiated, the input variables x and input variables y are differentiated, and the output variable z is differentiated.

[0119] In a feasible implementation, when the first expression or the second expression is a conditional statement or a loop statement, and the first variable is located in the conditional selection expression of the conditional statement or the loop control expression of the loop statement, the operation type of the first variable is non-differentiation.

[0120] Specifically, the operation type of the first variable, ie, non-differentiation, is obtained through the mathematical calculation rules contained in the semantics of the conditional statement or the loop statement.

[0121] It can be seen that in the embodiment of the present application, after the operation type of the first variable is determined by the mathematical calculation rules contained in the semantics of the expression, when the operation type of the first variable in the first expression is different from the operation type of the first variable in the second expression, the operation type of the first variable is updated, and then the expression is processed based on the updated first variable operation, the derivative function expression corresponding to the expression and the compilation result of the program are simplified.

[0122] Step S430: Process the first expression and the second expression according to the updated operation type of the first variable to obtain a derivative function expression corresponding to the first expression and a derivative function expression corresponding to the second expression.

[0123] Specifically, the operation types of all input variables and output variables in the first expression are updated according to the above rules, and then each input variable and output variable is differentiated according to the updated operation type of each variable to obtain the derivative function expression corresponding to the first expression; similarly, according to the generation process of the derivative function expression corresponding to the first expression, the derivative function expression corresponding to the second expression is obtained.

[0124] The following describes in detail how to update the operation type of each input variable and output variable in the first expression.

[0125] In the first expression, in addition to the first variable, the first expression may also include other input variables and output variables, the second variable is any variable among the other output variables and the output variables, and the third expression is an expression that has a dependency relationship with the first expression based on the second variable.

[0126] Furthermore, when the operation type of the second variable in the first expression is the same as the operation type of the second variable in the third expression, the operation type of the second variable is not updated; when the operation type of the second variable in the first expression is different from the operation type of the second variable in the third expression, the operation type of the second variable is updated using the same update method as the operation type of the first variable, and when processing the first expression, the second variable is differentiated according to the updated operation type of the second variable.

[0127] It should be understood that the first expression and the second expression described above are based on the first variable to generate a dependency relationship. The first expression and the second expression can also simultaneously generate a dependency relationship based on other variables, and this application does not limit this. When the first expression and the second expression simultaneously generate a dependency relationship based on multiple variables, and the operation type of each variable in the first expression is different from the operation type in the second expression, the operation type of each variable after the update is the same as the operation type of the first variable after the update.

[0128] Similarly, the updating process of the variable operation types contained in other expressions in the program is the same as the updating method of the variable operation types in the first expression, which will not be repeated here.

[0129] In summary, after the operation types of all input variables and output variables in the first expression are updated according to the above rules, the corresponding processing (derivation) rules for different types of first expressions can be as follows: the semantic information of the first expression is identified according to the syntax of the first expression, and the first expression is processed according to the mathematical calculation rules contained in the semantics and the updated operation types of each variable (input variable and output variable). Specifically:

[0130] (1) The first expression is a basic mathematical expression

[0131] According to the operation type of each variable after the update in the first expression, the derivative is performed according to the standard mathematical differentiation rules to obtain the derivative function expression corresponding to the first expression.

[0132] (2) The first expression is a control flow expression

[0133] i. When the control flow expression is a conditional branch expression, ignore the sub-expressions used for conditional judgment in the expression when performing derivation, and only traverse the sub-expressions in each conditional branch in the expression and perform derivation; then, based on the chain rule, combine the derivation results of the sub-expressions to obtain the derivative function expression corresponding to the first expression.

[0134] ii. When the control flow expression is a loop expression, ignore the sub-expressions used for loop condition determination and update in the expression, and only traverse the sub-expressions in the loop body and perform differentiation; then, based on the chain rule, combine the derivative results of each sub-expression to obtain the derivative function expression corresponding to the first expression.

[0135] iii. When a control flow expression contains a conditional branch expression and a loop expression, the conditional branch expression and the loop expression can be differentiated separately during derivation. Finally, the derivative results are combined based on the chain rule to obtain the derivative function expression corresponding to the first expression.

[0136] (3) The first expression is a function expression

[0137] Traverse the sub-expressions in the function body, derive them according to the operation type of each variable after the update in each sub-expression, and then combine the derivative results of the sub-expressions based on the chain rule to obtain the derivative function expression corresponding to the first expression.

[0138] For example, when the first expression contains an output variable a and input variables b, c, and e; after updating the operation type of each input variable and output variable according to the above steps, the derivatives of the output variable to each input variable are respectively calculated: da / db, da / dc, da / de; at this time, the derivative result of the first expression is: res_grad = da / db + da / dc + da / de; finally, the derivative result of the first expression is combined with the expression in the first expression that has not been differentiated to obtain the derivative function expression corresponding to the first expression.

[0139] Step S440: Based on the chain rule, the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression are combined to obtain the compilation result of the program. The compilation result of the program is related to the combination result of the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression.

[0140] Optionally, the first expression and the second expression may be any of multiple groups of dependent expressions in a program. The program compilation result is obtained by combining the derivative expressions corresponding to each expression in the program. In this case, the program compilation result is related to the combination of the derivative expression corresponding to the first expression and the derivative expression corresponding to the second expression. The processing process for each group of dependent expressions is the same as the processing process for the first expression and the second expression.

[0141] Specifically, for any set of two expressions with a dependency relationship, the operation types of the input and output variables of any of the two expressions can be updated according to the update method of the operation types of the input and output variables of the first expression. Then, the expression is differentiated based on the updated operation types to obtain the derivative function expression corresponding to the expression. Then, the derivative function expression corresponding to each expression in the program is obtained based on the method in the above embodiment, and the derivative function expressions corresponding to each expression are combined based on the chain rule to obtain the compilation result of the program.

[0142] In summary, in the above program compilation process, first, based on the semantics of each expression, the operation types of the input variables and output variables contained in each expression are obtained, then the operation types of the variables in all two expressions with dependency relationships in the program are updated, and finally, based on the updated operation types of the variables, each expression is processed separately to obtain the derivative function expression corresponding to each expression, and the derivative function expressions corresponding to each expression are combined based on the chain rule to obtain the compilation result of the program.

[0143] It can be seen that in the embodiment of the present application, since the first variable is the input variable in the first expression and the first variable is the output variable in the second expression, during the compilation of the program, the differential result of the first variable in the first expression and the differential result of the first variable in the second expression will be combined based on the chain rule. At this time, when the first variable has no differential result or the differential result is zero in one expression, for the compilation process of the entire program, it is not necessary to perform a differential operation on the first variable in the other expression. The embodiment of the present application can check the operation type of the first variable in the first expression and the second expression in advance. When the operation type of the first variable in the first expression and the operation type of the first variable in the second expression are different, the operation type of the first variable in the first expression and / or the operation type of the first variable in the second expression are updated. Since the operation type of the updated first variable indicates that the first variable has no differential result or the result is zero after encountering the differential operation, when the first expression and the second expression are processed separately based on the operation type of the updated first variable, the first variable will not be differentiated, that is, the differential result of the first variable in the first expression and the second expression is omitted, thereby simplifying the derivative function expressions corresponding to the first expression and the second expression respectively. Then, based on the processing of the first expression and the second expression, other groups of expressions with dependencies in the program are processed to simplify the derivative expressions corresponding to other expressions; and after combining the derivative expressions corresponding to each expression in the program based on the chain rule, the compiled result of the program does not contain redundant code, which can effectively improve the running performance of the program compilation result.

[0144] See Figure 5 , Figure 5 A schematic diagram of the execution process of a program compilation method provided in an embodiment of the present application, as a reference to Figure 4 Summary of the embodiments. Figure 5 As shown, the process 500 includes steps S510, S520, and S530, which are respectively as follows:

[0145] Step S510: Obtain N groups of expressions with dependency relationships from the program; wherein each group of expressions includes two first expressions with dependency relationships.

[0146] The detailed description of the dependency relationship can be found in the aforementioned embodiments and will not be repeated here.

[0147] Step S520: global operation type conflict check, update or not update the operation type of input variables and output variables in each expression.

[0148] Specifically, for each group of two expressions having a dependency relationship, the two expressions generate a dependency relationship based on a third variable, and a conflict check is performed on the operation types of the third variable in the two expressions.

[0149] The method for obtaining the initial operation types of the input variables and output variables in each expression can be found in the description of the aforementioned embodiment, which will not be repeated here.

[0150] If there is no conflict, step S521 is executed: check passed. That is, the third variable passes the type conflict check, and in the subsequent expression processing process, the expression is derived according to the operation type of the third variable.

[0151] If there is a conflict, then step S522 is executed to update the operation type of the third variable. For the specific update process, please refer to Figure 4 The corresponding processes in the embodiments will not be described in detail here.

[0152] Step S530: Differentiate each expression based on the type of operation used to differentiate the input and output variables in each expression to obtain the derivative function expression corresponding to each expression. Based on the chain rule, the derivative function expressions corresponding to each expression are combined to obtain the compiled result of the program.

[0153] Specifically, this process can be found in Figure 4 The relevant description in the embodiments will not be repeated here.

[0154] The following two examples will be used to describe the compilation process of this application in detail to illustrate the difference between the compilation method of this application and the existing compilation method:

[0155] Procedure 1:

[0156]

[0157] In the process of compiling Program 1 using existing compilation technology, Program 1 can be divided into four expressions: Expression ①, Expression ②, Expression ③, and Expression ④. Since the existing technology only performs differentiation on each expression obtained by each division, no global operation type conflict check is performed. The mathematical operation rules contained in the semantics of Expression ② and Expression ③ require ignoring the differentiation of Expression ② and Expression ③, that is, the initial operation types of the input variables x, input variables y, and output variables trueFunc and falseFunc in Expression ② and Expression ③ are non-differential. However, for Expression ④, the output variable of Expression ④ is also the output variable of Program 1. The user requires the output of the derivative of Program 1. That is, the operation type of the input variables trueFunc and falseFunc in Expression ④ is differential, which conflicts with the operation type of the output variables trueFunc and falseFunc in Expression ② and Expression ③, resulting in an erroneous compilation result of Program 1.

[0158] During the compilation process using the method in the embodiment of the present invention, similarly, the operation types of the input variables x and y, and the output variables trueFunc and falseFunc in expressions ② and ③ are non-differential, and the operation types of the input variables trueFunc and falseFunc in expression ④ are differential, so there is an operation type conflict. Since the compilation method in this application performs a global operation type conflict check before differentiation, after detecting the above conflict, a compilation warning is given to the user, indicating the specific locations of expressions ②, ③, and ④, as well as the variables that cause the operation type conflict (i.e., trueFunc and falseFunc), thereby helping the user to modify the problem code in advance.

[0159] Procedure 2:

[0160]

[0161] When compiling Program 2 using existing compilation technology, Program 2 can be divided into two expressions: Expression ⑤ and Expression ⑥. Expression ⑤ is t = x * 10, and Expression ⑥ is an if conditional expression. The operation type for the input and output variables in Expression ⑤ is differentiation. The mathematical operation rules contained in the semantic information of Expression ⑥ indicate that the derivation of the judgment statement (t == 100) in the if conditional expression is ignored, and the two branch expressions are differentiated. That is, the initial processing type of the input variable t in Expression ⑥ is non-differentiation, and the processing type of the output variables of the two branch expressions is differentiation. The derivation of Expression ⑤ yields the result: t_grad_x = 10. During the derivation of Expression ⑥, the derivation result of the input variable t is f_grad_t = 0, and the derivation results of the input variable x in the two branches are res_grad_x = 3 and res_grad_x = 2, respectively. Then, based on the chain derivation rule, the derivative result of expression ⑤ is combined with the derivative results of the sub-expressions in the two conditional branches to obtain the two derivative results of program 2: f_grad_x = res_grad_x + grad_t * t_grad_x and f_grad_x = res_grad_x + grad_t * t_grad_x; finally, the two derivative results of program 2 are combined with the underrived expressions in expressions ⑤ and ⑥ to obtain the compiled result of program 2, as shown below:

[0162] The derivative function expression obtained after compiling Program 2 using existing compilation technology is:

[0163]

[0164] During the compilation of Program 2 in the embodiment of the present application, Program 2 can also be divided into two expressions: Expression ⑤ and Expression ⑥. At this time, Expression ⑤ and Expression ⑥ have a dependency relationship based on the variable t. Therefore, using the compilation method in the embodiment of the present application, it will be checked in advance that the output variable t in Expression ⑤ and the input variable t in Expression ⑥ have a type conflict. If the operation type of the input variable t is updated to differential, it will be detected that the differential result of the input variable t in Expression ⑥ is zero. Therefore, the compiler will check that when the derivative results in Expression ⑤ and Expression ⑥ are combined, the differential result of the variable t can be simplified; at this time, the compiler will not perform differential operations on the variable t in Expression ⑤ and Expression ⑥ during the derivation process of Expression ⑤ and Expression ⑥. At this time, Expression ⑤ has no differential result, and the differential results of the two conditional branches in Expression ⑥ are: res_grad_x=3 and res_grad_x=2 respectively. Combine the derivative of expression ⑤ with the derivatives of the expressions in the two conditional branches to obtain the two derivative results of program 2: f_grad_x = res_grad_x and f_grad_x = res_grad_x. Finally, combine the two derivative results of program 2 with the underived expression to obtain the compiled result of program 2, as shown below:

[0165] The derivative function expression obtained after compiling Program 2 using the compilation technology of this application is:

[0166]

[0167]

[0168] It can be seen from the example of Program 2 that since the embodiment of the present application performs global type conflict judgment, the variables with operation type conflicts are updated to be non-differentiable, and it is determined that the expression "t=x*10" does not need to be differentiated, thereby simplifying the derivation result of Program 2 obtained based on the combination of chain derivation rules, that is, the compilation result of Program 2 is simplified, and there is no redundant code in the compilation result, which can effectively improve the running performance of the compiled program.

[0169] See Figure 6 , Figure 6 This is a schematic diagram of the structure of a compilation device 600 provided in an embodiment of the present application. The device 600 includes:

[0170] An acquisition unit 601 is used to acquire a first expression and a second expression of a program, where both the first expression and the second expression include a first variable, the first variable is an input variable in the first expression, and the first variable is an output variable in the second expression, and an operation type of the first variable in the first expression is different from an operation type of the first variable in the second expression.

[0171] An updating unit 602 is configured to update an operation type of the first variable in the first expression and / or an operation type of the first variable in the second expression, so that the updated operation type of the first variable is consistent in the first expression and the second expression, and the updated operation type of the first variable indicates that the first variable has no differential result or the result is zero after encountering a differential operation.

[0172] The processing unit 603 is used to process the first expression and the second expression according to the updated operation type of the first variable to obtain the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression.

[0173] The combination unit 604 is used to combine the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression based on the chain rule to obtain the compilation result of the program, and the compilation result of the program is related to the combination result of the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression.

[0174] It should be understood that the device 600 here is embodied in the form of a functional unit. The term "unit" here can refer to an application specific integrated circuit (ASIC), an electronic circuit, a processor (such as a shared processor, a dedicated processor or a group processor, etc.) and a memory for executing one or more software or firmware programs, a combined logic circuit and / or other suitable components that support the described functions. In an optional example, those skilled in the art will understand that the device 600 can be specifically the compiler in the above-mentioned embodiment 400, and the device 600 can be used for each process and / or step corresponding to the compiler that executes the above-mentioned method embodiment 400. To avoid repetition, it will not be repeated here.

[0175] In a feasible implementation manner, when the operation type of the updated first variable is differentiation, the result of the first variable encountering the differentiation operation is zero.

[0176] In a feasible implementation manner, when the operation type of the updated first variable is non-differentiation, the first variable has no differentiation result after encountering a differentiation operation.

[0177] Specifically, when the first variable has no differential result after encountering a differential operation, the compiled result of the program does not include an item corresponding to the first variable.

[0178] In a feasible embodiment, when a user requests to output the derivative of the first output variable of the program to the first input variable of the program, and the derivative is obtained based on the combination of differential results of the first variable, a warning message is generated; wherein the warning message is used to indicate the position of the first expression and the second expression in the program, as well as the first variable.

[0179] In a feasible implementation, the operation type of the first variable in the first expression is determined by the mathematical calculation rules indicated by the semantics of the first expression, and the operation type of the first variable in the second expression is determined by the mathematical calculation rules indicated by the semantics of the second expression.

[0180] In a feasible implementation, when the first expression or the second expression is a conditional statement or a loop statement, and the first variable is located in the conditional selection expression of the conditional statement or the loop control expression of the loop statement, the operation type of the first variable is non-differentiation.

[0181] See Figure 7 , Figure 7 This is a hardware structure diagram of a compilation device provided in an embodiment of the present application. Figure 7 As shown, the apparatus may include: a memory 701, one or more (only one is shown) processors 702, a communication interface 703, and a bus 704. The memory 701, processor 702, and communication interface 703 are communicatively connected to each other via the bus 704.

[0182] The memory 701 is used to store instructions, and the processor 702 is used to call the instructions stored in the memory 701; the instructions can be the programs in the aforementioned application embodiments.

[0183] The processor 702 is specifically used to obtain a first expression and a second expression of a program, where both the first expression and the second expression include a first variable, the first variable is an input variable in the first expression, and the first variable is an output variable in the second expression, and the operation type of the first variable in the first expression is different from the operation type of the first variable in the second expression; update the operation type of the first variable in the first expression and / or the operation type of the first variable in the second expression so that the updated operation type of the first variable is consistent in the first expression and the second expression, and the updated operation type of the first variable indicates that the first variable has no differential result or the result is zero after encountering a differential operation; process the first expression and the second expression according to the updated operation type of the first variable to obtain a derivative function expression corresponding to the first expression and a derivative function expression corresponding to the second expression; and combine the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression based on the chain rule to obtain a compilation result of the program, where the compilation result of the program is related to the combination result of the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression.

[0184] The compilation device of the embodiment of the present application can use the compilation method in the embodiment of the present application to simplify the derivative function expression corresponding to the program during the compilation stage, so that the compilation result of the program does not contain redundant code, thereby effectively improving the running performance of the compiled program.

[0185] It should be understood that the apparatus 700 may be specifically a computer, and may be used to execute the various steps and / or processes corresponding to the compiler in the above-mentioned method embodiments 400 and 500.

[0186] The memory 701 may be a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 701 may store programs. When the program stored in the memory 701 is executed by the processor 702, the processor 702 and the communication interface 703 are used to perform the various steps of the compilation method of the embodiment of the present application.

[0187] The processor 702 can be a general-purpose central processing unit (CPU), a microprocessor, an application-specific integrated circuit (ASIC), a graphics processing unit (GPU) or one or more integrated circuits, and is used to execute relevant programs to implement the functions required to be performed by the units in the compilation device of the embodiment of the present application, or to execute the compilation method of the method embodiment of the present application.

[0188] The processor 702 may also be an integrated circuit chip with signal processing capabilities. During implementation, the various steps of the compilation method of the present application may be completed by software instructions in the processor 702. The aforementioned processor 702 may also be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA), or other programmable logic device, discrete gate or transistor logic device, or discrete hardware component. The various methods, steps, and logic block diagrams disclosed in the embodiments of the present application may be implemented or executed. The general-purpose processor may be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of the present application may be directly implemented and executed by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software module may be located in a storage medium mature in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or the like. The storage medium is located in the memory 701, and the processor 702 reads the information in the memory 701 and combines its hardware to complete the functions required to be performed by the units included in the compilation device of the embodiment of the present application, or executes the compilation method of the method embodiment of the present application.

[0189] The communication interface 703 uses a transceiver device such as, but not limited to, a transceiver to implement communication between the apparatus 700 and other devices or a communication network. For example, a program can be obtained through the communication interface 703 .

[0190] The bus 704 may include a path for transmitting information between various components of the device 700 (eg, the memory 701 , the processor 702 , and the communication interface 703 ).

[0191] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0192] In the several embodiments provided in this application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0193] The units described above as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0194] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0195] If the above functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0196] The above description is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.

Claims

1. A program compiling method, characterized in that: The method comprises: Obtaining a first expression and a second expression of a program, wherein both the first expression and the second expression include a first variable, the first variable being an input variable in the first expression and the first variable being an output variable in the second expression, and an operation type of the first variable in the first expression is different from an operation type of the first variable in the second expression; the operation type of the first variable is differential or non-differential; the differential operation refers to performing a differential operation on the first variable; and the non-differential operation refers to not performing a differential operation on the first variable; updating an operation type of the first variable in the first expression and / or an operation type of the first variable in the second expression, so that the updated operation type of the first variable is consistent in the first expression and the second expression, and the updated operation type of the first variable indicates that the first variable has no differential result or the result is zero after encountering a differential operation; Processing the first expression and the second expression according to the updated operation type of the first variable to obtain a derivative function expression corresponding to the first expression and a derivative function expression corresponding to the second expression; omitting the differentiation results of the first variable in the first expression and the second expression; Based on the chain rule, the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression are combined to obtain the compilation result of the program. The compilation result of the program is related to the combination result of the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression.

2. The method according to claim 1, characterized in that When the operation type of the updated first variable is differentiation, the result of the first variable after encountering the differentiation operation is zero.

3. The method according to claim 1, characterized in that When the operation type of the updated first variable is non-differentiation, the first variable has no differentiation result after encountering a differentiation operation.

4. The method according to claim 3, characterized in that When a user requests to output the derivative of a first output variable of the program to a first input variable of the program, and the derivative is obtained based on a combination of differential results of the first variable, a warning message is generated; wherein the warning message is used to indicate the positions of the first expression and the second expression in the program, as well as the first variable.

5. The method according to any one of claims 1 to 4, characterized in that The operation type of the first variable in the first expression is determined by the mathematical calculation rules indicated by the semantics of the first expression, and the operation type of the first variable in the second expression is determined by the mathematical calculation rules indicated by the semantics of the second expression.

6. The method according to claim 5, characterized in that When the first expression or the second expression is a conditional statement or a loop statement, and the first variable is located in the conditional selection expression of the conditional statement or the loop control expression of the loop statement, the operation type of the first variable is non-differentiation.

7. The method according to claim 1, characterized in that When the first variable has no differential result after encountering a differential operation, the compiled result of the program does not include an item corresponding to the first variable.

8. A program compiling device, characterized in that: The device comprises: an acquisition unit, configured to acquire a first expression and a second expression of a program, wherein both the first expression and the second expression include a first variable, the first variable being an input variable in the first expression and an output variable in the second expression, and an operation type of the first variable in the first expression is different from an operation type of the first variable in the second expression; the operation type of the first variable is differential or non-differential; the differential operation refers to performing a differential operation on the first variable; and the non-differential operation refers to not performing a differential operation on the first variable; an updating unit, configured to update an operation type of the first variable in the first expression and / or an operation type of the first variable in the second expression, so that the updated operation type of the first variable is consistent in the first expression and the second expression, and the updated operation type of the first variable indicates that the first variable has no differential result or the result is zero after encountering a differential operation; a processing unit, configured to process the first expression and the second expression according to the updated operation type of the first variable to obtain a derivative function expression corresponding to the first expression and a derivative function expression corresponding to the second expression; and omit the differential results of the first variable in the first expression and the second expression; A combination unit is used to combine the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression based on the chain rule to obtain a compilation result of the program, and the compilation result of the program is related to the combination result of the derivative function expression corresponding to the first expression and the derivative function expression corresponding to the second expression.

9. The device according to claim 8, characterized in that When the operation type of the updated first variable is differentiation, the result of the first variable after encountering the differentiation operation is zero.

10. The device according to claim 8, characterized in that When the operation type of the updated first variable is non-differentiation, the first variable has no differentiation result after encountering a differentiation operation.

11. The device according to claim 10, characterized in that When a user requests to output the derivative of a first output variable of the program to a first input variable of the program, and the derivative is obtained based on a combination of differential results of the first variable, a warning message is generated; wherein the warning message is used to indicate the positions of the first expression and the second expression in the program, as well as the first variable.

12. The device according to any one of claims 8 to 11, characterized in that The operation type of the first variable in the first expression is determined by the mathematical calculation rules indicated by the semantics of the first expression, and the operation type of the first variable in the second expression is determined by the mathematical calculation rules indicated by the semantics of the second expression.

13. The device according to claim 12, characterized in that When the first expression or the second expression is a conditional statement or a loop statement, and the first variable is located in the conditional selection expression of the conditional statement or the loop control expression of the loop statement, the operation type of the first variable is non-differentiation.

14. The device according to claim 8, characterized in that When the first variable has no differential result after encountering a differential operation, the compiled result of the program does not include an item corresponding to the first variable.

15. A chip system, characterized in that: The chip system includes at least one processor, a memory and an interface circuit, the memory, the interface circuit and the at least one processor are interconnected by lines, and instructions are stored in the at least one memory; when the instructions are executed by the processor, the method described in any one of claims 1 to 7 is implemented.

16. A compiling device, characterized in that: The compiling device includes the chip system as claimed in claim 15 and a discrete device coupled to the chip system.

17. A computer-readable storage medium, characterized in that The computer-readable storage medium stores program codes for execution by a device, wherein the program codes include codes for executing the method according to any one of claims 1 to 7.

18. A computer program product, characterized in that The computer program product includes program instructions, and when the program instructions are run on a computer, the method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Dynamic reservoir simulation method based on reverse automatic differentiation

    CN103279685A

  • Method and system for variable tracking in program debugging

    CN105701006A

Cited By

  • Program compiling method and apparatus

    WO2023274092A1