Archived
convert: --chat-template, bake a template a release ships none of
DeepSeek's release carries no chat_template.jinja and its tokenizer_config.json names none, so an HF-direct container answers /v1/chat/completions with 501 unless the serve passes --override-chat-template. The converter now takes the template itself and bakes it into the vocab section, overriding whatever the checkpoint carried or lacked. Trailing newlines are stripped as for a sibling chat_template.jinja, and an unreadable or empty file is refused rather than skipped. Both paths are covered: text_test reads the file, convert_test converts the tiny checkpoint and reads the template back out of the container. (cherry picked from commit 6381f61794cd61d1f86925fd23ea61e1b1544dfe)
This commit is contained in:
1 parent
73bee2dcc2
commit
b64b0de7de
5 files changed
+90
-1
No files matched your search
@@ -320,6 +320,12 @@ int parse_tokenizer_json(const char* path, VocabBuild& out);
|
||||
/* Same, from an in-memory buffer, for a tokenizer.json that arrived some other way. */
|
||||
int parse_tokenizer_json_buf(std::string_view json, const char* origin, VocabBuild& out);
|
||||
|
||||
/* Read a standalone chat template file into out.chat_template, trailing newlines stripped.
|
||||
* A release that ships no template (DeepSeek's does not) needs one from elsewhere; the caller
|
||||
* passes the file. An unreadable or empty file is an error, never a silent skip. Returns
|
||||
* RAD_OK, or RAD_E_IO when the file cannot be read. */
|
||||
int read_chat_template_path(const char* path, VocabBuild& out);
|
||||
|
||||
/* Build a VocabBuild from a GGUF's tokenizer.ggml.* metadata.
|
||||
*
|
||||
* GGUF has no declared chain, only `tokenizer.ggml.pre`, a name llama.cpp derived by hashing the
|
||||
|
||||
@@ -638,6 +638,27 @@ void read_config(const std::string& dir, VocabBuild& out) {
|
||||
|
||||
} /* namespace */
|
||||
|
||||
int read_chat_template_path(const char* path, VocabBuild& out) {
|
||||
if (!path) return RAD_E_INVAL;
|
||||
std::ifstream f(path);
|
||||
if (!f) {
|
||||
RAD_ERR("tokenizer: cannot read chat template %s", path);
|
||||
return RAD_E_IO;
|
||||
}
|
||||
std::ostringstream ss;
|
||||
ss << f.rdbuf();
|
||||
std::string t = ss.str();
|
||||
while (!t.empty() && (t.back() == '\n' || t.back() == '\r')) t.pop_back();
|
||||
if (t.empty()) {
|
||||
RAD_ERR("tokenizer: chat template %s is empty", path);
|
||||
return RAD_E_IO;
|
||||
}
|
||||
RAD_INFO("tokenizer: chat template from %s (%zu bytes), overriding the checkpoint's",
|
||||
path, t.size());
|
||||
out.chat_template = std::move(t);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* ================================================================== parse_tokenizer_json */
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "format/encoding.h"
|
||||
#include "format/radfile.h"
|
||||
#include "gen_config.h"
|
||||
#include "text/tokenizer.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
@@ -967,4 +968,27 @@ TEST(an_fp8_kv_cache_declares) {
|
||||
CHECK(exists(d / "k.rad"));
|
||||
}
|
||||
|
||||
/* --chat-template bakes a template into the container's vocab section, for a release that ships
|
||||
* none (DeepSeek's). Without the flag the container carries what the checkpoint carried. A missing
|
||||
* file is refused, not silently skipped. */
|
||||
TEST(chat_template_flag_bakes_the_template) {
|
||||
World& w = world();
|
||||
REQUIRE(w.ok);
|
||||
tiny::TempDir d("convert_ct");
|
||||
const std::string tmpl = d / "chat_template.jinja";
|
||||
REQUIRE(tiny::write_text(tmpl, "{%- raw %}{{ messages }}{%- endraw %}\n\n"));
|
||||
std::string log;
|
||||
REQUIRE_EQ(convert(w.ckpt, { "-o", d / "with.rad", "--recipe", w.recipe,
|
||||
"--chat-template", tmpl }, &log), 0);
|
||||
RadFile f;
|
||||
REQUIRE_EQ(f.open((d / "with.rad").c_str()), RAD_OK);
|
||||
VocabBuild vb;
|
||||
REQUIRE_EQ(vocab_deserialize(f.base(), (uint64_t)f.size(), f.vocab().h, vb), RAD_OK);
|
||||
CHECK_EQ(vb.chat_template, std::string("{%- raw %}{{ messages }}{%- endraw %}"));
|
||||
|
||||
std::string bad;
|
||||
CHECK_EQ(convert(w.ckpt, { "-o", d / "bad.rad", "--recipe", w.recipe,
|
||||
"--chat-template", d / "absent.jinja" }, &bad), 1);
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
@@ -21,6 +21,8 @@
|
||||
*/
|
||||
#include "rad_test.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "text/gguf.h"
|
||||
#include "text/tokenizer.h"
|
||||
#include "text/tokenizer_nfc.h"
|
||||
@@ -2240,4 +2242,29 @@ TEST(the_normal_forms_agree_with_an_independent_implementation) {
|
||||
CHECK(rows > 2500);
|
||||
}
|
||||
|
||||
/* A release that ships no chat template (DeepSeek's) is why --chat-template exists: the file is
|
||||
* read, trailing newlines stripped exactly as the checkpoint's own file is, and an unreadable or
|
||||
* empty file is refused rather than skipped. */
|
||||
TEST(read_chat_template_path_strips_newlines_and_refuses_empty) {
|
||||
VocabBuild vb;
|
||||
const std::string p = radtest::tmp_path("chat_template.jinja");
|
||||
{
|
||||
std::ofstream f(p);
|
||||
f << "{%- raw %}{{ messages }}{%- endraw %}\n\n";
|
||||
}
|
||||
CHECK_OK(read_chat_template_path(p.c_str(), vb));
|
||||
CHECK_EQ(vb.chat_template, std::string("{%- raw %}{{ messages }}{%- endraw %}"));
|
||||
|
||||
vb.chat_template.clear();
|
||||
{
|
||||
std::ofstream f(p);
|
||||
f << "\n\r\n";
|
||||
}
|
||||
CHECK_EQ(read_chat_template_path(p.c_str(), vb), (int)RAD_E_IO);
|
||||
CHECK(vb.chat_template.empty());
|
||||
|
||||
CHECK_EQ(read_chat_template_path(radtest::tmp_path("absent.jinja").c_str(), vb), (int)RAD_E_IO);
|
||||
std::remove(p.c_str());
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
+12
-1
@@ -132,6 +132,10 @@ void usage() {
|
||||
" --imatrix F llama.cpp imatrix: the expert-popularity profile, the column\n"
|
||||
" importances handed to each quantiser, and the weighting of the\n"
|
||||
" error report\n"
|
||||
" --chat-template F a jinja template baked as the container's chat template,\n"
|
||||
" overriding the checkpoint's own (or its absence). A release that\n"
|
||||
" ships none (DeepSeek's) needs one, or /v1/chat/completions answers\n"
|
||||
" 501. Trailing newlines are stripped\n"
|
||||
" --set K=V a metadata key, seen by declare AND written into the container,\n"
|
||||
" so a convert-time choice that must also hold at serve time is\n"
|
||||
" stated once. Repeatable\n"
|
||||
@@ -201,7 +205,7 @@ int for_row_pieces(int64_t n, bool parallel, std::string* why, F fn) {
|
||||
} /* namespace */
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::string in, out, arch_override, home = rad_home(), tokjson, imat_path, draft_dir;
|
||||
std::string in, out, arch_override, home = rad_home(), tokjson, imat_path, draft_dir, chat_tmpl;
|
||||
std::string tokjson2, config_dir;
|
||||
std::vector<std::string> add_files;
|
||||
std::vector<std::string> hierarchy = rad_hierarchy_from_env();
|
||||
@@ -245,6 +249,7 @@ int main(int argc, char** argv) {
|
||||
else if (a == "--add") add_files.push_back(next("--add"));
|
||||
else if (a == "--config") config_dir = next("--config");
|
||||
else if (a == "--imatrix") imat_path = next("--imatrix");
|
||||
else if (a == "--chat-template") chat_tmpl = next("--chat-template");
|
||||
else if (a == "--set") {
|
||||
const std::string kv = next("--set");
|
||||
const size_t eq = kv.find('=');
|
||||
@@ -967,6 +972,12 @@ int main(int argc, char** argv) {
|
||||
w.abort();
|
||||
return 1;
|
||||
}
|
||||
/* The operator's template replaces whatever the checkpoint carried (or lacked), so a release
|
||||
* that ships none still bakes a container that serves /v1/chat/completions. */
|
||||
if (!chat_tmpl.empty() && read_chat_template_path(chat_tmpl.c_str(), vb) < 0) {
|
||||
w.abort();
|
||||
return 1;
|
||||
}
|
||||
/* Serialised at section offset 0; finish() rebases once the string blob is sized. */
|
||||
std::vector<uint8_t> vocab_blob;
|
||||
if (vocab_serialize(vb, sink, 0, vocab_blob) < 0) { w.abort(); return 1; }
|
||||
|
||||
Reference in new issue
Block a user