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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Fix delegated generation flag cleanup (#9469)
Pascal Andréc4b0e425016d
diff --git a/tests/test_for_training_generation_flag.py b/tests/test_for_training_generation_flag.pynew file mode 100644index 00000000000..03ec864fa5a--- /dev/null+++ b/tests/test_for_training_generation_flag.py@@ -0,0 +1,104 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++import ast+import builtins+import os+from pathlib import Path++import pytest++# Both for_training sites must survive a delegating PEFT wrapper. See issue #2490.+SITES = [("llama.py", "FastLlamaModel"), ("vision.py", "FastBaseModel")]+++class _Namespace(dict):+ """Globals for a method lifted out of its module; unused helpers resolve to None."""++ def __missing__(self, name):+ return getattr(builtins, name, None)+++def _for_training(module, class_name):+ path = Path(__file__).parents[1] / "unsloth" / "models" / module+ tree = ast.parse(path.read_text(encoding = "utf-8"))+ model_class = next(+ node for node in tree.body if isinstance(node, ast.ClassDef) and node.name == class_name+ )+ method = next(+ node+ for node in model_class.body+ if isinstance(node, ast.FunctionDef) and node.name == "for_training"+ )+ method.decorator_list = []+ compiled = ast.Module(body = [method], type_ignores = [])+ namespace = _Namespace(os = os)+ exec(compile(ast.fix_missing_locations(compiled), str(path), "exec"), namespace)+ return namespace["for_training"]+++class _Model:+ training = False+ gradient_checkpointing = False++ def __init__(self):+ self._flag_for_generation = True++ def parameters(self):+ return ()++ def modules(self):+ return ()++ def train(self):+ self.training = True+++class _PeftProxy:+ training = False++ def __init__(self, model):+ self.model = model++ def __getattr__(self, name):+ return getattr(self.__dict__["model"], name)++ def parameters(self):+ return ()++ def modules(self):+ return ()++ def train(self):+ self.training = True++[email protected]("module, class_name", SITES)+def test_for_training_deletes_a_generation_flag_delegated_by_a_peft_wrapper(module, class_name):+ model = _Model()+ proxy = _PeftProxy(model)+ assert hasattr(proxy, "_flag_for_generation")+ assert "_flag_for_generation" not in vars(proxy)++ _for_training(module, class_name)(proxy)++ assert not hasattr(model, "_flag_for_generation")+ assert not hasattr(proxy, "_flag_for_generation")++[email protected]("module, class_name", SITES)+def test_for_training_does_not_swallow_unrelated_errors(module, class_name):+ class _Exploding(_Model):+ def __init__(self):+ pass++ @property+ def _flag_for_generation(self):+ return True++ @_flag_for_generation.deleter+ def _flag_for_generation(self):+ raise RuntimeError("must propagate")++ with pytest.raises(RuntimeError):+ _for_training(module, class_name)(_Exploding())diff --git a/unsloth/models/llama.py b/unsloth/models/llama.pyindex 02c59f407f9..532df1a2a10 100644--- a/unsloth/models/llama.py+++ b/unsloth/models/llama.py@@ -3891,7 +3891,11 @@ class FastLlamaModel:m._saved_temp_tokenizer.padding_side = "right"# Set a flag for generation!if hasattr(m, "_flag_for_generation"):- del m._flag_for_generation+ try:+ # A PEFT wrapper delegates the read but owns nothing to delete+ del m._flag_for_generation+ except AttributeError:+ passm = modelwhile hasattr(m, "model"):diff --git a/unsloth/models/vision.py b/unsloth/models/vision.pyindex 8e121fea675..0776cce21d7 100644--- a/unsloth/models/vision.py+++ b/unsloth/models/vision.py@@ -2375,9 +2375,9 @@ class FastBaseModel:# Set a flag for generation!if hasattr(m, "_flag_for_generation"):try:- # Weirdly sometimes cannot succeed so do a try except+ # A PEFT wrapper delegates the read but owns nothing to deletedel m._flag_for_generation- except:+ except AttributeError:passm = model