Skip to content

Commit a02c63d

Browse files
committed
Support InfiniAI Megrez 3b
1 parent 7909e85 commit a02c63d

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

convert_hf_to_gguf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,9 @@ def get_vocab_base_pre(self, tokenizer) -> str:
671671
if chkhsh == "ad851be1dba641f2e3711822f816db2c265f788b37c63b4e1aeacb9ee92de8eb":
672672
# ref: https://huggingface.co/ai-sage/GigaChat-20B-A3B-instruct
673673
res = "gigachat"
674+
if chkhsh == "d4c8f286ea6b520b3d495c4455483cfa2302c0cfcd4be05d781b6a8a0a7cdaf1":
675+
# ref: https://huggingface.co/Infinigence/Megrez-3B-Instruct
676+
res = "megrez"
674677

675678
if res is None:
676679
logger.warning("\n")

convert_hf_to_gguf_update.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class TOKENIZER_TYPE(IntEnum):
105105
{"name": "minerva-7b", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/sapienzanlp/Minerva-7B-base-v1.0", },
106106
{"name": "roberta-bpe", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/sentence-transformers/stsb-roberta-base"},
107107
{"name": "gigachat", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/ai-sage/GigaChat-20B-A3B-instruct"},
108+
{"name": "megrez", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/Infinigence/Megrez-3B-Instruct"},
108109
]
109110

110111

src/llama.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,7 @@ enum llm_chat_template {
16911691
LLM_CHAT_TEMPLATE_RWKV_WORLD,
16921692
LLM_CHAT_TEMPLATE_GRANITE,
16931693
LLM_CHAT_TEMPLATE_GIGACHAT,
1694+
LLM_CHAT_TEMPLATE_MEGREZ,
16941695
LLM_CHAT_TEMPLATE_UNKNOWN,
16951696
};
16961697

@@ -1723,6 +1724,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
17231724
{ "rwkv-world", LLM_CHAT_TEMPLATE_RWKV_WORLD },
17241725
{ "granite", LLM_CHAT_TEMPLATE_GRANITE },
17251726
{ "gigachat", LLM_CHAT_TEMPLATE_GIGACHAT },
1727+
{ "megrez", LLM_CHAT_TEMPLATE_MEGREZ },
17261728
};
17271729

17281730
static llm_arch llm_arch_from_string(const std::string & name) {
@@ -6602,7 +6604,8 @@ static void llm_load_vocab(
66026604
vocab.type_pre = LLAMA_VOCAB_PRE_TYPE_COMMAND_R;
66036605
vocab.tokenizer_clean_spaces = false;
66046606
} else if (
6605-
tokenizer_pre == "qwen2") {
6607+
tokenizer_pre == "qwen2" ||
6608+
tokenizer_pre == "megrez") {
66066609
vocab.type_pre = LLAMA_VOCAB_PRE_TYPE_QWEN2;
66076610
vocab.tokenizer_clean_spaces = false;
66086611
} else if (
@@ -22654,6 +22657,8 @@ static llm_chat_template llama_chat_detect_template(const std::string & tmpl) {
2265422657
return LLM_CHAT_TEMPLATE_GRANITE;
2265522658
} else if (tmpl_contains("message['role'] + additional_special_tokens[0] + message['content'] + additional_special_tokens[1]")) {
2265622659
return LLM_CHAT_TEMPLATE_GIGACHAT;
22660+
} else if (tmpl_contains("<|role_start|>")) {
22661+
return LLM_CHAT_TEMPLATE_MEGREZ;
2265722662
}
2265822663
return LLM_CHAT_TEMPLATE_UNKNOWN;
2265922664
}
@@ -23003,6 +23008,16 @@ static int32_t llama_chat_apply_template_internal(
2300323008
if (add_ass) {
2300423009
ss << "assistant<|role_sep|>";
2300523010
}
23011+
} else if (tmpl == LLM_CHAT_TEMPLATE_MEGREZ) {
23012+
// Megrez template
23013+
for (auto message : chat) {
23014+
std::string role(message->role);
23015+
ss << "<|role_start|>" << role << "<|role_end|>" << message->content << "<|turn_end|>";
23016+
}
23017+
23018+
if (add_ass) {
23019+
ss << "<|role_start|>assistant<|role_end|>";
23020+
}
2300623021
} else {
2300723022
// template not supported
2300823023
return -1;

tests/test-chat-template.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ int main(void) {
7777
"{{ bos_token }}{% for message in messages %}{% if message['role'] == 'user' %}{{ '[INST] ' + message['content'] + '[/INST]' }}{% elif message['role'] == 'system' %}{{ '[SYSTEM_PROMPT] ' + message['content'] + '[/SYSTEM_PROMPT]' }}{% elif message['role'] == 'assistant' %}{{ ' ' + message['content'] + eos_token }}{% else %}{{ raise_exception('Only user, system and assistant roles are supported!') }}{% endif %}{% endfor %}",
7878
// ai-sage/GigaChat-20B-A3B-instruct
7979
"{% if messages[0]['role'] == 'system' -%}\n {%- set loop_messages = messages[1:] -%}\n {%- set system_message = bos_token + messages[0]['content'] + additional_special_tokens[1] -%}\n{%- else -%}\n {%- set loop_messages = messages -%}\n {%- set system_message = bos_token + '' -%}\n{%- endif -%}\n{%- for message in loop_messages %}\n {% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}\n {{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}\n {% endif %}\n \n {%- if loop.index0 == 0 -%}\n {{ system_message -}}\n {%- endif -%}\n {%- if message['role'] == 'user' -%}\n {{ message['role'] + additional_special_tokens[0] + message['content'] + additional_special_tokens[1] -}}\n {{ 'available functions' + additional_special_tokens[0] + additional_special_tokens[2] + additional_special_tokens[3] + additional_special_tokens[1] -}}\n {%- endif -%}\n {%- if message['role'] == 'assistant' -%}\n {{ message['role'] + additional_special_tokens[0] + message['content'] + additional_special_tokens[1] -}}\n {%- endif -%}\n {%- if loop.last and add_generation_prompt -%}\n {{ 'assistant' + additional_special_tokens[0] -}}\n {%- endif -%}\n{%- endfor %}",
80+
// Infinigence/Megrez-3B-Instruct
81+
u8"{% for message in messages %}{% if loop.first and messages[0]['role'] != 'system' %}{{ '<|role_start|>system<|role_end|>你是Megrez-3B-Instruct,将针对用户的问题给出详细的、积极的回答。<|turn_end|>' }}{% endif %}{{ '<|role_start|>' + message['role'] + '<|role_end|>' + message['content'] + '<|turn_end|>' }}{% endfor %}{% if add_generation_prompt %}{{ '<|role_start|>assistant<|role_end|>' }}{% endif %}"
8082
};
8183
std::vector<std::string> expected_output = {
8284
// teknium/OpenHermes-2.5-Mistral-7B
@@ -133,6 +135,8 @@ int main(void) {
133135
"[SYSTEM_PROMPT] You are a helpful assistant[/SYSTEM_PROMPT][INST] Hello[/INST] Hi there</s>[INST] Who are you[/INST] I am an assistant </s>[INST] Another question[/INST]",
134136
// ai-sage/GigaChat-20B-A3B-instruct
135137
"<s>You are a helpful assistant<|message_sep|>user<|role_sep|>Hello<|message_sep|>available functions<|role_sep|>[]<|message_sep|>assistant<|role_sep|>Hi there<|message_sep|>user<|role_sep|>Who are you<|message_sep|>available functions<|role_sep|>[]<|message_sep|>assistant<|role_sep|> I am an assistant <|message_sep|>user<|role_sep|>Another question<|message_sep|>available functions<|role_sep|>[]<|message_sep|>assistant<|role_sep|>",
138+
// Infinigence/Megrez-3B-Instruct
139+
"<|role_start|>system<|role_end|>You are a helpful assistant<|turn_end|><|role_start|>user<|role_end|>Hello<|turn_end|><|role_start|>assistant<|role_end|>Hi there<|turn_end|><|role_start|>user<|role_end|>Who are you<|turn_end|><|role_start|>assistant<|role_end|> I am an assistant <|turn_end|><|role_start|>user<|role_end|>Another question<|turn_end|><|role_start|>assistant<|role_end|>",
136140
};
137141
std::vector<char> formatted_chat(1024);
138142
int32_t res;

0 commit comments

Comments
 (0)