diff --git a/arch/spark25_bf16/chat.h b/arch/spark25_bf16/chat.h new file mode 100644 index 00000000..fbb1385b --- /dev/null +++ b/arch/spark25_bf16/chat.h @@ -0,0 +1,35 @@ +/* Spark's reply format: the checkpoint's template writes these tags around each call. */ +#pragma once +#include "rad_builder.h" + +namespace spark25_bf16 { + +inline const RadChatFormat* spark_chat_format() { + static const char* const breaks[] = {""}; + static const char* const triggers[] = {""}; + static const RadChatFormat f = [] { + RadChatFormat x{}; + x.struct_size = sizeof(x); + x.name = "plugin:spark25"; + x.reasoning_open = ""; + x.reasoning_close = ""; + x.reasoning_breaks = breaks; + x.n_reasoning_breaks = 1; + x.reasoning_in_prompt = RAD_CHAT_THINK_FROM_PROMPT; + /* There is no delimiter between the call opener and the function name. */ + x.name_prefix = ""; + x.call_end = ""; + x.arg_prefix = ""; + x.arg_name_suffix = ""; + x.arg_value_prefix = ""; + x.arg_value_suffix = ""; + x.string_value = RAD_CHAT_VALUE_RAW; + x.other_value = RAD_CHAT_VALUE_JSON; + x.triggers = triggers; + x.n_triggers = 1; + return x; + }(); + return &f; +} + +} /* namespace spark25_bf16 */ diff --git a/arch/spark25_bf16/spark25_bf16.cpp b/arch/spark25_bf16/spark25_bf16.cpp index 66e9601c..f87009d8 100644 --- a/arch/spark25_bf16/spark25_bf16.cpp +++ b/arch/spark25_bf16/spark25_bf16.cpp @@ -10,6 +10,7 @@ * accumulate in f32; rmsnorm reads that stream and writes bf16 for the linear projections. */ #include +#include "chat.h" #include #include @@ -315,7 +316,9 @@ static void step(RadCtx* c, const RadBatch* batch) { } } +static const RadChatFormat* chat_format(const RadModelMeta*) { return spark_chat_format(); } } #ifndef RAD_ARCH_NO_EXPORTS RAD_ARCH_PLUGIN(spark25_bf16, "spark25", "", "0.1.0", "Spark X2.6 BF16") +RAD_ARCH_CHAT_FORMAT(spark25_bf16) #endif diff --git a/tests/spark25_arch_test.cpp b/tests/spark25_arch_test.cpp index c3add59d..ae5c190d 100644 --- a/tests/spark25_arch_test.cpp +++ b/tests/spark25_arch_test.cpp @@ -35,6 +35,7 @@ TEST(layer_schedule_rope_and_window_follow_the_checkpoint) { RadBuilder b; RadBuildCtx ctx = build_ctx(); REQUIRE_EQ(spark25_bf16::declare(&b, k.meta(), &ctx), RAD_OK); + CHECK(spark25_bf16::chat_format(k.meta()) != nullptr); REQUIRE_EQ(count_op(b, "attn_paged"), 4); REQUIRE_EQ(count_op(b, "rope"), 8); REQUIRE_EQ(b.binds.size(), (size_t)4); diff --git a/tests/spark25_chat_test.cpp b/tests/spark25_chat_test.cpp new file mode 100644 index 00000000..d4b3b72d --- /dev/null +++ b/tests/spark25_chat_test.cpp @@ -0,0 +1,154 @@ +/* Spark's reasoning and XML/KV tool calls, including streaming at every byte. */ +#include "rad_test.h" +#include "text/chat.h" +#include "text/chatfmt.h" +#include "text/chatparse.h" +#include "../arch/spark25_bf16/chat.h" + +#include +#include +#include +#include + +using namespace rad; +using json = nlohmann::ordered_json; + +static const json& tools() { + static const json t = json::parse(R"([ + {"type":"function","function":{"name":"weather","parameters":{"type":"object", + "properties":{"city":{"type":"string"},"days":{"type":"integer"}, + "verbose":{"type":"boolean"},"units":{"type":"array","items":{"type":"string"}}}}}}, + {"type":"function","function":{"name":"ping","parameters":{"type":"object","properties":{}}}} + ])"); + return t; +} + +static ChatFormat format() { + ChatFormat f; + std::string why; + CHECK_OK(chat_format_from_abi(spark25_bf16::spark_chat_format(), &f, &why)); + CHECK_OK(chat_format_check(f, &why)); + return f; +} + +static std::shared_ptr parser(const std::string& prefix, bool with_tools = true) { + std::string why; + ChatFormat f = format(); + std::shared_ptr rf; + std::shared_ptr p; + CHECK_OK(ReplyFormat::compile(f, &rf, &why)); + ReplyOptions opts; + opts.generation_prompt = prefix; + opts.tools = with_tools ? &tools() : nullptr; + if (rf) CHECK_OK(ReplyParser::create(rf, opts, &p, &why)); + return p; +} + +TEST(thinking_and_typed_calls_survive_one_byte_streaming) { + auto p = parser("<|start▁of▁sentence|><|Bot|>"); + REQUIRE(p != nullptr); + const std::string s = "I should look.Checking." + "weathercityParis" + "days3" + "verbosetrue" + "units[\"C\",\"F\"]" + "ping"; + const ReplyMessage expected = reply_parse(*p, s); + CHECK_EQ(expected.reasoning, std::string("I should look.")); + CHECK_EQ(expected.content, std::string("Checking.")); + REQUIRE_EQ(expected.calls.size(), (size_t)2); + CHECK_EQ(expected.calls[0].name, std::string("weather")); + CHECK(expected.calls[0].closed); + const json args = json::parse(expected.calls[0].arguments); + CHECK_EQ(args["city"].get(), std::string("Paris")); + CHECK_EQ(args["days"].get(), 3); + CHECK_EQ(args["verbose"].get(), true); + CHECK(args["units"] == json::array({"C", "F"})); + CHECK_EQ(expected.calls[1].name, std::string("ping")); + CHECK(json::parse(expected.calls[1].arguments) == json::object()); + auto stream = p->open(); + for (char c : s) stream->feed(std::string_view(&c, 1), nullptr); + stream->finish(nullptr); + const ReplyMessage& got = stream->message(); + CHECK_EQ(got.reasoning, expected.reasoning); + CHECK_EQ(got.content, expected.content); + REQUIRE_EQ(got.calls.size(), expected.calls.size()); + for (size_t i = 0; i < got.calls.size(); ++i) { + CHECK_EQ(got.calls[i].name, expected.calls[i].name); + CHECK_EQ(got.calls[i].arguments, expected.calls[i].arguments); + CHECK(got.calls[i].closed); + } +} + +TEST(thinking_off_and_an_unclosed_thought_before_a_call) { + auto off = parser("<|start▁of▁sentence|><|Bot|>", false); + REQUIRE(off != nullptr); + const ReplyMessage a = reply_parse(*off, "Hello."); + CHECK(a.reasoning.empty()); + CHECK_EQ(a.content, std::string("Hello.")); + auto on = parser("<|start▁of▁sentence|><|Bot|>"); + REQUIRE(on != nullptr); + const ReplyMessage b = reply_parse(*on, "Plan.ping"); + CHECK_EQ(b.reasoning, std::string("Plan.")); + REQUIRE_EQ(b.calls.size(), (size_t)1); + CHECK_EQ(b.calls[0].name, std::string("ping")); +} + +TEST(grammar_starts_on_the_real_call_opener) { + const ChatFormat f = format(); + ChatGrammarRequest req; + req.tools = &tools(); + req.generation_prompt = ""; + ChatGrammar g; + std::string why; + CHECK_OK(chat_format_grammar(f, req, &g, &why)); + CHECK(g.lazy); + CHECK(!g.text.empty()); + REQUIRE_EQ(g.trigger_words.size(), (size_t)1); + CHECK_EQ(g.trigger_words[0], std::string("")); +} + +/* Set this to the checkpoint's actual chat_template.jinja to exercise it without redistributing + * model files in the repository. Ordinary CI still exercises the reply format above. */ +TEST(reference_template_defaults_to_thinking_and_allows_disabling_it) { + const char* path = std::getenv("SPARK25_REFERENCE_TEMPLATE"); + if (!path || !*path) SKIP("set SPARK25_REFERENCE_TEMPLATE to the checkpoint's template"); + std::ifstream file(path); + REQUIRE((bool)file); + std::ostringstream s; + s << file.rdbuf(); + ChatTemplate t; + REQUIRE_EQ(t.load(s.str(), "<|start▁of▁sentence|>", "<|end▁of▁sentence|>"), RAD_OK); + const json messages = json::array({{{"role", "user"}, {"content", "Hello"}}}); + for (bool think : {true, false}) { + ChatOptions opt; + opt.enable_thinking = think; + ChatPrompt prompt; + REQUIRE_EQ(t.apply(messages, json::array(), opt, &prompt), RAD_OK); + const std::string suffix = think ? "" : ""; + CHECK(prompt.prompt.size() >= suffix.size()); + CHECK(prompt.prompt.ends_with(suffix)); + if (think) CHECK(prompt.prompt.find("thinking_effort=high") != std::string::npos); + } + ChatOptions low; + low.template_kwargs["reasoning_effort"] = "\"low\""; + ChatPrompt lp; + REQUIRE_EQ(t.apply(messages, json::array(), low, &lp), RAD_OK); + CHECK(lp.prompt.find("thinking_effort=low") != std::string::npos); + + /* OpenAI tool arguments arrive as a JSON string; the checkpoint template requires a mapping. */ + const json history = json::parse(R"([ + {"role":"user","content":"Weather?"}, + {"role":"assistant","content":"","reasoning_content":"Check.","tool_calls":[ + {"id":"call_1","type":"function","function":{"name":"weather","arguments":"{\"city\":\"Paris\",\"days\":3}"}}]}, + {"role":"tool","tool_call_id":"call_1","content":"Sunny"} + ])"); + ChatPrompt hp; + ChatOptions options; + REQUIRE_EQ(t.apply(history, tools(), options, &hp), RAD_OK); + CHECK(hp.prompt.find("weathercityParis") != std::string::npos); + CHECK(hp.prompt.find("days3") != std::string::npos); + CHECK(hp.prompt.find("Sunny") != std::string::npos); +} + +RAD_TEST_MAIN()