Archived
server: a control_vector request field, its admin switch and the Model-view panel
The field is per request, with the deployment's runtime default when absent, and a deployment with no vector refuses 'true' by name rather than serving the stock model. Strata's `experimental_speed_projection` is accepted as an alias. The dashboard's Model view carries a "Control vectors" panel with the switch for that default; GET/POST /admin/control-vector is its endpoint, behind the API key like every other route. The state is in /stats and /server_info. Nothing here reloads the model: the directions are uploaded once and each step carries a per-request mask. The panel stays visible when the page has no API key, with the reason, instead of disappearing -- a control that vanishes because the page has not signed in reads exactly like a build without the feature.
This commit is contained in:
1 parent
66c6d71254
commit
6f26cdb5f4
7 files changed
+312
-3
No files matched your search
+127
-2
@@ -30,6 +30,47 @@ namespace server {
|
||||
* Derived rates are the page's job: they are counter differences over wall time, and only the
|
||||
* browser holds both ends of its poll interval. Computing them here would need per-client state. */
|
||||
|
||||
/* ------------------------------------------------------------------ /admin/control-vector
|
||||
*
|
||||
* The control vector's runtime switch (core/cvec.h). Turning it on for the next
|
||||
* request costs a boolean: the direction table was uploaded at startup and the per-request mask
|
||||
* is a derived buffer the core fills every step, so there is nothing to rebuild and nothing to
|
||||
* reload. The engine's own log records the load; this records the flip, because a deployment's
|
||||
* answer to "was that text produced with the vector" should not depend on a browser's memory.
|
||||
*
|
||||
* GET reports the state (and is what the Model view renders); POST takes {"enabled": true|false}
|
||||
* and answers with the same object. A deployment with no vector loaded answers 404 on both: it has
|
||||
* no such feature, and its requests refuse `control_vector: true` by name. */
|
||||
HttpResponse Server::handle_control_vector(const HttpRequest& req) {
|
||||
const bool have = deps_.control_vector.available && deps_.control_vector.available();
|
||||
if (!have)
|
||||
return error_response(err_not_implemented(
|
||||
"this deployment found no control vector for its model; there is nothing to switch"),
|
||||
req.peer);
|
||||
|
||||
if (req.method == "POST") {
|
||||
json body;
|
||||
try {
|
||||
body = json::parse(req.body.empty() ? "{}" : req.body);
|
||||
} catch (const std::exception& e) {
|
||||
return error_response(err_bad("body", std::string("not JSON: ") + e.what()), req.peer);
|
||||
}
|
||||
const auto it = body.find("enabled");
|
||||
if (it == body.end() || !it->is_boolean())
|
||||
return error_response(err_bad("enabled", "a boolean is required"), req.peer);
|
||||
const bool on = it->get<bool>();
|
||||
if (deps_.control_vector.set_default) deps_.control_vector.set_default(on);
|
||||
RAD_INFO("control vector: default %s by an admin request%s", on ? "ON" : "OFF",
|
||||
req.peer.empty() ? "" : (" from " + req.peer).c_str());
|
||||
} else if (req.method != "GET") {
|
||||
return HttpResponse::json_body(405, err_bad("method", "GET or POST").dump());
|
||||
}
|
||||
|
||||
metrics_.observe_http(200);
|
||||
return HttpResponse::json_body(
|
||||
200, deps_.control_vector.json ? deps_.control_vector.json() : std::string("{}"));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ /sessions
|
||||
*
|
||||
* A separate route, not a /stats field: /stats is polled every second, while a stored session
|
||||
@@ -124,6 +165,16 @@ HttpResponse Server::handle_stats(const HttpRequest&) {
|
||||
json o;
|
||||
o["model"] = opt_.model_id;
|
||||
o["version"] = opt_.version;
|
||||
/* The control vector (core/cvec.h), when this deployment has one: `available` is what a UI
|
||||
* tests before offering the control, `default` what a request that names no choice runs. The
|
||||
* dashboard's own control asks /admin/control-vector, which also reports this. */
|
||||
if (deps_.control_vector.json) {
|
||||
try {
|
||||
o["control_vector"] = json::parse(deps_.control_vector.json());
|
||||
} catch (const std::exception&) {
|
||||
/* A malformed snapshot is not worth failing /stats over; the field is omitted. */
|
||||
}
|
||||
}
|
||||
o["uptime_s"] = unix_now() - started_at_;
|
||||
/* Seconds this process took to come up, the cost of a restart; 0 before the server listens. */
|
||||
o["startup_s"] = startup_s_;
|
||||
@@ -643,7 +694,8 @@ th + th, td + td { padding-left:14px; }
|
||||
it as a dot, because on a server with long-lived agent clients the second group is most of the
|
||||
table and none of it needs reading to understand the first. */
|
||||
#view-sess .cell { height:auto; padding-bottom:12px; }
|
||||
#view-sess .panel + .panel { margin-top:14px; }
|
||||
/* Stacked panels in one view are 14px apart, the same gap the strip's own margin-bottom gives. */
|
||||
#view-sess .panel + .panel, #view-model .panel + .panel { margin-top:14px; }
|
||||
.stbl td, .stbl th { width:1%; }
|
||||
/* The two text columns take the slack and read left to right; the figures stay narrow. */
|
||||
.stbl .l { text-align:left; width:auto; }
|
||||
@@ -1177,6 +1229,22 @@ nav button.on { color:var(--fg); background:var(--track); }
|
||||
|
||||
<div id="view-model" hidden>
|
||||
<div class="strip" id="gstrip"></div>
|
||||
<!-- The control vector (core/cvec.h): one direction per layer, projected out of the residual
|
||||
stream at run time. Shown only on a deployment that found one; flipping it needs no
|
||||
reload, and every request that names its own choice overrides it. -->
|
||||
<section class="panel" id="cvpanel" hidden>
|
||||
<div class="phead">
|
||||
<span class="cap">Control vectors</span>
|
||||
<span class="aside" id="cvsum"></span>
|
||||
</div>
|
||||
<div class="pbody">
|
||||
<label class="cvrow">
|
||||
<input id="cvbox" type="checkbox">
|
||||
<span id="cvlabel">Apply the control vector to new requests</span>
|
||||
</label>
|
||||
<div class="aside" id="cvnote"></div>
|
||||
</div>
|
||||
</section>
|
||||
<section class="panel">
|
||||
<div class="phead">
|
||||
<span class="cap">Kernel libraries</span>
|
||||
@@ -1232,6 +1300,10 @@ $("key").addEventListener("change", (e) => {
|
||||
e.target.hidden = true;
|
||||
identify();
|
||||
poll();
|
||||
/* The Model view's controls are fetched on demand, so a key entered after the tab was opened
|
||||
* has to re-ask: otherwise the control-vector panel stays in its "sign in" state until the
|
||||
* tab is left and re-opened. */
|
||||
loadControlVector();
|
||||
});
|
||||
const HIST = 120; /* 60s of history at the 500ms poll */
|
||||
|
||||
@@ -2829,7 +2901,7 @@ function show(which) {
|
||||
$("tab-dash").className = (model || sess) ? "" : "on";
|
||||
$("tab-sess").className = sess ? "on" : "";
|
||||
$("tab-model").className = model ? "on" : "";
|
||||
if (model) loadGraph();
|
||||
if (model) { loadGraph(); loadControlVector(); }
|
||||
if (sess) loadSessions();
|
||||
window.scrollTo(0, 0);
|
||||
const h = model ? "#model" : sess ? "#sessions" : "#";
|
||||
@@ -2985,6 +3057,59 @@ async function identify() {
|
||||
} catch (err) { /* the header simply keeps its placeholder */ }
|
||||
}
|
||||
|
||||
/* THE SPEED PROJECTION, asked when the Model view opens. It is not a polled value: it changes only
|
||||
* when this page (or an admin call) flips it, and a control that silently reverted under the
|
||||
* reader would be worse than one that is a second old. A deployment without a control vector
|
||||
* answers 404 and the panel stays hidden. */
|
||||
async function loadControlVector() {
|
||||
/* THREE OUTCOMES, and only one of them hides the panel:
|
||||
* 404 this deployment has no control vector -- there is no such control, so it is omitted;
|
||||
* 200 the vector is loaded -- show the switch and its state;
|
||||
* anything else (most often 401, the page has no API key yet) -- the feature may well be
|
||||
* there, so SAY that instead of hiding the control. A control that silently disappears
|
||||
* because the page has not signed in reads exactly like a build without the feature. */
|
||||
try {
|
||||
const r = await api("admin/control-vector");
|
||||
if (r.status === 404) { $("cvpanel").hidden = true; return; }
|
||||
if (!r.ok) throw new Error(String(r.status));
|
||||
const p = await r.json();
|
||||
$("cvpanel").hidden = false;
|
||||
$("cvbox").disabled = false;
|
||||
$("cvbox").checked = !!p.default;
|
||||
$("cvsum").textContent = "layers " + p.first + "\u2013" + p.last + " \u00b7 " + p.mode +
|
||||
" \u00b7 scale " + p.scale;
|
||||
$("cvnote").textContent = "Applies to new requests only; a request's own control_vector " +
|
||||
"field wins. It removes a safety behaviour.";
|
||||
} catch (e) {
|
||||
$("cvpanel").hidden = false;
|
||||
$("cvbox").disabled = true;
|
||||
$("cvbox").checked = false;
|
||||
$("cvsum").textContent = "";
|
||||
$("cvnote").textContent =
|
||||
"Sign in with the API key (the field in the header) to read or change this: the vector " +
|
||||
"is present on this deployment, but its state needs the key.";
|
||||
}
|
||||
}
|
||||
$("cvbox").addEventListener("change", async (e) => {
|
||||
const want = e.target.checked;
|
||||
e.target.disabled = true;
|
||||
try {
|
||||
const r = await fetch("admin/control-vector", {
|
||||
method: "POST", cache: "no-store",
|
||||
headers: Object.assign({ "Content-Type": "application/json" },
|
||||
apiKey ? { Authorization: "Bearer " + apiKey } : {}),
|
||||
body: JSON.stringify({ enabled: want }),
|
||||
});
|
||||
if (!r.ok) throw new Error(String(r.status));
|
||||
const p = await r.json();
|
||||
$("cvbox").checked = !!p.default;
|
||||
} catch (err) {
|
||||
e.target.checked = !want; // the server kept the old value
|
||||
} finally {
|
||||
e.target.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
async function poll() {
|
||||
/* A background tab watches nothing, and neither does the Model view -- which is static. */
|
||||
if (document.hidden || $("view-dash").hidden) return;
|
||||
|
||||
@@ -240,6 +240,7 @@ const char* kAcceptedCommon[] = {
|
||||
"dry_sequence_breakers", "samplers",
|
||||
"grammar", "json_schema", "priority", "logprobs", "ignore_eos",
|
||||
"timings_per_token", "return_progress", "session_id", "n_probs",
|
||||
"control_vector", "experimental_speed_projection",
|
||||
};
|
||||
|
||||
const char* kAcceptedChat[] = {
|
||||
@@ -842,6 +843,17 @@ static int parse_sampling(const json& b, const OaiLimits& lim, OaiRequest* r, Ap
|
||||
/* ignore_eos: generate to max_tokens whatever the model emits. See Request::ignore_eos. */
|
||||
if (!get_bool(b, "ignore_eos", &r->ignore_eos, err)) return RAD_E_INVAL;
|
||||
|
||||
/* The control vector (core/cvec.h): an opt-in per request, and the only place an API client
|
||||
* turns it on when the deployment's default is off. `control_vector` is the field's name;
|
||||
* `experimental_speed_projection` is Strata's spelling of the same switch, kept as an alias so
|
||||
* a client written against that reports what it means. */
|
||||
for (const char* key : { "control_vector", "experimental_speed_projection" }) {
|
||||
if (!b.contains(key)) continue;
|
||||
bool on = false;
|
||||
if (!get_bool(b, key, &on, err)) return RAD_E_INVAL;
|
||||
r->control_vector = on ? 1 : 0;
|
||||
}
|
||||
|
||||
/* stop: a string or an array of strings. Bounded in count and length because every stop
|
||||
* string is searched in each piece of generated text (by the decoder and the server's scan),
|
||||
* so per-token cost is count x longest. OpenAI allows four; the bounds are far above that
|
||||
|
||||
@@ -94,6 +94,11 @@ struct OaiRequest {
|
||||
|
||||
int n = 1;
|
||||
SamplingParams sp;
|
||||
/* The experimental speed projection, tri-state: -1 the request named no choice (the engine's
|
||||
* runtime default applies), 0 it asked for the stock model, 1 for the loaded control vector.
|
||||
* Parsed from `control_vector` (or Strata's `experimental_speed_projection`); a deployment
|
||||
* with no vector refuses 1 rather than silently serving the stock model. */
|
||||
int control_vector = -1;
|
||||
int32_t max_tokens = 0;
|
||||
std::vector<std::string> stop;
|
||||
/* How many of `stop` the client sent; the rest are the chat format's end-of-turn markers.
|
||||
|
||||
@@ -204,6 +204,12 @@ void Server::register_routes() {
|
||||
router_.get ("/stats", [this](const HttpRequest& q) { return handle_stats(q); });
|
||||
router_.get ("/graph", [this](const HttpRequest& q) { return handle_graph(q); });
|
||||
router_.get ("/sessions", [this](const HttpRequest& q) { return handle_sessions(q); });
|
||||
/* The control vector's switch, for the dashboard's Model view and for automation: a
|
||||
* deployment with no vector found answers 404 here. */
|
||||
router_.get ("/admin/control-vector",
|
||||
[this](const HttpRequest& q) { return handle_control_vector(q); });
|
||||
router_.post("/admin/control-vector",
|
||||
[this](const HttpRequest& q) { return handle_control_vector(q); });
|
||||
}
|
||||
|
||||
/* Workers beyond the admission ceiling. A generation holds its connection's worker for its whole
|
||||
@@ -460,6 +466,25 @@ int Server::submit_all(const OaiRequest& r, std::vector<Generation>* gens, ApiEr
|
||||
}
|
||||
}
|
||||
|
||||
/* The control vector: the request's own choice, else the deployment's runtime default. A
|
||||
* deployment with no vector refuses the on state by name -- serving the stock model while the
|
||||
* client believes it asked for the vector would be a silent lie about what produced the text.
|
||||
* (Naming: `control_vector` is the field, `experimental_speed_projection` its alias.) */
|
||||
bool projection_on = false;
|
||||
{
|
||||
const bool named = r.control_vector >= 0;
|
||||
projection_on = named
|
||||
? (r.control_vector == 1)
|
||||
: (deps_.control_vector.get_default && deps_.control_vector.get_default());
|
||||
if (projection_on &&
|
||||
!(deps_.control_vector.available && deps_.control_vector.available())) {
|
||||
*err = err_bad("control_vector",
|
||||
"this deployment found no control vector for its model; the stock model "
|
||||
"is what it serves");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
}
|
||||
|
||||
int idx = 0;
|
||||
std::vector<Request> rqs;
|
||||
rqs.reserve(r.prompts.size() * (size_t)r.n);
|
||||
@@ -482,6 +507,7 @@ int Server::submit_all(const OaiRequest& r, std::vector<Generation>* gens, ApiEr
|
||||
Request rq;
|
||||
rq.prompt = r.prompts[p].tokens;
|
||||
rq.sp = r.sp;
|
||||
rq.sp.use_control_vector = projection_on;
|
||||
/* Distinct seeds per choice, or `n` identical completions. An offset suffices: the
|
||||
* sampler's stream is seeded, not chained. */
|
||||
rq.sp.seed = r.sp.seed + (uint64_t)k;
|
||||
|
||||
@@ -48,6 +48,19 @@ struct Deps {
|
||||
* (liveview.h), so the terminal and the web page agree. Absent, the dashboard shows only the
|
||||
* scheduler's metrics and omits the cards. */
|
||||
LiveView::Source live;
|
||||
|
||||
/* The control vector (core/cvec.h), as the server sees it: whether the engine found one for
|
||||
* this container, the runtime default for requests that name no choice, and the switch the
|
||||
* admin endpoint flips. Absent (the function members null) means the feature does not exist on
|
||||
* this deployment: a request that asks for it is refused by name, and the dashboard shows no
|
||||
* control. Nothing here reloads the model. */
|
||||
struct ControlVectorSwitch {
|
||||
std::function<bool()> available;
|
||||
std::function<bool()> get_default;
|
||||
std::function<void(bool)> set_default;
|
||||
std::function<std::string()> json;
|
||||
};
|
||||
ControlVectorSwitch control_vector;
|
||||
};
|
||||
|
||||
struct ServerOptions {
|
||||
@@ -312,6 +325,10 @@ private:
|
||||
HttpResponse handle_stats(const HttpRequest& req);
|
||||
HttpResponse handle_graph(const HttpRequest& req);
|
||||
HttpResponse handle_sessions(const HttpRequest& req);
|
||||
/* The control vector's runtime switch (dashboard.cpp): GET reports the state, POST
|
||||
* {"enabled": bool} sets the default for requests that name no choice. Guarded by the API key
|
||||
* like every other route but the health probes and the page. */
|
||||
HttpResponse handle_control_vector(const HttpRequest& req);
|
||||
|
||||
HttpResponse error_response(const ApiError& e, const std::string& peer,
|
||||
ApiShape shape = ApiShape::OpenAI);
|
||||
|
||||
Reference in new issue
Block a user