A method and system for automatically mixed precision optimization of programs

Through chained automatic differentiation and Delta Debugging search algorithms, the precision sensitivity of floating-point programs is automatically analyzed, solving the problems of low execution efficiency and long search time in existing technologies, providing an optimal variable precision profile, and achieving a balance between program execution efficiency and accuracy.

CN119597295BActive Publication Date: 2025-10-10HUNAN UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411649366.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-19
Publication Date
2025-10-10
Estimated Expiration
2044-11-19

AI Technical Summary

Technical Problem

Existing automatic mixed-precision optimization technology cannot guarantee improved program execution efficiency after optimization. The large variable search space leads to long search time, relying on manual labeling is inconvenient, and it cannot provide source code files with optimal variable precision profiles.

Method used

This paper adopts the static error analysis technology of chained automatic differentiation, uses the CodiPack function library and the LLVM compiler to automatically analyze the precision sensitivity of floating-point programs, narrow the variable precision search space, use the Delta Debugging search algorithm to find the optimal variable precision profile, and generate a mixed-precision version of the program source code.

Benefits of technology

It improves program execution efficiency, reduces search time and manual annotation costs, and provides optimal variable precision profiles for further optimization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119597295B_ABST
    Figure CN119597295B_ABST
Patent Text Reader

Abstract

The application discloses a kind of compilation methods for automatically mixed precision optimization procedure, first the preprocessing of the source code file of the program to be optimized is carried out based on the static error analysis technique of chain automatic differentiation, then the precision sensitivity of floating point variable in program is analyzed using the static error analysis technique based on the chain automatic differentiation, precision insensitive variable is determined, and variable information is stored in JSON file;Again, the source code file of the program to be optimized is used as input, the file is traversed by using variable information search tool, the information of all variables in the program is obtained to form a variable configuration file, and a configuration file of variable precision search space is formed according to the precision configuration scheme of the current program;Using the result of error analysis, the variable precision search space is reduced, and the optimized variable precision search space file is formed;The application can solve the technical problem that the execution efficiency of the optimized program cannot be improved by the automatic mixed precision optimization technology based on error analysis.
Need to check novelty before this filing date? Find Prior Art

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 advances in artificial intelligence (AI) and supercomputer performance, numerical software that heavily utilizes floating-point data for computation has become increasingly popular. However, the computational resource consumption associated with these calculations has also rapidly increased. Unfortunately, designing reliable and energy-efficient computationally intensive applications has remained a significant challenge in recent years. This is because determining the appropriate floating-point precision is crucial when performing calculations with floating-point variables. While high precision ensures program accuracy and reliability, it can also compromise efficiency and lead to unnecessary energy consumption. On most modern processors, single-precision formats run at least twice as fast as double-precision formats. Therefore, a balance between accuracy and performance in computational programs is often achieved through mixed precision, which involves using different precisions for different operations in floating-point programs. Automatic mixed-precision program optimization is considered a promising approach for finding mixed-precision programs that achieve the optimal trade-off between performance and accuracy.

[0004] Automatic mixed-precision program optimization technology replaces the original precision of floating-point variables in numerical programs with lower precision while ensuring accurate computational results. However, slight changes in the precision of floating-point variables can cause numerical errors in the computational program, making mixed-precision tuning difficult. This poses challenges in areas such as deep neural network acceleration, compiler optimization of floating-point computational 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 above automatic mixed precision optimization technique based on error analysis can only guarantee that the calculation result precision of the optimized program meets the threshold value, and cannot guarantee that the execution efficiency of the optimized program can be improved.

[0008] Second, the above automatic mixed precision program optimization technique based on dynamic search has a large variable search space in the case of using a large number of floating-point variables in the program, resulting in long search time and low search efficiency of the automatic mixed precision program optimization based on dynamic search.

[0009] Third, the above automatic mixed precision optimization technique based on error analysis cannot realize the automation of error analysis of the program, and largely depends on manual annotation by the user, which is not convenient for use.

[0010] Fourth, the above two methods cannot provide the corresponding source code file of the obtained optimal variable precision configuration file, which will not be conducive to further optimization by the user. SUMMARY

[0011] In view of the above defects or improvement needs of the prior art, the present application provides a compilation method and system for automatically optimizing a program with mixed precision, which aims to solve the technical problems that the existing automatic mixed precision optimization technique based on error analysis cannot guarantee that the execution efficiency of the optimized program can be improved, and the existing automatic mixed precision program optimization technique based on dynamic search has a large variable search space in the case of using a large number of floating-point variables in the program, resulting in long search time and low search efficiency of the automatic mixed precision program optimization based on dynamic search, and the existing automatic mixed precision optimization technique based on error analysis cannot realize the automation of error analysis of the program, and largely depends on manual annotation by the user, which is not convenient for use, and the existing two methods cannot provide the corresponding source code file of the obtained optimal variable precision configuration file, which will not be conducive to further optimization by the user.

[0012] To achieve the above-mentioned purpose, according to one aspect of the present application, a compilation method for automatically optimizing a program with mixed precision is provided, comprising the following steps:

[0013] (1) Obtain the source code of the program to be optimized, and preprocess the source code of the program to be optimized to obtain the preprocessed program source code, i.e., the program source code that can be subjected to static error analysis.

[0014] (2) Use the CodiPack function library to perform chain automatic differentiation on the program source code that can be subjected to static error analysis obtained in step (1) to obtain an error analysis result file.

[0015] (3) obtaining the program source code to be optimized, obtaining information of all variables from the program source code to be optimized, and using the information of all variables to establish an initial variable precision configuration file and using the precision configuration scheme of the current program to establish a variable precision to-be-searched space file of the current program.

[0016] (4) using the error analysis result file obtained in step (2) to optimize the variable precision to-be-searched space file obtained in step (3) to obtain a first updated variable precision to-be-searched space file.

[0017] (5) obtaining a plurality of variable precision configuration files from the first updated variable precision to-be-searched space file obtained in step (4), calculating a performance index of each variable precision configuration file, and outputting the variable precision configuration file with the minimum performance index as the optimal variable precision configuration file.

[0018] (6) using the optimal variable precision configuration file obtained in step (5) to process the program source code file to be optimized to obtain a mixed precision version of the program source code file.

[0019] (7) using an LLVM compiler to compile the mixed precision version of the program source code file obtained in step (6) into an executable program file, i.e., an optimized program.

[0020] Preferably, step (1) comprises the following sub-steps:

[0021] (1-1) taking the program source code to be optimized, inserting the relevant header files of the CodiPack function library into the program source code to obtain a first updated program source code;

[0022] (1-2) converting the type of the floating-point variable in the first updated program source code obtained in step (1-1) into a real number (Real, which is a data format realized based on the CodiPack function library) to obtain a second updated program source code;

[0023] (1-3) inserting a function call for declaring an intermediate variable into the second updated program source code obtained in step (1-2) to obtain a third updated program source code;

[0024] (1-4) inserting a function call for declaring an error analysis result variable into the third updated program source code obtained in step (1-3) to obtain a fourth updated program source code;

[0025] (1-5) inserting a function call for printing the error analysis result into the fourth updated program source code obtained in step (1-4) to obtain a fifth updated program source code, i.e., a program source code that can be subjected to 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 toolchain technologies, "LLVM" itself is not an acronym, but the full name of the project) compiler, perform static error analysis on the program source code after the five updates 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) includes 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 obtain 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 for 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, based on 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 search space file obtained in step (3) is reduced to the initial precision of the variable. If it can be, no processing is performed.

[0035] Then, for each remaining variable in the variable precision to be searched space file obtained in step (3), the above process is repeated until all variables in the variable precision to be searched space file are processed, thereby obtaining an updated variable precision to be searched space file.

[0036] Preferably, step (5) comprises the following sub-steps:

[0037] (5-1) Obtain an error threshold value;

[0038] (5-2) Set a counter cnt1 = 1;

[0039] (5-3) Use the Delta Debugging search algorithm in the time consumption minimum configuration search algorithm to search for a cnt1th variable precision configuration file different from the initial variable precision configuration file obtained in step (3) in the once-updated variable precision to-be-searched space file obtained in step (4);

[0040] (5-4) Convert the program source code to be optimized according to the cnt1th variable precision configuration file obtained in step (5-3) to obtain a converted source code file corresponding to the cnt1th variable precision configuration file;

[0041] (5-5) Use 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 run the program corresponding to the cnt1th variable precision configuration file to obtain the calculation result of the program corresponding to the cnt1th variable precision configuration file and the time required for program execution;

[0042] (5-6) Obtain the performance index of the cnt1th variable precision configuration file obtained in step (5-3) according to the calculation result of the program corresponding to the cnt1th variable precision configuration file obtained in step (5-5) and the time required for program execution;

[0043] (5-7) Determine whether cnt1 is equal to 1. If yes, go to step (5-9); otherwise, go to step (5-8).

[0044] (5-8) Use the time consumption minimum configuration search algorithm to determine whether the performance index of the cnt1th variable precision configuration file is better than the currently set optimal performance index. If yes, go to step (5-9); otherwise, go to step (5-10);

[0045] (5-9) Use the time consumption minimum configuration search algorithm to set the performance index obtained in step (5-6) as the optimal performance index, and record the cnt1th variable precision configuration file as the optimal variable precision configuration file;

[0046] (5-10) Determine whether cnt1 is greater than the size of the variable to-be-searched space. If yes, the process ends, and the optimal variable precision configuration file 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) includes 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 steps (5-6) is:

[0055] Gain=T*100 R

[0056] Where 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 search space is equal to:

[0058]

[0059] Where n represents the number of floating-point variables recorded in the variable precision search space file after a single 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 search space size K is equal to the cumulative value multiplied by the number of precision options available for each floating-point variable in the program as listed in the variable search space file.

[0060] According to another aspect of the present invention, a compilation system for automatic mixed-precision optimization programs 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 to be searched file obtained by the third module using the error analysis result file obtained by the second module to obtain an updated variable precision space to be searched file.

[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. Due to the adoption of steps (2) and (4), the present invention can utilize 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 profile, and can more efficiently find the appropriate variable precision profile;

[0071] 3. The variable precision profile provided by the present invention through steps (5-2) to (5-11) is less than the error threshold and ensures that the performance of the corresponding program is improved. In other words, it takes into account both the accuracy of the program's calculation results and the improvement of the program's execution efficiency.

[0072] 4. Since the present invention adopts steps (5-4) and (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 objectives, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely for the purpose of explaining 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 may 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 programs. First, the source code file of the program to be optimized is preprocessed using a static error analysis technique based on chained automatic differentiation. Then, the static error analysis technique based on chained automatic differentiation is used to analyze the precision sensitivity of floating-point variables in the program, determine 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 a variable information search tool is used to traverse the file to obtain information about all variables in the program to form a variable configuration file. At the same time, a configuration file for a variable precision search space is formed based on the current program's precision configuration scheme. Then, the error analysis results are used to narrow the variable precision search space to form an optimized variable precision search space file. Then, the variable configuration file, the optimized variable precision search space configuration file, 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 the search 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 to a program conversion tool to obtain the 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, which 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 source code of the program to be optimized and insert the relevant header files of the CodiPack function library into the source code of the program to obtain the updated source code of the program;

[0084] (1-2) converting the types of floating-point variables in the updated program source code obtained in step (1-1) into real numbers (Real, 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 the intermediate variable declaration into the second-updated program source code obtained in step (1-2) to obtain a third-updated program source code;

[0086] Specifically, the form of the function call used to declare an intermediate variable is: 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-updated program source code obtained in step (1-3) to obtain a four-updated program source code;

[0088] Specifically, the error analysis result variable declaration function call format is: Dependent(A,B,E), where the function parameter represented by A is the value of the floating-point variable that stores the final calculation result in the program source code, the function parameter represented by B 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 the program source code after five updates, that is, the program source code that can be used for static error analysis;

[0090] Specifically, the function call format for printing the error analysis results is: Report().

[0091] The advantage of this step (1) is that the pre-processing 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 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) Using the CodiPack function library, the program source code obtained in step (1) that can be used for static error analysis is subjected to chain automatic differentiation processing 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 toolchain technologies, "LLVM" itself is not an acronym, but the full name of the project) compiler, perform static error analysis on the program source code after the five updates 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 has a reliable mathematical principle as its basis, and the error analysis results obtained are rigorous 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 further analyze the error analysis results and then modify the error analysis result file as the 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, and use the Clang plug-in implemented based on the LLVM compiler front-end infrastructure to traverse the program source code to obtain 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 the language front-end and tool infrastructure that provides the C language family (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 for 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 for 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 initial variable precision configuration file and the variable precision search space file in the JSON 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 decide 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, based on 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 search space file obtained in step (3) is reduced to the initial precision of the variable. If it can be, no processing is performed.

[0107] Then, for each remaining variable in the variable precision to be searched space file obtained in step (3), the above process is repeated until all variables in the variable precision to be searched space file are processed, thereby obtaining an updated variable precision to be searched space file.

[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 in low precision. Then, the floating-point format that can be selected for variable A in the variable precision to be searched space file after one update is reduced to only long double-precision floating point (longdouble), 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 possible to reduce the number of situations that need to be explored when searching for variable precision profiles in the precision search space, speeding up the search process, reducing the number of quantity precision allocation schemes that need to be tested, reducing the number of times the program is run, and ultimately achieving 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 the error threshold;

[0113] Specifically, the error threshold is set by the user in 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, searching for the cnt1th variable precision profile that is different from the initial variable precision profile obtained in step (3) in the updated variable precision to-be-searched space file obtained in step (4);

[0116] The time-minimizing configuration search algorithm of the present invention is described in detail in the paper "Precimonious: Tuning Assistant for Floating-Point Precision" by Cindy Rubio-González 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 obtained variable precision configuration file is stored in JSON file format, including the variable's program location, 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 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) Compile the converted source code file corresponding to the cnt1th variable precision configuration file obtained in step (5-4) using the LLVM compiler to obtain a program corresponding to the cnt1th variable precision configuration, and run the program corresponding to the cnt1th variable precision configuration file to obtain the calculation result of the program corresponding to the cnt1th variable precision configuration and the time required for program execution;

[0126] (5-6) Obtain the performance index of the cnt1th variable precision configuration file obtained in step (5-3) according to the calculation result of the program corresponding to the cnt1th variable precision configuration file obtained in step (5-5) and the time required for program execution;

[0127] Specifically, the calculation formula of the performance index in this step is:

[0128] Gain = T * 100 R

[0129] Where 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 configuration file 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 configuration file is less than or equal to the error threshold obtained in step (5-1), then R is 0.

[0130] The smaller the performance index result obtained according to the calculation formula, the smaller the time required for calculation under the requirement of reaching the error limit, and the better the performance.

[0131] (5-7) Determine whether cnt1 is equal to 1, if yes, go to step (5-9), otherwise go to step (5-8).

[0132] (5-8) Use the time consumption minimum configuration search algorithm to determine whether the performance index of the cnt1th variable precision configuration file is better than the optimal performance index currently set, if yes, go to step (5-9), otherwise go to step (5-10);

[0133] (5-9) Use the time consumption minimum configuration search algorithm to set the performance index obtained in step (5-6) as the optimal performance index, and record the cnt1th variable precision configuration file as the optimal variable precision configuration file;

[0134] (5-10) Determine whether cnt1 is larger 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 search space file after a single 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 search space size K is equal to the cumulative value multiplied by the number of precision options available for each floating-point variable in the program as listed in the variable search space file.

[0138] (5-11) Set counter cnt1 = cnt1 + 1, and return to step (5-3);

[0139] (6) Using the optimal variable precision profile 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 merely 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 scope of protection of the present invention.

Claims

1. A compilation method for automatic mixed precision optimization program, characterized in that: The following steps are involved: (1) obtaining the source code of the program to be optimized and preprocessing the source code of the program to be optimized to obtain the preprocessed source code of the program, that is, the source code of the program for static error analysis; (2) Using the CodiPack function library, chain automatic differentiation is performed on the program source code that can be used for static error analysis obtained in step (1) to obtain an error analysis result file; (3) obtaining the source code of the program to be optimized, obtaining information of all variables from the source code of the program to be optimized, and using the information of all variables to establish an initial variable precision configuration file, and using the precision configuration scheme of the current program to establish a variable precision search space file of the current program; (4) Optimizing the variable precision search space file obtained in step (3) using the error analysis result file obtained in step (2) to obtain an updated variable precision search space file; step (4) is specifically as follows: 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, based on 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 search space file obtained in step (3) is reduced to the initial precision of the variable. If it can be, no processing is performed. Then, for each remaining variable in the variable precision to be searched space file obtained in step (3), the above process is repeated until all variables in the variable precision to be searched space file are processed, thereby obtaining an updated variable precision to be searched space file; (5) obtaining multiple variable precision profiles from the updated variable precision search space file obtained in step (4), calculating the performance index of each variable precision profile, and outputting 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) to process the program source code file to be optimized 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, wherein: Step (1) includes the following sub-steps: (1-1) Take the source code of the program to be optimized and insert the relevant header files of the CodiPack function library into the source code of the program to obtain the updated source code of the program; (1-2) converting the types of floating-point variables in the program source code after the primary update obtained in step (1-1) into real numbers to obtain the program source code after the secondary update; (1-3) inserting the function call for the intermediate variable declaration into the second-updated program source code obtained in step (1-2) to obtain a third-updated program source code; (1-4) inserting a function call for error analysis result variable declaration into the three-updated program source code obtained in step (1-3) to obtain a four-updated program source code; (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 the program source code after five updates, that is, the program source code that can be used for static error analysis.

3. The method for compiling an automatic mixed precision optimization program according to claim 1 or 2, wherein: Step (2) includes the following sub-steps: (2-1) Using the LLVM compiler to perform static error analysis on the program source code after the five updates obtained in step (1) to obtain an executable program; (2-2) Run the executable program obtained in step (2-1) to obtain the error analysis result file.

4. The method for compiling an automatic mixed precision optimization program according to claim 3, wherein: 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 obtain 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 for 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, wherein: Step (5) includes the following sub-steps: (5-1) Obtaining the error threshold; (5-2) Set counter cnt1 = 1; (5-3) using the Delta Debugging search algorithm in the time-consuming minimum configuration search algorithm, searching for the cnt1th variable precision profile that is different from the initial variable precision profile 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, 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 a time required for program execution; (5-6) obtaining the performance index of the cnt1th variable precision profile obtained in step (5-3) based on the calculation result of the program corresponding to the cnt1th variable precision profile obtained in step (5-5) and the time required for program execution; (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 larger 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).

6. The method for compiling an automatic mixed precision optimization program according to claim 5, wherein: 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) Traverse the source code of the program to be optimized according to the information of the cnt2th variable read in step (5-4-2) 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).

7. The method for compiling an automatic mixed precision optimization program according to claim 6, wherein: The calculation formula for the performance index in steps (5-6) is: Gain=T*100 R Where 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.

8. The method for compiling an automatic mixed precision optimization program according to claim 7, wherein: The size K of the variable to be searched 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 value of the number of optional precisions of each floating-point variable in the program in the variable to be searched space file.

9. A compilation system for automatic mixed precision optimization programs, characterized in that include: The first module is used to obtain the source code of the program to be optimized and preprocess the source code of the program 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 for static error analysis obtained in the first module to obtain an error analysis result file; The third module is used to 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 a variable precision search space file of the current program; The fourth module is used to optimize the variable precision search space file obtained by the third module using the error analysis result file obtained by the second module to obtain an updated variable precision search space file; specifically, First, obtain the name information of the first variable in the variable precision to be searched space file obtained by the third module, and obtain the variable corresponding to the name information in the error analysis result file obtained by the second module; Then, based on the error analysis result file obtained in the second module, 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 search space file obtained in the third module is reduced to the initial precision of the variable. If it can be, no processing is performed. Then, for each remaining variable in the variable precision to be searched space file obtained by the third module, the above process is repeated until all variables in the variable precision to be searched space file are processed, thereby obtaining an updated variable precision to be searched space file; A fifth module is configured to obtain multiple variable precision profiles from the updated variable precision to-be-searched 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; A sixth module is configured 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 in the sixth module into an executable program file, that is, the optimized program.

Citation Information

Patent Citations

  • Calculation compilation optimization implementation method and system based on error-free transformation

    CN118227138A

  • Systems and methods for performing static analysis on source code

    US7340726B1