TL;DR
I pretrained a 6.86B-parameter mixture of experts, 1.22B active per token, on 2.6B tokens of FineWeb-Edu on 8×H100. In a 40-step study, every learned router collapsed: by step 10 the aux-loss and both sign-rule runs sent every token to the same 8 of 64 experts in all 16 layers, and the quantile-balancing run was at a layer-mean MaxVio of 6.999. A decision rule written before the fix test chose V4’s scores with quantile balancing, which pulled every layer out of the collapse by step 100. The main run took 1,240 steps and 4.09 h, settled at 97% of the balanced speed ceiling, and ended at validation loss 3.046 (perplexity 21.0). The base model still scored 0 on every math test and looped to the length limit.
What you’ll learn
- How pretraining works: the next-token loss, perplexity, tokens per step and the learning-rate schedule.
- How to measure a router’s balance with MaxVio, and how the aux loss, the sign-rule bias and quantile balancing try to keep it.
- Why every router recipe collapsed in its first steps, and how a rule written in advance chose the fix.
- What a healthy mixture-of-experts pretraining run on 8×H100 looks like, in numbers.
- What a base model trained on 2.6B tokens can and cannot do.
Pretraining, from scratch
The objective: predict the next token
Pretraining teaches a model to continue text. Each document is cut into tokens (Part 3), and at every position the model outputs a probability for every possible next token. The loss is the average negative log-probability of the token that actually came next:
where \(x_n\) is the \(n\)-th token and \(x_{<n}\) the tokens before it in the same document. \(p_\theta\) is the model’s predicted probability with weights \(\theta\), and \(N\) the number of predicted tokens in the batch.
In words: the loss is the model’s average surprise at the true next token, in nats per token.
The number people usually quote is the perplexity:
where \(\mathcal{L}\) is the loss of equation (1), in nats.
In words: the model is as unsure as if it picked uniformly among PPL tokens at every position.
Two anchors make this concrete. A model guessing uniformly over our 128,256-token vocabulary has loss \(\ln 128{,}256 = 11.76\), and our model’s first loss was 12.16. Our final validation loss of 3.046 gives \(e^{3.046} \approx 21.0\).
Training lowers the loss by gradient descent. Each step computes the gradient of (1) on a batch and updates the weights with Adam. Its settings: β1 0.9, β2 0.95, weight decay 0.1, gradient norm clipped at 1.
Batches, tokens and steps
Our sequences are 4,096 tokens long, and each step reads 512 of them. A sequence packs several documents. Attention and position ids restart at every document boundary, so no token attends to an unrelated document.
where \(D\) is the number of training tokens and \(S\) the number of steps. \(B\) is the sequences per step (the global batch) and \(L\) the tokens per sequence.
In words: each step reads about 2.1M tokens, and 1,240 steps read the 2.6B prepared tokens about once.
In Megatron’s units the run is 634,880 samples. The study runs below are shorter: 120 steps (252M tokens) for the dense baseline and 40 steps (about 84M tokens) for each MoE variant.
The learning-rate schedule
The step size follows the warmup-stable-decay (WSD) schedule of the tested recipe I built on:
where \(n\) is the number of samples consumed and \(\eta_{\max} = 5 \times 10^{-4}\) the peak. The warmup lasts \(n_w = 2{,}000\) samples, the run \(N = 634{,}880\), and the decay \(n_d = 126{,}976\), the last 20%.
In words: a very short ramp, a long flat stretch at the peak, then a straight line down to zero.
The ramp is under four steps. After step 1 the rate is \(\eta_{\max} \cdot 512 / 2{,}000 = 1.28 \times 10^{-4}\), as the log shows. It reaches the peak at step 4.
The decay runs from step 992 to the end. The short ramp will matter for the router.
The recipe
| Setting | Value |
|---|---|
| Model | 16 layers, hidden 2,048, 64 routed experts per layer, 8 per token; 6.86B parameters, 1.22B active (Part 2) |
| Layout | experts split 8 ways (expert parallelism 8), the rest data-parallel (Part 4) |
| Batch | 512 sequences of 4,096 tokens; micro-batch of 8 sequences per GPU |
| Optimizer | Adam (0.9, 0.95), weight decay 0.1, clip 1.0, peak learning rate 5e-4 |
| Precision | bf16, router in fp32 (Part 5) |
| Memory | full activation recomputation |
| Seed | 1234 in every run |
Routing and balance
What the router does
In a mixture-of-experts (MoE) layer, the feed-forward block is split into experts, and a small router picks a few of them per token. Ours keeps DeepSeek V4’s scores [2]:
where \(z_{t,j}\) is the router’s logit for token \(t\) and expert \(j\), and \(\operatorname{softplus}(x) = \ln(1 + e^{x})\). \(b_j\) is a per-expert bias used only to select (zero if the recipe has none). \(\mathcal{S}_t\) is the set of \(k = 8\) chosen experts, and \(g_{t,j}\) is expert \(j\)’s output weight, scaled by \(\gamma = 2.5\).
In words: each token goes to the 8 experts with the highest scores, and their outputs are mixed in proportion to those scores, times 2.5.
The aux-loss variant below uses Megatron’s default softmax scores instead.
Why balance matters
Each GPU holds 8 of a layer’s 64 experts. An all-to-all exchange sends each token to the GPUs holding its experts, and the next exchange waits for the slowest GPU [1]. So the GPU holding the busiest experts sets the step time. Experts that receive almost no tokens are parameters that do no work.
Measuring balance: MaxVio
I measure balance per layer from the token counts Megatron saves every 100 steps (every 10 in the study):
where \(c_i\) is the number of tokens routed to expert \(i\) in one step, summed over all 8 GPUs. \(E = 64\) is the number of experts.
In words: how far the busiest expert sits above an even share, counted in even shares.
MaxVio is 0 when every expert gets the same number of tokens. My threshold, set before the run, calls anything above 2 concerning. I also count “starved” experts: those below 1% of an even share, \(c_i < 0.01\,\bar c\).
Derivation: the maximum is 7
Each token picks \(k\) different experts, so the counts sum to \(kT\) for \(T\) tokens, and no expert can receive more than \(T\). With \(\bar c = kT/E\):
where \(T\) is the number of tokens in the step.
In words: MaxVio reaches 7 exactly when the same 8 experts receive every token.
Three ways to balance
The aux loss adds a penalty to the training loss, in the Switch Transformer form that Megatron implements [6]:
where \(f_i\) is expert \(i\)’s share of the routed tokens and \(P_i\) its mean router probability \(p_{t,i}\) over the \(T\) tokens of a micro-batch. \(\alpha\) is the coefficient.
In words: the gradient flows through \(P_i\) alone and lowers each expert’s probability in proportion to the share of tokens it already gets.
At perfect balance \(f_i = P_i = 1/E\), and the penalty equals \(\alpha\). The tested aux-loss router uses a coefficient of 1e-3. V4 adds a per-sequence version at 1e-4, as DeepSeek-V3 does [3].
The sign-rule bias, DeepSeek’s auxiliary-loss-free method [3, 4], adds no loss. Once per step it nudges each expert’s selection bias \(b_j\) in (5):
where \(u\) is the update rate (1e-3 in my V4 recipe) and \(c_j\) the tokens expert \(j\) received in the global batch.
In words: every step, each underloaded expert’s bias rises by \(u\) and each overloaded one’s falls by \(u\), however far it is from the mean.
So after \(n\) steps a bias has moved at most \(n u\): 0.04 by step 40 at a rate of 1e-3.
Quantile balancing keeps a per-expert bias but solves for it every step instead of nudging it [1, 5]. For the \(m\) tokens of a micro-batch, with logits \(z_{t,j}\) and current bias \(\beta_j\), Megatron computes:
where \(\operatorname{top}_r\) is the \(r\)-th largest value of a set, \(\alpha_t\) is token \(t\)’s threshold and \(q_j\) is expert \(j\)’s quantile. \(C = mk/E\) is an even share of tokens per expert.
In words: tokens pick their top 8 after the bias, and each expert’s new bias is the bar that exactly its fair share clears.
Once per global batch, Megatron averages the \(q_j\) over micro-batches and GPUs into \(\bar q_j\), blends and re-centres:
where \(\lambda\) is the moving-average weight: 0 in pretraining, Megatron’s default, so each solution replaces the bias. RL sets it to 1.0, which freezes the bias.
In words: the new bias is this batch’s solved quantile, shifted so that the biases average zero.
Our micro-batch is 8 sequences of 4,096 tokens, so \(m = 32{,}768\) tokens and \(C = 4{,}096\). The bias is subtracted from the logits, so it is measured in logit units, and the output weights still come from V4’s scores in (5).
Derivation: why the solved bias gives each expert its share
Hold every token’s threshold \(\alpha_t\) fixed. Token \(t\) picks expert \(j\) when \(z_{t,j} - \beta_j > \alpha_t\), that is, when \(z_{t,j} - \alpha_t > \beta_j\). Setting \(\beta_j = q_j\), the \((C+1)\)-th largest of these values, lets exactly \(C\) tokens clear expert \(j\)’s bar. The thresholds shift once the biases change, so one solve is approximate; Megatron repeats it every global batch.
The plan: a 40-step study before the main run
Before 4 to 6 hours of pretraining, a balancing study (stage B1 of the run) compared routers in short runs. Every run shared the model, data order, optimizer and seed, so only the routing differed.
| Run | Steps | Purpose |
|---|---|---|
| Dense baseline (no experts) | 120 | loss and speed reference |
| MoE, forced-even routing | 40 | the speed ceiling: random scores give every expert an even share |
| MoE, aux loss 1e-3 | 40 | the handbook’s tested router |
| MoE, V4 router | 40 | my plan: V4 scores, sign-rule bias 1e-3, sequence aux loss 1e-4 |
| MoE, NVLink off; MoE, communication overlap | 40 | communication lessons |
Two checks were written before the run.
- Health: at step 40 the V4 run’s loss is finite and falling, no expert is dead, and MaxVio is within the threshold.
- Loss: the V4 run’s mean loss over steps 31–40 is at most 0.2 above the aux-loss run’s.
If either failed, I would debug for at most 30 minutes, then rerun V4 or fall back to the aux-loss router. If both routers broke, the rule said abort.
What happened in the study
First, a stalled data loader
The dense baseline averaged 18.6 s per step, against 5.7 s on its fastest step. At one sample, 5 of its 8 GPUs sat at 0% utilization.
The data was on network storage, and our recipe reads without memory-mapping, so Megatron opened and closed the data file for every document. Each close took about 150 ms. With the data copied to RAM, the same run took 5.74 s per step and gave the same first loss.
The restarted baseline learned as expected: loss 12.15 to 6.865 at step 120, validation loss 7.008 at step 100. The forced-even run then set the MoE’s speed ceiling: 10.69 s per step, 199.0 TFLOP/s per GPU.
The V4 router collapsed by step 10
In all 16 layers, the whole-layer MaxVio was 7.000 at steps 10 and 20: the maximum, every token to the same 8 experts. About 55 of the 64 experts received under 1% of an even share. On GPU 0, layer 0, step 10, the 8 local experts received 262,111, 2, 7, 2, 2, 8, 6 and 148,010 tokens. Steps took 16.6–18.5 s against forced-even’s 10.6–10.9 s, because the GPUs holding the chosen experts did all the expert work.
The loss did not show it. Over steps 31–40 the collapsed V4 run averaged 7.621, below forced-even’s 7.756: this early, a model using only 8 experts still learns. The failure was balance and speed.
Equation (9) explains why the bias could not help. At a rate of 1e-3 a bias moves at most 0.04 by step 40. The gaps between sqrt-softplus scores are of order 0.1 to 1. And the learning rate peaks by step 4, so the router settles on 8 experts long before the bias can answer.
At step 40, the largest bias in every layer was +0.038 to +0.040: the starved experts had been raised at nearly every step. The rule worked in the right direction at full speed. It was rate-limited, not broken.
The aux-loss router collapsed the same way
Its whole-layer MaxVio was 7.000 in all 16 layers at steps 10, 20, 30 and 40, with 51–55 experts starved. Check 2 passed (V4 minus aux over steps 31–40: −0.151), but both runs failed check 1.
Two different balancing methods failing identically pointed to a common cause. My leading suspect was the short warmup. Adam’s first updates move each router weight by about the learning rate. If the signs line up across tokens, a few experts’ logits rise for every token within a few steps.
That stayed an untested hypothesis. Meanwhile the plan’s rule read: both routers broken, abort.
What the handbook’s own runs say
The handbook’s tested run uses our settings exactly: 64 experts, top-8, hidden 2,048, 16 layers, aux loss 1e-3, batch 512, a 2,000-sample warmup to 5e-4. Its router comparison runs five routers for 120 steps from initialization, one seed each [1]. Median throughput over the last 20 steps: quantile balancing 139 TFLOP/s, aux loss 125, quantile with a 0.9 moving average 121, the sign-rule bias 99. The sign-rule run, which used sigmoid scores, stayed flat while the others climbed. Those runs used an older Megatron commit that summed the sign rule’s token counts in bf16, so a rounded count could flip a bias update; at our commit the counts are exact.
So every router of this recipe starts collapsed, and they differ in how fast they recover. My 40-step threshold, MaxVio at most 2, was set too early for this recipe. Our aux run agreed: its starved experts went 54.8, 55.3, 55.4, then 51.2 at step 40. A second caveat on the handbook’s ranking: quantile balancing also ended with the highest training loss of the five.
The decision rule, written before the data
The fix test, V4 with its bias rate raised tenfold to 1e-2, was already running. Before its step 20–40 counts arrived, I wrote the rule:
- If it reaches MaxVio ≤ 2 by step 40, the main run uses it.
- If it is clearly falling but above 2, use it, and require MaxVio ≤ 2 by step 200, or restart with the aux-loss router.
- If it stays at 7.0, run one 10-minute test of V4’s scores with quantile balancing. If that balances, use it; otherwise, the aux-loss router.
Before step 40 I gave “clearly falling” a number: a layer-mean MaxVio of at most 5.5, with at least 8 of 16 layers below 7.0.
The bias at 1e-2 did not break the collapse
Its layer-mean MaxVio at steps 10, 20, 30 and 40 was 7.000, 7.000, 6.870 and 7.000, with 55.0, 55.2, 52.2 and 50.4 experts starved. Ten times the rate, up to ±0.4 per bias by step 40, could not pull this recipe’s router out in 40 steps. Rule 3 applied.
Quantile balancing: the only recipe falling
The layer-mean MaxVio went 6.999, 7.000, 6.766, then 5.186 at step 40, with 15 of 16 layers below 7.0 and the best at 3.51. Starved experts fell from 52.4 to 26.6. Its loss over steps 31–40 was 7.6671, 0.104 below the aux run’s, and its step time was still falling: 14.6 s at step 40. No other router moved like this in 40 steps.
By the rule, the main run uses V4’s sqrt-softplus scores and routed scaling with quantile balancing. Megatron refuses any aux loss beside quantile balancing, so V4’s sequence loss and sign-rule bias are gone. This changes the V4 story: the balancing is quantile’s, as in Kimi K3, not DeepSeek’s sign rule. The scores, top-8 of 64 and the routed scaling stay V4’s.
Why the sign rule could not win
The quantile run’s solved biases at step 40 had a standard deviation of 4.59 logits in layer 0, with a minimum of −6.26. Balance needed biases of ±4–6 logits, about 1.5 in score units. The sign rule moves a bias at most 0.04 in 40 steps at 1e-3, and 0.4 at 1e-2. So it was 4 to 40 times too slow, by construction.
The study in one table
Medians over steps 11–40 (the dense baseline over 11–120):
| Run | Step time | TFLOP/s per GPU | MFU (Megatron) | Loss, mean of last 10 steps | Layer-mean MaxVio at step 40 |
|---|---|---|---|---|---|
| Dense baseline | 5.70 s | 373.1 | 37.7% | 6.900 (steps 111–120) | — |
| MoE, forced-even (ceiling) | 10.69 s | 199.0 | 20.1% | 7.756 | 0.009 |
| MoE, aux loss 1e-3 | 17.63 s | 120.7 | 12.2% | 7.771 | 7.000 |
| MoE, V4 sign rule 1e-3 | 17.80 s | 119.6 | 12.1% | 7.621 | 7.000 |
| MoE, V4 sign rule 1e-2 | 17.37 s | 122.5 | 12.4% | 7.664 | 7.000 |
| MoE, V4 scores + quantile balancing | 17.04 s | 124.9 | 12.6% | 7.667 | 5.186, falling |
At perfect balance, the MoE ran at 0.533× the dense model’s throughput, with the same feed-forward compute per token. Imbalance slows it further: communication took 30.8% of a forced-even step and 52.8% of a collapsed aux-loss step, as tokens piled onto the GPUs holding the 8 chosen experts.
Two planned runs did not happen. The communication-overlap run ran out of memory at its first step (78.83 of 79.18 GiB in use), so the main run keeps full recomputation. The NVLink-off run was dropped to give the router decision its time.
The main run
Sizing at step 200
My plan sized the run from the study’s step time, but here that rate came from the collapsed phase. At 17.70 s per step the run projected to about 6.4 h, past its 5.96 h time cap, cutting about 8% of the tokens. Both our runs and the handbook’s said the collapsed phase would not last.
So the run started with all 1,240 steps, and the measured rate at step 200 would decide. Read at step 276, the remaining 964 steps projected to about 3.0 h, inside the cap, so no restart was needed.
Balance: every layer out of the collapse
At step 100 the layer-mean MaxVio was 0.737, with the worst layer at 1.700. At step 200 it was 0.437, worst 0.739. No expert was starved at either step, so the in-run check, MaxVio ≤ 2 by step 200, passed.
| Step | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | 1,000 |
|---|---|---|---|---|---|---|---|---|---|---|
| Layer-mean MaxVio | 0.737 | 0.437 | 0.260 | 0.240 | 0.302 | 0.224 | 0.253 | 0.262 | 0.165 | 0.171 |
The solved biases settled as well. Their spread (largest minus smallest, in logits, averaged over layers) fell from 12.54 at step 200 to 5.21 at step 600. It ended at 5.47.
Speed
| Steps | Step time | TFLOP/s per GPU | Training loss | Gradient norm |
|---|---|---|---|---|
| 1–50 | 17.32 s | 130.5 | 8.438 | 8.80 |
| 51–100 | 12.45 s | 177.6 | 7.023 | 0.72 |
| 101–150 | 11.17 s | 190.6 | 6.506 | 0.71 |
| 151–200 | 11.09 s | 192.0 | 6.067 | 0.78 |
| 201–250 | 11.06 s | 192.5 | 5.632 | 0.60 |
The step time settled at 11.04 s, 97% of the forced-even ceiling. Over steps 11–1,240 the median was 10.93 s per step, 194.7 TFLOP/s per GPU, and 1.92×10⁵ tokens/s across the 8 GPUs. Model FLOPs utilization (MFU) puts this against the hardware’s peak:
where \(P_{\text{active}}\) is the parameters used per token and \(\dot D\) the tokens per second across the node. \(G\) is the number of GPUs, and \(F_{\text{peak}}\) one H100’s dense bf16 peak in FLOP/s. The 6 counts 2 FLOPs per parameter per token forward and 4 backward.
In words: the run turned 17.7% of the GPUs’ peak arithmetic into the model’s forward and backward passes.
Megatron’s own count, which includes attention, gives 19.7%. The handbook measured 13–15% MFU for its MoE on H100.
Loss
| Step | 100 | 200 | 400 | 600 | 800 | 1,000 | 1,200 | 1,240 |
|---|---|---|---|---|---|---|---|---|
| Validation loss | 6.722 | 5.863 | 4.459 | 3.718 | 3.389 | 3.222 | 3.061 | 3.046 |
The run ended with training loss 3.048 (lowest 3.022, at step 1,234) and validation loss 3.046, a perplexity of 21.0. There were 0 NaN values, 0 skipped steps and no loss spike after step 100. The gradient norm fell from 4.76 to 0.10–0.26 at the end.
Monitoring
- A monitor checked every threshold in my metrics reference every 2 minutes, and a GPU logger sampled every 10 s.
- The GPUs peaked at 60–67 °C, with one-second power readings of 700.6–714.9 W at the 700 W cap and 0 memory errors.
- Checkpoints: seven saves of 89.46 GiB each, in 44.6–54.3 s.
The end-of-run checks were written before the run, and all passed.
- Still learning: validation 3.0615 at step 1,200 against 3.5329 at step 700; the last 50 steps’ training loss 3.0546 against 3.5107 500 steps earlier.
- Stable: no NaN, and no spike that failed to recover.
- Complete: the run ended by its schedule, at step 1,240.
The base model
Three measurements followed on the final checkpoint.
- Held-out perplexity. Loss 3.0430 on the 26M-token validation set: perplexity 20.97.
- Rogue experts. The rule, fixed before reading, flags an expert whose mean output norm exceeds 10 times its layer’s median. None did: the largest ratio was 3.47 (layer 0, expert 18).
- Math. An evaluation on 1,430 problems from GSM8K, MATH, SVAMP, ASDiv, MAWPS, MultiArith and the curriculum’s dev sets.
The base model scored 0 on every set, with 0 answers in the required format. Median answers ran 1,474 to 1,528 tokens, and 98.5–100% of answers passed 1,024 tokens. The text was on topic but fell into loops:
“The distance between the two trains is 60 miles, and the distance between the two trains is 60 miles. The distance …”
This is the expected base. Pretraining on 2.6B tokens of web text teaches neither the answer format nor when to stop; that is SFT’s job (Part 7). The grader behaved as specified, so the zero belongs to the model, not the pipeline. For scale, the handbook’s tested run trained about 54,000 steps; ours trained 1,240.
What went wrong, and the fixes
Most of these appear only with 8 GPUs, which a one-GPU rehearsal cannot show.
- The health threshold came too early. MaxVio ≤ 2 at step 40 fails every router of this recipe. Fix: the step-200 in-run check.
- The data loader stalled on network storage. Fix: the data in RAM; the setup script now copies it there whenever the storage is a network mount.
- A race in Megatron’s W&B checkpoint hook. Rank 0 writes the tracker file and the last rank logs it, with no barrier, so the forced-even run crashed after its save. Unfixed, the main run would have stopped at its first save. Our patch now waits for the file and turns any failure into a warning.
- A reserved folder name. The network volume refused “aux”, a reserved Windows name; the folder became
aux_loss. - A profiled run that never started. A shell start-up script slowed every
python3-configcall to minutes under the profiler.env -u BASH_ENVfixed it. - I killed the next run by stopping processes by name while a driver script started it. Lesson: stop runs by process id.
- A wait looked for the wrong key. It searched the count files for “step”, but they write “iter”, so the step-100 balance reading came at step 276. Lesson: test a pattern on a real file.
- The monitor compared GPUs with each other. It keyed each MaxVio baseline by layer alone, so one GPU’s value met another’s baseline: 7 false alarms on the main run. Fix: compare whole-layer rows, with two new tests.
- An expert-parallel check at evaluation. The first base-model evaluation stopped on a Megatron assertion that needs expert padding for CUDA graphs whenever expert parallelism is above 1. Fix: the flag on every RL-loop launch, and our flag checker refuses a launch without it.
Run it yourself
Hardware and time. One node of 8×H100 80 GB SXM with NVLink. The study took about two hours, including its fixes (15:25 to 17:26 UTC); the main run took 4.09 h. Each full checkpoint is 89.46 GiB, and the run saves seven. Keep the training data on local disk or in RAM.
Setup. Follow the setup guide: code/scripts/pod_setup.sh checks the GPUs and NVLink, fetches the data and the pinned handbook scripts, and writes env.sh. The full step-by-step commands are in the pretraining guide.
source /workspace/env.sh && cd /workspace/MyModel
M=code/scripts/megatron; R=/workspace/runs
RW="--num-experts 64 --ranks 0 1 2 3 4 5 6 7 --expert-map $M/expert_map_ep8.json --pool-ranks"
run() { export RUN_NAME="${1}" RUN_DIR="$R/${1}"; mkdir -p "$RUN_DIR"; shift
python -I code/scripts/stall_guard.py --log "$RUN_DIR.log" --watch "$R" -- "$@"; }
# The balancing study: 120 dense steps, 40 steps per MoE run
run dense bash $M/dense_baseline.sh
run forced_even bash $M/moe_forced_even.sh
run aux_loss bash $M/moe_aux.sh
run v4router bash $M/moe_v4router.sh
EXTRA_ARGS="--moe-router-bias-update-rate 1e-2" run v4router_bias1e2 bash $M/moe_v4router.sh; unset EXTRA_ARGS
run v4_qb bash $M/moe_v4_qb.sh
for arm in forced_even aux_loss v4router v4router_bias1e2 v4_qb; do
python $GIST_DIR/router_watch.py counts $R/$arm/save/tokens_per_expert $RW > $R/$arm/maxvio.jsonl
done
# The main run, with V4 scores and quantile balancing
export MAIN_ROUTER=qb
EXIT_MINS=373 CKPT_DIR=/workspace/ckpt/pretrain run pretrain bash $M/moe_pretrain.sh
python $GIST_DIR/router_watch.py counts /workspace/ckpt/pretrain/tokens_per_expert $RW > $R/pretrain/maxvio.jsonl
# The base model: held-out perplexity, rogue experts, the math evaluation
LM_EVAL_CKPT=/workspace/ckpt/pretrain run lm_pretrain bash $M/lm_eval.sh
ROGUE_CKPT=/workspace/ckpt/pretrain run rogue bash $M/rogue_experts.sh
python -I code/scripts/eval_budget.py --name eval_base --ckpt /workspace/ckpt/pretrain \
--config code/rl/curriculum_env.yaml --budget-min 10
The last command’s --budget-min 10 is a time limit in minutes for the evaluation; Part 13 explains how it scores.
router_watch.py comes from the handbook’s companion scripts at commit 283029be, which the setup script fetches into $GIST_DIR. The per-layer MaxVio of equation (6) is the maxvio field of its pooled_captured_step rows.
Config: the shared flags are in code/scripts/megatron/common.sh: ROUTER_V4, ROUTER_AUX and ROUTER_V4_QB hold the three routers, and MAIN_ROUTER picks one for every later stage.
What to expect (our numbers, one run each):
- The study: MaxVio 7.0 in all layers for the aux and V4 runs; about 5.2 at step 40 for quantile balancing.
- The main run: MaxVio 0.737 at step 100 and 0.437 at step 200, then about 11.0 s per step. Validation loss 3.046 at step 1,240.
- The base model: held-out perplexity 20.97, and 0 on every math set.
Megatron is not bit-deterministic here: a restarted run gave the same first loss, but later losses differed by up to about 0.3%.
Code: code/scripts/megatron/moe_pretrain.sh, code/scripts/megatron/moe_v4_qb.sh, code/scripts/megatron/moe_v4router.sh, code/scripts/megatron/moe_aux.sh, code/scripts/megatron/moe_forced_even.sh, code/scripts/megatron/dense_baseline.sh, code/scripts/checkpoint_router_bias.py, code/scripts/pretrain_checks.py, code/scripts/run_report.py and code/scripts/run_monitor.py. The step-by-step version is the pretraining guide; what to watch is in the monitoring guide.
What to take away
- In this recipe every router starts collapsed; what differs is how fast it recovers. Judge a router over a hundred steps or more, not 40.
- Read balance from token counts. The collapsed V4 run had a lower loss than the perfectly balanced one.
- A fixed-step bias is rate-limited: after \(n\) steps it has moved at most \(n u\), while balance here needed biases of ±4–6 logits.
- Write the decision rule before the data. Here it turned “both routers broken: abort” into one 15-minute test and a working run.
- Read the step time over many steps; an idle GPU points to the host, here the data loader.
References
- N. Tazi, L. von Werra, T. Wolf, “The Ultimate MoE Training Handbook”, Hugging Face, 2026. https://huggingface.co/spaces/huggingface/moe-blogpost
- DeepSeek-AI, “DeepSeek-V4: Towards Highly Efficient Million-Token Context Intelligence”, arXiv:2606.19348, 2026. https://arxiv.org/abs/2606.19348
- DeepSeek-AI, “DeepSeek-V3 Technical Report”, arXiv:2412.19437, 2024. https://arxiv.org/abs/2412.19437
- L. Wang et al., “Auxiliary-Loss-Free Load Balancing Strategy for Mixture-of-Experts”, arXiv:2408.15664, 2024. https://arxiv.org/abs/2408.15664
- Neitemeier et al., “Exact Quantile Balancing and Load-Error Injection for Mixture-of-Experts”, arXiv:2609.28053, 2026. https://arxiv.org/abs/2609.28053
- W. Fedus, B. Zoph, N. Shazeer, “Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity”, arXiv:2101.03961, 2021. https://arxiv.org/abs/2101.03961
- NVIDIA Megatron-LM, at commit
d6316eb45. https://github.com/NVIDIA/Megatron-LM/tree/d6316eb45bf1bfbb6f1f848c0b56e55166564d80
Cite this
@misc{aboagye2026minidsv4part06,
author = {Aboagye, Prince Osei},
title = {Mini DeepSeek V4, Part 6: Pretraining, and the router that collapsed},
year = {2026},
howpublished = {\url{https://poaboagye.github.io/blog/06-pretraining/}}
}