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
+619
-73
@@ -20,6 +20,7 @@
|
||||
#include "sample/accept.h"
|
||||
#include "sched/scheduler.h"
|
||||
#include "server/adapters.h"
|
||||
#include "server/front.h"
|
||||
#include "server/server.h"
|
||||
#include "server/sink.h"
|
||||
#include "server/liveview.h"
|
||||
@@ -28,7 +29,6 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstdint>
|
||||
@@ -256,9 +256,493 @@ void kv_test_rollback(uint64_t seq, int proposals, int accepted, int64_t valid,
|
||||
} // namespace
|
||||
#endif
|
||||
|
||||
/* Owner ids for the device layer (engine_owner), one per engine; 0 is no engine. */
|
||||
namespace {
|
||||
std::atomic<int> g_next_owner{1};
|
||||
}
|
||||
|
||||
/* The operator's RADIANCE_* names, captured at construction for the diagnostics report. */
|
||||
Engine::Engine(Config cfg)
|
||||
: cfg_(std::move(cfg)), env_set_(diag::env_names(environ, "RADIANCE_")) {}
|
||||
: cfg_(std::move(cfg)), owner_(g_next_owner.fetch_add(1)),
|
||||
env_set_(diag::env_names(environ, "RADIANCE_")) {}
|
||||
|
||||
/* Every rank's compute stream, the mover's transfer stream and the KV tiers' copy stream,
|
||||
* synchronised. A tier copy reads the KV pool long after the step that issued it, and nothing on
|
||||
* the compute stream waits for it, so memory leaving the card must wait for it here: a gather over
|
||||
* a range just unbacked is a GPU memory fault. The compute stream goes first because the copy is
|
||||
* ordered after it. */
|
||||
void Engine::drain_streams() {
|
||||
const int dev = dev_current();
|
||||
for (auto& rk : ranks_) {
|
||||
(void)rad_stream_sync(rk->ctx.stream());
|
||||
if (RadStream t = rk->mover.transfer_stream()) (void)rad_stream_sync(t);
|
||||
rk->tierx.quiesce();
|
||||
}
|
||||
(void)rad_dev_set(dev);
|
||||
}
|
||||
|
||||
std::vector<int> Engine::rank_devices() const {
|
||||
std::vector<int> cards;
|
||||
cards.reserve(ranks_.size());
|
||||
for (auto& r : ranks_) cards.push_back(r->device);
|
||||
return cards;
|
||||
}
|
||||
|
||||
/* The step loop's rest point for a park: idle, nothing issued that still runs. The streams are
|
||||
* drained before the loop answers, so the fleet may page the memory out at once. */
|
||||
void Engine::hold_point(StallWatch* watch) {
|
||||
if (!hold_req_.load(std::memory_order_acquire)) return;
|
||||
drain_streams();
|
||||
hold_ack_.store(true, std::memory_order_release);
|
||||
/* A park lasts as long as the fleet keeps the model off its cards. The loop sleeps until the
|
||||
* hold clears or the engine stops, waking once a second besides to mark the step watch, so a
|
||||
* model parked for minutes is not reported as a step that stopped (and is not woken a thousand
|
||||
* times a second to look). */
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(hold_mu_);
|
||||
while (hold_req_.load(std::memory_order_acquire) && !stopping_.load(std::memory_order_acquire)) {
|
||||
hold_cv_.wait_for(lk, std::chrono::seconds(1));
|
||||
if (watch) watch->progress();
|
||||
}
|
||||
}
|
||||
hold_ack_.store(false, std::memory_order_release);
|
||||
}
|
||||
|
||||
void Engine::hold_release() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(hold_mu_);
|
||||
hold_req_.store(false, std::memory_order_release);
|
||||
}
|
||||
hold_cv_.notify_all();
|
||||
}
|
||||
|
||||
void Engine::wake_hold() {
|
||||
{ std::lock_guard<std::mutex> lk(hold_mu_); }
|
||||
hold_cv_.notify_all();
|
||||
}
|
||||
|
||||
int Engine::park(int64_t drain_ms, const PageOut& req, PageResult* got, std::string* why) {
|
||||
using clk = std::chrono::steady_clock;
|
||||
const auto secs = [](clk::time_point a, clk::time_point b) {
|
||||
return std::chrono::duration<double>(b - a).count();
|
||||
};
|
||||
const auto t0 = clk::now();
|
||||
auto page_out = [&] {
|
||||
PageOut r = req;
|
||||
r.discard_elastic = true;
|
||||
return dev_page_out(owner_, r, got);
|
||||
};
|
||||
/* Already parked: more of it leaves. */
|
||||
if (parked_.load()) {
|
||||
EngineScope es(owner_, cfg_.served_model_name.c_str());
|
||||
const int rc = page_out();
|
||||
got->out_s = secs(t0, clk::now());
|
||||
if (rc < 0) {
|
||||
*why = std::string("paging out failed: ") + rad_dev_last_error();
|
||||
return rc;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
if (!VMem::supported()) {
|
||||
*why = "the card has no virtual memory manager, so nothing can leave it at the same address";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (!front_ || !srv_) {
|
||||
*why = "the engine is not serving behind a front";
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
const std::string& name = cfg_.served_model_name;
|
||||
if (!front_->detach(name, (int)std::min<int64_t>(drain_ms, INT32_MAX))) {
|
||||
RAD_WARN("model %s: responses still open after %lld ms; ending them to park",
|
||||
name.c_str(), (long long)drain_ms);
|
||||
srv_->end_streams(true);
|
||||
const bool gone = front_->detach(name, 60000);
|
||||
srv_->end_streams(false);
|
||||
if (!gone) {
|
||||
*why = "responses still held a minute after they were ended";
|
||||
front_->attach(name, front_aliases_, srv_);
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
}
|
||||
hold_req_.store(true, std::memory_order_release);
|
||||
sched_.wake();
|
||||
for (int i = 0; i < 60000 && !hold_ack_.load(std::memory_order_acquire); ++i) {
|
||||
if (loop_status_.load(std::memory_order_acquire) < 0) break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
if (!hold_ack_.load(std::memory_order_acquire)) {
|
||||
hold_release();
|
||||
front_->attach(name, front_aliases_, srv_);
|
||||
*why = "the step loop did not come to rest";
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
const auto t1 = clk::now();
|
||||
EngineScope es(owner_, name.c_str());
|
||||
/* What need not be copied: the expert slab once its units are back at their host copies
|
||||
* (the heat engine fills it again after the restore), the KV cache once its index is reset
|
||||
* (nothing runs, so no block is live), and the activation arena above its fixed region:
|
||||
* transients live within one step, and the loop rests between steps. A unit still in a slot
|
||||
* the arena lent keeps the arena copied. */
|
||||
std::vector<std::pair<const void*, int64_t>> spare;
|
||||
for (auto& r : ranks_) {
|
||||
if (rad_dev_set(r->device) < 0) continue;
|
||||
if (r->mover.flex_capacity() > 0) {
|
||||
(void)r->mover.reclaim(Mover::kLendKV, 0, &r->heat);
|
||||
(void)r->mover.reclaim(Mover::kLendArena, 0, &r->heat);
|
||||
(void)r->mover.reclaim(Mover::kLendFleet, 0, &r->heat);
|
||||
}
|
||||
/* The fleet's loan is unbacked, not copied: nothing in it outlives the recall. */
|
||||
if (r->xflex.committed() > 0 && r->mover.loan_held(Mover::kLendFleet) == 0)
|
||||
(void)r->xflex.decommit(0, r->xflex.size());
|
||||
if (r->heat.enabled() && r->mover.drop_slab(&r->heat, &spare) < 0)
|
||||
RAD_WARN("rank %d: the expert slab could not be emptied for the park; it is copied", r->index);
|
||||
const int64_t fixed = r->program.arena_fixed_bytes, top = r->ctx.arena_bytes();
|
||||
if (r->ctx.arena_base() && top > fixed && r->mover.loan_held(Mover::kLendArena) == 0)
|
||||
spare.push_back({ (const char*)r->ctx.arena_base() + fixed, top - fixed });
|
||||
}
|
||||
if (!spare.empty()) dev_page_discard(owner_, spare);
|
||||
(void)xflex_settle();
|
||||
if (cfg_.prefix_cache) (void)sched_.reset_prefix_cache();
|
||||
const auto t2 = clk::now();
|
||||
const int rc = page_out();
|
||||
got->quiet_s = secs(t0, t1);
|
||||
got->slab_s = secs(t1, t2);
|
||||
got->out_s = secs(t2, clk::now());
|
||||
if (rc < 0) {
|
||||
/* Whatever left comes back, and the model serves as before. */
|
||||
*why = std::string("paging out failed: ") + rad_dev_last_error();
|
||||
(void)page_back_in(nullptr);
|
||||
hold_release();
|
||||
front_->attach(name, front_aliases_, srv_);
|
||||
return rc;
|
||||
}
|
||||
parked_.store(true, std::memory_order_release);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int64_t Engine::flex_bytes() const {
|
||||
const int64_t x = xflex_bytes_.load(std::memory_order_acquire);
|
||||
if (ranks_.empty() || !ranks_[0]->kv.flex()) return x;
|
||||
return x + std::max<int64_t>(ranks_[0]->pools.kv().committed() - flex_floor_bytes_, 0);
|
||||
}
|
||||
|
||||
int64_t Engine::flex_release(int64_t bytes, bool preempt, int64_t wait_ms, bool kv) {
|
||||
if (bytes <= 0 || ranks_.empty() || parked_.load()) return 0;
|
||||
const bool has_kv = kv && ranks_[0]->kv.flex();
|
||||
if (!has_kv && xflex_bytes_.load() <= 0) return 0;
|
||||
std::unique_lock<std::mutex> lk(flex_mu_);
|
||||
flex_want_ = bytes;
|
||||
flex_preempt_ = preempt;
|
||||
flex_kv_ = has_kv;
|
||||
flex_got_ = -1;
|
||||
flex_pending_.store(true, std::memory_order_release);
|
||||
flex_cv_.wait_for(lk, std::chrono::milliseconds(wait_ms), [&] {
|
||||
return flex_got_ >= 0 || stopping_.load(std::memory_order_acquire);
|
||||
});
|
||||
flex_pending_.store(false, std::memory_order_release);
|
||||
return flex_got_ > 0 ? flex_got_ : 0;
|
||||
}
|
||||
|
||||
/* On the step loop's thread, between steps with nothing issued ahead: the expert slab's fleet loan
|
||||
* first, then (unless only the experts' flex was asked for) the cache's free top, the prefix
|
||||
* cache and, with `flex_preempt_`, the running requests, until `flex_want_` bytes can go; then the
|
||||
* mark moves on every rank and the stripes above it are unbacked. */
|
||||
void Engine::flex_service() {
|
||||
if (!flex_pending_.load(std::memory_order_acquire)) return;
|
||||
std::lock_guard<std::mutex> lk(flex_mu_);
|
||||
if (flex_got_ >= 0) return;
|
||||
drain_streams();
|
||||
/* The experts' loan first: giving it back is an address change for each unit in it. */
|
||||
const int64_t xgot = xflex_give(flex_want_);
|
||||
if (xgot >= flex_want_ || !flex_kv_) {
|
||||
flex_got_ = xgot;
|
||||
flex_cv_.notify_all();
|
||||
return;
|
||||
}
|
||||
KVManager& k0 = ranks_[0]->kv;
|
||||
const int64_t tb = std::max<int64_t>(k0.flex_token_bytes(), 1);
|
||||
const int64_t want = (flex_want_ - xgot + tb - 1) / tb;
|
||||
const int64_t floor_loan = k0.flex_floor_loan();
|
||||
const int64_t cur = sched_.locked([&] { return k0.loan_tokens(); });
|
||||
/* spare_tokens() counts from the top of the carve, and a flex carve's top is the unbacked
|
||||
* reservation: what is free below the live mark is that less the loan. Never below the floor
|
||||
* the claim pays for. */
|
||||
auto room_locked = [&] {
|
||||
return std::max<int64_t>(std::min(k0.spare_tokens() - cur, floor_loan - cur), 0);
|
||||
};
|
||||
auto room = [&] { return sched_.locked(room_locked); };
|
||||
int64_t spare = room();
|
||||
/* What the floor lets go however empty the cache is. Past that, evicting or preempting costs
|
||||
* conversations and frees nothing the fleet can have. */
|
||||
const int64_t goal = std::min(want, std::max<int64_t>(floor_loan - cur, 0));
|
||||
if (spare < goal && cfg_.prefix_cache) {
|
||||
(void)sched_.evict_prefix_until([&] { return room_locked() >= goal; });
|
||||
spare = room();
|
||||
}
|
||||
if (spare < goal && flex_preempt_) {
|
||||
const int n = sched_.preempt_until([&] { return room_locked() >= goal; });
|
||||
if (sched_.has_deferred()) sched_.release_deferred();
|
||||
if (n > 0)
|
||||
RAD_INFO("flex: %d running request(s) go back to the queue so a higher-priority model "
|
||||
"takes the cache", n);
|
||||
spare = room();
|
||||
}
|
||||
const int64_t take = std::min(goal, spare);
|
||||
const int64_t before = ranks_[0]->pools.kv().committed();
|
||||
if (take > 0 && sched_.locked([&] { return k0.set_loan(cur + take); }) == RAD_OK) {
|
||||
for (size_t i = 1; i < ranks_.size(); ++i) (void)ranks_[i]->kv.set_loan(cur + take);
|
||||
for (auto& r : ranks_) {
|
||||
(void)rad_dev_set(r->device);
|
||||
if (const int rc = r->kv.flex_unback_above(cur + take); rc < 0)
|
||||
RAD_WARN("flex: rank %d could not unback its cache above the new mark: %s (%s)",
|
||||
r->index, rad_strerror(rc), rad_dev_last_error());
|
||||
}
|
||||
(void)rad_dev_set(ranks_[0]->device);
|
||||
}
|
||||
flex_got_ = xgot + std::max<int64_t>(before - ranks_[0]->pools.kv().committed(), 0);
|
||||
RAD_INFO("flex: asked for %s, the cache could give %lld of %lld tokens, gave back %s",
|
||||
humanb(flex_want_).c_str(), (long long)take, (long long)want, humanb(flex_got_).c_str());
|
||||
flex_cv_.notify_all();
|
||||
}
|
||||
|
||||
/* The slab's backed bytes a card after a recall, the most any rank still has backed; also clears
|
||||
* what the slab asks the fleet for. */
|
||||
int64_t Engine::xflex_settle() {
|
||||
int64_t left = 0;
|
||||
for (auto& r : ranks_) left = std::max(left, r->xflex.committed());
|
||||
xflex_bytes_.store(left, std::memory_order_release);
|
||||
xflex_want_.store(0, std::memory_order_release);
|
||||
return left;
|
||||
}
|
||||
|
||||
/* On the step loop, every stream drained: the slab's units in the fleet's slots past the new loan
|
||||
* go back to their host copies (Mover::reclaim), and the granules they leave are unbacked. The
|
||||
* bytes given back on each card. The slab does not grow again for five seconds, so the model that
|
||||
* asked for the room gets it first. */
|
||||
int64_t Engine::xflex_give(int64_t bytes) {
|
||||
const int64_t cur = xflex_bytes_.load(std::memory_order_acquire);
|
||||
if (bytes <= 0 || cur <= 0) return 0;
|
||||
const int64_t g = std::max<int64_t>(VMem::granularity(), 1);
|
||||
int64_t to = std::max<int64_t>(cur - (bytes + g - 1) / g * g, 0);
|
||||
for (auto& r : ranks_) {
|
||||
if (rad_dev_set(r->device) < 0) continue;
|
||||
if (const int st = r->mover.reclaim(Mover::kLendFleet, to, &r->heat); st < 0)
|
||||
RAD_WARN("expert flex: rank %d could not recall its slab to %s: %s", r->index,
|
||||
humanb(to).c_str(), rad_strerror(st));
|
||||
/* What a unit still holds stays backed. */
|
||||
to = std::max(to, (r->mover.loan_held(Mover::kLendFleet) + g - 1) / g * g);
|
||||
}
|
||||
for (auto& r : ranks_) {
|
||||
if (rad_dev_set(r->device) < 0) continue;
|
||||
const int64_t top = r->xflex.size();
|
||||
(void)r->mover.set_loan(Mover::kLendFleet, to);
|
||||
if (const int st = r->xflex.decommit(std::max<int64_t>(top - cur, 0), cur - to); st < 0)
|
||||
RAD_WARN("expert flex: rank %d could not unback %s: %s (%s)", r->index,
|
||||
humanb(cur - to).c_str(), rad_strerror(st), rad_dev_last_error());
|
||||
}
|
||||
(void)rad_dev_set(ranks_[0]->device);
|
||||
const int64_t left = xflex_settle();
|
||||
xflex_retry_ns_ = rad_mono_ns() + 5000000000ull;
|
||||
const int64_t got = std::max<int64_t>(cur - left, 0);
|
||||
if (got > 0)
|
||||
RAD_INFO("expert flex: the slab gave back %s a card, keeps %s", humanb(got).c_str(),
|
||||
humanb(left).c_str());
|
||||
return got;
|
||||
}
|
||||
|
||||
/* Every tenth of a second on the step loop. While the engine has been busy in the last five
|
||||
* seconds, the slab grows a GiB at a time into memory the fleet says no model holds, and grows
|
||||
* again only once the units fill what it already has, leaving no more than the turnover slots and
|
||||
* half a step empty (an empty slot is VRAM nothing uses). A refusal is remembered, so the fleet
|
||||
* can make room for it (expert_flex_wanted), and asked again a second later; a backing that fails
|
||||
* waits five seconds. */
|
||||
void Engine::xflex_tick(bool busy) {
|
||||
if (xflex_cap_ <= 0 || !broker_) return;
|
||||
const uint64_t now = rad_mono_ns();
|
||||
if (busy) xflex_busy_ns_ = now;
|
||||
if (now - xflex_ns_ < 100000000ull) return;
|
||||
xflex_ns_ = now;
|
||||
const int64_t cur = xflex_bytes_.load(std::memory_order_acquire);
|
||||
if (cur >= xflex_cap_ || now - xflex_busy_ns_ > 5000000000ull) {
|
||||
xflex_want_.store(0, std::memory_order_release);
|
||||
return;
|
||||
}
|
||||
if (now < xflex_retry_ns_) return;
|
||||
const Mover& m0 = ranks_[0]->mover;
|
||||
const int64_t g = std::max<int64_t>(VMem::granularity(), 1);
|
||||
const int64_t step = std::min<int64_t>(((int64_t)1 << 30) / g * g, xflex_cap_ - cur);
|
||||
/* Still filling: what the loans offer and no unit holds, past the slots kept free for swaps. */
|
||||
const int64_t kept = m0.turnover_bytes();
|
||||
if (m0.flex_offered_bytes() - m0.flex_used_bytes() > kept + step / 2) return;
|
||||
if (!broker_->flex_grow(rank_devices(), step)) {
|
||||
xflex_want_.store(xflex_cap_ - cur, std::memory_order_release);
|
||||
xflex_retry_ns_ = now + 1000000000ull;
|
||||
return;
|
||||
}
|
||||
struct Landed { VramBroker* b; ~Landed() { b->flex_done(); } } landed{ broker_ };
|
||||
const int64_t to = cur + step;
|
||||
bool ok = true;
|
||||
for (auto& r : ranks_) {
|
||||
if (rad_dev_set(r->device) < 0) { ok = false; break; }
|
||||
const int64_t top = r->xflex.size();
|
||||
if (r->xflex.commit(std::max<int64_t>(top - to, 0), std::min(step, top)) < 0) { ok = false; break; }
|
||||
}
|
||||
if (!ok) {
|
||||
RAD_WARN("expert flex: backing %s more a card failed (%s); trying again later",
|
||||
humanb(step).c_str(), rad_dev_last_error());
|
||||
for (auto& r : ranks_) {
|
||||
if (rad_dev_set(r->device) < 0) continue;
|
||||
const int64_t top = r->xflex.size();
|
||||
(void)r->xflex.decommit(std::max<int64_t>(top - to, 0), step);
|
||||
}
|
||||
(void)rad_dev_set(ranks_[0]->device);
|
||||
xflex_retry_ns_ = now + 5000000000ull;
|
||||
return;
|
||||
}
|
||||
for (auto& r : ranks_) {
|
||||
if (rad_dev_set(r->device) < 0) continue;
|
||||
(void)r->mover.set_loan(Mover::kLendFleet, to);
|
||||
}
|
||||
(void)rad_dev_set(ranks_[0]->device);
|
||||
xflex_bytes_.store(to, std::memory_order_release);
|
||||
xflex_want_.store(0, std::memory_order_release);
|
||||
if (to >= xflex_cap_ || cur == 0)
|
||||
RAD_INFO("expert flex: the slab grows into memory no model holds: %s a card of up to %s",
|
||||
humanb(to).c_str(), humanb(xflex_cap_).c_str());
|
||||
}
|
||||
|
||||
/* What the fleet could still grow the cache by, for admission, every tenth of a second. */
|
||||
void Engine::flex_room_tick(uint64_t now_ns) {
|
||||
if (!broker_ || ranks_.empty() || !ranks_[0]->kv.flex()) return;
|
||||
if (now_ns - flex_room_ns_ < 100000000ull) return;
|
||||
flex_room_ns_ = now_ns;
|
||||
const int64_t tb = std::max<int64_t>(ranks_[0]->kv.flex_token_bytes(), 1);
|
||||
ranks_[0]->kv.set_flex_room(broker_->flex_room(rank_devices()) / tb);
|
||||
}
|
||||
|
||||
int64_t Engine::release_host_spare() {
|
||||
if (!parked_.load() || ck_released_.load() || !ck_store_.enabled() || ck_store_.used_slots() > 0) return 0;
|
||||
EngineScope es(owner_, cfg_.served_model_name.c_str());
|
||||
ck_slot_ = ck_store_.slot_bytes();
|
||||
ck_cap_ = ck_store_.cap_bytes();
|
||||
ck_store_.close();
|
||||
ck_released_.store(true, std::memory_order_release);
|
||||
return ck_cap_;
|
||||
}
|
||||
|
||||
int Engine::repin_host_spare(std::string* why) {
|
||||
if (!ck_released_.load()) return RAD_OK;
|
||||
if (ck_store_.open(ck_slot_, ck_cap_) != RAD_OK) {
|
||||
*why = "the checkpoint slots' " + humanb(ck_cap_) + " of pinned host memory could not be pinned again";
|
||||
return RAD_E_NOMEM;
|
||||
}
|
||||
ck_released_.store(false, std::memory_order_release);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Engine::unpark(std::string* why) { return unpark_with(why, nullptr); }
|
||||
|
||||
int Engine::page_back_in(int64_t* left) {
|
||||
std::vector<PageSpan> refill;
|
||||
if (const int rc = dev_page_in_begin(owner_, left, &refill); rc < 0) return rc;
|
||||
/* The elastic ranges (the KV cache, the expert flex) are backed, and the chunks held in host
|
||||
* memory copied back (q27: 6 GiB, 0.3 s), beside the refill, which reads the drive and writes
|
||||
* none of them. */
|
||||
int erc = RAD_OK;
|
||||
std::string elastic_why; /* the device error is per thread: carried back to this one */
|
||||
std::thread elastic([&] {
|
||||
EngineScope es(owner_, cfg_.served_model_name.c_str());
|
||||
erc = dev_page_in_elastic(owner_);
|
||||
if (erc < 0) {
|
||||
const char* w = rad_dev_last_error();
|
||||
elastic_why = w && *w ? w : rad_strerror(erc);
|
||||
}
|
||||
});
|
||||
/* The refill's writes wait for the begin's clears of recycled chunks, which run beside its
|
||||
* first read. */
|
||||
const int frc = refill_weights(refill, "restore", [this] { return dev_page_in_cleared(owner_); });
|
||||
elastic.join();
|
||||
const int irc = dev_page_in_end(owner_);
|
||||
if (frc < 0) {
|
||||
device_set_error("the weights paged back empty could not be read from the container again (%s)",
|
||||
rad_strerror(frc));
|
||||
return frc;
|
||||
}
|
||||
if (irc < 0) return irc;
|
||||
if (erc < 0)
|
||||
device_set_error("the KV cache and expert ranges could not be backed again: %s",
|
||||
elastic_why.c_str());
|
||||
return erc;
|
||||
}
|
||||
|
||||
int Engine::unpark_live(std::string* why, int64_t* left) {
|
||||
*left = 0;
|
||||
return unpark_with(why, left);
|
||||
}
|
||||
|
||||
/* unpark() with `left` null, unpark_live() otherwise: the memory comes back (whole, or live with
|
||||
* `left` bytes still in host memory), the step loop leaves its rest, and the front routes to the
|
||||
* model again. */
|
||||
int Engine::unpark_with(std::string* why, int64_t* left) {
|
||||
if (!parked_.load()) return RAD_OK;
|
||||
EngineScope es(owner_, cfg_.served_model_name.c_str());
|
||||
/* The snapshot store is pinned again beside the page-in (q27's 4 GiB takes 0.2-0.3 s), not
|
||||
* before it: neither waits on the other, and the step loop stays at rest until both are done.
|
||||
* On a failure of either the fleet unloads the model, so the order of the two is free. */
|
||||
std::string pin_why;
|
||||
int pin_rc = RAD_OK;
|
||||
std::thread pin;
|
||||
if (ck_released_.load())
|
||||
pin = std::thread([&] {
|
||||
EngineScope pes(owner_, cfg_.served_model_name.c_str());
|
||||
pin_rc = repin_host_spare(&pin_why);
|
||||
});
|
||||
const int prc = page_back_in(left);
|
||||
if (pin.joinable()) pin.join();
|
||||
if (prc < 0) {
|
||||
*why = std::string("paging in failed: ") + rad_dev_last_error();
|
||||
return prc;
|
||||
}
|
||||
if (pin_rc < 0) {
|
||||
*why = pin_why;
|
||||
return pin_rc;
|
||||
}
|
||||
parked_.store(false, std::memory_order_release);
|
||||
hold_release();
|
||||
while (hold_ack_.load(std::memory_order_acquire) && !stopping_.load())
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||
front_->attach(cfg_.served_model_name, front_aliases_, srv_);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Engine::run_quiet(const std::function<int()>& fn) {
|
||||
std::unique_lock<std::mutex> lk(quiet_mu_);
|
||||
quiet_fn_ = &fn;
|
||||
quiet_done_ = false;
|
||||
quiet_pending_.store(true, std::memory_order_release);
|
||||
sched_.wake();
|
||||
/* Polled for a stop: the step loop may end without passing a step boundary again. */
|
||||
while (!quiet_cv_.wait_for(lk, std::chrono::milliseconds(50), [&] {
|
||||
return quiet_done_ || stopping_.load(std::memory_order_acquire);
|
||||
})) {}
|
||||
quiet_pending_.store(false, std::memory_order_release);
|
||||
quiet_fn_ = nullptr;
|
||||
return quiet_done_ ? quiet_rc_ : RAD_E_STATE;
|
||||
}
|
||||
|
||||
/* On the step loop's thread, with nothing issued ahead: every rank's streams drained (the compute
|
||||
* stream and the mover's, as for a park), then run_quiet's function. */
|
||||
void Engine::quiet_service() {
|
||||
if (!quiet_pending_.load(std::memory_order_acquire)) return;
|
||||
std::lock_guard<std::mutex> lk(quiet_mu_);
|
||||
if (quiet_done_ || !quiet_fn_) return;
|
||||
const int dev = dev_current();
|
||||
drain_streams();
|
||||
quiet_rc_ = (*quiet_fn_)();
|
||||
(void)rad_dev_set(dev);
|
||||
quiet_done_ = true;
|
||||
quiet_cv_.notify_all();
|
||||
}
|
||||
|
||||
int32_t* Engine::Readback::reserve(int64_t count) {
|
||||
if (count <= n) return p;
|
||||
@@ -309,13 +793,24 @@ Engine::~Engine() {
|
||||
* and frees the arena, but the mover's stream also copies into the arena (the prefill
|
||||
* stager's ring, lent slab slots) and would otherwise drain only later, with the mover. A fill
|
||||
* landing in a freed arena is a GPU memory fault. */
|
||||
for (auto& r : ranks_)
|
||||
for (auto& r : ranks_) {
|
||||
if (RadStream s = r->mover.transfer_stream()) (void)rad_stream_sync(s);
|
||||
r->tierx.quiesce();
|
||||
}
|
||||
for (auto& r : ranks_) r->ctx.shutdown();
|
||||
if (cards_claimed_) {
|
||||
const std::vector<int> cards = rank_devices();
|
||||
(void)rad_dev_cards_claim(cards.data(), (int)cards.size(), 0);
|
||||
cards_claimed_ = false;
|
||||
}
|
||||
/* The streamed groups' host mirrors are pinned outside every pool; freed after the contexts,
|
||||
* since a pass reads them. Safe after a failed bring-up. */
|
||||
for (auto& r : ranks_) r->kv.free_stream(rad_device_allocator());
|
||||
step_state_fini();
|
||||
/* The step batch's staging, events and media buffers (BatchBuilder), freed while the ranks'
|
||||
* KV managers the scheduler releases sequences into still exist. A process that unloads
|
||||
* models would otherwise keep every engine's. */
|
||||
sched_.fini();
|
||||
ranks_.clear();
|
||||
for (TableCopy& c : table_copies_) table_copy_free(&c);
|
||||
table_copies_.clear();
|
||||
@@ -325,7 +820,8 @@ Engine::~Engine() {
|
||||
card_copies_.clear();
|
||||
for (TableShare& t : table_shares_) table_share_close(&t);
|
||||
table_shares_.clear();
|
||||
rad_tools_registry().close();
|
||||
if (reg_) reg_->close();
|
||||
if (slot_base_ >= 0) rad_engine_release(plugins_.arch_plugin, slot_base_, cfg_.tp);
|
||||
}
|
||||
|
||||
/* ================================================================== 7. serve */
|
||||
@@ -720,6 +1216,9 @@ int Engine::prepare_release(const RadBatch* b) {
|
||||
int Engine::issue_ahead(const RadBatch** out) {
|
||||
*out = nullptr;
|
||||
if (!pipelining_) return RAD_OK;
|
||||
/* A flex release or a quiet window waits for a step boundary with nothing in flight. */
|
||||
if (flex_pending_.load(std::memory_order_acquire) || quiet_pending_.load(std::memory_order_acquire))
|
||||
return RAD_OK;
|
||||
for (int i = 0; i < serial_rows_ && serial_done_ > 0; ++i)
|
||||
sched_.expect_draft(serial_row_[(size_t)i].seq, serial_done_);
|
||||
for (int i = 0; i < block_rows_; ++i)
|
||||
@@ -762,6 +1261,7 @@ void Engine::phase_mark(int p) {
|
||||
* error instead of queueing requests nothing will drain (spec §17). */
|
||||
int Engine::fail_loop(int st) {
|
||||
stopping_.store(true, std::memory_order_release);
|
||||
wake_hold();
|
||||
if (barrier_) barrier_->abort();
|
||||
sched_.shutdown("the engine stopped on an error and is not serving", st < 0 ? st : RAD_E_STATE);
|
||||
loop_status_.store(st < 0 ? st : RAD_E_STATE, std::memory_order_release);
|
||||
@@ -814,9 +1314,17 @@ int Engine::step_loop() {
|
||||
if (!ahead && sched_.encodes_pending()) {
|
||||
if (const int es = run_encodes(); es < 0) return fail_loop(es);
|
||||
}
|
||||
if (!ahead) {
|
||||
flex_service();
|
||||
quiet_service();
|
||||
flex_room_tick(rad_mono_ns());
|
||||
}
|
||||
const bool issued = ahead != nullptr;
|
||||
const RadBatch* b = ahead;
|
||||
ahead = nullptr;
|
||||
/* Read before the step looks for work, so a request added after it found none still wakes
|
||||
* the idle wait below. */
|
||||
const uint64_t woke = issued ? 0 : sched_.wake_seq();
|
||||
if (issued) sched_.promote_next();
|
||||
else b = sched_.step();
|
||||
if (!b) {
|
||||
@@ -837,10 +1345,20 @@ int Engine::step_loop() {
|
||||
sched_.release_deferred();
|
||||
}
|
||||
tier_tick();
|
||||
/* A park waits here once the tiers' copies have landed, before the idle work that
|
||||
* would touch the card (the loan's fills). */
|
||||
if (hold_req_.load(std::memory_order_acquire) && !(pump_ && !pump_->copy.empty())) {
|
||||
hold_point(&step_watch);
|
||||
continue;
|
||||
}
|
||||
expire_tick(rad_mono_ns());
|
||||
loan_tick(/*idle=*/true);
|
||||
xflex_tick(/*busy=*/false);
|
||||
/* A request (or a cancel, a park, a quiet call) wakes the wait at once; otherwise it
|
||||
* lengthens, up to 20 ms, which the periodic idle work above tolerates. Polling at
|
||||
* 1 ms woke an idle engine a thousand times a second. */
|
||||
if (idle < 200) ++idle;
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(idle * 5));
|
||||
sched_.wait_idle(woke, std::chrono::microseconds(idle * 100));
|
||||
continue;
|
||||
}
|
||||
idle = 0;
|
||||
@@ -1152,6 +1670,7 @@ int Engine::step_loop() {
|
||||
sched_.step_finished();
|
||||
tier_tick();
|
||||
loan_tick(/*idle=*/false);
|
||||
xflex_tick(/*busy=*/true);
|
||||
const uint64_t now = rad_mono_ns();
|
||||
arena_tick(now);
|
||||
expire_tick(now);
|
||||
@@ -2039,6 +2558,7 @@ void Engine::clear_fresh_states() {
|
||||
}
|
||||
|
||||
int Engine::serve(std::unique_ptr<StallWatch> watch) {
|
||||
EngineScope es(owner_, cfg_.served_model_name.c_str());
|
||||
startup_watch_ = std::move(watch);
|
||||
if (ranks_.empty() || !ranks_[0]->ctx.program()) {
|
||||
RAD_ERR("serve: start() has not run, or stopped at --debug-graph");
|
||||
@@ -2308,8 +2828,17 @@ int Engine::serve(std::unique_ptr<StallWatch> watch) {
|
||||
done_.reserve((size_t)cfg_.max_seqs);
|
||||
|
||||
rank_threads_.clear();
|
||||
for (int i = 0; i < cfg_.tp; ++i) rank_threads_.emplace_back([this, i] { rank_loop(i); });
|
||||
std::thread stepper([this] { if (gen_) gen_loop(); else step_loop(); });
|
||||
for (int i = 0; i < cfg_.tp; ++i)
|
||||
rank_threads_.emplace_back([this, i] {
|
||||
EngineScope es(owner_, cfg_.served_model_name.c_str());
|
||||
rank_loop(i);
|
||||
});
|
||||
std::atomic<bool> stepped{false};
|
||||
std::thread stepper([this, &stepped] {
|
||||
EngineScope es(owner_, cfg_.served_model_name.c_str());
|
||||
if (gen_) gen_loop(); else step_loop();
|
||||
stepped.store(true, std::memory_order_release);
|
||||
});
|
||||
|
||||
if (cfg_.kld() || cfg_.calib()) {
|
||||
print_startup_report(RAD_OK);
|
||||
@@ -2321,7 +2850,16 @@ int Engine::serve(std::unique_ptr<StallWatch> watch) {
|
||||
/* Stop the step loop before the server's storage is destroyed: Request::sink points into it,
|
||||
* and an unfinished request still writes there (server.h). */
|
||||
stopping_.store(true, std::memory_order_release);
|
||||
wake_hold();
|
||||
barrier_->abort();
|
||||
/* A step loop that does not come back holds its cards and every model waiting for them, and
|
||||
* only a restart can free them, so past five minutes the process exits as for a GPU fault. */
|
||||
for (int t = 0; !stepped.load(std::memory_order_acquire); ++t) {
|
||||
if (t == 30000)
|
||||
fatal("model %s: the step loop has not come back five minutes after it was stopped; "
|
||||
"exiting so a restart can bring the server back", cfg_.served_model_name.c_str());
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
if (stepper.joinable()) stepper.join();
|
||||
for (auto& t : rank_threads_) if (t.joinable()) t.join();
|
||||
rank_threads_.clear();
|
||||
@@ -2355,7 +2893,7 @@ bool Engine::state_slots_of(uint64_t seq, std::vector<int32_t>* out) const {
|
||||
* what a client should test before sending the field: without a loaded vector a request that asks
|
||||
* for it is refused, so a UI can hide the control rather than offer a switch that fails. */
|
||||
std::string Engine::cvec_json() const {
|
||||
nlohmann::ordered_json o;
|
||||
Json o;
|
||||
o["available"] = cvec_.loaded();
|
||||
o["default"] = cvec_default_.load();
|
||||
if (cvec_.loaded()) {
|
||||
@@ -2377,7 +2915,7 @@ std::string Engine::cvec_json() const {
|
||||
* The server holds only interfaces, so the engine renders this object and the server splices it in
|
||||
* verbatim. */
|
||||
std::string Engine::engine_info() const {
|
||||
nlohmann::ordered_json e;
|
||||
Json e;
|
||||
e["arch"] = meta_.arch_id ? meta_.arch_id : "";
|
||||
e["quant"] = meta_.quant ? meta_.quant : "";
|
||||
e["n_layers"] = meta_.n_layers;
|
||||
@@ -2389,7 +2927,6 @@ std::string Engine::engine_info() const {
|
||||
e["tensor_parallel"] = cfg_.tp;
|
||||
e["tp_wire"] = cfg_.tp > 1 ? (cfg_.tp_wire_exact ? "exact" : "wht6") : "n/a";
|
||||
e["tp_wire_min_kb"] = cfg_.tp_wire_exact ? 0 : cfg_.tp_wire_min_kb;
|
||||
e["attn_prefill_fp8"] = attn_prefill_fp8_name(cfg_.attn_prefill_fp8);
|
||||
e["tp_attn_split"] = cfg_.tp_attn_rows ? "rows" : "heads";
|
||||
e["weight_prefetch"] = cfg_.weight_prefetch;
|
||||
e["max_num_batched_tokens"] = cfg_.max_tok;
|
||||
@@ -2402,15 +2939,15 @@ std::string Engine::engine_info() const {
|
||||
e["vision_tower_placement"] = { { "asked", table_placement_name(cfg_.tower_placement) },
|
||||
{ "resolved", table_placement_name(cfg_.tower_resolved) } };
|
||||
e["prefix_cache"] = cfg_.prefix_cache;
|
||||
e["control_vector"] = nlohmann::ordered_json::parse(cvec_json());
|
||||
e["control_vector"] = Json::parse(cvec_json());
|
||||
e["checkpoint_slots"] = cfg_.checkpoint_slots;
|
||||
e["deterministic"] = cfg_.deterministic;
|
||||
|
||||
nlohmann::ordered_json devs = nlohmann::ordered_json::array();
|
||||
Json devs = Json::array();
|
||||
for (const auto& r : ranks_) {
|
||||
RadDeviceProps p{};
|
||||
if (rad_dev_props(r->device, &p) < 0) continue;
|
||||
nlohmann::ordered_json d;
|
||||
Json d;
|
||||
d["rank"] = r->index;
|
||||
d["device"] = r->device;
|
||||
d["name"] = p.name;
|
||||
@@ -2427,13 +2964,13 @@ std::string Engine::engine_info() const {
|
||||
e["ops"] = (int64_t)p0.ops.size() - 1;
|
||||
e["weights"] = (int64_t)p0.weights.size() - 1;
|
||||
|
||||
nlohmann::ordered_json kvs = nlohmann::ordered_json::array();
|
||||
Json kvs = Json::array();
|
||||
const KVManager& kv = ranks_[0]->kv;
|
||||
for (int32_t g = 0; g < kv.n_groups(); ++g) {
|
||||
const KVGroupPlan* pl = kv.plan(g);
|
||||
/* Skip group 0, the sentinel that makes handle 0 mean "none". */
|
||||
if (!pl || pl->name.empty() || pl->n_layers <= 0) continue;
|
||||
nlohmann::ordered_json k;
|
||||
Json k;
|
||||
k["name"] = pl->name;
|
||||
k["kind"] = bld_kv_kind_name(pl->kind);
|
||||
k["n_layers"] = pl->n_layers;
|
||||
@@ -2449,7 +2986,7 @@ std::string Engine::engine_info() const {
|
||||
}
|
||||
e["kv_groups"] = std::move(kvs);
|
||||
|
||||
nlohmann::ordered_json sp;
|
||||
Json sp;
|
||||
if (drafter_.kind == RAD_DRAFT_NONE) {
|
||||
sp["enabled"] = false;
|
||||
} else {
|
||||
@@ -2464,18 +3001,7 @@ std::string Engine::engine_info() const {
|
||||
return e.dump();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ a clean stop
|
||||
*
|
||||
* SIGINT and SIGTERM are handled so `~Engine` runs: --profile-ops prints its table, the placement
|
||||
* summary prints, and in-flight requests drain instead of being cut.
|
||||
*
|
||||
* The handler only stores to a lock-free atomic; a watcher thread does the work, because
|
||||
* `Server::stop` takes locks and joins threads, which is not async-signal-safe. A second signal
|
||||
* gets the default disposition, so the operator can still force an immediate kill.
|
||||
*/
|
||||
namespace {
|
||||
std::atomic<bool> g_signalled{false};
|
||||
void on_signal(int) { g_signalled.store(true, std::memory_order_relaxed); }
|
||||
|
||||
/* ================================================================== sampling defaults */
|
||||
/* The sampler for a request that sends no sampler settings. Parsing and range checks live in
|
||||
@@ -2777,6 +3303,8 @@ int Engine::run_server() {
|
||||
|
||||
server::Deps d{};
|
||||
d.sched = &sched_bridge;
|
||||
d.background = &background_jobs_;
|
||||
d.front = front_;
|
||||
d.tok = &tok_bridge;
|
||||
d.grammar = &grammar_bridge;
|
||||
d.live = snap;
|
||||
@@ -2901,6 +3429,13 @@ int Engine::run_server() {
|
||||
if (cfg_.max_n <= 0)
|
||||
decide("--max-n", std::to_string(o.max_n), "the larger of 8 and --max-num-seqs");
|
||||
o.max_ctx = cfg_.max_ctx ? cfg_.max_ctx : meta_.n_ctx_train;
|
||||
if (!gen_ && !ranks_.empty()) {
|
||||
o.kv_cache_dtype = cfg_.kv_cache_dtype;
|
||||
for (const KVGroupInfo& kg : ranks_[0]->program.kv_groups)
|
||||
if (kg.block_size > 0) { o.kv_block_size = kg.block_size; break; }
|
||||
o.prefix_cache = cfg_.prefix_cache;
|
||||
o.prefix_cache_mib = cfg_.prefix_cache_mib;
|
||||
}
|
||||
o.model_path = cfg_.model;
|
||||
o.default_reasoning_effort = cfg_.reasoning_effort;
|
||||
o.reasoning_effort_map = cfg_.reasoning_effort_map;
|
||||
@@ -2955,7 +3490,7 @@ int Engine::run_server() {
|
||||
o.engine_info = engine_info();
|
||||
/* Per request, since OpInfo::issued changes once the run phase runs
|
||||
* (ServerOptions::graph_json). */
|
||||
o.graph_json = [this] { return dump_graph_json(ranks_[0]->program); };
|
||||
o.graph_json = [this] { return dump_graph_json(ranks_[0]->program, reg_.get()); };
|
||||
/* Per request, from a reading at most a second old (Engine::diagnostics_json). */
|
||||
o.diagnostics_json = [this] { return diagnostics_json(); };
|
||||
if (cfg_.lora.enabled() && ranks_[0]->program.is_pass_driven()) {
|
||||
@@ -3549,6 +4084,8 @@ int Engine::run_server() {
|
||||
: "takes no images or video");
|
||||
|
||||
server::Server srv(d, o);
|
||||
srv_ = &srv;
|
||||
struct ClearSrv { server::Server** p; ~ClearSrv() { *p = nullptr; } } clear_srv{ &srv_ };
|
||||
if (cfg_.max_queued_requests <= 0)
|
||||
decide("--max-queued-requests", std::to_string(srv.queue_cap()), "8 x --max-num-seqs");
|
||||
|
||||
@@ -3602,26 +4139,26 @@ int Engine::run_server() {
|
||||
why.c_str());
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
print_startup_report(RAD_OK);
|
||||
if (!cfg_.report_once) print_startup_report(RAD_OK);
|
||||
const char* const keyed = o.api_key.empty() ? "" : "; a bearer key is required";
|
||||
if (embedding_)
|
||||
RAD_INFO("serving on http://%s:%d (/v1/embeddings; completions and chat are 501%s)",
|
||||
o.host.c_str(), o.port, keyed);
|
||||
RAD_INFO("model %s serves (/v1/embeddings; completions and chat are 501%s)",
|
||||
cfg_.served_model_name.c_str(), keyed);
|
||||
else if (aud_gen_)
|
||||
RAD_INFO("serving on http://%s:%d (/v1/audio/speech; completions and chat are 501%s)",
|
||||
o.host.c_str(), o.port, keyed);
|
||||
RAD_INFO("model %s serves (/v1/audio/speech; completions and chat are 501%s)",
|
||||
cfg_.served_model_name.c_str(), keyed);
|
||||
else if (vid_gen_)
|
||||
RAD_INFO("serving on http://%s:%d (/v1/videos; completions and chat are 501%s)",
|
||||
o.host.c_str(), o.port, keyed);
|
||||
RAD_INFO("model %s serves (/v1/videos; completions and chat are 501%s)",
|
||||
cfg_.served_model_name.c_str(), keyed);
|
||||
else if (shp_gen_)
|
||||
RAD_INFO("serving on http://%s:%d (/generate, /send and /status/{uid}; completions and "
|
||||
"chat are 501%s)", o.host.c_str(), o.port, keyed);
|
||||
RAD_INFO("model %s serves (/generate, /send and /status/{uid}; completions and "
|
||||
"chat are 501%s)", cfg_.served_model_name.c_str(), keyed);
|
||||
else if (gen_)
|
||||
RAD_INFO("serving on http://%s:%d (/v1/images/generations%s; completions and chat are "
|
||||
"501%s)", o.host.c_str(), o.port,
|
||||
RAD_INFO("model %s serves (/v1/images/generations%s; completions and chat are "
|
||||
"501%s)", cfg_.served_model_name.c_str(),
|
||||
d.images && d.images->info().max_refs > 0 ? " and /v1/images/edits" : "", keyed);
|
||||
else
|
||||
RAD_INFO("serving on http://%s:%d (/v1/completions%s%s)", o.host.c_str(), o.port,
|
||||
RAD_INFO("model %s serves (/v1/completions%s%s)", cfg_.served_model_name.c_str(),
|
||||
d.chat ? "; /v1/chat/completions with tools and reasoning" : "; chat is 501",
|
||||
keyed);
|
||||
|
||||
@@ -3635,43 +4172,52 @@ int Engine::run_server() {
|
||||
live->start();
|
||||
}
|
||||
|
||||
struct sigaction sa {};
|
||||
sa.sa_handler = on_signal;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_flags = SA_RESETHAND; /* a second one takes the default disposition and kills */
|
||||
sigaction(SIGINT, &sa, nullptr);
|
||||
sigaction(SIGTERM, &sa, nullptr);
|
||||
|
||||
std::atomic<bool> watching{true};
|
||||
std::thread watcher([&] {
|
||||
while (watching.load(std::memory_order_relaxed)) {
|
||||
if (g_signalled.load(std::memory_order_relaxed)) {
|
||||
RAD_INFO("stopping: draining in-flight requests");
|
||||
srv.stop();
|
||||
return;
|
||||
}
|
||||
/* The step loop died and its requests were already failed (Engine::fail_loop); stop
|
||||
* the transport so no new request is queued. */
|
||||
if (loop_status_.load(std::memory_order_acquire) < 0) {
|
||||
RAD_ERR("stopping: the step loop failed with %s",
|
||||
rad_strerror(loop_status_.load(std::memory_order_acquire)));
|
||||
srv.stop();
|
||||
return;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
});
|
||||
|
||||
/* A model serves behind the process's front (server/front.h); the process's signals are the
|
||||
* fleet's (fleet/fleet.cpp). This model ends when the front stops or the fleet asks it to. */
|
||||
if (!front_) {
|
||||
RAD_ERR("model %s: no front to serve behind", cfg_.served_model_name.c_str());
|
||||
if (live) live->stop();
|
||||
return RAD_E_STATE;
|
||||
}
|
||||
/* Startup ends here; a quiet server from now on is an idle one. */
|
||||
startup_watch_.reset();
|
||||
srv.note_ready(startup_begin_ns_);
|
||||
const int rc = srv.run();
|
||||
/* The watcher tests `watching` first, so clearing it ends the thread without a second stop;
|
||||
* run() has already torn the transport down. Joining costs up to one sleep interval. */
|
||||
watching.store(false, std::memory_order_relaxed);
|
||||
watcher.join();
|
||||
front_->attach(cfg_.served_model_name, front_aliases_, &srv);
|
||||
serving_.store(true, std::memory_order_release);
|
||||
RAD_INFO("model %s: ready", cfg_.served_model_name.c_str());
|
||||
while (!front_->stopping() && !stop_requested_.load(std::memory_order_acquire)) {
|
||||
if (loop_status_.load(std::memory_order_acquire) < 0) {
|
||||
RAD_ERR("model %s: stopping: the step loop failed with %s",
|
||||
cfg_.served_model_name.c_str(),
|
||||
rad_strerror(loop_status_.load(std::memory_order_acquire)));
|
||||
break;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
serving_.store(false, std::memory_order_release);
|
||||
/* The responses already handed out run to their end, for as long as the stop allows: a
|
||||
* stream holds its lease until the client has it all or leaves. Past that the server ends
|
||||
* them (Server::retire stops every stream and job), and they let go at once. */
|
||||
const int64_t drain = stop_drain_ms_.load(std::memory_order_relaxed);
|
||||
if (!front_->detach(cfg_.served_model_name, (int)std::min<int64_t>(drain, INT32_MAX))) {
|
||||
RAD_WARN("model %s: responses still open after %lld ms; ending them",
|
||||
cfg_.served_model_name.c_str(), (long long)drain);
|
||||
srv.retire();
|
||||
/* `srv` lives in this frame and a response names it to the last, so it cannot go while one
|
||||
* is held. Past five minutes the model is wedged, and the process exits as for a GPU fault
|
||||
* so a restart brings the others back. */
|
||||
for (int min = 1; !front_->detach(cfg_.served_model_name, 60000); ++min) {
|
||||
if (min >= 5)
|
||||
fatal("model %s: responses still held %d minutes after they were ended; exiting "
|
||||
"so a restart can bring the server back", cfg_.served_model_name.c_str(), min);
|
||||
RAD_WARN("model %s: responses still held %d minute(s) after they were ended",
|
||||
cfg_.served_model_name.c_str(), min);
|
||||
}
|
||||
}
|
||||
srv.retire();
|
||||
if (live) live->stop();
|
||||
return rc;
|
||||
const int st = loop_status_.load(std::memory_order_acquire);
|
||||
return st < 0 ? st : RAD_OK;
|
||||
}
|
||||
|
||||
} /* namespace rad */
|
||||
Reference in new issue
Block a user