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 downcasting LoRA (#318)

Daniel Han648bde7f06e3
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index 88a9e9661e1..77509cd8dad 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -95,11 +95,22 @@ def prepare_model_for_kbit_training(
"""
# Freeze all parameters except LoRA
- for name, param in model.named_parameters():
- if ".lora_A." in name or ".lora_B." in name or ".lora_magnitude_vector" in name:
- param.requires_grad_(True)
- else:
- param.requires_grad_(False)
+ import re
+ with torch.inference_mode():
+ for name, param in model.named_parameters():
+ if ".lora_A." in name or ".lora_B." in name or ".lora_magnitude_vector" in name:
+ param.requires_grad_(True)
+ # Also must be in float32!
+ if param.dtype != torch.float32:
+ name = name.replace("base_model", "model", 1)
+ layer_number = re.search(r"\.[\d]{1,}\.", name).group(0)
+ name = name.replace(layer_number, f"[{layer_number[1:-1]}].")
+ name = name.replace(".weight", "", 1)
+ exec(f"{name}.to(torch.float32)")
+ pass
+ else:
+ param.requires_grad_(False)
+ pass
pass
# Gradient checkpointing!
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 0b8092eaaca..202c692c4e0 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -1030,7 +1030,7 @@ class FastLlamaModel:
f' "-____-" Free Apache license: http://github.com/unslothai/unsloth'
print(statistics)
model_patcher.pre_patch()
- get_statistics()
+ # get_statistics()
if dtype is None:
dtype = torch.float16 if not SUPPORTS_BFLOAT16 else torch.bfloat16
diff --git a/unsloth/models/mapper.py b/unsloth/models/mapper.py
index f24108b75ab..b1d2faedb35 100644
--- a/unsloth/models/mapper.py
+++ b/unsloth/models/mapper.py
@@ -122,10 +122,6 @@ __INT_TO_FLOAT_MAPPER = \
"unsloth/codegemma-7b",
"google/codegemma-7b",
),
- "unsloth/codegemma-2b-it-bnb-4bit" : (
- "unsloth/codegemma-2b-it",
- "google/codegemma-2b-it",
- ),
"unsloth/codegemma-7b-it-bnb-4bit" : (
"unsloth/codegemma-7b-it",
"google/codegemma-7b-it",
diff --git a/unsloth/models/mistral.py b/unsloth/models/mistral.py
index 87f5c85ad15..3034b83d2ab 100644
--- a/unsloth/models/mistral.py
+++ b/unsloth/models/mistral.py
@@ -319,7 +319,7 @@ class FastMistralModel(FastLlamaModel):
f' "-____-" Free Apache license: http://github.com/unslothai/unsloth'
print(statistics)
model_patcher.pre_patch()
- get_statistics()
+ # get_statistics()
if dtype is None:
dtype = torch.float16 if not SUPPORTS_BFLOAT16 else torch.bfloat16