| 529 | } |
| 530 | |
| 531 | void server_models::load(const std::string & name) { |
| 532 | if (!has_model(name)) { |
| 533 | throw std::runtime_error("model name=" + name + " is not found"); |
| 534 | } |
| 535 | unload_lru(); |
| 536 | |
| 537 | std::lock_guard<std::mutex> lk(mutex); |
| 538 | |
| 539 | auto meta = mapping[name].meta; |
| 540 | if (meta.status != SERVER_MODEL_STATUS_UNLOADED) { |
| 541 | SRV_INF("model %s is not ready\n", name.c_str()); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | // Re-check capacity under the lock to prevent concurrent loads from |
| 546 | // exceeding models_max. Without this, the window between unload_lru() |
| 547 | // releasing its lock and this lock_guard acquiring allows multiple |
| 548 | // threads to each observe capacity and all proceed to load. |
| 549 | if (base_params.models_max > 0) { |
| 550 | size_t count_active = 0; |
| 551 | for (const auto & m : mapping) { |
| 552 | if (m.second.meta.is_running()) { |
| 553 | count_active++; |
| 554 | } |
| 555 | } |
| 556 | if (count_active >= (size_t)base_params.models_max) { |
| 557 | throw std::runtime_error("model limit reached, try again later"); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | // prepare new instance info |
| 562 | instance_t inst; |
| 563 | inst.meta = meta; |
| 564 | inst.meta.port = get_free_port(); |
| 565 | inst.meta.status = SERVER_MODEL_STATUS_LOADING; |
| 566 | inst.meta.last_used = ggml_time_ms(); |
| 567 | |
| 568 | if (inst.meta.port <= 0) { |
| 569 | throw std::runtime_error("failed to get a port number"); |
| 570 | } |
| 571 | |
| 572 | inst.subproc = std::make_shared<subprocess_s>(); |
| 573 | { |
| 574 | SRV_INF("spawning server instance with name=%s on port %d\n", inst.meta.name.c_str(), inst.meta.port); |
| 575 | |
| 576 | inst.meta.update_args(ctx_preset, bin_path); // render args |
| 577 | |
| 578 | std::vector<std::string> child_args = inst.meta.args; // copy |
| 579 | std::vector<std::string> child_env = base_env; // copy |
| 580 | child_env.push_back("LLAMA_SERVER_ROUTER_PORT=" + std::to_string(base_params.port)); |
| 581 | |
| 582 | SRV_INF("%s", "spawning server instance with args:\n"); |
| 583 | for (const auto & arg : child_args) { |
| 584 | SRV_INF(" %s\n", arg.c_str()); |
| 585 | } |
| 586 | inst.meta.args = child_args; // save for debugging |
| 587 | |
| 588 | std::vector<char *> argv = to_char_ptr_array(child_args); |
no test coverage detected