Code compiling method and device
By evaluating the conceptual drift of the cost model and updating the model accordingly, the problem of low prediction accuracy in the prior art cost model in hardware scenarios is solved, and a more efficient code compilation process and performance is achieved.
Patent Information
- Application Number
- CN202311362186.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-19
- Publication Date
- 2025-04-22
AI Technical Summary
In the prior art, the cost model has incomplete coverage of hardware scenarios, resulting in low prediction accuracy, affecting the code compilation effect.
By obtaining the target code, determine the conceptual drift of the cost model. If the model can accurately predict, predict based on the model and select the optimal executable code; if it cannot accurately predict, run the candidate executable code on the target hardware and update the cost model to improve prediction accuracy.
It improves the accuracy of the cost model's running performance prediction of the target code, shortens the time-consuming process of code compilation, and improves compilation efficiency and performance.
Smart Images

Figure CN119861933A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a code compilation method and device. Background Art
[0002] Code compilation refers to the process of translating source code written in a high-level language into executable code that can run on hardware. During the code compilation process, a cost model is needed to predict the running performance of the candidate executable code obtained by compiling the source code on the hardware.
[0003] At present, the cost model is obtained by offline training based on historical operation data. During the code compilation process, the running performance of the candidate executable code corresponding to the source code to be compiled on the hardware is predicted directly based on the offline trained cost model. Historical operation data is also called historical compilation data, which includes historical code and the running performance data of the historical code running on the hardware, such as the running time.
[0004] However, the offline training cost model requires a large amount of rich historical operation data that can cover various hardware scenarios as training data. It is difficult to obtain these historical operation data. The historical operation data actually obtained is difficult to cover various hardware scenarios, resulting in the offline training cost model having low prediction accuracy in some hardware scenarios, affecting the code compilation effect. Summary of the invention
[0005] The present application provides a code compilation method and device, which can ensure the code compilation effect. The technical solution of the present application is as follows.
[0006] In a first aspect, a code compilation method is provided, the method comprising: obtaining a target code to be compiled; determining the concept drift degree of a cost model according to the target code, the cost model is obtained by training based on historical operation data and is used to predict the operation performance of multiple candidate executable codes corresponding to the target code, the historical operation data is the operation data of the historical code that can be run on the hardware that runs the executable code corresponding to the target code; determining whether the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code according to the concept drift degree, the multiple candidate executable codes corresponding to the target code are obtained by compiling the target code; in the case where it is determined that the concept drift degree indicates that the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code, predicting the operation performance of multiple candidate executable codes corresponding to the target code based on the cost model, and determining the optimal executable code for the target code from the multiple candidate executable codes based on the prediction result. The optimal executable code is the final compilation result obtained by compiling the target code, that is, the final compiled code of the target code.
[0007] The technical solution provided by the present application determines the degree of concept drift of the cost model obtained by pre-training based on historical operation data according to the target code to be compiled, and predicts the operation performance of multiple candidate executable codes corresponding to the target code based on the cost model when it is determined that the degree of concept drift of the cost model indicates that the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code. Therefore, the cost model has a high accuracy in predicting the operation performance of multiple candidate executable codes corresponding to the target code, which helps to ensure the code compilation effect. In addition, when it is determined that the degree of concept drift of the cost model indicates that the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code, it is not necessary to train the cost model during the compilation process of the target code, so the time consumption of the code compilation process can be shortened, the code compilation efficiency can be improved, and the compilation performance of the target code can be improved.
[0008] Optionally, the method also includes: when it is determined that the degree of concept drift indicates that the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code, running at least one candidate executable code corresponding to the target code on the target hardware, and updating the cost model based on the running data of the target code, the running data of the target code being the running data of the at least one candidate executable code running on the target hardware; predicting the running performance of multiple candidate executable codes corresponding to the target code based on the updated cost model, and determining the optimal executable code for the target code from the multiple candidate executable codes based on the prediction results.
[0009] The technical solution provided by the present application obtains the degree of concept drift of a cost model pre-trained based on historical running data according to a target code to be compiled, and updates the cost model based on running data of at least one candidate executable code corresponding to the target code on target hardware when it is determined that the degree of concept drift of the cost model indicates that the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code, and predicts the running performance of multiple candidate executable codes corresponding to the target code based on the updated cost model. Since the cost model can be updated based on the running data of at least one candidate executable code corresponding to the target code on target hardware, the cost model can learn the code features related to the target code and the hardware features of the target hardware. Therefore, the accuracy of the running performance prediction of the multiple candidate executable codes corresponding to the target code based on the updated cost model is higher, which is helpful for the code compilation effect and improves the compilation performance of the target code.
[0010] Optionally, updating the cost model based on the running data of the target code includes: using the running data of the target code and the running data sampled from the historical running data as training data for the cost model. Further, updating the cost model based on the training data, that is, using the running data of the target code currently to be compiled and the running data sampled from the historical running data used to train the cost model together as training data to update the cost model.
[0011] The technical solution provided by the present application uses the running data of the target code that currently needs to be compiled and the running data sampled from the historical running data used by the training cost model as training data to update the cost model. On the one hand, it can enable the cost model to learn the code features related to the target code and the hardware features related to the target code (such as the hardware features of the target hardware), which helps to improve the accuracy and prediction precision of the cost model in predicting the running performance of the candidate executable code corresponding to the target code, and improve the accuracy of compiling the target code; on the other hand, it can prevent the cost model from forgetting the code features related to the historical code and the hardware features related to the historical code that have been learned, which also helps to improve the accuracy and prediction precision of the cost model in predicting the running performance of the candidate executable code corresponding to the target code (the code features and hardware features learned by the cost model are relatively rich), and improve the accuracy of compiling the target code.
[0012] Optionally, the historical running data includes historical code and running performance data of the historical code, the running performance data of the historical code is used to characterize the running performance of the historical code; the cost model is updated based on the running data of the target code and the running data sampled from the historical running data, including: obtaining at least one candidate executable code corresponding to the target code; obtaining measured performance data of each candidate executable code in the at least one candidate executable code, the measured performance data of each candidate executable code is the actually detected running performance data of each candidate executable code running on the target hardware; the cost model is updated based on the historical code, the running performance data of the historical code, the at least one candidate executable code and the measured performance data of the at least one candidate executable code.
[0013] Optionally, obtaining at least one candidate executable code corresponding to the target code includes: obtaining multiple candidate executable codes corresponding to the target code; obtaining predicted performance data of each candidate executable code in the multiple candidate executable codes based on the cost model, the predicted performance data of each candidate executable code being obtained by predicting the running performance of each candidate executable code by the cost model; and determining the at least one candidate executable code in the multiple candidate executable codes based on the predicted performance data of the multiple candidate executable codes.
[0014] The technical solution provided by the present application, after obtaining multiple candidate executable codes corresponding to the target code, determines at least one candidate executable code from the multiple candidate executable codes according to the predicted performance data of the multiple candidate executable codes obtained by predicting the cost model, and obtains the measured performance data of the at least one candidate executable code, and updates the cost model based on the historical code, the running performance data of the historical code, the at least one candidate executable code and the measured performance data of the at least one candidate executable code. Therefore, on the one hand, the time spent on updating the cost model can be shortened, and on the other hand, the reliability of the at least one candidate executable code used to update the cost model can be guaranteed, and the updating effect of the cost model can be guaranteed.
[0015] Optionally, determining whether the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code based on the concept drift degree includes: comparing the determined concept drift degree with a preset value; when the concept drift degree is greater than the preset value, determining that the concept drift degree indicates that the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code; when the concept drift degree is less than or equal to the preset value, determining that the concept drift degree indicates that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code. Among them, when the concept drift degree is greater than the preset value, it means that the concept drift degree is large; when the concept drift degree of the cost model is less than or equal to the preset value, it means that the concept drift degree is small.
[0016] Optionally, determining the degree of concept drift of the cost model based on the target code includes: obtaining multiple candidate executable codes corresponding to the target code; obtaining measured performance data of each candidate executable code in the multiple candidate executable codes and predicted performance data of each candidate executable code, wherein the measured performance data of each candidate executable code is actually detected running performance data of each candidate executable code running on target hardware, and the predicted performance data of each candidate executable code is running performance data obtained by predicting the running performance of each candidate executable code through the cost model; determining the degree of concept drift of the cost model based on the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes.
[0017] The technical solution provided by the present application can improve the accuracy of the determined degree of concept drift because the process of determining the degree of concept drift of the cost model simultaneously considers the measured performance data and predicted performance data of multiple candidate executable codes corresponding to the target code.
[0018] Optionally, determining the degree of concept drift of the cost model according to the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes includes: determining the correlation between the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes; determining the degree of concept drift of the cost model according to the correlation between the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes. The correlation may be a Spearman correlation coefficient or a Kendall correlation coefficient.
[0019] Optionally, the running performance data used to characterize the running performance of the executable code includes the running time of the executable code. For example, the shorter the running time, the better the running performance of the corresponding executable code; the longer the running time, the worse the running performance of the corresponding executable code.
[0020] In a second aspect, a code compilation device is provided, the code compilation device comprising modules for executing the method provided in the first aspect or any optional manner of the first aspect. The modules in the code compilation device can be implemented based on software, hardware, or a combination of software and hardware, and the modules can be arbitrarily combined or divided based on specific implementations.
[0021] In a third aspect, a code compiling device is provided, which includes a memory and a processor; the memory is used to store a computer program; the processor is used to execute the computer program stored in the memory so that the code compiling device performs the method provided in the first aspect or any optional manner of the first aspect.
[0022] Optionally, the above-mentioned code compilation device is a compiler or a functional component in a compiler. The compiler can be deployed in a computer device. The computer device can be a terminal device, a computing server, etc. The terminal device can be a desktop computer, a laptop computer, a handheld computer, etc.
[0023] In a fourth aspect, a computer-readable storage medium is provided, in which a computer program is stored. When the computer program is executed, the method provided in the first aspect or any optional manner of the first aspect is implemented.
[0024] In a fifth aspect, a computer program product is provided, which includes a program or code, and when the program or code is executed, it implements the method provided in the first aspect or any optional manner of the first aspect.
[0025] In a sixth aspect, a chip is provided, which includes a programmable logic circuit and / or program instructions, and when the chip is running, it is used to implement the method provided by the first aspect or any optional manner of the first aspect.
[0026] The technical effects of the second to sixth aspects mentioned above can refer to the technical effects of the first aspect or any optional implementation method of the first aspect, and will not be elaborated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] Figure 1 is a flow chart of a code compilation method provided in an embodiment of the present application;
[0028] Figure 2 is a schematic diagram of a training cost model provided in an embodiment of the present application;
[0029] Figure 3 It is a flowchart of compiling a target code provided by an embodiment of the present application;
[0030] Figure 4 is another flowchart for compiling target code provided by an embodiment of the present application;
[0031] Figure 5 It is a schematic diagram of generating candidate executable codes corresponding to target codes provided by an embodiment of the present application;
[0032] Figure 6 is a schematic diagram of a code compiling device provided in an embodiment of the present application;
[0033] Figure 7 It is a schematic diagram of another code compilation device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0034] The implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.
[0035] Code compilation refers to the process of translating source code written in a high-level language into executable code that can run on hardware. For example, common high-level languages include the C++ programming language (C++), Java programming language, etc. Correspondingly, the source code can be C++ code, Java code, etc.
[0036] The code compilation process is usually performed by a compiler. Common compilers include low level virtual machine (LLVM), general public license compiler collection (GCC), etc. With the development of artificial intelligence (AI) technology, compilers for machine learning such as tensor virtual machine (TVM) and accelerated linear algebra (XLA) have also emerged. These compilers are usually divided into front-end, middle-end and back-end. The front-end is responsible for translating the source code into intermediate representation (IR) code, the middle-end is responsible for optimizing the IR code, and the back-end is responsible for translating the optimized IR code into executable code that can run on hardware. Code optimization in the middle-end and / or back-end combined with hardware features can improve the running performance of the final compiled code on the hardware, give full play to the hardware performance, and enhance the business performance and competitiveness of hardware products.
[0037] In the code compilation process, it is usually necessary to use a cost model to predict the running performance of the candidate executable code on the hardware, so that the optimal executable code obtained after the final compilation has better running performance on the hardware, for example, the optimal executable code obtained after the final compilation takes a shorter time to run on the hardware. The code compilation process is a continuous iterative process, and the optimal executable code obtained after the final compilation is the optimal candidate executable code obtained when the iteration termination condition is met. In each iteration process, the source code is first compiled to obtain multiple candidate executable codes that can run on the hardware; then the cost model is used to predict the running performance of the multiple candidate executable codes on the hardware, and the optimal candidate executable code among the multiple candidate executable codes is determined according to the running performance of the multiple candidate executable codes on the hardware, and finally it is detected whether the iteration termination condition is met; if the iteration termination condition is not met, the next iteration process is continued; if the iteration termination condition is met, the optimal candidate executable code obtained in this iteration process is used as the optimal executable code obtained after the final compilation and the code compilation process is terminated.
[0038] In traditional technology, the cost model is manually written based on experience. For example, LLVM defines a series of interfaces, and the cost model is manually written through these interfaces defined by LLVM. In one example, the getInstructionCost interface defined by LLVM can predict the running time of instructions on hardware, and the cost model for predicting the running time of executable code is manually written through the getInstructionCost interface. However, the running performance of executable code on hardware is related to the characteristics of the hardware. The running performance of the same executable code on different hardware is usually different. This requires considering the characteristics of the hardware when manually writing the cost model, writing the cost model in combination with the characteristics of the hardware, and writing different cost models for different hardware. However, manually writing the cost model is cumbersome and prone to errors, resulting in low reliability of the manually written cost model. In addition, it is difficult to traverse various hardware when manually writing the cost model. The cost model written for certain hardware is difficult to apply to other hardware, or even if it is applied to other hardware, it is difficult to accurately predict the running performance of the code on these hardware, thereby affecting the code compilation effect.
[0039] In order to solve the problems of manually writing cost models, the industry proposes to use machine learning algorithms to learn compilation experience to train cost models, and use cost models trained based on machine learning algorithms to predict the running performance of executable codes during code compilation. At present, cost models can be trained online or offline. Online training of cost models means that during code compilation, cost models are trained based on the running data of the code that needs to be compiled currently, and the running performance of candidate executable codes obtained by compiling the code that needs to be compiled currently is predicted based on the currently trained cost model. Offline training of cost models means that before code compilation, cost models are trained using the running data of historical codes running on hardware (referred to as historical running data), and the running performance of candidate executable codes obtained by compiling the code that needs to be compiled currently is directly predicted using the pre-trained cost model during code compilation. However, online training of cost models easily leads to a long code compilation process (usually taking hours to days), low compilation efficiency, and cannot guarantee the timeliness of services in scenarios with high timeliness requirements such as recommendation / click-throughrate (CTR), and online training of cost models cannot support scenarios with frequent code updates. Offline training of cost models requires a large amount of rich historical operation data that can cover various hardware scenarios as training data. The computing cost and time cost of collecting these historical operation data are high and difficult. If the collected historical operation data is not rich, it is difficult to ensure the generalization of the trained cost model, resulting in low prediction accuracy of the offline training cost model in some hardware scenarios, thus affecting the code compilation effect.
[0040] The embodiment of the present application provides a code compilation method and device, which determines the degree of concept drift of a cost model pre-trained based on historical operation data according to a target code to be compiled, and determines whether the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code according to the degree of concept drift of the cost model, and the multiple candidate executable codes corresponding to the target code are obtained by compiling the target code. When it is determined that the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code, the operation performance of the multiple candidate executable codes corresponding to the target code is predicted based on the cost model, so the cost model has a high accuracy in predicting the operation performance of the multiple candidate executable codes corresponding to the target code, which helps to ensure the code compilation effect, and when it is determined that the degree of concept drift of the cost model indicates that the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code, there is no need to train the cost model during the compilation process of the target code, which can shorten the time consumption of the code compilation process and improve the code compilation efficiency compared with the online training cost model. When it is determined that the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code, the cost model is updated based on the running data of at least one candidate executable code corresponding to the target code running on the target hardware, and the running performance of the multiple candidate executable codes corresponding to the target code is predicted based on the updated cost model. Since the cost model can learn the code features related to the target code and the hardware features of the target hardware during the process of updating the cost model based on the running data of at least one candidate executable code corresponding to the target code running on the target hardware, the updated cost model has higher accuracy in predicting the running performance of the multiple candidate executable codes corresponding to the target code and better compilation effect.
[0041] The technical solution of the present application is introduced below, and first the method embodiment of the present application is introduced.
[0042] Please refer to Figure 1 , which shows a flow chart of a code compilation method provided by an embodiment of the present application. The code compilation method is executed by a code compilation device, which is a compiler or a functional component in a compiler. The code compilation method includes the following steps S101 to S106.
[0043] S101. Obtain the target code that needs to be compiled.
[0044] The target code may be a source code or an IR code, and the target code may be a complete code or a code slice. A code slice is a section of a complete code, for example, a code that implements a functional module. In one embodiment, a code compiling device obtains a target code input to the code compiling device. For example, the target code is a source code, and the code compiling device obtains a target code input to the code compiling device by a user. For another example, the target code is an IR code, and the code compiling device obtains a target code input to the code compiling device by a front end of a compiler.
[0045] S102. Determine the degree of concept drift of the cost model according to the target code, where the cost model is trained based on historical operation data and is used to predict the operation performance of multiple candidate executable codes corresponding to the target code. The historical operation data is the operation data of historical codes that can be run on hardware that runs the executable code corresponding to the target code.
[0046] The cost model is obtained by offline training based on historical running data in advance. The historical running data is also called historical compilation data. The historical running data is the running data of the historical code that can be run on the hardware that runs the executable code corresponding to the target code. The historical running data includes the historical code and the running performance data of the historical code. The running performance data of the historical code can be measured performance data. The measured performance data is the running performance data of the historical code running on the hardware that is actually detected. For example, the historical running data includes multiple historical codes and the running performance data of each of the multiple historical codes. The multiple historical codes are all executable codes. Since the cost model is trained based on the running data of the historical code that can be run on the hardware that runs the executable code corresponding to the target code, the cost model can be used to predict the running performance of multiple candidate executable codes corresponding to the target code. The candidate executable codes corresponding to the target code are obtained by compiling the target code. Among them, the cost model can be various possible machine learning models such as extreme gradient boosting (XGBoost) model, long short term memory (LSTM) model, self-attention (transformer) model, deep neural networks (DNN) model, etc.
[0047] In an embodiment of the present application, when the code compiling device compiles the target code, the code compiling device determines the degree of concept drift of the cost model according to the target code. Wherein, concept drift refers to the phenomenon that the accuracy of the cost model prediction decreases due to the different distribution of the data to be predicted (such as the executable code corresponding to the target code in the embodiment of the present application) and the training data used to train the cost model (such as the historical operation data in the embodiment of the present application). The greater the degree of concept drift of the cost model, the more serious the loss of accuracy predicted by the cost model, and the lower the accuracy of the cost model prediction. The smaller the degree of concept drift of the cost model, the smaller the loss of accuracy predicted by the cost model, and the higher the accuracy of the cost model prediction. In an optional embodiment, the code compiling device obtains multiple candidate executable codes corresponding to the target code, the code compiling device obtains the measured performance data of each candidate executable code in the multiple candidate executable codes and the predicted performance data of each candidate executable code, and the code compiling device determines the degree of concept drift of the cost model according to the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes. Among them, the measured performance data of each candidate executable code in the multiple candidate executable codes is the actual detected running performance data of each candidate executable code running on the target hardware, and the predicted performance data of each candidate executable code in the multiple candidate executable codes is the running performance data obtained by predicting the running performance of each candidate executable code through the cost model. For example, the running performance data includes running time, the measured performance data of each candidate executable code includes the actual detected running time of each candidate executable code on the target hardware, and the predicted performance data of each candidate executable code includes the running time obtained by predicting the running time of each candidate executable code through the cost model. The target hardware is the hardware used to run the executable code corresponding to the target code, that is, the purpose of compiling the target code in the embodiment of the present application includes obtaining the executable code corresponding to the target code that can run on the target hardware. The target hardware can be a processor or other hardware, for example, the target hardware is a central processing unit (CPU) or a graphics processing unit (GPU).
[0048] In an optional embodiment, after the code compiling device obtains multiple candidate executable codes corresponding to the target code, for each candidate executable code in the multiple candidate executable codes: the code compiling device runs the candidate executable code on the target hardware and detects the running performance of the candidate executable code on the target hardware to obtain the measured performance data of the candidate executable code, and the code compiling device predicts the running performance of the candidate executable code through the cost model to obtain the predicted performance data of the candidate executable code. For example, the output parameter of the cost model is the predicted performance data, the code compiling device inputs the candidate executable code into the cost model, the cost model calculates the candidate executable code to obtain the predicted performance data of the candidate executable code and outputs the predicted performance data of the candidate executable code. Here, the output parameter of the cost model is taken as an example, which is the predicted performance data. In some embodiments, the output parameter of the cost model is a score used to characterize the running performance of the executable code. The higher the score, the better the running performance of the executable code, and the lower the score, the worse the running performance of the executable code. For example, the higher the score, the shorter the running time of the executable code, and the lower the score, the longer the running time of the executable code.
[0049] In an optional embodiment, after the code compiling device obtains the measured performance data of multiple candidate executable codes corresponding to the target code and the predicted performance data of the multiple candidate executable codes, the code compiling device determines the degree of concept drift of the cost model according to the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes, including: the code compiling device determines the correlation between the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes, and the code compiling device determines the degree of concept drift of the cost model according to the correlation between the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes. In one example, the code compiling device determines the degree of concept drift of the cost model as p=1-s, where p is the degree of concept drift of the cost model, and s is the correlation between the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes. The correlation between the measured performance data of the plurality of candidate executable codes and the predicted performance data of the plurality of candidate executable codes may be a Spearman correlation coefficient or a Kendall correlation coefficient.
[0050] In one embodiment, the code compiling device calculates the spearman correlation coefficient of the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes, and the code compiling device determines the spearman correlation coefficient of the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes as the correlation between the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes. In a specific embodiment, the code compiling device determines a first vector based on the measured performance data of the multiple candidate executable codes, the code compiling device determines a second vector based on the predicted performance data of the multiple candidate executable codes, and the code compiling device calculates the spearman correlation coefficient of the first vector and the second vector, and the spearman correlation coefficient of the first vector and the second vector is the spearman correlation coefficient of the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes. The first vector includes a plurality of first elements, and the second vector includes a plurality of second elements. The plurality of first elements correspond to the plurality of candidate executable codes one by one, and each first element is the measured performance data of the corresponding candidate executable code; the plurality of second elements correspond to the plurality of candidate executable codes one by one, and each second element is the predicted performance data of the corresponding candidate executable code; the arrangement order of the plurality of first elements is the same as the arrangement order of the plurality of second elements. In an example, the plurality of candidate executable codes are candidate executable codes C1, C2, C3...Ck, a total of k candidate executable codes, k is a positive integer, the first vector is [D11, D12, D13...D1k], and the second vector is [D21, D22, D23...D2k]. D11 is the measured performance data of candidate executable code C1, D12 is the measured performance data of candidate executable code C2, D13 is the measured performance data of candidate executable code C3, and so on, D1k is the measured performance data of candidate executable code Ck; D21 is the predicted performance data of candidate executable code C1, D22 is the predicted performance data of candidate executable code C2, D23 is the predicted performance data of candidate executable code C3, and so on, D2k is the predicted performance data of candidate executable code Ck; the code compilation device calculates the spearman correlation coefficient between [D11, D12, D13...D1k] and [D21, D22, D23...D2k].
[0051] In another embodiment, the code compilation device calculates the kendall correlation coefficient between the actual measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes, and the code compilation device determines the kendall correlation coefficient between the actual measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes as the correlation between the actual measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes. Specifically, the code compilation device determines a first vector based on the actual measured performance data of the multiple candidate executable codes, the code compilation device determines a second vector based on the predicted performance data of the multiple candidate executable codes, and the code compilation device calculates the kendall correlation coefficient between the first vector and the second vector, and the kendall correlation coefficient between the first vector and the second vector is the kendall correlation coefficient between the actual measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes.
[0052] In an optional embodiment, the code compiling device obtains multiple candidate executable codes corresponding to the target code, including: the code compiling device uses various possible search algorithms such as genetic algorithm, Monte Carlo tree search (MCTS) algorithm, reinforcement learning (RL) algorithm, etc. to obtain compilation parameters from the compilation parameter space, and the code compiling device compiles the target code according to the obtained compilation parameters to obtain multiple candidate executable codes corresponding to the target code. Among them, the compilation parameter space is also called a compilation parameter set, and the compilation parameter space can be input by the user to the code compiling device. The compilation parameter space includes multiple compilation parameters, and these multiple compilation parameters include loop expansion factor, parallelism, slice size, etc. The loop expansion factor refers to the number of loops in the code, the parallelism refers to the number of threads used to execute the code, and the slice size refers to the size of the code slice. For example, a code slice can be represented by a two-dimensional matrix or a three-dimensional matrix, and the slice size is the size of the two-dimensional matrix or the three-dimensional matrix. In a specific embodiment, the code compiling device modifies the parameters of the target code according to the obtained compilation parameters to obtain multiple candidate executable codes corresponding to the target code. For example, the code compiling device modifies at least one parameter of the loop expansion factor, parallelism, and slice size of the target code according to the obtained compilation parameters to obtain a candidate executable code corresponding to the target code. In the embodiment of the present application, the multiple candidate executable codes corresponding to the target code are all legal codes. After the code compiling device modifies at least one parameter of the loop expansion factor, parallelism, and slice size of the target code to obtain a candidate executable code corresponding to the target code, the code compiling device verifies the legality of the candidate executable code; if the code compiling device successfully verifies the legality of the candidate executable code, the code compiling device retains the candidate executable code; if the code compiling device fails to verify the legality of the candidate executable code, the code compiling device discards the candidate executable code.
[0053] The above embodiment is described by taking the example that the code compilation device determines the degree of concept drift of the cost model according to the correlation between the measured performance data of multiple candidate executable codes corresponding to the target code and the predicted performance data of the multiple candidate executable codes. The code compilation device can also determine the degree of concept drift of the cost model in other ways. In one embodiment, the historical operation data used for offline training of the cost model includes historical code, and the code compilation device determines the degree of concept drift of the cost model according to the KL divergence (Kullback-Leibler divergence, KL divergence) of the target code and the KL divergence of the historical code included in the historical operation data. Optionally, the historical code included in the historical operation data is an executable code, and the code compilation device obtains the source code corresponding to the historical code included in the historical operation data, and the code compilation device determines the degree of concept drift of the cost model according to the KL divergence of the target code and the KL divergence of the source code corresponding to the historical code.
[0054] In the embodiment of the present application, the cost model can be trained offline by the code compilation device described in the embodiment of the present application, or it can be trained by other devices and then transplanted into the code compilation device described in the embodiment of the present application. In an optional embodiment, the cost model is obtained by the code compilation device through offline training based on historical operation data, the historical operation data includes historical code and operation performance data of the historical code, the historical code is an executable code, the operation performance data of the historical code is used to characterize the operation performance of the historical code, and the operation performance data included in the historical operation data can be measured performance data, that is, the actual detected operation performance data of the corresponding historical code running on hardware, for example, the historical operation data includes multiple historical codes and the measured performance data of each of the multiple historical codes. When the code compilation device trains the cost model, the code compilation device first parses the historical operation data to obtain the multiple historical codes and the measured performance data of the multiple historical codes, and the code compilation device trains the cost model according to the multiple historical codes and the measured performance data of the multiple historical codes. In one embodiment, for each of the multiple historical codes, the code compiling device inputs the historical code into an initial model, so that the initial model performs performance prediction on the historical code to obtain predicted performance data of the historical code, and the code compiling device adjusts the model parameters of the initial model according to the predicted performance data of the historical code and the measured performance data of the historical code until a first preset condition is satisfied, and the code compiling device determines the model obtained when the first preset condition is satisfied as the final trained cost model. In a specific embodiment, for each of the multiple historical codes: the code compiling device adjusts the model parameters of the initial model according to the difference between the predicted performance data of the historical code obtained by the initial model prediction and the measured performance data of the historical code; the code compiling device inputs the historical code into the model after adjusting the parameters, so that the model after adjusting the parameters performs performance prediction on the historical code again to obtain the predicted performance data of the historical code; the code compiling device adjusts the model parameters again according to the difference between the predicted performance data of the historical code obtained by the re-prediction and the measured performance data of the historical code, and then performs prediction again; the code compiling device repeats the process of inputting the historical code to adjusting the model parameters to perform model training until the first preset condition is satisfied. Among them, the process from inputting historical code to adjusting model parameters is an iterative process.The first preset condition includes at least one of the following: the difference between the predicted performance data of the historical code obtained by the cost model and the actual performance data of the historical code is less than the preset difference; the number of training iterations (that is, the number of iterations in the training process) reaches the preset number; the change in the predicted performance data obtained by the cost model for multiple consecutive performance predictions on the same historical code is small, for example, the difference in the predicted performance data (for example, predicted running time) obtained by the cost model for multiple consecutive performance predictions on the same historical code is less than the preset difference.
[0055] The above embodiment is explained by taking the example of a code compiling device training a cost model according to multiple historical codes and the measured performance data of the multiple historical codes. In some embodiments, in order to simplify the training process, the code compiling device extracts the code features of each historical code in the multiple historical codes, and the code compiling device trains the cost model according to the code features of the multiple historical codes and the measured performance data of the multiple historical codes. In one embodiment, for each historical code in the multiple historical codes: the code compiling device inputs the code features of the historical code into the initial model, so that the initial model performs performance prediction on the historical code according to the code features of the historical code to obtain the predicted performance data of the historical code, the code compiling device adjusts the model parameters of the initial model according to the predicted performance data of the historical code and the measured performance data of the historical code until the first preset condition is met, and the code compiling device determines the model obtained when the first preset condition is met as the final trained cost model. The code features of any historical code in the multiple historical codes include numbers of float operations, number of bytes accessed, etc. The floating point calculation number of any historical code is the number of floating point operations in the historical code, and the memory access bit number of any historical code is the number of bits of memory data accessed by the historical code. As an example, please refer to Figure 2 , which shows a schematic diagram of a training cost model provided in an embodiment of the present application, Figure 2 Take the example of running performance data, which is running time. Figure 2As shown, the code compilation device parses the historical operation data to obtain multiple historical codes and the measured performance data (specifically the measured operation time) of each historical code in the multiple historical codes. The multiple historical codes include historical code 1 and historical code 2. The measured operation time of historical code 1 is 0.008ms (milliseconds), and the measured operation time of historical code 2 is 0.05ms. The code compilation device extracts the code feature Z1 of historical code 1 and the code feature Z2 of historical code 2. The code feature Z1 and the code feature Z2 respectively include floating point calculation numbers, memory access bits, etc. The code compilation device trains the cost model according to the code feature Z1 of historical code 1, the measured operation time "0.008ms" of historical code 1, the code feature Z2 of historical code 2, and the measured operation time "0.05ms" of historical code 2. The specific training process can be referred to the above description, which will not be repeated here.
[0056] S103. Determine whether the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code according to the concept drift degree of the cost model, and the multiple candidate executable codes corresponding to the target code are obtained by compiling the target code.
[0057] After the code compiling device determines the degree of concept drift of the cost model, the code compiling device determines whether the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code according to the degree of concept drift of the cost model; if the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code, the code compiling device executes S104; if the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code, the code compiling device executes S105 to S106. In one embodiment, the code compiling device compares the determined degree of concept drift of the cost model with a preset value; when the degree of concept drift is greater than the preset value, the code compiling device determines that the degree of concept drift of the cost model is large, and further determines that the degree of concept drift indicates that the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code; when the degree of concept drift is less than or equal to the preset value, the code compiling device determines that the degree of concept drift of the cost model is small, and further determines that the degree of concept drift indicates that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code. Among them, the preset value can be set according to actual conditions, for example, the preset value is 1.0, and the preset value can also be other values, which is not limited in the embodiment of the present application.
[0058] S104. When it is determined that the degree of concept drift of the cost model indicates that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code, the running performance of the multiple candidate executable codes corresponding to the target code is predicted based on the cost model, and the optimal executable code for the target code is determined from the multiple candidate executable codes based on the prediction result.
[0059] The code compiling device compiles the target code based on the cost model when it is determined that the concept drift degree of the cost model indicates that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code. In a specific embodiment, the code compiling device obtains multiple candidate executable codes corresponding to the target code, the code compiling device predicts the running performance of the multiple candidate executable codes based on the cost model, and the code compiling device determines the optimal executable code for the target code from the multiple candidate executable codes based on the prediction results of the multiple candidate executable codes, and the optimal executable code is also the final compilation result of the target code.
[0060] In the embodiment of the present application, the code compilation process is a continuously iterative process. When the code compilation device determines that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code, the code compilation device performs at least one iterative process on the target code based on the cost model to compile the target code. The code compilation device can obtain an optimal candidate executable code corresponding to the target code each time it executes an iterative process. The code compilation device determines the optimal candidate executable code obtained by the last iterative process as the final compilation result of the target code (that is, the optimal executable code of the target code). In a specific embodiment, in each iteration process, the code compiling device compiles the target code to obtain at least one candidate executable code corresponding to the target code, the code compiling device predicts the running performance of each candidate executable code in the at least one candidate executable code through the cost model to obtain the predicted performance data of each candidate executable code, the code compiling device determines the optimal candidate executable code in the at least one candidate executable code according to the predicted performance data of the at least one candidate executable code, the optimal candidate executable code is a candidate executable code with the best running performance in the at least one candidate executable code, and the code compiling device detects whether a first termination condition is met; if the first termination condition is met, the code compiling device determines the optimal candidate executable code obtained in this iteration process as the final compilation result of the target code (that is, the optimal executable code of the target code) and ends the compilation process of the target code; if the first termination condition is not met, the code compiling device continues to the next iteration process. Specifically, the code compiling device compiles the target code again to obtain at least one candidate executable code corresponding to the target code, the code compiling device predicts the running performance of each candidate executable code in the at least one candidate executable code obtained by the recompilation through the cost model to obtain the predicted performance data of each candidate executable code, the code compiling device determines the optimal candidate executable code in the at least one candidate executable code according to the predicted performance data of the at least one candidate executable code, and the code compiling device detects whether the first termination condition is satisfied. The first termination condition includes at least one of the following: the number of compilation iterations reaches a preset number; the predicted performance data of the optimal candidate executable code obtained in a certain iteration process meets the compilation requirements (for example, the predicted running time is less than the preset time); the difference in the running performance of the optimal candidate executable code corresponding to the target code predicted by the cost model in multiple consecutive iterations is small, for example, the difference in the predicted performance data obtained by the cost model for the performance prediction of the optimal candidate executable code corresponding to the target code in multiple consecutive iterations is less than the preset difference.
[0061] As an example, Figure 3A flowchart of compiling a target code provided by an embodiment of the present application is shown. Figure 3 When the code compiling device compiles the target code, the code compiling device generates at least one candidate executable code corresponding to the target code, that is, the code compiling device compiles the target code to obtain at least one candidate executable code corresponding to the target code. The code compiling device predicts the running performance of each candidate executable code in the at least one candidate executable code through the cost model to obtain the predicted performance data of each candidate executable code. The code compiling device determines a candidate executable code with the best running performance in the at least one candidate executable code according to the predicted performance data of the at least one candidate executable code, that is, determines the optimal candidate executable code in the at least one candidate executable code. For example, the predicted performance data of the at least one candidate executable code includes predicted running time, and the code compiling device determines a candidate executable code with the shortest predicted running time in the at least one candidate executable code as the optimal candidate executable code. And, the code compiling device detects whether the first termination condition is met. If the first termination condition is met, the code compiling device determines the optimal candidate executable code as the optimal executable code corresponding to the target code (that is, the final compilation result of the target code), and outputs the optimal executable code. If the first termination condition is not met, the code compiling device re-executes the process from generating at least one candidate executable code corresponding to the target code to detecting whether the first termination condition is met, that is, the code compiling device executes the next iteration process. Taking the predicted performance data including the predicted running time, the first termination condition including the predicted running time of the optimal candidate executable code corresponding to the target code obtained in a certain iteration process being less than the preset time as an example, in each iteration process, after the code compiling device determines the candidate executable code with the shortest predicted running time (that is, the optimal candidate executable code) among the at least one candidate executable code corresponding to the target code, the code compiling device detects whether the predicted running time of the optimal candidate executable code is less than the preset time; if the predicted running time of the optimal candidate executable code is less than the preset time, the code compiling device determines that the first termination condition is met; if the predicted running time of the optimal candidate executable code is not less than the preset time, the code compiling device determines that the first termination condition is not met. The implementation process of the code compiling device generating the candidate executable code corresponding to the target code can refer to the description in S102.
[0062] S105. When it is determined that the degree of concept drift of the cost model indicates that the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code, run at least one candidate executable code corresponding to the target code on the target hardware, and update the cost model based on the running data of the target code, where the running data of the target code is the running data of the at least one candidate executable code corresponding to the target code running on the target hardware.
[0063] The code compiling device updates the cost model based on the running data of the target code when it is determined that the concept drift degree of the cost model indicates that the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code, and compiles the target code based on the updated cost model, that is, the code compiling device executes S105 to S106. The running data of the target code is the running data of at least one candidate executable code corresponding to the target code running on the target hardware, and the running data of the target code includes the at least one candidate executable code corresponding to the target code and each candidate executable code in the at least one candidate executable code, that is, the running performance data of each candidate executable code actually detected running on the target hardware. In a specific embodiment, the code compiling device obtains at least one candidate executable code corresponding to the target code, runs the at least one candidate executable code on the target hardware to obtain the running data of the at least one candidate executable code running on the target hardware, that is, obtains the running data of the target code, and the code compiling device updates the cost model based on the running data of the target code.
[0064] The cost model is obtained by offline training based on historical operation data. Optionally, the code compilation device updates the cost model based on the operation data of the target code and the historical operation data, that is, the code compilation device uses the operation data of the target code and the historical operation data as training data to update the cost model. In a specific embodiment, the historical operation data includes historical code and operation performance data of the historical code, the historical code is executable code, and the operation performance data of the historical code is used to characterize the operation performance of the historical code. The operation performance data included in the historical operation data can be measured performance data, for example, the historical operation data includes multiple historical codes and measured performance data corresponding to the multiple historical codes. In one embodiment, the code compiling device updates the cost model based on the running data of the target code and the historical running data, including: the code compiling device obtains at least one candidate executable code corresponding to the target code, the code compiling device obtains the measured performance data of each candidate executable code in the at least one candidate executable code, and the code compiling device updates the cost model based on the multiple historical codes, the running performance data of the multiple historical codes (i.e., the measured performance data), the at least one candidate executable code, and the measured performance data of the at least one candidate executable code. The measured performance data of each candidate executable code in the at least one candidate executable code is the actually detected running performance data of each candidate executable code running on the target hardware, and the running data of the target code includes the at least one candidate executable code and the measured performance data of each candidate executable code in the at least one candidate executable code. In one embodiment, for each candidate executable code in the at least one candidate executable code, the code compiling device runs the candidate executable code on the target hardware, and the code compiling device detects the running performance of the candidate executable code on the target hardware to obtain the measured performance data of the candidate executable code. For the convenience of description, the multiple historical codes and the at least one candidate executable code are both referred to as sample codes. For each sample code in the multiple historical codes and the at least one candidate executable code: the code compiling device inputs the sample code into the cost model, so that the cost model performs performance prediction on the sample code to obtain the predicted performance data of the sample code. The code compiling device adjusts the model parameters of the cost model according to the predicted performance data of the sample code and the measured performance data of the sample code until the second preset condition is met. The code compiling device determines the code model obtained when the second preset condition is met as the updated cost model. The specific implementation process can refer to the implementation process of training the cost model in S102, which will not be described in detail here. Among them, the second preset condition can be the same as or different from the first preset condition described in S102.For example, the second preset condition includes at least one of the following: the difference between the predicted performance data of the sample code predicted by the cost model and the measured performance data of the sample code is less than the preset difference; the number of updated iterations (that is, the number of iterations in the update process) reaches the preset number; the change in the predicted performance data obtained by the cost model for multiple consecutive performance predictions of the same sample code is small, for example, the difference in the predicted performance data (for example, the predicted running time) obtained by the cost model for multiple consecutive performance predictions of the same sample code is less than the preset difference. The embodiment of the present application takes the example of a code compilation device updating a cost model according to a sample code and the measured performance data of the sample code as an example. In some embodiments, in order to simplify the update process, the code compilation device extracts code features of the sample code, and the code compilation device updates the cost model according to the code features of the sample code and the measured performance data of the sample code. The implementation process can refer to the implementation process of training the cost model in S102.
[0065] The above description takes the example that the code compiling device updates the cost model by using the running data of the target code and the historical running data used to train the cost model as training data. In some embodiments, the code compiling device samples the historical running data, and the code compiling device updates the cost model by using the running data of the target code and the running data sampled from the historical running data as training data. That is, the code compiling device updates the cost model based on the running data of the target code and part of the running data in the historical running data, thereby shortening the time of the updating process. For example, the running data obtained by the code compiling device by sampling the historical running data includes at least one historical code and the measured performance data of the at least one historical code, and the code compiling device updates the cost model based on the running data of the target code, the at least one historical code and the measured performance data of the at least one historical code.
[0066] In an optional embodiment, during the process of the code compiling device updating the cost model based on the running data of the target code and the historical running data, the code compiling device obtains at least one candidate executable code corresponding to the target code, including: the code compiling device obtains multiple candidate executable codes corresponding to the target code; the code compiling device obtains predicted performance data of each candidate executable code in the multiple candidate executable codes based on the cost model, the predicted performance data of each candidate executable code in the multiple candidate executable codes is obtained by predicting the running performance of each candidate executable code by the cost model; the code compiling device determines at least one candidate executable code in the multiple candidate executable codes according to the predicted performance data of the multiple candidate executable codes, that is, determines at least one candidate executable code corresponding to the target code. Among them, the at least one candidate executable code is a candidate executable code with the best predicted running performance among the multiple candidate executable codes, for example, a candidate executable code with a predicted running time less than a target running time among the multiple candidate executable codes, and another example is w candidate executable codes with the best running performance among the multiple candidate executable codes. For example, the multiple candidate executable codes are sorted in order of running performance from good to bad, and the at least one candidate executable code is the first w candidate executable codes among the multiple candidate executable codes, where w is a positive integer. In a specific embodiment, the code compiling device obtains multiple candidate executable codes corresponding to the target code, and the code compiling device predicts the running performance of each candidate executable code in the multiple candidate executable codes through the cost model to obtain the predicted performance data of each candidate executable code, and the code compiling device obtains at least one candidate executable code with the best predicted running performance in the multiple candidate executable codes according to the predicted performance data of the multiple candidate executable codes, and the code compiling device obtains the measured performance data of each candidate executable code in the at least one candidate executable code, and the code compiling device updates the cost model based on the multiple historical codes included in the historical running data, the running performance data of the multiple historical codes (that is, the measured performance data), the at least one candidate executable code, and the measured performance data of the at least one candidate executable code. The implementation process of the code compiling device obtaining the multiple candidate executable codes corresponding to the target code can refer to the relevant description in S102, which will not be repeated here.
[0067] It should be noted that the multiple candidate executable codes described in S104 may be the same as or different from the multiple candidate executable codes described in S102, and may or may not have an intersection. In the embodiment of the present application, the multiple candidate executable codes described in different schemes may be the same as or different, and may or may not have an intersection, and the embodiment of the present application does not limit this.
[0068] In an embodiment of the present application, a code compiling device updates a cost model based on the running data of a target code currently to be compiled and the historical running data used to train the cost model. For example, the cost model is updated based on the running data of the target code and the running data sampled from the historical running data. On the one hand, the cost model can learn code features related to the target code and hardware features related to the target code, such as learning code features of an executable code corresponding to the target code and related features of target hardware, which helps to improve the accuracy and prediction precision of the cost model in predicting the running performance of the executable code corresponding to the target code, and improves the accuracy of compiling the target code. On the other hand, the cost model can be prevented from forgetting the code features related to the historical code and the hardware features related to the historical code that have been learned, which also helps to improve the accuracy and prediction precision of the cost model in predicting the running performance of the executable code corresponding to the target code (the code features and hardware features learned by the cost model are relatively rich, so the prediction accuracy and prediction precision are high), and improves the accuracy of compiling the target code. The updating of the cost model described in the embodiments of the present application can also be described as incremental updating of the cost model. In the process of incremental updating of the model, catastrophic forgetting problems are usually encountered, that is, too much fitting of current data leads to forgetting of the knowledge that has been learned. The embodiments of the present application alleviate this phenomenon by means of data playback during the incremental updating of the cost model, that is, by updating the cost model using the running data used for training the cost model as training data, thereby alleviating this phenomenon.
[0069] S106. Predicting the running performance of multiple candidate executable codes corresponding to the target code based on the updated cost model, and determining the optimal executable code for the target code from the multiple candidate executable codes based on the prediction result.
[0070] After the code compiling device updates the cost model, it compiles the target code based on the updated cost model. In a specific embodiment, the code compiling device obtains multiple candidate executable codes corresponding to the target code, and the code compiling device predicts the running performance of the multiple candidate executable codes based on the updated cost model, and determines the optimal executable code for the target code from the multiple candidate executable codes based on the prediction results of the multiple candidate executable codes, and the optimal executable code is also the final compilation result of the target code.
[0071] In the embodiment of the present application, the code compilation process is a continuously iterative process. After the code compilation device updates the cost model, the code compilation device performs at least one iterative process on the target code based on the updated cost model to compile the target code. The code compilation device can obtain an optimal candidate executable code corresponding to the target code each time it executes an iterative process. The code compilation device determines the optimal candidate executable code obtained by the last iterative process as the final compilation result of the target code (that is, the optimal executable code of the target code). In a specific embodiment, during each iteration, the code compiling device compiles the target code to obtain multiple candidate executable codes corresponding to the target code, and the code compiling device predicts the running performance of each candidate executable code in the multiple candidate executable codes through the updated cost model to obtain the predicted performance data of each candidate executable code, and the code compiling device determines at least one candidate executable code with the best running performance from the multiple candidate executable codes according to the predicted performance data of the multiple candidate executable codes, for example, the at least one candidate executable code is a candidate executable code with a predicted running time less than a target running time among the multiple candidate executable codes, and for another example, the at least one candidate executable code is w candidate executable codes with the best running performance among the multiple candidate executable codes, and for example, the multiple candidate executable codes are sorted in order of running performance from good to bad, and the at least one candidate executable code is the w candidate executable codes with the best running performance among the multiple candidate executable codes. the first w candidate executable codes among the candidate executable codes, where w is a positive integer; the code compiling device runs each candidate executable code among the at least one candidate executable code on the target hardware to obtain the measured performance data of each candidate executable code, the code compiling device determines the optimal candidate executable code among the at least one candidate executable code according to the measured performance data of the at least one candidate executable code, the optimal candidate executable code is a candidate executable code with the best running performance among the at least one candidate executable code, and the code compiling device detects whether a second termination condition is met; if the second termination condition is met, the code compiling device determines the optimal candidate executable code obtained in this iterative process as the final compilation result of the target code (that is, the optimal executable code of the target code) and ends the compilation process of the target code; if the second termination condition is not met, the code compiling device continues to the next iterative process. Among them, the second termination condition includes at least one of the following: the number of compilation iterations (that is, the number of iterative processes) reaches a preset number; the measured performance data of the optimal candidate executable code obtained in a certain iteration process meets the compilation requirements (for example, the measured running time is less than the preset time); the difference in the running performance of the optimal candidate executable code obtained in multiple consecutive iterations is small, for example, the difference in the measured performance data of the optimal candidate executable code obtained in multiple consecutive iterations is less than the preset difference.
[0072] The above description takes the example that the code compiling device first completes the update of the cost model, and then performs at least one iteration process on the target code based on the updated cost model to compile the target code. In actual application, the process of updating the cost model and the process of compiling the target code can be interlaced. In an optional embodiment, the code compiling device performs at least one iteration process on the target code to compile the target code, and each iteration process includes updating the cost model. In the first iteration process, the code compiling device compiles the target code based on the original cost model. The code compiling device updates the cost model based on the running data of the target code obtained in this iteration process (that is, the running data of at least one candidate executable code corresponding to the target code obtained in this iteration process) and the historical running data used to train the cost model to obtain an updated cost model, for example, based on the running data of the target code obtained in this iteration process and the running data sampled from the historical running data, the cost model is updated to obtain an updated cost model. In each iteration process after the first iteration process, the code compilation device compiles the target code based on the updated cost model obtained in the previous iteration process, and the code compilation device updates the cost model again based on the running data of the target code obtained in this iteration process (that is, the running data of at least one candidate executable code corresponding to the target code obtained in this iteration process) and the historical running data used to train the cost model to obtain an updated cost model. For example, the cost model is updated based on the running data of the target code obtained in this iteration process and the running data sampled from the historical running data to obtain an updated cost model.In a specific embodiment, in a first iteration process, the code compiling device compiles the target code to obtain a plurality of candidate executable codes corresponding to the target code; the code compiling device predicts the running performance of each candidate executable code in the plurality of candidate executable codes through an original cost model to obtain predicted performance data of each candidate executable code; the code compiling device determines at least one candidate executable code with the best running performance from the plurality of candidate executable codes according to the predicted performance data of the plurality of candidate executable codes; the code compiling device runs each candidate executable code in the at least one candidate executable code on target hardware to obtain measured performance data of each candidate executable code, thereby obtaining running data of the target code, the running data of the target code including the at least one candidate executable code and the at least one candidate executable code. the measured performance data of at least one candidate executable code; the code compiling device updates the cost model based on the running data and historical running data of the target code to obtain an updated cost model; the code compiling device determines the best candidate executable code among the at least one candidate executable code according to the measured performance data of the at least one candidate executable code, and the best candidate executable code is a candidate executable code with the best running performance among the at least one candidate executable code, and the code compiling device detects whether a second termination condition is met; if the second termination condition is met, the code compiling device determines the best candidate executable code obtained in this iterative process as the final compilation result of the target code and ends the compilation process of the target code; if the second termination condition is not met, the code compiling device continues to the next iterative process.In each iteration process after the first iteration process, the code compiling device compiles the target code to obtain multiple candidate executable codes corresponding to the target code; the code compiling device predicts the running performance of each candidate executable code in the multiple candidate executable codes through the updated cost model obtained in the previous iteration process to obtain the predicted performance data of each candidate executable code; the code compiling device determines at least one candidate executable code with the best running performance from the multiple candidate executable codes according to the predicted performance data of the multiple candidate executable codes; the code compiling device runs each candidate executable code in the at least one candidate executable code on the target hardware to obtain the measured performance data of each candidate executable code, thereby obtaining the running data of the target code, and the running data of the target code includes the at least one candidate executable code. and the measured performance data of the at least one candidate executable code; the code compiling device updates the cost model based on the running data of the target code and the historical running data used to train the cost model to obtain an updated cost model; the code compiling device determines the optimal candidate executable code among the at least one candidate executable code according to the measured performance data of the at least one candidate executable code, and the code compiling device detects whether a second termination condition is met; if the second termination condition is met, the code compiling device determines the optimal candidate executable code obtained in this iteration process as the final compilation result of the target code and ends the compilation process of the target code, and the code compiling device determines the updated cost model obtained in the previous iteration process as the final updated model; if the second termination condition is not met, the code compiling device continues to the next iteration process.
[0073] As an example, Figure 4 Another flowchart of compiling the target code provided by the embodiment of the present application is shown. Figure 4 , the code compiling device performs at least one iterative process on the target code to compile the target code.
[0074] like Figure 4As shown, during the first iteration, the code compiling device generates multiple candidate executable codes corresponding to the target code. The code compiling device predicts the running performance of each candidate executable code in the multiple candidate executable codes through the original cost model to obtain the predicted performance data of each candidate executable code. The code compiling device determines at least one candidate executable code with the best running performance among the multiple candidate executable codes based on the predicted performance data of the multiple candidate executable codes. For example, the code compiling device determines w candidate executable codes with the best running performance among the multiple candidate executable codes, where w is a positive integer. For each candidate executable code in the at least one candidate executable code, the code compiling device runs the candidate executable code on the target hardware, and the code compiling device detects the running performance of the candidate executable code on the target hardware to obtain the measured performance data of the candidate executable code. The code compiling device can obtain the measured performance data of the at least one candidate executable code. The code compiling device updates the cost model according to the at least one candidate executable code, the measured performance data of the at least one candidate executable code, the multiple historical codes included in the historical operation data, and the measured performance data of the multiple historical codes to obtain an updated cost model. The code compiling device determines a candidate executable code with the best running performance among the at least one candidate executable code (that is, the optimal candidate executable code among the at least one candidate executable code) according to the measured performance data of the at least one candidate executable code. For example, the measured performance data of the at least one candidate executable code includes the measured running time, and the code compiling device determines a candidate executable code with the shortest measured running time among the at least one candidate executable code. And, the code compiling device detects whether the second termination condition is met. If the second termination condition is met, the code compiling device determines the optimal candidate executable code as the final compilation result of the target code (that is, the optimal executable code) and outputs the optimal executable code. If the second termination condition is not met, the code compiling device performs the next iteration process. Taking the example that the measured performance data includes the measured running time, and the second termination condition includes that the measured running time of the optimal candidate executable code corresponding to the target code obtained in a certain iteration process is less than the preset time, in the first iteration process, the code compilation device determines a candidate executable code with the shortest measured running time among the at least one candidate executable code (that is, the optimal candidate executable code), and then detects whether the measured running time of the optimal candidate executable code is less than the preset time; if the measured running time of the optimal candidate executable code is less than the preset time, the code compilation device determines that the second termination condition is met; if the measured running time of the optimal candidate executable code is not less than the preset time, the code compilation device determines that the second termination condition is not met.
[0075] like Figure 4As shown, in each iteration process after the first iteration process, the code compiling device generates multiple candidate executable codes corresponding to the target code. The code compiling device predicts the running performance of each candidate executable code in the multiple candidate executable codes through the updated cost model obtained in the previous iteration process to obtain the predicted performance data of each candidate executable code. The code compiling device determines at least one candidate executable code with the best running performance among the multiple candidate executable codes according to the predicted performance data of the multiple candidate executable codes. The code compiling device obtains the measured performance data of the at least one candidate executable code. The code compiling device updates the cost model according to the at least one candidate executable code, the measured performance data of the at least one candidate executable code, the multiple historical codes included in the historical running data, and the measured performance data of the multiple historical codes. And, the code compiling device determines a candidate executable code with the best running performance (that is, the optimal candidate executable code) among the at least one candidate executable code according to the measured performance data of the at least one candidate executable code. The code compiling device detects whether the second termination condition is met. If the second termination condition is met, the code compiling device determines the optimal candidate executable code as the final compilation result of the target code (ie, the optimal executable code) and outputs the optimal executable code. If the second termination condition is not met, the code compiling device continues the next iteration process.
[0076] In S106 , the implementation process of the code compiling device generating the candidate executable code corresponding to the target code may refer to the description in S102 .
[0077] It should be noted that in the embodiments of the present application, the preset differences, preset times, preset differences, preset time consumption, etc. involved in different conditions (including preset conditions and termination conditions) may be the same or different. The preset differences, preset times, preset differences, preset time consumption, etc. involved in any condition are set according to the actual situation specified by the condition, and the embodiments of the present application do not limit this.
[0078] To sum up, the technical solution provided by the embodiment of the present application determines the degree of concept drift of the cost model obtained by pre-training based on historical operation data according to the target code to be compiled, and determines whether the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code according to the degree of concept drift of the cost model. When it is determined that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code, the running performance of the multiple candidate executable codes corresponding to the target code is predicted based on the cost model. Therefore, the cost model has a high accuracy in predicting the running performance of the multiple candidate executable codes corresponding to the target code, which helps to ensure the code compilation effect. Moreover, when it is determined that the concept drift degree of the cost model indicates that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code, there is no need to train the cost model during the compilation process of the target code. Therefore, hardware testing (i.e., detecting the running performance of the code on the hardware) can be avoided, the time consumption of the code compilation process can be shortened, the code compilation efficiency can be improved, and the compilation performance of the target code can be improved. For example, compared with the solution of online training of the cost model, the time consumption of the code compilation process in the embodiment of the present application can be shortened by about 70%, and the compilation performance can be improved by about 19%. In scenarios with high timeliness requirements such as CTR, the timeliness of the business can be effectively guaranteed, and it is suitable for scenarios with frequent code updates. When it is determined that the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code, the cost model is updated based on the running data of at least one candidate executable code corresponding to the target code running on the target hardware, and the running performance of the multiple candidate executable codes corresponding to the target code is predicted based on the updated cost model. Since the cost model can learn the code features related to the target code and the hardware features of the target hardware during the process of updating the cost model based on the running data of at least one candidate executable code corresponding to the target code running on the target hardware, the updated cost model has a higher accuracy in predicting the running performance of the multiple candidate executable codes corresponding to the target code and a better compilation effect. Compared with the offline training cost model solution, the problem of low accuracy of cost model prediction caused by insufficient training data can be solved. Compared with the online training cost model solution, the time consumption of the code compilation process can also be shortened. For example, the time consumption of the code compilation process can be shortened by about 52%.
[0079] The technical solution provided in the embodiment of the present application can be applied not only to AI compilers such as TVM, but also to general compilers such as GCC. The technical solution provided in the embodiment of the present application is used for AI compilers, general compilers, etc. to estimate the running performance of the code on the hardware during the code compilation process. For example, the technical solution provided in the embodiment of the present application can be applied to the TARGET_RTX_COSTS calculation of GCC to more accurately estimate the performance overhead (i.e., running performance) of register transfer language (RTL) expressions, so that the compiler can select the RTL expression with the best performance from several equivalent RTL expressions, thereby improving the performance of the compiled code.
[0080] In order to facilitate understanding of the technical solution of the present application, the technical solution of the present application is introduced below with a specific example. Figure 5 The matrix multiplication code shown (that is, the code used to calculate matrix multiplication) is taken as an example to illustrate the running performance data, which is the running time.
[0081] The code compilation device obtains Figure 5 The target code shown. The code compiling device obtains the cost model M1 obtained by offline training based on historical operation data in advance. The code compiling device determines the degree of concept drift of the cost model M1 according to the target code. The code compiling device determines whether the degree of concept drift of the cost model M1 is greater than a preset value. When the degree of concept drift of the cost model M1 is not greater than (less than or equal to) the preset value, the code compiling device determines that the cost model M1 can accurately predict the running performance of multiple candidate executable codes corresponding to the target code, and the code compiling device predicts the running performance of multiple candidate executable codes corresponding to the target code based on the cost model M1, and determines the optimal executable code for the target code from the multiple candidate executable codes based on the prediction result. When the degree of concept drift of the cost model M1 is greater than the preset value, the code compiling device determines that the cost model M1 cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code. The code compiling device incrementally updates the cost model M1 based on the target code and the historical running data used when offline training the cost model M1 to obtain an updated cost model. The code compiling device predicts the running performance of multiple candidate executable codes corresponding to the target code based on the updated cost model, and determines the optimal executable code for the target code from the multiple candidate executable codes based on the prediction result.
[0082] For example, Figure 5As shown, the code compiling device uses various possible search algorithms such as genetic algorithm, MCTS algorithm, RL algorithm, etc. to obtain the compiling parameters from the compiling parameter space, and the compiling parameters include loop expansion factor, parallelism, slice size, etc. The code compiling device modifies at least one parameter of the loop expansion factor, parallelism, and slice size of the target code according to the obtained compiling parameters to obtain multiple candidate executable codes H1~H3 corresponding to the target code, and each candidate executable code in the candidate executable codes H1~H3 is obtained by modifying at least one parameter of the target code. For each candidate executable code in the candidate executable codes H1~H3: the code compiling device runs the candidate executable code on the target hardware, the code compiling device detects the running time of the candidate executable code on the target hardware to obtain the measured running time of the candidate executable code, and the code compiling device predicts the running time of the candidate executable code through the cost model M1 to obtain the predicted running time of the candidate executable code.
[0083] In an example, the measured running time of candidate executable code H1 is 0.1 milliseconds, the measured running time of candidate executable code H2 is 0.08 milliseconds, the measured running time of candidate executable code H3 is 0.07 milliseconds, the predicted running time of candidate executable code H1 is 0.13 milliseconds, the predicted running time of candidate executable code H2 is 0.12 milliseconds, and the predicted running time of candidate executable code H3 is 0.09 milliseconds. The code compiling device determines a first vector [0.1, 0.08, 0.07] according to the measured performance data of the candidate executable codes H1 to H3, and determines a second vector [0.13, 0.12, 0.09] according to the predicted performance data of the candidate executable codes H1 to H3. The code compiling device calculates the spearman correlation coefficient between the first vector and the second vector as SpearmanCorrelation([0.1, 0.08, 0.07], [0.13, 0.12, 0.09])=1.0. Therefore, the code compiling device determines that the correlation s between the measured performance data of the candidate executable codes H1 to H3 and the predicted performance data of the candidate executable codes H1 to H3 is 1.0. The code compiling device determines that the degree of concept drift of the cost model M1 is p=1-1.0=0 according to the correlation s between the measured performance data of the candidate executable codes H1 to H3 and the predicted performance data of the candidate executable codes H1 to H3. Assuming that the preset value is 1.0, since the concept drift degree "0" of the cost model M1 is not greater than the preset value "1.0", the code compiling device determines that the cost model M1 can accurately predict the running performance of multiple candidate executable codes corresponding to the target code. The code compiling device predicts the running performance of multiple candidate executable codes corresponding to the target code based on the cost model M1, and determines the optimal executable code for the target code from the multiple candidate executable codes based on the prediction result. The specific implementation process can refer to the description in S104. For example, the code compilation device makes the optimal candidate executable code corresponding to the target code be Sb = [candidate executable code, running time], the "running time" in the "[candidate executable code, running time]" is the running time of the "candidate executable code" in the "[candidate executable code, running time]", and in the initial state Sb = [null, double.max_value], "null" indicates that there is no optimal candidate executable code, and "double.max_value" indicates the type of the value of the running time; the code compilation device makes the maximum number of iterations of the loop compilation (that is, the preset number) be T, and T is a positive integer.During the tth iteration, t is a positive integer not greater than T, the code compiling device obtains a plurality of candidate executable codes G1-G3 corresponding to the target code by modifying at least one parameter of the loop expansion factor, parallelism, and slice size of the target code; the code compiling device predicts the running time of each candidate executable code in the candidate executable codes G1-G3 by using the cost model M1 to obtain the predicted running time of each candidate executable code; for example, the predicted running time of the candidate executable code G1 is 0.15 milliseconds, the predicted running time of the candidate executable code G2 is 0.08 milliseconds, and the predicted running time of the candidate executable code G3 is 0.1 milliseconds; since the predicted running time of the candidate executable code G2 is the shortest among the candidate executable codes G1-G3, the code compiling device determines that the candidate executable code G2 is the current optimal candidate executable code; the code compiling device updates the optimal candidate executable code to Sb=min according to the candidate executable code G2. _._2 (Sb, [candidate executable code G2, 0.08 milliseconds]), min _._2 It indicates that the optimal candidate executable code is selected according to the minimum value of the second item (i.e., the running time) in [candidate executable code, running time], and the optimal candidate executable code is the candidate executable code with the smallest second item in [candidate executable code, running time], that is, the candidate executable code with the shortest running time; the code compiling device determines whether t is less than T; if t is less than T, the code compiling device performs the t+1th iteration process on the target code; if t is not less than T (i.e., t is greater than or equal to T), the code compiling device determines that the iteration termination condition (e.g., the first termination condition described in S103) is reached, and the code compiling device determines the optimal candidate executable code G2 obtained in the tth iteration process as the final compilation result of the target code (i.e., the optimal executable code).
[0084] In another example, the measured running time of candidate executable code H1 is 0.1 milliseconds, the measured running time of candidate executable code H2 is 0.08 milliseconds, the measured running time of candidate executable code H3 is 0.07 milliseconds, the predicted running time of candidate executable code H1 is 0.09 milliseconds, the predicted running time of candidate executable code H2 is 0.12 milliseconds, and the predicted running time of candidate executable code H3 is 0.11 milliseconds. The code compiling device determines the first vector [0.1, 0.08, 0.07] according to the measured performance data of the candidate executable codes H1-H3, and determines the second vector [0.09, 0.12, 0.11] according to the predicted performance data of the candidate executable codes H1-H3. The code compiling device calculates the spearman correlation coefficient between the first vector and the second vector as SpearmanCorrelation ([0.1, 0.08, 0.07], [0.09, 0.12, 0.11]) = -0.5, so the code compiling device determines that the correlation s between the measured performance data of the candidate executable codes H1-H3 and the predicted performance data of the candidate executable codes H1-H3 is -0.5. The code compiling device determines that the concept drift degree of the cost model M1 is p = 1-s = 1-(-0.5) = 1.5 according to the correlation s between the measured performance data of the candidate executable codes H1-H3 and the predicted performance data of the candidate executable codes H1-H3. Assuming that the preset value is 1.0, since the concept drift degree "1.5" of the cost model M1 is greater than the preset value "1.0", the code compiling device determines that the cost model M1 cannot accurately predict the running performance of the multiple candidate executable codes corresponding to the target code. The code compiling device incrementally updates the cost model M1 based on the running data of the target code and the historical running data used when the cost model M1 is trained offline to obtain an updated cost model. The code compiling device predicts the running performance of the multiple candidate executable codes corresponding to the target code based on the updated cost model, and determines the optimal executable code for the target code from the multiple candidate executable codes based on the prediction result. The specific implementation process can refer to the description in S105 to S106. For example, the code compiling device sets the optimal candidate executable code Sb corresponding to the target code = [candidate executable code, running time], sets the measured code set Sc = {}, and sets the maximum number of iterations of loop compilation (i.e., the preset number) to T, where T is a positive integer.In the tth iteration process, t is a positive integer not greater than T, the code compiling device obtains candidate executable codes X1-X3 corresponding to the target code by modifying at least one parameter of the loop expansion factor, parallelism, and slice size of the target code; the code compiling device predicts each candidate executable code X1-X3 through a cost model M1 (when t=1, the cost model M1 is the original cost model, that is, the unupdated cost model; when t>1, the cost model M1 is the updated cost model obtained in the previous iteration process) The running time of the candidate executable code is calculated to obtain the predicted running time of each candidate executable code; for example, the predicted running time of the candidate executable code X1 is 0.15 milliseconds, the predicted running time of the candidate executable code X2 is 0.08 milliseconds, and the predicted running time of the candidate executable code X3 is 0.1 milliseconds; the code compiling device determines at least one candidate executable code with the shortest predicted running time among the candidate executable codes X1 to X3 according to the predicted running times of the candidate executable codes X1 to X3, for example, the code compiling device determines at least one candidate executable code with the shortest predicted running time among the candidate executable codes X1 to X3 The two candidate executable codes with the shortest running time are predicted to be candidate executable code X2 and candidate executable code X3; the code compiling device runs candidate executable code X2 and candidate executable code X3 on the target hardware, and the code compiling device detects the running time of each candidate executable code in candidate executable code X2 and candidate executable code X3 on the target hardware to obtain the measured running time of each candidate executable code; for example, the code compiling device determines that the measured running time of candidate executable code X2 is 0.07 milliseconds, and the measured running time of candidate executable code X3 is 0.07 milliseconds. The measured running time of code X3 is 0.09 milliseconds, and the code compiling device adds [candidate executable code X2, 0.07 milliseconds] and [candidate executable code X3, 0.09 milliseconds] to the measured code set Sc; and, because among candidate executable code X2 and candidate executable code X3, candidate executable code X2 has the shortest measured running time, the code compiling device determines that candidate executable code X2 is the current optimal candidate executable code; the code compiling device updates the optimal candidate executable code to Sb=min according to candidate executable code X2. _._2 (Sb, [candidate executable code X2, 0.07 milliseconds]); the code compiling device incrementally updates the cost model M1 based on the measured code set Sc and the historical operation data; the code compiling device determines whether t is less than T. If t is less than T, the code compiling device executes the t+1th iteration process on the target code; if t is not less than T, the code compiling device determines that the iteration termination condition (such as the second termination condition described in S105) is reached, and the code compiling device determines the optimal candidate executable code X2 obtained in the tth iteration process as the result (that is, the optimal executable code).
[0085] The above is an introduction to the method embodiment of the present application. The following is an introduction to the device embodiment of the present application. The device of the present application can be used to execute the method of the present application. For details not disclosed in the device embodiment of the present application, please refer to the method embodiment.
[0086] Please refer to Figure 6 , which shows a schematic diagram of a code compiling device 600 provided in an embodiment of the present application. The code compiling device 600 is used to execute Figure 1 The code compilation method provided in the embodiment shown is as follows. Figure 6 As shown, the code compiling device 600 includes: an acquisition module 601 , a first determination module 602 , a second determination module 603 and a first compiling module 604 .
[0087] The acquisition module 601 is used to acquire the target code to be compiled;
[0088] A first determination module 602 is used to determine the degree of concept drift of a cost model according to the target code, where the cost model is trained based on historical operation data and is used to predict the operation performance of multiple candidate executable codes corresponding to the target code, where the historical operation data is operation data of historical codes that can be run on hardware that runs the executable code corresponding to the target code;
[0089] A second determination module 603 is used to determine whether the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code according to the degree of concept drift, and the multiple candidate executable codes corresponding to the target code are obtained by compiling the target code;
[0090] The first compilation module 604 is used to predict the running performance of multiple candidate executable codes corresponding to the target code based on the cost model when it is determined that the degree of concept drift indicates that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code, and determine the optimal executable code for the target code from the multiple candidate executable codes based on the prediction result.
[0091] Among them, the functional implementation of the acquisition module 601 can refer to the relevant description in S101, the functional implementation of the first determination module 602 can refer to the relevant description in S102, the functional implementation of the second determination module 603 can refer to the relevant description in S103, and the functional implementation of the first compilation module 604 can refer to the relevant description in S104.
[0092] Optional, please continue to refer to Figure 6 The code compiling device 600 also includes: an updating module 605 and a second compiling module 606 .
[0093] An updating module 605 is configured to, when determining that the degree of concept drift indicates that the cost model cannot accurately predict the running performance of the plurality of candidate executable codes corresponding to the target code, run at least one candidate executable code corresponding to the target code on the target hardware, and update the cost model based on the running data of the target code, where the running data of the target code is the running data of the at least one candidate executable code corresponding to the target code running on the target hardware;
[0094] The second compiling module 606 is used to predict the running performance of multiple candidate executable codes corresponding to the target code based on the updated cost model, and determine the optimal executable code for the target code from the multiple candidate executable codes based on the prediction result.
[0095] The functional implementation of the updating module 605 may refer to the relevant description in S105 , and the functional implementation of the second compiling module 606 may refer to the relevant description in S106 .
[0096] Optionally, the updating module 605 is used to use the running data of the target code and the running data sampled from the historical running data as training data of the cost model.
[0097] Optionally, the second determination module 603 is used to: compare the determined degree of concept drift with a preset value; when the degree of concept drift is greater than the preset value, determine that the degree of concept drift indicates that the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code; when the degree of concept drift is less than or equal to the preset value, determine that the degree of concept drift indicates that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code.
[0098] Optionally, the first determination module 602 is used to: obtain multiple candidate executable codes corresponding to the target code; obtain the measured performance data of each candidate executable code in the multiple candidate executable codes and the predicted performance data of each candidate executable code, the measured performance data of each candidate executable code is the actual detected running performance data of each candidate executable code running on the target hardware, and the predicted performance data of each candidate executable code is the running performance data obtained by predicting the running performance of each candidate executable code through the cost model; determine the concept drift degree of the cost model according to the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes. For example, the first determination module 602 is used to: obtain the correlation between the measured performance data of the multiple candidate executable codes and the predicted performance data of the multiple candidate executable codes; obtain the concept drift degree of the cost model according to the correlation. The correlation can be a Spearman correlation coefficient or a Kendall correlation coefficient.
[0099] Optionally, the historical running data includes historical code and running performance data of the historical code, the running performance data of the historical code is used to characterize the running performance of the historical code, and the update module 605 is used to: obtain at least one candidate executable code corresponding to the target code; obtain measured performance data of each candidate executable code in the at least one candidate executable code, the measured performance data of each candidate executable code is the performance data actually detected when each candidate executable code runs on the target hardware; based on the historical code, the running performance data of the historical code, the at least one candidate executable code and the measured performance data of the at least one candidate executable code, update the cost model.
[0100] Optionally, the update module 605 is used to: obtain multiple candidate executable codes corresponding to the target code; obtain predicted performance data of each candidate executable code in the multiple candidate executable codes based on the cost model, and the predicted performance data of each candidate executable code is obtained by predicting the running performance of each candidate executable code by the cost model; and determine at least one candidate executable code in the multiple candidate executable codes based on the predicted performance data of the multiple candidate executable codes.
[0101] Optionally, the running performance data used to characterize the running performance of the executable code includes the running time of the executable code.
[0102] In summary, the technical solution provided by the embodiment of the present application determines the degree of concept drift of the cost model obtained by pre-training based on historical operation data according to the target code to be compiled, and determines whether the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code according to the degree of concept drift of the cost model. When it is determined that the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code, the operation performance of multiple candidate executable codes corresponding to the target code is predicted based on the cost model. Therefore, the cost model has a high accuracy in predicting the operation performance of multiple candidate executable codes corresponding to the target code, which helps to ensure the code compilation effect. Moreover, when it is determined that the degree of concept drift of the cost model indicates that the cost model can accurately predict the operation performance of multiple candidate executable codes corresponding to the target code, there is no need to train the cost model during the compilation process of the target code. Therefore, the time consumption of the code compilation process can be shortened, the code compilation efficiency can be improved, and the compilation performance of the target code can be improved. When it is determined that the cost model cannot accurately predict the running performance of multiple candidate executable codes corresponding to the target code, the cost model is updated based on the running data of at least one candidate executable code corresponding to the target code running on the target hardware, and the running performance of the multiple candidate executable codes corresponding to the target code is predicted based on the updated cost model. Since the cost model can learn the code features related to the target code and the hardware features of the target hardware during the process of updating the cost model based on the running data of at least one candidate executable code corresponding to the target code running on the target hardware, the updated cost model has higher accuracy in predicting the running performance of the multiple candidate executable codes corresponding to the target code, better compilation effect, and can improve the compilation performance of the target code.
[0103] It should be understood that the code compilation device provided in the embodiment of the present application can also be implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD), and the above-mentioned PLD can be a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL) or any combination thereof. The code compilation method provided in the above-mentioned method embodiment can also be implemented by software. When the code compilation method provided in the above-mentioned method embodiment is implemented by software, each module in the code compilation device can also be a software module.
[0104] The embodiment of the present application also provides a code compiling device, including a memory and a processor. The memory is used to store a computer program. The processor is used to execute the computer program stored in the memory so that the code compiling device executes Figure 1 The method embodiment is shown.
[0105] As an example, see Figure 7 , which shows a schematic diagram of another code compiling device 700 provided in an embodiment of the present application. The code compiling device 700 is a compiler or a functional component in a compiler. The code compiling device 700 is used to execute Figure 1 The method embodiment shown. Figure 7 The code compiling device 700 includes: a processor 701 and a memory 702. The memory 701 and the memory 702 are connected via a bus 703. Figure 7 The processor 701 and the memory 702 are described as being independent of each other. Optionally, the processor 701 and the memory 702 are integrated together.
[0106] The memory 702 is used to store computer programs, including operating systems and program codes. The memory 702 is a storage medium of various types, such as random access memory (RAM), read-only memory (ROM), non-volatile random access memory (NVRAM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), compact disc read-only memory (CD-ROM), flash memory, register, optical disc storage, optical disc storage (including compact disc, laser disc, optical disc, digital versatile disc, Blu-ray disc, etc.), disk or other magnetic storage device.
[0107] Among them, the processor 701 is a general-purpose processor or a special-purpose processor. A general-purpose processor is a processor that performs specific steps and / or operations by reading and executing a computer program stored in a memory (e.g., memory 702). The general-purpose processor may use a computer program stored in a memory (e.g., memory 702) in the process of performing the above steps and / or operations. The computer program is, for example, executed to implement the related functions of the aforementioned acquisition module 601, the first determination module 602, the second determination module 603, the first compilation module 604, the update module 605, and the second compilation module 606. A general-purpose processor is, for example, but not limited to, a central processing unit (CPU). A special-purpose processor is a processor specially designed to perform specific steps and / or operations, and a special-purpose processor is, for example, but not limited to, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL) or any combination thereof. Optionally, the processor 701 may be a single-CPU processor or a multi-CPU processor. The processor 701 includes at least one circuit to execute all or part of the steps of the code compiling method provided in the above embodiment.
[0108] Optionally, the code compiling apparatus 700 further includes a network interface 704, and the network interface 704 is connected to the processor 701 and the memory 702 via the bus 703. The network interface 704 enables the code compiling apparatus 700 to communicate with other devices.
[0109] Optionally, the code compiling device 700 further includes an input / output (I / O) interface 705, which is connected to the processor 701 and the memory 702 via the bus 703. The processor 701 can receive input commands or data, etc. via the I / O interface 705. The I / O interface 705 is used for the code compiling device 700 to connect input devices, such as a keyboard, a mouse, etc.
[0110] Optionally, the code compilation device 700 further includes a display 706, which is connected to the processor 701 and the memory 702 via the bus 703. The display 706 can be used to display the intermediate results and / or final results generated by the processor 701 executing the above code compilation method. For example, the display 706 is used to display the candidate executable code corresponding to the target code described in the above embodiment. In a possible implementation, the display 706 is a touch display screen to provide a human-computer interaction interface.
[0111] Optionally, in some possible scenarios, the network interface 704 and the I / O interface 705 are collectively referred to as a communication interface. In addition, the communication interface also includes a transceiver for sending and receiving information, which is not limited in the embodiment of the present application.
[0112] The bus 703 is any type of communication bus for interconnecting the internal components of the code compiling apparatus 700. The bus 703 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus 703 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 7 Only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus. The embodiment of the present application takes the above-mentioned devices in the code compiling device 700 being interconnected through the bus 703 as an example. Optionally, the above-mentioned devices in the code compiling device 700 are connected to each other in communication with each other using other connection methods other than the bus 703, for example, the above-mentioned devices in the code compiling device 700 are interconnected through a logical interface in the code compiling device 700.
[0113] The above devices may be arranged on independent chips, or at least partially or completely on the same chip. Whether to arrange each device independently on different chips or to integrate them on one or more chips often depends on the needs of product design. The embodiments of the present application do not limit the specific implementation form of the above devices.
[0114] Figure 7 The code compiling device 700 shown is merely exemplary. During implementation, the code compiling device 700 includes other components, which are not listed one by one herein. Figure 7 The code compiling apparatus 700 shown can compile the target code by executing all or part of the steps of the code compiling method provided in the above embodiment.
[0115] Based on the same inventive concept, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program. When the computer program is executed (for example, executed by a code compilation device, a compiler, one or more processors, etc.), it implements all or part of the steps of the method provided in the above method embodiment.
[0116] Based on the same inventive concept, an embodiment of the present application provides a computer program product, which includes a program or code. When the program or code is executed (for example, executed by a code compilation device, a compiler, one or more processors, etc.), it implements all or part of the steps of the method provided in the above method embodiment.
[0117] Based on the same inventive concept, an embodiment of the present application provides a chip, which includes a programmable logic circuit and / or program instructions. When the chip is running, it is used to implement all or part of the steps of the method provided in the above method embodiment.
[0118] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product, and the computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, a computer, a server or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server, a data center, etc. that contains one or more available media integrated. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium, or a semiconductor medium (e.g., a solid-state hard disk), etc.
[0119] It should be understood that the term "at least one" in this application refers to one or more, and "plurality" refers to two or more. In this application, unless otherwise specified, the symbol " / " generally means or, for example, A / B can represent A or B. The term "and / or" in this application is merely a description of the association relationship of associated objects, indicating that three relationships may exist, for example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, in order to facilitate clear description, in this application, the words "first", "second", "third" and the like are used to distinguish between identical or similar items with substantially the same functions and effects. Those skilled in the art can understand that the words "first", "second", "third" and the like do not limit the quantity and execution order.
[0120] The different types of embodiments such as the method embodiments and device embodiments provided in the embodiments of the present application can refer to each other, and the embodiments of the present application are not limited thereto. The sequence of operations of the method embodiments provided in the embodiments of the present application can be appropriately adjusted, and the operations can also be increased or decreased in response to the situation. Any technician familiar with the technical field can easily think of the changed methods within the technical scope disclosed in the present application, and they should be included in the protection scope of the present application, so they will not be repeated.
[0121] In the corresponding embodiments provided in the present application, it should be understood that the disclosed devices and the like can be implemented by other configuration methods. For example, the device embodiments described above are only schematic, for example, the division of units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, and the indirect coupling or communication connection of devices or units can be electrical or other forms.
[0122] The units described as separate components may or may not be physically separated, and the components described as units may or may not be physical units, and may be located in one place or distributed on multiple network nodes. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0123] The above is only an exemplary embodiment of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.
Claims
1. A code compilation method, characterized in that: The method comprises: Get the target code that needs to be compiled; Determining a degree of concept drift of a cost model according to the target code, wherein the cost model is trained based on historical running data and is used to predict running performance of a plurality of candidate executable codes corresponding to the target code, wherein the historical running data is running data of historical codes that can be run on hardware running the executable code corresponding to the target code; Determining whether the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code according to the degree of concept drift, wherein the multiple candidate executable codes corresponding to the target code are obtained by compiling the target code; When it is determined that the degree of concept drift indicates that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code, the running performance of multiple candidate executable codes corresponding to the target code is predicted based on the cost model, and the optimal executable code for the target code is determined from the multiple candidate executable codes based on the prediction result.
2. The method according to claim 1, characterized in that The method further comprises: In a case where it is determined that the degree of concept drift indicates that the cost model cannot accurately predict the running performance of the plurality of candidate executable codes corresponding to the target code, running at least one candidate executable code corresponding to the target code on target hardware, and updating the cost model based on running data of the target code, where the running data of the target code is running data of the at least one candidate executable code corresponding to the target code running on the target hardware; The running performance of multiple candidate executable codes corresponding to the target code is predicted based on the updated cost model, and the optimal executable code is determined for the target code from the multiple candidate executable codes based on the prediction result.
3. The method according to claim 2, characterized in that The updating of the cost model based on the running data of the target code includes: The operation data of the target code and the operation data sampled from the historical operation data are used as training data for the cost model.
4. The method according to any one of claims 1 to 3, characterized in that: The determining, according to the degree of concept drift, whether the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code includes: Comparing the determined degree of concept drift with a preset value; When the degree of concept drift is greater than the preset value, determining that the degree of concept drift indicates that the cost model cannot accurately predict the running performance of the plurality of candidate executable codes corresponding to the target code; When the concept drift degree is less than or equal to the preset value, determining that the concept drift degree indicates that the cost model can accurately predict the running performance of the plurality of candidate executable codes corresponding to the target code.
5. The method according to any one of claims 1 to 4, characterized in that: Determining the degree of concept drift of the cost model according to the target code includes: Obtaining multiple candidate executable codes corresponding to the target code; Obtaining measured performance data of each candidate executable code among the multiple candidate executable codes and predicted performance data of each candidate executable code, wherein the measured performance data of each candidate executable code is actually detected running performance data of each candidate executable code running on target hardware, and the predicted performance data of each candidate executable code is running performance data obtained by predicting the running performance of each candidate executable code by using the cost model; The degree of concept drift of the cost model is determined according to the measured performance data of the plurality of candidate executable codes and the predicted performance data of the plurality of candidate executable codes.
6. A code compiling device, characterized in that: The device comprises: Get module, used to get the target code that needs to be compiled; A first determination module is used to determine the degree of concept drift of a cost model according to the target code, wherein the cost model is obtained by training based on historical operation data and is used to predict the operation performance of multiple candidate executable codes corresponding to the target code, wherein the historical operation data is operation data of historical codes that can be run on hardware that runs the executable code corresponding to the target code; A second determination module is used to determine whether the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code according to the degree of concept drift, and the multiple candidate executable codes corresponding to the target code are obtained by compiling the target code; The first compilation module is used to predict the running performance of multiple candidate executable codes corresponding to the target code based on the cost model when it is determined that the degree of concept drift indicates that the cost model can accurately predict the running performance of multiple candidate executable codes corresponding to the target code, and determine the optimal executable code for the target code from the multiple candidate executable codes based on the prediction result.
7. The device according to claim 6, characterized in that The device also includes: an updating module, configured to, when determining that the degree of concept drift indicates that the cost model cannot accurately predict the running performance of the plurality of candidate executable codes corresponding to the target code, run at least one candidate executable code corresponding to the target code on target hardware, and update the cost model based on running data of the target code, wherein the running data of the target code is running data of the at least one candidate executable code corresponding to the target code running on the target hardware; The second compiling module is used to predict the running performance of multiple candidate executable codes corresponding to the target code based on the updated cost model, and determine the optimal executable code for the target code from the multiple candidate executable codes based on the prediction result.
8. The device according to claim 7, characterized in that The updating module is used to use the running data of the target code and the running data sampled from the historical running data as training data of the cost model.
9. The device according to any one of claims 6 to 8, characterized in that The second determining module is used to: Comparing the determined degree of concept drift with a preset value; When the degree of concept drift is greater than the preset value, determining that the degree of concept drift indicates that the cost model cannot accurately predict the running performance of the plurality of candidate executable codes corresponding to the target code; When the concept drift degree is less than or equal to the preset value, determining that the concept drift degree indicates that the cost model can accurately predict the running performance of the plurality of candidate executable codes corresponding to the target code.
10. The device according to any one of claims 6 to 9, characterized in that The first determining module is used to: Obtaining multiple candidate executable codes corresponding to the target code; Obtaining measured performance data of each candidate executable code among the multiple candidate executable codes and predicted performance data of each candidate executable code, wherein the measured performance data of each candidate executable code is actually detected running performance data of each candidate executable code running on target hardware, and the predicted performance data of each candidate executable code is running performance data obtained by predicting the running performance of each candidate executable code by using the cost model; The degree of concept drift of the cost model is determined according to the measured performance data of the plurality of candidate executable codes and the predicted performance data of the plurality of candidate executable codes.
11. A code compiling device, characterized in that: including memory and processor; The memory is used to store computer programs; The processor is configured to execute the computer program stored in the memory so that the code compiling device executes the method according to any one of claims 1 to 5.
12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed, the method according to any one of claims 1 to 5 is implemented.
13. A computer program product, characterized in that The computer program product comprises a program or a code, and when the program or the code is executed, the method according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Method, apparatus and program product for model update
CN114912497A
Code compiling method and device and execution method and device based on virtual machine
CN116360790A