Archived
radiance 1.7.0
- 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
This commit is contained in:
1 parent
b068190326
commit
1e968f6e1d
739 files changed
+128447
-84632
No files matched your search
+63
-63
@@ -1,55 +1,52 @@
|
||||
/* chatparse.h -- reading a model's reply back as it streams: reasoning, content, tool calls.
|
||||
/* chatparse.h -- reading a model's reply as it streams: reasoning, content, tool calls.
|
||||
*
|
||||
* ONE PASS, ONE PARSER. A reply is fed as it is generated, in pieces of any size, and every byte
|
||||
* is examined a bounded number of times: the whole reply costs O(its length), whether it arrives
|
||||
* in one piece or one byte at a time. Nothing is re-parsed, nothing is matched with a regular
|
||||
* expression, and there is no second pass that repairs what the first one missed -- the malformed
|
||||
* shapes a model actually writes are states of this machine, not fallbacks around it.
|
||||
* One pass, one parser. A reply is fed in pieces of any size as it is generated, and each byte is
|
||||
* examined a bounded number of times, so the whole reply costs O(length) however it is split.
|
||||
* Nothing is re-parsed, nothing is matched with a regular expression, and there is no repair
|
||||
* pass: the malformed shapes models actually write are states of this machine.
|
||||
*
|
||||
* It is driven by a ChatFormat (chatfmt.h) and built from three pieces:
|
||||
* Driven by a ChatFormat (chatfmt.h), it has three parts:
|
||||
*
|
||||
* * DELIMITER RECOGNITION is a deterministic Aho-Corasick automaton over the delimiters that can
|
||||
* end the current region -- "</think>" and the call opener inside reasoning, the call opener
|
||||
* in content, the value suffix inside a value. In the automaton's root state the scan skips
|
||||
* straight to the next byte that can begin a delimiter (memchr for the common one-byte case),
|
||||
* so a long run of prose or file content costs a memchr and a copy.
|
||||
* * THE TAG STRUCTURE is a transducer over those regions: call opener, name, arguments, close.
|
||||
* * A JSON-TYPED ARGUMENT is recognised by an incremental JSON scanner with an explicit stack.
|
||||
* * Delimiter recognition: a deterministic Aho-Corasick automaton over the delimiters that can
|
||||
* end the current region ("</think>" and the call opener in reasoning, the call opener in
|
||||
* content, the value suffix in a value). In the root state the scan skips to the next byte
|
||||
* that can begin a delimiter (memchr for the common one-byte case), so a long run of prose
|
||||
* costs a memchr and a copy.
|
||||
* * Tag structure: a transducer over those regions (call opener, name, arguments, close).
|
||||
* * JSON-typed arguments: an incremental JSON scanner with an explicit stack.
|
||||
*
|
||||
* WHAT IT PRODUCES is exactly what the format's grammar says the reply means, including the
|
||||
* places where the grammar is lenient on purpose: an argument the schema does not declare is
|
||||
* still an argument, a non-string argument that is not valid JSON is taken as text, a reasoning
|
||||
* block the model never closed ends where a call begins, prose before, between and after calls is
|
||||
* content. Where the reply leaves the grammar altogether -- a call to a function that is not a
|
||||
* tool, text inside a call, a missing close -- it recovers locally and says so (strict()).
|
||||
* The output is what the format's grammar says the reply means, including where the grammar is
|
||||
* deliberately lenient: an undeclared argument is still an argument, a non-string argument that
|
||||
* is not valid JSON is text, an unclosed reasoning block ends where a call begins, and prose
|
||||
* before, between and after calls is content. Where the reply leaves the grammar (a call to a
|
||||
* function that is not a tool, text inside a call, a missing close) it recovers locally and
|
||||
* clears strict().
|
||||
*
|
||||
* That includes a call written WITHOUT its wrapper. In a format whose calls are wrapped (Qwen's
|
||||
* `<tool_call>\n`) the grammar, and constrained decoding with it, begins only at the wrapper, so a
|
||||
* call the model writes as a bare `<function=NAME>`, or with the wrapper run straight into it, is
|
||||
* unconstrained -- and it is the one most likely to be lost. In content, name_prefix alone and
|
||||
* call_open without its trailing whitespace also open a call, with every tag's surrounding
|
||||
* whitespace optional inside it. Because such an opener is also ordinary text, it becomes a call
|
||||
* only once the name tag is closed and an argument or the call's close follows; until then it is
|
||||
* held (a bounded number of bytes), and if it is not a call it is content.
|
||||
* Unwrapped calls. In a wrapped format (Qwen's `<tool_call>\n`) the grammar, and so constrained
|
||||
* decoding, starts only at the wrapper, so a call written as a bare `<function=NAME>`, or with the
|
||||
* wrapper run into it, is unconstrained and the most likely to be lost. In content, name_prefix
|
||||
* alone and call_open without its trailing whitespace also open a call, with every tag's
|
||||
* surrounding whitespace optional inside it. Because such an opener is also ordinary text, it
|
||||
* becomes a call only once the name tag is closed and an argument or the call's close follows;
|
||||
* until then it is held (a bounded number of bytes), and if it is not a call it is content.
|
||||
*
|
||||
* THE STREAMING CONTRACT: what has been emitted is final. A delta is never taken back, so every
|
||||
* emitted prefix of content, reasoning or arguments is a prefix of the finished value, and the
|
||||
* concatenated deltas ARE the finished values. That forces some bytes to be held until their
|
||||
* meaning is known, and the holds are bounded:
|
||||
* Streaming contract: what has been emitted is final. A delta is never retracted, so the
|
||||
* concatenated deltas are the finished content, reasoning and arguments. Bytes whose meaning is
|
||||
* not yet known are held, within these bounds:
|
||||
*
|
||||
* * a possible delimiter prefix -- at most the longest delimiter, less one byte;
|
||||
* * an unfinished UTF-8 sequence -- at most three bytes, because a delta goes into a JSON string;
|
||||
* * whitespace whose fate depends on what follows -- the trailing whitespace of content, which
|
||||
* is trimmed if nothing follows it, and the whitespace that opens a reasoning block, which is
|
||||
* dropped if the block holds nothing else;
|
||||
* * a tag's own name while it is being read, since a name is only a name once it is closed;
|
||||
* * a JSON-typed argument value until its closing delimiter: `30` and `30 seconds` differ from
|
||||
* their first byte (a number, or the string "30 seconds"), so the value is emitted whole once
|
||||
* * a possible delimiter prefix: at most the longest delimiter less one byte;
|
||||
* * an unfinished UTF-8 sequence: at most three bytes, since a delta goes into a JSON string;
|
||||
* * whitespace that depends on what follows: content's trailing whitespace (trimmed if nothing
|
||||
* follows) and the whitespace opening a reasoning block (dropped if the block holds nothing
|
||||
* else);
|
||||
* * a tag's name while it is read, since it is a name only once closed;
|
||||
* * a JSON-typed argument value until its closing delimiter. `30` and `30 seconds` differ from
|
||||
* the first byte (a number, or the string "30 seconds"), so the value is emitted whole once
|
||||
* its type is known. A value that stops being JSON part-way is streamed as text from there,
|
||||
* except a container (`[`, `{`), which is held to its end: only there is it known whether
|
||||
* it is JSON once its Python quoting is normalised, or the JSON string of its text.
|
||||
* except a container (`[`, `{`), which is held to its end: only then is it known whether it
|
||||
* is JSON once its Python quoting is normalised, or becomes the JSON string of its text.
|
||||
*
|
||||
* Streaming emits in the reply's order; a call's arguments are streamed as they are parsed. */
|
||||
* Events are emitted in reply order; a call's arguments stream as they are parsed. */
|
||||
#pragma once
|
||||
#include "rad_internal.h"
|
||||
|
||||
@@ -83,9 +80,9 @@ struct ReplyCall {
|
||||
std::string name;
|
||||
std::string arguments; /* JSON text, as the OpenAI API carries it */
|
||||
bool closed = false;
|
||||
/* Where the call's text begins in the reply, and the content up to it: `content_at` bytes
|
||||
* of the message's content and the untrimmed whitespace held after them. What the content
|
||||
* is when the call is read as text (ReplyStream::content_with_open_call). */
|
||||
/* Where the call's text begins in the reply (`at`), and the content before it: `content_at`
|
||||
* bytes of the message's content plus the untrimmed whitespace held after them. Used to read
|
||||
* the call back as text (ReplyStream::content_with_open_call). */
|
||||
size_t at = 0;
|
||||
size_t content_at = 0;
|
||||
std::string content_held;
|
||||
@@ -119,16 +116,16 @@ struct ReplyOptions {
|
||||
/* A response_format was requested: the reply is one JSON document, optionally fenced as
|
||||
* ```json ... ```, and the document is the content. */
|
||||
bool response_format = false;
|
||||
/* The model's turn terminators. The decoder renders one only when the format needs it, and
|
||||
* one left on the end of content is not part of the answer. */
|
||||
/* The model's turn terminators. The decoder renders one only when the format needs it; one
|
||||
* left at the end of content is not part of the answer. */
|
||||
std::vector<std::string> terminators;
|
||||
};
|
||||
|
||||
class ReplyStream;
|
||||
|
||||
/* The reasoning block as one request's reply meets it: its markers with the optional whitespace
|
||||
* around them trimmed, and whether the reply starts inside it -- the generation prompt opened it,
|
||||
* or the format has no opener. Empty `close` is a format with no reasoning block. */
|
||||
/* The reasoning block as one request's reply sees it: the markers with their optional
|
||||
* surrounding whitespace trimmed, and whether the reply starts inside the block (the generation
|
||||
* prompt opened it, or the format has no opener). An empty `close` means no reasoning block. */
|
||||
struct ReasoningMarkers {
|
||||
std::string open, close;
|
||||
bool starts_open = false;
|
||||
@@ -155,19 +152,19 @@ private:
|
||||
class ReplyParser {
|
||||
public:
|
||||
~ReplyParser();
|
||||
/* RAD_OK, or RAD_E_INVAL with *why when a tool cannot be read unambiguously in this format --
|
||||
* a function or parameter name containing the delimiter that ends it -- and
|
||||
* RAD_E_UNSUPPORTED when the request sends tools and the format has no tool calls. */
|
||||
/* RAD_OK; RAD_E_INVAL with *why when a tool cannot be read unambiguously in this format (a
|
||||
* function or parameter name containing the delimiter that ends it); RAD_E_UNSUPPORTED when
|
||||
* the request sends tools and the format has no tool calls. */
|
||||
static int create(std::shared_ptr<const ReplyFormat> fmt, const ReplyOptions& opt,
|
||||
std::shared_ptr<const ReplyParser>* out, std::string* why);
|
||||
std::unique_ptr<ReplyStream> open() const;
|
||||
const ChatFormat& format() const;
|
||||
/* The most bytes ReplyHeld::delimiter can reach for this request: a delimiter or terminator
|
||||
* prefix in each place one can be pending at once, a value's undecided head, and an
|
||||
* unfinished UTF-8 sequence on each output. A constant of the format, not of the reply. */
|
||||
/* Upper bound on ReplyHeld::delimiter for this request: a delimiter or terminator prefix in
|
||||
* each place one can be pending at once, a value's undecided head, and an unfinished UTF-8
|
||||
* sequence per output. Depends on the format, not the reply. */
|
||||
size_t hold_bound() const;
|
||||
/* Read from the generation prompt exactly as a stream's first bytes are, so a reply this
|
||||
* says starts in the block is one the stream reads as reasoning from its first byte. */
|
||||
/* Read from the generation prompt the same way a stream reads its first bytes, so a reply
|
||||
* this says starts in the block is read as reasoning from its first byte. */
|
||||
ReasoningMarkers reasoning() const;
|
||||
|
||||
struct Impl;
|
||||
@@ -187,14 +184,17 @@ public:
|
||||
void finish(std::vector<ReplyEvent>* out);
|
||||
|
||||
const ReplyMessage& message() const;
|
||||
/* After finish(): the content were the last call text, when the reply ended inside that call
|
||||
* -- the content before it, then the call as `reply` (every byte fed) spells it, trimmed at
|
||||
* the end as content is. A call that never closed cannot be reported as a call, and its text
|
||||
* is what the model wrote. The message's content when the last call closed, or there is none. */
|
||||
/* After finish(): the content with the last call read as text. If the reply ended inside an
|
||||
* unclosed call, that is the content before the call followed by the call as `reply` (every
|
||||
* byte fed) spells it, end-trimmed like content, since an unclosed call cannot be reported as
|
||||
* a call. Otherwise (last call closed, or no calls) the message's content. */
|
||||
std::string content_with_open_call(std::string_view reply) const;
|
||||
/* False once the reply left the format's grammar and was read by a recovery rule. */
|
||||
bool strict() const;
|
||||
ReplyHeld held() const;
|
||||
/* Bytes queued so far to be read a second time. Each step of the parser consumes a byte or
|
||||
* queues bytes again, so it takes at most as many steps as the bytes fed plus these. */
|
||||
size_t reread() const;
|
||||
|
||||
struct Impl;
|
||||
private:
|
||||
|
||||
Reference in new issue
Block a user