Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?


Failed to load latest commit information.
Latest commit message
Commit time

SLM: Structural Language Models of Code

This is an official implementation of the model described in:

"Structural Language Models of Code" [PDF]

Appeared in ICML'2020.

An online demo is available at

This repository currently contains the dataset and the data extractor that we used to create the Java dataset in the paper.

Feel free to open a new issue for any question. We always respond quickly.

Table of Contents


  • python3
  • TensorFlow 1.13 or newer (install). To check TensorFlow version:

python3 -c 'import tensorflow as tf; print(tf.__version__)'

Download our preprocessed Java-small dataset

This dataset contains ~1.3M examples (1.1GB).

mkdir data
cd data
tar -xvzf java-small-preprocessed.tar.gz

This will create a data/java-small/ sub-directory, containing the files that hold training, test and validation sets, a dict file for various dataset properties and histograms, and a grammar file that is used during beam search to distinguish between terminal and non-terminal nodes.

Creating and preprocessing a new Java dataset

To create and preprocess a new dataset (for example, to compare SLM to a new model on another dataset):

  • Edit the file using the instructions there, pointing it to the correct training, validation and test directories.
  • Run the file:




To download the Java-small as raw *.java files, use:

To download the preprocessed dataset, use:

To download the dataset in a tokenized format that can be used in seq2seq models (for example, with OpenNMT-py), use:

The following JSON files are the files that are created by the JavaExtractor. The preprocessed and the seq2seq files are created from these JSON files:

Every line is a JSON object that contains the following fields: num_targets, num_nodes, targets, is_token, target_child_id, internal_paths, relative_paths, head_paths, head_root_path, head_child_id, linearized_tree, filepath, left_context, right_context, target_seq, line.


The C# dataset that we used in the paper was created using the raw (*.cs files) dataset of Allamanis et al., 2018, ( and can be found here:

To extract examples from the C# files, we modified the data extraction code of Brockschmidt et al., 2019:

Querying the Trained Model

To query the trained model, use the following API, where MYCODE is the given code snippet, that includes two question marks (??) to mark the "hole" that should be completed.

To query the expression-prediction model (the "paper model" in the demo website):

curl -X POST -d '{"code": "MYCODE"}'

For example:

curl -X POST -d '{"code": "public static Path[] stat2Paths(FileStatus[] stats) {  if (stats == null) return null;  Path[] ret = new Path[stats.length]; for (int i = 0; i < stats.length; ++i) { ret[i] = ??; } return ret; }"}'

To query the statement-prediction model (the "extended model" in the demo website):

curl -X POST -d '{"code": "MYCODE"}'

For example:

curl -X POST -d '{"code": "@Override public boolean retainAll(Collection<?> collection) { boolean changed = false;     for (Iterator<E> iter = iterator(); iter.hasNext(); ) {         Element elem =;        if (!collection.contains(elem)) {           iter.remove();             ??        }    }    return changed;}"}'


Structural Language Models of Code

  title={Structural language models of code},
  author={Alon, Uri and Sadaka, Roy and Levy, Omer and Yahav, Eran},
  booktitle={International Conference on Machine Learning},


TensorFlow code for the neural network presented in the paper: "Structural Language Models of Code" (ICML'2020)








No releases published


No packages published