Source-linked AI summary
Learning Spectral Representations of Code through Latent Graph Learning for Generalizable Cross-Language Code Clone Detection
Mohsen Hesamolhokama, Ali Sadeghi, Kousha Moeini, Behnam Rohani, Mohammadamin Fazli, Jafar Habibi
TL;DR
Fixed, language-specific program graphs can yield non-discriminative spectra for functionally equivalent code and weak cross-language generalization. SPECTRA-Siam learns fixed-size latent graphs from AST and data-dependence information, derives multi-scale spectra, and optimizes them for clone detection. The model improves fixed-graph baselines across benchmarks and shows the smallest degradation across 60 unseen bridge-assisted transfer paths.
Problem
Fixed AST, CFG, DDG, PDG, or CPG representations can misalign spectral similarity with functional equivalence, while semantic clone detectors often generalize poorly beyond training projects.
Method
SPECTRA-Siam learns a fixed-size weighted latent graph from each fragment’s AST and projected data-dependence information, then derives a multi-scale spectral representation for pairwise clone prediction.
Results
SPECTRA-Siam improves fixed-graph spectral F1 from .37 to .67 on BigCloneBench and accuracy from .60 to .71 on AtCoder, while its degradation across 60 unseen bridge-assisted paths is .058 versus .112–.228 for baselines.
Takeaways & Limitations
Learning graph structure before spectral analysis yields discriminative code spectra that transfer across unseen language-pair paths.
Takeaways & Limitations
Reported broad experiments use four training epochs because of computational constraints, so results do not represent fully converged models; graph inputs are limited to AST and projected DDG information.
Abstract
from arXiv · showhide
Current code clone detection (CCD) methods rely on fixed, language-specific graph representations like abstract syntax trees (ASTs) or program dependency graphs (PDGs). Because functionally identical code fragments can yield wildly different structures, these rigid graphs produce non-discriminative spectra that perform close to chance. To address this, we propose SPECTRA-Siam, a Siamese latent graph learning network that learns a latent space such that the graph's spectrum serves as a discriminative signature of code functionality by optimizing downstream CCD performance. Given a fragment's AST and data-dependencies, SPECTRA-Siam induces a fixed-size weighted latent graph through soft slot assignment and multi-head attention, and extracts a multi-scale spectral representation from its normalized Laplacian. Mapping all fragments into this shared space yields comparable spectra across programming languages. Experiments on BigCloneBench, AtCoder, and a four-language CodeNet benchmark (Java, Python, C++, C#) support this design choice. Using the same downstream classifier, moving from fixed to learned latent graphs spectra jumps F1 from 0.37 to 0.67 on BigCloneBench and accuracy from 0.60 to 0.71 on AtCoder. On CodeNet, the full model reaches 0.69 accuracy in four epochs and 0.79 after thirty epochs. In bridge-assisted language transfer across 60 unseen paths, SPECTRA-Siam's performance degrades by only 0.058, versus 0.112--0.228 for baselines, showing that learned graph spectra provide a highly generalizable representation for cross-language clone detection.
1. Introduction
Code clone detection depends on representations that capture functional similarity, yet fixed program graphs can mislead spectral comparison and generalize poorly across unseen code. SPECTRA-Siam addresses this by learning latent graphs optimized for clone supervision and evaluates their cross-language robustness.
- Code clone detection seeks identical, syntactically similar, or functionally equivalent fragments, with useful representations supporting automated reasoning about programs.
- Type-IV clones are difficult to detect, and semantic clone detectors often generalize poorly beyond their training projects.
- SPECTRA-Siam learns a language-independent latent graph from AST and projected data-dependence information, allowing clone supervision to determine topology and supporting cross-language comparison.
- Fixed CFG spectra can place semantically equivalent functions farther apart than an unrelated function, with distances 2.14 versus 1.02 and 1.24.
- Spectral F1 improves from .37 to .67 on BigCloneBench and from .65 to .73 on AtCoder against fixed-graph baselines using identical downstream classifiers.
- Across 60 transfer paths, SPECTRA-Siam has the smallest average language-shift degradation, .058 versus .112–.228 for baselines, and remains above chance on all six cross-language pairs without cross-language supervision.
2. Related Work
Related work progresses from text and syntax-based clone detection to program graphs, learned code representations, spectral analysis, and latent graph learning. These strands motivate learning graph structure rather than assuming parsed program relations are always adequate.
- Clone detection research spans four clone types, with Type-4 clones implementing the same functionality despite different syntax.
- Early detectors compared normalized tokens, lines, or copy-paste patterns, while tree-based methods made syntax explicit through AST matching and vectorization.
- Program dependence graphs expose control and data dependencies that tokens or syntax trees may miss, although graph and subgraph matching can be computationally expensive.
- Learned approaches include deep models, AST encoders, graph neural networks, knowledge distillation, token learners, semantic token enrichment, and image-based AST representations.
- Spectral graph analysis uses eigenvalues and eigenvectors of adjacency or Laplacian matrices, whose eigenvalues are invariant to node ordering and support graph comparison.
- Latent graph learning estimates graph structure jointly with the predictive model, addressing the restriction that message-passing methods commonly assume known graph structure.
3. Methodology
SPECTRA-Siam treats graph structure as learnable rather than fixing an AST, CFG, or DDG, and optimizes the latent graph jointly with semantic-clone prediction.
- SPECTRA-Siam learns a latent graph from semantic-clone supervision and optimizes its spectral representation jointly with downstream prediction.
- The learned graph replaces a predefined AST, CFG, or DDG as the structural basis for compact, permutation-invariant spectral comparison.
- The design makes graph structure a task-dependent representation rather than a fixed structural view of the program.
3.1. Problem Formulation
The problem formulation maps code fragments into fixed-size weighted latent graphs and represents each graph through its spectrum. A pairwise similarity objective then supports clone-probability prediction and thresholded detection.
- A labeled training set contains code-fragment pairs with labels indicating whether the fragments implement the same functionality.
- The parameterized mapping GΘ converts each fragment into a weighted attributed graph with m latent nodes and d-dimensional node features.
- Each induced graph is represented by a spectral signature sΘ(c) derived from its graph spectrum.
- The spectral loss separates positive pairs and limits negative-pair similarity using a margin µspec.
- At inference, the model produces a clone probability for each pair and predicts a clone when spectral similarity reaches the decision threshold γ.
3.2. SPECTRA-Siam
SPECTRA-Siam converts attributed program graphs into fixed-size latent graphs and represents them through multi-scale spectral features for pairwise clone prediction. Its training combines classification with spectral, reconstruction, ranking, and regularization objectives.
- Latent graph induction: Each Siamese branch encodes AST and data-dependence relations, then maps variable-size program graphs to fixed latent nodes with learned weighted adjacency.The shared architecture produces comparable latent structures for paired fragments before spectral extraction.
- Input representation: Canonical node types and hashed lexical features provide language-independent structural and limited lexical information at a fixed vocabulary size.Unmatched parser types map to Canonical Unknown, while hashed subtokens and character n-grams bound lexical dimensionality.
- Structural encoding: Relation-aware encoding uses separate transformations for AST and DDG edges, followed by residual normalization, GELU, and dropout across Lenc layers.AST supplies the node backbone, while DDG edges augment it with data-flow relations.
- Latent graph induction: The latent topology combines projected observed AST/DDG structure with task-dependent attention affinities and continuous temperature-controlled edge weights.The learned adjacency is symmetric, continuous, and zero-diagonal, with no hard edge threshold.
- Spectral representation: The spectral representation combines normalized-Laplacian eigenvalue density, heat traces, and graph-signal Chebyshev energies across multiple scales.The Laplacian is symmetrized and its spectrum is clamped to [0, 2] for numerical stability before spectral processing.
- Pairwise prediction and training: A learned projection normalizes each fragment’s spectral embedding, and a pairwise MLP uses concatenated embeddings, differences, products, and cosine similarity to predict clone probability.Training jointly includes classification, spectral contrast, AUC ranking, hard-negative separation, reconstruction, variance preservation, and graph regularization.
4. Experimental Design
The evaluation tests SPECTRA-Siam against fixed spectral, graph, code, hybrid, and pretrained baselines across multiple clone datasets and language-transfer settings. Model selection uses validation data, while the four-epoch main budget provides a conservative comparison because longer training improves CodeNet accuracy.
- Research questions: Three research questions assess latent-graph spectra against fixed graphs, conventional baselines, and unseen language combinations without target-language supervision.RQ1 concerns spectral discriminativeness, RQ2 conventional comparisons, and RQ3 cross-language generalization.
- Datasets: The evaluation uses BigCloneBench, Java–Python AtCoder, and balanced CodeNet configurations spanning four languages and within- and cross-language pairs.CodeNet includes Java, Python, C++, and C# configurations, with ten balanced language configurations reported in the dataset table.
- Baselines: Baseline families cover fixed spectral representations, conventional-graph GNNs, non-graph code models, other graph models, hybrid graph–code models, and pretrained code models.Fixed spectra are evaluated with No Train, Random Forest, Logistic Regression, and Siamese Neural Network settings over AST, CFG, DDG, and CPG graphs.
- Evaluation protocol: The protocol fits models on training pairs and uses validation splits for hyperparameters, checkpoints, thresholds, and dataset-specific model-selection criteria.The test split is accessed only once for final reporting; validation accuracy is used for AtCoder and CodeNet, while BigCloneBench uses binary F1.
- Cross-language transfer: RQ3 evaluates within-language, endpoint-only, and bridge-assisted transfer settings using paths among target and non-target languages.Bridge-assisted training includes consecutive cross-language pairs along a path toward the target language.
- Training budget: 893 neural training runs are compared under a four-epoch cap, which penalizes SPECTRA-Siam relative to longer training.On CodeNet, accuracy increases from .68 at four epochs to .77 at twenty, so the main tables provide a conservative lower bound.
5. Experimental Results
SPECTRA-Siam’s learned latent spectrum separates clone pairs more effectively than fixed program-graph spectra and remains robust across languages. It achieves strong benchmark performance and the smallest degradation under bridge-assisted language shift.
- RQ1: Discriminative Power of the Learned Spectral Representation: .67 F1 on BigCloneBench with an SNN exceeded the best fixed-graph configuration’s .37 F1.Without downstream training, the learned spectrum reached .50 F1.
- RQ1: Discriminative Power of the Learned Spectral Representation: .71 accuracy on AtCoder with an SNN improved over the strongest corresponding fixed-graph configuration by .12.The complete model with its learned prediction head reached .81 accuracy.
- RQ2: Comparative Effectiveness Against Existing Representations and Clone-Detection Methods: .76 F1 on BigCloneBench, .81 accuracy on AtCoder, and .69 accuracy on CodeNet were achieved by the final SPECTRA-Siam.On BigCloneBench, it outperformed Deckard at .36 F1 and the best implemented GNN baseline at .35 F1.
- RQ3: Bridge-Assisted Cross-Language Generalization: .058 average degradation across 60 bridge paths was the smallest under language shift, compared with .112–.228 for baselines.SPECTRA-Siam was also the only method above chance on all six cross-language pairs without cross-language supervision.
- RQ3: Bridge-Assisted Cross-Language Generalization: .71 accuracy on Java–Python test pairs exceeded the .49–.54 accuracy of the listed baselines.Across all 60 paths, the source language had no significant effect, while bridge length and intermediate-language reinforcement affected transfer.
6. Discussion
Discussion analyses show that performance improves with longer training despite worsening validation loss, while learned latent graphs exhibit structured assignments and nonuniform connectivity. The authors also identify incomplete tuning, bounded inputs, and task-specific topology as limitations.
- Hyperparameter Sensitivity: m = 32 gives consistently low validation loss, while increasing latent graph capacity to m = 48 provides no clear benefit.Batch size and latent-assignment iterations each shift test accuracy by three to four points.
- Training Dynamics: .8114 accuracy was reached at epoch 80, although validation cross-entropy rose from its minimum of .4407 at epoch 37 to 1.0288 by epoch 100.The selected decision threshold decreased from .42 to .03, indicating less calibrated probabilities later in training.
- Training Dynamics: .6770 accuracy at epoch 4 increased to .7886 at epoch 30 and .8114 at epoch 80, making the four-epoch results conservative.Both same-language and cross-language configurations benefited from longer training.
- Stability: Across five runs, BigCloneBench F1 varied by only .011 standard deviation, while CodeNet accuracy was .678 ± .005.AtCoder showed somewhat larger variation, with accuracy ranging from .768 to .812.
- What the Learned Latent Graph Captures: The assignment matrix maps AST/DDG nodes to m = 32 latent nodes, whose learned edge weights are not uniform across latent-node pairs.A correctly classified clone had eigenvalue distance .328 versus .754 for the non-clone.
- Limitations and Future Work: The broad study capped training at four epochs, so reported models were not fully converged and several design choices were not exhaustively tuned.The representation is limited to AST and projected DDG inputs, 256-node input graphs, fixed latent slots, and topology learned specifically for clone detection.
7. Threats to Validity
The paper identifies internal, external, and construct-validity threats that constrain how confidently its results can be interpreted and generalized.
- Internal Validity: The four-epoch training cap may favor faster-converging models, although the authors suggest it works against SPECTRA-Siam and may affect baselines differently.Single-run randomness is assessed through multiseed analysis.
- External Validity: AtCoder and CodeNet use programming-contest distributions that may not reflect general software development.This limits direct generalization beyond those data sources.
- External Validity: The function-level evaluation may not fully represent industrial codebases containing larger systems, evolving projects, and collaborative development patterns.The boundary concerns both fragment scale and development setting.
- Construct Validity: Benchmark positive-pair labels are proxies for semantic equivalence because BigCloneBench uses human-assisted annotations, whereas AtCoder and CodeNet use shared problem identity.These labeling schemes may measure related but non-identical properties.
8. Conclusions
SPECTRA-Siam learns fixed-size latent graphs from AST and data-dependence information, then uses their multi-scale spectra to represent functional similarity. Experiments support improved fixed-graph spectral performance, competitive benchmark results, and limited degradation across unseen language-transfer paths.
- Conclusions: SPECTRA-Siam induces a fixed-size latent graph from AST and data-dependence information, deriving a multi-scale spectral representation from its learned topology.Clone supervision determines the latent graph space used to capture functional similarity.
- Conclusions: F1 improves from .37 to .67 on BigCloneBench, while accuracy improves from .60 to .71 on AtCoder, using identical classifiers against fixed program-graph spectra.The comparison supports learning graph structure before spectral analysis.
- Conclusions: The complete model obtains .76 F1 on BigCloneBench, .82 F1 on AtCoder, and .69 accuracy on four-language CodeNet after four epochs.The reported CodeNet result surpasses ASTNN and GraphCodeBert under the stated training condition.
- Conclusions: Across 60 unseen bridge-assisted transfer paths, performance degradation is .058 for SPECTRA-Siam versus .112, .162, .170, and .228 for the listed baselines.Intermediate-language supervision further improves transfer accuracy by up to .036.
9. Declarations
The study states that its datasets and code repository are publicly available through the listed Kaggle and GitHub links.
- Datasets: CodeNet is available through the listed Kaggle dataset link.The declaration identifies the CodeNet resource separately.
- Datasets: AtCoder is available through the listed Kaggle dataset link.The declaration identifies the AtCoder resource separately.
- Datasets: BigCloneBench is available through the listed Kaggle dataset link.The declaration identifies the BigCloneBench resource separately.
- Code: The code repository is listed at GitHub under the Spectral-Software project.The declaration provides the repository URL.