Code generation method, device, equipment and storage medium

By converting the initial operation expression into the target arithmetic expression and generating the code, the problem of index operation code generation in tensor processing is solved, the storage space of the index operation result is reduced, and the operating efficiency is improved.

CN112947908BActive Publication Date: 2024-09-13SHANGHAI SENSETIME INTELLIGENT TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110221192.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-02-26
Publication Date
2024-09-13
Estimated Expiration
2041-02-26

AI Technical Summary

Technical Problem

The existing technology cannot automatically generate codes for index operations during tensor processing operations, especially processes including index operations.

Method used

By obtaining the initial operation expression, converting it into the target arithmetic expression, and generating code based on the expression of the index operation, the tensor is marked according to the index information of the index operation, thereby reducing the reading and writing operations of the index operation results.

Benefits of technology

Code generation for tensor operations including index operations is implemented, which saves storage space for index operation results and improves code running efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112947908B
    Figure CN112947908B
Patent Text Reader

Abstract

The present invention relates to a code generation method, device, equipment and storage medium, wherein the code generation method comprises: obtaining an initial operation expression, wherein the initial operation expression comprises an index operation of a first tensor; for an arithmetic operation on the result of the index operation in the initial operation expression, converting the initial operation expression into a target arithmetic expression by taking the expression of the index operation as input or output; generating code according to the target arithmetic expression, wherein the code of the expression of the index operation is obtained by annotating the first tensor according to the index information of the index operation. Therefore, code can be generated for tensor operations including index operations, and the read and write operations on the results of the index operations are reduced when the code is running, thereby saving storage space for the results of the index operations.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code generation, and in particular to a code generation method, device, equipment and storage medium. Background Art

[0002] With the development of artificial intelligence technology, objects such as images can be automatically processed in many aspects, reducing labor costs and improving efficiency and accuracy. The features of these objects are often represented by tensors, so the processing of features can be represented by the operation process of tensors. However, in related technologies, the operation process of tensor processing, especially the operation process including index operation, cannot automatically generate code. Summary of the invention

[0003] The present invention provides a code generation method, device, equipment and storage medium to solve the following technical problems in related technologies: it is impossible to automatically generate code for the operation process of tensor processing, especially the operation process including index operation.

[0004] According to a first aspect of an embodiment of the present invention, there is provided a code generation method, comprising:

[0005] Obtaining an initial operation expression, wherein the initial operation expression includes an index operation of the first tensor;

[0006] For the arithmetic operation on the result of the index operation in the initial operation expression, convert the initial operation expression into a target arithmetic expression by taking the expression of the index operation as input or output;

[0007] Generate code according to the target arithmetic expression, wherein the code of the expression of the index operation is obtained by marking the first tensor according to the index information of the index operation.

[0008] In combination with any embodiment provided by the present disclosure, the arithmetic operation on the result of the index operation in the initial operation expression, by taking the expression of the index operation as input or output, converting the initial operation expression into a target arithmetic expression, includes:

[0009] Generate a computation graph according to the initial computation expression;

[0010] According to the operation graph, the target arithmetic expression is generated.

[0011] In combination with any implementation manner provided in the present disclosure, generating an operation graph according to the initial operation expression includes:

[0012] In response to one of the input and the output of the arithmetic operation in the initial operation expression being a result of the index operation of the first tensor and the other being a second tensor, generating a first node representing the first tensor, a second node representing the second tensor, and an operation edge representing index information of the index operation and arithmetic information of the arithmetic operation;

[0013] In response to one of the input and output of the arithmetic operation in the initial operation expression being the first tensor and the other being the third tensor, a first node representing the first tensor, a third node representing the third tensor, and an arithmetic edge representing arithmetic information of the arithmetic operation are generated.

[0014] In combination with any embodiment provided in the present disclosure, it also includes:

[0015] In response to there being two operation edges between the first node and the second node, and the directions of the arithmetic information represented by the two operation edges are opposite, determining a fourth tensor according to the first tensor and index information represented by one operation edge;

[0016] The one operation edge is deleted, and a fourth node representing the fourth tensor, an arithmetic edge representing the arithmetic information represented by the one first edge, and a jump edge connecting the first node and the fourth node are generated.

[0017] In combination with any implementation manner provided in the present disclosure, generating a target arithmetic expression according to the operation graph includes:

[0018] A target arithmetic expression sub-formula is generated according to each operation edge of the operation graph and the nodes at both ends of the operation edge, and / or a target arithmetic expression sub-formula is generated according to each arithmetic edge of the operation graph and the nodes at both ends of the arithmetic edge.

[0019] In combination with any implementation manner provided in the present disclosure, the marking the first tensor according to the index information of the index operation includes:

[0020] Determine the dimension information, bias information and step information of the result of the index operation according to the index information, wherein the dimension information includes the dimension content of each dimension, the bias information represents the position of the first tensor element in the tensor, and the step information includes the step content of each dimension, and the step content represents the cycle period of the tensor element of the corresponding dimension;

[0021] The first tensor is labeled using the dimension information, the bias information, and the stride information.

[0022] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is an integer, determining the dimension information of the result of the index operation according to the index information includes:

[0023] The dimensional content of the first dimension is deleted from the original dimensional information to obtain the dimensional information of the result of the index operation, wherein the original dimensional information is the dimensional information of the tensor targeted by the index operation.

[0024] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is an integer, determining the bias information of the result of the index operation according to the index information includes:

[0025] The bias information of the result of the index operation is determined according to the original bias information, the integer and the step content of the first dimension in the original step information, wherein the original bias information is the bias information of the tensor targeted by the index operation, and the original step information is the step information of the tensor targeted by the index operation.

[0026] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is an integer, determining the step information of the result of the index operation according to the index information includes:

[0027] The step content of the first dimension is deleted from the original step information to obtain the step information of the result of the index operation, wherein the original step information is the step information of the tensor targeted by the index operation.

[0028] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is a slice, determining the dimension information of the result of the index operation according to the index information includes:

[0029] The dimensional content of the first dimension is determined according to the dimensional content of the first dimension in the original dimensional information, and the starting value, ending value and step value of the slice, and the dimensional content of other dimensions remains unchanged to obtain the dimensional information of the result of the index operation, wherein the original dimensional information is the dimensional information of the tensor targeted by the index operation.

[0030] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is a slice, determining the bias information of the result of the index operation according to the index information includes:

[0031] The bias information of the result of the index operation is determined according to the original bias information, the starting value of the slice and the step content of the first dimension in the original step information, wherein the original bias information is the bias information of the tensor targeted by the index operation, and the original step information is the step information of the tensor targeted by the index operation.

[0032] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is a slice, determining the step information of the result of the index operation according to the index information includes:

[0033] The stride content of the first dimension is determined according to the stride content of the first dimension in the original stride information and the stride value of the slice, and the stride contents of other dimensions remain unchanged to form the stride information of the result of the index operation, wherein the original stride information is the stride information of the tensor targeted by the index operation.

[0034] In combination with any implementation manner provided in the present disclosure, when the index information includes index elements of multiple dimensions, the dimension information, bias information, and step information of the result of the index operation determined according to the index information include:

[0035] The dimension information, bias information and step size information of the index result are determined according to the index elements of each dimension in turn.

[0036] In combination with any embodiment provided by the present disclosure, the first tensor is saved in the form of an ordered array, wherein the order of the ordered array is determined according to a preset order of each dimension of the tensor and a preset order of tensor elements in each dimension;

[0037] The labeling the tensor using the dimension information, the bias information, and the step information includes:

[0038] The position of each tensor element contained in the result of the index operation in the ordered array is marked using the dimension information, the bias information, and the stride information.

[0039] In combination with any implementation scheme provided in the present disclosure, the bias information of the first tensor is 0, the step content of the first dimension is 1, and the step content of other dimensions is the product of the step content of the previous dimension and the dimension content.

[0040] According to a second aspect of an embodiment of the present invention, there is provided a code generating device, comprising:

[0041] An acquisition module, configured to acquire an initial operation expression, wherein the initial operation expression includes an index operation of the first tensor;

[0042] a conversion module, for converting the initial operation expression into a target arithmetic expression by taking the expression of the index operation as input or output for the arithmetic operation on the result of the index operation in the initial operation expression;

[0043] A generation module is used to generate code according to the target arithmetic expression, wherein the code of the expression of the index operation is obtained by marking the first tensor according to the index information of the index operation.

[0044] In combination with any embodiment provided in the present disclosure, the conversion module is specifically used for:

[0045] Generate a computation graph according to the initial computation expression;

[0046] According to the operation graph, the target arithmetic expression is generated.

[0047] In combination with any implementation manner provided in the present disclosure, when the conversion module is used to generate a computation graph according to the initial computation expression, it is specifically used to:

[0048] In response to one of the input and the output of the arithmetic operation in the initial operation expression being a result of the index operation of the first tensor and the other being a second tensor, generating a first node representing the first tensor, a second node representing the second tensor, and an operation edge representing index information of the index operation and arithmetic information of the arithmetic operation;

[0049] In response to one of the input and output of the arithmetic operation in the initial operation expression being the first tensor and the other being the third tensor, a first node representing the first tensor, a third node representing the third tensor, and an arithmetic edge representing arithmetic information of the arithmetic operation are generated.

[0050] In combination with any embodiment provided in the present disclosure, the conversion module is further specifically used for:

[0051] In response to there being two operation edges between the first node and the second node, and the directions of the arithmetic information represented by the two operation edges are opposite, determining a fourth tensor according to the first tensor and index information represented by one operation edge;

[0052] The one operation edge is deleted, and a fourth node representing the fourth tensor, an arithmetic edge representing the arithmetic information represented by the one first edge, and a jump edge connecting the first node and the fourth node are generated.

[0053] In combination with any embodiment provided in the present disclosure, when the conversion module is used to generate a target arithmetic expression according to the operation graph, it is specifically used to:

[0054] A target arithmetic expression sub-formula is generated according to each operation edge of the operation graph and the nodes at both ends of the operation edge, and / or a target arithmetic expression sub-formula is generated according to each arithmetic edge of the operation graph and the nodes at both ends of the arithmetic edge.

[0055] In combination with any implementation manner provided in the present disclosure, when the generating module is used to label the first tensor according to the index information of the index operation, it is specifically used to:

[0056] Determine the dimension information, bias information and step information of the result of the index operation according to the index information, wherein the dimension information includes the dimension content of each dimension, the bias information represents the position of the first tensor element in the tensor, and the step information includes the step content of each dimension, and the step content represents the cycle period of the tensor element of the corresponding dimension;

[0057] The first tensor is labeled using the dimension information, the bias information, and the stride information.

[0058] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is an integer, the generating module is used to determine the dimension information of the result of the index operation according to the index information, specifically for:

[0059] The dimensional content of the first dimension is deleted from the original dimensional information to obtain the dimensional information of the result of the index operation, wherein the original dimensional information is the dimensional information of the tensor targeted by the index operation.

[0060] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is an integer, the generating module is used to determine the bias information of the result of the index operation according to the index information, specifically for:

[0061] The bias information of the result of the index operation is determined according to the original bias information, the integer and the step content of the first dimension in the original step information, wherein the original bias information is the bias information of the tensor targeted by the index operation, and the original step information is the step information of the tensor targeted by the index operation.

[0062] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is an integer, the generation module is used to determine the step information of the result of the index operation according to the index information, specifically for:

[0063] The step content of the first dimension is deleted from the original step information to obtain the step information of the result of the index operation, wherein the original step information is the step information of the tensor targeted by the index operation.

[0064] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is a slice, the generating module is used to determine the dimension information of the result of the index operation according to the index information, specifically to:

[0065] The dimensional content of the first dimension is determined according to the dimensional content of the first dimension in the original dimensional information, and the starting value, ending value and step value of the slice, and the dimensional content of other dimensions remains unchanged to obtain the dimensional information of the result of the index operation, wherein the original dimensional information is the dimensional information of the tensor targeted by the index operation.

[0066] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is a slice, the generating module is used to determine the bias information of the result of the index operation according to the index information, specifically for:

[0067] The bias information of the result of the index operation is determined according to the original bias information, the starting value of the slice and the step content of the first dimension in the original step information, wherein the original bias information is the bias information of the tensor targeted by the index operation, and the original step information is the step information of the tensor targeted by the index operation.

[0068] In combination with any implementation manner provided in the present disclosure, when the index element of the first dimension in the index information is a slice, the generating module is used to determine the step information of the result of the index operation according to the index information, specifically for:

[0069] The stride content of the first dimension is determined according to the stride content of the first dimension in the original stride information and the stride value of the slice, and the stride contents of other dimensions remain unchanged to form the stride information of the result of the index operation, wherein the original stride information is the stride information of the tensor targeted by the index operation.

[0070] In combination with any implementation manner provided in the present disclosure, when the index information includes index elements of multiple dimensions, the generation module is used to determine the dimension information, bias information and step information of the result of the index operation according to the index information, specifically for:

[0071] The dimension information, bias information and step size information of the index result are determined according to the index elements of each dimension in turn.

[0072] In combination with any embodiment provided by the present disclosure, the first tensor is saved in the form of an ordered array, wherein the order of the ordered array is determined according to a preset order of each dimension of the tensor and a preset order of tensor elements in each dimension;

[0073] When the generating module is used to label the tensor using the dimension information, the bias information and the step information, it is specifically used to:

[0074] The position of each tensor element contained in the result of the index operation in the ordered array is marked using the dimension information, the bias information, and the stride information.

[0075] In combination with any implementation scheme provided in the present disclosure, the bias information of the first tensor is 0, the step content of the first dimension is 1, and the step content of other dimensions is the product of the step content of the previous dimension and the dimension content.

[0076] According to a third aspect of an embodiment of the present invention, an electronic device is provided, the device comprising a memory and a processor, the memory being used to store computer instructions executable on the processor, the processor being used to implement the method described in the first aspect when executing the computer instructions.

[0077] According to a fourth aspect of an embodiment of the present invention, there is provided a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the method described in the first aspect is implemented.

[0078] According to the above embodiment, an initial operation expression of an index operation including a first tensor is obtained, and for an arithmetic operation on the result of the index operation in the above initial operation expression, the initial operation expression is converted into a target arithmetic expression by taking the expression of the index operation as input or output, and finally code is generated according to the target arithmetic expression, and when the code of the expression of the index operation is generated, the first tensor is annotated according to the index information of the index operation. Because the result of the index operation in the initial operation expression is replaced with the expression of the index operation before the code is generated to generate the target arithmetic expression, and the first tensor is annotated according to the index information of the index operation when the code is generated, code can be generated for the tensor operation including the index operation, and the read and write operations on the result of the index operation are reduced when the code is running, thereby saving storage space for the result of the index operation.

[0079] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0080] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.

[0081] Figure 1is a flow chart of a code generation method shown in an embodiment of the present invention;

[0082] Figure 2 is a schematic diagram of an initial diagram shown in an embodiment of the present invention;

[0083] Figure 3 The operation shown in the embodiment of the present invention is Figure 1 Schematic diagram of the form;

[0084] Figure 4 is a schematic diagram of another form of a computation graph shown in an embodiment of the present invention;

[0085] Figure 5 is a schematic diagram of a first tensor shown in an embodiment of the present invention;

[0086] Figure 6 is a schematic diagram of the structure of a code generating device shown in an embodiment of the present invention;

[0087] Figure 7 It is a schematic diagram of the structure of an electronic device shown in an embodiment of the present invention. DETAILED DESCRIPTION

[0088] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. Instead, they are merely examples of devices and methods consistent with some aspects of the present invention as detailed in the appended claims.

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

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

[0091] In a first aspect, at least one embodiment of the present invention provides a code generation method, please refer to the attached Figure 1 , which shows the process of the method, including steps S101 to S103.

[0092] Among them, the method generates code for the process of tensor operation, and the tensor can be a tensor representing an image or table to be processed, so the operation process of the tensor can correspond to the processing process of the object to be processed; for example, the values ​​of the three channels R (red), G (green), and B (blue) of all pixels of an image constitute a tensor representing the image, and the processing process for the image is the operation process of the above tensor. The process of tensor operation can include only index operations, only arithmetic operations, or both index operations and arithmetic operations. The code generation method provided in this embodiment is for the process of tensor operation that includes both index operations and arithmetic operations. The number of index operations and / or arithmetic operations included in the process of tensor operation can be one or more.

[0093] In step S101, an initial operation expression is obtained, wherein the initial operation expression includes an index operation of a first tensor.

[0094] Each index operation of the initial operation expression can be an index operation sub-formula, and each arithmetic operation can be an arithmetic operation sub-formula, that is, the initial operation expression is composed of at least one index operation sub-formula and at least one arithmetic operation sub-formula. There is a certain order between the at least one index operation sub-formula and the at least one arithmetic operation sub-formula, and different sub-formulas can have a reference relationship, that is, the operation result of the previous sub-formula can be used as the input or output of the subsequent sub-formula.

[0095] The initial operation expression can be obtained according to the representation tensor of the object to be processed, that is, the processing process of the object to be processed is converted into the operation process of the representation tensor, for example, the feature extraction process of an image is converted into the operation process of the representation tensor of the image.

[0096] In one example, the initial operation expression is as follows:

[0097] B = A[1,:] (initial subformula 1);

[0098] C = A[2,:] (initial subformula 2);

[0099] D = B + 2 (initial subformula three);

[0100] C = D + 4 (initial subformula four);

[0101] E=A+4 (initial sub-formula five).

[0102] In step S102, for the arithmetic operation on the result of the index operation in the initial operation expression, the initial operation expression is converted into a target arithmetic expression by taking the expression of the index operation as input or output.

[0103] Among them, there can be a reference relationship between the various sub-expressions in the initial operation expression, so the input or output of the arithmetic operation sub-expression can be a new tensor, the result of other arithmetic operation sub-expressions, or the result of index operation; the input or output of the index operation sub-expression can be a new tensor, the result of other index operation sub-expressions, or the result of arithmetic operation; it should be noted that the result of each index operation sub-expression is referenced at least once by the arithmetic operation sub-expression, because a single index operation cannot correspond to any processing process of the target object. The arithmetic operation on the result of the index operation refers to the arithmetic operation sub-formula that uses the result of the index operation sub-formula as input or output. For example, the initial sub-formula three in the example in step S101 is an arithmetic operation initial sub-formula, but its input includes the result of the initial sub-formula one, and the initial sub-formula one is the index operation initial sub-formula, so the initial sub-formula three is the arithmetic operation on the result of the index operation. For another example, the initial sub-formula four in the example in step S101 is an arithmetic operation initial sub-formula, but its output is the result of the initial sub-formula two, and the initial sub-formula two is the index operation initial sub-formula, so the initial sub-formula four is the arithmetic operation on the result of the index operation.

[0104] The index operation sub-formula includes an equal sign and index operation expressions on both sides of the equal sign and the result of the index operation. By taking the expression of the index operation as input or output, it means replacing the result of the index operation cited in the arithmetic operation sub-formula with the corresponding expression of the index operation, and deleting the corresponding index operation sub-formula, thereby converting the initial operation expression into a target arithmetic expression. Therefore, the target arithmetic expression only includes the arithmetic operation sub-formula but not the index operation sub-formula, and the input or output of at least one arithmetic operation sub-formula is an expression of the index operation.

[0105] In the example in step S101, the initial operation expression includes five sub-formulas, and the initial sub-formula 1 is an index operation initial sub-formula, the initial sub-formula 2 is an index operation initial sub-formula, the initial sub-formula 3 is an arithmetic operation initial sub-formula and one of its inputs is the result of the initial sub-formula 1, the initial sub-formula 4 is an arithmetic operation initial sub-formula and its output is the result of the initial sub-formula 2, and the initial sub-formula 5 is an arithmetic operation initial sub-formula and does not reference the result of the index operation. Therefore, in this step, B in the initial sub-formula 3 is replaced with A[1,:], so the initial sub-formula 3 is changed to D=A[1,:]+2, and the initial sub-formula 1 is deleted, and the C in the initial sub-formula 4 is replaced with A[2,:], so the initial sub-formula 4 is changed to A[2,:]=D+4, and then the initial operation expression composed of the initial sub-formulas 1, 2, 3, 4 and 5 is converted into the following target arithmetic expression:

[0106] D = A[1,:] + 2 (target sub-formula 1);

[0107] A[2,:]=D+4(target sub-formula 2);

[0108] E=A+4 (target sub-formula three).

[0109] By converting the initial operation expression into the target arithmetic expression, the number of sub-expressions is reduced, and the number of variables is reduced (that is, two variables, B and C, are reduced).

[0110] In step S103, code is generated according to the target arithmetic expression, wherein the code of the expression of the index operation is obtained by marking the first tensor according to the index information of the index operation.

[0111] Among them, in the example in the above step S102, the expression A[1,:] of the index operation in the target sub-formula one includes two parts, one part is the first tensor A, and the other part is the index information [1,:] of the index operation (i.e., indexing the first channel of the first dimension). When the code of A[1,:] is generated, A is annotated according to [1,:]. The expression A[2,:] of the index operation in the target sub-formula two also includes two parts, one part is the first tensor A, and the other part is the index information [2,:] of the index operation (i.e., indexing the second channel of the first dimension). When the code of A[2,:] is generated, A is annotated according to [2,:].

[0112] Generating code means using code to describe the above target arithmetic expression. The specific code syntax can be selected and preset as needed and is not limited here.

[0113] Therefore, the generated code does not contain any description of the results of the index operation (such as B and C in the above example). Therefore, when running the code, not only is it not necessary to store the results of the index operation, but when reading and writing the results of the index operation, you can directly read and write from the storage information of the first tensor (such as A in the above example) according to the annotation.

[0114] According to the above embodiment, an initial operation expression of an index operation including a first tensor is obtained, and for an arithmetic operation on the result of the index operation in the above initial operation expression, the initial operation expression is converted into a target arithmetic expression by taking the expression of the index operation as input or output, and finally code is generated according to the target arithmetic expression, and when the code of the expression of the index operation is generated, the first tensor is annotated according to the index information of the index operation. Because the result of the index operation in the initial operation expression is replaced with the expression of the index operation before the code is generated to generate the target arithmetic expression, and the first tensor is annotated according to the index information of the index operation when the code is generated, code can be generated for the tensor operation including the index operation, and the read and write operations on the result of the index operation are reduced when the code is running, thereby saving storage space for the result of the index operation.

[0115] In some embodiments of the present disclosure, the initial operation expression can be converted into a target arithmetic expression for the arithmetic operation on the result of the index operation in the initial operation expression by taking the expression of the index operation as input or output in the following manner: first, an operation graph is generated according to the initial operation expression; next, the target arithmetic expression is generated according to the operation graph.

[0116] Among them, an operation graph is generated according to the initial operation expression, and each index operation and / or arithmetic operation included in the initial operation expression (specifically each index operation sub-formula and / or arithmetic operation sub-formula) can be generated corresponding to an element in the operation graph, and these elements constitute the operation graph.

[0117] In one example, an operation graph can be generated in the following manner: in response to one of the input and output of the arithmetic operation in the initial operation expression being the result of the index operation of the first tensor and the other being the second tensor, a first node representing the first tensor, a second node representing the second tensor, and an operation edge representing index information of the index operation and arithmetic information of the arithmetic operation are generated; in response to one of the input and output of the arithmetic operation in the initial operation expression being the first tensor and the other being the third tensor, a first node representing the first tensor, a third node representing the third tensor, and an arithmetic edge representing the arithmetic information of the arithmetic operation are generated.

[0118] Among them, nodes and edges are elements in the operation graph, and different nodes and edges represent different contents, that is, different nodes represent different variables, and different edges represent different operations, and the contents represented by each edge include arithmetic operations, and the contents represented by some edges also include index operations, and the contents represented by some edges do not include index operations. It can be seen that no edge represents an index operation alone. Optionally, a specific process of generating an operation graph is introduced using the example in step S101: first, five nodes are generated for A (corresponding to the above-mentioned first tensor), B (corresponding to the index result of the above-mentioned first tensor), C (corresponding to the index result of the above-mentioned first tensor), D (corresponding to the above-mentioned second tensor), and E (corresponding to the above-mentioned third tensor) of the five initial sub-formulas, and then an edge is generated for each initial sub-formula, and each edge represents the operation information of the corresponding initial sub-formula (that is, the index information of the index operation or the arithmetic information of the arithmetic operation), so as to obtain the following. Figure 2 Next, the edges representing index operations, the nodes representing the results of index operations, and the edges representing arithmetic operations on the results of the index operations in the above initial graph are merged into operation edges to obtain the following: Figure 3 The operation diagram shown.

[0119] It should be noted that in some cases, the two index results of the first tensor perform arithmetic operations with the second tensor respectively, and in the two arithmetic operations, the variable is used as input and output respectively, thus resulting in two operation edges between the first node and the second node in the generated operation graph, and the directions of the arithmetic information represented by the two operation edges are opposite. For example, in the example in step S101, the initial sub-formula three and the initial sub-formula four are the arithmetic operations between the index results B and C of A and D respectively, and in the initial sub-formula three, the index result B is used as input and D is used as output, while in the initial sub-formula four, D is used as input and the index result C is used as output, which makes the generated graph as follows Figure 3 In the operation graph shown, there are two operation edges between A and D in opposite directions. In this case, a loop calculation is formed between the first tensor and the second tensor, and the target arithmetic expression generated based on this is the three target sub-formulas mentioned in the example in step S102, where there is a repeated loop calculation between A and D between the target sub-formula 1 and the target sub-formula 2, so the computer will become an infinite loop after generating the code, so it is necessary to solve this loop in this case.

[0120] Optionally, the infinite loop caused by the above situation is solved in the following manner: first, in response to the fact that there are two operation edges between the first node and the second node, and the directions of the arithmetic information represented by the two operation edges are opposite, the fourth tensor is determined according to the first tensor and the index information represented by an operation edge; next, the one operation edge is deleted, and a fourth node representing the fourth tensor, an arithmetic edge representing the arithmetic information represented by the one first edge, and a jump edge connecting the first node and the fourth node are generated. This method solves the problem of infinite loops by splitting one of the two operation edges into an arithmetic edge, a jump edge, and a node. As shown in the example Figure 3 As an example, there are two computational edges between A and D in opposite directions. Therefore, the computational edge from D to A can be decomposed into a new arithmetic edge, a new node A.shadow, and a jump edge to obtain Figure 4 The operation diagram shown.

[0121] Based on the above method of generating a computation graph, the target arithmetic expression can be generated according to the computation graph in the following manner: generating a target arithmetic expression sub-formula according to each computation edge of the computation graph and the nodes at both ends of the computation edge, and / or generating a target arithmetic expression sub-formula according to each arithmetic edge of the computation graph and the nodes at both ends of the arithmetic edge. That is, each edge in the computation graph (except for the jump edge) generates a target arithmetic expression sub-formula, such as Figure 4 Taking the operation graph shown in FIG. 1 as an example, the operation edge between A and D generates the target minor formula 1 in the example in step S102, the arithmetic edge between A and E generates the target minor formula 3 in the example in step S102, and according to the initial minor formula 2 in the example in step S101, the data of A.shadow (i.e., C) is stored, and according to the arithmetic edge between D and A.shadow, the following target minor formula 4 is generated:

[0122] A.shadow=D+4;

[0123] Therefore, the target arithmetic expression is:

[0124] D = A[1,:] + 2 (target sub-formula 1);

[0125] A.shadow=D+4(target subformula 4);

[0126] E=A+4 (target sub-formula three).

[0127] The target arithmetic expression generated in the above manner solves the infinite loop problem between A and D caused by the above situation, and reduces the reading and writing operations of the index operation results when the code is running, thereby saving storage space for the index operation results.

[0128] In some embodiments of the present disclosure, the first tensor can be labeled according to the index information of the index operation in the following manner: first, the dimension information, offset information (Offset) and stride information of the result of the index operation are determined according to the index information, wherein the dimension information includes the dimension content (Dims) of each dimension, the offset information represents the position of the first tensor element in the tensor, and the stride information includes the stride content (Strides) of each dimension, and the stride content represents the cycle period of the tensor elements of the corresponding dimension; next, the first tensor is labeled using the dimension information, the offset information and the stride information.

[0129] The use of dimension information, offset information and step information to label the first tensor is based on the storage method of the first tensor in the computer. That is, the first tensor is stored in the form of an ordered array, and the order of the ordered array is determined according to the preset order of each dimension of the tensor and the preset order of the tensor elements in each dimension. For example, Figure 5 The first tensor shown has a dimension of 3*4*5 and a total of 60 tensor elements. The corresponding ordered array includes 60 numbers, which can be arranged in the following order: first obtain the dimension "5" (i.e., the horizontal dimension in the figure), then obtain the dimension "4" (i.e., the vertical dimension in the figure), and finally obtain the dimension "3" (i.e., the width direction in the figure), and then obtain each tensor element from right to left when obtaining the dimension "5", obtain each tensor element from top to bottom when obtaining the dimension "4", and obtain each tensor element from front to back when obtaining the dimension "3".

[0130] Therefore, the first tensor includes all tensor elements in the corresponding ordered array, and the result of the index calculation includes some tensor elements in the ordered array, and then the use of the dimension information, the bias information and the step information to mark the tensor means: using the dimension information, the bias information and the step information to mark the positions of each tensor element contained in the result of the index operation in the ordered array.

[0131] The following describes the meaning of index operations and dimension information, bias information, and step information to clarify how to use the above three (or groups of) variables to annotate the results of index operations.

[0132] The index information of the index operation includes the index element for each dimension of the original tensor. The index element can be an integer or a slice, where an integer is an index of a channel of the dimension, and a slice is an index of multiple channels of the dimension. A slice consists of a start value (start), an end value (stop), and a step value (step). For example, if the start value is 1, the end value is 4, and the step value is 2, then the first channel and the third channel are indexed. In addition, the index element can be omitted (for example, represented by :), which means that all channels of the dimension are indexed. For example, for Figure 5 The index information shown is (1,:,:), then the index result is as follows Figure 5 The shaded part.

[0133] Dimension information is the dimensional information of the result of the index operation, including the dimensional content of each dimension (the dimensional content is the number of channels in that dimension), for example Figure 5 The dimension information of the result of the index operation shown in the shadow is 5*4; the offset information represents the position of the first tensor element in the ordered array corresponding to the tensor, for example Figure 5 The offset information of the result of the index operation shown in the shaded area is 0, because the first tensor element of the result is the first tensor element of the original tensor. For another example, the second channel of the dimension of index "3" has the offset information of 20, because the first tensor element of the result is 20 tensor elements away from the first tensor element of the original tensor in the ordered array corresponding to the original tensor. The step information includes the step content of each dimension, and the step content represents the cycle period of the tensor elements of the corresponding dimension. The cycle period refers to the number of tensor elements that need to be crossed in the ordered array to switch the channel of the dimension (for example, switching from the first tensor element of one channel to the first tensor element of the next channel), for example Figure 5 In the tensor shown, the stride content of the "5" dimension is 1, the stride content of the "4" dimension is 5, and the stride content of the "3" dimension is 20.

[0134] From the above introduction, we can know that the bias information of the first tensor is 0, the stride content of the first dimension is 1, and the stride content of other dimensions is the product of the stride content of the previous dimension and the dimension content. On this basis, the three (or groups) variables (i.e., dimension information, bias information, and stride information) of the result of the index calculation of the first tensor can be determined respectively. The determination method is described in detail below.

[0135] In some embodiments, when the index element of the first dimension in the index information is an integer, the dimension information of the result of the index operation can be determined according to the index information in the following manner: the dimension content of the first dimension is deleted from the original dimension information to obtain the dimension information of the result of the index operation, wherein the original dimension information is the dimension information of the tensor targeted by the index operation. When the index element is an integer, the first dimension only indexes one of the specific channels, so the dimension does not exist in the result of the index calculation, so the dimension content of the dimension is deleted, for example Figure 5 In the tensor shown, the first channel of the index "3" dimension has only one channel left in the "3" dimension, so the dimension information of the result of the index operation is 5*4.

[0136] In some embodiments, when the index element of the first dimension in the index information is an integer, the bias information of the result of the index operation can be determined according to the index information in the following manner: the bias information of the result of the index operation is determined according to the original bias information, the integer and the step content of the first dimension in the original step information, wherein the original bias information is the bias information of the tensor targeted by the index operation, and the original step information is the step information of the tensor targeted by the index operation. Optionally, the bias information is calculated according to the following formula:

[0137] O1=O0+(N-1)*S0;

[0138] Among them, O1 represents the bias information of the result of the index operation, O0 represents the original bias information, N represents the integer of the index element of the first dimension, and S0 represents the step content of the first dimension in the original step information.

[0139] For example Figure 5 For the tensor shown, the index element for the dimension "3" is 2, which indexes the second channel of this dimension, and the bias information is 20. As can be seen from the figure, the first tensor element of the first tensor is the first tensor element of the corresponding ordered array, and the first tensor element of the index result is the 21st tensor element of the ordered array, so the bias between them is 20.

[0140] In some embodiments, when the index element of the first dimension in the index information is an integer, the step information of the result of the index operation can be determined according to the index information in the following manner: the step content of the first dimension is deleted from the original step information to obtain the step information of the result of the index operation, wherein the original step information is the step information of the tensor targeted by the index operation. When the index element is an integer, the first dimension only indexes one of the specific channels, so the dimension does not exist in the result of the index calculation, so the step content of the dimension is deleted, for example Figure 5In the tensor shown, the first channel of the index "3" dimension has only one channel left in the "3" dimension, so the stride information in the result of the index operation only includes the stride content of the two dimensions "5" and "4".

[0141] In some embodiments, when the index element of the first dimension in the index information is a slice, the dimension information of the result of the index operation can be determined according to the index information in the following manner: the dimension content of the first dimension is determined according to the dimension content of the first dimension in the original dimension information, and the starting value, ending value and step value of the slice, and the dimension content of other dimensions remains unchanged to obtain the dimension information of the result of the index operation, wherein the original dimension information is the dimension information of the tensor targeted by the index operation. When the index element is a slice, the first dimension indexes at least two channels, so the number of channels of the dimension in the index result can be determined according to the original number of channels of the dimension and the three parameters of the slice, and the number of channels is used to determine the dimension content of the dimension. For example, for example Figure 5 For the dimension "4" of the tensor shown, the starting value of the slice is 1, the ending value is 4, and the step value is 2. The first channel and the third channel are indexed, a total of two channels. The dimension content of this dimension in the result of the index operation is 2.

[0142] In some embodiments, when the index element of the first dimension in the index information is a slice, the bias information of the result of the index operation can be determined according to the index information in the following manner: the bias information of the result of the index operation is determined according to the original bias information, the starting value of the slice and the step content of the first dimension in the original step information, wherein the original bias information is the bias information of the tensor targeted by the index operation, and the original step information is the step information of the tensor targeted by the index operation. Optionally, the bias information is calculated according to the following formula:

[0143] O1=O0+(M-1)*S0;

[0144] Among them, O1 represents the bias information of the result of the index operation, O0 represents the original bias information, M represents the starting value of the index element of the first dimension, and represents the step content of the first dimension in the original step information.

[0145] For example Figure 5 For the tensor shown, the starting value of the index element of the dimension "3" is 2. The index starts from the second channel of this dimension, and the bias information is 20. It can be seen from the figure that the first tensor element of the first tensor is the first tensor element of the corresponding ordered array, and the first tensor element of the index result is the 21st tensor element of the ordered array, so the bias between them is 20.

[0146] In some embodiments, when the index element of the first dimension in the index information is a slice, the step information of the result of the index operation can be determined according to the index information in the following manner: the step content of the first dimension is determined according to the step content of the first dimension in the original step information and the step value of the slice, and the step content of other dimensions remains unchanged to form the step information of the result of the index operation, wherein the original step information is the step information of the tensor targeted by the index operation. Optionally, the bias information is calculated according to the following formula:

[0147] S1=S0*S q ;

[0148] Among them, S1 represents the step length content of the first dimension in the result of the index operation; S0 represents the step length content of the first dimension in the original step length information; S q The stride value for slicing the index elements of the first dimension.

[0149] For example Figure 5 For the tensor shown, the starting value of the index element of the dimension "3" is 1, the ending value is 3, and the step value is 2. The first channel and the third channel of this dimension are indexed, and the bias information is 40. It can be seen from the figure that the step information of the dimension "3" of the first tensor is 20 (that is, switching from the first channel of this dimension to the second channel needs to span 20 tensor elements in the corresponding ordered array), and the result of the index operation switches from the first channel to the second channel (that is, the third channel in the original tensor) in the corresponding ordered array. The step content of the dimension "3" of the result of the index operation is 40.

[0150] It should be noted that when the dimension information, bias information and step information of the result of the index operation are determined according to the index information, if the index information includes index elements of multiple dimensions, the dimension information, bias information and step information of the index result are determined according to the index elements of each dimension in turn. That is, when indexing according to the index element of the first dimension, the first tensor is targeted, and when indexing according to the index elements of other dimensions, the index result of the previous dimension is targeted.

[0151] According to a second aspect of an embodiment of the present invention, a code generating device is provided. Figure 6 , which shows a schematic structural diagram of the device, including:

[0152] An acquisition module 601 is used to acquire an initial operation expression, wherein the initial operation expression includes an index operation of a first tensor;

[0153] A conversion module 602, for converting the initial operation expression into a target arithmetic expression by taking the expression of the index operation as input or output for the arithmetic operation on the result of the index operation in the initial operation expression;

[0154] A generation module 603 is used to generate code according to the target arithmetic expression, wherein the code of the expression of the index operation is obtained by marking the first tensor according to the index information of the index operation.

[0155] In some embodiments of the present disclosure, the conversion module is specifically used for:

[0156] Generate a computation graph according to the initial computation expression;

[0157] According to the operation graph, the target arithmetic expression is generated.

[0158] In some embodiments of the present disclosure, when the conversion module is used to generate a computation graph according to the initial computation expression, it is specifically used to:

[0159] In response to one of the input and the output of the arithmetic operation in the initial operation expression being a result of the index operation of the first tensor and the other being a second tensor, generating a first node representing the first tensor, a second node representing the second tensor, and an operation edge representing index information of the index operation and arithmetic information of the arithmetic operation;

[0160] In response to one of the input and output of the arithmetic operation in the initial operation expression being the first tensor and the other being the third tensor, a first node representing the first tensor, a third node representing the third tensor, and an arithmetic edge representing arithmetic information of the arithmetic operation are generated.

[0161] In some embodiments of the present disclosure, the conversion module is further specifically used for:

[0162] In response to there being two operation edges between the first node and the second node, and the directions of the arithmetic information represented by the two operation edges are opposite, determining a fourth tensor according to the first tensor and index information represented by one operation edge;

[0163] The one operation edge is deleted, and a fourth node representing the fourth tensor, an arithmetic edge representing the arithmetic information represented by the one first edge, and a jump edge connecting the first node and the fourth node are generated.

[0164] In some embodiments of the present disclosure, when the conversion module is used to generate a target arithmetic expression according to the operation graph, it is specifically used to:

[0165] A target arithmetic expression sub-formula is generated according to each operation edge of the operation graph and the nodes at both ends of the operation edge, and / or a target arithmetic expression sub-formula is generated according to each arithmetic edge of the operation graph and the nodes at both ends of the arithmetic edge.

[0166] In some embodiments of the present disclosure, when the generating module is used to label the first tensor according to the index information of the index operation, it is specifically used to:

[0167] Determine the dimension information, bias information and step information of the result of the index operation according to the index information, wherein the dimension information includes the dimension content of each dimension, the bias information represents the position of the first tensor element in the tensor, and the step information includes the step content of each dimension, and the step content represents the cycle period of the tensor element of the corresponding dimension;

[0168] The first tensor is labeled using the dimension information, the bias information, and the stride information.

[0169] In some embodiments of the present disclosure, when the index element of the first dimension in the index information is an integer, the generating module is used to determine the dimension information of the result of the index operation according to the index information, specifically to:

[0170] The dimensional content of the first dimension is deleted from the original dimensional information to obtain the dimensional information of the result of the index operation, wherein the original dimensional information is the dimensional information of the tensor targeted by the index operation.

[0171] In some embodiments of the present disclosure, when the index element of the first dimension in the index information is an integer, the generating module is used to determine the bias information of the result of the index operation according to the index information, specifically to:

[0172] The bias information of the result of the index operation is determined according to the original bias information, the integer and the step content of the first dimension in the original step information, wherein the original bias information is the bias information of the tensor targeted by the index operation, and the original step information is the step information of the tensor targeted by the index operation.

[0173] In some embodiments of the present disclosure, when the index element of the first dimension in the index information is an integer, the generating module is used to determine the step information of the result of the index operation according to the index information, specifically to:

[0174] The step content of the first dimension is deleted from the original step information to obtain the step information of the result of the index operation, wherein the original step information is the step information of the tensor targeted by the index operation.

[0175] In some embodiments of the present disclosure, when the index element of the first dimension in the index information is a slice, the generating module is used to determine the dimension information of the result of the index operation according to the index information, specifically to:

[0176] The dimensional content of the first dimension is determined according to the dimensional content of the first dimension in the original dimensional information, and the starting value, ending value and step value of the slice, and the dimensional content of other dimensions remains unchanged to obtain the dimensional information of the result of the index operation, wherein the original dimensional information is the dimensional information of the tensor targeted by the index operation.

[0177] In some embodiments of the present disclosure, when the index element of the first dimension in the index information is a slice, the generating module is used to determine the bias information of the result of the index operation according to the index information, specifically to:

[0178] The bias information of the result of the index operation is determined according to the original bias information, the starting value of the slice and the step content of the first dimension in the original step information, wherein the original bias information is the bias information of the tensor targeted by the index operation, and the original step information is the step information of the tensor targeted by the index operation.

[0179] In some embodiments of the present disclosure, when the index element of the first dimension in the index information is a slice, the generating module is used to determine the step information of the result of the index operation according to the index information, specifically to:

[0180] The stride content of the first dimension is determined according to the stride content of the first dimension in the original stride information and the stride value of the slice, and the stride contents of other dimensions remain unchanged to form the stride information of the result of the index operation, wherein the original stride information is the stride information of the tensor targeted by the index operation.

[0181] In some embodiments of the present disclosure, when the index information includes index elements of multiple dimensions, the generation module is used to determine the dimension information, bias information and step information of the result of the index operation according to the index information, specifically for:

[0182] The dimension information, bias information and step size information of the index result are determined according to the index elements of each dimension in turn.

[0183] In some embodiments of the present disclosure, the first tensor is stored in the form of an ordered array, wherein the order of the ordered array is determined according to a preset order of dimensions of the tensor and a preset order of tensor elements within each dimension;

[0184] When the generating module is used to label the tensor using the dimension information, the bias information and the step information, it is specifically used to:

[0185] The position of each tensor element contained in the result of the index operation in the ordered array is marked using the dimension information, the bias information, and the stride information.

[0186] In some embodiments of the present disclosure, the bias information of the first tensor is 0, the stride content of the first dimension is 1, and the stride content of other dimensions is the product of the stride content of the previous dimension and the dimension content.

[0187] Regarding the device in the above embodiment, the specific manner in which each module performs the operation has been described in detail in the embodiment of the method in the third aspect, and will not be elaborated here.

[0188] In a third aspect, at least one embodiment of the present invention provides a device, please refer to the attached Figure 7 , which shows the structure of the device, the device includes a memory and a processor, the memory is used to store computer instructions that can be executed on the processor, and the processor is used to generate code based on any method described in the first aspect when executing the computer instructions.

[0189] In a fourth aspect, at least one embodiment of the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the program, when executed by a processor, implements any of the methods described in the first aspect.

[0190] In the present invention, the terms "first" and "second" are used for descriptive purposes only and cannot be understood as indicating or implying relative importance. The term "plurality" refers to two or more than two, unless otherwise clearly defined.

[0191] Those skilled in the art will readily appreciate other embodiments of the present invention after considering the specification and practicing the disclosure disclosed herein. The present invention is intended to cover any variations, uses or adaptations of the present invention that follow the general principles of the present invention and include common knowledge or customary techniques in the art that are not disclosed by the present invention. The description and examples are to be considered exemplary only, and the true scope and spirit of the present invention are indicated by the following claims.

[0192] It should be understood that the present invention is not limited to the exact construction that has been described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the present invention is limited only by the appended claims.

Claims

1. A code generation method, characterized in that: include: Obtaining an initial operation expression, wherein the initial operation expression includes an index operation of the first tensor; For the arithmetic operation on the result of the index operation in the initial operation expression, convert the initial operation expression into a target arithmetic expression by taking the expression of the index operation as input or output; Generate code according to the target arithmetic expression, wherein the code of the expression of the index operation is obtained by marking the first tensor according to the index information of the index operation; The arithmetic operation on the result of the index operation in the initial operation expression, by using the expression of the index operation as input or output, converting the initial operation expression into a target arithmetic expression, comprises: Generate a computation graph according to the initial computation expression; According to the operation graph, generating the target arithmetic expression; Generating a target arithmetic expression according to the operation graph includes: A target arithmetic expression sub-formula is generated according to each operation edge of the operation graph and the nodes at both ends of the operation edge, and / or a target arithmetic expression sub-formula is generated according to each arithmetic edge of the operation graph and the nodes at both ends of the arithmetic edge.

2. The code generation method according to claim 1, characterized in that: Generate a computation graph according to the initial computation expression, including: In response to one of the input and the output of the arithmetic operation in the initial operation expression being a result of the index operation of the first tensor and the other being a second tensor, generating a first node representing the first tensor, a second node representing the second tensor, and an operation edge representing index information of the index operation and arithmetic information of the arithmetic operation; In response to one of the input and output of the arithmetic operation in the initial operation expression being the first tensor and the other being the third tensor, a first node representing the first tensor, a third node representing the third tensor, and an arithmetic edge representing arithmetic information of the arithmetic operation are generated.

3. The code generation method according to claim 2, characterized in that: Also includes: In response to there being two operation edges between the first node and the second node, and the directions of the arithmetic information represented by the two operation edges are opposite, determining a fourth tensor according to the first tensor and index information represented by one operation edge; The one operation edge is deleted, and a fourth node representing the fourth tensor, an arithmetic edge representing the arithmetic information represented by the one operation edge, and a jump edge connecting the first node and the fourth node are generated.

4. The code generation method according to claim 1, characterized in that: The labeling the first tensor according to the index information of the index operation includes: Determine the dimension information, bias information and step information of the result of the index operation according to the index information, wherein the dimension information includes the dimension content of each dimension, the bias information represents the position of the first tensor element in the tensor, and the step information includes the step content of each dimension, and the step content represents the cycle period of the tensor element of the corresponding dimension; The first tensor is labeled using the dimension information, the bias information, and the stride information.

5. The code generation method according to claim 4, characterized in that: When the index element of the first dimension in the index information is an integer, determining the dimension information of the result of the index operation according to the index information includes: The dimensional content of the first dimension is deleted from the original dimensional information to obtain the dimensional information of the result of the index operation, wherein the original dimensional information is the dimensional information of the tensor targeted by the index operation.

6. The code generation method according to claim 4, characterized in that: When the index element of the first dimension in the index information is an integer, the determining the bias information of the result of the index operation according to the index information includes: The bias information of the result of the index operation is determined according to the original bias information, the integer and the step content of the first dimension in the original step information, wherein the original bias information is the bias information of the tensor targeted by the index operation, and the original step information is the step information of the tensor targeted by the index operation.

7. The code generation method according to claim 4, characterized in that: When the index element of the first dimension in the index information is an integer, the step information of the result of the index operation determined according to the index information includes: The step content of the first dimension is deleted from the original step information to obtain the step information of the result of the index operation, wherein the original step information is the step information of the tensor targeted by the index operation.

8. The code generation method according to claim 4, characterized in that: When the index element of the first dimension in the index information is a slice, determining the dimension information of the result of the index operation according to the index information includes: The dimensional content of the first dimension is determined according to the dimensional content of the first dimension in the original dimensional information, and the starting value, ending value and step value of the slice, and the dimensional content of other dimensions remains unchanged to obtain the dimensional information of the result of the index operation, wherein the original dimensional information is the dimensional information of the tensor targeted by the index operation.

9. The code generation method according to claim 4, characterized in that: When the index element of the first dimension in the index information is a slice, determining the bias information of the result of the index operation according to the index information includes: The bias information of the result of the index operation is determined according to the original bias information, the starting value of the slice and the step content of the first dimension in the original step information, wherein the original bias information is the bias information of the tensor targeted by the index operation, and the original step information is the step information of the tensor targeted by the index operation.

10. The code generation method according to claim 4, characterized in that: When the index element of the first dimension in the index information is a slice, determining the step information of the result of the index operation according to the index information includes: The stride content of the first dimension is determined according to the stride content of the first dimension in the original stride information and the stride value of the slice, and the stride contents of other dimensions remain unchanged to form the stride information of the result of the index operation, wherein the original stride information is the stride information of the tensor targeted by the index operation.

11. The code generation method according to any one of claims 4 to 10, characterized in that: When the index information includes index elements of multiple dimensions, the dimension information, offset information and step information of the result of the index operation determined according to the index information include: The dimension information, bias information and step size information of the index result are determined according to the index elements of each dimension in turn.

12. The code generation method according to any one of claims 4 to 10, characterized in that: The first tensor is stored in the form of an ordered array, wherein the order of the ordered array is determined according to a preset order of dimensions of the tensor and a preset order of tensor elements within each dimension; The labeling the tensor using the dimension information, the bias information, and the step information includes: The position of each tensor element contained in the result of the index operation in the ordered array is marked using the dimension information, the bias information, and the stride information.

13. The code generation method according to claim 12, characterized in that: The bias information of the first tensor is 0, the stride content of the first dimension is 1, and the stride content of other dimensions is the product of the stride content of the previous dimension and the dimension content.

14. A code generating device, characterized in that: include: An acquisition module, configured to acquire an initial operation expression, wherein the initial operation expression includes an index operation of the first tensor; a conversion module, for converting the initial operation expression into a target arithmetic expression by taking the expression of the index operation as input or output for the arithmetic operation on the result of the index operation in the initial operation expression; A generation module, configured to generate code according to the target arithmetic expression, wherein the code of the expression of the index operation is obtained by marking the first tensor according to the index information of the index operation; The conversion module is specifically used for: Generate a computation graph according to the initial computation expression; According to the operation graph, generating the target arithmetic expression; When the conversion module is used to generate a target arithmetic expression according to the operation graph, it is specifically used to: A target arithmetic expression sub-formula is generated according to each operation edge of the operation graph and the nodes at both ends of the operation edge, and / or a target arithmetic expression sub-formula is generated according to each arithmetic edge of the operation graph and the nodes at both ends of the arithmetic edge.

15. An electronic device, characterized in that: The device comprises a memory and a processor, wherein the memory is used to store computer instructions executable on the processor, and the processor is used to implement the method according to any one of claims 1 to 13 when executing the computer instructions.

16. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 13 is implemented.