XSearch通过概念对齐实现可解释代码搜索,提升泛化能力与结果可信度。
XSearch: Explainable Code Search via Concept-to-Code Alignment

- 将代码搜索重构为概念对齐的演绎推理问题,而非依赖全局向量相似度
- 在分布外基准上性能从0.02提升至0.33,相较基线提升15倍
- 提供概念级解释,帮助用户更快更准评估检索结果
语义代码搜索在学术界和工业界广泛应用。现有方法将自然语言查询与代码片段映射到共享嵌入空间,基于向量相似性进行检索。尽管在基准数据集上表现良好,但普遍存在可解释性差、泛化能力弱的问题:检索结果可能语义相近却遗漏关键功能需求,且无法说明为何被选中。尤其在分布外场景下,模型性能显著下降。本文提出XSearch,一种内在可解释的代码搜索框架。核心思想是:现有方法依赖全局嵌入相似性,属于归纳学习,仅捕捉统计模式而非真正理解查询的功能需求。为此,我们将代码搜索重构为演绎式的概念对齐问题。XSearch(1)识别查询中的功能概念,(2)显式对齐对应代码语句。该“解释-再预测”设计生成概念级解释,缓解导致分布外泛化失败的捷径学习。我们使用显式概念对齐目标训练编码器,并通过查询概念与代码语句间的显式匹配完成检索。实验表明,在CodeSearchNet上以GraphCodeBERT(125M参数)为基线训练时,XSearch在分布外基准上性能从0.02提升至0.33(15倍),优于8种先进检索器;在70亿参数范围内,持续超越编码器与解码器基线。用户研究显示,概念对齐解释使用户能更快、更准确地评估检索结果。
原文摘要 · Abstract (English)
Semantic code search has been widely adopted in both academia and industry. These approaches embed natural-language queries and code snippets into a shared embedding space and retrieve results based on vector similarity. Despit strong performance on benchmark datasets, they often suffer from poor explainability and generalization. Retrieved code may appear semantically similar yet miss critical functional requirements of the query, while providing no explanation of why the result was retrieved. Moreover, such failures become more severe under distribution shift, where models struggle to generalize to unseen benchmarks. In this work, we propose XSearch, an intrinsically explainable code search framework. Our key insight is that by relying on global embedding similarity, existing retrievers inherently take an inductive view. They learn statistical patterns rather than truly understanding the query's functional requirements. We address this problem by reformulating code search as a deductive concept alignment problem. XSearch (i) identifies functional concepts in the query and (ii) explicitly aligns them with corresponding code statements. This explain-then-predict design produces inherent concept-level explanations and mitigates shortcut learning that harms out-of-distribution generalization. We train an encoder with explicit concept-alignment objectives and perform retrieval through explicit matching between query concepts and code statements. Experiments show that, trained on CodeSearchNet using GraphCodeBERT (125M parameters), XSearch improves performance on out-of-distribution benchmarks from 0.02 to 0.33 (15x) over eight state-of-the-art retrievers, and consistently outperforms both encoder- and decoder-based baselines with up to 7B parameters. A user study demonstrates that concept-alignment explanations enable users to evaluate retrieved results faster and more accurately.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。