Papers
Topics
Authors
Recent
Gemini 2.5 Flash
Gemini 2.5 Flash
41 tokens/sec
GPT-4o
59 tokens/sec
Gemini 2.5 Pro Pro
41 tokens/sec
o3 Pro
7 tokens/sec
GPT-4.1 Pro
50 tokens/sec
DeepSeek R1 via Azure Pro
28 tokens/sec
2000 character limit reached

SpecTra: Enhancing the Code Translation Ability of Language Models by Generating Multi-Modal Specifications (2405.18574v2)

Published 28 May 2024 in cs.SE

Abstract: LLMs are increasingly being used for the task of automated code translation, which has important real-world applications. However, most existing approaches use only the source code of a program as an input to an LLM, and do not consider the different kinds of specifications that can be extracted from a program. In this paper, we propose SpecTra, a multi-stage approach that uses a novel self-consistency filter to first generate high-quality static specifications, test cases, and natural language descriptions from a given program, and then uses these along with the source code to improve the quality of LLM-generated translations. We evaluate SpecTra on three code translation tasks - C to Rust, C to Go, and JavaScript to TypeScript - and show that it can enhance the performance of six popular LLMs on these tasks by up to 10 percentage points and a relative improvement of 26\%. Our research suggests that generating high-quality specifications could be a promising and efficient way to improve the performance of LLMs for code translation. We make our code and data available, anonymized for review.

Overview of SpecTra: Enhancing Code Translation with Multi-Modal Specifications

The paper "SpecTra: Enhancing the Code Translation Ability of LLMs by Generating Multi-Modal Specifications" introduces a novel methodology aimed at improving the performance of LLMs in automated code translation tasks. The authors address a critical gap wherein most existing techniques rely solely on the program's source code, neglecting the rich potential of program specifications to inform translation tasks.

Methodology

The core contribution of the paper is the SpecTra approach, a multi-stage methodology that leverages a combination of static specifications, test cases, and natural language descriptions to augment LLM-based code translations. The process is as follows:

  1. Specification Generation: SpecTra begins by generating multiple candidate specifications from the given code, utilizing a self-consistency filter for validation. The approach creates three types of specifications:
    • Static Specifications: Structured representations of the program's behavior.
    • Input-Output Specifications: Specific examples of input-output behavior.
    • Descriptions: Natural language summaries of the code functionality.
  2. Specification Validation: The generated specifications are validated for self-consistency. Static and descriptive specifications are verified by regenerating the source code and comparing it to the original using test cases. For input-output specifications, the program is executed to assess correctness.
  3. Specification-Guided Translation: Once validated, each type of specification is integrated into the translation task sequentially. This process is designed to combine the idiomatic expressiveness of LLM-generated code with the functional accuracy traditionally associated with transpilers.

Evaluation and Results

SpecTra was evaluated on three code translation tasks—converting C to Rust, C to Go, and JavaScript to TypeScript—using six popular LLMs. The results demonstrated significant improvements, with up to a 10 percentage point increase and a relative improvement of 26% over baseline models. This was particularly evident in tasks involving translations where initial specification limitations were overcome by integrating multiple modalities.

Implications and Future Directions

The implications of this research are twofold. Practically, it offers a method to harness specifications for more accurate and idiomatic code translations, potentially reducing the technical debt associated with maintaining legacy code. Theoretically, it provides insights into how multi-modal information can be utilized to enhance the capabilities of LLMs beyond traditional code-related applications.

For future research, the paper suggests exploring the generation of specifications in formal languages or assert statements for automatic cross-verification. Another promising direction is to evaluate the utility of these specifications in other code-related tasks such as debugging or code synthesis.

Conclusion

SpecTra represents an innovative step towards improving automated code translation by integrating multi-modal specifications into existing LLM frameworks. The proposed method not only enhances the quality of translations but also bridges the gap between traditional rule-based correctness and LLM-driven idiomatic coding practices. As AI continues to evolve, methodologies like SpecTra could lead to more reliable and maintainable software systems.

Definition Search Book Streamline Icon: https://streamlinehq.com
References (22)
  1. Summarize and generate to back-translate: Unsupervised translation of programming languages. arXiv preprint arXiv:2205.11116, 2022.
  2. Codet: Code generation with generated tests. arXiv preprint arXiv:2207.10397, 2022.
  3. Formalizing natural language intent into program specifications via large language models. arXiv preprint arXiv:2310.01831, 2023.
  4. Stuart I Feldman. A fortran to c converter. In ACM SIGPLAN Fortran Forum, volume 9, pages 21–22. ACM New York, NY, USA, 1990.
  5. Galois. C2Rust, 08 2018. URL https://galois.com/blog/2018/08/c2rust/.
  6. Code structure–guided transformer for source code summarization. ACM Transactions on Software Engineering and Methodology, 32(1):1–32, 2023.
  7. M2ts: Multi-scale multi-modal approach based on transformer for source code summarization. In Proceedings of the 30th IEEE/ACM International Conference on Program Comprehension, pages 24–35, 2022.
  8. Source code summarization with structural relative position guided transformer. In 2022 IEEE International Conference on Software Analysis, Evolution and Reengineering (SANER), pages 13–24. IEEE, 2022.
  9. Google. Rust in the Android platform, 4 2021a. URL https://security.googleblog.com/2021/04/rust-in-android-platform.html.
  10. Google. Rust in the Linux kernel, 4 2021b. URL https://security.googleblog.com/2021/04/rust-in-linux-kernel.html.
  11. Competition-level code generation with alphacode. Science, 378(6624):1092–1097, 2022.
  12. Is your code generated by chatgpt really correct? rigorous evaluation of large language models for code generation. In A. Oh, T. Neumann, A. Globerson, K. Saenko, M. Hardt, and S. Levine, editors, Advances in Neural Information Processing Systems, volume 36, pages 21558–21572. Curran Associates, Inc., 2023. URL https://proceedings.neurips.cc/paper_files/paper/2023/file/43e9d647ccd3e4b7b5baab53f0368686-Paper-Conference.pdf.
  13. Christopher Mims. The invisible $1.52 trillion problem: Clunky old software. The Wall Street Journal, March 2024. URL https://www.wsj.com/tech/personal-tech/the-invisible-1-52-trillion-problem-clunky-old-software-f5cbba27.
  14. Beyond accuracy: Evaluating self-consistency of code large language models with identitychain. arXiv preprint arXiv:2310.14053, 2023.
  15. Lost in translation: A study of bugs introduced by large language models while translating code. In Proceedings of the IEEE/ACM 46th International Conference on Software Engineering, pages 1–13, 2024.
  16. Codenet: A large-scale ai for code dataset for learning a diversity of coding tasks. arXiv preprint arXiv:2105.12655, 2021.
  17. Unsupervised translation of programming languages. Advances in neural information processing systems, 33:20601–20611, 2020.
  18. Leveraging automated unit tests for unsupervised code translation. arXiv preprint arXiv:2110.06773, 2021.
  19. Ast-transformer: Encoding abstract syntax trees efficiently for code summarization. In 2021 36th IEEE/ACM International Conference on Automated Software Engineering (ASE), pages 1193–1195. IEEE, 2021.
  20. Code summarization with structure-induced transformer. In Chengqing Zong, Fei Xia, Wenjie Li, and Roberto Navigli, editors, Findings of the Association for Computational Linguistics: ACL-IJCNLP 2021, pages 1078–1090, Online, August 2021. Association for Computational Linguistics. doi: 10.18653/v1/2021.findings-acl.93. URL https://aclanthology.org/2021.findings-acl.93.
  21. Exploring and unleashing the power of large language models in automated code translation. arXiv preprint arXiv:2404.14646, 2024.
  22. Parsel: Algorithmic reasoning with language models by composing decompositions. Advances in Neural Information Processing Systems, 36:31466–31523, 2023.
User Edit Pencil Streamline Icon: https://streamlinehq.com
Authors (3)
  1. Vikram Nitin (8 papers)
  2. Baishakhi Ray (88 papers)
  3. Rahul Krishna (28 papers)
Citations (3)
X Twitter Logo Streamline Icon: https://streamlinehq.com