TL;DR
This paper investigates how different ways of representing code (text, abstract syntax trees, graphs) impact the effectiveness of contrastive learning for detecting code clones. They found that graph-based representations, combined with contrastive learning methods like SimCLR and SwAV, generally yield the best results for identifying functionally similar code, even when implementations differ significantly.
What Problem Does This Paper Address?
The core problem this paper tackles is the accurate detection of "semantic code clones" – code snippets that perform the same function but are written very differently. This is a challenging task because traditional methods often rely on syntactic similarity, which these semantic clones lack. While contrastive learning (CL) has shown promise in other domains like computer vision and NLP for learning representations that capture underlying meaning, its application to code clone detection is still relatively unexplored, particularly concerning the choice of code representation. The authors aim to bridge this gap by systematically evaluating various CL algorithms with different code representations to understand which combinations are most effective for this task.
Methods
The researchers propose a unified, modular, and extensible framework for training and evaluating ML models for code clone detection. Their approach involves three main steps:
- Code Representation: They explore three primary ways to represent source code for machine learning models:
- Text-based: Treating code as a sequence of tokens, similar to natural language. They use BERT as the encoder.
- AST-based: Representing code as an Abstract Syntax Tree (AST), capturing its structural hierarchy. They use code2seq.
- Graph-based: Constructing a more complex graph representation that combines ASTs with Control Flow Graphs (CFGs) and Program Dependence Graphs (PDGs). They use DeeperGCN.
- Contrastive Learning Algorithms: They evaluate three popular CL algorithms:
- SimCLR: A straightforward approach that uses data augmentation to create positive pairs and other samples in the batch as negative pairs.
- Moco: Employs a momentum encoder and a queue to maintain a large number of negative samples, aiming for more stable training.
- SwAV: Uses online clustering to learn representations, avoiding the explicit need for negative samples.
- Training and Evaluation: They train models using these combinations on two tasks: code clone detection and plagiarism detection. They compare their CL-based models against several baselines, including traditional token-based tools (Simian, JPlag, CCAligner) and modern pre-trained models (InferCode, Trans-Coder).
Datasets
The study utilizes two datasets:
- POJ-104: A well-established dataset containing 500 implementations for each of 104 different C/C++ algorithms. This dataset is used for evaluating code clone detection, where implementations of the same algorithm are considered clones.
- CODEFORCES: A dataset derived from competitive programming solutions. To create a benchmark for plagiarism detection, they introduce a tool called Code Transformator. This tool augments existing code with nine types of transformations (e.g., renaming variables, adding comments, swapping code blocks) to mimic plagiarized code. This dataset is used for evaluating how well models can detect functionally equivalent code that has undergone these transformations.
Key Insights and Ideas
- Graphs are King for Code Semantics: The most significant insight is that graph-based representations of code (combining AST, CFG, and PDG) consistently outperform text and AST-based representations for clone detection. This suggests that capturing the intricate control and data flow dependencies is crucial for understanding code functionality.
- CL Algorithms Matter, but Representation is Paramount: While different CL algorithms (SimCLR, Moco, SwAV) show varying degrees of success, their performance is heavily influenced by the underlying code representation. Even the best CL algorithm struggles with less informative representations.
- SimCLR and SwAV Shine: SimCLR and SwAV generally achieved better results than Moco, especially when paired with effective representations. This might be because they allow the encoder to receive updates from both elements of a pair simultaneously, providing richer learning signals.
- Moco's Robustness: Moco, however, proved to be the most robust CL algorithm, particularly when combined with the BERT encoder, which is known to be sensitive to hyperparameters. This robustness is attributed to its momentum encoder and queue mechanism.
- Pre-trained Models Offer a Strong Baseline: Embeddings from pre-trained models like Trans-Coder (trained for code translation) showed surprisingly good performance, indicating that understanding program functionality is a transferable skill. However, InferCode, trained for subtree prediction, performed less well, suggesting its learned features are less generalizable to detecting semantic similarity.
Key Results
- Graph-based models (DeeperGCN) achieved the best performance on both the POJ-104 (clone detection) and CODEFORCES (plagiarism detection) datasets across all tested CL algorithms.
- SimCLR and SwAV generally outperformed Moco in terms of raw scores, with SimCLR achieving the highest F1 scores on POJ-104 (55.75%) and SwAV on CODEFORCES (59.27%) when paired with GCN.
- Moco demonstrated superior robustness, especially with the BERT encoder, where other CL algorithms failed to converge.
- BERT as an encoder was highly sensitive to hyperparameters and often failed to converge, highlighting the challenges of using large language models directly with certain CL approaches for code.
- Traditional token-based methods (Simian, JPlag) struggled with Type IV clones (semantic clones) but JPlag showed competitive results among token-based tools.
- Trans-Coder embeddings were nearly as effective as CL-trained embeddings, demonstrating the value of pre-training for code understanding.
Limitations and Future Work
The authors acknowledge that the BERT encoder's sensitivity to hyperparameters is a significant limitation. They also note that while Code Transformator is extensible, the choice of sampled paths in the AST-based code2class representation can significantly impact performance, suggesting a need for better path sampling techniques.
Future work directions include:
- Reusing learned embeddings from contrastive learning for other SE tasks like refactoring recommendation or documentation generation.
- Adding new code transformations to Code Transformator for more robust plagiarism detection.
- Extending the framework and tools to support other programming languages.
- Further exploring the interplay between CL algorithms, code representations, and encoder architectures to optimize performance for various code-related tasks.
Summary updated