diff --git a/unsloth/chat_templates.py b/unsloth/chat_templates.py index 63d310af8e..57543ed150 100644 --- a/unsloth/chat_templates.py +++ b/unsloth/chat_templates.py @@ -2108,14 +2108,18 @@ def _change_system_message(template: str, type_chat_template: str, system_messag if has_placeholder: if system_message is None: raise ValueError("Unsloth: You need to provide a system message for custom templates.") - new_template = re.sub(system_message_pattern, system_message, template) + # Escape single quotes to prevent Jinja2 template syntax errors + escaped_message = system_message.replace("'", "\\'") + new_template = re.sub(system_message_pattern, escaped_message, template) return new_template, system_message return template, system_message # For predefined templates with default system message message_to_use = system_message if system_message is not None else default_system_message - new_template = re.sub(system_message_pattern, message_to_use, template) + # Escape single quotes to prevent Jinja2 template syntax errors + escaped_message = message_to_use.replace("'", "\\'") + new_template = re.sub(system_message_pattern, escaped_message, template) return new_template, message_to_use diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py index 1d7695b9aa..69590afe2c 100644 --- a/unsloth/models/llama.py +++ b/unsloth/models/llama.py @@ -2223,6 +2223,7 @@ class FastLlamaModel: model_name, token = token, attn_implementation = "sdpa", + revision = revision, ) model_config.model_name = model_name model_max_seq_length = model_config.max_position_embeddings @@ -2312,6 +2313,7 @@ class FastLlamaModel: max_position_embeddings = max_position_embeddings, trust_remote_code = trust_remote_code, attn_implementation = "eager", + revision = revision, **kwargs, ) elif not fast_inference: @@ -2324,6 +2326,7 @@ class FastLlamaModel: max_position_embeddings = max_position_embeddings, trust_remote_code = trust_remote_code, attn_implementation = "eager", + revision = revision, **kwargs, ) model.fast_generate = model.generate @@ -2381,6 +2384,7 @@ class FastLlamaModel: token = token, trust_remote_code = trust_remote_code, fix_tokenizer = fix_tokenizer, + revision = revision, ) model, tokenizer = patch_tokenizer(model, tokenizer) diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.py index 0136e3498e..fa70e8f73f 100644 --- a/unsloth/tokenizer_utils.py +++ b/unsloth/tokenizer_utils.py @@ -503,6 +503,7 @@ def _load_correct_tokenizer( trust_remote_code = False, cache_dir = "huggingface_tokenizers_cache", fix_tokenizer = True, + revision = None, ): if IS_COLAB_ENVIRONMENT: cache_dir = cache_dir @@ -528,6 +529,7 @@ def _load_correct_tokenizer( legacy = False, from_slow = True, cache_dir = cache_dir, + revision = revision, ) except: slow_tokenizer = None @@ -546,6 +548,7 @@ def _load_correct_tokenizer( token = token, trust_remote_code = trust_remote_code, cache_dir = cache_dir, + revision = revision, ) if not fix_tokenizer or tokenizer_name in IGNORED_TOKENIZER_NAMES: @@ -587,6 +590,7 @@ def load_correct_tokenizer( trust_remote_code = False, cache_dir = "huggingface_tokenizers_cache", fix_tokenizer = True, + revision = None, ): tokenizer = _load_correct_tokenizer( tokenizer_name = tokenizer_name, @@ -596,6 +600,7 @@ def load_correct_tokenizer( trust_remote_code = trust_remote_code, cache_dir = cache_dir, fix_tokenizer = fix_tokenizer, + revision = revision, ) ### 1. Fixup tokenizer's chat_template