TL;DR
This paper introduces SACC, a novel neural network architecture for classifying source code based on its algorithmic knowledge points. SACC leverages sparse attention mechanisms within a Transformer model, combined with Abstract Syntax Tree (AST) representations, to efficiently and accurately capture both the structural and lexical nuances of code, outperforming existing methods.
What Problem Does This Paper Address?
Accurately categorizing source code by its underlying algorithms is crucial for programming education platforms, enabling better learning resources and management. Traditional methods often treat code as plain text, losing valuable structural information. While AST-based approaches improve this by representing code as trees, they can suffer from slow training times and information loss due to long sequences. This paper aims to bridge this gap by developing a method that effectively utilizes AST structure and accelerates training through sparse attention.
Methods
The proposed SACC (Sparse Attention-Based Neural Network for Code Classification) method has two main steps:
- AST Parsing and Encoding: Source code is first parsed into an Abstract Syntax Tree (AST). This AST is then split into sequences of "statement trees" (subtrees representing individual statements). A recursive neural network (RvNN) is used to encode these statement trees, capturing both lexical and structural information into high-dimensional representations. This process also extracts an "AST-pattern" representing the tree structure.
- Sparse Attention Transformer Classification: The encoded statement tree sequences are fed into a Transformer model. Crucially, this Transformer incorporates sparse attention mechanisms. These mechanisms are designed to reduce the computational cost of standard self-attention by selectively focusing on relevant parts of the sequence, rather than computing attention scores for all possible pairs. SACC integrates local, global, and the novel AST-pattern sparse attention mechanisms to better leverage code structure.
Datasets
The primary dataset used is CodeNet [43], a large-scale dataset containing approximately 14 million code samples across 55 programming languages. For this study, the authors focused on C language code. They also annotated a subset of CodeNet with 18 algorithmic knowledge point labels (e.g., Brute Force, Dynamic Programming, Sorting, etc.), creating a benchmark for code algorithm classification.
Key Insights and Ideas
- AST Structure is Key: The paper strongly emphasizes that code's inherent tree structure (captured by ASTs) is vital for accurate classification and cannot be ignored by treating code as simple text.
- Sparse Attention for Efficiency: Standard self-attention in Transformers can be computationally expensive for long sequences. Sparse attention, by selectively computing attention, significantly speeds up training without sacrificing accuracy.
- Tailored Sparse Attention for Code: The authors introduce an "AST-pattern" sparse attention mechanism that specifically leverages the tree structure derived from the AST, going beyond generic sparse attention patterns used in NLP.
- Balancing Granularity: Splitting the AST at the statement level strikes a balance between preserving structural information and managing sequence length, avoiding the pitfalls of either processing the entire AST or overly simplifying it.
Key Results
- Performance Improvement: SACC achieved the highest accuracy (92.35%) and F1-score (77.51%) among the evaluated methods, outperforming plain text + LSTM (82.88% accuracy) and ASTNN (89.44% accuracy).
- Efficiency Gains: SACC demonstrated significantly faster training times compared to ASTNN and TBCC (Transformer-based code classification). Specifically, SACC's training time per epoch was 747.29 seconds, compared to ASTNN's 1485.53 seconds.
- Ablation Studies: Removing specific sparse attention patterns (local, global, or AST-pattern) led to performance degradation, highlighting the importance of each component. Notably, removing the AST-pattern attention had the largest impact, underscoring the value of structural information.
Limitations and Future Work
- Limitations: The paper acknowledges that while SACC improves upon existing methods, the Transformer model can still face long-term dependency issues with very complex or deeply nested code structures. The current work is primarily focused on single-label classification.
- Future Work: The authors suggest expanding SACC to handle more diversified tasks beyond algorithmic knowledge point classification, such as code difficulty prediction or multi-label classification. They also aim to optimize and adapt the model to cope with these more complex challenges.
Summary updated