FlashPapers phi-1
Microsoft Research, 2023

Textbooks Are All You Need

Suriya Gunasekar, Yi Zhang, Jyoti Aneja, Sébastien Bubeck, Ronen Eldan, Yuanzhi Li, et al.

TL;DR

By training a tiny 1.3B parameter model (phi-1) on extremely high-quality, "textbook-style" synthetic data and filtered web sources, Microsoft achieved a stunning 50.6% pass@1 on HumanEval. This matches or outperforms models 10x to 100x larger, proving that data quality can fundamentally bend traditional scaling laws.

7B Token Dataset 1.3B Parameters 8x A100s, 4 Days Python Specialized

Why This Matters

For years, the dominant paradigm in Large Language Models (LLMs) has been "bigger is better." Modern scaling laws suggest that to improve performance, one must predictably scale up compute, parameter size, and token volume in tandem. This has led to massive, environmentally taxing models trained on trillions of noisy web-scraped tokens.

But what if we are feeding our models the digital equivalent of junk food? Raw web code is often full of boilerplate, poorly documented logic, and non-self-contained snippets. This paper explores a different axis: data quality. By feeding a small model a highly curated "clean diet" of textbook-style explanations and exercises, they demonstrate that we can achieve state-of-the-art reasoning with a fraction of the size.

Interactive: Bending the Scaling Law

Toggle the dataset quality to see how phi-1 breaks the standard token-to-performance ratio.

Model Size
1.3 Billion
Parameters
Dataset Size
7 Billion
Tokens (100x smaller)
HumanEval Score
50.6%
Pass@1 Accuracy
Performance vs. Tokens Seen
Standard Models (100B+ tokens)
phi-1 (7B tokens)

The Big Idea

The core hypothesis of the paper is that educational value matters more than raw volume. To prove this, the researchers curated a dataset specifically designed to teach basic programming concepts, logic, and algorithmic reasoning. They call this "textbook-quality" data.

Instead of training on raw repositories, they pretrain on a mixture of filtered web code and synthetically generated textbooks, then finetune on a small set of synthetic exercises. This curated diet acts as a catalyst, allowing a relatively small transformer model to construct robust mental representations of code logic.

How It Works

The training pipeline of phi-1 consists of two distinct phases: Pretraining on textbook-quality content, followed by Finetuning on targeted exercises.

1. Curating the Dataset

The dataset consists of less than 7 Billion tokens in total, divided into three main pillars:

~6B Tokens

Filtered Web Code

A subset of The Stack and StackOverflow, filtered using a GPT-4 based classifier to keep only highly educational samples.

<1B Tokens

Synthetic Textbooks

Python textbooks generated by GPT-3.5, designed to explain coding concepts, algorithms, and syntax with natural language.

~180M Tokens

Synthetic Exercises

A small set of Python exercises and solutions used for finetuning to align the model to follow function completion tasks.

Interactive: The Educational Quality Filter

The researchers trained a classifier to score code snippets based on their educational value. Adjust the threshold slider below to filter out noisy code.

euclidean_distance.py Score: 0.85
def dist(x, y):
    # Computes pairwise L2 distance
    return torch.sqrt(torch.pow(x - y, 2).sum())
Highly Educational: Self-contained, algorithmic logic.
config_boilerplate.py Score: 0.35
self._vim = vim
self._cursor = 0
self._entire_len = 0
self._result = []
Low Educational: Boilerplate, no meaningful computation.

2. Architecture & Training Details

The architecture is a classic decoder-only transformer with a few modern optimizations:

  • Parallel Configuration: MHA (Multi-Head Attention) and MLP layers are processed in parallel (similar to GPT-J/PaLM) to speed up training throughput.
  • FlashAttention: Used for faster context window processing.
  • Rotary Position Embeddings (RoPE): For superior sequence-length generalization.

The 1.3B parameters model has 24 layers, a hidden dimension of 2048, and 32 attention heads. It was trained on 8 Nvidia-A100 GPUs using DeepSpeed for only 4 days.

Interactive Demo: Base vs. Fine-Tuned

One of the most remarkable findings in the paper is that fine-tuning on simple exercises unlocked emergent capabilities. The base model (phi-1-base) struggled with complex instructions, but after fine-tuning on CodeExercises, the model (phi-1) suddenly excelled at using external libraries (like PyGame) and complex spatial reasoning, even though those libraries were never in the fine-tuning data.

Select a Prompt Scenario
Prompt:

"Write a PyGame. There is a ball. The x-axis increases by random integer (-10, 10). When space is pressed, reset ball to center (200, 400)..."

phi-1-base (Pretrained only)
# Loading...
phi-1 (After Fine-Tuning)
# Loading...

Results & Benchmarks

Despite having only 1.3 Billion parameters and being trained on 100x fewer tokens than competitors, phi-1 achieves remarkable accuracy on standard Python coding benchmarks.

HumanEval Pass@1 Accuracy Comparison

Compare phi-1 to other code models of varying sizes.

phi-1 (1.3B) — Textbook Data 50.6%
StarCoder (15.5B) — 1T tokens web code 33.6%
CodeGen-Mono (16.1B) — 577B tokens 29.3%
Replit Code (2.7B) — 525B tokens 21.9%

Addressing Data Contamination

A common criticism of synthetic data models is that they may have "memorized" the evaluation benchmarks during training. To address this, the researchers performed an aggressive decontamination study:

They calculated similarity between the CodeExercises dataset and HumanEval using both N-gram overlap and syntax-based AST (Abstract Syntax Tree) distances. They then pruned up to 40% of the training dataset (removing any files even vaguely similar to HumanEval) and retrained the model.

Interactive: AST Pruning & Robustness

Adjust the AST match threshold $\tau$. Lowering $\tau$ aggressively prunes more files that look structurally similar to evaluation tasks. See how performance holds up.

Remaining Dataset
879.5K exercises
Retrained HumanEval
50.6%

Limitations & Open Questions

While phi-1 is a breakthrough in data efficiency, the authors outline several key limitations:

  • Specialization: The model is highly specialized in Python. It lacks the multi-language versatility of larger models.
  • API Knowledge: It lacks domain-specific knowledge of less common packages and APIs, as it was trained primarily on basic concepts.
  • Prompt Sensitivity: It is highly sensitive to prompt variations and grammatical errors in instructions, often degrading significantly if the prompt structure changes slightly.

Glossary

HumanEval

A benchmark dataset of 164 hand-written Python programming problems used to evaluate the functional correctness of code generation models.

AST (Abstract Syntax Tree)

A tree representation of the abstract syntactic structure of source code written in a programming language. Used in this paper to measure structural code similarity.

Pass@1

An evaluation metric for code generation where a model generates a single solution for each problem, and the score is the percentage of solutions that pass all unit tests.

Cite this paper

@article{gunasekar2023textbooks,
  title={Textbooks Are All You Need},
  author={Gunasekar, Suriya and Zhang, Yi and Aneja, Jyoti and Mendes, Caio C{\'e}sar Teodoro and Del Giorno, Allie and Gopi, Sivakanth and Javaheripi, Mojan and Kauffmann, Piero and de Rosa, Gustavo and Saarikivi, Olli and others},
  journal={arXiv preprint arXiv:2306.11644},
  year={2023}
}
Made with Flash Papers — make your own