Hugging Face released TRL v1.19.0 on 25 November 2026. The biggest addition is multi-teacher distillation: DistillationTrainer can now train one small student model from several expert teachers, with each training example choosing which teacher grades it. The same release lets you distill agents against stateful environments, makes packed SFT up to 2x faster with SDPA, and fixes fine-tuned models that never stopped generating.
This guide explains on-policy distillation briefly, then shows how to set up a multi-teacher run, an agent distillation run, and the SFT changes, using the APIs documented in the Distillation Trainer docs.
Install
pip install -U "trl==1.19.0"
# Inkling 2 Mini support and the environment features need recent transformers
pip install -U "transformers>=5.24.0"
The release raises a few minimum versions too: peft>=0.18.0, accelerate>=1.12.0, and vLLM support now covers vllm>=0.23.0,<0.35.0.
On-policy distillation in one paragraph
Classic knowledge distillation trains a small student model to copy a big teacher’s outputs on a fixed dataset. On-policy distillation, as used by DistillationTrainer, works differently. The student generates its own completions, and the teacher scores every token of those completions with its full next-token distribution. The loss is a generalized Jensen-Shannon divergence controlled by beta: 0.0 gives forward KL, 1.0 (the default) reverse KL, and 0.5 the standard JSD. Because the student learns from its own mistakes instead of only from perfect teacher text, this tends to transfer better at inference time.
Multi-teacher distillation (MOPD)
Suppose you have two strong specialists: a math model and a code model. You want one small model that does both. In v1.19.0, you can pass a dict of teachers instead of a single teacher, and add a teacher_id column to your dataset. Each row’s completion is scored only by the teacher it names. The release notes describe this as the fusion stage of MOPD.
from datasets import Dataset
from trl import DistillationConfig, DistillationTrainer
dataset = Dataset.from_dict(
{
"prompt": [
[{"role": "user", "content": "What is the derivative of x^2?"}],
[{"role": "user", "content": "Write a function that reverses a string."}],
],
"teacher_id": ["math", "code"],
}
)
trainer = DistillationTrainer(
model="Qwen/Qwen2.5-0.5B-Instruct",
teacher_model={
"math": "Qwen/Qwen2.5-Math-1.5B-Instruct",
"code": "Qwen/Qwen2.5-Coder-1.5B-Instruct",
},
args=DistillationConfig(
output_dir="mopd-student",
teacher_model_init_kwargs={"code": {"dtype": "bfloat16"}},
),
train_dataset=dataset,
)
trainer.train()
This example is from the official docs. A few rules to keep in mind:
- Every teacher must share the student’s vocabulary. In practice that means the same model family and tokenizer, which is why all three models here are Qwen2.5.
teacher_model_init_kwargscan be set per teacher, as shown for the code teacher.- Each teacher is prepared like a single teacher, so DeepSpeed ZeRO-3 shards (and can offload) each of them. FSDP is not supported with a teacher mapping.
teacher_idis one of the columnsDistillationConfigkeeps by default, along withprompt,image,imagesandenvironment.
Building the dataset
The two-row dataset above is only an illustration. In a real project you’d mix prompts from each domain and tag them:
from datasets import concatenate_datasets, load_dataset
math = load_dataset("your-org/math-prompts", split="train").map(lambda _: {"teacher_id": "math"})
code = load_dataset("your-org/code-prompts", split="train").map(lambda _: {"teacher_id": "code"})
dataset = concatenate_datasets([math, code]).shuffle(seed=42)
Make sure both sets use the same prompt format (a list of chat messages) before concatenating.
Distilling agents with environment_factory
DistillationTrainer also accepts environment_factory, with the same contract as GRPOTrainer. The trainer creates one environment instance per rollout and exposes the environment’s public methods as tools. The student runs a multi-turn episode, the teacher scores every token the student generated with the whole episode (including tool results) as context, and tool-result tokens are masked out of the loss.
Here’s the counter example from the docs:
from datasets import Dataset
from trl import DistillationConfig, DistillationTrainer
targets = [i % 6 + 1 for i in range(64)]
dataset = Dataset.from_dict(
{
"prompt": [[{"role": "user", "content": f"Increment the counter to {t}."}] for t in targets],
"target": targets,
}
)
class IncrementEnv:
def reset(self, target, **kwargs) -> str | None:
self.counter = 0
self.target = target
return None
def get_reward(self) -> float: # optional: logged only, never used in the loss
return float(self.counter == self.target)
def increment(self, step: int) -> int:
"""
Increment the internal counter.
Args:
step: Value to add to the counter.
Returns:
The updated counter value.
"""
self.counter += step
return self.counter
trainer = DistillationTrainer(
model="Qwen/Qwen3-0.6B",
teacher_model="Qwen/Qwen3-4B",
args=DistillationConfig(max_tool_calling_iterations=4, chat_template_kwargs={"enable_thinking": False}),
train_dataset=dataset,
environment_factory=IncrementEnv,
)
trainer.train()
Some practical notes from the docs:
reset()is required. It receives the row’s other columns (heretarget) as keyword arguments.get_reward()is optional and is only logged asrewards/IncrementEnv/mean. Distillation has no reward signal, but the metric is a handy way to watch task success while the loss goes down.max_completion_lengthis the budget for the whole episode, tool results included. Size it for the episode, not for one reply.- Async tools and async environment methods aren’t supported yet.
environment_factoryrequirestransformers>=5.2.0.
2x faster packed SFT with SDPA
If you fine-tune with SFTTrainer and packing=True, this fix may make your runs noticeably faster. The default BFD packing used to flatten the batch into a single row, so SDPA attention ran across the whole batch. That’s quadratic, and on Trainium it ran out of memory. Packed examples now stay as separate rows, with position_ids restarting at each sequence. (padding_free=True still flattens.)
The release notes report seconds per step with packing_strategy="bfd" going from 2.06 to 1.02 on an H100 with Qwen3-1.7B and SDPA. With Flash Attention 2 the speed is unchanged, at 0.828 vs 0.832. A Trainium2 setup with Qwen3-0.6B that previously crashed now runs at 3.2 s/step.
You don’t need to change any code:
from trl import SFTConfig, SFTTrainer
trainer = SFTTrainer(
model="Qwen/Qwen3-1.7B",
train_dataset=dataset,
args=SFTConfig(output_dir="sft-out", packing=True), # BFD packing by default
)
Fine-tuned models now stop at the end of their turn
Some base checkpoints, including Qwen base, Gemma 1/2 instruct and Qwen3.5, don’t list their chat template’s end-of-turn token as a stop token. Models fine-tuned from them kept generating until max_new_tokens. SFTTrainer now adds that token to generation_config.eos_token_id when it’s missing, and changes nothing otherwise. If you added a manual workaround for runaway generations, you can probably remove it after retraining.
Other changes worth knowing
- AsyncGRPO packs micro-batches with array operations. On Qwen3-1.7B math rollouts (4×B300), planning and packing went from 18 s to 1 s per pass, a 28% gain in trained tokens per second with bit-identical batches.
- Accuracy rewards are graded in a separate process, so a pathological answer can’t hang training.
assistant_only_loss=Truenow works with more chat templates, including Llama 3.1/3.2, Phi-4, Gemma 4 and several DeepSeek models.- Breaking:
prefixis now a required argument ofparse_response. - vLLM 0.34: documented
vllm servecommands now pass--gpu-memory-utilization 0.9, because 0.34’s new default of 1.0 runs out of memory on the first weight sync.
Takeaway
TRL 1.19.0 makes it practical to merge several specialist models into one small student with a single dict and a teacher_id column, and to distill tool-using agents using the same environment classes you’d write for GRPO. Upgrade, re-run packed SFT jobs to get the free speed-up, and read the full release notes for the complete list of template and vLLM changes.
