TL;DR
This paper introduces Heterogeneous Program Graphs (HPGs) to represent source code, explicitly encoding node and edge types derived from Abstract Syntax Trees (ASTs). By feeding these richer graphs into a Heterogeneous Graph Transformer (HGT), the authors achieve state-of-the-art performance on method name prediction and code classification tasks, demonstrating the importance of type information for accurate code representation.
What Problem Does This Paper Address?
Representing source code in a way that captures its underlying structure and semantics is crucial for many software engineering tasks, from bug detection to code generation. While Abstract Syntax Trees (ASTs) provide structural information, and graph-based methods augment ASTs with additional relationships (like control or data flow), existing approaches often treat these graphs as "homogeneous." This means they overlook the inherent diversity of node types (e.g., identifiers, operators, control flow statements) and edge types (e.g., parent-child relationships, specific field names in ASTs). This homogenization can lead to ambiguous representations, where different code constructs might map to similar graph structures, hindering the model's ability to grasp fine-grained semantics. The paper aims to bridge this gap by proposing a method that explicitly leverages the rich type information present in ASTs to build more informative graph representations.
Methods
The core innovation is the Heterogeneous Program Graph (HPG). Unlike traditional homogeneous graphs, HPGs explicitly encode the types of both nodes and edges. This is achieved by parsing source code into an AST and then using the Abstract Syntax Description Language (ASDL) grammar to assign types to AST nodes (e.g., stmt, expr, identifier) and edges (e.g., test, body, left, right). Additionally, the authors incorporate manually crafted edges like NextSib and NextToken to enhance graph connectivity, similar to prior work.
To process these HPGs, the paper employs the Heterogeneous Graph Transformer (HGT) architecture. HGT is designed to handle graphs with diverse node and edge types by incorporating type-specific attention mechanisms. This allows HGT to learn different relationships and aggregate information in a type-aware manner, making it well-suited for the rich information present in HPGs. The framework also includes a subtokenization strategy for identifiers to handle large vocabularies and out-of-vocabulary words, with experiments comparing shared and independent subtoken schemes.
Datasets
The authors evaluate their approach on four widely-used datasets across two tasks:
- Method Name Prediction (Generation Task):
- CodeSearchNet-Python (CSN-Python)
- Java-small
- Code Classification (Classification Task):
- Python800
- Java250
These datasets are standard benchmarks for their respective tasks, allowing for direct comparison with existing methods.
Key Insights and Ideas
- Type Information is Crucial: The most significant insight is that simply representing code as a graph isn't enough; the types of nodes and edges within that graph carry vital semantic information that homogeneous approaches discard. This type information is key to disambiguating code structures.
- ASDL as a Bridge: Leveraging the ASDL grammar provides a systematic way to extract and assign meaningful types to AST components, forming the foundation for HPGs.
- HGT's Suitability: The Heterogeneous Graph Transformer is a natural fit for HPGs, as its architecture is specifically designed to handle and learn from the type diversity inherent in such graphs.
- Subtoken Schemes Matter: The choice between shared and independent subtoken schemes for identifiers impacts performance differently across tasks. Independent schemes seem better for generation (method name prediction), while shared schemes benefit classification. This suggests that task-specific representation strategies are important.
- Manually Crafted Edges Add Value: Including edges like
NextSibandNextTokenfurther improves performance, reinforcing the idea that enriching graph connectivity can enhance learning.
Key Results
The proposed HPG+HGT framework demonstrates strong performance across the board:
- Method Name Prediction: HPG+HGT (with independent subtokens) outperforms SOTA models like Code Transformer by up to 1.72% on CSN-Python (F1-score) and significantly improves performance on Java-small, often by over 10% on F1-score compared to other baselines.
- Code Classification: HPG+HGT consistently outperforms tree-based and graph-based baselines by at least 2% on both Python800 and Java250 datasets.
- Ablation Studies: Removing heterogeneous types from HPGs leads to a significant drop in performance (F1-score dropping by up to 8%), highlighting the importance of type information. Removing manually crafted edges also negatively impacts accuracy.
Limitations and Future Work
- Language Support: The current implementation relies on ASDL parsers for Python and Java. Extending support to more programming languages would require significant engineering effort, although the ASDL grammar is open-source.
- Dataset Selection: While the authors used widely adopted datasets, they acknowledge that dataset selection can be a threat to validity.
- Decoding Strategy: For generation tasks, the model decodes only over subtoken nodes. While this design choice is validated by an ablation study, exploring decoding over all node representations could be a future direction.
- Engineering Effort: Building HPG parsers for new languages is noted as a potential barrier.
The authors suggest that supporting more programming languages is a key area for future work.
Summary updated