Prince Osei Aboagye
← All posts
Mini DeepSeek V4 / Part 12·Dec 2026

Distillation and 4-bit trainingtested on a laptop and one H100, then cut

Spoiler: A teacher made from-scratch students worse on held-out text, because they matched it only at the training temperature. The 4-bit pipeline ran, but its model could not run in Megatron's RL engine.

TL;DR

On a laptop RTX 3060 (6 GB), I distilled 30M-parameter students from scratch: three arms, three seeds each. With MiniMind’s default loss, students trained with a teacher ended worse on held-out text than students trained alone: 2.978 against 2.940 nats per token. They matched their teacher only at the training temperature, \(\tau = 1.5\) (6 of 6 pairs), not at \(\tau = 1\) (1 of 6).

On one H100 with shrunk models, distillation and 4-bit quantization-aware distillation (QAD) both ran end to end. A ModelOpt 4-bit model cannot run in Megatron’s RL engine, and I cut both from the 8×H100 run to give RL more time.

// § 01

What you’ll learn

  • What distillation optimizes: a KL divergence between softened distributions, with a \(\tau^2\) factor.
  • Why a teacher made from-scratch students worse, and how a 2×2 check traced a failed prediction to the temperature.
  • How 4-bit weights work: a block scale, a rounding step, and the straight-through estimator behind QAT.
  • What a one-H100 rehearsal caught: a routing rule that fought QAD, and an engine that rejects 4-bit models.
  • Why I cut both from the 8×H100 run, and what I would try next.
// § 02

Distillation, explained

Teacher, student and soft targets

A language model ends each step with one score per vocabulary token, called a logit. A softmax turns the logits into probabilities for the next token. Ordinary training uses only the true next token: the cross-entropy (CE) loss is minus the log of its probability.

Distillation adds a second target: the full probability vector of a trained teacher model. That vector also says how plausible each wrong token is, which one correct token cannot. The model being trained is the student. The temperature-based form used here comes from Hinton, Vinyals and Dean (2015) [1].

Distillation: copy the teacher's softened probabilitiesThe student learns from the true next token (CE) and from the teacher at temperature τ (KL)Tokenssame inputTeacherfrozenStudenttrainablelogitslogits÷ τ, softmaxteacher probs÷ τ, softmaxstudent probslogitssoftmaxτ = 1KLteacher ‖ studentat τ (eq. 2)CE−log p of thetrue tokenTrue next tokenLoss, one ofMiniMind, eq. 3α·CE+ (1 − α)·τ²·KLα = 0.5, τ = 1.5orModelOpt, eq. 5(1 − αKD)·CE+ αKD·KL, no τ²αKD = 0.5, τ = 1gradient: student only
Figure 1. the student learns from the true next token and from the teacher’s softened probabilities. Diagram, no data.

Temperature

\[p_i^{(\tau)} = \frac{\exp(z_i / \tau)}{\sum_{j=1}^{V} \exp(z_j / \tau)} \tag{1}\]

where \(z_i\) is the logit of token \(i\), \(V\) the vocabulary size and \(\tau\) the temperature; \(\tau = 1\) gives the ordinary softmax.

In words: dividing every logit by a temperature above 1 flattens the distribution, so unlikely tokens gain probability.

For example, logits 4, 2 and 1 give 84%, 11% and 4% at \(\tau = 1\), and 72%, 19% and 10% at \(\tau = 1.5\). In distillation, that flattening exposes the teacher’s second and third choices for the student to copy.

The loss

\[\mathrm{KL}\big(p_T^{(\tau)} \,\big\|\, p_S^{(\tau)}\big) = \sum_{i=1}^{V} p_{T,i}^{(\tau)} \log \frac{p_{T,i}^{(\tau)}}{p_{S,i}^{(\tau)}} \tag{2}\]

where \(p_T^{(\tau)}\) and \(p_S^{(\tau)}\) are the teacher’s and the student’s softened distributions at one position. The sum runs over the vocabulary, and the loss averages it over all scored positions.

In words: the KL divergence is zero when the student’s softened probabilities equal the teacher’s, and it grows as they disagree.

MiniMind, the small open-source LLM project I used on the laptop [2], combines the two targets:

\[\mathcal{L}_{\text{MiniMind}} = \alpha \, \mathrm{CE} + (1 - \alpha)\, \tau^{2} \, \mathrm{KL}\big(p_T^{(\tau)} \,\big\|\, p_S^{(\tau)}\big) \tag{3}\]

where CE is the cross-entropy on the true tokens at \(\tau = 1\), and \(\alpha\) is its weight.

In words: the loss mixes a pull toward the true token with a pull toward the teacher’s softened view, scaled up by \(\tau^2\).

MiniMind’s defaults are \(\alpha = 0.5\) and \(\tau = 1.5\), which make the loss \(0.5\,\mathrm{CE} + 0.5 \times 2.25\,\mathrm{KL}\).

Why \(\tau^2\)? Look at the gradient the KL sends to one student logit:

\[\frac{\partial}{\partial z_{S,i}} \, \mathrm{KL}\big(p_T^{(\tau)} \,\big\|\, p_S^{(\tau)}\big) = \frac{1}{\tau} \Big( p_{S,i}^{(\tau)} - p_{T,i}^{(\tau)} \Big) \tag{4}\]

where \(z_{S,i}\) is the student’s logit for token \(i\); the derivation follows.

In words: each student logit is pushed by the gap between the two softened probabilities, divided by the temperature.

At a high temperature the two distributions flatten, so their gap also shrinks, roughly as \(1/\tau\). The gradient then shrinks roughly as \(1/\tau^2\), and Hinton and his co-authors multiply by \(\tau^2\) to undo that [1]. MiniMind follows them; NVIDIA’s ModelOpt library, used later in this post, does not.

Derivation: the gradient of the KL

The student’s log-probabilities are \(\log p_{S,j}^{(\tau)} = z_{S,j}/\tau - \log \sum_k \exp(z_{S,k}/\tau)\). Their derivative is \(\partial \log p_{S,j}^{(\tau)} / \partial z_{S,i} = (\delta_{ij} - p_{S,i}^{(\tau)}) / \tau\), where \(\delta_{ij}\) is 1 when \(i = j\) and 0 otherwise. In equation (2), only the cross term \(-\sum_j p_{T,j}^{(\tau)} \log p_{S,j}^{(\tau)}\) depends on the student. Its derivative is \(-\frac{1}{\tau} \sum_j p_{T,j}^{(\tau)} (\delta_{ij} - p_{S,i}^{(\tau)}) = \frac{1}{\tau} (p_{S,i}^{(\tau)} - p_{T,i}^{(\tau)})\), because the teacher’s probabilities sum to 1.

// § 03

On the laptop: a teacher made the student worse

The setup

This ran in Phase 0, on a laptop RTX 3060 with 6 GB, inside MiniMind. The question: does a teacher help a small model trained from scratch?

  • Teachers: MiniMind’s released models after supervised fine-tuning (SFT). The mixture-of-experts (MoE) teacher has 198M parameters, about 64M active per token. The dense teacher has 64M.
  • Student: dense, hidden 512 and 8 layers, 30.03M parameters, from random weights.
  • Training: one epoch over lines 1–20,000 of MiniMind’s SFT data, at most 340 tokens per sequence.
  • Recipe: learning rate 5e-4 with cosine decay, 128 sequences per update, 157 updates.
  • Arms: MoE teacher and dense teacher (both \(\alpha = 0.5\)), and no teacher (\(\alpha = 1\), CE only).
  • Seeds: 42, 43 and 44; within a seed, all arms share the initial weights and the data order.
  • Test: 2,000 held-out conversations, 552,142 scored tokens, in fp32 with PyTorch’s deterministic mode.

Before the run I wrote down predictions with confidences. P4: for each teacher, all three of its students would beat all three no-teacher students (75%). P7: each teacher’s KL to its own student would be below its KL to the same-seed no-teacher student (85%).

The result

Held-out cross-entropy, nats per token (lower is better):

Arm Seed 42 Seed 43 Seed 44 Mean
MoE teacher (\(\alpha\) 0.5) 2.9743 2.9934 2.9668 2.9782
Dense teacher (\(\alpha\) 0.5) 2.9551 2.9980 2.9818 2.9783
No teacher (\(\alpha\) 1.0) 2.9609 2.9335 2.9261 2.9401

On average, students trained without a teacher had the lowest held-out loss. The MoE-teacher student was worse in 3 of 3 seeds, by 0.014 to 0.060 nats per token. The dense-teacher student was worse in 2 of 3. The seed alone moved a no-teacher student by up to 0.035, the scale of training noise here.

P4 failed, reversed for the MoE teacher, and the two teachers tied: 2.9782 against 2.9783.

With MiniMind's defaults, the no-teacher students had the lowest held-out lossHeld-out cross-entropy, nats/token (lower is better); 30M students from scratch, laptop RTX 3060 (6 GB)seed 42seed 43seed 44mean of 3 seedsheld-out CE (nats/token)2.922.942.962.983.00MoE teacher (α 0.5)dense teacher (α 0.5)no teacher (α 1.0)mean 2.9782mean 2.9783mean 2.9401
Figure 2. with MiniMind’s defaults, the no-teacher student beat the MoE-teacher student in 3 of 3 seeds and the dense-teacher student in 2 of 3. Held-out cross-entropy per arm and seed. Source: the laptop distillation runs (all results).

What this does and does not show

With no teacher effect at all, “worse in 3 of 3” would still happen 1 time in 8: a description, not a claim. The arms also differ in two things. The no-teacher arm puts full weight on CE (\(\alpha = 1\)), the teacher arms only half (\(\alpha = 0.5\)).

That “alpha confound” is the likely cause, and I did not test it. The fair test, a no-teacher arm with the same CE weight, needs 3 runs, about 25 minutes in all; it stayed parked. The released teachers may also have seen these conversations in training, so their own scores here are not held-out.

// § 04

The P7 check: the temperature explains it

A failed prediction, and a contradiction

P7 failed too: on held-out text at \(\tau = 1\), the teacher was closer to its own student in 1 of 6 pairs. Yet MiniMind’s training log, at \(\tau = 1.5\), had every teacher-trained student closer to its teacher than every no-teacher student: 9 of 9. The two measurements differed in temperature, in text (training against held-out) and in code (MiniMind’s against mine).

I ruled out the code first. My KL function and MiniMind’s agree to 6 decimals on the same random logits: 5.812032 at \(\tau = 1\), 7.691720 at \(\tau = 1.5\). That check is now a unit test. Then I measured all 12 teacher-student pairs on both texts at both temperatures, with predictions written first:

  • Q1, held-out text at \(\tau = 1.5\): closer in 6 of 6 (75%).
  • Q2, training text at \(\tau = 1\): closer in at most 2 of 6 (60%).
  • Q3, training text at \(\tau = 1.5\): closer in 6 of 6 (90%); a miss would mean my measurement was broken.

If Q1 and Q2 held, the temperature would explain P7; if only Q3 held, the text would. I fixed that reading before the run.

The result

KL(teacher ‖ own student) minus KL(teacher ‖ no-teacher student), same seed, scaled by \(\tau^2\). Negative means the teacher’s own student is closer.

Text, temperature 42 MoE 42 dense 43 MoE 43 dense 44 MoE 44 dense Closer
Held-out, \(\tau = 1\) +0.0116 −0.0078 +0.0581 +0.0610 +0.0398 +0.0528 1 of 6
Held-out, \(\tau = 1.5\) −0.9379 −0.9490 −0.8371 −0.8151 −0.9256 −0.8728 6 of 6
Training, \(\tau = 1\) +0.0143 −0.0060 +0.0661 +0.0679 +0.0442 +0.0609 1 of 6
Training, \(\tau = 1.5\) −0.9545 −0.9572 −0.8444 −0.8147 −0.9314 −0.8695 6 of 6

All three predictions held. At \(\tau = 1.5\) the teacher’s own students were closer by 0.82 to 0.95; at \(\tau = 1\) the differences were at most 0.07 in size. Training text lowered every KL by about 0.15 to 0.3 with the same pattern: no sign that the students had memorised their data.

So distillation did what it optimised. At \(\tau = 1.5\) the loss gives the teacher’s less likely tokens more weight, and the students learned that view. At \(\tau = 1\) the teacher’s top tokens dominate, and there the no-teacher students, with full CE weight, matched it as well or better.

The lesson is about measurement: P7 measured at \(\tau = 1\) a quantity trained at \(\tau = 1.5\). A prediction about a trained quantity should be measured under the training condition, or say why not.

Students match the teacher at the training temperature, not at τ = 1KL(teacher ‖ own student) − KL(teacher ‖ no-teacher student), τ²-scaled; seeds 42, 43, 44 left to rightMoE teacherdense teachernats/token (below 0: the teacher's own student is closer)−1.0−0.8−0.6−0.4−0.20held-out, τ = 1held-out, τ = 1.5training, τ = 1training, τ = 1.5own closer: 1 of 6own closer: 6 of 6own closer: 1 of 6own closer: 6 of 6
Figure 3. students match their teacher at the training temperature, not at T = 1, on held-out and training text alike. KL to the own student minus KL to the no-teacher student, per seed and teacher. Source: the P7 check (all results).
// § 05

A distillation recipe for the 8×H100 run

For the full model I moved to Megatron-LM, NVIDIA’s training framework (Part 4). It has hooks for NVIDIA Model Optimizer (ModelOpt), a library for quantization and distillation [3]. I pinned ModelOpt 0.48.0, the newest stable release at least two weeks old. Reading its source changed the loss.

\[\mathcal{L}_{\text{ModelOpt}} = (1 - \alpha_{\text{KD}}) \, \mathrm{CE} + \alpha_{\text{KD}} \, \mathrm{KL}\big(p_T^{(\tau)} \,\big\|\, p_S^{(\tau)}\big) \tag{5}\]

where \(\alpha_{\text{KD}}\) (kd_loss_alpha) weights the KL, the reverse of MiniMind’s \(\alpha\), and the KL carries no \(\tau^2\) factor.

In words: ModelOpt mixes the usual next-token loss with the KL to the teacher, and makes no correction for the temperature.

I set \(\alpha_{\text{KD}} = 0.5\) and \(\tau = 1\), so the loss is \(0.5\,(\mathrm{CE} + \mathrm{KL})\), both terms at temperature 1.

Version 0.48 has two traps. Its default \(\alpha_{\text{KD}} = 1\) trains on the KL alone, with no CE at all. It also removed kd_loss_scale and skip_lm_loss, so the YAML in Megatron’s own distillation docs no longer runs.

Removing the alpha confound with Adam

A factor of 0.5 on the whole loss looks as if it halves the learning signal. With Adam, the optimizer used throughout this project, it does not:

\[\theta \leftarrow \theta - \eta \, \frac{\hat m}{\sqrt{\hat v} + \epsilon}, \qquad g \leftarrow g \cdot \min\!\Big(1, \frac{G_{\max}}{\lVert g \rVert}\Big) \tag{6}\]

where \(\theta\) are the weights, \(\eta\) the learning rate and \(g\) the gradient. \(\hat m\) and \(\hat v\) are Adam’s bias-corrected running means of \(g\) and \(g^2\), \(\epsilon\) is a small constant, and \(G_{\max}\) is the clip threshold.

In words: Adam divides the gradient’s running mean by its running size, so a constant factor on the loss cancels, except through \(\epsilon\) and clipping.

So the teacher arm halves its clip threshold, to 0.5 against the no-teacher arm’s 1.0. Its updates are then those of \(\mathrm{CE} + \mathrm{KL}\) under the same clipping, and the teacher is the only difference left.

Derivation: why a constant factor drops out of Adam

Multiply the loss by \(c > 0\). Every gradient becomes \(c\,g\), so \(\hat m\) becomes \(c\,\hat m\) and \(\hat v\) becomes \(c^2 \hat v\), leaving \(\hat m / \sqrt{\hat v}\) unchanged. Clipping keeps the match, because \(\lVert c\,g \rVert \le c\,G_{\max}\) exactly when \(\lVert g \rVert \le G_{\max}\). Megatron’s AdamW applies weight decay outside the loss, so the factor does not touch it either.

The design

  • Student: dense, 314M parameters (12 layers, hidden 1024, FFN 4,096), 131M of them in the tied embedding.
  • Data: 400 steps of 256 rows × 4,096 tokens, 420M tokens of FineWeb-Edu, with the same initial weights and data order in both arms.
  • Teacher: the pretrained MoE base (Part 6), frozen. FineWeb-Edu is its training distribution, while the RL model is tuned for math answers.
  • Measures: held-out CE at \(\tau = 1\), where the model is used, and the KL at the training temperature: the P7 lesson.
  • Memory: the KL holds full-vocabulary fp32 logits, 4,096 × 128,256 × 4 bytes = 2.1 GB per row and tensor. So runs with a teacher use micro-batches of 1 or 2 rows.
// § 06

4-bit weights, explained

Quantization: a scale and a rounding step

Quantization stores numbers in fewer bits. A bf16 weight takes 16 bits; a 4-bit integer (INT4) holds only 16 values, from −8 to 7. For our model, the plan estimated about 14 GB in bf16 and about 4 GB with 4-bit experts. The routed experts hold 94% of the weights.

Each block of 128 consecutive weights shares one scale. With “max calibration”, the block’s largest weight sets it:

\[s_b = \frac{\max_{i \in b} |w_i|}{7}, \qquad q_i = \operatorname{clamp}\!\Big(\operatorname{round}\big(w_i / s_b\big), \, -8, \, 7\Big), \qquad \hat w_i = s_b \, q_i \tag{7}\]

where \(b\) is a block of 128 weights along the input dimension, and \(w_i\) a weight in it. \(s_b\) is the block’s step size, \(q_i\) the stored 4-bit integer, and \(\hat w_i\) the dequantized weight that the matrix multiply uses.

In words: divide each weight by its block’s step, round to an integer, store it, and multiply back by the step when computing.

A worked example with four illustrative weights: 0.70, −0.23, 0.06 and −0.38. The step is 0.70 / 7 = 0.1, so the codes are 7, −2, 1 and −4. The weights come back as 0.70, −0.20, 0.10 and −0.40, each within half a step of the original.

Because the largest weight maps exactly to ±7, the codes stay within −7 and 7: the rehearsal’s checkpoint had none at −8. The damage is measured as a relative error:

\[e = \frac{\lVert \hat W - W \rVert_F}{\lVert W \rVert_F} \tag{8}\]

where \(W\) is a weight matrix, \(\hat W\) its dequantized copy, and \(\lVert \cdot \rVert_F\) the square root of the sum of its squared entries.

In words: the rounding error’s size as a fraction of the weights’ size.

Xuewen Yang names the weak point [5]: “Outliers are the whole problem.” One large weight stretches the step for its whole block, and its small neighbours round to zero. Smaller blocks contain the damage; NVIDIA’s NVFP4 format uses blocks of 16.

4-bit weights: one scale per block, rounding in the forward pass onlyINT4 weights with bf16 activations (W4A16), blocks of 128; illustrative values for four weightsQ(w): rounding, forward pass onlyMaster weights wfull precision (bf16)one block of 128:0.70, −0.23,0.06, −0.38, …Largest |w|max = 0.701Step ss = max / 7= 0.12Roundq = round(w / s)= 7, −2, 1, −43StoredINT4 codes in −7..7+ one scale s4Dequantizeŵ = s · q= 0.70, −0.20,0.10, −0.405ŵ, bf16bf16 matmuly = x ŵW4A16: 4-bit weights,16-bit activations6Activations xbf16backward (STE):∂L/∂w := ∂L/∂ŵ
Figure 4. 4-bit weights keep one scale per block and round only in the forward pass; the backward pass goes straight through. Diagram, labelled with this post’s illustrative four-weight example.

PTQ, QAT and QAD

Post-training quantization (PTQ) applies equation (7) once to the trained weights. With max calibration and weights only, it needs no data.

Quantization-aware training (QAT) keeps full-precision master weights and trains through the rounding, so the model learns to work on the grid. The catch: rounding has zero gradient almost everywhere. The straight-through estimator (STE) gets around it:

\[\text{forward: } \hat w = Q(w), \qquad \text{backward: } \frac{\partial \mathcal{L}}{\partial w} := \frac{\partial \mathcal{L}}{\partial \hat w} \tag{9}\]

where \(Q\) is the quantize-then-dequantize map of equation (7), \(w\) a full-precision master weight and \(\mathcal{L}\) the training loss.

In words: the forward pass uses the rounded weights, and the backward pass acts as if the rounding were not there.

In PyTorch it is one line, w_hat = w + (quantize(w) - w).detach(). As Xuewen Yang puts it, “Gradients pass straight through the rounding to full-precision master weights” [5].

Quantization-aware distillation (QAD) is QAT whose loss is the KL to the original model:

\[\mathcal{L}_{\text{QAD}} = \mathrm{KL}\big(p_{\text{bf16}} \,\big\|\, p_{\text{4-bit}}\big) \tag{10}\]

where \(p_{\text{bf16}}\) is the frozen bf16 model’s next-token distribution at temperature 1, and \(p_{\text{4-bit}}\) is its fake-quantized copy’s.

In words: the 4-bit copy learns to predict what the original predicts, token by token, with no labels.

The model to quantize is the final model of the RL stage, trained with the GSPO algorithm (Part 9). CE on the SFT answers would pull it back toward its pre-RL behaviour, so I chose QAD. QAT also comes after RL, because RL would undo it.

The format I chose

  • W4A16 INT4: 4-bit integer weights, 16-bit activations, one scale per block of 128 inputs.
  • Routed experts only: 2,048 quantizers, 16 layers × 64 experts × 2 linear layers. Router, attention, embeddings and output layer stay in bf16.
  • Why integers: H100 has no FP4 tensor cores, and NVFP4’s 16-value blocks would leave QAT little to show.
  • QAD length: one epoch of the SFT data, 35 steps of 64 sequences, or 15 minutes, whichever ends first.
  • The test: one validation pass each for bf16, PTQ and QAD on 46 SFT validation lines: held-out loss and KL to bf16.

Thinking Machines chose the same target in “End-to-End 4-Bit Training”: MoE layers, which “empirically tend to harm accuracy less than other layers when quantized” [4]. Their recipe, though, runs 4-bit forward passes from pretraining through RL, on GPUs with FP4 compute.

// § 07

What the one-H100 rehearsal showed

Before the 8×H100 run, I rehearsed every stage on one H100 with shrunk models: 2 layers, hidden size 512. The shrunk MoE teacher had 8 experts with top-2 routing. A rehearsal shows that each step runs and records its metrics; its losses say nothing about the full model.

Distillation ran end to end

Two shrunk dense students trained for 16 steps of 16 sequences each, from the same weights and data. The one with the shrunk MoE as teacher ended closer to it: KL 0.215, against 0.293 for the CE-only student. Their held-out CE was 7.808 and 7.828, against the teacher’s 7.480. That tests the plumbing, not distillation itself.

The serving benchmark also ran end to end in the same rehearsal session; Part 11 covers it.

The V4 expert-bias rule fought QAD

In the rehearsal, our router followed DeepSeek-V4: each expert has a bias, added to its routing score when the top experts are chosen. After every update the bias moves by a fixed step toward even load:

\[b_e \leftarrow b_e + \gamma \, \operatorname{sign}\big(\bar n - n_e\big) \tag{11}\]

where \(b_e\) is expert \(e\)’s bias and \(n_e\) the number of tokens routed to it in the step. \(\bar n\) is the mean over experts, and \(\gamma = 10^{-3}\) is the update rate.

In words: after every update, overloaded experts lose a little bias and underloaded ones gain a little, whatever the loss says.

The rule comes from auxiliary-loss-free balancing [6]. The rehearsal exposed its side effect in fine-tuning: it moves the model even when there is nothing to learn.

In GSPO calibration runs, every advantage (the per-answer learning signal) was 0. Yet the KL to the starting model rose after the first update. It reached 4.6e-4 at a learning rate of 1e-7, and 3.6e-4 at 1e-6: no growth with a 10× rate. The bias had changed in 87–100% of its entries.

With the rule frozen, the KL after updates 1, 2 and 3 was 0.0, 1.1e-6 and 7.0e-5. QAD showed the same signature. The table compares the rehearsal’s 4-bit models after 8 QAD steps, on one validation batch of 16 SFT sequences.

Model (shrunk, rehearsal) KL to bf16 (nats/token) Held-out loss (nats/token)
bf16 (the reference) 0 6.7945
Plain 4-bit (PTQ) 1.815e-3 6.7982
QAD, bias rule on 2.826e-3 6.7957
QAD, bias rule frozen 1.800e-3 6.7956
QAD, bias frozen, balance loss off 1.825e-3 6.7956

With the rule on, QAD ended further from the bf16 model than plain rounding did. With it frozen, QAD ended just under plain rounding: too small a gap to call a gain after 8 steps on a toy model. The held-out losses differ only in the third decimal, so only the KL column is sensitive here.

I froze the bias in QAT and GSPO. My reason for QAT: a short fine-tune is not long enough for the bias to settle on new routing patterns. I tested the router’s balance loss with a rule set beforehand: drop it only if the KL fell below 1.800e-3. It came out at 1.825e-3, so QAD keeps it.

The 8×H100 run later replaced the bias rule with quantile balancing (Part 6). Its QAT stage freezes that router too, keeping the learned biases exactly.

The expert-bias rule pushed QAD further from the bf16 model than plain roundingEvaluation KL to the bf16 model, one validation batch of 16 sequences, after 8 QAD steps; dashed: plain 4-bitevaluation KL(bf16 ‖ model), 10⁻³ nats/token (shrunk rehearsal model)00.511.522.53plain 4-bitQADQADQAD(PTQ)bias rule onbias frozenfrozen, balance loss off1.8152.8261.8001.825
Figure 5. on the rehearsal model, the expert-bias rule pushed QAD further from the bf16 model than plain rounding. KL(bf16 ‖ model) per variant after 8 QAD steps. Source: the rehearsal’s QAT evaluations (all results).

Take the 4-bit codes from the QAT model

A 4-bit kernel stores the codes \(q_i\) and scales \(s_b\), and computes with \(\hat w_i\). Where should the codes come from? My report script compared two answers on the rehearsal’s PTQ and QAD checkpoints:

  • Codes taken from ModelOpt’s own fake quantization match the QAT forward pass exactly: weight and layer-output gaps of 0.0.
  • Codes recomputed from the weights with equation (7) agree on 99.94% of weights. The rest are one step off, and layer outputs then differ by up to about 4%.

ModelOpt’s internal arithmetic differs from a plain recomputation on about 0.05% of weights. Even the order of operations changes the rounding at exact ties: 37 of 131,072 weights in one rehearsal tensor. So a 4-bit kernel must use the codes the QAT model trained with, not recompute them.

The same report gave the quantization error of equation (8): 11.7%, with the codes symmetric around 0.

A ModelOpt 4-bit model cannot run in Megatron’s RL inference engine

The number I wanted most was the 4-bit model’s math accuracy on GSM8K, a set of grade-school math word problems. Our evaluation script, eval.sh, runs Megatron’s RL program (train_rl.py) and its inference engine. Three failures came in a row:

  1. The checkpoint did not load: the RL program built Megatron’s standard model, not ModelOpt’s. Our Megatron patch fixed that.
  2. The engine raised NotImplementedError: flash_decode_and_prefill is not compatible with ModelOpt KV cache quantization. ModelOpt’s attention wrapper replaces that method with one that raises, and the engine has no other attention path.
  3. The run then hung. The error was raised inside the engine’s asynchronous task, so the main loop kept waiting. I stopped it by hand just before the stall guard would have; a new log file had restarted the guard’s clock.

The options, as I recorded them:

  • (a) leave the 4-bit accuracy out, and judge QAT by held-out loss and KL to bf16;
  • (b) a GSM8K evaluator on Megatron’s static generation path, about 1–2 hours of work;
  • © fold the 4-bit rounding into ordinary bf16 weights, then run the normal evaluation: about 2–3 hours, after the run.

I chose (a), and eval.sh now refuses the 4-bit mode with the reason. The guide for the 8×H100 run also lost a 4-bit step that would have hung for about 30 minutes.

// § 08

Why both were cut from the 8×H100 run

The plan ranked every stage before the run. Pretraining, the router-balancing study, SFT and GSPO could not be cut; QAT was to go first, then distillation. In the plan’s words, “Distillation and QAT are bonus results.”

Both sat in block B6 with a serving benchmark, after the main model was finished: about 65 minutes in all. The 8×H100 run had a fixed end, about 12.1 hours after the node started.

The decision came at GSPO’s calibration, just before the RL run. On 8 GPUs the very first update already showed small numerical differences, so I first raised GSPO’s clip above that noise (Part 9). Then I split the remaining time. The options:

  • GSPO for about 100 minutes, with an 85-minute training cap, and only serving from B6;
  • shorter GSPO runs, of 28, 55 or 70 minutes, keeping more of B6.

I chose the long GSPO run, as the plan’s priorities said. The 4-bit accuracy was already off the list, since Megatron’s engine could not run the model. Later that night I extended the run, and the extra time went to a second GSPO arm, not back to B6.

So no full-size distillation or QAT result exists. Every H100 number in this post comes from shrunk models.

// § 09

What I would try next

  1. The fair laptop test. A no-teacher arm with the teacher arms’ CE weight would settle the alpha confound: 3 runs, about 25 minutes.
  2. Full-size distillation with the recipe above: CE + KL at \(\tau = 1\), the clip halved.
  3. QAD at full length, with the bias frozen. One epoch of 35 steps, then the bf16, PTQ and QAD comparison.
  4. The 4-bit model’s math accuracy. ModelOpt can fold the rounding into ordinary weights (mtq.fold_weight), and eval.sh can run the exported bf16 checkpoint. I estimated about half a day of work plus GPU testing.
  5. A real INT4 kernel that takes its codes from the QAT model. Its gap to the fake-quantized forward should read 0.
  6. Low precision from the start. Thinking Machines configure numerics the same way “from the start of pretraining through the final RL rollout” [4]. That “eliminates the need for calibration when a quantized checkpoint is delivered at the end of training” [4].

In their largest jobs this gave an 8% wall-clock speedup in pretraining and 20% in RL over full MXFP8 training. Their RL sampler runs the 4-bit model itself, which a post-training stage like mine never reaches. It needs GPUs with FP4 compute, which H100 lacks.

// § 10

Run it yourself

Hardware and time.

  • Laptop: an RTX 3060 laptop GPU with 6 GB. The step-1 script took about 2 hours with the dense-against-MoE slices; each distillation run took 7.5–7.8 minutes. The P7 check took 23 minutes.
  • One H100, rehearsal size: each distillation run took 75–110 s, each QAT run 44–244 s. The full-size runs below were never made.

Laptop (Phase 0), from code/. The scripts refuse uncommitted code, run MiniMind’s trainers unmodified in deterministic mode, and checksum the downloaded teachers.

cd code
bash scripts/moe_distill.sh    # 3 arms x 3 seeds, the dense/MoE slices, the held-out evaluation, the analysis
bash scripts/p7_kl_check.sh    # the 2 x 2 check: {held-out, training} text x {T = 1, T = 1.5}

The results land in logs/moe_distill_analysis_corrected.txt, logs/p7_kl_heldout.json and logs/p7_kl_train.json.

H100 (Megatron and ModelOpt), from the repo root. Set up the machine as in the setup guide, which installs ModelOpt 0.48.0 and writes env.sh. The guide’s run helper wraps each stage in a stall guard:

source /workspace/env.sh
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

# Distillation: a 314M dense student, 400 steps, without (ce) and with (kd) the pretrained MoE as teacher
export DISTILL_DIR=/workspace/distill TEACHER_CKPT=/workspace/ckpt/pretrain
DISTILL_ARM=ce run distill_ce bash $M/distill.sh
DISTILL_ARM=kd run distill_kd bash $M/distill.sh
for a in ce kd teacher; do DISTILL_STAGE=eval DISTILL_ARM=$a run distill_eval_$a bash $M/distill.sh; done
grep -h "validation loss" /workspace/runs/distill_eval_*.log

# 4-bit: PTQ, then QAD, then one validation pass each for bf16, PTQ and QAD
export GSPO_CKPT=/workspace/ckpt/gspo QAT_DIR=/workspace/qat
QAT_STAGE=ptq run qat_ptq bash $M/qat_4bit.sh
QAT_STAGE=qad run qat_qad bash $M/qat_4bit.sh
for m in bf16 ptq qad; do QAT_STAGE=eval QAT_MODEL=$m run qat_eval_$m bash $M/qat_4bit.sh; done
grep -h "validation loss" /workspace/runs/qat_eval_*.log

# The 4-bit weights: quantization error, code histogram, and the export gaps
for m in ptq qad; do
  python -I code/scripts/qat_weights_report.py "$MEGATRON_PATH" /workspace/qat/$m --name $m \
      --json /workspace/runs/qat_weights_$m.json
done
  • In each evaluation log, “lm loss” is the held-out CE at \(\tau = 1\); “logits distillation loss” is the KL to the teacher or to bf16.
  • The rehearsal ran these same scripts at toy size, with the shrunk teacher of teacher_moe_rehearsal.yaml; the distillation and 4-bit guide has the steps.

What to expect. At rehearsal size, the KD student’s KL to the teacher was 0.215 against 0.293. PTQ’s KL to bf16 was 1.815e-3, and QAD’s with the bias frozen 1.800e-3. At full size there are no numbers yet.

Config files, all in code/scripts/megatron/modelopt/: distill_alpha05.yaml (the loss), int4_experts_w4a16.yaml (the 4-bit recipe), and teacher_moe.yaml with teacher_moe_rehearsal.yaml (the teacher, full and shrunk).

Code:

The step-by-step version is the distillation and 4-bit guide.

// § 11

What to take away

  • Distillation optimizes a match at its own temperature. Measure the KL there, and accuracy where the model is used.
  • A teacher is not free: with half the CE weight, from-scratch students ended worse in 5 of 6 seed pairs.
  • Under Adam, a constant factor on the loss only changes clipping, so scale the clip threshold with it.
  • 4-bit weights are a block scale and a rounding step. QAT trains through the rounding with the STE, and QAD needs no labels.
  • Freeze anything that moves the weights on its own, such as the V4 expert-bias rule, before distilling into a model.
  • Before planning around a 4-bit model, check that it runs where you need it.

References

  1. G. Hinton, O. Vinyals and J. Dean, “Distilling the Knowledge in a Neural Network”, 2015. arXiv:1503.02531
  2. J. Gong, MiniMind. github.com/jingyaogong/minimind
  3. NVIDIA Model Optimizer (nvidia-modelopt), version 0.48.0. pypi.org/project/nvidia-modelopt
  4. J. Cook and Thinking Machines Lab, “End-to-End 4-Bit Training”, Thinking Machines Lab: Connectionism, November 2026. thinkingmachines.ai/blog/end-to-end-4-bit-training
  5. X. Yang, “Quantization, from one block of weights to 4-bit training”, November 2026. xuewyang.github.io/posts/quant
  6. L. Wang et al., “Auxiliary-Loss-Free Load Balancing Strategy for Mixture-of-Experts”, 2024. arXiv:2408.15664
  7. NVIDIA, Megatron-LM, at commit d6316eb45. github.com/NVIDIA/Megatron-LM

Cite this

@misc{aboagye2026minidsv4part12,
  author       = {Aboagye, Prince Osei},
  title        = {Mini DeepSeek V4, Part 12: Distillation and 4-bit training: tested on a laptop and one H100, then cut},
  year         = {2026},
  howpublished = {\url{https://poaboagye.github.io/blog/12-distillation-and-4bit/}}
}