MGS3: A Multi-Granularity Self-Supervised Code Search Framework

Fuente: arXiv
Saved in:
Bibliographic Details
Main Authors: Li, Rui, Kang, Junfeng, Liu, Qi, He, Liyang, Zhang, Zheng, Sha, Yunhao, Zhu, Linbo, Huang, Zhenya
Format: Preprint
Published: 2025
Subjects:
Online Access:
Tags: Add Tag
No Tags, Be the first to tag this record!
_version_ 1866908385200308224
author Li, Rui
Kang, Junfeng
Liu, Qi
He, Liyang
Zhang, Zheng
Sha, Yunhao
Zhu, Linbo
Huang, Zhenya
author_facet Li, Rui
Kang, Junfeng
Liu, Qi
He, Liyang
Zhang, Zheng
Sha, Yunhao
Zhu, Linbo
Huang, Zhenya
contents In the pursuit of enhancing software reusability and developer productivity, code search has emerged as a key area, aimed at retrieving code snippets relevant to functionalities based on natural language queries. Despite significant progress in self-supervised code pre-training utilizing the vast amount of code data in repositories, existing methods have primarily focused on leveraging contrastive learning to align natural language with function-level code snippets. These studies have overlooked the abundance of fine-grained (such as block-level and statement-level) code snippets prevalent within the function-level code snippets, which results in suboptimal performance across all levels of granularity. To address this problem, we first construct a multi-granularity code search dataset called MGCodeSearchNet, which contains 536K+ pairs of natural language and code snippets. Subsequently, we introduce a novel Multi-Granularity Self-Supervised contrastive learning code Search framework (MGS$^{3}$}). First, MGS$^{3}$ features a Hierarchical Multi-Granularity Representation module (HMGR), which leverages syntactic structural relationships for hierarchical representation and aggregates fine-grained information into coarser-grained representations. Then, during the contrastive learning phase, we endeavor to construct positive samples of the same granularity for fine-grained code, and introduce in-function negative samples for fine-grained code. Finally, we conduct extensive experiments on code search benchmarks across various granularities, demonstrating that the framework exhibits outstanding performance in code search tasks of multiple granularities. These experiments also showcase its model-agnostic nature and compatibility with existing pre-trained code representation models.
format Preprint
id arxiv_https___arxiv_org_abs_2505_24274
institution arXiv
publishDate 2025
record_format arxiv
spellingShingle MGS3: A Multi-Granularity Self-Supervised Code Search Framework
Li, Rui
Kang, Junfeng
Liu, Qi
He, Liyang
Zhang, Zheng
Sha, Yunhao
Zhu, Linbo
Huang, Zhenya
Software Engineering
Information Retrieval
In the pursuit of enhancing software reusability and developer productivity, code search has emerged as a key area, aimed at retrieving code snippets relevant to functionalities based on natural language queries. Despite significant progress in self-supervised code pre-training utilizing the vast amount of code data in repositories, existing methods have primarily focused on leveraging contrastive learning to align natural language with function-level code snippets. These studies have overlooked the abundance of fine-grained (such as block-level and statement-level) code snippets prevalent within the function-level code snippets, which results in suboptimal performance across all levels of granularity. To address this problem, we first construct a multi-granularity code search dataset called MGCodeSearchNet, which contains 536K+ pairs of natural language and code snippets. Subsequently, we introduce a novel Multi-Granularity Self-Supervised contrastive learning code Search framework (MGS$^{3}$}). First, MGS$^{3}$ features a Hierarchical Multi-Granularity Representation module (HMGR), which leverages syntactic structural relationships for hierarchical representation and aggregates fine-grained information into coarser-grained representations. Then, during the contrastive learning phase, we endeavor to construct positive samples of the same granularity for fine-grained code, and introduce in-function negative samples for fine-grained code. Finally, we conduct extensive experiments on code search benchmarks across various granularities, demonstrating that the framework exhibits outstanding performance in code search tasks of multiple granularities. These experiments also showcase its model-agnostic nature and compatibility with existing pre-trained code representation models.
title MGS3: A Multi-Granularity Self-Supervised Code Search Framework
topic Software Engineering
Information Retrieval
url https://arxiv.org/abs/2505.24274