[Paper Review] Automatic Code Generation using Pre-Trained Language Models
This paper proposes fine-tuning a pre-trained GPT-2 language model for end-to-end code generation in Python, using function signatures and docstrings as input to generate correct function bodies. The approach achieves a BLEU score of 0.22, representing a 46% improvement over a character-level RNN baseline, demonstrating that large-scale pre-trained models can effectively generate syntactically correct and semantically meaningful code.
Recent advancements in natural language processing \cite{gpt2} \cite{BERT} have led to near-human performance in multiple natural language tasks. In this paper, we seek to understand whether similar techniques can be applied to a highly structured environment with strict syntax rules. Specifically, we propose an end-to-end machine learning model for code generation in the Python language built on-top of pre-trained language models. We demonstrate that a fine-tuned model can perform well in code generation tasks, achieving a BLEU score of 0.22, an improvement of 46\% over a reasonable sequence-to-sequence baseline. All results and related code used for training and data processing are available on GitHub.
Motivation & Objective
- To investigate whether pre-trained language models can be effectively fine-tuned for code generation in a highly structured domain like Python.
- To compare the performance of a fine-tuned GPT-2 model against a character-level RNN baseline in generating function bodies from function signatures and docstrings.
- To evaluate the impact of pre-training on code generation quality, particularly in terms of syntactic correctness and semantic relevance.
- To assess the feasibility of transferring knowledge from natural language pre-training to programming language generation.
Proposed method
- Fine-tuning a small GPT-2 model (117M parameters) on a curated subset of the CodeSearchNet dataset containing 500k (docstring, code) pairs in Python.
- Using a sequence-to-sequence framework where the input is a function signature and docstring, and the output is the corresponding function body.
- Employing byte pair encoding (BPE) for tokenization, which improves code generation quality over character-level embeddings.
- Training the model for 100,000 mini-batch iterations with a batch size of 2, using standard language modeling objectives.
- Evaluating performance using BLEU score on a held-out test set to measure n-gram overlap between generated and reference code.
- Comparing results against a strong baseline: a character-level RNN trained on 10% of the Python data, using 128-token sequences and softmax cross-entropy loss.
Experimental results
Research questions
- RQ1Can a pre-trained language model like GPT-2 be effectively fine-tuned to generate syntactically correct and semantically meaningful Python function bodies from function signatures and docstrings?
- RQ2How does the performance of a fine-tuned GPT-2 model compare to a character-level RNN baseline in code generation tasks?
- RQ3To what extent does pre-training on large-scale natural language data transfer to code generation in a structured programming language like Python?
- RQ4Does the use of byte pair encoding (BPE) improve code generation quality compared to character-level tokenization?
Key findings
- The fine-tuned GPT-2 model achieved a BLEU score of 0.22 on the code generation task, significantly outperforming the character-level RNN baseline.
- The GPT-2 model showed a 46% relative improvement in BLEU score over the RNN baseline, which achieved a score of 0.015 on the same evaluation set.
- The model generated novel code with correct Python syntax, including proper use of if-statements, function calls, and method definitions, as verified by qualitative analysis.
- The model demonstrated robust generalization, producing semantically relevant code even when prompted with diverse function signatures and documentation.
- The use of BPE tokenization in GPT-2 led to better performance and more coherent code generation compared to character-level embeddings.
- Training loss and BLEU score continued to improve over time, indicating that further fine-tuning could yield additional gains.
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.