Skip to content

Commit 61715d5

Browse files
arch-btwngxson
andauthored
llama : Add IBM granite template (#10013)
* Add granite template to llama.cpp * Add granite template to test-chat-template.cpp * Update src/llama.cpp Co-authored-by: Xuan Son Nguyen <[email protected]> * Update tests/test-chat-template.cpp Co-authored-by: Xuan Son Nguyen <[email protected]> * Added proper template and expected output * Small change to \n Small change to \n * Add code space & Co-authored-by: Xuan Son Nguyen <[email protected]> * Fix spacing * Apply suggestions from code review * Update src/llama.cpp --------- Co-authored-by: Xuan Son Nguyen <[email protected]>
1 parent 07028f9 commit 61715d5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/llama.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -21706,6 +21706,16 @@ static int32_t llama_chat_apply_template_internal(
2170621706
ss << message->content << "\n\n";
2170721707
}
2170821708
}
21709+
} else if (tmpl == "granite" || tmpl_contains("<|start_of_role|>")) {
21710+
// IBM Granite template
21711+
for (const auto & message : chat) {
21712+
std::string role(message->role);
21713+
ss << "<|start_of_role|>" << role << "<|end_of_role|>"
21714+
<< message->content << "<|end_of_text|>\n";
21715+
}
21716+
if (add_ass) {
21717+
ss << "<|start_of_role|>assistant<|end_of_role|>\n";
21718+
}
2170921719
} else {
2171021720
// template not supported
2171121721
return -1;

tests/test-chat-template.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ int main(void) {
6565
u8"{% for message in messages %}{% if message['role'] == 'user' %}{{'<用户>' + message['content'].strip() + '<AI>'}}{% else %}{{message['content'].strip()}}{% endif %}{% endfor %}",
6666
// DeepSeek-V2
6767
"{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{{ bos_token }}{% for message in messages %}{% if message['role'] == 'user' %}{{ 'User: ' + message['content'] + '\n\n' }}{% elif message['role'] == 'assistant' %}{{ 'Assistant: ' + message['content'] + eos_token }}{% elif message['role'] == 'system' %}{{ message['content'] + '\n\n' }}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ 'Assistant:' }}{% endif %}",
68+
// ibm-granite/granite-3.0-8b-instruct
69+
"{%- if tools %}\n {{- '<|start_of_role|>available_tools<|end_of_role|>\n' }}\n {%- for tool in tools %}\n {{- tool | tojson(indent=4) }}\n {%- if not loop.last %}\n {{- '\n\n' }}\n {%- endif %}\n {%- endfor %}\n {{- '<|end_of_text|>\n' }}\n{%- endif %}\n{%- for message in messages %}\n {%- if message['role'] == 'system' %}\n {{- '<|start_of_role|>system<|end_of_role|>' + message['content'] + '<|end_of_text|>\n' }}\n {%- elif message['role'] == 'user' %}\n {{- '<|start_of_role|>user<|end_of_role|>' + message['content'] + '<|end_of_text|>\n' }}\n {%- elif message['role'] == 'assistant' %}\n {{- '<|start_of_role|>assistant<|end_of_role|>' + message['content'] + '<|end_of_text|>\n' }}\n {%- elif message['role'] == 'assistant_tool_call' %}\n {{- '<|start_of_role|>assistant<|end_of_role|><|tool_call|>' + message['content'] + '<|end_of_text|>\n' }}\n {%- elif message['role'] == 'tool_response' %}\n {{- '<|start_of_role|>tool_response<|end_of_role|>' + message['content'] + '<|end_of_text|>\n' }}\n {%- endif %}\n {%- if loop.last and add_generation_prompt %}\n {{- '<|start_of_role|>assistant<|end_of_role|>' }}\n {%- endif %}\n{%- endfor %}",
6870
};
6971
std::vector<std::string> expected_output = {
7072
// teknium/OpenHermes-2.5-Mistral-7B
@@ -109,6 +111,8 @@ int main(void) {
109111
u8"You are a helpful assistant<用户>Hello<AI>Hi there<用户>Who are you<AI>I am an assistant<用户>Another question<AI>",
110112
// DeepSeek-V2
111113
u8"You are a helpful assistant\n\nUser: Hello\n\nAssistant: Hi there<|end▁of▁sentence|>User: Who are you\n\nAssistant: I am an assistant <|end▁of▁sentence|>User: Another question\n\nAssistant:",
114+
// ibm-granite/granite-3.0-8b-instruct
115+
"<|start_of_role|>system<|end_of_role|>You are a helpful assistant<|end_of_text|>\n<|start_of_role|>user<|end_of_role|>Hello<|end_of_text|>\n<|start_of_role|>assistant<|end_of_role|>Hi there<|end_of_text|>\n<|start_of_role|>user<|end_of_role|>Who are you<|end_of_text|>\n<|start_of_role|>assistant<|end_of_role|> I am an assistant <|end_of_text|>\n<|start_of_role|>user<|end_of_role|>Another question<|end_of_text|>\n<|start_of_role|>assistant<|end_of_role|>\n",
112116
};
113117
std::vector<char> formatted_chat(1024);
114118
int32_t res;

0 commit comments

Comments
 (0)