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:
baKon authored and StillDeadcode committed 2026-10-28 13:50:16 +01:00
1 parent 73bee2dcc2
commit b64b0de7de
5 files changed
+90 -1

No files matched your search

+24
View File
@@ -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()
+27
View File
@@ -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()