Deepmind built a competitive coding AI called AlphaCode here's the gist of it
Tags: deepmind, machinelearning, transformer
Published: 2022-02-11 00:49:15

---
title: Deepmind built a competitive coding AI called AlphaCode here's the gist of it
published: true
date: 2022-02-06 16:33:00 UTC
tags: deepmind,machinelearning,transformer
canonical_url: https://dramsch.net/posts/deepmind-built-a-competitive-coding-ai-called-alphacode-heres-the-gist-of-it/
---

![](https://dramsch.net/images/banner/banner-deepmind-alphacode.thumbnail.jpg)

 

I'm obsessed with Alphacode.

How do you go from a mediocre text generator to an AI that can compete competitively?

Deepmind attempts this with AlphaCode.

Let's look at their research together.

> I can safely say the results of AlphaCode exceeded my expectations. I was sceptical because even in simple competitive problems it is often required not only to implement the algorithm, but also (and this is the most difficult part) to invent it. AlphaCode managed to perform at the level of a promising new competitor. I can't wait to see what lies ahead! ~ Mike Mirzayanov

![Quick scroll though the Alphacode blog post](https://dramsch.net/images/alphacode.gif)

## 😼 The Problem

Competitive programming is a whiteboard interview on steroids.

You get:

- Text problem statements
- Description of inputs
- Specifics of outputs

You have to understand and implement a solution.

Finally there are hidden tests.

The Backspace Problem [(Mirzayanov, 2020)](https://ioi.te.lv/oi/pdf/v14_2020_133_142.pdf):

![The Backspace Problem (Mirzayanov, 2020)](https://dramsch.net/images/alphacode-backspace-problem.jpg)

## 💾 The Data

Best results were obtained by Pre-training on the entirety of Github. Not just Python (the target language) but C++, C#, Go, Java, Rust......

Then fine-tuning the AI on the CodeContests dataset

1. Teach AI code
2. Get into competitions
3. ???
4. Profit!

## 3️⃣ ???

There is a third step:

Reduce False Positives.

Create extra hidden tests.

Large test variance reduces false positives.

Implementing "slow positives" reduces inefficient code.

Going from 12 to 200+ tests/problem shows significant improvement!

![False positive rate on different datasets](https://dramsch.net/images/alphacode-codeforces-false-positives.jpg)

## ☝️ Intermission: Amazing tools

"Hypothesis" can automatically generate more tests for you:

[https://hypothesis.works](https://hypothesis.works)

Advent of code is competitive coding light often with "slow positives":

[adventofcode.com](https://adventofcode.com)

[My Short essay](https://dramsch.net/posts/every-december-i-become-a-better-programmer-and-you-could-too/)

## 🔬 The Evaluation

You can't just submit solutions until one works in competitive coding.

So to simulate that Deepmind settled on a n@k metric.

Submit n solutions to k coding problems.

They're going for n=10, so 10@k to evaluate AlphaCode.

(Also pass@k as an upper bound.)

![Performance of Deepmind AlphaCode on different problems](https://dramsch.net/images/alphacode-nk.jpg)

## 🧮 The Algorithm

- Train a model on a bunch of code.
- Then focus on contests. 
- Then generate a bunch of potential solutions.
- Filter out trivial solutions.
- Cluster similar solutions.
- Select candidates (at most 10).
- Submit and evaluate.

![The Deepmind AlphaCode algorithm](https://dramsch.net/images/alphacode-algorithm.jpg)

## 🤖 It's a transformer!

Specifically, encoder-decoder architecture takes flat text + tokenized metadata.

Tokenizer: [SentencePiece (Kudo & Richardson, 2018)](https://arxiv.org/abs/1808.06226)

Internally, uses [multi-query attention (Shazeer, 2019)](https://arxiv.org/abs/1911.02150) but shared key & value per attention block.

[Visualize the multi-query model here](https://alphacode.deepmind.com/)

## 🦾 Let's stop here. This is long.

AlphaCode is an above-average competitive programmer.

The paper has ablation studies on tricks in the transformer

and compares impact of different pre-training datasets

and different training tricks.

It's a [great read](https://storage.googleapis.com/deepmind-media/AlphaCode/competition_level_code_generation_with_alphacode.pdf).

![Performance of AlphaCode on the Codeforces website.](https://dramsch.net/images/alphacode-performance.jpg)

## Conclusion

- Pre-train on huge GitHub data
- Fine-tune on CodeContest data with extra test cases
- Transformer with multi-query attention
- Generate then filter 99% & cluster solutions
- Submit a max of 10

Read the [Deepmind blog post](https://www.deepmind.com/blog/article/Competitive-programming-with-AlphaCode)