Source-linked AI summary

Pythia: AI-assisted Code Completion System

Alexey Svyatkovskiy, Ying Zhao, Shengyu Fu, Neel Sundaresan

arXiv:1912.00742v1cs.SEcs.LG

TL;DR

Pythia addresses the problem of unranked code-completion suggestions by generating ranked method and API recommendations with a neural model trained on AST-derived code contexts. It achieves 92% top-5 accuracy on a large Python corpus and is deployed in Visual Studio Code, with model-size trade-offs for lightweight devices.

  • Problem

    Traditional IDE completion lists methods alphabetically, motivating ranked suggestions that reduce manual search through candidate methods.

  • Method

    Pythia uses LSTM models trained on long-range code contexts extracted from abstract syntax trees, incorporating inferred type information.

  • Results

    92% top-5 accuracy was achieved on 15.8 million method calls, beating simpler baselines.

  • Takeaways & Limitations

    Pythia provides ranked method and API recommendations at edit time and is deployed as part of Intellicode in Visual Studio Code.

  • Takeaways & Limitations

    The reported scope is Python; advanced deep-learning code completion for other programming languages remains future work.

Abstract

from arXiv · show

In this paper, we propose a novel end-to-end approach for AI-assisted code completion called Pythia. It generates ranked lists of method and API recommendations which can be used by software developers at edit time. The system is currently deployed as part of Intellicode extension in Visual Studio Code IDE. Pythia exploits state-of-the-art large-scale deep learning models trained on code contexts extracted from abstract syntax trees. It is designed to work at a high throughput predicting the best matching code completions on the order of 100 $ms$. We describe the architecture of the system, perform comparisons to frequency-based approach and invocation-based Markov Chain language model, and discuss challenges serving Pythia models on lightweight client devices. The offline evaluation results obtained on 2700 Python open source software GitHub repositories show a top-5 accuracy of 92\%, surpassing the baseline models by 20\% averaged over classes, for both intra and cross-project settings.

1 Introduction

Pythia addresses the limitation of alphabetically ordered code-completion lists by ranking likely methods with a neural model trained on code contexts. Its evaluation reports 92% accuracy on 15.8 million method calls, outperforming simpler baselines.

  • 1 Introduction: Traditional IDE completion lists methods alphabetically, forcing developers to scroll through many choices instead of receiving ranked suggestions.Prefix filtering is commonly used to reduce the number of choices.
  • 1 Introduction: Pythia predicts the most likely method from a code snippet using a model that scores candidate responses.The task is defined over a vocabulary and the set of possible methods.
  • 1 Introduction: LSTM networks process partial ASTs containing member-access expressions and module-function invocations to capture semantics from distant nodes.The approach exploits the long-range sequential nature of source code.
  • 1 Introduction: 92% accuracy was achieved on 15.8 million method calls, surpassing simpler frequency-based and Markov Chain baselines.The paper also documents deployment and training challenges for high-throughput use on lightweight devices.

2 Baseline code completion systems

The baseline systems rank methods using popularity or invocation-history patterns. They include alphabetic ordering, frequency models with conditional context, and Markov Chains based on recent method-call sequences.

  • 2.1 Frequency models: Alphabetic ordering lists all possible attributes or methods without ranking, becoming difficult to use for classes with many members.It can work adequately when a class has relatively few members.
  • 2.1 Frequency models: Frequency models order methods within each class by their occurrence counts in the training corpus.The frequency-if variant maintains separate popularity lists for calls inside and outside if-statements.
  • 2.1 Frequency models: The frequency-if model increases overall accuracy by using whether a call occurs inside an if-statement as context.Table 1 contrasts popular TensorFlow calls in the two contexts.
  • 2.2 Invocation-based Markov Chain model: More than 20% of chains following os.path.isfile → os.remove lead to either os.rename or shutil.move.This illustrates sequential invocation patterns used for prediction.
  • 2.2 Invocation-based Markov Chain model: Markov Chain completion predicts the next invocation from previous invocations in the same document scope and class.An n-th order model estimates the next-call probability from the preceding n −1 invocations.

3 Dataset

The dataset consists of diverse open-source Python repositories from GitHub, with repository-level splits used for development and testing. The training and evaluation corpus contains over 15.8 million method calls.

  • 3 Dataset: 2700 top-starred, non-fork Python projects were selected from GitHub across scientific computing, machine learning, dataflow programming, and web development.The corpus contains over 15.8 million method calls.
  • 3 Dataset: The dataset was split 70-30 into development and test sets at the repository level.The development set was then divided 80-20 into training and validation sets.
  • 3 Dataset: Figure 2 summarizes method-call counts for the ten libraries with the most occurrences in the training dataset.The figure provides a library-level view of corpus composition.

4 Representing code snippets

Pythia represents code as serialized AST context for LSTM processing, while adding type information and normalization to make representations more robust to Python’s dynamic typing and naming variation.

  • 4 Representing code snippets: ASTs provide structural representations of source code, with non-leaf nodes encoding grammar structure and leaves encoding syntax tokens.Pythia uses partial file-level ASTs corresponding to relevant code snippets.
  • 4 Representing code snippets: Partial ASTs are serialized by in-order depth-first traversal, retaining up to T preceding lookback tokens for each method call.These sequences are used as LSTM inputs.
  • 4 Representing code snippets: Word2Vec-style lookup tables map discrete syntax nodes and tokens into dense low-dimensional vectors before LSTM processing.The vectors are learned through backpropagation along with the LSTM weights.
  • 4 Representing code snippets: The preprocessing workflow parses files into ASTs and extracts method invocations, syntax sequences, invocation spans, and receiver-token runtime types.The resulting metadata supports training and online recommendation serving.
  • 4.1 Leveraging type information: Type inference adds receiver and local-variable type information to training sequences because Python performs type checking at runtime.This helps represent aliased imports and other naming variations.
  • 4 Representing code snippets: Variable names are normalized using a var:<variabletype> convention to reduce vocabulary dependence on developer-specific spelling.Normalization also limits vocabulary growth.

5 Neural code completion model

Pythia models method completion as sequence prediction over AST-derived code contexts, using an LSTM to encode syntax tokens and predict the most likely method. Reusing the input embedding matrix for output classification reduces model size by removing a large fully connected layer.

  • Method completion: Pythia predicts a method token from terminal AST syntax tokens and an end-of-sequence marker representing a code snippet.The prediction is conditioned on the serialized syntax-token sequence.
  • LSTM model: The model uses a stacked LSTM whose hidden state is transformed into vocabulary probabilities with an output projection and softmax.The LSTM processes the current input and previous hidden state at each temporal step.
  • Predicted embedding: Pythia reuses the input word-embedding matrix as the output classification matrix, removing the large fully connected layer and reducing trainable parameters and disk size.A projection matrix maps the final hidden state into the predicted embedding space.
  • Architecture: The deployed neural network architecture is documented as Figure 4.The figure presents the architecture of the neural network used in Pythia.

6 Model training

Pythia is trained with stateful backpropagation and data-parallel distributed optimization, using batching, truncated sequence gradients, and learning-rate scheduling. Model selection tunes architecture and other hyperparameters while balancing predictive accuracy against serving cost.

  • Distributed training: Pythia trains replicated neural models with data-parallel distributed optimization, processing different mini-batches in parallel lockstep with Adam.The offline training module integrates TensorFlow, CUDA-aware MPI, and GPU infrastructure.
  • Sequence training: Truncated backpropagation through time approximates gradients for long sequences to address the gradient-vanishing problem.The model considers sequence lengths from 100 to 1000 to capture long-range dependencies.
  • Batching: Training batches variable-length sequences by sorting them into length buckets, padding within buckets, and masking padding tokens from loss calculation.A training buffer maintains sequences from distinct ASTs for efficient GPU utilization.
  • Learning-rate schedule: The learning rate decays exponentially by epoch and is scaled with the number of workers during a four-epoch warm-up period.The scaling fraction α = 4 was found to work best through hyperparameter search.
  • Training regimes: 92% is the target model accuracy shown against validation accuracy across serial, distributed, and learning-rate training regimes.Figure 6 compares serial and eight-worker distributed training, plus multiple learning-rate schedules.
  • Hyperparameter tuning: Architecture is treated as a hyperparameter, with LSTM, GRU, attention-based LSTM, and alternative classification layers considered.Random search selects the best-performing configuration on the validation set.
  • Architecture selection: Removing the large fully connected classification layer reduces model size on disk by 25%, while attention achieves the best top-5 accuracy but produces an 8% larger, slower model.The deployed choice is LSTM with predicted embedding because serving requires balancing accuracy and model size.

7 Evaluation

Pythia is evaluated against multiple recommendation baselines using top-k accuracy and mean reciprocal rank, with results showing substantial gains over the invocation-based Markov Chain model.

  • Evaluation metrics: Top-k accuracy measures whether relevant recommendations appear within the first k suggestions, while MRR measures the overall rank of recommendations.Top-1 focuses on the first recommendation; top-5 reflects whether the desired suggestion appears among five recommendations.
  • Baseline comparison: Pythia significantly outperforms alphabetic, frequency-based, and invocation-based Markov Chain baselines, especially for top-1 accuracy.The comparison is reported in Table 5 across the evaluated completion recommendations.
  • Baseline comparison: Over 50% accuracy improvement occurs for nearly 6000 completion classes relative to the Markov Chain baseline.The comparison is shown as a histogram of relative accuracy differences on the test set.
  • Coverage: The Markov Chain baseline has lower coverage because it relies on type inference, leaving uncovered classes in an overflow bin.The reported comparison therefore includes classes that the Markov Chain model does not cover.

8 Model deployment

Pythia is deployed in Visual Studio Code, where quantization addresses lightweight-device constraints by reducing model size while retaining most top-5 accuracy.

  • Deployment: Pythia is deployed as part of the Intellicode extension in Visual Studio Code, with latency and memory footprint identified as key lightweight-device challenges.The system targets online prediction during code editing.
  • Quantization: Quantization reduces storage precision by processing weights and activations layer-by-layer using minimum and maximum values, zero shifting, and scaling.The procedure converts the 32-bit floating-point model representation into a lower-bit representation.
  • Quantization: 8-bit post-training quantization reduces the model from 152 MB to 38 MB while lowering top-5 accuracy from 92% to 89%.The quantized model is one quarter the original size.
  • Online recommendations: Pythia serves online method recommendations such as optimizer calls, including Adam and SGD among its top five suggestions.The example also includes Saver, exponential_decay, and Feature recommendations in a TensorFlow training context.

9 Conclusions

The paper presents Pythia as a deployed neural code-completion system that uses long-range AST-derived contexts and achieves strong evaluation performance. It also documents deployment challenges and identifies broader language support as future work.

  • Conclusions: Pythia generates ranked method and API recommendations using LSTM networks trained on long-range code contexts extracted from abstract syntax trees.The system is deployed as part of the Intellicode extension in Visual Studio Code.
  • Conclusions: 92% top-5 accuracy on 15.8 million method calls shows that the best model beats simpler baselines.The evaluation uses method calls extracted from real-world source code.
  • Deployment challenges: The paper documents practical challenges in training, tuning, and deploying deep neural networks on lightweight client devices for edit-time prediction.These challenges include serving models under client-device constraints.
  • Future work: Advanced deep-learning approaches for languages beyond Python remain future work, while current non-Python Intellicode completions use a Markov Chain model.The stated languages include C#, Java, C++, and XAML.
Loading 1912.00742v1…