Skip to content

Knowledge Distillation Feature in Unsloth, Utilizing Unsloths Incredible Memory Efficiency #3008

Closed

Description

@kkailaasa

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?

Activity

  1. added
    feature request
    on Jul 20, 2025
  2. Erland366 commented on Jul 29, 2025

    @Erland366
    Collaborator

    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.

  3. Rakshith12-pixel commented on Aug 7, 2025

    @Rakshith12-pixel

    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

  4. danielhanchen commented on Sep 22, 2026

    @danielhanchen
    Member

    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:

    Measured end to end on a free Kaggle 2x T4:

    student teacher loss peak
    Qwen3-0.6B Qwen3-1.7B 0.413 10.9 GB
    gemma-4 E2B gemma-4 E4B 0.079 14.3 GB

    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=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:

    1. 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.
    2. 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.
    3. 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.

  5. danielhanchen commented on Sep 22, 2026

    @danielhanchen
    Member

    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).

  6. danielhanchen commented on Oct 15, 2026

    @danielhanchen
    Member

    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.

  7. danielhanchen commented on Oct 15, 2026

    @danielhanchen
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions