Post · 8 min read· pp. 30–34

The KL Gradient–Recovery Trick

Jayden Teoh

5 Nov 2026

In this post, I'll introduce the KL gradient–recovery trick, a method to recover the gradient of a forward KL divergence between two distributions produced by the same linear output head, without materializing logits or computing the KL itself.

Setup

Let's say you have a shared linear output head WW mapping hidden states, hT\mathbf{h}_{T} and hS\mathbf{h}_{S}, to different distributions:

q=softmax ⁣(WhT),p=softmax ⁣(WhS).q = \mathrm{softmax}\!\left(W \mathbf{h}_{T}\right), \qquad p = \mathrm{softmax}\!\left(W \mathbf{h}_{S}\right).

We'll call qq the teacher and pp the student. We want the gradient of the forward KL with the teacher detached,

∇hSDKL ⁣(sg(q) ∥ p),\nabla_{\mathbf{h}_{S}} D_{\mathrm{KL}}\!\left(\mathrm{sg}(q) \,\|\, p\right),

and we want it without ever materializing pp or qq.

KL gradient–recovery trick

Let's measure the cross-entropy of each distribution against the same arbitrary token yy, a one-hot target. We can use the fused cross-entropy kernels1.Hsu et al. (2025)Liger-Kernel: Efficient Triton Kernels for LLM TrainingCODEML Workshop @ ICML 20252.Wijmans et al. (2025)Cut Your Losses in Large-Vocabulary Language ModelsICLR 2025 common in modern pre-training, which save memory by never materializing full logits.

The student's and teacher's cross-entropy gradients with respect to their own hidden states and the KL gradient we want are:

∇hSCE(y,p)=W⊤ ⁣(p−y),∇hTCE(y,q)=W⊤ ⁣(q−y),\nabla_{\mathbf{h}_{S}} \mathrm{CE}(y, p) = W^\top\!\left(p - y\right), \qquad \nabla_{\mathbf{h}_{T}} \mathrm{CE}(y, q) = W^\top\!\left(q - y\right),
∇hSDKL ⁣(sg(q) ∥ p)=W⊤ ⁣(p−q).\nabla_{\mathbf{h}_{S}} D_{\mathrm{KL}}\!\left(\mathrm{sg}(q) \,\|\, p\right) = W^\top\!\left(p - q\right).

Now subtract the teacher's gradient from the student's:

∇hSCE(y,p)−∇hTCE(y,q)=W⊤ ⁣(p−y)−W⊤ ⁣(q−y)=W⊤ ⁣(p−q)=∇hSDKL ⁣(sg(q) ∥ p).\begin{aligned} \nabla_{\mathbf{h}_{S}} \mathrm{CE}(y, p) - \nabla_{\mathbf{h}_{T}} \mathrm{CE}(y, q) &= W^\top\!\left(p - y\right) - W^\top\!\left(q - y\right) \\[2pt] &= W^\top\!\left(p - q\right) \\[2pt] &= \nabla_{\mathbf{h}_{S}} D_{\mathrm{KL}}\!\left(\mathrm{sg}(q) \,\|\, p\right). \end{aligned}

The forward-KL gradient is the difference of the two cross-entropy gradients; the shared one-hot target cancels.

> implementing it

You do not need to touch the two gradients by hand. You can let autograd handle it for you. We'll augment the teacher's hidden state before calculating the softmax:

h~T=hT+[sg(hS)−hS]⏟=0.\tilde{\mathbf{h}}_{T} = \mathbf{h}_{T} + \underbrace{\big[\mathrm{sg}(\mathbf{h}_{S}) - \mathbf{h}_{S}\big]}_{=0}.

This resembles the stop-gradient used in straight-through estimators3.Bengio et al. (2013)Estimating or Propagating Gradients Through Stochastic Neurons for Conditional ComputationarXiv:1308.34324.van den Oord et al. (2017)Neural Discrete Representation LearningNeurIPS 2017. The bracket term is numerically zero:

softmax ⁣(WhT)=softmax ⁣(Wh~T)=q,\mathrm{softmax}\!\left(W \mathbf{h}_{T}\right) = \mathrm{softmax}\!\left(W \tilde{\mathbf{h}}_{T} \right) = q,

so the teacher's cross-entropy loss and gradient are both untouched. Now, autograd injects the teacher's cross-entropy gradient into the student's hidden state via the bracket term; hS\mathbf{h}_{S} picks up an additional gradient term ∇hSCE(y,q)=−W⊤(q−y)\nabla_{\mathbf{h}_{S}} \mathrm{CE}(y, q) = -W^\top(q-y), since the bracket has Jacobian −I-I with respect to hS\mathbf{h}_{S}.

Sum the gradient terms that the student receives:

∇hSCE(y,p)+∇hSCE(y,q)=W⊤(p−y)−W⊤(q−y)=W⊤(p−q)=∇hSDKL ⁣(sg(q) ∥ p),\begin{aligned} \nabla_{\mathbf{h}_{S}} \mathrm{CE}(y, p) + \nabla_{\mathbf{h}_{S}} \mathrm{CE}(y, q) &= W^\top(p-y) - W^\top(q-y) \\[2pt] &= W^\top(p-q) \\[2pt] &= \nabla_{\mathbf{h}_{S}} D_{\mathrm{KL}}\!\left(\mathrm{sg}(q) \,\|\, p\right), \end{aligned}

which is the forward KL gradient we wanted!

Here is a PyTorch implementation of the trick:

python
# teacher and student share the output head W
h_t = teacher(x)
h_s = student(x)

# straight-through gradient routing
# h_t is numerically unchanged
h_t = h_t + (h_s.detach() - h_s)

loss = ce(W, h_t, y) + ce(W, h_s, y)
loss.backward()

# h_s.grad is now exactly W.T @ (p - q),
# the forward-KL gradient. no logits were built.

There are two things that make this trick appealing computation-wise.

  • First, the subtraction happens in hidden-state space, of dimension dd, not in logit space, of dimension VV, and d≪Vd \ll V in cases like language pre-training.
  • Second, you never compute either gradient term yourself. W⊤(p−y)W^\top(p-y) is just the gradient that backpropagating a cross-entropy loss already gives you, and fused cross-entropy kernels produce it without ever materializing the logits pp. So the trick keeps the memory-saving benefits that those fused kernels provide.

> caveats

There are, of course, some caveats to this trick. It only works under certain conditions:

  • Teacher and student must share the same linear output head WW, and therefore the same vocabulary.
  • Both must have a cross-entropy computed against the same target token, any shared token will do.
  • Forward KL only, with a detached teacher.

Note: hS\mathbf{h}_{S} and hT\mathbf{h}_{T} receive the exact KL gradient, but the shared head WW itself still receives a cross-entropy gradient, not a KL one. If you need WW updated under the KL too, this trick will not give you that. For the many methods that freeze the output head under the KL loss, this is a non-issue.

Multi-token prediction

Multi-token prediction (MTP; 5.Stern et al. (2018)Blockwise Parallel Decoding for Deep Autoregressive ModelsNeurIPS 20186.Qi et al. (2020)ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-trainingEMNLP Findings 20207.Gloeckle et al. (2024)Better & Faster Large Language Models via Multi-token PredictionICML 2024) trains a model to predict several future tokens instead of just the next one. It is now a standard ingredient in frontier pre-training8.DeepSeek-AI (2024)DeepSeek-V3 Technical ReportarXiv:2412.194379.Qwen Team (2025)Qwen3 Technical ReportarXiv:2505.0938810.Meituan LongCat Team (2025)LongCat-Flash Technical ReportarXiv:2509.0132211.GLM-4.5 Team (2025)GLM-4.5: Agentic, Reasoning, and Coding (ARC) Foundation ModelsarXiv:2508.0647112.Ling Team (2025)Every Activation Boosted: Scaling General Reasoner to 1 Trillion Open Language FoundationarXiv:2510.2211513.NVIDIA (2025)NVIDIA Nemotron 3: Efficient and Open IntelligencearXiv:2512.2085614.Xiaomi LLM-Core Team (2026)MiMo-V2-Flash Technical ReportarXiv:2601.0278015.GLM-5-Team (2026)GLM-5: from Vibe Coding to Agentic EngineeringarXiv:2602.1576316.DeepSeek-AI (2026)DeepSeek-V4: Towards Highly Efficient Million-Token Context IntelligencearXiv:2606.1934817.MiniMax (2026)The MiniMax-M2 Series: Mini Activations Unleashing Max Real-World IntelligencearXiv:2605.2649418.Kimi Team (2026)Kimi K3: Open Frontier IntelligencearXiv:2607.24653 because it enables faster inference via speculative decoding.

But everyone trains the MTP heads with plain cross-entropy against the data. If the goal is acceptance under the next-token prediction (NTP) distribution, we should arguably self-distill the MTP heads toward it instead. Naively minimizing the forward KL between the MTP and NTP distributions is expensive, and this is exactly where the KL gradient–recovery trick comes in!

MTP satisfies the requirements of the KL gradient–recovery trick by construction: the kk-th MTP head pushes its hidden state through the same output matrix (also commonly called the "LM head") as the next-token head, and it predicts the same target token that the NTP head predicts kk steps ahead.

The KL gradient–recovery trick can be applied to each MTP head, and the resulting gradient is exactly the gradient of the forward KL between the MTP and NTP distributions. This enables what we call Soft Multi-Token Prediction (Soft-MTP).

Illustration of Soft-MTP.

Illustration of Soft-MTP.

From 167M to 1.2B parameters, Soft-MTP improves over standard MTP with lower NTP perplexity, faster speculative decoding and higher zero-shot accuracy at every scale, plus stronger math and code reasoning after fine-tuning. It also matches or beats explicit KL minimization on most metrics, at a fraction of the cost.

Please check out the Soft-MTP paper and the code for more details.

> other use cases

The KL gradient–recovery trick applies more broadly beyond MTP. Many post-training methods require the expensive KL, e.g., MTP Boosting, on-policy self-distillation, score centering for off-policy RL19.NVIDIA (2026)Nemotron 3 Ultra: Open, Efficient Mixture-of-Experts Hybrid Mamba-Transformer Model for Agentic ReasoningarXiv:2606.1500720.Zhao et al. (2026)Self-Distilled Reasoner: On-Policy Self-Distillation for Large Language ModelsarXiv:2601.1873421.Marek and Ryabinin (2026)Score Centering Stabilizes Off-Policy Reinforcement LearningarXiv:2609.20807. This trick, with some creative adaptations, can be useful in these settings too, and I hope it will be adopted more widely.

BibTeX

If you find this work useful, please cite the Soft-MTP paper where this trick came from:

text
@article{teoh2026multitokenpredictionright,
title={Multi-Token Prediction Done Right},
author={Teoh, Jayden},
journal={arXiv preprint arXiv:2610.38331},
year={2026}
}