Skip to content

Commit 8a71eb0

Browse files
committed
Merge branch 'upstream' into concedo_experimental
# Conflicts: # .github/workflows/build.yml # ggml/cmake/ggml-config.cmake.in # ggml/src/ggml-cann/CMakeLists.txt # ggml/src/ggml-cann/common.h # ggml/src/ggml-cann/ggml-cann.cpp # ggml/src/ggml-cuda/fattn.cu # ggml/src/ggml-opencl/CMakeLists.txt # ggml/src/ggml-opencl/ggml-opencl.cpp # requirements/requirements-convert_hf_to_gguf.txt # scripts/compare-llama-bench.py # tests/test-chat-template.cpp # tests/test-chat.cpp # tools/llama-bench/llama-bench.cpp
2 parents 338b1fe + 9a96389 commit 8a71eb0

File tree

29 files changed

+1350
-291
lines changed

29 files changed

+1350
-291
lines changed

common/chat-parser.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ bool common_chat_msg_parser::add_tool_call(const std::string & name, const std::
5555
bool common_chat_msg_parser::add_tool_call(const json & tool_call) {
5656
std::string name = tool_call.contains("name") ? tool_call.at("name") : "";
5757
std::string id = tool_call.contains("id") ? tool_call.at("id") : "";
58-
std::string arguments = tool_call.contains("arguments") ? tool_call.at("arguments") : "";
58+
std::string arguments = "";
59+
if (tool_call.contains("arguments")) {
60+
if (tool_call.at("arguments").is_object()) {
61+
arguments = tool_call.at("arguments").dump();
62+
} else {
63+
arguments = tool_call.at("arguments");
64+
}
65+
}
66+
5967
return add_tool_call(name, id, arguments);
6068
}
6169

common/chat.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ const char * common_chat_format_name(common_chat_format format) {
606606
case COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1: return "Functionary v3.1 Llama 3.1";
607607
case COMMON_CHAT_FORMAT_HERMES_2_PRO: return "Hermes 2 Pro";
608608
case COMMON_CHAT_FORMAT_COMMAND_R7B: return "Command R7B";
609+
case COMMON_CHAT_FORMAT_GRANITE: return "Granite";
609610
case COMMON_CHAT_FORMAT_GPT_OSS: return "GPT-OSS";
610611
default:
611612
throw std::runtime_error("Unknown chat format");
@@ -618,6 +619,7 @@ const char * common_reasoning_format_name(common_reasoning_format format) {
618619
case COMMON_REASONING_FORMAT_AUTO: return "auto";
619620
case COMMON_REASONING_FORMAT_DEEPSEEK: return "deepseek";
620621
case COMMON_REASONING_FORMAT_DEEPSEEK_LEGACY: return "deepseek-legacy";
622+
case COMMON_REASONING_FORMAT_GRANITE: return "granite";
621623
default:
622624
throw std::runtime_error("Unknown reasoning format");
623625
}
@@ -1734,6 +1736,124 @@ static void common_chat_parse_hermes_2_pro(common_chat_msg_parser & builder) {
17341736
builder.add_content(builder.consume_rest());
17351737
}
17361738

1739+
static common_chat_params common_chat_params_init_granite(const common_chat_template & tmpl, const struct templates_params & inputs) {
1740+
common_chat_params data;
1741+
1742+
// Pass thinking context for Granite template
1743+
json additional_context = {
1744+
{"thinking", inputs.enable_thinking},
1745+
};
1746+
1747+
data.prompt = apply(tmpl, inputs, /* messages_override= */ std::nullopt, /* tools_override= */ std::nullopt, additional_context);
1748+
data.format = COMMON_CHAT_FORMAT_GRANITE;
1749+
1750+
if (string_ends_with(data.prompt, "<think>\n") || string_ends_with(data.prompt, "<think>")) {
1751+
if (!inputs.enable_thinking) {
1752+
data.prompt += "</think>";
1753+
} else {
1754+
data.thinking_forced_open = true;
1755+
}
1756+
}
1757+
1758+
if (!inputs.tools.is_null()) {
1759+
// Granite uses <|tool_call|> followed by JSON list
1760+
data.grammar_lazy = inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_REQUIRED;
1761+
data.grammar = build_grammar([&](const common_grammar_builder & builder) {
1762+
std::vector<std::string> tool_rules;
1763+
foreach_function(inputs.tools, [&](const json & tool) {
1764+
const auto & function = tool.at("function");
1765+
std::string name = function.at("name");
1766+
auto parameters = function.at("parameters");
1767+
builder.resolve_refs(parameters);
1768+
tool_rules.push_back(builder.add_rule(name + "-call", builder.add_schema(name +
1769+
"-args", {
1770+
{"type", "object"},
1771+
{"properties", {
1772+
{"name", {{"const", name}}},
1773+
{"arguments", parameters},
1774+
}},
1775+
{"required", json::array({"name", "arguments"})},
1776+
})));
1777+
});
1778+
1779+
auto tool_call = builder.add_rule("tool_call", string_join(tool_rules, " | "));
1780+
auto tool_list = builder.add_rule("tool_list", "\"[\" space " + tool_call + " (\",\" space " + tool_call + ")* space \"]\"");
1781+
1782+
if (data.thinking_forced_open) {
1783+
builder.add_rule("root", "\"</think>\" space \"<response>\" space [^<]* \"</response>\" space \"<|tool_call|>\" space " + tool_list);
1784+
} else {
1785+
builder.add_rule("root", "\"<|tool_call|>\" space " + tool_list);
1786+
}
1787+
1788+
data.grammar_triggers.push_back({
1789+
COMMON_GRAMMAR_TRIGGER_TYPE_WORD,
1790+
"<|tool_call|>"
1791+
});
1792+
1793+
data.preserved_tokens = {
1794+
"<think>",
1795+
"</think>",
1796+
"<response>",
1797+
"</response>",
1798+
"<|tool_call|>",
1799+
};
1800+
});
1801+
} else {
1802+
// Handle thinking tags for non-tool responses
1803+
if (data.thinking_forced_open && inputs.enable_thinking) {
1804+
data.grammar_lazy = false;
1805+
data.grammar = build_grammar([&](const common_grammar_builder & builder) {
1806+
builder.add_rule("root", "\"</think>\" space \"<response>\" space .* \"</response>\" space");
1807+
});
1808+
data.preserved_tokens = {
1809+
"<think>",
1810+
"</think>",
1811+
"<response>",
1812+
"</response>",
1813+
};
1814+
}
1815+
}
1816+
1817+
return data;
1818+
}
1819+
1820+
static void common_chat_parse_granite(common_chat_msg_parser & builder) {
1821+
// Parse thinking tags
1822+
builder.try_parse_reasoning("<think>", "</think>");
1823+
1824+
// Parse response tags using regex
1825+
static const common_regex response_regex("<response>([\\s\\S]*?)</response>");
1826+
if (auto res = builder.try_find_regex(response_regex)) {
1827+
// Extract the content between the tags (capture group 1)
1828+
auto content = builder.str(res->groups[1]);
1829+
builder.add_content(content);
1830+
builder.move_to(res->groups[0].end);
1831+
}
1832+
1833+
if (!builder.syntax().parse_tool_calls) {
1834+
builder.add_content(builder.consume_rest());
1835+
return;
1836+
}
1837+
1838+
// Look for tool calls
1839+
static const common_regex tool_call_regex(regex_escape("<|tool_call|>"));
1840+
if (auto res = builder.try_find_regex(tool_call_regex)) {
1841+
builder.move_to(res->groups[0].end);
1842+
1843+
// Expect JSON array of tool calls
1844+
auto tool_calls_data = builder.consume_json();
1845+
if (tool_calls_data.json.is_array()) {
1846+
if (!builder.add_tool_calls(tool_calls_data.json)) {
1847+
builder.add_content("<|tool_call|>" + tool_calls_data.json.dump());
1848+
}
1849+
} else {
1850+
builder.add_content("<|tool_call|>" + tool_calls_data.json.dump());
1851+
}
1852+
} else {
1853+
builder.add_content(builder.consume_rest());
1854+
}
1855+
}
1856+
17371857
static common_chat_params common_chat_params_init_without_tools(const common_chat_template & tmpl, const struct templates_params & inputs) {
17381858
common_chat_params data;
17391859
data.prompt = apply(tmpl, inputs);
@@ -1805,6 +1925,11 @@ static common_chat_params common_chat_templates_apply_jinja(
18051925
return common_chat_params_init_command_r7b(tmpl, params);
18061926
}
18071927

1928+
// Granite (IBM) - detects thinking / tools support
1929+
if (src.find("elif thinking") != std::string::npos && src.find("<|tool_call|>") != std::string::npos) {
1930+
return common_chat_params_init_granite(tmpl, params);
1931+
}
1932+
18081933
// Hermes 2/3 Pro, Qwen 2.5 Instruct (w/ tools)
18091934
if (src.find("<tool_call>") != std::string::npos && params.json_schema.is_null()) {
18101935
return common_chat_params_init_hermes_2_pro(tmpl, params);
@@ -1865,6 +1990,7 @@ static common_chat_params common_chat_templates_apply_legacy(
18651990
int alloc_size = 0;
18661991
std::vector<llama_chat_message> chat;
18671992
std::vector<std::string> contents;
1993+
18681994
for (const auto & msg : inputs.messages) {
18691995
auto content = msg.content;
18701996
for (const auto & part : msg.content_parts) {
@@ -1966,6 +2092,9 @@ static void common_chat_parse(common_chat_msg_parser & builder) {
19662092
case COMMON_CHAT_FORMAT_COMMAND_R7B:
19672093
common_chat_parse_command_r7b(builder);
19682094
break;
2095+
case COMMON_CHAT_FORMAT_GRANITE:
2096+
common_chat_parse_granite(builder);
2097+
break;
19692098
case COMMON_CHAT_FORMAT_GPT_OSS:
19702099
common_chat_parse_gpt_oss(builder);
19712100
break;

common/chat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ enum common_chat_format {
109109
COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1,
110110
COMMON_CHAT_FORMAT_HERMES_2_PRO,
111111
COMMON_CHAT_FORMAT_COMMAND_R7B,
112+
COMMON_CHAT_FORMAT_GRANITE,
112113
COMMON_CHAT_FORMAT_GPT_OSS,
113114

114115
COMMON_CHAT_FORMAT_COUNT, // Not a format, just the # formats

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ enum common_reasoning_format {
235235
COMMON_REASONING_FORMAT_AUTO,
236236
COMMON_REASONING_FORMAT_DEEPSEEK_LEGACY, // Extract thinking tag contents and return as `message.reasoning_content`, or leave inline in <think> tags in stream mode
237237
COMMON_REASONING_FORMAT_DEEPSEEK, // Extract thinking tag contents and return as `message.reasoning_content`, including in streaming deltas.
238+
COMMON_REASONING_FORMAT_GRANITE, // Extract thinking tag contents and return as `message.reasoning_content`, including in streaming deltas.
238239
};
239240

240241
struct common_params {

ggml/src/ggml-backend.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,11 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
10771077
}
10781078
}
10791079
}
1080+
// if the node is still unassigned, assign it to the first backend that supports it
1081+
for (int b = 0; b < sched->n_backends && *cur_backend_id == -1; b++) {
1082+
ggml_backend_sched_set_if_supported(sched, node, b, cur_backend_id);
1083+
}
1084+
GGML_ASSERT(*cur_backend_id != -1);
10801085
}
10811086

10821087
// pass 5: split graph, find tensors that need to be copied
@@ -1104,7 +1109,7 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
11041109

11051110
const int node_backend_id = tensor_backend_id(node);
11061111

1107-
assert(node_backend_id != -1); // all nodes should be assigned by now, this can happen if there is no CPU fallback
1112+
GGML_ASSERT(node_backend_id != -1); // all nodes should be assigned by now, this can happen if there is no CPU fallback
11081113

11091114
// check if we should start a new split based on the sources of the current node
11101115
bool need_new_split = false;
@@ -1162,7 +1167,7 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
11621167

11631168
size_t src_id = hash_id(src);
11641169
const int src_backend_id = sched->hv_tensor_backend_ids[src_id];
1165-
assert(src_backend_id != -1); // all inputs should be assigned by now
1170+
GGML_ASSERT(src_backend_id != -1); // all inputs should be assigned by now
11661171

11671172
if (src->flags & GGML_TENSOR_FLAG_INPUT && sched->n_copies > 1) {
11681173
if (tensor_id_copy(src_id, src_backend_id, 0) == NULL) {

ggml/src/ggml-cpu/ggml-cpu.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
// ggml-backend interface
3737

38-
std::vector<ggml_backend_buffer_type_t>& ggml_backend_cpu_get_extra_buffers_type() {
38+
std::vector<ggml_backend_buffer_type_t> & ggml_backend_cpu_get_extra_buffer_types() {
3939
static std::vector<ggml_backend_buffer_type_t> bufts = []() {
4040
std::vector<ggml_backend_buffer_type_t> bufts;
4141

@@ -57,23 +57,27 @@ std::vector<ggml_backend_buffer_type_t>& ggml_backend_cpu_get_extra_buffers_type
5757
}
5858
#endif
5959

60-
bufts.push_back(NULL);
61-
6260
return bufts;
6361
}();
6462

6563
return bufts;
6664
}
6765

6866
static ggml_backend_buffer_type_t * ggml_backend_cpu_device_get_extra_buffers_type(ggml_backend_dev_t device) {
69-
return ggml_backend_cpu_get_extra_buffers_type().data();
67+
static std::vector<ggml_backend_buffer_type_t> extra_bufts = [] {
68+
std::vector<ggml_backend_buffer_type_t> bufts = ggml_backend_cpu_get_extra_buffer_types();
69+
bufts.push_back(nullptr);
70+
return bufts;
71+
}();
72+
73+
return extra_bufts.data();
7074

7175
GGML_UNUSED(device);
7276
}
7377

7478
static bool ggml_backend_cpu_is_extra_buffer_type(ggml_backend_buffer_type_t buft) {
75-
for (auto * extra : ggml_backend_cpu_get_extra_buffers_type()) {
76-
if (extra && extra == buft) {
79+
for (auto * extra : ggml_backend_cpu_get_extra_buffer_types()) {
80+
if (extra == buft) {
7781
return true;
7882
}
7983
}
@@ -397,20 +401,13 @@ static bool ggml_backend_cpu_device_supports_op(ggml_backend_dev_t dev, const st
397401
return true;
398402
}
399403

400-
// extra_buffer_op?
401-
for (auto extra : ggml_backend_cpu_get_extra_buffers_type()) {
402-
if (extra) {
403-
auto buf_extra = (ggml::cpu::extra_buffer_type*) extra->context;
404-
if (buf_extra && buf_extra->supports_op(dev, op)) {
405-
return true;
406-
}
407-
}
408-
}
409-
410-
// the other case need host buffer.
411-
for (int i = 0; i < GGML_MAX_SRC; i++) {
412-
if (op->src[i] && op->src[i]->buffer && !ggml_backend_buft_is_host(op->src[i]->buffer->buft)) {
413-
return false;
404+
// check extra buffer types
405+
// note: only the first sources are checked for extra buffer types to reduce overhead, increase if necessary
406+
for (int i = 0; i < 4; i++) {
407+
if (op->src[i] && op->src[i]->buffer &&
408+
ggml_backend_cpu_is_extra_buffer_type(op->src[i]->buffer->buft)) {
409+
auto * buf_extra = (ggml::cpu::extra_buffer_type *) op->src[i]->buffer->buft->context;
410+
return buf_extra->supports_op(dev, op);
414411
}
415412
}
416413

ggml/src/ggml-cpu/traits.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extra_buffer_type::~extra_buffer_type() {}
1010
} // namespace ggml::cpu
1111

1212
bool ggml_cpu_extra_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * op) {
13-
for (auto extra : ggml_backend_cpu_get_extra_buffers_type()) {
13+
for (auto extra : ggml_backend_cpu_get_extra_buffer_types()) {
1414
if (extra && extra->context) {
1515
auto buf_extra = (ggml::cpu::extra_buffer_type *) extra->context;
1616
auto tensor_traits = buf_extra->get_tensor_traits(op);
@@ -23,7 +23,7 @@ bool ggml_cpu_extra_compute_forward(struct ggml_compute_params * params, struct
2323
}
2424

2525
bool ggml_cpu_extra_work_size(int n_threads, const struct ggml_tensor * op, size_t * size) {
26-
for (auto extra : ggml_backend_cpu_get_extra_buffers_type()) {
26+
for (auto extra : ggml_backend_cpu_get_extra_buffer_types()) {
2727
if (extra && extra->context) {
2828
auto buf_extra = (ggml::cpu::extra_buffer_type *) extra->context;
2929
auto tensor_traits = buf_extra->get_tensor_traits(op);

ggml/src/ggml-cpu/traits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ class extra_buffer_type {
3333
} // namespace ggml::cpu
3434

3535
// implemented in ggml-cpu.cpp.
36-
std::vector<ggml_backend_buffer_type_t> & ggml_backend_cpu_get_extra_buffers_type();
36+
std::vector<ggml_backend_buffer_type_t> & ggml_backend_cpu_get_extra_buffer_types();
3737

3838
#endif

ggml/src/ggml-cuda/common.cuh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,13 @@ typedef float2 dfloat2;
237237
#endif // defined(GGML_USE_HIP) && defined(CDNA) && !defined(GGML_HIP_NO_MMQ_MFMA)
238238

239239
#if !defined(GGML_USE_HIP) && __CUDA_ARCH__ >= GGML_CUDA_CC_TURING
240-
#define NEW_MMA_AVAILABLE
240+
#define TURING_MMA_AVAILABLE
241241
#endif // !defined(GGML_USE_HIP) && __CUDA_ARCH__ >= GGML_CUDA_CC_TURING
242242

243+
#if !defined(GGML_USE_HIP) && __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE
244+
#define AMPERE_MMA_AVAILABLE
245+
#endif // !defined(GGML_USE_HIP) && __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE
246+
243247
#if !defined(GGML_USE_HIP) && __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE
244248
#define CP_ASYNC_AVAILABLE
245249
#endif // !defined(GGML_USE_HIP) && __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE
@@ -307,10 +311,14 @@ static bool amd_mfma_available(const int cc) {
307311
}
308312

309313
// Volta technically had FP16 tensor cores but they work very differently compared to Turing and later.
310-
static bool new_mma_available(const int cc) {
314+
static bool turing_mma_available(const int cc) {
311315
return GGML_CUDA_CC_IS_NVIDIA(cc) && ggml_cuda_highest_compiled_arch(cc) >= GGML_CUDA_CC_TURING;
312316
}
313317

318+
static bool ampere_mma_available(const int cc) {
319+
return cc < GGML_CUDA_CC_OFFSET_AMD && ggml_cuda_highest_compiled_arch(cc) >= GGML_CUDA_CC_AMPERE;
320+
}
321+
314322
static bool cp_async_available(const int cc) {
315323
return cc < GGML_CUDA_CC_OFFSET_AMD && ggml_cuda_highest_compiled_arch(cc) >= GGML_CUDA_CC_AMPERE;
316324
}

0 commit comments

Comments
 (0)