Abstract
Large-scale pre-trained models (PTMs) for code, such as CodeBERT and CodeT5, have revolutionized tasks like code search and summarization. However, their Transformer-based architecture imposes a heavy "computational tax" due to quadratic complexity relative to input length. This paper introduces DietCode, a framework designed to simplify source code inputs by pruning non-essential tokens. By analyzing attention weights, the authors demonstrate that PTMs act as "skimmers," prioritizing method signatures and data types over complex internal logic. DietCode leverages this insight to reduce computational costs by up to 40% with minimal impact on accuracy across Java and Python benchmarks.
Introduction
Source code is inherently verbose. While humans require full context for execution and debugging, PTMs often derive sufficient semantic meaning from a subset of the code. As models grow, the cost of processing long, complex functions becomes a bottleneck for fine-tuning and inference. DietCode addresses this by identifying and retaining only the "critical" parts of a program, effectively putting the code on a "diet" before it reaches the GPU.
Empirical Motivation: How Models "Read" Code
To understand what information PTMs value, the researchers conducted an empirical study on CodeBERT's attention mechanisms. They found that attention is not distributed uniformly; instead, it concentrates on specific "functional" tokens and "structural" statements.
As shown in the keyword analysis, access modifiers like public and private receive significantly higher attention than logic-heavy keywords like if, switch, or int. This suggests that the model relies on these keywords as anchors for method signatures, which contain dense semantic information about the function's purpose. In contrast, branching logic actually receives some of the lowest attention weights.
The DietCode Framework
DietCode simplifies programs using three primary strategies, with Attention-based Pruning serving as the core innovation. This method uses the pre-trained model's own internal weights to determine token importance.
- Statement Selection (Knapsack Optimization): The pruning process is modeled as a 0-1 Knapsack Problem. Each statement is treated as an item with a "value" (its cumulative attention weight) and a "weight" (its token length). The algorithm maximizes the total attention value within a predefined token capacity.
- Token Pruning: After selecting key statements, the framework further prunes low-attention tokens—such as auxiliary punctuation or generic variables—to reach the exact target length.
Experimental Results
The authors evaluated DietCode using the CodeSearchNet corpus for Java (~900k pairs) and Python (~824k pairs). The results demonstrate a significant efficiency-performance trade-off:
- Efficiency Gains: DietCode reduced FLOPs and execution time by approximately 40%. For example, fine-tuning time for Java on CodeBERT was nearly halved, dropping from 21 to 11 hours.
- Performance Retention: Despite the drastic reduction in input size, performance remained robust. In Java Code Search, the Mean Reciprocal Rank (MRR) only decreased from 0.74 to 0.71.
- Model Agnostic: The pruning strategy proved effective across various architectures, including CodeT5 and RoBERTa, indicating that "skimming" is a general property of code PTMs.
Conclusion and Future Work
DietCode proves that PTMs treat code more like natural language than formal logic, relying on identifiers and structural markers rather than full executability. While the simplified code is no longer runnable, it remains semantically rich for representation learning. Future research may explore "naturalizing" code structures—converting syntax like brackets into descriptive tokens—to further bridge the gap between structural logic and model understanding without increasing verbosity.
Summary updated