[Feature] Add DistillationLoss for token-level LLM knowledge distillation - #4038
Open
theap06 wants to merge 1 commit into
Open
[Feature] Add DistillationLoss for token-level LLM knowledge distillation#4038theap06 wants to merge 1 commit into
theap06 wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4038
Note: Links to docs will display an error until the docs builds have been completed.
|
…tion Adds DistillationLoss, DistillationLossOutput and the per-token reverse_kl_token_estimate k3 estimator to torchrl.objectives.llm. The loss distills a student policy toward a frozen teacher by minimizing KL(student || teacher) (on-policy, default) or KL(teacher || student) (off-policy corpora), estimated from per-token log-probs on shared tokens with the same k3 approximation used by the GRPO and SFT KL regularizers. Teacher log-probs are consumed from the tensordict as written by RetrieveLogProb (or an offline scoring pass) and detached; student log-probs are computed by the wrapped model. Supports assistant-only masking with tokenizer fallback, sequence length normalization and mean/sum/none reductions. Closes pytorch#3921
theap06
force-pushed
the
feature/llm-distillation-loss
branch
from
July 21, 2026 10:01
6839cea to
eb849fa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Implements
DistillationLoss, the on-policy KL knowledge-distillation objective for LLM policies proposed in #3921, undertorchrl/objectives/llm/.A student policy is distilled toward a (typically stronger, frozen) teacher by minimizing a KL divergence estimated from the per-token log-probabilities the two models assign to the same tokens. It mirrors
SFTLossand reuses the same teacher log-prob plumbing asGRPOLoss/SFTLoss:actor_networkon the input history;RetrieveLogProb(teacher, log_probs_full_key=("teacher_log_probs", "full"))or an offline scoring pass, and are detached inside the loss;reverse_kl_token_estimate(the sameexpm1(d) - dapproximation the GRPO/SFT KL regularizers inline);("masks", ...)when present, tokenizer fallback otherwise; masked positions are zeroed on both log-prob tensors before the estimator so extreme masked values cannot poison gradients.Both directions are supported via
kl_direction:"reverse"(default):KL(student || teacher)for on-policy distillation on student-generated tokens (mode-seeking, GKD-style);"forward":KL(teacher || student)for off-policy distillation on teacher/corpus data (mass-covering).Plus
reduction(mean/sum/none),normalize_by_seq_length,assistant_only=Falsefor plain-corpus distillation, andset_keys()support for custom key layouts.Example
The class docstring contains a complete runnable example (verified end-to-end against a randomly initialized OPT pair).
Tests
TestDistillationintest/llm/test_llm_objectives.py, following theTestSFTpattern (tiny OPT models +RetrieveLogProb):mean/sumconsistent withnone) and length normalization;set_keys(); missing-teacherKeyError; invalidkl_direction/reductionvalidation.Docs entries added to
llms.rstandllms_objectives.rst.Notes
GRPOLoss/SFTLossincluded), so this follows the existing pattern.Closes #3921