LLVMFuzzerTestOneInput:
   18|  1.64k|extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   19|  1.64k|  FuzzedDataProvider fuzzed_data(data, size);
   20|       |
   21|  1.64k|  std::string p1 = fuzzed_data.ConsumeRandomLengthString();
   22|  1.64k|  std::string p2 = fuzzed_data.ConsumeRandomLengthString();
   23|  1.64k|  std::string p3 = fuzzed_data.ConsumeRandomLengthString();
   24|  1.64k|  std::string p4 = fuzzed_data.ConsumeRandomLengthString();
   25|  1.64k|  std::string p5 = fuzzed_data.ConsumeRandomLengthString();
   26|  1.64k|  std::string p6 = fuzzed_data.ConsumeRandomLengthString();
   27|  1.64k|  std::string p7 = fuzzed_data.ConsumeRandomLengthString();
   28|       |
   29|  1.64k|  llama_chat_message conversation[] = {
   30|  1.64k|      {"system", p2.c_str()},    {"user", p3.c_str()},
   31|  1.64k|      {"assistant", p4.c_str()}, {"user", p5.c_str()},
   32|  1.64k|      {"assistant", p6.c_str()}, {"user", p7.c_str()},
   33|  1.64k|  };
   34|  1.64k|  size_t message_count = 6;
   35|       |
   36|  1.64k|  llama_chat_apply_template(p1.c_str(), conversation, message_count,
   37|  1.64k|                            true, buf, 4096);
   38|  1.64k|  return 0;
   39|  1.64k|}

_Z26llm_chat_template_from_strRKNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE:
   85|  1.64k|llm_chat_template llm_chat_template_from_str(const std::string & name) {
   86|  1.64k|    return LLM_CHAT_TEMPLATES.at(name);
   87|  1.64k|}
_Z24llm_chat_detect_templateRKNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE:
   89|  1.64k|llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
   90|  1.64k|    try {
   91|  1.64k|        return llm_chat_template_from_str(tmpl);
   92|  1.64k|    } catch (const std::out_of_range &) {
   93|       |        // ignore
   94|  1.03k|    }
   95|       |
   96|  1.03k|    auto tmpl_contains = [&tmpl](const char * haystack) -> bool {
   97|  1.03k|        return tmpl.find(haystack) != std::string::npos;
   98|  1.03k|    };
   99|  1.03k|    if (tmpl_contains("<|im_start|>")) {
  ------------------
  |  Branch (99:9): [True: 5, False: 1.03k]
  ------------------
  100|      5|        return tmpl_contains("<|im_sep|>")
  ------------------
  |  Branch (100:16): [True: 2, False: 3]
  ------------------
  101|      5|            ? LLM_CHAT_TEMPLATE_PHI_4
  102|      5|            : tmpl_contains("<end_of_utterance>")
  ------------------
  |  Branch (102:15): [True: 0, False: 3]
  ------------------
  103|      3|                ? LLM_CHAT_TEMPLATE_SMOLVLM // SmolVLM uses <|im_start|> as BOS, but it is NOT chatml
  104|      3|                : LLM_CHAT_TEMPLATE_CHATML;
  105|  1.03k|    } else if (tmpl.find("mistral") == 0 || tmpl_contains("[INST]")) {
  ------------------
  |  Branch (105:16): [True: 54, False: 979]
  |  Branch (105:45): [True: 73, False: 906]
  ------------------
  106|    127|        if (tmpl_contains("[SYSTEM_PROMPT]")) {
  ------------------
  |  Branch (106:13): [True: 10, False: 117]
  ------------------
  107|     10|            return LLM_CHAT_TEMPLATE_MISTRAL_V7;
  108|    117|        } else if (
  109|       |            // catches official 'v1' template
  110|    117|            tmpl_contains("' [INST] ' + system_message")
  ------------------
  |  Branch (110:13): [True: 7, False: 110]
  ------------------
  111|       |            // catches official 'v3' and 'v3-tekken' templates
  112|    110|            || tmpl_contains("[AVAILABLE_TOOLS]")
  ------------------
  |  Branch (112:16): [True: 12, False: 98]
  ------------------
  113|    117|        ) {
  114|       |            // Official mistral 'v1', 'v3' and 'v3-tekken' templates
  115|       |            // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/chat_templates.md
  116|       |            // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/templates.md
  117|     19|            if (tmpl_contains(" [INST]")) {
  ------------------
  |  Branch (117:17): [True: 9, False: 10]
  ------------------
  118|      9|                return LLM_CHAT_TEMPLATE_MISTRAL_V1;
  119|     10|            } else if (tmpl_contains("\"[INST]\"")) {
  ------------------
  |  Branch (119:24): [True: 3, False: 7]
  ------------------
  120|      3|                return LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN;
  121|      3|            }
  122|      7|            return LLM_CHAT_TEMPLATE_MISTRAL_V3;
  123|     98|        } else {
  124|       |            // llama2 template and its variants
  125|       |            // [variant] support system message
  126|       |            // See: https://huggingface.co/blog/llama2#how-to-prompt-llama-2
  127|     98|            bool support_system_message = tmpl_contains("<<SYS>>");
  128|     98|            bool add_bos_inside_history = tmpl_contains("bos_token + '[INST]");
  129|     98|            bool strip_message = tmpl_contains("content.strip()");
  130|     98|            if (strip_message) {
  ------------------
  |  Branch (130:17): [True: 9, False: 89]
  ------------------
  131|      9|                return LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP;
  132|     89|            } else if (add_bos_inside_history) {
  ------------------
  |  Branch (132:24): [True: 2, False: 87]
  ------------------
  133|      2|                return LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS;
  134|     87|            } else if (support_system_message) {
  ------------------
  |  Branch (134:24): [True: 4, False: 83]
  ------------------
  135|      4|                return LLM_CHAT_TEMPLATE_LLAMA_2_SYS;
  136|     83|            } else {
  137|     83|                return LLM_CHAT_TEMPLATE_LLAMA_2;
  138|     83|            }
  139|     98|        }
  140|    906|    } else if (tmpl_contains("<|assistant|>") && tmpl_contains("<|end|>")) {
  ------------------
  |  Branch (140:16): [True: 125, False: 781]
  |  Branch (140:50): [True: 4, False: 121]
  ------------------
  141|      4|        return LLM_CHAT_TEMPLATE_PHI_3;
  142|    902|    } else if (tmpl_contains("[gMASK]<sop>")) {
  ------------------
  |  Branch (142:16): [True: 6, False: 896]
  ------------------
  143|      6|        return LLM_CHAT_TEMPLATE_CHATGLM_4;
  144|    896|    } else if (tmpl_contains("<|assistant|>") && tmpl_contains("<|user|>")) {
  ------------------
  |  Branch (144:16): [True: 119, False: 777]
  |  Branch (144:50): [True: 28, False: 91]
  ------------------
  145|     28|        if (tmpl_contains("<|tool_declare|>")) {
  ------------------
  |  Branch (145:13): [True: 1, False: 27]
  ------------------
  146|      1|            return LLM_CHAT_TEMPLATE_EXAONE_MOE;
  147|      1|        }
  148|     27|        return tmpl_contains("</s>") ? LLM_CHAT_TEMPLATE_FALCON_3 : LLM_CHAT_TEMPLATE_GLMEDGE;
  ------------------
  |  Branch (148:16): [True: 17, False: 10]
  ------------------
  149|    868|    } else if (tmpl_contains("<|{{ item['role'] }}|>") && tmpl_contains("<|begin_of_image|>")) {
  ------------------
  |  Branch (149:16): [True: 12, False: 856]
  |  Branch (149:59): [True: 4, False: 8]
  ------------------
  150|      4|        return LLM_CHAT_TEMPLATE_GLMEDGE;
  151|    864|    } else if (tmpl_contains("<|user|>") && tmpl_contains("<|endoftext|>")) {
  ------------------
  |  Branch (151:16): [True: 6, False: 858]
  |  Branch (151:45): [True: 1, False: 5]
  ------------------
  152|      1|        return LLM_CHAT_TEMPLATE_ZEPHYR;
  153|    863|    } else if (tmpl_contains("bos_token + message['role']")) {
  ------------------
  |  Branch (153:16): [True: 4, False: 859]
  ------------------
  154|      4|        return LLM_CHAT_TEMPLATE_MONARCH;
  155|    859|    } else if (tmpl_contains("<start_of_turn>")) {
  ------------------
  |  Branch (155:16): [True: 9, False: 850]
  ------------------
  156|      9|        return LLM_CHAT_TEMPLATE_GEMMA;
  157|    850|    } else if (tmpl_contains("'\\n\\nAssistant: ' + eos_token")) {
  ------------------
  |  Branch (157:16): [True: 2, False: 848]
  ------------------
  158|       |        // OrionStarAI/Orion-14B-Chat
  159|      2|        return LLM_CHAT_TEMPLATE_ORION;
  160|    848|    } else if (tmpl_contains("GPT4 Correct ")) {
  ------------------
  |  Branch (160:16): [True: 2, False: 846]
  ------------------
  161|       |        // openchat/openchat-3.5-0106
  162|      2|        return LLM_CHAT_TEMPLATE_OPENCHAT;
  163|    846|    } else if (tmpl_contains("USER: ") && tmpl_contains("ASSISTANT: ")) {
  ------------------
  |  Branch (163:16): [True: 23, False: 823]
  |  Branch (163:43): [True: 6, False: 17]
  ------------------
  164|       |        // eachadea/vicuna-13b-1.1 (and Orca variant)
  165|      6|        if (tmpl_contains("SYSTEM: ")) {
  ------------------
  |  Branch (165:13): [True: 3, False: 3]
  ------------------
  166|      3|            return LLM_CHAT_TEMPLATE_VICUNA_ORCA;
  167|      3|        }
  168|      3|        return LLM_CHAT_TEMPLATE_VICUNA;
  169|    840|    } else if (tmpl_contains("### Instruction:") && tmpl_contains("<|EOT|>")) {
  ------------------
  |  Branch (169:16): [True: 14, False: 826]
  |  Branch (169:53): [True: 2, False: 12]
  ------------------
  170|       |        // deepseek-ai/deepseek-coder-33b-instruct
  171|      2|        return LLM_CHAT_TEMPLATE_DEEPSEEK;
  172|    838|    } else if (tmpl_contains("<|START_OF_TURN_TOKEN|>") && tmpl_contains("<|USER_TOKEN|>")) {
  ------------------
  |  Branch (172:16): [True: 14, False: 824]
  |  Branch (172:60): [True: 2, False: 12]
  ------------------
  173|       |        // CohereForAI/c4ai-command-r-plus
  174|      2|        return LLM_CHAT_TEMPLATE_COMMAND_R;
  175|    836|    } else if (tmpl_contains("<|start_header_id|>") && tmpl_contains("<|end_header_id|>")) {
  ------------------
  |  Branch (175:16): [True: 29, False: 807]
  |  Branch (175:56): [True: 3, False: 26]
  ------------------
  176|      3|        return LLM_CHAT_TEMPLATE_LLAMA_3;
  177|    833|    } else if (tmpl_contains("[gMASK]sop")) {
  ------------------
  |  Branch (177:16): [True: 5, False: 828]
  ------------------
  178|       |        // chatglm3-6b
  179|      5|        return LLM_CHAT_TEMPLATE_CHATGLM_3;
  180|    828|    } else if (tmpl_contains(LU8("<用户>"))) {
  ------------------
  |  |   12|    828|    #define LU8(x) u8##x
  ------------------
  |  Branch (180:16): [True: 11, False: 817]
  ------------------
  181|       |        // MiniCPM-3B-OpenHermes-2.5-v2-GGUF
  182|     11|        return LLM_CHAT_TEMPLATE_MINICPM;
  183|    817|    } else if (tmpl_contains("'Assistant: ' + message['content'] + eos_token")) {
  ------------------
  |  Branch (183:16): [True: 1, False: 816]
  ------------------
  184|      1|        return LLM_CHAT_TEMPLATE_DEEPSEEK_2;
  185|    816|    } else if (tmpl_contains(LU8("<｜Assistant｜>")) && tmpl_contains(LU8("<｜User｜>")) && tmpl_contains(LU8("<｜end▁of▁sentence｜>"))) {
  ------------------
  |  |   12|    816|    #define LU8(x) u8##x
  ------------------
                  } else if (tmpl_contains(LU8("<｜Assistant｜>")) && tmpl_contains(LU8("<｜User｜>")) && tmpl_contains(LU8("<｜end▁of▁sentence｜>"))) {
  ------------------
  |  |   12|     24|    #define LU8(x) u8##x
  ------------------
                  } else if (tmpl_contains(LU8("<｜Assistant｜>")) && tmpl_contains(LU8("<｜User｜>")) && tmpl_contains(LU8("<｜end▁of▁sentence｜>"))) {
  ------------------
  |  |   12|      7|    #define LU8(x) u8##x
  ------------------
  |  Branch (185:16): [True: 24, False: 792]
  |  Branch (185:59): [True: 7, False: 17]
  |  Branch (185:97): [True: 1, False: 6]
  ------------------
  186|      1|        return LLM_CHAT_TEMPLATE_DEEPSEEK_3;
  187|    815|    } else if (tmpl_contains("[|system|]") && tmpl_contains("[|assistant|]") && tmpl_contains("[|endofturn|]")) {
  ------------------
  |  Branch (187:16): [True: 27, False: 788]
  |  Branch (187:47): [True: 9, False: 18]
  |  Branch (187:81): [True: 6, False: 3]
  ------------------
  188|      6|        if (tmpl_contains("[|tool|]")) {
  ------------------
  |  Branch (188:13): [True: 2, False: 4]
  ------------------
  189|      2|            return LLM_CHAT_TEMPLATE_EXAONE_4;
  190|      2|        }
  191|       |        // ref: https://huggingface.co/LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct/discussions/8#66bae61b1893d14ee8ed85bb
  192|       |        // EXAONE-3.0-7.8B-Instruct
  193|      4|        return LLM_CHAT_TEMPLATE_EXAONE_3;
  194|    809|    } else if (tmpl_contains("rwkv-world") || tmpl_contains("{{- 'User: ' + message['content']|trim + '\\n\\n' -}}")) {
  ------------------
  |  Branch (194:16): [True: 13, False: 796]
  |  Branch (194:47): [True: 0, False: 796]
  ------------------
  195|     13|        return LLM_CHAT_TEMPLATE_RWKV_WORLD;
  196|    796|    } else if (tmpl_contains("<|start_of_role|>")) {
  ------------------
  |  Branch (196:16): [True: 11, False: 785]
  ------------------
  197|     11|        if (tmpl_contains("<tool_call>") || tmpl_contains("<tools>")) {
  ------------------
  |  Branch (197:13): [True: 2, False: 9]
  |  Branch (197:45): [True: 4, False: 5]
  ------------------
  198|      6|            if (tmpl_contains("g4_default_system_message")) {
  ------------------
  |  Branch (198:17): [True: 1, False: 5]
  ------------------
  199|      1|                return LLM_CHAT_TEMPLATE_GRANITE_4_0;
  200|      1|            }
  201|      5|            return LLM_CHAT_TEMPLATE_GRANITE_4_1;
  202|      6|        }
  203|      5|        return LLM_CHAT_TEMPLATE_GRANITE_3_X;
  204|    785|    } else if (tmpl_contains("message['role'] + additional_special_tokens[0] + message['content'] + additional_special_tokens[1]")) {
  ------------------
  |  Branch (204:16): [True: 1, False: 784]
  ------------------
  205|      1|        return LLM_CHAT_TEMPLATE_GIGACHAT;
  206|    784|    } else if (tmpl_contains("<|role_start|>")) {
  ------------------
  |  Branch (206:16): [True: 5, False: 779]
  ------------------
  207|      5|        return LLM_CHAT_TEMPLATE_MEGREZ;
  208|    779|    } else if (tmpl_contains(" Ассистент:")) {
  ------------------
  |  Branch (208:16): [True: 4, False: 775]
  ------------------
  209|      4|        return LLM_CHAT_TEMPLATE_YANDEX;
  210|    775|    } else if (tmpl_contains("<role>ASSISTANT</role>") && tmpl_contains("'HUMAN'")) {
  ------------------
  |  Branch (210:16): [True: 22, False: 753]
  |  Branch (210:59): [True: 2, False: 20]
  ------------------
  211|      2|        return LLM_CHAT_TEMPLATE_BAILING;
  212|    773|    } else if (tmpl_contains("<role>ASSISTANT</role>") && tmpl_contains("\"HUMAN\"") && tmpl_contains("<think>")) {
  ------------------
  |  Branch (212:16): [True: 20, False: 753]
  |  Branch (212:59): [True: 8, False: 12]
  |  Branch (212:89): [True: 3, False: 5]
  ------------------
  213|      3|        return LLM_CHAT_TEMPLATE_BAILING_THINK;
  214|    770|    } else if (tmpl_contains("<role>ASSISTANT</role>") && tmpl_contains("<role>HUMAN</role>") && tmpl_contains("<|role_end|>")) {
  ------------------
  |  Branch (214:16): [True: 17, False: 753]
  |  Branch (214:59): [True: 7, False: 10]
  |  Branch (214:98): [True: 4, False: 3]
  ------------------
  215|      4|        return LLM_CHAT_TEMPLATE_BAILING2;
  216|    766|    } else if (tmpl_contains("<|header_start|>") && tmpl_contains("<|header_end|>")) {
  ------------------
  |  Branch (216:16): [True: 8, False: 758]
  |  Branch (216:53): [True: 2, False: 6]
  ------------------
  217|      2|        return LLM_CHAT_TEMPLATE_LLAMA4;
  218|    764|    } else if (tmpl_contains("<|endofuserprompt|>")) {
  ------------------
  |  Branch (218:16): [True: 4, False: 760]
  ------------------
  219|      4|        return LLM_CHAT_TEMPLATE_DOTS1;
  220|    760|    } else if (tmpl_contains("<|extra_0|>") && tmpl_contains("<|extra_4|>")) {
  ------------------
  |  Branch (220:16): [True: 11, False: 749]
  |  Branch (220:48): [True: 3, False: 8]
  ------------------
  221|      3|        return LLM_CHAT_TEMPLATE_HUNYUAN_MOE;
  222|    757|    } else if (tmpl_contains("<|start|>") && tmpl_contains("<|channel|>")) {
  ------------------
  |  Branch (222:16): [True: 9, False: 748]
  |  Branch (222:46): [True: 3, False: 6]
  ------------------
  223|      3|        return LLM_CHAT_TEMPLATE_OPENAI_MOE;
  224|    754|    } else if (tmpl_contains("<｜hy_Assistant｜>") && tmpl_contains("<｜hy_begin▁of▁sentence｜>")) {
  ------------------
  |  Branch (224:16): [True: 14, False: 740]
  |  Branch (224:57): [True: 3, False: 11]
  ------------------
  225|      3|        return LLM_CHAT_TEMPLATE_HUNYUAN_VL;
  226|    751|    } else if (tmpl_contains("<｜hy_Assistant｜>") && tmpl_contains("<｜hy_place▁holder▁no▁3｜>")) {
  ------------------
  |  Branch (226:16): [True: 11, False: 740]
  |  Branch (226:57): [True: 3, False: 8]
  ------------------
  227|      3|        return LLM_CHAT_TEMPLATE_HUNYUAN_DENSE;
  228|    748|    } else if (tmpl_contains("<|im_assistant|>assistant<|im_middle|>")) {
  ------------------
  |  Branch (228:16): [True: 3, False: 745]
  ------------------
  229|      3|        return LLM_CHAT_TEMPLATE_KIMI_K2;
  230|    745|    } else if (tmpl_contains("<seed:bos>")) {
  ------------------
  |  Branch (230:16): [True: 7, False: 738]
  ------------------
  231|      7|        return LLM_CHAT_TEMPLATE_SEED_OSS;
  232|    738|    } else if (tmpl_contains("'Assistant: '  + message['content'] + '<|separator|>")) {
  ------------------
  |  Branch (232:16): [True: 2, False: 736]
  ------------------
  233|      2|        return LLM_CHAT_TEMPLATE_GROK_2;
  234|    736|    } else if (tmpl_contains(LU8("[unused9]系统：[unused10]"))) {
  ------------------
  |  |   12|    736|    #define LU8(x) u8##x
  ------------------
  |  Branch (234:16): [True: 2, False: 734]
  ------------------
  235|      2|        return LLM_CHAT_TEMPLATE_PANGU_EMBED;
  236|    734|    } else if (tmpl_contains("<|begin|>") && tmpl_contains("<|end|>") && tmpl_contains("<|content|>")) {
  ------------------
  |  Branch (236:16): [True: 15, False: 719]
  |  Branch (236:46): [True: 7, False: 8]
  |  Branch (236:74): [True: 4, False: 3]
  ------------------
  237|      4|        return LLM_CHAT_TEMPLATE_SOLAR_OPEN;
  238|      4|    }
  239|    730|    return LLM_CHAT_TEMPLATE_UNKNOWN;
  240|  1.03k|}
_Z23llm_chat_apply_template17llm_chat_templateRKNSt3__16vectorIPK18llama_chat_messageNS0_9allocatorIS4_EEEERNS0_12basic_stringIcNS0_11char_traitsIcEENS5_IcEEEEb:
  247|    913|    std::string & dest, bool add_ass) {
  248|       |    // Taken from the research: https://github.com/ggml-org/llama.cpp/issues/5527
  249|    913|    std::stringstream ss;
  250|    913|    if (tmpl == LLM_CHAT_TEMPLATE_CHATML) {
  ------------------
  |  Branch (250:9): [True: 4, False: 909]
  ------------------
  251|       |        // chatml template
  252|     24|        for (auto message : chat) {
  ------------------
  |  Branch (252:27): [True: 24, False: 4]
  ------------------
  253|     24|            ss << "<|im_start|>" << message->role << "\n" << message->content << "<|im_end|>\n";
  254|     24|        }
  255|      4|        if (add_ass) {
  ------------------
  |  Branch (255:13): [True: 4, False: 0]
  ------------------
  256|      4|            ss << "<|im_start|>assistant\n";
  257|      4|        }
  258|    909|    } else if (tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7 || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7_TEKKEN) {
  ------------------
  |  Branch (258:16): [True: 71, False: 838]
  |  Branch (258:56): [True: 1, False: 837]
  ------------------
  259|       |        // Official mistral 'v7' template
  260|       |        // See: https://huggingface.co/mistralai/Mistral-Large-Instruct-2411#basic-instruct-template-v7
  261|       |        //      https://huggingface.co/mistralai/Mistral-Small-3.1-24B-Instruct-2503#basic-instruct-template-v7-tekken
  262|     72|        const char * trailing_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7 ? " " : "";
  ------------------
  |  Branch (262:39): [True: 71, False: 1]
  ------------------
  263|    432|        for (auto message : chat) {
  ------------------
  |  Branch (263:27): [True: 432, False: 72]
  ------------------
  264|    432|            std::string role(message->role);
  265|    432|            std::string content(message->content);
  266|    432|            if (role == "system") {
  ------------------
  |  Branch (266:17): [True: 72, False: 360]
  ------------------
  267|     72|                ss << "[SYSTEM_PROMPT]" << trailing_space << content << "[/SYSTEM_PROMPT]";
  268|    360|            } else if (role == "user") {
  ------------------
  |  Branch (268:24): [True: 216, False: 144]
  ------------------
  269|    216|                ss << "[INST]" << trailing_space << content << "[/INST]";
  270|    216|            } else {
  271|    144|                ss << trailing_space << content << "</s>";
  272|    144|            }
  273|    432|        }
  274|    837|    } else if (tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V1
  ------------------
  |  Branch (274:16): [True: 71, False: 766]
  ------------------
  275|    766|            || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3
  ------------------
  |  Branch (275:16): [True: 32, False: 734]
  ------------------
  276|    734|            || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN) {
  ------------------
  |  Branch (276:16): [True: 5, False: 729]
  ------------------
  277|       |        // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/chat_templates.md
  278|       |        // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/templates.md
  279|    108|        std::string leading_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V1 ? " " : "";
  ------------------
  |  Branch (279:37): [True: 71, False: 37]
  ------------------
  280|    108|        std::string trailing_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN ? "" : " ";
  ------------------
  |  Branch (280:38): [True: 5, False: 103]
  ------------------
  281|    108|        bool trim_assistant_message = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3;
  282|    108|        bool is_inside_turn = false;
  283|    648|        for (auto message : chat) {
  ------------------
  |  Branch (283:27): [True: 648, False: 108]
  ------------------
  284|    648|            if (!is_inside_turn) {
  ------------------
  |  Branch (284:17): [True: 324, False: 324]
  ------------------
  285|    324|                ss << leading_space << "[INST]" << trailing_space;
  286|    324|                is_inside_turn = true;
  287|    324|            }
  288|    648|            std::string role(message->role);
  289|    648|            std::string content(message->content);
  290|    648|            if (role == "system") {
  ------------------
  |  Branch (290:17): [True: 108, False: 540]
  ------------------
  291|    108|                ss << content << "\n\n";
  292|    540|            } else if (role == "user") {
  ------------------
  |  Branch (292:24): [True: 324, False: 216]
  ------------------
  293|    324|                ss << content << leading_space << "[/INST]";
  294|    324|            } else {
  295|    216|                ss << trailing_space << (trim_assistant_message ? trim(content) : content) << "</s>";
  ------------------
  |  Branch (295:42): [True: 64, False: 152]
  ------------------
  296|    216|                is_inside_turn = false;
  297|    216|            }
  298|    648|        }
  299|    729|    } else if (
  300|    729|            tmpl == LLM_CHAT_TEMPLATE_LLAMA_2
  ------------------
  |  Branch (300:13): [True: 110, False: 619]
  ------------------
  301|    619|            || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS
  ------------------
  |  Branch (301:16): [True: 7, False: 612]
  ------------------
  302|    612|            || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS
  ------------------
  |  Branch (302:16): [True: 3, False: 609]
  ------------------
  303|    609|            || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP) {
  ------------------
  |  Branch (303:16): [True: 74, False: 535]
  ------------------
  304|       |        // llama2 template and its variants
  305|       |        // [variant] support system message
  306|       |        // See: https://huggingface.co/blog/llama2#how-to-prompt-llama-2
  307|    194|        bool support_system_message = tmpl != LLM_CHAT_TEMPLATE_LLAMA_2;
  308|       |        // [variant] add BOS inside history
  309|    194|        bool add_bos_inside_history = tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS;
  310|       |        // [variant] trim spaces from the input message
  311|    194|        bool strip_message = tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP;
  312|       |        // construct the prompt
  313|    194|        bool is_inside_turn = true; // skip BOS at the beginning
  314|    194|        ss << "[INST] ";
  315|  1.16k|        for (auto message : chat) {
  ------------------
  |  Branch (315:27): [True: 1.16k, False: 194]
  ------------------
  316|  1.16k|            std::string content = strip_message ? trim(message->content) : message->content;
  ------------------
  |  Branch (316:35): [True: 444, False: 720]
  ------------------
  317|  1.16k|            std::string role(message->role);
  318|  1.16k|            if (!is_inside_turn) {
  ------------------
  |  Branch (318:17): [True: 388, False: 776]
  ------------------
  319|    388|                is_inside_turn = true;
  320|    388|                ss << (add_bos_inside_history ? "<s>[INST] " : "[INST] ");
  ------------------
  |  Branch (320:24): [True: 6, False: 382]
  ------------------
  321|    388|            }
  322|  1.16k|            if (role == "system") {
  ------------------
  |  Branch (322:17): [True: 194, False: 970]
  ------------------
  323|    194|                if (support_system_message) {
  ------------------
  |  Branch (323:21): [True: 84, False: 110]
  ------------------
  324|     84|                    ss << "<<SYS>>\n" << content << "\n<</SYS>>\n\n";
  325|    110|                } else {
  326|       |                    // if the model does not support system message, we still include it in the first message, but without <<SYS>>
  327|    110|                    ss << content << "\n";
  328|    110|                }
  329|    970|            } else if (role == "user") {
  ------------------
  |  Branch (329:24): [True: 582, False: 388]
  ------------------
  330|    582|                ss << content << " [/INST]";
  331|    582|            } else {
  332|    388|                ss << content << "</s>";
  333|    388|                is_inside_turn = false;
  334|    388|            }
  335|  1.16k|        }
  336|    535|    } else if (tmpl == LLM_CHAT_TEMPLATE_PHI_3) {
  ------------------
  |  Branch (336:16): [True: 5, False: 530]
  ------------------
  337|       |        // Phi 3
  338|     30|        for (auto message : chat) {
  ------------------
  |  Branch (338:27): [True: 30, False: 5]
  ------------------
  339|     30|            std::string role(message->role);
  340|     30|            ss << "<|" << role << "|>\n" << message->content << "<|end|>\n";
  341|     30|        }
  342|      5|        if (add_ass) {
  ------------------
  |  Branch (342:13): [True: 5, False: 0]
  ------------------
  343|      5|            ss << "<|assistant|>\n";
  344|      5|        }
  345|    530|    } else if (tmpl == LLM_CHAT_TEMPLATE_PHI_4) {
  ------------------
  |  Branch (345:16): [True: 4, False: 526]
  ------------------
  346|       |        // chatml template
  347|     24|        for (auto message : chat) {
  ------------------
  |  Branch (347:27): [True: 24, False: 4]
  ------------------
  348|     24|            ss << "<|im_start|>" << message->role << "<|im_sep|>" << message->content << "<|im_end|>";
  349|     24|        }
  350|      4|        if (add_ass) {
  ------------------
  |  Branch (350:13): [True: 4, False: 0]
  ------------------
  351|      4|            ss << "<|im_start|>assistant<|im_sep|>";
  352|      4|        }
  353|    526|    } else if (tmpl == LLM_CHAT_TEMPLATE_FALCON_3) {
  ------------------
  |  Branch (353:16): [True: 18, False: 508]
  ------------------
  354|       |        // Falcon 3
  355|    108|        for (auto message : chat) {
  ------------------
  |  Branch (355:27): [True: 108, False: 18]
  ------------------
  356|    108|            std::string role(message->role);
  357|    108|            ss << "<|" << role << "|>\n" << message->content << "\n";
  358|    108|        }
  359|     18|        if (add_ass) {
  ------------------
  |  Branch (359:13): [True: 18, False: 0]
  ------------------
  360|     18|            ss << "<|assistant|>\n";
  361|     18|        }
  362|    508|    } else if (tmpl == LLM_CHAT_TEMPLATE_ZEPHYR) {
  ------------------
  |  Branch (362:16): [True: 3, False: 505]
  ------------------
  363|       |        // zephyr template
  364|     18|        for (auto message : chat) {
  ------------------
  |  Branch (364:27): [True: 18, False: 3]
  ------------------
  365|     18|            ss << "<|" << message->role << "|>" << "\n" << message->content << "<|endoftext|>\n";
  366|     18|        }
  367|      3|        if (add_ass) {
  ------------------
  |  Branch (367:13): [True: 3, False: 0]
  ------------------
  368|      3|            ss << "<|assistant|>\n";
  369|      3|        }
  370|    505|    } else if (tmpl == LLM_CHAT_TEMPLATE_MONARCH) {
  ------------------
  |  Branch (370:16): [True: 5, False: 500]
  ------------------
  371|       |        // mlabonne/AlphaMonarch-7B template (the <s> is included inside history)
  372|     30|        for (auto message : chat) {
  ------------------
  |  Branch (372:27): [True: 30, False: 5]
  ------------------
  373|     30|            std::string bos = (message == chat.front()) ? "" : "<s>"; // skip BOS for first message
  ------------------
  |  Branch (373:31): [True: 5, False: 25]
  ------------------
  374|     30|            ss << bos << message->role << "\n" << message->content << "</s>\n";
  375|     30|        }
  376|      5|        if (add_ass) {
  ------------------
  |  Branch (376:13): [True: 5, False: 0]
  ------------------
  377|      5|            ss << "<s>assistant\n";
  378|      5|        }
  379|    500|    } else if (tmpl == LLM_CHAT_TEMPLATE_GEMMA) {
  ------------------
  |  Branch (379:16): [True: 168, False: 332]
  ------------------
  380|       |        // google/gemma-7b-it
  381|    168|        std::string system_prompt = "";
  382|  1.00k|        for (auto message : chat) {
  ------------------
  |  Branch (382:27): [True: 1.00k, False: 168]
  ------------------
  383|  1.00k|            std::string role(message->role);
  384|  1.00k|            if (role == "system") {
  ------------------
  |  Branch (384:17): [True: 168, False: 840]
  ------------------
  385|       |                // there is no system message for gemma, but we will merge it with user prompt, so nothing is broken
  386|    168|                system_prompt += trim(message->content);
  387|    168|                continue;
  388|    168|            }
  389|       |            // in gemma, "assistant" is "model"
  390|    840|            role = role == "assistant" ? "model" : message->role;
  ------------------
  |  Branch (390:20): [True: 336, False: 504]
  ------------------
  391|    840|            ss << "<start_of_turn>" << role << "\n";
  392|    840|            if (!system_prompt.empty() && role != "model") {
  ------------------
  |  Branch (392:17): [True: 138, False: 702]
  |  Branch (392:43): [True: 138, False: 0]
  ------------------
  393|    138|                ss << system_prompt << "\n\n";
  394|    138|                system_prompt = "";
  395|    138|            }
  396|    840|            ss << trim(message->content) << "<end_of_turn>\n";
  397|    840|        }
  398|    168|        if (add_ass) {
  ------------------
  |  Branch (398:13): [True: 168, False: 0]
  ------------------
  399|    168|            ss << "<start_of_turn>model\n";
  400|    168|        }
  401|    332|    } else if (tmpl == LLM_CHAT_TEMPLATE_ORION) {
  ------------------
  |  Branch (401:16): [True: 26, False: 306]
  ------------------
  402|       |        // OrionStarAI/Orion-14B-Chat
  403|     26|        std::string system_prompt = "";
  404|    156|        for (auto message : chat) {
  ------------------
  |  Branch (404:27): [True: 156, False: 26]
  ------------------
  405|    156|            std::string role(message->role);
  406|    156|            if (role == "system") {
  ------------------
  |  Branch (406:17): [True: 26, False: 130]
  ------------------
  407|       |                // there is no system message support, we will merge it with user prompt
  408|     26|                system_prompt += message->content;
  409|     26|                continue;
  410|    130|            } else if (role == "user") {
  ------------------
  |  Branch (410:24): [True: 78, False: 52]
  ------------------
  411|     78|                ss << "Human: ";
  412|     78|                if (!system_prompt.empty()) {
  ------------------
  |  Branch (412:21): [True: 23, False: 55]
  ------------------
  413|     23|                    ss << system_prompt << "\n\n";
  414|     23|                    system_prompt = "";
  415|     23|                }
  416|     78|                ss << message->content << "\n\nAssistant: </s>";
  417|     78|            } else {
  418|     52|                ss << message->content << "</s>";
  419|     52|            }
  420|    156|        }
  421|    306|    } else if (tmpl == LLM_CHAT_TEMPLATE_OPENCHAT) {
  ------------------
  |  Branch (421:16): [True: 3, False: 303]
  ------------------
  422|       |        // openchat/openchat-3.5-0106,
  423|     18|        for (auto message : chat) {
  ------------------
  |  Branch (423:27): [True: 18, False: 3]
  ------------------
  424|     18|            std::string role(message->role);
  425|     18|            if (role == "system") {
  ------------------
  |  Branch (425:17): [True: 3, False: 15]
  ------------------
  426|      3|                ss << message->content << "<|end_of_turn|>";
  427|     15|            } else {
  428|     15|                role[0] = toupper(role[0]);
  429|     15|                ss << "GPT4 Correct " << role << ": " << message->content << "<|end_of_turn|>";
  430|     15|            }
  431|     18|        }
  432|      3|        if (add_ass) {
  ------------------
  |  Branch (432:13): [True: 3, False: 0]
  ------------------
  433|      3|            ss << "GPT4 Correct Assistant:";
  434|      3|        }
  435|    303|    } else if (tmpl == LLM_CHAT_TEMPLATE_VICUNA || tmpl == LLM_CHAT_TEMPLATE_VICUNA_ORCA) {
  ------------------
  |  Branch (435:16): [True: 4, False: 299]
  |  Branch (435:52): [True: 4, False: 295]
  ------------------
  436|       |        // eachadea/vicuna-13b-1.1 (and Orca variant)
  437|     48|        for (auto message : chat) {
  ------------------
  |  Branch (437:27): [True: 48, False: 8]
  ------------------
  438|     48|            std::string role(message->role);
  439|     48|            if (role == "system") {
  ------------------
  |  Branch (439:17): [True: 8, False: 40]
  ------------------
  440|       |                // Orca-Vicuna variant uses a system prefix
  441|      8|                if (tmpl == LLM_CHAT_TEMPLATE_VICUNA_ORCA) {
  ------------------
  |  Branch (441:21): [True: 4, False: 4]
  ------------------
  442|      4|                    ss << "SYSTEM: " << message->content << "\n";
  443|      4|                } else {
  444|      4|                    ss << message->content << "\n\n";
  445|      4|                }
  446|     40|            } else if (role == "user") {
  ------------------
  |  Branch (446:24): [True: 24, False: 16]
  ------------------
  447|     24|                ss << "USER: " << message->content << "\n";
  448|     24|            } else if (role == "assistant") {
  ------------------
  |  Branch (448:24): [True: 16, False: 0]
  ------------------
  449|     16|                ss << "ASSISTANT: " << message->content << "</s>\n";
  450|     16|            }
  451|     48|        }
  452|      8|        if (add_ass) {
  ------------------
  |  Branch (452:13): [True: 8, False: 0]
  ------------------
  453|      8|            ss << "ASSISTANT:";
  454|      8|        }
  455|    295|    } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK) {
  ------------------
  |  Branch (455:16): [True: 3, False: 292]
  ------------------
  456|       |        // deepseek-ai/deepseek-coder-33b-instruct
  457|     18|        for (auto message : chat) {
  ------------------
  |  Branch (457:27): [True: 18, False: 3]
  ------------------
  458|     18|            std::string role(message->role);
  459|     18|            if (role == "system") {
  ------------------
  |  Branch (459:17): [True: 3, False: 15]
  ------------------
  460|      3|                ss << message->content;
  461|     15|            } else if (role == "user") {
  ------------------
  |  Branch (461:24): [True: 9, False: 6]
  ------------------
  462|      9|                ss << "### Instruction:\n" << message->content << "\n";
  463|      9|            } else if (role == "assistant") {
  ------------------
  |  Branch (463:24): [True: 6, False: 0]
  ------------------
  464|      6|                ss << "### Response:\n" << message->content << "\n<|EOT|>\n";
  465|      6|            }
  466|     18|        }
  467|      3|        if (add_ass) {
  ------------------
  |  Branch (467:13): [True: 3, False: 0]
  ------------------
  468|      3|            ss << "### Response:\n";
  469|      3|        }
  470|    292|    } else if (tmpl == LLM_CHAT_TEMPLATE_COMMAND_R) {
  ------------------
  |  Branch (470:16): [True: 28, False: 264]
  ------------------
  471|       |        // CohereForAI/c4ai-command-r-plus
  472|    168|        for (auto message : chat) {
  ------------------
  |  Branch (472:27): [True: 168, False: 28]
  ------------------
  473|    168|            std::string role(message->role);
  474|    168|            if (role == "system") {
  ------------------
  |  Branch (474:17): [True: 28, False: 140]
  ------------------
  475|     28|                ss << "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  476|    140|            } else if (role == "user") {
  ------------------
  |  Branch (476:24): [True: 84, False: 56]
  ------------------
  477|     84|                ss << "<|START_OF_TURN_TOKEN|><|USER_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  478|     84|            } else if (role == "assistant") {
  ------------------
  |  Branch (478:24): [True: 56, False: 0]
  ------------------
  479|     56|                ss << "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  480|     56|            }
  481|    168|        }
  482|     28|        if (add_ass) {
  ------------------
  |  Branch (482:13): [True: 28, False: 0]
  ------------------
  483|     28|            ss << "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>";
  484|     28|        }
  485|    264|    } else if (tmpl == LLM_CHAT_TEMPLATE_LLAMA_3) {
  ------------------
  |  Branch (485:16): [True: 11, False: 253]
  ------------------
  486|       |        // Llama 3
  487|     66|        for (auto message : chat) {
  ------------------
  |  Branch (487:27): [True: 66, False: 11]
  ------------------
  488|     66|            std::string role(message->role);
  489|     66|            ss << "<|start_header_id|>" << role << "<|end_header_id|>\n\n" << trim(message->content) << "<|eot_id|>";
  490|     66|        }
  491|     11|        if (add_ass) {
  ------------------
  |  Branch (491:13): [True: 11, False: 0]
  ------------------
  492|     11|            ss << "<|start_header_id|>assistant<|end_header_id|>\n\n";
  493|     11|        }
  494|    253|    } else if (tmpl == LLM_CHAT_TEMPLATE_CHATGLM_3) {
  ------------------
  |  Branch (494:16): [True: 6, False: 247]
  ------------------
  495|       |        // chatglm3-6b
  496|      6|        ss << "[gMASK]" << "sop";
  497|     36|        for (auto message : chat) {
  ------------------
  |  Branch (497:27): [True: 36, False: 6]
  ------------------
  498|     36|            std::string role(message->role);
  499|     36|            ss << "<|" << role << "|>" << "\n " << message->content;
  500|     36|        }
  501|      6|        if (add_ass) {
  ------------------
  |  Branch (501:13): [True: 6, False: 0]
  ------------------
  502|      6|            ss << "<|assistant|>";
  503|      6|        }
  504|    247|    } else if (tmpl == LLM_CHAT_TEMPLATE_CHATGLM_4) {
  ------------------
  |  Branch (504:16): [True: 7, False: 240]
  ------------------
  505|      7|        ss << "[gMASK]" << "<sop>";
  506|     42|        for (auto message : chat) {
  ------------------
  |  Branch (506:27): [True: 42, False: 7]
  ------------------
  507|     42|            std::string role(message->role);
  508|     42|            ss << "<|" << role << "|>" << "\n" << message->content;
  509|     42|        }
  510|      7|        if (add_ass) {
  ------------------
  |  Branch (510:13): [True: 7, False: 0]
  ------------------
  511|      7|            ss << "<|assistant|>\n";
  512|      7|        }
  513|    240|    } else if (tmpl == LLM_CHAT_TEMPLATE_GLMEDGE) {
  ------------------
  |  Branch (513:16): [True: 14, False: 226]
  ------------------
  514|     84|        for (auto message : chat) {
  ------------------
  |  Branch (514:27): [True: 84, False: 14]
  ------------------
  515|     84|            std::string role(message->role);
  516|     84|            ss << "<|" << role << "|>" << "\n" << message->content;
  517|     84|        }
  518|     14|        if (add_ass) {
  ------------------
  |  Branch (518:13): [True: 14, False: 0]
  ------------------
  519|     14|            ss << "<|assistant|>";
  520|     14|        }
  521|    226|    } else if (tmpl == LLM_CHAT_TEMPLATE_MINICPM) {
  ------------------
  |  Branch (521:16): [True: 22, False: 204]
  ------------------
  522|       |        // MiniCPM-3B-OpenHermes-2.5-v2-GGUF
  523|    132|        for (auto message : chat) {
  ------------------
  |  Branch (523:27): [True: 132, False: 22]
  ------------------
  524|    132|            std::string role(message->role);
  525|    132|            if (role == "user") {
  ------------------
  |  Branch (525:17): [True: 66, False: 66]
  ------------------
  526|     66|                ss << LU8("<用户>");
  ------------------
  |  |   12|     66|    #define LU8(x) u8##x
  ------------------
  527|     66|                ss << trim(message->content);
  528|     66|                ss << "<AI>";
  529|     66|            } else {
  530|     66|                ss << trim(message->content);
  531|     66|            }
  532|    132|        }
  533|    204|    } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK_2) {
  ------------------
  |  Branch (533:16): [True: 2, False: 202]
  ------------------
  534|       |        // DeepSeek-V2
  535|     12|        for (auto message : chat) {
  ------------------
  |  Branch (535:27): [True: 12, False: 2]
  ------------------
  536|     12|            std::string role(message->role);
  537|     12|            if (role == "system") {
  ------------------
  |  Branch (537:17): [True: 2, False: 10]
  ------------------
  538|      2|                ss << message->content << "\n\n";
  539|     10|            } else if (role == "user") {
  ------------------
  |  Branch (539:24): [True: 6, False: 4]
  ------------------
  540|      6|                ss << "User: " << message->content << "\n\n";
  541|      6|            } else if (role == "assistant") {
  ------------------
  |  Branch (541:24): [True: 4, False: 0]
  ------------------
  542|      4|                ss << "Assistant: " << message->content << LU8("<｜end▁of▁sentence｜>");
  ------------------
  |  |   12|      4|    #define LU8(x) u8##x
  ------------------
  543|      4|            }
  544|     12|        }
  545|      2|        if (add_ass) {
  ------------------
  |  Branch (545:13): [True: 2, False: 0]
  ------------------
  546|      2|            ss << "Assistant:";
  547|      2|        }
  548|    202|    } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK_3) {
  ------------------
  |  Branch (548:16): [True: 2, False: 200]
  ------------------
  549|       |        // DeepSeek-V3
  550|     12|        for (auto message : chat) {
  ------------------
  |  Branch (550:27): [True: 12, False: 2]
  ------------------
  551|     12|            std::string role(message->role);
  552|     12|            if (role == "system") {
  ------------------
  |  Branch (552:17): [True: 2, False: 10]
  ------------------
  553|      2|                ss << message->content << "\n\n";
  554|     10|            } else if (role == "user") {
  ------------------
  |  Branch (554:24): [True: 6, False: 4]
  ------------------
  555|      6|                ss << LU8("<｜User｜>") << message->content;
  ------------------
  |  |   12|      6|    #define LU8(x) u8##x
  ------------------
  556|      6|            } else if (role == "assistant") {
  ------------------
  |  Branch (556:24): [True: 4, False: 0]
  ------------------
  557|      4|                ss << LU8("<｜Assistant｜>") << message->content << LU8("<｜end▁of▁sentence｜>");
  ------------------
  |  |   12|      4|    #define LU8(x) u8##x
  ------------------
                              ss << LU8("<｜Assistant｜>") << message->content << LU8("<｜end▁of▁sentence｜>");
  ------------------
  |  |   12|      4|    #define LU8(x) u8##x
  ------------------
  558|      4|            }
  559|     12|        }
  560|      2|        if (add_ass) {
  ------------------
  |  Branch (560:13): [True: 2, False: 0]
  ------------------
  561|      2|            ss << LU8("<｜Assistant｜>");
  ------------------
  |  |   12|      2|    #define LU8(x) u8##x
  ------------------
  562|      2|        }
  563|    200|    } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK_OCR) {
  ------------------
  |  Branch (563:16): [True: 4, False: 196]
  ------------------
  564|     24|        for (auto message : chat) {
  ------------------
  |  Branch (564:27): [True: 24, False: 4]
  ------------------
  565|       |            // no template
  566|     24|            ss << message->content;
  567|     24|        }
  568|    196|    } else if (tmpl == LLM_CHAT_TEMPLATE_EXAONE_3) {
  ------------------
  |  Branch (568:16): [True: 14, False: 182]
  ------------------
  569|       |        // ref: https://huggingface.co/LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct/discussions/8#66bae61b1893d14ee8ed85bb
  570|       |        // EXAONE-3.0-7.8B-Instruct
  571|     84|        for (auto message : chat) {
  ------------------
  |  Branch (571:27): [True: 84, False: 14]
  ------------------
  572|     84|            std::string role(message->role);
  573|     84|            if (role == "system") {
  ------------------
  |  Branch (573:17): [True: 14, False: 70]
  ------------------
  574|     14|                ss << "[|system|]" << trim(message->content) << "[|endofturn|]\n";
  575|     70|            } else if (role == "user") {
  ------------------
  |  Branch (575:24): [True: 42, False: 28]
  ------------------
  576|     42|                ss << "[|user|]" << trim(message->content) << "\n";
  577|     42|            } else if (role == "assistant") {
  ------------------
  |  Branch (577:24): [True: 28, False: 0]
  ------------------
  578|     28|                ss << "[|assistant|]" << trim(message->content) << "[|endofturn|]\n";
  579|     28|            }
  580|     84|        }
  581|     14|        if (add_ass) {
  ------------------
  |  Branch (581:13): [True: 14, False: 0]
  ------------------
  582|     14|            ss << "[|assistant|]";
  583|     14|        }
  584|    182|    } else if (tmpl == LLM_CHAT_TEMPLATE_EXAONE_4) {
  ------------------
  |  Branch (584:16): [True: 16, False: 166]
  ------------------
  585|     96|        for (auto message : chat) {
  ------------------
  |  Branch (585:27): [True: 96, False: 16]
  ------------------
  586|     96|            std::string role(message->role);
  587|     96|            if (role == "system") {
  ------------------
  |  Branch (587:17): [True: 16, False: 80]
  ------------------
  588|     16|                ss << "[|system|]" << trim(message->content) << "[|endofturn|]\n";
  589|     80|            } else if (role == "user") {
  ------------------
  |  Branch (589:24): [True: 48, False: 32]
  ------------------
  590|     48|                ss << "[|user|]" << trim(message->content) << "\n";
  591|     48|            } else if (role == "assistant") {
  ------------------
  |  Branch (591:24): [True: 32, False: 0]
  ------------------
  592|     32|                ss << "[|assistant|]" << trim(message->content) << "[|endofturn|]\n";
  593|     32|            } else if (role == "tool") {
  ------------------
  |  Branch (593:24): [True: 0, False: 0]
  ------------------
  594|      0|                ss << "[|tool|]" << trim(message->content) << "[|endofturn|]\n";
  595|      0|            }
  596|     96|        }
  597|     16|        if (add_ass) {
  ------------------
  |  Branch (597:13): [True: 16, False: 0]
  ------------------
  598|     16|            ss << "[|assistant|]";
  599|     16|        }
  600|    166|    } else if (tmpl == LLM_CHAT_TEMPLATE_EXAONE_MOE) {
  ------------------
  |  Branch (600:16): [True: 11, False: 155]
  ------------------
  601|     66|        for (auto message : chat) {
  ------------------
  |  Branch (601:27): [True: 66, False: 11]
  ------------------
  602|     66|            std::string role(message->role);
  603|     66|            if (role == "system") {
  ------------------
  |  Branch (603:17): [True: 11, False: 55]
  ------------------
  604|     11|                ss << "<|system|>\n" << trim(message->content) << "<|endofturn|>\n";
  605|     55|            } else if (role == "user") {
  ------------------
  |  Branch (605:24): [True: 33, False: 22]
  ------------------
  606|     33|                ss << "<|user|>\n" << trim(message->content) << "<|endofturn|>\n";
  607|     33|            } else if (role == "assistant") {
  ------------------
  |  Branch (607:24): [True: 22, False: 0]
  ------------------
  608|     22|                ss << "<|assistant|>\n" << trim(message->content) << "<|endofturn|>\n";
  609|     22|            } else if (role == "tool") {
  ------------------
  |  Branch (609:24): [True: 0, False: 0]
  ------------------
  610|      0|                ss << "<|tool|>\n" << trim(message->content) << "<|endofturn|>\n";
  611|      0|            }
  612|     66|        }
  613|     11|        if (add_ass) {
  ------------------
  |  Branch (613:13): [True: 11, False: 0]
  ------------------
  614|     11|            ss << "<|assistant|>\n";
  615|     11|        }
  616|    155|    } else if (tmpl == LLM_CHAT_TEMPLATE_RWKV_WORLD) {
  ------------------
  |  Branch (616:16): [True: 20, False: 135]
  ------------------
  617|       |        // this template requires the model to have "\n\n" as EOT token
  618|    140|        for (size_t i = 0; i < chat.size(); i++) {
  ------------------
  |  Branch (618:28): [True: 120, False: 20]
  ------------------
  619|    120|            std::string role(chat[i]->role);
  620|    120|            if (role == "system") {
  ------------------
  |  Branch (620:17): [True: 20, False: 100]
  ------------------
  621|     20|                ss << "System: " << trim(chat[i]->content) << "\n\n";
  622|    100|            } else if (role == "user") {
  ------------------
  |  Branch (622:24): [True: 60, False: 40]
  ------------------
  623|     60|                ss << "User: " << trim(chat[i]->content) << "\n\n";
  624|     60|                if (i == chat.size() - 1) {
  ------------------
  |  Branch (624:21): [True: 20, False: 40]
  ------------------
  625|     20|                    ss << "Assistant:";
  626|     20|                }
  627|     60|            } else if (role == "assistant") {
  ------------------
  |  Branch (627:24): [True: 40, False: 0]
  ------------------
  628|     40|                ss << "Assistant: " << trim(chat[i]->content) << "\n\n";
  629|     40|            }
  630|    120|        }
  631|    135|    } else if (tmpl == LLM_CHAT_TEMPLATE_GRANITE_3_X) {
  ------------------
  |  Branch (631:16): [True: 6, False: 129]
  ------------------
  632|       |        // IBM Granite 3.x template
  633|     36|        for (const auto & message : chat) {
  ------------------
  |  Branch (633:35): [True: 36, False: 6]
  ------------------
  634|     36|            std::string role(message->role);
  635|     36|            ss << "<|start_of_role|>" << role << "<|end_of_role|>";
  636|     36|            if (role == "assistant_tool_call") {
  ------------------
  |  Branch (636:17): [True: 0, False: 36]
  ------------------
  637|      0|                ss << "<|tool_call|>";
  638|      0|            }
  639|     36|            ss << message->content << "<|end_of_text|>\n";
  640|     36|        }
  641|      6|        if (add_ass) {
  ------------------
  |  Branch (641:13): [True: 6, False: 0]
  ------------------
  642|      6|            ss << "<|start_of_role|>assistant<|end_of_role|>";
  643|      6|        }
  644|    129|    } else if (tmpl == LLM_CHAT_TEMPLATE_GRANITE_4_0) {
  ------------------
  |  Branch (644:16): [True: 2, False: 127]
  ------------------
  645|       |        // IBM Granite 4.0 template
  646|     12|        for (const auto & message : chat) {
  ------------------
  |  Branch (646:35): [True: 12, False: 2]
  ------------------
  647|     12|            std::string role(message->role);
  648|     12|            if (role == "assistant_tool_call") {
  ------------------
  |  Branch (648:17): [True: 0, False: 12]
  ------------------
  649|      0|                ss << "<|start_of_role|>assistant<|end_of_role|><|tool_call|>";
  650|     12|            } else {
  651|     12|                ss << "<|start_of_role|>" << role << "<|end_of_role|>";
  652|     12|            }
  653|     12|            ss << message->content << "<|end_of_text|>\n";
  654|     12|        }
  655|      2|        if (add_ass) {
  ------------------
  |  Branch (655:13): [True: 2, False: 0]
  ------------------
  656|      2|            ss << "<|start_of_role|>assistant<|end_of_role|>";
  657|      2|        }
  658|    127|    } else if (tmpl == LLM_CHAT_TEMPLATE_GRANITE_4_1) {
  ------------------
  |  Branch (658:16): [True: 5, False: 122]
  ------------------
  659|       |        // IBM Granite 4.1 template
  660|     30|        for (const auto & message : chat) {
  ------------------
  |  Branch (660:35): [True: 30, False: 5]
  ------------------
  661|     30|            std::string role(message->role);
  662|     30|            if (role == "assistant_tool_call") {
  ------------------
  |  Branch (662:17): [True: 0, False: 30]
  ------------------
  663|      0|                ss << "<|start_of_role|>assistant<|end_of_role|><|tool_call|>";
  664|     30|            } else {
  665|     30|                ss << "<|start_of_role|>" << role << "<|end_of_role|>";
  666|     30|            }
  667|     30|            ss << message->content << "<|end_of_text|>\n";
  668|     30|        }
  669|      5|        if (add_ass) {
  ------------------
  |  Branch (669:13): [True: 5, False: 0]
  ------------------
  670|      5|            ss << "<|start_of_role|>assistant<|end_of_role|>";
  671|      5|        }
  672|    122|    } else if (tmpl == LLM_CHAT_TEMPLATE_GIGACHAT) {
  ------------------
  |  Branch (672:16): [True: 2, False: 120]
  ------------------
  673|       |        // GigaChat template
  674|      2|        bool has_system = !chat.empty() && std::string(chat[0]->role) == "system";
  ------------------
  |  Branch (674:27): [True: 2, False: 0]
  |  Branch (674:44): [True: 2, False: 0]
  ------------------
  675|       |
  676|       |        // Handle system message if present
  677|      2|        if (has_system) {
  ------------------
  |  Branch (677:13): [True: 2, False: 0]
  ------------------
  678|      2|            ss << "<s>" << chat[0]->content << "<|message_sep|>";
  679|      2|        } else {
  680|      0|            ss << "<s>";
  681|      0|        }
  682|       |
  683|       |        // Process remaining messages
  684|     12|        for (size_t i = has_system ? 1 : 0; i < chat.size(); i++) {
  ------------------
  |  Branch (684:25): [True: 2, False: 0]
  |  Branch (684:45): [True: 10, False: 2]
  ------------------
  685|     10|            std::string role(chat[i]->role);
  686|     10|            if (role == "user") {
  ------------------
  |  Branch (686:17): [True: 6, False: 4]
  ------------------
  687|      6|                ss << "user<|role_sep|>" << chat[i]->content << "<|message_sep|>"
  688|      6|                << "available functions<|role_sep|>[]<|message_sep|>";
  689|      6|            } else if (role == "assistant") {
  ------------------
  |  Branch (689:24): [True: 4, False: 0]
  ------------------
  690|      4|                ss << "assistant<|role_sep|>" << chat[i]->content << "<|message_sep|>";
  691|      4|            }
  692|     10|        }
  693|       |
  694|       |        // Add generation prompt if needed
  695|      2|        if (add_ass) {
  ------------------
  |  Branch (695:13): [True: 2, False: 0]
  ------------------
  696|      2|            ss << "assistant<|role_sep|>";
  697|      2|        }
  698|    120|    }  else if (tmpl == LLM_CHAT_TEMPLATE_MEGREZ) {
  ------------------
  |  Branch (698:17): [True: 6, False: 114]
  ------------------
  699|       |        // Megrez template
  700|     36|        for (auto message : chat) {
  ------------------
  |  Branch (700:27): [True: 36, False: 6]
  ------------------
  701|     36|            std::string role(message->role);
  702|     36|            ss << "<|role_start|>" << role << "<|role_end|>" << message->content << "<|turn_end|>";
  703|     36|        }
  704|       |
  705|      6|        if (add_ass) {
  ------------------
  |  Branch (705:13): [True: 6, False: 0]
  ------------------
  706|      6|            ss << "<|role_start|>assistant<|role_end|>";
  707|      6|        }
  708|    114|    } else if (tmpl == LLM_CHAT_TEMPLATE_YANDEX) {
  ------------------
  |  Branch (708:16): [True: 4, False: 110]
  ------------------
  709|       |        // Yandex template ("\n\n" is defined as EOT token)
  710|       |
  711|     28|        for (size_t i = 0; i < chat.size(); i++) {
  ------------------
  |  Branch (711:28): [True: 24, False: 4]
  ------------------
  712|     24|            std::string role(chat[i]->role);
  713|     24|            if (role == "user") {
  ------------------
  |  Branch (713:17): [True: 12, False: 12]
  ------------------
  714|     12|                ss << " Пользователь: " << chat[i]->content << "\n\n";
  715|     12|            } else if (role == "assistant") {
  ------------------
  |  Branch (715:24): [True: 8, False: 4]
  ------------------
  716|      8|                ss << " Ассистент: " << chat[i]->content << "\n\n";
  717|      8|            }
  718|     24|        }
  719|       |
  720|       |        // Add generation prompt if needed
  721|      4|        if (add_ass) {
  ------------------
  |  Branch (721:13): [True: 4, False: 0]
  ------------------
  722|      4|            ss << " Ассистент:[SEP]";
  723|      4|        }
  724|    110|    } else if (tmpl == LLM_CHAT_TEMPLATE_BAILING || tmpl == LLM_CHAT_TEMPLATE_BAILING_THINK) {
  ------------------
  |  Branch (724:16): [True: 3, False: 107]
  |  Branch (724:53): [True: 4, False: 103]
  ------------------
  725|       |        // Bailing (Ling/Ring) template
  726|     42|        for (auto message : chat) {
  ------------------
  |  Branch (726:27): [True: 42, False: 7]
  ------------------
  727|     42|            std::string role(message->role);
  728|       |
  729|     42|            if (role == "user") {
  ------------------
  |  Branch (729:17): [True: 21, False: 21]
  ------------------
  730|     21|                role = "HUMAN";
  731|     21|            } else {
  732|     21|                std::transform(role.begin(), role.end(), role.begin(), ::toupper);
  733|     21|            }
  734|       |
  735|     42|            ss << "<role>" << role << "</role>" << message->content;
  736|     42|        }
  737|       |
  738|      7|        if (add_ass) {
  ------------------
  |  Branch (738:13): [True: 7, False: 0]
  ------------------
  739|      7|            ss << "<role>ASSISTANT</role>";
  740|       |
  741|      7|            if (tmpl == LLM_CHAT_TEMPLATE_BAILING_THINK) {
  ------------------
  |  Branch (741:17): [True: 4, False: 3]
  ------------------
  742|      4|                ss << "<think>";
  743|      4|            }
  744|      7|        }
  745|    103|    } else if (tmpl == LLM_CHAT_TEMPLATE_BAILING2) {
  ------------------
  |  Branch (745:16): [True: 5, False: 98]
  ------------------
  746|       |        // Bailing2 (Ling 2.0) template
  747|      5|        bool has_system = !chat.empty() && std::string(chat[0]->role) == "system";
  ------------------
  |  Branch (747:27): [True: 5, False: 0]
  |  Branch (747:44): [True: 5, False: 0]
  ------------------
  748|       |
  749|      5|        if (!has_system) {
  ------------------
  |  Branch (749:13): [True: 0, False: 5]
  ------------------
  750|      0|            ss << "<role>SYSTEM</role>detailed thinking off<|role_end|>";
  751|      0|        }
  752|       |
  753|     30|        for (auto message : chat) {
  ------------------
  |  Branch (753:27): [True: 30, False: 5]
  ------------------
  754|     30|            std::string role(message->role);
  755|       |
  756|     30|            if (role == "user") {
  ------------------
  |  Branch (756:17): [True: 15, False: 15]
  ------------------
  757|     15|                role = "HUMAN";
  758|     15|            } else {
  759|     15|                std::transform(role.begin(), role.end(), role.begin(), ::toupper);
  760|     15|            }
  761|       |
  762|     30|            ss << "<role>" << role << "</role>" << message->content << "<|role_end|>";
  763|     30|        }
  764|       |
  765|      5|        if (add_ass) {
  ------------------
  |  Branch (765:13): [True: 5, False: 0]
  ------------------
  766|      5|            ss << "<role>ASSISTANT</role>";
  767|      5|        }
  768|     98|    } else if (tmpl == LLM_CHAT_TEMPLATE_LLAMA4) {
  ------------------
  |  Branch (768:16): [True: 14, False: 84]
  ------------------
  769|       |        // Llama 4
  770|     84|        for (auto message : chat) {
  ------------------
  |  Branch (770:27): [True: 84, False: 14]
  ------------------
  771|     84|            std::string role(message->role);
  772|     84|            ss << "<|header_start|>" << role << "<|header_end|>\n\n" << trim(message->content) << "<|eot|>";
  773|     84|        }
  774|     14|        if (add_ass) {
  ------------------
  |  Branch (774:13): [True: 14, False: 0]
  ------------------
  775|     14|            ss << "<|header_start|>assistant<|header_end|>\n\n";
  776|     14|        }
  777|     84|    } else if (tmpl == LLM_CHAT_TEMPLATE_SMOLVLM) {
  ------------------
  |  Branch (777:16): [True: 1, False: 83]
  ------------------
  778|       |        // SmolVLM
  779|      1|        ss << "<|im_start|>"; // uses <|im_start|> as BOS, but the actual content is NOT chatml
  780|      6|        for (auto message : chat) {
  ------------------
  |  Branch (780:27): [True: 6, False: 1]
  ------------------
  781|      6|            std::string role(message->role);
  782|      6|            if (role == "system") {
  ------------------
  |  Branch (782:17): [True: 1, False: 5]
  ------------------
  783|      1|                ss << message->content << "\n\n";
  784|      5|            } else if (role == "user") {
  ------------------
  |  Branch (784:24): [True: 3, False: 2]
  ------------------
  785|      3|                ss << "User: " << message->content << "<end_of_utterance>\n";
  786|      3|            } else {
  787|      2|                ss << "Assistant: " << message->content << "<end_of_utterance>\n";
  788|      2|            }
  789|      6|        }
  790|      1|        if (add_ass) {
  ------------------
  |  Branch (790:13): [True: 1, False: 0]
  ------------------
  791|      1|            ss << "Assistant:";
  792|      1|        }
  793|     83|    } else if (tmpl == LLM_CHAT_TEMPLATE_DOTS1) {
  ------------------
  |  Branch (793:16): [True: 4, False: 79]
  ------------------
  794|       |        // dots.llm1.inst (DOTS1)
  795|     24|        for (auto message : chat) {
  ------------------
  |  Branch (795:27): [True: 24, False: 4]
  ------------------
  796|     24|            std::string role(message->role);
  797|     24|            if (role == "system") {
  ------------------
  |  Branch (797:17): [True: 4, False: 20]
  ------------------
  798|      4|                ss << "<|system|>" << message->content << "<|endofsystem|>";
  799|     20|            } else if (role == "user") {
  ------------------
  |  Branch (799:24): [True: 12, False: 8]
  ------------------
  800|     12|                ss << "<|userprompt|>" << message->content << "<|endofuserprompt|>";
  801|     12|            } else {
  802|      8|                ss << "<|response|>" << message->content << "<|endofresponse|>";
  803|      8|            }
  804|     24|        }
  805|      4|        if (add_ass) {
  ------------------
  |  Branch (805:13): [True: 4, False: 0]
  ------------------
  806|      4|            ss << "<|response|>";
  807|      4|        }
  808|     79|    } else if (tmpl == LLM_CHAT_TEMPLATE_HUNYUAN_MOE) {
  ------------------
  |  Branch (808:16): [True: 4, False: 75]
  ------------------
  809|       |        // tencent/Hunyuan-A13B-Instruct
  810|     24|        for (auto message : chat) {
  ------------------
  |  Branch (810:27): [True: 24, False: 4]
  ------------------
  811|     24|            std::string role(message->role);
  812|     24|            if (role == "system") {
  ------------------
  |  Branch (812:17): [True: 4, False: 20]
  ------------------
  813|      4|                ss << "<|startoftext|>" << message->content << "<|extra_4|>";
  814|     20|            } else if (role == "assistant") {
  ------------------
  |  Branch (814:24): [True: 8, False: 12]
  ------------------
  815|      8|                ss << message->content << "<|eos|>";
  816|     12|            } else {
  817|     12|                ss << "<|startoftext|>" << message->content << "<|extra_0|>";
  818|     12|            }
  819|     24|        }
  820|     75|    } else if (tmpl == LLM_CHAT_TEMPLATE_OPENAI_MOE) {
  ------------------
  |  Branch (820:16): [True: 4, False: 71]
  ------------------
  821|       |        // OpenAI MoE (based on Harmony chat template)
  822|     24|        for (auto message : chat) {
  ------------------
  |  Branch (822:27): [True: 24, False: 4]
  ------------------
  823|     24|            std::string role(message->role);
  824|     24|            ss << "<|start|>" << role << "<|message|>" << message->content;
  825|     24|            ss << (role == "assistant" ? "<|return|>" : "<|end|>");
  ------------------
  |  Branch (825:20): [True: 8, False: 16]
  ------------------
  826|     24|        }
  827|      4|        if (add_ass) {
  ------------------
  |  Branch (827:13): [True: 4, False: 0]
  ------------------
  828|      4|            ss << "<|start|>assistant";
  829|      4|        }
  830|     71|    } else if (tmpl == LLM_CHAT_TEMPLATE_HUNYUAN_DENSE) {
  ------------------
  |  Branch (830:16): [True: 4, False: 67]
  ------------------
  831|       |        // tencent/Hunyuan-4B-Instruct
  832|     28|        for (size_t i = 0; i < chat.size(); i++) {
  ------------------
  |  Branch (832:28): [True: 24, False: 4]
  ------------------
  833|     24|            std::string role(chat[i]->role);
  834|     24|            if (i == 0) {
  ------------------
  |  Branch (834:17): [True: 4, False: 20]
  ------------------
  835|      4|                if (role == "system") {
  ------------------
  |  Branch (835:21): [True: 4, False: 0]
  ------------------
  836|      4|                    ss << chat[i]->content << "<｜hy_place▁holder▁no▁3｜>";
  837|      4|                }
  838|      4|            }
  839|       |
  840|     24|            if (role == "assistant") {
  ------------------
  |  Branch (840:17): [True: 8, False: 16]
  ------------------
  841|      8|                ss << "<｜hy_Assistant｜>" << chat[i]->content << "<｜hy_place▁holder▁no▁2｜>";
  842|     16|            } else if (role == "user") {
  ------------------
  |  Branch (842:24): [True: 12, False: 4]
  ------------------
  843|     12|                ss << "<｜hy_User｜>" << chat[i]->content << "<｜hy_Assistant｜>";
  844|     12|            }
  845|     24|        }
  846|     67|    } else if (tmpl == LLM_CHAT_TEMPLATE_HUNYUAN_VL) {
  ------------------
  |  Branch (846:16): [True: 3, False: 64]
  ------------------
  847|       |        // tencent/HunyuanOCR & tencent/HunyuanVL
  848|      3|        ss << "<｜hy_begin▁of▁sentence｜>";
  849|     21|        for (size_t i = 0; i < chat.size(); i++) {
  ------------------
  |  Branch (849:28): [True: 18, False: 3]
  ------------------
  850|     18|            std::string role(chat[i]->role);
  851|     18|            if (i == 0 && role == "system") {
  ------------------
  |  Branch (851:17): [True: 3, False: 15]
  |  Branch (851:27): [True: 3, False: 0]
  ------------------
  852|      3|                ss << chat[i]->content << "<｜hy_place▁holder▁no▁3｜>";
  853|      3|                continue;
  854|      3|            }
  855|       |
  856|     15|            if (role == "user") {
  ------------------
  |  Branch (856:17): [True: 9, False: 6]
  ------------------
  857|      9|                ss << chat[i]->content << "<｜hy_User｜>";
  858|      9|            } else if (role == "assistant") {
  ------------------
  |  Branch (858:24): [True: 6, False: 0]
  ------------------
  859|      6|                ss << chat[i]->content << "<｜hy_Assistant｜>";
  860|      6|            }
  861|     15|        }
  862|     64|    } else if (tmpl == LLM_CHAT_TEMPLATE_KIMI_K2) {
  ------------------
  |  Branch (862:16): [True: 4, False: 60]
  ------------------
  863|       |        // moonshotai/Kimi-K2-Instruct
  864|     24|        for (auto message : chat) {
  ------------------
  |  Branch (864:27): [True: 24, False: 4]
  ------------------
  865|     24|            std::string role(message->role);
  866|     24|            if (role == "system") {
  ------------------
  |  Branch (866:17): [True: 4, False: 20]
  ------------------
  867|      4|                ss << "<|im_system|>system<|im_middle|>";
  868|     20|            } else if (role == "user") {
  ------------------
  |  Branch (868:24): [True: 12, False: 8]
  ------------------
  869|     12|                ss << "<|im_user|>user<|im_middle|>";
  870|     12|            } else if (role == "assistant") {
  ------------------
  |  Branch (870:24): [True: 8, False: 0]
  ------------------
  871|      8|                ss << "<|im_assistant|>assistant<|im_middle|>";
  872|      8|            } else if (role == "tool") {
  ------------------
  |  Branch (872:24): [True: 0, False: 0]
  ------------------
  873|      0|                ss << "<|im_system|>tool<|im_middle|>";
  874|      0|            }
  875|       |
  876|     24|            ss << message->content << "<|im_end|>";
  877|     24|        }
  878|      4|        if (add_ass) {
  ------------------
  |  Branch (878:13): [True: 4, False: 0]
  ------------------
  879|      4|            ss << "<|im_assistant|>assistant<|im_middle|>";
  880|      4|        }
  881|     60|    } else if (tmpl == LLM_CHAT_TEMPLATE_SEED_OSS) {
  ------------------
  |  Branch (881:16): [True: 17, False: 43]
  ------------------
  882|    102|        for (auto message: chat) {
  ------------------
  |  Branch (882:26): [True: 102, False: 17]
  ------------------
  883|    102|            std::string role(message->role);
  884|    102|            ss << "<seed:bos>" << role << "\n" << (role == "assistant" ? trim(message->content) : message->content) << "<seed:eos>";
  ------------------
  |  Branch (884:52): [True: 34, False: 68]
  ------------------
  885|    102|        }
  886|     17|        if (add_ass) {
  ------------------
  |  Branch (886:13): [True: 17, False: 0]
  ------------------
  887|     17|            ss << "<seed:bos>assistant\n";
  888|     17|        }
  889|     43|    } else if (tmpl == LLM_CHAT_TEMPLATE_GROK_2) {
  ------------------
  |  Branch (889:16): [True: 26, False: 17]
  ------------------
  890|    156|        for (auto message : chat) {
  ------------------
  |  Branch (890:27): [True: 156, False: 26]
  ------------------
  891|    156|            std::string role(message->role);
  892|    156|            if (role == "system") {
  ------------------
  |  Branch (892:17): [True: 26, False: 130]
  ------------------
  893|     26|                ss << "System: " << trim(message->content) << "<|separator|>\n\n";
  894|    130|            } else if (role == "user") {
  ------------------
  |  Branch (894:24): [True: 78, False: 52]
  ------------------
  895|     78|                ss << "Human: " << trim(message->content) << "<|separator|>\n\n";
  896|     78|            } else if (role == "assistant") {
  ------------------
  |  Branch (896:24): [True: 52, False: 0]
  ------------------
  897|     52|                ss << "Assistant: " << message->content << "<|separator|>\n\n";
  898|     52|            }
  899|    156|        }
  900|     26|        if (add_ass) {
  ------------------
  |  Branch (900:13): [True: 26, False: 0]
  ------------------
  901|     26|            ss << "Assistant:";
  902|     26|        }
  903|     26|    }else if (tmpl == LLM_CHAT_TEMPLATE_PANGU_EMBED) {
  ------------------
  |  Branch (903:15): [True: 12, False: 5]
  ------------------
  904|       |        // [unused9]系统：xxx[unused10]
  905|       |        // [unused9]用户：xxx[unused10]
  906|       |        // [unused9]助手：xxx[unused10]
  907|       |        // ...
  908|     84|        for (size_t i = 0; i < chat.size(); ++i) {
  ------------------
  |  Branch (908:28): [True: 72, False: 12]
  ------------------
  909|     72|            const auto & msg = chat[i];
  910|     72|            const std::string & role = msg->role;
  911|     72|            const std::string & content = msg->content;
  912|       |
  913|     72|            if (i == 0 && role != "system") {
  ------------------
  |  Branch (913:17): [True: 12, False: 60]
  |  Branch (913:27): [True: 0, False: 12]
  ------------------
  914|      0|                ss << "[unused9]系统：[unused10]";
  915|      0|            }
  916|       |
  917|     72|            if (role == "system") {
  ------------------
  |  Branch (917:17): [True: 12, False: 60]
  ------------------
  918|     12|                ss << "[unused9]系统：" << content << "[unused10]";
  919|     60|            } else if (role == "user") {
  ------------------
  |  Branch (919:24): [True: 36, False: 24]
  ------------------
  920|     36|                ss << "[unused9]用户：" << content << "[unused10]";
  921|     36|            } else if (role == "assistant") {
  ------------------
  |  Branch (921:24): [True: 24, False: 0]
  ------------------
  922|     24|                ss << "[unused9]助手：" << content << "[unused10]";
  923|     24|            } else if (role == "tool") {
  ------------------
  |  Branch (923:24): [True: 0, False: 0]
  ------------------
  924|      0|                ss << "[unused9]工具：" << content << "[unused10]";
  925|      0|            } else if (role == "function") {
  ------------------
  |  Branch (925:24): [True: 0, False: 0]
  ------------------
  926|      0|                ss << "[unused9]方法：" << content << "[unused10]";
  927|      0|            }
  928|     72|        }
  929|     12|        if (add_ass) {
  ------------------
  |  Branch (929:13): [True: 12, False: 0]
  ------------------
  930|     12|            ss << "[unused9]助手：";
  931|     12|        }
  932|     12|    } else if (tmpl == LLM_CHAT_TEMPLATE_SOLAR_OPEN) {
  ------------------
  |  Branch (932:16): [True: 5, False: 0]
  ------------------
  933|     30|        for (auto message : chat) {
  ------------------
  |  Branch (933:27): [True: 30, False: 5]
  ------------------
  934|     30|            std::string role(message->role);
  935|     30|            ss << "<|begin|>" << role << "<|content|>" << message->content << "<|end|>";
  936|     30|        }
  937|      5|        if (add_ass) {
  ------------------
  |  Branch (937:13): [True: 5, False: 0]
  ------------------
  938|      5|            ss << "<|begin|>assistant";
  939|      5|        }
  940|      5|    } else {
  941|       |        // template not supported
  942|      0|        return -1;
  943|      0|    }
  944|    913|    dest = ss.str();
  945|    913|    return dest.size();
  946|    913|}
llama-chat.cpp:_ZZ24llm_chat_detect_templateRKNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEEENK3$_0clEPKc:
   96|  33.9k|    auto tmpl_contains = [&tmpl](const char * haystack) -> bool {
   97|  33.9k|        return tmpl.find(haystack) != std::string::npos;
   98|  33.9k|    };
llama-chat.cpp:_ZL4trimRKNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE:
   16|  2.47k|static std::string trim(const std::string & str) {
   17|  2.47k|    size_t start = 0;
   18|  2.47k|    size_t end = str.size();
   19|   106k|    while (start < end && isspace(static_cast<unsigned char>(str[start]))) {
  ------------------
  |  Branch (19:12): [True: 104k, False: 1.85k]
  |  Branch (19:27): [True: 103k, False: 612]
  ------------------
   20|   103k|        start += 1;
   21|   103k|    }
   22|  1.68M|    while (end > start && isspace(static_cast<unsigned char>(str[end - 1]))) {
  ------------------
  |  Branch (22:12): [True: 1.68M, False: 1.85k]
  |  Branch (22:27): [True: 1.68M, False: 612]
  ------------------
   23|  1.68M|        end -= 1;
   24|  1.68M|    }
   25|  2.47k|    return str.substr(start, end - start);
   26|  2.47k|}

llama_chat_apply_template:
  476|  1.64k|                                 int32_t   length) {
  477|  1.64k|    const std::string curr_tmpl(tmpl == nullptr ? "chatml" : tmpl);
  ------------------
  |  Branch (477:33): [True: 0, False: 1.64k]
  ------------------
  478|       |
  479|       |    // format the chat to string
  480|  1.64k|    std::vector<const llama_chat_message *> chat_vec;
  481|  1.64k|    chat_vec.resize(n_msg);
  482|  11.5k|    for (size_t i = 0; i < n_msg; i++) {
  ------------------
  |  Branch (482:24): [True: 9.85k, False: 1.64k]
  ------------------
  483|  9.85k|        chat_vec[i] = &chat[i];
  484|  9.85k|    }
  485|       |
  486|  1.64k|    std::string formatted_chat;
  487|  1.64k|    llm_chat_template detected_tmpl = llm_chat_detect_template(curr_tmpl);
  488|  1.64k|    if (detected_tmpl == LLM_CHAT_TEMPLATE_UNKNOWN) {
  ------------------
  |  Branch (488:9): [True: 730, False: 913]
  ------------------
  489|    730|        return -1;
  490|    730|    }
  491|    913|    int32_t res = llm_chat_apply_template(detected_tmpl, chat_vec, formatted_chat, add_ass);
  492|    913|    if (res < 0) {
  ------------------
  |  Branch (492:9): [True: 0, False: 913]
  ------------------
  493|      0|        return res;
  494|      0|    }
  495|    913|    if (buf && length > 0) {
  ------------------
  |  Branch (495:9): [True: 913, False: 0]
  |  Branch (495:16): [True: 913, False: 0]
  ------------------
  496|    913|        strncpy(buf, formatted_chat.c_str(), length);
  497|    913|    }
  498|    913|    return res;
  499|    913|}

