fix: add revision parameter support and escape quotes in chat templates

- Fix #3544: Add revision parameter to AutoConfig, AutoModelForCausalLM,
  AutoModelForSequenceClassification, and load_correct_tokenizer calls
  in FastLlamaModel.from_pretrained. This enables loading specific model
  revisions/branches from HuggingFace Hub.

- Fix #3667: Escape single quotes in system messages before substituting
  into Jinja2 templates. This prevents TemplateSyntaxError when system
  messages contain apostrophes (e.g., "user's" in Vicuna templates).

Signed-off-by: majiayu000 <1835304752@qq.com>
This commit is contained in:
majiayu000 2025-12-29 13:45:07 +08:00
commit b0a6e4154b
3 changed files with 15 additions and 2 deletions

View file

@ -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

View file

@ -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)

View file

@ -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