Skip to content

Commit 200a8bf

Browse files
authored
Merge branch 'ggml-org:master' into master
2 parents e1c40d4 + 005a1e1 commit 200a8bf

104 files changed

Lines changed: 5088 additions & 1406 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎ci/run.sh‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,6 @@ function gg_run_test_backend_ops {
649649
fi
650650
local args_extra="-j ${n_jobs}"
651651

652-
# TODO: fix multi-threaded for ROCm
653-
# https://github.com/ggml-org/llama.cpp/actions/runs/34576278519/job/103297889044?pr=28740#step:3:4865
654-
if [ ! -z ${GG_BUILD_ROCM} ]; then
655-
args_extra=""
656-
fi
657-
658652
# TODO: MoltenVK bug?
659653
# https://github.com/ggml-org/llama.cpp/actions/runs/34611260059/job/103302413736?pr=28740#step:3:5897
660654
if [ ! -z "${GG_BUILD_VULKAN}" ] && [ "$(uname -s)" = "Darwin" ]; then

‎common/chat.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,14 @@ std::optional<common_chat_params> common_chat_try_specialized_template(
11391139
return common_chat_params_init_kimi_k3(tmpl, params);
11401140
}
11411141

1142+
// K2 Horizon - <|ifm|im_start|> turns, <ifm|think*> reasoning picked by reasoning_effort and
1143+
// <ifm|tool_calls> sections; the three think tag pairs defeat the autoparser's reasoning detection
1144+
if (src.find("<|ifm|im_start|>") != std::string::npos &&
1145+
src.find("<ifm|tool_calls>") != std::string::npos) {
1146+
LOG_DBG("Using specialized template: K2 Horizon\n");
1147+
return common_chat_params_init_k2_horizon(tmpl, params);
1148+
}
1149+
11421150
// Ling 3.0 / Bailing V3 - <role>X</role> sections with <arg_key>/<arg_value> tagged
11431151
// tool calls. <role> sections are unique to this family among the tagged-arg templates.
11441152
if (src.find("<role>ASSISTANT</role>") != std::string::npos &&

‎common/common.cpp‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,12 +1163,13 @@ struct common_init_result::impl {
11631163
};
11641164

11651165
static const std::map<common_decision_type, std::string> COMMON_DECISION_TYPE_NAMES = {
1166-
{ COMMON_DECISION_TYPE_OPENJEV, "openjev" },
1167-
{ COMMON_DECISION_TYPE_LEV, "lev" },
1168-
{ COMMON_DECISION_TYPE_KEV, "kev" },
1169-
{ COMMON_DECISION_TYPE_NIMBLE, "nimble" },
1170-
{ COMMON_DECISION_TYPE_LAYA, "laya" },
1171-
{ COMMON_DECISION_TYPE_CLEF, "clef" },
1166+
{ COMMON_DECISION_TYPE_OPENJEV, "openjev" },
1167+
{ COMMON_DECISION_TYPE_LEV, "lev" },
1168+
{ COMMON_DECISION_TYPE_KEV, "kev" },
1169+
{ COMMON_DECISION_TYPE_NIMBLE, "nimble" },
1170+
{ COMMON_DECISION_TYPE_LAYA, "laya" },
1171+
{ COMMON_DECISION_TYPE_CLEF, "clef" },
1172+
{ COMMON_DECISION_TYPE_PPLX_DECIDER, "pplx-decider" },
11721173
};
11731174

11741175
static common_decision_type common_decision_type_from_string(const std::string & str) {

‎common/common.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ enum common_decision_type {
960960
COMMON_DECISION_TYPE_NIMBLE, // same as openjev, the prompt lists all the questions of the request
961961
COMMON_DECISION_TYPE_LAYA, // score of one marker token per option, read from the embeddings output
962962
COMMON_DECISION_TYPE_CLEF, // all questions in one prompt, score of option i read from the embeddings output at row i
963+
COMMON_DECISION_TYPE_PPLX_DECIDER, // same as openjev, label codes of 1 or 2 letters
963964
COMMON_DECISION_TYPE_UNKNOWN, // a decision model of a type that is not supported
964965
};
965966

‎common/jinja/caps.cpp‎

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,57 @@ static void caps_try_execute(jinja::program & prog,
3737
const caps_ctx_fn & ctx_fn,
3838
const caps_json_fn & tools_fn,
3939
const caps_analyze_fn & analyze_fn) {
40-
context ctx;
41-
ctx.is_get_stats = true;
42-
jinja::global_from_json(ctx, json{
43-
{"messages", messages_fn()},
44-
{"tools", tools_fn ? tools_fn() : json::array()},
45-
{"bos_token", ""},
46-
{"eos_token", ""},
47-
{"add_generation_prompt", true}
48-
}, true);
49-
50-
if (ctx_fn) {
51-
ctx_fn(ctx);
52-
}
40+
json msgs = messages_fn();
41+
for (int attempt = 0; attempt < 2; attempt++) {
42+
context ctx;
43+
ctx.is_get_stats = true;
44+
jinja::global_from_json(ctx, json{
45+
{"messages", msgs},
46+
{"tools", tools_fn ? tools_fn() : json::array()},
47+
{"bos_token", ""},
48+
{"eos_token", ""},
49+
{"add_generation_prompt", true}
50+
}, true);
51+
52+
if (ctx_fn) {
53+
ctx_fn(ctx);
54+
}
5355

54-
auto messages = ctx.get_val("messages");
55-
auto tools = ctx.get_val("tools");
56-
57-
bool success = false;
58-
std::string result;
59-
try {
60-
jinja::runtime runtime(ctx);
61-
auto results = runtime.execute(prog);
62-
auto parts = jinja::runtime::gather_string_parts(results);
63-
result = parts->as_string().str();
64-
success = true;
65-
} catch (const std::exception & e) {
66-
JJ_DEBUG("Exception during execution: %s", e.what());
67-
result = "";
68-
// ignore exceptions during capability analysis
69-
}
56+
auto messages = ctx.get_val("messages");
57+
auto tools = ctx.get_val("tools");
58+
59+
bool success = false;
60+
std::string result;
61+
try {
62+
jinja::runtime runtime(ctx);
63+
auto results = runtime.execute(prog);
64+
auto parts = jinja::runtime::gather_string_parts(results);
65+
result = parts->as_string().str();
66+
success = true;
67+
} catch (const std::exception & e) {
68+
JJ_DEBUG("Exception during execution: %s", e.what());
69+
result = "";
70+
// ignore exceptions during capability analysis
71+
}
72+
73+
// some templates require a thinking field on every assistant turn (e.g. K2 Horizon):
74+
// retry once with an empty reasoning_content on the assistant turns that lack one
75+
if (!success && attempt == 0) {
76+
bool added = false;
77+
for (auto & msg : msgs) {
78+
if (msg.is_object() && msg.value("role", "") == "assistant" && !msg.contains("reasoning_content")) {
79+
msg["reasoning_content"] = "";
80+
added = true;
81+
}
82+
}
83+
if (added) {
84+
continue;
85+
}
86+
}
7087

71-
analyze_fn(ctx, success, messages, tools, result);
88+
analyze_fn(ctx, success, messages, tools, result);
89+
return;
90+
}
7291
}
7392

7493
// for debugging only

‎common/parsers/k2-horizon.cpp‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#include "parsers.h"
2+
3+
// K2 Horizon format:
4+
// - Reasoning: <ifm|think>...</ifm|think>, or <ifm|think_fast>/<ifm|think_faster> for medium/low reasoning_effort
5+
// - Tool calls: <ifm|tool_calls><ifm|tool_call>...</ifm|tool_call>...</ifm|tool_calls>, one call per <ifm|tool_call>:
6+
// xml (default): name <ifm|arg_key>k</ifm|arg_key> [<ifm|arg_type>t</ifm|arg_type>] <ifm|arg_value>v</ifm|arg_value> ...
7+
// json: {"name": "...", "arguments": {...}}
8+
common_chat_params common_chat_params_init_k2_horizon(const common_chat_template & tmpl,
9+
const autoparser::generation_params & inputs) {
10+
common_chat_params data;
11+
12+
// The template requires a thinking field on every assistant message
13+
auto messages = inputs.messages;
14+
for (auto & msg : messages) {
15+
if (msg.value("role", "") == "assistant" && !msg.contains("reasoning_content")) {
16+
msg["reasoning_content"] = "";
17+
}
18+
}
19+
20+
data.prompt = common_chat_template_direct_apply_impl(tmpl, inputs, messages);
21+
data.generation_prompt = common_chat_template_generation_prompt_impl(tmpl, inputs, messages);
22+
data.format = COMMON_CHAT_FORMAT_PEG_NATIVE;
23+
data.supports_thinking = true;
24+
25+
const std::string effort = inputs.extra_context.value("reasoning_effort", "high");
26+
const std::string call_format = inputs.extra_context.value("tool_call_format", "xml");
27+
28+
// Templates that handle enable_thinking disable it with an empty <ifm|think></ifm|think> block for every effort
29+
const bool thinking_off = !inputs.enable_thinking && tmpl.source().find("enable_thinking") != std::string::npos;
30+
const std::string think = thinking_off ? "ifm|think" :
31+
effort == "medium" ? "ifm|think_fast" :
32+
effort == "low" ? "ifm|think_faster" : "ifm|think";
33+
34+
const std::string GEN_PREFIX = "<|ifm|im_start|>assistant\n";
35+
const std::string THINK_START = "<" + think + ">";
36+
const std::string THINK_END = "</" + think + ">";
37+
const std::string SECTION_START = "<ifm|tool_calls>";
38+
const std::string SECTION_END = "</ifm|tool_calls>";
39+
const std::string CALL_START = "<ifm|tool_call>";
40+
const std::string CALL_END = "</ifm|tool_call>";
41+
const std::string ARG_KEY = "<ifm|arg_key>";
42+
const std::string ARG_KEY_END = "</ifm|arg_key>";
43+
const std::string ARG_TYPE = "<ifm|arg_type>";
44+
const std::string ARG_TYPE_END = "</ifm|arg_type>";
45+
const std::string ARG_VAL = "<ifm|arg_value>";
46+
const std::string ARG_VAL_END = "</ifm|arg_value>";
47+
48+
data.thinking_start_tag = THINK_START;
49+
data.thinking_end_tags = { THINK_END };
50+
51+
data.preserved_tokens = data.thinking_end_tags;
52+
data.preserved_tokens.insert(data.preserved_tokens.end(), {
53+
THINK_START, SECTION_START, SECTION_END, CALL_START, CALL_END,
54+
ARG_KEY, ARG_KEY_END, ARG_TYPE, ARG_TYPE_END, ARG_VAL, ARG_VAL_END,
55+
});
56+
57+
data.message_delimiters = {
58+
{ COMMON_CHAT_ROLE_ASSISTANT, "<|ifm|im_start|>assistant" },
59+
{ COMMON_CHAT_ROLE_USER, "<|ifm|im_start|>user" },
60+
{ COMMON_CHAT_ROLE_TOOL, "<|ifm|im_start|>tool" },
61+
{ COMMON_CHAT_ROLE_SYSTEM, "<|ifm|im_start|>system" },
62+
};
63+
64+
auto has_tools = inputs.tools.is_array() && !inputs.tools.empty();
65+
auto has_response_format = inputs.json_schema.is_object() && !inputs.json_schema.empty();
66+
auto extract_reasoning = inputs.reasoning_format != COMMON_REASONING_FORMAT_NONE;
67+
auto include_grammar = has_response_format || (has_tools && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE);
68+
69+
if (inputs.has_continuation()) {
70+
const auto & msg = inputs.continue_msg;
71+
72+
data.generation_prompt = GEN_PREFIX + THINK_START + "\n" + msg.reasoning_content;
73+
if (inputs.continue_final_message == COMMON_CHAT_CONTINUATION_CONTENT) {
74+
data.generation_prompt += THINK_END + msg.render_content();
75+
}
76+
77+
data.prompt += data.generation_prompt;
78+
}
79+
80+
auto parser = build_chat_peg_parser([&](common_chat_peg_builder & p) {
81+
auto generation_prompt = p.literal(GEN_PREFIX);
82+
83+
auto think_end = p.choice();
84+
for (const auto & tag : data.thinking_end_tags) {
85+
think_end |= p.literal(tag);
86+
}
87+
auto think_body = p.until_one_of(data.thinking_end_tags);
88+
auto think_block = [&](const common_peg_parser & body) {
89+
return p.optional(THINK_START + p.space() + p.ac(body + think_end, data.thinking_end_tags));
90+
};
91+
auto reasoning = extract_reasoning ? think_block(p.reasoning(think_body)) : p.eps();
92+
93+
if (has_response_format) {
94+
// The answer must be bare JSON, so the think block is consumed even when it is not extracted
95+
auto thoughts = extract_reasoning ? reasoning : think_block(think_body);
96+
return generation_prompt + (thoughts << p.content(p.schema(p.json(), "response-format", inputs.json_schema)));
97+
}
98+
99+
if (!has_tools || inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_NONE) {
100+
return generation_prompt + (reasoning << p.content(p.rest()));
101+
}
102+
103+
auto tool_choice = p.choice();
104+
if (call_format == "json") {
105+
tool_choice = p.standard_json_tools(CALL_START, CALL_END, inputs.tools, false, true);
106+
} else {
107+
auto arg_close = p.tool_arg_close(p.literal(ARG_VAL_END));
108+
auto arg_string = p.rule("xml-arg-string", p.ac(p.tool_arg_string_value(p.until(ARG_VAL_END)) + arg_close, ARG_VAL_END));
109+
110+
// The models leave out <ifm|arg_type> even when asked for xml_typed
111+
auto arg_type = call_format == "xml_typed" ? p.optional(ARG_TYPE + p.until(ARG_TYPE_END) + ARG_TYPE_END + p.space()) : p.eps();
112+
113+
foreach_function(inputs.tools, [&](const json & tool) {
114+
const auto & function = tool.at("function");
115+
std::string name = function.at("name");
116+
117+
std::vector<common_peg_parser> required_args;
118+
std::vector<common_peg_parser> optional_args;
119+
foreach_parameter(function, [&](const common_chat_schema_property & param, const common_chat_schema_document_ptr & doc) {
120+
auto rule_name = "tool-" + name + "-arg-" + param.name;
121+
auto types = param.schema->value_types();
122+
auto arg_value = arg_string;
123+
if (!types.has(common_chat_schema::TYPE_STRING)) {
124+
arg_value = p.tool_arg_json_value(p.schema(p.json(), rule_name + "-schema", doc, *param.schema)) + arg_close;
125+
}
126+
if (types.has(common_chat_schema::TYPE_STRING) && !types.is_only(common_chat_schema::TYPE_STRING)) {
127+
// The string alternative accepts any text, so only the parser needs the JSON alternatives.
128+
auto json_value = p.choice();
129+
if (types.has(common_chat_schema::TYPE_OBJECT)) {
130+
json_value |= p.json_object();
131+
}
132+
if (types.has(common_chat_schema::TYPE_ARRAY)) {
133+
json_value |= p.json_array();
134+
}
135+
if (types.has(common_chat_schema::TYPE_NUMBER) || types.has(common_chat_schema::TYPE_INTEGER)) {
136+
json_value |= p.json_number();
137+
}
138+
if (types.has(common_chat_schema::TYPE_BOOLEAN)) {
139+
json_value |= p.json_bool();
140+
}
141+
if (types.has(common_chat_schema::TYPE_NULL)) {
142+
json_value |= p.json_null();
143+
}
144+
arg_value = p.gbnf(p.atomic(p.tool_arg_json_value(json_value) + arg_close) | arg_string, "xml-arg-string");
145+
}
146+
147+
auto arg = p.space() + p.tool_arg(p.tool_arg_open(ARG_KEY + p.tool_arg_name(p.literal(param.name)) + ARG_KEY_END) <<
148+
arg_type + ARG_VAL + arg_value);
149+
(param.required ? required_args : optional_args).push_back(p.rule(rule_name, arg));
150+
});
151+
152+
auto args = p.permute("tool-" + name + "-args", required_args);
153+
if (!optional_args.empty()) {
154+
args = args + p.zero_or_more(p.choice(optional_args));
155+
}
156+
157+
tool_choice |= p.rule("tool-" + name, p.tool(
158+
p.tool_open(CALL_START + p.tool_name(p.literal(name)) + "\n") + p.tool_args(args) << p.tool_close(p.literal(CALL_END))));
159+
});
160+
}
161+
162+
auto required = inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_REQUIRED;
163+
auto calls = inputs.parallel_tool_calls ? tool_choice + p.zero_or_more(p.space() + tool_choice) : tool_choice;
164+
auto tool_calls = p.trigger_rule("tool-calls", p.repeat(SECTION_START << calls << SECTION_END, required ? 1 : 0, 1));
165+
166+
// Keep thinking inline when required calls bypass the content parser.
167+
if (required && !extract_reasoning) {
168+
reasoning = p.content(think_block(think_body));
169+
}
170+
171+
// A required call follows the reasoning directly, the models otherwise keep writing content
172+
auto content = required ? p.eps() : p.content(p.until(SECTION_START));
173+
174+
return generation_prompt + (reasoning << content << tool_calls);
175+
});
176+
177+
data.parser = parser.save();
178+
179+
if (include_grammar) {
180+
data.grammar_lazy = !(has_response_format || inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_REQUIRED);
181+
data.grammar = build_grammar([&](const common_grammar_builder & builder) {
182+
parser.build_grammar(builder, data.grammar_lazy);
183+
});
184+
185+
if (data.grammar_lazy) {
186+
data.grammar_triggers = {
187+
{ COMMON_GRAMMAR_TRIGGER_TYPE_WORD, SECTION_START },
188+
};
189+
}
190+
}
191+
192+
return data;
193+
}

‎common/parsers/parsers.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ common_chat_params common_chat_params_init_gigachat_v3(const common_chat_templat
5959

6060
common_chat_params common_chat_params_init_gpt_oss(const common_chat_template & tmpl, const autoparser::generation_params & inputs);
6161

62+
common_chat_params common_chat_params_init_k2_horizon(const common_chat_template & tmpl, const autoparser::generation_params & inputs);
63+
6264
common_chat_params common_chat_params_init_kimi_k2(const common_chat_template & tmpl, const autoparser::generation_params & inputs);
6365

6466
common_chat_params common_chat_params_init_kimi_k3(const common_chat_template & tmpl, const autoparser::generation_params & inputs);

‎common/parsers/sources.cmake‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(LLAMA_CHAT_PARSERS_SOURCES
99
${CMAKE_CURRENT_LIST_DIR}/gemma4.cpp
1010
${CMAKE_CURRENT_LIST_DIR}/gigachat-v3.cpp
1111
${CMAKE_CURRENT_LIST_DIR}/gpt-oss.cpp
12+
${CMAKE_CURRENT_LIST_DIR}/k2-horizon.cpp
1213
${CMAKE_CURRENT_LIST_DIR}/kimi-k2.cpp
1314
${CMAKE_CURRENT_LIST_DIR}/kimi-k3.cpp
1415
${CMAKE_CURRENT_LIST_DIR}/ling3.cpp

‎conversion/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"CohereForCausalLM": "command_r",
5151
"DbrxForCausalLM": "dbrx",
5252
"DeciLMForCausalLM": "deci",
53+
"PplxDeciderModel": "pplx_decider",
5354
"DeepseekForCausalLM": "deepseek",
5455
"DeepseekOCRForCausalLM": "deepseek",
5556
"DeepseekV2ForCausalLM": "deepseek",
@@ -73,6 +74,7 @@
7374
"Dots3NoteTextForCausalLM": "dots3",
7475
"DotsOCRForCausalLM": "qwen",
7576
"DreamModel": "dream",
77+
"EmbeddingGemma2Model": "gemma",
7678
"Ernie4_5ForCausalLM": "ernie",
7779
"Ernie4_5_ForCausalLM": "ernie",
7880
"Ernie4_5_MoeForCausalLM": "ernie",
@@ -140,6 +142,7 @@
140142
"JinaBertForMaskedLM": "bert",
141143
"JinaBertModel": "bert",
142144
"JinaEmbeddingsV5Model": "bert",
145+
"K2HorizonForCausalLM": "k2_horizon",
143146
"KORMoForCausalLM": "qwen",
144147
"KimiK25ForConditionalGeneration": "deepseek",
145148
"KimiK3ForConditionalGeneration": "kimi_k3",
@@ -300,12 +303,14 @@
300303
"AudioFlamingo3ForConditionalGeneration": "ultravox",
301304
"ClefModel": "clef",
302305
"CogVLMForCausalLM": "cogvlm",
306+
"PplxDeciderModel": "pplx_decider",
303307
"DeepseekOCR2ForCausalLM": "deepseek",
304308
"DeepseekOCRForCausalLM": "deepseek",
305309
"DeepseekV4ForCausalLM": "deepseek",
306310
"Dots3NoteForCausalLM": "dots3",
307311
"Dots3NoteForConditionalGeneration": "dots3",
308312
"DotsOCRForCausalLM": "dotsocr",
313+
"EmbeddingGemma2Model": "gemma",
309314
"Exaone4_5_ForConditionalGeneration": "exaone",
310315
"Gemma3ForConditionalGeneration": "gemma",
311316
"Gemma3nForConditionalGeneration": "gemma",

0 commit comments

Comments
 (0)