Deep learning model compiling method and device, equipment, medium and program product

By optimizing code written based on a common language, the compilation process of deep learning models is simplified, solving the problems of high learning cost and difficulty in implementation of optimizing intermediate representation calculation graphs in the existing technology, and achieving efficient deep learning model compilation and inference.

CN118585196APending Publication Date: 2024-09-03BEIJING BAIDU NETCOM SCI & TECH CO LTD
View PDF 0 Cites 5 Cited by

Patent Information

Application Number
CN202410802631.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-20
Publication Date
2024-09-03

AI Technical Summary

Technical Problem

In the compilation process of the deep learning model in the existing technology, there are problems such as high learning cost, high implementation difficulty, and high development and maintenance cost of optimizing the intermediate representation calculation graph. In particular, the optimization pass of the mode rewriting type needs to be learned and developed from scratch. language syntax and dependence on a specific interpreter.

Method used

Provides a compilation method for deep learning models. Through optimized code written based on a common language, a calculation graph indicating the subgraph to be replaced is generated, and matching and replacement are performed to obtain an optimized intermediate representation calculation graph and generate target instructions for processor execution. , simplifying the optimization process and reducing learning costs and implementation difficulty.

Benefits of technology

It enables the optimization of intermediate representation calculation graphs without learning a new programming language, simplifies the development of optimization passes, reduces the difficulty of learning and implementation, and improves the average inference speed and memory usage efficiency of deep learning models.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118585196A
    Figure CN118585196A
Patent Text Reader

Abstract

The invention provides a deep learning model compiling method and device, equipment, a medium and a program product, and relates to the technical field of artificial intelligence, in particular to the technical field of deep learning and the like. According to the specific implementation scheme, the compiling method for the deep learning model comprises the steps of generating a first calculation graph for indicating a to-be-replaced sub-graph and a second calculation graph for indicating a replaced sub-graph on the basis of an optimization code of an initial intermediate representation calculation graph for the deep learning model; matching the first calculation graph with the initial intermediate representation calculation graph; and in response to the condition that the initial intermediate representation calculation graph comprises a to-be-replaced sub-graph matched with the first calculation graph, replacing the to-be-replaced sub-graph in the initial intermediate representation calculation graph based on the second calculation graph to obtain an optimized intermediate representation calculation graph, and generating an execution code for the processor to perform deep learning reasoning based on the optimized intermediate representation computational graph, the optimization code being a code written based on a general language.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of artificial intelligence technology, specifically to technical fields such as deep learning, and in particular to a compilation method, device, equipment, medium and program product of a deep learning model. Background Art

[0002] With the development of computer technology and network technology, deep learning models are being used more and more widely, and deep learning models have made breakthrough progress in various fields. In order to facilitate the deployment of deep learning models, hardware devices that can deploy deep learning models are emerging in an endless stream.

[0003] Different hardware devices may correspond to specific programming languages. In order to facilitate the deployment of deep learning models on different hardware devices and improve the efficient use of hardware performance, the deep learning model can be compiled. Summary of the invention

[0004] The present disclosure aims to provide a method, apparatus, device, medium and program product for compiling a deep learning model, which can realize the compilation of a deep learning model at low cost to improve the average inference speed, memory occupancy and / or power consumption of the hardware device of the model.

[0005] According to a first aspect of the present disclosure, a compiling method for a deep learning model is provided, comprising: generating a first computational graph indicating a subgraph to be replaced and a second computational graph indicating a replaced subgraph based on an optimized code for an initial intermediate representation computational graph of the deep learning model; matching the first computational graph with the initial intermediate representation computational graph; and in response to matching the initial intermediate representation computational graph to include a subgraph to be replaced that matches the first computational graph, replacing the subgraph to be replaced in the initial intermediate representation computational graph based on the second computational graph to obtain an optimized intermediate representation computational graph, so as to generate an execution code for a processor to perform deep learning reasoning based on the optimized intermediate representation computational graph, wherein the optimized code is a code written based on a general language.

[0006] According to the second aspect of the present disclosure, a method for compiling a deep learning model is provided, comprising: generating an initial intermediate representation computation graph expressing the deep learning model based on the loaded deep learning model; processing the initial intermediate representation computation graph using the method provided in the first aspect of the present disclosure to obtain an optimized intermediate representation computation graph; and generating target instructions for a processor to perform deep learning reasoning based on the optimized intermediate representation computation graph.

[0007] According to a third aspect of the present disclosure, a compilation device for a deep learning model is provided, comprising: a graph generation module, for generating, based on an optimized code for an initial intermediate representation computation graph of the deep learning model, a first computation graph indicating a subgraph to be replaced and a second computation graph indicating a replaced subgraph; a graph matching module, for matching the first computation graph with the initial intermediate representation computation graph; and a graph replacement module, for replacing, based on the second computation graph, the subgraph to be replaced in the initial intermediate representation computation graph in response to matching the initial intermediate representation computation graph to include a subgraph to be replaced that matches the first computation graph, to obtain an optimized intermediate representation computation graph, so as to generate an execution code for a processor to perform deep learning reasoning based on the optimized intermediate representation computation graph, wherein the optimized code is a code written based on a general language.

[0008] According to the fourth aspect of the present disclosure, a compilation device for a deep learning model is provided, comprising: a graph generation module, for generating an initial intermediate representation calculation graph expressing the deep learning model based on a loaded deep learning model; a graph processing module, for processing the initial intermediate representation calculation graph using the device provided by the fourth aspect of the present disclosure to obtain an optimized intermediate representation calculation graph; and an instruction generation module, for generating target instructions for a processor to perform deep learning reasoning based on the optimized intermediate representation calculation graph.

[0009] According to a fifth aspect of the present disclosure, an electronic device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the compilation method of the deep learning model provided by the present disclosure.

[0010] According to a sixth aspect of the present disclosure, a non-transitory computer-readable storage medium storing computer instructions is provided, wherein the computer instructions are used to enable a computer to execute the compilation method of the deep learning model provided by the present disclosure.

[0011] According to a seventh aspect of the present disclosure, a computer program product is provided, comprising a computer program / instruction, wherein the computer program / instruction is stored on at least one of a readable storage medium and an electronic device, and wherein the computer program / instruction, when executed by a processor, implements the compilation method of the deep learning model provided by the present disclosure.

[0012] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] The accompanying drawings are used to better understand the present solution and do not constitute a limitation of the present disclosure.

[0014] Figure 1 It is a schematic diagram of an application scenario of a method and device for compiling a deep learning model according to an embodiment of the present disclosure;

[0015] Figure 2 is a flowchart of a method for compiling a deep learning model according to the first embodiment of the present disclosure;

[0016] Figure 3 is a schematic diagram of the principle of matching the first computation graph and the initial intermediate representation computation graph according to an embodiment of the present disclosure;

[0017] Figure 4 is a schematic diagram of the principle of roughly matching the first computation graph and the initial intermediate representation computation graph according to an embodiment of the present disclosure;

[0018] Figure 5 is a schematic diagram of an implementation principle of a method for compiling a deep learning model according to a second embodiment of the present disclosure;

[0019] Figure 6 is a flowchart of a method for compiling a deep learning model according to a third embodiment of the present disclosure;

[0020] Figure 7 is a structural block diagram of a compiling device for a deep learning model according to a first embodiment of the present disclosure;

[0021] Figure 8 is a structural block diagram of a compiling device for a deep learning model according to a second embodiment of the present disclosure; and

[0022] Fig. 9 It is a block diagram of an electronic device used to implement the compilation method of the deep learning model of the embodiment of the present disclosure. DETAILED DESCRIPTION

[0023] The following is a description of exemplary embodiments of the present disclosure in conjunction with the accompanying drawings, including various details of the embodiments of the present disclosure to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present disclosure. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.

[0024] For ease of understanding, the technical terms that may be involved in the embodiments of the present disclosure will be explained below.

[0025] Compilation: refers to the process of using a compiler to generate target code from a source program written in a source language. The target code can be based on a language representation between a high-level language (such as Java, Python, C++, etc.) and a machine language. The target code can also be an executable binary machine code. In simple terms, compilation is the conversion of code written in a high-level language into a target code that is closer to a machine language.

[0026] Intermediate Representation (IR): is an internal representation of the source program. The role of the intermediate representation is to make the structure of the compiled program logically simpler and clearer, especially to make the optimization of the target code easier to achieve. The complexity of the intermediate representation is between the source program language and the machine language. IR can represent the structure and calculation process of the deep learning model in the form of a computational graph.

[0027] Code optimization: refers to performing multiple equivalent transformations on a program so that more effective target code can be generated when triggered by the transformed program. Equivalence here means not changing the program's running results. Effectiveness here means shortening the target code's running time and reducing the storage space occupied by the target code when it is running.

[0028] Optimization Pass: It is an important part of the compilation framework. Optimization Pass analyzes and modifies the intermediate representation. During the code optimization process, multiple optimization passes analyze and modify the intermediate representation, and each optimization pass completes a specific optimization task.

[0029] Operator fusion: By analyzing and optimizing the existing graph logic, the original calculation logic is reorganized to form a fused sub-graph logic. Operator fusion can significantly reduce the execution overhead of operators while improving the utilization of device resources.

[0030] Constant Folding: It is an optimization method in traditional compilers. The principle is that if all the inputs that a calculation depends on are constants, the calculation result can be obtained during compilation.

[0031] Dead Code Elimination: A compilation optimization technique. Its purpose is to remove code that has no effect on the program execution results. Removing this part of the code can not only reduce the length of the program and increase the readability of the code, but also prevent the program from performing unnecessary operations, improve the code running efficiency, save unnecessary resource allocation, and optimize the storage space usage.

[0032] Depth-First-Search is an important graph traversal algorithm used to explore nodes and edges in a graph. Basic principle: DFS is a recursive or stack data structure algorithm used for graph traversal. It starts from a starting node, goes as deep as possible into the branch of the graph until it can no longer go deeper, then backtracks and explores other branches. The algorithm avoids repeated visits by marking visited nodes. The process of the DFS algorithm can be: 1. Select the starting node as the point to start the exploration; 2. Mark the starting node as visited to prevent repeated visits; 3. Starting from the current node, select an adjacent node for exploration. If the adjacent node has not been visited, mark it and add it to the list of nodes to be visited; 4. Repeat step 3 to go as deep as possible until the target node is reached or it can no longer go further; 5. When it is impossible to go deeper, backtrack to the last node that has not been fully explored and select another adjacent node to explore; 6. Repeat steps 3 to 5 until the entire graph is traversed or the target node is found.

[0033] Breadth First Search algorithm, also known as breadth first search. Breadth First Search uses the queue as its core. Its search core starts from the starting node, finds a legal and feasible point that can be reached in one step, adds it to the queue, and then pops the starting node. The search operation is performed on the nodes in the queue in turn until the queue is empty.

[0034] Topological sorting rules: First, explain the concepts of in-degree and out-degree. If the arrow points to the vertex, the in-degree of the vertex is +1. If the arrow points out, the out-degree is +1. Topological sorting is to find out the nodes with in-degree 0 one by one. For example, first put the nodes with in-degree 0 into the sequence. Then erase the nodes in the sequence and the arrows pointing out, and continue to query the nodes with in-degree 0 to put them in the sequence. By analogy, the topological sorting of the computational graph can be obtained. It should be noted that the result of topological sorting is not unique, because among the nodes to be sorted, there may be multiple nodes with in-degree 0 at the same time. For multiple nodes with in-degree 0, the sequence can be sorted in any order. Topological sorting algorithms can be used to determine whether a graph is a directed acyclic graph, and to detect whether there are loops in a directed graph.

[0035] In deep learning frameworks, intermediate representations often correspond to a set of optimization passes. By using optimization passes to optimize IR, the performance indicators of the model can be improved, such as increasing the average inference speed of the model, reducing the model's memory usage or power consumption on specific devices, etc. Some optimization passes (such as those used to fuse operators) are typical directed acyclic graph (DAG) to DAG pattern rewrite types. These optimization passes account for the vast majority of optimization passes used in IR optimization. The implementation is not only cumbersome and complex, but also has high development and maintenance costs.

[0036] In the related technologies, deep learning basic platforms including Multi-Level Intermediate Representation (MLIR), Pytorch (open source Python machine learning library), TensorFlow (end-to-end open source machine learning platform), etc. all have means to implement pattern rewriting type Pass. For example, MLIR provides a declarative rule rewriting (DRR) solution to simplify the development of Pass. However, the implementation of this solution depends on the self-developed Python-like development language, which needs to be compiled with the corresponding interpreter to achieve simplified development of optimized Pass. These methods have the problem of needing to learn the self-developed development language syntax from scratch, and the learning cost is high. And because it needs to rely on the supporting interpreter, the implementation is difficult and the implementation process is complicated.

[0037] In order to solve the problems existing in the traditional technology, the present disclosure provides a compilation method, device, equipment, medium and program product of a deep learning model. Figure 1 The application scenarios of the method and device provided by the present disclosure are described.

[0038] Figure 1 It is a schematic diagram of an application scenario of a method and device for compiling a deep learning model according to an embodiment of the present disclosure.

[0039] like Figure 1 As shown, the application scenario 100 may include a host 110 and a device 120. The host 110 and the device 120 may communicate via a network. For example, the host 110 may be implemented based on a processor, and the device 120 may be implemented based on a graphics card. The graphics card may be integrated with a processor, a video memory, a circuit board, and other application-specific integrated circuits. The processor in the graphics card may be a graphics processor, or a neural processing unit, etc., which is not limited in the present disclosure.

[0040] In the application scenario 100, the host end 110 and the device end 120 may be integrated into the same electronic device, and the electronic device may implement deep learning reasoning based on the host end and the device end. For example, various applications may be integrated into the electronic device, such as image processing applications, translation applications, and text and image applications, and at least some of these applications may provide business services based on deep learning models.

[0041] For example, those applications that provide business services based on deep learning models can rely on the host end 110 and the device end 120 to use the deep learning model for deep learning reasoning. For example, the host end 110 can be used to compile the deep learning model 101 loaded from the server end 130 to obtain target instructions that can be executed by the host end 110 and the device end 120.

[0042] For example, the host end 110 can send the instructions 102 that can be executed in parallel among the compiled target instructions to the device end 120, so that the processor of the device end executes the instructions 102. At the same time, the processor of the host end 110 can execute the instructions that need to be executed serially among the target instructions. By executing the target instructions, the deep learning reasoning of the deep learning model 101 can be realized.

[0043] For example, the device end 120 may store the processing result 103 obtained by the processor executing the instruction 102 in the video memory, and the host end 110 may read the processing result 103 from the video memory, and obtain the inference result of the deep learning inference based on the processing result and the processing result obtained by the processor of the host end 110 executing the instruction that needs to be executed serially. For example, the inference result may be a predicted image category, an image generated based on text, etc., which is not limited in the present disclosure.

[0044] For example, during the process of compiling the deep learning model 101 by the host side 110, the optimization pass can be used to optimize the intermediate representation calculation graph expressing the deep learning model, and the optimized intermediate representation calculation graph can be compiled to obtain the target instructions for execution by the host side 110 and / or the device side 120. In this way, the memory access overhead can be reduced when executing the target instructions for deep learning reasoning, and the reasoning performance and reasoning efficiency can be improved. For example, for the device side 120, the access overhead to the video memory can be reduced.

[0045] In one embodiment, the host side 110 may also directly load the target instructions from the server side, and the server side compiles the deep learning model 101, and uses the optimization pass to optimize the intermediate representation calculation graph during the compilation process, and compiles the optimized intermediate representation calculation graph to obtain the target instructions for the host side 110 to call.

[0046] In one embodiment, when training a deep learning model, the server 130 or other servers communicating with the server 130 may also compile the deep learning model to perform deep learning reasoning based on the compiled target instructions, and perform supervised, semi-supervised or unsupervised training on the deep learning model based on the results of the deep learning reasoning.

[0047] It should be noted that the compilation method of the deep learning model provided by the present disclosure can be executed by the host end 110, or by the server end 130 or other servers connected to the server end 130 in communication. Accordingly, the compilation device of the deep learning model provided by the present disclosure can be set in the host end 110, or in the server end 130 or other servers connected to the server end 130 in communication.

[0048] The following will be combined Figure 2~Figure 6 The compilation method of the deep learning model provided in the present disclosure is described in detail.

[0049] Figure 2 It is a flowchart of a method for compiling a deep learning model according to the first embodiment of the present disclosure.

[0050] like Figure 2 As shown, the deep learning model compilation method 200 of this embodiment may include operations S210 to S230.

[0051] In operation S210, based on the optimized code of the initial intermediate representation computation graph of the deep learning model, a first computation graph indicating a subgraph to be replaced and a second computation graph indicating a replaced subgraph are generated.

[0052] According to an embodiment of the present disclosure, the optimization code may be input by a developer based on an application programming interface (Application Programming Interface, referred to as API). The optimization code may be a code written in a general language, for example, a general language may be C++, Java, Python, etc. The optimization code may indicate the subgraph to be replaced and the subgraph after replacement by calling a function in a DRR primitive written in the general language. Specifically, the optimization code may be a code written in a DRR primitive in a general language.

[0053] In this embodiment, a first computational graph may be generated based on the code indicating the subgraph to be replaced included in the optimization code, and a second computational graph may be generated based on the code indicating the replaced subgraph included in the optimization code. It should be noted that the computational graph includes nodes and edges, and nodes may represent data (such as tensors) or operators (Op for short), and edges represent the connection relationship between nodes, which may specifically indicate the data flow in the deep learning model.

[0054] For example, operation S210 can be understood as a process of parsing the optimized code. The implementation principle of the parsing process is somewhat similar to the principle of networking by the compiler through the Python interface in the static graph mode. For example, after obtaining the optimized code, the corresponding operator object can be created according to the operator name passed in the optimized code. When the operator object is passed in the Tensor object, the corresponding operator node will be created in the pattern graph, and the connection relationship between the operator node and the Tensor object will be established, thereby generating a calculation graph.

[0055] According to an embodiment of the present disclosure, the initial intermediate representation calculation graph can be understood as an IR calculation graph, namely, Graph IR. The IR calculation graph is generated based on the source language of the deep learning model. The source language of the deep learning model can be, for example, a code written in C language, C++ or Java.

[0056] In operation S220 , the first computation graph is matched with the initial intermediate representation computation graph.

[0057] According to an embodiment of the present disclosure, a depth-first-search algorithm or a breadth-first-search algorithm may be used to traverse the first computation graph and the initial intermediate representation computation graph, and a match is performed between the nodes in the first computation graph obtained through traversal and the nodes in the initial intermediate representation computation graph during the traversal process. If the names of the two nodes match, it is determined that the two nodes have a mapping relationship. If the node sequence of the traversed initial intermediate representation computation graph includes the node sequence of the first computation graph, for example, if the node sequence of the traversed first computation graph is a subsequence of the node sequence of the traversed initial intermediate representation computation graph, it can be determined that there is a subgraph in the initial intermediate representation computation graph that matches the first computation graph. Otherwise, it is determined that there is no subgraph in the initial intermediate representation computation graph that matches the first computation graph. In the case of a matched subgraph, the matched subgraph can be used as a subgraph to be replaced.

[0058] In operation S230, in response to matching the subgraph to be replaced that matches the first computation graph in the initial intermediate representation computation graph, the subgraph to be replaced in the initial intermediate representation computation graph is replaced based on the second computation graph to obtain an optimized intermediate representation computation graph.

[0059] For example, the first computation graph and the second computation graph generated in this embodiment may be computation graphs represented by IR. In this embodiment, the subgraph to be replaced in the initial intermediate representation computation graph may be deleted, and the second computation graph may be added to the position of the subgraph to be replaced, thereby replacing the subgraph to be replaced.

[0060] For example, it is also possible to start from the input node of the second computation graph, traverse the second computation graph, and construct an IR computation graph corresponding to the second computation graph. Then, the IR computation graph is connected to the position between the input node and the output node of the subgraph to be replaced, and the input node and the output node in the constructed IR computation graph are used to replace the input node and the output node of the subgraph to be replaced respectively. Then, all nodes and edges between the input node and the output node in the subgraph to be replaced are deleted to complete the replacement of the subgraph to be replaced.

[0061] Through the technical solution of the embodiment of the present disclosure, the optimization of the intermediate representation calculation graph can be realized based on the optimization code of the general language, so that the relevant R&D personnel do not need to learn a new programming language, and the compilation and interpretation process of the optimization code does not need to rely on a specific interpreter. Therefore, compared with the related technology, the learning cost and implementation difficulty of optimizing the intermediate representation calculation graph can be reduced, and the optimization process can be simplified, realizing the simplified development of the optimization pass.

[0062] Figure 3 It is a schematic diagram of the principle of matching the first computation graph and the initial intermediate representation computation graph according to an embodiment of the present disclosure.

[0063] In one embodiment, a function for setting matching constraints may also be provided through an API interface. This allows developers to pass matching constraints that need to be considered in the calculation graph optimization process through an API interface according to actual needs, thereby improving the optimization accuracy of the IR calculation graph. Specifically, when a custom constraint function is included in the optimization code, it can be determined that there are matching constraints. For example, the operator attributes set in the constraint function, the data type and / or dimension information of the Tensor, etc. may be used as matching constraints.

[0064] like Figure 3As shown, in the embodiment 300, when matching the first computation graph and the initial intermediate representation computation graph (i.e., the initial IR computation graph), a rough match between the two can be achieved by traversing the first computation graph 301 and the initial intermediate representation computation graph 302. The subgraph in the initial intermediate representation computation graph determined by the rough match that matches the first computation graph is used as the candidate subgraph 303. Subsequently, the candidate subgraph 303 is checked based on the matching constraint 304. The graph in the candidate subgraph 303 that meets the matching constraint is used as the subgraph to be replaced 305.

[0065] For example, in the rough matching process, the first computation graph and the initial intermediate representation computation graph can be matched based on the names of the edges and nodes included in the first computation graph. That is, in the matched alternative subgraph 303, the name of each node is the same as the name of the corresponding node in the first computation graph, and in the alternative subgraph 303, the connection relationship between the nodes is the same as the connection relationship between the corresponding nodes in the first computation graph. For example, in the process of traversing the first computation graph and the initial intermediate representation computation graph, when matching the nodes in the first computation graph with the nodes in the initial intermediate representation computation graph, the names of the two nodes and the connection relationship between the two nodes and other nodes in their respective computation graphs should be compared. If the comparison results are consistent, it is determined that the two nodes match.

[0066] For example, the properties of operator a may be limited by a custom constraint function in the optimization code. This embodiment may verify the properties of operator a in the alternative subgraph 303. If they are consistent with the properties defined in the custom constraint function, it is determined that the alternative subgraph satisfies the matching constraint condition. Alternatively, the type and / or dimension of the data output by operator b may be limited by a custom constraint function in the optimization code. This embodiment may verify the type and / or attribute of the data represented by the data node pointed to by operator b in the alternative subgraph 303. If they are consistent with the type and / or dimension of the data defined in the custom constraint function, it is determined that the alternative subgraph satisfies the matching constraint condition. It is understandable that the custom constraint function may not only define the properties of the operator, but also define the data type and / or dimension of the operator output, etc., which is not limited in the present disclosure.

[0067] According to an embodiment of the present disclosure, in the rough matching process, any data node in the first computation graph can be used as an anchor point, and the anchor point can be used as the starting node to traverse the first computation graph and the initial intermediate representation computation graph. For example, the output node in the initial intermediate representation computation graph that matches the anchor point can be traversed as the starting node for traversing the initial intermediate representation computation graph. And gradually match the nodes in the traversed first computation graph with the traversed initial intermediate representation computation graph. If the nodes in the two traversed computation graphs match in sequence, and the traversal of the first computation graph is completed, the nodes and edges in the initial intermediate representation computation graph that have been traversed from the starting node can be used to form an alternative subgraph.

[0068] For example, the anchor point can be an output node with an out-degree of zero in the first computation graph, so as to make the implementation principle of the traversal matching process simpler from an engineering perspective, but the present disclosure does not limit this. According to actual needs, the output node with an in-degree of zero in the first computation graph can also be used as an anchor point, or any output node in the first computation graph can be used as an anchor point.

[0069] Through the technical solution of the embodiment of the present disclosure, by performing rough matching based only on node names and connection relationships, and further screening the candidate subgraphs obtained by the rough matching based on matching constraints, the matching efficiency can be improved while ensuring the matching accuracy.

[0070] Figure 4 It is a schematic diagram of the principle of coarsely matching the first computation graph and the initial intermediate representation computation graph according to an embodiment of the present disclosure.

[0071] In one embodiment, when roughly matching the first computation graph and the initial intermediate representation computation graph, for example, a mapping relationship between an output node with an out-degree of zero in the first computation graph and an output node in the initial intermediate representation computation graph can be first established. Subsequently, the two nodes in the mapping relationship are used as starting points for traversing the first computation graph and the initial intermediate representation computation graph, respectively. If the traversal result is that there is a subgraph in the initial intermediate representation computation graph that matches the first computation graph, the matching subgraph is used as an alternative subgraph. In this way, the matching efficiency and matching accuracy of the computation graph are improved, and complex and diverse computation graph matching scenarios can be handled.

[0072] For example, Figure 4 As shown, in embodiment 400, a first search algorithm 410 may be first used to traverse the first computation graph 401 and the initial intermediate representation computation graph 402. During the traversal process, the traversed nodes are matched, and a mapping relationship is established between the output nodes with an out-degree of zero in the first computation graph and the nodes in the matched initial intermediate representation computation graph. The first search algorithm may be, for example, a depth-first search algorithm or a breadth-first search algorithm. Considering that the depth-first search algorithm is implemented based on an easy-to-implement recursive principle, the first search algorithm may preferably be a depth-first search algorithm. It should be noted that for ease of description, Figure 4 The initial intermediate representation computation graph 402 shown in FIG. 4 may be a portion of the entire IR computation graph.

[0073] For example, in embodiment 400, the established mapping relationship may include at least the following mapping relationship combination 403 {(T15, T25), (T16, T26), (T17, T27)}. It should be noted that when matching the traversed nodes during the traversal process, only the names and connection edges of the matched nodes may be compared. Through matching, one or more mapping relationship combinations may be obtained. For example, for the mapping relationship combination 403 {(T15, T25), (T16, T26), (T17, T27)}, it indicates that the output node T15 in the first computation graph 401 matches the output node T25 in the initial intermediate representation computation graph 402, the output node T16 in the first computation graph 401 matches the output node T26 in the initial intermediate representation computation graph 402, and the output node T17 in the first computation graph 401 matches the output node T27 in the initial intermediate representation computation graph 402.

[0074] After obtaining the mapping relationship combination, the two output nodes with the mapping relationship in each combination can be used as the search starting point, and the first calculation graph 401 and the initial intermediate representation calculation graph 402 can be searched and matched using the second search algorithm. For example, for the mapping relationship (T15, T25), T15 can be used as the starting point, and the first calculation graph 401 can be traversed using the second search algorithm, and T25 can be used as the starting point, and the initial intermediate representation calculation graph 402 can be traversed using the second search algorithm, and the traversed nodes and edges are matched in sequence until the node with zero in-degree of the first calculation graph 401 is traversed. If the i-th node in the traversed first calculation graph 401 matches the i-th node in the traversed initial intermediate representation calculation graph 402, i is an integer value greater than or equal to 1 and less than or equal to the total number of nodes in the first calculation graph, then the matching result for the mapping relationship (T15, T25) is determined to be a successful match. Similarly, the mapping relationship (T16, T26) and the mapping relationship (T17, T27) can be traversed and matched. If the matching results of all two output nodes with mapping relationships in the mapping relationship combination are successful matches, then the mapping relationship combination is determined to be a target combination with successful matching.

[0075] Finally, the embodiment 400 can determine, based on the target combination, an alternative subgraph included in the initial intermediate representation computation graph 402 that matches the first computation graph 401. Specifically, a subgraph consisting of nodes and edges in the initial intermediate representation computation graph 402 traversed based on the target combination can be used as an alternative subgraph.

[0076] The second search algorithm may be, for example, a breadth-first search algorithm or a depth-first search algorithm, which is not limited in the present disclosure.

[0077] In one embodiment, the implementation process of the rough matching can first obtain the node with zero degree in the first calculation graph as the anchor point, that is, the starting node of the rough matching. Then, the DFS algorithm is used to traverse the first calculation graph and the initial intermediate representation calculation graph from the anchor point at the same time, and the nodes in the initial intermediate representation calculation graph that each output node with zero degree in the first calculation graph may correspond to are searched. If no corresponding node is found for an output node with zero degree, the rough matching fails. If the corresponding node is found for each output node with zero degree, all mapping combinations of the corresponding relationship of all output nodes are exhaustively enumerated. For each combination, the BFS algorithm is used to start reverse matching from the output node in the mapping combination (for example, only the input edge of the node is looked at, and the output edge of the node is not looked at), and the corresponding relationship between the nodes and edges in the first calculation graph and the nodes and edges in the initial intermediate representation calculation graph is recorded while matching, until all nodes in the calculation graph are matched, the rough matching is confirmed to be successful, and the alternative subgraph of the rough matching project is determined. If all combinations are not matched successfully, it is considered that the matching fails.

[0078] It should be noted that if there are multiple output nodes with zero out-degree in the first computation graph, any output node with zero out-degree can be used as an anchor point. The output node with the smallest out-degree can also be used as an anchor point, which is not limited in the present disclosure.

[0079] In one embodiment, for example, the following method can be used to replace the subgraph to be replaced in the initial intermediate representation calculation graph based on the second calculation graph. First, the nodes included in the second calculation graph are traversed with the input node of the second calculation graph as the starting point. For example, the traversal can be performed according to the topological sorting rule. When the node is traversed, the node construction interface is called to construct the intermediate representation node (i.e., IR node) corresponding to the traversed node. Through the traversal, the construction of all nodes except the input node and the output node in the replaced subgraph is completed. For example, the node construction interface may include an operator construction interface (op interface), which may be provided by the underlying optimization Pass component. Subsequently, based on the output node with an out-degree of zero in the second calculation graph, the tensor replacement interface may be called to replace the output node in the subgraph to be replaced. At this point, the IR subgraph that replaces the subgraph to be replaced can be constructed. Among them, the tensor replacement interface may be a Tensor replacement interface provided by the underlying optimization Pass component. By replacing the output node in the subgraph to be replaced with an output node with an out-degree of zero, it can be ensured that the output data of the replaced subgraph is consistent with the data type, dimension, etc. of the output data specified by the optimization code. In this way, this embodiment can connect the constructed IR subgraph to the position of the subgraph to be replaced in the initial intermediate representation calculation graph.

[0080] Subsequently, the operator deletion interface can be called to delete the subgraph to be replaced in the initial intermediate representation calculation graph. Specifically, the operator deletion interface provided by the underlying optimization Pass component can be called to start from the output node of the subgraph to be replaced, and delete the nodes in the subgraph to be replaced one by one from bottom to top in the opposite direction of the topological sorting rule until the input node of the subgraph to be replaced is traversed.

[0081] Specifically, during the replacement process, the input Tensor of the subgraph to be replaced is maintained unchanged, and the internal structure and output Tensor of the subgraph to be replaced are replaced based on the second computation graph.

[0082] Through the above technical solution of replacing the subgraph to be replaced, the optimization of the DAG to DAG mode rewriting type can be realized for the initial intermediate representation calculation graph, such as operator fusion, etc. Through the above technical solution, some general transformation optimizations can be performed on the initial intermediate representation calculation graph, and the operators can also be normalized to convert the operation operators between function classes and within function classes.

[0083] Figure 5 It is a schematic diagram of the implementation principle of the compilation method of the deep learning model according to the second embodiment of the present disclosure.

[0084] like Figure 5 As shown, in an embodiment 500, after generating a first computation graph indicating a subgraph to be replaced and a second computation graph indicating a subgraph after replacement based on an optimized code through operation S510, the correctness of the directed acyclic graph can be checked on the first computation graph and the second computation graph through operation S540. Only when the correctness check passes, the first computation graph is matched with the initial intermediate representation computation graph through operation S520. Among them, the implementation principles of operation S510 and operation S520 are similar to those of the aforementioned operation S210 and operation S220, respectively, and will not be repeated here.

[0085] According to an embodiment of the present disclosure, a topological sorting algorithm can be used to traverse the first computation graph and the second computation graph to obtain a topological sorting of the first computation graph and the second computation graph, and determine whether each computation graph is a valid directed acyclic graph according to the topological sorting of each computation graph. For example, if it is determined according to the topological sorting that a certain computation graph has a loop, or there are disconnected nodes in the topological sorting, it is determined that the certain computation graph has not passed the correctness check.

[0086] The reason for performing the correctness check of the directed acyclic graph is that in the computational reasoning process of deep learning, data flows in one direction. If the first computational graph or the second computational graph is a cyclic graph, it may indicate that there is an irrationality in the writing logic of the optimization code. If the optimization of the intermediate representation computational graph continues, the target instructions of the machine language obtained by compilation will not be able to perform deep learning reasoning. This embodiment can save computing resources while ensuring the correctness of the computational graph optimization and the correctness of the deep learning model compilation by matching the first computational graph with the initial intermediate representation computational graph only when the first computational graph and the second computational graph pass the correctness check.

[0087] In one embodiment, a function for changing the properties of operator nodes in the initial intermediate representation calculation graph can also be provided through an API interface to obtain, update, delete or insert the properties of the operator calculation, so as to provide various forms of optimizing the initial intermediate representation calculation graph. For example, in the case where a custom node processing function is included in the optimization code, it can be determined that a target operation needs to be performed on the operator nodes in the initial intermediate representation calculation graph, and the target operation can include changing the properties of the operator node, changing the name of the operator node, changing the type and dimension of the input or output data of the operator node, etc., which needs to be determined according to the custom node processing function included in the optimization code.

[0088] For example, Figure 5 As shown, in embodiment 500, when it is determined through the matching of operation S520 that the initial intermediate representation calculation graph includes a subgraph to be replaced that matches the first calculation graph, it can be determined first based on the optimization code whether there is a target operation for the operator node in the initial intermediate representation calculation graph. That is, after operation S520, operation S550 can be performed first to determine whether the initial intermediate representation calculation graph includes a subgraph to be replaced that matches the first calculation graph. If included, operation S560 can be performed to determine whether there is a target operation for the operator node in the initial intermediate representation calculation graph. If not included, the optimization of the initial intermediate representation calculation graph is terminated. If it is determined through operation S560 that there is a target operation for the operator node in the initial intermediate representation calculation graph, operation S570 is performed, otherwise operation S530 can be performed.

[0089] In operation S570, the target operator node targeted by the target operation in the initial intermediate representation calculation graph is determined, and the target operation is performed on the target operator node. Specifically, the target operator node in the initial intermediate representation calculation graph can be located according to the node name passed in by the customized node processing function in the optimization code. Then, the target operation is performed on the target operator node according to the operation information passed in by the customized node processing function. The operation information can be, for example, the changed attribute information, or information indicating the acquisition of attributes, etc., which is not limited in the present disclosure.

[0090] After operation S570 is performed, operation S530 may continue to be performed.

[0091] In operation S530, based on the second computation graph, the subgraph to be replaced in the initial intermediate representation computation graph is replaced to obtain an optimized intermediate representation computation graph. The implementation principle of operation S530 is similar to the implementation principle of operation S230 described above, and will not be repeated here.

[0092] According to the embodiments of the present disclosure, by providing a custom node processing function through an API interface, developers can post-process the operator nodes in the initial intermediate representation calculation graph according to the actual needs of the optimization pass. The efficiency of optimizing the initial intermediate representation calculation graph can be further improved, and the compilation efficiency of the deep learning model can be improved.

[0093] According to the embodiments of the present disclosure, in order to realize the compilation of the deep learning model described above, the present disclosure adopts the idea of ​​layered design, and divides the modules used to realize the compilation method of the deep learning model described above into three layers from top to bottom. The top layer is the user interface layer, which is used to provide developers with an API interface for rewriting DRR using declarative rules. The middle layer is the pattern subgraph representation layer, which can convert the optimized code passed in by the developer through the API interface into an internal Pattern data structure, which can include the aforementioned first calculation graph (which can be understood as Source Pattern) and the second calculation graph (which can be understood as Result Pattern). The bottom layer is the IR matching replacement layer, which is used to match the IR calculation graph according to the first calculation graph obtained in the middle layer. After the match is successful, the second calculation graph is used to replace the subgraph to be replaced.

[0094] Among them, Source Pattern, Result Pattern and Constraints are the three elements of the optimization pass of the pattern rewriting type. The difference between different optimization passes lies in how to express and organize the logic of the three parts in code. The purpose of the embodiment of the present disclosure is to enable developers to rewrite primitives by focusing only on declarative rules based on a common language while ensuring complete functions. That is, the subgraph to be replaced can be specified by Source Pattern, the subgraph after replacement can be specified by ResultPattern, and the matching constraints can be specified by Constraints, thereby realizing a complete optimization pass.

[0095] For example, the declarative rule rewriting primitive should at least include functions of the Source Pattern class and functions of the ResultPattern class. Then the aforementioned first computation graph can be generated based on the functions of the Source Pattern class included in the optimized code, and the aforementioned second computation graph can be generated based on the functions of the Result Pattern class included in the optimized code.

[0096] For example, the aforementioned function for setting matching constraints and the customized node processing function may belong to the SourcePattern class. The declarative rule rewriting primitive may also include, for example, a DRR pattern base class (DrrPatternBase) and a DRR pattern content class (DrrPatternContext), etc., which is not limited in the present disclosure.

[0097] In one embodiment, the declarative rule rewriting primitive may be provided based on the C++ language, for example. Accordingly, the above-mentioned optimized code is a code written based on the declarative rule rewriting primitive of the C++ language.

[0098] The following Table 1 takes the declarative rule rewriting primitive provided by the C++ language as an example, and lists the function class, function, function description and parameter explanation in the declarative rule rewriting primitive.

[0099] Table 1 Examples of declarative rule rewriting primitives

[0100] It can be understood that the above-mentioned declarative rule rewriting primitive is only used as an example to facilitate understanding of the present disclosure, and the present disclosure is not limited to this.

[0101] Based on the deep learning model compilation method described above, another embodiment of the present disclosure also provides another deep learning model compilation method to compile the deep learning model represented by a high-level language into target instructions that can be executed by a processor, so that the processor performs deep learning reasoning based on the target instructions.

[0102] Figure 6 It is a flowchart of a method for compiling a deep learning model according to the third embodiment of the present disclosure.

[0103] like Figure 6 As shown, the deep learning model compilation method 600 of this embodiment may include operations S610 to S630.

[0104] In operation S610 , based on the loaded deep learning model, an initial intermediate representation computation graph expressing the deep learning model is generated.

[0105] According to an embodiment of the present disclosure, a trained deep learning model or a deep learning model that needs to be trained can be loaded from the server. Operation S610 can be performed, for example, by a compiler front end abstracted from a deep learning model compiler, which receives a deep learning model written in a high-level language (e.g., C language, C++ language, or Java language), then performs code generation, and spits out an intermediate representation of the deep learning model in the form of a computational graph. That is, the compiler front end generates an initial intermediate representation computational graph that expresses the deep learning model.

[0106] For example, the artificial intelligence compilation framework TVM (Tensor Virtual Machine) can be used to compile the loaded deep learning model written in a high-level language, so as to obtain the initial intermediate representation calculation graph expressing the deep learning model. It is understandable that the above compilation architecture is only used as an example to facilitate the understanding of the present disclosure, and the present disclosure does not limit this.

[0107] In operation S620, the initial intermediate representation calculation graph is processed to obtain an optimized intermediate representation calculation graph. Operation S620 can use the deep learning model compilation method 200 described above to process the initial intermediate representation calculation graph, which will not be described in detail here.

[0108] In operation S630 , a target instruction for a processor to perform deep learning reasoning is generated based on the optimized intermediate representation computation graph.

[0109] According to an embodiment of the present disclosure, a compiler built based on a low-level virtual machine (LLVM) framework system can be used to compile the optimized intermediate representation calculation graph, thereby obtaining a machine code (i.e., a target instruction) that can be executed on a processor. It is understandable that the compiler of this embodiment is only used as an example to facilitate understanding of the present disclosure, and the present disclosure does not limit this.

[0110] It should be noted that the above-mentioned target instructions can be executed, for example, by a CPU, and / or a processor included in any of the following devices: a graphics card, a neural network chip, to perform deep learning reasoning based on the target instructions.

[0111] Based on the compilation method of the deep learning model provided by the first embodiment of the present disclosure, the present disclosure also provides a compilation device of the deep learning model. Figure 7 The device is described in detail.

[0112] Figure 7 4 is a structural block diagram of a compiling device for a deep learning model according to the first embodiment of the present disclosure.

[0113] like Figure 7As shown, the compiling device 700 of the deep learning model of this embodiment may include a first graph generating module 710, a graph matching module 720 and a graph replacing module 730.

[0114] The first graph generation module 710 is used to generate a first computational graph indicating a subgraph to be replaced and a second computational graph indicating a subgraph after replacement based on an optimized code for an initial intermediate representation computational graph of a deep learning model. The optimized code is a code written in a general language. In one embodiment, the first graph generation module 710 can be used to perform the operation S210 described above, which will not be described in detail here.

[0115] The graph matching module 720 is used to match the first computation graph with the initial intermediate representation computation graph. In one embodiment, the graph matching module 720 can be used to perform the operation S220 described above, which will not be described in detail here.

[0116] The graph replacement module 730 is used to replace the subgraph to be replaced in the initial intermediate representation calculation graph based on the second calculation graph in response to matching the subgraph to be replaced in the initial intermediate representation calculation graph, to obtain an optimized intermediate representation calculation graph, so as to generate an execution code for the processor to perform deep learning reasoning based on the optimized intermediate representation calculation graph. In one embodiment, the graph replacement module 730 can be used to perform the operation S230 described above, which will not be repeated here.

[0117] According to an embodiment of the present disclosure, the compilation device 700 of the deep learning model may further include a verification module for performing a correctness verification of the first computation graph and the second computation graph as directed acyclic graphs, wherein the graph matching module performs matching in response to the first computation graph and the second computation graph passing the correctness verification.

[0118] According to an embodiment of the present disclosure, the graph matching module 720 may include a coarse matching submodule and a fine matching submodule. The coarse matching submodule is used to perform a coarse matching between the first computation graph and the initial intermediate representation computation graph based on the names of the edges and nodes included in the first computation graph. The fine matching submodule is used to determine, in response to the initial intermediate representation computation graph including an alternative subgraph that coarsely matches the first computation graph and the existence of a matching constraint condition determined based on the optimization code, a graph in the alternative subgraph that satisfies the matching constraint condition as a subgraph to be replaced.

[0119] According to an embodiment of the present disclosure, the above-mentioned coarse matching submodule may include an anchor point determination unit and a traversal matching unit. The anchor point determination unit is used to determine any output node in the first computation graph as an anchor point. The traversal matching unit is used to traverse the first computation graph and the initial intermediate representation computation graph with the anchor point as the starting node, and determine the candidate subgraph included in the initial intermediate representation computation graph that is coarsely matched with the first computation graph.

[0120] According to an embodiment of the present disclosure, the traversal matching unit may include a first matching subunit, a second matching subunit and a graph determination subunit. The first matching subunit is used to traverse the first computation graph and the initial intermediate representation computation graph using a first search algorithm, determine the mapping relationship between the output nodes with an out-degree of zero in the first computation graph and the output nodes in the initial intermediate representation computation graph, and obtain at least one mapping relationship combination. The second matching subunit is used to match the first computation graph and the initial intermediate representation computation graph using a second search algorithm for each combination in at least one mapping relationship combination, starting from the two output nodes with a mapping relationship in each combination. The graph determination subunit is used to determine, in response to the presence of a successfully matched target combination in at least one mapping relationship combination, an alternative subgraph included in the initial intermediate representation computation graph that roughly matches the first computation graph based on the target combination.

[0121] According to an embodiment of the present disclosure, the above-mentioned graph replacement module 730 may include a traversal submodule, a node construction submodule, an output replacement submodule and a graph deletion submodule. The traversal submodule is used to traverse the nodes included in the second computational graph with the input node of the second computational graph as the starting point. The node construction submodule is used to call the node construction interface to construct an intermediate representation node corresponding to the traversed node in response to traversing to the node. The output replacement submodule is used to call the tensor replacement interface to replace the output node in the subgraph to be replaced based on the output node with zero out-degree in the second computational graph. The graph deletion submodule is used to call the operator deletion interface to delete the subgraph to be replaced.

[0122] According to an embodiment of the present disclosure, the compilation device 700 of the above-mentioned deep learning model may further include a target operator determination module and an operation execution module. The target operator determination module is used to determine the target operator node in the initial intermediate representation calculation graph for the target operation in response to matching the subgraph to be replaced in the initial intermediate representation calculation graph that matches the first calculation graph, and determining the existence of the target operation for the operator node in the initial intermediate representation calculation graph based on the optimization code. The operation execution module is used to execute the target operation on the target operator node.

[0123] According to an embodiment of the present disclosure, the above-mentioned optimization code includes code written based on a declarative rule rewriting primitive in C++ language. The declarative rule rewriting primitive includes at least a function of a source pattern class and a function of a target pattern class; the first calculation graph is generated based on the function of the source pattern class included in the optimization code, and the second calculation graph is generated based on the function of the target pattern class included in the optimization code.

[0124] Based on the compilation method of the deep learning model provided in the third embodiment of the present disclosure, the present disclosure also provides a compilation device of the deep learning model. Figure 8 The device is described in detail.

[0125] Figure 8 4 is a structural block diagram of a compiling device for a deep learning model according to a second embodiment of the present disclosure.

[0126] like Figure 8 As shown, the compiling device 800 of the deep learning model of this embodiment may include a second graph generating module 810, a graph processing module 820 and an instruction generating module 830.

[0127] The second graph generation module 810 is used to generate an initial intermediate representation calculation graph expressing the deep learning model based on the loaded deep learning model. In one embodiment, the second graph generation module 810 can be used to perform the operation S610 described above, which will not be repeated here.

[0128] The graph processing module 820 is used to process the initial intermediate representation calculation graph using the apparatus provided in the first embodiment of the present disclosure to obtain an optimized intermediate representation calculation graph. In one embodiment, the graph processing module 820 can be used to perform the operation S620 described above, which will not be described in detail here.

[0129] The instruction generation module 830 is used to generate a target instruction for the processor to perform deep learning reasoning based on the optimized intermediate representation calculation graph. In one embodiment, the instruction generation module 830 can be used to perform the operation S630 described above, which will not be repeated here.

[0130] In one embodiment, the processor for performing deep learning reasoning may be a processor included in any of the following devices: a graphics card, a neural network chip.

[0131] It should be noted that the collection, storage, use, processing, transmission, provision, disclosure and application of user personal information involved in the technical solution of this disclosure are in compliance with the provisions of relevant laws and regulations, necessary confidentiality measures have been taken, and they do not violate public order and good morals. In the technical solution of this disclosure, the user's authorization or consent is obtained before obtaining or collecting user personal information.

[0132] According to an embodiment of the present disclosure, the present disclosure also provides an electronic device, a readable storage medium and a computer program product.

[0133] Fig. 9A schematic block diagram of an example electronic device 900 that can be used to implement the compilation method of the deep learning model of an embodiment of the present disclosure is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present disclosure described and / or required herein.

[0134] like Fig. 9 As shown, the device 900 includes a computing unit 901, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 902 or a computer program loaded from a storage unit 908 into a random access memory (RAM) 903. In the RAM 903, various programs and data required for the operation of the device 900 can also be stored. The computing unit 901, the ROM 902, and the RAM 903 are connected to each other via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.

[0135] A number of components in the device 900 are connected to the I / O interface 905, including: an input unit 906, such as a keyboard, a mouse, etc.; an output unit 907, such as various types of displays, speakers, etc.; a storage unit 908, such as a disk, an optical disk, etc.; and a communication unit 909, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 909 allows the device 900 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.

[0136] The computing unit 901 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 901 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 901 performs the various methods and processes described above, such as the compilation method of the deep learning model. For example, in some embodiments, the compilation method of the deep learning model may be implemented as a computer software program, which is tangibly contained in a machine-readable medium, such as a storage unit 908. In some embodiments, part or all of the computer program may be loaded and / or installed on the device 900 via the ROM 902 and / or the communication unit 909. When the computer program is loaded into the RAM 903 and executed by the computing unit 901, one or more steps of the compilation method of the deep learning model described above may be performed. Alternatively, in other embodiments, the computing unit 901 may be configured to execute the compilation method of the deep learning model in any other appropriate manner (e.g., by means of firmware).

[0137] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

[0138] The program code for implementing the method of the present disclosure may be written in any combination of one or more programming languages. These program codes may be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that the program code, when executed by the processor or controller, enables the functions / operations specified in the flow chart and / or block diagram to be implemented. The program code may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.

[0139] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or device, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0140] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).

[0141] The systems and techniques described herein may be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), and the Internet.

[0142] A computer system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The relationship between the client and the server is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. Among them, the server may be a cloud server, also known as a cloud computing server or a cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services ("Virtual Private Server", or "VPS" for short). The server may also be a server of a distributed system, or a server combined with a blockchain.

[0143] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this disclosure can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and this document does not limit this.

[0144] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present disclosure shall be included in the protection scope of the present disclosure.

Claims

1. A compilation method for deep learning models, include: Based on the optimized code for the initial intermediate representation computation graph of the deep learning model, generate a first computation graph indicating the subgraph to be replaced and a second computation graph indicating the subgraph after replacement; Matching the first computation graph with the initial intermediate representation computation graph; as well as In response to matching the subgraph to be replaced in the initial intermediate representation computation graph that matches the first computation graph, based on the second computation graph, the subgraph to be replaced in the initial intermediate representation computation graph is replaced to obtain an optimized intermediate representation computation graph, so as to generate an execution code for a processor to perform deep learning reasoning based on the optimized intermediate representation computation graph, The optimized code is a code written based on a general language.

2. The method according to claim 1, further comprising: include: Performing a directed acyclic graph correctness check on the first computation graph and the second computation graph; The first computation graph and the initial intermediate representation computation graph are matched in response to the first computation graph and the second computation graph passing the correctness check.

3. The method according to claim 1, in, Matching the first computation graph with the initial intermediate representation computation graph includes: Based on the names of the edges and nodes included in the first computation graph, roughly matching the first computation graph with the initial intermediate representation computation graph; and In response to the initial intermediate representation computation graph including an alternative subgraph that roughly matches the first computation graph and the existence of a matching constraint condition determined based on the optimization code, a graph in the alternative subgraph that satisfies the matching constraint condition is determined as the subgraph to be replaced.

4. The method according to claim 3, in, The roughly matching the first computation graph with the initial intermediate representation computation graph based on the nodes included in the first computation graph includes: Determine any output node in the first computation graph as an anchor point; and Taking the anchor point as a starting node, traverse the first computation graph and the initial intermediate representation computation graph, and determine a candidate subgraph included in the initial intermediate representation computation graph that roughly matches the first computation graph.

5. The method according to claim 4, in, Taking the anchor point as the starting node, traversing the first computation graph and the initial intermediate representation computation graph, and determining that the candidate subgraph included in the initial intermediate representation computation graph and roughly matching the first computation graph includes: Using a first search algorithm to traverse the first computation graph and the initial intermediate representation computation graph, determine a mapping relationship between an output node with an out-degree of zero in the first computation graph and an output node in the initial intermediate representation computation graph, and obtain at least one mapping relationship combination; For each of the at least one mapping relationship combination, using a second search algorithm to match the first computation graph and the initial intermediate representation computation graph starting from two output nodes having a mapping relationship in each combination; and In response to the presence of a successfully matched target combination in the at least one mapping relationship combination, a candidate subgraph included in the initial intermediate representation computation graph and roughly matching the first computation graph is determined based on the target combination.

6. The method according to claim 1, in, Based on the second computation graph, replacing the subgraph to be replaced in the initial intermediate representation computation graph includes: Taking the input node of the second computation graph as a starting point, traversing the nodes included in the second computation graph; In response to traversing to a node, calling a node construction interface to construct an intermediate representation node corresponding to the traversed node; Based on the output node with zero out-degree in the second computation graph, calling the tensor replacement interface to replace the output node in the subgraph to be replaced; and Call the operator deletion interface to delete the subgraph to be replaced.

7. The method according to claim 1, further comprising: include: In response to matching the subgraph to be replaced that matches the first computation graph in the initial intermediate representation computation graph, and determining based on the optimization code that there is a target operation for an operator node in the initial intermediate representation computation graph, determining a target operator node targeted by the target operation in the initial intermediate representation computation graph; as well as The target operation is performed on the target operator node.

8. The method according to any one of claims 1 to 7, in: The optimized code is a code written based on the declarative rule rewriting primitive of C++ language; Among them, the declarative rule rewriting primitive includes at least functions of the source pattern class and functions of the target pattern class; the first computation graph is generated based on the functions of the source pattern class included in the optimized code, and the second computation graph is generated based on the functions of the target pattern class included in the optimized code.

9. A compilation method for a deep learning model, include: Based on the loaded deep learning model, generate an initial intermediate representation computation graph expressing the deep learning model; Processing the initial intermediate representation computation graph using the method described in any one of claims 1 to 8 to obtain an optimized intermediate representation computation graph; and Based on the optimized intermediate representation computation graph, target instructions for the processor to perform deep learning reasoning are generated.

10. The method according to claim 1 or 9, in: The processor is a processor included in any of the following devices: a graphics card, a neural network chip.

11. A compilation device for a deep learning model, include: A graph generation module, configured to generate a first computational graph indicating a subgraph to be replaced and a second computational graph indicating a subgraph after replacement based on an optimized code for an initial intermediate representation computational graph of a deep learning model; A graph matching module, used to match the first computation graph with the initial intermediate representation computation graph; as well as A graph replacement module is configured to, in response to matching the subgraph to be replaced in the initial intermediate representation computation graph and including the subgraph to be replaced that matches the first computation graph, replace the subgraph to be replaced in the initial intermediate representation computation graph based on the second computation graph to obtain an optimized intermediate representation computation graph, so as to generate an execution code for a processor to perform deep learning reasoning based on the optimized intermediate representation computation graph, The optimized code is a code written based on a general language.

12. The device according to claim 11, further comprising: include: A verification module, used to verify the correctness of the directed acyclic graph of the first calculation graph and the second calculation graph; The graph matching module performs matching in response to the first computation graph and the second computation graph passing the correctness check.

13. The device according to claim 11, in, The graph matching module comprises: a coarse matching submodule, configured to perform coarse matching between the first computation graph and the initial intermediate representation computation graph based on the names of the edges and nodes included in the first computation graph; and A fine matching submodule is used to determine, in response to the initial intermediate representation computation graph including an alternative subgraph that roughly matches the first computation graph and the existence of a matching constraint condition determined based on the optimization code, a graph in the alternative subgraph that satisfies the matching constraint condition as the subgraph to be replaced.

14. The device according to claim 13, in, The coarse matching submodule comprises: an anchor point determination unit, configured to determine any output node in the first computation graph as an anchor point; and A traversal matching unit is used to traverse the first computation graph and the initial intermediate representation computation graph with the anchor point as a starting node, and determine a candidate subgraph included in the initial intermediate representation computation graph that roughly matches the first computation graph.

15. The device according to claim 14, in, The traversal matching unit comprises: A first matching subunit is used to traverse the first computation graph and the initial intermediate representation computation graph by using a first search algorithm to determine a mapping relationship between an output node with an out-degree of zero in the first computation graph and an output node in the initial intermediate representation computation graph, and obtain at least one mapping relationship combination; a second matching subunit, configured to match the first computation graph and the initial intermediate representation computation graph by using a second search algorithm starting from two output nodes having a mapping relationship in each combination for each of the at least one mapping relationship combination; and The graph determination subunit is used to determine, in response to the presence of a successfully matched target combination in the at least one mapping relationship combination, a candidate subgraph included in the initial intermediate representation calculation graph that roughly matches the first calculation graph based on the target combination.

16. The device according to claim 11, in, The graph replacement module comprises: A traversal submodule, used to traverse the nodes included in the second computation graph with the input node of the second computation graph as a starting point; A node construction submodule, for, in response to traversing to a node, calling a node construction interface to construct an intermediate representation node corresponding to the traversed node; an output replacement submodule, configured to call a tensor replacement interface to replace output nodes in the subgraph to be replaced based on output nodes with an out-degree of zero in the second computation graph; and The graph deletion submodule is used to call the operator deletion interface to delete the subgraph to be replaced.

17. The device according to claim 11, further comprising: include: a target operator determination module, configured to determine a target operator node in the initial intermediate representation computation graph targeted by the target operation in response to matching the subgraph to be replaced in the initial intermediate representation computation graph that matches the first computation graph, and determining based on the optimization code that there is a target operation for an operator node in the initial intermediate representation computation graph; as well as An operation execution module is used to execute the target operation on the target operator node.

18. The device according to any one of claims 11 to 17, in: The optimized code includes rewriting the code written in primitive based on the declarative rules of C++ language; Among them, the declarative rule rewriting primitive includes at least functions of the source pattern class and functions of the target pattern class; the first computation graph is generated based on the functions of the source pattern class included in the optimized code, and the second computation graph is generated based on the functions of the target pattern class included in the optimized code.

19. A compilation device for a deep learning model, include: A graph generation module, used to generate an initial intermediate representation computation graph expressing the deep learning model based on the loaded deep learning model; A graph processing module, configured to process the initial intermediate representation computation graph using the apparatus described in any one of claims 11 to 18 to obtain an optimized intermediate representation computation graph; and An instruction generation module is used to generate target instructions for the processor to perform deep learning reasoning based on the optimized intermediate representation calculation graph.

20. The device according to claim 11 or 19, in: The processor is a processor included in any of the following devices: a graphics card, a neural network chip.

21. An electronic device, include: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute any one of claims 1 to 10.

22. A non-transitory computer-readable storage medium storing computer instructions, in, The computer instructions are used to cause the computer to execute the method according to any one of claims 1 to 10.

23. A computer program product, comprising a computer program / instruction, wherein the computer program / instruction is stored on at least one of a readable storage medium and an electronic device, and when the computer program / instruction is executed by a processor, the steps of the method according to any one of claims 1 to 10 are implemented.

Citation Information

Cited By

  • Joint compilation optimization method and device for heterogeneous computing and medium

    CN118819543A

  • Compilation optimization method for deep learning model with partial parameters as constants

    CN119336334A

  • Compilation optimization method for deep learning models with some constant parameters

    CN119336334B

  • Cross-platform automatic code generation method and device, equipment, medium and product

    CN119336336A

  • Code updating method and device, electronic equipment, chip and storage medium

    CN119883328A