Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Eval step: restore UNSLOTH_RETURN_LOGITS even when evaluation raises (#11865)

Daniel Han5b5b78e7b823
diff --git a/tests/test_eval_step_restores_return_logits.py b/tests/test_eval_step_restores_return_logits.py
new file mode 100644
index 00000000000..d27f6829709
--- /dev/null
+++ b/tests/test_eval_step_restores_return_logits.py
@@ -0,0 +1,90 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+"""The eval prediction_step forces UNSLOTH_RETURN_LOGITS=1 and must put the caller's value back.
+
+UNSLOTH_RETURN_LOGITS=1 also blocks packing and padding-free when the next trainer is built, so
+an evaluate() that raised part way used to leave logits forced on for the rest of the process.
+The step is lifted out of PatchRL with ``ast`` so this runs without ``import unsloth``.
+"""
+
+from __future__ import annotations
+
+import ast
+import os
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+torch = pytest.importorskip("torch")
+
+SOURCE = Path(__file__).resolve().parents[1] / "unsloth" / "models" / "rl.py"
+
+
+def _prediction_step():
+ tree = ast.parse(SOURCE.read_text(encoding = "utf-8"))
+ patch_rl = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == "PatchRL")
+ step = next(
+ n
+ for n in ast.walk(patch_rl)
+ if isinstance(n, ast.FunctionDef) and n.name == "unsloth_prediction_step"
+ )
+ namespace = {"os": os, "torch": torch, "nested_detach": lambda x: x}
+ exec(compile(ast.Module(body = [step], type_ignores = []), str(SOURCE), "exec"), namespace)
+ return namespace["unsloth_prediction_step"]
+
+
+class _Trainer:
+ label_names = ["labels"]
+ can_return_loss = False
+ args = SimpleNamespace(device = "cpu", past_index = -1)
+
+ def __init__(self, compute_loss):
+ self.model = SimpleNamespace()
+ self.compute_loss = compute_loss
+ self.seen = []
+
+ def _prepare_inputs(self, inputs):
+ return inputs
+
+ def compute_loss_context_manager(self):
+ import contextlib
+ return contextlib.nullcontext()
+
+ def _get_num_items_in_batch(self, batches, device):
+ return None
+
+
[email protected]("before", [None, "0", "1"])
+def test_a_failed_eval_step_restores_the_callers_setting(monkeypatch, before):
+ if before is None:
+ monkeypatch.delenv("UNSLOTH_RETURN_LOGITS", raising = False)
+ else:
+ monkeypatch.setenv("UNSLOTH_RETURN_LOGITS", before)
+ seen = []
+
+ def compute_loss(model, inputs, **kwargs):
+ seen.append(os.environ.get("UNSLOTH_RETURN_LOGITS"))
+ raise RuntimeError("CUDA out of memory")
+
+ trainer = _Trainer(compute_loss)
+ with pytest.raises(RuntimeError, match = "out of memory"):
+ _prediction_step()(trainer, trainer.model, {"labels": torch.zeros(1)}, True, None)
+
+ assert seen == ["1"], "the step no longer forces logits on while it runs"
+ # An unset variable comes back as "0", the value the step treats as the default.
+ assert os.environ.get("UNSLOTH_RETURN_LOGITS") == (before or "0")
+
+
+def test_a_successful_eval_step_still_restores_it(monkeypatch):
+ monkeypatch.setenv("UNSLOTH_RETURN_LOGITS", "0")
+
+ def compute_loss(model, inputs, **kwargs):
+ return torch.tensor(1.0), (None, torch.zeros(1, 2))
+
+ trainer = _Trainer(compute_loss)
+ loss, logits, labels = _prediction_step()(
+ trainer, trainer.model, {"labels": torch.zeros(1)}, True, None
+ )
+ assert float(loss) == 1.0 and logits is None and labels is None
+ assert os.environ["UNSLOTH_RETURN_LOGITS"] == "0"
diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py
index f5d1138fa44..d6be24f4feb 100644
--- a/unsloth/models/rl.py
+++ b/unsloth/models/rl.py
@@ -250,44 +250,50 @@ def PatchRL(FastLanguageModel):
# Force logits during eval, but restore the user's prior setting after so an explicit UNSLOTH_RETURN_LOGITS="1" is not silently turned off.
_old_return_logits = os.environ.get("UNSLOTH_RETURN_LOGITS", "0")
os.environ["UNSLOTH_RETURN_LOGITS"] = "1"
- with torch.no_grad():
- if has_labels or loss_without_labels:
- with self.compute_loss_context_manager():
- try:
- num_items_in_batch = self._get_num_items_in_batch(
- [inputs], self.args.device
+ try:
+ with torch.no_grad():
+ if has_labels or loss_without_labels:
+ with self.compute_loss_context_manager():
+ try:
+ num_items_in_batch = self._get_num_items_in_batch(
+ [inputs], self.args.device
+ )
+ except (AttributeError, TypeError):
+ num_items_in_batch = None
+ loss, outputs = self.compute_loss(
+ model,
+ inputs,
+ return_outputs = True,
+ num_items_in_batch = num_items_in_batch,
)
- except (AttributeError, TypeError):
- num_items_in_batch = None
- loss, outputs = self.compute_loss(
- model,
- inputs,
- return_outputs = True,
- num_items_in_batch = num_items_in_batch,
- )
- loss = loss.mean().detach()
+ loss = loss.mean().detach()
- if isinstance(outputs, dict):
- logits = tuple(v for k, v in outputs.items() if k not in ignore_keys + ["loss"])
- else:
- logits = outputs[1:]
- else:
- loss = None
- with self.compute_loss_context_manager():
- tokenized_output = self.processing_class(
- inputs["prompt"],
- padding = True,
- truncation = True,
- return_tensors = "pt",
- ).to(model.device)
- outputs = model(**tokenized_output)
- if isinstance(outputs, dict):
- logits = tuple(v for k, v in outputs.items() if k not in ignore_keys)
+ if isinstance(outputs, dict):
+ logits = tuple(
+ v for k, v in outputs.items() if k not in ignore_keys + ["loss"]
+ )
+ else:
+ logits = outputs[1:]
else:
- logits = outputs
- if self.args.past_index >= 0:
- self._past = outputs[self.args.past_index - 1]
- os.environ["UNSLOTH_RETURN_LOGITS"] = _old_return_logits
+ loss = None
+ with self.compute_loss_context_manager():
+ tokenized_output = self.processing_class(
+ inputs["prompt"],
+ padding = True,
+ truncation = True,
+ return_tensors = "pt",
+ ).to(model.device)
+ outputs = model(**tokenized_output)
+ if isinstance(outputs, dict):
+ logits = tuple(v for k, v in outputs.items() if k not in ignore_keys)
+ else:
+ logits = outputs
+ if self.args.past_index >= 0:
+ self._past = outputs[self.args.past_index - 1]
+ finally:
+ # An eval that raises must not leave logits forced on: UNSLOTH_RETURN_LOGITS=1 also
+ # blocks packing and padding-free for the next train() in this process.
+ os.environ["UNSLOTH_RETURN_LOGITS"] = _old_return_logits
if prediction_loss_only:
return (loss, None, None)