From 368d0363eb2666425341fdefac4420728fcbca60 Mon Sep 17 00:00:00 2001 From: Ashlyn Date: Sun, 11 Oct 2026 10:02:09 -0500 Subject: [PATCH] arch: Gemma 4's reply format into arch/common, for both Gemma 4 plugins Two plugins are about to serve Gemma 4 -- gemma4_bf16 the CT-INT4 dense release and arch/gemma4 the bf16 / fp8-expert containers -- and the reply format is the model's, not the weights': the thought channel, and calls written as a name and one object in Gemma's dialect of JSON. One declaration in the shared header library (installed as ), so the two cannot drift apart and the reply-format tests read what both export. The declaration moves unchanged, from gemma4_bf16::chat_format_decl() to rad::arch::gemma4_chat_format(); chatparse_gemma4_test follows it. Co-Authored-By: Claude Opus 5.5 --- .../rad_gemma4_chat.h} | 16 +++++++++------ arch/gemma4_bf16/gemma4_bf16.cpp | 7 +++---- tests/chatparse_gemma4_test.cpp | 20 +++++++++---------- 3 files changed, 23 insertions(+), 20 deletions(-) rename arch/{gemma4_bf16/gemma4_chat_format.h => common/rad_gemma4_chat.h} (81%) diff --git a/arch/gemma4_bf16/gemma4_chat_format.h b/arch/common/rad_gemma4_chat.h similarity index 81% rename from arch/gemma4_bf16/gemma4_chat_format.h rename to arch/common/rad_gemma4_chat.h index 7520d298..74dc4ae7 100644 --- a/arch/gemma4_bf16/gemma4_chat_format.h +++ b/arch/common/rad_gemma4_chat.h @@ -1,4 +1,4 @@ -/* gemma4_chat_format.h -- how Gemma 4 spells a turn: its reasoning and its tool calls, as data. +/* rad_gemma4_chat.h -- how Gemma 4 spells a turn: its reasoning and its tool calls, as data. * * The chat template is one llama.cpp reads with a hand-written handler, so the template analysis * finds nothing in it, and the engine would serve the model with no reasoning split and refuse @@ -16,14 +16,17 @@ * the answer, and a lone . `reasoning_anywhere` reads the first as reasoning and drops * the second, so neither reaches the content. * - * Kept in a header of its own so the reply-format tests read the declaration the plugin exports - * rather than a copy of it. */ + * Here rather than in one plugin because two plugins serve Gemma 4 -- arch/gemma4 (bf16 and fp8 + * experts) and arch/gemma4_bf16 (the CT-INT4 dense release) -- and the format is the model's, not + * the weights'. One declaration, so the two cannot drift apart, and so the reply-format tests read + * the declaration the plugins export rather than a copy of it. */ #pragma once #include "rad_builder.h" -namespace gemma4_bf16 { +namespace rad { +namespace arch { -inline const RadChatFormat* chat_format_decl() { +inline const RadChatFormat* gemma4_chat_format() { static const char* const kBreaks[] = { "<|tool_call>" }; static const char* const kTriggers[] = { "<|tool_call>" }; static const RadChatFormat f = [] { @@ -57,4 +60,5 @@ inline const RadChatFormat* chat_format_decl() { return &f; } -} /* namespace gemma4_bf16 */ +} /* namespace arch */ +} /* namespace rad */ diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp index a12acb94..f61206cc 100644 --- a/arch/gemma4_bf16/gemma4_bf16.cpp +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -9,8 +9,7 @@ * cap * tanh(x / cap). */ #include - -#include "gemma4_chat_format.h" +#include #include #include @@ -881,8 +880,8 @@ static void step(RadCtx* c, const RadBatch* batch) { } } -/* The reply format: gemma4_chat_format.h. */ -const RadChatFormat* chat_format(const RadModelMeta*) { return chat_format_decl(); } +/* The reply format: the model's, shared with arch/gemma4 (arch/rad_gemma4_chat.h). */ +const RadChatFormat* chat_format(const RadModelMeta*) { return gemma4_chat_format(); } #ifndef RAD_ARCH_NO_EXPORTS RAD_ARCH_PLUGIN(gemma4_bf16, "gemma4", "compressed-tensors", "0.1.0", diff --git a/tests/chatparse_gemma4_test.cpp b/tests/chatparse_gemma4_test.cpp index 6e62a944..8a6a36d3 100644 --- a/tests/chatparse_gemma4_test.cpp +++ b/tests/chatparse_gemma4_test.cpp @@ -2,7 +2,7 @@ * name and ONE object in a dialect of JSON. * * Gemma 4's template is one llama.cpp reads with a hand-written handler, so the template analysis - * finds no format in it and the plugin declares one (arch/gemma4_bf16/gemma4_chat_format.h): + * finds no format in it and the plugins declare one (arch/common/rad_gemma4_chat.h): * * <|channel>thought\n ... reasoning * <|tool_call>call:NAME{key:<|"|>text<|"|>,n:3} a call, repeated for more @@ -28,7 +28,7 @@ #include "text/chatparse.h" #include "text/tokenizer.h" -#include "../arch/gemma4_bf16/gemma4_chat_format.h" +#include #include @@ -60,7 +60,7 @@ static std::string fixture() { static const Model& gemma() { static const Model m = { "gemma4", "gemma4", "", "", { "", "", "<|tool_response>" }, - gemma4_bf16::chat_format_decl() }; + rad::arch::gemma4_chat_format() }; return m; } @@ -136,7 +136,7 @@ static std::shared_ptr parser(const std::string& gp, bool too ChatFormat f; std::string why; std::shared_ptr r; - if (chat_format_from_abi(gemma4_bf16::chat_format_decl(), &f, &why) < 0 || + if (chat_format_from_abi(rad::arch::gemma4_chat_format(), &f, &why) < 0 || ReplyFormat::compile(f, &r, &why) < 0) ::radtest::fail(__FILE__, __LINE__, "gemma4 format: " + why); return r; @@ -167,7 +167,7 @@ static const std::string kAfter = "<|channel>thought\n"; TEST(the_declaration_is_a_format_the_parser_runs) { ChatFormat f; std::string why; - CHECK_OK(chat_format_from_abi(gemma4_bf16::chat_format_decl(), &f, &why)); + CHECK_OK(chat_format_from_abi(rad::arch::gemma4_chat_format(), &f, &why)); CHECK_OK(chat_format_check(f, &why)); CHECK(f.object_args()); CHECK(f.reasoning_anywhere); @@ -189,7 +189,7 @@ TEST(the_declaration_is_a_format_the_parser_runs) { * the tagged family, no dialect, reasoning at the start only -- and Gemma's declaration read that * way is refused by name rather than run wrongly. */ TEST(an_older_struct_reads_the_object_fields_as_zero) { - RadChatFormat c = *gemma4_bf16::chat_format_decl(); + RadChatFormat c = *rad::arch::gemma4_chat_format(); c.struct_size = (uint32_t)offsetof(RadChatFormat, args_form); ChatFormat f; std::string why; @@ -204,7 +204,7 @@ TEST(an_older_struct_reads_the_object_fields_as_zero) { TEST(an_object_format_the_parser_cannot_read_is_refused_by_name) { auto refused = [](void (*edit)(RadChatFormat&), const char* word) { - RadChatFormat c = *gemma4_bf16::chat_format_decl(); + RadChatFormat c = *rad::arch::gemma4_chat_format(); edit(c); ChatFormat f; std::string why; @@ -662,7 +662,7 @@ static int admits(const std::string& gbnf, const std::string& prefix, const std: static std::string grammar_for(const ChatGrammarRequest& gq) { ChatFormat f; std::string why; - CHECK_OK(chat_format_from_abi(gemma4_bf16::chat_format_decl(), &f, &why)); + CHECK_OK(chat_format_from_abi(rad::arch::gemma4_chat_format(), &f, &why)); ChatGrammar g; CHECK_OK(chat_format_grammar(f, gq, &g, &why)); return g.text; @@ -678,7 +678,7 @@ TEST(the_call_grammar_admits_what_the_model_writes_and_refuses_the_rest) { gq.generation_prompt = b->prompt().generation_prompt; ChatFormat f; std::string why; - CHECK_OK(chat_format_from_abi(gemma4_bf16::chat_format_decl(), &f, &why)); + CHECK_OK(chat_format_from_abi(rad::arch::gemma4_chat_format(), &f, &why)); ChatGrammar g; CHECK_OK(chat_format_grammar(f, gq, &g, &why)); CHECK(g.lazy); @@ -791,7 +791,7 @@ TEST(the_chat_bridge_serves_gemma_from_its_declaration) { REQUIRE(v != nullptr); CHECK(v->is_eog(6)); /* <|tool_response>: after its calls the model stops there */ server::ChatTemplateBridge br; - CHECK_OK(br.init(*v, gemma4_bf16::chat_format_decl(), "gemma4_bf16")); + CHECK_OK(br.init(*v, rad::arch::gemma4_chat_format(), "gemma4_bf16")); CHECK(br.supports_tools()); CHECK(br.tool_refusal().empty());