Gemma 3 chat template
This commit is contained in:
parent
5349526e35
commit
7fe2874157
2 changed files with 82 additions and 4 deletions
|
|
@ -934,6 +934,84 @@ DEFAULT_SYSTEM_MESSAGE["phi-4"] = None # No system message in Phi-4
|
|||
pass
|
||||
|
||||
|
||||
# =========================================== Gemma-3
|
||||
# Obtained via
|
||||
# print(tokenizer.chat_template.replace("}\n", "####").replace("\n", "\\n").replace("####", "}\n"))
|
||||
gemma3_template = \
|
||||
"""{{ bos_token }}
|
||||
{%- if messages[0]['role'] == 'system' -%}
|
||||
{%- if messages[0]['content'] is string -%}
|
||||
{%- set first_user_prefix = messages[0]['content'] + '\n\n' -%}
|
||||
{%- else -%}
|
||||
{%- set first_user_prefix = messages[0]['content'][0]['text'] + '\n\n' -%}
|
||||
{%- endif -%}
|
||||
{%- set loop_messages = messages[1:] -%}
|
||||
{%- else -%}
|
||||
{%- set first_user_prefix = "" -%}
|
||||
{%- set loop_messages = messages -%}
|
||||
{%- endif -%}
|
||||
{%- for message in loop_messages -%}
|
||||
{%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
|
||||
{{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
|
||||
{%- endif -%}
|
||||
{%- if (message['role'] == 'assistant') -%}
|
||||
{%- set role = "model" -%}
|
||||
{%- else -%}
|
||||
{%- set role = message['role'] -%}
|
||||
{%- endif -%}
|
||||
{{ '<start_of_turn>' + role + '\n' + (first_user_prefix if loop.first else "") }}
|
||||
{%- if message['content'] is string -%}
|
||||
{{ message['content'] | trim }}
|
||||
{%- elif message['content'] is iterable -%}
|
||||
{%- for item in message['content'] -%}
|
||||
{%- if item['type'] == 'image' -%}
|
||||
{{ '<start_of_image>' }}
|
||||
{%- elif item['type'] == 'text' -%}
|
||||
{{ item['text'] | trim }}
|
||||
{%- endif -%}
|
||||
{%- endfor -%}
|
||||
{%- else -%}
|
||||
{{ raise_exception("Invalid content type") }}
|
||||
{%- endif -%}
|
||||
{{ '<end_of_turn>\n' }}
|
||||
{%- endfor -%}
|
||||
{%- if add_generation_prompt -%}
|
||||
{{ '<start_of_turn>model\n' }}
|
||||
{%- endif -%}
|
||||
"""
|
||||
|
||||
# Ollama from https://ollama.com/library/gemma3/blobs/e0a42594d802
|
||||
gemma3_ollama = \
|
||||
'''
|
||||
FROM {__FILE_LOCATION__}
|
||||
TEMPLATE """{{- range $i, $_ := .Messages }}
|
||||
{{- $last := eq (len (slice $.Messages $i)) 1 }}
|
||||
{{- if or (eq .Role "user") (eq .Role "system") }}<start_of_turn>user
|
||||
{{ .Content }}<end_of_turn>
|
||||
{{ if $last }}<start_of_turn>model
|
||||
{{ end }}
|
||||
{{- else if eq .Role "assistant" }}<start_of_turn>model
|
||||
{{ .Content }}{{ if not $last }}<end_of_turn>
|
||||
{{ end }}
|
||||
{{- end }}
|
||||
{{- end }}"""
|
||||
PARAMETER stop "<end_of_turn>"
|
||||
PARAMETER stop "<eos>"
|
||||
PARAMETER temperature 0.1
|
||||
PARAMETER min_p 0.0
|
||||
PARAMETER top_k 64
|
||||
PARAMETER top_p 0.95
|
||||
PARAMETER num_predict 32768
|
||||
'''
|
||||
|
||||
gemma3_template_eos_token = "<end_of_turn>"
|
||||
CHAT_TEMPLATES["gemma-3"] = (gemma3_template, gemma3_template_eos_token, False, gemma3_ollama,)
|
||||
DEFAULT_SYSTEM_MESSAGE["gemma-3"] = None # No system message in Gemma-3
|
||||
|
||||
CHAT_TEMPLATES["gemma3"] = (gemma3_template, gemma3_template_eos_token, False, gemma3_ollama,)
|
||||
DEFAULT_SYSTEM_MESSAGE["gemma3"] = None # No system message in Gemma-3
|
||||
pass
|
||||
|
||||
def _change_system_message(template: str, type_chat_template: str, system_message: str = None):
|
||||
system_message_pattern = r"\{system_message\}"
|
||||
|
||||
|
|
|
|||
|
|
@ -129,12 +129,12 @@ class FastBaseModel:
|
|||
try: vllm_version = f" vLLM: {importlib_version('vllm')}."
|
||||
except: vllm_version = ""
|
||||
|
||||
model_name = model_types[0]
|
||||
if model_name == "siglip" and len(model_types) != 1:
|
||||
model_name = model_types[1]
|
||||
model_type_arch = model_types[0]
|
||||
if model_type_arch == "siglip" and len(model_types) != 1:
|
||||
model_type_arch = model_types[1]
|
||||
|
||||
statistics = \
|
||||
f"==((====))== Unsloth {__version__}: Fast {model_name.title()} patching. Transformers: {transformers_version}.{vllm_version}\n"\
|
||||
f"==((====))== Unsloth {__version__}: Fast {model_type_arch.title()} patching. Transformers: {transformers_version}.{vllm_version}\n"\
|
||||
f" {chr(92)}{chr(92)} /| {gpu_stats.name}. Num GPUs = {torch.cuda.device_count()}. Max memory: {max_memory} GB. Platform: {platform_system}.\n"\
|
||||
f"O^O/ {chr(92)}_/ {chr(92)} Torch: {torch.__version__}. CUDA: {gpu_stats.major}.{gpu_stats.minor}. CUDA Toolkit: {torch.version.cuda}. Triton: {triton_version}\n"\
|
||||
f"{chr(92)} / Bfloat16 = {str(SUPPORTS_BFLOAT16).upper()}. FA [Xformers = {xformers_version}. FA2 = {HAS_FLASH_ATTENTION}]\n"\
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue