Skip to content

Keep a user's UNSLOTH_RETURN_LOGITS=1 after for_inference() on FastModel - #13406

Merged
danielhanchen merged 4 commits into
mainfrom
stop-for-inference-forcing-return-logits
Oct 12, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
stop-for-inference-forcing-return-logits

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Fixes #3071 (remaining path after #13311).

Problem

#13311 made for_training() put back whatever for_inference() replaced when it forced UNSLOTH_RETURN_LOGITS=1, instead of forcing "0". One path still lost the user's value: with a FastModel / FastVisionModel model, for_inference() (also called by every model.generate()) followed by the user setting UNSLOTH_RETURN_LOGITS=1, then trainer.train(). The env var is already "1" at that point, so the user's write is invisible, and for_training() restores the saved "0":

trainer.train()                       # train once
FastModel.for_inference(model); model.generate(...)
os.environ["UNSLOTH_RETURN_LOGITS"] = "1"
trainer.train()                       # NotImplementedError: Unsloth: Logits are empty from 2024.11 onwards ...

Fix

FastBaseModel.for_inference() no longer writes UNSLOTH_RETURN_LOGITS, which is what FastLanguageModel.for_inference() (the FastLlamaModel path) 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 with labels, generation / GRPO / DPO forwards have no labels, and evaluation already forces "1" per prediction_step and 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.
  • The env snapshot / restore around the trainer's for_inference() calls and the _UNSLOTH_RETURN_LOGITS_BEFORE_INFERENCE marker 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 through FastBaseModel.for_inference()); with nothing forced it had nothing left to undo.
  • The generated RL trainer __init__ no longer sets "1" when compute_metrics / preprocess_logits_for_metrics is passed (that leaked into training and the next trainer); eval still gets logits from prediction_step.

Net: 51 lines added, 125 removed.

Evidence

Repro (tiny Gemma3 and Qwen3, LoRA SFT with a compute_loss that reads outputs.logits), base = current main vs this branch, both FastLanguageModel and FastModel loaders:

Scenario main this PR
train, for_inference() + generate, set flag, train again (Gemma3, both loaders) FAIL, Logits are empty PASS, logits every step, env 1 after
same on Qwen3 PASS PASS
flag set before import / before train() / between two train() / with compute_metrics PASS PASS
flag never set (default, generate first, compute_metrics without flag) fused CE, EmptyLogits in training, env 0 identical

Every other cell (16 scenario x model cells per loader) gives the same result on both sides.

Behaviour note: a forward with labels after FastModel.for_inference() now returns EmptyLogits for .logits (the loss is unchanged), as it already did after FastLanguageModel.for_inference() on the same model. Setting UNSLOTH_RETURN_LOGITS=1 returns the logits as before.

Unit tests: tests/test_return_logits_survives_for_training.py rewritten for the new contract (fails on main, passes here).

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

chatgpt-codex-connector Bot commented Oct 12, 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 ✅ Completed 2026-10-12T15:55:05.588444Z 6d94089 Manual request
ℹ️ 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 12, 2026
@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. Chef's kiss.

Reviewed commit: 5d90509438

ℹ️ 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

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. Keep them coming!

Reviewed commit: 6d94089ef9

ℹ️ 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".

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.

[Bug] os.environ["UNSLOTH_RETURN_LOGITS"] = "1" becomes unset to "0" once I start to train

1 participant