Compare commits

...
Author SHA1 Message Date
jaredmeaux f16e79da89 chat : Allow K2 Horizon tool call during thought (#32993)
Allow tool calls that would otherwise appear inside reasoning/thinking blocks for K2 Horizon.

K2 Horizon models can emit tool calls without closing the thinking block. Without this
fix the parser would only stop on explicit end tag, and the tool call was lost without
executing. With this PR we now end reasoning before the tool call starts.
In short, when reasoning mentions `<ifm|tool_calls>`, we end reasoning there.

My tests cover this case at medium and high effort.

This fixes #32972, and is the same pattern as #32232 (Gemma4) and #32271 (Ministral-3).

- I have read and agree with the [contributing guidelines](https://github.com/ggml-org/llama.cpp/blob/master/CONTRIBUTING.md)

I have used Opus to run and score benchmarks, help find the K2 parser, draft
the fix and test, and see how others have resolved this for other models. I have carefully
examined all changes and tested with my local benchmarks.

I have used Sol to code review twice.

Assisted-by: Claude Opus 5.5
Assisted-by: GPT 6.2 Sol
2026-11-28 12:22:49 +01:00
Pascal 0c2209c88b tests : use the fused mul_mat tolerance for LOOP_CTRL (#33238) 2026-11-28 11:49:47 +01:00
anujj 2881150433 model : expand Inkling 2 tau after embeddings (#33186)
* model : inkling2 expand tau after the embeddings

* model : build inkling2 embeddings before tau
2026-11-28 11:09:28 +01:00
Johannes Gäßler 786da85942 CUDA: consistent ncols2 selection for mma FA (#32626) 2026-11-28 08:57:29 +01:00
5 changed files with 83 additions and 78 deletions
+7 -2
View File
@@ -71,6 +71,10 @@ common_chat_params common_chat_params_init_k2_horizon(const common_chat_template
auto extract_reasoning = inputs.reasoning_format != COMMON_REASONING_FORMAT_NONE;
auto include_grammar = has_response_format || (has_tools && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE);
// A tool call may start before the thought is closed, the real closers stay first, the budget sampler forces them
const auto think_closers = data.thinking_end_tags;
data.thinking_end_tags.push_back(SECTION_START);
if (inputs.has_continuation()) {
const auto & msg = inputs.continue_msg;
@@ -86,12 +90,13 @@ common_chat_params common_chat_params_init_k2_horizon(const common_chat_template
auto generation_prompt = p.literal(GEN_PREFIX);
auto think_end = p.choice();
for (const auto & tag : data.thinking_end_tags) {
for (const auto & tag : think_closers) {
think_end |= p.literal(tag);
}
auto think_body = p.until_one_of(data.thinking_end_tags);
auto think_block = [&](const common_peg_parser & body) {
return p.optional(THINK_START + p.space() + p.through(body, think_end));
// Peek only, because the tool call rule will consume <ifm|tool_calls>
return p.optional(THINK_START + p.space() + body + (think_end | p.peek(p.literal(SECTION_START))));
};
auto reasoning = extract_reasoning ? think_block(p.reasoning(think_body)) : p.eps();
+49 -70
View File
@@ -198,9 +198,7 @@ static void ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1(ggml_backend_cuda_con
ggml_cuda_flash_attn_ext_mma_f16_case<DKQ, DV, 64/ncols2, ncols2, type_K, type_V>(ctx, dst);
}
template <int DKQ, int DV, ggml_type type_K, ggml_type type_V>
static void ggml_cuda_flash_attn_ext_mma_f16_switch_ncols2(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc;
static int ggml_cuda_flash_attn_ext_mma_f16_get_ncols2(const int cc, const ggml_tensor * dst) {
const ggml_tensor * KQV = dst;
const ggml_tensor * Q = dst->src[0];
const ggml_tensor * K = dst->src[1];
@@ -230,71 +228,56 @@ static void ggml_cuda_flash_attn_ext_mma_f16_switch_ncols2(ggml_backend_cuda_con
}
}
if (!use_gqa_opt) {
return 1;
}
GGML_ASSERT(Q->ne[2] % K->ne[2] == 0);
const int gqa_ratio = Q->ne[2] / K->ne[2];
// On Volta the GQA optimizations aren't as impactful vs. minimizing wasted compute:
if (cc == GGML_CUDA_CC_VOLTA) {
if (use_gqa_opt && gqa_ratio % 8 == 0) {
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 8, type_K, type_V>(ctx, dst);
return;
}
const int ncols2_max = Q->ne[0] == 320 ? 32 : ((Q->ne[0] == 576 || Q->ne[0] == 192) ? 16 : 8);
if (use_gqa_opt && gqa_ratio % 4 == 0) {
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 4, type_K, type_V>(ctx, dst);
return;
}
if constexpr (DKQ <= 256) {
if (use_gqa_opt && gqa_ratio % 2 == 0) {
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 2, type_K, type_V>(ctx, dst);
return;
}
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 1, type_K, type_V>(ctx, dst);
return;
} else {
GGML_ABORT("fatal error");
// On Volta and RDNA it is preferable to minimize wasted compute vs. duplicate I/O for the mask:
if (cc == GGML_CUDA_CC_VOLTA || (amd_wmma_available(cc) && gqa_ratio % 2 == 0)) {
int ncols2 = 1;
while (gqa_ratio % (2*ncols2) == 0 && ncols2 < ncols2_max) {
ncols2 *= 2;
}
return ncols2;
}
// On RDNA it is preferable to minimize wasted compute vs. duplicate I/O for the mask.
if (amd_wmma_available(cc)) {
if (use_gqa_opt && gqa_ratio % 8 == 0) {
int ncols2 = 1;
while (gqa_ratio > ncols2 && ncols2 < ncols2_max) {
ncols2 *= 2;
}
return ncols2;
}
template <int DKQ, int DV, ggml_type type_K, ggml_type type_V>
static void ggml_cuda_flash_attn_ext_mma_f16_switch_ncols2(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
const int cc = ggml_cuda_info().devices[ggml_cuda_get_device()].cc;
const int ncols2 = ggml_cuda_flash_attn_ext_mma_f16_get_ncols2(cc, dst);
switch (ncols2) {
case 8:
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 8, type_K, type_V>(ctx, dst);
return;
}
if (use_gqa_opt && gqa_ratio % 4 == 0) {
break;
case 4:
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 4, type_K, type_V>(ctx, dst);
return;
}
if (use_gqa_opt && gqa_ratio % 2 == 0) {
break;
case 2:
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 2, type_K, type_V>(ctx, dst);
return;
}
}
if (use_gqa_opt && gqa_ratio > 4) {
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 8, type_K, type_V>(ctx, dst);
return;
}
if (use_gqa_opt && gqa_ratio > 2) {
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 4, type_K, type_V>(ctx, dst);
return;
}
if (use_gqa_opt && gqa_ratio > 1) {
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 2, type_K, type_V>(ctx, dst);
return;
}
if constexpr (DKQ <= 256) {
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 1, type_K, type_V>(ctx, dst);
} else {
GGML_ABORT("fatal error");
break;
case 1:
if constexpr (DKQ <= 256) {
ggml_cuda_flash_attn_ext_mma_f16_switch_ncols1<DKQ, DV, 1, type_K, type_V>(ctx, dst);
break;
} else {
GGML_ABORT("fatal error");
}
default:
GGML_ABORT("fatal error");
}
}
@@ -757,20 +740,16 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
return BEST_FATTN_KERNEL_MMA_F16;
}
const int ncols2_max = Q->ne[0] == 320 ? 32 : ((Q->ne[0] == 576 || Q->ne[0] == 192) ? 16 : 8);
int gqa_ratio_eff = 1;
while (max_bias == 0.0f && (!mask || mask->ne[2] == 1) && gqa_ratio % (2*gqa_ratio_eff) == 0 && gqa_ratio_eff < ncols2_max) {
gqa_ratio_eff *= 2;
}
const int ncols2 = ggml_cuda_flash_attn_ext_mma_f16_get_ncols2(cc, dst);
// the tile kernel for DKQ=576 only has the ncols2 = 4 and 16 variants
const best_fattn_kernel tile_kernel = Q->ne[0] == 576 && gqa_ratio % 4 != 0 ? BEST_FATTN_KERNEL_NONE : BEST_FATTN_KERNEL_TILE;
if (volta_mma_available(cc) && Q->ne[0] != 40 && Q->ne[0] != 72) {
if (can_use_vector_kernel && Q->ne[1] * gqa_ratio_eff <= 2) {
if (can_use_vector_kernel && Q->ne[1] * ncols2 <= 2) {
return BEST_FATTN_KERNEL_VEC;
}
if (Q->ne[1] * gqa_ratio_eff <= 16) {
if (Q->ne[1] * ncols2 <= 16) {
return tile_kernel; // On Volta tensor cores are only faster for sufficiently large matrices.
}
return BEST_FATTN_KERNEL_MMA_F16;
@@ -783,23 +762,23 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
// AMD MFMA needs a certain minimum batch size to outscale the tile kernel for large head sizes.
if (amd_mfma_available(cc) && Q->ne[0] != 40 && Q->ne[0] != 72) {
if ((Q->ne[0] <= 64 && Q->ne[1] * gqa_ratio_eff > 8)) {
if ((Q->ne[0] <= 64 && Q->ne[1] * ncols2 > 8)) {
return BEST_FATTN_KERNEL_MMA_F16;
}
if ((Q->ne[0] <= 128 && Q->ne[1] * gqa_ratio_eff > 16)) {
if ((Q->ne[0] <= 128 && Q->ne[1] * ncols2 > 16)) {
return BEST_FATTN_KERNEL_MMA_F16;
}
if ((Q->ne[0] <= 256 && Q->ne[1] * gqa_ratio_eff > 64)) {
if ((Q->ne[0] <= 256 && Q->ne[1] * ncols2 > 64)) {
return BEST_FATTN_KERNEL_MMA_F16;
}
if (Q->ne[0] > 256 && gqa_opt_applies && Q->ne[1] * gqa_ratio_eff > 128) {
if (Q->ne[0] > 256 && gqa_opt_applies && Q->ne[1] * ncols2 > 128) {
return BEST_FATTN_KERNEL_MMA_F16;
}
}
// AMD WMMA is faster than the tile kernel if the wide tiles with high arithmetic intensity can be utilized.
if ((amd_wmma_available(cc) && gqa_opt_applies && Q->ne[0] <= 256) && Q->ne[0] != 40 && Q->ne[0] != 72 &&
Q->ne[1] * gqa_ratio_eff > (Q->ne[0] <= 128 ? 8 : 16)) {
Q->ne[1] * ncols2 > (Q->ne[0] <= 128 ? 8 : 16)) {
return BEST_FATTN_KERNEL_MMA_F16;
}
+6 -6
View File
@@ -279,12 +279,6 @@ ggml_tensor * llama_model_inkling2::graph::build_inputs() {
ggml_tensor * inp_pos = build_inp_pos();
inp_out_ids = build_inp_out_ids();
// log-scaled positions: 1 + alpha*log(max(1, (pos + 1)/floor)), multiplies q and r of the full-attention layers
tau = ggml_scale_bias(ctx0, ggml_cast(ctx0, inp_pos, GGML_TYPE_F32), 1.0f, 1.0f);
tau = ggml_log(ctx0, ggml_clamp(ctx0, ggml_scale(ctx0, tau, 1.0f/hparams.n_log_scale_floor), 1.0f, INFINITY));
tau = ggml_reshape_3d(ctx0, ggml_scale_bias(ctx0, tau, hparams.f_log_scale_alpha, 1.0f), 1, 1, n_tokens);
ggml_build_forward_expand(gf, tau);
ggml_tensor * cur = build_inp_embd(model.tok_embd);
// important: do not normalize raw embeddings input (i.e. encoded image embeddings)
@@ -292,6 +286,12 @@ ggml_tensor * llama_model_inkling2::graph::build_inputs() {
cur = build_norm(cur, model.tok_norm, nullptr, LLM_NORM_RMS, -1);
}
// log-scaled positions: 1 + alpha*log(max(1, (pos + 1)/floor)), multiplies q and r of the full-attention layers
tau = ggml_scale_bias(ctx0, ggml_cast(ctx0, inp_pos, GGML_TYPE_F32), 1.0f, 1.0f);
tau = ggml_log(ctx0, ggml_clamp(ctx0, ggml_scale(ctx0, tau, 1.0f/hparams.n_log_scale_floor), 1.0f, INFINITY));
tau = ggml_reshape_3d(ctx0, ggml_scale_bias(ctx0, tau, hparams.f_log_scale_alpha, 1.0f), 1, 1, n_tokens);
ggml_build_forward_expand(gf, tau);
return cur;
}
+5
View File
@@ -5101,6 +5101,10 @@ struct test_loop_ctrl : public test_case {
test_loop_ctrl(int64_t n = 2304, int64_t r = 64, int64_t T = 1) : n(n), r(r), T(T) {}
double max_nmse_err() override {
return 5e-3;
}
ggml_tensor * build_graph(ggml_context * ctx) override {
ggml_tensor * cur = ggml_scale(ctx, ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n, T), 1.0f);
ggml_tensor * msg = ggml_scale(ctx, ggml_new_tensor_2d(ctx, GGML_TYPE_F32, n, T), 1.0f);
@@ -15040,6 +15044,7 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {2, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 4, {4, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
test_cases.emplace_back(new test_flash_attn_ext(256, 256, 2, {12, 1}, 4096, 512, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
test_cases.emplace_back(new test_flash_attn_ext(128, 128, 4, { 3, 1}, 512, 32, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_F16, GGML_TYPE_F16));
// dense-allocated (non-view) quant K/V at batch >= 64, in cache and native layouts
test_cases.emplace_back(new test_flash_attn_ext(64, 64, 4, {1, 1}, 512, 75, true, false, 0, 0, GGML_PREC_F32, GGML_TYPE_Q8_0, GGML_TYPE_Q8_0, {0, 2, 1, 3}, false));
+16
View File
@@ -5601,6 +5601,22 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
.expect(message_assist_call_thoughts)
.run();
// The models sometimes open the tool call section without closing the reasoning at all
for (const char * effort : { R"("medium")", R"("high")" }) {
tst.test(
"I'm\nthinking<ifm|tool_calls>\n"
"<ifm|tool_call>special_function\n"
"<ifm|arg_key>arg1</ifm|arg_key>\n"
"<ifm|arg_value>1</ifm|arg_value>\n"
"</ifm|tool_call>\n"
"</ifm|tool_calls>")
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
.tools({ special_function_tool })
.chat_template_kwargs({ { "reasoning_effort", effort } })
.expect(message_assist_call_thoughts)
.run();
}
tst.test(
"I'm\nthinking</ifm|think><ifm|tool_calls>\n"
"<ifm|tool_call>{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}</ifm|tool_call>\n"