Repository navigation
Patch TRL's served-teacher, GOLD and SDFT distillation trainers - #13580
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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".
| "num_generations": [ | ||
| getattr(c, "__dataclass_fields__", {}).get("num_generations", None) and c.__dataclass_fields__["num_generations"].default | ||
| for c in (config, config.__mro__[1]) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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".
| # 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") |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| "orpo", | ||
| "ppo", | ||
| "prm", | ||
| "sdft", |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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>server_distillationServerDistillationTrainer(teacher behind a vLLM server, top-k logprobs over HTTP)goldGOLDTrainer(cross-tokenizer distillation)sdftSDFTTrainer(self-distillation fine-tuning)The user-visible part: on a T4 (Colab / Kaggle free tier), with Unsloth imported and
bf16/fp16left unset, all three crash at config creation on main withValueError: Your setup doesn't support bf16/gpu, because their configs keep TRL'sbf16=Truedefault. 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.
ServerDistillationTrainersubclassesDistillationTrainerandGOLDTrainersubclassesSFTTrainer, so imported after Unsloth they inheritedUnslothDistillationTrainer/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), andSDFTTrainerwas plain TRL always.Changes
gold,sdftandserver_distillationto_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 thetrl.trainerwalk likegkd, so each keeps TRL's own base whatever the import order and gets its ownUnsloth*Trainer/Unsloth*Config.num_generations=1and is skipped by the GRPO multiple-of-num_generationsbatch rewrite. Itsnum_generationsis samples per prompt, and TRL already checksgeneration_batch_size * num_generations == batch * grad accum; without this, the default of 8 (or a user'snum_generations=8with batch 4 x grad accum 2) silently raised the batch size.per_device_eval_batch_size=8. SDFT requires the eval batch to divide bynum_generations(8), so Unsloth's default of 4 madeSDFTConfig(eval_strategy="steps")raise.async_distillationis left out on purpose:AsyncDistillationTrainerloads the student itself from a model name, so an Unsloth model never reaches it.repr.SDFTConfig.teacher_prompt_template = "{prompt}\n\n{privileged_context}"otherwise ended the'...'literal early and generation failed.ServerDistillationTrainer.__init__doesfrom ...generation.vllm_client import VLLMClient, which raisedImportError: attempted relative import with no known parent packagefrom 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+) andUnslothServerDistillationTrainer.py(1.10+). NoCould not build/Could not patchwarning on either side, andimport unslothprints nothing new.Training, 3 steps, Unsloth LoRA student
unsloth/Qwen3-0.6B, teacherunsloth/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:
ValueError: Your setup doesn't support bf16/gpuValueError: Your setup doesn't support bf16/gpuValueError: Your setup doesn't support bf16/gpuSame T4 with
fp16=Truepassed 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) oncewarmup_ratio=0is set; left unset there, steps 2 and 3 differ only through Unsloth's usualwarmup_ratio=0.1default.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.
With
fast_inference=True(B200), GOLD and SDFT now setuse_vllmand 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
UnslothSFTTrainerwhen imported after Unsloth; it now keeps TRL'sSFTTrainerunder its own wrapper).Tests
tests/version_compat/test_trl_experimental_trainers_patched.py(already in version-compat CI) now also coversgold,sdftandserver_distillation, asserts that generated trainer modules contain no relative imports, that GOLD keeps TRL'snum_generationsdefault (read from the generated constructor) and a valid batch size, thatSDFTConfig(eval_strategy="steps")still builds, and thatServerDistillationTrainer/GOLDTrainerkeep TRL's ownDistillationTrainer/SFTTrainerbase whether TRL or Unsloth is imported first.trl.experimental)With the GOLD batch guard and the SDFT eval default removed, exactly
test_gold_keeps_a_valid_batch_sizeandtest_sdft_config_with_evaluation_still_buildsfail; without thenum_generationspop,test_gold_keeps_trls_num_generations_defaultfails withassert 8 == 1.