Tea: Program Repair Using Neural Network Based on Program Information Attention Matrix

Wenshuo Wang, Chen Wu, Liang Cheng, Yang ZhangarXiv 2021

Abstract

The advance in machine learning (ML)-driven natural language process (NLP) points a promising direction for automatic bug fixing for software programs, as fixing a buggy program can be transformed to a translation task. While software programs contain much richer information than one-dimensional natural language documents, pioneering work on using ML-driven NLP techniques for automatic program repair only considered a limited set of such information. We hypothesize that more comprehensive information of software programs, if appropriately utilized, can improve the effectiveness of ML-driven NLP approaches in repairing software programs. As the first step towards proving this hypothesis, we propose a unified representation to capture the syntax, data flow, and control flow aspects of software programs, and devise a method to use such a representation to guide the transformer model from NLP in better understanding and fixing buggy programs. Our preliminary experiment confirms that the more comprehensive information of software programs used, the better ML-driven NLP techniques can perform in fixing bugs in these programs.

Introduction

Automated Program Repair (APR) is often considered the "holy grail" of software maintenance, aiming to drastically reduce the costs associated with identifying and fixing software bugs. Recently, researchers have begun treating bug fixing as a neural machine translation task: "translating" buggy code into its corrected version using Deep Learning architectures like the Transformer.

However, a significant limitation of standard NLP models is that they treat code as a one-dimensional sequence of tokens. Unlike natural language, software is inherently multi-dimensional and structural. A variable's state may depend on a value defined many lines prior (Data Flow), and logical branching determines whether specific blocks of code ever execute (Control Flow). Existing models often ignore these structural relationships, which limits their ability to grasp the underlying logic of a program.

Methodology: The Tea Framework

To bridge the gap between sequence-based models and the structural reality of software, we propose Tea (Transformer Code Attention). Tea modifies the standard Transformer architecture to be "code-aware" by injecting structural information directly into the model's attention mechanism.

The Program Information Attention Matrix (PIAM)

The core of the Tea framework is the PIAM, a multi-dimensional matrix that explicitly maps out the program's internal relationships. It captures three primary dimensions:

  1. Syntax (AST): The hierarchical structure of the code (e.g., identifying which "if" statement a specific variable belongs to).
  2. Control Flow (CFG): The possible execution paths within the program.
  3. Data Flow (DFG): The movement of data between variables and functions.

Revised Attention Mechanism

In a standard Transformer, attention is calculated based on the relationship between tokens in a sequence. Tea revises this formula to incorporate the PIAM, allowing the model to prioritize structurally relevant tokens regardless of their distance in the text.

The Tea model architecture integrates structural information (AST, CFG, DFG) into the Transformer's attention mechanism via the PIAM.

The modified attention formula is: $$Attention(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}} \cdot \alpha + W \cdot PIAM \cdot (1 - \alpha)\right)V$$ Here, $\alpha$ is a learnable parameter that enables the model to dynamically balance raw sequence data with the structural facts provided by the PIAM, such as the Nearest Common Ancestor in a syntax tree.

Experimental Evaluation

We evaluated Tea using the Tufano dataset, a standard benchmark in the program repair community consisting of real-world buggy and fixed code snippets. To test our hypothesis that "more information equals better repairs," we compared a baseline Transformer against three versions of Tea with increasing structural granularity:

  • TEA1: Basic structural neighbor relationships.
  • TEA2: Intermediate structural features.
  • TEA3: Comprehensive path-based structural information.

Results and Discussion

The results confirm that structural awareness leads to significantly more effective bug fixes. As the granularity of the PIAM increased, the model's performance improved steadily.

Comparison of the number of bugs fixed by the baseline Transformer and the three TEA variants.

Key performance metrics include:

  • Baseline Transformer: Fixed 345 bugs.
  • TEA1: Fixed 417 bugs.
  • TEA2: Fixed 455 bugs.
  • TEA3: Fixed 484 bugs.

By providing the model with a comprehensive understanding of the program's internal relationships, TEA3 achieved a ~40% improvement in bug-fixing capability over the vanilla Transformer model. This suggests that the "code-as-text" paradigm has a performance ceiling that can only be broken by incorporating structural awareness.

Conclusion and Future Work

The Tea framework demonstrates that structural injection is a powerful method for improving ML-driven program repair. While these preliminary results are promising, future work will involve scaling the model to larger datasets and refining the static analysis required to build the PIAM, especially for code that is too broken to be easily parsed. We also intend to conduct broader head-to-head comparisons with other state-of-the-art repair methods to further establish Tea's advantages.

Summary updated

BibTeX
@misc{wang2021tea,
  author = {Wenshuo Wang and Chen Wu and Liang Cheng and Yang Zhang},
  title = {{Tea: Program Repair Using Neural Network Based on Program Information Attention Matrix}},
  year = {2021},
  eprint = {2107.08262},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2107.08262},
}
Figure