From 82e6a93ef25c7d3da50b4a4b717c44736d3bfc42 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Sun, 7 Jul 2024 09:33:01 -0700 Subject: [PATCH] Fix exec, eval (#743) * Update gemma2.py * Update llama.py * Update llama.py * Update gemma2.py * init * Update gemma2.py * Update gemma2.py * Update _utils.py * Update _utils.py * Update _utils.py * Update _utils.py * Update _utils.py * Update gemma2.py * Update gemma2.py * Update gemma2.py * All RoPE Scaling support * cleanup * Update llama.py * Update llama.py * Update _utils.py * Update _utils.py * exec * exec * Attention_Module * attention_module * imports * exec * Update llama.py * Update llama.py * boolean mask * revert masking * Update llama.py * Update save.py * Update llama.py * Update gemma2.py * Update gemma2.py * Update gemma2.py * Update utils.py * retry * Update gemma2.py * Update gemma2.py * Update gemma2.py * Update _utils.py * Update _utils.py * Update gemma2.py * Update chat_templates.py * Gemma 2 Ollama support * Update llama.py * Update llama.py * error handling * Update _utils.py * Update _utils.py * Stats for debugging * Update _utils.py * Update _utils.py * Debugging * Update tokenizer_utils.py * Update _utils.py * Update cross_entropy_loss.py * Update cross_entropy_loss.py * Update cross_entropy_loss.py * Update rms_layernorm.py * Update rms_layernorm.py * Update rms_layernorm.py * Update rms_layernorm.py * Update rms_layernorm.py * Update rms_layernorm.py * Update rms_layernorm.py * Check exec, eval --- unsloth/models/_utils.py | 6 +++++- unsloth/models/gemma.py | 6 ++++-- unsloth/models/gemma2.py | 6 ++++-- unsloth/models/mistral.py | 6 ++++-- unsloth/models/qwen2.py | 6 ++++-- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/unsloth/models/_utils.py b/unsloth/models/_utils.py index e0df218a93..725e9b3ff3 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 4d3db8d393..c47e65e87b 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 4a1420fb42..fda78534d8 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 28f664ca2d..6eb3fccfab 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 dcd05af60e..82de1951b8 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