Skip to content

FastLanguageModel.for_training: undo the UNSLOTH_RETURN_LOGITS=1 that generate() forces - #13392

Merged
danielhanchen merged 2 commits into
mainfrom
fix-flm-return-logits
Oct 12, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix-flm-return-logits

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #13311. FastLanguageModel loads Qwen3.5, Gemma 3, Gemma 4 and other models through FastModel, so their generate() runs FastBaseModel.for_inference(), which sets UNSLOTH_RETURN_LOGITS=1 (saving the previous value in _UNSLOTH_RETURN_LOGITS_BEFORE_INFERENCE). FastModel.for_training() restores it, but FastLanguageModel.for_training() (the Llama-path method) never touched it, so the common notebook flow of trying the model and then training left it at 1:

  • the trainer keeps full logits instead of the fused cross-entropy path, and
  • packing and padding-free are turned off: Unsloth: packing=True ignored (UNSLOTH_RETURN_LOGITS=1).

Change

FastLlamaModel.for_training() now undoes only the value for_inference() forced, exactly as FastBaseModel.for_training() does since #13311: it pops _UNSLOTH_RETURN_LOGITS_BEFORE_INFERENCE and restores it when present. A UNSLOTH_RETURN_LOGITS=1 the user set themselves is left alone, and models loaded on the Llama path (whose generate() never sets the flag) are unaffected.

Before / after

Tiny Gemma 3 text model, FastLanguageModel.from_pretrained, LoRA, for_inference, generate(), FastLanguageModel.for_training, then SFTTrainer(..., packing=True) (torch 2.14, transformers 5.17, TRL 1.13):

UNSLOTH_RETURN_LOGITS after for_training trainer
main 1 packing=True ignored (UNSLOTH_RETURN_LOGITS=1), padded rows
this PR 0 packing and padding-free on

The same sequence through FastModel already gives 0 on main.

Tests

tests/test_return_logits_survives_for_training.py lifts FastLlamaModel.for_training the same way the file already lifts the FastBaseModel methods: after FastBaseModel.for_inference() it restores an unset, "0" or "1" previous value, and it leaves a user-set 1 alone. Without the change the two undo cases fail. The file passes on torch 2.14 + transformers 5.17 and torch 2.7.1 + transformers 4.57.6.

@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-12T09:54:00.948869Z 9ecfd4d 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 9ecfd4d18a

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

1 participant