Less Is More: DocString Compression in Code Generation

Guang Yang, Yu Zhou, Wei Cheng, Xiangyu Zhang, Xiang Chen, Terry Yue Zhuo, Ke Liu, Xin Zhou, David Lo, Taolue Chen

TL;DR

Large Language Models (LLMs) are revolutionizing code generation, but their reliance on lengthy DocStrings as prompts leads to high computational costs. This paper introduces ShortenDoc, a novel method that intelligently compresses DocStrings, achieving significant reductions (25-40%) while preserving code generation quality and even improving it in some cases. This work offers a practical solution for making LLM-based code generation more efficient and cost-effective.

What Problem Does This Paper Address?

The increasing adoption of Large Language Models (LLMs) for software engineering tasks, particularly code generation, has brought about a significant challenge: efficiency. LLMs often use DocStrings – natural language descriptions of functions or methods – as prompts to generate code. However, these DocStrings frequently contain redundant information, leading to longer input sequences. This redundancy increases the computational cost (FLOPs) and financial cost (API calls) associated with using LLMs, especially at scale. While prompt compression techniques exist for natural language processing, their effectiveness and applicability to the unique characteristics of code generation DocStrings remain uncertain. Existing methods struggle to achieve substantial compression without degrading the quality of the generated code.

Methods

The authors propose ShortenDoc, a novel, adaptive DocString compression method specifically designed for code generation. Its core innovation lies in dynamically adjusting compression based on token importance, rather than using a fixed compression ratio. The process involves:

  1. Pre-processing: Removing line breaks and tabs from DocStrings, and identifying and removing "stop-words" (common words that don't significantly impact semantic meaning) while ensuring semantic similarity with the original using cosine similarity (threshold > 0.999).
  2. Token Importance Sort: Quantifying the importance of each token by its contribution to the conditional entropy of the sequence, using a language model (CodeGPT-py-adapted) to calculate negative log-probabilities.
  3. Search Space Construction: Identifying candidate tokens and sequences (k-grams) for removal by selecting the least important tokens and exploring combinations to capture potential benefits of compressing consecutive tokens.
  4. Constraint Definition: Ensuring that the compressed DocString (D') does not significantly alter the model's output probability distribution for code generation. This is achieved by enforcing a constraint on the cosine similarity of the logits (z(T, D) and z(T, D')) to be above a threshold (τ = 0.999).

The optimization goal is to minimize the length of the compressed DocString (||D'||) subject to the semantic similarity and logit similarity constraints.

Datasets

The paper utilizes a variety of datasets for evaluation:

  • Python Code Generation Datasets:
    • HumanEval and its variants (EvoEval Subtle, EvoEval Creative): Standard benchmarks for evaluating code generation.
  • MBPP (Mostly Basic Python Problems): Contains crowd-sourced Python programming problems.
  • CodeHarmony: LLM-synthesized high-quality Python code samples.
  • BigCodeBench: Practical and challenging programming tasks.
  • Multilingual Code Generation Dataset:
    • HumanEval-X: A dataset containing code generation problems in C++, Go, Java, and JavaScript, used to test generalization.

Key Insights and Ideas

  • DocStrings are Redundant, but Not Entirely: While DocStrings contain redundant information, they are crucial for code generation. Aggressive compression or complete removal can significantly degrade performance.
  • Token Importance is Key: Simply removing common words isn't enough. Understanding the semantic contribution of each token is vital for effective compression.
  • Consecutive Token Compression Matters: Compressing sequences of low-importance tokens (n-grams) can sometimes yield better results than compressing individual tokens.
  • Method Names Carry Significant Information: High-quality method names can compensate for some loss of information from compressed DocStrings, suggesting a synergistic relationship.
  • Language-Specific Models Aid Compression: The base model used for compression (CodeGPT-py-adapted) being trained on code and natural language is crucial for understanding DocString semantics.

Key Results

  • Superior Compression: ShortenDoc achieves 25-40% DocString compression, outperforming existing methods like Selective_Context and LLMLingua2.
  • Improved or Maintained Pass@1: ShortenDoc consistently achieves higher or comparable Pass@1 scores compared to uncompressed prompts and other compression methods across various datasets and LLMs. In some cases, it even improves Pass@1 scores.
  • Efficiency Gains:
    • GPU Memory Usage: Negligible overhead for the compression process itself.
  • FLOPs Reduction: Models generally show a decrease in FLOPs after compression (e.g., ~17.6% on HumanEval, ~20% on CodeHarmony).
  • Inference Time: While compression adds a small overhead (~2 seconds), the overall pipeline remains efficient.
  • Generalization: ShortenDoc maintains its effectiveness across multiple programming languages (C++, Go, Java, JavaScript), though a slight performance degradation is observed when migrating compressed DocStrings from Python.
  • Human Study Validation: Human evaluators rated ShortenDoc-compressed DocStrings as significantly more informative and comprehensible than those compressed by other methods.

Limitations and Future Work

  • Hyperparameter Tuning: While τ=0.999 is a good default, optimal values might vary across datasets, suggesting future work on dataset-specific tuning strategies.
  • Base Model Dependence: The performance of ShortenDoc is influenced by the base model used for token importance ranking. Future work could explore more robust methods for token importance assessment.
  • Cross-Language Evaluation: Reliance on translated versions of HumanEval-X for multilingual evaluation is a limitation. Future work should evaluate on native, language-specific datasets.
  • Extension to Class-Level and General Code Completion: The current work focuses on function-level generation. Future research will extend ShortenDoc to handle more complex code structures (classes) and general code completion tasks.
  • Multilingual Models: Training a multilingual base language model for compression is planned to enhance applicability across different programming languages.

Summary updated

BibTeX
@misc{yang2024less,
  author = {Guang Yang and Yu Zhou and Wei Cheng and Xiangyu Zhang and Xiang Chen and Terry Yue Zhuo and Ke Liu and Xin Zhou and David Lo and Taolue Chen},
  title = {{Less Is More: DocString Compression in Code Generation}},
  year = {2024},
  eprint = {2410.22793},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.22793},
}
Figure