Problem-oriented code optimization method and system and electronic equipment

By building problem-oriented data sets and training of large language models, combined with model merging technology, the limitations in existing code optimization methods are solved, global algorithm innovation and efficient code generation are realized, and the efficiency and correctness of code optimization are significantly improved. sex.

CN119148993AInactive Publication Date: 2024-12-17ZHEJIANG UNIV +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410987919.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-23
Publication Date
2024-12-17
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

Existing code optimization methods rely on iterative improvements of a single programmer, resulting in local optimization, inefficiency and lack of diversity, making it difficult to achieve global algorithm innovation and efficient code generation.

Method used

By collecting code submissions from different programmers, building problem-oriented data sets, using large language models for training, and using model merging technology to improve the correctness and performance of the code, including low-rank adaptation and spherical interpolation technology to adjust model parameter weights.

Benefits of technology

Global algorithm innovation has been achieved, the efficiency and correctness of code optimization has been significantly improved, the limitations of traditional methods have been overcome, and the generated optimization code has high accuracy and stability in practical applications.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119148993A_ABST
    Figure CN119148993A_ABST
Patent Text Reader

Abstract

The invention discloses a problem-oriented code optimization method and system. The method belongs to the field of software engineering, and aims to solve the problem that an existing code optimization method depends on multiple times of submission of a single programmer and neglects global algorithm innovation. According to the method, multiple times of code submission of different programmers for the same problem are collected, a problem-oriented data set is constructed, and a large language model is used for training to generate optimized codes. In order to improve the correctness and performance of code optimization, model merging technologies such as linear model merging and spherical interpolation are introduced. The system comprises a data set construction module, a large language model training module and a model merging module, all the modules work cooperatively, and the whole process from data set construction to optimization code generation is completed. The method provided by the invention has remarkable advantages in optimization ratio and speed improvement, can realize global algorithm innovation and efficient code performance optimization, and has a wide application prospect.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of code generation and code optimization, and in particular to a problem-oriented code optimization method and system, and an electronic device. Background Art

[0002] As large language models are increasingly used in the field of code generation, how to use these models for efficient code optimization has become an important research direction. Existing code optimization methods mainly rely on the same programmer submitting multiple times to the same problem to perform iterative local performance improvements. This method has the following limitations:

[0003] 1. Local optimization: Due to the thinking inertia of the same programmer, the optimization process is often limited to local performance improvement, making it difficult to achieve global algorithm innovation.

[0004] 2. Inefficiency: The iterative improvement process takes a long time and the optimization effect is limited.

[0005] 3. Lack of diversity: The solutions provided by a single programmer lack diversity, and it is difficult to tap into the innovative ideas of different programmers.

[0006] Therefore, a new method is urgently needed to break through the limitations of existing technologies and achieve more efficient code optimization. Summary of the invention

[0007] In order to solve the above shortcomings, the embodiments of the present application provide a problem-oriented code optimization method and system, and an electronic device, which integrates the diverse ideas of different programmers, uses a large language model for training and optimization, generates efficient code, and further improves the correctness and performance of the code through model merging technology.

[0008] According to a first aspect of an embodiment of the present application, a problem-oriented code optimization method is provided, comprising: Collect code submissions from different programmers for the same problem; Sort the code submissions by execution time and construct a problem-oriented dataset; Using a large language model to train the problem-oriented dataset and generate optimized code; The correctness and performance of the code optimization are improved through model merging technology.

[0009] Optionally, the code submissions are sorted from slow to fast according to execution time.

[0010] Optional steps to construct a problem-oriented dataset include: Collect multiple code submissions from different programmers for the same problem; Code pairs whose relative time improvement exceeds a predetermined threshold are selected to construct a problem-oriented dataset.

[0011] Optionally, the large language model is fine-tuned by using a low rank adaptation (LoRA) method.

[0012] Optionally, the model merging technology includes linear model merging and spherical interpolation (Slerp), which are used to adjust the weight ratio of model parameters to ensure the correctness and performance improvement of the optimized code.

[0013] Optionally, this also includes evaluating the correctness and performance of the generated optimized code.

[0014] Optionally, the correctness and performance of the generated optimized code is evaluated by running predetermined test cases to evaluate the performance of the optimized code.

[0015] According to a second aspect of an embodiment of the present application, a problem-oriented code optimization system is provided, including: The code collection module is used to collect code submissions from different programmers for the same problem; A dataset module, used to sort the code submissions according to execution time and construct a problem-oriented dataset; A training module, used to train the problem-oriented data set using a large language model to generate optimized code; The optimization module is used to improve the correctness and performance of the code optimization through model merging technology.

[0016] Optionally, an optimized code evaluation module is also included to evaluate the correctness and performance of the generated optimized code.

[0017] According to a third aspect of an embodiment of the present application, there is provided an electronic device, including: one or more processors; A memory for storing one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in the first aspect.

[0018] The technical solution provided by the embodiments of the present application may have the following beneficial effects: 1. Problem-oriented dataset construction: We collected and organized multiple code submissions from different programmers for the same problem, sorted the code submissions by execution time, selected code pairs whose relative time improvement exceeded a predetermined threshold, and constructed a problem-oriented dataset. This ensured that the code pairs in the dataset had significant performance improvements, overcoming the problem of low dataset quality in traditional methods and thereby improving the effectiveness of training data.

[0019] 2. Efficient training of large language models: The constructed problem-oriented dataset is used to train the large language model, and the low-rank adaptation (LoRA) method is used to fine-tune the parameters, which significantly improves the training efficiency, reduces the training time and resource consumption, overcomes the problem of inefficiency in the traditional large language model training process, and enables the model to efficiently identify and generate optimized code.

[0020] 3. Multi-model merging technology: Linear model merging and spherical interpolation (Slerp) technology are used to merge multiple large language models that have undergone different training processes, and the weight ratio of the model parameters is adjusted during the merging process, thereby ensuring the correctness of the optimized code and performance improvement, overcoming the optimization limitations that may arise from a single model, and thereby improving the diversity and reliability of the optimization effect.

[0021] 4. Global algorithm innovation: This application achieves global algorithm innovation by collecting and integrating diverse solutions from different programmers, thus breaking through the limitations of traditional methods, overcoming the problem of only being able to make local performance improvements, and thus achieving global performance improvement of the code.

[0022] 5. Significant performance improvement and high accuracy: This application adopts problem-oriented dataset construction, efficient training of large language models, multi-model merging technology and global algorithm innovation, so the experimental results show that the method of the present invention has significant advantages in optimization ratio and speed improvement, overcomes the correctness bottleneck in code optimization, and thus ensures that the generated optimized code has high correctness and stability in practical applications.

[0023] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0025] Figure 1 The figure is a flowchart of a problem-oriented code optimization method according to an exemplary embodiment.

[0026] Figure 2 The figure is a block diagram of a problem-oriented code optimization device according to an exemplary embodiment.

[0027] Figure 3 The diagram is a schematic structural diagram of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION

[0028] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.

[0029] The terms used in this application are for the purpose of describing specific embodiments only and are not intended to limit this application. The singular forms of "a", "said" and "the" used in this application and the appended claims are also intended to include plural forms unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.

[0030] It should be understood that although the terms first, second, third, etc. may be used in the present application to describe various information, these information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of the present application, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".

[0031] Figure 1 is a flowchart of a problem-oriented code optimization method according to an exemplary embodiment. Figure 1 As shown, the method is applied in a terminal and may include the following steps: S1: Collect code submissions from different programmers for the same problem; Specifically, we collect and organize multiple code submissions from different programmers for the same problem. This step ensures that code submissions cover a variety of programming styles and optimization strategies by collecting different solutions to the same problem from programmers on open source code algorithm platforms (leetcode, codeforces) and the runtime overhead of the corresponding solutions. The benefit of this design is that it can build a diverse training data set and improve the generalization ability of the model.

[0032] S2: Sort the code submissions by execution time and construct a problem-oriented dataset; Specifically, the code submissions are sorted from slow to fast according to their execution time to construct a problem-oriented dataset. The steps to construct a problem-oriented dataset include: collecting multiple code submissions from different programmers for the same problem; selecting code pairs with relative time improvement exceeding a predetermined threshold to construct a problem-oriented dataset. Execution time is an important indicator of code performance, and sorting can help identify and select code versions with significant performance improvements. The quality of the dataset is further optimized by selecting code pairs with relative time improvement exceeding a predetermined threshold (for example, 10%). The benefit of this design is that it ensures that the dataset contains code with significant performance improvements, which helps improve the training effect of the model.

[0033] S3: Using a large language model to train the problem-oriented dataset and generate optimized code; Specifically, a large language model is used to train the constructed problem-oriented dataset. The large language model is fine-tuned through the low-rank adaptation (LoRA) method to significantly improve training efficiency and reduce training time and resource consumption. The LoRA method reduces the number of model parameters through low-rank approximation, allowing the model to quickly adapt to new data while retaining performance. The benefit of this design is that it improves the efficiency and effect of training, allowing the model to generate optimized code more quickly.

[0034] S4: Improve the correctness and performance of the code optimization through model merging technology.

[0035] Specifically, multiple large language models that have undergone different training processes are merged using linear model merging and spherical interpolation (Slerp) technology. During the merging process, the correctness and performance improvement of the optimization code are ensured by adjusting the weight ratio of the model parameters. The linear model merging and Slerp method overcomes the optimization limitations that may occur in a single model by combining the advantages of multiple models. The benefit of this design is that it improves the diversity and reliability of the optimization code.

[0036] S5: Also includes: evaluating the correctness and performance of the generated optimized code.

[0037] Specifically, the correctness and performance of the generated optimized code are evaluated by running predetermined test cases. The evaluation process includes functional testing and performance testing to ensure the high correctness and stability of the optimized code in practical applications. We use the gem5 CPU simulator to measure the execution time of each program. gem5 is recognized as the gold standard for CPU simulation in academia and industry, ensuring complete determinism and reliability of results and repeatability. The benefit of this design is that the effect and quality of the optimized code are verified through a rigorous testing process to ensure its availability and reliability in practical applications.

[0038] The correctness and performance of the generated optimized code are evaluated by running predetermined test cases.

[0039] To this end, we conducted experimental tests in the gem5 CPU simulation environment; the experiment selected different large models (GPT3.5, GPT4, CodeLLama, Deepseek-Coder), and compared the test results under prompt engineering (Prompt, CoT), dataset fine-tuning from the user perspective (PIE-User), and fine-tuning from the problem perspective (PIE-Problem) proposed in this application. The experimental results are shown in Tables 1 and 2 below: Table 1: In Table 1, Instruct refers to directly asking the big model, CoT refers to guiding the big model to answer by thinking chain; PIE-User is a dataset constructed by multiple code submissions of the same programmer for the same problem; and PIE-Problem is a dataset constructed based on problem orientation proposed in this application, that is, a dataset constructed by collecting and collating multiple code submissions of different programmers for the same problem; %OPT is the correct and accelerated proportion of the entire test set, SpeedUp is the acceleration ratio, and Correct is the correct rate. As can be seen from Table 1, the opportunity problem-oriented dataset constructed by this method has obvious effects on %OPT and SpeedUp on different big models and big models with different parameter sizes.

[0040] Table 2: As can be seen from Table 2, the linear model merging (Merge-Linear) and spherical interpolation (Merge-Slerp) methods of this application have obvious improvements over other methods. Therefore, by merging and adjusting the model parameters, the correctness and performance of the optimization code can be ensured, thereby improving the overall optimization effect.

[0041] The technical solution provided by the embodiments of the present application may have the following beneficial effects: Problem-oriented dataset construction: We collected and organized multiple code submissions from different programmers for the same problem, sorted the code submissions by execution time, selected code pairs whose relative time improvement exceeded a predetermined threshold, and constructed a high-quality problem-oriented dataset. This ensured that the code pairs in the dataset had significant performance improvements, overcoming the problem of low dataset quality in traditional methods and thereby improving the effectiveness of training data.

[0042] Efficient training of large language models: The constructed problem-oriented dataset is used to train the large language model, and the low-rank adaptation (LoRA) method is used to fine-tune the parameters, which significantly improves the training efficiency, reduces the training time and resource consumption, and overcomes the inefficiency problem in the traditional large language model training process, thereby enabling the model to efficiently identify and generate optimized code.

[0043] Multi-model merging technology: Multiple large language models that have undergone different training processes are merged using linear model merging and spherical interpolation (Slerp) technology, and the weight ratio of model parameters is adjusted during the merging process, thereby ensuring the correctness and performance improvement of the optimized code, overcoming the optimization limitations that may arise from a single model, and thereby improving the diversity and reliability of the optimization effect.

[0044] Global algorithm innovation: By collecting and integrating diverse solutions from different programmers, global algorithm innovation is achieved, thus breaking through the limitations of traditional methods and overcoming the problem of only being able to make local performance improvements, thereby achieving global performance improvements for the code.

[0045] Significant performance improvement and high correctness: The above-mentioned problem-oriented dataset construction, efficient training of large language models, multi-model merging technology and global algorithm innovation are adopted. The above experimental results show (as shown in Tables 1 and 2) that the method of the present invention has significant advantages in optimization ratio and speed improvement, overcomes the correctness bottleneck in code optimization, and thus ensures that the generated optimized code has high correctness and stability in practical applications.

[0046] In summary, the present invention not only solves multiple technical problems in existing code optimization methods through a series of innovative technical means, but also achieves significant optimization effects and application value.

[0047] Corresponding to the aforementioned embodiment of the problem-oriented code optimization method, the present application also provides an embodiment of a problem-oriented code optimization device.

[0048] Figure 2 is a block diagram of a problem-oriented code optimization device according to an exemplary embodiment. Figure 2 , the device comprises:

[0049] Code collection module 1, used to collect code submissions from different programmers for the same problem; Dataset module 2, used to sort the code submissions according to execution time and construct a problem-oriented dataset; A training module 3 is used to train the problem-oriented data set using a large language model to generate optimized code; The optimization module 4 is used to improve the correctness and performance of the code optimization through model merging technology.

[0050] In one embodiment, an optimized code evaluation module is also included to evaluate the correctness and performance of the generated optimized code.

[0051] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.

[0052] For the device embodiment, since it basically corresponds to the method embodiment, the relevant parts can refer to the partial description of the method embodiment. The device embodiment described above is only schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present application scheme. A person of ordinary skill in the art can understand and implement it without paying any creative work.

[0053] Accordingly, the present application also provides an electronic device, comprising: one or more processors; a memory for storing one or more programs; when the one or more programs are executed by the one or more processors, the one or more processors implement the above-mentioned problem-oriented code optimization method. Figure 3 As shown in FIG. 1 , a hardware structure diagram of a problem-oriented code optimization device provided by an embodiment of the present invention is provided in any device with data processing capability, except Figure 3 In addition to the processor, memory, DMA controller, disk, and non-volatile memory shown, any device with data processing capabilities in which the apparatus in the embodiment is located may also include other hardware, which will not be described in detail, generally based on the actual functions of the device with data processing capabilities.

[0054] Accordingly, the present application also provides a computer-readable storage medium on which computer instructions are stored, and when the instructions are executed by the processor, the problem-oriented code optimization method as described above is implemented. The computer-readable storage medium can be an internal storage unit of any device with data processing capabilities described in any of the aforementioned embodiments, such as a hard disk or a memory. The computer-readable storage medium can also be an external storage device, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), an SD card, a flash card (Flash Card), etc. equipped on the device. Furthermore, the computer-readable storage medium can also include both an internal storage unit and an external storage device of any device with data processing capabilities. The computer-readable storage medium is used to store the computer program and other programs and data required by any device with data processing capabilities, and can also be used to temporarily store data that has been output or is to be output.

[0055] Those skilled in the art will readily appreciate other embodiments of the present application after considering the description and practicing the contents disclosed herein. The present application is intended to cover any modification, use or adaptation of the present application, which follows the general principles of the present application and includes common knowledge or customary techniques in the art that are not disclosed in the present application. The description and examples are intended to be exemplary only, and the true scope and spirit of the present application are indicated by the claims.

[0056] It should be understood that the present application is not limited to the precise structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.

Claims

1. A problem-oriented code optimization method, characterized in that: include: Collect code submissions from different programmers for the same problem; Sort the code submissions by execution time and construct a problem-oriented dataset; Using a large language model to train the problem-oriented dataset and generate optimized code; The correctness and performance of the code optimization are improved through model merging technology.

2. The problem-oriented code optimization method according to claim 1, characterized in that: The code submissions are sorted from slowest to fastest according to execution time.

3. The problem-oriented code optimization method according to claim 1, characterized in that: The steps to construct a problem-oriented dataset include: Collect multiple code submissions from different programmers for the same problem; Code pairs whose relative time improvement exceeds a predetermined threshold are selected to construct a problem-oriented dataset.

4. The problem-oriented code optimization method according to claim 1, characterized in that: The large language model is fine-tuned in parameters through a low-rank adaptation method.

5. The problem-oriented code optimization method according to claim 1, characterized in that: The model merging technology includes linear model merging and spherical interpolation, which are used to adjust the weight ratio of model parameters to ensure the correctness and performance improvement of the optimized code.

6. The problem-oriented code optimization method according to claim 1, characterized in that: It also includes evaluating the correctness and performance of the generated optimized code.

7. The problem-oriented code optimization method according to claim 6, characterized in that: The correctness and performance of the generated optimized code are evaluated by running predetermined test cases.

8. A problem-oriented code optimization system, characterized in that: include: The code collection module is used to collect code submissions from different programmers for the same problem; A dataset module, used to sort the code submissions according to execution time and construct a problem-oriented dataset; A training module, used to train the problem-oriented data set using a large language model to generate optimized code; The optimization module is used to improve the correctness and performance of the code optimization through model merging technology.

9. The problem-oriented code optimization system according to claim 8, characterized in that: It also includes an optimized code evaluation module for evaluating the correctness and performance of the generated optimized code.

10. An electronic device, characterized in that: include: one or more processors; A memory for storing one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 8.

Citation Information

Patent Citations

  • Code optimization method and device based on artificial intelligence and storage medium

    CN117492760A

  • Method and device for analyzing application performance, computer equipment and storage medium

    CN118227434A