Skip to main content
QUICK REVIEW

[论文解读] Verified Code Transpilation with LLMs

Sahil Bhatia, Jie Qiu|arXiv (Cornell University)|Jun 5, 2024
Natural Language Processing Techniques被引用 4
一句话总结

该论文提出LLMLift,一种基于大语言模型(LLM)的框架,用于经过形式化验证的代码转译,能够在自动生成功能等价的领域特定语言(DSL)代码的同时,自动生成正确性证明。通过利用LLM推理将源代码转换为基于Python的中间表示(IR),再进一步转换为目标DSL语法,LLMLift在23个张量处理基准测试中实现了100%的正确率,其准确率和速度均优于符号化工具Tenspiler,且无需依赖领域特定的启发式规则。

ABSTRACT

Domain-specific languages (DSLs) are integral to various software workflows. Such languages offer domain-specific optimizations and abstractions that improve code readability and maintainability. However, leveraging these languages requires developers to rewrite existing code using the specific DSL's API. While large language models (LLMs) have shown some success in automatic code transpilation, none of them provide any functional correctness guarantees on the transpiled code. Another approach for automating this task is verified lifting, which relies on program synthesis to find programs in the target language that are functionally equivalent to the source language program. While several verified lifting tools have been developed for various application domains, they are specialized for specific source-target languages or require significant expertise in domain knowledge to make the search efficient. In this paper, leveraging recent advances in LLMs, we propose an LLM-based approach (LLMLift) to building verified lifting tools. We use the LLM's capabilities to reason about programs to translate a given program into its corresponding equivalent in the target language. Additionally, we use LLMs to generate proofs for functional equivalence. We develop lifting-based compilers for {\em four different} DSLs targeting different application domains. Our approach not only outperforms previous symbolic-based tools in both the number of benchmarks transpiled and transpilation time, but also requires significantly less effort to build.

研究动机与目标

  • 为解决手动重写代码以适配领域特定语言(DSL)所面临的错误频发且耗时的问题。
  • 通过大语言模型(LLMs)实现从通用编程语言到DSL的自动化、正确性保障的代码转译。
  • 消除对先前已验证转译工具中所依赖的领域特定启发式规则或复杂规则系统的需求。
  • 证明大语言模型能够联合生成正确的DSL代码以及功能等价性的形式化证明。
  • 构建可扩展、可重用的转译编译器,适用于多种DSL,且工程投入极少。

提出的方法

  • LLMLift采用两阶段方法:首先,LLM将源代码翻译为基于Python的中间表示(IR),该IR语义上捕获了DSL操作符的语义。
  • 其次,LLM利用编码了DSL语义的提示词,通过重写规则从IR生成最终的DSL代码。
  • LLM被提示同时生成源代码与转译后代码之间功能等价性的证明,使用不变量和验证条件作为依据。
  • 形式化验证通过后端验证器执行,以检查生成的证明和代码的正确性。
  • 该方法依赖提示工程,引导LLM在程序语义推理方面生成正确且可验证的输出。
  • 该框架应用于四个真实世界的DSL:TACO(张量处理)、DSP(数字信号处理)、BLAS和UTDSP,并在领域特定基准上进行了评估。
Figure 2 : A high-level overview of our LLMLift framework for building verified lifting-based tools.
Figure 2 : A high-level overview of our LLMLift framework for building verified lifting-based tools.

实验结果

研究问题

  • RQ1大语言模型能否在单一流程中有效生成功能正确的DSL代码以及等价性的形式化证明?
  • RQ2基于LLM的方法在转译准确率和性能方面是否优于符号化已验证转译工具?
  • RQ3LLMLift是否能在不依赖自定义领域特定启发式规则或优化规则的情况下实现高正确率?
  • RQ4与分步或两阶段提示法相比,联合生成证明与代码在可靠性方面表现如何?
  • RQ5大语言模型在仅需极少配置调整的情况下,能否在多种不同的DSL之间实现良好泛化?

主要发现

  • LLMLift在张量处理领域的23个基准测试中全部成功转译,准确率达到100%,表现与最先进的符号化已验证转译工具Tenspiler相当或更优。
  • LLMLift在未启用任何领域特定优化的情况下解决了全部23个基准测试,而Tenspiler在启用优化时解决了全部23个,但在关闭优化时失败了18个。
  • LLMLift的平均转译时间为每项基准95.89秒,即使在启用所有优化的情况下,仍优于Tenspiler的115.14秒。
  • 单阶段提示法(同时生成证明和不变量)成功解决了20个基准,表明两阶段方法通过允许模型利用生成的程序状态来指导不变量构建,从而提升了可靠性。
  • LLMLift在所有评估的DSL中均实现了100%的准确率,涵盖TACO、DSP、BLAS、mathfu和UTDSP,并在多个基准套件中进行了验证。
  • 与先前的已验证转译工具相比,该方法显著减少了工程投入,因其不依赖手工编写的规则或大规模并行语料库进行训练。
Figure 3 : End-to-End Lifting Example
Figure 3 : End-to-End Lifting Example

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。