arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2310.01602cs.SEcs.AI
收藏

CAT-LM: 在对齐的代码与测试上训练语言模型

CAT-LM: Training Language Models on Aligned Code And Tests

Carnegie Mellon University(卡内基梅隆大学)

Nikitha Rao, Kush Jain, Uri Alon, Claire Le Goues, Vincent J. Hellendoorn

AI总结:

针对现有代码语言模型生成测试时忽略被测代码上下文的问题,提出27亿参数的CAT-LM模型,通过代码-测试映射预训练信号和8192 token长上下文,在测试生成效果上超越更大规模通用模型及专用测试模型。

摘要:

测试是软件开发流程中不可或缺的一环。然而,编写测试十分耗时,因此常常被忽视。EvoSuite等经典测试生成工具通过优化覆盖率来生成行为测试套件,但生成的测试往往难以理解。在代码上训练的语言模型能够生成与人类编写的代码高度相似的代码,但现有模型遵循自然语言处理的标准范式,被训练为单独生成每个文件,因此在生成测试文件时未能考虑被测代码的上下文。在本研究中,我们提出对齐代码与测试语言模型(Aligned Code And Tests Language Model,CAT-LM),这是一种拥有27亿参数的GPT风格语言模型,在Python和Java项目语料库上训练。我们采用了一种新颖的预训练信号,当存在代码与测试文件的对应关系时,该信号会显式考虑二者之间的映射关系。我们还将输入的最大序列长度大幅提升至8192个token,是典型代码生成模型的4倍,以确保模型在生成测试代码时能够获取代码上下文。我们分析了该模型在实际应用中的效用,结果表明,结合过滤(例如按可编译性、覆盖率过滤)的采样策略能够让模型高效生成测试,这些测试的覆盖率与开发人员编写的测试相当,同时还能贴合开发人员的编写风格。通过利用代码上下文,CAT-LM生成的有效测试甚至多于使用更多数据训练的、规模大得多的语言模型(CodeGen 16B和StarCoder),并且在测试补全任务上大幅优于最新的测试专用模型TeCo。总体而言,本研究凸显了在训练代码语言模型时融入软件领域特定洞见的重要性,为更强大的自动化测试生成铺平了道路。

英文摘要:

Testing is an integral part of the software development process. Yet, writing tests is time-consuming and therefore often neglected. Classical test generation tools such as EvoSuite generate behavioral test suites by optimizing for coverage, but tend to produce tests that are hard to understand. Language models trained on code can generate code that is highly similar to that written by humans, but current models are trained to generate each file separately, as is standard practice in natural language processing, and thus fail to consider the code-under-test context when producing a test file. In this work, we propose the Aligned Code And Tests Language Model (CAT-LM), a GPT-style language model with 2.7 Billion parameters, trained on a corpus of Python and Java projects. We utilize a novel pretraining signal that explicitly considers the mapping between code and test files when available. We also drastically increase the maximum sequence length of inputs to 8,192 tokens, 4x more than typical code generation models, to ensure that the code context is available to the model when generating test code. We analyze its usefulness for realistic applications, showing that sampling with filtering (e.g., by compilability, coverage) allows it to efficiently produce tests that achieve coverage similar to ones written by developers while resembling their writing style. By utilizing the code context, CAT-LM generates more valid tests than even much larger language models trained with more data (CodeGen 16B and StarCoder) and substantially outperforms a recent test-specific model (TeCo) at test completion. Overall, our work highlights the importance of incorporating software-specific insights when training language models for code and paves the way to more powerful automated test generation.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑