---
title: "Training CodeParrot 🦜 from Scratch"
description: "CodeParrot is a GPT-2 model (110M and 1.5B parameters) trained from scratch for Python code completion, using a 180 GB GitHub Python dump from BigQuery."
url: "https://swe.quest/articles/training-codeparrot-from-scratch-20fa62d39adc1089"
canonical: "https://huggingface.co/blog/codeparrot"
source: "Hugging Face"
published: "2021-12-08"
difficulty: "medium"
kind: "How-to"
reading_minutes: 11
tags: ["model-training","llms","ai-coding","python"]
---

# Training CodeParrot 🦜 from Scratch

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2021-12-08, 11 min read, How-to. Original: [https://huggingface.co/blog/codeparrot](https://huggingface.co/blog/codeparrot)

## Summary

CodeParrot is a GPT-2 model (110M and 1.5B parameters) trained from scratch for Python code completion, using a 180 GB GitHub Python dump from BigQuery. Heavy duplication in that dump (10% of unique files made up 66% of all files) hurt model quality, so deduplication plus Codex-style filtering cut it to a 50 GB clean dataset. The guide walks through training a code-specific tokenizer, initializing a GPT-2-large config with attention stability flags, a streaming constant-length dataset, and a ~150-line distributed training loop with 🤗 Accelerate using gradient accumulation and checkpoints pushed to the Hub. Training ran on 16 A100s for 1 day (small) and 1 week (large) on about 25-30B tokens, and the models were evaluated on HumanEval with the pass@k metric.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [AI for coding](https://swe.quest/tags/ai-coding.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- PyTorch training loops ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- Transformer language models
- Tokenization basics

## Keywords

CodeParrot, GPT-2, Hugging Face Accelerate, HumanEval, Codex, BigQuery, pass@k, DistributedDataParallel

## Related posts

- [StarCoder: A State-of-the-Art LLM for Code](https://swe.quest/articles/starcoder-a-state-of-the-art-llm-for-code-ae224d2374e67db4.md): Hugging Face · 2023-05-04 · Easy · 5 min read · LLMs, AI for coding, Evaluation, Open source
  StarCoder and StarCoderBase are ~15B-parameter code LLMs trained on 1 trillion tokens of permissively licensed GitHub data from The Stack 1.2, covering 80+ languages, commits, issues and notebooks, with StarCoder further fine-tuned on 35B Python tokens.
- [Code Llama: Meta’s state-of-the-art LLM for coding](https://swe.quest/articles/code-llama-meta-s-state-of-the-art-llm-for-coding-c2f826fd04e42ba1.md): Meta Engineering · 2023-08-24 · Easy · 7 min read · LLMs, AI for coding, Model training
  Code Llama is a code-specialized family of Llama 2 models, built by further training Llama 2 on 500B tokens of code (1T for the 70B model).
- [Beat GPT-4o at Python by searching with 100 dumb LLaMAs](https://swe.quest/articles/beat-gpt-4o-at-python-by-searching-with-100-dumb-llamas-907f6997678a8d61.md): Modal · 2024-08-05 · Medium · 7 min read · LLMs, Inference & serving, Evaluation, AI for coding, Serverless
  Sampling LLaMA 3.1 8B Instruct many times and checking each candidate against HumanEval's unit tests raises pass@k from 66.4% at k=1 to 90.5% at k=100, matching GPT-4o's reported 90.2% pass@1, and to 95.1% at k=1000.
- [Code Llama: Llama 2 learns to code](https://swe.quest/articles/code-llama-llama-2-learns-to-code-bbc6e9367fb26138.md): Hugging Face · 2023-08-25 · Medium · 12 min read · LLMs, AI for coding, Inference & serving, Python, Evaluation
  Code Llama is a family of 7B, 13B and 34B Llama 2 derivatives trained on 500B tokens of code.
- [StarCoder2-Instruct: Fully Transparent and Permissive Self-Alignment for Code Generation](https://swe.quest/articles/starcoder2-instruct-fully-transparent-and-permissive-self-alignment-for-code-7d548d6a3e65fc89.md): Hugging Face · 2024-04-29 · Hard · 6 min read · Model training, LLMs, AI for coding, Evaluation
  StarCoder2-15B-Instruct is instruction-tuned entirely on data generated by its own base model, with no human annotations or distillation from proprietary LLMs.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
