Prince Osei Aboagye
Guide

Data

How to build the RL curriculum and test sets, 2.6B tokens of FineWeb-Edu in Megatron's format, and the packed SFT set, then stage them with checksums for the GPU machine.

// § 01

What you will do

  • Fetch the tokenizer and build the RL curriculum with its six held-out test sets.
  • Turn a sample of FineWeb-Edu into Megatron’s binary format: 2.6 billion training tokens.
  • Build the supervised fine-tuning (SFT) set from OpenMathInstruct-2, then pack it into 4,096-token lines.
  • Check that the RL prompt is exactly the SFT user turn.
  • Stage everything, with SHA-256 sums, in one folder that the setup script downloads and checks.

Decontamination comes up at every step. It means removing training text that overlaps the test questions, so a test score measures the model and not its memory.

// § 02

Hardware and time

A laptop CPU is enough; no step here needs a GPU. On our laptop the FineWeb-Edu preparation took 910.6 s, at 2.86M training tokens per second. The SFT build took 851.6 s. The downloads were 10.76 GB for FineWeb-Edu, 9 min 03 s on our connection, and 7.58 GB for the SFT source, 8 minutes.

// § 03

Before you start

Run the laptop setup first, so .venv and data/curriculum exist. The pretraining data script writes Megatron’s own file format, so it needs Megatron-LM at our commit in code/third_party/Megatron-LM.

These are the setup script’s own clone and patch lines, with that folder as the target:

MEGATRON_PATH=code/third_party/Megatron-LM
git clone -q https://github.com/NVIDIA/Megatron-LM.git "$MEGATRON_PATH"
git -C "$MEGATRON_PATH" checkout -q --detach d6316eb45bf1bfbb6f1f848c0b56e55166564d80
git -C "$MEGATRON_PATH" apply "$PWD/code/patches/megatron-d6316eb45.patch"     # git -C reads relative paths from MEGATRON_PATH
// § 04

Step 1: the tokenizer

The model uses the Llama 3.2 tokenizer: 128,256 tokens, with <|end_of_text|> at id 128001.

It comes from unsloth/Llama-3.2-1B, an ungated copy with the same vocabulary and token IDs as Meta’s.

The setup script pins its revision; this is its download line, pointed at the laptop’s folder:

hf download unsloth/Llama-3.2-1B tokenizer.json tokenizer_config.json special_tokens_map.json \
    --revision 9535bd9b1d1dea6acafbdc4813b728796aeb28da --local-dir data/tokenizer/llama-3.2

The tokenizer is Llama 3.2 material under Meta’s Llama 3.2 Community License; read its terms before you publish weights trained with it.

// § 05

Step 2: the RL curriculum and the held-out test sets

The laptop setup already ran this. To rebuild it on its own, from code/:

../.venv/bin/python scripts/build_curriculum.py

It reads every source dataset at a revision pinned in code/rl/curriculum_data.py, and writes data/curriculum/:

File What it holds Problems
warmup.jsonl generated arithmetic, levels 1–2 2,000
easy.jsonl MAWPS + MultiArith 1,195
medium.jsonl ASDiv + SVAMP 1,905
hard.jsonl MATH levels 1–3 + GSM8K train 10,400 (10,391 after the 9 dropped at run time)
*_test.jsonl GSM8K, MATH, MAWPS, MultiArith, ASDiv, SVAMP 1,319 / 5,000 / 520 / 180 / 407 / 300

Only training splits go into the tiers. ASDiv has a single split, so 20% of it is held out by a hash of the normalised question. Nine MATH problems in the hard tier, whose answers our checker cannot grade reliably, are dropped at run time; 19 more are left out of the MATH test score.

// § 06

Step 3: FineWeb-Edu for pretraining

How much text you need

Pretraining reads a fixed number of tokens:

\[N_{\text{train}} = S \times B \times L\]

where \(S\) is the number of optimizer steps, \(B\) the global batch in sequences, and \(L\) the sequence length in tokens.

In words: every step reads one batch of full-length sequences, so the tokens are steps times batch times length.

Our run used \(S = 1{,}240\), \(B = 512\) and \(L = 4{,}096\), which gives about \(2.60 \times 10^9\) tokens.

FineWeb-Edu counts its tokens with GPT-2’s tokenizer, so convert before you choose how many files to download:

\[N_{\text{raw}} \times r \times (1 - f_{\text{val}} - f_{\text{drop}}) \;\ge\; N_{\text{train}}\]

where \(N_{\text{raw}}\) is the raw token count in GPT-2 tokens, \(r\) our tokenizer’s tokens per GPT-2 token, \(f_{\text{val}}\) the share held out for validation, and \(f_{\text{drop}}\) the share decontamination removes.

In words: after the tokenizer change, the hold-out and the drops, the downloaded text must still cover the training target.

On 1,000 sampled documents, Llama 3.2 gave 0.9804 tokens per GPT-2 token. Five files held 3,758,074,759 GPT-2 tokens, about 3.68 billion of ours. With 1% held out and 0.044% dropped, that leaves about \(3.6 \times 10^9\), comfortably above the target.

Download

hf download HuggingFaceFW/fineweb-edu --repo-type dataset --include "sample/10BT/00[0-4]_00000.parquet" \
    --local-dir data/fineweb-edu/raw

These are files 000–004 of the sample/10BT subset, which has 14 files and about 10B tokens.

On our connection they took 9 min 03 s, and all five matched Hugging Face’s listed size and SHA-256.

Prepare

From code/:

python3 scripts/prepare_pretrain_data.py --input-dir ../data/fineweb-edu/raw/sample/10BT \
    --tokenizer ../data/tokenizer/llama-3.2/tokenizer.json --heldout-dir ../data/curriculum \
    --out-dir ../data/pretrain --max-train-tokens 2600000000 --workers 7

code/scripts/prepare_pretrain_data.py handles each document in file order, so the output is deterministic:

  1. It masks e-mail addresses and IPv4 addresses.
  2. It drops the document if any 10 consecutive words also occur in a held-out test question. That is 212,854 10-grams from all six test files.
  3. It puts the document in validation if a hash of its id falls in 1 of 100 buckets.
  4. It tokenizes with no special tokens added, then appends <|end_of_text|>.
  5. It writes Megatron’s .bin/.idx files with Megatron’s own IndexedDatasetBuilder, as 32-bit integers, since the vocabulary is larger than 65,535.

It stops at --max-train-tokens, and writes data/pretrain/manifest.json with every count, the inputs’ and the tokenizer’s SHA-256, and the code and Megatron commits.

What to expect

Measure Our result
Documents read 2,622,000
Dropped by the 10-gram rule 1,141 (0.044%)
E-mails / IPs masked 75,946 / 21,665
Training 2,594,765 documents, 2,600,591,645 tokens
Validation 26,094 documents (1.00%), 26,012,769 tokens

A size check you can run on the output:

\[\text{bytes}(\texttt{.bin}) = 4 \times N_{\text{tokens}}\]

where \(N_{\text{tokens}}\) is the token count in the manifest, and 4 is the bytes of one 32-bit token.

In words: each token is stored as one 32-bit integer, so the file size gives the token count back.

Ours: \(4 \times 2{,}600{,}591{,}645 = 10{,}402{,}366{,}580\) bytes, the size of our training .bin.

One check I did not run: reading documents back with Megatron’s reader and decoding them.

// § 07

Step 4: the SFT set

Download the source

bash code/scripts/download_openmathinstruct2.sh

It fetches the 32 parquet files of NVIDIA’s OpenMathInstruct-2 train split at a pinned revision, and checks each against the SHA-256 the Hub records. The dataset is CC BY 4.0, and its solutions were written by Llama-3.1-405B-Instruct.

Build

From code/:

python3 scripts/prepare_sft_data.py \
    --input-dir ../data/sft/openmathinstruct2/469216e3f46f4dacf476b382e192485ea51a143e \
    --curriculum-dir ../data/curriculum --tokenizer ../data/tokenizer/llama-3.2/tokenizer.json --out-dir ../data/sft

The script’s docstring lists every rule. The main ones:

  • It keeps the augmented_gsm8k questions, GSM8K-style word problems that do not overlap our RL questions.

  • It keeps a question only if at least 16 of the dataset’s 32 votes agree on its answer, and only solutions whose every equation is exactly true.

  • It decontaminates four ways: exact matches and 10-word overlaps against the test and RL questions, and, against the six test sets, 10-word overlaps with the numbers masked and near-duplicates by sentence embedding.

  • It drops a question when at least half its solutions were rejected for rounding. Such questions usually have no exact answer.

What to expect (our final build):

Measure Result
Kept questions 55,083 (53,970 train, 1,113 validation)
Dropped because at least half the solutions rounded 7,997
Response tokens: mean / median / max / total 107.5 / 100 / 441 / 5.92M

A blind check of 50 random kept examples found 0 wrong labels, a 95% upper bound of 7.1% on the error rate.

Pack it for Megatron

Megatron’s SFT dataset turns each JSONL line into one 4,096-token row. One short example per line would make each row about 96% padding. So code/scripts/make_megatron_sft.py packs about 24 examples per line:

python -I code/scripts/make_megatron_sft.py --tokenizer data/tokenizer/llama-3.2 \
    --inputs data/sft/sft_train.jsonl data/sft/sft_valid.jsonl --out-dir data/sft/megatron

Each example becomes a user turn (the question and a newline) and an assistant turn: <think>...</think>\n<answer>X</answer> followed by <|end_of_text|>. Megatron adds no end-of-text token itself, so the model learns to stop only from this one.

The script stops unless every example fits one line, every conversation tokenizes the same in pieces as whole, and each has exactly one end token.

File Examples Lines Examples per line Fill
train 53,970 2,210 24.4 97.8%
validation 1,113 46 24.2 96.0%
// § 08

Step 5: the RL prompt must be the SFT user turn

RL feeds the model a prompt and grades what follows. If that prompt is tokenized differently from the SFT user turn, RL starts from a format the model never saw. code/scripts/check_prompt_tokens.py compares them token by token:

python -I code/scripts/check_prompt_tokens.py "$MEGATRON_PATH" data/tokenizer/llama-3.2 data/curriculum/*.jsonl

Expect RL prompt != SFT user turn: 0 | RL prompts starting with BOS: 0. On our data all 23,226 prompts matched.

The GPU machine’s setup runs the same check again, because its tokenizer packages differ.

// § 09

Step 6: stage the data for the GPU machine

The setup script downloads one dataset folder at a pinned revision and checks it with sha256sum -c before anything runs. Lay out your copy the way it expects:

SHA256SUMS                  # sha256sum -c format: every file below except sft/megatron/ and the tokenizer
tokenizer-SHA256SUMS        # tokenizer.json, tokenizer_config.json, special_tokens_map.json
README.md
curriculum/                 # warmup, easy, medium, hard, the six *_test.jsonl, report.json
pretrain/                   # fineweb_edu_llama32_{train,valid}_text_document.{bin,idx}, manifest.json
sft/                        # sft_train.jsonl, sft_valid.jsonl, manifest.json
sft/megatron/               # the packed lines, manifest.json, and their own SHA256SUMS

The tokenizer itself is not in the folder: the setup script downloads it at the pinned revision and checks it against tokenizer-SHA256SUMS.

Specific to our machines: I uploaded the folder to a private Hugging Face dataset with hf upload-large-folder, which resumes after an interruption. Ours held 26 files, 10.6 GB, and took 15 min 22 s. Afterwards I checked all 26 against the local folder, by SHA-256 for the large files and by git blob id for the small ones. Set HF_DATASET_REV to the commit of your upload, so every machine reads the same bytes.

// § 10

Check that it worked

  • data/pretrain/manifest.json shows the token counts above, and the .bin sizes equal 4 bytes per token.
  • sha256sum -c SHA256SUMS passes in the staged folder, and in sft/megatron/.
  • check_prompt_tokens.py reports 0 mismatches and 0 prompts starting with a BOS token.
  • On the GPU machine, setup step 6 prints data: N + M files match. Ours read 21 + 3.
// § 11

Common problems

Symptom Cause Fix
a filter rejects far more than a 100-row sample suggested, yet its unit tests pass the converter took every $ for a LaTeX delimiter, so currency amounts broke whole solutions a currency-aware $; read 10 real rejects of every filter
the “equation false” filter flags correct solutions machine floats such as 0.6666666666666666 and labels read as numbers separate float artifacts from false equations; re-sample
about 11% of questions have no exact answer the teacher’s own solutions round (“a fifth of 48 CDs”) drop questions whose solutions mostly round
test questions with changed numbers survive decontamination a 10-word overlap misses a copy with new numbers add a numbers-masked 10-gram rule and embedding near-duplicates
a few stock phrases drop real documents some MATH questions contain “0 1 2 3 4 5 6 7 8 9” accepted: 0.044% of documents, and over-dropping is the safe side
the worker pool can deadlock fork from a process that already runs threads the script uses spawn
a committed log holds your home path tools print absolute paths redact logs before committing

The full list, with the fixes and where each was found, is in the troubleshooting table.

// § 12

Read more