Sorting method, storage medium and electronic equipment
By obtaining the global depth of the directed acyclic graph and optimizing the node execution order through heuristic sorting, the problem of the edge dependency between nodes not being considered in the existing technology is solved, and the computing efficiency and storage resource utilization of the artificial intelligence network model are improved.
Patent Information
- Application Number
- CN202410302884.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-15
- Publication Date
- 2025-09-16
AI Technical Summary
When sorting the computational graphs of artificial intelligence network models, existing algorithms fail to effectively consider the global edge dependencies between nodes, resulting in increased data residency and hardware storage resource overhead and reduced computing efficiency.
By obtaining the global depth and input-output relationship of nodes in a directed acyclic graph, a global depth table is generated. The global depth of the nodes is updated in combination with a heuristic sorting set, the node execution order is optimized, a node sorting table is generated, and hardware memory usage is reduced.
It improves the computational efficiency of artificial intelligence network models, reduces the use of hardware storage resources, and ensures the compiler's sorting consistency before and after computational graph simplification, merging, and transformation.
Smart Images

Figure CN120654732A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of data processing technology and relates to a sorting method, a storage medium and an electronic device. Background Art
[0002] A directed acyclic graph (DAG) is a graph structure consisting of nodes and directed edges, with no paths forming cycles. Each node in a DAG represents an element, and directed edges represent directed relationships from one element to another. Currently, DAGs are widely used in many fields, including database design, digital signal processing, algorithm compilation, and artificial intelligence (AI).
[0003] Taking the artificial intelligence network model as an example, the artificial intelligence network model is essentially a computation graph, which is a type of directed acyclic graph. In the computation graph of the artificial intelligence network model, each node corresponds to an operation (such as Figure 1 The splitting, convolution, etc. shown in the figure) and the edges correspond to the data transmission (data input / output) relationship between nodes.
[0004] After the artificial intelligence model network calculation graph is introduced, the nodes in the calculation graph need to be sorted to determine the order of operation of each node. A variety of sorting algorithms are provided in the prior art, such as the Depth-First Search (DFS) algorithm and the Breadth-First Search (BFS) algorithm. However, these algorithms often only consider the edges that the current node depends on when sorting, without considering the dependency relationship of the nodes on the edges in the entire calculation graph. When the dependency relationship of the edges in a calculation graph is more complex, the "edge" span between adjacent nodes after sorting by the existing algorithm is larger, that is, the "data" correlation between adjacent operations is low, which will cause data to reside, increase the resource overhead of hardware storage memory, and further may cause memory migration, thereby reducing the computing efficiency of the entire network. Summary of the Invention
[0005] The purpose of this application is to provide a sorting method, a storage medium and an electronic device for improving the computational efficiency of a network model.
[0006] In a first aspect, an embodiment of the present application provides a sorting method, which includes: obtaining a directed acyclic graph to be sorted, and recording the input nodes and / or output nodes of each node in the directed acyclic graph; obtaining the global depth of each node to generate a global depth table based on the input and output relationship between the nodes in the directed acyclic graph, wherein the global depth of each node is the number of nodes involved from the node in the directed acyclic graph along the directed edge to the output corresponding to the node; obtaining the execution order of each node based on the global depth of each node and the total number of nodes included in the directed acyclic graph, and generating a node sorting table.
[0007] In an implementation of the first aspect, obtaining the global depth of a node in the directed acyclic graph includes: for any node in the directed acyclic graph, starting from the node, searching for the output nodes of each node step by step downward along the output direction until it is empty, and the global depth of the node is N+1, where N is the number of all nodes found.
[0008] In an implementation manner of the first aspect, for any node in the directed acyclic graph, the execution order of the node is the difference between the total number of nodes included in the directed acyclic graph and the global depth of the node.
[0009] In an implementation of the first aspect, after obtaining the global depth of each node, the sorting method further includes: determining whether there is a heuristic sorting set; if there is the heuristic sorting set, updating the global depth of the corresponding node in the directed acyclic graph according to the calculation order dependency relationship between the nodes in the heuristic sorting set, so that the execution order relationship between the corresponding nodes in the directed acyclic graph is the same as the execution order relationship between the corresponding nodes in the heuristic sorting set.
[0010] In an implementation of the first aspect, the heuristic sorting set includes a previous sorting result corresponding to the directed acyclic graph and / or a preset calculation order between nodes.
[0011] In an implementation of the first aspect, after obtaining the execution order of each node and generating a node sorting table, the sorting method also includes: searching the node sorting table to confirm whether there are nodes with the same execution order; if there are nodes with the same execution order, updating the execution order of the nodes according to the interval sorting principle so that the execution order of each node is different; wherein, the interval sorting principle includes: when a node depends on multiple nodes, the fewer data dependencies the dependent nodes have in the directed acyclic graph, the later the order will be in the interval sorting.
[0012] In an implementation of the first aspect, searching the node sorting table includes searching for nodes corresponding to each execution order in order from largest to smallest execution order in the node sorting table.
[0013] In an implementation of the first aspect, corresponding to each execution order, when two nodes with the same execution order are found, the search is paused, and the execution order of the two nodes with the same execution order is updated according to the interval sorting principle.
[0014] In an implementation of the first aspect, updating the execution order of the two nodes with the same execution order according to the interval sorting principle includes: determining the interval start point and interval end point of each of the two nodes with the same execution order; and updating the execution order of the two nodes with the same execution order according to the size relationship between the number of nodes included in the intervals corresponding to the two nodes with the same execution order.
[0015] In an implementation of the first aspect, determining the interval start point and interval end point of each of the two nodes with the same execution order includes: if the two nodes have an intersection node in the input direction, then the intersection node closest to the two nodes in the input direction is used as the interval start point of each of the two nodes, otherwise the input corresponding to the two nodes is used as the respective interval start point; if the two nodes have an intersection node in the output direction, then the intersection node closest to the two nodes in the output direction is used as the interval end point of each of the two nodes, otherwise the two nodes themselves are used as the respective interval end points.
[0016] In an implementation of the first aspect, updating the execution order of the two nodes with the same execution order according to the size relationship between the numbers of nodes included in the intervals corresponding to the two nodes includes: if the intervals corresponding to the two nodes include different numbers of nodes, then the node corresponding to the interval with fewer nodes is used as the next node of the other node; if the intervals corresponding to the two nodes include the same number of nodes, then selectively adjusting the intervals corresponding to the two nodes according to the starting points of the intervals of the two nodes.
[0017] In an implementation of the first aspect, selectively adjusting the intervals corresponding to the two nodes based on the starting points of the intervals of the two nodes includes: if the starting points of the intervals of the two nodes are the intersection nodes closest to the input direction, adjusting the intervals corresponding to the two nodes respectively; if the starting points of the intervals of the two nodes are the inputs corresponding to each other, randomly updating the execution order of the two nodes.
[0018] In one implementation of the first aspect, adjusting the intervals corresponding to the two nodes includes: taking the inputs corresponding to the two nodes as the starting points of their respective intervals, taking the two nodes themselves as the end points of their respective intervals, and re-updating the execution order of the two nodes based on the size relationship of the number of nodes included in the intervals corresponding to the two nodes.
[0019] In an implementation manner of the first aspect, randomly updating the execution order of the two nodes includes: randomly selecting one of the nodes as the next node of the other node.
[0020] In an implementation of the first aspect, corresponding to each execution order, the search is paused after all nodes corresponding to the execution order are searched, and the execution order of all nodes corresponding to the execution order is updated according to the interval sorting principle.
[0021] In a second aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the sorting method described in any one of the first aspects of the embodiment of the present application is implemented.
[0022] In a third aspect, an embodiment of the present application provides an electronic device, comprising: a memory storing a computer program; and a processor communicatively connected to the memory, for executing any one of the sorting methods described in the first aspect of the embodiment of the present application when the computer program is called.
[0023] In the sorting method provided in the embodiments of the present application, the global depth of each node is obtained based on the input-output relationship between nodes in a directed acyclic graph, and the nodes are sorted based on the global depth of each node and the total number of nodes contained in the directed acyclic graph. This method can improve the data dependencies between the sorted nodes, reduce hardware memory usage, and thus significantly improve the computing efficiency of the entire network.
[0024] Furthermore, embodiments of the present application can update the global depth of corresponding nodes in a directed acyclic graph based on a heuristic sorting set, providing a heuristic sorting function. When using the sorting method provided in embodiments of the present application to sort the computational graph of an artificial intelligence network model, the compiler can ensure consistency in the sorting order before and after each simplification, merging, and transformation of the computational graph. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] Figure 1 Schematic diagram shown as a directed acyclic graph.
[0026] Figure 2A Shown is a structural schematic diagram of an electronic device in an embodiment of the present application.
[0027] Figure 2BShown is a flowchart of the sorting method provided in an embodiment of the present application.
[0028] Figure 3 Shown is a schematic diagram of updating a directed acyclic graph according to a heuristic sorting set in an embodiment of the present application.
[0029] Figure 4 Shown is a flowchart of a sorting method provided in an embodiment of the present application.
[0030] Figure 5A Shown is a schematic diagram of the execution sequence of the nodes obtained according to step S23 in an embodiment of the present application.
[0031] Figure 5B The diagram shows the execution order of each node after the update.
[0032] Figure 6A and Figure 6B Shown is an example diagram of interval sorting in an embodiment of the present application. DETAILED DESCRIPTION
[0033] The following describes the embodiments of the present application through specific examples. Those skilled in the art can easily understand the other advantages and effects of the present application from the content disclosed in this specification. The present application can also be implemented or applied through other different specific embodiments. The details in this specification can also be modified or changed based on different viewpoints and applications without departing from the spirit of the present application. It should be noted that the following embodiments and features in the embodiments can be combined with each other unless they conflict.
[0034] It should be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present application. Therefore, the illustrations only show components related to the present application and are not drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component can be changed at will, and the component layout type may also be more complicated.
[0035] Directed acyclic graphs (DAGs) are widely used in fields such as database design, digital signal processing, algorithm compilation, and artificial intelligence. When applying DAGs in these fields, it is often necessary to sort the nodes within the DAG. For ease of explanation, the following will use the computational graph of an artificial intelligence network model as an example to illustrate DAGs and their sorting, but this is not a limitation.
[0036] An embodiment of the present application provides a sorting method, which can be applied to electronic devices. Figure 2A It is a hardware structure diagram of the electronic device 2. Figure 2A As shown, the electronic device 2 includes at least one processor 21 and a memory 22 .
[0037] The processor 21 may be one or more of a central processing unit (CPU), a neural network processing unit (NPU), a graphics processing unit (GPU), a microprocessor, a microcontroller, a main processor, a controller, and an application specific integrated circuit (ASIC). The processor 21 is configured to execute various types of instructions and operations, such as executing software or firmware programs stored in the memory 22, so that the electronic device 2 provides a variety of functions and services. For example, the processor 21 can execute programs or process data to implement the sorting method provided in the embodiments of the present application.
[0038] In some embodiments, the processor 21 may be a neural network processor for accelerating neural network operations and solving the problem of low efficiency of traditional chips in neural network operations.
[0039] It should be noted that the name of the neural network processor does not constitute a limitation of this application. In other application scenarios, the neural network processor can also be transformed and replaced by other processors with similar functions, such as a tensor processing unit (TPU), a deep learning processing unit (DPU), etc.
[0040] The memory 22 may include a volatile memory, such as a random access memory (RAM) or a cache. The memory 22 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD), or a solid-state drive (SSD). The memory 22 may be used to store program instructions and data for the processor 21 to call and implement the sorting method provided in the embodiment of the present application.
[0041] In some embodiments, the electronic device 2 may also communicate with one or more external devices, such as a terminal, a display, etc. Such communication may be performed, for example, via an input / output (I / O) interface. Furthermore, the electronic device 2 may also communicate with one or more networks via a network adapter.
[0042] In some embodiments, the electronic device 2 may further include a display 23 for displaying a graphical user interface related to the sorting method provided in the embodiment of the present application.
[0043] Figure 2B The flowchart of the sorting method provided in the embodiment of the present application is shown. For example, the method is applied to Figure 2A The processor 21 shown. Figure 2B As shown, the sorting method provided in the embodiment of the present application may include the following steps S21 to S23.
[0044] S21, obtaining a directed acyclic graph to be sorted, and recording the input node and / or output node of each node in the directed acyclic graph. The directed acyclic graph is, for example, a computational graph of an artificial intelligence network, but the present application is not limited thereto.
[0045] In the computational graph of an artificial intelligence network, a directed acyclic graph consists of nodes and directed edges, where nodes represent operators (calculation operations), such as split nodes, convolution nodes, Relu nodes, Softmax nodes, etc. Directed edges (hereinafter referred to as "edges") represent data dependencies between nodes. Two nodes directly connected by an edge have data dependencies. Each node has at least one input edge (input data) and one output edge (output data). For example, for Figure 1 For node 8 in the example, the nodes 3 and 7 connected by its input edges are the input nodes of node 8, and the node 9 connected by its output edges is the output node of node 8; Figure 1 Node 10 in the example has no connected nodes on its output edge, that is, the output node of node 10 is empty, and the nodes 12, 11, and 9 connected by its input edges are the input nodes of node 10.
[0046] S22, according to the input-output relationship between nodes in the directed acyclic graph, obtain the global depth of each node to generate a global depth table, wherein the global depth of each node is the number of nodes involved (included) from the node in the directed acyclic graph along the directed edge to the output corresponding to the node.
[0047] In some embodiments, the global depth of each node can be obtained by traversal: corresponding to any node, starting from this node, the output nodes of each node are searched downward step by step along the output direction until it is empty. The global depth of this node is N+1, where N is the number of all nodes found and 1 is the node itself.
[0048] by Figure 1For example, let's take node 5 in the DAG as an example. The output nodes of node 5 are nodes 6 and 7, and the output nodes of nodes 6 and 7 are nodes 9 and 8 respectively. The output node of node 8 is node 9, and the output node of node 9 is node 10. The output node of node 10 is empty. At this point, the traversal ends. The nodes involved (including) along the directed edge from node 5 in the DAG to the output corresponding to node 5 are node 5 itself, nodes 6 to node 10, and the number of nodes involved is 6. That is, the global depth of node 5 is 6. Similarly, the global depth of any other node can be obtained.
[0049] S23 , obtaining the execution order of each node according to the global depth of each node and the total number of nodes included in the directed acyclic graph, and generating a node sorting table.
[0050] In some embodiments, the execution order of each node can be the difference between the total number of nodes in the directed acyclic graph and the global depth of the node. Figure 1 Take node 5 in the example as an example. The global depth of node 5 is 6. Figure 1 The total number of nodes in the illustrated directed acyclic graph is 12, and the execution order of node 5 is 12-6=6.
[0051] In some embodiments, after obtaining the global depth of each node in the directed acyclic graph in step S22, the sorting method may further include: determining whether there is a heuristic sorting set, and if so, updating the global depth of the corresponding node in the directed acyclic graph based on the computational order dependency between the nodes in the heuristic sorting set, so that the execution order relationship between the corresponding nodes in the directed acyclic graph is the same as the execution order relationship between the corresponding nodes in the heuristic sorting set. Thereafter, in step S23, the execution order of each node can be obtained based on the global depth of the updated node and the total number of nodes included in the directed acyclic graph.
[0052] Among them, the heuristic sorting set includes, for example, the last sorting result corresponding to the directed acyclic graph. It is understandable that the AI compiler may compile the directed acyclic graph multiple times during the compilation process, and each compilation process may simplify, merge, transform, and other operations on the nodes in the directed acyclic graph. In order to ensure the consistency of the relative order between the nodes before and after each compilation, the embodiment of the present application can use the last sorting result (the sorting result before this compilation) as a reference to update the global depth of each node obtained this time.
[0053] Figure 3 The diagram shows the updated directed acyclic graph based on the heuristic sorting set, where the left side is the directed acyclic graph and the right side is the updated directed acyclic graph based on the heuristic sorting set. Figure 3In the directed acyclic graph on the left, taking nodes e and f as an example, the global depths of nodes e and f are the same, both 4 (node e to output involves nodes e, i, l, m, and node f to output involves nodes f, i, l, m). According to the execution order of nodes = the total number of nodes included in the directed acyclic graph - the global depth of the nodes, we can see that the execution order of nodes e and f is the same. Figure 3 In the heuristic sorted set shown, the execution order of node f is after the execution order of node e. At this time, the global depth of the nodes in the directed acyclic graph can be updated according to the compute dependency relationship between the nodes in the heuristic sorted set, so that the execution order relationship (sequential order) between the corresponding nodes in the directed acyclic graph is the same as the execution order relationship between the corresponding nodes in the heuristic sorted set. Similarly, in the heuristic sorted set, the execution order of node c is after the execution order of node b, and the execution order of node j is after the execution order of node i. At this time, it is also necessary to update the global depth of the nodes in the directed acyclic graph according to the compute dependency relationship between the nodes in the heuristic sorted set. Figure 3 The right side is the directed acyclic graph corresponding to the updated global depth table. It can be understood that there is a computation order dependency between nodes e and f in the heuristic sorting, that is, the computation of node f depends on the completion of the computation of node e. Figure 3 In the updated directed acyclic graph shown on the right, the computation order dependencies between nodes can be represented by dotted lines. Figure 3 As shown on the right, after updating according to the heuristic sorted set, the global depth of node e becomes 7 (node e, f, h, i, j, l, m are involved from node e to the output), and the global depth of node f becomes 6 (node f, h, i, j, l, m are involved from node f to the output). At this time, the execution order of node f is after the execution order of node e.
[0054] In other embodiments, the heuristic sorting set may also include a preset computation order between nodes. It is understood that in some specific scenarios, it may be necessary to limit the execution order between certain nodes, such as Figure 3 In the left figure, there is no data input / output relationship between nodes j and c. If you need to limit node j to execute before node c in certain scenarios, you need to adjust the depth of nodes j and c. The specific adjustment method is the same as in the heuristic sorting set and will not be repeated here.
[0055] See also Figure 4In some embodiments, after obtaining the execution order of each node in step S23, the sorting method may further include step S24: searching the node sorting table to determine whether there are nodes with the same execution order. If so, updating (sorting) the execution order of the nodes according to the interval sorting principle so that the execution order of each node is different. The principle of interval sorting is: when a node (operator) depends on multiple nodes, the fewer data dependencies the dependent nodes have in the entire directed acyclic graph, the later they are ranked in the interval sorting.
[0056] In some embodiments, when confirming whether there are nodes with the same execution order, the nodes corresponding to each execution order can be searched one by one to confirm whether there are nodes with the same execution order. Figure 5A As shown, Figure 5A The figure shows the result obtained in step S23. Figure 1 The execution order of each node in the figure is shown in FIG. 1 , where the number in the circle corresponding to each node is the execution order of the node. The depth value marked in the figure is the global depth of each node obtained according to step S22. Figure 5A It can be seen that there are multiple groups of nodes with the same execution order in the directed acyclic graph.
[0057] In order to accurately and without omission search for all nodes with the same execution order, when searching for the nodes corresponding to each execution order one by one, the embodiment of the present application can search for the nodes corresponding to each execution order in order from largest to smallest according to the execution order obtained in step S23. Figure 5A For example, first search for the node with execution order 11. If only one node with execution order 11 is found, then continue searching for the node with execution order 10. Figure 5A It can be seen that it includes 3 nodes with an execution order of 10 (nodes 9, 11, and 12).
[0058] In some embodiments, for each execution order, when two nodes with the same execution order are found, the search may be paused, and the execution order of the two nodes may be updated according to the interval sorting principle. After the update, the search may continue to search for two nodes with the same execution order, and so on, until all execution orders are traversed. For example, when two nodes with execution order 10 are found (e.g., Figure 5A After finding nodes 9 and 11 in the query, the search can be paused and the execution order of nodes 9 and 11 can be adjusted according to the interval sorting principle. After the update, the search continues to see if there are two nodes with the same execution order, and so on, until all execution orders are traversed.
[0059] When applying the interval sorting principle, it is necessary to first determine the interval starting point and interval end point of the two nodes with the same execution order, so as to determine the interval corresponding to each node. Among them, the interval starting point gives priority to the intersection node latestInJunctionNode that is closest to the two nodes in the input direction (closest to the two nodes). When latestInJunctionNode does not exist, the input (input) corresponding to the two nodes can be used as the starting point of each interval. The interval end point gives priority to the intersection node latestOutJunctionNode that is closest to the two nodes in the output direction (closest to the two nodes). When latestOutJunctionNode does not exist, the two nodes themselves are used as the end points of their respective intervals. It can be understood that the interval corresponding to each node must include the node itself.
[0060] After determining the intervals corresponding to the two nodes with the same execution order, the execution order of the nodes can be adjusted based on the number of nodes included in the intervals corresponding to each node. In some embodiments, the execution order of the nodes can be adjusted based on the relative number of nodes included (involved) in the intervals corresponding to each node.
[0061] In some embodiments, adjusting the execution order of nodes based on the number of nodes included in the interval corresponding to each node may include: if the number of nodes included in the interval corresponding to each node is different, then the node corresponding to the interval with a smaller number of nodes is used as the next (execution) node of the node corresponding to the interval with a larger number of nodes, thereby changing the global depth of the node; if the number of nodes included in the interval corresponding to each node is equal, then the interval corresponding to the node is selectively adjusted based on the starting point of the interval of the node.
[0062] Specifically, according to the starting point of the node's interval, the interval corresponding to the node is selectively adjusted, including: if the starting point of the node's interval is the latestInJunctionNode, the nearest intersection node in the input direction, then the interval corresponding to the node is adjusted, and the adjustment method is, for example: the node itself is the end point of the interval, and the input corresponding to the node is used as the starting point of the interval; if the starting point of the node's interval is the input corresponding to the node, the execution order of the nodes can be randomly updated (randomly sorted in order), and the update method is, for example: randomly selecting one of the nodes as the next node of the other node.
[0063] In one embodiment, after adjusting the intervals corresponding to the nodes, if the number of intervals corresponding to the nodes is still the same, the execution order of the nodes can be randomly updated (randomly sorted). Figure 5AFor example, as described above, corresponding to the execution order 10, when two nodes with the same execution order are searched (for example, nodes 9 and 11), the execution order of nodes 9 and 11 can be sorted according to the interval sorting principle. Figure 5A As shown, the latestInJunctionNode of nodes 9 and 11 is node 4, and the latestOutJunctionNode of nodes 9 and 11 is node 10. Therefore, node 4 can be used as the starting point of the interval of nodes 9 and 11, and node 10 can be used as the end point of the interval of nodes 9 and 11. The interval corresponding to node 9 includes nodes 4, 2, 1, 3, 8, 9, and 10, a total of 7 nodes, while the interval corresponding to node 11 includes nodes 4, 11, and 10, a total of 3 nodes. That is, the number of nodes included in the interval corresponding to node 11 is less than the number of nodes included in the interval corresponding to node 9. Therefore, node 11 can be used as the next node of node 9, as shown in Figure 5B As shown by the dotted line in , the execution order of node 9 is reduced by one to 9, and the global depth of node 9 is increased by one to 3. Then, the search continues for the node with an execution order of 10, and so on, until all execution orders are traversed.
[0064] Figures 6A to 6B Shown is an example diagram of interval sorting according to an embodiment of the present application. Figure 6A This is a node execution sequence diagram obtained based on the node global depth table. As shown in the figure, the execution order of nodes 63 and 66 is 5. The latestInJunctionNode corresponding to nodes 63 and 66 is node 61, and the latestOutJunctionNode corresponding to nodes 63 and 66 is node 64. Node 61 is used as the starting point of each interval for nodes 63 and 66, and node 64 is used as the end point of each interval for nodes 63 and 66. It can be seen that the interval corresponding to node 63 includes nodes 61, 62, 63, and 64, that is, the number of nodes included in the interval corresponding to node 63 is M1 = 4. The interval corresponding to node 66 includes nodes 61, 65, 66, and 64, that is, the number of nodes included in the interval corresponding to node 66 is M2 = 4, that is, M1 = M2. At this point, it is necessary to reconfirm the intervals corresponding to nodes 63 and 66, that is, use the input terminals corresponding to nodes 63 and 66 as the starting points of each interval and nodes 63 and 66 themselves as the end points of each interval. That is, the interval starting point of node 63 is Figure 6A The input in the interval is node 63 itself, and the interval corresponding to node 63 includes nodes 61, 62, and 63. That is, the number of nodes included in the interval corresponding to node 63 is M1'=3; the starting point of the interval of node 66 is also Figure 6AIn the input, the interval endpoint is node 66 itself, and the interval corresponding to node 66 includes nodes 61, 65, and 66. That is, the number of nodes included in the interval corresponding to node 66 is M2'=3. In other words, after the interval is re-divided, the number of nodes included in the intervals corresponding to the two nodes is still equal. The order of the two nodes in the graph does not affect the order of the entire graph. Therefore, one of the nodes can be made the next node of the other node. For example, node 63 can be made the next node of node 66. Figure 6B As shown by the dotted line, at this time, the global depth of node 66 increases by one and becomes 3, the execution order of node 66 decreases by one and becomes 4, and the execution order of nodes 63 and 66 is no longer the same.
[0065] In some embodiments, for each execution order, after searching for all nodes corresponding to the execution order, the execution order of all nodes corresponding to the execution order can be sorted according to the application interval sorting principle. For example, after searching for all nodes with an execution order of 10 (e.g., Figure 5A After finding nodes 9, 11, and 12 in the search, the search is paused, and the execution order of these three nodes is adjusted according to the interval sorting principle. The specific method and principle are similar to the above and will not be repeated here.
[0066] According to the above content, it can be seen that an embodiment of the present application provides a sorting method. Compared to the depth-first sorting algorithm, the breadth-first sorting algorithm, etc., the sorting method provided by the embodiment of the present application establishes the data dependency and execution order relationship of each node relative to other nodes in the entire network through the data dependency relationship between nodes and the heuristic algorithm, and then obtains a global depth table containing all nodes, and the depth of each node in the directed acyclic graph in the global depth table is unique. The sorting of all nodes in the directed acyclic graph can be obtained through the global depth table. This sorting method can improve the data dependency relationship between the sorted nodes and reduce the hardware memory usage, thereby improving the computing efficiency of the entire network. In addition, the sorting method provided by the embodiment of the present application, when used to sort the calculation graph of the artificial intelligence network model, can ensure the consistency of the sorting order of the compiler before and after each simplification, merging and transformation of the calculation graph.
[0067] It should be noted that the protection scope of the sorting method provided in the embodiment of the present application is not limited to the execution order of the steps listed in this embodiment. All solutions implemented by adding, subtracting, or replacing steps in the existing technology based on the principles of the present application are included in the protection scope of the present application.
[0068] The embodiment of the present application further provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by the processor, the sorting method provided by the embodiment of the present application is implemented. The above-mentioned storage medium can be any available medium that can be accessed by the computer or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a magnetic tape), an optical medium (e.g., a digital video disc (DVD)), or a semiconductor medium (e.g., a solid state drive (SSD)).
[0069] An embodiment of the present application also provides an electronic device. The electronic device includes a memory and a processor. The memory stores a computer program. The processor is communicatively connected to the memory and executes the sorting method provided in the embodiment of the present application when the computer program is invoked.
[0070] The descriptions of the processes or structures corresponding to the above figures have different emphases. For parts that are not described in detail in a certain process or structure, please refer to the relevant descriptions of other processes or structures.
[0071] The above embodiments are merely illustrative of the principles and effects of this application and are not intended to limit this application. Anyone skilled in the art may modify or alter the above embodiments without departing from the spirit and scope of this application. Therefore, all equivalent modifications or alterations made by one of ordinary skill in the art without departing from the spirit and technical concepts disclosed in this application shall be covered by the claims of this application.
Claims
1. A sorting method, characterized in that: include: Obtain a directed acyclic graph to be sorted, and record the input node and / or output node of each node in the directed acyclic graph; According to the input-output relationship between nodes in the directed acyclic graph, obtaining the global depth of each node to generate a global depth table, wherein the global depth of each node is the number of nodes involved from the node in the directed acyclic graph along the directed edge to the output corresponding to the node; According to the global depth of each node and the total number of nodes included in the directed acyclic graph, the execution order of each node is obtained, and a node sorting table is generated.
2. The sorting method according to claim 1, wherein: Obtaining the global depth of a node in the directed acyclic graph includes: For any node in the directed acyclic graph, the output nodes of each node are searched downward step by step along the output direction from the node until it is empty. The global depth of the node is N+1, where N is the number of all nodes found.
3. The sorting method according to claim 1, wherein: For any node in the directed acyclic graph, the execution order of the node is the difference between the total number of nodes included in the directed acyclic graph and the global depth of the node.
4. The sorting method according to claim 1, wherein: After obtaining the global depth of each node, the sorting method further includes: Determine whether there is a heuristic sorting set; if so, update the global depths of corresponding nodes in the directed acyclic graph according to the computational order dependency between the nodes in the heuristic sorting set, so that the execution order relationship between the corresponding nodes in the directed acyclic graph is the same as the execution order relationship between the corresponding nodes in the heuristic sorting set.
5. The sorting method according to claim 4, characterized in that: The heuristic sorting set includes the last sorting result corresponding to the directed acyclic graph and / or a preset calculation order between nodes.
6. The sorting method according to claim 1, characterized in that After obtaining the execution order of each node and generating a node sorting table, the sorting method further includes: Searching the node sorting table to confirm whether there are nodes with the same execution order; If there are nodes with the same execution order, the execution order of the nodes is updated according to the interval sorting principle so that the execution order of the nodes is different; The interval sorting principle includes: when a node depends on multiple nodes, the fewer data dependencies the dependent nodes have in the directed acyclic graph, the later the node is in the interval sorting order.
7. The sorting method according to claim 6, characterized in that: Searching the node sorting table includes searching the nodes corresponding to each execution order in order from largest to smallest according to the execution order in the node sorting table.
8. The sorting method according to claim 6, characterized in that: Corresponding to each execution order, when two nodes with the same execution order are found, the search is paused, and the execution order of the two nodes with the same execution order is updated according to the interval sorting principle.
9. The sorting method according to claim 8, characterized in that: Updating the execution order of the two nodes with the same execution order according to the interval sorting principle includes: Determine the interval start point and interval end point of each of the two nodes with the same execution order; The execution order of the two nodes with the same execution order is updated according to the size relationship of the number of nodes included in the intervals corresponding to the two nodes with the same execution order.
10. The sorting method according to claim 9, characterized in that: Determining the interval start point and interval end point of each of the two nodes with the same execution order includes: If there is an intersection node between the two nodes in the input direction, the intersection node closest to the two nodes in the input direction is used as the starting point of the interval of each of the two nodes; otherwise, the input corresponding to each of the two nodes is used as the starting point of each interval; If there is an intersection node between the two nodes in the output direction, the intersection node closest to the two nodes in the output direction is used as the end point of the interval of each of the two nodes; otherwise, the two nodes themselves are used as the end point of each interval.
11. The sorting method according to claim 9, characterized in that: Updating the execution order of the two nodes with the same execution order according to the size relationship of the number of nodes included in the intervals corresponding to the two nodes with the same execution order includes: If the intervals corresponding to the two nodes have different numbers of nodes, the node corresponding to the interval with fewer nodes is used as the next node of the other node; If the intervals corresponding to the two nodes include the same number of nodes, the intervals corresponding to the two nodes are selectively adjusted according to the starting points of the intervals of the two nodes.
12. The sorting method according to claim 11, characterized in that: Selectively adjusting the intervals corresponding to the two nodes according to the interval starting points of the two nodes includes: If the starting point of the interval of the two nodes is the nearest intersection node in the input direction, the interval corresponding to the two nodes is adjusted; If the interval starting points of the two nodes are their respective corresponding inputs, the execution order of the two nodes is randomly updated.
13. The sorting method according to claim 12, characterized in that: Adjusting the intervals corresponding to the two nodes includes: taking the inputs corresponding to the two nodes as the starting points of their respective intervals, taking the two nodes themselves as the end points of their respective intervals, and re-updating the execution order of the two nodes based on the size relationship of the number of nodes included in the intervals corresponding to the two nodes.
14. The sorting method according to claim 12, characterized in that: Randomly updating the execution order of the two nodes includes randomly selecting one of the nodes as the next node of the other node.
15. The sorting method according to claim 6, characterized in that: Corresponding to each execution sequence, after all nodes corresponding to the execution sequence are searched, the search is paused, and the execution sequence of all nodes corresponding to the execution sequence is updated according to the interval sorting principle.
16. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the sorting method according to any one of claims 1 to 15 is implemented.
17. An electronic device, characterized in that: The electronic device comprises: a memory storing a computer program; A processor is communicatively connected to the memory, and executes the sorting method according to any one of claims 1 to 15 when calling the computer program.