Skip to content

Patch TRL's served-teacher, GOLD and SDFT distillation trainers - #13580

Merged
danielhanchen merged 6 commits into
mainfrom
distill-trl-experimental-trainers
Oct 13, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
distill-trl-experimental-trainers

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Part of #11556 (the served-teacher half).

TRL's teacher-free and cross-tokenizer distillation trainers never lived in trl.trainer, so Unsloth's trainer walk never patched them:

trl.experimental.<pkg> Trainer Patched from TRL
server_distillation ServerDistillationTrainer (teacher behind a vLLM server, top-k logprobs over HTTP) 1.10
gold GOLDTrainer (cross-tokenizer distillation) 0.29
sdft SDFTTrainer (self-distillation fine-tuning) 1.0

The user-visible part: on a T4 (Colab / Kaggle free tier), with Unsloth imported and bf16 / fp16 left unset, all three crash at config creation on main with ValueError: Your setup doesn't support bf16/gpu, because their configs keep TRL's bf16=True default. With this PR they get Unsloth's config and precision handling, like every other patched trainer, and train in fp16.

On main, whether these got any of Unsloth's trainer work depended on import order. ServerDistillationTrainer subclasses DistillationTrainer and GOLDTrainer subclasses SFTTrainer, so imported after Unsloth they inherited UnslothDistillationTrainer / UnslothSFTTrainer, and imported before Unsloth they stayed plain TRL. Their configs were never patched in either order, so they kept TRL's defaults (bf16=True, optim="adamw_torch_fused", no Unsloth fields), and SDFTTrainer was plain TRL always.

Changes

  • Add gold, sdft and server_distillation to _TRL_EXPERIMENTAL_TRAINERS, the allow-list from Patch the TRL trainers that moved to trl.experimental (ORPO, CPO, Online DPO, GKD, ...) #12097. They are imported before the trl.trainer walk like gkd, so each keeps TRL's own base whatever the import order and gets its own Unsloth*Trainer / Unsloth*Config.
  • Two config defaults are kept at TRL's values so the generic Unsloth config cannot break these trainers:
    • GOLD keeps num_generations=1 and is skipped by the GRPO multiple-of-num_generations batch rewrite. Its num_generations is samples per prompt, and TRL already checks generation_batch_size * num_generations == batch * grad accum; without this, the default of 8 (or a user's num_generations=8 with batch 4 x grad accum 2) silently raised the batch size.
    • SDFT keeps per_device_eval_batch_size=8. SDFT requires the eval batch to divide by num_generations (8), so Unsloth's default of 4 made SDFTConfig(eval_strategy="steps") raise.
  • async_distillation is left out on purpose: AsyncDistillationTrainer loads the student itself from a model name, so an Unsloth model never reaches it.
  • Two generator fixes these trainers needed. Every trainer Unsloth already generated is byte-identical before and after (checked below):
    • A string default holding a newline or quote is now written with repr. SDFTConfig.teacher_prompt_template = "{prompt}\n\n{privileged_context}" otherwise ended the '...' literal early and generation failed.
    • Function-level relative imports are made absolute against the trainer's package. ServerDistillationTrainer.__init__ does from ...generation.vllm_client import VLLMClient, which raised ImportError: attempted relative import with no known parent package from the generated module.

Not in this PR: the offline top-k teacher logprob dataset path from #11556, which is a new feature rather than a patching gap.

Evidence

Generated trainers, base vs head, fresh compile cache per arm (TRL 0.22.2, 0.24.0, 0.27.0, 0.29.0, 1.0.0, 1.5.0, 1.10.0, 1.14.0, 1.15.0): every file generated on base is byte-identical on head. The only new files are UnslothGOLDTrainer.py (0.29+), UnslothSDFTTrainer.py (1.0+) and UnslothServerDistillationTrainer.py (1.10+). No Could not build / Could not patch warning on either side, and import unsloth prints nothing new.

Training, 3 steps, Unsloth LoRA student unsloth/Qwen3-0.6B, teacher unsloth/Qwen3-1.7B (server teacher: a stubbed vLLM client), TRL 1.15.0, main (8149f57) vs this PR (cb2bd53) vs plain TRL + PEFT, fresh process and compile cache per run.

Kaggle T4 (no bf16), precision flags left unset, as in a notebook:

Trainer main + Unsloth This PR + Unsloth Plain TRL + PEFT
ServerDistillation ValueError: Your setup doesn't support bf16/gpu -0.0195, -0.2132, -0.1631 -0.0154, -0.1711, -0.1409
GOLD ValueError: Your setup doesn't support bf16/gpu 0.2078, 0.1334, 0.1221 0.2129, 0.1365, 0.1197
SDFT ValueError: Your setup doesn't support bf16/gpu 0.1372, 0.0735, 0.1449 0.1282, 0.0693, 0.0856

Same T4 with fp16=True passed explicitly: main and this PR give bit-identical losses and grad norms for all three trainers. The same holds on transformers 4.57.6 + TRL 1.15 (Kaggle T4) once warmup_ratio=0 is set; left unset there, steps 2 and 3 differ only through Unsloth's usual warmup_ratio=0.1 default.

Colab L4 (bf16), both with precision unset and set: main and this PR are bit-identical for all three trainers (6 of 6 runs), so nothing moves for bf16 GPUs.

Trainer (L4, unset) main + Unsloth This PR + Unsloth Plain TRL + PEFT
ServerDistillation -0.0068, 0.1735, 0.0458 -0.0068, 0.1735, 0.0458 -0.0055, 0.1648, -0.0524
GOLD 0.2087, 0.1324, 0.1198 0.2087, 0.1324, 0.1198 0.2061, 0.1336, 0.1246
SDFT 0.1583, 0.1243, 0.1383 0.1583, 0.1243, 0.1383 0.1565, 0.1246, 0.1367

With fast_inference=True (B200), GOLD and SDFT now set use_vllm and generate through Unsloth's own vLLM engine (trainer.vllm_generation.llm is model.vllm_engine), with no second engine.

B200, explicit bf16: ServerDistillation and SDFT bit-identical to main; GOLD 0.2094, 0.1325, 0.1237 vs main 0.2077, 0.1324, 0.1229 vs plain TRL 0.2017, 0.1329, 0.1223 (on main GOLD inherited UnslothSFTTrainer when imported after Unsloth; it now keeps TRL's SFTTrainer under its own wrapper).

Tests

tests/version_compat/test_trl_experimental_trainers_patched.py (already in version-compat CI) now also covers gold, sdft and server_distillation, asserts that generated trainer modules contain no relative imports, that GOLD keeps TRL's num_generations default (read from the generated constructor) and a valid batch size, that SDFTConfig(eval_strategy="steps") still builds, and that ServerDistillationTrainer / GOLDTrainer keep TRL's own DistillationTrainer / SFTTrainer base whether TRL or Unsloth is imported first.

TRL main This PR
0.24.0 7 skipped (nothing in trl.experimental) 7 skipped
0.29.0 4 failed, 3 passed, 2 skipped 7 passed, 2 skipped
1.0.0 4 failed, 4 passed, 1 skipped 8 passed, 1 skipped
1.10.0 5 failed, 4 passed 9 passed
1.15.0 5 failed, 4 passed 9 passed

With the GOLD batch guard and the SDFT eval default removed, exactly test_gold_keeps_a_valid_batch_size and test_sdft_config_with_evaluation_still_builds fail; without the num_generations pop, test_gold_keeps_trls_num_generations_default fails with assert 8 == 1.

@danielhanchen danielhanchen self-assigned this Oct 13, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-13T11:37:08.180551Z 1138e72 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6b1ddc13c3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +80 to +82
"num_generations": [
getattr(c, "__dataclass_fields__", {}).get("num_generations", None) and c.__dataclass_fields__["num_generations"].default
for c in (config, config.__mro__[1])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Check the rewritten constructor default directly

This records __dataclass_fields__["num_generations"].default, but UnslothGOLDConfig does not redeclare that dataclass field; it only supplies a generated custom __init__. Consequently, both the patched class and its parent report GOLD's inherited field default of 1 even if the generated constructor is accidentally rewritten to num_generations=8. The test therefore still passes if the new replacements.pop("num_generations") protection is removed; inspect the constructor signature's parameter default (or instantiate the generated config) instead.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 388aa36: the test now reads the default from the generated init signature, and fails with assert 8 == 1 when the gold_trainer pop is removed.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 388aa36d00

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/models/rl.py Outdated
Comment on lines +3084 to +3086
# GOLD's num_generations is samples per prompt (default 1), not a GRPO group: 8 would also lift its batch size to 8.
if trainer_file == "gold_trainer":
replacements.pop("num_generations")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Skip GOLD's generic batch-size rewrite

When a GOLD user explicitly sets num_generations to a value that does not divide per_device_train_batch_size (for example, 8 with the generated default batch size of 4), removing only the default replacement is insufficient: the generic check below still silently changes per_device_train_batch_size to num_generations. GOLD uses this value as samples per prompt rather than a GRPO grouping constraint, so this unexpectedly changes optimization behavior and can increase memory use enough to OOM; GOLD should also be excluded from that divisibility rewrite.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in cb2bd53: GOLD is excluded from the multiple-of-num_generations batch rewrite (TRL already checks generation_batch_size * num_generations == batch * grad accum). num_generations=8, batch 4, GA 2 now keeps batch 4; test_gold_keeps_a_valid_batch_size fails without the guard.

Comment thread unsloth/models/rl.py
"orpo",
"ppo",
"prm",
"sdft",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve a valid SDFT evaluation batch default

Adding SDFT to the generic patch pipeline changes per_device_eval_batch_size from TRL's default of 8 to 4 while retaining num_generations=8. On a single-process run with evaluation enabled and num_generations_eval unset, SDFTConfig.__post_init__ requires the global evaluation batch size to be divisible by the effective eight generations, so an otherwise default SDFTConfig(eval_strategy="steps") now raises during construction. Give SDFT a compatible evaluation default or avoid overriding this field for SDFT.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in cb2bd53: SDFT keeps TRL's per_device_eval_batch_size of 8, so SDFTConfig(eval_strategy="steps") builds again. test_sdft_config_with_evaluation_still_builds fails without the guard.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: cb2bd53688

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit a43cf77 into main Oct 13, 2026
@danielhanchen
danielhanchen deleted the distill-trl-experimental-trainers branch October 13, 2026 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant