diff --git a/src/llamafactory/data/template.py b/src/llamafactory/data/template.py index 8a29e32ee..7a923de3e 100644 --- a/src/llamafactory/data/template.py +++ b/src/llamafactory/data/template.py @@ -459,6 +459,18 @@ class ReasoningTemplate(Template): return [(encoded_messages[i], encoded_messages[i + 1]) for i in range(0, len(encoded_messages), 2)] +@dataclass +class Glm47ReasoningTemplate(ReasoningTemplate): + r"""GLM-4.7 uses only the closing tag for empty thinking blocks.""" + + @override + def add_thought(self, content: str = "") -> str: + if not content: + return self.thought_words[1] + + return self.thought_words[0] + content + self.thought_words[1] + + TEMPLATES: dict[str, "Template"] = {} @@ -1049,6 +1061,23 @@ register_template( ) +# copied from glm4_moe template +register_template( + name="glm4_7", + format_user=StringFormatter(slots=["<|user|>\n{{content}}<|assistant|>"]), + format_assistant=StringFormatter(slots=["\n{{content}}"]), + format_system=StringFormatter(slots=["<|system|>\n{{content}}"]), + format_function=FunctionFormatter(slots=["{{content}}"], tool_format="glm4_moe"), + format_observation=StringFormatter(slots=["<|observation|>\n{{content}}<|assistant|>"]), + format_tools=ToolFormatter(tool_format="glm4_moe"), + format_prefix=EmptyFormatter(slots=["[gMASK]"]), + stop_words=["<|user|>", "<|observation|>"], + thought_words=("", ""), + efficient_eos=True, + template_class=Glm47ReasoningTemplate, +) + + # copied from glm4 template register_template( name="glmz1", diff --git a/src/llamafactory/extras/constants.py b/src/llamafactory/extras/constants.py index 0c9967fd4..cd9791e13 100644 --- a/src/llamafactory/extras/constants.py +++ b/src/llamafactory/extras/constants.py @@ -939,6 +939,17 @@ register_model_group( ) +register_model_group( + models={ + "GLM-4.7-Flash": { + DownloadSource.DEFAULT: "zai-org/GLM-4.7-Flash", + DownloadSource.MODELSCOPE: "ZhipuAI/GLM-4.7-Flash", + }, + }, + template="glm4_7", +) + + register_model_group( models={ "GLM-Z1-0414-9B-Chat": { diff --git a/src/llamafactory/model/model_utils/moe.py b/src/llamafactory/model/model_utils/moe.py index 250c38c70..d7c19c44c 100644 --- a/src/llamafactory/model/model_utils/moe.py +++ b/src/llamafactory/model/model_utils/moe.py @@ -77,6 +77,11 @@ def add_z3_leaf_module(model: "PreTrainedModel") -> None: _set_z3_leaf_modules(model, [Glm4MoeMoE]) + if model_type == "glm4_moe_lite": + from transformers.models.glm4_moe_lite.modeling_glm4_moe_lite import Glm4MoeLiteMoE + + _set_z3_leaf_modules(model, [Glm4MoeLiteMoE]) + if model_type == "glm4v_moe": from transformers.models.glm4v_moe.modeling_glm4v_moe import Glm4vMoeTextMoE