radiance 1.2.4

- EmbeddingGemma 2 (google/embeddinggemma-2): text, image, video and audio embeddings on the
  card (libr4d) and on the CPU (libavx); prompt_name, Matryoshka dimensions, image budgets,
  mixed content parts and the messages form on /v1/embeddings
- #43: on serves that lend the activation arena to the experts (Qwen3.8-Flash-Next with
  expert_tiered), every image after the first read its rows from the wrong place -- garbage
  replies, or an earlier request's image. Fixed
- the Qwen vision towers' attention resolves on the card again (broken on main after 1.2.3,
  never released)
- #42: a warning at startup when amdgpu cwsr_enable is 0, and a stalled queue is reported as
  slow or stuck; the guide covers the GPU-hang-and-reset chain
This commit is contained in:
Deadcode committed 2026-10-07 22:42:45 +02:00
1 parent 05af4db458
commit 7d840861af
76 files changed
+8324 -467

No files matched your search

+7 -4
View File
@@ -698,14 +698,17 @@ public:
if (!p_ || !mm::decoder_available()) return false;
if (k == MediaKind::Image) return (mod_ & RAD_MM_IMAGE) != 0;
if (k == MediaKind::Video) return (mod_ & RAD_MM_VIDEO) != 0;
if (k == MediaKind::Audio) return (mod_ & RAD_MM_AUDIO) != 0 && p_->has_audio();
return false;
}
int prepare(MediaKind k, const std::vector<uint8_t>& bytes, std::shared_ptr<mm::Item>* out,
std::string* why) override {
int prepare(MediaKind k, const std::vector<uint8_t>& bytes, const MediaOpts& opts,
std::shared_ptr<mm::Item>* out, std::string* why) override {
if (!accepts(k)) { if (why) *why = "this kind of media is not served"; return RAD_E_UNSUPPORTED; }
return k == MediaKind::Image ? p_->image(bytes.data(), bytes.size(), out, why)
: p_->video(bytes.data(), bytes.size(), out, why);
if (k == MediaKind::Audio) return p_->audio(bytes.data(), bytes.size(), out, why);
return k == MediaKind::Image
? p_->image(bytes.data(), bytes.size(), opts.image_tokens, out, why)
: p_->video(bytes.data(), bytes.size(), out, why);
}
private:
+30 -4
View File
@@ -515,13 +515,32 @@ public:
/* ================================================================== optional backends */
/* /v1/embeddings. Absent, the endpoint is 501 naming that this model was not loaded with an
* embedding head -- not an empty 200. */
/* /v1/embeddings: WHAT THE LOADED MODEL EMBEDS, present only when it is an embedding model
* (rad_declare_pooled). Absent, the endpoint is 501 naming that -- not an empty 200.
*
* The vectors themselves come through the scheduler like any other request: the server submits
* one Request a input with Request::embed set, and each finishes at its one step with its pooled
* vector in its sink (Sink::embedding). So batching, admission, media encoding and cancellation
* are the scheduler's, exactly as they are for a completion. */
class IEmbedder {
public:
virtual ~IEmbedder() = default;
virtual int embed(const std::vector<int32_t>& tokens, std::vector<float>& out) = 0;
/* The width of a pooled vector. */
virtual int64_t dim() const = 0;
/* The longest input one step carries: an input is never split, so a longer one is refused. */
virtual int64_t max_input() const = 0;
/* THE MODEL'S NAMED PROMPTS (sentence-transformers' `prompts`): the text a `prompt_name` puts
* in front of an input -- "query" -> "task: search result | query: ". False when the model
* states no prompt of that name. */
virtual bool prompt(const std::string& name, std::string* text) const {
(void)name; (void)text;
return false;
}
/* The prompt an input takes when the request names none, "" for none. */
virtual std::string default_prompt() const { return std::string(); }
/* The template a `messages` input renders through -- the model's chat template -- or null
* when it has none. */
virtual IChatTemplate* message_template() const { return nullptr; }
};
/* Multimodal preprocessing (spec §11). The server pulls the bytes out of a content part; the
@@ -536,6 +555,13 @@ enum class MediaKind { Image, Video, Audio };
* the templates are the same templates. */
#define RAD_MEDIA_MARKER "<__media__>"
/* WHAT A REQUEST ASKED OF ITS MEDIA, beside the bytes. Zero is the server's default. */
struct MediaOpts {
/* Soft tokens (encoder output rows) an image may become, for a processor that sizes images
* by a token budget (Gemma 4's: 70, 140, 280, 560 or 1120). */
int32_t image_tokens = 0;
};
class IMultimodal {
public:
virtual ~IMultimodal() = default;
@@ -543,7 +569,7 @@ public:
/* Decode and process one part. `bytes` is the decoded file, not a URL: the server never
* fetches a remote URL on a request's behalf. On success `*out` carries the patches and the
* token run that replaces the marker; on failure `why` says what is wrong with the media. */
virtual int prepare(MediaKind k, const std::vector<uint8_t>& bytes,
virtual int prepare(MediaKind k, const std::vector<uint8_t>& bytes, const MediaOpts& opts,
std::shared_ptr<mm::Item>* out, std::string* why) = 0;
};
+346 -114
View File
@@ -6,6 +6,7 @@
#include <nlohmann/json.hpp>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <ctime>
@@ -59,29 +60,48 @@ std::string base64_encode(const void* data, size_t len) {
}
bool base64_decode(const std::string& in, std::vector<uint8_t>* out) {
auto val = [](char c) -> int {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1;
};
/* One table lookup a character: 0..63 a digit, -2 padding or whitespace (skipped wherever it
* stands), -1 anything else. A media part is megabytes of this, and a branch ladder a
* character decoded a 30 s clip at about 120 MB/s. */
static const auto kVal = [] {
std::array<int8_t, 256> t{};
t.fill(-1);
for (int i = 0; i < 26; ++i) { t[(size_t)('A' + i)] = (int8_t)i; t[(size_t)('a' + i)] = (int8_t)(26 + i); }
for (int i = 0; i < 10; ++i) t[(size_t)('0' + i)] = (int8_t)(52 + i);
t[(size_t)'+'] = 62;
t[(size_t)'/'] = 63;
for (char c : { '=', '\n', '\r', ' ', '\t' }) t[(size_t)(uint8_t)c] = -2;
return t;
}();
out->resize(in.size() * 3 / 4 + 3);
uint8_t* o = out->data();
const uint8_t* p = (const uint8_t*)in.data();
const uint8_t* const end = p + in.size();
uint32_t acc = 0;
int bits = 0;
out->clear();
out->reserve(in.size() * 3 / 4);
for (char c : in) {
if (c == '=' || c == '\n' || c == '\r' || c == ' ' || c == '\t') continue;
int v = val(c);
if (v < 0) return false;
while (p < end) {
/* Four digits at once while they are four digits: the body of any real payload. */
if (bits == 0 && end - p >= 4) {
const int a = kVal[p[0]], b = kVal[p[1]], c = kVal[p[2]], d = kVal[p[3]];
if ((a | b | c | d) >= 0) {
const uint32_t v = ((uint32_t)a << 18) | ((uint32_t)b << 12) | ((uint32_t)c << 6) | (uint32_t)d;
o[0] = (uint8_t)(v >> 16); o[1] = (uint8_t)(v >> 8); o[2] = (uint8_t)v;
o += 3;
p += 4;
continue;
}
}
const int v = kVal[*p++];
if (v == -2) continue;
if (v < 0) { out->clear(); return false; } /* nothing of a refused payload is left */
acc = (acc << 6) | (uint32_t)v;
bits += 6;
if (bits >= 8) {
bits -= 8;
out->push_back((uint8_t)((acc >> bits) & 0xFF));
*o++ = (uint8_t)((acc >> bits) & 0xFF);
}
}
out->resize((size_t)(o - out->data()));
return true;
}
@@ -209,7 +229,8 @@ const char* kAcceptedChat[] = {
const char* kAcceptedCompletion[] = { "prompt", "echo" };
const char* kAcceptedEmbedding[] = { "model", "input", "encoding_format", "dimensions" };
const char* kAcceptedEmbedding[] = { "model", "input", "messages", "encoding_format",
"dimensions", "image_tokens", "prompt_name" };
/* Inert by definition in the OpenAI schema: routing and bookkeeping labels the server is free to
* ignore. Named explicitly so that "we ignore it" is a decision in the source and not an
@@ -945,6 +966,103 @@ static int check_template_json(const json& msg, const std::string& role, ApiErro
/* Rewrite content parts in place: text stays, media becomes the marker and its bytes are handed
* to the encoder. `media` collects (kind, bytes) in the order they appear, which is the order the
* markers appear in the rendered prompt. */
/* ONE MEDIA CONTENT PART, read and decoded: 1 with `*kind` and `*bytes` set when `part` is an
* image, video or audio part, 0 when it is some other type (the caller's to judge), negative with
* `*err` set when it is a media part that cannot be served. `field` prefixes the error's field
* name -- "messages[].content[]" for chat, "input[]" for embeddings.
*
* IMAGE AND VIDEO PARTS, in the two spellings clients send: OpenAI's
* {"type":"image_url","image_url":{"url":...}} (and the bare-string form), the Responses API's
* "input_image", and the video equivalents vLLM's clients use -- {"type":"video_url",
* "video_url":{"url":...}} and "input_video". AUDIO is OpenAI's {"type":"input_audio",
* "input_audio":{"data": base64, "format": "wav"|"mp3"}} -- and the other formats the decoder
* admits by the same spelling -- or vLLM's {"type":"audio_url","audio_url":{"url": data URL}}. */
static int read_media_part(const json& part, const std::string& type, const std::string& field,
const OaiDeps& d, const OaiLimits& lim, MediaKind* kind,
std::vector<uint8_t>* bytes, ApiError* err) {
const bool is_image = type == "image_url" || type == "input_image";
const bool is_video = type == "video_url" || type == "input_video";
if (is_image || is_video) {
*kind = is_image ? MediaKind::Image : MediaKind::Video;
const char* what = is_image ? "image" : "video";
const bool allowed = is_image ? lim.allow_image : lim.allow_video;
if (!allowed || !d.mm || !d.mm->accepts(*kind)) {
*err = err_not_implemented(
std::string(what) + " input requires a vision encoder (spec 11); this "
"model was loaded without one");
return RAD_E_UNSUPPORTED;
}
const std::string key = is_image ? "image_url" : "video_url";
const std::string fld = field + "." + key;
std::string url;
auto iu = part.find(key);
if (iu == part.end() && type == "input_image") iu = part.find("image_url");
if (iu == part.end() && type == "input_video") iu = part.find("video_url");
if (iu != part.end() && iu->is_object() && iu->contains("url") &&
iu->at("url").is_string())
url = iu->at("url").get<std::string>();
else if (iu != part.end() && iu->is_string())
url = iu->get<std::string>();
else {
*err = err_bad(fld, "expected {\"url\": ...}");
return RAD_E_INVAL;
}
RAD_TRY(decode_media_url(url, fld + ".url", bytes, err));
return 1;
}
if (type == "input_audio" || type == "audio_url") {
*kind = MediaKind::Audio;
if (!lim.allow_audio || !d.mm || !d.mm->accepts(MediaKind::Audio)) {
*err = err_not_implemented(
"audio input requires an audio encoder (spec 11); this model was loaded "
"without one");
return RAD_E_UNSUPPORTED;
}
if (type == "audio_url") {
const std::string fld = field + ".audio_url";
auto au = part.find("audio_url");
std::string url;
if (au != part.end() && au->is_object() && au->contains("url") &&
au->at("url").is_string())
url = au->at("url").get<std::string>();
else if (au != part.end() && au->is_string())
url = au->get<std::string>();
else {
*err = err_bad(fld, "expected {\"url\": ...}");
return RAD_E_INVAL;
}
RAD_TRY(decode_media_url(url, fld + ".url", bytes, err));
return 1;
}
auto ia = part.find("input_audio");
if (ia == part.end() || !ia->is_object() || !ia->contains("data") ||
!ia->at("data").is_string()) {
*err = err_bad(field + ".input_audio", "expected {\"data\": ..., \"format\": ...}");
return RAD_E_INVAL;
}
auto fit = ia->find("format");
const std::string fmt =
fit != ia->end() && fit->is_string() ? fit->get<std::string>() : std::string();
/* The decoder recognises the container by its bytes; the format is checked against
* what it admits so a typo is named here rather than as an undecodable clip. */
static const char* const kAudioFormats[] = { "wav", "mp3", "flac", "ogg", "opus",
"m4a", "aac", "webm", "aiff" };
bool known = false;
for (const char* f : kAudioFormats) known = known || fmt == f;
if (!known) {
*err = err_bad(field + ".input_audio.format",
"must be one of wav, mp3, flac, ogg, opus, m4a, aac, webm, aiff");
return RAD_E_INVAL;
}
if (!base64_decode(ia->at("data").get<std::string>(), bytes)) {
*err = err_bad(field + ".input_audio.data", "invalid base64");
return RAD_E_INVAL;
}
return 1;
}
return 0;
}
static int lower_content_parts(json& messages, const OaiDeps& d, const OaiLimits& lim,
std::vector<std::pair<MediaKind, std::vector<uint8_t>>>* media,
ApiError* err) {
@@ -997,82 +1115,28 @@ static int lower_content_parts(json& messages, const OaiDeps& d, const OaiLimits
}
continue;
}
/* IMAGE AND VIDEO PARTS, in the two spellings clients send: OpenAI's
* {"type":"image_url","image_url":{"url":...}} (and the bare-string form), the
* Responses API's "input_image", and the video equivalents vLLM's clients use --
* {"type":"video_url","video_url":{"url":...}} and "input_video". */
const bool is_image = type == "image_url" || type == "input_image";
const bool is_video = type == "video_url" || type == "input_video";
if (is_image || is_video) {
const MediaKind kind = is_image ? MediaKind::Image : MediaKind::Video;
const char* what = is_image ? "image" : "video";
const bool allowed = is_image ? lim.allow_image : lim.allow_video;
if (!allowed || !d.mm || !d.mm->accepts(kind)) {
*err = err_not_implemented(
std::string(what) + " input requires a vision encoder (spec 11); this "
"model was loaded without one");
return RAD_E_UNSUPPORTED;
}
const std::string key = is_image ? "image_url" : "video_url";
const std::string field = "messages[].content[]." + key;
std::string url;
auto iu = part.find(key);
if (iu == part.end() && type == "input_image") iu = part.find("image_url");
if (iu == part.end() && type == "input_video") iu = part.find("video_url");
if (iu != part.end() && iu->is_object() && iu->contains("url") &&
iu->at("url").is_string())
url = iu->at("url").get<std::string>();
else if (iu != part.end() && iu->is_string())
url = iu->get<std::string>();
else {
*err = err_bad(field, "expected {\"url\": ...}");
return RAD_E_INVAL;
}
/* IMAGE, VIDEO AND AUDIO PARTS (read_media_part). A MEDIA MARKER, NOT TEXT, takes the
* part's place: the chat layer flattens a message's parts into one string and joins
* TEXT parts with a newline; it joins a marker to its neighbours with nothing, which
* is what the model's own template does with an image part (<|vision_end|> runs
* straight into the text after it). Joined as text, every picture would cost one
* '\n' token the model's own prompt does not have. */
{
MediaKind kind;
std::vector<uint8_t> bytes;
RAD_TRY(decode_media_url(url, field + ".url", &bytes, err));
media->emplace_back(kind, std::move(bytes));
/* A MEDIA MARKER, NOT TEXT. The chat layer flattens a message's parts into one
* string and joins TEXT parts with a newline; it joins a marker to its neighbours
* with nothing, which is what the model's own template does with an image part
* (<|vision_end|> runs straight into the text after it). Joined as text, every
* picture would cost one '\n' token the model's own prompt does not have. */
part.erase(key);
part["type"] = "media_marker";
part["text"] = MEDIA_MARKER;
continue;
}
if (type == "input_audio") {
if (!lim.allow_audio || !d.mm || !d.mm->accepts(MediaKind::Audio)) {
*err = err_not_implemented(
"audio input requires an audio encoder (spec 11); this model was loaded "
"without one");
return RAD_E_UNSUPPORTED;
const int mr = read_media_part(part, type, "messages[].content[]", d, lim, &kind,
&bytes, err);
if (mr < 0) return mr;
if (mr > 0) {
media->emplace_back(kind, std::move(bytes));
part.erase(type == "input_audio" ? "input_audio"
: type == "audio_url" ? "audio_url"
: (type == "image_url" || type == "input_image") ? "image_url"
: "video_url");
part["type"] = "media_marker";
part["text"] = MEDIA_MARKER;
continue;
}
auto ia = part.find("input_audio");
if (ia == part.end() || !ia->is_object() || !ia->contains("data") ||
!ia->at("data").is_string()) {
*err = err_bad("messages[].content[].input_audio",
"expected {\"data\": ..., \"format\": ...}");
return RAD_E_INVAL;
}
auto fit = ia->find("format");
const std::string fmt =
fit != ia->end() && fit->is_string() ? fit->get<std::string>() : std::string();
if (fmt != "wav" && fmt != "mp3") {
*err = err_bad("messages[].content[].input_audio.format",
"must be \"wav\" or \"mp3\"");
return RAD_E_INVAL;
}
std::vector<uint8_t> bytes;
if (!base64_decode(ia->at("data").get<std::string>(), &bytes)) {
*err = err_bad("messages[].content[].input_audio.data", "invalid base64");
return RAD_E_INVAL;
}
media->emplace_back(MediaKind::Audio, std::move(bytes));
part.erase("input_audio");
part["type"] = "media_marker";
part["text"] = MEDIA_MARKER;
continue;
}
*err = err_bad("messages[].content[].type",
"unsupported content part type \"" + type + "\"");
@@ -1093,7 +1157,7 @@ static int lower_content_parts(json& messages, const OaiDeps& d, const OaiLimits
* scheduler's thread, where every other request's step would wait on it. */
static int tokenize_with_media(const std::string& rendered, const OaiDeps& d,
std::vector<std::pair<MediaKind, std::vector<uint8_t>>>& media,
PromptInput* out, ApiError* err) {
const MediaOpts& opts, PromptInput* out, ApiError* err) {
out->text = rendered;
auto pm = std::make_shared<mm::PromptMedia>();
const std::string marker = MEDIA_MARKER;
@@ -1117,7 +1181,7 @@ static int tokenize_with_media(const std::string& rendered, const OaiDeps& d,
}
std::shared_ptr<mm::Item> item;
std::string why;
const int s = d.mm->prepare(media[mi].first, media[mi].second, &item, &why);
const int s = d.mm->prepare(media[mi].first, media[mi].second, opts, &item, &why);
if (s < 0 || !item) {
*err = err_bad("messages[].content[]", "the media could not be used: " +
(why.empty() ? std::string(rad_strerror(s))
@@ -1536,7 +1600,7 @@ int parse_chat_request(const std::string& body, const OaiDeps& d, const OaiLimit
p.tokens = d.tok->encode(rendered, false, /*parse_special=*/true);
p.text = rendered;
} else {
RAD_TRY(tokenize_with_media(rendered, d, media, &p, err));
RAD_TRY(tokenize_with_media(rendered, d, media, MediaOpts{}, &p, err));
}
if (p.tokens.empty()) {
*err = err_bad("messages", "the rendered prompt tokenised to nothing");
@@ -1702,16 +1766,109 @@ int parse_embeddings_request(const std::string& body, const OaiDeps& d, const Oa
out->base64_embeddings = (enc == "base64");
if (b.contains("dimensions") && !b.at("dimensions").is_null()) {
*err = err_unsupported("dimensions",
"Matryoshka truncation is not implemented; the model's embedding "
"dimension is fixed");
return RAD_E_UNSUPPORTED;
const json& dv = b.at("dimensions");
if (!dv.is_number_integer() || dv.get<int64_t>() <= 0) {
*err = err_bad("dimensions", "must be a positive integer");
return RAD_E_INVAL;
}
/* Bounded by the model's width in the handler, which knows it. */
out->dimensions = dv.get<int64_t>();
}
/* A soft-token budget for this request's images (Gemma 4's processor); the processor refuses
* it on a model that sizes images otherwise. */
if (b.contains("image_tokens") && !b.at("image_tokens").is_null()) {
const json& iv = b.at("image_tokens");
const int64_t n = iv.is_number_integer() ? iv.get<int64_t>() : 0;
if (n != 70 && n != 140 && n != 280 && n != 560 && n != 1120) {
*err = err_bad("image_tokens", "must be one of 70, 140, 280, 560, 1120");
return RAD_E_INVAL;
}
out->image_tokens = (int32_t)n;
}
/* THE NAMED PROMPT: sentence-transformers' `prompt_name`, the model's own text in front of
* each text or content-part input. A model that states a default prompt applies it to a
* request that names none, as its reference library does. */
std::string prefix;
const bool prompt_named = b.contains("prompt_name") && !b.at("prompt_name").is_null();
{
std::string pname;
const bool named = prompt_named;
if (named) {
if (!b.at("prompt_name").is_string()) {
*err = err_bad("prompt_name", "expected a string");
return RAD_E_INVAL;
}
pname = b.at("prompt_name").get<std::string>();
} else if (d.embed) {
pname = d.embed->default_prompt();
}
if (!pname.empty() && (!d.embed || !d.embed->prompt(pname, &prefix))) {
*err = err_bad("prompt_name", "this model states no prompt named \"" + pname + "\"");
return RAD_E_INVAL;
}
}
/* THE MESSAGES FORM, vLLM's chat embedding: one input rendered through the model's own chat
* template (EmbeddingGemma 2's concatenates the system text and then every message's text and
* media placeholders), the tokenizer's special tokens around the whole as for an input. */
if (b.contains("messages") && !b.at("messages").is_null()) {
if (b.contains("input") && !b.at("input").is_null()) {
*err = err_bad("messages", "a request takes `input` or `messages`, not both");
return RAD_E_INVAL;
}
if (b.contains("prompt_name") && !b.at("prompt_name").is_null()) {
*err = err_bad("prompt_name", "a `messages` input is rendered by the chat template "
"and takes no prompt");
return RAD_E_INVAL;
}
IChatTemplate* tpl = d.embed ? d.embed->message_template() : nullptr;
if (!tpl) {
*err = err_bad("messages", "this model has no chat template to render messages with");
return RAD_E_INVAL;
}
json messages = b.at("messages");
std::vector<std::pair<MediaKind, std::vector<uint8_t>>> media;
RAD_TRY(lower_content_parts(messages, d, lim, &media, err));
ChatRenderOptions opt;
opt.add_generation_prompt = false;
ChatRender render;
std::string why;
if (tpl->apply(messages, json::array(), opt, &render, &why) < 0 || render.prompt.empty()) {
*err = err_bad("messages", "the chat template did not render the messages" +
(why.empty() ? std::string() : ": " + why));
return RAD_E_INVAL;
}
PromptInput p;
if (media.empty()) {
p.text = render.prompt;
p.tokens = d.tok->encode(render.prompt, true, /*parse_special=*/true);
} else {
MediaOpts mo;
mo.image_tokens = out->image_tokens;
RAD_TRY(tokenize_with_media(render.prompt, d, media, mo, &p, err));
const ITokenizer::VocabInfo vi = d.tok->vocab_info();
if (vi.add_bos && vi.bos >= 0) {
p.tokens.insert(p.tokens.begin(), vi.bos);
for (auto& pl : p.media->items) pl.tok += 1;
p.media->layout();
}
if (vi.add_eos && vi.eos >= 0) p.tokens.push_back(vi.eos);
}
out->prompts.push_back(std::move(p));
return RAD_OK;
}
auto it = b.find("input");
if (it == b.end() || it->is_null()) { *err = err_bad("input", "is required"); return RAD_E_INVAL; }
auto push_ids = [&](const json& arr, ApiError* e) -> bool {
/* A prompt is text; token ids take none, and asking for one by name is refused rather
* than dropped. A model's default prompt applies to text inputs only. */
if (prompt_named && !prefix.empty()) {
*e = err_bad("prompt_name", "a prompt is text and cannot go in front of token ids");
return false;
}
PromptInput p;
for (const auto& v : arr) {
if (!v.is_number_integer()) {
@@ -1729,33 +1886,108 @@ int parse_embeddings_request(const std::string& body, const OaiDeps& d, const Oa
return true;
};
/* ONE INPUT OF CONTENT PARTS -- text, images, video, audio, in order -- the same parts a chat
* message carries, and one vector for the lot. The parts are joined with nothing between
* them and each media part becomes its processor's token run, which is what an embedding
* model's own template does (EmbeddingGemma 2's concatenates text and placeholders), and the
* tokenizer's own special tokens go around the whole: what HuggingFace's processor feeds the
* model. */
auto push_parts = [&](const json& parts, ApiError* e) -> int {
std::vector<std::pair<MediaKind, std::vector<uint8_t>>> media;
std::string rendered = prefix;
for (const auto& part : parts) {
if (!part.is_object()) {
*e = err_bad("input[]", "a content part must be an object");
return RAD_E_INVAL;
}
auto tit = part.find("type");
if (tit == part.end() || !tit->is_string()) {
*e = err_bad("input[].type", "expected a string");
return RAD_E_INVAL;
}
const std::string type = tit->get<std::string>();
if (type == "text") {
if (!part.contains("text") || !part.at("text").is_string()) {
*e = err_bad("input[].text", "expected a string");
return RAD_E_INVAL;
}
const std::string t = part.at("text").get<std::string>();
/* The marker is how a media part's position survives into tokenisation, so text
* that spells it would be taken for one. */
if (t.find(MEDIA_MARKER) != std::string::npos) {
*e = err_bad("input[].text", std::string("contains \"") + MEDIA_MARKER +
"\", which this server reserves for media");
return RAD_E_INVAL;
}
rendered += t;
continue;
}
MediaKind kind;
std::vector<uint8_t> bytes;
const int mr = read_media_part(part, type, "input[]", d, lim, &kind, &bytes, e);
if (mr < 0) return mr;
if (mr == 0) {
*e = err_bad("input[].type", "unsupported content part type \"" + type + "\"");
return RAD_E_INVAL;
}
media.emplace_back(kind, std::move(bytes));
rendered += MEDIA_MARKER;
}
PromptInput p;
if (media.empty()) {
p.text = rendered;
p.tokens = d.tok->encode(rendered, true, /*parse_special=*/true);
} else {
MediaOpts mo;
mo.image_tokens = out->image_tokens;
RAD_TRY(tokenize_with_media(rendered, d, media, mo, &p, e));
/* tokenize_with_media adds no special tokens (a chat template renders its own);
* this input has no template, so the tokenizer's own rule applies around it. */
const ITokenizer::VocabInfo vi = d.tok->vocab_info();
if (vi.add_bos && vi.bos >= 0) {
p.tokens.insert(p.tokens.begin(), vi.bos);
for (auto& pl : p.media->items) pl.tok += 1;
p.media->layout();
}
if (vi.add_eos && vi.eos >= 0) p.tokens.push_back(vi.eos);
}
out->prompts.push_back(std::move(p));
return RAD_OK;
};
auto is_parts = [](const json& a) { return a.is_array() && !a.empty() && a.front().is_object(); };
/* Parsed like a /v1/completions prompt, and for the same reason: the input is the client's
* own text with no template around it. */
if (it->is_string()) {
PromptInput p;
p.text = it->get<std::string>();
p.text = prefix + it->get<std::string>();
p.tokens = d.tok->encode(p.text, true, /*parse_special=*/true);
out->prompts.push_back(std::move(p));
} else if (it->is_array()) {
if (it->empty()) { *err = err_bad("input", "must not be empty"); return RAD_E_INVAL; }
if (it->front().is_string()) {
for (const auto& s : *it) {
if (!s.is_string()) { *err = err_bad("input", "expected all strings"); return RAD_E_INVAL; }
PromptInput p;
p.text = s.get<std::string>();
p.tokens = d.tok->encode(p.text, true, /*parse_special=*/true);
out->prompts.push_back(std::move(p));
}
} else if (it->front().is_number_integer()) {
if (it->front().is_number_integer()) {
if (!push_ids(*it, err)) return RAD_E_INVAL;
} else if (it->front().is_array()) {
for (const auto& a : *it) {
if (!a.is_array()) { *err = err_bad("input", "expected arrays"); return RAD_E_INVAL; }
if (!push_ids(a, err)) return RAD_E_INVAL;
}
} else if (is_parts(*it)) {
RAD_TRY(push_parts(*it, err));
} else {
*err = err_bad("input", "expected a string, an array of strings, or token ids");
return RAD_E_INVAL;
/* A BATCH: each element its own input -- a string, a token array or an array of
* content parts. */
for (const auto& el : *it) {
if (el.is_string()) {
PromptInput p;
p.text = prefix + el.get<std::string>();
p.tokens = d.tok->encode(p.text, true, /*parse_special=*/true);
out->prompts.push_back(std::move(p));
} else if (is_parts(el)) {
RAD_TRY(push_parts(el, err));
} else if (el.is_array()) {
if (!push_ids(el, err)) return RAD_E_INVAL;
} else {
*err = err_bad("input", "expected strings, token arrays or arrays of content "
"parts");
return RAD_E_INVAL;
}
}
}
} else {
*err = err_bad("input", "expected a string or an array");
+6
View File
@@ -114,6 +114,11 @@ struct OaiRequest {
/* /v1/embeddings */
bool base64_embeddings = false;
/* OpenAI's `dimensions`: the vector's first `dimensions` components, renormalised -- a
* Matryoshka-trained model's shorter embedding. 0 is the model's own width. */
int64_t dimensions = 0;
/* /v1/embeddings: a soft-token budget for the request's images, 0 the server's. */
int32_t image_tokens = 0;
int n_choices() const { return (int)prompts.size() * n; }
};
@@ -239,6 +244,7 @@ struct OaiDeps {
IChatTemplate* chat = nullptr;
IGrammarCompiler* grammar = nullptr;
IMultimodal* mm = nullptr;
IEmbedder* embed = nullptr;
};
/* Parse. `body` is the raw request body; a malformed body is a 400 that says where. */
+127 -14
View File
@@ -7,6 +7,7 @@
#include <nlohmann/json.hpp>
#include <algorithm>
#include <cmath>
#include <chrono>
#include <thread>
@@ -76,6 +77,7 @@ Server::Server(const Deps& d, const ServerOptions& o)
odeps_.chat = deps_.chat;
odeps_.grammar = deps_.grammar;
odeps_.mm = deps_.mm;
odeps_.embed = deps_.embed;
queue_depth_ = opt_.queue_depth > 0 ? opt_.queue_depth : opt_.max_seqs * 8;
started_at_ = unix_now();
@@ -956,6 +958,10 @@ HttpResponse Server::streaming_completion(const HttpRequest& req, OaiRequest& r)
HttpResponse Server::handle_chat(const HttpRequest& req) {
if (!deps_.sched || !deps_.tok)
return error_response(err_not_implemented("the server has no engine attached"), req.peer);
if (opt_.embedding_model)
return error_response(err_not_implemented("this is an embedding model: it serves "
"/v1/embeddings and generates no text"),
req.peer);
OaiRequest r;
ApiError err;
@@ -977,6 +983,10 @@ HttpResponse Server::handle_chat(const HttpRequest& req) {
HttpResponse Server::handle_completion(const HttpRequest& req) {
if (!deps_.sched || !deps_.tok)
return error_response(err_not_implemented("the server has no engine attached"), req.peer);
if (opt_.embedding_model)
return error_response(err_not_implemented("this is an embedding model: it serves "
"/v1/embeddings and generates no text"),
req.peer);
OaiRequest r;
ApiError err;
@@ -996,35 +1006,138 @@ HttpResponse Server::handle_completion(const HttpRequest& req) {
}
HttpResponse Server::handle_embeddings(const HttpRequest& req) {
if (!deps_.tok)
return error_response(err_not_implemented("the server has no tokenizer attached"),
if (!deps_.sched || !deps_.tok)
return error_response(err_not_implemented("the server has no engine attached"),
req.peer);
if (!deps_.embed)
return error_response(err_not_implemented(
"/v1/embeddings requires an embedding head; this model was not loaded with one"),
req.peer);
"/v1/embeddings requires an embedding model; this one generates text"), req.peer);
OaiRequest r;
ApiError err;
if (parse_embeddings_request(req.body, odeps_, lim_, &r, &err) < 0)
return error_response(err, req.peer);
r.peer = req.peer;
std::vector<std::vector<float>> vecs;
UsageOut u;
vecs.reserve(r.prompts.size());
for (const auto& p : r.prompts) {
std::vector<float> v;
int s = deps_.embed->embed(p.tokens, v);
if (s < 0) {
const int64_t dim = deps_.embed->dim();
if (r.dimensions > dim)
return error_response(err_bad("dimensions", "this model's vectors are " +
std::to_string(dim) + " wide"), req.peer);
/* AN INPUT IS NEVER SPLIT (Request::embed), so one longer than a step is refused here, with
* the number, rather than by the scheduler with none. */
for (size_t i = 0; i < r.prompts.size(); ++i) {
const int64_t n = (int64_t)r.prompts[i].tokens.size();
if (n > deps_.embed->max_input()) {
ApiError e = err_bad("input", "input " + std::to_string(i) + " is " +
std::to_string(n) + " tokens; this deployment "
"embeds at most " +
std::to_string(deps_.embed->max_input()) +
" (--max-num-batched-tokens)");
e.code = "context_length_exceeded";
return error_response(e, req.peer);
}
}
sweep_abandoned();
const int n_in = (int)r.prompts.size();
if (!admit(n_in)) {
metrics_.observe_rejected();
return error_response(err_overloaded(
"the admission queue is full; retry after " + std::to_string(opt_.retry_after_s) +
"s. The server refuses rather than queueing without bound"), req.peer);
}
log_admitted(r);
/* EVERY INPUT AT ONCE, so the scheduler can put them in one step. */
std::vector<Generation> gens;
std::vector<Request> rqs;
gens.reserve((size_t)n_in);
rqs.reserve((size_t)n_in);
for (int i = 0; i < n_in; ++i) {
Generation g;
g.index = i;
g.prompt_index = i;
g.sink = std::make_shared<Sink>(16);
Request rq;
rq.prompt = r.prompts[(size_t)i].tokens;
rq.media = r.prompts[(size_t)i].media;
rq.embed = true;
rq.max_tokens = 1;
rq.sp.temp = 0.0f;
rq.sink = g.sink.get();
rqs.push_back(std::move(rq));
gens.push_back(std::move(g));
}
std::vector<uint64_t> ids;
deps_.sched->submit_all(rqs, &ids);
for (size_t i = 0; i < ids.size(); ++i) {
gens[i].id = ids[i];
gens[i].sink->id = ids[i];
if (!ids[i]) {
retire(gens);
release(n_in);
ApiError e;
e.status = 500;
e.type = "internal_server_error";
e.message = std::string("the embedding pass failed: ") + rad_strerror(s);
e.message = "the scheduler refused input " + std::to_string(i);
return error_response(e, req.peer);
}
u.prompt += (int64_t)p.tokens.size();
vecs.push_back(std::move(v));
}
for (;;) {
bool all = true;
for (auto& g : gens) {
if (!g.closed && g.sink->done()) { g.closed = true; g.finish = g.sink->finish_reason(); }
all = all && g.closed;
}
if (all) break;
if (req.peer_gone()) {
cancel_all(gens, true);
retire(gens);
release(n_in);
metrics_.observe_http(499);
return HttpResponse::text(499, "client closed request\n");
}
if (stopping_.load()) {
cancel_all(gens, false);
retire(gens);
release(n_in);
return error_response(err_overloaded("the server is shutting down"), req.peer);
}
for (auto& g : gens) if (!g.closed) { g.sink->wait(); break; }
}
/* THE VECTORS, truncated to `dimensions` and L2-normalised: what OpenAI's endpoint returns and
* what a sentence-transformers model's Normalize module does. Truncating a unit vector does
* not keep it unit, so the normalisation comes after. */
std::vector<std::vector<float>> vecs;
vecs.reserve((size_t)n_in);
UsageOut u;
const int64_t keep = r.dimensions > 0 ? r.dimensions : dim;
for (auto& g : gens) {
const std::vector<float>& v = g.sink->embedding();
if (g.finish == Finish::Error || g.finish == Finish::Cancelled || (int64_t)v.size() != dim) {
const std::string why = g.finish == Finish::Error ? engine_failure(*g.sink)
: std::string("the engine returned no vector for input ") +
std::to_string(g.index);
retire(gens);
release(n_in);
ApiError e;
e.status = 500;
e.type = "internal_server_error";
e.message = why;
return error_response(e, req.peer);
}
std::vector<float> out(v.begin(), v.begin() + keep);
double ss = 0.0;
for (float x : out) ss += (double)x * x;
const float inv = ss > 0.0 ? (float)(1.0 / std::sqrt(ss)) : 0.0f;
for (float& x : out) x *= inv;
u.prompt += (int64_t)r.prompts[(size_t)g.prompt_index].tokens.size();
vecs.push_back(std::move(out));
}
retire(gens);
release(n_in);
metrics_.observe_http(200);
return HttpResponse::json_body(200, embeddings_body(r, vecs, u));
}
+4
View File
@@ -53,6 +53,10 @@ struct Deps {
struct ServerOptions {
std::string model_id = "radiance";
/* AN EMBEDDING MODEL (rad_declare_pooled) GENERATES NOTHING: /v1/chat/completions and
* /v1/completions answer 501 naming /v1/embeddings, rather than queueing a request the engine
* would finish after one step with no text. */
bool embedding_model = false;
std::string host = "0.0.0.0";
int port = 8000;
int n_threads = 0;
+11
View File
@@ -142,6 +142,13 @@ public:
/* The engine's request id, so a dropped connection can reach IScheduler::cancel. */
uint64_t id = 0;
/* AN EMBEDDING REQUEST'S ANSWER: the pooled vector, written once by the engine before the
* step's commit finishes the request, and read by the consumer only after done(). finish()
* publishes with a release store and done() reads with an acquire load, so the vector is
* visible to anyone who has seen the request finish. Empty for every generating request. */
void set_embedding(const float* v, int64_t n) { embd_.assign(v, v + n); }
const std::vector<float>& embedding() const { return embd_; }
private:
void notify();
@@ -168,6 +175,7 @@ private:
std::atomic<int32_t> n_prompt_{0};
std::atomic<int32_t> n_cached_{0};
bool has_logprobs_ = false;
std::vector<float> embd_;
std::mutex m_;
std::condition_variable cv_;
@@ -196,6 +204,9 @@ inline void sink_admitted(void* s) {
inline void sink_drafts(void* s, int32_t n, int32_t accepted) {
if (s) static_cast<Sink*>(s)->add_drafts(n, accepted);
}
inline void sink_embedding(void* s, const float* v, int64_t n) {
if (s) static_cast<Sink*>(s)->set_embedding(v, n);
}
} /* namespace server */
} /* namespace rad */