一句话:提出面向代码 LLM 的双阶段即插即用上下文压缩框架,最高达 5.6× 压缩比且不损失任务性能。
随着代码大模型需要在庞大代码库上推理,长上下文代码生成日益关键,但高 API 成本与生成延迟仍是主要瓶颈。现有通用文本剪枝方法(如 LLMLingua)忽略代码特有的结构与依赖,在编程任务上效果欠佳。本文提出 LongCodeZip,一个专为代码 LLM 设计、即插即用的压缩框架,采用双阶段策略:粗粒度压缩基于对指令的条件困惑度对函数级代码块排序,只保留最相关函数;细粒度压缩按困惑度将保留函数切分为块,并在自适应 token 预算下选出最优子集以最大化相关性。在代码补全、代码摘要与问答等任务上的评估显示,LongCodeZip 持续优于基线方法,在任务性能不下降的前提下实现最高 5.6× 的压缩比,从而助力 LLM 扩展到真实大规模代码场景。
Code generation under long contexts is becoming increasingly critical as Large Language Models (LLMs) are required to reason over extensive information in the code-base. While recent advances enable code LLMs to process long inputs, high API costs and generation latency remain substantial bottlenecks. Existing context pruning techniques, such as LLMLingua, achieve promising results for general text but overlook code-specific structures and dependencies, leading to suboptimal performance in programming tasks. In this paper, we propose LongCodeZip, a novel plug-and-play code compression framework designed specifically for code LLMs. LongCodeZip employs a dual-stage strategy: (1) coarse-grained compression, which identifies and ranks function-level chunks using conditional perplexity with respect to the instruction, retaining only the most relevant functions; and (2) fine-grained compression, which segments retained functions into blocks based on perplexity and selects an optimal subset under an adaptive token budget to maximize relevance. Evaluations across multiple tasks, including code completion, summarization, and question answering, show that LongCodeZip consistently outperforms baseline methods, achieving up to a 5.6× compression ratio without degrading task performance. By effectively reducing context size while preserving essential information, LongCodeZip enables LLMs to better scale to real-world, large-scale code scenarios, advancing the efficiency and capability of code intelligence applications1.
DOI 原文 ·
@inproceedings{paperbot9219,
title = {LongCodeZip: Compress Long Context for Code Language Models},
author = {Yuling Shi and Yichun Qian and Hongyu Zhang and Beijun Shen and Xiaodong Gu},
booktitle = {ASE},
year = {2025},
doi = {10.1109/ase63991.2025.00020}
}