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.

Fix PEFT

Daniel Hanf20bc23e8417
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 213cb5b0ae9..23297cc4a85 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -404,32 +404,36 @@ pass
# =============================================
# Weirdly LoraLayer.update_layer downcasts PEFT layers to float16??
# For mixed precision, we need it to be in float32 not float16.
-from peft.tuners.lora.layer import LoraLayer
-import inspect, re
-try:
- source = inspect.getsource(LoraLayer.update_layer)
- text = "if weight is not None:\n"
- start = source.find(text) + len(text)
- end = source.find("self.to(weight.device)", start)
- spaces = re.findall(r"^([ ]{1,})break", source, flags = re.MULTILINE)[0]
- source = source.replace(source[start : end], spaces)
- spaces = len(re.match(r"[\s]{1,}", source).group(0))
- lines = source.split("\n")
- source = "\n".join(x[spaces:] for x in lines)
- source = re.sub("([^\.])nn\.", r"\1torch.nn.", source)
- source = source.replace("def update_layer", "def LoraLayer_update_layer")
- exec(source, globals())
-
- # Fix up incorrect downcasting of LoRA weights
+from packaging import Version
+from peft import __version__
+if Version(__version__) < Version("0.12.0"):
from peft.tuners.lora.layer import LoraLayer
- LoraLayer.update_layer = LoraLayer_update_layer
- from peft.tuners.lora import LoraLayer
- LoraLayer.update_layer = LoraLayer_update_layer
-except:
- logger.warning_once(
- "Unsloth unsuccessfully patched LoraLayer.update_layer. Please file a bug report.\n"\
- "Luckily, your training run will still work in the meantime!"
- )
+ import inspect, re
+ try:
+ source = inspect.getsource(LoraLayer.update_layer)
+ text = "if weight is not None:\n"
+ start = source.find(text) + len(text)
+ end = source.find("self.to(weight.device)", start)
+ spaces = re.findall(r"^([ ]{1,})break", source, flags = re.MULTILINE)[0]
+ source = source.replace(source[start : end], spaces)
+ spaces = len(re.match(r"[\s]{1,}", source).group(0))
+ lines = source.split("\n")
+ source = "\n".join(x[spaces:] for x in lines)
+ source = re.sub("([^\.])nn\.", r"\1torch.nn.", source)
+ source = source.replace("def update_layer", "def LoraLayer_update_layer")
+ exec(source, globals())
+
+ # Fix up incorrect downcasting of LoRA weights
+ from peft.tuners.lora.layer import LoraLayer
+ LoraLayer.update_layer = LoraLayer_update_layer
+ from peft.tuners.lora import LoraLayer
+ LoraLayer.update_layer = LoraLayer_update_layer
+ except:
+ logger.warning_once(
+ "Unsloth unsuccessfully patched LoraLayer.update_layer. Please file a bug report.\n"\
+ "Luckily, your training run will still work in the meantime!"
+ )
+ pass
pass
# =============================================