CoditT5: Pretraining for Source Code and Natural Language Editing

Jiyang Zhang, Sheena Panthaplackel, Pengyu Nie, Junyi Jessy Li, Milos Gligoric

TL;DR

CoditT5 is a large language model specifically designed for software editing tasks like bug fixing and code review. Unlike standard models that try to rewrite code from scratch, CoditT5 uses a novel pretraining objective that forces the model to first generate an explicit "edit plan" (a sequence of insertions, deletions, and replacements) before producing the final code, leading to significantly higher precision and state-of-the-art performance.

What Problem Does This Paper Address?

While Large Language Models (LLMs) like CodeT5 and PLBART are excellent at generating code from natural language descriptions, they often stumble when the task is to edit existing code.

The authors identify a specific failure mode: standard models are trained on "denoising" objectives that prioritize sequence probability over structural change. Consequently, when asked to fix a bug or update a comment, these models often either naively copy the input (performing no edit at all) or introduce irrelevant, sweeping changes that ignore the original context. In software engineering, edits are usually localized and intentional; standard LLMs lack a mechanism to reason explicitly about where and how these localized changes should occur.

Methods

The researchers built CoditT5, a 223M-parameter encoder-decoder model based on the T5 architecture. The core innovation lies in its edit-based pretraining objective.

  1. The Edit Plan: Instead of mapping a "buggy" input directly to a "fixed" output, the model is trained to generate a two-part sequence: [Edit Plan] <s> [Target Sequence].
  2. The Edit Plan uses special tokens (e.g., <ReplaceOld>, <Insert>, <Delete>) to describe the delta between the input and output.
  3. The Target Sequence is the final, edited version of the code or text.
  4. Pretraining Strategy: The authors used a "noising function" on massive amounts of raw code. They would take a clean snippet, randomly delete or mask spans to create a "corrupted" version, and then train the model to recover the original by first outlining the necessary edits.
  5. Reranking: The authors discovered that edit-based models (precise but sometimes less fluent) and standard generation models (fluent but less precise) are complementary. They implemented a reranking strategy where candidates from CoditT5 are scored by a standard CodeT5 model (and vice versa) to select the final output that is both edit-accurate and linguistically sound.

Datasets

  • Pretraining: The model was pretrained on the CodeSearchNet dataset, encompassing 6.1 million code snippets and 1.9 million natural language comments across six programming languages (Java, Python, Ruby, PHP, Go, and JavaScript).
  • Downstream Evaluation:
    • Comment Updating: Updating Java comments to match code changes (using a dataset from Panthaplackel et al.).
  • Bug Fixing: The B2F (Bug2Fix) Small and Medium datasets.
  • Automated Code Review: A dataset of Java methods paired with reviewer comments prescribing specific changes.

Key Insights and Ideas

  • Explicit Reasoning Matters: By forcing the model to output an edit plan, the model "stops to think" about the transformation before committing to the final tokens. This drastically reduces the "copying" problem (where models just repeat the buggy input).
  • The "Content Planning" Analogy: The authors draw inspiration from classical NLP content planning. The edit plan acts as a structural skeleton that guides the decoder, ensuring the final output stays grounded in the requested changes.
  • Robustness to Plan Errors: Interestingly, even if the model generates a slightly ambiguous or erroneous edit plan, the decoder is often powerful enough to "fix" the plan during the generation of the target sequence, resulting in correct code.
  • Synergy through Reranking: The most significant performance gains came from combining CoditT5 with standard models. This suggests that "editing" and "generating" are distinct skills that benefit from different training objectives.

Key Results

  • Reduced Copying: In the Comment Updating task, standard models like PLBART copied the input without editing up to 34% of the time. CoditT5 reduced this to just 5.7%.
  • Superior Accuracy: CoditT5 outperformed SOTA models in xMatch (Exact Match) across all three tasks.
  • In Bug Fixing (B2F-Small), CoditT5 achieved an xMatch of 37.52, compared to CodeT5's 34.81.
  • When using the reranking strategy, performance jumped even further (e.g., reaching 40.22 xMatch on B2F-Small), setting new state-of-the-art benchmarks.
  • Generalizability: Although pretrained on six languages, the model showed strong zero-shot/few-shot transfer to the specific nuances of the Java-based downstream tasks.

Limitations and Future Work

  • Language Scope: While pretrained on six languages, the downstream evaluations were primarily focused on Java. The authors acknowledge the need to verify performance on other languages like Python or Go.
  • Data Contamination: As with all models trained on open-source data (GitHub), there is a risk that some test examples were seen during pretraining, though the authors note this is a general challenge in the field.
  • Complexity of Edits: The current edit plan format is optimized for localized changes. Extremely large-scale refactorings or architectural changes might require more complex plan representations.
  • Future Direction: The authors suggest exploring more sophisticated ways to integrate the edit-based and generation-based models beyond simple reranking, perhaps through joint training or multi-task learning.

Summary updated

BibTeX
@inproceedings{zhang2022coditt,
  author = {Jiyang Zhang and Sheena Panthaplackel and Pengyu Nie and Junyi Jessy Li and Milos Gligoric},
  title = {{CoditT5: Pretraining for Source Code and Natural Language Editing}},
  booktitle = {ASE},
  year = {2022},
  url = {https://openreview.net/forum?id=h36qOz7OKJ},
}

Loading related content...

Figure