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 exec, eval (#743)

Daniel Han775fb647d5b7
diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py
index e0df218a930..725e9b3ff3a 100644
--- a/unsloth/models/_utils.py
+++ b/unsloth/models/_utils.py
@@ -618,7 +618,11 @@ def patch_linear_scaling(
f"from {model_filepath} import logger, "\
f"{model_name.title()}Attention, {model_name.title()}Config"
- function = inspect.getsource(attention_module.__init__)
+ try:
+ function = inspect.getsource(attention_module.__init__)
+ except:
+ # Most likely already patched!
+ return None, None
where = function.find("def")
function = function.split("\n")
function = "\n".join(x[where:] for x in function)
diff --git a/unsloth/models/gemma.py b/unsloth/models/gemma.py
index 4d3db8d3931..c47e65e87b2 100644
--- a/unsloth/models/gemma.py
+++ b/unsloth/models/gemma.py
@@ -281,8 +281,10 @@ class FastGemmaModel(FastLlamaModel):
scaled_rope_module = GemmaFixedLinearScalingRotaryEmbedding,
attention_module = GemmaAttention,
)
- exec(function, globals())
- GemmaAttention.__init__ = eval(init_name)
+ if init_name is not None:
+ exec(function, globals())
+ GemmaAttention.__init__ = eval(init_name)
+ pass
GemmaAttention .forward = LlamaAttention_fast_forward
GemmaSdpaAttention .forward = LlamaAttention_fast_forward
GemmaFlashAttention2.forward = LlamaAttention_fast_forward
diff --git a/unsloth/models/gemma2.py b/unsloth/models/gemma2.py
index 4a1420fb42d..fda78534d89 100644
--- a/unsloth/models/gemma2.py
+++ b/unsloth/models/gemma2.py
@@ -436,8 +436,10 @@ class FastGemma2Model(FastLlamaModel):
scaled_rope_module = GemmaFixedLinearScalingRotaryEmbedding,
attention_module = Gemma2Attention,
)
- exec(function, globals())
- Gemma2Attention.__init__ = eval(init_name)
+ if init_name is not None:
+ exec(function, globals())
+ Gemma2Attention.__init__ = eval(init_name)
+ pass
Gemma2Attention .forward = Gemma2Attention_fast_forward
Gemma2SdpaAttention .forward = Gemma2Attention_fast_forward
Gemma2FlashAttention2.forward = Gemma2Attention_fast_forward
diff --git a/unsloth/models/mistral.py b/unsloth/models/mistral.py
index 28f664ca2d9..6eb3fccfabc 100644
--- a/unsloth/models/mistral.py
+++ b/unsloth/models/mistral.py
@@ -277,8 +277,10 @@ class FastMistralModel(FastLlamaModel):
scaled_rope_module = LlamaLinearScalingRotaryEmbedding,
attention_module = MistralAttention,
)
- exec(function, globals())
- MistralAttention.__init__ = eval(init_name)
+ if init_name is not None:
+ exec(function, globals())
+ MistralAttention.__init__ = eval(init_name)
+ pass
MistralAttention .forward = MistralAttention_fast_forward
MistralSdpaAttention .forward = MistralAttention_fast_forward
MistralFlashAttention2.forward = MistralAttention_fast_forward
diff --git a/unsloth/models/qwen2.py b/unsloth/models/qwen2.py
index dcd05af60e7..82de1951b89 100644
--- a/unsloth/models/qwen2.py
+++ b/unsloth/models/qwen2.py
@@ -45,8 +45,10 @@ class FastQwen2Model(FastLlamaModel):
scaled_rope_module = LlamaLinearScalingRotaryEmbedding,
attention_module = Qwen2Attention,
)
- exec(function, globals())
- Qwen2Attention.__init__ = eval(init_name)
+ if init_name is not None:
+ exec(function, globals())
+ Qwen2Attention.__init__ = eval(init_name)
+ pass
Qwen2Attention .forward = LlamaAttention_fast_forward
Qwen2SdpaAttention .forward = LlamaAttention_fast_forward
Qwen2FlashAttention2.forward = LlamaAttention_fast_forward