Prince Osei Aboagye
Guide

Setup

How to prepare a laptop, one H100 or an 8×H100 node for this project: the pinned environment, Megatron-LM at d6316eb45 with our patch, the setup script, and the health checks to run before any training.

// § 01

What you will do

  • Choose the hardware level that matches what you want to run.
  • Build the pinned environment: a Python virtual environment on a laptop, or NVIDIA’s PyTorch container on an H100 machine.
  • On an H100 machine, run one setup script. It installs Megatron-LM at a fixed commit with our patch, checks the data, and checks every launch script’s flags.
  • On an 8-GPU node, run the GPU health check and the NCCL all-to-all test before any training.

The commands in this guide are the ones I used, and the scripts they call are in code/. Paths are relative to the repository root, the folder that holds code/. Lines marked Specific to our machines hold paths or settings you will need to adapt.

// § 02

The three hardware levels

Level What I ran on it What it can tell you
A laptop RTX 3060 (6 GB) Phase 0: GSPO and distillation on MiniMind’s 64M dense model; data preparation; unit tests; flag checks whether the algorithms and the code are right
One H100 (80 GB) the rehearsal, with shrunk models (2 layers, hidden size 512); the control run on Qwen2.5-Math-1.5B whether every stage runs and records its metrics
8×H100 SXM with NVLink the full model: 6.86B parameters in total, 1.22B active per token the real results

The shrunk models on one H100 only test the plumbing. Their speeds and losses say nothing about the full model.

Which guide pages need which level:

// § 03

Level 1: the laptop

What you need

Linux, Python 3.12, git, curl, an NVIDIA GPU with at least 6 GB and a driver for CUDA 13.0, and about 10 GB of disk.

You need the GPU only for GPU work: data preparation and the unit tests run on the CPU.

One command

From the repository root:

bash code/scripts/day1_setup.sh       # venv with the exact packages, pinned MiniMind/TorchFeather commits, data and
                                      # weights from pinned Hugging Face revisions, slices, RL curriculum; then checks
                                      # all 22 inputs against code/reproducibility/inputs.sha256 and stops if one differs

The script does seven things, and it is safe to re-run: finished steps are skipped.

  1. It builds a virtual environment at .venv from code/reproducibility/requirements-lock.txt. That file is the exact pip freeze of the environment our laptop results came from: Python 3.12.3, with PyTorch 2.11.0 for CUDA 13.0.

  2. It clones MiniMind and TorchFeather into code/third_party/ at pinned commits, and refuses a checkout on another commit.

  3. It downloads MiniMind’s minimal data from a pinned Hugging Face dataset revision.

  4. It downloads MiniMind’s released SFT weights from a pinned model revision.

  5. It cuts fixed line ranges of that data into training and held-out slices.

  6. It builds the RL curriculum (code/scripts/build_curriculum.py).

  7. It checks every input against code/reproducibility/inputs.sha256 and stops if one differs.

Check that it worked: the last lines read all 22 inputs match and == done.

Then run the unit tests, as the testing guide shows.

// § 04

Levels 2 and 3: an H100 machine

What the machine needs

  • The container image: nvcr.io/nvidia/pytorch:26.08-py3, the base image of Megatron d6316eb45’s own CI. Its release notes give PyTorch 2.14.0a0, CUDA 13.4.1, Python 3.12 and Transformer Engine 2.18 (we need 2.9 or newer).

  • The host driver: CUDA 13 or newer, or PyTorch sees no GPU. Our hosts reported CUDA 13.2 and ran the CUDA 13.4 image without trouble.

  • For 8 GPUs: H100 SXM, with NVLink between every pair of GPUs. The experts’ all-to-all traffic needs it.

  • Disk: a 1,200 GB volume at /workspace for the main run, and about 50 GB of container disk. The rehearsal and the control run used a 100 GB volume.

  • SSH with rsync, if you copy the repository over SSH.

Sizing the volume. Pretraining keeps every full checkpoint until you delete the old ones, so size the disk for the worst moment:

\[D_{\text{volume}} \;\gtrsim\; N_{\text{kept}} \times S_{\text{checkpoint}} \;+\; S_{\text{data}} \;+\; S_{\text{outputs}}\]

where \(D_{\text{volume}}\) is the volume size, \(N_{\text{kept}}\) the number of full checkpoints on disk at once, \(S_{\text{checkpoint}}\) the size of one full checkpoint (weights and optimizer state), \(S_{\text{data}}\) the prepared data, and \(S_{\text{outputs}}\) the logs, traces and reports.

In words: the volume must hold every full checkpoint you keep at once, plus the data and the run outputs.

Our main run saved 7 full checkpoints of 89.46 GiB each before the clean-up, which is about 626 GiB.

The data was 10.6 GB, and the runs folder reached 59 GB: 6.7 GB of outputs and 52 GB of the router study’s 40-step saves.

So 500 GB would have filled mid-run, and I used 1,200 GB.

Specific to our machines: the image does not start an SSH server. Our container’s start command installed and started one. Our platform passed my public key into the container as $PUBLIC_KEY; adapt that part to yours.

bash -c 'apt update; DEBIAN_FRONTEND=noninteractive apt-get install openssh-server rsync -y; mkdir -p ~/.ssh; cd ~/.ssh; chmod 700 ~/.ssh; echo "$PUBLIC_KEY" >> authorized_keys; chmod 600 authorized_keys; service ssh start; sleep infinity'

Step 1: copy the repository

From the folder that holds the repository (here called MyModel/), copy it without the local data, virtual environment and third-party clones:

rsync -az --no-owner --no-group -e "ssh -p <port> -i ~/.ssh/id_ed25519" \
    --exclude /data --exclude /.venv --exclude /code/third_party --exclude __pycache__ \
    MyModel/ root@<host>:/workspace/MyModel/

--no-owner --no-group is there because our network volume refused chown, and rsync then exits with code 23.

A git clone of the repository works as well.

Step 2: log in by hand

cd /workspace/MyModel
python -c "import wandb, huggingface_hub" 2>/dev/null || PIP_BREAK_SYSTEM_PACKAGES=1 python -m pip install -q wandb huggingface_hub
hf auth login                      # a READ token for your private dataset
wandb login                        # the key, typed here only

No key or token ever goes into a file of the repository. Type both at their prompts.

To run without W&B, set export WANDB_MODE=offline, as the control run did.

Step 3: run the setup script

The data lives in a private Hugging Face dataset, which you build yourself (see the data guide). Point HF_DATASET at your copy:

HF_DATASET=<your-hf-user>/mini-deepseek-v4-data HF_DATASET_REV=<your-dataset-commit> WORKDIR=/workspace \
    EXPECT_GPUS=8 MIN_FREE_GB=1000 bash code/scripts/pod_setup.sh      # rehearsal: EXPECT_GPUS=1 MIN_FREE_GB=60
source /workspace/env.sh
mkdir -p /workspace/runs /workspace/ckpt

For the control run on one H100, the script takes no dataset, because the model and data are public:

CONTROL_ONLY=1 EXPECT_GPUS=1 MIN_FREE_GB=80 WORKDIR=/workspace bash code/scripts/pod_setup.sh

code/scripts/pod_setup.sh runs these steps in order and stops at the first failure:

Step What it does
1 counts the GPUs, checks they are H100s, and with several GPUs checks NVLink in nvidia-smi topo -m
2 checks Transformer Engine is 2.9.0 or newer, PyTorch sees a GPU, and flash-attn is 2.7.3 or newer
3 installs the packages the image lacks, at the versions of Megatron d6316eb45’s own lock file
4 clones Megatron-LM, checks out commit d6316eb45bf1bfbb6f1f848c0b56e55166564d80, applies our patch
4b upgrades NVIDIA Model Optimizer to 0.48.0 without changing torch, Transformer Engine or flash-attn
4c imports every entry point our scripts launch, and checks the inference engine’s optional imports
5 clones the MoE handbook’s companion gist (router_watch.py, expert_watch.py) at a pinned commit
6–7b downloads the data and the tokenizer, checks their SHA-256 sums, and re-checks the RL prompts
8, 8b checks free disk; copies the data to RAM when /workspace is network storage
9 writes /workspace/env.sh for every launch
10 runs check_flags.py --validate: every launch script’s flags through Megatron’s parser, validate_args and TransformerConfig

Flash-attn matters because Megatron’s dynamic inference engine, which makes GSPO’s rollouts, asserts it with no fallback. Its report is /workspace/pod_setup_report.txt. Keep it.

What our patch changes. code/patches/megatron-d6316eb45.patch touches seven files of Megatron-LM:

  • rl_utils.py and train_rl.py: GSPO’s sequence-level ratio with a per-answer clip, the correctness filter, and per-answer statistics; rl_config.py: their flags;
  • train_rl.py: detects a 4-bit ModelOpt checkpoint before the model config is built;
  • sft_tokenizer.py: a pad token for tokenizers without <unk>, such as Llama 3’s, and a refusal of a pad equal to the end token;
  • registry.py: registers our curriculum agent, since Megatron loads only allowlisted agents;
  • dynamic_engine.py: starts the inference coordinator as a daemon process, so a crashed RL run exits;
  • wandb_utils.py: the checkpoint hook waits for the tracker file and never stops training.

What to expect. On the rehearsal machine, the setup passed on its second run. The only package it changed was ModelOpt, 0.45.0 to 0.48.0, and it held 22 packages at their versions.

On the 8-GPU node it finished in about 9 minutes and ended with “all 24 runs: parser, validate_args and TransformerConfig OK (24 configs built)”. In control mode, all 26 launch configurations passed. Pulling and unpacking the image took about 9 minutes on the one-GPU machine and about 20 minutes on the 8-GPU node.

Step 4: the environment of every launch

An SSH shell does not see the container’s environment. On our rehearsal machine, the container’s first process had 114 environment variables and an SSH shell had 12. The missing ones included CUDA_HOME, LD_LIBRARY_PATH and TRITON_CUDART_PATH, so Triton could not find cuda.h. The setup script copies the container’s environment from /proc/1/environ into /workspace/container_env.sh, leaving out anything that looks like a secret. env.sh sources it.

So start every shell with:

source /workspace/env.sh

env.sh also sets MEGATRON_PATH, GIST_DIR, TOKENIZER_DIR, DATA_DIR, SFT_DIR, CURRICULUM_DIR and DATA_CACHE_DIR. It does not set MAIN_ROUTER, the router choice common.sh reads for every launch, and common.sh then falls back to v4. Once you pretrain with quantile balancing, as we did, add export MAIN_ROUTER=qb to /workspace/env.sh. Then every later launch, in any shell, routes the way the weights were trained; ours had that line.

The GPU logger, for the whole time the machine runs. It samples every GPU every 10 seconds:

nohup nvidia-smi --query-gpu=timestamp,index,utilization.gpu,temperature.gpu,power.draw,memory.used,clocks.sm,ecc.errors.uncorrected.volatile.total \
    --format=csv -l 10 > /workspace/gpu.csv 2>&1 &

The run helper. Every training command on the other guide pages goes through it. It gives each run its own folder and log, and it stops a run that has gone quiet:

run() { export RUN_NAME=$1 RUN_DIR=/workspace/runs/$1; mkdir -p "$RUN_DIR"; shift
        python -I code/scripts/stall_guard.py --log "$RUN_DIR.log" --watch /workspace/runs -- "$@"; }
M=code/scripts/megatron
RW="--num-experts 64 --ranks 0 1 2 3 4 5 6 7 --expert-map $M/expert_map_ep8.json --pool-ranks"   # router_watch.py

If neither the run’s output nor any /workspace/runs/*.log changes for STALL_MIN minutes (default 15), the guard stops the run and its children and returns 124. It exists because a crashed RL run can hang instead of exiting: one rehearsal run sat for 24 minutes at 0% GPU, holding 25.9 GB. For a step that is quiet for longer, such as hashing large checkpoints, use STALL_MIN=30 run ....

The monitor loop, which checks every alarm threshold every 2 minutes, is on the monitoring page.

Step 5: health checks on the 8-GPU node

Run both before any training. They take about 5 minutes.

GPU health. DCGM is not in the image, so code/scripts/gpu_health_check.py stands in for dcgmi diag -r 1:

python -I code/scripts/gpu_health_check.py --expect 8 --out /workspace/runs/gpu_health.json   # must exit 0

Per GPU, it reads ECC, retired and remapped pages, NVLink, temperature, power and clocks. It then runs a 20-second bf16 matmul burn, and a GPU more than 10% slower than the median fails.

ECC turned off by the host is a warning, not a failure: a container cannot turn it on.

Our node passed on the second run. The first failed on one reading: GPU 6’s power peak of 706.3 W, against the 700 W limit plus the reading’s 5 W tolerance. That was a single one-second reading as the load started; the re-run passed with 0 failures and 0 warnings.

Everything else read healthy: NVLink NV18 on all 28 GPU pairs with 18 of 18 links up per GPU, ECC on with no errors, and a peak of 63 °C. The burn’s median was 682.3 TFLOP/s, 69% of the H100’s dense bf16 peak of 989 TFLOP/s, and the slowest GPU was 0.5% below it.

NCCL all-to-all bandwidth, with nccl-tests pinned at v2.21.1. The image has nvcc and NCCL, so it builds as is; it builds for the H100 only (sm_90).

git clone -q https://github.com/NVIDIA/nccl-tests.git /workspace/nccl-tests
git -C /workspace/nccl-tests checkout -q --detach afd59abab774a6a4b492eb344bd0c845a0b7bb4d      # v2.21.1
make -C /workspace/nccl-tests -j 16 NVCC_GENCODE="-gencode=arch=compute_90,code=sm_90" \
    > /workspace/nccl-tests-build.log 2>&1
/workspace/nccl-tests/build/alltoall_perf -b 8M -e 8G -f 4 -g 8 -c 1 | tee /workspace/nccl_alltoall.txt
NCCL_P2P_DISABLE=1 /workspace/nccl-tests/build/alltoall_perf -b 8M -e 2G -f 4 -g 8 -c 1 \
    | tee /workspace/nccl_alltoall_p2p_off.txt

Record busbw (the bus bandwidth) by message size, and check that #wrong is 0. The second run turns NVLink off, which shows how much slower the experts’ traffic would be without it. Our node:

Message size NVLink on (out of place / in place) NVLink off (NCCL_P2P_DISABLE=1)
8 MB 56.1 / 63.1 GB/s 12.2 / 9.5 GB/s
128 MB 286.4 / 288.0 GB/s 8.6 / 10.1 GB/s
2 GB 341.1 / 340.8 GB/s 11.4 / 11.8 GB/s
8 GB 346.2 / 345.3 GB/s not run

#wrong was 0 at every size in both runs. At 2 GB, NVLink carried about 30 times the all-to-all bandwidth of the path without it.

// § 05

Storage: keep the training data off network storage

Our recipe reads the training data without memory-mapping (--no-mmap-bin-files). Megatron then opens and closes the data file for about every document it reads. On our network volume each close took about 150 ms, and the 8-GPU dense baseline ran 3× slow with its GPUs idle.

After the data moved to RAM, the same run took 5.74 s per step instead of 19.15 s.

The setup script now does this for you. Step 8b checks the file system type of /workspace. On network storage it copies the checked data and the dataset-index cache to /dev/shm (RAM), checks the copy, and points env.sh there.

STAGE_DATA=1 always copies and STAGE_DATA=0 never does.

Specific to our machines: /workspace and /dev/shm are the mount points of our container. A container restart empties /dev/shm, so run pod_setup.sh again after one. Our network volume also refused a folder named aux, a reserved Windows device name. Avoid aux, con, prn and nul as run names.

// § 06

Check that it worked

  • The setup report ends with == ready.
  • gpu_health_check.py exits 0.
  • Both NCCL logs show #wrong 0 at every size.
  • /workspace/env.sh exists, and a new shell that sources it sees the GPUs from Python.
// § 07

Common problems

Symptom Cause Fix
pip refuses to install a package the image marks its system Python as externally managed (PEP 668) the setup script sets PIP_BREAK_SYSTEM_PACKAGES=1
torch.compile fails: Triton cannot find cuda.h an SSH shell lacks the container’s environment source /workspace/env.sh in every shell
“no NVLink between the GPUs” on a node that has it nvidia-smi topo -m | grep -q under pipefail: grep exits early, nvidia-smi gets SIGPIPE fixed in the script: it greps a captured output
the power check fails at about 706 W one one-second reading at the 700 W cap as the burn starts re-run the check; a real fault repeats
training is 3× slow, GPUs idle, processes in state D the data is on network storage let step 8b stage it in /dev/shm
PyTorch sees no GPU the host driver is older than the image’s CUDA choose a host with CUDA 13 or newer

Every other problem I met is in the troubleshooting table.

// § 08

Read more