You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Knowledge Distillation requires both LLM's to be loaded into memory causing extreme vram requirements. With unsloths incredible vram efficiency, will we be able to create a practical solution to distill from the Open Source Behemoths like Deepseek and Kimi K2?
Knowledge distillation now works under Unsloth. Answering the original question directly first, because the honest answer is mixed: no, this does not let you distil from Deepseek or Kimi K2 on a small card. What it does is make logit-level KD work correctly and efficiently when both models fit. More on why the Behemoth case is different, and what actually solves it, at the bottom.
What works now
GKDTrainer under Unsloth crashed before this, which is what #1941 reported. Three fixes landed and are live on PyPI in unsloth==2026.9.8 and unsloth_zoo==2026.9.7:
Keep generated RL configs on the reentrant gradient checkpoint path #11519, the one that actually unblocked it. GKDConfig turns gradient_checkpointing on by default and older TRL leaves gradient_checkpointing_kwargs as None, at which point transformers substitutes {"use_reentrant": False}. That overrides use_gradient_checkpointing="unsloth" and puts the run on the non-reentrant path, which is the only one that compares what each pass saved, so training died with CheckpointError: A different number of tensors was saved during the original forward and recomputation (81 against 79 on a Kaggle 2x T4). The value is now pinned rather than deleted.
Two limits found while testing, both documented in the notebook rather than papered over. gemma-4 needs UNSLOTH_COMPILE_DISABLE=1 on T4-class cards, where the gradient-checkpoint recompute disagrees with the forward about tensor rank; it is fine on newer cards. And self-distillation, where the teacher is the student's own frozen base, cannot learn from a fresh adapter: lora_B is zero-initialised, so the student starts identical to its teacher, and GKD's loss is a divergence and nothing else, so it starts at its global minimum with no gradient. Use that mode to continue from an adapter you already have.
Why this does not reach Deepseek or Kimi K2, and what does
The memory that KD wastes comes in two parts, and this work only addresses one of them.
The first is the logits. GKDTrainer materialises a full (batch, seq, vocab) tensor for both models, which on a 262144-token vocabulary dominates everything else. A chunked generalized JSD that never builds that tensor is merged in unslothai/unsloth-zoo#1310, though it is not yet wired into the GKD path, so today's runs still pay for it. That is the next piece of work.
The second is the teacher's weights, and chunking does nothing for those. A 671B teacher does not fit next to a student on any single machine, so no amount of loss-side efficiency makes the original request work as stated.
The approaches that actually do:
Offline top-k teacher logprobs. Run the teacher once, ahead of time, and store only its top-k logprobs per position in the dataset. Training then holds no teacher at all. This is what @Erland366 described above, and it is the only route under which distilling from a Behemoth on consumer hardware is real. It needs a dataset-preparation path rather than just a trainer patch, so it is scoped separately.
Served teacher. TRL ships ServerDistillationTrainer and AsyncDistillationTrainer, where the teacher sits behind a vLLM server and returns top-k logprobs over HTTP, never occupying training VRAM. These live under trl.experimental.*, which Unsloth's trainer discovery does not currently scan, so they run unpatched today.
4-bit teacher, which works now and helps mid-size teachers only.
To @Rakshith12-pixel's question, for a trained OCR teacher and an untrained student: the simplest effective route is sequence-level KD, which is just SFT on the teacher's generated outputs and is already supported. Use the logit-level path in the notebook above when you want the teacher's full distribution rather than its argmax, and when both models fit.
Closing this, since logit-level KD works and is released. The chunked-loss wiring and the top-k path will be tracked as their own issues rather than left under a thread this long.
Follow-ups opened, so the remaining work is tracked rather than left here: #11554 for wiring the chunked JSD into the GKD path, and #11556 for the teacher that never enters training memory (offline top-k logprobs and served teachers).
Updating this, because the limitation I described when I closed it no longer holds and the numbers it was missing now exist.
I said the chunked generalized JSD was merged in unslothai/unsloth-zoo#1310 but "not yet wired into the GKD path, so today's runs still pay for it". That wiring landed in #12136 (with #13190 for the GradScaler) and is released in unsloth==2026.10.5 / unsloth_zoo==2026.10.5.
So I measured the thing that had only ever been asserted. UNSLOTH_GKD_CHUNKED=0 falls back to TRL's dense loss, which makes a clean one-variable differential: same seed, same data, same models, 10 steps, batch 2, seq 1024, 4-bit student and teacher.
student <- teacher
vocab
peak VRAM, dense
peak VRAM, chunked
loss, dense
loss, chunked
Qwen3-0.6B <- Qwen3-1.7B
151936
22.11 GiB
3.33 GiB
0.094933
0.094956
gemma-4 E2B <- E4B
262144
51.34 GiB
23.63 GiB
0.069254
0.069089
6.6x and 2.2x off the peak, with the loss agreeing to 2.3e-05 and 1.7e-04 respectively, which is accumulation order rather than a behaviour change. It is not slower either: on gemma-4 the chunked arm finished in 11.1 s against 14.8 s dense, since it never allocates or writes the big tensor.
Two honest qualifications. The reduction is not uniform: gemma-4 gains less in relative terms because its peak carries more than logits, so the win depends on how much of your footprint was the (batch, seq, vocab) tensor. And this still does nothing for the teacher's weights, which was the original ask here. A Deepseek- or Kimi-K2-class teacher does not fit beside a student regardless of how cheap the loss becomes; that needs an offline top-k or served teacher, tracked in #11556.
Worth it for anyone arriving at this thread: logit-level KD on a free Kaggle 2x T4 is now considerably cheaper than the numbers in the notebook, which were measured before the chunked path existed. I am refreshing those.
Correcting one line in my comment above. I wrote "it is not slower either" and generalised that from a single configuration, which was wrong.
Re-measuring at the settings the notebook actually ships (batch 1, gradient accumulation 4, 30 steps, seq 1024, Qwen3-0.6B from Qwen3-1.7B, both 4-bit):
peak VRAM
loss
runtime
dense (UNSLOTH_GKD_CHUNKED=0)
9.94 GiB
0.375788
49.3 s
chunked
3.03 GiB
0.375766
63.5 s
So 3.3x off the peak, same loss to 2.2e-05, but about 30% slower at batch 1. The chunk loop's per-chunk overhead is fixed while the memory it saves scales with batch and sequence, which is why the earlier batch-2 runs showed no slowdown and gemma-4 at batch 2 was actually faster. The honest summary is that chunking trades a little time for a large amount of memory at small batch, and stops costing anything as the batch grows.
The memory numbers in my previous comment stand; only the blanket claim about speed was wrong.
Activity
Erland366 commentedon Jul 29, 2025
Nowadays the way we do KD is to take many outputs of giants model and then use it to finetune the smaller model.
This is how Deepseek were doing it for their smaller variant. They did not do any GRPO on the smaller model. Just pure SFT which we already support.
We are not sure of the urgency of the traditional KD as of right now since the above method proven to be very effective already.
Rakshith12-pixel commentedon Aug 7, 2025
Hey @Erland366 can you be bit more clear on "outputs of giants model and then use it to finetune the smaller model."
So suppose I have an ocr task. I have trained a large model (teacher) on a dataset with SFT. I have a smaller student model , untrained on my dataset.
What are the next steps I should employ?
thanks
feat(mlx): off-policy knowledge distillation (GKD)
Add a chunked generalized JSD loss for knowledge distillation (#1310)
danielhanchen commentedon Sep 22, 2026
Knowledge distillation now works under Unsloth. Answering the original question directly first, because the honest answer is mixed: no, this does not let you distil from Deepseek or Kimi K2 on a small card. What it does is make logit-level KD work correctly and efficiently when both models fit. More on why the Behemoth case is different, and what actually solves it, at the bottom.
What works now
GKDTrainerunder Unsloth crashed before this, which is what #1941 reported. Three fixes landed and are live on PyPI inunsloth==2026.9.8andunsloth_zoo==2026.9.7:GKDConfigturnsgradient_checkpointingon by default and older TRL leavesgradient_checkpointing_kwargsasNone, at which point transformers substitutes{"use_reentrant": False}. That overridesuse_gradient_checkpointing="unsloth"and puts the run on the non-reentrant path, which is the only one that compares what each pass saved, so training died withCheckpointError: A different number of tensors was saved during the original forward and recomputation(81 against 79 on a Kaggle 2x T4). The value is now pinned rather than deleted.lm_headupcast on the GKD path.Measured end to end on a free Kaggle 2x T4:
Notebook: Qwen3 (0.6B) Knowledge Distillation.
Two limits found while testing, both documented in the notebook rather than papered over. gemma-4 needs
UNSLOTH_COMPILE_DISABLE=1on T4-class cards, where the gradient-checkpoint recompute disagrees with the forward about tensor rank; it is fine on newer cards. And self-distillation, where the teacher is the student's own frozen base, cannot learn from a fresh adapter:lora_Bis zero-initialised, so the student starts identical to its teacher, and GKD's loss is a divergence and nothing else, so it starts at its global minimum with no gradient. Use that mode to continue from an adapter you already have.Why this does not reach Deepseek or Kimi K2, and what does
The memory that KD wastes comes in two parts, and this work only addresses one of them.
The first is the logits.
GKDTrainermaterialises a full(batch, seq, vocab)tensor for both models, which on a 262144-token vocabulary dominates everything else. A chunked generalized JSD that never builds that tensor is merged in unslothai/unsloth-zoo#1310, though it is not yet wired into the GKD path, so today's runs still pay for it. That is the next piece of work.The second is the teacher's weights, and chunking does nothing for those. A 671B teacher does not fit next to a student on any single machine, so no amount of loss-side efficiency makes the original request work as stated.
The approaches that actually do:
ServerDistillationTrainerandAsyncDistillationTrainer, where the teacher sits behind a vLLM server and returns top-k logprobs over HTTP, never occupying training VRAM. These live undertrl.experimental.*, which Unsloth's trainer discovery does not currently scan, so they run unpatched today.To @Rakshith12-pixel's question, for a trained OCR teacher and an untrained student: the simplest effective route is sequence-level KD, which is just SFT on the teacher's generated outputs and is already supported. Use the logit-level path in the notebook above when you want the teacher's full distribution rather than its argmax, and when both models fit.
Closing this, since logit-level KD works and is released. The chunked-loss wiring and the top-k path will be tracked as their own issues rather than left under a thread this long.
danielhanchen commentedon Sep 22, 2026
Follow-ups opened, so the remaining work is tracked rather than left here: #11554 for wiring the chunked JSD into the GKD path, and #11556 for the teacher that never enters training memory (offline top-k logprobs and served teachers).
Add a knowledge distillation notebook (unslothai#374)
feat(mlx): off-policy knowledge distillation (GKD) (#965)
danielhanchen commentedon Oct 15, 2026
Updating this, because the limitation I described when I closed it no longer holds and the numbers it was missing now exist.
I said the chunked generalized JSD was merged in unslothai/unsloth-zoo#1310 but "not yet wired into the GKD path, so today's runs still pay for it". That wiring landed in #12136 (with #13190 for the GradScaler) and is released in
unsloth==2026.10.5/unsloth_zoo==2026.10.5.So I measured the thing that had only ever been asserted.
UNSLOTH_GKD_CHUNKED=0falls back to TRL's dense loss, which makes a clean one-variable differential: same seed, same data, same models, 10 steps, batch 2, seq 1024, 4-bit student and teacher.6.6x and 2.2x off the peak, with the loss agreeing to 2.3e-05 and 1.7e-04 respectively, which is accumulation order rather than a behaviour change. It is not slower either: on gemma-4 the chunked arm finished in 11.1 s against 14.8 s dense, since it never allocates or writes the big tensor.
Two honest qualifications. The reduction is not uniform: gemma-4 gains less in relative terms because its peak carries more than logits, so the win depends on how much of your footprint was the
(batch, seq, vocab)tensor. And this still does nothing for the teacher's weights, which was the original ask here. A Deepseek- or Kimi-K2-class teacher does not fit beside a student regardless of how cheap the loss becomes; that needs an offline top-k or served teacher, tracked in #11556.Worth it for anyone arriving at this thread: logit-level KD on a free Kaggle 2x T4 is now considerably cheaper than the numbers in the notebook, which were measured before the chunked path existed. I am refreshing those.
danielhanchen commentedon Oct 15, 2026
Correcting one line in my comment above. I wrote "it is not slower either" and generalised that from a single configuration, which was wrong.
Re-measuring at the settings the notebook actually ships (batch 1, gradient accumulation 4, 30 steps, seq 1024, Qwen3-0.6B from Qwen3-1.7B, both 4-bit):
UNSLOTH_GKD_CHUNKED=0)So 3.3x off the peak, same loss to 2.2e-05, but about 30% slower at batch 1. The chunk loop's per-chunk overhead is fixed while the memory it saves scales with batch and sequence, which is why the earlier batch-2 runs showed no slowdown and gemma-4 at batch 2 was actually faster. The honest summary is that chunking trades a little time for a large amount of memory at small batch, and stops costing anything as the batch grows.
The memory numbers in my previous comment stand; only the blanket claim about speed was wrong.