Archived
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
228 lines
10 KiB
C++
228 lines
10 KiB
C++
#include "text/chat.h"
|
|
#include "text/tokenizer.h"
|
|
|
|
/* Lifted headers: only this file, chatfmt.cpp and chatparse.cpp include them (see chat.h). */
|
|
#include "llama/common/chat.h"
|
|
/* For jinja::enable_debug (RADIANCE_DEBUG_JINJA). */
|
|
#include "llama/common/jinja/runtime.h"
|
|
#include "shim/log.h"
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <cstdlib>
|
|
|
|
namespace rad {
|
|
|
|
using ojson = nlohmann::ordered_json;
|
|
|
|
struct ChatTemplate::Impl {
|
|
common_chat_templates_ptr tmpls;
|
|
ChatCaps caps;
|
|
std::string src;
|
|
bool add_bos = false, add_eos = false;
|
|
};
|
|
|
|
ChatTemplate::ChatTemplate() : impl_(std::make_unique<Impl>()) {}
|
|
ChatTemplate::~ChatTemplate() = default;
|
|
|
|
const char* ChatTemplate::chatml_source() {
|
|
/* Byte-identical to CHATML_TEMPLATE_SRC in the lift, copied so the lifted header stays
|
|
* unedited. Keep in sync on a vendor refresh. */
|
|
return
|
|
"{%- for message in messages -%}\n"
|
|
" {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>\n' -}}\n"
|
|
"{%- endfor -%}\n"
|
|
"{%- if add_generation_prompt -%}\n"
|
|
" {{- '<|im_start|>assistant\n' -}}\n"
|
|
"{%- endif -%}";
|
|
}
|
|
|
|
int ChatTemplate::load(const std::string& jinja_source, const std::string& bos,
|
|
const std::string& eos, bool add_bos, bool add_eos) {
|
|
/* Gates the vendored LOG_DBG to stderr. Set here, where the template is first touched,
|
|
* because the auto-parser analyses it in apply() and again in chatfmt.cpp; any later is too
|
|
* late to trace a format the analyzer could not handle. */
|
|
rad_vendor_log::verbose() = (int)log_level() >= (int)Log::Debug;
|
|
|
|
/* The template engine's own trace. The capability probe in common_chat_templates_init
|
|
* swallows every exception, so a template without tool support and one that throws on the
|
|
* probe both report tools=0; this trace tells them apart. Very verbose: it logs every
|
|
* statement of every render, serving ones included. */
|
|
if (const char* dbg = std::getenv("RADIANCE_DEBUG_JINJA"); dbg && *dbg && *dbg != '0')
|
|
jinja::enable_debug(true);
|
|
|
|
const bool fallback = jinja_source.empty();
|
|
if (fallback) {
|
|
/* ChatML is only a guess at the prompt format, so warn once. */
|
|
RAD_WARN("chat: the model declares no chat template; falling back to ChatML. "
|
|
"If the model was not trained on ChatML its replies will be subtly wrong.");
|
|
}
|
|
const std::string src = fallback ? std::string(chatml_source()) : jinja_source;
|
|
|
|
try {
|
|
/* model = nullptr: what would be read off a llama_model is passed in as overrides
|
|
* (vendor/shim/llama.h). */
|
|
impl_->tmpls = common_chat_templates_init(nullptr, src, bos, eos);
|
|
} catch (const std::exception& e) {
|
|
RAD_ERR("chat: template did not compile: %s", e.what());
|
|
impl_->tmpls.reset();
|
|
return RAD_E_FORMAT;
|
|
}
|
|
if (!impl_->tmpls) return RAD_E_FORMAT;
|
|
|
|
impl_->src = src;
|
|
impl_->add_bos = add_bos;
|
|
impl_->add_eos = add_eos;
|
|
|
|
const std::map<std::string, bool> m = common_chat_templates_get_caps(impl_->tmpls.get());
|
|
auto get = [&](const char* k, bool dflt) {
|
|
auto it = m.find(k);
|
|
return it == m.end() ? dflt : it->second;
|
|
};
|
|
impl_->caps.tools = get("supports_tools", true);
|
|
impl_->caps.tool_calls = get("supports_tool_calls", true);
|
|
impl_->caps.system_role = get("supports_system_role", true);
|
|
impl_->caps.parallel_tool_calls = get("supports_parallel_tool_calls", true);
|
|
impl_->caps.preserve_reasoning = get("supports_preserve_reasoning", false);
|
|
impl_->caps.string_content = get("supports_string_content", true);
|
|
impl_->caps.typed_content = get("supports_typed_content", false);
|
|
impl_->caps.object_arguments = get("supports_object_arguments", false);
|
|
|
|
RAD_INFO("chat: template loaded (%zu bytes)%s; tools=%d tool_calls=%d system=%d thinking=%d",
|
|
src.size(), fallback ? " [ChatML fallback]" : "",
|
|
(int)impl_->caps.tools, (int)impl_->caps.tool_calls,
|
|
(int)impl_->caps.system_role,
|
|
(int)common_chat_templates_support_enable_thinking(impl_->tmpls.get()));
|
|
return RAD_OK;
|
|
}
|
|
|
|
int ChatTemplate::load_from_vocab(const Vocab& v) {
|
|
/* Templates reference bos_token/eos_token by text; Vocab::text() returns a control token's
|
|
* literal spelling. */
|
|
const std::string bos = v.bos() >= 0 ? v.text(v.bos()) : std::string();
|
|
const std::string eos = v.eos() >= 0 ? v.text(v.eos()) : std::string();
|
|
return load(v.chat_template(), bos, eos, v.add_bos(), v.add_eos());
|
|
}
|
|
|
|
bool ChatTemplate::ready() const { return impl_ && impl_->tmpls != nullptr; }
|
|
const ChatCaps& ChatTemplate::caps() const { return impl_->caps; }
|
|
const std::string& ChatTemplate::source() const { return impl_->src; }
|
|
|
|
/* Maximum nesting (objects and arrays) of a client's tool or response schema. The JSON-schema
|
|
* converter recurses once per level and names each rule after its full path, so its output grows
|
|
* with the square of the depth; a few thousand levels exhaust memory or the rendering thread's
|
|
* stack. 512 still converts in tens of milliseconds. grammar.cpp bounds the same converter on the
|
|
* path that bypasses the template. */
|
|
static constexpr size_t kMaxSchemaDepth = 512;
|
|
|
|
/* Iterative, because the values it must catch are too deep to recurse over. */
|
|
static size_t json_depth(const ojson& root) {
|
|
std::vector<std::pair<const ojson*, size_t>> todo{ { &root, 1 } };
|
|
size_t deepest = 0;
|
|
while (!todo.empty()) {
|
|
const auto [j, d] = todo.back();
|
|
todo.pop_back();
|
|
if (!j->is_structured()) continue;
|
|
deepest = std::max(deepest, d);
|
|
for (const auto& c : *j) todo.push_back({ &c, d + 1 });
|
|
}
|
|
return deepest;
|
|
}
|
|
|
|
int ChatTemplate::apply(const ojson& messages, const ojson& tools, const ChatOptions& opt,
|
|
ChatPrompt* out) const {
|
|
if (!ready()) {
|
|
RAD_ERR("chat: apply() before load()");
|
|
return RAD_E_STATE;
|
|
}
|
|
if (!out) return RAD_E_INVAL;
|
|
|
|
/* Checked before either schema reaches the converter. A response format that does not parse
|
|
* is left for the template to refuse. */
|
|
{
|
|
const ojson schema = opt.json_schema.empty()
|
|
? ojson()
|
|
: ojson::parse(opt.json_schema, nullptr, /*allow_exceptions=*/false);
|
|
if (json_depth(tools) > kMaxSchemaDepth || json_depth(schema) > kMaxSchemaDepth) {
|
|
out->error = "a tool or response schema nests more than " +
|
|
std::to_string(kMaxSchemaDepth) + " levels deep";
|
|
RAD_WARN("chat: %s", out->error.c_str());
|
|
return RAD_E_INVAL;
|
|
}
|
|
}
|
|
|
|
common_chat_templates_inputs in;
|
|
try {
|
|
in.messages = common_chat_msgs_parse_oaicompat(messages);
|
|
if (tools.is_array() && !tools.empty())
|
|
in.tools = common_chat_tools_parse_oaicompat(tools);
|
|
in.tool_choice = common_chat_tool_choice_parse_oaicompat(opt.tool_choice);
|
|
} catch (const std::exception& e) {
|
|
/* A malformed request: return a 400, never throw through the request loop. */
|
|
RAD_WARN("chat: request did not parse: %s", e.what());
|
|
out->error = e.what();
|
|
return RAD_E_INVAL;
|
|
}
|
|
if (!in.tools.empty() && !impl_->caps.tools) {
|
|
RAD_WARN("chat: the model's template declares no tool support; request refused");
|
|
out->error = "this model's chat template declares no tool support";
|
|
return RAD_E_UNSUPPORTED;
|
|
}
|
|
|
|
in.grammar = opt.grammar;
|
|
in.json_schema = opt.json_schema;
|
|
in.add_generation_prompt = opt.add_generation_prompt;
|
|
in.use_jinja = true;
|
|
in.parallel_tool_calls = opt.parallel_tool_calls;
|
|
in.enable_thinking = opt.enable_thinking;
|
|
in.chat_template_kwargs = opt.template_kwargs;
|
|
in.add_bos = opt.add_bos || impl_->add_bos;
|
|
in.add_eos = opt.add_eos || impl_->add_eos;
|
|
in.reasoning_format = common_reasoning_format_from_name(opt.reasoning_format);
|
|
in.continue_final_message =
|
|
opt.continue_final.empty() ? COMMON_CHAT_CONTINUATION_NONE
|
|
: opt.continue_final == "content" ? COMMON_CHAT_CONTINUATION_CONTENT
|
|
: opt.continue_final == "reasoning_content" ? COMMON_CHAT_CONTINUATION_REASONING
|
|
: COMMON_CHAT_CONTINUATION_AUTO;
|
|
|
|
common_chat_params p;
|
|
try {
|
|
p = common_chat_templates_apply(impl_->tmpls.get(), in);
|
|
} catch (const std::exception& e) {
|
|
if (!opt.quiet) RAD_ERR("chat: template refused the request: %s", e.what());
|
|
out->error = e.what();
|
|
return RAD_E_UNSUPPORTED;
|
|
}
|
|
|
|
out->prompt = std::move(p.prompt);
|
|
out->generation_prompt = std::move(p.generation_prompt);
|
|
out->grammar = std::move(p.grammar);
|
|
out->grammar_lazy = p.grammar_lazy;
|
|
out->preserved_tokens = std::move(p.preserved_tokens);
|
|
out->additional_stops = std::move(p.additional_stops);
|
|
out->supports_thinking = p.supports_thinking;
|
|
out->thinking_start_tag = std::move(p.thinking_start_tag);
|
|
out->thinking_end_tag = std::move(p.thinking_end_tag);
|
|
out->format = (int)p.format;
|
|
out->parser = std::move(p.parser);
|
|
out->grammar_triggers.clear();
|
|
for (const common_grammar_trigger& t : p.grammar_triggers)
|
|
out->grammar_triggers.push_back({ (int)t.type, t.value });
|
|
|
|
RAD_DEBUG("chat: rendered %zu bytes, format %s, grammar %zu bytes%s, %zu extra stop(s)",
|
|
out->prompt.size(), common_chat_format_name(p.format),
|
|
out->grammar.size(), out->grammar_lazy ? " (lazy)" : "",
|
|
out->additional_stops.size());
|
|
if ((int)log_level() >= (int)Log::Trace) {
|
|
/* The rendered prompt as raw bytes, control tokens included, for checking the template's
|
|
* output format. Trace only, because it is user content. */
|
|
RAD_TRACE("chat: prompt <<<%s>>>", out->prompt.c_str());
|
|
if (!out->grammar.empty()) RAD_TRACE("chat: grammar <<<%s>>>", out->grammar.c_str());
|
|
for (const auto& s : out->additional_stops) RAD_TRACE("chat: extra stop <<<%s>>>", s.c_str());
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
} /* namespace rad */
|