Deep learning model compiling method and compiler supporting multiple kinds of hardware

By designing hardware models and dynamically adjusting optimization paths, the problem that existing compilers cannot adapt to multiple hardware devices is solved, and efficient deep learning model compilation and inference are achieved, reducing compilation costs and improving inference speed.

CN119045823AActive Publication Date: 2024-11-29INST OF COMPUTING TECH CHINESE ACAD OF SCI

Patent Information

Application Number
CN202410642450.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-05-23
Publication Date
2024-11-29
Estimated Expiration
2044-05-23

AI Technical Summary

Technical Problem

Existing deep learning compilers lack universality and cannot adapt to multiple hardware devices. Compilation methods and optimization strategies are limited to a single hardware, resulting in high model compilation costs and low inference efficiency.

Method used

By designing hardware models, abstracting the hardware attributes of target devices, dynamically adjusting optimization paths and parameters, decoupling the compilation methods and hardware devices is achieved, and a deep learning model compiler suitable for a variety of hardware is generated.

Benefits of technology

It realizes efficient compilation and inference of the model on a variety of hardware devices, reduces compilation costs, maintains the original inference accuracy, and achieves a speed increase of 2-3 times in the best case.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119045823A_ABST
    Figure CN119045823A_ABST
Patent Text Reader

Abstract

The invention provides a deep learning model compiling method supporting multiple pieces of hardware and a compiler. The deep learning model compiling method supporting multiple pieces of hardware comprises the steps of obtaining a computational graph of a deep learning model needing to be compiled and target equipment information; converting the target equipment information into a hardware attribute intermediate form based on a preset hardware model, obtaining a linear operation intermediate form based on basic operation of a deep learning model, and obtaining a tensor shape intermediate form based on the tensor of the deep learning model; obtaining a memory intermediate form based on the tensor and the tensor shape intermediate form, and optimizing a memory copying behavior by adopting a heterogeneous memory transmission folding method; acquiring a loop intermediate form based on the acquired linear operation intermediate form, optimizing a loop in the loop intermediate form by adopting a fusion, stacking and vectorization method, converting the loop into a vector intermediate form, and acquiring a heterogeneous device data operation rule intermediate form based on a preset calculation model; and based on the plurality of acquired intermediate forms, acquiring an executable code which can be reasoned in the target equipment.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of deep learning technology, specifically to the field of hardware resource utilization technology of deep learning compilers, and more specifically to a deep learning compilation method and compiler that supports multiple hardware. Background Art

[0002] In the field of deep learning technology, breakthroughs have been made in many fields such as computer vision, natural language processing, and speech recognition. These advances have greatly promoted the development of artificial intelligence technology, making the application of deep learning models more and more extensive in our daily lives, from smart voice assistants to self-driving cars, to medical diagnosis and natural language translation, etc. The application of deep learning technology is almost everywhere. Behind this rapid development, it is not only the innovation in algorithm and model design, but more importantly, how these models can be efficiently deployed and run on various computing devices. As the scale of the model increases, the demand for hardware computing power also increases. This not only requires the continuous improvement of hardware performance to adapt to the growth of models and data, but also promotes the continuous evolution of deep learning training and reasoning frameworks, so as to achieve optimization for hardware performance and model requirements.

[0003] To address these challenges, a series of hardware accelerators and dedicated chips have been developed to provide more efficient computing power for deep learning applications. The diversity of these hardware platforms provides developers with a wealth of choices, from general-purpose graphics processing units (GPGPUs) to various dedicated accelerators, each with its own unique advantages and specific usage scenarios. However, hardware diversity is also accompanied by many challenges. Each hardware platform has its own specific architecture and software ecosystem, which means that in order to fully utilize the performance of these hardware, developers need to perform a lot of customized development for each platform. This not only increases the complexity of development, but also raises the threshold for efficiently deploying deep learning models in diverse hardware environments.

[0004] The introduction of compiler technology provides a more automated and universal optimization path. By abstracting the commonalities of hardware, the compiler aims to generate efficient execution code for different hardware platforms, reducing developers' direct dependence on hardware features. This approach not only improves the compatibility and performance of the model on a variety of hardware, but also helps reduce development complexity, making the deployment and optimization of deep learning models more efficient and flexible. Advances in compiler technology, especially the development of specialized compilers for deep learning models, provide powerful tools for achieving high-performance deep learning applications across hardware platforms, and open up a new path to optimize the inference performance of deep learning models.

[0005] However, the current deep learning compilation methods still have many shortcomings. At present, deep learning model compilers are mainly divided into two types: compilers developed by hardware manufacturers and compilers developed based on open source frameworks. The model compilation method of the first type of compiler is customized by the manufacturer according to the released hardware. The inference model compiled by this type is only suitable for a single hardware device and is not universal. If the second type of compiler is used to compile the model, if the hardware device changes, the compiler needs to be redeveloped before compilation. Because the hardware device has changed, the compiler's compilation method or model optimization strategy may not be able to adapt to the new hardware device. In order to better exert the hardware performance, it is necessary to modify the compilation method or optimization strategy, or even reconstruct the compiler, resulting in high model inference costs.

[0006] Based on the above analysis, the existing deep learning compilers have the following problems: (1) The compilers are not universal: In the current compiler stage, since the compilers are customized according to the hardware devices, it is impossible to compile the deep learning model into an inference model suitable for multiple hardware devices using only one compiler. The model compilation and inference process are strongly coupled with the hardware devices, and have limitations in applicability. It is impossible to achieve one compiler to adapt to multiple hardware devices; (2) There are limitations in compilation methods and optimization strategies: The compilation methods and optimization strategies in the existing compilers are only applicable to a single hardware device. The compilation methods and optimization strategies are specially customized for a certain hardware device. Due to differences in architecture, computing power, etc. between different hardware, they cannot be applied to other hardware devices. There are limitations in compilation methods and optimization strategies; (3) The model compilation cost is high: Since the compilation methods and optimization strategies in each compiler are only applicable to a single hardware device, in order to ensure that the compiled inference model can fully utilize the performance of the hardware device, it is necessary to redevelop the compiler according to the hardware device before model compilation, resulting in the problem of high model compilation cost. Summary of the invention

[0007] Therefore, the purpose of the present invention is to overcome the defects of the above-mentioned prior art and provide a deep learning compilation method and compiler that supports multiple hardware.

[0008] The objective of the present invention is achieved through the following technical solutions:

[0009] According to a first aspect of the present invention, the present invention provides a deep learning model compilation method supporting multiple hardwares, which is used to compile a deep learning model into an executable code that can perform model reasoning on a target device, and is characterized in that the method comprises: an information acquisition step: obtaining a computational graph and target device information of the deep learning model to be compiled, wherein the computational graph at least includes basic operations and tensors of the deep learning model; a logic generation step: converting the target device information into an intermediate form of hardware attributes based on a preset hardware model, obtaining an intermediate form of linear operations based on the basic operations of the deep learning model, and obtaining an intermediate form of tensor shape based on the tensors of the deep learning model; a memorization step: obtaining a memory intermediate form based on the obtained tensors of the deep learning model and the intermediate form of tensor shape , and use the first preset method to optimize the memory copy behavior corresponding to each memory segment in the memory intermediate form; loop processing step: based on the obtained linear operation intermediate form, obtain the loop intermediate form, use the second preset method, according to the hardware attribute intermediate form, select the corresponding strategy to optimize the loop in the loop intermediate form, and convert the optimized loop into a vector intermediate form. In addition, based on the preset computing model, obtain the intermediate form of heterogeneous device data operation rules; hardware code generation step: based on the hardware attribute intermediate form obtained in the logic generation step, the memory intermediate form obtained in the memorization step, and the loop intermediate form obtained in the loop processing step, obtain the executable code that can be inferred on the target device, wherein the hardware information represented by the hardware attribute intermediate form determines the hardware device adapted to the executable code.

[0010] Preferably, in the logic generation step, the preset hardware model is a three-level memory structure model, and different target devices convert the hardware information into an intermediate form of hardware attributes based on the hardware model according to the following steps: simplify the target device into a hardware model including a three-level memory structure of memory, cache, and register; based on the hardware model, obtain attributes that affect the computational efficiency of the deep learning model, wherein the attributes include at least hardware architecture, number of CPU cores, GPU / TPU tensor cores, cache size, hardware computing power, and bandwidth size, and the hardware architecture is a CPU architecture or a GPU architecture or a TPU architecture; convert all attributes obtained from the hardware model into an intermediate form of hardware attributes.

[0011] Preferably, in the memorization step, the first preset method is a heterogeneous memory transmission folding method, and the method is used to optimize the memory copy behavior according to the following steps, wherein each memory segment corresponds to multiple memory transmission behaviors, and each memory transmission behavior corresponds to a memory segment: traversing all memory copy behaviors, matching multiple memory copy behaviors with the same corresponding memory segments into groups to obtain memory copy behavior groups corresponding to multiple different memory segments; and optimizing the redundant memory copy behaviors in each memory copy behavior group and the memory management operations of the memory segment corresponding to the group by deletion / replacement.

[0012] Preferably, in the loop processing step, the second preset method is: using a fusion method to process redundant loops in the loop intermediate form to obtain a fused loop intermediate form; according to the hardware attribute intermediate form, adopting different stacking strategies to adjust the structure of all loops in the fused loop intermediate form to obtain a stacked loop intermediate form; setting a vectorization length coefficient according to the hardware attribute intermediate form, and converting the loops in the stacked loop intermediate form into a vector intermediate form according to the vectorization length coefficient.

[0013] Preferably, the method of processing redundant loops in the loop intermediate form by fusion to obtain a fused loop intermediate form includes: traversing all loops in the loop intermediate form, and fusing multiple loops that meet fusion conditions into one loop, and the fusion conditions include: Condition 1: multiple loops operate on the same memory segment and the memory between the multiple loops is not affected; Condition 2: multiple loops operate on different memories and the multiple loops have the same number of iterations; wherein, multiple loops that meet any one of Condition 1 and Condition 2 are deemed to meet the loop fusion condition, otherwise they are deemed to not meet the fusion condition; generating a fused loop intermediate form based on the fused loops and the unfused loops.

[0014] Preferably, the adopting of different stacking strategies to adjust the structures of all loops in the fused loop intermediate form according to the intermediate form of the hardware attributes to obtain the stacked loop intermediate form includes: obtaining the hardware architecture information of the target device based on the intermediate form in the hardware attributes, and then adopting corresponding stacking strategies according to the hardware architecture information to adjust the structure of the loop in the fused loop intermediate form, wherein the stacking strategies include: for the case where the target device is a homogeneous device, that is, the hardware architecture of the target device is a CPU architecture, the stacking strategy adopted is: stacking all loops with single-layer loop structures into two-layer loop structures, and then stacking 2 to 4 layers of loops according to the number of CPU cores of the target device; for the case where the target device is a heterogeneous device, that is, the hardware architecture of the target device is a GPU architecture or a TPU architecture, adopting The stacking strategy is: stacking the structures of all loops in the fused loop intermediate form into a multi-layer loop structure of at least three layers from the outside to the inside, including at least one outer loop, one inner loop and one innermost loop, wherein the outer loop is a grid responsible for managing multiple concurrently executed thread blocks, the inner loop is a thread block responsible for managing the execution of a group of threads, the innermost loop is a thread, and the internal loop of the thread is a single-layer loop or a multi-layer nested loop; adjusting the number of loops of the inner loop in all multi-layer loops to a preset number, and transferring the remaining number of loops of the inner loop to the outer loop, wherein the preset number corresponding to the GPU architecture is an arbitrary integer less than or equal to 1024, and the preset number corresponding to the TPU architecture is an arbitrary integer less than or equal to 512; generating a stacked loop intermediate form based on the stacked loops.

[0015] Preferably, the method of setting the vectorization length coefficient according to the intermediate form of hardware attributes includes: for a target device with a CPU architecture, obtaining the instruction set architecture of the target device based on the intermediate form of hardware attributes, setting the length coefficient according to the obtained instruction set architecture information, and vectorizing each loop according to the same length coefficient; for a target device with a GPU architecture and a TPU architecture, the length coefficient corresponding to each loop is set in the following manner: obtaining the Tensor Core operation scale of the target device based on the intermediate form of hardware attributes, wherein the Tensor Core is a tensor core of a GPU or TPU; and setting the length coefficient according to a corresponding strategy based on the number of innermost loop layers of a multi-layer loop, wherein if the innermost loop is a multi-layer nested loop, the length coefficient is set to the Tensor Core operation scale in the hardware attributes of the target device; otherwise, if the innermost loop is a single-layer loop, the length coefficient is set to 128 bytes.

[0016] Preferably, in the loop processing step, the computing model is used to generate data operation rules for heterogeneous devices, wherein the heterogeneous devices are target devices whose hardware architecture is a GPU architecture or a TPU architecture, and the computing model generates data operation rules for heterogeneous devices in the following manner: standardize each data operation in the deep learning model so that they have the same operation steps, and each data operation is performed according to the operation steps; based on all data operations after the standardized operation steps, save the same steps in the operation steps in the same queue to obtain multiple queues, wherein each queue saves the same steps in different data operations; add data dependencies between event processing queues to form a multi-stream parallel data operation operation mode, and then generate an intermediate form of the data operation rules based on this operation mode.

[0017] Preferably, in the hardware code generation step, the obtaining of executable code that can be inferred on the target device includes the following steps: obtaining the hardware architecture of the target device based on the hardware attribute intermediate form, thereby determining the type of instruction set supported by the target device; generating executable code based on the obtained loop intermediate form, vector intermediate form and memory intermediate form, wherein, during the generation process, all loops, vectors, and memory operations in the deep learning model are converted into computer instructions that call the target device supported.

[0018] According to a second aspect of the present invention, the present invention provides a deep learning model compiler supporting multiple hardware based on any method described in the first aspect of the present invention, which is used to compile a deep learning model into executable code supporting a target device through target device attributes, and is characterized in that the system includes: a logic generation module, a memorization module, a loop processing module, and a hardware code generation module, wherein: the logic generation module is used to obtain a linear operation intermediate form, a hardware attribute intermediate form, and a tensor shape intermediate form based on the acquired deep learning model and target device information, and generate a logic model of the deep learning model to be compiled, wherein the logic model includes a linear operation intermediate form, a tensor, a tensor shape intermediate form, and a hardware attribute intermediate form; the memorization module is used to obtain a memory intermediate form based on the tensors and tensor intermediate forms in the logic model, and use a memory folding method to optimize the memory copy behavior to generate a memory model of the deep learning model to be compiled, wherein the memory model includes a linear operation intermediate form. form, memory intermediate form, tensor shape intermediate form and hardware attribute intermediate form; the loop processing module is used to obtain the loop intermediate form based on the linear operation intermediate form in the memory model, and select the corresponding optimization strategy to adjust the structure of the loop according to the hardware attribute intermediate form, and convert the optimized loop into a vector intermediate form. In addition, the target device data operation rule intermediate form is obtained based on the preset multi-stream parallel computing model, and the loop model of the deep learning model that needs to be compiled is generated through the above operations, wherein the loop model includes the loop intermediate form, the memory intermediate form, the tensor shape intermediate form, the hardware attribute intermediate form, and the data operation rule intermediate form; the hardware code generation module is used to obtain the executable code that can perform reasoning tasks on the target device based on the loop processing module, wherein the loop, vector, and memory operations of the deep learning model are converted into computer instructions supported by the target device according to the hardware attribute intermediate description, so as to generate the reasoning model of the deep learning model that needs to be compiled.

[0019] According to a third aspect of the present invention, a computer-readable storage medium is provided, wherein a computer program is stored thereon, and the computer program can be executed by a processor to implement any of the method steps described in the first aspect of the present invention.

[0020] According to a third aspect of the present invention, an electronic device is provided, characterized in that it comprises: one or more processors; and a memory, wherein the memory is used to store executable instructions; and the one or more processors are configured to implement any method steps described in the first aspect of the present invention by executing the executable instructions.

[0021] Compared with the prior art, the advantages of the present invention are:

[0022] First, the present invention will design a hardware model, and abstract the hardware properties of the target device based on the model, and compile the model according to the hardware properties. Compared with the existing compilation method that uses the method of obtaining specific hardware models to implement model compilation, the present invention realizes the decoupling of the compilation method and the hardware device, which can meet the needs of model compilation on various hardware devices. Secondly, the model optimization strategy proposed in the present invention can dynamically adjust the optimization path, optimization parameters, etc. according to the hardware properties of the target device, realizing the decoupling of model optimization and hardware devices, and the compiled reasoning model can make full use of the hardware resources of the target device. Finally, based on the compiler constructed by the deep learning model compiler supporting multiple hardware proposed by the present invention, the deep learning model can be compiled into executable code supporting the target device according to the properties of the input target device, which is relative to the prior art that the target device needs to be reconstructed when the compiler changes. The present invention realizes that a compiler is adapted to multiple compilation tasks, effectively reducing the cost of model compilation. And compared with the existing deep learning compiler, there is no loss in model reasoning accuracy, the model reasoning speed can maintain the original level at the worst, and the best can reach 2-3 times the speed improvement. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] The embodiments of the present invention are further described below with reference to the accompanying drawings, in which:

[0024] Figure 1 A schematic diagram of a model conversion process according to an embodiment of the present invention;

[0025] Figure 2 A flow chart of a deep learning compilation method supporting multiple hardware according to an embodiment of the present invention;

[0026] Figure 3 A schematic diagram of a raw memory transfer paradigm according to an embodiment of the present invention;

[0027] Figure 4 A schematic diagram of a memory transmission paradigm optimized by a memory folding method according to an embodiment of the present invention;

[0028] Figure 5 Schematic diagram of a calculation model according to an embodiment of the present invention. DETAILED DESCRIPTION

[0029] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail by specific embodiments below. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.

[0030] As mentioned in the background technology section, existing compilers for compiling deep learning models have the following disadvantages: (1) The compiler is not universal: Existing deep learning compilers are not universal, and the deep learning model compilation method used is customized according to the hardware information of a certain target device. Since the hardware information of different hardware devices varies in type and quantity, the compilation method cannot be applied to other hardware devices; (2) There are limitations in optimization strategies: The optimization strategies in existing compilers are only applicable to a single hardware device. The optimization strategies are specially customized according to a certain hardware device. Since different hardware has differences in architecture, computing power, etc., they cannot be applied to other hardware devices. There are limitations in compilation methods and optimization strategies; (3) The model compilation cost is high: Since the compilation method and optimization strategy used by existing compilers are only applicable to a single hardware device, if the hardware of the target device changes, in order to ensure that the compiled inference model can fully utilize the performance of the hardware device, it is necessary to redevelop the compiler according to the hardware device before model compilation, so the model compilation cost is high.

[0031] To solve the above problems, the inventors have discovered through research that the existing deep learning compilers are not universal, and the deep learning model compilation method used is customized according to the hardware information of a certain target device. Since the hardware information of different hardware devices differs in type and quantity, the compilation method cannot be applied to other hardware devices. If a standard is set and the hardware information of different target devices is abstracted according to the standard, the applicability of the compilation method can be improved. Therefore, the present invention proposes a hardware model, which designs a hardware model and obtains the hardware properties of the target device based on the model, thereby abstracting different hardware devices into hardware models with the same structure, and compiling the model according to the hardware properties, thereby realizing the compilation method. The decoupling of the method and the hardware device can meet the needs of model compilation on multiple hardware devices. In addition, the optimization strategy in the existing compiler is only applicable to a single hardware device. The optimization strategy is specially customized according to a certain hardware device. Due to the differences in architecture, computing power, etc. between different hardware, it cannot be applied to other hardware devices. There are limitations in the compilation method and optimization strategy. If the hardware attributes of the target device are used as parameters of the optimization strategy, then the decoupling of model optimization and hardware devices can be achieved. Therefore, the model optimization strategy proposed in the present invention can dynamically adjust the optimization path, optimization parameters, etc. according to the hardware attributes of the target device, and the decoupling of model optimization and hardware devices is achieved. The compiled reasoning model can make full use of the hardware resources of the target device. Finally, the model compilation cost is high: since the compilation method and optimization strategy adopted by the existing compiler are only applicable to a single hardware device, if the hardware of the target device changes, in order to ensure that the compiled reasoning model can make full use of the performance of the hardware device, it is necessary to redevelop the compiler according to the hardware device before model compilation, so the model compilation cost is high. If the compiler can adjust the model optimization strategy and compilation method according to the hardware information of the input target device, then the function of a compiler adapting to multiple compilation tasks can be realized. Therefore, the present invention proposes a deep learning model compiler that supports multiple hardware. Compared with existing deep learning compilers, this compiler has no loss in model inference accuracy, and the model inference speed can at worst maintain the original level, and at best can achieve a 2-3 times speed improvement.

[0032] Before specifically introducing the embodiments of the present invention, some of the terms used therein are explained as follows:

[0033] Basic operations: Refers to the various mathematical and data processing steps in deep learning models.

[0034] IR (Intermediate Representation): is an intermediate form used to represent deep learning models. It can represent the structure and calculation process of neural network models in the form of computational graphs. By representing deep learning models as IR, it can be used for model cross-framework conversion, model performance optimization, model cross-platform deployment, and model interpretation and analysis.

[0035] ONNX IR (Open Neural Network Exchange Intermediate Representation): ONNX is an open, cross-platform deep learning model exchange format. It allows models to be shared and deployed between different deep learning frameworks (such as TensorFlow, PyTorch, etc.). ONNX IR is an intermediate format for representing all operations of deep learning models. It provides a unified model representation to make it easier to exchange models between different deep learning frameworks.

[0036] Tensor IR (Tensor Intermediate Representation): Tensor means multidimensional array, which is usually used to represent data and weights in neural networks. Tensor IR is an intermediate format for representing tensors in deep learning models. This representation helps to operate and optimize data.

[0037] Linalg IR (Linear Algebra Intermediate Representation): Linalg is the abbreviation of "Linear Algebra", which involves vectors, matrices, and linear equations. Linalg IR is an intermediate form of representing logical operations in neural networks. The logical operations in the present invention mainly include traversing the input and output and performing common matrix multiplication, vector addition, and other operations on each traversed element.

[0038] Device IR (Device Intermediate Representation): Device refers to hardware devices, such as CPU (central processing unit), GPU (graphics processing unit) TPU (), etc. Device IR represents the properties of hardware devices, such as hardware architecture, number of CPU cores, GPU / TPU tensor cores, cache size, hardware computing power, bandwidth size, etc.

[0039] Shape IR (Shape Intermediate Representation): Shape refers to the dimensional information of a tensor. Shape IR is an intermediate form that represents the shape information (i.e. the dimension of a tensor) of a tensor in a deep learning model, which is very important for compiler optimization and memory allocation.

[0040] MemRef IR (Memory Reference Intermediate Representation): MemRef is the abbreviation of Memory Reference, which means memory reference. MemRef IR represents the intermediate form of all memory segments corresponding to logical operations in the deep learning model.

[0041] Affine IR (Affine Intermediate Representation): Affine describes the number of layers and iterations of a loop in this invention. Affine IR represents the intermediate form of a loop in a deep learning model and is used to maintain elements such as loop conditions, loop counts, and loop iteration size.

[0042] Vector IR (Vector Intermediate Representation): Vector refers to a series of data or operations. Vector IR is an intermediate form of representing vectors in deep learning models.

[0043] DMA IR (Direct Memory Access Intermediate Representation): DMA is the abbreviation of direct memory access, which is a computer technology used to achieve high-speed data transfer. DMA IR is an intermediate form of the rules for operations and data transfer on the target hardware device. This is critical for efficient data transfer and memory management.

[0044] LLVM IR (LLVM IR Intermediate Representation): LLVM is a compiler infrastructure for optimizing and transforming code. LLVM IR is the machine code generated by the LLVM compiler that represents basic functions such as loop jumps in deep learning models. This machine code can be deployed on inference devices to perform inference tasks.

[0045] Device ISA (Instruction Set Architecture): It is a set of instructions that can be executed on a specified device. In the field of deep learning compilation, Device ISA usually refers to the instruction set supported by specific hardware (such as GPU, TPU, CPU, etc.), which contains instructions that can be directly executed by the hardware. The compiler compiles the deep learning model into an instruction sequence suitable for the device.

[0046] Homogeneous device: a hardware device that contains only one type of processor, the CPU.

[0047] Heterogeneous devices: hardware devices that contain one of the two processor types: CPU and TPU or GPU.

[0048] In order to better understand the present invention, it is described in detail below with reference to specific embodiments.

[0049] According to one embodiment of the present invention, the present invention proposes a deep learning compilation method that supports multiple hardware, which is used to compile a deep learning model so that it can implement the reasoning process on various labeling devices. The overall compilation process is as follows: Figure 1 and Figure 2 As shown, among which, Figure 2 As shown, the deep learning compilation method of the present invention includes five steps: information acquisition step, logic generation step, memory step, loop processing step, and hardware code generation step. Among them, the logic generation step corresponds to Figure 1 The logical generation process of Figure 1 The memorization process, loop generation and loop optimization correspond to Figure 1 The loop generation process and loop optimization process of the hardware code generation step correspond to Figure 1 The hardware code generation process. Figure 2 The five steps shown in the figure introduce in detail a deep learning compilation method supporting multiple hardwares proposed by the present invention. It should be noted that the present invention involves multiple intermediate form conversion operations (i.e., IR conversion). Since the implementation of IR conversion is a technology known to those skilled in the art, each intermediate form conversion operation is referred to as IR conversion in the embodiment of the present invention, and the technical details of IR conversion are not described in detail.

[0050] 1. Information Acquisition Steps

[0051] In the logic generation step, the computation graph and target device information of the deep learning model to be compiled are obtained, wherein the computation graph includes at least operations and tensors of the deep learning model. In the present invention, ONNX IR and Tensor IR intermediate forms are used to represent the basic operations and tensors in the computation graph of the deep learning model to be compiled.

[0052] 2. Logic Generation Steps

[0053] In the logic generation step, the target device information is converted into an intermediate form of hardware attributes based on a preset hardware model, the intermediate form of linear operations is obtained based on the basic operations of the deep learning model, and the intermediate form of tensor shape, i.e., shape IR, is obtained based on the tensor of the deep learning model. In subsequent steps, the memory space of the same size as Shape IR will be planned to store its elements. At the same time, the storage method and arrangement order of the tensor in the memory are also determined according to Shape IR.

[0054] According to an embodiment of the present invention, the intermediate form of the linear operation is Linalg IR, which uses linear operations to describe how to traverse the input and output of the operation and what operation needs to be performed on each traversed element.

[0055] According to one embodiment of the present invention, the preset hardware model is a three-level memory structure model. In the present invention, different target devices are based on the three-level memory structure model and convert the hardware information into an intermediate form of hardware attributes according to the following steps, and use Device IR to represent it. First, simplify the target device into a hardware model containing a three-level memory structure of memory, cache, and register. The actual hardware structure of the target device is relatively complex. Simplifying it into a three-level structure facilitates the acquisition of hardware attribute abstraction. Then, based on the hardware model, obtain the attributes that affect the computational efficiency of the deep learning model, wherein the attributes at least include hardware architecture, number of CPU cores, GPU / TPU tensor core, cache size, hardware computing power, and bandwidth size. The hardware architecture is a CPU architecture or a GPU architecture or a TPU architecture. The obtained hardware attributes are combined to form the hardware attributes of the target device; finally, all the attributes obtained from the hardware model are converted into an intermediate form of hardware attributes. The compiler adaptively adjusts the compilation method and optimization strategy according to the obtained hardware attributes, thereby converting the deep learning model into executable code that can perform model reasoning on the target device.

[0056] According to one embodiment of the present invention, the acquisition of attributes that affect the computational efficiency of the deep learning model needs to consider the following constraints: (1) The total size of the cache on the inference device is limited, and its transmission speed is related to the bandwidth. (2) The time consumption of each calculation is the sum of the time for data to be transferred from the cache to the register, the calculation time obtained based on the computing power and the amount of computation, and the time for the register to be transferred to the cache. However, since the bandwidth from the cache to the register is extremely high, only the calculation time obtained based on the computing power is considered. (3) Each transmission and calculation time are independent of each other. Therefore, when compiling the model, it is necessary to provide the compiler with hardware attributes such as hardware architecture, number of CPU cores, GPU / TPU tensor cores, cache size, hardware computing power, bandwidth size, etc., and the hardware architecture is CPU architecture or GPU architecture or TPU architecture.

[0057] According to one embodiment of the present invention, the logic generation step also includes a tensor reuse operation, that is, an element-by-element operation on a tensor. If there is only one consumer for the output result of the operation, the operation can be performed directly in situ without using a new tensor. This method can avoid repeated computing operations, thereby reducing computing overhead, and improve computing efficiency and speed during model inference. On the other hand, it can reduce memory usage, effectively reduce memory consumption, and improve model performance and efficiency.

[0058] 3. Memorization Steps

[0059] In the memorization step, a memory intermediate form is obtained based on the tensors and tensor shape intermediate forms of the acquired deep learning model, and a first preset method is used to optimize the memory copy behavior corresponding to each memory segment in the memory intermediate form.

[0060] According to an embodiment of the present invention, the tensor of the deep learning model is the Tensor IR obtained in the information acquisition step, and the intermediate form of the tensor shape is the Shape IR obtained in the logic generation step. Based on the Tensor IR and the Shape IR, the IR conversion is performed to generate the memory intermediate form MemRefIR. MemRefIR uses a specific memory segment to represent the tensor, and can calculate the offset and adjust the memory layout. In the IR conversion process, the transformation of the tensor will involve the life cycle calculation of the memory segment. The use of a memory segment involves allocation operations, read and write operations, and release operations. The present invention will record the life cycle m of each memory segment. x, x represents the serial number of the memory segment, and changes its state according to the impact of each operation. Specifically, there are four possible memory effects of the operator, namely allocation, reading, writing, and release. For each memory segment, the initialization includes a memory allocation operation, which will be the beginning of the life cycle of the memory segment. The operator will then perform corresponding operations on the memory segment, resulting in corresponding read or write effects. After all operations are completed, the memory segment will be released, and the life cycle of the corresponding memory segment will be ended through this operation. The recorded life cycle will also involve the optimization of memory copy behavior.

[0061] According to an embodiment of the present invention, the first preset method is a heterogeneous memory transmission folding method, and the method is used to optimize the memory copy behavior according to the following steps, wherein each memory segment corresponds to multiple memory transmission behaviors, and each memory transmission behavior corresponds to a memory segment. Traverse all memory copy behaviors, match multiple memory copy behaviors with the same corresponding memory segments into groups, so as to obtain memory copy behavior groups corresponding to multiple different memory segments; and optimize the redundant memory copy behaviors in each memory copy behavior group and the memory management operations of the memory segment corresponding to the group by deletion / replacement.

[0062] In order to better understand the steps of optimizing memory transfer operations using the heterogeneous memory transfer folding method, the following will be based on Figure 3 The memory transfer paradigm is shown in Figure 1 and described in detail with specific examples. Figure 3 As shown, the figure shows a memory transfer paradigm including three operations and multiple memory copy behaviors. The three operations are (1) C = GEMM (A, B), which is a matrix multiplication operation for two tensors stored in memory segments A and B on the CPU side; (2) D = RELU (C), which is a rectified linear unit operation for the tensor stored in memory segment C on the CPU side; (3) Y = ADD (C, D), which is an addition operation for two tensors stored in memory segments C and D on the CPU side. The multiple memory copies include: (1) the device side initiates a copy to the CPU side, namely, memory copy 3, memory copy 5 and memory copy 8, and the three memory copy behaviors correspond to memory segment C, memory segment D and memory segment Y respectively. (2) the CPU side initiates a copy to the device side, namely, memory copy 1, memory copy 2, memory copy 4, memory copy 6 and memory copy 7, and the five memory copy behaviors correspond to memory segment A, memory segment B, memory segment C, memory segment C and memory segment D respectively. It can be seen that each operation requires copying data from the CPU side and then copying the operation result to the CPU side. The time cost required for each copy operation will affect the computational efficiency of the deep learning model. Figure 3Taking the transmission paradigm shown as an example, the steps of eliminating redundant memory copy behaviors of the heterogeneous memory transmission folding method are described in detail. First, the operation traverses all memory copy behaviors. If it is a memory copy behavior that initiates a copy from the device side to the CPU side, the memory copy behavior and its correspondence with the memory segment are saved in the global record table. That is, memory copy 3, memory copy 5, and memory copy 8 and their correspondence with the memory segment will be saved in the global record table during the traversal process. If the memory copy behavior is a copy initiated from the CPU side to the device side, then the memory copy behavior with the same memory segment is searched in the global record table. There may be two situations for the search results. Case 1: There is a memory copy operation that meets the query conditions in the global record table, such as Figure 3 Memory copy 4, memory copy 7, and memory copy 8 in the memory copy table. Taking memory copy 4 as an example, memory copy 4 corresponds to memory segment C, and memory copy 3 in the global record table also corresponds to memory segment C, that is, the two memory copy behaviors match successfully. The following describes in detail how to eliminate redundant operations in the two memory copy behaviors. Delete the copy behavior 3 on the computing device side to write the calculation result of C=GEMM(A,B) back to the CPU's memory segment release operation and the memory copy 4 to read the memory segment into the computing device corresponding to the memory allocation operation, modify the D=RELU(C) to obtain memory segment C operation to obtain C=GEMM(A,B) and write it back to the CPU corresponding memory operation, then record the copy behavior initiated from the device side to the CPU side, that is, memory copy 3, into the to-be-deleted list, and delete the memory copy behavior from the CPU side to the device side, that is, delete memory copy 4. The whole process can be summarized as: Figure 2 Delete the second half of each pair of behaviors in the dotted box, add the first half to the list to be deleted, and replace all uses of the second half with the memory corresponding to the first half. Case 2: There is no memory copy behavior that meets the query conditions in the global record table, such as Figure 3 Memory copy 1 and memory copy 2 in the , this case is skipped and not processed. Then, after completing the traversal of all memory copy behaviors, the list to be deleted includes memory copy 3 and memory copy 5, and all memory copy behaviors in the list to be deleted are deleted. Finally, the memory transfer paradigm optimized by the heterogeneous memory transfer folding method is obtained. The optimization results are as follows Figure 4 As shown, compared Figure 3 It can be concluded that the complex memory copy operations between the three operations are removed, and the data required for each operation is directly obtained from the previous operation, thereby eliminating redundant context switching operations, effectively improving the data access and reading speed, and greatly improving the inference efficiency of the model on the computing device. At the same time, the algorithm does not involve the hardware parameters of the target device, so that the model memory optimization is independent of the device parameters, and can optimize the deep learning model on different target devices, which is universal.

[0063] 4. Cycle Processing Steps

[0064] In the loop processing step, a loop intermediate form is obtained based on the obtained linear operation intermediate form, and a second preset method is adopted to select a corresponding strategy to optimize the loop in the loop intermediate form according to the hardware attribute intermediate form, and the optimized loop is converted into a vector intermediate form. In addition, an intermediate form of heterogeneous device data operation rules is obtained based on a preset computing model.

[0065] According to one embodiment of the present invention, the present invention performs IR conversion on the Linalg IR obtained in the logic generation step to generate an Affine IR that describes a specific loop. During the conversion process, the operation of traversing to obtain elements is used as the inside of the loop structure without major changes, and each dimension of the input and output is used as a layer of the loop structure. The traversal method determines the number of loops and the loop conditions. The Affine IR contains a basic loop body structure and maintains elements such as the loop condition, the number of loops, and the loop iteration step size. The loop condition in Affine may be in the form of an Affine expression. The Affine expression mathematically describes a high-dimensional space whose dimensions are the number of layers and the number of iterations of the loop. The reading and writing of data within the loop constitutes access to the space.

[0066] According to one embodiment of the present invention, the second preset method is: using a fusion method to process redundant loops in Affine IR to obtain fused Affine IR; according to Device IR, adopting different stacking strategies to adjust the structure of all loops in the fused loop intermediate form to obtain stacked Affine IR; setting the vectorization length coefficient according to the hardware attribute intermediate form, and converting the loops in the stacked loop intermediate form into VectorIR according to the vectorization length coefficient. Compared with the model optimization method in the existing compiler, the optimization strategy of this method is not limited to a single target device. During the model compilation process, the optimization path can be dynamically adjusted according to the acquired hardware device attributes. The compiled model can achieve native framework level performance on platforms such as CPU, GPU, TPU, and can even achieve 2-3 times acceleration.

[0067] According to one embodiment of the present invention, the redundant loops in Affine IR are processed by fusion to obtain the fused Affine IR, including: traversing all loops converted from calculation operations, and then fusing multiple loops that meet the fusion conditions into one loop, the fusion conditions including: Condition 1: multiple loop structures operate on the same memory segment and the memory between the multiple loops is not affected; Condition 2: multiple loop structures operate on different memories and the iteration times of the multiple loop structures are consistent; wherein, multiple loop structures that meet any one of Conditions 1 and 2 are deemed to meet the loop fusion conditions, otherwise the two loop structures are deemed to not meet the fusion conditions. In order to better understand the steps of fusing redundant loops, the following will be described in detail with specific examples.

[0068] Schematically, assume that the following loops exist in Affine IR: Loop 1: traverse the elements in memory space M1 and add 5 to each element. Loop 2: traverse the elements in memory space M1 and multiply each element by 5. Loop 3: traverse the elements in memory space M2, which includes 64 elements and is only one-dimensional. Loop 4: traverse the elements in memory space M3, which includes 64 elements and is only one-dimensional. Between loop 1 and loop 2, there are no other operations that modify the elements in memory space M1. Combining the fusion conditions, it can be concluded that loop 1 and loop 2 meet condition 1, the two loops operate on the elements of the same memory space, and there are no other memory operations between the two operations. This loop can be fused into one loop, that is, traverse all elements in memory space M1, and add 5 to each element first, and then multiply the addition result by 5. It should be understood that if multiple loops meet condition 1, but the multiple loops have differences in the elements operated and the number of operated elements, the multiple loops can also be fused. By integrating redundant loops in deep learning models, we can reduce memory usage and improve memory utilization, thereby optimizing the computing efficiency and operating performance of the model.

[0069] According to one embodiment of the present invention, according to Device The hardware attribute information in the IR is used to adopt different stacking strategies to adjust the structure of all loops in the intermediate form of the fused loop to obtain the stacked intermediate form of the loop, including: obtaining the hardware architecture information of the target device based on the intermediate form in the hardware attributes, and then adopting the corresponding stacking strategy according to the hardware architecture information to adjust the structure of the loop in the intermediate form of the fused loop, wherein the stacking strategy includes: (1) when the target device is a homogeneous device, that is, the hardware architecture of the target device is a CPU architecture, all loops with single-layer loop structures are stacked into a two-layer loop structure, and then 2 to 4 layers of loops are stacked according to the number of CPU cores of the target device; (2) when the target device is a heterogeneous device, that is, the hardware architecture of the target device is a GPU architecture or a TPU architecture, the structure of all loops in the intermediate form of the fused loop is stacked into a multi-layer loop structure of at least three layers from the outside to the inside, including at least one outer loop, one inner loop and one innermost loop, wherein the outer loop is a grid responsible for managing multiple concurrently executed thread blocks, the inner loop is a thread block responsible for managing the execution of a group of threads, the innermost loop is a thread, and the internal loop of the thread is a single-layer loop or a multi-layer nested loop. Then, the number of loops of the inner loops in all multi-layer loops is adjusted to a preset number, and the remaining number of loops of the inner loops is transferred to the outer loops, wherein the preset number corresponding to the GPU architecture is an arbitrary integer less than or equal to 1024, and the preset number corresponding to the TPU architecture is an arbitrary integer less than or equal to 512. The stacking strategy of the present invention is highly adaptable and flexible, and can dynamically optimize the loop structure based on different hardware device properties, thereby maintaining the best computing performance and hardware resource utilization efficiency under various target devices. Such a strategy enables deep learning models to run more efficiently on a variety of hardware platforms.

[0070] According to one embodiment of the present invention, setting the vectorization length coefficient according to the intermediate form of hardware attributes can improve the hardware resource utilization rate of the compiled model for the target device, and the vectorization length coefficient setting method includes: for the target device of CPU architecture, obtaining the target device instruction set architecture based on the intermediate form of hardware attributes, setting the length coefficient according to the obtained instruction set architecture information, and each loop is vectorized according to the same length coefficient; for the target device of GPU architecture and TPU architecture, the length coefficient corresponding to each loop is set in the following manner: obtaining the Tensor Core operation scale of the target device based on the intermediate form of hardware attributes, wherein the Tensor Core is the tensor core of the GPU or TPU; according to the number of innermost loop layers of multi-layer loops, adopting corresponding strategies to set the length coefficient, wherein the innermost loop is a multi-layer nested loop, the length coefficient is set to the Tensor Core operation scale in the hardware attributes of the target device, and vice versa, the innermost loop is a single-layer loop, and the length coefficient is set to 128 bytes. The present invention adaptively adjusts the vectorization parameters based on the obtained target hardware attributes, and can give full play to the hardware computing capabilities of different target devices, significantly improving computing efficiency, data throughput and computing stability.

[0071] According to one embodiment of the present invention, the computing model is used to generate data operation rules for heterogeneous devices, wherein the heterogeneous device is a target device whose hardware architecture is a GPU architecture or a TPU architecture and supports a parallel mode in which multiple computing functions are executed simultaneously, and the computing model generates multi-stream parallel data operation rules for heterogeneous devices according to the following steps: standardize each data operation in the deep learning model so that it has the same operation steps, and each data operation is executed according to the operation steps; based on all data operations after the standardized operation steps, the same steps in the operation steps are saved in the same queue to obtain multiple queues, wherein the same steps in different data operations are saved in each queue; add data dependencies between event processing queues to form a multi-stream parallel data operation operation mode, and generate an intermediate form of data operation rules based on this operation mode. It can improve the parallelism of heterogeneous devices, ensure the correctness of model operations, and maximize the use of hardware resources.

[0072] In order to better understand the steps of integrating the calculation model to generate multi-stream parallel data operation rules, the following will combine Figure 4 Describe in detail, such as Figure 4As shown in the figure, a method for implementing the computational model is shown, in which all data operations are standardized so that they have the same steps, namely, the steps including data transmission steps and computation steps, and then the data transmission steps and computation steps corresponding to C=GEMM(A,B), D=RELU(C), and C=GEMM(A,B) are stored in the data transmission queue and the computation queue respectively, and then the data dependency between the queues is solved by adding events. In the actual reasoning process, each queue is an operation flow, making full use of the characteristics of heterogeneous devices supporting the parallel execution of multiple functions, and effectively improving the model reasoning speed. It should be understood that the data operations are standardized into the same operation steps, and the steps and number of steps included in the operation steps are not limited, and users can selectively configure them according to their needs.

[0073] 5. Hardware code generation steps

[0074] In the hardware code generation step, based on the loop intermediate form, vector intermediate form, memory intermediate form and hardware attribute intermediate form obtained in the logic generation step, the memorization step and the loop processing step, an executable code that can be inferred on the target device is obtained, wherein the hardware information represented by the hardware attribute intermediate form determines the hardware device to which the executable code is adapted.

[0075] According to one embodiment of the present invention, obtaining executable code that can be used for reasoning on a target device includes the following steps: obtaining the hardware architecture of the target device based on the intermediate form of hardware attributes, thereby determining the type of instruction set supported by the target device; generating executable code based on the obtained loop intermediate form, vector intermediate form, and memory intermediate form, wherein, during the code generation process, all loops, vectors, and memory operations in the deep learning model are converted into computer instructions that call the target device. This method converts the basic operations in the deep learning model into computer instructions that call the target device based on the properties of the target device, so as to achieve the best adaptation of the model to the target device. In order to better understand the steps of generating executable code, the following will be described in detail with specific examples.

[0076] Indicatively, the loop structure described by Affine IR is further converted into basic judgment and jump instructions, and the memory operations contained in Vector IR and MemRefIR will also be converted into specific computer instructions after calculating the offset. This process will be converted into different results depending on the hardware. For example, the CPU side is mainly AVX, the GPU side is PTX, and the TPU side is TPU. At the same time, a general computing function is used to implement the model call, and a general computing function is designed with a consistent function signature, so that the same general computing function and the same deep learning model can perform reasoning tasks on different hardware. During model reasoning, all operations will be implemented by calling the computer instructions supported on the device, thereby realizing the decoupling of the model from the hardware. It should be understood that in the hardware code compilation step, the supported hardware instruction sets are not limited to AVX, PTX, and TPU, and users can add other hardware instruction sets in the hardware code compilation step as needed.

[0077] Based on the above-mentioned deep learning compilation method that supports multiple hardwares, the present invention also proposes a deep learning model compiler that supports multiple hardwares, which is used to compile the deep learning model into executable code that supports the target device through the target device attributes. The system includes: a logic generation module, a memory module, a loop processing module, and a hardware code generation module, wherein the logic generation module is used to obtain a linear operation intermediate form, a hardware attribute intermediate form, and a tensor shape intermediate form based on the acquired deep learning model and target device information, and generate a logical model of the deep learning model to be compiled, wherein the logical model includes a linear operation intermediate form, a tensor, a tensor shape intermediate form, and a hardware attribute intermediate form; the memory module is used to obtain a memory intermediate form based on the tensors and tensor intermediate forms in the logic model, and use a memory folding method to optimize the memory copy behavior to generate a memory model of the deep learning model to be compiled, wherein the memory model includes a linear operation intermediate form, a hardware attribute intermediate form, and a tensor shape intermediate form. The intermediate form, tensor shape intermediate form and hardware attribute intermediate form are used; the loop processing module is used to obtain the loop intermediate form based on the linear operation intermediate form in the memory model, and select the corresponding optimization strategy to adjust the structure of the loop according to the hardware attribute intermediate form, and convert the optimized loop into a vector intermediate form. In addition, the target device data operation rule intermediate form is obtained based on the preset multi-stream parallel computing model, and the loop model of the deep learning model that needs to be compiled is generated through the above operations, wherein the loop model includes the loop intermediate form, the memory intermediate form, the tensor shape intermediate form, the hardware attribute intermediate form, and the data operation rule intermediate form; the hardware code generation module is used to obtain the executable code that can perform reasoning tasks on the target device based on the loop processing module, wherein the loop, vector, and memory operations of the deep learning model are converted into computer instructions that call the target device supported according to the hardware attribute intermediate description, so as to generate the reasoning model of the deep learning model that needs to be compiled.

[0078] In order to better illustrate the technical effect of the present invention, the inventors designed a number of comparative experiments. The following is a combination of comparative experiments and comparative experimental results to further demonstrate the technical effect of the present invention.

[0079] In order to verify the compatibility of the compilation method supporting multiple hardware proposed in the present invention, by comparing the hardware types supported by the ONNXRuntime deep learning model accelerator, TensorRT, TPU-MLIR and the compiler proposed in the present invention, the results shown in Table 1 can be obtained. The following conclusions can be drawn from the data in the analysis table: ONNXRuntime deep learning model accelerator, TensorRT and TPU-MLIR only support some hardware in CPU, GPU and TPU. For the adaptation of other hardware, only ONNXRuntime deep learning model accelerator can adapt to other hardware by re-implementing the calculation kernel. The deep learning compiler proposed in the present invention not only supports common CPU, GPU and TPU hardware, but also can complete the adaptation of other hardware through simple extension and addition, and has excellent device compatibility and scalability.

[0080] Table 1: Comparison of compiler support for hardware

[0081]

[0082] In order to further verify the reasoning accuracy of the inference model compiled by the compiler proposed in the present invention, the inventors compared the Add, Linear, Conv, Relu, and Maxpool operation accuracy of the deep learning model when it was running on the ONNXRuntime deep learning model accelerator and when the deep learning model was compiled by TensorRT, TPU-MLIR and the compiler proposed in the present invention to obtain the comparative experimental results shown in Table 2. The left side of the non-zero item in the table represents the mean of the absolute error, and the right side represents the mean of the relative error. By analyzing the experimental results, the following conclusions can be drawn: the model compiled by the compiler proposed in the present invention basically remains error-free in different operations, and even if there is an error, it is close to the single-precision floating point representation range, so it can be considered that there is no error. It can be concluded that the model reasoning accuracy is not lost when the compiler proposed in the present invention is compared with the existing deep learning compiler.

[0083] Table 2: Experimental results of inference model accuracy comparison

[0084]

[0085] In order to further verify the inference speed of the inference model compiled by the compiler proposed in the present invention, the inventors compared the operation time and inference time of the inference model compiled by the compiler proposed in the present invention when running on the ONNXRuntime deep learning model accelerator and when running on the deep learning model accelerator via TensorRT, TPU-MLIR, and obtained the comparative experimental results shown in Tables 3 and 4. From the analysis of the data in the tables, the following conclusions can be drawn: the inference model compiled by the deep learning compiler supporting multiple hardware proposed in the present invention can achieve an optimal improvement of 2 to 3 times in inference speed and operation time compared with the prior art, and can maintain the same level as the existing compiler in the worst case.

[0086] Table 3: Experimental results of time comparison of inference model operation

[0087]

[0088] Table 4: Comparison of inference model inference time experimental results

[0089]

[0090] It should be noted that although the above describes the various steps in a specific order, it does not mean that the various steps must be executed in the above specific order. In fact, some of these steps can be executed concurrently or even in a different order as long as the required functions can be achieved.

[0091] The present invention may be a system, a method and / or a computer program product. The computer program product may include a computer-readable storage medium carrying computer-readable program instructions for causing a processor to implement various aspects of the present invention.

[0092] Computer readable storage medium can be a tangible device that holds and stores instructions used by an instruction execution device. Computer readable storage medium can include, for example, but is not limited to, an electrical storage device, a magnetic storage device, an optical storage device, an electromagnetic storage device, a semiconductor storage device, or any suitable combination thereof. More specific examples (non-exhaustive list) of computer readable storage medium include: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a static random access memory (SRAM), a portable compact disk read-only memory (CD-ROM), a digital versatile disk (DVD), a memory stick, a floppy disk, a mechanical encoding device, for example, a punch card or a protruding structure in a groove on which instructions are stored, and any suitable combination thereof.

[0093] The embodiments of the present invention have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The selection of terms used herein is intended to best explain the principles of the embodiments, practical applications, or technical improvements in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.

Claims

1. A deep learning model compilation method supporting multiple hardwares, used to compile a deep learning model into executable code that can perform model reasoning on a target device, characterized in that: The method comprises: Information acquisition step: acquiring a computational graph and target device information of a deep learning model to be compiled, wherein the computational graph includes at least basic operations and tensors of the deep learning model; Logic generation step: converting target device information into an intermediate form of hardware attributes based on a preset hardware model, obtaining an intermediate form of linear operations based on basic operations of a deep learning model, and obtaining an intermediate form of tensor shape based on a tensor of a deep learning model; Memorization step: based on the obtained tensors and tensor shape intermediate forms of the deep learning model, a memory intermediate form is obtained, and a memory copy behavior corresponding to each memory segment in the memory intermediate form is optimized by a first preset method; Loop processing step: obtaining a loop intermediate form based on the obtained linear operation intermediate form, using a second preset method, selecting a corresponding strategy to optimize the loop in the loop intermediate form according to the hardware attribute intermediate form, and converting the optimized loop into a vector intermediate form, and in addition, obtaining a heterogeneous device data operation rule intermediate form based on a preset calculation model; Hardware code generation step: based on the hardware attribute intermediate form obtained in the logic generation step, the memory intermediate form obtained in the memorization step, and the loop intermediate form obtained in the loop processing step, the executable code that can be inferred on the target device is obtained, wherein the hardware information represented by the hardware attribute intermediate form determines the hardware device adapted by the executable code.

2. The method according to claim 1, characterized in that In the logic generation step, the preset hardware model is a three-level memory structure model, and different target devices convert hardware information into an intermediate form of hardware attributes based on the hardware model according to the following steps: Simplify the target device into a hardware model that includes a three-level memory structure of memory, cache, and registers; Based on the hardware model, the attributes that affect the computational efficiency of the deep learning model are obtained, wherein the attributes at least include hardware architecture, number of CPU cores, GPU / TPU tensor cores, cache size, hardware computing power, and bandwidth size, and the hardware architecture is CPU architecture, GPU architecture, or TPU architecture; Convert all attributes obtained from the hardware model into an intermediate form of hardware attributes.

3. The method according to claim 1, characterized in that: In the memorization step, the first preset method is a heterogeneous memory transmission folding method, and the method is used to optimize the memory copy behavior according to the following steps, wherein each memory segment corresponds to multiple memory transmission behaviors, and each memory transmission behavior corresponds to one memory segment: Traverse all memory copy behaviors, match multiple memory copy behaviors with the same corresponding memory segments into groups, so as to obtain memory copy behavior groups corresponding to multiple different memory segments; The redundant memory copy behaviors in each memory copy behavior group and the memory management operations of the corresponding memory segments of the group are optimized by using a delete / replace method.

4. The method according to claim 1, characterized in that: In the loop processing step, the second preset method is: The redundant loops in the loop intermediate form are processed by fusion to obtain the fused loop intermediate form; According to the hardware attribute intermediate form, different stacking strategies are adopted to adjust the structures of all loops in the fused loop intermediate form to obtain the stacked loop intermediate form; A vectorization length coefficient is set according to the hardware attribute intermediate form, and the loop in the stacked loop intermediate form is converted into a vector intermediate form according to the vectorization length coefficient.

5. The method according to claim 4, characterized in that The method of processing redundant loops in the loop intermediate form in a fusion manner to obtain a fused loop intermediate form includes: Traverse all loops in the loop intermediate form and merge multiple loops that meet the fusion conditions into one loop. The fusion conditions include: Condition 1: Multiple loops operate on the same memory segment and the memory is not affected between the multiple loops; Condition 2: Multiple loops operate on different memories and the number of iterations of the multiple loops is the same; Among them, if multiple loops meet any one of conditions 1 and condition 2, they are considered to meet the loop fusion condition, otherwise they are considered to not meet the fusion condition; Generates a fused loop intermediate form based on the fused loop and the unfused loop.

6. The method according to claim 4, characterized in that: The intermediate form according to the hardware attribute adopts different stacking strategies to adjust the structure of all loops in the intermediate form of the fused loop to obtain the intermediate form of the stacked loop, including: The hardware architecture information of the target device is obtained based on the intermediate form in the hardware attribute, and then a corresponding stacking strategy is adopted according to the hardware architecture information to adjust the structure of the loop in the intermediate form of the fusion loop, wherein the stacking strategy includes: If the target device is a homogeneous device, that is, the hardware architecture of the target device is a CPU architecture, the stacking strategy adopted is: Stack all single-layer loop structures into two-layer loop structures, and then stack 2 to 4 layers of loops according to the number of CPU cores of the target device; When the target device is a heterogeneous device, that is, the hardware architecture of the target device is a GPU architecture or a TPU architecture, the stacking strategy adopted is: The structures of all loops in the fused loop intermediate form are stacked into a multi-layer loop structure of at least three layers including at least one outer loop, one inner loop and one innermost loop from the outside to the inside, wherein the outer loop is a grid responsible for managing multiple concurrently executed thread blocks, the inner loop is a thread block responsible for managing the execution of a group of threads, the innermost loop is a thread, and the inner loop of the thread is a single-layer loop or a multi-layer nested loop; Adjust the number of loops of the inner loops in all multi-layer loops to a preset number, and transfer the remaining number of loops of the inner loops to the outer loops, wherein the preset number corresponding to the GPU architecture is an arbitrary integer less than or equal to 1024, and the preset number corresponding to the TPU architecture is an arbitrary integer less than or equal to 512; Generates a stacked loop intermediate form based on the stacked loop.

7. The method according to claim 4, characterized in that: The step of setting the vectorization length coefficient according to the intermediate form of the hardware attribute comprises: For a target device with a CPU architecture, the target device instruction set architecture is obtained based on the intermediate form of hardware attributes, the length coefficient is set according to the obtained instruction set architecture information, and each loop is vectorized according to the same length coefficient; For GPU and TPU target devices, the length factor for each loop is set as follows: Get the Tensor Core operation scale of the target device based on the intermediate form of hardware attributes, where Tensor Core is the tensor core of the GPU or TPU; According to the number of innermost loop layers of multi-layer loops, the corresponding strategy is adopted to set the length coefficient. If the innermost loop is a multi-layer nested loop, the length coefficient is set to the Tensor Core operation scale in the hardware properties of the target device. Conversely, if the innermost loop is a single-layer loop, the length coefficient is set to 128 bytes.

8. The method according to claim 1, characterized in that In the loop processing step, the computing model is used to generate heterogeneous device data operation rules, wherein the heterogeneous device is a target device whose hardware architecture is a GPU architecture or a TPU architecture, and the computing model generates the heterogeneous device data operation rules in the following manner: Standardize each data operation in the deep learning model so that it has the same operation steps and each data operation is performed according to the operation steps; Based on all data operations after the standardized operation steps, the same steps in the operation steps are saved in the same queue to obtain multiple queues, wherein the same steps in different data operations are saved in each queue; Add data dependencies between event processing queues to form a multi-stream parallel data operation operation mode, and then generate the intermediate form of data operation rules based on this operation mode.

9. The method according to claim 1, characterized in that: In the hardware code generation step, the obtaining of executable code that can be inferred on the target device includes the following steps: Obtain the hardware architecture of the target device based on the intermediate form of hardware attributes, so as to determine the type of instruction set supported by the target device; Executable codes are generated based on the obtained loop intermediate form, vector intermediate form, and memory intermediate form, wherein during the generation process, all loops, vectors, and memory operations in the deep learning model are converted into computer instructions that call the target device supported.

10. A deep learning model compiler supporting multiple hardware based on the method of any one of claims 1 to 9, used to compile a deep learning model into executable code supporting a target device through target device attributes, characterized in that: The system includes: a logic generation module, a memory module, a loop processing module, and a hardware code generation module, wherein: The logic generation module is used to obtain a linear operation intermediate form, a hardware attribute intermediate form, and a tensor shape intermediate form based on the acquired deep learning model and target device information, and generate a logic model of the deep learning model to be compiled, wherein the logic model includes a linear operation intermediate form, a tensor, a tensor shape intermediate form, and a hardware attribute intermediate form; The memorization module is used to obtain a memory intermediate form based on tensors and tensor intermediate forms in the logical model, and use a memory folding method to optimize the memory copy behavior to generate a memory model of the deep learning model to be compiled, wherein the memory model includes a linear operation intermediate form, a memory intermediate form, a tensor shape intermediate form, and a hardware attribute intermediate form; The loop processing module is used to obtain a loop intermediate form based on a linear operation intermediate form in a memory model, and select a corresponding optimization strategy to adjust the structure of the loop according to the hardware attribute intermediate form, and convert the optimized loop into a vector intermediate form. In addition, based on a preset multi-stream parallel computing model, the target device data operation rule intermediate form is obtained, and the loop model of the deep learning model to be compiled is generated through the above operations, wherein the loop model includes a loop intermediate form, a memory intermediate form, a tensor shape intermediate form, a hardware attribute intermediate form, and a data operation rule intermediate form; The hardware code generation module is used to obtain executable code that can perform reasoning tasks on a target device based on a loop processing module, wherein the loops, vectors, and memory operations of the deep learning model are converted into computer instructions that call the target device supported according to the intermediate description of the hardware attributes to generate an inference model of the deep learning model that needs to be compiled.

11. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and the computer program can be executed by a processor to implement the steps of any one of the methods of claims 1 to 9.

12. An electronic device, characterized in that: include: one or more processors; as well as A memory, wherein the memory is used to store executable instructions; The one or more processors are configured to implement the steps of the method of any one of claims 1 to 9 by executing the executable instructions.

Citation Information

Patent Citations

  • Automatic vector optimization method for width inconsistency of deep learning framework compiler

    CN112527262A

  • Compilation optimization method and system for deep learning operator

    CN113821208A

  • Neural network model compiling method and system, equipment and storage medium

    CN113918163A

  • Data processing method, device and equipment and computer storage medium

    CN114385180A

  • Back-end extensible framework and method for deep learning model parsing, optimization and deployment

    CN116306812A

Cited By

  • Compiling method and device and electronic equipment

    CN119621073A

  • Heterogeneous computing power adaptive compiling method and system for large model

    CN121683959A

  • A heterogeneous computing power adaptive compilation method and system for large models

    CN121683959B