Abstract
Programming languages are defined by rigid syntax and logical flow, yet many deep learning models treat code as simple linear text. This paper introduces StructCoder, a Transformer-based model that explicitly incorporates structural information from Abstract Syntax Trees (AST) and Data Flow Graphs (DFG) into both its encoder and decoder. By utilizing structure-aware self-attention and novel auxiliary tasks—AST Path Prediction (APP) and Data Flow Prediction (DFP)—StructCoder achieves state-of-the-art performance in code translation and text-to-code generation, demonstrating that structural awareness is more effective than simple model scaling.
Introduction
While large-scale language models have revolutionized natural language processing, applying them to programming languages presents unique challenges. Code is governed by formal structural rules (syntax) and specific information flows (semantics). Existing models often struggle to maintain these constraints, leading to "hallucinations" where generated code appears correct but contains syntax errors or logical flaws.
Most prior research has focused on making the encoder structure-aware. However, the decoder—the component responsible for generating the code—is often left to produce tokens without a formal understanding of the tree structure it is constructing. StructCoder bridges this gap by making the entire Transformer architecture structure-aware.
Methodology
StructCoder is built upon the T5 (Text-to-Text Transfer Transformer) architecture, enhanced with several key structural innovations.
Structure-Aware Encoder
The encoder moves beyond linear token sequences by employing a modified self-attention mechanism. It calculates attention scores based on the distance between nodes in an AST and the connectivity within a DFG. Additionally, it embeds "root-to-leaf" paths to provide the model with a hierarchical context for each token.
Structure-Aware Decoder
The primary contribution of this work is the structure-aware decoder, which utilizes two novel auxiliary tasks during generation:
- AST Path Prediction (APP): When generating a token, the decoder must also predict the types of all parent nodes in the AST path leading to that token.
- Data Flow Prediction (DFP): The decoder identifies relationships (edges) between the current token and previous tokens, such as linking a variable usage to its definition.
Pre-training
The model undergoes structure-based pre-training using a Denoising Autoencoding (DAE) objective. Given "corrupted" code, the model must reconstruct the missing text while simultaneously predicting the associated AST paths and DFG edges.
Experimental Results
StructCoder was evaluated on industry-standard benchmarks, including CodeXGLUE and the challenging APPS dataset.
- Code Translation: On the Java-to-C# task, StructCoder achieved a BLEU score of 84.43 and a CodeBLEU of 88.19, establishing a new state-of-the-art.
- Text-to-Code Generation: On the CONCODE dataset, it reached a BLEU score of 41.35, outperforming previous models of comparable size.
- Efficiency and Scaling: On the APPS benchmark, StructCoder (224M parameters) achieved higher strict accuracy than a GPT-2 model with 1.5B parameters. This suggests that structural awareness allows for greater efficiency than simply increasing parameter counts.
Discussion and Conclusion
Ablation studies reveal that AST-related features provide the most significant performance boost, though the combination of AST and DFG yields the best results. While incorporating these structures increases input sequence length by up to 83%, the actual inference time only increases by approximately 29% due to efficient GPU utilization.
Despite its success, StructCoder occasionally struggles with complex mapping errors during translation if variable naming conventions are misleading. Future work will explore incorporating additional constraints, such as security requirements and modularity, into the generation process to further enhance code reliability.
Summary updated