Automatically Testing Functional Properties of Code Translation Models

Hasan Ferit Eniser, Valentin Wüstholz, Maria Christakis

TL;DR

This paper introduces the first automated, property-based testing framework for code translation models, allowing users to specify and verify functional correctness beyond simple syntax. They also developed a novel search procedure that leverages these properties to find better code translations without retraining the model.

What Problem Does This Paper Address?

Large language models (LLMs) are increasingly used for code translation (transpilation), a process that can significantly boost developer productivity. However, a major concern is the correctness of the generated code. Existing methods rely on manually crafted or automatically generated test suites for individual functions, which don't fully capture the functional properties of the translation models themselves or guarantee correctness across a broader range of scenarios. This paper addresses the need for a more robust and comprehensive way to test and improve code translation models by focusing on verifying specific functional properties.

Methods

The core of this work is an extension of the NOMOS framework, which is designed for expressing and automatically testing functional-correctness properties of machine learning models. The authors adapted NOMOS to handle code translation models by:

  • Formalizing k-safety properties: They introduced a formal language to express properties that can range from purely syntactic (e.g., matching the number of loops) to purely semantic (e.g., producing the same output for the same input). These properties can involve multiple model executions (k > 1) to capture more complex behaviors.
  • Domain-specific functions: They added new functions for program transformation (e.g., renaming parameters, adding conditionals) and program inspection (e.g., counting loops, checking compilation, retrieving return values) to enable the expression of these properties.
  • Metamorphic Testing: The testing procedure is built upon metamorphic testing principles, where inputs are transformed, and the relationship between original and transformed outputs is checked against the specified property.
  • Property-Guided Search: For users who want to improve a specific translation rather than the entire model, they developed a search procedure. This procedure iteratively queries the translation model with slightly varied parameters (like temperature) and uses the property violations as a fitness function to guide the search towards better translations.

Datasets

The paper uses the benchmark set introduced in TRANSCODER (Rozière et al. 2020), which consists of 545 solutions to LeetCode problems implemented in Java, C++, and Python. Each program in this set comes with approximately 10 tests that specify expected output values for given inputs.

Key Insights and Ideas

  • Beyond Syntactic Correctness: The ability to define and test semantic properties (like matching return values) is crucial for ensuring functional correctness, going beyond just whether the code compiles or has the same structure.
  • k-Safety for Nuance: Using k-safety properties (where k > 1) allows for more sophisticated testing by comparing multiple model executions, which is essential for detecting subtle bugs that might be missed by single-execution tests.
  • Testing Models, Not Just Translations: The framework tests the models themselves for adherence to properties, rather than just evaluating the output of a single translation. This provides a more fundamental assessment of model quality.
  • Search for Targeted Improvement: The property-guided search offers a practical way for users to improve specific code translations without the need for expensive model retraining, by intelligently exploring the model's parameter space.
  • Syntactic vs. Semantic Properties: Models generally perform better on syntactic properties than semantic ones, suggesting that semantic correctness remains a significant challenge for current code translation models.

Key Results

  • Effective Property Violation Detection: The testing procedure was highly effective, detecting thousands of violations across 38 properties for four state-of-the-art code translation models (TRANSCODER, DOBF, TRANSCODER-IR, and STARCODER). For example, when using a beam size of 1, between 27 and 37 properties were violated for each model.
  • Search Improves Translations: The property-guided search procedure significantly increased the number of "passing programs" (those with no property violations) from 66 to 84 for TRANSCODER and from 71 to 83 for DOBF within 20 search iterations. It also reduced the number of violated properties by almost half.
  • Search Excels at Semantic Properties: The search procedure was particularly effective at decreasing violations of semantic properties (like compiles and retValues), which are generally harder for models to satisfy.
  • Performance Varies by Target Language: TRANSCODER performed better for Java-to-C++ translations than Java-to-Python, likely due to the closer relationship between Java and C++.

Limitations and Future Work

  • Computational Cost: While the framework is efficient, testing and especially the search procedure can be computationally intensive, requiring significant budgets. The paper notes that increasing beam size or search iterations increases running time.
  • Generalizability of Search: The search procedure is currently applied to a single program at a time. Future work could explore extending this to optimize translations for entire codebases.
  • Future Directions: The authors plan to transfer their approach to other domains to enforce quality criteria on model outputs, suggesting the broad applicability of their property-based testing and search methodologies.

Summary updated

BibTeX
@inproceedings{eniser2024automatically,
  author = {Hasan Ferit Eniser and Valentin Wüstholz and Maria Christakis},
  title = {{Automatically Testing Functional Properties of Code Translation Models}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2024},
  doi = {10.1609/aaai.v38i19.30097},
}
Figure