TL;DR
This paper introduces PyCoder, a novel approach to code completion that achieves syntax awareness without requiring syntactically correct code during inference. By leveraging "token types" (like keywords, identifiers, and literals) in a multi-task learning framework, PyCoder significantly outperforms existing methods on both token-level and line-level code completion tasks, making it more practical for real-world, on-the-fly use.
What Problem Does This Paper Address?
Modern Integrated Development Environments (IDEs) offer code completion to boost developer productivity. Existing approaches generally fall into two camps:
- On-the-fly code completion: These models can suggest code as you type, but often ignore syntactic correctness, leading to potentially flawed suggestions.
- Syntax-aware code completion: These models use Abstract Syntax Trees (ASTs) to ensure syntactic correctness. However, ASTs can only be generated from syntactically valid code. The paper's analysis reveals that developers' code is syntactically incorrect about two-thirds of the time, rendering AST-based approaches impractical for real-time, on-the-fly completion.
This creates a gap: developers need code completion that is both on-the-fly and syntax-aware, but existing methods fail to deliver both simultaneously.
Methods
The core innovation of PyCoder lies in its use of token types as a lightweight, readily available form of syntactic information. Unlike ASTs, token types can be extracted even from incomplete or syntactically incorrect code.
PyCoder employs a multi-task learning (MTT) strategy:
- Target Task: Predict the next code token.
- Supporting Task: Predict the token type of each code token.
This MTT approach is trained using a GPT-2 architecture (specifically, a pre-trained CodeGPT model). During training, the model learns to predict both the code tokens and their types. Crucially, during inference, only the code tokens are needed, allowing for on-the-fly completion.
The paper explores different MTT techniques:
- Hard Parameter Sharing: A single model optimizes both tasks.
- Soft Parameter Sharing: Separate models for each task, with a shared loss.
- Intermediate Fine-Tuning (IFT): Sequential training, first on token types, then on code.
The authors also investigate the impact of task weighting parameters and various decoding methods (like Beam Search, Greedy, Sampling) to optimize performance.
Datasets
The primary dataset used is the ETH PY150 Python dataset. This dataset is a standard benchmark for code completion and is also used in Microsoft's CodeXGLUE benchmark. The authors confirm that it contains no exact code duplication.
For token-level predictions, the dataset is split into:
- Training: 95,000 files
- Validation: 5,000 files
- Testing: 50,000 files
For line-level predictions, a separate testing set of 10,000 Python files from CodeXGLUE is used.
Key Insights and Ideas
- Token Types as a Practical Syntactic Signal: The paper demonstrates that "token types" (e.g., NAME, NUMBER, STRING, KEYWORD) are a viable and effective alternative to ASTs for achieving syntax awareness in code completion, especially when dealing with incomplete code.
- Multi-Task Learning Synergy: Training a model to predict both code tokens and their types significantly boosts performance on the primary code completion task. The supporting task acts as a regularizer and helps the model learn richer representations.
- Hard Parameter Sharing Dominance: Among the MTT strategies explored, Hard Parameter Sharing yielded the best results, suggesting that tightly coupling the learning of code and token type information is highly beneficial.
- Decoding Method Matters: The choice of decoding method has a substantial impact on line-level prediction accuracy, with Beam Search outperforming other methods like Sampling.
- Robustness to Task Weighting: PyCoder shows robustness to task weighting parameters, indicating that the two tasks are complementary rather than conflicting.
Key Results
PyCoder achieved state-of-the-art performance:
- CodeXGLUE Leaderboard: PyCoder-Hard ranked first with an accuracy of 76.93% for token-level predictions and an exact match of 43.91% for line-level predictions.
- Internal Evaluation: PyCoder achieved an accuracy of 77.12% for token-level predictions (0.43%-24.25% better than baselines) and an exact match of 43.37% for line-level predictions (3.63%-84.73% better than baselines).
- Line-Level Improvement: The use of token-type information improved line-level predictions by 6.61%-15.22% compared to models without it.
- Decoding Impact: Beam Search achieved an exact match of 43.37% for line-level predictions, while Sampling achieved only 33.80%.
Limitations and Future Work
- Generalizability: The evaluation was primarily conducted on Python code. While the approach could be extended to other languages, further research is needed.
- Developer Productivity: The study focuses on prediction accuracy metrics. Future work could investigate how PyCoder's predictions directly impact developer productivity through user studies.
- Fully Syntactically Correct Completion: While PyCoder is syntax-aware and on-the-fly, it doesn't guarantee fully syntactically correct code in all cases (e.g., predicting the next line after a newline can still be challenging due to endless possibilities). Developing models that achieve this while remaining on-the-fly is a future research direction.
- Hyperparameter Optimization: Due to resource constraints, extensive hyperparameter tuning was not performed, suggesting that further optimization could yield even better results.
- Computational Cost: While inference cost is similar to existing models, there's an additional cost for token type extraction during training.
Summary updated