Natural Is The Best: Model-Agnostic Code Simplification for Pre-trained Large Language Models

Yan Wang, Xiaoning Li, Tien Nguyen, Shaohua Wang, Chao Ni, Ling Ding · DOI

Abstract

Pre-trained Large Language Models (LLM) have achieved remarkable successes in several domains. However, code-oriented LLMs are heavy in computational complexity, and quadratically with the length of the input. Toward simplifying the input program of an LLM, the state-of-the-art approach has the strategies to filter the input code tokens based on the attention scores given by the LLM. The decision to simplify the input should not rely on the attention patterns of an LLM, as these patterns are influenced by both the model architecture and the pre-training dataset. Since the model and dataset are part of the solution domain, not the problem domain where the input belongs, the outcome may differ when the model is pre-trained on a different dataset. We propose SlimCode, a model-agnostic code simplification solution for LLMs that depends on the nature of input code tokens. As an empirical study on the LLMs including CodeBERT, CodeT5, and GPT-4 for two main tasks: code search and summarization, we reported that 1) the removal ratio of code has a linear-like relation with the saving ratio on training time, 2) the impact of categorized tokens on code simplification can vary significantly, 3) the impact of categorized tokens on code simplification is task-specific but model-agnostic, and 4) the above findings hold for the paradigm-prompt engineering and interactive in-context learning. The empirical results showed that SlimCode can improve the state-of-the-art technique by 9.46% and 5.15% in terms of MRR and BLEU score on code search and summarization. Moreover, SlimCode is 133 times faster than the state-of-the-art approach. Additionally, SlimCode can reduce the cost of invoking GPT-4 by up to 24% per API query, while still producing comparable results to those with the original code.

Introduction

Large Language Models (LLMs) for code, such as CodeBERT, CodeT5, and GPT-4, are transformative but come with a "length tax." Because these models are based on the Transformer architecture, their computational complexity grows quadratically with the length of the input sequence. Longer code snippets mean slower training, higher latency, and—in the case of commercial APIs—higher bills.

Previous attempts to solve this, like the state-of-the-art DietCode, relied on "attention-based" pruning. They would run the code through the model, see which tokens the model "paid attention" to, and delete the rest. This approach is fundamentally flawed: attention patterns are specific to a model's architecture and training data (the "solution domain"). If you change the model, the pruning strategy might break. Furthermore, running a heavy model just to decide what to delete is computationally expensive.

Methodology: The SlimCode Approach

The authors propose a "Natural" approach: simplify code based on the inherent properties of the programming language itself (the "problem domain"), making the solution model-agnostic.

They categorized code tokens into three levels:

  1. Lexical Level: Symbols (brackets, operators) and Identifiers (variable/function names).
  2. Syntactic Level: Control structures (if, for, while), method invocations, and method signatures.
  3. Semantic Level: Tokens present in a Program Dependence Graph (PDG), which tracks how data and control flow through a program.

SlimCode uses a greedy algorithm modeled as a 0-1 Knapsack Problem. It assigns importance scores to these categories based on empirical findings (e.g., Method Signatures > Identifiers > Symbols). When a user wants to reduce code size by X%, SlimCode identifies and removes the least important tokens first until the target is met.

Experimental Evaluation

The study utilized the CodeSearchNet dataset, specifically focusing on the Java language extensions. They evaluated two primary downstream tasks:

  • Code Search: Finding the right code snippet given a natural language query (measured by Mean Reciprocal Rank or MRR).
  • Code Summarization: Generating a natural language description of what a code snippet does (measured by BLEU-4 score).

The evaluation included "traditional" pre-trained models (CodeBERT, CodeT5) and modern LLMs via API (GPT-4).

Key Results and Discussion

The empirical results demonstrate that SlimCode significantly outperforms existing attention-based methods while offering substantial efficiency gains.

Performance and time savings for Code Search across different pruning ratios.

Efficiency and Performance

  • Massive Speedup: SlimCode is 133 times faster at pruning code than DietCode because it doesn't need to perform a model inference pass to decide what to delete.
  • Better Accuracy: Despite being simpler, SlimCode outperformed DietCode by 9.46% in MRR (Code Search) and 5.15% in BLEU (Summarization).
  • Linear Time Savings: As shown in the experimental tables, there is a nearly 1:1 linear relationship between the amount of code removed and the time saved during training for code search tasks.
Performance and time savings for Code Summarization across different pruning ratios.

Insights into Code Structure

  • The "Symbol Tax": Symbol tokens (like {, }, (, )) account for over 50% of the total tokens in a typical code snippet. However, removing them has almost zero impact on model performance. LLMs are surprisingly good at "filling in the blanks" of code structure.
  • Signatures are Sacred: Method signatures (the function name and its parameters) are the most informative part of the code. Pruning these leads to a massive drop in performance, especially for summarization tasks.
  • Model-Agnosticism: The researchers found that the tokens important to CodeBERT were almost identical to the tokens important to CodeT5. This proves that code importance is a property of the code, not the model.

GPT-4 Cost Efficiency

Using SlimCode to prune 50% of tokens before sending them to GPT-4 actually improved precision by 2.35% compared to using the original code, while reducing API costs by 24%. This suggests that removing "noise" (like symbols) helps the model focus on the core logic.

Conclusion and Future Work

SlimCode provides a blueprint for making AI-assisted software engineering both more powerful and significantly cheaper by leveraging the "natural" structure of code. While the study focused on Java, future work will explore language diversity (e.g., Python), PDG complexity for incomplete code, and a broader range of tasks like code generation or vulnerability detection.

Summary updated

Abstract

Pre-trained Large Language Models (LLM) have achieved remarkable successes in several domains. However, code-oriented LLMs are heavy in computational complexity, and quadratically with the length of the input. Toward simplifying the input program of an LLM, the state-of-the-art approach has the strategies to filter the input code tokens based on the attention scores given by the LLM. The decision to simplify the input should not rely on the attention patterns of an LLM, as these patterns are influenced by both the model architecture and the pre-training dataset. Since the model and dataset are part of the solution domain, not the problem domain where the input belongs, the outcome may differ when the model is pre-trained on a different dataset. We propose SlimCode, a model-agnostic code simplification solution for LLMs that depends on the nature of input code tokens. As an empirical study on the LLMs including CodeBERT, CodeT5, and GPT-4 for two main tasks: code search and summarization, we reported that 1) the removal ratio of code has a linear-like relation with the saving ratio on training time, 2) the impact of categorized tokens on code simplification can vary significantly, 3) the impact of categorized tokens on code simplification is task-specific but model-agnostic, and 4) the above findings hold for the paradigm-prompt engineering and interactive in-context learning. The empirical results showed that SlimCode can improve the state-of-the-art technique by 9.46% and 5.15% in terms of MRR and BLEU score on code search and summarization. Moreover, SlimCode is 133 times faster than the state-of-the-art approach. Additionally, SlimCode can reduce the cost of invoking GPT-4 by up to 24% per API query, while still producing comparable results to those with the original code.
BibTeX
@misc{wang2024natural,
  author = {Yan Wang and Xiaoning Li and Tien Nguyen and Shaohua Wang and Chao Ni and Ling Ding},
  title = {{Natural Is The Best: Model-Agnostic Code Simplification for Pre-trained Large Language Models}},
  year = {2024},
  eprint = {2405.11196},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2405.11196},
}
Figure