Code generation method and device, electronic equipment and storage medium

By optimizing the hardware device architecture parameters of the deep learning model's computing structure and generating high-performance code, the problem of low computing efficiency and memory access efficiency in the existing technology is solved, and the speed of model training and inference is improved and development is simplified. .

CN118642693APending Publication Date: 2024-09-13BEIJING BAIDU NETCOM SCI & TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

Existing deep learning compilers are difficult to effectively optimize computing efficiency and memory access when generating high-performance code, especially in parallel computing and dynamic shape scenarios, resulting in limited model deployment and inference speed.

Method used

By determining the computing structure of the deep learning model, tuning based on the architecture parameters of the hardware device, generating code suitable for the hardware device, improving parallelism and memory merging characteristics, and reducing memory access time.

Benefits of technology

It significantly improves the training and inference speed of deep learning models on hardware devices, improves the utilization of computing resources and code execution efficiency, and simplifies the development process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118642693A
    Figure CN118642693A
Patent Text Reader

Abstract

The invention provides a code generation method and device, electronic equipment and a storage medium, and relates to the technical field of artificial intelligence, in particular to the field of deep learning and computer compiling. According to the specific implementation scheme, a first calculation structure of a deep learning model is determined; wherein the first calculation structure comprises multi-layer calculation, and the multi-layer calculation comprises loop calculation and / or protocol calculation; based on system structure parameters of hardware equipment, the first calculation structure is adjusted and optimized, and a second calculation structure is obtained; wherein the system structure parameters comprise quantity information related to threads and / or blocks; and based on the second computing structure, generating a code for deploying the deep learning model on the hardware equipment. According to the method, the parallelism degree of model calculation can be improved, the memory access time can be shortened, the utilization rate of calculation resources and the code execution efficiency are improved, and the training and reasoning processes of the model on hardware equipment can be accelerated.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of artificial intelligence technology, and in particular to the field of deep learning and computer compilation. Background Art

[0002] With the development of deep learning technology, the complexity and scale of deep neural networks (DNN) continue to increase, and traditional programming models and hardware architectures are difficult to meet their computing needs. In order to give full play to the performance of hardware and improve the training and reasoning speed of deep learning models, deep learning compilers have emerged. Deep learning compilers optimize and compile deep learning models, converting high-level model representations into efficient underlying code, thereby significantly improving computing performance.

[0003] High-performance code generation plays a core role in deep learning compilers. By converting high-level model representations into efficient underlying code, it can significantly speed up the training and reasoning of deep learning models, simplify the development process, and reduce the burden on developers. Developers can focus on model design and algorithm innovation without having to worry about the specific implementation details of the underlying hardware. High-performance code generation can promote the coordinated optimization of deep learning compilers and hardware platforms, realize the combination of software and hardware, and thus promote the improvement of overall system performance. How to generate a high-performance code is a highly concerned issue in the field of deep learning. Summary of the invention

[0004] The present disclosure provides a method, device, electronic device and storage medium for processing model information.

[0005] According to one aspect of the present disclosure, there is provided a code generation method, comprising:

[0006] Determine a first computing structure of the deep learning model; wherein the first computing structure includes multiple layers of computing, and the multiple layers of computing include loop computing and / or reduction computing;

[0007] Based on the architecture parameters of the hardware device, the first computing structure is tuned to obtain the second computing structure; wherein the architecture parameters include quantity information related to threads and / or blocks;

[0008] Based on the second computing structure, code is generated for deploying the deep learning model on a hardware device.

[0009] According to another aspect of the present disclosure, there is provided a code generation device, comprising:

[0010] A structure determination module, used to determine a first computing structure of the deep learning model; wherein the first computing structure includes multi-layer computing, and the multi-layer computing includes loop computing and / or reduction computing;

[0011] A tuning module, configured to tune the first computing structure based on architecture parameters of the hardware device to obtain a second computing structure; wherein the architecture parameters include quantity information related to threads and / or blocks;

[0012] A code generation module is used to generate code for deploying the deep learning model on a hardware device based on the second computing structure.

[0013] According to another aspect of the present disclosure, there is provided an electronic device, comprising:

[0014] at least one processor; and

[0015] a memory communicatively connected to the at least one processor; wherein,

[0016] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute any method in the embodiments of the present disclosure.

[0017] According to another aspect of the present disclosure, a non-transitory computer-readable storage medium storing computer instructions is provided, wherein the computer instructions are used to enable the computer to execute any method according to the embodiments of the present disclosure.

[0018] According to another aspect of the present disclosure, a computer program product is provided, including a computer program, which implements any method according to the embodiments of the present disclosure when executed by a processor.

[0019] In the disclosed embodiment, the computing structure of the deep learning model is tuned based on the architecture parameters of the hardware device, and then the code is generated based on the tuned computing structure. The computing structure is tuned according to the architecture of the hardware device, and the generated code has high performance. Specifically, the parallelism of the calculation can be improved, and the memory merging characteristics of the hardware device can be used to reduce the memory access time, thereby improving the utilization of computing resources and the efficiency of code execution, which is conducive to accelerating the training and reasoning process of the model on the hardware device.

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

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

[0022] Figure 1 is a schematic diagram of a code generation method provided by an embodiment of the present disclosure;

[0023] Figure 2 is a schematic diagram of an example of layered application in an embodiment of the present disclosure;

[0024] Figure 3 is a schematic diagram of an application example of the code generation method according to an embodiment of the present disclosure;

[0025] Figure 4 is a schematic diagram of an application example of a mode specification in an embodiment of the present disclosure;

[0026] Figure 5 is a schematic diagram of an application example of performance tuning in an embodiment of the present disclosure;

[0027] Figure 6 is a schematic diagram of another application example of performance tuning in an embodiment of the present disclosure;

[0028] Figure 7 is a schematic block diagram of a code generation device provided by an embodiment of the present disclosure;

[0029] Figure 8 is a schematic block diagram of a code generating device provided by another embodiment of the present disclosure;

[0030] Fig. 9 It is a block diagram of an electronic device used to implement the code generation method according to an embodiment of the present disclosure. DETAILED DESCRIPTION

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

[0032] In order to facilitate understanding of the processing method of model information provided by the embodiment of the present disclosure, the relevant technologies of the embodiment of the present disclosure are explained below. The following related technologies can be arbitrarily combined with the technical solutions of the embodiment of the present disclosure as optional solutions, and they all belong to the protection scope of the embodiment of the present disclosure.

[0033] Deep learning compilers use a series of optimization techniques to maximize the performance of deep learning models from multiple aspects such as computing efficiency, memory management, platform adaptation and automatic optimization. In order to achieve the above goals, deep learning compilers usually use high-level code generation, static analysis and optimization to optimize performance. The intermediate representation (IR) is generated from the model description of the deep learning framework, and then the underlying code is generated according to the target hardware platform.

[0034] In the process of performance tuning, there are two main optimization directions: optimizing computing efficiency and video memory access. In fact, for memory-intensive operations, you do not need to pay too much attention to computing logic, but should focus on computing efficiency, especially parallel computing efficiency. Logically, hardware devices such as CUDA manage and execute parallel computing through three levels of abstraction:

[0035] Grid: Grid is the largest unit and usually contains all threads in a CUDA kernel function;

[0036] Block: A block is a collection of threads, usually placed on an SM (Streaming Multiprocessor) for execution. Blocks are a concept introduced to better manage threads. Threads in a block can communicate and collaborate through shared memory, and threads within a block can execute synchronously.

[0037] Thread: Each thread represents the smallest execution unit in CUDA programming. Each thread executes a CUDA kernel function once, and threads are equal without priority distinction. Each thread has a unique identifier that can be used to specify different operations in the code.

[0038] In the existing compiler technology solutions, most compilers can only handle code generation and optimization under static shapes. For scenarios with unknown shapes, the difficulty of generating efficient code increases sharply. The compiler support is not good enough, and it is necessary to call the code generation module multiple times or pin hopes on automatic tuning. For upper-level users, the user experience in terms of deployment ease of use, performance stability, etc. is very limited.

[0039] Figure 1 The code generation method provided by an embodiment of the present disclosure is shown. The method can be applied to a code generation device, such as a deep learning compiler, and the device can be deployed in an electronic device. The electronic device is, for example, a single or multi-machine terminal, server or other processing device. Among them, the terminal can be a mobile device, PDA (Personal Digital Assistant), handheld device, computing device, vehicle-mounted device, wearable device and other UE (User Equipment). In some possible implementations, the method can also be implemented by a processor calling computer-readable instructions stored in a memory. For example Figure 1 As shown, the method may include:

[0040] S110, determining a first computing structure of the deep learning model; wherein the first computing structure includes multi-layer computing, and the multi-layer computing includes loop computing and / or reduction computing;

[0041] S120, based on the architecture parameters of the hardware device, tuning the first computing structure to obtain a second computing structure; wherein the architecture parameters include quantity information related to threads and / or blocks;

[0042] S130. Based on the second computing structure, generate code for deploying the deep learning model on a hardware device.

[0043] In the embodiments of the present disclosure, the deep learning model may include, but is not limited to, an image processing model, a speech processing model, a natural language processing model (such as a large language model LLM or a conversational generation model), etc. The computational structure in the deep learning model may include processing of tensors, where tensors are, for example, images, speech sequences, text sequences, etc.

[0044] Exemplarily, the first computing structure of the deep learning model may include the computing structure of any operation in the deep learning model, that is, the structure of the computing process required to complete the operation in the deep learning model, including information such as how many layers of calculation the operation contains, the calculation type of each layer, and the order of calculations between each layer.

[0045] For example, a deep learning model may include multiple neural network layers, and each neural network layer performs specific operations on one or more feature maps, such as convolution, concatenation, flattening, etc. These specific operations often require traversing each element in data of different dimensions or sizes for calculation. For example, convolution processing requires traversing each element in the feature map and the convolution kernel for calculation, and the operation between each element in the feature map and the convolution kernel involves traversing each element in the convolution kernel and the element in the feature map for calculation, thus forming a multi-level computing structure. The first computing structure of the disclosed embodiment may include a multi-level computing structure corresponding to any operation in the model.

[0046] In the disclosed embodiments, loop calculation and reduction calculation are two different calculations completed by iteration, that is, both loop calculation and reduction calculation need to be cyclically and repeatedly calculated, and the difference is that each round of iteration in loop calculation does not need to rely on the result of the previous iterative calculation, while each round of iteration in reduction calculation needs to rely on the result of the previous iterative calculation. In other words, there is no parallel dependency between the axis data of loop calculation, and there is a parallel dependency between the axis data of reduction calculation.

[0047] For example, the first calculation structure may be Example 1 below.

[0048] Example 1:

[0049] for i in range(4):

[0050] for j in range(8):

[0051] out[i,j]=a[i,j]+b[i,j]

[0052] The first calculation structure in Example 1 can be understood to include two layers of loop calculations, the first layer is based on index i and is repeated four times, and the second layer is based on index j and is repeated eight times. For ease of understanding, it is formalized as S(4)S(8) in the disclosed embodiment, where S is the abbreviation of spatial (related to space), indicating that it can be parallel in time and space, that is, it represents loop calculation. In practical applications, since there is no parallel dependency between the axis data in the loop calculation, the for loop represented by S can be assigned to multiple threads (thread) and blocks (block) of the hardware device for parallel execution.

[0053] The first calculation structure may also be the following Example 2.

[0054] Example 2:

[0055] for i in range(4):

[0056] for j in range(8):

[0057] out[i]=out[i]+a[i,j]

[0058] The first calculation structure in Example 2 can be understood as including a layer of loop calculation and a layer of reduction calculation. Among them, the loop in the inner layer calculation is reduced, so the inner layer calculation is a reduced calculation. For ease of understanding, the first calculation structure of Example 2 is formalized as S(4)R(8) in the embodiment of the present disclosure, where R is the abbreviation of Reduce, which means reduced calculation. In practical applications, since there are parallel dependencies between the axis data in the reduced calculation, and there is no efficient synchronization mechanism between blocks, the data of the Reduce axis can only be allocated at the thread level, that is, it can only be executed in parallel on multiple threads, but not on multiple blocks.

[0059] In an actual deep learning model, the first computing structure can be any combination of S and R, such as S, SR, RS, SRS, etc., and the shapes of S and R can also be of any type, such as S(4)R(8), S(64)R(128)S(32), etc. When the parameters of S and R are specific values, they represent a static shape; when the parameters of S and R are unknown numbers, for example, when the parameters are represented by strings or symbols, such as S(a0)R(a1), where a0 and a1 represent unknown numbers, they represent a dynamic shape. In a dynamic shape scenario, the number of repeated executions of a loop cannot be predicted during code generation, so it is necessary to represent the number of repeated executions based on symbols / strings. Based on this, after the computing structure is transformed, complex symbols and string operation expressions are needed to represent the number of repeated executions of each layer of calculation. For the sake of simplicity, in the following description of this specification, when the number of repeated executions of a layer of calculation cannot be represented by a specific numerical value and needs to be represented by an expression, the parameter is represented by -1.

[0060] In an embodiment of the present disclosure, a first computing structure is tuned according to the architecture parameters of a hardware device to obtain a second computing structure, and the second computing structure is applied to code generation for model deployment. The architecture parameters include quantity information related to blocks and / or threads. Exemplarily, the architecture parameters may include the number of blocks used to execute loop calculations in parallel, the number of threads used to execute loop calculations in parallel, the number of blocks used to execute reduction calculations in parallel, the number of threads included in a single thread bundle, etc. These architecture parameters may reflect the parallelism of computing units in hardware devices, memory merging characteristics, etc.

[0061] Therefore, tuning the first computing structure based on the architecture parameters can be beneficial to utilizing the characteristics of the hardware device and formulating targeted optimization strategies. Specifically, a series of transformations can be performed on the first computing structure based on these architecture parameters. Tuning the computing structure based on the architecture of the hardware device can improve the parallelism of the calculation, and can also use the memory merging characteristics of the hardware device to reduce the memory access time, thereby improving the utilization of computing resources and code execution efficiency, which is beneficial to accelerate the training and reasoning process of the model on the hardware device.

[0062] In some embodiments, determining a first computing structure of a deep learning model includes:

[0063] Determine a third computing structure in the deep learning model based on the description information of the deep learning model;

[0064] The calculation mode in the third calculation structure is simplified to obtain the first calculation structure.

[0065] In the above embodiment, the description information of the deep learning model may include the description information of the deep learning framework, specifically, it may include the description of each neural network layer of the deep learning model and the connection relationship between each neural network layer and other information.

[0066] The third computing structure may be an original computing structure or an initialized computing structure in the deep learning model, that is, a computing structure that can be directly mapped according to the description information of the deep learning model. The third computing structure may include any computing mode, such as any combination of cyclic computing (S) and reduced computing (R), such as S, SR, RS, SRS, SRSR, SSR, etc., and the shapes of S and R may also be of any type.

[0067] In the embodiment of the present disclosure, the calculation mode in the third calculation structure is simplified. Specifically, the calculation mode in the third calculation structure can be transformed into a limited number of several calculation modes to reduce the complexity of the tuning steps.

[0068] Exemplarily, the first computing structure obtained after simplification is a computing structure that does not include a reduction calculation or includes a layer of reduction calculation, that is, the first computing structure includes at most one layer of reduction calculation. It can be understood that since the third computing structure can be any computing structure, the third computing structure may include multiple layers of reduction calculation. If it is simplified to a computing structure including at most one layer of reduction calculation, it may be necessary to convert the third computing structure into multiple first computing structures.

[0069] On hardware devices such as high-performance computing cards, the nature of reduced calculations is poor. This type of calculation cannot be allocated to any block and thread like ordinary loop calculations, but can only be allocated to inner-layer calculations or to threads to be parallelized. When the loop level of the reduced calculation is discontinuous, the corresponding memory access efficiency becomes extremely low. The above embodiment simplifies the original calculation structure to obtain a calculation structure for performance tuning, which is conducive to reducing the number of layers of reduced calculations in a single calculation structure and avoiding discontinuous reduced calculation levels, thereby improving memory access efficiency, correspondingly improving code execution efficiency, and accelerating the training and reasoning process of the model on the hardware device.

[0070] In some embodiments, the calculation mode in the third calculation structure is simplified to obtain the first calculation structure, including:

[0071] Merging adjacent loop calculations and adjacent reduction calculations in the third calculation structure to obtain a fourth calculation structure;

[0072] When the fourth calculation structure does not include the reduced calculation or includes one layer of reduced calculation, the fourth calculation structure is used as the first calculation structure.

[0073] In the embodiment of the present disclosure, fusion can be the merging of structures of adjacent levels of the same nature. Therefore, the above-mentioned fusion of adjacent loop calculations and adjacent reduction calculations in the third calculation structure specifically includes the fusion of adjacent loop calculations in the third calculation structure and the merging of adjacent reduction calculations in the third calculation structure.

[0074] The above fusion can be called loop fusion (LoopFuse). For example, LoopFuse(S(4), S(8) means that S(4) and S(8) are fused, and the fused parameters are multiplied, that is, LoopFuse(S(4), S(8) = S(32). It should be noted that the fusion process can only be performed between S and S, and between R and R. S and R will not be fused.

[0075] According to the above embodiment, for the calculation levels of the same nature, they are fused through the LoopFuse primitive. For example, S1 S2 R3 S4 R5 can be S12 R3 S4 R5 after loop fusion. It should be noted that S1, S2, etc. represent different loop calculations, 1 and 2 are used to distinguish, and do not represent parameter values. Similarly, S12 represents the result of the fusion of S1 and S2, and does not mean that 12 is the fused parameter). After the fusion step, there will be no adjacent calculations of the same nature, and the calculation mode of the fourth calculation structure only includes SR iterations (such as SR, SRSR, SRSRSR, etc.) or RS iterations (such as RS, RSRS, RSRSRS, etc.). If the fused fourth calculation structure does not include reduction calculations or only includes one layer of reduction calculations, it can be directly used as the first calculation structure for subsequent performance tuning.

[0076] It can be seen that according to the above embodiments, the computing mode of the computing structure of the deep learning model can be significantly simplified, and the possible computing modes can be limited to two computing modes, which can effectively reduce the complexity of performance tuning.

[0077] In some embodiments, simplifying the calculation mode in the third calculation structure to obtain the first calculation structure further includes:

[0078] In the case where the fourth computing structure includes multiple layers of reduced computing, the fourth computing structure is layered to obtain multiple first computing structures; wherein each of the multiple first computing structures includes a layer of reduced computing.

[0079] In the embodiment of the present disclosure, if the fourth computing structure includes multiple layers of reduced computing, the first computing structure including at most one layer of reduced computing is obtained through layering.

[0080] Figure 2FIG. 1 is a schematic diagram showing an example of layered application in an embodiment of the present disclosure. Figure 2 As shown, taking the calculation structure S1 S2 R3 S4 R5 as an example, after loop fusion (LoopFuse), it can be transformed into S12 R3 S4 R5. Among them, the two reduction calculations of R3 and R5 are not adjacent, so reasonable layering is required. Specifically, in the first layer of calculation (the first first calculation structure), only the R3 loop is calculated, and R5 is converted to the loop calculation S5; in the second layer of calculation (the second first calculation structure), the loop corresponding to R3 has been calculated, and the corresponding output tensor no longer requires the R3 loop. Therefore, the second layer of calculation is S12 S4 R5, and the reduce calculation is performed on R5.

[0081] To facilitate understanding, a specific code example is given below.

[0082] The fourth calculation structure before calculation layering is:

[0083]

[0084] After the calculation is layered, the two layers of calculation actually correspond to two kernel functions in the hardware device. The first one is:

[0085]

[0086] The second one is:

[0087]

[0088] It can be understood that if the fourth loop structure includes N reduction calculations, then after layering, it will be divided into N first calculation structures, each of which is independent of each other, and each first calculation structure only includes one layer of reduction calculation. Therefore, the calculation mode of each calculation structure can only be one of S, SR, RS, and SRS.

[0089] It can be seen that according to the above embodiment, the number of reduction calculations in a computing structure can be reduced, so that each computing structure only includes one layer of reduction calculations, which simplifies the complexity of performance tuning. At the same time, since the resource allocation restrictions of the reduction calculations are relatively high (can only be allocated to threads in parallel), the above embodiment can improve the flexibility of resource allocation, help improve the utilization of computing resources, and thus improve the overall computing efficiency.

[0090] Figure 3 is a schematic diagram of an application example of the code generation method of an embodiment of the present disclosure. The code generation method includes the following steps:

[0091] S31, determining the model calculation logic;

[0092] S32, loop abstraction; specifically, abstracting the computational logic of the deep learning model into a specific computational structure for subsequent module optimization;

[0093] S33, mode specification; specifically, transforming the computing mode of the computing structure and constraining it to several specific computing modes, such as S, SR, RS, SRS;

[0094] S34, performance tuning; specifically, for the constrained computing modes S, SR, RS, and SRS, performance tuning is performed;

[0095] S35. Efficient code generation.

[0096] In this application example, the calculation mode is simplified and changed according to the above embodiment, so that the tuning step S34 only needs to be performed for a few fixed modes.

[0097] Specifically, Figure 4 Schematic diagram of an application example of the mode specification in the embodiment of the present disclosure. The mode specification process includes:

[0098] S41, input calculation structure;

[0099] S42, adjacent calculation fusion of the same nature;

[0100] S43, determining whether there is only one layer of reduction calculation in the calculation structure, if yes, executing S46; if no, executing S44;

[0101] S44, layering the multi-layered reduction calculation to obtain a plurality of layered calculation structures;

[0102] S45, traverse each layered computing structure; specifically, enter S42 for each computing structure to merge newly emerged adjacent computing of the same nature, and output after judging whether there is only one layer of reduced computing.

[0103] S46. Output the reduced calculation structure.

[0104] An exemplary implementation of performance tuning is provided below.

[0105] In some embodiments, the architecture parameter includes the number of threads in a single warp. The number is, for example, 16, 32, 64, etc. Accordingly, based on the architecture parameter of the hardware device, the first computing structure is tuned, including:

[0106] The innermost computation in the first computation structure is divided based on the number of threads in a single warp.

[0107] Among them, splitting (LoopSplit) refers to splitting a layer of calculation into multiple layers of calculation (different from the stratification in the aforementioned embodiment, the stratification in the aforementioned embodiment refers to dividing a calculation structure into multiple independent calculation structures). For example, LoopSplit(S(32), factor = [2,4,4]) means splitting S(32) according to the parameter conversion method of 32 = 2*4*4, and the result obtained is S(2)S(4)S(4), that is:

[0108] LoopSplit(S(32), factor=[2,4,4])=S(2)S(4)S(4).

[0109] In order to facilitate the application in dynamic shape scenarios, the above split parameter factor can have -1 (as described above, it indicates an uncertain value, such as an expression based on symbols / strings). If the split is done on a static dimension, the value corresponding to -1 can be directly calculated; if the split is done on a dynamic dimension, -1 indicates uncertainty.

[0110] Optionally, a parameter (number of repeated executions) of the innermost calculation in the first calculation structure after segmentation is equal to the number of threads in a single thread warp.

[0111] For example, for the first computing structure S(a)R(b)S(c), the innermost loop S(c) can be split into S(-1)S(THREAD_PER_WARP), that is, the processed first computing structure is S(a)R(b)S(-1)S(THREAD_PER_WARP), where THREAD_PER_WARP is the number of threads in a single thread bundle.

[0112] In actual applications, memory coalescing operations will exist in the programming of specific hardware devices. For example, in cuda programming, for global memory, continuous threads accessing continuous memory addresses will trigger memory coalescing. This means that when each thread in a warp reads a continuous memory address, these read operations can be merged into one or several memory transfer operations, thereby significantly reducing the time for memory access. The disclosed embodiment utilizes this feature and divides the innermost calculation in the first computing structure based on the number of threads in a single warp, so that the generated code can maximize the number of occurrences of memory coalescing, reduce memory access time, and thus improve the overall computing efficiency of the model.

[0113] In some embodiments, the architecture parameters include the number of threads used to perform the reduction calculation in parallel. Accordingly, based on the architecture parameters of the hardware device, the first computing structure is tuned, including:

[0114] The reduction calculation in the first calculation structure is divided based on the number of threads used to execute the reduction calculation in parallel.

[0115] Optionally, the number of repeated executions of at least one reduced calculation in the segmented first calculation structure is equal to the number of threads used to execute the reduced calculation in parallel.

[0116] For example, for the calculation structure S(-1)R(b)S(THREAD_PER_WARP), R(b) can be split into R(-1)R(REDUCE_THREAD), that is, the calculation structure S(-1)R(b)S(THREAD_PER_WARP) is transformed into S(-1)R(-1)R(REDUCE_THREAD)S(THREAD_PER_WARP). REDUCE_THREAD is the number of threads used to execute the reduced calculation in parallel.

[0117] The above embodiment divides the reduction calculation in the first computing structure based on the number of threads that execute the reduction calculation in parallel, so that the generated code can maximize the parallelism of the reduction calculation, that is, distribute the reduction calculation to as many threads as possible for parallel processing, thereby improving resource utilization through parallel computing, and then improving the overall computing efficiency of the model.

[0118] Optionally, the number of threads used for parallel execution of the reduced calculations may be determined based on manual experience, such as manually selecting a suitable number of threads based on characteristics of a hardware device.

[0119] In some embodiments, the architecture parameter includes the number of blocks used to perform cyclic calculations in parallel. Accordingly, based on the architecture parameter of the hardware device, the first computing structure is tuned, including:

[0120] The outermost loop calculation in the first calculation structure is divided based on the number of blocks for executing the loop calculation in parallel.

[0121] Optionally, the number of repetitions of the outermost loop calculation in the divided first calculation structure is equal to the number of blocks used to execute the loop calculation in parallel.

[0122] For example, for the calculation structure S(-1)R(-1)R(REDUCE_THREAD)S(THREAD_PER_WARP), the outermost layer S(-1) can be divided into S(SPATIAL_BLOCK)S(-1), that is, the calculation structure S(-1)R(-1)R(REDUCE_THREAD)S(THREAD_PER_WARP) is transformed into S(SPATIAL_BLOCK)S(-1)R(-1)R(REDUCE_THREAD)S(THREAD_PER_WARP). Among them, SPATIAL_BLOCK is the number of blocks that execute the loop calculation in parallel.

[0123] The above embodiment divides the loop calculation in the first computing structure based on the number of blocks that execute the loop calculation in parallel, so that the generated code can maximize the parallelism of the loop calculation, that is, distribute the loop calculation to as many blocks as possible for parallel processing, thereby improving resource utilization through parallel computing, and further improving the overall computing efficiency of the model.

[0124] Optionally, the number of blocks used to execute the cyclic calculation in parallel may be determined based on manual experience, for example, manually selecting a suitable number of blocks based on the characteristics of the hardware device.

[0125] In some embodiments, before dividing the reduced calculation in the first computing structure based on the number of threads for executing the reduced calculation in parallel, and / or dividing the outermost loop calculation in the first computing structure based on the number of blocks for executing the loop calculation in parallel, tuning the first computing structure based on the architecture parameters of the hardware device further includes:

[0126] If the first calculation structure contains multiple layers of calculations of the same calculation type in other layers except the innermost layer, the multiple layers of calculations are merged.

[0127] That is to say, after the innermost calculation is split, the innermost loop in the first calculation structure can be retained, and for other levels, multiple layers of calculations of the same calculation type or the same calculation properties can be merged to facilitate the subsequent process to split the specification calculation and loop calculation.

[0128] For example, for the computing structure S(a)R(b)S(-1)S(THREAD_PER_WARP), keep the innermost layer S(THREAD_PER_WARP) unchanged, merge S(a) and S(-1) with the same properties in other layers, then S(a)R(b)S(-1)S(THREAD_PER_WARP) can be transformed into S(-1)R(b)S(THREAD_PER_WARP).

[0129] According to the above embodiment, it is convenient to divide the loop calculation or reduction calculation in the first calculation structure based on the parameters of parallel execution, so as to maximize the parallelism, improve the utilization of resources, and further improve the overall calculation efficiency of the model.

[0130] In some embodiments, based on the second computing structure, generating code for deploying the deep learning model on a hardware device includes:

[0131] According to the calculation type and the number of repeated executions of each calculation level in the second calculation structure, each calculation level is bound to a block and a thread of a hardware device to generate code.

[0132] It can be understood that by optimizing the performance of the computing structure, a computing structure that maximizes parallelism and memory merging can be obtained. Based on such a computing structure, the computing type and the number of repeated executions are bundled to the blocks and threads of the hardware device, and the resulting code can have high computing efficiency when running on the hardware device. In this way, high-performance code generation can be achieved, solving the key problem of compilers in related technologies.

[0133] To facilitate understanding of the above performance tuning process, Figure 5 FIG. 1 is a schematic diagram showing an application example of performance tuning in an embodiment of the present disclosure. Figure 5 As shown, the performance tuning process includes:

[0134] S51, input calculation structure;

[0135] S52, split the innermost loop according to the number of threads THREAD_PER_WARP in a single thread warp;

[0136] S53, merge the same type of loop levels;

[0137] S54, split-reduce calculation (reduce type loop hierarchy);

[0138] S55, segmentation loop calculation (spatial type loop hierarchy);

[0139] S56. Allocate blocks and threads to the loop hierarchy.

[0140] Figure 6 FIG. 2 is a schematic diagram showing another application example of performance tuning in an embodiment of the present disclosure. Figure 6 As shown, taking the computing structure S1 R2 S3 as an example, the performance tuning process includes:

[0141] S61, split the innermost loop to obtain the calculation structure S1 R2 S3'S4; wherein the number of repeated executions of S4 is equal to the parameter THREAD_PER_WARP;

[0142] S62, merge the same type of loop levels to obtain the calculation structure S13'R2 S4;

[0143] S63, split and reduce calculation (reduce type loop level), and obtain the calculation structure S13'R2_1R2_2S4;

[0144] S64, split loop calculation (spatial type loop hierarchy); obtain calculation structure S13'_1S13'_2R2_1R2_2S4;

[0145] S65. Allocate blocks and threads to the loop hierarchy.

[0146] It can be seen that the technical solution of the embodiment of the present disclosure proposes a deep learning compiler performance tuning technology based on code templates. Different from the existing solutions, the embodiment of the present disclosure abstracts expert experience into code templates, reduces the hierarchical structure of loops to several unique modes, and completes performance tuning in the computing mode. The embodiment of the present disclosure has the following advantages:

[0147] 1. Fast tuning speed: By reducing the computing structure, the search space for performance tuning is actually constrained within a very small and reasonable range, making the tuning speed of this solution much faster than that of competing products.

[0148] 2. High code performance: A large number of operations in deep learning models are memory-intensive. During the performance tuning process, for hardware devices such as the GPU architecture, memory access continuity is guaranteed at the thread level as much as possible to achieve higher code performance.

[0149] 3. Good adaptability: The technical solution of the embodiment of the present disclosure does not need to deliberately distinguish between scenes with dynamic shapes or static shapes. A unified performance tuning process is adopted, and static shapes can be understood as special cases of dynamic shapes.

[0150] In addition, the embodiments of the present disclosure can be used to accelerate any deep learning model, including but not limited to NLP, CV, scientific computing and other fields, and the technical solutions of the embodiments of the present disclosure can be used to accelerate the reasoning and training processes.

[0151] According to an embodiment of the present disclosure, the present disclosure also provides a code generation device, Figure 7 A schematic block diagram of a code generation device provided by an embodiment of the present disclosure is shown, Figure 7 As shown, the device comprises:

[0152] A structure determination module 710 is used to determine a first computing structure of the deep learning model; wherein the first computing structure includes multi-layer computing, and the multi-layer computing includes loop computing and / or reduction computing;

[0153] A tuning module 720, configured to tune the first computing structure based on the architecture parameters of the hardware device to obtain a second computing structure; wherein the architecture parameters include quantity information related to threads and / or blocks;

[0154] The code generation module 730 is used to generate code for deploying the deep learning model on a hardware device based on the second computing structure.

[0155] In some embodiments, the structure determination module 710 includes:

[0156] A loop abstraction unit 810, configured to determine a third computing structure in the deep learning model based on the description information of the deep learning model;

[0157] The mode reduction unit 820 is used to simplify the calculation mode in the third calculation structure to obtain the first calculation structure.

[0158] In some embodiments, the mode specification unit 820 is used to:

[0159] Merging adjacent loop calculations and adjacent reduction calculations in the third calculation structure to obtain a fourth calculation structure;

[0160] When the fourth calculation structure does not include the reduced calculation or includes one layer of reduced calculation, the fourth calculation structure is used as the first calculation structure.

[0161] In some embodiments, the mode specification unit 820 is used to:

[0162] In the case where the fourth computing structure includes multiple layers of reduced computing, the fourth computing structure is layered to obtain multiple first computing structures; wherein each of the multiple first computing structures includes a layer of reduced computing.

[0163] In some embodiments, the architectural parameters include the number of threads in a single warp;

[0164] The tuning module 720 is used to:

[0165] The innermost computation in the first computation structure is divided based on the number of threads in a single warp.

[0166] In some embodiments, the architectural parameters include the number of threads used to perform the reduction calculations in parallel;

[0167] The tuning module 720 is used to:

[0168] The reduction calculation in the first calculation structure is divided based on the number of threads used to execute the reduction calculation in parallel.

[0169] In some embodiments, the architectural parameters include the number of blocks used to perform loop calculations in parallel;

[0170] The tuning module 720 is used to:

[0171] The outermost loop calculation in the first calculation structure is divided based on the number of blocks for executing the loop calculation in parallel.

[0172] In some embodiments, the tuning module 720 is further configured to:

[0173] If the first calculation structure contains multiple layers of calculations of the same calculation type in other layers except the innermost layer, the multiple layers of calculations are merged.

[0174] In some embodiments, the code generation module 730 is used to:

[0175] According to the calculation type and the number of repeated executions of each calculation level in the second calculation structure, each calculation level is bound to a block and a thread of a hardware device to generate code.

[0176] For the description of specific functions and examples of each module and submodule of the device in the embodiment of the present disclosure, reference can be made to the relevant description of the corresponding steps in the above method embodiment, which will not be repeated here.

[0177] In the technical solution disclosed herein, the acquisition, storage and application of user personal information involved are in compliance with the provisions of relevant laws and regulations and do not violate public order and good morals.

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

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

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

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

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

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

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

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

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

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

[0188] A computer system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The relationship of client and server is generated by computer programs running on respective computers and having a client-server relationship with each other. The server may be a cloud server, a server of a distributed system, or a server combined with a blockchain.

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

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

Claims

1. A code generation method, comprising: Determine a first computing structure of the deep learning model; wherein the first computing structure includes multiple layers of computing, and the multiple layers of computing include loop computing and / or reduction computing; Based on the architecture parameters of the hardware device, the first computing structure is tuned to obtain a second computing structure; wherein the architecture parameters include quantity information related to threads and / or blocks; Based on the second computing structure, generate code for deploying the deep learning model on the hardware device.

2. The method according to claim 1, wherein: The step of determining a first computing structure of a deep learning model includes: Determine a third computing structure in the deep learning model based on the description information of the deep learning model; The calculation mode in the third calculation structure is simplified to obtain the first calculation structure.

3. The method according to claim 2, wherein: The step of simplifying the calculation mode in the third calculation structure to obtain the first calculation structure includes: Merging adjacent loop calculations and adjacent reduction calculations in the third calculation structure to obtain a fourth calculation structure; In the case that the fourth computing structure does not include a reduction computing or includes a layer of reduction computing, the fourth computing structure is used as the first computing structure.

4. The method according to claim 3, wherein: The simplification of the calculation mode in the third calculation structure to obtain the first calculation structure further includes: In the case where the fourth computing structure includes multiple layers of reduced computing, the fourth computing structure is layered to obtain multiple first computing structures; wherein each of the multiple first computing structures includes a layer of reduced computing.

5. The method according to any one of claims 1 to 4, wherein: The architectural parameters include the number of threads in a single warp; The step of optimizing the first computing structure based on the architecture parameters of the hardware device includes: The innermost layer calculation in the first computing structure is divided based on the number of threads in the single thread warp.

6. The method according to any one of claims 1 to 5, wherein: The architecture parameters include the number of threads used to perform the reduction calculations in parallel; The step of optimizing the first computing structure based on the architecture parameters of the hardware device includes: The reduction calculation in the first calculation structure is divided based on the number of threads used to execute the reduction calculation in parallel.

7. The method according to any one of claims 1 to 6, wherein: The architectural parameters include the number of blocks used to perform loop calculations in parallel; The step of optimizing the first computing structure based on the architecture parameters of the hardware device includes: The outermost loop calculation in the first calculation structure is divided based on the number of blocks used to execute the loop calculation in parallel.

8. The method according to claim 6 or 7, wherein: The step of optimizing the first computing structure based on the architecture parameters of the hardware device further includes: If the first computing structure contains multiple layers of computing of the same computing type in other layers except the innermost layer, the multiple layers of computing are merged.

9. The method according to any one of claims 1 to 8, wherein: The generating, based on the second computing structure, a code for deploying the deep learning model on the hardware device comprises: According to the calculation type and the number of repeated executions of each calculation level in the second calculation structure, each calculation level is bound to the block and thread of the hardware device to generate the code.

10. A code generating device, comprising: A structure determination module, used to determine a first computing structure of a deep learning model; wherein the first computing structure includes multi-layer computing, and the multi-layer computing includes loop computing and / or reduction computing; A tuning module, configured to tune the first computing structure based on architecture parameters of a hardware device to obtain a second computing structure; wherein the architecture parameters include quantity information related to threads and / or blocks; A code generation module is used to generate code for deploying the deep learning model on the hardware device based on the second computing structure.

11. The device according to claim 10, wherein: The structure determination module comprises: A loop abstraction unit, configured to determine a third computing structure in the deep learning model based on the description information of the deep learning model; A mode reduction unit is used to simplify the calculation mode in the third calculation structure to obtain the first calculation structure.

12. The device according to claim 11, wherein The mode specification unit is used for: Merging adjacent loop calculations and adjacent reduction calculations in the third calculation structure to obtain a fourth calculation structure; In the case that the fourth computing structure does not include a reduction computing or includes a layer of reduction computing, the fourth computing structure is used as the first computing structure.

13. The device according to claim 12, wherein: The mode specification unit is used for: In the case where the fourth computing structure includes multiple layers of reduced computing, the fourth computing structure is layered to obtain multiple first computing structures; wherein each of the multiple first computing structures includes a layer of reduced computing.

14. The device according to any one of claims 10 to 13, wherein: The architectural parameters include the number of threads in a single warp; The tuning module is used to: The innermost layer calculation in the first computing structure is divided based on the number of threads in the single thread warp.

15. The device according to any one of claims 10 to 14, wherein: The architecture parameters include the number of threads used to perform the reduction calculations in parallel; The tuning module is used to: The reduction calculation in the first calculation structure is divided based on the number of threads used to execute the reduction calculation in parallel.

16. The device according to any one of claims 10 to 15, wherein: The architectural parameters include the number of blocks used to perform loop calculations in parallel; The tuning module is used to: The outermost loop calculation in the first calculation structure is divided based on the number of blocks used to execute the loop calculation in parallel.

17. The device according to claim 15 or 16, wherein: The tuning module is also used for: If the first computing structure contains multiple layers of computing of the same computing type in other layers except the innermost layer, the multiple layers of computing are merged.

18. The device according to any one of claims 10 to 17, wherein: The code generation module is used to: According to the calculation type and the number of repeated executions of each calculation level in the second calculation structure, each calculation level is bound to the block and thread of the hardware device to generate the code.

19. An electronic device comprising: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method according to any one of claims 1 to 9.

20. A non-transitory computer-readable storage medium storing computer instructions, wherein: The computer instructions are used to cause the computer to execute the method according to any one of claims 1-9.

21. A computer program product comprising a computer program, which, when executed by a processor, implements the method according to any one of claims 1 to 9.

Citation Information

Cited By

  • Model training system and method, storage medium and electronic equipment

    CN119917860A