File size: 20,330 Bytes
89565fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | #include "server-models-sched.h"
#include "server-models.h"
#include "fit.h"
#include <algorithm>
#include <cinttypes>
static constexpr int MEASURE_TIMEOUT = 300; // seconds, then the measure child is killed
//
// server_model_memory
//
server_model_memory::server_model_memory(uint32_t n_ctx, const std::vector<common_memory_breakdown_row> & rows) : n_ctx(n_ctx) {
for (const auto & row : rows) {
device_t d;
switch (row.kind) {
case common_memory_breakdown_row::DEVICE: d.kind = "device"; break;
case common_memory_breakdown_row::HOST: d.kind = "host"; break;
case common_memory_breakdown_row::BUFFER_TYPE: d.kind = "buffer_type"; break;
}
d.name = row.name;
d.total = row.total;
d.free = row.free;
d.model = row.model;
d.context = row.context;
d.compute = row.compute;
devices.push_back(std::move(d));
}
}
json server_model_memory::to_json() const {
json devs = json::array();
for (const auto & d : devices) {
json jd = {
{"kind", d.kind},
{"name", d.name},
{"model", d.model},
{"context", d.context},
{"compute", d.compute},
};
if (d.is_device()) {
jd["total"] = d.total;
jd["free"] = d.free;
}
devs.push_back(std::move(jd));
}
return {
{"n_ctx", n_ctx},
{"devices", devs},
};
}
server_model_memory server_model_memory::from_json(const json & j) {
server_model_memory mem;
mem.n_ctx = j.at("n_ctx").get<uint32_t>();
for (const auto & jd : j.at("devices")) {
device_t d;
d.kind = jd.at("kind").get<std::string>();
d.name = jd.at("name").get<std::string>();
d.total = json_value(jd, "total", (size_t) 0);
d.free = json_value(jd, "free", (size_t) 0);
d.model = jd.at("model").get<size_t>();
d.context = jd.at("context").get<size_t>();
d.compute = jd.at("compute").get<size_t>();
mem.devices.push_back(std::move(d));
}
return mem;
}
json server_model_memory_info::to_json() const {
return {
{"status", server_model_memory_status_to_string(status)},
{"min", min ? min->to_json() : json(nullptr)},
{"max", max ? max->to_json() : json(nullptr)},
{"effective", effective ? effective->to_json() : json(nullptr)},
};
}
//
// request queue
//
bool server_models_sched::has_capacity(std::unique_lock<std::mutex> & lk) {
check_lock(lk);
return models.base_params.models_max <= 0
|| count_running() < (size_t) models.base_params.models_max;
}
std::string server_models_sched::pick_victim(std::unique_lock<std::mutex> & lk) {
check_lock(lk);
std::string victim;
int64_t victim_last_used = 0;
for (const auto & m : models.mapping) {
// a busy model is mid-request, one still coming up has no request to finish
if (m.second.req_count != 0 || !m.second.meta.is_ready_or_sleep()) {
continue;
}
// already on its way out, or a queued request wants it
if (models.stopping_models.count(m.first) || find(m.first)) {
continue;
}
if (victim.empty() || m.second.meta.last_used < victim_last_used) {
victim = m.first;
victim_last_used = m.second.meta.last_used;
}
}
return victim;
}
void server_models_sched::join(std::unique_lock<std::mutex> & lk, const std::string & model_id) {
check_lock(lk);
if (entry_t * e = find(model_id)) {
e->n_waiters++;
SRV_INF("request for name=%s joined the queue, %d waiting\n", model_id.c_str(), e->n_waiters);
return;
}
queue.push_back({ model_id, 1, false });
SRV_INF("request for name=%s queued at position %zu\n",
model_id.c_str(), queue.size());
}
void server_models_sched::leave(std::unique_lock<std::mutex> & lk, const std::string & model_id) {
check_lock(lk);
for (auto it = queue.begin(); it != queue.end(); ++it) {
if (it->model_id == model_id) {
if (--it->n_waiters <= 0) {
queue.erase(it); // last one waiting for this model went away
}
return;
}
}
}
bool server_models_sched::queue_empty(std::unique_lock<std::mutex> & lk) {
check_lock(lk);
return queue.empty();
}
bool server_models_sched::try_claim(std::unique_lock<std::mutex> & lk, const std::string & model_id) {
check_lock(lk);
if (queue.empty() || queue.front().model_id != model_id || queue.front().loading) {
return false;
}
if (!has_capacity(lk)) {
return false;
}
// over the memory margin with every running model busy: wait for one to go idle
if (memory_enabled() && limits_exceeded(lk, model_id, /* verbose */ false) && pick_victim(lk).empty()) {
return false;
}
queue.front().loading = true;
return true;
}
void server_models_sched::claim_done(std::unique_lock<std::mutex> & lk, const std::string & model_id, bool ok) {
check_lock(lk);
if (ok) {
return;
}
for (auto it = queue.begin(); it != queue.end(); ++it) {
if (it->model_id == model_id) {
it->loading = false;
return;
}
}
}
void server_models_sched::tick(std::unique_lock<std::mutex> & lk) {
check_lock(lk);
if (models.base_params.models_max <= 0 || queue.empty()) {
return;
}
int n_running = 0;
int n_stopping = 0;
for (const auto & m : models.mapping) {
if (m.second.meta.is_running()) {
n_running++;
if (models.stopping_models.count(m.first)) {
n_stopping++;
}
}
}
int n_needed = 0;
int n_claimed = 0; // claimed the slot, but load() has not spawned yet
for (const auto & e : queue) {
if (!e.loading) {
n_needed++;
continue;
}
auto it = models.mapping.find(e.model_id);
if (it != models.mapping.end() && !it->second.meta.is_running()) {
n_claimed++;
}
}
int n_free = models.base_params.models_max - n_running + n_stopping - n_claimed;
while (n_free < n_needed) {
std::string victim = pick_victim(lk);
if (victim.empty()) {
return; // all remaining models are busy, wait for a request to end
}
SRV_INF("evicting idle LRU name=%s for a queued request\n", victim.c_str());
models.request_stop(victim);
n_free++;
}
}
server_models_sched::entry_t * server_models_sched::find(const std::string & model_id) {
for (auto & e : queue) {
if (e.model_id == model_id) {
return &e;
}
}
return nullptr;
}
void server_models_sched::check_lock(std::unique_lock<std::mutex> & lk) {
GGML_ASSERT(lk.owns_lock() && lk.mutex() == &models.mutex);
}
size_t server_models_sched::count_running() {
size_t count = 0;
for (const auto & m : models.mapping) {
if (m.second.meta.is_running()) {
count++;
}
}
return count;
}
//
// loading a model
//
bool server_models_sched::limits_exceeded(std::unique_lock<std::mutex> & lk, const std::string & name, bool verbose) {
if (!has_capacity(lk)) {
return true;
}
if (!memory_enabled()) {
return false;
}
const auto missing = deficit(name);
if (missing.empty()) {
return false;
}
bool any_running = false;
for (const auto & m : models.mapping) {
if (m.second.meta.is_running()) {
any_running = true;
break;
}
}
for (const auto & [dev, bytes] : missing) {
if (!verbose) {
break;
}
if (any_running) {
SRV_DBG("model name=%s is short by %zu MiB on %s\n", name.c_str(), bytes / (1024 * 1024), dev.c_str());
} else {
// nothing to unload: min is pessimistic, let --fit in the instance try
SRV_WRN("model name=%s is projected to be short by %zu MiB on %s with nothing else loaded, loading anyway\n",
name.c_str(), bytes / (1024 * 1024), dev.c_str());
}
}
return any_running;
}
void server_models_sched::make_room(const std::string & name) {
if (memory_enabled()) {
// the measure result decides what to unload
std::unique_lock<std::mutex> lk(models.mutex);
enqueue_measure(name, /* front */ true);
auto measured = [this, &name]() {
auto it = models.mapping.find(name);
return it == models.mapping.end() || it->second.meta.memory.is_settled();
};
if (!measured()) {
SRV_INF("waiting for the memory measurement of model name=%s\n", name.c_str());
models.cv.wait(lk, measured);
}
}
while (true) {
std::string lru_model_name;
{
std::unique_lock<std::mutex> lk(models.mutex);
if (!limits_exceeded(lk, name)) {
return;
}
lru_model_name = pick_victim(lk);
}
if (lru_model_name.empty()) {
return; // every running model is busy, load() reports the limit
}
SRV_INF("limits reached, removing LRU name=%s\n", lru_model_name.c_str());
models.unload(lru_model_name);
// wait for unload to complete
{
std::unique_lock<std::mutex> lk(models.mutex);
models.cv.wait(lk, [this, &lru_model_name]() {
return models.mapping[lru_model_name].meta.status == SERVER_MODEL_STATUS_UNLOADED;
});
}
}
}
//
// memory accounting
//
bool server_models_sched::memory_enabled() const {
return models.base_params.models_memory_margin > 0;
}
void server_models_sched::measure_unmeasured(std::unique_lock<std::mutex> & lk) {
check_lock(lk);
for (const auto & [name, inst] : models.mapping) {
enqueue_measure(name);
}
}
void server_models_sched::on_spawned(std::unique_lock<std::mutex> & lk, const std::string & name) {
check_lock(lk);
reserve(name);
}
void server_models_sched::on_ready(std::unique_lock<std::mutex> & lk, const std::string & name, const std::optional<server_model_memory> & memory) {
check_lock(lk);
if (!memory_enabled()) {
return;
}
auto it = models.mapping.find(name);
if (it == models.mapping.end()) {
return;
}
if (!memory.has_value()) {
reserve(name); // woke up from sleep
return;
}
// use the real usage, not the estimate
auto & info = it->second.meta.memory;
info.effective = memory;
release(name);
reserve(name);
calibrate(*memory, name);
if (info.status == SERVER_MODEL_MEMORY_FAILED && info.n_attempts < 2) {
// the model was maybe not downloaded at measure time, retry
info.status = SERVER_MODEL_MEMORY_UNMEASURED;
enqueue_measure(name);
}
}
void server_models_sched::on_sleeping(std::unique_lock<std::mutex> & lk, const std::string & name) {
check_lock(lk);
release(name);
}
void server_models_sched::on_unloaded(std::unique_lock<std::mutex> & lk, const std::string & name) {
check_lock(lk);
release(name);
auto it = models.mapping.find(name);
if (it != models.mapping.end()) {
it->second.meta.memory.effective.reset();
}
}
void server_models_sched::cancel_measurements(std::unique_lock<std::mutex> & lk) {
check_lock(lk);
measure_queue.clear();
if (measure_proc) {
SRV_INF("stopping measure process for model name=%s\n", measuring.c_str());
measure_proc->terminate();
// the monitor reaps it and clears measuring
}
}
bool server_models_sched::is_measuring(std::unique_lock<std::mutex> & lk) {
check_lock(lk);
return !measuring.empty();
}
void server_models_sched::enqueue_measure(const std::string & name, bool front) {
if (!memory_enabled()) {
return;
}
auto it = models.mapping.find(name);
if (it == models.mapping.end() || it->second.meta.memory.status != SERVER_MODEL_MEMORY_UNMEASURED) {
return;
}
auto pos = std::find(measure_queue.begin(), measure_queue.end(), name);
if (pos != measure_queue.end()) {
if (!front) {
return; // already queued
}
measure_queue.erase(pos);
}
if (front) {
measure_queue.push_front(name);
} else {
measure_queue.push_back(name);
}
measure_next();
}
void server_models_sched::measure_next() {
if (!measuring.empty()) {
return; // one measure child at a time
}
while (!measure_queue.empty()) {
std::string name = std::move(measure_queue.front());
measure_queue.pop_front();
auto it = models.mapping.find(name);
if (it == models.mapping.end() || it->second.meta.memory.status != SERVER_MODEL_MEMORY_UNMEASURED) {
continue; // removed or measured meanwhile
}
auto & meta = it->second.meta;
// same args as the instance, --offline: never download
std::vector<std::string> child_args = meta.args;
child_args.push_back("--offline");
std::vector<std::string> child_env = models.base_env;
child_env.push_back(std::string(SERVER_ENV_ROUTER_PORT) + "=" + std::to_string(models.base_params.port));
child_env.push_back(std::string(SERVER_ENV_CHILD_ROLE) + "=" + server_role_type_to_str(SERVER_ROLE_MEASURE));
auto proc = std::make_shared<server_subproc>();
if (!proc->sproc.create(child_args, subprocess_option_no_window, child_env)) {
SRV_ERR("failed to spawn measure process for model name=%s\n", name.c_str());
meta.memory.status = SERVER_MODEL_MEMORY_FAILED;
continue;
}
SRV_INF("measuring memory for model name=%s\n", name.c_str());
meta.memory.status = SERVER_MODEL_MEMORY_MEASURING;
meta.memory.n_attempts++;
measuring = name;
measure_proc = proc;
models.watch_child(name, proc, SERVER_ROLE_MEASURE, 0, MEASURE_TIMEOUT);
return;
}
}
void server_models_sched::on_measure_exit(const std::string & name, const std::shared_ptr<server_subproc> & proc, int exit_code) {
std::lock_guard<std::mutex> lk(models.mutex);
if (measure_proc != proc) {
return; // not the child we are waiting for
}
measure_proc.reset();
measuring.clear();
auto it = models.mapping.find(name);
if (it != models.mapping.end() && it->second.meta.memory.status == SERVER_MODEL_MEMORY_MEASURING) {
// exited without a result: crashed, or killed after the timeout
SRV_WRN("measure process for model name=%s exited with status %d before reporting, memory limits will not apply to it\n",
name.c_str(), exit_code);
it->second.meta.memory.status = SERVER_MODEL_MEMORY_FAILED;
models.notify_sse("model_memory", name, it->second.meta.memory.to_json());
}
models.cv.notify_all();
measure_next();
}
void server_models_sched::handle_measured(const std::string & name, const json & payload) {
std::lock_guard<std::mutex> lk(models.mutex);
auto it = models.mapping.find(name);
if (it == models.mapping.end()) {
return;
}
auto & info = it->second.meta.memory;
const std::string result = json_value(payload, "result", std::string());
bool ok = result == "measured";
if (ok) {
try {
info.min = server_model_memory::from_json(payload.at("min"));
info.max = server_model_memory::from_json(payload.at("max"));
} catch (const std::exception & e) {
SRV_ERR("malformed memory measurement for model name=%s: %s\n", name.c_str(), e.what());
info.min.reset();
info.max.reset();
ok = false;
}
} else {
SRV_WRN("measuring memory for model name=%s failed: %s\n",
name.c_str(), json_value(payload, "error", std::string("unknown error")).c_str());
}
if (ok) {
info.status = SERVER_MODEL_MEMORY_MEASURED;
calibrate(*info.max, ""); // latest view of the devices
const auto & mn = *info.min;
const auto & mx = *info.max;
for (size_t i = 0; i < mn.devices.size() && i < mx.devices.size(); i++) {
if (!mn.devices[i].is_device() || mn.devices[i].name != mx.devices[i].name) {
continue;
}
SRV_INF("model name=%s needs on %s: %zu MiB at n_ctx=%u (min), %zu MiB at n_ctx=%u (max), %zu MiB free\n",
name.c_str(), mn.devices[i].name.c_str(),
mn.devices[i].self() / (1024 * 1024), mn.n_ctx,
mx.devices[i].self() / (1024 * 1024), mx.n_ctx,
mx.devices[i].free / (1024 * 1024));
}
} else {
info.status = SERVER_MODEL_MEMORY_FAILED;
}
models.notify_sse("model_memory", name, info.to_json());
models.cv.notify_all();
}
void server_models_sched::calibrate(const server_model_memory & mem, const std::string & reporter) {
for (const auto & d : mem.devices) {
if (!d.is_device() || d.total == 0) {
continue;
}
// other instances are tracked, so they are not external (CUDA counts them in free, Metal does not)
int64_t tracked = 0;
for (const auto & [name, devs] : held) {
if (name == reporter) {
continue;
}
auto h = devs.find(d.name);
if (h != devs.end()) {
tracked += (int64_t) h->second;
}
}
// a measure child allocates nothing, its free does not include self
const int64_t self = reporter.empty() ? 0 : (int64_t) d.self();
auto & dm = device_memory[d.name];
dm.total = (int64_t) d.total;
dm.external = std::max<int64_t>(0, dm.total - (int64_t) d.free - self - tracked);
SRV_DBG("device %s: total=%" PRId64 " MiB, external=%" PRId64 " MiB, available=%" PRId64 " MiB\n",
d.name.c_str(), dm.total / (1024 * 1024), dm.external / (1024 * 1024), available(d.name) / (1024 * 1024));
}
}
void server_models_sched::reserve(const std::string & name) {
auto it = models.mapping.find(name);
if (it == models.mapping.end() || held.count(name)) {
return;
}
const auto & info = it->second.meta.memory;
// prefer real usage over the estimate
const auto & mem = info.effective ? info.effective : info.min;
if (!mem) {
return;
}
auto & devs = held[name];
for (const auto & d : mem->devices) {
if (d.is_device() && d.total > 0) {
devs[d.name] = d.self();
}
}
}
void server_models_sched::release(const std::string & name) {
held.erase(name);
}
int64_t server_models_sched::available(const std::string & dev) const {
auto dm = device_memory.find(dev);
if (dm == device_memory.end()) {
return INT64_MAX; // never reported, nothing to check against
}
int64_t n_held = 0;
for (const auto & [name, devs] : held) {
auto h = devs.find(dev);
if (h != devs.end()) {
n_held += (int64_t) h->second;
}
}
return dm->second.total - dm->second.external - n_held;
}
std::map<std::string, size_t> server_models_sched::deficit(const std::string & name) const {
std::map<std::string, size_t> res;
auto it = models.mapping.find(name);
if (it == models.mapping.end() || !it->second.meta.memory.min) {
return res;
}
const int64_t margin = (int64_t) models.base_params.models_memory_margin * 1024 * 1024;
for (const auto & d : it->second.meta.memory.min->devices) {
// host memory is assumed unlimited, same as --fit
if (!d.is_device() || d.total == 0) {
continue;
}
if (device_memory.find(d.name) == device_memory.end()) {
continue;
}
const int64_t avail = available(d.name) - margin;
if ((int64_t) d.self() > avail) {
res[d.name] = (size_t) ((int64_t) d.self() - avail);
}
}
return res;
}
|