Archived
The gemma4_bf16 architecture (sliding and global attention, the proportional RoPE ladder, the assistant head as a drafter that reads the trunk's cache, the vision tower), its w4a8 decode junctions, the h512/gqa8 and h256/gqa2 attention kernels, compressed-tensors int4 import, multi-EOS from generation_config, the call/reasoning reply format, a window group that keeps the keys a prefill chunk still reads, and window snapshots in checkpoint slots so a prefix hit on a sliding-window trunk resumes. Fixes on top: the investigation switches and diagnostic blocks out of the served paths; int4 import through the checkpoint import framework; attention_k_eq_v from the config; the fused junctions only where every reader takes their pair; a shared-KV head binds no group of its own; the compose file without HSA_ENABLE_IPC_MODE_LEGACY (TP2 fails with it on ROCm 10.1); a checkpoint carries a window only where a hit resumes from it; the README says the linked container is keptn42's calibrated GPTQ build and what the in-tree recipe gives.
This commit is contained in:
commit
7c7f755931
84 files changed
+10292
-230
No files matched your search
+203
-32
@@ -13,6 +13,7 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
@@ -45,6 +46,75 @@ bool starts_with(const std::string& s, const std::string& p) {
|
||||
|
||||
std::string show(const std::string& s) { return nlohmann::json(s).dump(); }
|
||||
|
||||
int refuse(std::string* why, const std::string& s) {
|
||||
if (why) *why = s;
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
/* A byte that begins a bare word or a JSON value, or is the object's own punctuation. */
|
||||
bool object_byte(unsigned char c) {
|
||||
return c == 0 || std::isalnum(c) || is_ws(c) || std::strchr("-+._'\"{}[],:", (int)c) != nullptr;
|
||||
}
|
||||
|
||||
/* An argument object, as the streaming parser reads it (chatparse.cpp). Where a key or a value
|
||||
* may begin, it tells three literals apart by their first bytes: a dialect string's opener, and
|
||||
* the call's close and end, which a reply that leaves its object unclosed writes there. None may
|
||||
* begin another. A bare word ends at the object's punctuation or at the call's close; one
|
||||
* automaton finds both in the order they start only when the close holds none of it. */
|
||||
int object_check(const ChatFormat& f, std::string* why) {
|
||||
const std::string& so = f.object_string_open;
|
||||
const std::string& sc = f.object_string_close;
|
||||
if (so.empty() != sc.empty())
|
||||
return refuse(why, "object_string_open and object_string_close are set together or not "
|
||||
"at all");
|
||||
if (!so.empty()) {
|
||||
if (trim_ws(so) != so || trim_ws(sc) != sc)
|
||||
return refuse(why, "an object string delimiter may not begin or end with "
|
||||
"whitespace");
|
||||
if (object_byte((unsigned char)so[0]))
|
||||
return refuse(why, "object_string_open " + show(so) + " begins with a byte that also "
|
||||
"begins a bare word, a JSON value or the object's punctuation");
|
||||
}
|
||||
std::vector<std::string> lits;
|
||||
if (!so.empty()) lits.push_back(so);
|
||||
const std::string cc = trim_ws(f.call_close), ce = trim_ws(f.call_end);
|
||||
for (const std::string* s : { &cc, &ce }) {
|
||||
if (s->empty()) continue;
|
||||
for (char c : *s)
|
||||
if (std::strchr(",:{}[]\"", c))
|
||||
return refuse(why, "the call's close " + show(*s) + " holds " +
|
||||
show(std::string(1, c)) + ", which is the argument object's "
|
||||
"punctuation");
|
||||
if (std::find(lits.begin(), lits.end(), *s) == lits.end()) lits.push_back(*s);
|
||||
else if (!so.empty() && *s == so)
|
||||
return refuse(why, show(*s) + " both opens an object string and closes the call");
|
||||
}
|
||||
for (size_t i = 0; i < lits.size(); ++i)
|
||||
for (size_t j = 0; j < lits.size(); ++j)
|
||||
if (i != j && (starts_with(lits[i], lits[j]) || occurs_inside(lits[j], lits[i])))
|
||||
return refuse(why, show(lits[j]) + " and " + show(lits[i]) + " cannot be told "
|
||||
"apart inside an argument object");
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Content is scanned for a later reasoning block's opener and for a stray close beside the call
|
||||
* openers, in one automaton, which reports them in the order they start only when none occurs
|
||||
* inside another or begins it. */
|
||||
int reasoning_anywhere_check(const ChatFormat& f, std::string* why) {
|
||||
if (!f.reasoning_anywhere) return RAD_OK;
|
||||
std::vector<std::string> d = { trim_ws(f.reasoning_open), trim_ws(f.reasoning_close) };
|
||||
if (f.has_tools()) {
|
||||
if (!f.call_open.empty()) d.push_back(trim_ws(f.call_open));
|
||||
d.push_back(f.name_prefix);
|
||||
}
|
||||
for (size_t i = 0; i < d.size(); ++i)
|
||||
for (size_t j = 0; j < d.size(); ++j)
|
||||
if (i != j && (starts_with(d[i], d[j]) || occurs_inside(d[j], d[i])))
|
||||
return refuse(why, show(d[j]) + " and " + show(d[i]) + " cannot be told apart in "
|
||||
"content, which a reasoning block may open anywhere in");
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* ================================================================== validity */
|
||||
@@ -61,16 +131,24 @@ int chat_format_check(const ChatFormat& f, std::string* why) {
|
||||
return no("reasoning_close is only whitespace");
|
||||
if ((uint32_t)f.reasoning_in_prompt > (uint32_t)ChatThink::Never)
|
||||
return no("reasoning_in_prompt is not a RAD_CHAT_THINK_* value");
|
||||
if ((uint32_t)f.args_form > (uint32_t)ChatArgs::Object)
|
||||
return no("args_form is not a RAD_CHAT_ARGS_* value");
|
||||
/* A block that opens later in the reply is found by its opener, in content. */
|
||||
if (f.reasoning_anywhere && (!f.has_reasoning() || trim_ws(f.reasoning_open).empty()))
|
||||
return no("reasoning_anywhere needs a reasoning block with an opener");
|
||||
|
||||
if (!f.has_tools()) {
|
||||
for (const std::string* s : { &f.section_open, &f.section_close, &f.call_separator,
|
||||
&f.call_open, &f.call_close, &f.name_suffix, &f.call_end,
|
||||
&f.arg_prefix, &f.arg_name_suffix, &f.arg_value_prefix,
|
||||
&f.arg_value_suffix })
|
||||
&f.arg_value_suffix, &f.object_string_open,
|
||||
&f.object_string_close })
|
||||
if (!s->empty()) return no("tool-call delimiters are set but name_prefix is empty");
|
||||
if (f.object_args() || f.object_bare_keys)
|
||||
return no("an argument object is described but name_prefix is empty");
|
||||
if (!f.reasoning_breaks.empty()) return no("reasoning_breaks without tool calls");
|
||||
if (!f.triggers.empty()) return no("grammar triggers without tool calls");
|
||||
return RAD_OK;
|
||||
return reasoning_anywhere_check(f, why);
|
||||
}
|
||||
|
||||
if (!f.section_open.empty() || !f.section_close.empty())
|
||||
@@ -81,22 +159,43 @@ int chat_format_check(const ChatFormat& f, std::string* why) {
|
||||
") is not read by the streaming parser; tagged calls separate on whitespace");
|
||||
if (f.call_open.empty() && !f.call_close.empty())
|
||||
return no("call_close without call_open");
|
||||
if (f.name_suffix.empty() || trim_ws(f.name_suffix).empty())
|
||||
return no("name_suffix must end the function name with something besides whitespace");
|
||||
if (is_ws((unsigned char)f.name_suffix[0]))
|
||||
return no("name_suffix " + show(f.name_suffix) + " may not begin with whitespace");
|
||||
if (f.call_end.empty()) return no("call_end is empty: a call must close with its own tag");
|
||||
if (f.arg_prefix.empty()) return no("arg_prefix is empty");
|
||||
if (f.arg_value_suffix.empty()) return no("arg_value_suffix is empty");
|
||||
if (f.arg_name_suffix.empty() && f.arg_value_prefix.empty())
|
||||
return no("nothing ends an argument name: arg_name_suffix and arg_value_prefix are empty");
|
||||
if (!f.arg_name_suffix.empty() && !f.arg_value_prefix.empty())
|
||||
return no("arg_name_suffix and arg_value_prefix are both set; the streaming parser reads "
|
||||
"one delimiter between an argument's name and its value");
|
||||
if (f.string_value != ChatValue::Raw && f.string_value != ChatValue::Cdata)
|
||||
return no("string_value must be RAD_CHAT_VALUE_RAW or RAD_CHAT_VALUE_CDATA");
|
||||
if ((uint32_t)f.other_value > (uint32_t)ChatValue::Json)
|
||||
return no("other_value is not a RAD_CHAT_VALUE_* value");
|
||||
const bool object = f.object_args();
|
||||
/* An object's own `{` may be what ends the name; anything else needs its suffix. */
|
||||
if (!object || !f.name_suffix.empty()) {
|
||||
if (f.name_suffix.empty() || trim_ws(f.name_suffix).empty())
|
||||
return no("name_suffix must end the function name with something besides whitespace");
|
||||
if (is_ws((unsigned char)f.name_suffix[0]))
|
||||
return no("name_suffix " + show(f.name_suffix) + " may not begin with whitespace");
|
||||
}
|
||||
if (!object) {
|
||||
if (f.call_end.empty()) return no("call_end is empty: a call must close with its own tag");
|
||||
if (f.arg_prefix.empty()) return no("arg_prefix is empty");
|
||||
if (f.arg_value_suffix.empty()) return no("arg_value_suffix is empty");
|
||||
if (f.arg_name_suffix.empty() && f.arg_value_prefix.empty())
|
||||
return no("nothing ends an argument name: arg_name_suffix and arg_value_prefix are "
|
||||
"empty");
|
||||
if (!f.arg_name_suffix.empty() && !f.arg_value_prefix.empty())
|
||||
return no("arg_name_suffix and arg_value_prefix are both set; the streaming parser "
|
||||
"reads one delimiter between an argument's name and its value");
|
||||
if (f.string_value != ChatValue::Raw && f.string_value != ChatValue::Cdata)
|
||||
return no("string_value must be RAD_CHAT_VALUE_RAW or RAD_CHAT_VALUE_CDATA");
|
||||
if ((uint32_t)f.other_value > (uint32_t)ChatValue::Json)
|
||||
return no("other_value is not a RAD_CHAT_VALUE_* value");
|
||||
if (!f.object_string_open.empty() || !f.object_string_close.empty() ||
|
||||
f.object_bare_keys)
|
||||
return no("the object_* fields describe an argument object, and args_form is "
|
||||
"RAD_CHAT_ARGS_TAGGED");
|
||||
} else {
|
||||
for (const std::string* s : { &f.arg_prefix, &f.arg_name_suffix, &f.arg_value_prefix,
|
||||
&f.arg_value_suffix })
|
||||
if (!s->empty())
|
||||
return no("the arg_* fields describe tagged arguments, and args_form is "
|
||||
"RAD_CHAT_ARGS_OBJECT");
|
||||
if (!f.call_end.empty() && trim_ws(f.call_end).empty())
|
||||
return no("call_end is only whitespace");
|
||||
const int st = object_check(f, why);
|
||||
if (st < 0) return st;
|
||||
}
|
||||
|
||||
/* A wrapped format's content is scanned for the wrapper's core and for name_prefix alone,
|
||||
* which opens a call written without its wrapper; the two must not be confusable. */
|
||||
@@ -127,16 +226,19 @@ int chat_format_check(const ChatFormat& f, std::string* why) {
|
||||
return no("reasoning break " + show(b) + " overlaps reasoning_close " +
|
||||
show(close));
|
||||
}
|
||||
/* Inside a call the next tag is an argument or the close, and the close is recognised by its
|
||||
* core as well: the whitespace around it is optional when a reply leaves the format. */
|
||||
const std::string end = trim_ws(f.call_end);
|
||||
if (end.empty()) return no("call_end is only whitespace");
|
||||
for (const std::string* e : { &f.call_end, &end })
|
||||
if (starts_with(f.arg_prefix, *e) || starts_with(*e, f.arg_prefix) ||
|
||||
occurs_inside(f.arg_prefix, *e) || occurs_inside(*e, f.arg_prefix))
|
||||
return no("arg_prefix " + show(f.arg_prefix) + " and call_end " + show(f.call_end) +
|
||||
" cannot be told apart");
|
||||
return RAD_OK;
|
||||
if (!object) {
|
||||
/* Inside a call the next tag is an argument or the close, and the close is recognised by
|
||||
* its core as well: the whitespace around it is optional when a reply leaves the
|
||||
* format. */
|
||||
const std::string end = trim_ws(f.call_end);
|
||||
if (end.empty()) return no("call_end is only whitespace");
|
||||
for (const std::string* e : { &f.call_end, &end })
|
||||
if (starts_with(f.arg_prefix, *e) || starts_with(*e, f.arg_prefix) ||
|
||||
occurs_inside(f.arg_prefix, *e) || occurs_inside(*e, f.arg_prefix))
|
||||
return no("arg_prefix " + show(f.arg_prefix) + " and call_end " +
|
||||
show(f.call_end) + " cannot be told apart");
|
||||
}
|
||||
return reasoning_anywhere_check(f, why);
|
||||
}
|
||||
|
||||
/* ================================================================== the plugin's declaration */
|
||||
@@ -183,6 +285,11 @@ int chat_format_from_abi(const RadChatFormat* in, ChatFormat* out, std::string*
|
||||
f.other_value = (ChatValue)c.other_value;
|
||||
f.value_newlines = c.value_newlines != 0;
|
||||
list(c.triggers, c.n_triggers, &f.triggers);
|
||||
f.args_form = (ChatArgs)c.args_form;
|
||||
f.object_string_open = s(c.object_string_open);
|
||||
f.object_string_close = s(c.object_string_close);
|
||||
f.object_bare_keys = c.object_bare_keys != 0;
|
||||
f.reasoning_anywhere = c.reasoning_anywhere != 0;
|
||||
*out = std::move(f);
|
||||
return RAD_OK;
|
||||
}
|
||||
@@ -286,13 +393,23 @@ int chat_format_from_template(const std::string& jinja, const std::string& bos,
|
||||
std::string chat_format_describe(const ChatFormat& f) {
|
||||
std::string s = f.name + ":";
|
||||
if (f.has_reasoning())
|
||||
s += " reasoning " + show(f.reasoning_open) + ".." + show(f.reasoning_close);
|
||||
s += " reasoning " + show(f.reasoning_open) + ".." + show(f.reasoning_close) +
|
||||
(f.reasoning_anywhere ? " anywhere" : "");
|
||||
else
|
||||
s += " no reasoning";
|
||||
if (f.has_tools()) {
|
||||
if (f.has_tools() && f.object_args()) {
|
||||
s += "; calls " + show(f.call_open) + show(f.name_prefix) + "NAME" + show(f.name_suffix) +
|
||||
" {OBJECT";
|
||||
if (!f.object_string_open.empty())
|
||||
s += ", strings " + show(f.object_string_open) + ".." + show(f.object_string_close);
|
||||
if (f.object_bare_keys) s += ", bare keys";
|
||||
s += "} " + show(f.call_end) + show(f.call_close);
|
||||
} else if (f.has_tools()) {
|
||||
s += "; calls " + show(f.call_open) + show(f.name_prefix) + "NAME" + show(f.name_suffix) +
|
||||
" " + show(f.arg_prefix) + "ARG" + show(f.arg_name_suffix) + show(f.arg_value_prefix) +
|
||||
"VALUE" + show(f.arg_value_suffix) + " " + show(f.call_end) + show(f.call_close);
|
||||
}
|
||||
if (f.has_tools()) {
|
||||
if (!f.triggers.empty()) {
|
||||
s += "; grammar on";
|
||||
for (const std::string& t : f.triggers) s += " " + show(t);
|
||||
@@ -310,7 +427,8 @@ std::vector<std::string> chat_format_markers(const ChatFormat& f) {
|
||||
for (const std::string* s : { &f.reasoning_open, &f.reasoning_close, &f.section_open,
|
||||
&f.section_close, &f.call_open, &f.call_close, &f.name_prefix,
|
||||
&f.name_suffix, &f.call_end, &f.arg_prefix, &f.arg_name_suffix,
|
||||
&f.arg_value_prefix, &f.arg_value_suffix }) {
|
||||
&f.arg_value_prefix, &f.arg_value_suffix,
|
||||
&f.object_string_open, &f.object_string_close }) {
|
||||
const std::string t = trim_ws(*s);
|
||||
if (!t.empty() && std::find(out.begin(), out.end(), t) == out.end()) out.push_back(t);
|
||||
}
|
||||
@@ -538,6 +656,46 @@ void add_json_rules(const common_grammar_builder& b) {
|
||||
"json-null");
|
||||
}
|
||||
|
||||
/* THE ARGUMENT OBJECT of the object family, in its dialect: JSON, or strings between the
|
||||
* declared delimiters and keys written bare. As llama.cpp's own Gemma 4 grammar does, it fixes
|
||||
* the syntax and leaves the values to the model: a tool's schema says what JSON a value must be,
|
||||
* and the dialect is not JSON, so the schema converter has nothing to say about it. Whitespace
|
||||
* goes where the dialect's writers put it -- after a separator and inside the brackets -- and
|
||||
* nowhere else. */
|
||||
void add_object_rules(const common_grammar_builder& b, const ChatFormat& f) {
|
||||
const std::string& so = f.object_string_open;
|
||||
if (!so.empty())
|
||||
b.add_rule("obj-string", gbnf_format_literal(so) + " " +
|
||||
until_pattern({ f.object_string_close }) + " " +
|
||||
gbnf_format_literal(f.object_string_close));
|
||||
else
|
||||
b.add_rule("obj-string", "\"\\\"\" ( [^\"\\\\] | \"\\\\\" ( [\"\\\\/ bfnrt] | \"u\" "
|
||||
"[0-9a-fA-F]{4} ) )* \"\\\"\"");
|
||||
b.add_rule("obj-number", "\"-\"? (\"0\" | [1-9] [0-9]*) (\".\" [0-9]+)? ([eE] [+-]? [0-9]+)?");
|
||||
/* A dialect with strings of its own still gets a value in Python's quotes, `'...'`, which
|
||||
* is how Gemma 4 writes arguments it was handed as JSON (measured). Refused, the model
|
||||
* takes the next token the grammar allows -- a `-` -- and every such string becomes a
|
||||
* number; the parser reads the quoted text verbatim but for an escaped quote. */
|
||||
const char* sq = so.empty() ? "" : " | obj-sqstring";
|
||||
if (!so.empty()) b.add_rule("obj-sqstring", "\"'\" ( [^'\\\\] | \"\\\\\" [^\\x00] )* \"'\"");
|
||||
b.add_rule("obj-value", std::string("obj-object | obj-array | obj-string") + sq +
|
||||
" | obj-number | \"true\" | \"false\" | \"null\"");
|
||||
b.add_rule("obj-array", "\"[\" space (\"]\" | obj-value (\",\" space obj-value)* space "
|
||||
"\"]\")");
|
||||
if (f.object_bare_keys) {
|
||||
/* A bare key: no whitespace, no punctuation, and not the string opener's first byte. */
|
||||
std::string cls = "[^ \\t\\n\\r:,{}\\[\\]\"";
|
||||
if (!so.empty()) cls += class_char((unsigned char)so[0]);
|
||||
cls += "]+";
|
||||
b.add_rule("obj-key", cls + " | obj-string");
|
||||
} else {
|
||||
b.add_rule("obj-key", "obj-string");
|
||||
}
|
||||
b.add_rule("obj-member", "obj-key \":\" space obj-value");
|
||||
b.add_rule("obj-object", "\"{\" space (\"}\" | obj-member (\",\" space obj-member)* space "
|
||||
"\"}\")");
|
||||
}
|
||||
|
||||
G optspace(const std::string& tag) {
|
||||
size_t a = 0, b = tag.size();
|
||||
while (a < b && is_ws((unsigned char)tag[a])) ++a;
|
||||
@@ -644,6 +802,18 @@ int chat_format_grammar(const ChatFormat& f, const ChatGrammarRequest& req, Chat
|
||||
const auto& fn = tool.at("function");
|
||||
const std::string name = fn.at("name");
|
||||
if (!req.only_tool.empty() && name != req.only_tool) continue;
|
||||
if (f.object_args()) {
|
||||
/* The name, then the object; the object's own closing brace may be all
|
||||
* that ends the call. */
|
||||
G open = wrap(seq({ lit(f.name_prefix), wrap(lit(name)), lit(f.name_suffix) }));
|
||||
G func = f.call_end.empty()
|
||||
? seq({ open, rule_ref("obj-object") })
|
||||
: seq({ open, rule_ref("obj-object"), space(),
|
||||
wrap(lit(f.call_end)) });
|
||||
b.add_rule(rule_name("tool-" + name), render(func));
|
||||
choices.push_back(rule_ref(rule_name("tool-" + name)));
|
||||
continue;
|
||||
}
|
||||
auto params = fn.contains("parameters") ? fn.at("parameters") : ojson::object();
|
||||
const auto properties = params.contains("properties") ? params.at("properties")
|
||||
: ojson::object();
|
||||
@@ -704,7 +874,8 @@ int chat_format_grammar(const ChatFormat& f, const ChatGrammarRequest& req, Chat
|
||||
if (choices.empty() && !req.only_tool.empty())
|
||||
throw std::invalid_argument("no tool named \"" + req.only_tool + "\" to call");
|
||||
if (typed) add_json_rules(b);
|
||||
if (!choices.empty()) b.add_rule("until-suffix", until_pattern({ f.arg_value_suffix }));
|
||||
if (!choices.empty() && f.object_args()) add_object_rules(b, f);
|
||||
else if (!choices.empty()) b.add_rule("until-suffix", until_pattern({ f.arg_value_suffix }));
|
||||
|
||||
G choice = alt(choices);
|
||||
G calls;
|
||||
|
||||
+38
-6
@@ -32,10 +32,21 @@
|
||||
* the raw text (optionally CDATA-wrapped), any other type is JSON, and text that does not parse as
|
||||
* JSON is taken raw -- a model writing into an XML tag writes Python as often as JSON.
|
||||
*
|
||||
* The analyser also recognises formats outside this family (a JSON object per call, JSON
|
||||
* arguments inside a tag, call ids). For those chat_format_from_template keeps the reasoning split
|
||||
* and names why tool calls are refused, and a model that needs one gets it by adding the shape to
|
||||
* the parser, not by a fallback. */
|
||||
* THE OBJECT FAMILY writes a call as its name and one object holding the arguments
|
||||
* (ChatArgs::Object):
|
||||
*
|
||||
* Gemma 4 <|tool_call>call:NAME{key:<|"|>text<|"|>,n:3}<tool_call|>
|
||||
*
|
||||
* The object is JSON or a dialect of it -- strings between two declared delimiters with their
|
||||
* text verbatim, keys written bare -- and the arguments a client receives are that object in
|
||||
* JSON. Here the values carry their own types, so no schema is consulted to read them; a bare
|
||||
* word that is no JSON literal or number is read as a string, as a typed argument that is not
|
||||
* JSON is in the tagged family.
|
||||
*
|
||||
* The analyser also recognises formats outside these families (a JSON object per call with its
|
||||
* name inside it, call ids). For those chat_format_from_template keeps the reasoning split and
|
||||
* names why tool calls are refused, and a model that needs one gets it by adding the shape to the
|
||||
* parser, not by a fallback. */
|
||||
#pragma once
|
||||
#include "rad_internal.h"
|
||||
|
||||
@@ -68,6 +79,12 @@ enum class ChatThink : uint32_t {
|
||||
Never = 2, /* the model opens its own block, if any */
|
||||
};
|
||||
|
||||
/* How a call's arguments are written. The numbering is the ABI's (RAD_CHAT_ARGS_*). */
|
||||
enum class ChatArgs : uint32_t {
|
||||
Tagged = 0, /* each argument in its own tags: arg_prefix NAME .. VALUE arg_value_suffix */
|
||||
Object = 1, /* one object after the name: `{` key `:` value, ... `}` */
|
||||
};
|
||||
|
||||
struct ChatFormat {
|
||||
std::string name; /* for logs: "plugin:<arch>" or "template" */
|
||||
|
||||
@@ -83,6 +100,10 @@ struct ChatFormat {
|
||||
* Must name the call opener (`call_open`) when non-empty. */
|
||||
std::vector<std::string> reasoning_breaks;
|
||||
ChatThink reasoning_in_prompt = ChatThink::FromPrompt;
|
||||
/* A block may also open later in the reply -- after the answer, between calls -- and is read
|
||||
* as reasoning there; a close with no block open is dropped. Gemma 4 writes an empty block
|
||||
* after its answer now and then, and a lone close. Needs `reasoning_open`. */
|
||||
bool reasoning_anywhere = false;
|
||||
|
||||
/* ------------------------------------------------------------------ tool calls
|
||||
* `name_prefix` is what opens a call (after `call_open`, when there is one). Empty means the
|
||||
@@ -94,8 +115,18 @@ struct ChatFormat {
|
||||
/* Around EACH call: "<tool_call>\n", "</tool_call>". When set, a call written without it --
|
||||
* name_prefix alone -- is still read as a call, leniently (chatparse.h). */
|
||||
std::string call_open, call_close;
|
||||
std::string name_prefix, name_suffix; /* "<function=", ">\n" */
|
||||
std::string call_end; /* "</function>\n" */
|
||||
/* "<function=", ">\n". In the object family name_suffix may be empty: the name then ends
|
||||
* where the object begins, at its `{`. */
|
||||
std::string name_prefix, name_suffix;
|
||||
std::string call_end; /* "</function>\n"; may be empty for an object */
|
||||
|
||||
/* How the arguments are written. Object: the arg_* fields below are empty, and these three
|
||||
* describe the object's dialect -- a string delimiter besides JSON's `"`, between which the
|
||||
* text is verbatim ("<|\"|>" twice for Gemma 4; empty for JSON strings only), and whether a
|
||||
* key may be written bare. */
|
||||
ChatArgs args_form = ChatArgs::Tagged;
|
||||
std::string object_string_open, object_string_close;
|
||||
bool object_bare_keys = false;
|
||||
|
||||
std::string arg_prefix; /* "<parameter=" */
|
||||
std::string arg_name_suffix; /* ">\n" (MiniCPM: empty, the value prefix ends it) */
|
||||
@@ -115,6 +146,7 @@ struct ChatFormat {
|
||||
|
||||
bool has_reasoning() const { return !reasoning_close.empty(); }
|
||||
bool has_tools() const { return !name_prefix.empty(); }
|
||||
bool object_args() const { return args_form == ChatArgs::Object; }
|
||||
};
|
||||
|
||||
/* Can the streaming parser run this format? RAD_OK, or RAD_E_UNSUPPORTED with *why naming the
|
||||
|
||||
+748
-15
@@ -744,6 +744,30 @@ struct ReplyFormat::Impl {
|
||||
std::string cc_core;
|
||||
Dfa think, think_breaks, text, arg_name, value, junk, arg_name_loose, value_loose;
|
||||
size_t longest = 0; /* the longest delimiter */
|
||||
|
||||
/* THE OBJECT FAMILY. `object`: a call's arguments are one object; `ns_object`: the object's
|
||||
* own `{` is what ends the name (name_suffix empty), and ns_core is "{". */
|
||||
bool object = false, ns_object = false;
|
||||
/* Where a key or a value may begin, besides the object's punctuation: the dialect string's
|
||||
* opener, and the call's close and end -- a reply that leaves its object unclosed writes them
|
||||
* there. Read anchored, by their first bytes (chat_format_check keeps them apart). */
|
||||
enum : uint8_t { LitString, LitClose, LitEnd };
|
||||
std::vector<std::string> obj_lits;
|
||||
std::vector<uint8_t> obj_lit_kind;
|
||||
/* A dialect string runs to its close. A bare value ends at `,` `}` `]` and a bare key at `:`
|
||||
* `,` `}` -- patterns 0..2 -- or at the call's close or end, which follow at the indices
|
||||
* below (-1: none). */
|
||||
Dfa obj_str, obj_bare, obj_key;
|
||||
/* A value the model opened with a plain `"` -- or `'` (obj_sqstr) -- in a dialect that has
|
||||
* strings of its own: read as that dialect's strings are, verbatim, to a quote the object's
|
||||
* syntax goes on after. Patterns: the quote, the escaped quote, the dialect's string close,
|
||||
* the call's close (if any). */
|
||||
Dfa obj_qstr, obj_sqstr;
|
||||
int bare_cc = -1, bare_ce = -1, key_cc = -1, key_ce = -1;
|
||||
|
||||
/* REASONING ANYWHERE: content is scanned for a later block's opener and a stray close too,
|
||||
* after the call openers (text_think) or alone when the request has no calls (think_only). */
|
||||
Dfa text_think, think_only;
|
||||
};
|
||||
|
||||
ReplyFormat::ReplyFormat() : impl_(std::make_unique<Impl>()) {}
|
||||
@@ -773,6 +797,12 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr<const ReplyFormat>
|
||||
if (f.call_open.empty()) m.openers = { f.name_prefix };
|
||||
else m.openers = { m.co_core, f.name_prefix };
|
||||
m.name_end = f.arg_name_suffix.empty() ? f.arg_value_prefix : f.arg_name_suffix;
|
||||
m.object = f.has_tools() && f.object_args();
|
||||
if (m.object && f.name_suffix.empty()) {
|
||||
m.ns_object = true;
|
||||
m.ns_core = "{";
|
||||
m.ns_trail.clear();
|
||||
}
|
||||
|
||||
if (f.has_reasoning()) {
|
||||
m.think.build({ m.close_core });
|
||||
@@ -780,7 +810,50 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr<const ReplyFormat>
|
||||
for (const std::string& s : f.reasoning_breaks) b.push_back(s);
|
||||
m.think_breaks.build(b);
|
||||
}
|
||||
if (f.has_tools()) {
|
||||
if (f.reasoning_anywhere && f.has_reasoning()) {
|
||||
std::vector<std::string> t;
|
||||
if (f.has_tools()) t = m.openers;
|
||||
t.push_back(m.open_core);
|
||||
t.push_back(m.close_core);
|
||||
m.text_think.build(t);
|
||||
m.think_only.build({ m.open_core, m.close_core });
|
||||
}
|
||||
if (m.object) {
|
||||
m.text.build(m.openers);
|
||||
std::string trail;
|
||||
split_ws(f.call_close, &m.cc_core, &trail);
|
||||
std::vector<std::string> bare{ ",", "}", "]" }, key{ ":", ",", "}" };
|
||||
if (!f.object_string_open.empty()) {
|
||||
m.obj_lits.push_back(f.object_string_open);
|
||||
m.obj_lit_kind.push_back(Impl::LitString);
|
||||
/* The call's close inside a string: where a model left the string unclosed. */
|
||||
std::vector<std::string> sp{ f.object_string_close };
|
||||
std::vector<std::string> qp{ "\"", "\\\"", f.object_string_close };
|
||||
std::vector<std::string> sq{ "'", "\\'", f.object_string_close };
|
||||
if (!m.cc_core.empty()) {
|
||||
sp.push_back(m.cc_core);
|
||||
qp.push_back(m.cc_core);
|
||||
sq.push_back(m.cc_core);
|
||||
}
|
||||
m.obj_str.build(sp);
|
||||
m.obj_qstr.build(qp);
|
||||
m.obj_sqstr.build(sq);
|
||||
}
|
||||
if (!m.cc_core.empty()) {
|
||||
m.obj_lits.push_back(m.cc_core);
|
||||
m.obj_lit_kind.push_back(Impl::LitClose);
|
||||
m.bare_cc = (int)bare.size(); bare.push_back(m.cc_core);
|
||||
m.key_cc = (int)key.size(); key.push_back(m.cc_core);
|
||||
}
|
||||
if (!m.end_core.empty() && m.end_core != m.cc_core) {
|
||||
m.obj_lits.push_back(m.end_core);
|
||||
m.obj_lit_kind.push_back(Impl::LitEnd);
|
||||
m.bare_ce = (int)bare.size(); bare.push_back(m.end_core);
|
||||
m.key_ce = (int)key.size(); key.push_back(m.end_core);
|
||||
}
|
||||
m.obj_bare.build(bare);
|
||||
m.obj_key.build(key);
|
||||
} else if (f.has_tools()) {
|
||||
m.text.build(m.openers);
|
||||
m.arg_name.build({ m.name_end });
|
||||
m.value.build({ f.arg_value_suffix });
|
||||
@@ -800,7 +873,8 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr<const ReplyFormat>
|
||||
const std::string* const delims[] = { &f.reasoning_open, &f.reasoning_close, &f.call_open,
|
||||
&f.call_close, &f.name_prefix, &f.name_suffix,
|
||||
&f.call_end, &f.arg_prefix, &m.name_end,
|
||||
&f.arg_value_suffix };
|
||||
&f.arg_value_suffix, &f.object_string_open,
|
||||
&f.object_string_close };
|
||||
for (const std::string* s : delims) m.longest = std::max(m.longest, s->size());
|
||||
for (const std::string& s : f.reasoning_breaks) m.longest = std::max(m.longest, s.size());
|
||||
*out = std::move(r);
|
||||
@@ -831,6 +905,9 @@ struct ReplyParser::Impl {
|
||||
bool calls = false; /* the request sent tools and the format has calls */
|
||||
bool breaks = false; /* an unclosed reasoning block ends at a call */
|
||||
bool pure = false; /* no reasoning, calls or document: every byte is content */
|
||||
/* A reasoning block may open later in the reply too (ChatFormat::reasoning_anywhere), and
|
||||
* this request extracts reasoning. */
|
||||
bool anywhere = false;
|
||||
std::vector<Tool> tools;
|
||||
std::unordered_map<std::string, size_t> by_name;
|
||||
std::vector<std::string> terminators;
|
||||
@@ -897,9 +974,10 @@ static int bind_tools(const ReplyFormat::Impl& F, const ojson& tools, ReplyParse
|
||||
optional.push_back(pname);
|
||||
}
|
||||
/* A name the argument's own delimiter could end early would be read as a
|
||||
* different name. */
|
||||
* different name. An object's keys are delimited by its own punctuation, and
|
||||
* one that holds it is written as a string. */
|
||||
const std::string probe = pname + F.name_end;
|
||||
if (probe.find(F.name_end) != pname.size()) {
|
||||
if (!F.object && probe.find(F.name_end) != pname.size()) {
|
||||
*why = "parameter '" + pname + "' of tool '" + t.name + "' contains '" +
|
||||
F.name_end + "', which this model's tool-call format uses to end an "
|
||||
"argument name";
|
||||
@@ -909,7 +987,8 @@ static int bind_tools(const ReplyFormat::Impl& F, const ojson& tools, ReplyParse
|
||||
}
|
||||
t.arms.insert(t.arms.end(), optional.begin(), optional.end());
|
||||
if (t.name.find(F.ns_core) != std::string::npos ||
|
||||
(t.name + F.f.name_suffix).find(F.f.name_suffix) != t.name.size()) {
|
||||
(!F.ns_object &&
|
||||
(t.name + F.f.name_suffix).find(F.f.name_suffix) != t.name.size())) {
|
||||
*why = "tool '" + t.name + "' has a name containing '" + F.ns_core +
|
||||
"', which this model's tool-call format uses to end a function name";
|
||||
return RAD_E_INVAL;
|
||||
@@ -949,6 +1028,8 @@ int ReplyParser::create(std::shared_ptr<const ReplyFormat> fmt, const ReplyOptio
|
||||
P.calls = want_calls && !opt.response_format;
|
||||
P.breaks = want_calls && F.f.has_reasoning() && !F.f.reasoning_breaks.empty();
|
||||
P.pure = !F.f.has_reasoning() && !want_calls && !opt.response_format;
|
||||
P.anywhere = F.f.reasoning_anywhere && F.f.has_reasoning() && opt.extract_reasoning &&
|
||||
!opt.response_format;
|
||||
|
||||
if (F.f.has_reasoning()) {
|
||||
switch (F.f.reasoning_in_prompt) {
|
||||
@@ -1004,6 +1085,19 @@ enum class St : uint8_t {
|
||||
RfClose, /* "```" */
|
||||
RfEnd, /* after the closing fence */
|
||||
RfRaw, /* a reply that is not the document it was asked for: content */
|
||||
ObjStart, /* after a name_suffix: whitespace, then the argument object's `{` */
|
||||
ObjKey, /* where a key may begin, or the object's `}` */
|
||||
ObjKeyBare, /* a bare key, to its `:` */
|
||||
ObjColon, /* after a quoted key: whitespace, then `:` */
|
||||
ObjValue, /* where a value may begin, or an empty array's `]` */
|
||||
ObjLit, /* the dialect's string opener, or the call's close or end, being matched */
|
||||
ObjStr, /* a dialect string, to its close */
|
||||
ObjQStr, /* a value opened with a plain quote in a dialect with strings of its own */
|
||||
ObjQLook, /* after a plain quote there: does the object's syntax go on? */
|
||||
ObjJStr, /* a JSON string, to its quote */
|
||||
ObjBare, /* a bare value: a number, a literal, or a word read as a string */
|
||||
ObjAfter, /* after a value: whitespace, then `,` or a closing bracket */
|
||||
ObjEnd, /* after the object: whitespace, then call_end */
|
||||
};
|
||||
|
||||
/* A function name the grammar does not know is still read as a call, up to this long. */
|
||||
@@ -1018,8 +1112,20 @@ struct ReplyStream::Impl {
|
||||
explicit Impl(const ReplyParser::Impl& p) : P(p), F(p.fmt->impl()) {
|
||||
content.init(P.terminators.empty() ? nullptr : &P.terms, &P.terminators);
|
||||
st = P.pure ? St::Text : St::LeadWs;
|
||||
if (P.anywhere) {
|
||||
text_dfa = P.calls ? &F.text_think : &F.think_only;
|
||||
n_open = P.calls ? F.openers.size() : 0;
|
||||
} else if (P.calls) {
|
||||
text_dfa = &F.text;
|
||||
n_open = F.openers.size();
|
||||
}
|
||||
}
|
||||
|
||||
/* What content is scanned with: the call openers when calls are on, then -- reasoning
|
||||
* anywhere -- a later block's opener and a stray close. Null: content is every byte. */
|
||||
const Dfa* text_dfa = nullptr;
|
||||
size_t n_open = 0;
|
||||
|
||||
ReplyMessage msg;
|
||||
ContentOut content;
|
||||
bool think_text = false; /* the reasoning has had a non-blank byte */
|
||||
@@ -1062,6 +1168,28 @@ struct ReplyStream::Impl {
|
||||
size_t vs_pos = 0;
|
||||
bool fenced = false;
|
||||
|
||||
/* THE ARGUMENT OBJECT being read: its open brackets, whether a key (rather than a value) is
|
||||
* being read, a separator read but not yet written -- it is written only once the next
|
||||
* member shows the object goes on, so a trailing one never reaches the JSON -- whether the
|
||||
* container was just opened, and whether a key's `:` went out with no value yet. */
|
||||
std::vector<char> ostack;
|
||||
bool okey = false, ocomma = false, ofirst = false, vpending = false;
|
||||
/* A bare value's first bytes, to tell at its end whether it was a JSON number or literal --
|
||||
* which the grammar admits -- or a word read as a string, which it does not. */
|
||||
std::string bare_txt;
|
||||
/* A JSON string's escape being read -- the backslash, and a "u" escape's four hex digits,
|
||||
* in vheld -- and a high surrogate's escape, held until the low one shows it is a pair. */
|
||||
std::string jhi;
|
||||
/* INSIDE A STRING: its trailing run of whitespace, closing brackets and quotes, held -- the
|
||||
* brackets are the object's own if the call's close follows (srun) -- the quotes and
|
||||
* backticks it has written (nq, nb), the bytes after a plain quote while they decide what it
|
||||
* was (qtail), and how a plain-quoted value writes its quotes: 0 not yet seen, 1 as \" (one
|
||||
* is unescaped), 2 as they are. */
|
||||
std::string srun, qtail;
|
||||
size_t nq = 0, nb = 0, ns = 0;
|
||||
uint8_t qmode = 0;
|
||||
char qc = '"';
|
||||
|
||||
/* published offsets, for the deltas */
|
||||
size_t sent_r = 0, sent_c = 0, sent_a = 0;
|
||||
int cur = -1; /* the channel last published: 0 r, 1 c, 2 args */
|
||||
@@ -1202,7 +1330,7 @@ struct ReplyStream::Impl {
|
||||
|
||||
bool declared_prefix(std::string_view n) const {
|
||||
for (const Tool& t : P.tools) {
|
||||
const std::string full = t.name + F.f.name_suffix;
|
||||
const std::string full = t.name + (F.ns_object ? F.ns_core : F.f.name_suffix);
|
||||
if (full.size() >= n.size() && full.compare(0, n.size(), n) == 0) return true;
|
||||
}
|
||||
return false;
|
||||
@@ -1283,14 +1411,307 @@ struct ReplyStream::Impl {
|
||||
cur = -1;
|
||||
event(ReplyEvent::CallBegin, (uint32_t)msg.calls.size() - 1, msg.calls.back().name);
|
||||
depth = 0; in_str = false; esc = false;
|
||||
args_out("{");
|
||||
argc = 0;
|
||||
++calls;
|
||||
att.clear();
|
||||
post_call = false;
|
||||
if (F.object) {
|
||||
/* The arguments are the object's, and its `{` is written now: a reply that ends
|
||||
* here is a call with no arguments yet, as in the tagged family. */
|
||||
obj_out("{");
|
||||
ostack.assign(1, '{');
|
||||
okey = ocomma = vpending = false;
|
||||
ofirst = true;
|
||||
jhi.clear();
|
||||
st = F.ns_object ? St::ObjKey : St::ObjStart;
|
||||
return;
|
||||
}
|
||||
args_out("{");
|
||||
st = St::Body;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ the argument object
|
||||
*
|
||||
* The object of the object family, read as it streams and written as JSON as it is read:
|
||||
* brackets and separators as they come, a key as soon as its `:` shows it is one, a string
|
||||
* as its bytes arrive, and a bare value as the tagged family writes a typed one (ValueOut,
|
||||
* Raw): held while it could still be a literal or a number, else a string. What the dialect
|
||||
* spells otherwise -- its string delimiters, its bare keys -- is respelled; what leaves it
|
||||
* -- a missing separator, a trailing one, a bracket of the wrong kind, an object the call's
|
||||
* close cuts off -- is repaired locally and said so (strict). The arguments a closed call
|
||||
* reports are JSON whatever was written. */
|
||||
|
||||
void obj_out(std::string_view s) {
|
||||
if (s.empty()) return;
|
||||
touch(2);
|
||||
msg.calls.back().arguments.append(s.data(), s.size());
|
||||
}
|
||||
void obj_flush() { if (!scratch.empty()) { obj_out(scratch); scratch.clear(); } }
|
||||
|
||||
/* A member or an element begins: the separator before it, if one was read, goes out now. */
|
||||
void obj_sep() {
|
||||
if (ocomma) { obj_out(","); ocomma = false; }
|
||||
ofirst = false;
|
||||
}
|
||||
|
||||
/* The outermost bracket closed: what follows the object. */
|
||||
void obj_done() {
|
||||
if (!F.end_core.empty()) { lpos = 0; lhold.clear(); st = St::ObjEnd; }
|
||||
else end_call();
|
||||
}
|
||||
|
||||
/* A closing bracket where a key, a value or a separator may stand. A separator before it is
|
||||
* dropped, and a bracket of the other kind closes the open container all the same. */
|
||||
void obj_close_bracket(char c) {
|
||||
if (ocomma) { lenient(); ocomma = false; }
|
||||
const char top = ostack.back();
|
||||
if ((top == '{') != (c == '}')) lenient();
|
||||
if (vpending) { lenient(); obj_out("null"); vpending = false; }
|
||||
ostack.pop_back();
|
||||
ofirst = false;
|
||||
obj_out(top == '{' ? "}" : "]");
|
||||
if (ostack.empty()) obj_done();
|
||||
else st = St::ObjAfter;
|
||||
}
|
||||
|
||||
/* The punctuation after a value. */
|
||||
void obj_struct(char c) {
|
||||
if (c != ',') { obj_close_bracket(c); return; }
|
||||
if (ocomma) lenient(); /* `,,`: one separator */
|
||||
ocomma = true;
|
||||
ofirst = false;
|
||||
okey = ostack.back() == '{';
|
||||
st = okey ? St::ObjKey : St::ObjValue;
|
||||
}
|
||||
|
||||
bool obj_lit_start(unsigned char c) const {
|
||||
for (const std::string& l : F.obj_lits)
|
||||
if ((unsigned char)l[0] == c) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Bytes that open no string and close no call: a bare key or a bare value. */
|
||||
void obj_bare_begin() {
|
||||
ds = 0;
|
||||
hold.clear();
|
||||
if (okey) {
|
||||
if (!F.f.object_bare_keys) lenient();
|
||||
name.clear();
|
||||
st = St::ObjKeyBare;
|
||||
return;
|
||||
}
|
||||
obj_sep();
|
||||
vpending = false;
|
||||
bare_txt.clear();
|
||||
val.begin(ValueOut::Raw, /*cdata=*/false, /*lead=*/false, /*trail=*/false, scratch);
|
||||
obj_flush();
|
||||
st = St::ObjBare;
|
||||
}
|
||||
|
||||
static constexpr size_t kBareKeep = 64; /* longer than any number a model writes */
|
||||
|
||||
/* A bare value ended: one that is no JSON number or literal was read by the recovery rule. */
|
||||
void bare_done() {
|
||||
std::string_view v(bare_txt);
|
||||
while (!v.empty() && is_ws((unsigned char)v.back())) v.remove_suffix(1);
|
||||
const bool json = bare_txt.size() < kBareKeep && !v.empty() &&
|
||||
(v == "true" || v == "false" || v == "null" ||
|
||||
((v[0] == '-' || (v[0] >= '0' && v[0] <= '9')) &&
|
||||
nlohmann::json::accept(v)));
|
||||
if (!json) lenient();
|
||||
}
|
||||
|
||||
/* A JSON string, which is the dialect's own only when it declares no string delimiters. */
|
||||
void json_string_begin() {
|
||||
if (!F.f.object_string_open.empty()) lenient();
|
||||
obj_out("\"");
|
||||
vheld.clear();
|
||||
st = St::ObjJStr;
|
||||
}
|
||||
|
||||
/* One of the anchored literals matched where a key or a value may begin. */
|
||||
void obj_lit_done(uint8_t kind) {
|
||||
if (kind == ReplyFormat::Impl::LitString) {
|
||||
obj_sep();
|
||||
if (!okey) vpending = false;
|
||||
obj_out("\"");
|
||||
ds = 0;
|
||||
hold.clear();
|
||||
str_begin();
|
||||
st = St::ObjStr;
|
||||
return;
|
||||
}
|
||||
/* The call's close or end where the object wanted more: the reply left the object
|
||||
* unclosed, and the call ends here with it closed. */
|
||||
lenient();
|
||||
ocomma = false;
|
||||
if (kind == ReplyFormat::Impl::LitClose) end_call_at_close();
|
||||
else end_core_done();
|
||||
}
|
||||
|
||||
/* A JSON string's text goes out as it is, its escapes checked: one JSON has not is the
|
||||
* backslash and the letter as text, a raw control character is escaped, and a surrogate
|
||||
* escape that is not half of a pair is U+FFFD -- a client's JSON parser refuses all three. */
|
||||
void jstr_char(const char* q, size_t n) {
|
||||
if (n == 0) return;
|
||||
if (!jhi.empty()) { lenient(); jhi.clear(); obj_out("\\ufffd"); }
|
||||
touch(2);
|
||||
json_escape_into(msg.calls.back().arguments, q, n);
|
||||
}
|
||||
|
||||
void jstr_escape_done(const std::string& e) {
|
||||
/* e: a complete two-character escape, or a "u" escape with its four digits. */
|
||||
if (e.size() == 6) {
|
||||
const unsigned long cp = std::strtoul(e.c_str() + 2, nullptr, 16);
|
||||
if (cp >= 0xD800 && cp <= 0xDBFF) {
|
||||
if (!jhi.empty()) { lenient(); obj_out("\\ufffd"); }
|
||||
jhi = e;
|
||||
return;
|
||||
}
|
||||
if (cp >= 0xDC00 && cp <= 0xDFFF) {
|
||||
if (jhi.empty()) { lenient(); obj_out("\\ufffd"); return; }
|
||||
obj_out(jhi + e);
|
||||
jhi.clear();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!jhi.empty()) { lenient(); jhi.clear(); obj_out("\\ufffd"); }
|
||||
obj_out(e);
|
||||
}
|
||||
|
||||
static constexpr size_t kRunKeep = 16; /* the closers a string's end may hold back */
|
||||
static constexpr size_t kLookKeep = 32; /* what deciding a close may read ahead */
|
||||
|
||||
static bool run_byte(char c) {
|
||||
return c == '}' || c == ']' || c == '"' || c == '`' || c == '\'' || is_ws((unsigned char)c);
|
||||
}
|
||||
|
||||
void str_begin() { srun.clear(); nq = nb = ns = 0; }
|
||||
void str_emit(const char* q, size_t n) {
|
||||
if (n == 0) return;
|
||||
nq += (size_t)std::count(q, q + n, '"');
|
||||
nb += (size_t)std::count(q, q + n, '`');
|
||||
ns += (size_t)std::count(q, q + n, '\'');
|
||||
touch(2);
|
||||
json_escape_into(msg.calls.back().arguments, q, n);
|
||||
}
|
||||
void str_flush() { str_emit(srun.data(), srun.size()); srun.clear(); }
|
||||
|
||||
/* A string's text: all of it goes out but a trailing run of whitespace and closers. */
|
||||
void str_text(const char* q, size_t n) {
|
||||
size_t k = n;
|
||||
while (k > 0 && run_byte(q[k - 1])) --k;
|
||||
if (k > 0) { str_flush(); str_emit(q, k); }
|
||||
srun.append(q + k, n - k);
|
||||
if (srun.size() > kRunKeep) {
|
||||
const size_t out = srun.size() - kRunKeep;
|
||||
str_emit(srun.data(), out);
|
||||
srun.erase(0, out);
|
||||
}
|
||||
}
|
||||
|
||||
/* Where the held run stops being text: the closers after it close every open bracket of the
|
||||
* object, innermost first, as the model writes them before the call's close -- read from the
|
||||
* end, the last closes the outermost. npos: they do not. */
|
||||
size_t run_cut() const {
|
||||
size_t i = srun.size(), k = 0;
|
||||
while (i > 0 && k < ostack.size()) {
|
||||
const char c = srun[i - 1];
|
||||
if (is_ws((unsigned char)c)) { --i; continue; }
|
||||
if (c != (ostack[k] == '{' ? '}' : ']')) break;
|
||||
--i;
|
||||
++k;
|
||||
}
|
||||
return k == ostack.size() && k > 0 ? i : std::string::npos;
|
||||
}
|
||||
|
||||
static bool starts(std::string_view a, std::string_view b) {
|
||||
return a.size() >= b.size() && a.compare(0, b.size(), b) == 0;
|
||||
}
|
||||
|
||||
/* The call's close in a string whose held run closes the whole object: the model left the
|
||||
* string open (measured: the close delimiter at p=0.86, `}` at p=0.08), and this is where
|
||||
* the call ends -- as vLLM's and HF's Gemma 4 parsers end it, at the first `}<tool_call|>`.
|
||||
* The closers are the object's. A quote or backtick just before them that leaves the string
|
||||
* with an odd count of its kind was the string's close, written in the wrong spelling. */
|
||||
void cut_begin() {
|
||||
const size_t cut = run_cut();
|
||||
if (cut == std::string::npos) { str_flush(); str_emit(F.cc_core.data(), F.cc_core.size()); return; }
|
||||
lenient();
|
||||
size_t keep = cut;
|
||||
if (keep > 0 && (srun[keep - 1] == '"' || srun[keep - 1] == '`' || srun[keep - 1] == '\'')) {
|
||||
const char c = srun[keep - 1];
|
||||
const size_t n = (c == '"' ? nq : c == '`' ? nb : ns) +
|
||||
(size_t)std::count(srun.begin(), srun.begin() + (long)keep, c);
|
||||
if (n % 2 == 1) --keep;
|
||||
}
|
||||
str_emit(srun.data(), keep);
|
||||
srun.clear();
|
||||
if (!jhi.empty()) jhi.clear();
|
||||
obj_out(okey ? "\":null" : "\"");
|
||||
vpending = false;
|
||||
ocomma = false;
|
||||
/* As after a call's close of its own: a repeated close goes with it. */
|
||||
end_call();
|
||||
}
|
||||
|
||||
/* After a plain quote in a plain-quoted value: 0 not yet known, 1 it closed the value -- the
|
||||
* object goes on with a member, an element, its closers and the call's close -- 2 it was
|
||||
* text. */
|
||||
int q_verdict() const {
|
||||
const std::string& q = qtail;
|
||||
if (q.size() > kLookKeep) return 2;
|
||||
const size_t n = q.size();
|
||||
size_t i = 0, top = ostack.size();
|
||||
auto ws = [&] { while (i < n && is_ws((unsigned char)q[i])) ++i; };
|
||||
ws();
|
||||
while (i < n && top > 0 && q[i] == (ostack[top - 1] == '{' ? '}' : ']')) { --top; ++i; ws(); }
|
||||
if (i == n) return 0;
|
||||
const std::string& so = F.f.object_string_open;
|
||||
std::string_view r(q.data() + i, n - i);
|
||||
if (!F.cc_core.empty()) {
|
||||
if (starts(r, F.cc_core)) return 1;
|
||||
if (starts(F.cc_core, r)) return 0;
|
||||
}
|
||||
if (top == 0 || q[i] != ',') return 2;
|
||||
++i;
|
||||
ws();
|
||||
if (i == n) return 0;
|
||||
r = std::string_view(q.data() + i, n - i);
|
||||
const unsigned char c = (unsigned char)q[i];
|
||||
if (starts(r, so)) return 1;
|
||||
if (starts(so, r)) return 0;
|
||||
if (ostack[top - 1] == '{') {
|
||||
if (!(std::isalpha(c) || c == '_')) return 2;
|
||||
while (i < n && (std::isalnum((unsigned char)q[i]) || q[i] == '_')) ++i;
|
||||
ws();
|
||||
if (i == n) return 0;
|
||||
return q[i] == ':' ? 1 : 2;
|
||||
}
|
||||
return (c == '"' || c == '\'' || c == '{' || c == '[' || c == '-' || std::isdigit(c)) ? 1 : 2;
|
||||
}
|
||||
|
||||
void q_done(bool closed) {
|
||||
std::string again;
|
||||
again.swap(qtail);
|
||||
if (closed) {
|
||||
str_flush();
|
||||
obj_string_done();
|
||||
} else {
|
||||
if (qmode == 0) qmode = 2;
|
||||
str_text(&qc, 1);
|
||||
st = St::ObjQStr;
|
||||
}
|
||||
read_again(again);
|
||||
}
|
||||
|
||||
/* A string or a key closed. */
|
||||
void obj_string_done() {
|
||||
if (!jhi.empty()) { lenient(); jhi.clear(); obj_out("\\ufffd"); }
|
||||
obj_out("\"");
|
||||
st = okey ? St::ObjColon : St::ObjAfter;
|
||||
}
|
||||
|
||||
/* The core of call_end has matched. The whitespace the format writes after it is part of the
|
||||
* tag, so the call closes when that has arrived too -- a reply that ends in between leaves the
|
||||
* call open, as the grammar does -- and a reply that goes straight on without it has closed
|
||||
@@ -1306,8 +1727,18 @@ struct ReplyStream::Impl {
|
||||
* match, so no quote is ever pending here; a raw value that left a string open is passed
|
||||
* through as the model wrote it. */
|
||||
void end_call() {
|
||||
std::string close((size_t)std::max(depth, 0), '}');
|
||||
args_out(close);
|
||||
if (F.object) {
|
||||
/* What the object left open is closed, so the arguments are JSON: a key whose value
|
||||
* never came is null, and each bracket gets its partner. */
|
||||
std::string close = vpending ? "null" : "";
|
||||
vpending = false;
|
||||
for (auto it = ostack.rbegin(); it != ostack.rend(); ++it) close += *it == '{' ? '}' : ']';
|
||||
ostack.clear();
|
||||
obj_out(close);
|
||||
} else {
|
||||
std::string close((size_t)std::max(depth, 0), '}');
|
||||
args_out(close);
|
||||
}
|
||||
publish(2, true);
|
||||
msg.calls.back().closed = true;
|
||||
event(ReplyEvent::CallEnd, (uint32_t)msg.calls.size() - 1, {});
|
||||
@@ -1499,14 +1930,22 @@ struct ReplyStream::Impl {
|
||||
return p;
|
||||
|
||||
case St::Text:
|
||||
if (!P.calls) {
|
||||
if (!text_dfa) {
|
||||
text_out(p, (size_t)(e - p));
|
||||
return e;
|
||||
}
|
||||
p = scan(F.text, p, e, &hit, [this](const char* q, size_t n) { text_out(q, n); });
|
||||
if (hit >= 0) {
|
||||
p = scan(*text_dfa, p, e, &hit, [this](const char* q, size_t n) { text_out(q, n); });
|
||||
if (hit < 0) return p;
|
||||
if ((size_t)hit < n_open) {
|
||||
att_at = here(p) - F.openers[(size_t)hit].size();
|
||||
begin_text_attempt(hit);
|
||||
} else if ((size_t)hit == n_open) {
|
||||
/* A reasoning block opens again, later in the reply. */
|
||||
trail = 0;
|
||||
st = St::ThinkOpenWs;
|
||||
} else {
|
||||
/* Its close with no block open: not part of the answer. */
|
||||
lenient();
|
||||
}
|
||||
return p;
|
||||
|
||||
@@ -1558,12 +1997,18 @@ struct ReplyStream::Impl {
|
||||
lpos = 0;
|
||||
lhold.clear();
|
||||
tag_end = att.size();
|
||||
if (loose) st = St::OpenPeek;
|
||||
if (loose && F.object) {
|
||||
/* A call written without its wrapper, in the object family: a declared
|
||||
* tool's name and the object's opening are what make it one. */
|
||||
if (tool < 0) { fail_attempt(); return p + 1; }
|
||||
begin_call(false);
|
||||
} else if (loose) st = St::OpenPeek;
|
||||
else if (F.ns_trail.empty()) begin_call(true);
|
||||
else st = St::OpenTail;
|
||||
return p + 1;
|
||||
}
|
||||
if (name.size() > kMaxLooseName || (c == '<' && !declared_prefix(name))) {
|
||||
if (name.size() > kMaxLooseName || (c == '<' && !declared_prefix(name)) ||
|
||||
(loose && F.object && !declared_prefix(name))) {
|
||||
fail_attempt();
|
||||
return p + 1;
|
||||
}
|
||||
@@ -1775,6 +2220,279 @@ struct ReplyStream::Impl {
|
||||
if (p < e) { post_call = true; ds = 0; hold.clear(); st = St::Text; }
|
||||
return p;
|
||||
|
||||
case St::ObjStart:
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
if (*p == '{') { ofirst = true; okey = true; st = St::ObjKey; return p + 1; }
|
||||
/* No object after the name: the call takes none, and what stands there instead is
|
||||
* read as what follows a call. */
|
||||
lenient();
|
||||
obj_done();
|
||||
return p;
|
||||
|
||||
case St::ObjKey: {
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
const char c = *p;
|
||||
okey = true;
|
||||
if (c == '}' || c == ']') { obj_close_bracket(c); return p + 1; }
|
||||
if (c == ',') { lenient(); return p + 1; } /* an empty member */
|
||||
if (c == '"') {
|
||||
obj_sep();
|
||||
json_string_begin();
|
||||
return p + 1;
|
||||
}
|
||||
if (obj_lit_start((unsigned char)c)) { lhold.clear(); st = St::ObjLit; return p; }
|
||||
obj_bare_begin();
|
||||
return p;
|
||||
}
|
||||
|
||||
case St::ObjKeyBare:
|
||||
p = scan(F.obj_key, p, e, &hit, [this](const char* q, size_t n) { name.append(q, n); });
|
||||
if (hit < 0) return p;
|
||||
if (hit <= 2) {
|
||||
std::string_view k(name);
|
||||
while (!k.empty() && is_ws((unsigned char)k.back())) k.remove_suffix(1);
|
||||
if (k.empty()) lenient();
|
||||
obj_sep();
|
||||
std::string out = json_quote(k);
|
||||
name.clear();
|
||||
if (hit == 0) {
|
||||
obj_out(out + ":");
|
||||
vpending = true;
|
||||
okey = false;
|
||||
st = St::ObjValue;
|
||||
return p;
|
||||
}
|
||||
/* A key with no value: it is null. */
|
||||
lenient();
|
||||
obj_out(out + ":null");
|
||||
obj_struct(hit == 1 ? ',' : '}');
|
||||
return p;
|
||||
}
|
||||
/* The call's close or end inside a key: the key had no value, and goes. */
|
||||
name.clear();
|
||||
lenient();
|
||||
ocomma = false;
|
||||
if (hit == F.key_cc) end_call_at_close();
|
||||
else end_core_done();
|
||||
return p;
|
||||
|
||||
case St::ObjColon:
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
vpending = true;
|
||||
okey = false;
|
||||
st = St::ObjValue;
|
||||
if (*p == ':') { obj_out(":"); return p + 1; }
|
||||
lenient();
|
||||
obj_out(":");
|
||||
return p;
|
||||
|
||||
case St::ObjValue: {
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
const char c = *p;
|
||||
const bool in_array = ostack.back() == '[';
|
||||
okey = false;
|
||||
if (c == '{' || c == '[') {
|
||||
obj_sep();
|
||||
vpending = false;
|
||||
obj_out(c == '{' ? "{" : "[");
|
||||
ostack.push_back(c);
|
||||
ofirst = true;
|
||||
okey = c == '{';
|
||||
st = c == '{' ? St::ObjKey : St::ObjValue;
|
||||
return p + 1;
|
||||
}
|
||||
if (c == ']' && in_array && ofirst) { obj_close_bracket(c); return p + 1; }
|
||||
if (c == '}' || c == ']' || c == ',') {
|
||||
/* No value where one was due: a member's is null, and an array's gap is no
|
||||
* element. */
|
||||
lenient();
|
||||
if (!in_array) {
|
||||
obj_out("null");
|
||||
vpending = false;
|
||||
st = St::ObjAfter;
|
||||
return p;
|
||||
}
|
||||
if (c == ',') return p + 1;
|
||||
obj_close_bracket(c);
|
||||
return p + 1;
|
||||
}
|
||||
if ((c == '"' || c == '\'') && !F.f.object_string_open.empty()) {
|
||||
/* Measured on Gemma 4: such a quote -- `"`, or `'` as Python and JavaScript
|
||||
* write strings -- is followed by the text verbatim, or with only its quotes
|
||||
* escaped, never JSON's other escapes. */
|
||||
obj_sep();
|
||||
vpending = false;
|
||||
lenient();
|
||||
obj_out("\"");
|
||||
qc = c;
|
||||
qmode = 0;
|
||||
str_begin();
|
||||
st = St::ObjQStr;
|
||||
return p + 1;
|
||||
}
|
||||
if (c == '"') {
|
||||
obj_sep();
|
||||
vpending = false;
|
||||
json_string_begin();
|
||||
return p + 1;
|
||||
}
|
||||
if (obj_lit_start((unsigned char)c)) { lhold.clear(); st = St::ObjLit; return p; }
|
||||
obj_bare_begin();
|
||||
return p;
|
||||
}
|
||||
|
||||
case St::ObjLit: {
|
||||
const char c = *p;
|
||||
lhold.push_back(c);
|
||||
bool part = false;
|
||||
for (size_t i = 0; i < F.obj_lits.size(); ++i) {
|
||||
const std::string& l = F.obj_lits[i];
|
||||
if (lhold.size() > l.size() || l.compare(0, lhold.size(), lhold) != 0) continue;
|
||||
if (lhold.size() < l.size()) { part = true; continue; }
|
||||
lhold.clear();
|
||||
obj_lit_done(F.obj_lit_kind[i]);
|
||||
return p + 1;
|
||||
}
|
||||
if (part) return p + 1;
|
||||
/* None of them: the bytes begin a bare key or value. */
|
||||
std::string again;
|
||||
again.swap(lhold);
|
||||
obj_bare_begin();
|
||||
read_again(again);
|
||||
return p + 1;
|
||||
}
|
||||
|
||||
case St::ObjStr:
|
||||
p = scan(F.obj_str, p, e, &hit, [this](const char* q, size_t n) { str_text(q, n); });
|
||||
if (hit < 0) return p;
|
||||
if (hit == 0) { str_flush(); obj_string_done(); }
|
||||
else cut_begin();
|
||||
return p;
|
||||
|
||||
case St::ObjQStr:
|
||||
p = scan(qc == '"' ? F.obj_qstr : F.obj_sqstr, p, e, &hit,
|
||||
[this](const char* q, size_t n) { str_text(q, n); });
|
||||
if (hit < 0) return p;
|
||||
if (hit == 0) { qtail.clear(); st = St::ObjQLook; }
|
||||
else if (hit == 1) {
|
||||
if (qmode == 0) qmode = 1;
|
||||
const char esc[2] = { '\\', qc };
|
||||
if (qmode == 1) str_text(&qc, 1);
|
||||
else str_text(esc, 2);
|
||||
} else if (hit == 2) { lenient(); str_flush(); obj_string_done(); }
|
||||
else cut_begin();
|
||||
return p;
|
||||
|
||||
case St::ObjQLook: {
|
||||
qtail.push_back(*p);
|
||||
const int v = q_verdict();
|
||||
if (v) q_done(v == 1);
|
||||
return p + 1;
|
||||
}
|
||||
|
||||
case St::ObjJStr: {
|
||||
const char* run = p;
|
||||
while (p < e) {
|
||||
const unsigned char c = (unsigned char)*p;
|
||||
if (vheld.size() == 1) { /* after the backslash */
|
||||
if (c && std::strchr("\"\\/bfnrt", (int)c)) {
|
||||
vheld.push_back((char)c);
|
||||
jstr_escape_done(vheld);
|
||||
vheld.clear();
|
||||
run = ++p;
|
||||
continue;
|
||||
}
|
||||
if (c == 'u') { vheld.push_back('u'); run = ++p; continue; }
|
||||
/* No JSON escape: the backslash is text, and what follows it is read as
|
||||
* the string's next byte. */
|
||||
lenient();
|
||||
vheld.clear();
|
||||
jstr_char("\\", 1);
|
||||
run = p;
|
||||
continue;
|
||||
}
|
||||
if (!vheld.empty()) { /* a "u" escape's digits */
|
||||
if (std::isxdigit(c)) {
|
||||
vheld.push_back((char)c);
|
||||
if (vheld.size() == 6) { jstr_escape_done(vheld); vheld.clear(); }
|
||||
run = ++p;
|
||||
continue;
|
||||
}
|
||||
lenient();
|
||||
const std::string text = vheld;
|
||||
vheld.clear();
|
||||
jstr_char(text.data(), text.size());
|
||||
run = p;
|
||||
continue;
|
||||
}
|
||||
if (c != '"' && c != '\\') { ++p; continue; }
|
||||
jstr_char(run, (size_t)(p - run));
|
||||
++p;
|
||||
if (c == '"') { obj_string_done(); return p; }
|
||||
vheld.assign(1, '\\');
|
||||
run = p;
|
||||
}
|
||||
jstr_char(run, (size_t)(p - run));
|
||||
return p;
|
||||
}
|
||||
|
||||
case St::ObjBare:
|
||||
p = scan(F.obj_bare, p, e, &hit, [this](const char* q, size_t n) {
|
||||
if (bare_txt.size() < kBareKeep)
|
||||
bare_txt.append(q, std::min(n, kBareKeep - bare_txt.size()));
|
||||
val.add(q, n, scratch);
|
||||
obj_flush();
|
||||
});
|
||||
if (hit < 0) return p;
|
||||
val.finish(false, scratch);
|
||||
obj_flush();
|
||||
bare_done();
|
||||
if (hit <= 2) { obj_struct(",}]"[hit]); return p; }
|
||||
lenient();
|
||||
if (hit == F.bare_cc) end_call_at_close();
|
||||
else end_core_done();
|
||||
return p;
|
||||
|
||||
case St::ObjAfter: {
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
const char c = *p;
|
||||
if (c == ',' || c == '}' || c == ']') { obj_struct(c); return p + 1; }
|
||||
/* A key or a value with no separator before it: one is supplied. */
|
||||
lenient();
|
||||
ocomma = true;
|
||||
ofirst = false;
|
||||
okey = ostack.back() == '{';
|
||||
st = okey ? St::ObjKey : St::ObjValue;
|
||||
return p;
|
||||
}
|
||||
|
||||
case St::ObjEnd: {
|
||||
if (lpos == 0) {
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p == e) return p;
|
||||
}
|
||||
const char c = *p;
|
||||
if (c == F.end_core[lpos]) {
|
||||
lhold.push_back(c);
|
||||
if (++lpos == F.end_core.size()) { lhold.clear(); lpos = 0; end_core_done(); }
|
||||
return p + 1;
|
||||
}
|
||||
/* The object is not followed by the call's end: the call ends with it all the same,
|
||||
* and what stands there is read as what follows a call. */
|
||||
lenient();
|
||||
std::string again;
|
||||
again.swap(lhold);
|
||||
lpos = 0;
|
||||
end_call();
|
||||
read_again(again);
|
||||
return p;
|
||||
}
|
||||
|
||||
case St::RfWs:
|
||||
while (p < e && is_ws((unsigned char)*p)) ++p;
|
||||
if (p < e) {
|
||||
@@ -1962,6 +2680,14 @@ struct ReplyStream::Impl {
|
||||
}
|
||||
|
||||
void finish_all() {
|
||||
/* The reply ended while a close was being decided: it was one. */
|
||||
while (st == St::ObjQLook) {
|
||||
q_done(true);
|
||||
std::string again = std::move(refeed);
|
||||
refeed.clear();
|
||||
run(again, fed >= again.size() ? fed - again.size() : 0);
|
||||
}
|
||||
if (st == St::ObjStr || st == St::ObjQStr) str_flush();
|
||||
while (in_loose_attempt()) {
|
||||
fail_attempt();
|
||||
std::string again = std::move(refeed);
|
||||
@@ -1998,6 +2724,11 @@ struct ReplyStream::Impl {
|
||||
case St::ValJsonEnd:
|
||||
json_value_done(std::string_view(vheld).substr(0, vheld.size() - vs_pos));
|
||||
break;
|
||||
case St::ObjBare:
|
||||
/* A bare value the reply was cut off inside: as it arrived. The call is open. */
|
||||
val.finish(true, scratch);
|
||||
obj_flush();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -2066,7 +2797,9 @@ ReplyHeld ReplyStream::held() const {
|
||||
(m.msg.reasoning.size() - m.sent_r) + (m.msg.content.size() - m.sent_c) +
|
||||
(m.msg.calls.empty() ? 0 : m.msg.calls.back().arguments.size() - m.sent_a);
|
||||
h.space = m.content.held_space() + m.think_pend.size();
|
||||
h.structure = m.att.size() + m.name.size() + m.vheld.size() + m.val.held_container();
|
||||
h.delimiter += m.srun.size() + m.qtail.size();
|
||||
h.structure = m.att.size() + m.name.size() + m.vheld.size() + m.val.held_container() +
|
||||
m.jhi.size();
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
@@ -623,6 +623,22 @@ int Vocab::load(VocabBuild b) {
|
||||
RAD_INFO("vocab: end-of-generation also on %s (a control token the container declares "
|
||||
"neither as eos nor eot; without this a turn ending on it runs on)",
|
||||
added.c_str());
|
||||
|
||||
/* AND WHAT THE CHECKPOINT ITSELF NAMES. generation_config.json's eos_token_id is the
|
||||
* model's own list of tokens a generation ends on, and the one that is not a guess:
|
||||
* Gemma 4 names <|tool_response> there beside <eos> and <turn|>, because after its calls
|
||||
* the model writes it and waits for the response -- a turn that runs past it answers
|
||||
* its own call. Every id in range is honoured, as transformers honours them. */
|
||||
std::string named;
|
||||
for (int32_t id : b_.eos_ids) {
|
||||
if (id < 0 || id >= (int32_t)b_.tokens.size()) continue;
|
||||
if (std::find(eog_.begin(), eog_.end(), id) != eog_.end()) continue;
|
||||
eog_.push_back(id);
|
||||
named += (named.empty() ? "" : " ") + b_.tokens[id] + "=" + std::to_string(id);
|
||||
}
|
||||
if (!named.empty())
|
||||
RAD_INFO("vocab: end-of-generation also on %s (named by the checkpoint's "
|
||||
"generation_config.json)", named.c_str());
|
||||
}
|
||||
|
||||
/* The encoder: the chain compiled and the tables flattened. A pattern the regex compiler
|
||||
|
||||
@@ -86,6 +86,11 @@ struct VocabBuild {
|
||||
|
||||
/* Diagnostics only: what rad-convert thought it was reading. Not baked. */
|
||||
std::string source;
|
||||
|
||||
/* The ids the checkpoint's generation_config.json names as eos_token_id, which the container
|
||||
* carries as metadata (`generation.eos_token_id`) rather than in the vocabulary section. The
|
||||
* engine fills it from there before load(); not baked. */
|
||||
std::vector<int32_t> eos_ids;
|
||||
};
|
||||
|
||||
/* ================================================================== the loaded vocab */
|
||||
|
||||
Reference in new issue
Block a user