用大模型+知识库,自动优化Python代码运行效率
FasterPy: An LLM-based Code Execution Efficiency Optimization Framework
- 结合检索增强生成与低秩适配,提升代码优化能力
- 在PIE基准上多项指标超越现有模型
- 低成本可扩展,适合开发者快速改进代码性能
代码常存在性能缺陷,传统基于规则的优化方法依赖人工设计规则,成本高且适用性有限。近年来,基于机器学习的方法虽能从标注代码和性能数据中学习优化启发式策略,但需特定程序表示和精心构建的数据集,开发成本高、难扩展。随着大语言模型(LLM)在代码生成方面表现突出,我们提出FasterPy框架,利用由已有性能优化代码对及对应性能测量构建的知识库,结合检索增强生成(RAG)与低秩适配(LoRA),高效优化Python代码执行效率。在Performance Improving Code Edits(PIE)基准上的实验表明,该方法在多个指标上优于现有模型。工具与实验结果已开源:https://github.com/WuYue22/fasterpy。
原文摘要 · Abstract (English)
Code often suffers from performance bugs. These bugs necessitate the research and practice of code optimization. Traditional rule-based methods rely on manually designing and maintaining rules for specific performance bugs (e.g., redundant loops, repeated computations), making them labor-intensive and limited in applicability. In recent years, machine learning and deep learning-based methods have emerged as promising alternatives by learning optimization heuristics from annotated code corpora and performance measurements. However, these approaches usually depend on specific program representations and meticulously crafted training datasets, making them costly to develop and difficult to scale. With the booming of Large Language Models (LLMs), their remarkable capabilities in code generation have opened new avenues for automated code optimization. In this work, we proposed FasterPy, a low-cost and efficient framework that adapts LLMs to optimize the execution efficiency of Python code. FasterPy combines Retrieval-Augmented Generation (RAG), supported by a knowledge base constructed from existing performance-improving code pairs and corresponding performance measurements, with Low-Rank Adaptation (LoRA) to enhance code optimization performance. Our experimental results on the Performance Improving Code Edits (PIE) benchmark demonstrate that our method outperforms existing models on multiple metrics. The FasterPy tool and the experimental results are available at https://github.com/WuYue22/fasterpy.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。