As the use of large language models (LLMs) for code generation becomes more prevalent in software development, it is critical to enhance both the efficiency and correctness of the generated code. Existing methods and models primarily focus on the correctness of LLM-generated code, ignoring efficiency. In this work, we present Effi-Code, an approach to enhancing code generation in LLMs that can improve both efficiency and correctness. We introduce a Self-Optimization process based on Overhead Profiling that leverages open-source LLMs to generate a high-quality dataset of correct and efficient code samples. This dataset is then used to fine-tune various LLMs. Our method involves the iterative refinement of generated code, guided by runtime performance metrics and correctness checks. Extensive experiments demonstrate that models fine-tuned on the Effi-Code show significant improvements in both code correctness and efficiency across task types. For example, the pass@1 of DeepSeek-Coder-6.7B-Instruct generated code increases from \textbf{43.3\%} to \textbf{76.8\%}, and the average execution time for the same correct tasks decreases by \textbf{30.5\%}. Effi-Code offers a scalable and generalizable approach to improving code generation in AI systems, with potential applications in software development, algorithm design, and computational problem-solving. The source code of Effi-Code was released in \url{https://github.com/huangd1999/Effi-Code}.
翻译:随着大型语言模型(LLM)在软件开发中用于代码生成的日益普及,提升生成代码的效率和正确性变得至关重要。现有方法和模型主要关注LLM生成代码的正确性,而忽视了效率。在本工作中,我们提出了Effi-Code,一种增强LLM代码生成能力的方法,能够同时提升效率和正确性。我们引入了一种基于开销剖析的自优化过程,该过程利用开源LLM生成一个包含正确且高效代码样本的高质量数据集。该数据集随后被用于微调各种LLM。我们的方法涉及在运行时性能指标和正确性检查的指导下,对生成的代码进行迭代优化。大量实验表明,在Effi-Code数据集上微调的模型,在不同任务类型上,其代码正确性和效率均有显著提升。例如,DeepSeek-Coder-6.7B-Instruct生成代码的pass@1从 \textbf{43.3\%} 提升至 \textbf{76.8\%},相同正确任务的平均执行时间降低了 \textbf{30.5\%}。Effi-Code为改进AI系统中的代码生成提供了一种可扩展且可泛化的方法,在软件开发、算法设计和计算问题求解中具有潜在的应用前景。Effi-Code的源代码已发布于 \url{https://github.com/huangd1999/Effi-Code}。