Skip to content
mentoor

Topic 2 of 5 · 5 min read

Logit versus output distillation

Goal: Choose the distillation method that fits your access to the teacher and describe its loss or data recipe.

Your access to the teacher decides the method: if you can read its token probabilities, match them with a KL loss; if you only get text from an API, fine-tune the student on that text.

Check yourself · 1 of 3

You can only call a hosted model through an API that returns text. Which distillation method is available?

Choose one answer

Why it matters

Picking the wrong method wastes a training run. Logit distillation needs the teacher's full output distribution and, in practice, the teacher running on your own hardware. Hosted APIs usually return text, and sometimes a few top log-probabilities. That cuts you off from the first method and leaves the second.

Logit distillation (white-box)

The teacher and student both produce a probability distribution over the vocabulary at each position. The loss pulls the student's distribution toward the teacher's. The measure is KL divergence, a number for how different two distributions are. A temperature T above 1 softens both, so smaller probabilities still matter. Both models must share a tokenizer, or you must map between them.

import torch.nn.functional as F

def distill_loss(student_logits, teacher_logits, labels, T=2.0, alpha=0.5):
    soft = F.kl_div(
        F.log_softmax(student_logits / T, dim=-1),
        F.softmax(teacher_logits / T, dim=-1),
        reduction="batchmean",
    ) * (T * T)
    hard = F.cross_entropy(
        student_logits.view(-1, student_logits.size(-1)), labels.view(-1)
    )
    return alpha * soft + (1 - alpha) * hard

The T * T factor keeps the gradient size steady as T changes, as in Hinton et al. The hard term is ordinary cross-entropy on the true labels.

Output distillation (black-box)

You send prompts to the teacher, store the answers, and run supervised fine-tuning on the pairs. There is no KL term. The student sees only the text the teacher chose, not the alternatives it considered.

  1. Write prompts that cover the real task, including awkward cases.
  2. Sample the teacher, at low temperature for a stable format.
  3. Filter: drop malformed, refused or failing outputs, ideally with a check you can run, such as a JSON schema.
  4. Fine-tune the student with a library such as TRL or Axolotl.

On-policy variants

Fixed teacher text can differ from what the student would generate itself. On-policy methods, such as generalized knowledge distillation (GKD), let the student write its own answer and the teacher then scores it, which narrows that gap. The TRL GKDTrainer supports this and needs teacher logits.

Before and after

Weak: "We will use KL distillation from the API model." The API gives no full distribution, so the plan cannot run. Better: "We will sample the API model on 30,000 prompts, filter with the schema check, and fine-tune the student on what passes."

What goes wrong

  • Tokenizer mismatch makes teacher and student distributions impossible to compare directly.
  • Unfiltered outputs teach the student the teacher's bad habits and formatting slips.
  • Only easy prompts in the set give a student that fails on hard ones.

Examples

Choosing by access

Team A hosts an open-weight 70B teacher and a 3B student from the same family. They have logits and a shared tokenizer, so they use KL distillation. Team B only calls a hosted API. They have no distribution to match, so they sample 30,000 answers, filter them, and fine-tune.

A filter that earns its keep

import json

def keep(sample):
    try:
        out = json.loads(sample["teacher_output"])
    except ValueError:
        return False
    return set(out) == {"category", "order_id"}

Only outputs that parse and have exactly the two expected keys enter the training set. The check is cheap and removes the malformed teacher answers before they become labels.

Key points

  • If you can read the teacher's logits, match them with a KL divergence term; if you only get text, fine-tune on filtered outputs.
  • A temperature above 1 softens both distributions, and the loss is scaled by T squared to keep gradients steady.
  • Logit distillation needs a shared tokenizer, or an explicit mapping between the two vocabularies.
  • Black-box output distillation is ordinary supervised fine-tuning, so data quality and filtering decide the result.
  • On-policy methods such as GKD train on the student's own samples, which reduces the mismatch with fixed teacher text.

Common mistakes

  • Planning logit distillation against an API that returns only text.
  • Mixing teacher and student with different tokenizers and comparing their probabilities position by position.
  • Fine-tuning on every teacher output without a validity filter.
  • Dropping the T squared scaling and then wondering why the soft loss shrinks as temperature rises.

Check yourself · 2 of 3

In a KL distillation loss, why multiply the soft term by T squared?

Choose one answer

Check yourself · 3 of 3

Before fine-tuning on 30,000 teacher outputs that must be valid JSON, what is the best next step?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic