[Paper Review] StructCoder: Structure-Aware Transformer for Code Generation
StructCoder proposes a structure-aware Transformer encoder-decoder model for code generation that enhances syntactic and semantic fidelity by jointly modeling Abstract Syntax Trees (ASTs) and Data Flow Graphs (DFGs) in both encoder and decoder. It introduces novel auxiliary tasks—AST path prediction and DFG edge prediction—during decoding, achieving state-of-the-art performance on CodeXGLUE and APPS benchmarks.
There has been a recent surge of interest in automating software engineering tasks using deep learning. This paper addresses the problem of code generation, where the goal is to generate target code given source code in a different language or a natural language description. Most state-of-the-art deep learning models for code generation use training strategies primarily designed for natural language. However, understanding and generating code requires a more rigorous comprehension of the code syntax and semantics. With this motivation, we develop an encoder-decoder Transformer model where both the encoder and decoder are explicitly trained to recognize the syntax and data flow in the source and target codes, respectively. We not only make the encoder structure-aware by leveraging the source code's syntax tree and data flow graph, but we also support the decoder in preserving the syntax and data flow of the target code by introducing two novel auxiliary tasks: AST (Abstract Syntax Tree) paths prediction and data flow prediction. To the best of our knowledge, this is the first work to introduce a structure-aware Transformer decoder that models both syntax and data flow to enhance the quality of generated code. The proposed StructCoder model achieves state-of-the-art performance on code translation and text-to-code generation tasks in the CodeXGLUE benchmark, and improves over baselines of similar size on the APPS code generation benchmark. Our code is publicly available at https://github.com/reddy-lab-code-research/StructCoder/.
Motivation & Objective
- Address the limitation of existing code generation models that treat code like natural language, ignoring syntactic and semantic structures.
- Improve code generation quality by explicitly modeling source code's syntax (via AST) and data flow (via DFG) in the encoder.
- Enhance decoder output by training it to predict root-to-leaf AST paths and DFG edges in the target code, ensuring structural correctness.
- Achieve state-of-the-art results on code translation and text-to-code generation tasks while maintaining model efficiency.
- Enable more reliable and semantically accurate code generation by integrating structural inductive biases into both encoder and decoder.
Proposed method
- Augment the encoder input with root-leaf paths from the source code's AST and DFG nodes, using structure-aware self-attention to model dependencies.
- Introduce a modified self-attention mechanism that preserves structural relationships between code tokens, AST nodes, and DFG variables.
- Train the decoder with two auxiliary tasks: predicting all root-to-leaf paths in the target AST and predicting DFG edges in the target code.
- Pre-train the model using a structure-based denoising autoencoder (DAE) task to improve representation learning.
- Use a multi-task learning objective combining masked language modeling, AST path prediction, and DFG edge prediction during fine-tuning.
- Implement efficient attention masking to handle sparse attention patterns between code tokens, DFG variables, and AST leaves on GPU.
Experimental results
Research questions
- RQ1Can joint modeling of AST and DFG structures in both encoder and decoder improve code generation quality compared to standard Transformers?
- RQ2Does training the decoder to predict AST paths and DFG edges lead to more syntactically and semantically correct code generation?
- RQ3How does the inclusion of structural components affect inference efficiency and scalability?
- RQ4To what extent does the structure-aware design improve performance on code translation and text-to-code generation benchmarks?
- RQ5Can structure-aware pretraining and auxiliary tasks generalize across different programming languages and code generation tasks?
Key findings
- StructCoder achieves state-of-the-art performance on the CodeXGLUE benchmark for code translation and text-to-code generation tasks.
- On the APPS benchmark, StructCoder outperforms baselines of similar size, demonstrating improved generalization and correctness.
- The model improves code summarization performance by 4.62 BLEU points on Go (from 19.56 to 24.18), indicating broader applicability.
- Inference time increases by only 28–29% when adding AST and DFG components to the encoder, despite a 75–83% increase in input length, indicating efficient GPU utilization.
- The decoder's auxiliary tasks (AST path and DFG prediction) significantly improve structural fidelity without increasing inference cost.
- The structure-aware self-attention mechanism enables better modeling of code semantics and syntax, especially in complex transformations.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.