Compilation method and system for automatic mixing precision optimization program
Through automated static error analysis and variable accuracy search optimization technology, the problem of low efficiency and long search time of automatic hybrid precision program optimization in the existing technology is solved, and efficient program optimization and source code file provision are achieved.
Patent Information
- Application Number
- CN202411649366.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-19
- Publication Date
- 2025-03-11
- Estimated Expiration
- 2044-11-19
AI Technical Summary
The existing automatic hybrid precision program optimization technology cannot effectively improve program execution efficiency, and the search time is long, the search efficiency is low, the error analysis cannot be automated, and the source code file of the optimal variable accuracy configuration file cannot be provided.
By obtaining the source code of the program to be optimized, performing preprocessing and chain automatic differential processing, obtaining the error analysis result file, establishing the initial variable accuracy configuration file and the variable accuracy space file to be searched, optimizing the space to be searched, searching and outputting the optimal variable accuracy configuration file, and applying it to the program source code to generate a mixed-precision version of the program source code.
Automatic static error analysis is realized, which reduces the variable accuracy search space, improves search efficiency, finds the optimal variable accuracy configuration file that can improve program execution efficiency, and provides corresponding source code files for further optimization.
Smart Images

Figure CN119597295A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of compilation optimization, and more specifically, relates to a compilation method and system for automatic mixed precision optimization programs. Background Art
[0002] Compiler optimization is a process that is automatically performed during the compilation process to improve program performance and efficiency. Since the development of computer technology, compiler optimization has become a key component of compiler design.
[0003] With the advancement of artificial intelligence technology and supercomputer performance, numerical software that uses floating-point data for calculations has become increasingly popular, but the computing resource consumption caused by the corresponding calculations has also risen rapidly. Unfortunately, designing computationally intensive applications that are both reliable and energy-efficient has remained a huge challenge in recent years. The reason is that when using floating-point variables for calculations, it is crucial to determine the appropriate floating-point precision. Although high precision can ensure the accuracy and reliability of the program, it may also affect efficiency and lead to unnecessary energy consumption. On most modern processors, the single-precision format runs at least twice as fast as the double-precision format. Therefore, the balance between the accuracy and performance of the calculation program is usually achieved through mixed precision, which refers to the use of different precisions in different operations of the floating-point calculation program. Automatic mixed-precision program optimization technology is seen as a promising method for finding mixed-precision programs that can achieve the best trade-off between performance and accuracy.
[0004] Automatic mixed-precision program optimization technology needs to replace the original precision of floating-point variables in numerical programs with lower precision while ensuring the accuracy of calculation results. However, slight changes in the precision of floating-point variables may cause numerical errors in the calculation program, making it difficult to perform mixed-precision tuning on the calculation program, which has brought difficulties in the fields of deep neural network acceleration, compiler optimization in floating-point calculation programs, and CUDA program acceleration.
[0005] There are two existing automatic mixed-precision program optimization technologies. The first is an automatic mixed-precision program optimization method based on static analysis. This method is based on error analysis and determines whether the precision of the variables can be reduced by analyzing the errors in the calculation results caused by the reduction of the precision of the variables in the program. The second is an automatic mixed-precision program optimization method based on dynamic search. This method obtains a variable precision profile by searching the variable precision search space, and determines whether the execution efficiency is improved by running the program corresponding to the variable precision profile, thereby finding a variable precision profile that can improve the execution efficiency of the program.
[0006] However, both of the above methods have some drawbacks that cannot be ignored:
[0007] First, the automatic mixed-precision optimization technology based on error analysis can only ensure that the accuracy of the calculation results of the program after optimization meets the threshold, but cannot ensure that the execution efficiency of the program after optimization can be improved.
[0008] Second, the automatic mixed-precision program optimization technology based on dynamic search has a large variable search space when there are many floating-point variables in the program, which results in a long search time and low search efficiency for the automatic mixed-precision program optimization based on dynamic search.
[0009] Third, the automatic mixed precision optimization technology based on error analysis cannot realize the automation of error analysis of the program, and largely relies on the manual annotation of the user, which is not convenient to use;
[0010] Fourth, neither of the above two methods can provide the corresponding source code file of the obtained optimal variable precision configuration file, which will be detrimental to further optimization by the user. Summary of the invention
[0011] In response to the above defects or improvement needs of the prior art, the present invention provides a compilation method and system for automatic mixed precision optimization programs, with the purpose of solving the technical problem that the existing automatic mixed precision optimization technology based on error analysis cannot ensure that the execution efficiency of the program can be improved after optimization, and the technical problem that the existing automatic mixed precision program optimization technology based on dynamic search has a large variable search space in scenarios where there are many floating-point variables in the program, resulting in a long search time and low search efficiency for the automatic mixed precision program optimization based on dynamic search, and the technical problem that the existing automatic mixed precision optimization technology based on error analysis cannot realize the automation of error analysis of the program, relies heavily on manual annotation by the user, and the use process is not convenient enough, and the technical problem that neither of the two existing methods can provide the source code file corresponding to the obtained optimal variable precision configuration file, which will be not conducive to further optimization by the user.
[0012] To achieve the above object, according to one aspect of the present invention, a compilation method for an automatic mixed precision optimization program is provided, comprising the following steps:
[0013] (1) Obtain the program source code to be optimized, and preprocess the program source code to be optimized to obtain the preprocessed program source code, that is, the program source code for static error analysis.
[0014] (2) Use the CodiPack function library to perform chain automatic differentiation processing on the program source code that can perform static error analysis obtained in step (1) to obtain an error analysis result file.
[0015] (3) Obtain the source code of the program to be optimized, obtain the information of all variables from the source code of the program to be optimized, and use the information of all variables to establish an initial variable precision configuration file, and use the precision configuration scheme of the current program to establish the variable precision search space file of the current program.
[0016] (4) Using the error analysis result file obtained in step (2), the variable precision search space file obtained in step (3) is optimized to obtain an updated variable precision search space file.
[0017] (5) Obtain multiple variable precision profiles from the updated variable precision search space file obtained in step (4), calculate the performance index of each variable precision profile, and output the variable precision profile with the smallest performance index as the optimal variable precision profile.
[0018] (6) Using the optimal variable precision configuration file obtained in step (5), the program source code file to be optimized is processed to obtain a mixed precision version of the program source code file.
[0019] (7) Use the LLVM compiler to compile the mixed precision version of the program source code file obtained in step (6) into an executable program file, which is the optimized program.
[0020] Preferably, step (1) comprises the following sub-steps:
[0021] (1-1) Take the program source code to be optimized, and insert the relevant header files of the CodiPack function library into the program source code to obtain an updated program source code;
[0022] (1-2) converting the type of the floating-point variable in the updated program source code obtained in step (1-1) into a real number (Real, which is a data format implemented based on the CodiPack function library) to obtain a second updated program source code;
[0023] (1-3) inserting the function call for declaring the intermediate variable into the second updated program source code obtained in step (1-2) to obtain the third updated program source code;
[0024] (1-4) inserting a function call for error analysis result variable declaration into the three-time updated program source code obtained in step (1-3) to obtain a four-time updated program source code;
[0025] (1-5) inserting a function call for printing error analysis results into the program source code after four updates obtained in step (1-4) to obtain a program source code after five updates, that is, a program source code that can perform static error analysis;
[0026] Preferably, step (2) comprises the following sub-steps:
[0027] (2-1) using the LLVM (LLVM project is a collection of modular and reusable compiler and tool chain technologies, "LLVM" itself is not an acronym, but the full name of the project) compiler to perform static error analysis on the five updated program source codes obtained in step (1) to obtain an executable program;
[0028] (2-2) Run the executable program obtained in step (2-1) to obtain an error analysis result file (i.e., a precision sensitivity file of floating-point variables in the program).
[0029] Preferably, step (3) comprises the following sub-steps:
[0030] (3-1) Obtain the program source code to be optimized, use the Clang plug-in implemented based on the LLVM compiler front-end infrastructure to traverse the program source code to be optimized, obtain the variables whose precision can be adjusted, record the information of each variable, and store the obtained information of all variables in JSON file format as an initial variable precision configuration file;
[0031] (3-2) Obtain multiple precision-adjustable variables from the initial variable precision configuration file obtained in step (3-1), add a floating-point format field to each variable to obtain the precision space information to be searched of the variable, and store the precision space information to be searched of all constructed variables in JSON file format as a variable precision space file to be searched.
[0032] Preferably, step (4) is specifically,
[0033] First, obtain the name information of the first variable in the variable precision to be searched space file obtained in step (3), and obtain the variable corresponding to the name information in the error analysis result file obtained in step (2);
[0034] Then, according to the error analysis result file obtained in step (2), it is determined whether the precision of the variable can be reduced. If not, the number of floating point formats used for the variable in the variable precision to be searched space file obtained in step (3) is reduced to the initial precision of the variable. If it is possible, no processing is performed.
[0035] Then, for each remaining variable in the variable precision space file to be searched obtained in step (3), the above process is repeated until all variables in the variable precision space file to be searched are processed, thereby obtaining an updated variable precision space file to be searched.
[0036] Preferably, step (5) comprises the following sub-steps:
[0037] (5-1) Obtaining an error threshold;
[0038] (5-2) Set counter cnt1 = 1;
[0039] (5-3) using the Delta Debugging search algorithm in the time-consuming minimum configuration search algorithm to search for the cnt1th variable precision configuration file that is different from the initial variable precision configuration file obtained in step (3) in the updated variable precision to-be-searched space file obtained in step (4);
[0040] (5-4) converting the program source code to be optimized according to the cnt1th variable precision profile obtained in step (5-3) to obtain a converted source code file corresponding to the cnt1th variable precision profile;
[0041] (5-5) using the LLVM compiler to compile the converted source code file corresponding to the cnt1th variable precision configuration file obtained in step (5-4) to obtain a program corresponding to the cnt1th variable precision configuration file, and running the program corresponding to the cnt1th variable precision configuration file to obtain a calculation result of the program corresponding to the cnt1th variable precision configuration and the time required for program execution;
[0042] (5-6) according to the calculation result of the program corresponding to the cnt1th variable precision profile obtained in step (5-5) and the time required for the program execution, obtain the performance index of the cnt1th variable precision profile obtained in step (5-3);
[0043] (5-7) Determine whether cnt1 is equal to 1. If so, go to step (5-9); otherwise, go to step (5-8).
[0044] (5-8) Use the time-consuming minimum configuration search algorithm to determine whether the performance index of the cnt1th variable accuracy configuration file is better than the currently set optimal performance index. If so, proceed to step (5-9), otherwise proceed to step (5-10);
[0045] (5-9) Using the time-consuming minimum configuration search algorithm, the performance index obtained in step (5-6) is set as the optimal performance index, and the cnt1th variable precision profile is recorded as the optimal variable precision profile;
[0046] (5-10) Determine whether cnt1 is greater than the size of the variable to be searched space. If so, the process ends and the optimal variable accuracy profile is output. Otherwise, go to step (5-11);
[0047] (5-11) Set counter cnt1=cnt1+1 and return to step (5-3);
[0048] Preferably, step (5-4) comprises the following sub-steps:
[0049] (5-4-1) Set counter cnt2 = 1;
[0050] (5-4-2) From the cnt1th variable precision configuration file obtained in step (5-1), obtain the information of the cnt2th variable, including the program module where the cnt2th variable is located, the category of the variable (global variable, local variable), the name of the variable, the precision assigned to the variable, and the line number of the declaration statement corresponding to the variable in the source code to be optimized;
[0051] (5-4-3) According to the information of the cnt2th variable read in step (5-4-2), traverse the source code of the program to be optimized to obtain the declaration statement corresponding to the cnt2th variable, and modify the data type of the cnt2th variable to the floating point format of the cnt1th variable precision configuration file obtained in step (5-3) to obtain the source code file after the cnt2th conversion.
[0052] (5-4-4) Determine whether cnt2 is greater than the total number of variables that need to change precision in the program source code to be optimized. If so, the process ends and the source code file after the cnt2th conversion is output. Otherwise, go to step (5-4-5);
[0053] (5-4-5) Set counter cnt2=cnt2+1 and return to step (5-4-2).
[0054] Preferably, the calculation formula of the performance index in step (5-6) is:
[0055] Gain=T*100 R
[0056] Wherein T represents the time required for program execution; R is 1 or 0. If the calculation result of the program corresponding to the cnt1th variable precision profile is greater than the error threshold obtained in step (5-1), then R is 1; if the calculation result of the program corresponding to the cnt1th variable precision profile is less than or equal to the error threshold obtained in step (5-1), then R is 0.
[0057] Preferably, the size K of the variable to be searched space is equal to:
[0058]
[0059] Where n represents the number of floating-point variables recorded in the variable precision to be searched space file after one update, ai represents the number of floating-point formats used for the i-th floating-point variable, and i ranges from 1 to n. Therefore, the above equation indicates that the value of the variable to be searched space size K is equal to the cumulative multiplication value of the number of optional precisions of each floating-point variable in the program in the variable to be searched space file.
[0060] According to another aspect of the present invention, a compilation system for automatic mixed precision optimization program is provided, comprising:
[0061] The first module is used to obtain the program source code to be optimized and preprocess the program source code to be optimized to obtain the preprocessed program source code, that is, the program source code for static error analysis.
[0062] The second module is used to use the CodiPack function library to perform chain automatic differentiation processing on the program source code that can perform static error analysis obtained in the first module to obtain an error analysis result file.
[0063] The third module is used to obtain the program source code to be optimized, obtain the information of all variables from the program source code to be optimized, and use the information of all variables to establish an initial variable precision configuration file, and use the precision configuration scheme of the current program to establish the variable precision search space file of the current program.
[0064] The fourth module is used to optimize the variable precision space file to be searched obtained by the third module using the error analysis result file obtained by the second module to obtain an updated variable precision space file to be searched.
[0065] The fifth module is used to obtain multiple variable precision profiles from the updated variable precision search space file obtained in the fourth module, calculate the performance index of each variable precision profile, and output the variable precision profile with the smallest performance index as the optimal variable precision profile.
[0066] The sixth module is used to process the program source code file to be optimized using the optimal variable precision configuration file obtained in the fifth module to obtain a mixed precision version of the program source code file.
[0067] The seventh module is used to use the LLVM compiler to compile the mixed precision version of the program source code file obtained in the sixth module into an executable program file, that is, the optimized program.
[0068] In general, the above technical solutions conceived by the present invention can achieve the following beneficial results compared with the prior art:
[0069] 1. Due to the adoption of step (1), the present invention can automatically perform static error analysis on floating-point programs based on chained automatic differentiation, which can reduce a lot of time and labor costs compared with the existing methods that rely on manual annotation and conversion;
[0070] 2. Since the present invention adopts step (2) and step (4), it can use the static error analysis technology based on chained automatic differentiation to obtain the analysis results of the precision sensitivity of the program floating-point variables, thereby reducing the size of the variable search space that needs to be explored when searching for the variable precision configuration file, and can more efficiently find a suitable variable precision configuration file;
[0071] 3. The variable precision profile provided by the present invention in the final step (5-2) to step (5-11) is a variable precision profile that is less than the error threshold and ensures that the performance of the corresponding program is improved. That is to say, it takes into account both the accuracy of the calculation results of the program and the improvement of the execution efficiency of the program.
[0072] 4. Since the present invention adopts steps (5-4) and step (6), its final output provides a variable precision configuration file and a corresponding source code file, which allows the user to further optimize the program using other optimization methods based on the output results of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0073] Figure 1 is a flow chart of a method for compiling an automatic mixed precision optimization program according to the present invention;
[0074] Figure 2 is a detailed flow chart of step (2) in the method of the present invention;
[0075] Figure 3 is a detailed flow chart of step (5) in the method of the present invention;
[0076] Figure 4 It is a detailed flow chart of step (5-4) in the method of the present invention. DETAILED DESCRIPTION
[0077] 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 below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention. In addition, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.
[0078] The basic idea of the present invention is to provide a compilation method for automatic mixed precision optimization program. First, the source code file of the program to be optimized is preprocessed by a static error analysis technology based on chained automatic differentiation, and then the static error analysis technology based on chained automatic differentiation is used to analyze the precision sensitivity of floating-point variables in the program, determine the precision-insensitive variables, and store the variable information in a JSON file; then the source code file of the program to be optimized is used as input, and the file is traversed by using a variable information search tool to obtain the information of all variables in the program to form a variable configuration file, and at the same time, a configuration file of the variable precision to be searched space is formed according to the precision configuration scheme of the current program; then, the result of the error analysis is used to narrow the variable precision to be searched space to form an optimized variable precision to be searched space file; then, the obtained variable configuration file, the optimized configuration file of the variable precision to be searched space and the source code of the program to be optimized are used as input, and the variable precision space search tool is used to search in the to-be-searched space to obtain a recommended precision configuration scheme; finally, the recommended precision configuration scheme and the source code of the program to be optimized are used as inputs of a program conversion tool to obtain an optimized program.
[0079] like Figure 1 As shown, the present invention provides a compilation method for an automatic mixed precision optimization program, comprising the following steps:
[0080] (1) Obtain the program source code to be optimized, and preprocess the program source code to be optimized to obtain the preprocessed program source code, that is, the program source code for static error analysis.
[0081] In the present invention, the source code of the program is a program written in C++ language.
[0082] This step (1) specifically includes the following sub-steps:
[0083] (1-1) Take the program source code to be optimized, and insert the relevant header files of the CodiPack function library into the program source code to obtain an updated program source code;
[0084] (1-2) converting the type of the floating-point variable in the updated program source code obtained in step (1-1) into a real number (Real, which is a data format implemented based on the CodiPack function library) to obtain a second updated program source code;
[0085] (1-3) inserting the function call for declaring the intermediate variable into the second updated program source code obtained in step (1-2) to obtain the third updated program source code;
[0086] Specifically, the function call used for intermediate variable declaration is in the form of: Intermediate(A,B), where the function parameter represented by A is the value of the floating-point variable in the program source code that participates in calculating the final calculation result, and the function parameter represented by B is the name of the floating-point variable.
[0087] (1-4) inserting a function call for error analysis result variable declaration into the three-time updated program source code obtained in step (1-3) to obtain a four-time updated program source code;
[0088] Specifically, the error analysis result variable declaration function call form is: Dependent(A,B,E), where A represents the function parameter which is the value of the floating-point variable storing the final calculation result in the program source code, B represents the function parameter which is the name of the floating-point variable, and E represents the value of the calculation result error threshold set by the user in scientific notation.
[0089] (1-5) inserting a function call for printing error analysis results into the program source code after four updates obtained in step (1-4) to obtain a program source code after five updates, that is, a program source code that can perform static error analysis;
[0090] Specifically, the function call form for printing the error analysis results is: Report().
[0091] The advantage of this step (1) is that the preprocessing before error analysis of obtaining the program source code that can perform static error analysis from the source code of the program to be optimized is all automated. The user only needs to provide the program source code, the variables storing the calculation results in the program, and the error threshold of the calculation results, without having to spend time manually converting the program source code, which can save time and labor costs.
[0092] (2) Use the CodiPack function library to perform chain automatic differentiation processing on the program source code that can perform static error analysis obtained in step (1) to obtain an error analysis result file.
[0093] like Figure 2 As shown, this step (2) specifically includes the following sub-steps:
[0094] (2-1) using the LLVM (LLVM project is a collection of modular and reusable compiler and tool chain technologies, "LLVM" itself is not an acronym, but the full name of the project) compiler to perform static error analysis on the five updated program source codes obtained in step (1) to obtain an executable program;
[0095] (2-2) Run the executable program obtained in step (2-1) to obtain an error analysis result file (i.e., a precision sensitivity file of floating-point variables in the program).
[0096] The advantages of this step (2) are that, on the one hand, the static error analysis technology based on chained automatic differentiation is based on reliable mathematical principles, and the error analysis results obtained are strict and highly credible; on the other hand, the error analysis results are stored in the form of a JSON file, which is highly readable. Users can choose to use the analysis results directly, or they can have mathematical experts conduct further analysis based on the error analysis results and then modify the error analysis result file as a basis for the subsequent optimization of the variable accuracy search space.
[0097] (3) Obtain the source code of the program to be optimized, obtain the information of all variables from the source code of the program to be optimized, and use the information of all variables to establish an initial variable precision configuration file, and use the precision configuration scheme of the current program to establish the variable precision search space file of the current program.
[0098] This step (3) specifically includes the following sub-steps:
[0099] (3-1) Obtain the program source code to be optimized, use the Clang plug-in implemented based on the LLVM compiler front-end infrastructure to traverse the program source code to be optimized, obtain the variables whose precision can be adjusted, and record the information of each variable (including the current precision of the variable, the position and name in the program source code, the line number of the declaration statement corresponding to the variable in the program source code, etc.), and store the information of all variables obtained in JSON file format as an initial variable precision configuration file;
[0100] It should be noted that the Clang project is a language front-end and tool infrastructure that provides C language series languages (C, C++, ObjectiveC / C++, OpenCL and CUDA) in the LLVM project. "Clang" itself is not an acronym, but the full name of the project.
[0101] (3-2) Obtain multiple precision-adjustable variables from the initial variable precision configuration file obtained in step (3-1), add a floating-point format field to each variable to obtain the precision space information to be searched of the variable (including the current precision of the variable, the floating-point format that can be selected for the variable, the position and name of the variable in the program source code, the line number of the declaration statement corresponding to the variable in the program source code, etc.), and store the precision space information to be searched of all constructed variables in JSON file format as a variable precision space to be searched file.
[0102] It should be noted that the floating point formats in this step include half-precision floating point (Brain float16, referred to as BF16), single-precision floating point (float), double-precision floating point (double), and long double-precision floating point (long double).
[0103] The advantages of this step (3) are that, on the one hand, the variable information search function is implemented based on the Clang plug-in written based on the LLVM front-end infrastructure, acts on the source code, is independent of the computer architecture, and has good portability; on the other hand, the JSON file initial variable precision configuration file and the variable precision search space file have good readability and modifiability. At the same time, the file records the names of the variables in the program source code. The user can freely determine the variable set to be analyzed by modifying the JSON file.
[0104] (4) Using the error analysis result file obtained in step (2), the variable precision search space file obtained in step (3) is optimized to obtain an updated variable precision search space file.
[0105] Specifically, step (4) includes first obtaining the name information of the first variable in the variable precision to-be-searched space file obtained in step (3), and obtaining the variable corresponding to the name information in the error analysis result file obtained in step (2);
[0106] Then, according to the error analysis result file obtained in step (2), it is determined whether the precision of the variable can be reduced. If not, the number of floating point formats used for the variable in the variable precision to be searched space file obtained in step (3) is reduced to the initial precision of the variable. If it is possible, no processing is performed.
[0107] Then, for each remaining variable in the variable precision space file to be searched obtained in step (3), the above process is repeated until all variables in the variable precision space file to be searched are processed, thereby obtaining an updated variable precision space file to be searched.
[0108] For example, the original precision of variable A recorded in the variable precision to be searched space file obtained in step (3) is long double precision floating point (long double), and the floating point formats that can be selected for variable A include four different floating point formats: half precision floating point (BF16), single precision floating point (float), double precision floating point (double), and long double precision floating point (long double). However, the error analysis result file obtained in step (2) specifies that variable A cannot be represented with low precision. Then, the floating point format that can be selected for variable A in the variable precision to be searched space file after an update is reduced to only long double precision floating point (longdouble) floating point format, thereby reducing the size of the variable to be searched space.
[0109] The advantage of the above steps (1) to (4) is that the error analysis results obtained in step (2) can effectively reduce the number of floating-point formats that can be selected for some variables, thereby achieving the purpose of narrowing the variable precision search space, making it necessary to explore fewer situations when searching for variable precision profiles in the precision search space, which can speed up the search process, reduce the number of precision allocation schemes that need to be tested, reduce the number of program runs, and ultimately achieve the purpose of reducing the overall optimization time.
[0110] (5) Obtain multiple variable precision profiles from the updated variable precision search space file obtained in step (4), calculate the performance index of each variable precision profile, and output the variable precision profile with the smallest performance index as the optimal variable precision profile.
[0111] like Figure 3 As shown, this step (5) specifically includes the following sub-steps:
[0112] (5-1) Obtaining an error threshold;
[0113] Specifically, the error threshold is set by the user in the form of scientific notation, and its value range is 10E-4 to 10E-10, preferably 10E-6.
[0114] (5-2) Set counter cnt1 = 1;
[0115] (5-3) using the Delta Debugging search algorithm in the time-consuming minimum configuration search algorithm to search for the cnt1th variable precision configuration file that is different from the initial variable precision configuration file obtained in step (3) in the updated variable precision to-be-searched space file obtained in step (4);
[0116] The time-consuming minimum configuration search algorithm in the present invention is described in detail in the paper Precimonious: Tuning Assistant for Floating-Point Precision by Cindy Rubio-Gonzalez et al., published in the Proceedings of the International Conference for High Performance Computing, Networking, Storage and Analysis (SC) in November 2013.
[0117] Specifically, the variable precision configuration file obtained is stored in the JSON file format, including the variable's program position, name, line number in the source code where the variable declaration statement is located, and the representation precision assigned to the variable, which will help improve the work efficiency of subsequent program conversion.
[0118] (5-4) converting the program source code to be optimized according to the cnt1th variable precision profile obtained in step (5-3) to obtain a converted source code file corresponding to the cnt1th variable precision profile;
[0119] like Figure 4 As shown, this step (5-4) specifically includes the following sub-steps:
[0120] (5-4-1) Set counter cnt2 = 1;
[0121] (5-4-2) From the cnt1th variable precision configuration file obtained in step (5-1), obtain the information of the cnt2th variable, including the program module where the cnt2th variable is located, the category of the variable (global variable, local variable), the name of the variable, the precision assigned to the variable, and the line number of the declaration statement corresponding to the variable in the source code to be optimized;
[0122] (5-4-3) According to the information of the cnt2th variable read in step (5-4-2), traverse the source code of the program to be optimized to obtain the declaration statement corresponding to the cnt2th variable, and modify the data type of the cnt2th variable to the floating point format of the cnt1th variable precision configuration file obtained in step (5-3) to obtain the source code file after the cnt2th conversion.
[0123] (5-4-4) Determine whether cnt2 is greater than the total number of variables that need to change precision in the program source code to be optimized. If so, the process ends and the source code file after the cnt2th conversion is output. Otherwise, go to step (5-4-5);
[0124] (5-4-5) Set counter cnt2 = cnt2 + 1, and return to step (5-4-2);
[0125] (5-5) using the LLVM compiler to compile the converted source code file corresponding to the cnt1th variable precision configuration file obtained in step (5-4) to obtain a program corresponding to the cnt1th variable precision configuration file, and running the program corresponding to the cnt1th variable precision configuration file to obtain a calculation result of the program corresponding to the cnt1th variable precision configuration and the time required for program execution;
[0126] (5-6) according to the calculation result of the program corresponding to the cnt1th variable precision profile obtained in step (5-5) and the time required for the program execution, obtain the performance index of the cnt1th variable precision profile obtained in step (5-3);
[0127] Specifically, the calculation formula of the performance index in this step is:
[0128] Gain=T*100 R
[0129] Wherein T represents the time required for program execution; R is 1 or 0. If the calculation result of the program corresponding to the cnt1th variable precision profile is greater than the error threshold obtained in step (5-1), then R is 1; if the calculation result of the program corresponding to the cnt1th variable precision profile is less than or equal to the error threshold obtained in step (5-1), then R is 0.
[0130] The smaller the performance indicator result obtained according to the calculation formula, the shorter the time required to calculate the variable accuracy profile while meeting the error limit requirement, and the better the performance.
[0131] (5-7) Determine whether cnt1 is equal to 1. If so, go to step (5-9); otherwise, go to step (5-8).
[0132] (5-8) Use the time-consuming minimum configuration search algorithm to determine whether the performance index of the cnt1th variable accuracy configuration file is better than the currently set optimal performance index. If so, proceed to step (5-9), otherwise proceed to step (5-10);
[0133] (5-9) Using the time-consuming minimum configuration search algorithm, the performance index obtained in step (5-6) is set as the optimal performance index, and the cnt1th variable precision profile is recorded as the optimal variable precision profile;
[0134] (5-10) Determine whether cnt1 is greater than the size of the variable to be searched space. If so, the process ends and the optimal variable accuracy profile is output. Otherwise, go to step (5-11);
[0135] Specifically, the size K of the variable to be searched space in the present invention is equal to:
[0136]
[0137] Where n represents the number of floating-point variables recorded in the variable precision to be searched space file after one update, ai represents the number of floating-point formats used for the i-th floating-point variable, and i ranges from 1 to n. Therefore, the above equation indicates that the value of the variable to be searched space size K is equal to the cumulative multiplication value of the number of optional precisions of each floating-point variable in the program in the variable to be searched space file.
[0138] (5-11) Set counter cnt1=cnt1+1 and return to step (5-3);
[0139] (6) Using the optimal variable precision configuration file obtained in step (5), the program source code file to be optimized is processed to obtain a mixed precision version of the program source code file.
[0140] (7) Use the LLVM compiler to compile the mixed precision version of the program source code file obtained in step (6) into an executable program file, which is the optimized program.
[0141] It will be easily understood by those skilled in the art that the above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A compilation method for automatic mixed precision optimization program, characterized in that: The following steps are involved: (1) Obtain the program source code to be optimized, and preprocess the program source code to be optimized to obtain the preprocessed program source code, that is, the program source code for static error analysis. (2) Use the CodiPack function library to perform chain automatic differentiation processing on the program source code that can perform static error analysis obtained in step (1) to obtain an error analysis result file. (3) Obtain the source code of the program to be optimized, obtain the information of all variables from the source code of the program to be optimized, and use the information of all variables to establish an initial variable precision configuration file, and use the precision configuration scheme of the current program to establish the variable precision search space file of the current program. (4) Using the error analysis result file obtained in step (2), the variable precision search space file obtained in step (3) is optimized to obtain an updated variable precision search space file. (5) Obtain multiple variable precision profiles from the updated variable precision search space file obtained in step (4), calculate the performance index of each variable precision profile, and output the variable precision profile with the smallest performance index as the optimal variable precision profile. (6) Using the optimal variable precision configuration file obtained in step (5), the program source code file to be optimized is processed to obtain a mixed precision version of the program source code file. (7) Use the LLVM compiler to compile the mixed precision version of the program source code file obtained in step (6) into an executable program file, which is the optimized program.
2. The method for compiling an automatic mixed precision optimization program according to claim 1, characterized in that: Step (1) includes the following sub-steps: (1-1) Take the program source code to be optimized, and insert the relevant header files of the CodiPack function library into the program source code to obtain an updated program source code; (1-2) converting the type of the floating-point variable in the updated program source code obtained in step (1-1) into a real number (Real, which is a data format implemented based on the CodiPack function library) to obtain a second updated program source code; (1-3) inserting the function call for declaring the intermediate variable into the second updated program source code obtained in step (1-2) to obtain the third updated program source code; (1-4) inserting a function call for error analysis result variable declaration into the three-time updated program source code obtained in step (1-3) to obtain a four-time updated program source code; (1-5) Inserting the function call for printing the error analysis result into the program source code after four updates obtained in step (1-4) to obtain the program source code after five updates, that is, the program source code that can perform static error analysis.
3. The method for compiling an automatic mixed precision optimization program according to claim 1 or 2, characterized in that: Step (2) includes the following sub-steps: (2-1) using the LLVM (LLVM project is a collection of modular and reusable compiler and tool chain technologies, "LLVM" itself is not an acronym, but the full name of the project) compiler to perform static error analysis on the five updated program source codes obtained in step (1) to obtain an executable program; (2-2) Run the executable program obtained in step (2-1) to obtain an error analysis result file (i.e., a precision sensitivity file of floating-point variables in the program).
4. The method for compiling an automatic mixed precision optimization program according to any one of claims 1 to 3, characterized in that: Step (3) includes the following sub-steps: (3-1) Obtain the program source code to be optimized, use the Clang plug-in implemented based on the LLVM compiler front-end infrastructure to traverse the program source code to be optimized, obtain the variables whose precision can be adjusted, record the information of each variable, and store the obtained information of all variables in JSON file format as an initial variable precision configuration file; (3-2) Obtain multiple precision-adjustable variables from the initial variable precision configuration file obtained in step (3-1), add a floating-point format field to each variable to obtain the precision space information to be searched of the variable, and store the precision space information to be searched of all constructed variables in JSON file format as a variable precision space file to be searched.
5. The method for compiling an automatic mixed precision optimization program according to claim 4, characterized in that: Step (4) is specifically: First, obtain the name information of the first variable in the variable precision to be searched space file obtained in step (3), and obtain the variable corresponding to the name information in the error analysis result file obtained in step (2); Then, according to the error analysis result file obtained in step (2), it is determined whether the precision of the variable can be reduced. If not, the number of floating point formats used for the variable in the variable precision to be searched space file obtained in step (3) is reduced to the initial precision of the variable. If it is possible, no processing is performed. Then, for each remaining variable in the variable precision space file to be searched obtained in step (3), the above process is repeated until all variables in the variable precision space file to be searched are processed, thereby obtaining an updated variable precision space file to be searched.
6. The method for compiling an automatic mixed precision optimization program according to claim 5, characterized in that: Step (5) includes the following sub-steps: (5-1) Obtaining an error threshold; (5-2) Set counter cnt1 = 1; (5-3) using the Delta Debugging search algorithm in the time-consuming minimum configuration search algorithm to search for the cnt1th variable precision configuration file that is different from the initial variable precision configuration file obtained in step (3) in the updated variable precision to-be-searched space file obtained in step (4); (5-4) converting the program source code to be optimized according to the cnt1th variable precision profile obtained in step (5-3) to obtain a converted source code file corresponding to the cnt1th variable precision profile; (5-5) using the LLVM compiler to compile the converted source code file corresponding to the cnt1th variable precision configuration file obtained in step (5-4) to obtain a program corresponding to the cnt1th variable precision configuration file, and running the program corresponding to the cnt1th variable precision configuration file to obtain a calculation result of the program corresponding to the cnt1th variable precision configuration and the time required for program execution; (5-6) according to the calculation result of the program corresponding to the cnt1th variable precision profile obtained in step (5-5) and the time required for the program execution, obtain the performance index of the cnt1th variable precision profile obtained in step (5-3); (5-7) Determine whether cnt1 is equal to 1. If so, go to step (5-9); otherwise, go to step (5-8). (5-8) Use the time-consuming minimum configuration search algorithm to determine whether the performance index of the cnt1th variable accuracy configuration file is better than the currently set optimal performance index. If so, proceed to step (5-9), otherwise proceed to step (5-10); (5-9) Using the time-consuming minimum configuration search algorithm, the performance index obtained in step (5-6) is set as the optimal performance index, and the cnt1th variable precision profile is recorded as the optimal variable precision profile; (5-10) Determine whether cnt1 is greater than the size of the variable to be searched space. If so, the process ends and the optimal variable accuracy profile is output. Otherwise, go to step (5-11); (5-11) Set counter cnt1=cnt1+1 and return to step (5-3).
7. The method for compiling an automatic mixed precision optimization program according to claim 6, characterized in that: Step (5-4) includes the following sub-steps: (5-4-1) Set counter cnt2 = 1; (5-4-2) From the cnt1th variable precision configuration file obtained in step (5-1), obtain the information of the cnt2th variable, including the program module where the cnt2th variable is located, the category of the variable (global variable, local variable), the name of the variable, the precision assigned to the variable, and the line number of the declaration statement corresponding to the variable in the source code to be optimized; (5-4-3) According to the information of the cnt2th variable read in step (5-4-2), traverse the source code of the program to be optimized to obtain the declaration statement corresponding to the cnt2th variable, and modify the data type of the cnt2th variable to the floating point format of the cnt1th variable precision configuration file obtained in step (5-3) to obtain the source code file after the cnt2th conversion. (5-4-4) Determine whether cnt2 is greater than the total number of variables that need to change precision in the program source code to be optimized. If so, the process ends and the source code file after the cnt2th conversion is output. Otherwise, go to step (5-4-5); (5-4-5) Set counter cnt2=cnt2+1 and return to step (5-4-2).
8. The method for compiling an automatic mixed precision optimization program according to claim 7, characterized in that: The calculation formula of the performance index in step (5-6) is: Gain=T*100 R Wherein T represents the time required for program execution; R is 1 or 0. If the calculation result of the program corresponding to the cntl-th variable precision profile is greater than the error threshold obtained in step (5-1), then R is 1; if the calculation result of the program corresponding to the cntl-th variable precision profile is less than or equal to the error threshold obtained in step (5-1), then R is 0.
9. The method for compiling an automatic mixed precision optimization program according to claim 8, characterized in that: The size K of the variable to be searched space is equal to: Where n represents the number of floating-point variables recorded in the variable precision to be searched space file after one update, ai represents the number of floating-point formats used for the i-th floating-point variable, and i ranges from 1 to n. Therefore, the above equation indicates that the value of the variable to be searched space size K is equal to the cumulative multiplication value of the number of optional precisions of each floating-point variable in the program in the variable to be searched space file.
10. A compilation system for automatic mixed precision optimization program, characterized in that: include: The first module is used to obtain the program source code to be optimized and preprocess the program source code to be optimized to obtain the preprocessed program source code, that is, the program source code for static error analysis. The second module is used to use the CodiPack function library to perform chain automatic differentiation processing on the program source code that can perform static error analysis obtained in the first module to obtain an error analysis result file. The third module is used to obtain the program source code to be optimized, obtain the information of all variables from the program source code to be optimized, and use the information of all variables to establish an initial variable precision configuration file, and use the precision configuration scheme of the current program to establish the variable precision search space file of the current program. The fourth module is used to optimize the variable precision space file to be searched obtained by the third module using the error analysis result file obtained by the second module to obtain an updated variable precision space file to be searched. The fifth module is used to obtain multiple variable precision profiles from the updated variable precision search space file obtained in the fourth module, calculate the performance index of each variable precision profile, and output the variable precision profile with the smallest performance index as the optimal variable precision profile. The sixth module is used to process the program source code file to be optimized using the optimal variable precision configuration file obtained in the fifth module to obtain a mixed precision version of the program source code file. The seventh module is used to use the LLVM compiler to compile the mixed precision version of the program source code file obtained by the sixth module into an executable program file, that is, the optimized program.
Citation Information
Patent Citations
A software defined satellite adaptive pruning model compression method
CN113657592A
Grouping mixing precision configuration scheme searching method based on sensitivity model
CN114969446A
Program efficiency optimization method and system based on multi-compilation fusion optimization
CN118132078A
Calculation compilation optimization implementation method and system based on error-free transformation
CN118227138A
Systems and methods for performing static analysis on source code
US7340726B1