Archived
radiance 2.0.0
Radiance becomes a multi-model engine: one process serves several models behind one port, loads each when a request names it, parks it in host memory or restores it from its container, and shares the cards between them (docs/MULTIMODEL.md). The configuration is one YAML file; the command line flags are gone (`radiance --config FILE`, hot reload on SIGHUP). Formats. IQ8R (rotated int8) and IQR-V4 replace FP8 and MXFP4 as whole-model formats, with an int8 KV cache (`kv-cache-dtype: i8`) in place of the fp8 one. Containers whose weights are block FP8, MXFP4, M2XFP4, NVFP4 or ParoQuant are refused at startup by the name of the first such weight. Not served until they have int8 containers: GLM-5.3-Flash, Ornith-1.5, Sage-A1.5, Nemotron-3.5-Bolt-74B, Ling-3.0-flash, drama-3 and Anima's FP8 container; Nemotron-3.5-Bolt-E4B, the Gemma 4 31B DFlash containers and MiniCPM5-2B bf16 serve without their drafter. 1.17.0 serves all of them. Speed. Chained launches for Qwen3.8-27B, Gemma 4, Gemma 3 and the small bf16 models, a new Qwen3.8-Flash-Next container (IQR-V4 experts, int8 trunk) with its decode step and prefill reworked, and a reworked adaptive draft window. Deployment. deploy/compose has two fleet files, tp1.yaml and tp2.yaml, each with its configuration beside it, in place of the per-model files. `host-pool-mib` is a model's host pool in all, split among its ranks. The dashboard is rebuilt: an hour of history kept by the server, each accelerator's memory by model, model states over time. Merged since 1.17.0: Nemotron-3.5-Bolt-74B-A9B by keptn42 (#339); GLM-5.3-Flash on four cards by AnimusNull (#397); chained launches for Flash-Next's decode step (#396) and preempted requests keeping their prefix and place (#405) by mici81; --prefill-interleave auto by Code000 (#410); eight more libr3d kernels and the w4a4 reference by RikshaDriver (#403, #407, #408); libr3d large-BAR and E4M3 decode fixes by neuromanMD (#438, #439); int4/int5 decode tiles by Kezi (#426); attention for Nanbeige 4.3 33B by everplays (#417); expert_stats_report.py by baKon (#431). Fixed: #398, #402, #419, #437 (dschmitz's patch); added the metrics of #392, and multi-model serving as asked for in #367 and #109. Plugin ABI 39: out-of-tree plugins need to be rebuilt. Thanks to @keptn42, @AnimusNull, @mici81, @Code000, @RikshaDriver, @neuromanMD, @Kezi, @everplays, @baKon and @dschmitz.
This commit is contained in:
1 parent
b4c04b6042
commit
ddcb9c69bb
696 files changed
+120770
-56596
No files matched your search
@@ -0,0 +1,74 @@
|
||||
/* fleet/broker.h -- how an engine asks the fleet for its part of the cards (docs/MULTIMODEL.md §6.3).
|
||||
*
|
||||
* An engine in a fleet of several models does not split a card by itself: the other models hold
|
||||
* part of it, and the fleet decides who leaves to make room. Once declare has told the engine what
|
||||
* it costs, the engine asks its broker for a claim between what it cannot start without and what it
|
||||
* could use at most; the broker makes room (evicting by the fleet's policy) and grants a claim, and
|
||||
* the engine splits that claim as a one-model process splits its card (Config::vram_claim).
|
||||
*/
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rad {
|
||||
|
||||
struct VramAsk {
|
||||
std::vector<int> cards; /* the engine's cards, rank i on cards[i]; the claim is per card */
|
||||
int64_t need = 0; /* the fixed part: weights, arena, queues, one full-length context */
|
||||
int64_t want = 0; /* the most the engine can use: every expert, the cache's ceiling */
|
||||
/* The cache's terms, so a broker can price a token count: kv_fixed is state no context uses,
|
||||
* (kv_ceiling - kv_fixed) pages max_seqs x max_ctx tokens, and kv_one is the cache's part of
|
||||
* `need` (kv_fixed and the pages of one full-length context). */
|
||||
int64_t kv_fixed = 0;
|
||||
int64_t kv_ceiling = 0;
|
||||
int64_t kv_one = 0;
|
||||
int64_t max_tokens = 0; /* max_seqs x max_ctx */
|
||||
int64_t experts = 0; /* the whole expert plane, 0 for a dense model */
|
||||
int64_t expert_floor = 0; /* the experts the host pool cannot hold */
|
||||
};
|
||||
|
||||
class VramBroker {
|
||||
public:
|
||||
virtual ~VramBroker() = default;
|
||||
/* Grants a claim per card of at least `ask.need`, blocking until it is free on every card of
|
||||
* the ask. The claim is at most `want` unless the model's configuration fixes it. RAD_OK with
|
||||
* *grant set, or an error with *why naming what is in the way (models that may not leave, a
|
||||
* card too small). */
|
||||
virtual int grant(const VramAsk& ask, int64_t* grant, std::string* why) = 0;
|
||||
|
||||
/* Room for `bytes` of host memory the engine will pin beyond the pools the fleet already counted
|
||||
* (the tables placed in host memory), made and waited for as for a load's pools, and counted
|
||||
* from here until the load ends. False when it cannot be had, and nothing is counted. */
|
||||
virtual bool host_room(int64_t bytes) { (void)bytes; return true; }
|
||||
/* All that is counted for this load, about to be pinned, once more. It was there when counted,
|
||||
* and the load may have waited minutes for its VRAM since. False when it cannot be had: pinning
|
||||
* anyway gets the process killed by the kernel. */
|
||||
virtual bool host_pools_room() { return true; }
|
||||
/* Host memory this engine must leave free beside what it pins: the host's floor and what other
|
||||
* loads have counted on and not pinned yet. */
|
||||
virtual int64_t host_margin() { return 0; }
|
||||
|
||||
/* ---- flex (docs/MULTIMODEL.md §6). A granted engine's cache may grow past its claim into
|
||||
* memory no model holds, and gives it back when the fleet needs it. */
|
||||
/* For the engine being granted: the most tokens its cache may grow to, 0 for none (it stays at
|
||||
* its claim), -1 for its ceiling. */
|
||||
virtual int64_t flex_kv_max_tokens() { return 0; }
|
||||
/* May `bytes` more be backed on each of `cards` now? Never evicts and never blocks; refused
|
||||
* while the fleet is making room for a load. From an engine's step loop. */
|
||||
virtual bool flex_grow(const std::vector<int>& cards, int64_t bytes) {
|
||||
(void)cards; (void)bytes; return false;
|
||||
}
|
||||
/* After every flex_grow that said yes, once the engine has backed the bytes or given up: a load
|
||||
* or restore starting meanwhile waits for granted growth to land before it reads the cards'
|
||||
* free memory, or it would plan room that the growth then takes. */
|
||||
virtual void flex_done() {}
|
||||
/* The bytes a cache on `cards` could still grow by now, for admission (KVManager::set_flex_room). */
|
||||
virtual int64_t flex_room(const std::vector<int>& cards) { (void)cards; return 0; }
|
||||
/* For the engine being granted: the most VRAM its experts may take on a card, the claim's
|
||||
* share included, as its slab grows into memory no model holds (docs/MULTIMODEL.md §6.7); 0
|
||||
* for no growth, -1 for every expert it keeps in host memory. */
|
||||
virtual int64_t flex_experts_max_bytes() { return 0; }
|
||||
};
|
||||
|
||||
} // namespace rad
|
||||
@@ -0,0 +1,971 @@
|
||||
/* fleet/config.cpp -- reading the YAML file (fleet/config.h, docs/MULTIMODEL.md §4).
|
||||
*
|
||||
* The file is parsed into a tree with locations, anchors and merge keys resolved, then walked level
|
||||
* by level. Every scalar keeps its source text, so `temp: 1.0` reaches config_parse as "1.0" and is
|
||||
* judged by the same rules as the flag was. Errors name the file and line; the first one stops the
|
||||
* load, since a later error after a misread key would only mislead.
|
||||
*/
|
||||
#include "fleet/config.h"
|
||||
#include "util/json.h"
|
||||
|
||||
#include <ryml.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace rad::fleet {
|
||||
|
||||
namespace {
|
||||
|
||||
/* ------------------------------------------------------------------ levels */
|
||||
|
||||
/* Options that configure the process rather than a model. They travel in every engine's flag list
|
||||
* while one process holds one listener (§5.1), so each engine reads the same values. */
|
||||
const char* const kServerKeys[] = {
|
||||
"host", "port", "api-key", "api-key-env", "allow-host", "trusted-proxy", "http-threads",
|
||||
"read-timeout", "write-timeout", "keep-alive-timeout", "max-body-mib", "retry-after", "no-cors",
|
||||
"live", "radiance-home", "kernels", "download-dir", "proceed-with-incompatible-configuration",
|
||||
"debug-accept-reference-kernels",
|
||||
};
|
||||
|
||||
/* The memory level's options: the per-card pair (gpu-memory-mib, gpu-headroom-mib) and the host
|
||||
* headroom. Under memory: they apply to every model; a model that states either half of the card
|
||||
* pair itself replaces the memory level's pair, which is how models sharing a card divide it. */
|
||||
const char* const kMemoryFlagKeys[] = { "gpu-headroom-mib", "gpu-memory-mib", "host-headroom-mib" };
|
||||
|
||||
/* Options with no YAML key: the switches the YAML spells differently, and help. */
|
||||
struct Replaced { const char* flag; const char* instead; };
|
||||
const Replaced kReplaced[] = {
|
||||
{ "served-model-name", "the entry's name under models: is the model id; add more with aliases:" },
|
||||
{ "help", "run radiance --help for the key reference" },
|
||||
{ "v", "set server.log: debug" },
|
||||
{ "vv", "set server.log: trace" },
|
||||
};
|
||||
|
||||
/* Flags given more than once on a command line; a YAML sequence repeats them. */
|
||||
const char* const kRepeatable[] = { "lora", "allow-host", "trusted-proxy" };
|
||||
|
||||
/* The fleet's own keys on a model. `defaults:` may carry every one but aliases (read_model_keys). */
|
||||
const char* const kModelFleetKeys[] = { "aliases", "priority", "pin", "ttl", "flex", "park" };
|
||||
|
||||
const char* const kTopKeys[] = { "server", "cards", "memory", "scheduler", "defaults", "models", "groups" };
|
||||
|
||||
template <size_t N>
|
||||
bool in(const char* const (&set)[N], const std::string& k) {
|
||||
for (const char* s : set)
|
||||
if (k == s) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
std::vector<std::string> as_vector(const char* const (&set)[N]) {
|
||||
return std::vector<std::string>(std::begin(set), std::end(set));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ errors */
|
||||
|
||||
/* ryml reports through callbacks that must not return. A throw carries the message out to the
|
||||
* single catch in parse(); nothing else in this file throws. */
|
||||
struct YamlError : std::runtime_error {
|
||||
size_t line;
|
||||
YamlError(const std::string& m, size_t l) : std::runtime_error(m), line(l) {}
|
||||
};
|
||||
|
||||
[[noreturn]] void on_basic(ryml::csubstr msg, ryml::ErrorDataBasic const&, void*) {
|
||||
throw YamlError(std::string(msg.str, msg.len), ryml::npos);
|
||||
}
|
||||
[[noreturn]] void on_parse(ryml::csubstr msg, ryml::ErrorDataParse const& e, void*) {
|
||||
throw YamlError(std::string(msg.str, msg.len), e.ymlloc.line);
|
||||
}
|
||||
[[noreturn]] void on_visit(ryml::csubstr msg, ryml::ErrorDataVisit const&, void*) {
|
||||
throw YamlError(std::string(msg.str, msg.len), ryml::npos);
|
||||
}
|
||||
|
||||
std::string str(ryml::csubstr s) { return std::string(s.str, s.len); }
|
||||
|
||||
size_t edit_distance(const std::string& a, const std::string& b) {
|
||||
std::vector<size_t> row(b.size() + 1);
|
||||
for (size_t j = 0; j <= b.size(); ++j) row[j] = j;
|
||||
for (size_t i = 1; i <= a.size(); ++i) {
|
||||
size_t diag = row[0];
|
||||
row[0] = i;
|
||||
for (size_t j = 1; j <= b.size(); ++j) {
|
||||
const size_t up = row[j];
|
||||
row[j] = std::min({ row[j] + 1, row[j - 1] + 1, diag + (a[i - 1] != b[j - 1]) });
|
||||
diag = up;
|
||||
}
|
||||
}
|
||||
return row[b.size()];
|
||||
}
|
||||
|
||||
/* " (did you mean X?)" when a candidate is within a third of the key's length, else "". */
|
||||
std::string suggest(const std::string& key, const std::vector<std::string>& candidates) {
|
||||
std::string best;
|
||||
size_t best_d = SIZE_MAX;
|
||||
for (const std::string& c : candidates) {
|
||||
const size_t d = edit_distance(key, c);
|
||||
if (d < best_d) { best_d = d; best = c; }
|
||||
}
|
||||
if (best.empty() || best_d > std::max<size_t>(2, key.size() / 3)) return "";
|
||||
return " (did you mean " + best + "?)";
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the walk */
|
||||
|
||||
class Reader {
|
||||
public:
|
||||
Reader(const std::string& origin, ryml::Parser& parser, const ryml::Tree& tree)
|
||||
: origin_(origin), parser_(parser), tree_(tree) {
|
||||
size_t n = 0;
|
||||
const ConfigFlag* f = config_flags(&n);
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const char* name = f[i].name;
|
||||
while (*name == '-') ++name;
|
||||
flags_[name] = &f[i];
|
||||
}
|
||||
}
|
||||
|
||||
int run(FleetConfig* out);
|
||||
|
||||
private:
|
||||
using Node = ryml::ConstNodeRef;
|
||||
|
||||
/* A key's node, the key itself, and the YAML path it was read from, e.g. "models.qwen.tp".
|
||||
* The key is kept on its own because a model id may hold dots. */
|
||||
struct Item { Node node; std::string path; std::string key; };
|
||||
|
||||
const std::string& origin_;
|
||||
ryml::Parser& parser_;
|
||||
const ryml::Tree& tree_;
|
||||
std::map<std::string, const ConfigFlag*> flags_;
|
||||
|
||||
std::vector<std::string> shared_args_; /* server flags, in file order */
|
||||
std::vector<std::string> memory_args_; /* memory flags, in file order */
|
||||
std::map<std::string, std::string> shared_given_; /* flag (with dashes) -> YAML path */
|
||||
|
||||
std::string where(Node n) const {
|
||||
const ryml::Location loc = n.location(parser_);
|
||||
if (loc.line == ryml::npos) return origin_;
|
||||
return origin_ + ":" + std::to_string(loc.line + 1);
|
||||
}
|
||||
|
||||
int fail(Node n, const std::string& msg) const {
|
||||
RAD_ERR("%s: %s", where(n).c_str(), msg.c_str());
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* The map's children in order, refusing a key given twice; YAML parsers keep both silently. */
|
||||
int children(Node map, const std::string& path, std::vector<Item>* out) const {
|
||||
if (!map.is_map()) return fail(map, (path.empty() ? std::string("the file") : path) + " must be a mapping of keys");
|
||||
std::set<std::string> seen;
|
||||
for (Node c : map.children()) {
|
||||
const std::string k = str(c.key());
|
||||
if (!seen.insert(k).second)
|
||||
return fail(c, (path.empty() ? k : path + "." + k) + " is given twice");
|
||||
out->push_back({ c, path.empty() ? k : path + "." + k, k });
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
bool scalar(Node n) const { return n.has_val() && !n.is_map() && !n.is_seq(); }
|
||||
bool null_val(Node n) const { return scalar(n) && !n.is_val_quoted() && (n.val_is_null() || n.val().empty()); }
|
||||
|
||||
int scalar_text(const Item& it, std::string* out) const {
|
||||
if (!scalar(it.node)) return fail(it.node, it.path + " takes a single value");
|
||||
if (null_val(it.node)) return fail(it.node, it.path + " needs a value");
|
||||
*out = str(it.node.val());
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int boolean(const Item& it, bool* out) const {
|
||||
std::string v;
|
||||
if (int s = scalar_text(it, &v); s < 0) return s;
|
||||
if (v == "true" || v == "True" || v == "TRUE") { *out = true; return RAD_OK; }
|
||||
if (v == "false" || v == "False" || v == "FALSE") { *out = false; return RAD_OK; }
|
||||
return fail(it.node, it.path + " takes true or false, not '" + v + "'");
|
||||
}
|
||||
|
||||
int integer(const Item& it, int64_t lo, int64_t hi, int64_t* out) const {
|
||||
std::string v;
|
||||
if (int s = scalar_text(it, &v); s < 0) return s;
|
||||
char* end = nullptr;
|
||||
errno = 0;
|
||||
const long long n = std::strtoll(v.c_str(), &end, 10);
|
||||
if (v.empty() || *end || errno == ERANGE)
|
||||
return fail(it.node, it.path + " takes a whole number, not '" + v + "'");
|
||||
if (n < lo || n > hi)
|
||||
return fail(it.node, it.path + " must be in [" + std::to_string(lo) + ", " + std::to_string(hi) +
|
||||
"], not " + v);
|
||||
*out = n;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int number(const Item& it, double* out) const {
|
||||
std::string v;
|
||||
if (int s = scalar_text(it, &v); s < 0) return s;
|
||||
char* end = nullptr;
|
||||
errno = 0;
|
||||
const double d = std::strtod(v.c_str(), &end);
|
||||
if (v.empty() || *end || errno == ERANGE) return fail(it.node, it.path + " takes a number, not '" + v + "'");
|
||||
*out = d;
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int duration(const Item& it, int64_t* out_ms) const {
|
||||
std::string v;
|
||||
if (int s = scalar_text(it, &v); s < 0) return s;
|
||||
if (!parse_duration_ms(v, out_ms))
|
||||
return fail(it.node, it.path + " takes a duration such as 250ms, 30s, 30m or 2h, not '" + v + "'");
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* A YAML value as JSON: mappings and sequences recursively, plain scalars typed the way YAML's
|
||||
* core schema types them (true/false/null/numbers), everything quoted as a string. */
|
||||
rad::Json to_json(Node n) const {
|
||||
if (n.is_map()) {
|
||||
rad::Json j = rad::Json::object();
|
||||
for (Node c : n.children()) j[str(c.key())] = to_json(c);
|
||||
return j;
|
||||
}
|
||||
if (n.is_seq()) {
|
||||
rad::Json j = rad::Json::array();
|
||||
for (Node c : n.children()) j.push_back(to_json(c));
|
||||
return j;
|
||||
}
|
||||
const std::string v = str(n.val());
|
||||
if (n.is_val_quoted()) return v;
|
||||
if (n.val_is_null() || v.empty() || v == "~" || v == "null") return nullptr;
|
||||
if (v == "true") return true;
|
||||
if (v == "false") return false;
|
||||
char* end = nullptr;
|
||||
errno = 0;
|
||||
const long long i = std::strtoll(v.c_str(), &end, 10);
|
||||
if (!*end && errno != ERANGE) return i;
|
||||
const double d = std::strtod(v.c_str(), &end);
|
||||
if (!*end && errno != ERANGE) return d;
|
||||
return v;
|
||||
}
|
||||
|
||||
/* `c`, an entry of a sequence value or a member of a mapping one, as flag text: the entry
|
||||
* itself, or KEY=VALUE. `what` names the children in the refusal. */
|
||||
int child_text(const Item& it, Node c, const char* what, std::string* out) const {
|
||||
if (!scalar(c) || null_val(c)) return fail(c, it.path + " " + what + " must be single values");
|
||||
*out = it.node.is_map() ? str(c.key()) + "=" + str(c.val()) : str(c.val());
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* One option key as flag arguments, appended to *out. The placeholder in the flag table says
|
||||
* how a structured value is spelled: LIST joins a sequence with commas, MAP joins a mapping as
|
||||
* KEY=VALUE pairs, JSON takes a mapping or sequence as JSON text, and repeatable flags repeat. */
|
||||
int flag_args(const Item& it, std::vector<std::string>* out) const {
|
||||
const ConfigFlag* f = flags_.at(it.key);
|
||||
const std::string flag = f->name;
|
||||
const std::string arg = f->arg ? f->arg : "";
|
||||
const bool structured = it.node.is_seq() || it.node.is_map();
|
||||
if (!f->arg) {
|
||||
bool on = false;
|
||||
if (int s = boolean(it, &on); s < 0) return s;
|
||||
if (on) out->push_back(flag);
|
||||
return RAD_OK;
|
||||
}
|
||||
if (in(kRepeatable, it.key) && structured) {
|
||||
for (Node c : it.node.children()) {
|
||||
std::string v;
|
||||
if (int s = child_text(it, c, "entries", &v); s < 0) return s;
|
||||
out->push_back(flag);
|
||||
out->push_back(v);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
std::string v;
|
||||
if (arg == "JSON" && structured) {
|
||||
v = to_json(it.node).dump();
|
||||
} else if ((arg == "LIST" && it.node.is_seq()) || (arg == "MAP" && it.node.is_map())) {
|
||||
for (Node c : it.node.children()) {
|
||||
std::string one;
|
||||
if (int s = child_text(it, c, arg == "MAP" ? "values" : "entries", &one); s < 0) return s;
|
||||
v += (v.empty() ? "" : ",") + one;
|
||||
}
|
||||
} else if (int s = scalar_text(it, &v); s < 0) {
|
||||
return s;
|
||||
}
|
||||
out->push_back(flag);
|
||||
out->push_back(v);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Where a key that is not valid here belongs, or why it does not exist. */
|
||||
std::string misplaced(const std::string& key, const std::string& level) const {
|
||||
for (const Replaced& r : kReplaced)
|
||||
if (key == r.flag) return key + " is not a key: " + r.instead;
|
||||
if (level != "server" && (in(kServerKeys, key) || key == "log")) return key + " belongs under server:";
|
||||
if (level != "memory" && level != "model" && in(kMemoryFlagKeys, key))
|
||||
return key + " belongs under memory: or a model";
|
||||
if (level != "memory" && (key == "host-mib" || key == "disk-tier"))
|
||||
return key + " belongs under memory:";
|
||||
if (level != "model" && flags_.count(key))
|
||||
return key + " is a per-model key; put it under the model or defaults:";
|
||||
return "";
|
||||
}
|
||||
|
||||
/* A key `level` does not take: where it belongs, else "PATH is not a NOUN key" with the
|
||||
* closest of the keys the level does take. */
|
||||
int refuse_key(const Item& it, const std::string& level, const std::string& noun,
|
||||
const std::vector<std::string>& valid) const {
|
||||
std::string m = misplaced(it.key, level);
|
||||
if (m.empty()) m = it.path + " is not a " + noun + " key" + suggest(it.key, valid);
|
||||
return fail(it.node, m);
|
||||
}
|
||||
|
||||
std::vector<std::string> model_key_names() const {
|
||||
std::vector<std::string> v;
|
||||
for (const auto& [k, f] : flags_)
|
||||
if (misplaced(k, "model").empty()) v.push_back(k);
|
||||
for (const char* k : kModelFleetKeys) v.push_back(k);
|
||||
return v;
|
||||
}
|
||||
|
||||
int read_server(const Item& sec, FleetConfig* out);
|
||||
int read_memory(const Item& sec, FleetConfig* out);
|
||||
int read_scheduler(const Item& sec, FleetConfig* out);
|
||||
int read_cards(const Item& sec, FleetConfig* out);
|
||||
int read_groups(const Item& sec, FleetConfig* out);
|
||||
int read_model_keys(const Item& sec, bool is_defaults, std::vector<Item>* out);
|
||||
int read_fleet_key(const Item& it, ModelEntry* e);
|
||||
int build_model(const std::string& name, Node at, const std::vector<Item>& keys, ModelEntry* e);
|
||||
};
|
||||
|
||||
int Reader::read_server(const Item& sec, FleetConfig* out) {
|
||||
std::vector<Item> items;
|
||||
if (int s = children(sec.node, sec.path, &items); s < 0) return s;
|
||||
for (const Item& it : items) {
|
||||
const std::string& k = it.key;
|
||||
if (k == "log") {
|
||||
std::string v;
|
||||
if (int s = scalar_text(it, &v); s < 0) return s;
|
||||
if (v == "info") out->log = Log::Info;
|
||||
else if (v == "debug") out->log = Log::Debug;
|
||||
else if (v == "trace") out->log = Log::Trace;
|
||||
else return fail(it.node, "server.log takes info, debug or trace, not '" + v + "'");
|
||||
out->log_set = true;
|
||||
continue;
|
||||
}
|
||||
if (!in(kServerKeys, k)) {
|
||||
std::vector<std::string> valid = as_vector(kServerKeys);
|
||||
valid.push_back("log");
|
||||
return refuse_key(it, "server", "server", valid);
|
||||
}
|
||||
if (int s = flag_args(it, &shared_args_); s < 0) return s;
|
||||
shared_given_[flags_.at(k)->name] = it.path;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Reader::read_memory(const Item& sec, FleetConfig* out) {
|
||||
std::vector<Item> items;
|
||||
if (int s = children(sec.node, sec.path, &items); s < 0) return s;
|
||||
for (const Item& it : items) {
|
||||
const std::string& k = it.key;
|
||||
if (k == "host-mib") {
|
||||
if (int s = integer(it, 1, INT64_MAX / (1 << 20), &out->host_mib); s < 0) return s;
|
||||
continue;
|
||||
}
|
||||
if (k == "disk-tier") {
|
||||
if (int s = scalar_text(it, &out->disk_tier); s < 0) return s;
|
||||
continue;
|
||||
}
|
||||
if (!in(kMemoryFlagKeys, k)) {
|
||||
std::vector<std::string> valid = as_vector(kMemoryFlagKeys);
|
||||
valid.push_back("host-mib");
|
||||
valid.push_back("disk-tier");
|
||||
return refuse_key(it, "memory", "memory", valid);
|
||||
}
|
||||
if (int s = flag_args(it, &memory_args_); s < 0) return s;
|
||||
shared_given_[flags_.at(k)->name] = it.path;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Reader::read_scheduler(const Item& sec, FleetConfig* out) {
|
||||
std::vector<Item> items;
|
||||
if (int s = children(sec.node, sec.path, &items); s < 0) return s;
|
||||
for (const Item& it : items) {
|
||||
const std::string& k = it.key;
|
||||
int s = RAD_OK;
|
||||
if (k == "load-timeout") s = duration(it, &out->load_timeout_ms);
|
||||
else if (k == "drain-timeout") s = duration(it, &out->drain_timeout_ms);
|
||||
else if (k == "keepalive") s = duration(it, &out->keepalive_ms);
|
||||
else if (k == "warm-up") s = boolean(it, &out->warm_up);
|
||||
else {
|
||||
return fail(it.node, "scheduler." + k + " is not a scheduler key" +
|
||||
suggest(k, { "load-timeout", "drain-timeout", "keepalive", "warm-up" }));
|
||||
}
|
||||
if (s < 0) return s;
|
||||
}
|
||||
if (out->keepalive_ms <= 0) return fail(sec.node, "scheduler.keepalive must be above zero");
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Reader::read_cards(const Item& sec, FleetConfig* out) {
|
||||
if (scalar(sec.node) && str(sec.node.val()) == "all") return RAD_OK;
|
||||
if (!sec.node.is_seq() || sec.node.num_children() == 0)
|
||||
return fail(sec.node, "cards takes all or a list of card indices");
|
||||
std::set<int64_t> seen;
|
||||
for (Node c : sec.node.children()) {
|
||||
int64_t idx = 0;
|
||||
if (int s = integer({ c, sec.path, "cards" }, 0, 63, &idx); s < 0) return s;
|
||||
if (!seen.insert(idx).second) return fail(c, "card " + std::to_string(idx) + " is listed twice");
|
||||
out->cards.push_back((int)idx);
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Reader::read_groups(const Item& sec, FleetConfig* out) {
|
||||
std::vector<Item> items;
|
||||
if (int s = children(sec.node, sec.path, &items); s < 0) return s;
|
||||
for (const Item& it : items) {
|
||||
std::vector<Item> kinds;
|
||||
if (int s = children(it.node, it.path, &kinds); s < 0) return s;
|
||||
if (kinds.size() != 1)
|
||||
return fail(it.node, it.path + " takes exactly one of together: or exclusive:");
|
||||
const Item& k = kinds[0];
|
||||
Group g;
|
||||
g.name = it.key;
|
||||
const std::string& kind = k.key;
|
||||
if (kind == "together") g.kind = Group::Kind::Together;
|
||||
else if (kind == "exclusive") g.kind = Group::Kind::Exclusive;
|
||||
else return fail(k.node, k.path + ": a group is together: or exclusive:" + suggest(kind, { "together", "exclusive" }));
|
||||
if (!k.node.is_seq() || k.node.num_children() < 2)
|
||||
return fail(k.node, k.path + " takes a list of at least two models");
|
||||
for (Node m : k.node.children()) {
|
||||
if (!scalar(m) || null_val(m)) return fail(m, k.path + " entries are model names");
|
||||
const std::string name = str(m.val());
|
||||
if (std::find(g.members.begin(), g.members.end(), name) != g.members.end())
|
||||
return fail(m, k.path + " lists " + name + " twice");
|
||||
g.members.push_back(name);
|
||||
}
|
||||
out->groups.push_back(std::move(g));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* A model's (or the defaults') keys, checked against the levels; option values are read later. */
|
||||
int Reader::read_model_keys(const Item& sec, bool is_defaults, std::vector<Item>* out) {
|
||||
if (int s = children(sec.node, sec.path, out); s < 0) return s;
|
||||
for (const Item& it : *out) {
|
||||
const std::string& k = it.key;
|
||||
if (in(kModelFleetKeys, k)) {
|
||||
if (is_defaults && k == "aliases") return fail(it.node, "aliases name one model; they cannot be a default");
|
||||
continue;
|
||||
}
|
||||
std::string m = misplaced(k, "model");
|
||||
if (!m.empty()) return fail(it.node, m);
|
||||
if (!flags_.count(k)) return fail(it.node, it.path + " is not a key" + suggest(k, model_key_names()));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Reader::read_fleet_key(const Item& it, ModelEntry* e) {
|
||||
const std::string& k = it.key;
|
||||
if (k == "aliases") {
|
||||
std::vector<Node> names;
|
||||
if (scalar(it.node)) names.push_back(it.node);
|
||||
else if (it.node.is_seq()) for (Node c : it.node.children()) names.push_back(c);
|
||||
else return fail(it.node, it.path + " takes a name or a list of names");
|
||||
for (Node n : names) {
|
||||
if (!scalar(n) || null_val(n)) return fail(n, it.path + " entries are names");
|
||||
e->aliases.push_back(str(n.val()));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
if (k == "priority") {
|
||||
int64_t p = 0;
|
||||
if (int s = integer(it, -1000000, 1000000, &p); s < 0) return s;
|
||||
e->priority = (int)p;
|
||||
return RAD_OK;
|
||||
}
|
||||
if (k == "pin") return boolean(it, &e->pin);
|
||||
if (k == "ttl") return duration(it, &e->ttl_ms);
|
||||
if (k == "flex") {
|
||||
std::vector<Item> parts;
|
||||
if (int s = children(it.node, it.path, &parts); s < 0) return s;
|
||||
for (const Item& p : parts) {
|
||||
const std::string& pk = p.key;
|
||||
std::vector<Item> fields;
|
||||
if (pk != "kv" && pk != "experts")
|
||||
return fail(p.node, p.path + ": flex has kv: and experts:" + suggest(pk, { "kv", "experts" }));
|
||||
if (int s = children(p.node, p.path, &fields); s < 0) return s;
|
||||
for (const Item& f : fields) {
|
||||
const std::string& fk = f.key;
|
||||
int s = RAD_OK;
|
||||
if (pk == "kv" && fk == "min-tokens") s = integer(f, 1, INT64_MAX, &e->flex.kv_min_tokens);
|
||||
else if (pk == "kv" && fk == "max-tokens") s = integer(f, 1, INT64_MAX, &e->flex.kv_max_tokens);
|
||||
else if (pk == "kv" && fk == "weight") {
|
||||
if ((s = number(f, &e->flex.kv_weight)) == RAD_OK && !(e->flex.kv_weight > 0))
|
||||
return fail(f.node, f.path + " must be above zero");
|
||||
}
|
||||
else if (pk == "experts" && fk == "min-mib") s = integer(f, 0, INT64_MAX, &e->flex.experts_min_mib);
|
||||
else if (pk == "experts" && fk == "max-mib") s = integer(f, 0, INT64_MAX, &e->flex.experts_max_mib);
|
||||
else {
|
||||
const std::vector<std::string> c = pk == "kv"
|
||||
? std::vector<std::string>{ "min-tokens", "max-tokens", "weight" }
|
||||
: std::vector<std::string>{ "min-mib", "max-mib" };
|
||||
return fail(f.node, f.path + " is not a key" + suggest(fk, c));
|
||||
}
|
||||
if (s < 0) return s;
|
||||
}
|
||||
}
|
||||
if (e->flex.kv_min_tokens > 0 && e->flex.kv_max_tokens > 0 && e->flex.kv_min_tokens > e->flex.kv_max_tokens)
|
||||
return fail(it.node, it.path + ".kv: min-tokens is above max-tokens");
|
||||
if (e->flex.experts_min_mib >= 0 && e->flex.experts_max_mib > 0 && e->flex.experts_min_mib > e->flex.experts_max_mib)
|
||||
return fail(it.node, it.path + ".experts: min-mib is above max-mib");
|
||||
return RAD_OK;
|
||||
}
|
||||
if (k == "park") {
|
||||
std::vector<Item> fields;
|
||||
if (int s = children(it.node, it.path, &fields); s < 0) return s;
|
||||
for (const Item& f : fields) {
|
||||
const std::string& fk = f.key;
|
||||
std::string v;
|
||||
if (fk != "vram" && fk != "ram" && fk != "restore")
|
||||
return fail(f.node, f.path + ": park has vram:, ram: and restore:" +
|
||||
suggest(fk, { "vram", "ram", "restore" }));
|
||||
if (int s = scalar_text(f, &v); s < 0) return s;
|
||||
if (fk == "vram") {
|
||||
if (v == "none") e->park.vram = ParkSpec::Vram::None;
|
||||
else if (v == "statics") e->park.vram = ParkSpec::Vram::Statics;
|
||||
else {
|
||||
int64_t mib = 0;
|
||||
if (integer(f, 1, INT64_MAX, &mib) < 0)
|
||||
return fail(f.node, f.path + " takes none, statics or a size in MiB");
|
||||
e->park.vram = ParkSpec::Vram::Mib;
|
||||
e->park.vram_mib = mib;
|
||||
}
|
||||
} else if (fk == "restore") {
|
||||
if (v == "auto") e->park.restore = ParkSpec::Restore::Auto;
|
||||
else if (v == "full") e->park.restore = ParkSpec::Restore::Full;
|
||||
else if (v == "live") e->park.restore = ParkSpec::Restore::Live;
|
||||
else return fail(f.node, f.path + " takes auto, full or live, not '" + v + "'");
|
||||
} else {
|
||||
if (v == "required") e->park.ram = ParkSpec::Ram::Required;
|
||||
else if (v == "allowed") e->park.ram = ParkSpec::Ram::Allowed;
|
||||
else if (v == "never") e->park.ram = ParkSpec::Ram::Never;
|
||||
else return fail(f.node, f.path + " takes required, allowed or never, not '" + v + "'");
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
/* `keys` is the defaults with the model's own keys over them. */
|
||||
int Reader::build_model(const std::string& name, Node at, const std::vector<Item>& keys, ModelEntry* e) {
|
||||
e->name = name;
|
||||
std::vector<const Item*> options;
|
||||
for (const Item& it : keys) {
|
||||
if (!in(kModelFleetKeys, it.key)) options.push_back(&it);
|
||||
else if (int s = read_fleet_key(it, e); s < 0) return s;
|
||||
}
|
||||
std::sort(options.begin(), options.end(),
|
||||
[](const Item* a, const Item* b) { return a->key < b->key; });
|
||||
|
||||
/* A model stating either half of the card pair replaces the memory level's pair. */
|
||||
bool own_card = false;
|
||||
for (const Item* it : options) own_card |= it->key == "gpu-memory-mib" || it->key == "gpu-headroom-mib";
|
||||
std::vector<std::string> args = shared_args_;
|
||||
std::map<std::string, std::string> given = shared_given_;
|
||||
for (size_t i = 0; i < memory_args_.size();) {
|
||||
const std::string& f = memory_args_[i];
|
||||
const bool takes = flags_.at(f.substr(2))->arg != nullptr;
|
||||
const size_t n = takes ? 2 : 1;
|
||||
if (own_card && (f == "--gpu-memory-mib" || f == "--gpu-headroom-mib")) given.erase(f);
|
||||
else args.insert(args.end(), memory_args_.begin() + (long)i, memory_args_.begin() + (long)(i + n));
|
||||
i += n;
|
||||
}
|
||||
args.push_back("--served-model-name");
|
||||
args.push_back(name);
|
||||
given["--served-model-name"] = "models." + name;
|
||||
for (const Item* it : options) {
|
||||
if (int s = flag_args(*it, &args); s < 0) return s;
|
||||
given[flags_.at(it->key)->name] = it->path;
|
||||
}
|
||||
|
||||
std::vector<std::string> argv_store{ "radiance" };
|
||||
argv_store.insert(argv_store.end(), args.begin(), args.end());
|
||||
std::vector<char*> argv;
|
||||
for (std::string& a : argv_store) argv.push_back(a.data());
|
||||
argv.push_back(nullptr);
|
||||
Config cfg;
|
||||
if (config_parse((int)argv_store.size(), argv.data(), &cfg) != RAD_OK) {
|
||||
return fail(at, "models." + name + " refused by the line above (YAML keys are the options it names, "
|
||||
"without the leading --)");
|
||||
}
|
||||
/* config_parse records every option it read as given on the command line; they came from the
|
||||
* file, so each takes its YAML path. Options it filled from the environment keep that source. */
|
||||
for (auto& [flag, src] : cfg.given) {
|
||||
auto g = given.find(flag);
|
||||
if (g != given.end() && src == "command line") src = g->second;
|
||||
}
|
||||
e->args = std::move(args);
|
||||
e->cfg = std::move(cfg);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
bool valid_name(const std::string& n) {
|
||||
if (n.empty()) return false;
|
||||
for (unsigned char ch : n)
|
||||
if (ch == ':' || std::isspace(ch) || std::iscntrl(ch)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
int Reader::run(FleetConfig* out) {
|
||||
std::vector<Item> top;
|
||||
const Node root = tree_.crootref();
|
||||
if (root.is_stream() && root.num_children() != 1)
|
||||
return fail(root, "the file must hold exactly one YAML document");
|
||||
const Node doc = root.is_stream() ? root.first_child() : root;
|
||||
if (int s = children(doc, "", &top); s < 0) return s;
|
||||
|
||||
const Item* defaults = nullptr;
|
||||
const Item* models = nullptr;
|
||||
for (const Item& it : top) {
|
||||
const std::string& k = it.key;
|
||||
int s = RAD_OK;
|
||||
if (k == "server") s = read_server(it, out);
|
||||
else if (k == "memory") s = read_memory(it, out);
|
||||
else if (k == "scheduler") s = read_scheduler(it, out);
|
||||
else if (k == "cards") s = read_cards(it, out);
|
||||
else if (k == "groups") s = read_groups(it, out);
|
||||
else if (k == "defaults") defaults = ⁢
|
||||
else if (k == "models") models = ⁢
|
||||
else if (k.rfind("x-", 0) == 0) continue;
|
||||
else return refuse_key(it, "top", "top-level", as_vector(kTopKeys));
|
||||
if (s < 0) return s;
|
||||
}
|
||||
if (!models) return fail(doc, "no models: section; the file must name at least one model");
|
||||
|
||||
std::vector<Item> base;
|
||||
if (defaults)
|
||||
if (int s = read_model_keys(*defaults, true, &base); s < 0) return s;
|
||||
|
||||
std::vector<Item> entries;
|
||||
if (int s = children(models->node, models->path, &entries); s < 0) return s;
|
||||
if (entries.empty()) return fail(models->node, "models: names no model");
|
||||
|
||||
std::map<std::string, std::string> taken; /* name or alias -> the model it names */
|
||||
for (const Item& ent : entries) {
|
||||
const std::string& name = ent.key;
|
||||
if (!valid_name(name))
|
||||
return fail(ent.node, "'" + name + "' cannot be a model id: ids are non-empty and hold no ':' "
|
||||
"(reserved for base:adapter) and no whitespace");
|
||||
std::vector<Item> own;
|
||||
if (int s = read_model_keys(ent, false, &own); s < 0) return s;
|
||||
std::vector<Item> merged;
|
||||
for (const Item& d : base)
|
||||
if (std::none_of(own.begin(), own.end(), [&](const Item& o) { return o.key == d.key; }))
|
||||
merged.push_back(d);
|
||||
merged.insert(merged.end(), own.begin(), own.end());
|
||||
|
||||
ModelEntry e;
|
||||
if (int s = build_model(name, ent.node, merged, &e); s < 0) return s;
|
||||
std::vector<std::string> ids{ name };
|
||||
ids.insert(ids.end(), e.aliases.begin(), e.aliases.end());
|
||||
for (const std::string& n : ids) {
|
||||
if (!valid_name(n))
|
||||
return fail(ent.node, "models." + name + ": alias '" + n + "' holds ':' or whitespace");
|
||||
auto [it, fresh] = taken.emplace(n, name);
|
||||
if (!fresh)
|
||||
return fail(ent.node, "models." + name + ": '" + n + "' already names model " + it->second);
|
||||
}
|
||||
out->models.push_back(std::move(e));
|
||||
}
|
||||
|
||||
std::map<std::pair<std::string, std::string>, Group::Kind> pairs;
|
||||
for (const Group& g : out->groups) {
|
||||
for (const std::string& m : g.members)
|
||||
if (const ModelEntry* me = out->find(m); !me || me->name != m)
|
||||
return fail(doc, "groups." + g.name + " names '" + m + "', which is not a model id");
|
||||
for (size_t i = 0; i < g.members.size(); ++i)
|
||||
for (size_t j = i + 1; j < g.members.size(); ++j) {
|
||||
auto key = std::minmax(g.members[i], g.members[j]);
|
||||
auto [it, fresh] = pairs.emplace(key, g.kind);
|
||||
if (!fresh && it->second != g.kind)
|
||||
return fail(doc, "groups: " + key.first + " and " + key.second +
|
||||
" are in both a together and an exclusive group");
|
||||
}
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
const ModelEntry* FleetConfig::find(const std::string& n) const {
|
||||
for (const ModelEntry& m : models) {
|
||||
if (m.name == n) return &m;
|
||||
for (const std::string& a : m.aliases)
|
||||
if (a == n) return &m;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<std::string> level_keys(const std::string& level) {
|
||||
if (level == "server") return as_vector(kServerKeys);
|
||||
if (level == "memory") return as_vector(kMemoryFlagKeys);
|
||||
return {};
|
||||
}
|
||||
|
||||
bool parse_duration_ms(const std::string& s, int64_t* out_ms) {
|
||||
size_t i = 0;
|
||||
while (i < s.size() && std::isdigit((unsigned char)s[i])) ++i;
|
||||
if (i == 0 || i > 12) return false;
|
||||
const int64_t n = std::stoll(s.substr(0, i));
|
||||
const std::string unit = s.substr(i);
|
||||
int64_t mul = 0;
|
||||
if (unit == "ms") mul = 1;
|
||||
else if (unit == "s" || unit.empty()) mul = 1000;
|
||||
else if (unit == "m") mul = 60 * 1000;
|
||||
else if (unit == "h") mul = 3600 * 1000;
|
||||
else return false;
|
||||
*out_ms = n * mul;
|
||||
return true;
|
||||
}
|
||||
|
||||
int parse(const std::string& text, const std::string& origin, FleetConfig* out) {
|
||||
ryml::Callbacks cb;
|
||||
cb.set_error_basic(&on_basic).set_error_parse(&on_parse).set_error_visit(&on_visit);
|
||||
FleetConfig fc;
|
||||
fc.path = origin;
|
||||
try {
|
||||
ryml::EventHandlerTree handler(cb);
|
||||
ryml::Parser parser(&handler, ryml::ParserOptions().locations(true));
|
||||
ryml::Tree tree(cb);
|
||||
ryml::parse_in_arena(&parser, ryml::to_csubstr(origin), ryml::to_csubstr(text), &tree);
|
||||
/* Anchors, aliases and `<<` merge keys, so a file can share a block between models. */
|
||||
tree.resolve();
|
||||
if (tree.empty() || !tree.crootref().has_children()) {
|
||||
RAD_ERR("%s: the file is empty; it must name at least one model under models:", origin.c_str());
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
Reader r(origin, parser, tree);
|
||||
if (int s = r.run(&fc); s < 0) return s;
|
||||
} catch (const YamlError& e) {
|
||||
if (e.line != ryml::npos) RAD_ERR("%s:%zu: %s", origin.c_str(), e.line + 1, e.what());
|
||||
else RAD_ERR("%s: %s", origin.c_str(), e.what());
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
*out = std::move(fc);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int load(const std::string& path, FleetConfig* out) {
|
||||
std::ifstream f(path, std::ios::binary);
|
||||
if (!f) {
|
||||
RAD_ERR("%s: cannot read the configuration: %s", path.c_str(), std::strerror(errno));
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
std::ostringstream ss;
|
||||
ss << f.rdbuf();
|
||||
return parse(ss.str(), path, out);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the key reference */
|
||||
|
||||
namespace {
|
||||
|
||||
void print_keys(FILE* to, const std::vector<const ConfigFlag*>& flags, const char* indent) {
|
||||
for (const ConfigFlag* f : flags) {
|
||||
const char* name = f->name;
|
||||
while (*name == '-') ++name;
|
||||
char left[64];
|
||||
snprintf(left, sizeof left, "%s%s: %s", indent, name, f->arg ? f->arg : "true|false");
|
||||
std::string help;
|
||||
for (const char* p = f->help; *p; ++p) {
|
||||
help += *p;
|
||||
if (*p != '\n') continue;
|
||||
help.append(39, ' ');
|
||||
while (p[1] == ' ') ++p;
|
||||
}
|
||||
fprintf(to, "%-38s %s\n", left, help.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void print_reference(FILE* to) {
|
||||
size_t n = 0;
|
||||
const ConfigFlag* f = config_flags(&n);
|
||||
std::vector<const ConfigFlag*> server, memory, model;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const char* name = f[i].name;
|
||||
while (*name == '-') ++name;
|
||||
bool replaced = false;
|
||||
for (const Replaced& r : kReplaced) replaced |= !std::strcmp(name, r.flag);
|
||||
if (replaced) continue;
|
||||
if (in(kServerKeys, name)) server.push_back(&f[i]);
|
||||
else if (in(kMemoryFlagKeys, name)) memory.push_back(&f[i]);
|
||||
else model.push_back(&f[i]);
|
||||
}
|
||||
fprintf(to,
|
||||
"usage: radiance --config FILE [--check]\n"
|
||||
"\n"
|
||||
" --config FILE the YAML file naming every model this process serves (docs/MULTIMODEL.md)\n"
|
||||
" --check read and validate the file, print what it configures, serve nothing\n"
|
||||
"\n"
|
||||
"The file's levels, and the keys each takes:\n\n"
|
||||
"server:\n"
|
||||
" log: info|debug|trace the log level\n");
|
||||
print_keys(to, server, " ");
|
||||
fprintf(to,
|
||||
"\ncards: all | [N, ...] the cards models may use\n"
|
||||
"\nmemory:\n"
|
||||
" host-mib: N pinned host RAM the process may hold\n"
|
||||
" disk-tier: DIR the prefix-cache disk tier and park files\n");
|
||||
print_keys(to, memory, " ");
|
||||
fprintf(to,
|
||||
"\nscheduler:\n"
|
||||
" load-timeout: DURATION how long a request waits for its model (default 120s)\n"
|
||||
" drain-timeout: DURATION how long a model being evicted may finish requests (30s)\n"
|
||||
" keepalive: DURATION SSE comment interval while a request waits (10s)\n"
|
||||
" warm-up: true|false fill free VRAM, then host RAM, with models at start (true)\n"
|
||||
"\ndefaults: any model key, merged under every model\n"
|
||||
"\nmodels:\n"
|
||||
" NAME: the model id requests name\n"
|
||||
" aliases: [NAME, ...] more ids for this model\n"
|
||||
" priority: N higher keeps VRAM and takes flex from lower\n"
|
||||
" pin: true|false never leaves VRAM\n"
|
||||
" ttl: DURATION park after this long without requests\n"
|
||||
" flex: {kv: {min-tokens, max-tokens, weight}, experts: {min-mib, max-mib}}\n"
|
||||
" park: {vram: none|statics|MIB, ram: required|allowed|never, restore: auto|full|live}\n");
|
||||
print_keys(to, model, " ");
|
||||
fprintf(to,
|
||||
"\ngroups:\n"
|
||||
" NAME: {together: [A, B, ...]} loaded and parked together\n"
|
||||
" NAME: {exclusive: [A, B, ...]} never resident at the same time\n"
|
||||
"\nDurations are 250ms, 30s, 30m or 2h. Top-level keys starting with x- are ignored.\n");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the migration hint */
|
||||
|
||||
namespace {
|
||||
|
||||
/* Plain when YAML would read it back as the same string, double-quoted otherwise. */
|
||||
std::string yaml_scalar(const std::string& v) {
|
||||
bool plain = !v.empty();
|
||||
for (unsigned char ch : v)
|
||||
if (!(std::isalnum(ch) || ch == '_' || ch == '.' || ch == '/' || ch == '-' || ch == '+' || ch == '='))
|
||||
plain = false;
|
||||
if (plain && (v[0] == '-' || v == "true" || v == "false" || v == "null")) plain = false;
|
||||
if (plain) return v;
|
||||
std::string q = "\"";
|
||||
/* A double-quoted scalar folds a raw line break into a space: control characters go escaped. */
|
||||
for (char ch : v) {
|
||||
if (ch == '\n') q += "\\n";
|
||||
else if (ch == '\t') q += "\\t";
|
||||
else if (ch == '\r') q += "\\r";
|
||||
else {
|
||||
if (ch == '"' || ch == '\\') q += '\\';
|
||||
q += ch;
|
||||
}
|
||||
}
|
||||
return q + "\"";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string yaml_from_args(int argc, char** argv) {
|
||||
size_t n = 0;
|
||||
const ConfigFlag* f = config_flags(&n);
|
||||
std::map<std::string, const ConfigFlag*> table;
|
||||
for (size_t i = 0; i < n; ++i) table[f[i].name] = &f[i];
|
||||
|
||||
struct Kv { std::string key; std::vector<std::string> vals; bool sw = false; bool json = false; };
|
||||
std::vector<Kv> server, memory, model;
|
||||
std::string name, log;
|
||||
auto put = [](std::vector<Kv>& to, const std::string& k, const std::string* v) {
|
||||
for (Kv& e : to)
|
||||
if (e.key == k) { if (v) e.vals.push_back(*v); return; }
|
||||
to.push_back({ k, v ? std::vector<std::string>{ *v } : std::vector<std::string>{}, v == nullptr });
|
||||
};
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
const std::string a = argv[i];
|
||||
if (a == "-v") { log = "debug"; continue; }
|
||||
if (a == "-vv") { log = "trace"; continue; }
|
||||
auto t = table.find(a);
|
||||
if (t == table.end()) {
|
||||
/* Not an option: written as a model key anyway, so loading the file refuses it by name
|
||||
* (with the closest key) instead of the setting vanishing. */
|
||||
if (a.size() > 2 && a.compare(0, 2, "--") == 0) {
|
||||
const bool has_val = i + 1 < argc && std::strncmp(argv[i + 1], "--", 2) != 0;
|
||||
const std::string v = has_val ? argv[++i] : std::string("true");
|
||||
put(model, a.substr(2), &v);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const std::string key = a.substr(2);
|
||||
std::string v;
|
||||
if (t->second->arg) {
|
||||
if (i + 1 >= argc) break;
|
||||
v = argv[++i];
|
||||
}
|
||||
if (key == "served-model-name") { name = v; continue; }
|
||||
std::vector<Kv>& to = in(kServerKeys, key) ? server : in(kMemoryFlagKeys, key) ? memory : model;
|
||||
put(to, key, t->second->arg ? &v : nullptr);
|
||||
/* JSON is YAML's flow style, so a JSON argument is written as it stands, unquoted. */
|
||||
if (t->second->arg && !std::strcmp(t->second->arg, "JSON")) {
|
||||
const rad::Json j = rad::Json::parse(v, nullptr, false);
|
||||
if (!j.is_discarded() && j.is_structured())
|
||||
for (Kv& e : to)
|
||||
if (e.key == key) e.json = true;
|
||||
}
|
||||
}
|
||||
if (name.empty()) {
|
||||
for (const Kv& e : model)
|
||||
if (e.key == "model" && !e.vals.empty()) {
|
||||
std::string m = e.vals[0];
|
||||
while (!m.empty() && m.back() == '/') m.pop_back();
|
||||
const size_t slash = m.rfind('/');
|
||||
if (slash != std::string::npos) m = m.substr(slash + 1);
|
||||
/* Only a container's extension goes: a checkpoint directory or repo name such as
|
||||
* Qwen3.8-27B-FP8 keeps its dots. */
|
||||
if (m.size() > 4 && m.compare(m.size() - 4, 4, ".rad") == 0) m.resize(m.size() - 4);
|
||||
name = m;
|
||||
}
|
||||
}
|
||||
if (!valid_name(name)) name = "default";
|
||||
|
||||
std::string y;
|
||||
auto emit = [&](const std::vector<Kv>& kvs, const char* indent) {
|
||||
for (const Kv& e : kvs) {
|
||||
if (e.sw) { y += std::string(indent) + e.key + ": true\n"; continue; }
|
||||
if (e.json && e.vals.size() == 1) {
|
||||
y += std::string(indent) + e.key + ": " + rad::Json::parse(e.vals[0]).dump() + "\n";
|
||||
continue;
|
||||
}
|
||||
if (e.vals.size() == 1) { y += std::string(indent) + e.key + ": " + yaml_scalar(e.vals[0]) + "\n"; continue; }
|
||||
y += std::string(indent) + e.key + ":\n";
|
||||
for (const std::string& v : e.vals) y += std::string(indent) + " - " + yaml_scalar(v) + "\n";
|
||||
}
|
||||
};
|
||||
if (!server.empty() || !log.empty()) {
|
||||
y += "server:\n";
|
||||
if (!log.empty()) y += " log: " + log + "\n";
|
||||
emit(server, " ");
|
||||
}
|
||||
if (!memory.empty()) {
|
||||
y += "memory:\n";
|
||||
emit(memory, " ");
|
||||
}
|
||||
y += "models:\n " + yaml_scalar(name) + ":\n";
|
||||
emit(model, " ");
|
||||
return y;
|
||||
}
|
||||
|
||||
} // namespace rad::fleet
|
||||
@@ -0,0 +1,141 @@
|
||||
/* fleet/config.h -- the YAML file that configures a radiance process (docs/MULTIMODEL.md §4).
|
||||
*
|
||||
* One file names every model the process serves and the settings they share. Each former command
|
||||
* line flag is a key with the same name and no leading dashes, at the level it belongs to:
|
||||
*
|
||||
* server: the listener, authentication, logging and plugin search path (§4.3)
|
||||
* cards: the cards models may use
|
||||
* memory: per-card VRAM headroom and the host budgets
|
||||
* scheduler: load and drain timeouts, the keepalive interval
|
||||
* defaults: per-model keys merged under every model
|
||||
* models: one entry per model, keyed by its id
|
||||
* groups: together / exclusive co-residency rules
|
||||
*
|
||||
* Top-level keys that start with `x-` are ignored, so a file can keep anchors there and merge them
|
||||
* into models with `<<: *name`.
|
||||
*
|
||||
* A model's keys are turned back into the flag list config_parse reads, so a key and its flag are
|
||||
* validated by the same code and cannot drift. The loader adds only what the flag table cannot
|
||||
* express: levels, the defaults merge, the fleet's own keys, and provenance (which YAML path set
|
||||
* each option, for the startup report).
|
||||
*/
|
||||
#pragma once
|
||||
#include "rad_core.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rad::fleet {
|
||||
|
||||
/* A model's share of the card's flex pool above its fixed part (§6.3). -1 is "not set": the
|
||||
* floor defaults to one sequence at the model's context bound and the ceiling to everything the
|
||||
* card can lend. */
|
||||
struct FlexSpec {
|
||||
int64_t kv_min_tokens = -1;
|
||||
int64_t kv_max_tokens = -1;
|
||||
double kv_weight = 1.0;
|
||||
int64_t experts_min_mib = -1;
|
||||
int64_t experts_max_mib = -1;
|
||||
|
||||
bool operator==(const FlexSpec&) const = default;
|
||||
};
|
||||
|
||||
/* What a parked model keeps (§7.2). */
|
||||
struct ParkSpec {
|
||||
enum class Vram { None, Statics, Mib };
|
||||
enum class Ram { Required, Allowed, Never };
|
||||
/* How it comes back from host memory: every byte onto the card before it serves, or serving
|
||||
* at once with the rest read in place over the link while it moves (dev_page_in_live). Live
|
||||
* suits a model whose steps read a small part of what is parked (a tiered MoE's experts) or
|
||||
* that computes long per byte read; a dense model decoding reads all of it every step, so its
|
||||
* reads share the link with the move and both run slower (docs/MULTIMODEL.md §7.4). Auto is
|
||||
* Full, except that a one-step model (an embedding program) whose cards lack the room for all
|
||||
* of it serves at once with its weights read in place until free memory takes them (§7.5). */
|
||||
enum class Restore { Auto, Full, Live };
|
||||
Vram vram = Vram::None;
|
||||
int64_t vram_mib = 0; /* with Vram::Mib: the head of the first-use order kept */
|
||||
Ram ram = Ram::Allowed;
|
||||
Restore restore = Restore::Auto;
|
||||
|
||||
bool operator==(const ParkSpec&) const = default;
|
||||
};
|
||||
|
||||
struct ModelEntry {
|
||||
std::string name; /* the model id requests name; never contains ':' */
|
||||
std::vector<std::string> aliases;
|
||||
int priority = 0;
|
||||
bool pin = false;
|
||||
int64_t ttl_ms = 0; /* idle time before parking; 0 = never */
|
||||
FlexSpec flex;
|
||||
ParkSpec park;
|
||||
|
||||
/* The flag list `cfg` was parsed from, without the program name, in a fixed order: server
|
||||
* keys, then the model's own and inherited keys sorted by name. Two entries with equal args
|
||||
* configure identical engines, which is what hot reload compares (§10). */
|
||||
std::vector<std::string> args;
|
||||
Config cfg;
|
||||
};
|
||||
|
||||
struct Group {
|
||||
enum class Kind { Together, Exclusive };
|
||||
std::string name;
|
||||
Kind kind = Kind::Together;
|
||||
std::vector<std::string> members;
|
||||
};
|
||||
|
||||
struct FleetConfig {
|
||||
std::string path; /* the file read, for messages */
|
||||
|
||||
/* server.log: Info, Debug or Trace. Unset (`log_set` false), the level RADIANCE_LOG chose
|
||||
* stands. */
|
||||
Log log = Log::Info;
|
||||
bool log_set = false;
|
||||
|
||||
/* cards: empty means every card. */
|
||||
std::vector<int> cards;
|
||||
|
||||
/* memory: the per-card keys travel inside every model's flag list; these are the fleet's own. */
|
||||
int64_t host_mib = 0; /* 0 = not set */
|
||||
std::string disk_tier;
|
||||
|
||||
/* scheduler: */
|
||||
int64_t load_timeout_ms = 120000;
|
||||
int64_t drain_timeout_ms = 30000;
|
||||
int64_t keepalive_ms = 10000;
|
||||
/* Once the listener is open and nothing waits, unpinned models load one at a time into free
|
||||
* VRAM and park to host memory and the disk tier as later ones displace them, so a first
|
||||
* request finds its model parked rather than cold (docs/MULTIMODEL.md §8.6). */
|
||||
bool warm_up = true;
|
||||
|
||||
std::vector<ModelEntry> models;
|
||||
std::vector<Group> groups;
|
||||
|
||||
/* The model a name or alias resolves to, or null. */
|
||||
const ModelEntry* find(const std::string& name_or_alias) const;
|
||||
};
|
||||
|
||||
/* Read and validate `path`. RAD_OK, or RAD_E_INVAL after RAD_ERR lines that name the file, the
|
||||
* line and the key. Nothing is half-applied: on failure *out is left as it was. */
|
||||
int load(const std::string& path, FleetConfig* out);
|
||||
|
||||
/* The same from text already in memory; `origin` names it in messages. */
|
||||
int parse(const std::string& text, const std::string& origin, FleetConfig* out);
|
||||
|
||||
/* The YAML equivalent of a command line, for the message that refuses the removed flags: the
|
||||
* flags as one model named after the container, server and memory keys at their levels. A word
|
||||
* that is not an option is kept as a model key, so loading the result refuses it by name. */
|
||||
std::string yaml_from_args(int argc, char** argv);
|
||||
|
||||
/* The option keys a level holds ("server" or "memory"), without the fleet's own keys. */
|
||||
std::vector<std::string> level_keys(const std::string& level);
|
||||
|
||||
/* The key reference `radiance --help` prints: the levels, then every key with its argument and
|
||||
* help, grouped by level. */
|
||||
void print_reference(FILE* to);
|
||||
|
||||
/* Durations: "250ms", "30s", "30m", "2h"; a bare number is seconds. False on anything else. */
|
||||
bool parse_duration_ms(const std::string& s, int64_t* out_ms);
|
||||
|
||||
} // namespace rad::fleet
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,30 @@
|
||||
/* fleet/fleet.h -- the models of one configuration, served by one process (docs/MULTIMODEL.md).
|
||||
*
|
||||
* Every model the YAML names is known to the Front (server/front.h) from the start and loaded when
|
||||
* a request first needs it: the fleet's scheduler thread builds its Engine on cards it chooses,
|
||||
* making room by unloading models the policy lets go (fleet/policy.h). A model with an idle `ttl`
|
||||
* is unloaded after that long without requests; a pinned one is loaded at start and never leaves;
|
||||
* a one-model configuration loads its model at start, as a process of one model always has.
|
||||
*
|
||||
* Engines start one after another, since bring-up loads plugins and declares against process-wide
|
||||
* tables; they serve side by side. Unloading destroys the engine; its hardware queues stay with the
|
||||
* process for the next engine on that card (core/device/aql.cpp, g_spare) until the card is left
|
||||
* empty or a model needs their memory.
|
||||
*
|
||||
* Signals: SIGINT and SIGTERM drain and stop every model; SIGHUP re-reads the file (§10).
|
||||
*/
|
||||
#pragma once
|
||||
#include "fleet/config.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace rad::fleet {
|
||||
|
||||
/* Serve every model until SIGINT or SIGTERM. The process's exit code. */
|
||||
int run(FleetConfig& fc);
|
||||
|
||||
/* The keys this build reads but does not act on yet, as one message naming them by YAML path, or
|
||||
* "". A file setting them is refused rather than half-honoured. */
|
||||
std::string unsupported(const FleetConfig& fc);
|
||||
|
||||
} // namespace rad::fleet
|
||||
@@ -0,0 +1,179 @@
|
||||
/* fleet/policy.cpp -- card choice and eviction order (policy.h). */
|
||||
#include "fleet/policy.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
|
||||
namespace rad::fleet {
|
||||
|
||||
int64_t Resident::gives(int card) const {
|
||||
for (size_t i = 0; i < cards.size(); ++i)
|
||||
if (cards[i] == card) return i < charges.size() ? charges[i] : charge;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool leaves_before(const Resident& a, const Resident& b) {
|
||||
if (a.parked != b.parked) return !a.parked;
|
||||
if (a.priority != b.priority) return a.priority < b.priority;
|
||||
if (a.busy != b.busy) return !a.busy;
|
||||
const double sa = a.idle_s / std::max(a.restore_s, 0.05);
|
||||
const double sb = b.idle_s / std::max(b.restore_s, 0.05);
|
||||
if (sa != sb) return sa > sb;
|
||||
return a.id < b.id;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
/* Every ascending subset of `n` indices of size k, in lexicographic order. */
|
||||
void subsets(int n, int k, std::vector<std::vector<int>>* out) {
|
||||
if (k > n || k <= 0) return;
|
||||
std::vector<int> pick(k);
|
||||
for (int i = 0; i < k; ++i) pick[i] = i;
|
||||
for (;;) {
|
||||
out->push_back(pick);
|
||||
int i = k - 1;
|
||||
while (i >= 0 && pick[i] == n - k + i) --i;
|
||||
if (i < 0) return;
|
||||
++pick[i];
|
||||
for (int j = i + 1; j < k; ++j) pick[j] = pick[j - 1] + 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool on(const Resident& r, int card) {
|
||||
return std::find(r.cards.begin(), r.cards.end(), card) != r.cards.end();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool plan_room(const std::vector<CardRoom>& cards, const std::vector<Resident>& residents, int tp,
|
||||
int64_t need, int priority, const std::vector<size_t>& forced,
|
||||
const std::vector<int>& prefer, const std::vector<int>* fixed, RoomPlan* out,
|
||||
std::string* why, const std::map<int, int64_t>* need_on) {
|
||||
auto need_of = [&](int card) {
|
||||
if (need_on)
|
||||
if (const auto it = need_on->find(card); it != need_on->end()) return it->second;
|
||||
return need;
|
||||
};
|
||||
if (tp <= 0 || tp > (int)cards.size()) {
|
||||
*why = "it needs " + std::to_string(tp) + " cards and " + std::to_string(cards.size()) +
|
||||
" are available";
|
||||
return false;
|
||||
}
|
||||
std::vector<const Resident*> order;
|
||||
for (const Resident& r : residents)
|
||||
if (!r.pin && !r.keep && r.priority <= priority) order.push_back(&r);
|
||||
std::sort(order.begin(), order.end(),
|
||||
[](const Resident* a, const Resident* b) { return leaves_before(*a, *b); });
|
||||
auto is_forced = [&](const Resident& r) {
|
||||
return std::find(forced.begin(), forced.end(), r.id) != forced.end();
|
||||
};
|
||||
|
||||
std::vector<std::vector<int>> sets;
|
||||
if (fixed) {
|
||||
std::vector<int> idx;
|
||||
for (int c : *fixed)
|
||||
for (size_t i = 0; i < cards.size(); ++i)
|
||||
if (cards[i].card == c) idx.push_back((int)i);
|
||||
if ((int)idx.size() != tp) {
|
||||
*why = "its cards are not all available";
|
||||
return false;
|
||||
}
|
||||
std::sort(idx.begin(), idx.end());
|
||||
sets.push_back(idx);
|
||||
} else {
|
||||
subsets((int)cards.size(), tp, &sets);
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
std::tuple<int64_t, int, int, size_t> best{};
|
||||
int64_t short_best = INT64_MAX;
|
||||
for (size_t si = 0; si < sets.size(); ++si) {
|
||||
const std::vector<int>& set = sets[si];
|
||||
std::map<int, int64_t> room;
|
||||
for (int i : set) room[cards[(size_t)i].card] = cards[(size_t)i].free;
|
||||
std::vector<const Resident*> gone;
|
||||
auto take = [&](const Resident* r) {
|
||||
gone.push_back(r);
|
||||
for (int c : r->cards)
|
||||
if (room.count(c)) room[c] += r->gives(c);
|
||||
};
|
||||
for (const Resident& r : residents)
|
||||
if (is_forced(r)) take(&r);
|
||||
auto shortfall = [&] {
|
||||
int64_t s = 0;
|
||||
for (auto& [c, f] : room) s += std::max<int64_t>(need_of(c) - f, 0);
|
||||
return s;
|
||||
};
|
||||
for (const Resident* r : order) {
|
||||
if (shortfall() == 0) break;
|
||||
if (std::find(gone.begin(), gone.end(), r) != gone.end()) continue;
|
||||
bool helps = false;
|
||||
for (int c : r->cards)
|
||||
if (room.count(c) && room[c] < need_of(c)) helps = true;
|
||||
if (helps) take(r);
|
||||
}
|
||||
const int64_t sf = shortfall();
|
||||
if (sf > 0) {
|
||||
short_best = std::min(short_best, sf);
|
||||
continue;
|
||||
}
|
||||
int64_t displaced = 0;
|
||||
for (const Resident* r : gone)
|
||||
for (int c : r->cards) displaced += r->gives(c);
|
||||
int not_preferred = 0;
|
||||
for (int i : set)
|
||||
if (std::find(prefer.begin(), prefer.end(), cards[(size_t)i].card) == prefer.end())
|
||||
++not_preferred;
|
||||
/* Models that stay on the set's cards: fewer is better, so models spread over the cards
|
||||
* before they share one and each computes on a card of its own while it can. */
|
||||
int crowd = 0;
|
||||
for (const Resident& r : residents) {
|
||||
if (std::find(gone.begin(), gone.end(), &r) != gone.end()) continue;
|
||||
for (int i : set)
|
||||
if (on(r, cards[(size_t)i].card)) ++crowd;
|
||||
}
|
||||
const auto key = std::make_tuple(displaced, crowd, not_preferred, si);
|
||||
if (found && !(key < best)) continue;
|
||||
found = true;
|
||||
best = key;
|
||||
out->cards.clear();
|
||||
for (int i : set) out->cards.push_back(cards[(size_t)i].card);
|
||||
out->victims.clear();
|
||||
for (const Resident* r : gone) {
|
||||
if (r->members.empty()) out->victims.push_back(r->id);
|
||||
for (size_t m : r->members) out->victims.push_back(m);
|
||||
}
|
||||
out->displaced = displaced;
|
||||
}
|
||||
if (found) return true;
|
||||
|
||||
/* Name what stays in the way: every resident that may not leave for this one. */
|
||||
std::string blocking;
|
||||
for (const Resident& r : residents)
|
||||
if (r.pin || r.keep || r.priority > priority)
|
||||
blocking += (blocking.empty() ? "" : ", ") + std::to_string(r.id);
|
||||
*why = "short by " + std::to_string(short_best == INT64_MAX ? need : short_best) +
|
||||
" bytes even after every model allowed to leave has left";
|
||||
if (!blocking.empty()) *why += "; staying: #" + blocking;
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t flex_claim(int64_t need, int64_t want, int64_t kv_fixed, int64_t kv_ceiling,
|
||||
int64_t kv_one, int64_t max_tokens, int64_t experts, int64_t expert_floor,
|
||||
int64_t kv_min_tokens, int64_t experts_min_mib) {
|
||||
int64_t claim = need;
|
||||
if (kv_min_tokens > 0 && max_tokens > 0 && kv_ceiling > kv_fixed) {
|
||||
const int64_t t = std::min(kv_min_tokens, max_tokens);
|
||||
const int64_t kv = kv_fixed + (int64_t)((double)(kv_ceiling - kv_fixed) * (double)t /
|
||||
(double)max_tokens);
|
||||
claim += std::max<int64_t>(kv - kv_one, 0);
|
||||
}
|
||||
if (experts_min_mib > 0 && experts > expert_floor) {
|
||||
const int64_t e = std::min(experts_min_mib * ((int64_t)1 << 20), experts);
|
||||
claim += std::max<int64_t>(e - expert_floor, 0);
|
||||
}
|
||||
return std::max(need, std::min(claim, want));
|
||||
}
|
||||
|
||||
} // namespace rad::fleet
|
||||
@@ -0,0 +1,79 @@
|
||||
/* fleet/policy.h -- which cards a model loads on and who leaves to make room (docs/MULTIMODEL.md
|
||||
* §8.3, §8.4). Pure arithmetic on a snapshot, so the rules are tested without a card.
|
||||
*/
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace rad::fleet {
|
||||
|
||||
/* A card as the fleet sees it now: the bytes a model may still claim on it (the driver's free
|
||||
* reading less the headroom). */
|
||||
struct CardRoom {
|
||||
int card = 0;
|
||||
int64_t free = 0;
|
||||
};
|
||||
|
||||
/* A loaded model as a candidate to leave. */
|
||||
struct Resident {
|
||||
size_t id = 0;
|
||||
std::vector<int> cards;
|
||||
int64_t charge = 0; /* bytes it gives back on each of its cards when it leaves */
|
||||
int priority = 0;
|
||||
bool pin = false; /* never leaves */
|
||||
bool keep = false; /* may not leave for this load (a member of its together group) */
|
||||
bool busy = false; /* requests in flight or waiting */
|
||||
double idle_s = 0; /* since its last request */
|
||||
double restore_s = 1; /* how long it takes to come back */
|
||||
/* Parked already: it holds little VRAM and loses its host copy if it leaves, so it is the
|
||||
* last candidate whatever its priority. */
|
||||
bool parked = false;
|
||||
/* Bytes it gives back on each of `cards`, when they differ (a together group leaving as one,
|
||||
* whose members sit on different cards); empty takes `charge` on every card. */
|
||||
std::vector<int64_t> charges;
|
||||
/* The models that leave with it (a together group): Resident::id of each, `id` included.
|
||||
* Empty means `id` alone. */
|
||||
std::vector<size_t> members;
|
||||
|
||||
int64_t gives(int card) const;
|
||||
};
|
||||
|
||||
struct RoomPlan {
|
||||
std::vector<int> cards; /* the model's cards, ascending; rank i on cards[i] */
|
||||
std::vector<size_t> victims; /* Resident::id of every model that leaves, in eviction order;
|
||||
* a unit's members follow one another */
|
||||
int64_t displaced = 0; /* bytes the victims held, summed over their cards */
|
||||
};
|
||||
|
||||
/* The order models leave in: resident models before parked ones; then lower priority first; at
|
||||
* equal priority idle before busy; then the one that comes back cheapest relative to how long it
|
||||
* has been idle (idle_s / restore_s, highest first); then the lower id. True when `a` leaves
|
||||
* before `b`. */
|
||||
bool leaves_before(const Resident& a, const Resident& b);
|
||||
|
||||
/* Cards for a model of `tp` ranks that needs `need` bytes on each, and the models that leave so it
|
||||
* fits. Candidates to leave are the residents that are neither pinned nor kept, at the model's
|
||||
* `priority` or lower; `forced` leave whatever happens (the other members of an exclusive group).
|
||||
* Among the card sets that can be made to fit, the one displacing the fewest bytes wins, then the
|
||||
* one whose cards keep the fewest other models (models spread before they share a card), then the
|
||||
* one with most cards in `prefer` (where the model was before), then the lowest cards. With
|
||||
* `fixed` set only that card set is considered. A card named in `need_on` needs that many bytes
|
||||
* instead of `need`: a parked model's ranks can each have a different part still on their cards.
|
||||
*
|
||||
* True with *out filled, or false with *why naming what is in the way. */
|
||||
bool plan_room(const std::vector<CardRoom>& cards, const std::vector<Resident>& residents, int tp,
|
||||
int64_t need, int priority, const std::vector<size_t>& forced,
|
||||
const std::vector<int>& prefer, const std::vector<int>* fixed, RoomPlan* out,
|
||||
std::string* why, const std::map<int, int64_t>* need_on = nullptr);
|
||||
|
||||
/* The claim a model gets in a fleet of several (§6.3): its need plus what its flex floor asks for,
|
||||
* never above what it can use. `kv_min_tokens` and `experts_min_mib` are the YAML flex floors
|
||||
* (-1 unset). The cache's cost of T tokens is kv_fixed + (kv_ceiling - kv_fixed) * T / max_tokens. */
|
||||
int64_t flex_claim(int64_t need, int64_t want, int64_t kv_fixed, int64_t kv_ceiling,
|
||||
int64_t kv_one, int64_t max_tokens, int64_t experts, int64_t expert_floor,
|
||||
int64_t kv_min_tokens, int64_t experts_min_mib);
|
||||
|
||||
} // namespace rad::fleet
|
||||
Reference in new issue
Block a user