Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions src/common/AutoModel/modeling_gemma4_12b.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,12 @@ std::string Gemma4_12B::apply_chat_template(nlohmann::ordered_json& messages, nl
opt.polyfill_tool_responses = false;
opt.polyfill_object_arguments = false;

// The template raises if tool_calls[].function.arguments is a string, but OpenAI-style
// clients send it serialized. Deserialize it here (what polyfill_object_arguments would do).
nlohmann::ordered_json normalized = messages;
for (auto& message : normalized) {
if (!message.is_object() || !message.contains("tool_calls")) continue;
for (auto& tool_call : message["tool_calls"]) {
if (!tool_call.is_object() || !tool_call.contains("function")) continue;
auto& function = tool_call["function"];
if (!function.is_object() || !function.contains("arguments")) continue;
auto& arguments = function["arguments"];
if (!arguments.is_string()) continue;
auto raw = arguments.get<std::string>();
if (raw.empty()) {
arguments = nlohmann::ordered_json::object();
continue;
}
auto parsed = nlohmann::ordered_json::parse(raw, nullptr, /* allow_exceptions= */ false);
if (parsed.is_object()) arguments = parsed;
}
// OpenAI-style clients send tool call arguments serialized; the template renders only an object
nlohmann::ordered_json normalized;
try {
normalized = gemma4_template_messages(messages);
} catch (const std::invalid_argument& e) {
throw ModelRequestError(400, false, e.what(), "invalid_request_error");
}

minja::chat_template_inputs inputs;
Expand Down
25 changes: 6 additions & 19 deletions src/common/AutoModel/modeling_gemma4e.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,25 +317,12 @@ std::string Gemma4e::apply_chat_template(nlohmann::ordered_json& messages, nlohm
opt.polyfill_tool_responses = false;
opt.polyfill_object_arguments = false;

// The template raises if tool_calls[].function.arguments is a string, but OpenAI-style clients send it
// serialized. Deserialize it here (what polyfill_object_arguments would do).
nlohmann::ordered_json normalized = messages;
for (auto& message : normalized) {
if (!message.is_object() || !message.contains("tool_calls")) continue;
for (auto& tool_call : message["tool_calls"]) {
if (!tool_call.is_object() || !tool_call.contains("function")) continue;
auto& function = tool_call["function"];
if (!function.is_object() || !function.contains("arguments")) continue;
auto& arguments = function["arguments"];
if (!arguments.is_string()) continue;
auto raw = arguments.get<std::string>();
if (raw.empty()) {
arguments = nlohmann::ordered_json::object();
continue;
}
auto parsed = nlohmann::ordered_json::parse(raw, nullptr, /* allow_exceptions= */ false);
if (parsed.is_object()) arguments = parsed;
}
// OpenAI-style clients send tool call arguments serialized; the template renders only an object
nlohmann::ordered_json normalized;
try {
normalized = gemma4_template_messages(messages);
} catch (const std::invalid_argument& e) {
throw ModelRequestError(400, false, e.what(), "invalid_request_error");
}

minja::chat_template_inputs inputs;
Expand Down
26 changes: 26 additions & 0 deletions src/common/chat/chat_gemma4_tools.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// \file chat_gemma4_tools.cpp
/// \brief Prepare a request's tools for the Gemma4 chat template
#include "chat/chat_gemma4_tools.hpp"
#include <stdexcept>
#include <string>

namespace {
/// \brief Rewrite the nullable types in a JSON schema and in the schemas it holds
Expand Down Expand Up @@ -40,3 +42,27 @@ nlohmann::ordered_json gemma4_template_tools(nlohmann::ordered_json tools) {
}
return tools;
}

nlohmann::ordered_json gemma4_template_messages(nlohmann::ordered_json messages) {
for (auto& message : messages) {
if (!message.is_object() || !message.contains("tool_calls") || !message["tool_calls"].is_array()) continue;
for (auto& tool_call : message["tool_calls"]) {
if (!tool_call.is_object() || !tool_call.contains("function")) continue;
auto& function = tool_call["function"];
if (!function.is_object() || !function.contains("arguments")) continue;
auto& arguments = function["arguments"];
if (arguments.is_string()) {
const auto raw = arguments.get<std::string>();
arguments = raw.empty() ? nlohmann::ordered_json::object()
: nlohmann::ordered_json::parse(raw, nullptr, /* allow_exceptions= */ false);
}
if (!arguments.is_object() && !arguments.is_null()) {
const auto name = function.value("name", nlohmann::ordered_json()).is_string()
? function["name"].get<std::string>() : std::string("?");
throw std::invalid_argument("tool_calls[].function.arguments of the call to \"" + name +
"\" must be a JSON object");
}
}
}
return messages;
}
6 changes: 6 additions & 0 deletions src/include/chat/chat_gemma4_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@
/// a single nullable parameter would fail the whole request. A type that still lists
/// more than one type after dropping "null" is left as it is.
nlohmann::ordered_json gemma4_template_tools(nlohmann::ordered_json tools);

/// \brief The messages with every tool call's arguments as the JSON object the template renders
/// \note OpenAI-style clients send arguments serialized; an empty string is no arguments ({}).
/// The template refuses any arguments that are not an object or null, so those throw
/// std::invalid_argument, naming the call, instead of failing in the template.
nlohmann::ordered_json gemma4_template_messages(nlohmann::ordered_json messages);
64 changes: 32 additions & 32 deletions src/model_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,13 @@
},
{
"type": "file",
"oid": "c19999a347da729cf62806a8ddb7eb8e315223b5",
"size": 17336,
"oid": "9d494178d6f050c786d4b6fa70a0e1a24fac4ae6",
"size": 18454,
"path": "chat_template.jinja"
},
{
"type": "file",
"oid": "51a20dbc7844107d06280c51882dbad273f1a9c4",
"oid": "a2222087e803782a01c982c2419f96b5754a2cf9",
"size": 7528,
"path": "config.json"
},
Expand All @@ -320,14 +320,14 @@
},
{
"type": "file",
"oid": "1ff9f3e3439a939b971f9919e821bf87e835a503",
"size": 32169626,
"oid": "423b777a1620a1e582e0843d01b86d1d1c698965",
"size": 32155733,
"lfs": {
"oid": "cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f",
"size": 32169626,
"oid": "ab26b2add729e6f645bd869f228f14190a06615538f3be6f7786f33d13d78ad2",
"size": 32155733,
"pointerSize": 133
},
"xetHash": "c62336ad134cad6f154d84eb0e5a5fa9ca17cd665ef3ba5ac4fd02b1486760b4",
"xetHash": "d3e2cbf0094f678aa49dbe05422fc1e70302e85a138fe38163a6a8683c169265",
"path": "tokenizer.json"
},
{
Expand Down Expand Up @@ -370,13 +370,13 @@
},
{
"type": "file",
"oid": "c19999a347da729cf62806a8ddb7eb8e315223b5",
"size": 17336,
"oid": "9d494178d6f050c786d4b6fa70a0e1a24fac4ae6",
"size": 18454,
"path": "chat_template.jinja"
},
{
"type": "file",
"oid": "07a6fb2f7c407853d83725b513ab1fc2d165e6a5",
"oid": "e504aebcb59eae9e583cc95b419d6d7ccdd5a90a",
"size": 7229,
"path": "config.json"
},
Expand All @@ -394,14 +394,14 @@
},
{
"type": "file",
"oid": "1ff9f3e3439a939b971f9919e821bf87e835a503",
"size": 32169626,
"oid": "0e3703ae5fe4459202ad171ccb127b4fcfb7f275",
"size": 32156428,
"lfs": {
"oid": "cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f",
"size": 32169626,
"oid": "91d9cf86906f78e197021fda88214d448e5c120665d0243a8e776e046986c6bc",
"size": 32156428,
"pointerSize": 133
},
"xetHash": "c62336ad134cad6f154d84eb0e5a5fa9ca17cd665ef3ba5ac4fd02b1486760b4",
"xetHash": "521d9f0390742dc5890fc5f92931dae9bdda70191dd92fd0f551fae33d8ded1c",
"path": "tokenizer.json"
},
{
Expand Down Expand Up @@ -444,13 +444,13 @@
},
{
"type": "file",
"oid": "c19999a347da729cf62806a8ddb7eb8e315223b5",
"size": 17336,
"oid": "9d494178d6f050c786d4b6fa70a0e1a24fac4ae6",
"size": 18454,
"path": "chat_template.jinja"
},
{
"type": "file",
"oid": "51a20dbc7844107d06280c51882dbad273f1a9c4",
"oid": "a2222087e803782a01c982c2419f96b5754a2cf9",
"size": 7528,
"path": "config.json"
},
Expand All @@ -468,14 +468,14 @@
},
{
"type": "file",
"oid": "1ff9f3e3439a939b971f9919e821bf87e835a503",
"size": 32169626,
"oid": "423b777a1620a1e582e0843d01b86d1d1c698965",
"size": 32155733,
"lfs": {
"oid": "cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f",
"size": 32169626,
"oid": "ab26b2add729e6f645bd869f228f14190a06615538f3be6f7786f33d13d78ad2",
"size": 32155733,
"pointerSize": 133
},
"xetHash": "c62336ad134cad6f154d84eb0e5a5fa9ca17cd665ef3ba5ac4fd02b1486760b4",
"xetHash": "d3e2cbf0094f678aa49dbe05422fc1e70302e85a138fe38163a6a8683c169265",
"path": "tokenizer.json"
},
{
Expand Down Expand Up @@ -518,13 +518,13 @@
},
{
"type": "file",
"oid": "c19999a347da729cf62806a8ddb7eb8e315223b5",
"size": 17336,
"oid": "9d494178d6f050c786d4b6fa70a0e1a24fac4ae6",
"size": 18454,
"path": "chat_template.jinja"
},
{
"type": "file",
"oid": "07a6fb2f7c407853d83725b513ab1fc2d165e6a5",
"oid": "e504aebcb59eae9e583cc95b419d6d7ccdd5a90a",
"size": 7229,
"path": "config.json"
},
Expand All @@ -542,14 +542,14 @@
},
{
"type": "file",
"oid": "1ff9f3e3439a939b971f9919e821bf87e835a503",
"size": 32169626,
"oid": "0e3703ae5fe4459202ad171ccb127b4fcfb7f275",
"size": 32156428,
"lfs": {
"oid": "cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f",
"size": 32169626,
"oid": "91d9cf86906f78e197021fda88214d448e5c120665d0243a8e776e046986c6bc",
"size": 32156428,
"pointerSize": 133
},
"xetHash": "c62336ad134cad6f154d84eb0e5a5fa9ca17cd665ef3ba5ac4fd02b1486760b4",
"xetHash": "521d9f0390742dc5890fc5f92931dae9bdda70191dd92fd0f551fae33d8ded1c",
"path": "tokenizer.json"
},
{
Expand Down
32 changes: 16 additions & 16 deletions src/model_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,11 @@
"tools": true,
"supported_platforms": ["aie2p"],
"name": "Gemma4-E2B-IT-NPU2",
"url": "https://huggingface.co/FastFlowLM/Gemma4-E2B-IT-NPU2",
"file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E2B-IT-NPU2/tree/main",
"ms_url": "https://modelscope.cn/models/amd/Gemma4-E2B-IT-NPU2",
"url": "https://huggingface.co/FastFlowLM/Gemma4-E2B-IT-NPU2/resolve/flm_1.0.15",
"file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E2B-IT-NPU2/tree/flm_1.0.15",
"ms_url": "https://modelscope.cn/models/amd/Gemma4-E2B-IT-NPU2/resolve/flm_1.0.15",
"size": 2000000000,
"flm_min_version": "0.9.43",
"flm_min_version": "1.0.15",
"files": [
"config.json",
"model.q4nx",
Expand Down Expand Up @@ -399,11 +399,11 @@
"tools": true,
"supported_platforms": ["aie2p"],
"name": "Gemma4-E4B-IT-NPU2",
"url": "https://huggingface.co/FastFlowLM/Gemma4-E4B-IT-NPU2",
"file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E4B-IT-NPU2/tree/main",
"ms_url": "https://modelscope.cn/models/amd/Gemma4-E4B-IT-NPU2",
"url": "https://huggingface.co/FastFlowLM/Gemma4-E4B-IT-NPU2/resolve/flm_1.0.15",
"file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E4B-IT-NPU2/tree/flm_1.0.15",
"ms_url": "https://modelscope.cn/models/amd/Gemma4-E4B-IT-NPU2/resolve/flm_1.0.15",
"size": 4000000000,
"flm_min_version": "0.9.43",
"flm_min_version": "1.0.15",
"files": [
"config.json",
"model.q4nx",
Expand Down Expand Up @@ -591,11 +591,11 @@
"e2b": {
"supported_platforms": ["aie2p"],
"name": "Gemma4-E2B-IT-NPU2",
"url": "https://huggingface.co/FastFlowLM/Gemma4-E2B-IT-NPU2",
"file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E2B-IT-NPU2/tree/main",
"ms_url": "https://modelscope.cn/models/amd/Gemma4-E2B-IT-NPU2",
"url": "https://huggingface.co/FastFlowLM/Gemma4-E2B-IT-NPU2/resolve/flm_1.0.15",
"file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E2B-IT-NPU2/tree/flm_1.0.15",
"ms_url": "https://modelscope.cn/models/amd/Gemma4-E2B-IT-NPU2/resolve/flm_1.0.15",
"size": 2000000000,
"flm_min_version": "0.9.43",
"flm_min_version": "1.0.15",
"files": [
"config.json",
"model.q4nx",
Expand Down Expand Up @@ -629,11 +629,11 @@
"e4b": {
"supported_platforms": ["aie2p"],
"name": "Gemma4-E4B-IT-NPU2",
"url": "https://huggingface.co/FastFlowLM/Gemma4-E4B-IT-NPU2",
"file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E4B-IT-NPU2/tree/main",
"ms_url": "https://modelscope.cn/models/amd/Gemma4-E4B-IT-NPU2",
"url": "https://huggingface.co/FastFlowLM/Gemma4-E4B-IT-NPU2/resolve/flm_1.0.15",
"file_url": "https://huggingface.co/api/models/FastFlowLM/Gemma4-E4B-IT-NPU2/tree/flm_1.0.15",
"ms_url": "https://modelscope.cn/models/amd/Gemma4-E4B-IT-NPU2/resolve/flm_1.0.15",
"size": 4000000000,
"flm_min_version": "0.9.43",
"flm_min_version": "1.0.15",
"files": [
"config.json",
"model.q4nx",
Expand Down
31 changes: 31 additions & 0 deletions src/test/chat_tools/test_chat_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <filesystem>
#include <fstream>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>

Expand Down Expand Up @@ -2035,6 +2036,35 @@ void test_penalty_free_spans() {
CHECK(!token_ban_constraint(64, {7}).penalty_free());
}

// Tool call arguments for the Gemma4 templates, which render only an object (or null)
void test_gemma4_template_messages() {
std::printf("gemma4 template gets tool call arguments as an object, or a reason it cannot\n");
auto call = [](json arguments) {
return json::array({{{"role", "user"}, {"content", "hi"}},
{{"role", "assistant"}, {"content", ""},
{"tool_calls", json::array({{{"type", "function"},
{"function", {{"name", "f"}, {"arguments", arguments}}}}})}}});
};
auto arguments = [](const json& messages) { return messages[1]["tool_calls"][0]["function"]["arguments"]; };
CHECK(arguments(gemma4_template_messages(call("{\"a\":null,\"b\":{\"c\":1}}"))) ==
(json{{"a", nullptr}, {"b", {{"c", 1}}}}));
CHECK(arguments(gemma4_template_messages(call(""))) == json::object());
CHECK(arguments(gemma4_template_messages(call(json{{"a", 1}}))) == (json{{"a", 1}}));
CHECK(arguments(gemma4_template_messages(call(nullptr))).is_null());
// messages without calls are left as they are
const json plain = json::array({{{"role", "user"}, {"content", "hi"}}});
CHECK(gemma4_template_messages(plain) == plain);
for (const json bad : {json("oops"), json("[1]"), json("5"), json("{\"a\":1} trailing"), json::array({1}), json(5)}) {
bool threw = false;
try {
gemma4_template_messages(call(bad));
} catch (const std::invalid_argument& e) {
threw = std::string(e.what()).find("\"f\"") != std::string::npos;
}
CHECK(threw);
}
}

} // namespace

int main() {
Expand Down Expand Up @@ -2073,6 +2103,7 @@ int main() {
test_minicpm_template();
test_inkling();
test_penalty_free_spans();
test_gemma4_template_messages();
if (failures) {
std::printf("%d check(s) failed\n", failures);
return 1;
Expand Down
5 changes: 5 additions & 0 deletions src/test/model_extension_gemma4e/gemma4_open_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ class Frontend {
!call["function"].contains("arguments")) continue;
auto& arguments = call["function"]["arguments"];
if (!arguments.is_string()) continue;
// an empty string is a call without arguments, as in Gemma4e
if (arguments.get<std::string>().empty()) {
arguments = json::object();
continue;
}
const auto parsed = json::parse(
arguments.get<std::string>(), nullptr, false);
if (parsed.is_object()) arguments = parsed;
Expand Down
Loading
Loading