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.

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

Daniel Hand4ea5a51d256
diff --git a/tests/test_return_logits_survives_for_training.py b/tests/test_return_logits_survives_for_training.py
index ec756f4a891..efb004ec0c8 100644
--- a/tests/test_return_logits_survives_for_training.py
+++ b/tests/test_return_logits_survives_for_training.py
@@ -136,6 +136,41 @@ def test_for_training_still_defaults_an_unset_flag_to_zero(fbm):
assert os.environ[FLAG] == "0"
+def _fast_llama_for_training():
+ tree = ast.parse((MODELS / "llama.py").read_text(encoding = "utf-8"))
+ cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "FastLlamaModel")
+ fn = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == "for_training")
+ fn.decorator_list = []
+ namespace = _Namespace(
+ os = os,
+ resolve_training_gradient_checkpointing = lambda model, flag: False,
+ arm_gradient_checkpointing = lambda model: None,
+ set_module_gradient_checkpointing = lambda module, flag: None,
+ )
+ module = ast.fix_missing_locations(ast.Module(body = [fn], type_ignores = []))
+ exec(compile(module, str(MODELS / "llama.py"), "exec"), namespace)
+ return namespace["for_training"]
+
+
[email protected]("before", [None, "0", "1"])
+def test_fast_language_model_for_training_undoes_generate(fbm, monkeypatch, before):
+ # FastModel-backed models (Qwen3.5, Gemma 3/4) run FastBaseModel.for_inference() in generate().
+ if before is not None:
+ monkeypatch.setenv(FLAG, before)
+ model = _Model()
+ fbm.for_inference(model)
+ assert os.environ[FLAG] == "1"
+ _fast_llama_for_training()(model)
+ assert os.environ[FLAG] == (before or "0")
+ assert MARK not in os.environ
+
+
+def test_fast_language_model_for_training_leaves_a_user_flag(fbm, monkeypatch):
+ monkeypatch.setenv(FLAG, "1")
+ _fast_llama_for_training()(_Model())
+ assert os.environ[FLAG] == "1"
+
+
def _trailing_for_inference_block():
tree = ast.parse((MODELS / "rl.py").read_text(encoding = "utf-8"))
template = next(
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 8dc484b9bbb..f2b89953182 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -4758,6 +4758,10 @@ class FastLlamaModel:
disable_use_cache(model)
except ImportError:
pass
+ # Undo only the UNSLOTH_RETURN_LOGITS=1 that FastBaseModel.for_inference() forced in generate().
+ _return_logits = os.environ.pop("_UNSLOTH_RETURN_LOGITS_BEFORE_INFERENCE", None)
+ if _return_logits is not None:
+ os.environ["UNSLOTH_RETURN_LOGITS"] = _return_logits
return model