Skip to content

Commit 92164cf

Browse files
committed
remove built-in cpp chat template
1 parent 9a0afa8 commit 92164cf

5 files changed

Lines changed: 7 additions & 99 deletions

File tree

common/CMakeLists.txt

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -47,52 +47,6 @@ if (BUILD_SHARED_LIBS)
4747
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
4848
endif()
4949

50-
#
51-
# inline chat templates
52-
#
53-
# Built-in Jinja chat templates for model architectures whose GGUF files ship
54-
# without an embedded chat_template. The header is generated from the manifest
55-
# common/chat-inline-templates.h.in into the build directory and included by
56-
# common/chat.cpp (a quoted include resolves to the generated copy because no
57-
# chat-inline-templates.h exists next to chat.cpp).
58-
59-
set(CHAT_INLINE_TEMPLATES_MANIFEST ${CMAKE_CURRENT_SOURCE_DIR}/chat-inline-templates.h.in)
60-
set(CHAT_INLINE_TEMPLATES_GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
61-
set(CHAT_INLINE_TEMPLATES_HEADER ${CHAT_INLINE_TEMPLATES_GEN_DIR}/chat-inline-templates.h)
62-
set(CHAT_INLINE_TEMPLATES_SCRIPT ${PROJECT_SOURCE_DIR}/scripts/gen-chat-inline-templates.py)
63-
64-
# every template the manifest may reference, so the header regenerates on edits
65-
file(GLOB CHAT_INLINE_TEMPLATE_FILES ${PROJECT_SOURCE_DIR}/models/templates/*.jinja)
66-
67-
find_package(Python3 COMPONENTS Interpreter QUIET)
68-
69-
if (Python3_FOUND)
70-
add_custom_command(
71-
OUTPUT ${CHAT_INLINE_TEMPLATES_HEADER}
72-
COMMAND ${CMAKE_COMMAND} -E make_directory ${CHAT_INLINE_TEMPLATES_GEN_DIR}
73-
COMMAND ${Python3_EXECUTABLE} ${CHAT_INLINE_TEMPLATES_SCRIPT}
74-
--manifest ${CHAT_INLINE_TEMPLATES_MANIFEST}
75-
--repo-root ${PROJECT_SOURCE_DIR}
76-
--output ${CHAT_INLINE_TEMPLATES_HEADER}
77-
DEPENDS ${CHAT_INLINE_TEMPLATES_MANIFEST}
78-
${CHAT_INLINE_TEMPLATES_SCRIPT}
79-
${CHAT_INLINE_TEMPLATE_FILES}
80-
COMMENT "Generating chat-inline-templates.h"
81-
VERBATIM
82-
)
83-
else()
84-
message(WARNING "Python3 not found: inline chat templates disabled (generating empty chat-inline-templates.h)")
85-
file(MAKE_DIRECTORY ${CHAT_INLINE_TEMPLATES_GEN_DIR})
86-
file(WRITE ${CHAT_INLINE_TEMPLATES_HEADER}
87-
"// AUTO-GENERATED fallback (Python3 not found) - no inline templates available.
88-
#pragma once
89-
#include <cstddef>
90-
struct common_chat_inline_template_entry { const char * arch; const char * tmpl; };
91-
static const common_chat_inline_template_entry COMMON_CHAT_INLINE_TEMPLATES[] = { { nullptr, nullptr } };
92-
static const size_t COMMON_CHAT_INLINE_TEMPLATES_COUNT = 0;
93-
")
94-
endif()
95-
9650
#
9751
# llama-common
9852
#
@@ -174,7 +128,6 @@ set_target_properties(${TARGET} PROPERTIES
174128
)
175129

176130
target_include_directories(${TARGET} PUBLIC . ../vendor)
177-
target_include_directories(${TARGET} PRIVATE ${CHAT_INLINE_TEMPLATES_GEN_DIR})
178131
target_compile_features (${TARGET} PUBLIC cxx_std_17)
179132

180133
if (BUILD_SHARED_LIBS)

common/chat-inline-templates.h.in

Lines changed: 0 additions & 22 deletions
This file was deleted.

common/chat.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -653,15 +653,6 @@ std::string common_chat_templates_source(const struct common_chat_templates * tm
653653
return tmpls->template_default->source();
654654
}
655655

656-
std::string common_chat_template_inline(const std::string & arch) {
657-
for (size_t i = 0; i < COMMON_CHAT_INLINE_TEMPLATES_COUNT; ++i) {
658-
if (arch == COMMON_CHAT_INLINE_TEMPLATES[i].arch) {
659-
return COMMON_CHAT_INLINE_TEMPLATES[i].tmpl;
660-
}
661-
}
662-
return "";
663-
}
664-
665656
common_chat_templates_ptr common_chat_templates_init(const struct llama_model * model,
666657
const std::string & chat_template_override,
667658
const std::string & bos_token_override,
@@ -686,21 +677,6 @@ common_chat_templates_ptr common_chat_templates_init(const struct llama_model *
686677
default_template_src = chat_template_override;
687678
}
688679

689-
// No template override and the model has no embedded chat template: fall back
690-
// to a built-in inline template selected by the model architecture, if any.
691-
if (chat_template_override.empty() && default_template_src.empty() && template_tool_use_src.empty() && model != nullptr) {
692-
char arch_buf[128] = { 0 };
693-
if (llama_model_meta_val_str(model, "general.architecture", arch_buf, sizeof(arch_buf)) > 0) {
694-
std::string inline_template = common_chat_template_inline(arch_buf);
695-
if (!inline_template.empty()) {
696-
LOG_INF("%s: no chat template found in model, using built-in inline template for arch '%s'\n",
697-
__func__, arch_buf);
698-
default_template_src = inline_template;
699-
has_explicit_template = true;
700-
}
701-
}
702-
}
703-
704680
if (default_template_src.empty() || default_template_src == "chatml") {
705681
if (!template_tool_use_src.empty()) {
706682
default_template_src = template_tool_use_src;

common/chat.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,6 @@ common_chat_templates_ptr common_chat_templates_init(const struct llama_model *
261261
bool common_chat_templates_was_explicit(const struct common_chat_templates * tmpls);
262262
std::string common_chat_templates_source(const struct common_chat_templates * tmpls, const std::string & variant = "");
263263

264-
// Returns the built-in ("inline") chat template registered for the given model
265-
// architecture name (see common/chat-inline-templates.h.in), or an empty string
266-
// if none is registered. Used as a fallback for models that ship without an
267-
// embedded chat_template and when no template override is supplied.
268-
std::string common_chat_template_inline(const std::string & arch);
269-
270264
struct common_chat_params common_chat_templates_apply(const struct common_chat_templates * tmpls,
271265
const struct common_chat_templates_inputs & inputs);
272266

conversion/deepseek.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import re
5+
from pathlib import Path
56

67
from typing import Any, Callable, Iterable, TYPE_CHECKING
78

@@ -489,6 +490,12 @@ def __init__(self, *args, **kwargs):
489490
if type(self)._skipped_mtp_tensors:
490491
logger.info("Skipping %d DeepSeek-V4 MTP tensor(s) for conversion v0", type(self)._skipped_mtp_tensors)
491492

493+
# add a default chat template; if the model has a built-in template, it will be overridden later
494+
template_path = Path(__file__).parent.parent / "models" / "templates" / "deepseek-ai-DeepSeek-V4.jinja"
495+
if template_path.is_file():
496+
with open(template_path, "r", encoding="utf-8") as f:
497+
self.gguf_writer.add_chat_template(f.read())
498+
492499
@classmethod
493500
def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:
494501
name, _ = item

0 commit comments

Comments
 (0)