Repository navigation
Keep a user's UNSLOTH_RETURN_LOGITS=1 after for_inference() on FastModel - #13406
Conversation
… survives train()
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 |
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
Fixes #3071 (remaining path after #13311).
Problem
#13311 made
for_training()put back whateverfor_inference()replaced when it forcedUNSLOTH_RETURN_LOGITS=1, instead of forcing"0". One path still lost the user's value: with aFastModel/FastVisionModelmodel,for_inference()(also called by everymodel.generate()) followed by the user settingUNSLOTH_RETURN_LOGITS=1, thentrainer.train(). The env var is already"1"at that point, so the user's write is invisible, andfor_training()restores the saved"0":Fix
FastBaseModel.for_inference()no longer writesUNSLOTH_RETURN_LOGITS, which is whatFastLanguageModel.for_inference()(theFastLlamaModelpath) has always done. Nothing needs the forced value: every reader of the flag (zoo compiled forwards, the fused loss rewrite,llama.py/mistral.py) only consults it on a forward withlabels, generation / GRPO / DPO forwards have no labels, and evaluation already forces"1"perprediction_stepand restores it afterwards. With nothing forced there is nothing to undo, so:for_training()only defaults an unset flag to"0"(os.environ.setdefault), never overwrites it.for_inference()calls and the_UNSLOTH_RETURN_LOGITS_BEFORE_INFERENCEmarker are removed.FastLanguageModel.for_training()no longer restores that marker either (added by FastLanguageModel.for_training: undo the UNSLOTH_RETURN_LOGITS=1 that generate() forces #13392 for FastModel-backed models generated throughFastBaseModel.for_inference()); with nothing forced it had nothing left to undo.__init__no longer sets"1"whencompute_metrics/preprocess_logits_for_metricsis passed (that leaked into training and the next trainer); eval still gets logits fromprediction_step.Net: 51 lines added, 125 removed.
Evidence
Repro (tiny Gemma3 and Qwen3, LoRA SFT with a
compute_lossthat readsoutputs.logits), base = current main vs this branch, bothFastLanguageModelandFastModelloaders:for_inference()+ generate, set flag, train again (Gemma3, both loaders)Logits are empty1aftertrain()/ between twotrain()/ withcompute_metricscompute_metricswithout flag)EmptyLogitsin training, env0Every other cell (16 scenario x model cells per loader) gives the same result on both sides.
Behaviour note: a forward with
labelsafterFastModel.for_inference()now returnsEmptyLogitsfor.logits(the loss is unchanged), as it already did afterFastLanguageModel.for_inference()on the same model. SettingUNSLOTH_RETURN_LOGITS=1returns the logits as before.Unit tests:
tests/test_return_logits_survives_for_training.pyrewritten for the new contract (fails on main, passes here).