Source-linked AI summary
A Self-Attentional Neural Architecture for Code Completion with Multi-Task Learning
Fang Liu, Ge Li, Bolin Wei, Xin Xia, Zhiyi Fu, Zhi Jin
TL;DR
Prior code-completion models leave hierarchical program structure and relationships between prediction tasks underused. This paper combines node-to-root path information, Transformer-XL, and multi-task learning; experiments report better results than previous state-of-the-art models.
Problem
Method
The model represents hierarchical context with the path from the predicting node to the AST root, uses Transformer-XL, and jointly learns two related code-completion tasks with multi-task learning.
Source: §1 · p. 2, §8 · p. 10Results
The proposed model achieves better results than previous state-of-the-art models in experiments on three real-world datasets.
Source: §1 · p. 2, §8 · p. 10Takeaways & Limitations
The authors present the work as a potential building block for other programming-language-modeling research.
Source: §8 · p. 10Further studies are needed to validate generalization to other programming languages, and more user evaluation is needed to confirm and improve the model’s usefulness.
Source: §6.5 · p. 10
Abstract
from arXiv · showhide
Code completion, one of the most useful features in the Integrated Development Environments (IDEs), can accelerate software development by suggesting the libraries, APIs, and method names in real-time. Recent studies have shown that statistical language models can improve the performance of code completion tools through learning from large-scale software repositories. However, these models suffer from three major drawbacks: a) The hierarchical structural information of the programs is not fully utilized in the program's representation; b) In programs, the semantic relationships can be very long. Existing recurrent neural networks based language models are not sufficient to model the long-term dependency. c) Existing approaches perform a specific task in one model, which leads to the underuse of the information from related tasks. To address these challenges, in this paper, we propose a self-attentional neural architecture for code completion with multi-task learning. To utilize the hierarchical structural information of the programs, we present a novel method that considers the path from the predicting node to the root node. To capture the long-term dependency in the input programs, we adopt a self-attentional architecture based network as the base language model. To enable the knowledge sharing between related tasks, we creatively propose a Multi-Task Learning (MTL) framework to learn two related tasks in code completion jointly. Experiments on three real-world datasets demonstrate the effectiveness of our model when compared with state-of-the-art methods.
1 INTRODUCTION
The introduction frames code completion as useful for IDEs but identifies gaps in how existing models represent program structure, learn long-range dependencies, and use related prediction tasks. The paper combines explicit AST path information, Transformer-XL, and multi-task learning, and reports improved results on three real-world datasets.
1 INTRODUCTION
- Existing token and flattened AST sequences underuse hierarchical structure, recurrent models struggle with long dependencies, and separate type/value models miss related-task information.The paper notes that LSTM language models use 200 context words on average and treats node type and value as closely related prediction tasks.Source: §1 · pp. 1–2
- The proposed model adds predicting-node-to-root path information, uses Transformer-XL for long-range dependencies, and jointly predicts node type and value through multi-task learning.The paper describes these as responses to the structural, dependency, and task-sharing limitations.Source: §1 · p. 2
- The model reports next-node type accuracies of 87%, 82%, and 91% on Python, Java, and JavaScript, respectively.Type accuracy improves over Nested N-gram by 51%, 40%, and 72%, while next-node value accuracy is 73%, 73%, and 83% on the three datasets.Source: §1 · p. 2
- Experiments on Python, Java, and JavaScript compare the model with Nested N-gram and Pointer Mixture Network baselines.The introduction reports statistically significant improvements over the baselines, with non-negligible effect sizes.Source: §1 · p. 2
2 MOTIVATING EXAMPLE
The examples show how the path from a predicting AST node to the root supplies hierarchical context absent from a flattened sequence. They also illustrate how node type and value can inform one another, motivating joint prediction.
2 MOTIVATING EXAMPLE
- For the Break example, the path {body, If, body, While, Module} identifies the node’s nested position, whereas flattening omits that hierarchical level.The path represents the predicting node’s hierarchy in the AST.Source: §2 · pp. 2–3
- Node types can constrain values: Num suggests a numeric value, while NameLoad signals variable access and helps identify a previously defined variable.The examples present the relationship in both directions: values can also inform type predictions.Source: §2 · p. 3
- The examples motivate learning node type and value jointly so their common features can support both prediction tasks.The paper contrasts joint learning with training separate models that ignore task correlations.Source: §2 · p. 3
3 BACKGROUND
The background introduces statistical language modeling, then contrasts N-gram and recurrent models with attention-based approaches for code. It also describes multi-task learning as knowledge transfer across related tasks, with shared representations as one common implementation.
3 BACKGROUND
- N-gram models restrict a token’s probability to the n − 1 most recent tokens, while recurrent models can capture longer dependencies than N-grams.LSTM and GRU use gating to selectively retain or forget context information.Source: §3 · p. 3
- LSTM language models use about 200 context words on average, motivating attention mechanisms that connect distant token pairs.The background describes Transformer as replacing recurrent layers with multiheaded self-attention, while Transformer-XL adds recurrence to address fixed-length context.Source: §3 · p. 3
- Multi-task learning transfers knowledge across related tasks; hard parameter sharing uses shared hidden layers and task-specific output layers.The background says shared representations can capture common features and reduce overfitting risk.Source: §3 · p. 3
4 PROPOSED MODEL
The model combines AST-based program representation, a Transformer-XL partial-AST encoder, and a path2root encoder to predict the next node’s type and value jointly. Its shared encoders and task-specific output layers support joint learning of those predictions.
4.1 Overall Architecture
- The architecture combines partial-AST and path2root encoders to predict the next node’s type and value, sharing both encoders across tasks while using task-specific output layers.The two tasks are learned jointly through hard parameter sharing.Source: §4.1 · pp. 3–4, Figure 2 · p. 4
4.2 Program Representation
- Programs are flattened into in-order depth-first AST node sequences, with each node represented by concatenated type and value vectors.Nodes use the form Type[value]; non-leaf nodes without a value use EMPTY.Source: §4.2 · p. 4
4.3 Partial AST Encoder
- Transformer-XL encodes the partial AST and reuses hidden states from previous segments as memories, enabling long-range dependencies beyond a fixed-length context.Its cached states are stop-gradient, and relative positional embeddings preserve positional information when states are reused.Source: §4.3 · p. 4
4.4 Path2root Encoder
- A bidirectional LSTM encodes the path from the predicting node to the AST root, representing the node’s hierarchical position.The path vector is designed to reduce the chance of forgetting information from top or bottom nodes on long paths.Source: §4.4 · pp. 4–5
4.5 Task-specific Output Layers
- For each task, the model concatenates the partial-AST output and path vector, then uses a task-specific output layer and softmax to produce predictions.The two predicted attributes are node type and value.Source: §4.5 · p. 5
4.6 Training
- Training minimizes a weighted sum of task-specific losses, with nonnegative weights that sum to one and default weights of 0.5 for each task.The paper discusses the effect of alternative weight settings in Section 6.Source: §4.6 · p. 5
5 EXPERIMENTS AND ANALYSIS
Experiments evaluate AST-node prediction on Python, Java, and JavaScript, compare the model with established baselines, and use ablations to examine its components.
5.1 Dataset and Metrics
- Python, Java, and JavaScript each contribute 100,000 training and 50,000 test programs; ASTs are serialized in-order depth-first, with one prediction query per node.Source: §5.1 · p. 5
- Evaluation uses top-1 accuracy and normalized improvement; value-prediction upper bounds are below 100% because UNK targets count as wrong and depend on OoV rates.Source: §5.1 · p. 5
5.3 Research Questions and Results
- Across all three datasets, the model beats every baseline on type and value prediction, with type accuracies 87%, 82%, 91% and value accuracies 73%, 73%, 83%.Nested Cache N-gram has no value-prediction results because its token-sequence input could supply only the complete type sequence.Source: Table 2 · p. 6, §5.3 · p. 6
5.2 Experimental Setup
- The partial-AST encoder uses six-layer Transformer-XL with 256 cached states, while accuracy saturates near 1,024 context tokens; path2root retains or pads five nodes.Source: §5.2 · p. 6
5.3 Research Questions and Results
- Removing MTL or path2root lowers accuracy, with MTL removal causing a larger drop; removing recurrence drops accuracy below vanilla Transformer-XL.Source: Table 3 · p. 7, §5.3 · p. 7
- JavaScript gains were smaller; the authors suggest its 95 node types and weaker type–value correlation may limit improvement.Source: §5.3 · p. 7
6 DISCUSSION
The analyses report lower test loss and a smaller train–test gap, modest parameter and training costs relative to Pointer Mixture Network, sensitivity to task-loss weights, and qualitative gains alongside stated validity limits.
6.1 Learning Process Analysis.
- The proposed model's test loss is lower than the baseline at every epoch, with a smaller train–test loss gap; the same observations hold on Java and JavaScript.The authors attribute performance to hierarchical AST information and related-task training signals, Transformer-XL's long-range modeling, and multi-task learning's regularization.Source: Figure 4 · p. 7, §6.1 · p. 7
6.2 Training Cost Analysis
- 59% of Pointer Mixture Network's parameter budget and 74% of its training time are used by the model, which the authors report still outperforms it significantly.The full model adds 3.2% parameters over vanilla Transformer-XL (95.8M to 98.9M); removing MTL reduces training from 25 to 22 hours, while removing path2root reduces it by five hours.Source: Table 4 · p. 8, §6.2 · p. 8
6.4 Qualitative Analysis
- On JavaScript, SwitchStatement accuracy is 88.2% for our model versus 45.9% for Pointer Mixture Network; our model outperforms it across difficult types.The model's difficult-type accuracies mostly fall between 88% and 93%, while the baseline's low-frequency-token accuracies are much lower.Source: Table 6 · p. 8, §6.4 · p. 8
6.3 Effect of Weights for Task-specific Loss.
- Assigning more weight to one task's loss increases that task's accuracy, but a high weight on one task weakens the advantage of multi-task learning.The analysis varies the weights for next-node type and value prediction; setting either weight to zero yields a single-task model.Source: §6.3 · p. 8, Table 5 · p. 8
6.4 Qualitative Analysis
- Examples show path-to-root cues helping type predictions and auxiliary type prediction helping the value task retrieve a previously used value through the pointer.When the path is ambiguous, the model can miss the target return; it ranks the correct prediction second, while the baseline ranks it third.Source: §6.4 · p. 9
6.5 Threats to Validity
- The evidence comes from GitHub datasets in Python, Java, and JavaScript; further language validation and user evaluation are needed, and task-loss weights were hand-tuned.The authors also describe their code-completion case study as small scale.Source: §6.5 · pp. 9–10
7 RELATED WORK
Prior code-completion work spans heuristic, statistical, and neural approaches. Although multi-task learning had been used in other fields, it had not been applied to programming-language processing, motivating joint prediction of AST node type and value.
- Early code-completion systems used heuristics or static type information, similar code examples, or program history.Source: §7 · p. 10
- Cache-based N-grams exploited source-code localness, while nested-cache N-grams modeled vocabulary, scope, locality, and dynamism and outperformed statistical and deep-learning language models.Source: §7 · p. 10
- Neural work included vanilla LSTMs and sparse-pointer RNNs [3], while LSTM-based models were considered insufficient for long-term dependencies [19].Source: §7 · p. 10
- Li et al.'s Pointer Mixture Network [21] addressed OoV and achieved state-of-the-art results, making it a baseline for this paper.Source: §7 · p. 10
- Multi-task learning had not been applied to programming-language processing; this paper jointly predicts AST node type and value to use information from related tasks.Source: §7 · p. 10
8 CONCLUSIONS AND FUTURE WORK
The paper concludes that its MTL-based self-attentional architecture, with explicit hierarchical representation and Transformer-XL, outperforms previous state-of-the-art models. It proposes adding domain-specific constraints such as grammar rules in future work.
- The model combines explicit representation of the predicting node's hierarchical information, Transformer-XL for long-term dependencies, and MTL for knowledge sharing between related tasks.Source: §8 · p. 10
- The authors report better results than previous state-of-the-art models and, to their knowledge, are first to apply MTL and Transformer-XL to code completion.Source: §8 · p. 10
- Future work will introduce domain-specific constraints such as grammar rules to improve the model's effectiveness.Source: §8 · p. 10