Abstract
Researchers have investigated the potential of leveraging pre-trained language models, such as CodeBERT, to enhance source code-related tasks. Previous methodologies have relied on CodeBERT's '[CLS]' token as the embedding representation of input sequences for task performance, necessitating additional neural network layers to enhance feature representation, which in turn increases computational expenses. These approaches have also failed to fully leverage the comprehensive knowledge inherent within the source code and its associated text, potentially limiting classification efficacy. We propose CodeClassPrompt, a text classification technique that harnesses prompt learning to extract rich knowledge associated with input sequences from pre-trained models, thereby eliminating the need for additional layers and lowering computational costs. By applying an attention mechanism, we synthesize multi-layered knowledge into task-specific features, enhancing classification accuracy. Our comprehensive experimentation across four distinct source code-related tasks reveals that CodeClassPrompt achieves competitive performance while significantly reducing computational overhead.
1. Introduction
Classifying source code—whether identifying programming languages, detecting design flaws ("code smells"), or categorizing developer comments—is foundational to modern software engineering tools. Current state-of-the-art methods typically involve fine-tuning models like CodeBERT. However, these methods often rely exclusively on the [CLS] token from the model's final layer as a summary representation.
Because a single vector often lacks the nuance required for complex classification, researchers frequently append heavy auxiliary layers, such as LSTMs or CNNs, to enhance feature representation. This introduces significant computational overhead without necessarily exploiting the rich, hierarchical knowledge (syntactic and semantic) already embedded within the pre-trained model’s hidden layers. We address these limitations with CodeClassPrompt, a framework that utilizes prompt learning and multi-layer knowledge extraction.
2. Methodology
CodeClassPrompt shifts the paradigm from simple fine-tuning to prompt-based learning. The architecture consists of four primary components as illustrated in Figure 1.
2.1 Prompt Wrapper
Instead of feeding raw code directly, we wrap it in a natural language template, such as "It was [MASK]. [Source Code]". This encourages the model to use its masked language modeling (MLM) capabilities to "recall" relevant knowledge about the code snippet.
2.2 Multi-Layer Knowledge Extraction
Research indicates that BERT-style models capture different types of information at varying depths: surface features at the bottom, syntax in the middle, and semantics at the top. CodeClassPrompt extracts the vector for the [MASK] token from layers 2 through 12, capturing a comprehensive knowledge profile.
2.3 Task-Specific Attention
To synthesize these layers, we employ an attention mechanism that learns which layers are most relevant for a specific task. For example, programming language identification may prioritize semantic layers, while technical debt detection might rely more on syntactic middle layers. This approach eliminates the need for external BiLSTM layers, reducing the total parameter count by over 22%.
3. Experimental Setup
We evaluated CodeClassPrompt across four distinct source code tasks using established benchmarks. The dataset characteristics are summarized in Figure 3.
- Code Language Classification: Using the SC++ dataset (19 languages).
- Code Smell Classification: A Java-based corpus for identifying problematic design patterns.
- Code Comment Classification: Over 11,000 code reviews categorized into 16 intention classes.
- Technical Debt (SATD): Comments where developers admit to temporary fixes requiring future rework.
4. Results and Discussion
CodeClassPrompt achieved a new state-of-the-art (SOTA) on the Code Smell dataset with an accuracy of 86.17%, significantly outperforming previous models like EL-CodeBERT. Performance across other tasks remained highly competitive, with accuracies of 87.9% for Language, 95.2% for Comments, and 97.8% for Technical Debt.
Key insights from our analysis include:
- Knowledge Retrieval vs. Compression: Targeting the
[MASK]token via prompts forces the model into a knowledge retrieval mode, producing more discriminative features than simple sequence compression via[CLS]. - Layer Sensitivity: Attention analysis revealed that tasks utilize layers differently. Language classification places 99% of its attention on layer 12, whereas Technical Debt classification distributes attention across layers 7, 8, and 9.
- Efficiency: Removing the supplementary LSTM layers resulted in an 11.18% reduction in inference time and a lower memory footprint, enhancing viability for real-time IDE integration.
5. Conclusion
CodeClassPrompt demonstrates that leveraging the internal hierarchical knowledge of pre-trained models through prompt learning is more effective and efficient than adding external neural layers. Future work will explore automatic prompt generation to optimize the templates and investigate the potential of zero-shot or few-shot learning to reduce the reliance on large labeled datasets.
Summary updated