laya: Laya decision models on /v1/systemone (arch/laya)

This commit is contained in:
Brandon Bertelsen committed 2026-10-28 23:44:38 -04:00
1 parent d24cf32a13
commit 9c605a103d
18 files changed
+2346 -175

No files matched your search

+2 -1
View File
@@ -390,7 +390,8 @@ json Server::capabilities() const {
* whatever its conditioning network's processor would take. */
const bool img = opt_.image_model || opt_.video_model;
caps["grammar"] = !img && deps_.grammar != nullptr;
caps["embedding"] = deps_.embed != nullptr;
/* Laya runs on the embedding path, but answers /v1/systemone, not /v1/embeddings. */
caps["embedding"] = deps_.embed != nullptr && opt_.decision_type != "laya";
caps["image"] = deps_.mm && deps_.mm->accepts(MediaKind::Image);
caps["video"] = !img && deps_.mm && deps_.mm->accepts(MediaKind::Video);
caps["audio"] = !img && deps_.mm && deps_.mm->accepts(MediaKind::Audio);
+402
View File
@@ -0,0 +1,402 @@
/* laya.cpp -- Laya's sequences, words and answers (laya.h).
*
* Every string below is laya's own, from laya/common.py: "<type> question: ", the option forms
* ("label: description", "level i: description", "false: ..."/"true: ...") and the noul defaults.
* Each piece is tokenized on its own and the ids concatenated, as build_head does, so a marker
* always sits at its own option's first position.
*/
#include "laya.h"
#include "rad_laya.h"
#include <nlohmann/json.hpp>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <limits>
namespace rad {
namespace server {
namespace {
bool only_keys(const json& o, std::initializer_list<const char*> allowed, const std::string& path,
ApiError* err) {
for (const auto& kv : o.items()) {
bool ok = false;
for (const char* k : allowed) ok = ok || kv.key() == k;
if (!ok) {
*err = err_unsupported(path + kv.key(), "not a field of a System One request");
return false;
}
}
return true;
}
bool blank(const std::string& s) {
return std::all_of(s.begin(), s.end(), [](unsigned char c) { return std::isspace(c) != 0; });
}
std::string replace_all(std::string s, const std::string& from, const std::string& to) {
if (from.empty()) return s;
for (size_t at = s.find(from); at != std::string::npos; at = s.find(from, at + to.size()))
s.replace(at, from.size(), to);
return s;
}
/* render_criterion: a string as it is, anything else as JSON text. */
std::string criterion(const json& v) { return v.is_string() ? v.get<std::string>() : laya_render(v); }
/* A choice label as Python's str() writes it (the option text) and as its json module writes a
* dict key (the response's key): they differ only for a bool. */
std::string label_text(const json& v) {
if (v.is_boolean()) return v.get<bool>() ? "True" : "False";
return v.is_string() ? v.get<std::string>() : laya_render(v);
}
std::string label_key(const json& v) {
return v.is_string() ? v.get<std::string>() : laya_render(v);
}
/* Python compares 1, 1.0 and True as one dict key. */
bool same_label(const json& a, const json& b) {
const bool na = a.is_number() || a.is_boolean(), nb = b.is_number() || b.is_boolean();
if (na && nb) {
auto num = [](const json& v) { return v.is_boolean() ? (v.get<bool>() ? 1.0 : 0.0) : v.get<double>(); };
return num(a) == num(b);
}
return a == b;
}
/* _check_question and _to_internal, then render_options: the option texts in model order. */
int question(const std::string& path, const json& src, LayaQuestion* q, std::string* ins,
std::vector<std::string>* opts, ApiError* err) {
if (!src.is_object()) { *err = err_bad(path, "must be an object"); return RAD_E_INVAL; }
if (!only_keys(src, { "type", "instructions", "criteria" }, path + ".", err)) return RAD_E_INVAL;
const auto ty = src.find("type");
if (ty == src.end() || !ty->is_string() ||
(*ty != "choice" && *ty != "score" && *ty != "noul")) {
*err = err_bad(path + ".type", "must be choice, score or noul");
return RAD_E_INVAL;
}
q->type = ty->get<std::string>();
q->type_id = q->type == "choice" ? RAD_LAYA_CHOICE : q->type == "score" ? RAD_LAYA_SCORE : RAD_LAYA_NOUL;
const auto in = src.find("instructions");
if (in == src.end() || in->is_null() || (in->is_string() && blank(in->get<std::string>())) ||
((in->is_array() || in->is_object()) && in->empty())) {
*err = err_bad(path + ".instructions", "required: the text the model should answer");
return RAD_E_INVAL;
}
*ins = in->is_string() ? in->get<std::string>() : laya_render(*in);
const json crit = src.contains("criteria") ? src.at("criteria") : json();
if (q->type == "choice") {
if (!(crit.is_object() || crit.is_array()) || crit.empty()) {
*err = err_bad(path + ".criteria", "must be a non-empty object of label to description, "
"or a non-empty array of labels");
return RAD_E_INVAL;
}
if (crit.is_object()) {
for (const auto& kv : crit.items()) {
const json& d = kv.value();
q->keys.push_back(kv.key());
q->labels.push_back(kv.key());
opts->push_back(d.is_null() || d == "" ? kv.key() : kv.key() + ": " + criterion(d));
}
} else {
for (size_t i = 0; i < crit.size(); ++i) {
const json& l = crit[i];
if (!(l.is_string() || l.is_number() || l.is_boolean())) {
*err = err_bad(path + ".criteria", "label " + std::to_string(i) + " must be a "
"string, number or bool: a label is the option's text and its "
"answer key");
return RAD_E_INVAL;
}
for (size_t j = 0; j < i; ++j)
if (same_label(crit[j], l)) {
*err = err_bad(path + ".criteria", "label " + std::to_string(i) +
" repeats label " + std::to_string(j) + "; every option "
"needs its own answer key");
return RAD_E_INVAL;
}
q->keys.push_back(label_key(l));
q->labels.push_back(l);
opts->push_back(label_text(l));
}
}
return RAD_OK;
}
if (q->type == "score") {
if (!crit.is_array() || crit.empty()) {
*err = err_bad(path + ".criteria", "must be a non-empty array of level descriptions, "
"level 0 first");
return RAD_E_INVAL;
}
for (size_t i = 0; i < crit.size(); ++i) {
if (crit[i].is_null()) {
*err = err_bad(path + ".criteria", "level " + std::to_string(i) + " is null; give "
"every level a description");
return RAD_E_INVAL;
}
q->keys.push_back(std::to_string(i));
q->legend.push_back(criterion(crit[i]));
opts->push_back("level " + std::to_string(i) + ": " + q->legend.back());
}
return RAD_OK;
}
/* noul: false then true, so the second probability is P(true). */
json t, f;
if (!crit.is_null()) {
if (!crit.is_object()) {
*err = err_bad(path + ".criteria", "must be an object with optional `true` and `false` "
"descriptions, or absent");
return RAD_E_INVAL;
}
for (const auto& kv : crit.items()) {
std::string k = kv.key();
for (char& ch : k) ch = (char)std::tolower((unsigned char)ch);
if (k == "true") t = kv.value();
else if (k == "false") f = kv.value();
else {
*err = err_bad(path + ".criteria", "takes only `true` and `false`, got `" + kv.key() + "`");
return RAD_E_INVAL;
}
}
}
q->keys = { "false", "true" };
opts->push_back("false: " + (f.is_null() || f == "" ? std::string("no, the statement does not hold")
: criterion(f)));
opts->push_back("true: " + (t.is_null() || t == "" ? std::string("yes, the statement holds")
: criterion(t)));
return RAD_OK;
}
double round4(double v) { return std::round(v * 10000.0) / 10000.0; }
/* temp_bucket, and the clamp the package applies to every fitted temperature as it loads. */
double temperature_of(const LayaQuestion& q, size_t k, const LayaSettings& s) {
const char* size = k <= 2 ? "2" : k <= 5 ? "3-5" : k <= 10 ? "6-10" : "11+";
const auto it = s.temperature_by_options.find(q.type + ":" + size);
const double t = it != s.temperature_by_options.end() ? it->second : s.temperature[q.type_id];
return std::isfinite(t) ? std::min(5.0, std::max(0.5, t)) : 1.0;
}
/* confidence_from_probs: 1 - H(p) / log(k), within [0, 1]; 1 below two options. */
double entropy_confidence(const std::vector<float>& p) {
const size_t k = p.size();
if (k < 2) return 1.0;
float ent = 0.0f;
for (float v : p) ent += v * std::log(std::min(1.0f, std::max(1e-12f, v)));
const double c = 1.0 - (double)(-ent) / std::log((double)k);
return c < 0.0 ? 0.0 : c > 1.0 ? 1.0 : c;
}
} /* namespace */
std::string laya_render(const json& v) {
if (v.is_object()) {
std::string s = "{";
bool first = true;
for (const auto& kv : v.items()) {
if (!first) s += ", ";
first = false;
s += json(kv.key()).dump(-1, ' ', false, nlohmann::ordered_json::error_handler_t::replace);
s += ": " + laya_render(kv.value());
}
return s + "}";
}
if (v.is_array()) {
std::string s = "[";
for (size_t i = 0; i < v.size(); ++i) s += (i ? ", " : "") + laya_render(v[i]);
return s + "]";
}
return v.dump(-1, ' ', false, nlohmann::ordered_json::error_handler_t::replace);
}
int parse_laya_request(const std::string& body, const OaiDeps& d, const OaiLimits& lim,
const std::string& model_id, const LayaSettings& s, LayaRequest* out,
ApiError* err) {
*out = LayaRequest{};
json b;
RAD_TRY(parse_request_object(body, lim, &b, err));
if (!only_keys(b, { "model", "state", "questions", "images" }, "", err)) return RAD_E_INVAL;
if (b.contains("model") && (!b["model"].is_string() || b["model"] != model_id)) {
*err = err_bad("model", "must name the loaded model, " + model_id);
return RAD_E_INVAL;
}
if (!b.contains("state") || b["state"].is_null() ||
(b["state"].is_string() && b["state"].get<std::string>().empty())) {
*err = err_bad("state", "required: a non-empty string or any JSON value");
return RAD_E_INVAL;
}
if (b.contains("images") && !(b["images"].is_array() && b["images"].empty())) {
*err = err_not_implemented("images: Laya reads its state as text only");
return RAD_E_UNSUPPORTED;
}
const json& qs = b.contains("questions") ? b["questions"] : json();
if (!qs.is_object() || qs.empty()) {
*err = err_bad("questions", "must be a non-empty object of named questions");
return RAD_E_INVAL;
}
const ITokenizer& tok = *d.tok;
auto ids = [&](const std::string& text) {
return tok.encode(replace_all(text, s.mask_token, " "), false, true);
};
/* The state is tokenized once and cut per question; a list of turns keeps its end. */
const json& st = b["state"];
const bool keep_end = st.is_array();
const std::vector<int32_t> state = ids(st.is_string() ? st.get<std::string>() : laya_render(st));
out->state_tokens = (int64_t)state.size();
for (const auto& kv : qs.items()) {
const std::string path = "questions." + kv.key();
if (blank(kv.key())) {
*err = err_bad("questions", "a question's name must not be empty");
return RAD_E_INVAL;
}
LayaQuestion q;
q.name = kv.key();
std::string ins;
std::vector<std::string> opts;
RAD_TRY(question(path, kv.value(), &q, &ins, &opts, err));
const int64_t n = (int64_t)opts.size();
if (n > RAD_LAYA_MAX_OPTIONS) {
*err = err_bad(path + ".criteria", "at most " + std::to_string(RAD_LAYA_MAX_OPTIONS) +
" options a question");
return RAD_E_INVAL;
}
/* build_head: options capped at 48 tokens, all of them shrunk evenly when they leave the
* instruction fewer than 16 of head_max_len, and the instruction cut to what is left. */
std::vector<std::vector<int32_t>> opt;
int64_t used = 0;
for (const std::string& o : opts) {
std::vector<int32_t> t = ids(" " + o);
if (t.size() > 48) t.resize(48);
t.insert(t.begin(), s.mask);
used += (int64_t)t.size();
opt.push_back(std::move(t));
}
int64_t budget = s.head_max_len - used;
if (budget < 16) {
const int64_t per = std::max<int64_t>(4, (s.head_max_len - 16) / std::max<int64_t>(1, n));
used = 0;
for (auto& t : opt) {
if ((int64_t)t.size() > per) t.resize((size_t)per);
used += (int64_t)t.size();
}
budget = s.head_max_len - used;
}
std::vector<int32_t> head = ids(q.type + " question: " + ins);
head.resize((size_t)std::min<int64_t>((int64_t)head.size(), std::max<int64_t>(8, budget)));
std::vector<int32_t>& seq = q.tokens;
seq.push_back(s.cls);
seq.insert(seq.end(), head.begin(), head.end());
seq.push_back(s.sep);
for (const auto& t : opt) {
q.markers.push_back((int32_t)seq.size());
seq.insert(seq.end(), t.begin(), t.end());
}
seq.push_back(s.sep);
/* build_sequence: the state gets the room left before the closing [SEP]. */
const int64_t room = std::max<int64_t>(0, s.max_len - (int64_t)seq.size() - 1);
const int64_t take = std::min<int64_t>(room, (int64_t)state.size());
if (keep_end) seq.insert(seq.end(), state.end() - take, state.end());
else seq.insert(seq.end(), state.begin(), state.begin() + take);
seq.push_back(s.sep);
q.state_used = take;
if ((int64_t)seq.size() > s.max_len) seq.resize((size_t)s.max_len);
q.markers.erase(std::remove_if(q.markers.begin(), q.markers.end(),
[&](int32_t m) { return m >= s.max_len; }),
q.markers.end());
if ((int64_t)q.markers.size() != n) {
*err = err_bad(path + ".criteria", std::to_string(n) + " options do not fit this "
"model's " + std::to_string(s.max_len) + "-token sequence (head_max_len " +
std::to_string(s.head_max_len) + "); fewer or shorter options fit");
return RAD_E_INVAL;
}
out->questions.push_back(std::move(q));
}
return RAD_OK;
}
std::vector<int32_t> laya_words(const LayaQuestion& q) {
std::vector<int32_t> w((size_t)RAD_LAYA_W_MARKERS, 0);
w[RAD_LAYA_W_N] = (int32_t)q.tokens.size();
w[RAD_LAYA_W_TYPE] = q.type_id;
w[RAD_LAYA_W_K] = (int32_t)q.markers.size();
w.insert(w.end(), q.markers.begin(), q.markers.end());
return w;
}
int laya_answer(const LayaQuestion& q, const std::vector<float>& pooled, const LayaSettings& s,
json* out, ApiError* err) {
const size_t k = q.keys.size();
if (pooled.size() < k) {
*err = err_internal("the plugin returned no logits for question " + q.name);
return RAD_E_STATE;
}
/* _decode_answers, in float32 as the package's numpy rows are. */
const float t = (float)temperature_of(q, k, s);
std::vector<float> p(k);
float peak = -std::numeric_limits<float>::infinity();
for (size_t i = 0; i < k; ++i) {
if (!std::isfinite(pooled[i])) {
*err = err_internal("question " + q.name + " scored a non-finite logit");
return RAD_E_STATE;
}
p[i] = pooled[i] / t;
peak = std::max(peak, p[i]);
}
float total = 0.0f;
for (float& v : p) total += (v = std::exp(v - peak));
for (float& v : p) v /= total;
json a = json::object();
a["type"] = q.type;
if (q.type == "noul") {
a["noul"] = round4(p[1]);
*out = std::move(a);
return RAD_OK;
}
json probs = json::object();
for (size_t i = 0; i < k; ++i) probs[q.keys[i]] = round4(p[i]);
if (q.type == "choice") {
const size_t best = (size_t)(std::max_element(p.begin(), p.end()) - p.begin());
a["choice"] = q.labels[best];
a["confidence"] = round4(entropy_confidence(p));
a["probabilities"] = std::move(probs);
} else {
double expected = 0.0;
json legend = json::object();
for (size_t i = 0; i < k; ++i) {
expected += (double)i * (double)p[i];
legend[q.keys[i]] = q.legend[i];
}
a["score"] = round4(expected);
a["confidence"] = round4(entropy_confidence(p));
a["probabilities"] = std::move(probs);
a["legend"] = std::move(legend);
}
*out = std::move(a);
return RAD_OK;
}
json laya_usage(const LayaRequest& r) {
int64_t input = 0, dropped = 0;
json cut = json::array();
for (const LayaQuestion& q : r.questions) {
input += (int64_t)q.tokens.size();
dropped = std::max(dropped, r.state_tokens - q.state_used);
if (q.state_used < r.state_tokens) cut.push_back(q.name);
}
return json{ { "input_tokens", input }, { "output_tokens", 0 },
{ "state_tokens", r.state_tokens }, { "state_tokens_dropped", dropped },
{ "truncated", dropped > 0 }, { "truncated_questions", std::move(cut) } };
}
} /* namespace server */
} /* namespace rad */
+85
View File
@@ -0,0 +1,85 @@
/* laya.h -- POST /v1/systemone for Convai Innovations' Laya decision models: each question's
* sequence, its words for the plugin, and the answer from the plugin's option logits.
*
* Laya reads one sequence a question, `[CLS] <type> question: <instructions> [SEP] [MASK] opt0
* [MASK] opt1 ... [SEP] <state> [SEP]`, and scores each option at its [MASK] (arch/laya). This
* file is a C++ rewrite of _check_question, _to_internal, render_options, build_head,
* build_sequence and _decode_answers in laya's own package (laya/agent.py, laya/common.py), its
* strings kept as they are: the model was trained on exactly this text.
*
* As the package does, a state longer than the room a question leaves is cut (to its start, or to
* its end when the state is a list of turns), and `usage` says so with the package's own keys:
* state_tokens, state_tokens_dropped, truncated and truncated_questions.
*
* Served only when the container declares laya (`decision.type` = `laya`, which rad-convert sets
* for a Laya checkpoint directory).
*/
#pragma once
#include "oai.h"
#include <map>
#include <string>
#include <vector>
namespace rad {
namespace server {
/* The container's Laya settings (decision.laya.*, which rad-convert writes from
* rl_agent_config.json and tokenizer/; the engine copies them here). */
struct LayaSettings {
int64_t max_len = 0, head_max_len = 0;
int32_t cls = -1, sep = -1, mask = -1;
std::string mask_token;
double temperature[3] = { 1.0, 1.0, 1.0 }; /* choice, score, noul */
std::map<std::string, double> temperature_by_options; /* "choice:3-5" -> t */
bool ok() const {
return max_len > 0 && head_max_len > 0 && cls >= 0 && sep >= 0 && mask >= 0 &&
!mask_token.empty();
}
};
struct LayaQuestion {
std::string name, type; /* type: choice | score | noul */
int type_id = 0; /* RAD_LAYA_CHOICE, _SCORE or _NOUL (abi/rad_laya.h) */
/* One entry an option, in the order the model scores them: a choice's labels as the request
* gives them (`keys` the response's keys, `labels` the values `choice` answers with), a score's
* levels "0".."n-1", noul false then true. */
std::vector<std::string> keys;
std::vector<json> labels;
std::vector<std::string> legend; /* score: each level as the model read it */
std::vector<int32_t> tokens; /* the sequence */
std::vector<int32_t> markers; /* each option's [MASK] position in it */
int64_t state_used = 0; /* state tokens the sequence holds */
};
struct LayaRequest {
std::vector<LayaQuestion> questions;
int64_t state_tokens = 0; /* the state, tokenized whole */
};
/* Parse, validate and build every question's sequence. 400 for what the package refuses, and for
* options that do not fit the head's budget. */
int parse_laya_request(const std::string& body, const OaiDeps& d, const OaiLimits& lim,
const std::string& model_id, const LayaSettings& s, LayaRequest* out,
ApiError* err);
/* The question's words for the plugin (abi/rad_laya.h): its tokens, type and markers. */
std::vector<int32_t> laya_words(const LayaQuestion& q);
/* One question's answer from the plugin's pooled row (its first k values are the option logits):
* the fitted temperature, clamped to [0.5, 5] as the package clamps it, a softmax, and the
* package's rounding and confidence. A non-finite logit is an error. */
int laya_answer(const LayaQuestion& q, const std::vector<float>& pooled, const LayaSettings& s,
json* out, ApiError* err);
/* The request's `usage`: input_tokens (every sequence) and the package's truncation report. */
json laya_usage(const LayaRequest& r);
/* json.dumps(v, ensure_ascii=False) with Python's default separators, keys in the request's
* order: how the package renders a state, an instruction or a criterion that is not a string.
* Exposed for the tests. */
std::string laya_render(const json& v);
} /* namespace server */
} /* namespace rad */
+104 -1
View File
@@ -3,6 +3,8 @@
#include <cstdio>
#include "clef.h"
#include "laya.h"
#include "rad_laya.h"
#include "json_partial.h"
#include "systemone.h"
#include "rad_internal.h"
@@ -1621,6 +1623,11 @@ HttpResponse Server::handle_embeddings(const HttpRequest& req) {
if (!deps_.sched || !deps_.tok)
return error_response(err_not_implemented("the server has no engine attached"),
req.peer);
/* Laya runs on the embedding path, but its pooled row is option logits, not a vector. */
if (opt_.decision_type == "laya")
return error_response(err_not_implemented("/v1/embeddings requires an embedding model; "
"this one is Laya, which answers /v1/systemone"),
req.peer);
if (!deps_.embed)
return error_response(err_not_implemented(
opt_.image_model ? "/v1/embeddings requires an embedding model; this one makes images"
@@ -1746,10 +1753,12 @@ 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 == "laya") return handle_laya(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, clef with --set decision.type=clef)"),
"(convert d1 with --set decision.type=d1, clef with --set decision.type=clef; "
"rad-convert sets laya for a Laya checkpoint directory)"),
req.peer);
DecisionRequest r;
@@ -1952,6 +1961,100 @@ HttpResponse Server::handle_clef(const HttpRequest& req) {
{ "usage", { { "input_tokens", (int64_t)r.tokens.size() }, { "output_tokens", 0 } } } }));
}
/* Laya (laya.h): one engine request a question, each its own sequence with the state in it, as
* Laya reads them, submitted together so the scheduler runs them in one step. Laya's plugin
* (arch/laya) declares a pooled output, so each request runs on the embedding path and comes back
* as its pooled row, whose first k values are the question's option logits. */
HttpResponse Server::handle_laya(const HttpRequest& req) {
if (!opt_.laya.ok() || !deps_.embed || deps_.embed->dim() < RAD_LAYA_POOLED)
return error_response(err_internal("this container declares laya but carries no complete "
"decision.laya.* settings or no Laya plugin; convert the "
"checkpoint directory again with rad-convert"), req.peer);
LayaRequest r;
ApiError err;
if (parse_laya_request(req.body, odeps_, lim_, opt_.model_id, opt_.laya, &r, &err) < 0)
return error_response(err, req.peer);
const int n = (int)r.questions.size();
for (const LayaQuestion& q : r.questions)
if ((int64_t)q.tokens.size() > deps_.embed->max_input())
return error_response(err_bad("questions", "question " + q.name + " is " +
std::to_string(q.tokens.size()) + " tokens; this "
"deployment runs at most " +
std::to_string(deps_.embed->max_input()) +
" a step (--max-num-batched-tokens)"), req.peer);
if (auto no = admit(n, req.peer, ApiShape::OpenAI, "questions")) return *no;
RAD_INFO("systemone (laya) admitted%s questions=%d state=%lld tok",
from_peer(req.peer).c_str(), n, (long long)r.state_tokens);
std::vector<Generation> gens;
std::vector<Request> rqs;
gens.reserve((size_t)n);
rqs.reserve((size_t)n);
for (int i = 0; i < n; ++i) {
Generation g;
g.index = i;
g.prompt_index = i;
g.sink = std::make_shared<Sink>(16);
Request rq;
rq.prompt = r.questions[(size_t)i].tokens;
rq.plugin_words = laya_words(r.questions[(size_t)i]);
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]) {
cancel_all(gens, false);
retire(gens);
release(n);
return error_response(err_internal("the scheduler refused question " +
r.questions[i].name), req.peer);
}
}
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 (auto no = abandoned(req, gens, n)) return *no;
for (auto& g : gens) if (!g.closed) { g.sink->wait(); break; }
}
json answers = json::object();
for (int i = 0; i < n; ++i) {
const Generation& g = gens[(size_t)i];
const LayaQuestion& q = r.questions[(size_t)i];
if (g.finish == Finish::Error || g.finish == Finish::Cancelled) {
const std::string why = g.finish == Finish::Error ? engine_failure(*g.sink)
: "the engine cancelled question " + q.name;
retire(gens);
release(n);
return error_response(err_internal(why), req.peer);
}
json a;
if (laya_answer(q, g.sink->embedding(), opt_.laya, &a, &err) < 0) {
retire(gens);
release(n);
return error_response(err, req.peer);
}
answers[q.name] = std::move(a);
}
retire(gens);
release(n);
metrics_.observe_http(200);
return HttpResponse::json_body(200, json_dump(json{ { "answers", std::move(answers) },
{ "usage", laya_usage(r) } }));
}
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. */
+7 -2
View File
@@ -16,6 +16,7 @@
#include <functional>
#include "http.h"
#include "iface.h"
#include "laya.h"
#include "liveview.h"
#include "metrics.h"
#include "oai.h"
@@ -82,13 +83,16 @@ 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), "clef" serves it through the head on the card (clef.h); empty answers it
* 501. Never inferred from a name. */
* (systemone.h), "clef" serves it through the head on the card (clef.h), "laya" through the
* Laya plugin's option logits (laya.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;
/* laya: the sequence lengths, special tokens and fitted temperatures from the container
* header (decision.laya.*, written by rad-convert). */
LayaSettings laya;
/* 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
@@ -251,6 +255,7 @@ private:
HttpResponse handle_embeddings(const HttpRequest& req);
HttpResponse handle_systemone(const HttpRequest& req);
HttpResponse handle_clef(const HttpRequest& req);
HttpResponse handle_laya(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);