This commit is contained in:
Francesco Buciuni 2025-02-10 10:41:51 +01:00 committed by GitHub
commit 310fa58686
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 42 additions and 0 deletions

View file

@ -699,6 +699,9 @@ class Model:
if chkhsh == "b3f499bb4255f8ca19fccd664443283318f2fd2414d5e0b040fbdd0cc195d6c5":
# ref: https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
res = "deepseek-r1-qwen"
if chkhsh == "a3df2b8943e01cfd7d68c9f8446b294f3d8706d1d6853df65df7fda5d4fcb19f":
# ref: https://huggingface.co/Almawave/Velvet-14B
res = "velvet"
if res is None:
logger.warning("\n")

View file

@ -109,6 +109,7 @@ models = [
{"name": "megrez", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/Infinigence/Megrez-3B-Instruct"},
{"name": "deepseek-v3", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/deepseek-ai/DeepSeek-V3"},
{"name": "deepseek-r1-qwen", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"},
{"name": "velvet", "tokt": TOKENIZER_TYPE.BPE, "repo": "https://huggingface.co/Almawave/Velvet-14B"}
]

View file

@ -105,6 +105,7 @@ extern "C" {
LLAMA_VOCAB_PRE_TYPE_CHAMELEON = 26,
LLAMA_VOCAB_PRE_TYPE_MINERVA = 27,
LLAMA_VOCAB_PRE_TYPE_DEEPSEEK3_LLM = 28,
LLAMA_VOCAB_PRE_TYPE_VELVET = 29,
};
enum llama_rope_type {

View file

@ -58,6 +58,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
{ "granite", LLM_CHAT_TEMPLATE_GRANITE },
{ "gigachat", LLM_CHAT_TEMPLATE_GIGACHAT },
{ "megrez", LLM_CHAT_TEMPLATE_MEGREZ },
{ "velvet", LLM_CHAT_TEMPLATE_VELVET },
};
llm_chat_template llm_chat_template_from_str(const std::string & name) {
@ -167,6 +168,8 @@ llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
return LLM_CHAT_TEMPLATE_GIGACHAT;
} else if (tmpl_contains("<|role_start|>")) {
return LLM_CHAT_TEMPLATE_MEGREZ;
} else if (tmpl_contains("<instruction>")) {
return LLM_CHAT_TEMPLATE_VELVET;
}
return LLM_CHAT_TEMPLATE_UNKNOWN;
}
@ -566,6 +569,31 @@ int32_t llm_chat_apply_template(
if (add_ass) {
ss << "<|role_start|>assistant<|role_end|>";
}
} else if (tmpl == LLM_CHAT_TEMPLATE_VELVET) {
// Velvet template
std::string leading_space = "";
std::string trailing_space = "";
bool trim_assistant_message = false;
bool is_inside_turn = false;
std::string system_message = "";
std::string last_message(chat.back()->content);
ss << "<s>";
for (auto message : chat) {
if (!is_inside_turn) {
ss << leading_space << "<instruction>" << trailing_space;
is_inside_turn = true;
}
std::string role(message->role);
std::string content(message->content);
if (role == "system") {
system_message = content + "\n\n";
} else if (role == "user") {
ss << (content==last_message ? system_message : "") << content << leading_space << "</instruction>";
} else {
ss << trailing_space << (trim_assistant_message ? trim(content) : content) << "</s>";
is_inside_turn = false;
}
}
} else {
// template not supported
return -1;

View file

@ -38,6 +38,7 @@ enum llm_chat_template {
LLM_CHAT_TEMPLATE_GRANITE,
LLM_CHAT_TEMPLATE_GIGACHAT,
LLM_CHAT_TEMPLATE_MEGREZ,
LLM_CHAT_TEMPLATE_VELVET,
LLM_CHAT_TEMPLATE_UNKNOWN,
};

View file

@ -262,6 +262,14 @@ int main(void) {
/* .bos_token= */ "",
/* .eos_token= */ "",
},
{
/* .name= */ "velvet",
/* .template_str= */ "{%- if messages[0][\"role\"] == \"system\" %}\n {%- set system_message = messages[0][\"content\"] %}\n {%- set loop_messages = messages[1:] %}\n{%- else %}\n {%- set loop_messages = messages %}\n{%- endif %}\n{%- if not tools is defined %}\n {%- set tools = none %}\n{%- endif %}\n{%- set user_messages = loop_messages | selectattr(\"role\", \"equalto\", \"user\") | list %}\n\n{#- This block checks for alternating user/assistant messages, skipping tool calling messages #}\n{%- set ns = namespace() %}\n{%- set ns.index = 0 %}\n{%- for message in loop_messages %}\n {%- if not (message.role == \"tool\" or message.role == \"tool_results\" or (message.tool_calls is defined and message.tool_calls is not none)) %}\n {%- if (message[\"role\"] == \"user\") != (ns.index % 2 == 0) %}\n {{- raise_exception(\"After the optional system message, conversation roles must alternate user/assistant/user/assistant/...\") }}\n {%- endif %}\n {%- set ns.index = ns.index + 1 %}\n {%- endif %}\n{%- endfor %}\n\n{{- bos_token }}\n{%- for message in loop_messages %}\n {%- if message[\"role\"] == \"user\" %}\n {%- if message == user_messages[-1] and system_message is defined and tools is not none %}\n {{- \"<instruction>\" + system_message + \"\\n\\n\" + \"<list_tools>[\" }}\n {%- for tool in tools %}\n{{ tool }}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- else %}\n {{- \"]\" }}\n {%- endif %}\n {%- endfor %}\n {{- \"</list_tools>\" + \"\\n\\n\" + message[\"content\"] + \"</instruction>\" }}\n {%- elif message == user_messages[-1] and system_message is defined and tools is none %}\n {{- \"<instruction>\" + system_message + \"\\n\\n\" + message[\"content\"] + \"</instruction>\" }}\n {%- else %}\n {{- \"<instruction>\" + message[\"content\"] + \"</instruction>\" }}\n {%- endif %}\n {%- elif (message.tool_calls is defined and message.tool_calls is not none) %}\n {{- \"<tool_call>[\" }}\n {%- for tool_call in message.tool_calls %}\n{{ tool_call }}\n {%- if not loop.last %}\n {{- \", \" }}\n {%- else %}\n {{- \"]\" }}\n {%- endif %}\n {%- endfor %} {{- \"</tool_call>\" + eos_token }}\n {%- elif message[\"role\"] == \"assistant\" %}\n {{- message[\"content\"] + eos_token}}\n {%- elif message[\"role\"] == \"tool_results\" or message[\"role\"] == \"tool\" %}\n {%- if message.content is defined and message.content.content is defined %}\n {%- set content = message.content.content %}\n {%- else %}\n {%- set content = message.content %}\n {%- endif %}\n {{- '<tool_result>{\"content\": ' + content|string }}\n {{- '}</tool_result>' }}\n {%- else %}\n {{- raise_exception(\"Only user and assistant roles are supported, with the exception of an initial optional system message!\") }}\n {%- endif %}\n{%- endfor %}\n",
/* .expected_output= */ "<s><instruction>Hello</instruction>Hi there</s><instruction>Who are you</instruction> I am an assistant </s><instruction>You are a helpful assistant\n\nAnother question</instruction>",
/* .expected_output_jinja= */ "<s><instruction>Hello</instruction>Hi there</s><instruction>Who are you</instruction> I am an assistant </s><instruction>You are a helpful assistant\n\nAnother question</instruction>",
/* .bos_token= */ "<s>",
/* .eos_token= */ "</s>",
},
};
std::vector<char> formatted_chat(1024);
int32_t res;