Archived
clef: the joint schema head on the card, served on /v1/systemone
This commit is contained in:
1 parent
555fbf62f5
commit
a96bfe821f
17 files changed
+1786
-27
No files matched your search
@@ -7,6 +7,8 @@
|
||||
*/
|
||||
#include "clef.h"
|
||||
|
||||
#include "rad_clef.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -202,6 +204,108 @@ int parse_clef_request(const std::string& body, const OaiDeps& d, const OaiLimit
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int64_t clef_option_count(const ClefRequest& r) {
|
||||
int64_t o = 0;
|
||||
for (const ClefQuestion& q : r.questions) o += (int64_t)q.options.size();
|
||||
return o;
|
||||
}
|
||||
|
||||
int clef_words(const ClefRequest& r, std::vector<int32_t>* out, ApiError* err) {
|
||||
const int64_t N = (int64_t)r.tokens.size(), Q = (int64_t)r.questions.size();
|
||||
const int64_t O = clef_option_count(r);
|
||||
int64_t L = 0;
|
||||
for (const ClefQuestion& q : r.questions)
|
||||
for (const TokenSpan& s : q.option_spans) L += s.second - s.first;
|
||||
if (Q > RAD_CLEF_MAX_QUESTIONS) {
|
||||
*err = err_bad("questions", "at most " + std::to_string(RAD_CLEF_MAX_QUESTIONS) +
|
||||
" questions a request");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (O > RAD_CLEF_MAX_OPTIONS) {
|
||||
*err = err_bad("questions", "at most " + std::to_string(RAD_CLEF_MAX_OPTIONS) +
|
||||
" options a request, every question together");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
if (L > RAD_CLEF_MAX_LEX) {
|
||||
*err = err_bad("questions", "the options render to " + std::to_string(L) +
|
||||
" tokens; at most " + std::to_string(RAD_CLEF_MAX_LEX) +
|
||||
" a request");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
/* The prompt cut at every span's edges. Spans are disjoint, so each is one whole segment. */
|
||||
std::vector<int32_t> cuts = { 0, (int32_t)N };
|
||||
for (const ClefQuestion& q : r.questions) {
|
||||
cuts.push_back((int32_t)q.question.first);
|
||||
cuts.push_back((int32_t)q.question.second);
|
||||
for (const TokenSpan& s : q.option_spans) {
|
||||
cuts.push_back((int32_t)s.first);
|
||||
cuts.push_back((int32_t)s.second);
|
||||
}
|
||||
}
|
||||
std::sort(cuts.begin(), cuts.end());
|
||||
cuts.erase(std::unique(cuts.begin(), cuts.end()), cuts.end());
|
||||
const int64_t S = (int64_t)cuts.size() - 1;
|
||||
auto seg_at = [&](int64_t start) {
|
||||
return (int32_t)(std::lower_bound(cuts.begin(), cuts.end(), (int32_t)start) - cuts.begin());
|
||||
};
|
||||
|
||||
const RadClefWords w = rad_clef_words_layout((int32_t)N, (int32_t)Q, (int32_t)O, (int32_t)L,
|
||||
(int32_t)S);
|
||||
std::vector<int32_t>& v = *out;
|
||||
v.assign((size_t)w.total, 0);
|
||||
v[RAD_CLEF_W_N] = (int32_t)N;
|
||||
v[RAD_CLEF_W_Q] = (int32_t)Q;
|
||||
v[RAD_CLEF_W_O] = (int32_t)O;
|
||||
v[RAD_CLEF_W_L] = (int32_t)L;
|
||||
v[RAD_CLEF_W_S] = (int32_t)S;
|
||||
std::copy(cuts.begin(), cuts.end(), v.begin() + w.seg_cu);
|
||||
int64_t o = 0, l = 0;
|
||||
for (int64_t qi = 0; qi < Q; ++qi) {
|
||||
const ClefQuestion& q = r.questions[(size_t)qi];
|
||||
v[(size_t)(w.q_seg + qi)] = seg_at(q.question.first);
|
||||
v[(size_t)(w.type + qi)] = q.type_id;
|
||||
v[(size_t)(w.opt_cu + qi)] = (int32_t)o;
|
||||
for (const TokenSpan& s : q.option_spans) {
|
||||
v[(size_t)(w.o_seg + o)] = seg_at(s.first);
|
||||
v[(size_t)(w.o_q + o)] = (int32_t)qi;
|
||||
v[(size_t)(w.lex_cu + o)] = (int32_t)l;
|
||||
for (int64_t t = s.first; t < s.second; ++t) v[(size_t)(w.lex_ids + l++)] = r.tokens[(size_t)t];
|
||||
++o;
|
||||
}
|
||||
}
|
||||
v[(size_t)(w.opt_cu + Q)] = (int32_t)O;
|
||||
v[(size_t)(w.lex_cu + O)] = (int32_t)L;
|
||||
v[(size_t)w.last] = (int32_t)(N - 1);
|
||||
v[(size_t)w.cu_q] = 0; v[(size_t)w.cu_q + 1] = (int32_t)Q;
|
||||
v[(size_t)w.cu_o] = 0; v[(size_t)w.cu_o + 1] = (int32_t)O;
|
||||
v[(size_t)w.cu_k] = 0; v[(size_t)w.cu_k + 1] = (int32_t)N;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
std::vector<int32_t> clef_score_ids(int64_t n_options) {
|
||||
std::vector<int32_t> ids;
|
||||
ids.reserve((size_t)(3 * n_options));
|
||||
for (int k = 0; k < 3; ++k)
|
||||
for (int64_t o = 0; o < n_options; ++o) ids.push_back((int32_t)(k * RAD_CLEF_MAX_OPTIONS + o));
|
||||
return ids;
|
||||
}
|
||||
|
||||
std::vector<float> clef_logits(const std::vector<float>& scores, int64_t n_options,
|
||||
const ClefScalars& s) {
|
||||
const double cap = std::log(100.0);
|
||||
const double ps = std::exp(std::min(s.prior_logit_scale, cap));
|
||||
const double js = std::exp(std::min(s.joint_logit_scale, cap));
|
||||
const double gate = 1.0 / (1.0 + std::exp(-s.residual_gate));
|
||||
std::vector<float> out((size_t)n_options, 0.0f);
|
||||
if ((int64_t)scores.size() < 3 * n_options) return out;
|
||||
for (int64_t o = 0; o < n_options; ++o) {
|
||||
const double prior = scores[(size_t)o], cosine = scores[(size_t)(n_options + o)];
|
||||
const double residual = scores[(size_t)(2 * n_options + o)];
|
||||
out[(size_t)o] = (float)(ps * prior + gate * (js * cosine + residual));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int clef_answer(const ClefQuestion& q, const std::vector<float>& logits, json* out, ApiError* err) {
|
||||
if (logits.size() != q.options.size()) {
|
||||
*err = err_internal("the head returned no logits for question " + q.name);
|
||||
|
||||
@@ -59,5 +59,29 @@ int clef_answer(const ClefQuestion& q, const std::vector<float>& logits, json* o
|
||||
* sorted and non-ASCII kept. Exposed for the tests. */
|
||||
std::string clef_render(const json& v);
|
||||
|
||||
/* The request's words for the head on the card (abi/rad_clef.h, Request::plugin_words): the
|
||||
* prompt cut into segments at every span's edges, which segment each question and option is,
|
||||
* each option's question and tokens, each question's type. 400 past the head's bounds. */
|
||||
int clef_words(const ClefRequest& r, std::vector<int32_t>* out, ApiError* err);
|
||||
|
||||
/* The options a request has in all, in head order: the head returns three values for each. */
|
||||
int64_t clef_option_count(const ClefRequest& r);
|
||||
|
||||
/* The logits-row positions the head's three values an option land at (prior, cosine, residual;
|
||||
* each stretch RAD_CLEF_MAX_OPTIONS wide), as TokenScores asks for them. */
|
||||
std::vector<int32_t> clef_score_ids(int64_t n_options);
|
||||
|
||||
/* The head's learned scalars, from the container header (rad-convert writes them from
|
||||
* joint_head.safetensors; the engine copies them to ServerOptions). */
|
||||
struct ClefScalars {
|
||||
double prior_logit_scale = 0.0, joint_logit_scale = 0.0, residual_gate = 0.0;
|
||||
};
|
||||
|
||||
/* One logit an option from the head's three values (`scores` in clef_score_ids' order):
|
||||
* exp(prior_logit_scale) * prior + sigmoid(residual_gate) * (exp(joint_logit_scale) * cosine +
|
||||
* residual), the scales clamped at 100 as the release clamps them. */
|
||||
std::vector<float> clef_logits(const std::vector<float>& scores, int64_t n_options,
|
||||
const ClefScalars& s);
|
||||
|
||||
} /* namespace server */
|
||||
} /* namespace rad */
|
||||
+91
-1
@@ -2,6 +2,7 @@
|
||||
#include "server_priv.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include "clef.h"
|
||||
#include "json_partial.h"
|
||||
#include "systemone.h"
|
||||
#include "rad_internal.h"
|
||||
@@ -1744,10 +1745,12 @@ HttpResponse Server::handle_embeddings(const HttpRequest& req) {
|
||||
HttpResponse Server::handle_systemone(const HttpRequest& req) {
|
||||
if (!deps_.sched || !deps_.tok)
|
||||
return error_response(err_not_implemented("the server has no engine attached"), req.peer);
|
||||
if (opt_.decision_type == "clef") return handle_clef(req);
|
||||
if (opt_.decision_type != "d1")
|
||||
return error_response(err_not_implemented(
|
||||
"/v1/systemone serves a decision model; this container does not declare one "
|
||||
"(convert d1 with --set decision.type=d1)"), req.peer);
|
||||
"(convert d1 with --set decision.type=d1, clef with --set decision.type=clef)"),
|
||||
req.peer);
|
||||
|
||||
DecisionRequest r;
|
||||
ApiError err;
|
||||
@@ -1862,6 +1865,93 @@ HttpResponse Server::handle_systemone(const HttpRequest& req) {
|
||||
{ "usage", { { "input_tokens", r.input_tokens }, { "output_tokens", 0 } } } }));
|
||||
}
|
||||
|
||||
/* clef (clef.h): one request is the whole prompt, every question decided jointly by the head on
|
||||
* the card (arch/common/rad_block_clef.h). The request's spans and option tokens go down as its
|
||||
* words, and the head's three values an option come back through Request::token_scores, off the
|
||||
* logits row; the learned scalars combine them here and each question's softmax is its answer. */
|
||||
HttpResponse Server::handle_clef(const HttpRequest& req) {
|
||||
if (!opt_.clef_scalars)
|
||||
return error_response(err_internal("this container declares clef but carries no "
|
||||
"decision.clef.* scalars; convert it again with "
|
||||
"rad-convert --set decision.type=clef"), req.peer);
|
||||
ClefRequest r;
|
||||
ApiError err;
|
||||
if (parse_clef_request(req.body, odeps_, lim_, opt_.model_id, &r, &err) < 0)
|
||||
return error_response(err, req.peer);
|
||||
std::vector<int32_t> words;
|
||||
if (clef_words(r, &words, &err) < 0) return error_response(err, req.peer);
|
||||
if ((int64_t)words.size() > opt_.plugin_words)
|
||||
return error_response(err_internal("the loaded plugin takes fewer words a request than "
|
||||
"this one needs"), req.peer);
|
||||
const int64_t n_opt = clef_option_count(r);
|
||||
if (auto no = admit(1, req.peer, ApiShape::OpenAI, "questions")) return *no;
|
||||
RAD_INFO("systemone (clef) admitted%s questions=%zu options=%lld prompt=%zu tok",
|
||||
from_peer(req.peer).c_str(), r.questions.size(), (long long)n_opt, r.tokens.size());
|
||||
|
||||
std::vector<Generation> gens(1);
|
||||
Generation& g = gens[0];
|
||||
g.index = 0;
|
||||
g.sink = std::make_shared<Sink>(16);
|
||||
auto ts = std::make_shared<TokenScores>();
|
||||
ts->tokens = clef_score_ids(n_opt);
|
||||
Request rq;
|
||||
rq.prompt = r.tokens;
|
||||
rq.token_scores = ts;
|
||||
rq.plugin_words = std::move(words);
|
||||
rq.max_tokens = 1;
|
||||
rq.sp.temp = 0.0f;
|
||||
rq.sink = g.sink.get();
|
||||
std::vector<Request> part;
|
||||
part.push_back(std::move(rq));
|
||||
std::vector<uint64_t> ids;
|
||||
deps_.sched->submit_all(part, &ids);
|
||||
g.id = ids.empty() ? 0 : ids[0];
|
||||
g.sink->id = g.id;
|
||||
if (!g.id) {
|
||||
retire(gens);
|
||||
release(1);
|
||||
return error_response(err_internal("the scheduler refused the request"), req.peer);
|
||||
}
|
||||
for (;;) {
|
||||
if (g.sink->done()) { g.closed = true; g.finish = g.sink->finish_reason(); break; }
|
||||
if (req.peer_gone() || stopping_.load()) return *abandoned(req, gens, 1);
|
||||
g.sink->wait();
|
||||
}
|
||||
if (g.finish == Finish::Error || g.finish == Finish::Cancelled) {
|
||||
const std::string why = g.finish == Finish::Error ? engine_failure(*g.sink)
|
||||
: "the engine cancelled the request";
|
||||
retire(gens);
|
||||
release(1);
|
||||
return error_response(err_internal(why), req.peer);
|
||||
}
|
||||
|
||||
ClefScalars sc;
|
||||
sc.prior_logit_scale = opt_.clef_prior_logit_scale;
|
||||
sc.joint_logit_scale = opt_.clef_joint_logit_scale;
|
||||
sc.residual_gate = opt_.clef_residual_gate;
|
||||
const std::vector<float> logits = clef_logits(ts->logits, n_opt, sc);
|
||||
json answers = json::object();
|
||||
size_t at = 0;
|
||||
for (const ClefQuestion& q : r.questions) {
|
||||
std::vector<float> ql(logits.begin() + (std::ptrdiff_t)at,
|
||||
logits.begin() + (std::ptrdiff_t)(at + q.options.size()));
|
||||
at += q.options.size();
|
||||
json a;
|
||||
if (clef_answer(q, ql, &a, &err) < 0) {
|
||||
retire(gens);
|
||||
release(1);
|
||||
return error_response(err, req.peer);
|
||||
}
|
||||
answers[q.name] = std::move(a);
|
||||
}
|
||||
retire(gens);
|
||||
release(1);
|
||||
metrics_.observe_http(200);
|
||||
return HttpResponse::json_body(200, json_dump(json{
|
||||
{ "answers", std::move(answers) },
|
||||
{ "usage", { { "input_tokens", (int64_t)r.tokens.size() }, { "output_tokens", 0 } } } }));
|
||||
}
|
||||
|
||||
HttpResponse Server::handle_models(const HttpRequest& req) {
|
||||
const std::string id = req.param("id");
|
||||
/* Runtime LoRA: each loaded adapter is a model of its own, whose parent is the served one. */
|
||||
|
||||
@@ -82,8 +82,15 @@ struct ServerOptions {
|
||||
int64_t video_ttl_s = 3600;
|
||||
int64_t max_videos = 32;
|
||||
/* The decision protocol from the container's `decision.type`: "d1" serves /v1/systemone
|
||||
* (systemone.h); empty answers it 501. Never inferred from a name. */
|
||||
* (systemone.h), "clef" serves it through the head on the card (clef.h); empty answers it
|
||||
* 501. Never inferred from a name. */
|
||||
std::string decision_type;
|
||||
/* clef: the head's learned scalars from the container header (decision.clef.*, written by
|
||||
* rad-convert from joint_head.safetensors), and whether the header carried them. */
|
||||
double clef_prior_logit_scale = 0.0, clef_joint_logit_scale = 0.0, clef_residual_gate = 0.0;
|
||||
bool clef_scalars = false;
|
||||
/* The most words one request may hand the plugin (Program::plugin_words). */
|
||||
int64_t plugin_words = 0;
|
||||
/* What --model named, reported as the model entry's `root` as vLLM reports its loaded path
|
||||
* (the served name says nothing about the file behind it). Empty omits the field. */
|
||||
std::string model_path;
|
||||
@@ -243,6 +250,7 @@ private:
|
||||
HttpResponse handle_infill(const HttpRequest& req);
|
||||
HttpResponse handle_embeddings(const HttpRequest& req);
|
||||
HttpResponse handle_systemone(const HttpRequest& req);
|
||||
HttpResponse handle_clef(const HttpRequest& req);
|
||||
HttpResponse handle_apply_template(const HttpRequest& req);
|
||||
using ToChat = int (*)(const std::string& body, const OaiLimits& lim, std::string* chat,
|
||||
std::string* extra, ApiError* err);
|
||||
|
||||
Reference in new issue
Block a user