Testing the pipeline
How to run the 541 unit tests, what the known-answer tests and the planted-bug checks are, how to check every launch script's flags without a GPU, and the rehearsal checklist for one H100.
What you will do
- Run the unit tests on a CPU.
- Understand the two kinds of tests that matter most here: known-answer tests and planted-bug checks.
- Check every launch script’s flags against Megatron’s own parser and config, without a GPU.
- Rehearse every stage on one H100 with shrunk models, row by row.
The order matters. Each layer catches what the one before cannot, and each needs more hardware and time.
Hardware
| Check | Needs |
|---|---|
| unit tests | a laptop CPU and the virtual environment from the laptop setup |
| flag checks | a CPU and a Megatron-LM checkout at our commit; Transformer Engine’s version checks wait for the GPU machine |
| rehearsal | one H100 with the container image |
1. The unit tests
From the repository root:
.venv/bin/python -m unittest discover -s code/tests
The suite holds 541 test functions in 59 files, all written with Python’s standard unittest, and it runs from any
directory.
Some tests skip themselves when an optional package is missing.
It grew with every fix, because each bug found got a test that fails on it. It had 106 tests when a fresh clone first reproduced our laptop setup, and 283 after the GSPO patch. It had 442 at the end of the rehearsal, and 511 after the control run’s build.
| Area | Files (tests) | What they pin down |
|---|---|---|
| The GSPO loss | test_gspo.py (12) |
the loss against its formula; the ratio is 1 on the first mini-batch; the clip stops the gradient in the right direction; the KL term |
| Our Megatron patch | test_megatron_gspo_patch.py (16) |
the patched loss and gradient equal our tested gspo.py; the comparison really exercises the clip; the ratio is exactly 1 when the policy equals the old one |
| Rewards and graders | test_boxed_grader.py (7), test_checker_audit.py (11), test_score_boxed_guards.py (4) |
the vendored grader is unchanged; mutated answers are refused; a grader that spins forever is stopped |
| Statistics | test_stats.py (12) |
t quantiles against published tables, McNemar’s exact test, the sign-flip test, Welch’s t, a least-squares slope |
| Conversion | test_convert_qwen2_hf.py (8) |
Megatron reads the fused QKV as Hugging Face’s heads; Megatron’s SwiGLU on the stacked weights is Hugging Face’s MLP |
| Infrastructure | test_stall_guard.py (4), test_shell_pipes.py (2), test_metrics_report.py (2) |
a quiet run is stopped; no early-exiting reader under pipefail; every reference metric has a producer (this one skips itself in the public repository, which lacks the metrics reference) |
2. Known-answer tests
A known-answer test runs code on an input whose correct output is known some other way, and compares. Four kinds carried this project:
-
Against a tested reference. Before our GSPO patch to Megatron ran on any GPU, its loss and gradient were checked against our laptop-tested
gspo.py. They were equal in 3 seeds, with the KL term on and off.The first version never clipped an answer in its advantage’s direction, so it “passed” without testing the clip. Now every batch forces a clip in each direction, and a test checks that it happens.
-
Against a published original. The control run’s grader is PocketRL’s, vendored, and a test checks the file is byte for byte theirs.
-
Against mathematics. The rollout engine was tested on a toy model with known logits. A chi-square test checked its draws, and its recorded log-probability was checked at every step.
-
Against mutated answers. The answer checker was audited with deliberately wrong variants of every gold answer. It rejected 0 of 20,528 gold answers, and accepted no wrong or degenerate answer.
Two rules came out of the mistakes. Never assert a value against itself, and make every check able to fail.
3. Planted-bug checks
A test suite is only as good as the bugs it catches. So in each code review, deliberate bugs were planted in the code to count how many the tests caught. Each survivor got a new test.
| Code under test | Planted bugs | Result |
|---|---|---|
| the rollout engine | 9 | each made a test fail |
| the step-3 analysis, first round | 14 | 12 passed every test |
| the step-3 analysis, after the fixes | 16 | each made a test fail |
| the memory probe | 15 | 14 caught at once; the new test found a real bug; then 15 of 15 |
| the memory-saving code, first round | 54 | 8 caught, 5 equivalent changes, 41 survived |
| the memory-saving code, second round | 38 | 37 made a test fail |
The first rounds are the lesson: tests that pass prove little until you have watched them fail.
4. The flag check
Megatron checks a launch’s arguments in three places: its parser, validate_args, and the TransformerConfig it
builds before the model. Some refusals live only in the last.
code/scripts/megatron/check_flags.py runs every launch script with
DRY_RUN=1, and puts the printed arguments through all three, at the run’s own world size. Nothing trains and no GPU
is used.
python -I code/scripts/megatron/check_flags.py --validate "$MEGATRON_PATH"
python -I code/scripts/megatron/check_flags.py --validate --rehearsal "$MEGATRON_PATH" # the one-H100 settings
It also reports a flag given twice, since argparse silently keeps the last value. And it refuses the combinations that broke our runs, which neither of Megatron’s checks catches:
--check-for-spiky-lossin an RL run;- an RL micro-batch above 1;
- expert parallelism in an RL run without
--moe-pad-experts-for-cuda-graph-inference; --log-max-attention-logitin an inference-only run;--finetunewith--loadequal to--save;- a run that must load a checkpoint, without
--exit-on-missing-checkpoint; - a warm-up not shorter than the decay, or a missing
--lr.
The setup script runs the same check on the GPU machine. On our 8-GPU node it ended “all 24 runs: parser, validate_args and TransformerConfig OK (24 configs built)”.
5. The rehearsal on one H100
The full model does not fit one GPU, so the rehearsal shrinks it, then runs every stage for minutes:
SHRINK="--num-layers 2 --hidden-size 512 --ffn-hidden-size 2048 --num-attention-heads 8 --num-query-groups 2"
export NPROC=1 EXTRA_ARGS="$SHRINK --num-experts 8 --moe-ffn-hidden-size 256 --moe-router-topk 2 \
--expert-model-parallel-size 1 --micro-batch-size 2 --global-batch-size 16" # MoE runs
RL_EXTRA="$SHRINK --num-experts 8 --moe-ffn-hidden-size 256 --moe-router-topk 2 --expert-model-parallel-size 1"
DENSE_EXTRA="$SHRINK --micro-batch-size 1 --global-batch-size 16" # dense runs
The RL rows take EXTRA_ARGS="$RL_EXTRA" and keep their own batch: GSPO shrinks with PROMPTS_PER_STEP=8 instead,
64 rollouts and still 4 updates per step.
| Row | What it tests | Command (shrunk) | Look for |
|---|---|---|---|
| 1 | every pretraining script runs | EXTRA_ARGS="$DENSE_EXTRA" run r_dense bash $M/dense_baseline.sh, then the MoE arms |
each starts, trains, and stops at its exit interval |
| 2, 12 | save, reload, resume; save time | run r_pre bash $M/moe_pretrain.sh with --save-interval 20 --exit-interval 40 appended, twice |
the second launch resumes at 40 |
| 14 | the time-limit safety net; arms save weights only | EXIT_MINS=2 on a pretraining run |
it saves and exits; an arm’s folder holds tokens_per_expert/ and a small checkpoint |
| 10 | SFT trains, masks and packs | PRETRAIN_CKPT=<ckpt> run r_sft bash $M/sft.sh |
the loss falls; no assertion on masks or reset flags |
| 17 | the RL prompt is the SFT user turn | check_prompt_tokens.py, then one generation |
finish_reason “stop”, not “length” |
| 3, 19 | eval.sh and generation speed |
EXTRA_ARGS="$RL_EXTRA" EVAL_CKPT=<sft> EVAL_CONFIG=code/rl/curriculum_env.yaml EVAL_PROMPTS=20 run r_eval bash $M/eval.sh |
eval_results.jsonl has rows; no weights change |
| 13 | the rate calibration | CALIBRATION=1 with PROMPTS_PER_STEP=8, then gspo_calibration_report.py |
one GSPO step, nothing saved, a value of C |
| 4, 5, 18 | GSPO’s invariants | gspo.sh with PROMPTS_PER_STEP=8 EXIT_MINS=10 |
first update: ratio exactly 1, KL 0; no BOS; curriculum_state.json written |
| 7 | the RL restart | stop and relaunch row 4 with the same RUN_DIR |
it resumes from its checkpoint and curriculum state |
| 15 | the weights-only copy | the conversion of the checkpoints guide | a copy without optimizer state that eval.sh loads |
| 20 | serving | serve_bench.sh with 16 and 4 requests of 32 tokens |
every answer exactly 32 tokens; a KV cache of 1 KiB per token |
| 22, 23 | 4-bit and distillation stages | qat_4bit.sh and distill.sh on the shrunk models |
32 expert weight quantizers; bf16’s KL 0; the students’ losses |
What our rehearsal showed: every stage ran end to end and recorded its metrics. The first update’s ratio was exactly 1 with KL exactly 0, the restart resumed, and GSPO learned from a real signal against a control run.
It also found faults that would have wasted hours of the 8-GPU run. Among them: missing packages, a metric measured against zeros, an evaluation of random weights that exited 0, and a crashed run that hung. They are all in the troubleshooting table, marked “rehearsal”.
What one GPU cannot test. Anything that needs several GPUs: expert parallelism’s own checks, NVLink, and races between ranks. On our 8-GPU node those produced five faults the rehearsal could not reach:
- the NVLink check failing with NVLink present;
- a W&B checkpoint hook racing the tracker file across ranks;
- a monitor rule comparing one GPU’s MaxVio with another’s;
- a CUDA-graph padding flag that Megatron requires only with expert parallelism;
- a first update whose ratio was not exactly 1.
For those, check the full-size configuration on the CPU first. Then read the code the first full-size run will take, looking for conditions on the number of GPUs.
Read more
- Part 15: lessons discusses what each layer of testing caught.
- Part 5: numerics and reproducibility covers determinism and batch invariance.
- Code: code/tests/, code/scripts/megatron/check_flags.py, code/scripts/restart_check.py, code/scripts/learning_check.py.