GPU-oriented hybrid precision code automatic generation method and system

The method addresses the challenge of determining optimal floating-point precision for GPU scientific computing by using automated parameter optimization and evolutionary algorithms to generate mixed precision code, improving computational efficiency and reducing memory access.

CN119248242AActive Publication Date: 2025-01-03BEIHANG UNIV

Patent Information

Application Number
CN202411445457.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-16
Publication Date
2025-01-03
Estimated Expiration
2044-10-16

AI Technical Summary

Technical Problem

Existing mixed precision frameworks struggle with determining the specific floating-point precision required for different regions and variables in GPU scientific computing programs, leading to inefficiencies in computing and memory access, and lack automated methods for generating mixed precision source code.

Method used

A multi-level, highly automated method for generating mixed precision code for GPUs, involving parameter setting, function signature extraction, measurement, and evolutionary algorithms to optimize precision configurations, allowing for automated code generation without user intervention.

Benefits of technology

The method effectively determines optimal precision configurations for GPU programs, reducing computational intensity and memory access while generating accurate mixed precision source code efficiently.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119248242A_ABST
    Figure CN119248242A_ABST
Patent Text Reader

Abstract

The invention provides a GPU-oriented hybrid precision code automatic generation method and system, and belongs to the field of GPU hybrid precision computation.The method comprises the steps that S1, an input program and search parameters are set; s2, required parameters are extracted from source codes, and mixing precision parameters are set; s3, screening functions meeting conditions; s4, setting a current group and a historical optimal group; s5, respectively selecting one mixing precision configuration of the current group and the historical optimal group to construct a new mixing precision configuration, adding the new mixing precision configuration into the generated group, and repeating S5 until a set number of individuals exist in the generated group; s6, screening the generated group to obtain a next generation group, and updating a historical optimal group; if the result of the step S6 does not change the current group, turning to the step S7, otherwise, turning to the step S5; and S7, selecting the mixing precision configuration with the maximum performance data in the historical optimal group, and generating a mixing precision code. According to the method and the device, full-process highly-automatic mixed-precision source code generation is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of GPU mixed precision computing, and in particular relates to a mixed precision code automatic generation method and system for GPU. Background Art

[0002] Different regions and variables in GPU scientific computing programs have different requirements for floating-point precision. By using mixed-precision computing, a variety of precision formats are mixed in the program, and the different precision sensitivities of different data are fully utilized to assign corresponding floating-point formats to the data, and convert some computing instructions into low-precision computing instructions, it can effectively reduce the intensity of computing and memory access and improve the computing speed. However, how to determine the specific floating-point precision required for different regions and variables in the program, and how to determine the performance improvement effect brought by different mixed-precision methods are difficult and time-consuming. Therefore, how to automatically determine the floating-point precision used by different variables and regions in scientific computing programs under the constraints of error conditions has become a problem that needs to be solved urgently. Summary of the invention

[0003] In order to solve the above technical problems, the present invention provides a mixed-precision code automatic generation method for GPU, comprising the following steps: Step S1: Set the location of the source code file to be optimized, set the high-precision type and the low-precision type, set the maximum number of search iterations to m, set the error limit to Eb, set the maximum number of functions to Nf, set the number of search individuals to N0, set the number of search generation individuals to Ng, set the number of search measurement individuals to Nm, and set the mutation probability to U0; Step S2: extracting function signatures, variable names, and function call points from the source code, wherein the variable name refers to the name of a variable of a floating-point number or a floating-point array type in the function, and the function call point refers to a statement in the function body that calls other functions; setting multiple mixed-precision parameters, and all the obtained mixed-precision parameters constitute a mixed-precision space corresponding to the source code, and a group of specific values ​​of all mixed-precision parameters in the mixed-precision space is called a mixed-precision configuration; Step S3: traverse all the function signatures one by one, execute the measurement module for each traversed function signature, and select and retain at most Nf functions according to the measurement results; Step S4: setting the current group to the N0 mixed precision configurations generated by initialization, and setting the historical optimal group to the N0 mixed precision configurations generated by initialization; Step S5: Select a mixed precision configuration C0 in the current population, select a mixed precision configuration C1 in the historical optimal population, and construct a new mixed precision configuration C2, wherein for each mixed precision parameter P in the mixed precision space, set the value of P in C2 according to the following probability distribution: there is a probability of U0 being a random value, a probability of (1-U0) / 2 being the value of P in C0, and a probability of (1-U0) / 2 being the value of P in C1; add C2 to the generated population, and repeat step S5 until there are Ng mixed precision configurations in the generated population; Step S6: Execute a prediction process on the generated population to select and retain Nm mixed precision configurations, execute a measurement process on the Nm mixed precision configurations to select and retain N0 mixed precision configurations, and use a data management module to update the current population to the N0 mixed precision configurations; merge the historical optimal population with the current population and sort them according to the corresponding performance data, and update the historical optimal population to at most N0 mixed precision configurations with the largest performance data after the sorting; if the result of step S6 does not change the current population or the number of times the measurement module is started i satisfies i>m, jump to step S7, otherwise jump to step S5; Step S7: Select the mixed precision configuration with the largest performance data in the historical optimal group, and use the code generation module to generate the corresponding mixed precision code.

[0004] Beneficial effects: 1. Existing mixed precision frameworks mainly rely on variable-based error analysis. For example, SATIRE provides strict error bounds for floating-point calculations. FPdetect uses affine calculations to estimate rounding errors, thereby determining the number of digits of precision required within the error range given by the user. Herbgrind attempts to find the causal relationship that causes the error, Adapt gives floating-point precision sensitivity and precision recommendations, and S3FP identifies unstable data in the input data. However, the above mixed precision frameworks based on variable error analysis cannot achieve good results on GPU scientific computing programs for the following reasons: (1) Error estimation is performed through differentiation. The time and space overhead of the differentiation process on scientific computing programs with many variables and high iteration times is huge. The differentiation process of GPU scientific computing programs is usually unable to be completed due to insufficient video memory; (2) Variable-based mixed precision ignores the multi-level mixed precision optimization methods in the program, cannot use coarse-grained analysis at the function level to reduce the complexity of the search process, and cannot use the different precision requirements of different data regions to achieve a better mixed precision strategy. GRAM is a GPU-based block mixed precision framework, however, GRAM completely ignores the mixed precision opportunities at the variable level and does not accurately analyze the location of the mixed precision region. The present invention discloses a multi-level, highly automated GPU scientific computing program mixed precision calculation method to solve the above problems.

[0005] 2. Most existing mixed precision frameworks require users to modify source code and can only output mixed precision suggestions or intermediate results. The present invention achieves highly automated mixed precision source code generation throughout the entire process through automated analysis, search, and generation. BRIEF DESCRIPTION OF THE DRAWINGS

[0006] Figure 1 It is a flowchart of a method for automatically generating mixed-precision code for a GPU in an embodiment of the present invention; Figure 2 Schematic diagram of the pruning process in an embodiment of the present invention; Figure 3 It is a structural diagram of the MLP model in an embodiment of the present invention; Figure 4 A schematic diagram of a mixed precision configuration flow chart in an embodiment of the present invention; Figure 5 A schematic diagram of a process for generating mixed precision code in an embodiment of the present invention; Figure 6 This is a structural block diagram of a mixed-precision code automatic generation system for a GPU in an embodiment of the present invention. DETAILED DESCRIPTION

[0007] 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.

[0008] Embodiment 1 like Figure 1 As shown, an embodiment of the present invention provides a method for automatically generating mixed-precision code for a GPU, comprising the following steps: Step S1: Set the location of the source code file to be optimized, set the high-precision type and the low-precision type, set the maximum number of search iterations to m, set the error limit to Eb, set the maximum number of functions to Nf, set the number of search individuals to N0, set the number of search generation individuals to Ng, set the number of search measurement individuals to Nm, and set the mutation probability to U0; Step S2: extracting function signatures, variable names, and function call points from the source code, where the variable name refers to the name of a variable of type floating point or floating point array in the function, and the function call point refers to a statement in the function body that calls other functions; setting multiple mixed precision parameters, and all the obtained mixed precision parameters constitute a mixed precision space corresponding to the source code, and a group of specific values ​​of all mixed precision parameters in the mixed precision space is called a mixed precision configuration; Step S3: traverse all function signatures one by one, execute the measurement module for each traversed function signature, and select and retain at most Nf functions according to the measurement results; Step S4: Set the current group to the N0 mixed precision configurations generated by initialization, and set the historical optimal group to the N0 mixed precision configurations generated by initialization; Step S5: Select a mixed precision configuration C0 in the current population, select a mixed precision configuration C1 in the historical optimal population, and construct a new mixed precision configuration C2, where for each mixed precision parameter P in the mixed precision space, set the value of P in C2 according to the following probability distribution: there is a probability of U0 being a random value, a probability of (1-U0) / 2 being the value of P in C0, and a probability of (1-U0) / 2 being the value of P in C1; add C2 to the generated population, and repeat step S5 until there are Ng mixed precision configurations in the generated population; Step S6: Perform a prediction process on the generated population to select and retain Nm mixed precision configurations, perform a measurement process on the Nm mixed precision configurations to select and retain N0 mixed precision configurations, and use the data management module to update the current population to N0 mixed precision configurations; merge the historical optimal population with the current population and sort them according to the corresponding performance data, and update the historical optimal population to at most N0 mixed precision configurations with the largest performance data after sorting; if the result of step S6 does not change the current population or the number of measurement module startups i satisfies i>m, jump to step S7, otherwise jump to step S5; Step S7: Select the mixed precision configuration with the largest performance data in the historical optimal group, and use the code generation module to generate the corresponding mixed precision code.

[0009] In one embodiment, the above step S1: setting the location of the source code file of the mixed precision optimization program, setting the high precision type and the low precision type, setting the maximum number of search iterations to m times, setting the error limit to Eb, setting the maximum number of functions to Nf, setting the number of search individuals to N0, setting the number of search generation individuals to Ng, setting the number of search measurement individuals to Nm, and setting the mutation probability to U0, specifically includes: Set the high-precision type to double and the low-precision type to float. Set the maximum number of search iterations to m, set the error limit to Eb, set the maximum number of functions to Nf, set the number of search individuals to N0, set the number of search generated individuals to Ng, set the number of search measured individuals to Nm, and set the mutation probability to U0.

[0010] In one embodiment, the above step S2: extracting function signatures, variable names, and function call points from the source code, where the variable name refers to the name of a variable of type floating point or floating point array in the function, and the function call point refers to a statement in the function body that calls other functions; setting multiple mixed precision parameters, and all the mixed precision parameters obtained constitute a mixed precision space corresponding to the source code, and a group of specific values ​​of all mixed precision parameters in the mixed precision space is called a mixed precision configuration, which specifically includes: Based on AST traversal, scan all function definitions in the source code file. For each function, extract its function name, parameter list, return type and other information to form a function signature. In each function body, detect and extract variable names of type floating point numbers (such as float, double) or floating point arrays. These variable names are recorded in a variable list for subsequent analysis. Identify all function call statements in the function body. Record the function names and actual parameter lists of these calls as function call points. Set a low-precision area parameter for each function signature; set a mixed-precision area parameter for each function signature; set a variable precision parameter for each variable name; set a function call point precision parameter for each function call point; record all mixed-precision parameters as mixed-precision spaces. The embodiments of the present invention include the following four mixed-precision spaces: Parameter type P1: low-precision area parameter, which has a one-to-one mapping relationship with the function signature; Parameter type P2: mixed precision area parameter, which has a one-to-one mapping relationship with the function signature; Parameter type P3: variable precision parameter, which has a one-to-one mapping relationship with the variable name; Parameter type P4: function call point precision parameter, the function call point precision parameter and the function call point form a one-to-one mapping relationship; Set the initial values ​​of all mixed precision parameters to the minimum values ​​to form the initial mixed precision configuration. For example, suppose after analyzing a program, the function signatures obtained include Func1(double), Func2(double , double , double , double), Func3(double , double ), and Func1(double) is recorded as Func1, Func2(double ,double , double , double) is recorded as Func2, Func3 (double , double ) is recorded as Func3; Func1 is a device function, Func2 and Func3 are global functions; the variable names in Func1 include A1, A2, A3, K, B; the variable names in Func2 include V1, V2, V3, V4, V5, V6, V7, V8, V9, and the variable name in Func3 is empty; the function call points in Func2 include Func1(V3), Func1(V4), and the function call points in Func3 include Func1(a[t]-b[t]). In the mixed precision space corresponding to the program, the low-precision area parameters include Func2_Low and Func3_Low, with a value range of 0 to 100, indicating the percentage of the low-precision area; the mixed precision area parameters include Func2_Mix and Func3_Mix, with a value range of 0 to 100, indicating the percentage of the mixed precision area parameters; the variable precision parameters include Func1_A1, Func1_A2, Func1_A3, Func1_K, Func1_B, Func2_V1, Func2_V2, Func2 _V3, Func2_V4, Func2_V5, Func2_V6, Func2_V7, Func2_V8, Func2_V9, the value is 0 or 1, 0 means the corresponding variable uses double type, 1 means the corresponding variable uses float type, the function call point precision parameters include Func2_Func1_1, Func2_Func1_2, Func3_Func1_1, the value is 0 or 1, 0 means the corresponding function call point is not modified, 1 means the corresponding function call point is changed to call the corresponding low-precision version function.

[0011] In one embodiment, the above step S3: traversing all function signatures one by one, executing the measurement module for each traversed function signature, and selecting and retaining at most Nf functions according to the measurement results, specifically includes: Step S31: traverse all function signatures one by one, set the low-precision area parameters mapped to the function signatures to the highest values, and keep all other parameters at the lowest values, start the code generation module to generate corresponding mixed-precision codes according to the parameter values, use the measurement module to compile and execute the mixed-precision codes, and obtain error data and performance data; For example, set Func2_Low to 100, Func2_Mix to 0, and all other parameters to 0; then the code generates a low-precision version of the module constructor Func2, Func2_VersionFloat, in which all floating-point variable types are changed to float, and the corresponding functions are modified to call the low-precision version. Then compile and execute the generated code together with the test part of the program to obtain error data and performance data. For example, the error indicator uses the L1 norm, with a value of 7.9e-4, and the performance indicator uses the program execution time, with a value of 102ms; Step S32: sort the function signatures according to the size of the performance data, select at most Nf functions with the highest performance and set them to be retained, and set the remaining function signatures to be removed; For example, assuming that Nf is 1, the function with the best performance should be retained. Step S31 shows that the error data of Func2 is 7.9e-4 and the performance data is 102ms; the error data of Func3 is 3.2e-4 and the performance data is 198ms. Since the execution time of 102ms is better than 198ms, Func2 is retained. Step S33: traverse all function signatures one by one again, and for the function signatures set to be removed in step S32, set the low-precision area parameter mapped to the function signature to a fixed value of 0, and set the corresponding mixed-precision area parameter to a fixed value of 0; For example, assuming that it is determined in step S32 that Func2 is retained and Func3 is removed, then Func3_Low, Func3_Mix and all Func3 related parameters are set to 0 and are all set to frozen.

[0012] Figure 2 A schematic diagram of the pruning process is shown.

[0013] In one embodiment, the above step S4: setting the current group as the N0 mixed precision configurations generated by initialization, and setting the historical optimal group as the N0 mixed precision configurations generated by initialization, specifically includes: Initialize the search module, randomly generate N0 mixed precision configurations and add them to the current population, and make the historical optimal population equal to the current population. For example, assuming that the mixed precision space after ignoring the frozen parameters is [Func2_Low, Func2_Mix, Func1_A1, Func1_A2, Func1_A3, Func1_K, Func1_B, Func2_V1, Func2_V2, Func2_V3, Func2_V4, Func2_V5, Func2_V6, Func2_V7, Func2_V8, Func2_V9, Func2_Func1_1, Func2_Func1_2], there are 18 parameters in total, assuming that N0 is 20, then randomly generate 18 parameter values ​​to form a mixed precision configuration, repeat 20 times to construct 20 mixed precision configurations, and add them to the current population and the historical optimal population.

[0014] In one embodiment, the above step S5: select a mixed precision configuration C0 in the current population, select a mixed precision configuration C1 in the historical optimal population, and construct a new mixed precision configuration C2, wherein for each mixed precision parameter P in the mixed precision space, the value of P in C2 is set according to the following probability distribution: there is a probability of U0 being a random value, a probability of (1-U0) / 2 being the value of P in C0, and a probability of (1-U0) / 2 being the value of P in C1; C2 is added to the generated population, and step S5 is repeated until there are Ng mixed precision configurations in the generated population, specifically including: Assume that a set of mixed precision configurations C0 is [0, 80, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0,1, 0, 1, 0] and C1 is [20, 30, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0], and U0 is 2%. Taking the first parameter Func2_Low in C2 as an example, Func2_Low has a 2% probability of taking a random value, a 49% probability of taking the value of C0, which is 0, and a 49% probability of taking the value of C1, which is 20. For example, a possible C2 generation result is [20, 10, 1, 0, 0, 0, 1,0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1], where the first parameter takes the value of C1, i.e. 20, the second parameter takes the random value 10, and the third parameter takes the value of C0, 1. Repeat this generation process until Ng mixed precision configurations are obtained. If Ng is taken as 1000, a total of 1000 mixed precision configurations are generated.

[0015] In one embodiment, the above step S6: perform screening on the generated population, screen and retain Nm mixed precision configurations, perform a measurement process on the Nm mixed precision configurations to screen and retain N0 mixed precision configurations, and use the data management module to update the current population to N0 mixed precision configurations; merge the historical optimal population with the current population and sort them according to the corresponding performance data, and update the historical optimal population to at most N0 mixed precision configurations with the largest performance data after sorting; if the result of step S6 does not change the current population or the number of measurement module startups i satisfies i>m, jump to step S7, otherwise jump to step S5, which specifically includes: Step S61: Execute prediction to obtain an estimated value Ta of performance data of each mixed precision configuration in the generated population, and an estimated value Ea of error data; The prediction process is as follows Figure 3 As shown, a 3-layer MLP model is constructed. Its input is the parameter value of the normalized mixed precision configuration. The MLP model performs the forward process, inversely normalizes the predicted value output by the model, and outputs the performance data estimation value and the error data estimation value. For example, the prediction result for a certain configuration is Ta=149, Ea=4.3e-4. Step S62: Remove from the generated population those that satisfy Ea>1.2 All mixed precision configurations of Eb are sorted according to the size of Ta, and at most Nm mixed precision configurations with the largest Ta are retained; Assuming Eb is 5e-4, Nm is 4, the prediction results Ea of mixed precision configurations C0, C1, C2, C3, C4, C5, C6, and C7 are [4.2e-4, 3.8e-4, 4.5e-4, 6.5e-4, 5.1e-4, 4.9e-4, 7.2e-4, 2.8e-4], and Ta are [102, 113, 143, 89, 182, 131, 144, 122], respectively. According to Ea>1.2 Eb removes C3 and C6, and sorts them by Ta size into C0, C1, C5, C7, C2, and C6, retaining the four mixed precision configurations with the largest Ta, namely C0, C1, C5, and C7; Step S63: for Nm mixed precision configurations, query the data management module, if the performance data and error data exist, read the performance data Tx and error data Ex of the mixed precision configuration, otherwise start the code generation module and the measurement module, measure and obtain the performance data Tx and error data Ex; Assuming that the mixed precision configuration C0 has been measured in the previous search process, the data management module can read the performance data and error data of C0 and directly use the results of the data management module. Assuming that the mixed precision configuration C1 has never appeared, the data management module returns a failure, the code generation module is started to generate the code corresponding to C1, and the measurement module is started to compile and measure to obtain the performance data and error data; Step S64: storing the performance data Tx and error data Ex of the Nm mixed precision configurations into the data management module to update the data, and performing fitting to correct the prediction accuracy; For example, in step S63, C1 is the performance data and error data obtained by the code generation module and the measurement module, and the performance data and error data corresponding to C1 need to be saved in the data management module. Using the real measured data as the training label, the MLP model performs the forward and reverse process on Nm mixed precision configurations to correct the accuracy.

[0016] Step S65: Remove all mixed precision configurations with Ex>Eb from the Nm mixed precision configurations, sort them according to Tx size, and retain at most N0 mixed precision configurations with the largest Tx.

[0017] For example, assume that step S63 shows that the Ex of mixed precision configurations C0, C1, C5, and C7 are [4.1e-4, 3.4e-4, 4.8e-4, 5.1e-4], and Tx are [113, 98, 102, 94], respectively. C7 is removed according to Ex>Eb, and the order is C1, C5, and C0 according to Tx size. Assuming N0 is 2, C1 and C5 are retained.

[0018] like Figure 4 A flowchart showing the mixed precision configuration.

[0019] In one embodiment, the above step S7: selecting the mixed precision configuration with the largest performance data in the historical optimal group, and using the code generation module to generate the corresponding mixed precision code, specifically includes: Traverse the above historical optimal population to obtain the mixed precision configuration with the highest performance. According to the parameter values ​​in the mixed precision configuration, use the code generation module to generate different versions of the function, modify the function call to generate mixed precision code, and output the result. The code generation process is as follows: Figure 5 As shown, low-precision functions, variables, or function calls are represented in gray.

[0020] Embodiment 2 like Figure 6 As shown, an embodiment of the present invention provides a mixed precision code automatic generation system for a GPU, comprising the following modules: Parameter setting module 81, used to set the location of the source code file to be optimized, set the high-precision type and the low-precision type, set the maximum number of search iterations to m times, set the error limit to Eb, set the maximum number of functions to Nf, set the number of search individuals to N0, set the number of search generation individuals to Ng, set the number of search measurement individuals to Nm, and set the mutation probability to U0; A program analysis module 82 is used to extract function signatures, variable names, and function call points from the source code, where the variable name refers to the name of a variable of a floating-point number or a floating-point array type in the function, and the function call point refers to a statement in the function body that calls other functions; multiple mixed-precision parameters are set, and all the mixed-precision parameters obtained constitute a mixed-precision space corresponding to the source code, and a group of specific values ​​of all mixed-precision parameters in the mixed-precision space is called a mixed-precision configuration; The pruning module 83 is used to traverse all function signatures one by one, execute the measurement module for each traversed function signature, and select and retain at most Nf functions according to the measurement results; wherein the measurement module is used to compile the mixed precision code into an executable file, execute the executable file and parse the output content to obtain performance data and error data; An initialization search module 84 is used to set the current group to be the N0 mixed precision configurations generated by initialization, and to set the historical optimal group to be the N0 mixed precision configurations generated by initialization; A search module 85 is used to select a mixed precision configuration C0 in the current population, select a mixed precision configuration C1 in the historical optimal population, and construct a new mixed precision configuration C2, wherein for each mixed precision parameter P in the mixed precision space, the value of P in C2 is set according to the following probability distribution: there is a probability of U0 being a random value, a probability of (1-U0) / 2 being the value of P in C0, and a probability of (1-U0) / 2 being the value of P in C1; C2 is added to the generated population, and the search module is repeatedly executed until there are Ng mixed precision configurations in the generated population; A screening module 86 is used to perform a prediction process on the generated group to screen and retain Nm mixed precision configurations, perform a measurement process on the Nm mixed precision configurations to screen and retain N0 mixed precision configurations, and use the data management module to update the current group to N0 mixed precision configurations; merge the historical optimal group with the current group and sort them according to the corresponding performance data, and update the historical optimal group to at most N0 mixed precision configurations with the largest performance data after sorting; if the result of the screening module does not change the current group or the number of measurement module startups i satisfies i>m, jump to the code generation module, otherwise jump to the search module; the data management module is used to persistently store the results of the code generation module and the measurement module, and for the mixed precision configurations that have obtained performance data and error data through the code generation module and the measurement module, the corresponding performance data and error data can be read any number of times without using the code generation module and the measurement module; The code generation module 87 is used to select the mixed precision configuration with the largest performance data in the historical optimal group, and use the code generation module to generate the corresponding mixed precision code.

[0021] The above embodiments are provided only for the purpose of describing the present invention, and are not intended to limit the scope of the present invention. The scope of the present invention is defined by the appended claims. Various equivalent substitutions and modifications made without departing from the spirit and principles of the present invention should all be included within the scope of the present invention.

Claims

1. A mixed-precision code automatic generation method for GPU, characterized in that: include: Step S1: Set the location of the source code file to be optimized, set the high-precision type and the low-precision type, set the maximum number of search iterations to m, set the error limit to Eb, set the maximum number of functions to Nf, set the number of search individuals to N0, set the number of search generation individuals to Ng, set the number of search measurement individuals to Nm, and set the mutation probability to U0; Step S2: extracting function signatures, variable names, and function call points from the source code, wherein the variable name refers to the name of a variable of a floating-point number or a floating-point array type in the function, and the function call point refers to a statement in the function body that calls other functions; setting multiple mixed-precision parameters, and all the obtained mixed-precision parameters constitute a mixed-precision space corresponding to the source code, and a group of specific values ​​of all mixed-precision parameters in the mixed-precision space is called a mixed-precision configuration; Step S3: traverse all the function signatures one by one, execute the measurement module for each traversed function signature, and select and retain at most Nf functions according to the measurement results; Step S4: setting the current group to the N0 mixed precision configurations generated by initialization, and setting the historical optimal group to the N0 mixed precision configurations generated by initialization; Step S5: Select a mixed precision configuration C0 in the current population, select a mixed precision configuration C1 in the historical optimal population, and construct a new mixed precision configuration C2, wherein for each mixed precision parameter P in the mixed precision space, set the value of P in C2 according to the following probability distribution: there is a probability of U0 being a random value, a probability of (1-U0) / 2 being the value of P in C0, and a probability of (1-U0) / 2 being the value of P in C1; add C2 to the generated population, and repeat step S5 until there are Ng mixed precision configurations in the generated population; Step S6: Execute a prediction process on the generated population to select and retain Nm mixed precision configurations, execute a measurement process on the Nm mixed precision configurations to select and retain N0 mixed precision configurations, and use a data management module to update the current population to the N0 mixed precision configurations; merge the historical optimal population with the current population and sort them according to the corresponding performance data, and update the historical optimal population to at most N0 mixed precision configurations with the largest performance data after the sorting; if the result of step S6 does not change the current population or the number of times the measurement module is started i satisfies i>m, jump to step S7, otherwise jump to step S5; Step S7: Select the mixed precision configuration with the largest performance data in the historical optimal group, and use the code generation module to generate the corresponding mixed precision code.

2. The method for automatic generation of mixed-precision code for GPU according to claim 1, characterized in that: The mixed precision space in step S2 specifically includes: Parameter type P1: low-precision area parameter, the low-precision area parameter and the function signature form a one-to-one mapping relationship; Parameter type P2: mixed precision area parameter, the mixed precision area parameter and the function signature form a one-to-one mapping relationship; Parameter type P3: variable precision parameter, which forms a one-to-one mapping relationship with the variable name; Parameter type P4: function call point precision parameter, the function call point precision parameter and the function call point form a one-to-one mapping relationship.

3. The method for automatic generation of mixed-precision code for GPU according to claim 2, characterized in that: The step S3: traversing all the function signatures one by one, executing the measurement module for each traversed function signature, and selecting and retaining at most Nf functions according to the measurement results, specifically includes: Step S31: traverse all the function signatures one by one, set the low-precision area parameter mapped to the function signature to the highest value, and keep all other parameters at the lowest value, start the code generation module to generate corresponding mixed-precision code according to the parameter value, use the measurement module to compile and execute the mixed-precision code, and obtain error data and performance data; Step S32: sorting the function signatures according to the size of the performance data, selecting at most Nf functions with the highest performance to be retained, and setting the remaining function signatures to be removed; Step S33: traverse all function signatures one by one again, and for the function signature set to be removed in the step S32, set the low-precision area parameter mapped to the function signature to a fixed value of 0, and set the corresponding mixed-precision area parameter to a fixed value of 0.

4. The method for automatic generation of mixed-precision code for GPU according to claim 3, characterized in that: The step S6: performing a prediction process on the generated population to filter and retain Nm mixed precision configurations, performing a measurement process on the Nm mixed precision configurations to filter and retain N0 mixed precision configurations, and using a data management module to update the current population to the N0 mixed precision configurations; merging the historical optimal population with the current population and sorting them according to the corresponding performance data, and updating the historical optimal population to at most N0 mixed precision configurations with the largest performance data after the sorting; if the result of step S6 does not change the current population or the number of times the measurement module is started i satisfies i>m, then jump to step S7, otherwise jump to step S5, specifically including: Step S61: Execute prediction to obtain an estimated value Ta of performance data and an estimated value Ea of error data of each mixed precision configuration in the generation population; Step S62: Remove from the generated population those that satisfy Ea>1.2 All mixed precision configurations of Eb are sorted according to the size of Ta, and at most Nm mixed precision configurations with the largest Ta are retained; Step S63: query the data management module for the Nm mixed precision configurations, and if the performance data and error data exist, read the performance data Tx and error data Ex of the mixed precision configuration; otherwise, start the code generation module and the measurement module to measure and obtain the performance data Tx and error data Ex; Step S64: storing the performance data Tx and error data Ex of the Nm mixed precision configurations into the data management module to update the data, and performing fitting to correct the prediction accuracy; Step S65: remove all mixed precision configurations with Ex>Eb from the Nm mixed precision configurations, sort them according to Tx size, and retain at most N0 mixed precision configurations with the largest Tx.

5. A mixed-precision code automatic generation system for GPU, characterized in that: Includes the following modules: The parameter setting module is used to set the location of the source code file to be optimized, set the high-precision type and the low-precision type, set the maximum number of search iterations to m, set the error limit to Eb, set the maximum number of functions to Nf, set the number of search individuals to N0, set the number of search generation individuals to Ng, set the number of search measurement individuals to Nm, and set the mutation probability to U0; A program analysis module is used to extract function signatures, variable names, and function call points from the source code, wherein the variable name refers to the name of a variable of a floating-point number or a floating-point array type in a function, and the function call point refers to a statement in a function body that calls other functions; multiple mixed-precision parameters are set, and all the mixed-precision parameters obtained constitute a mixed-precision space corresponding to the source code, and a group of specific values ​​of all the mixed-precision parameters in the mixed-precision space is called a mixed-precision configuration; A pruning module is used to traverse all the function signatures one by one, execute the measurement module on each traversed function signature, and select and retain at most Nf functions according to the measurement results; An initialization search module is used to set the current group to be the N0 mixed precision configurations generated by initialization, and to set the historical optimal group to be the N0 mixed precision configurations generated by initialization; A search module is used to select a mixed precision configuration C0 in the current population, select a mixed precision configuration C1 in the historical optimal population, and construct a new mixed precision configuration C2, wherein for each mixed precision parameter P in the mixed precision space, the value of P in C2 is set according to the following probability distribution: there is a probability of U0 being a random value, a probability of (1-U0) / 2 being the value of P in C0, and a probability of (1-U0) / 2 being the value of P in C1; C2 is added to the generated population, and the search module is repeatedly executed until there are Ng mixed precision configurations in the generated population; a screening module, configured to perform a prediction process on the generated population to screen and retain Nm mixed precision configurations, perform a measurement process on the Nm mixed precision configurations to screen and retain N0 mixed precision configurations, and use a data management module to update the current population to the N0 mixed precision configurations; merge the historical optimal population with the current population and sort them according to the corresponding performance data, and update the historical optimal population to at most N0 mixed precision configurations with the largest performance data after the sorting; if the result of the screening module does not change the current population or the number of startup times i of the measurement module satisfies i>m, jump to the code generation module, otherwise jump to the search module; A code generation module is used to select a mixed precision configuration with the largest performance data in the historical optimal group, and use the code generation module to generate a corresponding mixed precision code.

6. The mixed-precision code automatic generation system for GPU according to claim 5, characterized in that: The measurement module in the pruning module is used to compile the mixed precision code into an executable file, execute the executable file and parse the output content to obtain performance data and error data.

7. The mixed-precision code automatic generation system for GPU according to claim 5, characterized in that: The data management module in the screening module is used to persistently store the results of the code generation module and the measurement module. For the mixed precision configuration in which the performance data and error data have been obtained through the code generation module and the measurement module, the corresponding performance data and error data can be read any number of times without using the code generation module and the measurement module.

Citation Information

Patent Citations

  • SVM classifier parameter optimization method based on improved particle swarm algorithm

    CN108875788A

  • Loop nested level mixed precision calculation code automatic generation method for floating point calculation application program

    CN116755710A

  • Optimization of the execution time of a computer program by determining the implementation of a function according to range of input parameters and accuracy

    US20190163508A1

Cited By

  • Method and system for repeated analysis of customer group diagnosis

    CN121660721A