让代码检索从匹配候选变成分层定位,提升精准度。
CodeHID: Learning an Addressable Hierarchical Code Index for Generative Code Retrieval

- 用多级量化与伪监督构建语义相关的分层代码索引
- 在CoSQA和ProCQA上显著超越现有方法,尤其提升首位召回率
- 适合需要高精度代码检索的开发与研究场景
代码检索模型长期依赖扁平匹配范式,难以区分相似代码片段。生成式检索通过在代码语料库上构建可学习索引,引导检索器理解代码的语义组织结构。然而,直接应用生成式检索可能面临标识符前缀与语义区域不对应的问题。本文提出CodeHID框架,将代码检索任务重构为粗到细的语义地址生成。核心包含两部分:首先,伪邻近引导文档编号学习通过多级残差量化与k近邻伪监督,构建全局静态分层索引,使语义相近代码共享前缀并保持目标层级可区分性;其次,双阶段文档编号生成引导结合训练侧排序增强(使用难样本与排名蒸馏)与推理侧候选约束及前缀感知解码,可靠导航该固定索引。在CoSQA和ProCQA基准上的大量实验表明,CodeHID在多数情况下大幅超越现有稀疏检索、预训练代码模型、密集检索及生成式检索基线,尤其在一级检索指标上表现突出。
原文摘要 · Abstract (English)
Code retrieval models have predominantly relied on a flat matching paradigm that treats code snippets as independent candidates, making them less capable of distinguishing similar code candidates. Generative retrieval offers a solution by constructing a learnable index over the code corpus, guiding the retriever to better understand how code candidates are semantically organized and addressed. However, naively applying generative retrieval in the code retrieval task may result in operating over an identifier space whose prefixes do not correspond to meaningful code-semantic regions. In this paper, we propose CodeHID, a generative code retrieval framework that reformulates the code retrieval task from flat candidate matching to coarse-to-fine semantic address generation. CodeHID relies on two core components. First, Pseudo-Neighbor Guided DocID Learning constructs a globally static hierarchical index by applying multi-level residual quantization and $k$-nearest-neighbor pseudo-supervision, ensuring that semantically related code snippets share prefixes while preserving target-level separability. Second, Dual-Phase DocID Generation Guidance reliably navigates this fixed index by combining training-side ranking enhancements, using hard negatives and rank distillation, with inference-side candidate constraints and prefix-aware decoding. Extensive experiments on CoSQA and ProCQA benchmarks demonstrate that CodeHID outperforms existing sparse retrieval, pre-trained code models, dense code retrieval, and generative retrieval baselines by a large margin in most cases, achieving particularly strong improvements in rank-one retrieval metrics.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。