Manuscript Offprint

arXiv:2609.17338

Type-IV Code Clone Detection via Layer-Wise Non-Contrastive Representation Learning

Luciano Marchezan, Kevin Delcourt, Eugene Syriani, Houari Sahraoui

Luciano Marchezan, Kevin Delcourt, Eugene Syriani, and Houari Sahraoui

Université de Montréal. Montreal, QC, Canada

Email: {luciano.augusto.marchezan.de.paula, kevin.delcourt}@umontreal.ca

{syriani, sahraouh}@iro.umontreal.ca

Abstract—Software clones are fragments of code that are similar or functionally equivalent to each other. They pose significant challenges for maintenance, refactoring, and bug detection. Detecting Type-IV clones, which are semantically equivalent but may differ syntactically, is particularly difficult for traditional token- or syntax-based methods. Recent machine learning approaches rely on contrastive learning, which requires careful negative sampling and can introduce bias. In this paper, we propose LWVIC4Code, a non-contrastive representation learning approach specifically designed for Type-IV clone detection. Building on the Variance-Invariance-Covariance Regularization (VICReg) framework and prior layer-wise VICReg training, LWVIC4Code introduces cross-layer consistency regularization and depth-dependent layer weighting to progressively refine semantic information across transformer layers, producing robust and discriminative code representations. We conduct an empirical study comparing LWVIC4Code against a contrastive learning baseline and zero-shot large language models on Python (Kamino) and multi-language (GPTCloneBench) datasets. Results show that LWVIC4Code achieves competitive or superior performance without negative samples, benefits from layer-wise supervision, and generalizes effectively from Python to other languages, particularly Java and C#. These results demonstrate that non-contrastive, layer-wise representation learning is a promising direction for robust semantic code clone detection.

Index Terms—semantic code similarity; clone detection; representation Learning; non-contrastive learning; software maintenance

I. INTRODUCTION

Software clones, or similar fragments of code within or across software systems, are a common phenomenon in software development [1]. Detecting clones is critical for maintenance tasks such as code reuse, refactoring, bug detection, and quality assurance [2]. For instance, over 50% of files in two major open-source autonomous driving projects contain code clones, a substantial fraction of which are associated with previously reported bugs [3].

Code clones are typically categorized into four types [4], [5]: Types I–III represent exact or near-identical fragments with syntactic similarity, while Type-IV clones are semantic clones, i.e., functionally equivalent fragments that may differ entirely in syntax or structure. Detecting Type-IV clones requires reasoning about program semantics rather than surface-level syntactic patterns, which limits the effectiveness of traditional text- or token-based methods, especially when implementations differ in control-flow strategies, library usage, or algorithmic approach [6]–[8].

In this context, the growing adoption of large language models (LLMs) for code generation further amplifies the importance of clone detection, particularly for Type-IV clones. Recent studies indicate that AI-assisted programming is rapidly becoming standard practice in modern development workflows [9]. While LLMs improve productivity, they also introduce new challenges as the generated code may replicate patterns, logic, or entire solutions seen during training, sometimes closely resembling existing open-source implementations [10]. Such duplication is often not syntactically identical but semantically equivalent, making it difficult to detect using traditional clone detection techniques. This raises concerns related to code maintainability, redundancy, and licensing compliance when generated code inadvertently mirrors copyrighted sources [11]. Consequently, robust Type-IV clone detection is increasingly essential for identifying such semantic duplication across diverse implementations, programming styles, and programming languages.

Early research on semantic clone detection explored behavioral equivalence via program transformations [7], whereas more recent studies leverage machine learning (ML) to learn semantic representations of code [12], [13]. Pretrained code embedding models such as CodeBERT [14] and CodeT5 [15] generate vector representations that capture structural and semantic properties of code. These models can be finetuned for downstream tasks such as clone detection. These methods allow models to leverage large amounts of labeled code, typically using contrastive objectives to bring semantically similar fragments closer in embedding space while pushing dissimilar fragments apart [16]. However, contrastive approaches require careful selection of negative samples, which can introduce biases and complicate training [17].

Non-contrastive representation learning provides an alternative by enforcing constraints on the embedding space itself, without the need for negative pairs [18]. Variance-Invariance-Covariance Regularization (VICReg) [19] is a prominent example, optimizing three complementary properties: (i) invariance between different views of the same sample, (ii) variance across embedding dimensions to prevent collapse, and (iii) covariance between features to capture diverse aspects of the input. These properties make VICReg particularly well-suited for Type-IV clone detection, where semantic equivalence exists despite syntactic differences.

Building on this idea, we propose LWVIC4Code, a non-contrastive representation learning approach specifically de-signed for Type-IV clone detection. Inspired by the layer-wise VICReg formulation of Datta et al. [20], our approach applies VICReg objectives at multiple transformer layers and optimizes them jointly through end-to-end training. Building on this foundation, we introduce a cross-layer consistency regularizer and depth-dependent layer weighting to encourage the progressive refinement of semantic information throughout the network. This leads to the introduction of three novel components as part of the LWVIC4Code architecture. The resulting representations are robust and discriminative, making them suitable for detecting semantically equivalent yet syntactically diverse code fragments.

We conduct an empirical study to evaluate the effectiveness of LWVIC4Code for Type-IV clone detection, comparing it with a contrastive learning model (CodeBERT CL_{CL} [21]) and zero-shot LLMs. Specifically, we investigate (i) whether LWVIC4Code achieves comparable or superior performance without negative samples, (ii) the benefits of layer-wise supervision over traditional VICReg, (iii) which aspects of the novel components contribute the most to performance gains, and (iv) the generalization of models trained on Python-only data to other programming languages. Our evaluation uses two benchmark datasets: Kamino, a large Python-only dataset of Type-IV clones [22], and GPTCloneBench, a smaller multi-language dataset covering Python, Java, C#, and C [23]. Performance is assessed using F1F_{1}-score and Matthews Correlation Coefficient (MCC) [24]. Statistical significance is evaluated using p-values corrected for multiple comparisons via the Benjamini-Hochberg procedure [25].

Results demonstrate that LWVIC4Code achieves strong performance for Type-IV clone detection across multiple programming languages and datasets. For example, on GPT-CloneBench, LWVIC4Code reaches F1F_1 scores of 0.977 on C#, 0.968 on Java, and 0.920 on Python, with corresponding MCC values of 0.957, 0.938, and 0.840. These results outperform the contrastive baseline CodeBERT CL_{CL} (e.g., F1F_1 0.899 and MCC 0.794 on C#) while avoiding the need for negative sampling. Overall, LWVIC4Code achieves higher performance than the original VICReg objective on GPTCloneBench ( p<0.05p < 0.05), with the ablation study demonstrating that both layer-wise weighting and cross-layer consistency contribute to improving representation quality. Despite this, the last-layer-only variant achieves better results in specific scenarios.

In addition, LWVIC4Code outperforms zero-shot LLM-based approaches, achieving significantly higher performance than GPT-OSS and Qwen and comparable performance to DeepSeek-r1. These results demonstrate that a dedicated non-contrastive representation learning approach can provide more reliable and efficient semantic code representations than general-purpose LLMs for Type-IV clone detection, while requiring substantially less inference cost. The main contributions of this paper are:

i) We introduce VIC4Code, a non-contrastive representation learning approach that adapts the VICReg objective to code representation learning, enabling the learning of semantic embeddings without requiring negative samples;

ii) Building on prior layer-wise VICReg formulations, we propose LWVIC4Code, a transformer-based approach for Type-IV clone detection that introduces cross-layer consistency regularization and depth-dependent layer weighting, enabling end-to-end learning of hierarchical code representations and improving the robustness and discriminative power of code embeddings;

iii) A systematic empirical evaluation of LWVIC4Code for Type-IV clone detection, demonstrating that it outperforms a state-of-the-art contrastive learning baseline, improves over the standard VICReg objective, and achieves competitive or superior performance compared to zero-shot LLM-based approaches across multiple programming languages and datasets;

iv) An ablation study of the LWVIC4Code components, demonstrating that layer-wise weighting and cross-layer consistency are important contributors to performance improvements, with layer-wise weighting providing the largest impact;

v) An analysis of the impact of training data composition for LWVIC4Code, comparing Python-only vvs. multi-language training, demonstrating that representations learned from Python transfer effectively to Java and C#, though less to syntactically divergent languages such as C;

vi) A set of models for Type-IV clone detection: two variants of the complete LWVIC4Code (Python-only and multi-language), three variants without each novel component point, and one variant of VIC4Code.

The remainder of the paper is structured as follows. Section II presents background on semantic clone detection and representation learning; Section III describes the model architectures and non-contrastive training strategies; Section IV details the experimental protocol and results; Section V provides analysis and limitations; Section VI reviews related work; and Section VII concludes the paper.

References (59)