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.

Keep the training state intact across a standalone evaluate() / predict() (#11860)

Daniel Han2e29cac7f382
diff --git a/tests/test_full_eval_keeps_training_state.py b/tests/test_full_eval_keeps_training_state.py
new file mode 100644
index 00000000000..789b8e5e8ae
--- /dev/null
+++ b/tests/test_full_eval_keeps_training_state.py
@@ -0,0 +1,438 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""Standalone evaluate() under fp16/bf16_full_eval must not change trainable dtypes, buffers or placement."""
+
+from __future__ import annotations
+
+import ast
+import os
+from pathlib import Path
+
+import pytest
+import torch
+
+from real_accelerator import has_real_cuda
+
+
+SOURCE_PATH = Path(__file__).resolve().parents[1] / "unsloth" / "models" / "rl.py"
+HELPERS = (
+ "_accelerator_indices",
+ "_model_spans_devices",
+ "_place_for_full_eval",
+ "_wrap_full_eval_keeps_trainable_dtype",
+)
+
+
+class _Logger:
+ def __init__(self):
+ self.messages = []
+
+ def info(self, message):
+ self.messages.append(message)
+
+
+def _load_helpers():
+ tree = ast.parse(SOURCE_PATH.read_text(encoding = "utf-8"), filename = str(SOURCE_PATH))
+ found = [n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name in HELPERS]
+ missing = set(HELPERS) - {n.name for n in found}
+ assert not missing, f"missing module-level defs in {SOURCE_PATH}: {sorted(missing)}"
+ namespace = {"torch": torch, "logger": _Logger()}
+ exec(compile(ast.Module(body = found, type_ignores = []), str(SOURCE_PATH), "exec"), namespace)
+ return namespace
+
+
+def _cuda_kernels_run(count):
+ """Torch can launch a kernel on the card (not just see it)."""
+ if not has_real_cuda() or torch.cuda.device_count() < count:
+ return False
+ try:
+ for index in range(count):
+ (torch.ones(2, device = f"cuda:{index}") * 2).sum().item()
+ return True
+ except Exception:
+ return False
+
+
+def test_wrapper_is_applied_to_every_generated_trainer():
+ """Wrap sits at function level so every trainer gets it."""
+ tree = ast.parse(SOURCE_PATH.read_text(encoding = "utf-8"))
+ impl = next(
+ n
+ for n in tree.body
+ if isinstance(n, ast.FunctionDef) and n.name == "_patch_trl_rl_trainers_impl"
+ )
+
+ def calls_wrapper(node):
+ return any(
+ isinstance(c, ast.Call)
+ and isinstance(c.func, ast.Name)
+ and c.func.id == "_wrap_full_eval_keeps_trainable_dtype"
+ for c in ast.walk(node)
+ )
+
+ top_level = [s for s in impl.body if calls_wrapper(s)]
+ assert top_level, "_wrap_full_eval_keeps_trainable_dtype is never called"
+ assert all(
+ not isinstance(s, ast.If) for s in top_level
+ ), "the full-eval wrap is gated on a trainer_file branch"
+
+
+transformers = pytest.importorskip("transformers")
+
+
+def _tiny_model():
+ """Attention projections trainable, the rest frozen, all fp32."""
+ torch.manual_seed(0)
+ config = transformers.LlamaConfig(
+ vocab_size = 64,
+ hidden_size = 16,
+ intermediate_size = 32,
+ num_hidden_layers = 2,
+ num_attention_heads = 2,
+ num_key_value_heads = 2,
+ max_position_embeddings = 64,
+ tie_word_embeddings = False,
+ )
+ model = transformers.LlamaForCausalLM(config).float()
+ for name, param in model.named_parameters():
+ trainable = "q_proj" in name or "v_proj" in name
+ param.requires_grad_(trainable)
+ return model
+
+
+class _Rows(torch.utils.data.Dataset):
+ def __init__(
+ self,
+ n = 4,
+ length = 8,
+ ):
+ g = torch.Generator().manual_seed(1)
+ self.ids = torch.randint(0, 64, (n, length), generator = g)
+
+ def __len__(self):
+ return len(self.ids)
+
+ def __getitem__(self, i):
+ return {"input_ids": self.ids[i], "labels": self.ids[i].clone()}
+
+
+def _args(
+ tmp_path,
+ precision,
+ autocast = True,
+ **extra,
+):
+ kwargs = dict(
+ output_dir = str(tmp_path),
+ use_cpu = True,
+ per_device_train_batch_size = 2,
+ per_device_eval_batch_size = 2,
+ max_steps = 2,
+ learning_rate = 1e-3,
+ report_to = [],
+ save_strategy = "no",
+ logging_steps = 1,
+ )
+ kwargs[f"{precision}_full_eval"] = True
+ kwargs[precision] = autocast
+ kwargs.update(extra)
+ return transformers.TrainingArguments(**kwargs)
+
+
+def _trainer(
+ tmp_path,
+ precision,
+ wrap = True,
+ model = None,
+ helpers = None,
+ **extra,
+):
+ cls = type("_T", (transformers.Trainer,), {})
+ if wrap:
+ (helpers or _load_helpers())["_wrap_full_eval_keeps_trainable_dtype"](cls)
+ return cls(
+ model = model if model is not None else _tiny_model(),
+ args = _args(tmp_path, precision, **extra),
+ train_dataset = _Rows(),
+ eval_dataset = _Rows(),
+ )
+
+
+def _state(model):
+ return {
+ name: (t.data_ptr(), t.dtype, t.detach().clone())
+ for name, t in list(model.named_parameters()) + list(model.named_buffers())
+ if t.is_floating_point()
+ }
+
+
+def _assert_untouched(model, before):
+ after = _state(model)
+ assert after.keys() == before.keys()
+ for name, (ptr, dtype, value) in before.items():
+ assert after[name][0] == ptr, f"{name} was reallocated"
+ assert after[name][1] is dtype, name
+ assert torch.equal(after[name][2], value), name
+
+
+def test_premise_transformers_leaves_the_whole_model_cast(tmp_path):
+ """Upstream still casts; if it casts back, the wrapper is redundant, not wrong."""
+ trainer = _trainer(tmp_path, "bf16", wrap = False)
+ trainer.evaluate()
+ dtypes = {p.dtype for p in trainer.model.parameters() if p.requires_grad}
+ if dtypes == {torch.float32}:
+ pytest.skip("this Transformers no longer leaves trainable parameters cast after full eval")
+ assert dtypes == {torch.bfloat16}
+
+
[email protected]("precision", ["bf16", "fp16"])
[email protected]("entry", ["evaluate", "predict"])
+def test_standalone_eval_leaves_every_weight_untouched(tmp_path, precision, entry):
+ trainer = _trainer(tmp_path, precision)
+ model = trainer.model
+ before = _state(model)
+ assert any(".inv_freq" in n or n.endswith("inv_freq") for n in before), "no rotary buffer"
+
+ if entry == "evaluate":
+ trainer.evaluate()
+ else:
+ trainer.predict(_Rows())
+
+ _assert_untouched(model, before)
+ assert getattr(trainer.args, f"{precision}_full_eval") is True
+
+
[email protected]("precision", ["bf16", "fp16"])
+def test_standalone_eval_matches_an_eval_inside_train(tmp_path, precision):
+ standalone = _trainer(tmp_path / "a", precision).evaluate()
+ in_train = _trainer(tmp_path / "b", precision)
+ in_train.is_in_train = True
+ try:
+ reference = in_train.evaluate()
+ finally:
+ in_train.is_in_train = False
+ assert standalone["eval_loss"] == reference["eval_loss"]
+
+
+def test_without_autocast_nothing_is_cast(tmp_path):
+ """bf16 full finetuning: bf16_full_eval without autocast, so nothing is cast."""
+ trainer = _trainer(tmp_path, "bf16", autocast = False)
+ dtypes = {n: p.dtype for n, p in trainer.model.named_parameters()}
+ metrics = trainer.evaluate()
+ assert "eval_loss" in metrics
+ assert {n: p.dtype for n, p in trainer.model.named_parameters()} == dtypes
+
+
+def test_evaluate_then_train_matches_train_alone(tmp_path):
+ first = _trainer(tmp_path / "a", "bf16")
+ first.evaluate()
+ first.train()
+ second = _trainer(tmp_path / "b", "bf16")
+ second.train()
+ for (name, p1), (_, p2) in zip(first.model.named_parameters(), second.model.named_parameters()):
+ assert p1.dtype is p2.dtype, name
+ assert torch.equal(p1, p2), name
+
+
+def test_evaluate_after_train_then_resume(tmp_path):
+ trainer = _trainer(tmp_path, "bf16")
+ trainer.train()
+ trainer.evaluate()
+ assert {p.dtype for p in trainer.model.parameters() if p.requires_grad} == {torch.float32}
+ trainer.args.max_steps = 4
+ trainer.train()
+ assert {p.dtype for p in trainer.model.parameters() if p.requires_grad} == {torch.float32}
+
+
+def test_eval_inside_train_is_passed_through(tmp_path):
+ trainer = _trainer(tmp_path, "bf16")
+ before = {n: p.dtype for n, p in trainer.model.named_parameters()}
+ trainer.is_in_train = True
+ try:
+ trainer.evaluate()
+ finally:
+ trainer.is_in_train = False
+ assert {n: p.dtype for n, p in trainer.model.named_parameters()} == before
+
+
+def test_legacy_prediction_loop(tmp_path):
+ if not hasattr(transformers.Trainer, "prediction_loop"):
+ pytest.skip("this Transformers has no legacy prediction_loop")
+ trainer = _trainer(tmp_path, "bf16", use_legacy_prediction_loop = True)
+ before = _state(trainer.model)
+ trainer.evaluate()
+ _assert_untouched(trainer.model, before)
+
+
+def test_no_full_eval_is_untouched(tmp_path):
+ trainer = _trainer(tmp_path, "bf16", bf16_full_eval = False)
+ trainer.evaluate()
+ assert {p.dtype for p in trainer.model.parameters()} == {torch.float32}
+
+
+def test_split_model_is_not_moved():
+ """Split model must not be collapsed; cpu/meta simulates a split."""
+ helpers = _load_helpers()
+
+ model = _tiny_model()
+ model.model.layers[1].to("meta")
+ model.hf_device_map = {"model.layers.0": "cpu", "model.layers.1": "meta"}
+ before = {n: p.device for n, p in model.named_parameters()}
+ helpers["_place_for_full_eval"](model, torch.device("cpu"))
+ assert {n: p.device for n, p in model.named_parameters()} == before
+
+ spans = helpers["_model_spans_devices"]
+ split = _tiny_model()
+ split.hf_device_map = {"model.layers.0": 0, "model.layers.1": 1}
+ assert spans(split)
+ offloaded = _tiny_model()
+ offloaded.hf_device_map = {"model.layers.0": 0, "model.layers.1": "disk"}
+ assert spans(offloaded)
+ same = _tiny_model()
+ same.hf_device_map = {"model.layers.0": 0, "model.layers.1": "cuda:0"}
+ assert not spans(same)
+ assert not spans(_tiny_model())
+
+
+def test_a_failing_placement_still_evaluates(tmp_path):
+ helpers = _load_helpers()
+
+ def fail(model, device):
+ raise RuntimeError("boom")
+
+ helpers["_place_for_full_eval"] = fail
+ trainer = _trainer(tmp_path, "bf16", helpers = helpers)
+ before = _state(trainer.model)
+ assert "eval_loss" in trainer.evaluate()
+ assert trainer.args.bf16_full_eval is True
+ _assert_untouched(trainer.model, before)
+ assert any("boom" in m for m in helpers["logger"].messages)
+
+
[email protected]("where", ["place", "eval"])
+def test_keyboard_interrupt_leaves_flags_and_weights(tmp_path, where):
+ """BaseException propagates with flags restored."""
+ helpers = _load_helpers()
+ if where == "place":
+ real_place = helpers["_place_for_full_eval"]
+
+ def place_then_interrupt(model, device):
+ real_place(model, device)
+ raise KeyboardInterrupt
+
+ helpers["_place_for_full_eval"] = place_then_interrupt
+ trainer = _trainer(tmp_path, "fp16", helpers = helpers)
+ if where == "eval":
+
+ def interrupt(*args, **kwargs):
+ assert trainer.args.fp16_full_eval is False
+ raise KeyboardInterrupt
+
+ trainer.compute_loss = interrupt
+ before = _state(trainer.model)
+ with pytest.raises(KeyboardInterrupt):
+ trainer.evaluate()
+ assert trainer.args.fp16_full_eval is True and trainer.args.bf16_full_eval is False
+ _assert_untouched(trainer.model, before)
+
+
[email protected](not _cuda_kernels_run(2), reason = "needs two CUDA devices torch can run on")
+def test_split_model_evaluate_train_evaluate_on_two_gpus(tmp_path):
+ accelerate = pytest.importorskip("accelerate")
+ peft = pytest.importorskip("peft")
+
+ model = _tiny_model().to(torch.bfloat16)
+ for param in model.parameters():
+ param.requires_grad_(False)
+ device_map = {
+ "model.embed_tokens": 1,
+ "model.layers": 0,
+ "model.norm": 0,
+ "model.rotary_emb": 1,
+ "lm_head": 1,
+ }
+ model = accelerate.dispatch_model(model, device_map = device_map)
+ model.hf_device_map = device_map
+ model = peft.get_peft_model(
+ model, peft.LoraConfig(r = 4, lora_alpha = 4, target_modules = ["q_proj", "v_proj"])
+ )
+ for param in model.parameters():
+ if param.requires_grad:
+ param.data = param.data.float()
+ cls = type("_T", (transformers.Trainer,), {})
+ _load_helpers()["_wrap_full_eval_keeps_trainable_dtype"](cls)
+ args = transformers.TrainingArguments(
+ output_dir = str(tmp_path),
+ per_device_train_batch_size = 2,
+ per_device_eval_batch_size = 2,
+ max_steps = 2,
+ report_to = [],
+ bf16 = True,
+ save_strategy = "no",
+ )
+ trainer = cls(model = model, args = args, train_dataset = _Rows(), eval_dataset = _Rows())
+ trainer.args.bf16_full_eval = True
+ devices = {p.device for p in model.parameters()}
+ assert len(devices) == 2
+
+ def trainable_dtypes():
+ return {p.dtype for p in model.parameters() if p.requires_grad}
+
+ trainer.evaluate()
+ assert {p.device for p in model.parameters()} == devices
+ assert trainable_dtypes() == {torch.float32}
+ trainer.train()
+ trainer.evaluate()
+ assert {p.device for p in model.parameters()} == devices
+ assert trainable_dtypes() == {torch.float32}
+
+
[email protected](not _cuda_kernels_run(1), reason = "runs the generated SFTTrainer on a GPU")
[email protected]("dtype", ["float16", "bfloat16", "float32"])
+def test_generated_sft_trainer_evaluate_then_train(tmp_path, dtype):
+ """On main fp16 raised "Attempting to unscale FP16 gradients"; a float32 base stays float32 (frozen too)."""
+ from unsloth import FastLanguageModel
+ from datasets import Dataset
+ from trl import SFTConfig, SFTTrainer
+
+ dtype = getattr(torch, dtype)
+ if dtype is torch.bfloat16 and not torch.cuda.is_bf16_supported():
+ pytest.skip("no bf16 on this card")
+ model_name = os.environ.get(
+ "UNSLOTH_TEST_TINY_QWEN3", "trl-internal-testing/tiny-Qwen3ForCausalLM"
+ )
+ model, tokenizer = FastLanguageModel.from_pretrained(
+ model_name, max_seq_length = 64, dtype = dtype, load_in_4bit = False
+ )
+ model = FastLanguageModel.get_peft_model(
+ model, r = 8, lora_alpha = 8, target_modules = ["q_proj", "v_proj"], random_state = 0
+ )
+ texts = [f"Question {i}? Answer {i}." for i in range(8)]
+ trainer = SFTTrainer(
+ model = model,
+ processing_class = tokenizer,
+ train_dataset = Dataset.from_dict({"text": texts}),
+ eval_dataset = Dataset.from_dict({"text": texts[:4]}),
+ args = SFTConfig(
+ output_dir = str(tmp_path),
+ dataset_text_field = "text",
+ max_steps = 2,
+ per_device_train_batch_size = 2,
+ per_device_eval_batch_size = 2,
+ report_to = "none",
+ save_strategy = "no",
+ max_length = 64,
+ ),
+ )
+ assert trainer.args.fp16_full_eval or trainer.args.bf16_full_eval
+ before = {n: p.dtype for n, p in trainer.model.named_parameters()}
+ assert {before[n] for n, p in trainer.model.named_parameters() if p.requires_grad} == {
+ torch.float32
+ }
+ trainer.evaluate()
+ assert {n: p.dtype for n, p in trainer.model.named_parameters()} == before
+ trainer.train()
+ assert {n: p.dtype for n, p in trainer.model.named_parameters()} == before
diff --git a/unsloth/models/rl.py b/unsloth/models/rl.py
index b95b7ebcdd4..25d61082955 100644
--- a/unsloth/models/rl.py
+++ b/unsloth/models/rl.py
@@ -630,6 +630,102 @@ def _register_config_pickle_fallback(displaced_config, patched_config):
)
+def _accelerator_indices(device_map):
+ """Accelerator devices in an ``hf_device_map``; cpu/disk/meta are offload targets."""
+ indices = set()
+ for value in device_map.values():
+ if isinstance(value, bool):
+ continue
+ if isinstance(value, int):
+ indices.add(value)
+ continue
+ text = str(value)
+ if text.isdigit():
+ indices.add(int(text))
+ continue
+ if text in ("cpu", "disk", "meta"):
+ continue
+ try:
+ device = torch.device(text)
+ except Exception:
+ continue
+ if device.type in ("cpu", "meta"):
+ continue
+ indices.add(0 if device.index is None else device.index)
+ return indices
+
+
+def _model_spans_devices(model):
+ """Judged from real placement, not ``is_model_parallel`` (also True for a model on one non-default card)."""
+ for module in model.modules():
+ device_map = getattr(module, "hf_device_map", None)
+ if not isinstance(device_map, dict):
+ continue
+ accelerators = _accelerator_indices(device_map)
+ offloaded = any(str(v) in ("cpu", "disk", "meta") for v in device_map.values())
+ if len(accelerators) > 1 or (accelerators and offloaded):
+ return True
+ devices = {t.device for t in model.parameters()}
+ devices.update(t.device for t in model.buffers())
+ return len(devices) > 1
+
+
+def _place_for_full_eval(model, device):
+ """Move to ``args.device`` (``Trainer.__init__`` skips placement under full eval); never cast: an in-place cast of
+ frozen weights rounds fp32 bases for later training and corrupts packed ``Params4bit`` float quant storage."""
+ if device is None or _model_spans_devices(model):
+ return
+ device = torch.device(device)
+ if device.type == "cuda" and device.index is None and torch.cuda.is_available():
+ device = torch.device("cuda", torch.cuda.current_device())
+ if any(t.device != device for t in model.parameters()):
+ model.to(device = device)
+
+
+def _wrap_full_eval_keeps_trainable_dtype(trainer_cls):
+ """Transformers casts the whole model on standalone full eval and never casts back; flags restored in ``finally``."""
+ for loop_name in ("evaluation_loop", "prediction_loop"):
+ original = getattr(trainer_cls, loop_name, None)
+ if original is None or getattr(original, "_unsloth_full_eval_wrapped", False):
+ continue
+
+ def _make(original, loop_name):
+ def wrapped(self, *args, **kwargs):
+ train_args = getattr(self, "args", None)
+ fp16_full_eval = getattr(train_args, "fp16_full_eval", False)
+ bf16_full_eval = getattr(train_args, "bf16_full_eval", False)
+ model = getattr(self, "model", None)
+ if (
+ getattr(self, "is_in_train", False)
+ or not (fp16_full_eval or bf16_full_eval)
+ or not isinstance(model, torch.nn.Module)
+ # Sharded parameters are Transformers' and the engine's to cast.
+ or getattr(self, "is_deepspeed_enabled", False)
+ or getattr(self, "is_fsdp_enabled", False)
+ ):
+ return original(self, *args, **kwargs)
+ try:
+ _place_for_full_eval(model, getattr(train_args, "device", None))
+ except Exception as e:
+ logger.info(f"Unsloth: Full eval could not move the model to args.device: {e}")
+ try:
+ train_args.fp16_full_eval = False
+ train_args.bf16_full_eval = False
+ return original(self, *args, **kwargs)
+ finally:
+ train_args.fp16_full_eval = fp16_full_eval
+ train_args.bf16_full_eval = bf16_full_eval
+
+ wrapped.__name__ = getattr(original, "__name__", loop_name)
+ wrapped.__qualname__ = getattr(original, "__qualname__", loop_name)
+ wrapped.__doc__ = getattr(original, "__doc__", None)
+ wrapped.__wrapped__ = original
+ wrapped._unsloth_full_eval_wrapped = True
+ return wrapped
+
+ setattr(trainer_cls, loop_name, _make(original, loop_name))
+
+
def _wrap_grpo_generate_and_score(trainer_cls):
if not hasattr(trainer_cls, "_generate_and_score_completions"):
return
@@ -3185,6 +3281,11 @@ def _patch_trl_rl_trainers_impl(trainer_file = "grpo_trainer"):
except Exception as e:
logger.info(f"Unsloth: Could not wrap evaluate for {RLTrainer_name}: {e}")
+ try:
+ _wrap_full_eval_keeps_trainable_dtype(getattr(created_module, f"Unsloth{RLTrainer_name}"))
+ except Exception as e:
+ logger.info(f"Unsloth: Could not wrap the full-eval cast for {RLTrainer_name}: {e}")
+
if trainer_file == "grpo_trainer":
try:
_wrap_grpo_generate_and_score(getattr(created_module, f"Unsloth{RLTrainer_name}"))