Compiling method, electronic device and storage medium

By selecting a multi-objective optimization mode and a genetic algorithm to determine the compilation optimization strategy, the computation graph of the deep learning model is optimized, which solves the problems of resource allocation imbalance and poor performance caused by the single-objective strategy, achieves a balance between memory usage and inference latency, and improves the running performance on the NPU.

CN123157221APending Publication Date: 2026-11-24QINGDAO GUOKE MICROELECTRONICS CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611476083.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-21
Publication Date
2026-11-24

AI Technical Summary

Technical Problem

In existing technologies, a fixed compilation strategy for a single target can lead to problems such as unbalanced resource allocation or poor performance in the generated executable files under certain scenarios.

Method used

By selecting the target optimization mode from multiple compilation optimization modes, and combining compilation optimization modes based on memory usage and inference latency, the optimal compilation optimization strategy is determined using techniques such as multi-objective optimization and genetic algorithms. This optimizes the computation graph of the deep learning model and generates the NPU executable file.

Benefits of technology

In specific scenarios, it balances memory usage and inference latency, improving the overall performance of executable files and avoiding resource allocation imbalances and poor performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123157221A_ABST
    Figure CN123157221A_ABST
Patent Text Reader

Abstract

This application relates to the field of computer technology, providing a compilation method, an electronic device, and a storage medium. The method includes: acquiring a first computational graph of a deep learning model; determining a target optimization mode from multiple compilation optimization modes; wherein the multiple compilation optimization modes include a first optimization mode, which characterizes a compilation optimization mode that simultaneously addresses memory usage and inference latency; and optimizing the first computational graph according to the target optimization mode to generate an executable file for an NPU. Based on the solution of this application, when the target optimization mode is the first optimization mode, optimizing the first computational graph can simultaneously consider memory usage and inference latency, so that when the generated executable file runs on the NPU, both memory usage and inference latency can be effectively controlled.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a compilation method, electronic device, and storage medium. Background Technology

[0002] With the rapid development of artificial intelligence technology, neural network processing units (NPUs) have been widely used in various smart terminals and computing centers. When deploying deep learning models to NPUs, it is usually necessary to convert the models into executable files through a compiler. However, the generation process of these executable files typically uses a fixed compilation strategy with a single objective to process the model's computation graph, which can lead to resource imbalances or poor performance in certain scenarios. Summary of the Invention

[0003] This application provides a compilation method, an electronic device, and a storage medium to solve the technical problem that the generated executable file is prone to resource allocation imbalance or poor performance in specific scenarios due to the use of a fixed compilation strategy with a single target.

[0004] In a first aspect, embodiments of this application provide a compilation method, including: Obtain the first computational graph of the deep learning model; The target optimization mode is determined from multiple compilation optimization modes; wherein, the multiple compilation optimization modes include a first optimization mode, which is used to characterize the compilation optimization mode that simultaneously targets memory usage and inference latency; The first computation graph is optimized according to the target optimization mode to generate the NPU executable file.

[0005] In conjunction with the first aspect, in some possible implementations, when the target optimization mode is the first optimization mode, the first computational graph is optimized according to the target optimization mode, including: Obtain multiple candidate compilation optimization strategies corresponding to the first optimization mode, as well as the memory usage and inference latency of each candidate compilation optimization strategy; Based on the memory usage and inference latency of each candidate compilation optimization strategy, a multi-objective optimization is performed on multiple candidate compilation optimization strategies to determine the compilation optimization strategy corresponding to the first optimization mode; The first computation graph is optimized according to the compilation optimization strategy.

[0006] Combining the first aspect and the above implementation methods, in some possible implementation methods, multi-objective optimization is performed on multiple candidate compilation optimization strategies based on the memory usage and inference latency of each candidate compilation optimization strategy to determine the compilation optimization strategy corresponding to the first optimization mode, including: Based on the memory usage and inference latency of each candidate compilation optimization strategy, a multi-objective optimization is performed on multiple candidate compilation optimization strategies to obtain a Pareto optimal solution set; The compilation optimization strategy corresponding to the first optimization mode is determined from the Pareto optimal solution set by threshold screening.

[0007] Combining the first aspect and the above implementation methods, in some possible implementations, multi-objective optimization is performed on multiple candidate compilation optimization strategies based on the memory footprint and inference latency of each candidate strategy to obtain a Pareto optimal solution set, including: Each candidate compilation optimization strategy is encoded as an individual of the genetic algorithm, resulting in a population consisting of multiple individuals; Perform the following operations iteratively until the preset iteration termination condition is met: Based on the memory usage and inference latency of the candidate compilation optimization strategy corresponding to each individual in the population, perform non-dominated sorting of the individuals in the population according to the Pareto dominance relationship to obtain the non-dominated sorting result; perform genetic evolution operation on the population based on the non-dominated sorting result to obtain a new population. In a population that meets the iteration termination condition, the solution set consisting of the candidate compilation optimization strategies corresponding to individuals that are not dominated by other individuals is determined as the Pareto optimal solution set.

[0008] Combining the first aspect and the above implementation methods, in some possible implementation methods, the non-dominated ranking result includes the dominance level of each individual; genetic evolutionary operations are performed on the population based on the non-dominated ranking result to obtain a new population, including: Based on the memory usage and inference latency of each individual, the crowding level of each individual within its dominance level is determined. Based on each individual's dominance level and its crowding level within that dominance level, M individuals are selected from the population through a tournament selection process for crossover and mutation operations to obtain a new population; where M is a positive integer.

[0009] Combining the first aspect and the above implementation methods, in some possible implementation methods, the compilation optimization strategy corresponding to the first optimization mode is determined from the Pareto optimal solution set through threshold filtering, including: From the Pareto optimal solution set, select one of the N candidate compilation optimization strategies whose first parameter does not exceed the corresponding threshold and whose second parameter is the smallest, as the compilation optimization strategy corresponding to the first optimization mode; The first parameter is either the memory usage or the inference latency of the candidate compilation optimization strategy, the second parameter is either the memory usage or the inference latency of the candidate compilation optimization strategy, and N is a positive integer.

[0010] Combining the first aspect and the above implementation methods, in some possible implementation methods, multiple compiler optimization modes include a second optimization mode, which is used to characterize a compiler optimization mode specifically targeting memory usage; when the target optimization mode is the second optimization mode, the first computation graph is optimized according to the compiler optimization strategy corresponding to the target optimization mode, including: The first computation graph is optimized according to the compilation optimization strategy corresponding to the second optimization mode; wherein, the computation graph optimized according to the compilation optimization strategy corresponding to the second optimization mode satisfies at least one of the following conditions: the memory reuse rate is greater than the preset memory reuse rate threshold, the tensor quantization bit width is lower than the preset quantization bit width threshold, and the peak memory usage is lower than the preset peak memory threshold.

[0011] Combining the first aspect and the above implementation methods, in some possible implementations, multiple compiler optimization modes include a third optimization mode, which is used to characterize a compiler optimization mode specifically targeting inference latency; when the target optimization mode is the third optimization mode, the first computation graph is optimized according to the compiler optimization strategy corresponding to the target optimization mode, including: The first computation graph is optimized according to the compilation optimization strategy corresponding to the third optimization mode; wherein the computation graph optimized according to the compilation optimization strategy corresponding to the third optimization mode satisfies at least one of the following conditions: the scheduling parallelism is greater than the preset parallelism threshold, the operator block size is greater than the preset block size threshold, and the inference latency is lower than the preset inference latency threshold.

[0012] Secondly, embodiments of this application provide a compilation apparatus, including: The acquisition module is used to acquire the first computational graph of the deep learning model. A determination module is used to determine a target optimization mode from multiple compilation optimization modes; wherein, the multiple compilation optimization modes include a first optimization mode, which is used to characterize the compilation optimization mode that simultaneously targets memory usage and inference latency; The generation module is used to optimize the first computation graph according to the target optimization mode to generate the executable file of the NPU.

[0013] Thirdly, embodiments of this application provide an electronic device, including a processor and a memory storing a computer program, wherein the processor executes the program to implement the compilation method of the first aspect.

[0014] Fourthly, embodiments of this application provide a non-transitory computer-readable storage medium storing a computer program thereon, wherein the computer program, when executed by a processor, implements the steps of the compilation method of the first aspect.

[0015] Fifthly, embodiments of this application provide a computer program product, including a computer program, wherein when the computer program is executed by a processor, it implements the steps of the compilation method of the first aspect.

[0016] The compilation method, electronic device, and storage medium provided in this application first obtain a first computational graph of a deep learning model; simultaneously or subsequently, a target optimization mode is determined from multiple compilation optimization modes, including a first optimization mode, which characterizes a compilation optimization mode that simultaneously targets memory usage and inference latency; then, the first computational graph is optimized according to the target optimization mode to generate an executable file for the NPU. Through the above process, since the multiple compilation optimization modes include a first optimization mode that simultaneously targets memory usage and inference latency, when the target optimization mode is the first optimization mode, the optimization of the first computational graph can simultaneously take into account memory usage and inference latency. This allows the generated executable file to effectively control both memory usage and inference latency when running on the NPU, improving the overall performance of the executable file. In summary, this solution enables the executable file to balance resource allocation and performance in specific scenarios, thereby preventing resource allocation imbalance or poor performance caused by a fixed strategy with a single objective. Attached Figure Description

[0017] To more clearly illustrate the technical solutions in this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0018] Figure 1 This is a flowchart illustrating the compilation method provided in an embodiment of this application; Figure 2 This is a schematic diagram of the overall process provided in the embodiments of this application; Figure 3 This is a schematic diagram of the structure of the compilation device provided in the embodiments of this application; Figure 4 This is a schematic diagram of the structure of the electronic device provided in the embodiments of this application. Detailed Implementation

[0019] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0020] With the rapid development of artificial intelligence technology, neural network processing units (NPUs) have been widely used in various smart terminals and computing centers. When deploying deep learning models to NPUs, it is usually necessary to use a compiler to convert the model into an executable file.

[0021] In related technologies, compilation methods typically optimize the computation graph for only a single optimization objective, such as optimizing only memory usage or only inference latency.

[0022] However, there is often a trade-off between memory usage and inference latency. Compilation approaches focused solely on reducing memory usage typically employ aggressive optimization techniques such as memory reuse, low-precision quantization, and small-block computation. While these techniques reduce memory usage, they often increase inference latency. Conversely, compilation approaches focused solely on reducing inference latency typically employ optimization techniques such as increasing operator block size and improving scheduling parallelism. These techniques, while reducing inference latency, often increase memory usage. For example, in memory-constrained edge computing scenarios, optimizing only for inference latency can easily lead to memory overflow; while in high-throughput data center scenarios, optimizing only for memory usage will waste on-chip cache resources, resulting in low computing power utilization.

[0023] It is evident that among the aforementioned related technologies, compilation methods that only target a single optimization objective are difficult to balance memory usage and inference latency. This can lead to unbalanced resource allocation or poor performance of the generated executable file when it encounters specific scenarios on the NPU.

[0024] To address the aforementioned issues, the solution provided in this application mainly includes: firstly, obtaining a first computational graph of the deep learning model; then, or simultaneously, determining a target optimization mode from multiple compilation optimization modes, wherein the multiple compilation optimization modes include a first optimization mode, which characterizes a compilation optimization mode that simultaneously addresses memory usage and inference latency; subsequently, optimizing the first computational graph according to the target optimization mode to generate an executable file for the NPU. Through this process, since the multiple compilation optimization modes include a first optimization mode that simultaneously addresses memory usage and inference latency, when the target optimization mode is the first optimization mode, optimizing the first computational graph can simultaneously consider both memory usage and inference latency. This ensures that when the generated executable file runs on the NPU, both memory usage and inference latency are effectively controlled, improving the overall performance of the executable file.

[0025] The compilation method provided in the embodiments of this application will be described in detail below.

[0026] Please see Figure 1 , Figure 1 This is a flowchart illustrating a compilation method provided in an embodiment of this application. Figure 1 As shown, the method in this application embodiment may include the following steps S101~S103: S101, Obtain the first computational graph of the deep learning model; S102, determine the target optimization mode from multiple compilation optimization modes; wherein, the multiple compilation optimization modes include a first optimization mode, which is used to characterize the compilation optimization mode that simultaneously targets memory usage and inference latency; S103, optimize the first computation graph according to the target optimization mode to generate the NPU executable file.

[0027] Specifically, the first step is to obtain the first computational graph of the deep learning model. Here, a deep learning model refers to a data-driven model built on artificial neural networks to perform specific tasks, such as models for image classification, object detection, or natural language processing; this embodiment does not impose any limitations on this. The aforementioned deep learning model can be represented by the first computational graph, or the deep learning model can be transformed to obtain the first computational graph. This first computational graph is a graph structure representation of the computation process of the deep learning model, specifically a directed acyclic graph (DAG). This DAG includes nodes and edges; nodes represent operators, and edges represent tensor data flows or dependencies between operators, thereby depicting the computational path from the input tensor to the output tensor.

[0028] Regarding this step, in some possible implementations, the model file of the deep learning model can be parsed to extract operator attributes, tensor shapes, and tensor lifecycle information, and a first computational graph can be constructed based on the extracted operator attributes, tensor shapes, and tensor lifecycle information. In other possible implementations, a computational graph already constructed by other compilation frameworks or front-end tools can be directly received as the first computational graph of the deep learning model.

[0029] For example, the compiler's input layer receives model files uploaded by users. These model files can be YOLOv5 object detection models to be compiled. The front-end parsing unit of the core compilation layer in the compiler is responsible for parsing the model, constructing a static computation graph, and extracting operator attributes, tensor shapes, and lifecycle information.

[0030] For example, when parsing the model, the front-end parsing unit can perform basic optimizations such as constant folding and dead code elimination to generate a clean computation graph.

[0031] Further, a target optimization mode is determined from multiple compiler optimization modes. This step can be performed concurrently with the preceding steps. The multiple compiler optimization modes include at least a first optimization mode, which characterizes the compiler optimization mode that simultaneously addresses memory footprint and inference latency.

[0032] Specifically, the first optimization mode is used to characterize the compilation optimization process during the operation of this mode, which simultaneously optimizes the two dimensions of memory usage and inference latency. That is, it reduces memory usage while minimizing inference latency, so as to achieve a dynamic balance between memory usage and inference latency.

[0033] Multiple compilation optimization modes may include multiple or a single first optimization mode, as well as second and / or third optimization modes.

[0034] The second optimization mode is used to characterize a compilation optimization mode that focuses solely on memory usage. Specifically, the second optimization mode is used to characterize the compilation optimization process that optimizes memory usage during the execution of this mode in order to reduce peak memory usage.

[0035] The third optimization mode is used to characterize a compiler optimization mode that focuses solely on inference latency. Specifically, the third optimization mode is used to characterize the compiler optimization process that optimizes inference latency during runtime in order to improve computing power utilization and throughput.

[0036] The target optimization mode refers to one of multiple compilation optimization modes. The target optimization mode may be the first optimization mode, the second optimization mode, the third optimization mode, or other compilation optimization modes other than the first optimization mode, the second optimization mode, and the third optimization mode. This embodiment does not limit this.

[0037] Understandably, by offering multiple compiler optimization modes to choose from, diverse optimization needs can be met in different hardware scenarios and deployment environments. Furthermore, a single compiler system can support multiple compilation optimization modes simultaneously, allowing for flexible selection based on different NPU hardware architectures and deployment scenarios, greatly improving the versatility of the solution.

[0038] Regarding this step, some possible implementations include receiving a mode selection command input by the user and using the compilation optimization mode indicated by the command as the target optimization mode. Alternatively, hardware resource information of the current deployment environment can be obtained, and the target optimization mode can be automatically matched from multiple compilation optimization modes based on this information.

[0039] Furthermore, the first computation graph is optimized according to the target optimization mode to generate an executable file for the NPU.

[0040] In some possible implementations, the compilation optimization strategy corresponding to the target optimization mode can be determined. Based on the compilation optimization strategy corresponding to the target optimization mode, the first computation graph is optimized to obtain the second computation graph, and then the executable file of the NPU is generated based on the second computation graph.

[0041] Among them, the compilation optimization strategy refers to the set of rules for scheduling and transforming operators and tensors in the computation graph; the optimization process refers to the process of reorganizing, merging, scheduling and transforming or otherwise processing operators and tensors in the first computation graph according to the compilation optimization strategy; the second computation graph refers to the computation graph that is adapted to the target optimization mode after performing optimization processing on the first computation graph.

[0042] It should be noted that different compilation optimization modes correspond to different compilation optimization strategies. After determining the target optimization mode, a compilation optimization strategy that matches the target optimization mode will be obtained, and this strategy will be used to optimize the first computation graph.

[0043] The second computation graph obtained by optimizing the compiler optimization strategies corresponding to different compiler optimization modes may have differences in operator fusion degree, tensor memory allocation method or operator execution scheduling order, so that the second computation graph reflects the characteristics of the corresponding compiler optimization mode in terms of memory usage or inference latency.

[0044] Regarding the generation of the NPU executable file based on the second computation graph, in some possible implementations, the compiler's output layer can convert the second computation graph into an instruction stream adapted to the NPU hardware architecture, and then compile and link the instruction stream to generate the NPU executable file.

[0045] It should be noted that after generating the NPU executable file, the output layer can also load the NPU executable file into the NPU hardware to run, so that the NPU can perform the inference task corresponding to the deep learning model.

[0046] In addition, after generating the NPU executable file, an evaluation report corresponding to the NPU executable file can also be generated. This evaluation report is a file containing resource usage data of the NPU executable file during runtime. Regarding this step, in some possible implementations, the NPU executable file can be input into a simulation environment or hardware platform for runtime testing, collecting memory usage data and inference latency data during the runtime, and then summarizing the memory usage data and inference latency data to generate the evaluation report corresponding to the NPU executable file.

[0047] In this embodiment, a first computational graph of the deep learning model is first obtained. Simultaneously or subsequently, a target optimization mode is determined from multiple compilation optimization modes, including a first optimization mode that characterizes compilation optimization modes that simultaneously address memory usage and inference latency. Then, the first computational graph is optimized according to the target optimization mode to generate an executable file for the NPU. Through this process, since the target optimization mode can be determined from multiple compilation optimization modes, including the first optimization mode that simultaneously addresses memory usage and inference latency, in certain specific scenarios, the target optimization mode can be the first optimization mode. When optimizing the first computational graph, both memory usage and inference latency can be considered simultaneously. The resulting executable file, when running on the NPU, exhibits effective control over both memory usage and inference latency, improving the overall performance of the executable file. In summary, this solution, by setting a first optimization mode, ensures that the executable file obtained using this first optimization model can balance resource allocation and performance, thereby preventing resource allocation imbalances or poor performance caused by a fixed strategy with a single objective.

[0048] In one embodiment, when the target optimization mode is the first optimization mode, the above step "optimizing the first computation graph according to the target optimization mode" can be further refined and may include the following steps: Obtain multiple candidate compilation optimization strategies corresponding to the first optimization mode, as well as the memory usage and inference latency of each candidate compilation optimization strategy; Based on the memory usage and inference latency of each candidate compilation optimization strategy, a multi-objective optimization is performed on multiple candidate compilation optimization strategies to determine the compilation optimization strategy corresponding to the first optimization mode; The first computation graph is optimized according to the compilation optimization strategy.

[0049] Specifically, the first step is to obtain multiple candidate compilation optimization strategies corresponding to the first optimization mode. These multiple candidate compilation optimization strategies refer to a set of various optional rules for scheduling and transforming operators and tensors in the computation graph during the compilation optimization process.

[0050] Regarding this step, in some possible implementations, the compilation parameters can be combined according to preset parameter combination rules, and the resulting set of multiple rules can be used as multiple candidate compilation optimization strategies corresponding to the first optimization mode; wherein, the type of compilation parameters includes at least one of operator fusion granularity, tensor quantization bit width, operator block size, and scheduling parallelism.

[0051] Furthermore, the memory usage and inference latency of each candidate compilation optimization strategy among multiple candidate compilation optimization strategies are obtained.

[0052] Here, memory usage refers to the amount of memory required to process and run the computation graph using the corresponding candidate compiler optimization strategy; inference latency refers to the time required to execute the inference task while processing and running the computation graph using the corresponding candidate compiler optimization strategy. Regarding this step, in some possible implementations, the running state of each candidate compiler optimization strategy after it is applied to the first computation graph can be evaluated and predicted, thereby calculating the memory usage and inference latency of each candidate compiler optimization strategy.

[0053] Furthermore, based on the memory footprint and inference latency of each candidate compilation optimization strategy, a multi-objective optimization process is performed on multiple candidate compilation optimization strategies to determine the compilation optimization strategy corresponding to the first optimization mode. Here, multi-objective optimization refers to the optimization process of weighing multiple conflicting optimization objectives to find the optimal solution set.

[0054] Regarding this step, in some possible implementations, a multi-objective optimization model can be constructed with the memory footprint and inference latency of each candidate compiler optimization strategy as the optimization objectives. A heuristic search algorithm is then used to perform multi-objective optimization on the multi-objective optimization model to obtain a Pareto optimal solution set. Finally, a threshold screening method is used to determine the compiler optimization strategy corresponding to the first optimization mode from the Pareto optimal solution set.

[0055] Furthermore, by optimizing the first computation graph according to the compiler optimization strategy, a second computation graph can be obtained. Here, optimization refers to the process of reorganizing, merging, scheduling transformations, or otherwise processing the operators and tensors in the first computation graph according to the compiler optimization strategy; the second computation graph refers to the computation graph obtained after performing optimization on the first computation graph and adapting it to the first optimization mode.

[0056] Regarding this step, in some possible implementations, the first computation graph can be optimized simultaneously in terms of memory and latency based on the compilation optimization strategy to achieve a balance between memory usage and inference latency, thus obtaining the second computation graph. The memory optimization includes at least one of tensor memory reuse and tensor quantization, and the latency optimization includes at least one of operator fusion and hardware affinity scheduling.

[0057] In this embodiment, when the target optimization mode is the first optimization mode, since memory usage and inference latency need to be considered simultaneously, multiple candidate compilation optimization strategies and their corresponding memory usage and inference latency values ​​are obtained to perform multi-objective optimization. This allows for a trade-off between conflicting optimization objectives, determining a compilation optimization strategy that balances memory usage and inference latency, and then optimizing the first computation graph. This approach is suitable for scenarios where both memory usage and inference latency require optimization.

[0058] In one embodiment, the step of "obtaining the memory usage and inference latency of each candidate compilation optimization strategy among multiple candidate compilation optimization strategies" can be further refined and may include the following steps: Simulation inference is performed on the first computation graph based on each of the multiple candidate compilation optimization strategies to obtain the memory usage and inference latency of each candidate compilation optimization strategy.

[0059] Specifically, simulation inference refers to the process of simulating the execution of a computation graph in order to extract runtime characteristic parameters without actually calling the NPU hardware, by constructing a virtual runtime environment.

[0060] On the one hand, during the simulation inference process, for each candidate compilation optimization strategy among multiple candidate compilation optimization strategies, the intermediate tensor memory space and constant parameter memory space required by the candidate compilation optimization strategy when it is applied to the first computation graph are counted, and the sum of the intermediate tensor memory space and constant parameter memory space is determined as the memory usage value of the candidate compilation optimization strategy.

[0061] On the other hand, during the simulation inference process, for each candidate compilation optimization strategy among multiple candidate compilation optimization strategies, the computation time of each operator node and the memory access and transfer time between operator nodes are recorded when the candidate compilation optimization strategy is applied to the first computation graph. The sum of the computation time of each operator node and the memory access and transfer time is determined as the inference latency value of the candidate compilation optimization strategy.

[0062] In this embodiment, by performing simulation inference on the first computation graph according to each of the multiple candidate compilation optimization strategies, the memory usage and inference latency of each candidate compilation optimization strategy can be obtained without actually running on the physical NPU hardware, thus reducing the evaluation overhead.

[0063] In one embodiment, the step of "performing multi-objective optimization of multiple candidate compilation optimization strategies based on the memory usage and inference latency of each candidate compilation optimization strategy to determine the compilation optimization strategy corresponding to the first optimization mode" can be further refined and may include the following steps: Based on the memory usage and inference latency of each candidate compilation optimization strategy, a multi-objective optimization is performed on multiple candidate compilation optimization strategies to obtain a Pareto optimal solution set; The compilation optimization strategy corresponding to the first optimization mode is determined from the Pareto optimal solution set by threshold screening.

[0064] Specifically, the first step is to perform multi-objective optimization on multiple candidate compilation optimization strategies based on their memory footprint and inference latency, thereby obtaining a Pareto optimal solution set. This Pareto optimal solution set represents the set of candidate compilation optimization strategies that cannot be simultaneously surpassed by other candidate strategies in both the memory footprint and inference latency optimization objectives.

[0065] It should be noted that multi-objective optimization can be implemented using heuristic search algorithms, such as genetic algorithms, particle swarm optimization, or ant colony optimization. In some possible implementations, the memory footprint and inference latency of each candidate compiler optimization strategy can be used as optimization objectives to construct a multi-objective optimization model. By performing multi-objective optimization on this model, the set of non-dominated solutions obtained is taken as the Pareto optimal set.

[0066] Furthermore, a compilation optimization strategy corresponding to the first optimization mode is determined from the Pareto optimal solution set through threshold filtering. Threshold filtering refers to a method of selecting the target solution by filtering the solution set using preset threshold boundary conditions.

[0067] Regarding this step, in some possible implementations, a threshold screening method can be used to filter the Pareto optimal solution set by using at least one of the first preset threshold corresponding to the memory usage value and the second preset threshold corresponding to the inference latency value, and the remaining candidate compilation optimization strategies after screening can be used as the compilation optimization strategy corresponding to the first optimization mode.

[0068] Preferably, the Pareto optimal solution set is screened by simultaneously using a first preset threshold corresponding to the memory usage value and a second preset threshold corresponding to the inference latency value, so as to simultaneously constrain both memory usage and inference latency dimensions, so that the compilation optimization strategy corresponding to the determined first optimization mode meets the requirements of balanced optimization.

[0069] In this embodiment, multiple candidate compilation optimization strategies are optimized for multiple objectives based on the memory usage and inference latency of each candidate compilation optimization strategy to obtain a Pareto optimal solution set. The compilation optimization strategy corresponding to the first optimization mode is determined from the Pareto optimal solution set by threshold screening. This allows for a trade-off between conflicting optimization objectives, so that the compilation optimization strategy corresponding to the determined first optimization mode can simultaneously take into account memory usage and inference latency.

[0070] In one embodiment, the step of "performing multi-objective optimization of multiple candidate compilation optimization strategies based on the memory usage and inference latency of each candidate compilation optimization strategy to obtain a Pareto optimal solution set" can be further refined and may include the following steps: Each candidate compilation optimization strategy is encoded as an individual of the genetic algorithm, resulting in a population consisting of multiple individuals; Perform the following operations iteratively until the preset iteration termination condition is met: Based on the memory usage and inference latency of the candidate compilation optimization strategy corresponding to each individual in the population, perform non-dominated sorting of the individuals in the population according to the Pareto dominance relationship to obtain the non-dominated sorting result; perform genetic evolution operation on the population based on the non-dominated sorting result to obtain a new population. In a population that meets the iteration termination condition, the solution set consisting of the candidate compilation optimization strategies corresponding to individuals that are not dominated by other individuals is determined as the Pareto optimal solution set.

[0071] Specifically, a genetic algorithm is a heuristic search algorithm that searches for the optimal solution by simulating the natural evolutionary process.

[0072] First, each candidate compilation optimization strategy needs to be encoded as an individual of the genetic algorithm to obtain a population consisting of multiple individuals.

[0073] Regarding this step, in some possible implementations, the compilation parameters in each candidate compilation optimization strategy can be serialized and mapped, the result of the serialization mapping can be used as an individual of the genetic algorithm, and the set of individuals from multiple genetic algorithms can be used as a population of multiple individuals.

[0074] Furthermore, the following operations are performed iteratively until a preset iteration termination condition is met: based on the memory usage and inference latency of the candidate compilation optimization strategy corresponding to each individual in the population, the individuals in the population are sorted non-dominated according to the Pareto dominance relationship to obtain the non-dominated sorting result, which includes the dominance level of each individual in the population; based on the non-dominated sorting result, the population is subjected to genetic evolution operations to obtain a new population.

[0075] Specifically, dominance level refers to the non-dominated stratification level of an individual within a population. Individuals in the population can be compared and stratified according to Pareto dominance. Based on the stratification information represented by the comparison stratification, each individual in the population is assigned a corresponding stratification value, which is used as the individual's dominance level, thus obtaining a non-dominated ranking result that includes the dominance level of each individual in the population.

[0076] Regarding this step, in some possible implementations, an iterative approach can be adopted, gradually approaching the optimal solution through multiple rounds of iteration. Iteration is chosen because a single processing cannot guarantee obtaining the globally optimal candidate compiler optimization strategy. The specific execution process is as follows: In each iteration, the memory usage and inference latency of the candidate compiler optimization strategy corresponding to each individual in the population are obtained. Individuals in the population are compared and stratified according to the Pareto dominance relationship to complete the non-dominated sorting and obtain the non-dominated sorting result, which includes the dominance level of each individual in the population. Subsequently, genetic evolution operations are performed on the population based on the non-dominated sorting result to generate a new population, and this new population is used as the input for the next iteration until the preset iteration termination condition is met.

[0077] It should be noted that the preset iteration termination condition can be expressed as the number of iterations reaching a preset threshold, or as the non-dominated sorting results in the population no longer being updated in consecutive generations of iterations. This embodiment does not impose any restrictions on this.

[0078] Furthermore, in the population that satisfies the iteration termination condition, the solution set consisting of the candidate compilation optimization strategies corresponding to individuals that are not dominated by other individuals is determined as the Pareto optimal solution set.

[0079] Regarding this step, in some possible implementations, individuals that are not dominated by other individuals can be extracted from the population that meets the iteration termination condition, and the candidate compilation optimization strategies corresponding to these individuals can be obtained. The solution set composed of these candidate compilation optimization strategies is then determined as the Pareto optimal solution set.

[0080] In this embodiment, each candidate compilation optimization strategy is encoded as an individual of a genetic algorithm to obtain a population consisting of multiple individuals. Non-dominated sorting and genetic evolution operations are performed iteratively until a preset iteration termination condition is met. Finally, in the population that meets the iteration termination condition, the solution set consisting of the candidate compilation optimization strategies corresponding to individuals that are not dominated by other individuals is determined as the Pareto optimal solution set, thus achieving dual-objective optimization of memory usage and inference latency.

[0081] In one embodiment, the non-dominated ranking result includes the dominance level of each individual; further refining the above step "performing genetic evolutionary operations on the population based on the non-dominated ranking result to obtain a new population" may include the following steps: Based on the memory usage and inference latency of each individual, the crowding level of each individual within its dominance level is determined. Based on each individual's dominance level and its crowding level within that dominance level, M individuals are selected from the population through a tournament selection process for crossover and mutation operations to obtain a new population; where M is a positive integer.

[0082] In the first optimization mode, this scheme introduces Pareto non-dominated sorting based on genetic algorithms. Through dual evaluation of dominance level and crowding, it automatically searches for the optimal balance between memory and performance, eliminating the need for manual tuning and significantly shortening the model deployment cycle.

[0083] Specifically, firstly, based on the memory usage and inference latency of each individual, the crowding degree of each individual within its dominance level is determined. Here, crowding degree refers to the distance metric between an individual and its neighboring individuals in the target space, which is a space constructed using memory usage and inference latency as two optimization objectives.

[0084] Regarding this step, in some possible implementations, for each dominance level in the population, individuals within that dominance level can be sorted according to their memory usage and inference latency values. For each individual within that dominance level, the individuals immediately before and after that individual in the sorting result are identified as its neighbors. Then, based on the memory usage and inference latency values ​​corresponding to each individual within that dominance level, the sum of the differences between that individual and its neighbors in the memory and latency dimensions is calculated, and this sum of differences is used as the crowding degree of that individual.

[0085] Furthermore, based on each individual's dominance level and crowding level within that dominance level, M individuals are selected from the population through a tournament selection process for crossover and mutation operations to obtain a new population; where M is a positive integer. The tournament selection process refers to a random competition-based individual selection mechanism; crossover refers to the process of recombinating parts of the genes between different individuals; mutation refers to the process of randomly altering the genes within an individual; and an individual's genes refer to the basic units representing compilation parameters in its encoding. These basic units are the compilation parameters received by the compiler and can include block size, operator fusion granularity, quantization bit width, and parallelism, etc.

[0086] The tournament selection process can be summarized as follows: A predetermined number of individuals are randomly selected from the population to form a competition group. Within the competition group, the dominance level and crowding of each individual are compared. Specifically, the individual with the lower hierarchy value corresponding to the dominance level is determined as the winner. If multiple individuals in the competition group have the same dominance level, their crowding is further compared, and the individual with the higher crowding is determined as the winner, thus identifying a winning individual from the competition group. The above steps of random selection, comparison, and determination of the winning individual are repeated until a total of M individuals are obtained. Crossover and mutation operations are performed on these M individuals, and the resulting set of individuals is used as a new population.

[0087] In this embodiment, the non-dominated sorting result includes the dominance level of each individual in the population. By determining the crowding degree of each individual within its dominance level based on the memory usage value and inference latency value corresponding to each individual, and then selecting M individuals from the population through a tournament selection method based on the dominance level and the crowding degree of each individual within its dominance level, a new population is obtained by performing crossover and mutation operations. This allows for the preservation of superior individuals and the maintenance of population diversity during the genetic evolution process.

[0088] In one embodiment, the step of "determining the compilation optimization strategy corresponding to the first optimization mode from the Pareto optimal solution set by threshold filtering" can be further refined and may include the following steps: From the Pareto optimal solution set, select one of the N candidate compilation optimization strategies whose first parameter does not exceed the corresponding threshold and whose second parameter is the smallest, as the compilation optimization strategy corresponding to the first optimization mode; The first parameter is either the memory usage or the inference latency of the candidate compilation optimization strategy, the second parameter is either the memory usage or the inference latency of the candidate compilation optimization strategy, and N is a positive integer.

[0089] Specifically, when the first parameter is the memory footprint of the candidate compilation optimization strategy and the second parameter is the inference latency of the candidate compilation optimization strategy, the following operations are performed: From the Pareto optimal solution set, candidate compilation optimization strategies whose first parameter does not exceed the corresponding threshold are selected, where the first parameter is the memory footprint of the candidate compilation optimization strategy; then, from the selected candidate compilation optimization strategies, N candidate compilation optimization strategies with the smallest second parameter are selected, where the second parameter is the inference latency of the candidate compilation optimization strategy; finally, any one of the N candidate compilation optimization strategies is taken as the compilation optimization strategy corresponding to the first optimization mode.

[0090] When the first parameter is the inference latency value of the candidate compilation optimization strategy and the second parameter is the memory usage value of the candidate compilation optimization strategy, the following operations are performed: From the Pareto optimal solution set, candidate compilation optimization strategies whose first parameter does not exceed the corresponding threshold are selected, where the first parameter is the inference latency value of the candidate compilation optimization strategy; then, from the selected candidate compilation optimization strategies, N candidate compilation optimization strategies with the smallest second parameter are selected, where the second parameter is the memory usage value of the candidate compilation optimization strategy; finally, any one of the N candidate compilation optimization strategies is taken as the compilation optimization strategy corresponding to the first optimization mode.

[0091] In this embodiment, by using threshold screening, under the premise that the first parameter does not exceed the corresponding threshold, one of the N candidate compilation optimization strategies with the smallest second parameter is selected as the compilation optimization strategy corresponding to the first optimization mode. This can further achieve optimization in another dimension while meeting specific resource or performance constraints, making the compilation optimization strategy corresponding to the determined first optimization mode more adaptable to the diverse needs of actual deployment scenarios.

[0092] In one embodiment, the multiple compilation optimization modes include a second optimization mode, which is used to characterize a compilation optimization mode specifically targeting memory usage; when the target optimization mode is the second optimization mode, the above step "optimize the first computation graph according to the compilation optimization strategy corresponding to the target optimization mode" can be further refined and may include the following steps: The first computation graph is optimized according to the compilation optimization strategy corresponding to the second optimization mode; wherein, the computation graph optimized according to the compilation optimization strategy corresponding to the second optimization mode satisfies at least one of the following conditions: the memory reuse rate is greater than the preset memory reuse rate threshold, the tensor quantization bit width is lower than the preset quantization bit width threshold, and the peak memory usage is lower than the preset peak memory threshold.

[0093] Specifically, memory reuse rate refers to the proportion of memory space reused during the computation graph's operation to the total allocated memory space; tensor quantization bit width refers to the bit width corresponding to the data type of the tensor after quantization; peak memory usage refers to the maximum amount of memory space occupied during the computation graph's operation. The memory reuse rate threshold, quantization bit width threshold, and peak memory threshold can all be preset according to the memory resource constraints of the actual deployment scenario, and this embodiment does not impose any restrictions on them.

[0094] It should be noted that the above three conditions are used to describe the optimization effect of the optimized computation graph in terms of memory usage from different dimensions. The optimized computation graph only needs to meet any one of the conditions, or it can meet two or three of the conditions at the same time. This embodiment does not impose any restrictions on this.

[0095] Regarding this step, in some possible implementations, the first computation graph can be optimized using at least one of the following methods based on the compilation optimization strategy corresponding to the second optimization mode: tensor memory reuse, in-place computation, tensor quantization, and operator block computation. This ensures that the optimized computation graph satisfies at least one of the above conditions. Tensor memory reuse refers to a memory management method that allocates the same memory space to tensors in the computation graph whose lifecycles do not overlap. In-place computation refers to a computation method that directly overwrites the memory space occupied by the input tensor with the output tensor of the operator. Tensor quantization refers to a data conversion method that converts tensors of high-precision data types into tensors of low-precision data types. Operator block computation refers to a computation method that splits a large-size operator into multiple small-size sub-operators and executes them sequentially.

[0096] In the case where at least one optimization process in this embodiment includes tensor memory reuse, the following operations can be performed: according to the compilation optimization strategy corresponding to the second optimization mode, lifecycle analysis is performed on each tensor in the first computation graph to identify tensors whose lifecycles do not overlap, and the same memory space is allocated to the identified tensors to improve the memory reuse rate of the computation graph after optimization, so that the memory reuse rate is greater than the preset memory reuse rate threshold.

[0097] In the case where at least one optimization process in this embodiment includes in-place computation, the following operations can be performed: based on the compilation optimization strategy corresponding to the second optimization mode, operators in the first computation graph that do not have data dependency conflicts are identified, and the output tensor of the identified operator is directly overwritten with the memory space occupied by its input tensor to reduce the allocation of additional memory space, so that the peak memory usage of the computation graph after optimization is lower than the preset peak memory threshold.

[0098] In the case where at least one optimization process in this embodiment includes tensor quantization, the following operations can be performed: according to the compilation optimization strategy corresponding to the second optimization mode, the data type of tensors in the first computation graph that meet the preset quantization conditions is converted from a high-precision data type to a low-precision data type, so as to reduce the tensor quantization bit width of the computation graph after optimization, and make the tensor quantization bit width lower than the preset quantization bit width threshold.

[0099] In the case where at least one optimization process in this embodiment includes operator block computation, the following operations can be performed: according to the compilation optimization strategy corresponding to the second optimization mode, the operators in the first computation graph with a size greater than a preset size threshold are split into multiple smaller sub-operators, and the split sub-operators are executed sequentially according to a preset execution order, so as to reduce the memory space required for a single computation and make the peak memory usage of the optimized computation graph lower than the preset peak memory threshold.

[0100] In this embodiment, when the target optimization mode is the second optimization mode, the first computation graph is optimized according to the compilation optimization strategy corresponding to the second optimization mode, and the optimized computation graph satisfies at least one of the following conditions: memory reuse rate is greater than a preset memory reuse rate threshold, tensor quantization bit width is lower than a preset quantization bit width threshold, and peak memory usage is lower than a preset peak memory threshold. This can constrain the direction of compilation optimization with clear indicators, and reduce the memory usage of the computation graph during runtime in a targeted manner, so that the generated executable file can be adapted to deployment scenarios with strict limitations on memory usage.

[0101] In one embodiment, the multiple compiler optimization modes include a third optimization mode, which is used to characterize a compiler optimization mode specifically for inference latency; when the target optimization mode is the third optimization mode, the above step "optimize the first computation graph according to the compiler optimization strategy corresponding to the target optimization mode" can be further refined and may include the following steps: The first computation graph is optimized according to the compilation optimization strategy corresponding to the third optimization mode; wherein the computation graph optimized according to the compilation optimization strategy corresponding to the third optimization mode satisfies at least one of the following conditions: the scheduling parallelism is greater than the preset parallelism threshold, the operator block size is greater than the preset block size threshold, and the inference latency is lower than the preset inference latency threshold.

[0102] Specifically, scheduling parallelism refers to the number of operators or sub-operators that can be executed simultaneously during the computation graph's operation; operator block size refers to the size of the sub-operators obtained after the operator is divided into blocks; and inference latency refers to the time required for the computation graph to perform one inference task. The parallelism threshold, block size threshold, and inference latency threshold can all be preset according to the latency performance requirements of the actual deployment scenario, and this embodiment does not impose any restrictions on them.

[0103] It should be noted that the above three conditions are used to describe the optimization effect of the optimized computation graph in terms of inference latency from different dimensions. The optimized computation graph only needs to satisfy any one of the conditions, or it can satisfy two or three of the conditions at the same time. This embodiment does not impose any restrictions on this.

[0104] Regarding this step, in some possible implementations, the first computation graph can be optimized using at least one of the following methods based on the compilation optimization strategy corresponding to the third optimization mode: operator fusion, operator block execution, hardware affinity scheduling, and pipeline scheduling. This optimization ensures that the optimized computation graph satisfies at least one of the aforementioned conditions. Specifically, operator fusion refers to a graph transformation method that merges multiple consecutive operators into a single composite operator; operator block execution refers to a computation method that splits a large-sized operator into multiple small-sized sub-operators and executes them in parallel; hardware affinity scheduling refers to a scheduling method that allocates operator execution tasks based on the topology of the hardware processing units; and pipeline scheduling refers to a scheduling method that divides computation tasks into multiple stages and overlaps the data processing and transport processes of different stages.

[0105] In the case where at least one optimization process in this embodiment includes operator fusion, the following operations can be performed: according to the compilation optimization strategy corresponding to the third optimization mode, multiple operators with adjacent dependencies in the first computation graph are merged to generate corresponding composite operators, so as to reduce the data transfer overhead and scheduling overhead between operators from DDR to NPU, and make the inference latency of the optimized computation graph lower than the preset inference latency threshold.

[0106] In the case where at least one optimization process in this embodiment includes operator block execution, the following operations can be performed: according to the compilation optimization strategy corresponding to the third optimization mode, operators with a size greater than a preset size threshold in the first computation graph are split into multiple sub-operators, and parallel execution resources are allocated to the split sub-operators to improve the scheduling parallelism of the computation graph after optimization, so that the scheduling parallelism is greater than the preset parallelism threshold; wherein, the operator block size of the split sub-operators is greater than the preset block size threshold to avoid increased scheduling overhead caused by excessively fine blocks.

[0107] In the case where at least one optimization process in this embodiment includes hardware affinity scheduling, the following operations can be performed: according to the compilation optimization strategy corresponding to the third optimization mode, obtain the processing unit topology information of the target hardware, and allocate corresponding target processing units to each operator in the first computation graph according to the processing unit topology information, so as to make full use of the parallel processing capability of the target hardware and make the scheduling parallelism of the optimized computation graph greater than the preset parallelism threshold.

[0108] In the case where at least one optimization process in this embodiment includes pipeline scheduling, the following operations can be performed: according to the compilation optimization strategy corresponding to the third optimization mode, the operator execution process in the first computation graph is divided into multiple computation stages, and the data transfer operations and computation operations in adjacent computation stages are controlled to overlap in time to hide the waiting time of data transfer, so that the inference latency of the optimized computation graph is lower than the preset inference latency threshold.

[0109] In this embodiment, when the target optimization mode is the third optimization mode, the first computation graph is optimized according to the compilation optimization strategy corresponding to the third optimization mode, and the optimized computation graph satisfies at least one of the following conditions: scheduling parallelism is greater than a preset parallelism threshold, operator block size is greater than a preset block size threshold, and inference latency is lower than a preset inference latency threshold. This can constrain the direction of compilation optimization with clear indicators, and reduce the execution overhead and waiting time of the computation graph in a targeted manner, so that the generated executable file can be adapted to deployment scenarios with strict limitations on inference latency.

[0110] Please see Figure 2 , Figure 2 This is a schematic diagram of the overall process provided for an embodiment of this application.

[0111] Specifically, the first computational graph of the deep learning model is first obtained. Then / simultaneously, a target optimization mode is determined from multiple compilation optimization modes. This target optimization mode can be a first optimization mode, a second optimization mode, or a third optimization mode. The first optimization mode is used to characterize a compilation optimization mode that addresses both memory usage and inference latency, the second optimization mode is used to characterize a compilation optimization mode that addresses only memory usage, and the third optimization mode is used to characterize a compilation optimization mode that addresses only inference latency.

[0112] If the target optimization mode is the first optimization mode, then multiple candidate compilation optimization strategies corresponding to the first optimization mode are obtained, along with the memory usage and inference latency of each candidate compilation optimization strategy. The memory usage and inference latency can be obtained through simulation inference on the first computation graph based on each candidate compilation optimization strategy. Then, based on the memory usage and inference latency of each candidate compilation optimization strategy, multi-objective optimization is performed on the multiple candidate compilation optimization strategies: each candidate compilation optimization strategy is encoded as an individual in a genetic algorithm, resulting in a population composed of multiple individuals. Iteratively, the individuals in the population are sorted according to the Pareto dominance relationship, yielding a non-dominated sorting result including the dominance level of each individual. The crowding degree of each individual within its respective dominance level is determined based on the memory usage and inference latency of each individual. Based on the dominance level and the crowding degree of each individual within its respective dominance level, the population is subjected to genetic evolution operations through a tournament selection method until a preset iteration termination condition is met, resulting in a Pareto optimal solution set. Subsequently, the compilation optimization strategy corresponding to the first optimization mode is determined from the Pareto optimal solution set by threshold screening. For example, one of the N candidate compilation optimization strategies whose first parameter does not exceed the corresponding threshold and whose second parameter is the smallest is selected from the Pareto optimal solution set as the compilation optimization strategy corresponding to the first optimization mode.

[0113] If the target optimization mode is the second optimization mode, then according to the compilation optimization strategy corresponding to the second optimization mode, the first computation graph is subjected to at least one of the following optimization processes: tensor memory reuse, in-place computation, tensor quantization, and operator block computation, so that the optimized computation graph satisfies at least one of the following conditions: memory reuse rate is greater than a preset memory reuse rate threshold, tensor quantization bit width is lower than a preset quantization bit width threshold, and peak memory usage is lower than a preset peak memory threshold. By running the memory-priority second optimization mode, tensor pooling and aggressive quantization are achieved, effectively solving the memory overflow problem.

[0114] If the target optimization mode is the third optimization mode, then according to the compilation optimization strategy corresponding to the third optimization mode, at least one of the following optimization processes—operator fusion, operator block execution, hardware affinity scheduling, and pipeline scheduling—is applied to the first computation graph to ensure that the optimized computation graph meets at least one of the following conditions: the scheduling parallelism is greater than a preset parallelism threshold, the operator block size is greater than a preset block size threshold, and the inference latency is lower than a preset inference latency threshold. By implementing the performance-prioritized third optimization mode, operator fusion and hardware affinity scheduling are achieved, maximizing computational power utilization.

[0115] After optimizing the first computational graph according to the target optimization pattern, a second computational graph is obtained. Finally, an executable file for the NPU is generated based on the second computational graph.

[0116] In this embodiment, by providing multiple compilation optimization modes, including a first optimization mode, the target optimization mode can be determined according to the needs of the actual deployment scenario. When the target optimization mode is the first optimization mode, memory usage and inference latency are obtained through simulation inference, and a genetic algorithm iterates through multiple objectives for optimization and threshold selection, achieving a dynamic balance between memory usage and inference latency. When the target optimization mode is the second or third optimization mode, the optimization direction can be constrained by clear indicator conditions, specifically reducing memory usage or inference latency. Thus, when the generated executable file runs on the NPU, both memory usage and inference latency can be effectively controlled, improving the overall performance of the executable file.

[0117] In one embodiment, the compiler first receives a deep learning model file to be compiled and obtains a first computational graph of the deep learning model. The deep learning model file can be, for example, a file supporting formats such as ONNX and PB. For instance, the compiler receives an ONNX file of a YOLOv5 object detection model, parses the ONNX file, extracts operator attributes, tensor shapes, and tensor lifetime information, and constructs the first computational graph. Next, the compiler receives a mode selection instruction input by the user and determines a target optimization mode from multiple compilation optimization modes. If the received instruction is BALANCED, the target optimization mode is determined to be the first optimization mode; if the instruction is MEM, the target optimization mode is determined to be the second optimization mode; and if the instruction is PERF, the target optimization mode is determined to be the third optimization mode.

[0118] After determining the target optimization mode, the cost function is initialized, and the cost function is expressed by the following formula: ; In the formula, This indicates the possible values ​​of the cost function; The weights corresponding to the inference delay values; Indicates the inference delay value; This indicates the memory usage value. If it's the second optimization mode, then... If it is the third optimization mode, then If it is the first optimization mode, then .

[0119] If the target optimization mode is the first optimization mode, then multiple candidate compilation optimization strategies corresponding to the first optimization mode are obtained. Compilation parameters such as operator fusion granularity, tensor quantization bit width, operator block size, and scheduling parallelism are combined to obtain multiple candidate compilation optimization strategies. Simulation reasoning is performed on the first computation graph based on each candidate compilation optimization strategy to obtain the memory usage value of each candidate compilation optimization strategy. and inference delay value During the simulation inference process, the sum of the intermediate tensor memory space and the constant parameter memory space required when the candidate compilation optimization strategy is applied to the first computation graph is used to determine the memory usage value. Record the sum of the computation time of each operator node and the memory access and transfer time between operator nodes, and determine it as the inference latency value. .

[0120] Based on the memory footprint and inference latency of each candidate compilation optimization strategy, a multi-objective optimization is performed on multiple candidate compilation optimization strategies to obtain a Pareto optimal solution set. The specific process includes: encoding each candidate compilation optimization strategy as an individual using a genetic algorithm, resulting in a population of multiple individuals. For example, compilation parameters such as operator block size and tensor quantization bit width are encoded as individual genes in the form of integer sequences, and the size is randomly generated. The initial population is 100, where the operator block size can be, for example, 16, 32, or 64, and the tensor quantization bit width can be, for example, FP16 or INT8. The following operations are performed iteratively until a preset iteration termination condition is met, such as reaching the maximum number of iterations (50 generations): Based on the memory usage and inference latency of the candidate compiler optimization strategy corresponding to each individual in the population, the individuals in the population are non-dominated according to the Pareto dominance relation, resulting in a non-dominated ranking result, which includes the dominance level of each individual in the population. Specifically, for any two individuals in the population... , An individual is considered to be in a certain condition if and only if the following conditions are met. Dominant Individual : ,and ; ,or ; In the formula, , Each represents an individual , Memory usage value; , Each represents an individual , The reasoning delay value. The dominance level is determined as follows: an individual that is not dominated by any other individual is determined as dominance level 1; an individual that is dominated only by individuals of dominance level 1 is determined as dominance level 2; an individual that is dominated only by individuals of dominance level 1 and dominance level 2 is determined as dominance level 3, and so on.

[0121] Based on the memory usage and inference latency of each individual, the congestion level of each individual within its dominance level is determined. Specifically, for each dominance level, all individuals within that level are sorted in ascending order of memory usage and inference latency, and the congestion levels of the first and last individuals in the sorting result are set to infinity. For the middle individual, based on the adjacent individuals in the sorting result, the congestion components in the memory dimension and the congestion components in the latency dimension of that individual are calculated, expressed as follows: ; ; In the formula, , Each represents an individual Crowding components in the memory and latency dimensions; , These represent the results after sorting by memory usage and the individual values. The memory usage of adjacent individuals; , These represent the results after sorting by inference delay value and the individual. The inference delay values ​​of adjacent individuals; , These represent the maximum and minimum memory usage values ​​for individuals within that dominance level, respectively. , These represent the maximum and minimum inference latency values ​​for individuals within that dominance level, respectively. Furthermore, the crowding component on the memory dimension is added to the crowding component on the latency dimension to obtain the crowding degree of that individual, expressed as the following formula: ; In the formula, Represents an individual The degree of crowding. Then, based on each individual's dominance level and the degree of crowding within that dominance level, selection is made from the population through a tournament selection method. Each individual undergoes crossover and mutation operations to obtain a new population, in which... The value is a positive integer. The comparison rule for the tournament selection method is as follows: the individual with the smaller level value corresponding to the dominance level is prioritized as the winner; if multiple individuals have the same dominance level, the individual with the greater crowding is selected as the winner. In the population that satisfies the iteration termination condition, the solution set consisting of the candidate compiler optimization strategies corresponding to individuals with dominance level 1 is determined as the Pareto optimal solution set.

[0122] The compilation optimization strategy corresponding to the first optimization mode is determined from the Pareto optimal solution set using a threshold screening method. From the Pareto optimal solution set, the candidate compilation optimization strategy with the first parameter not exceeding the corresponding threshold and the smallest second parameter is selected as the compilation optimization strategy corresponding to the first optimization mode. For example, the first parameter is set as the memory usage value, and the corresponding threshold is the memory limit of the edge NPU chip, 200MB; the second parameter is the inference latency value. Assume the Pareto optimal solution set converges to three candidate compilation optimization strategies: Strategy A has an operator block size of 16, a tensor quantization bit width of INT8, a memory usage value of 150MB, and an inference latency value of 5ms; Strategy B has an operator block size of 32, a tensor quantization bit width of INT8, a memory usage value of 180MB, and an inference latency value of 3ms; Strategy C has an operator block size of 32, a tensor quantization bit width of FP16, an inference latency value of 2ms, and a memory usage value of 250MB. Since strategy C's memory usage of 250MB exceeds the 200MB threshold, it is filtered out. From the remaining candidate compiler optimization strategies, strategy B, with the smallest second parameter, is selected as the compiler optimization strategy corresponding to the first optimization mode. Finally, the first computation graph is optimized according to this compiler optimization strategy to obtain the second computation graph.

[0123] If the target optimization mode is the second optimization mode, then according to the compilation optimization strategy corresponding to the second optimization mode, the first computation graph is optimized using at least one of the following methods: tensor memory reuse, in-place computation, tensor quantization, and operator block computation. This ensures that the optimized computation graph meets at least one of the following conditions: memory reuse rate is greater than a preset memory reuse rate threshold, tensor quantization bit width is lower than a preset quantization bit width threshold, and peak memory usage is lower than a preset peak memory threshold. Specifically, this includes: traversing the computation graph to analyze the lifecycle of intermediate tensors; allocating the same memory space for tensors with non-overlapping lifecycles to perform tensor memory reuse, thereby improving memory reuse rate; enabling in-place computation for operators without data dependency conflicts; converting tensors of high-precision data types to low-precision data types such as INT4 or INT8 for tensor quantization, thereby reducing tensor quantization bit width; splitting large-size operators into multiple small-size sub-operators and executing them sequentially for operator block computation, strictly controlling peak memory usage, and obtaining the second computation graph.

[0124] If the target optimization mode is the third optimization mode, then according to the compilation optimization strategy corresponding to the third optimization mode, the first computation graph is optimized by at least one of the following: operator fusion, operator block execution, hardware affinity scheduling, and pipeline scheduling, so that the optimized computation graph satisfies at least one of the following conditions: the scheduling parallelism is greater than a preset parallelism threshold, the operator block size is greater than a preset block size threshold, and the inference latency is lower than a preset inference latency threshold. Specifically, this includes: merging multiple operators with adjacent dependencies into composite operators for operator fusion; splitting large-size operators into multiple sub-operators with operator block sizes greater than a preset block size threshold, and allocating parallel execution resources for operator block execution to improve the computation / memory access ratio; allocating operator execution tasks according to the NPU internal execution unit array structure for hardware affinity scheduling to improve scheduling parallelism; dividing the operator execution process into multiple stages and overlapping data transport and computation operations in time for pipeline scheduling to reduce inference latency, thus obtaining the second computation graph.

[0125] After optimizing the first computation graph according to the target optimization mode to obtain the second computation graph, the second computation graph is converted into an instruction stream adapted to the NPU hardware architecture, and the instruction stream is compiled and linked to generate the NPU executable file. The generated NPU executable file is loaded into the edge intelligent vision NPU chip for execution, enabling the NPU to perform inference tasks of the YOLOv5 object detection model.

[0126] In this embodiment, by providing multiple compilation optimization modes, including a first optimization mode, the target optimization mode can be determined according to the needs of the actual deployment scenario. When the target optimization mode is the first optimization mode, memory usage and inference latency are obtained through simulation inference, and a genetic algorithm iterates through multiple objectives for optimization and threshold selection, achieving a dynamic balance between memory usage and inference latency. When the target optimization mode is the second or third optimization mode, the optimization direction can be constrained by clear indicator conditions, specifically reducing memory usage or inference latency. Thus, when the generated executable file runs on the NPU, both memory usage and inference latency can be effectively controlled, improving the overall performance of the executable file.

[0127] The following will combine Figure 3 The compilation apparatus 800 provided in this application embodiment will be described in detail. The compilation apparatus 800 and the compilation method described above can be referred to in correspondence. Specifically, the compilation apparatus 800 may include an acquisition module 810, a determination module 820, and a generation module 830, as detailed below: Module 810 is used to acquire the first computational graph of the deep learning model; The determination module 820 is used to determine the target optimization mode from multiple compilation optimization modes; wherein, the multiple compilation optimization modes include a first optimization mode, which is used to characterize the compilation optimization mode that simultaneously targets memory usage and inference latency; The generation module 830 is used to optimize the first computation graph according to the target optimization mode to generate the executable file of the NPU.

[0128] Optionally, in some embodiments, when the target optimization mode is a first optimization mode, the generation module 830 can be used to optimize the first computation graph according to the target optimization mode, including: Obtain multiple candidate compilation optimization strategies corresponding to the first optimization mode, as well as the memory usage and inference latency of each candidate compilation optimization strategy; Based on the memory usage and inference latency of each candidate compilation optimization strategy, a multi-objective optimization is performed on multiple candidate compilation optimization strategies to determine the compilation optimization strategy corresponding to the first optimization mode; The first computation graph is optimized according to the compilation optimization strategy.

[0129] Optionally, in some embodiments, the generation module 830 can be used to perform multi-objective optimization on multiple candidate compilation optimization strategies based on the memory footprint and inference latency of each candidate compilation optimization strategy, so as to determine the compilation optimization strategy corresponding to the first optimization mode, including: Based on the memory usage and inference latency of each candidate compilation optimization strategy, a multi-objective optimization is performed on multiple candidate compilation optimization strategies to obtain a Pareto optimal solution set; The compilation optimization strategy corresponding to the first optimization mode is determined from the Pareto optimal solution set by threshold screening.

[0130] Optionally, in some embodiments, the generation module 830 can be used to perform multi-objective optimization on multiple candidate compilation optimization strategies based on the memory footprint and inference latency of each candidate compilation optimization strategy, to obtain a Pareto optimal solution set, including: Each candidate compilation optimization strategy is encoded as an individual of the genetic algorithm, resulting in a population consisting of multiple individuals; Perform the following operations iteratively until the preset iteration termination condition is met: Based on the memory usage and inference latency of the candidate compilation optimization strategy corresponding to each individual in the population, perform non-dominated sorting of the individuals in the population according to the Pareto dominance relationship to obtain the non-dominated sorting result; perform genetic evolution operation on the population based on the non-dominated sorting result to obtain a new population. In a population that meets the iteration termination condition, the solution set consisting of the candidate compilation optimization strategies corresponding to individuals that are not dominated by other individuals is determined as the Pareto optimal solution set.

[0131] Optionally, in some embodiments, the non-dominated ranking result includes the dominance level of each individual; the generation module 830 can be used to perform genetic evolution operations on the population based on the non-dominated ranking result to obtain a new population, including: Based on the memory usage and inference latency of each individual, the crowding level of each individual within its dominance level is determined. Based on each individual's dominance level and its crowding level within that dominance level, M individuals are selected from the population through a tournament selection process for crossover and mutation operations to obtain a new population; where M is a positive integer.

[0132] Optionally, in some embodiments, the generation module 830 can be used to determine the compilation optimization strategy corresponding to the first optimization mode from the Pareto optimal solution set through threshold filtering, including: From the Pareto optimal solution set, select one of the N candidate compilation optimization strategies whose first parameter does not exceed the corresponding threshold and whose second parameter is the smallest, as the compilation optimization strategy corresponding to the first optimization mode; The first parameter is either the memory usage or the inference latency of the candidate compilation optimization strategy, the second parameter is either the memory usage or the inference latency of the candidate compilation optimization strategy, and N is a positive integer.

[0133] Optionally, in some embodiments, the multiple compilation optimization modes include a second optimization mode, which is used to characterize a compilation optimization mode specifically targeting memory usage; when the target optimization mode is the second optimization mode, the generation module 830 can be used to optimize the first computation graph according to the compilation optimization strategy corresponding to the target optimization mode, including: The first computation graph is optimized according to the compilation optimization strategy corresponding to the second optimization mode; wherein, the computation graph optimized according to the compilation optimization strategy corresponding to the second optimization mode satisfies at least one of the following conditions: the memory reuse rate is greater than the preset memory reuse rate threshold, the tensor quantization bit width is lower than the preset quantization bit width threshold, and the peak memory usage is lower than the preset peak memory threshold.

[0134] Optionally, in some embodiments, the multiple compiler optimization modes include a third optimization mode, which is used to characterize a compiler optimization mode specifically for inference latency; when the target optimization mode is the third optimization mode, the generation module 830 can be used to optimize the first computation graph according to the compiler optimization strategy corresponding to the target optimization mode, including: The first computation graph is optimized according to the compilation optimization strategy corresponding to the third optimization mode; wherein the computation graph optimized according to the compilation optimization strategy corresponding to the third optimization mode satisfies at least one of the following conditions: the scheduling parallelism is greater than the preset parallelism threshold, the operator block size is greater than the preset block size threshold, and the inference latency is lower than the preset inference latency threshold.

[0135] The effects achievable in this embodiment can be found in the relevant embodiments of the above compilation method, and will not be repeated here.

[0136] Figure 4 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 4 As shown, the electronic device may include: a processor 1301, a communication interface 1302, a memory 1303, and a communication bus 1304, wherein the processor 1301, the communication interface 1302, and the memory 1303 communicate with each other via the communication bus 1304. The processor 1301 can call a computer program in the memory 1303 to execute the steps of a compilation method, such as: Obtain the first computational graph of the deep learning model; The target optimization mode is determined from multiple compilation optimization modes; wherein, the multiple compilation optimization modes include a first optimization mode, which is used to characterize the compilation optimization mode that simultaneously targets memory usage and inference latency; The first computation graph is optimized according to the target optimization mode to generate the NPU executable file.

[0137] Furthermore, when the logical instructions in the aforementioned memory can be implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0138] On the other hand, embodiments of this application also provide a computer program product, which includes a computer program. The computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can perform the steps of the compilation methods provided in the above embodiments, such as including: Obtain the first computational graph of the deep learning model; The target optimization mode is determined from multiple compilation optimization modes; wherein, the multiple compilation optimization modes include a first optimization mode, which is used to characterize the compilation optimization mode that simultaneously targets memory usage and inference latency; The first computation graph is optimized according to the target optimization mode to generate the NPU executable file.

[0139] On the other hand, embodiments of this application also provide a non-transitory computer-readable storage medium storing a computer program. The computer program is used to cause a processor to execute the steps of the methods provided in the above embodiments, including, for example: Obtain the first computational graph of the deep learning model; The target optimization mode is determined from multiple compilation optimization modes; wherein, the multiple compilation optimization modes include a first optimization mode, which is used to characterize the compilation optimization mode that simultaneously targets memory usage and inference latency; The first computation graph is optimized according to the target optimization mode to generate the NPU executable file.

[0140] Non-transitory computer-readable storage media can be any available medium or data storage device that can be accessed by a processor, including but not limited to magnetic storage (e.g., floppy disks, hard disks, magnetic tapes, magneto-optical disks (MOs), etc.), optical storage (e.g., CDs, DVDs, BDs, HVDs, etc.), and semiconductor storage (e.g., ROMs, EPROMs, EEPROMs, non-volatile memory (NAND flash), solid-state drives (SSDs)).

[0141] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.

[0142] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., including several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods of various embodiments or some parts of embodiments.

[0143] All actions involving the acquisition of signal information or data in this application are carried out in accordance with the relevant data protection laws and policies of the country where the application is located, and with the authorization of the owner of the relevant device.

[0144] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.

Claims

1. A method of compiling, characterized by, include: Obtain the first computational graph of the deep learning model; A target optimization mode is determined from multiple compilation optimization modes; wherein, the multiple compilation optimization modes include a first optimization mode, which is used to characterize a compilation optimization mode that simultaneously targets memory usage and inference latency; The first computation graph is optimized according to the target optimization mode to generate an executable file for the NPU.

2. The compiling method of claim 1, wherein, When the target optimization mode is the first optimization mode, the optimization process of the first computation graph according to the target optimization mode includes: Obtain multiple candidate compilation optimization strategies corresponding to the first optimization mode, as well as the memory usage and inference latency of each candidate compilation optimization strategy; Based on the memory usage and inference latency of each candidate compilation optimization strategy, a multi-objective optimization is performed on multiple candidate compilation optimization strategies to determine the compilation optimization strategy corresponding to the first optimization mode; The first computation graph is optimized according to the compilation optimization strategy.

3. The compiling method of claim 2, wherein, The step of performing multi-objective optimization on multiple candidate compilation optimization strategies based on the memory usage and inference latency of each candidate compilation optimization strategy to determine the compilation optimization strategy corresponding to the first optimization mode includes: Based on the memory usage and inference latency of each candidate compilation optimization strategy, a multi-objective optimization is performed on multiple candidate compilation optimization strategies to obtain a Pareto optimal solution set; The compilation optimization strategy corresponding to the first optimization mode is determined from the Pareto optimal solution set by threshold filtering.

4. The compilation method according to claim 3, characterized in that, The step of performing multi-objective optimization on multiple candidate compilation optimization strategies based on the memory usage and inference latency of each candidate compilation optimization strategy to obtain a Pareto optimal solution set includes: Each of the candidate compilation optimization strategies is encoded into an individual of the genetic algorithm, resulting in a population consisting of multiple individuals; The following operations are performed iteratively until a preset iteration termination condition is met: based on the memory usage and inference latency of the candidate compilation optimization strategy corresponding to each individual in the population, the individuals in the population are sorted non-dominated according to the Pareto dominance relationship to obtain a non-dominated sorting result; based on the non-dominated sorting result, the population is subjected to genetic evolution operations to obtain a new population; In a population that satisfies the iteration termination condition, the solution set consisting of the candidate compilation optimization strategies corresponding to individuals that are not dominated by other individuals is determined as the Pareto optimal solution set.

5. The compilation method according to claim 4, characterized in that, The non-dominated ranking result includes the dominance level of each individual; the step of performing genetic evolutionary operations on the population based on the non-dominated ranking result to obtain a new population includes: Based on the memory usage and inference latency values ​​corresponding to each individual, the crowding level of each individual within its respective dominance level is determined; Based on the dominance level of each individual and the crowding degree of each individual within its dominance level, M individuals are selected from the population through a tournament selection method to perform crossover and mutation operations, resulting in a new population; where M is a positive integer.

6. The compilation method according to claim 3, characterized in that, The step of determining the compilation optimization strategy corresponding to the first optimization mode from the Pareto optimal solution set through threshold filtering includes: From the Pareto optimal solution set, select one of the N candidate compilation optimization strategies whose first parameter does not exceed the corresponding threshold and whose second parameter is the smallest, as the compilation optimization strategy corresponding to the first optimization mode; Wherein, the first parameter is one of the memory usage value and the inference latency value of the candidate compilation optimization strategy, the second parameter is the other of the memory usage value and the inference latency value of the candidate compilation optimization strategy, and N is a positive integer.

7. The compilation method according to claim 1, characterized in that, The plurality of compiler optimization modes includes a second optimization mode, which is used to characterize a compiler optimization mode specifically targeting memory usage; When the target optimization mode is the second optimization mode, the step of optimizing the first computation graph according to the compilation optimization strategy corresponding to the target optimization mode includes: The first computation graph is optimized according to the compilation optimization strategy corresponding to the second optimization mode; wherein the computation graph optimized according to the compilation optimization strategy corresponding to the second optimization mode satisfies at least one of the following conditions: memory reuse rate is greater than a preset memory reuse rate threshold, tensor quantization bit width is lower than a preset quantization bit width threshold, and peak memory usage is lower than a preset peak memory threshold.

8. The compilation method according to claim 1, characterized in that, The plurality of compiler optimization modes includes a third optimization mode, which is used to characterize a compiler optimization mode specifically for inference latency; When the target optimization mode is the third optimization mode, the step of optimizing the first computation graph according to the compilation optimization strategy corresponding to the target optimization mode includes: The first computation graph is optimized according to the compilation optimization strategy corresponding to the third optimization mode; wherein the computation graph optimized according to the compilation optimization strategy corresponding to the third optimization mode satisfies at least one of the following conditions: the scheduling parallelism is greater than a preset parallelism threshold, the operator block size is greater than a preset block size threshold, and the inference latency is lower than a preset inference latency threshold.

9. An electronic device comprising a processor and a memory storing a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the compilation method according to any one of claims 1 to 8.

10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the compilation method according to any one of claims 1 to 8.