stable-diffusion.cpp/examples/server/routes_models.cpp
leejet 3b12781fac
Some checks are pending
CI / ubuntu-latest-cmake (push) Waiting to run
CI / ubuntu-latest-cmake-vulkan (push) Waiting to run
CI / Build and push container images (push) Waiting to run
CI / macOS-latest-cmake (push) Waiting to run
CI / windows-latest-cmake (cpu, -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_LIBS=ON -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Waiting to run
CI / windows-latest-cmake (cuda12, -DSD_CUDA=ON -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_CUDA_COMPILER_LAUNCHER=ccache -DSD_BUILD_SHARED_LIBS=ON -DCMAKE_CUDA_ARCHITECTURES='61;70;75;80;86;89;90;100;120' -DCMAKE_CUDA_FL… (push) Waiting to run
CI / windows-latest-cmake (vulkan, -DSD_VULKAN=ON -DSD_BUILD_SHARED_LIBS=ON -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Waiting to run
CI / windows-latest-rocm (push) Waiting to run
CI / ubuntu-latest-rocm (10.1.0, x64, gfx900;gfx906;gfx908;gfx90a;gfx90c;gfx942;gfx950;gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1153;gfx1200;gfx1201) (push) Waiting to run
CI / release (push) Blocked by required conditions
feat(server): add idle TTLs for model weights (#2406)
2026-11-24 01:17:22 +08:00

97 lines
4.1 KiB
C++

#include "routes.h"
#include "async_jobs.h"
#include "model_catalog.h"
class SynchronousContextJob {
AsyncJobManager& manager_;
const SDSvrParams& params_;
public:
explicit SynchronousContextJob(const ServerRuntime& runtime)
: manager_(*runtime.async_job_manager), params_(*runtime.svr_params) {
std::lock_guard<std::mutex> lock(manager_.mutex);
++manager_.synchronous_jobs;
}
SynchronousContextJob(const SynchronousContextJob&) = delete;
SynchronousContextJob& operator=(const SynchronousContextJob&) = delete;
~SynchronousContextJob() {
{
std::lock_guard<std::mutex> lock(manager_.mutex);
--manager_.synchronous_jobs;
manager_.record_context_job_completion(params_);
}
manager_.cv.notify_one();
}
};
static void model_error(httplib::Response& res, int status, const std::string& code, const std::string& message) {
res.status = status;
res.set_content(json({{"error", message}, {"code", code}}).dump(), "application/json");
}
httplib::Server::Handler RuntimeRoutes::protect(httplib::Server::Handler handler) {
ServerRuntime* runtime = &runtime_;
return [runtime, handler](const httplib::Request& req, httplib::Response& res) {
const bool generation = req.path == "/sdapi/v1/txt2img" || req.path == "/sdapi/v1/img2img" ||
req.path == "/v1/images/generations" || req.path == "/v1/images/edits";
if (generation) {
SynchronousContextJob job(*runtime);
std::lock_guard<std::mutex> lock(*runtime->sd_ctx_mutex);
if (!has_current_model(*runtime)) {
model_error(res, 503, "model_not_loaded", "No current model is configured");
return;
}
handler(req, res);
} else {
handler(req, res);
}
};
}
void RuntimeRoutes::get(const std::string& pattern, httplib::Server::Handler handler) {
if (pattern.find("/sdcpp/v1/jobs/") != std::string::npos) {
server_.Get(pattern, handler);
return;
}
ServerModels* models = runtime_.models;
server_.Get(pattern, [models, handler](const httplib::Request& req, httplib::Response& res) {
std::shared_lock<std::shared_mutex> lock(models->model_mutex);
handler(req, res);
});
}
void RuntimeRoutes::post(const std::string& pattern, httplib::Server::Handler handler) {
server_.Post(pattern, protect(handler));
}
void register_model_endpoints(httplib::Server& svr, ServerRuntime& runtime) {
svr.Get("/sdcpp/v1/recipes", [&runtime](const httplib::Request&, httplib::Response& res) {
auto& models = *runtime.models;
std::lock_guard<std::mutex> lock(models.catalog_mutex);
json items = json::array();
for (const auto& entry : models.recipes)
items.push_back(entry.second);
res.set_content(json({{"recipes", items}, {"errors", models.catalog_errors}}).dump(), "application/json");
});
svr.Get("/sdcpp/v1/model-files", [&runtime](const httplib::Request&, httplib::Response& res) {
auto& models = *runtime.models;
std::lock_guard<std::mutex> lock(models.catalog_mutex);
json items = json::array();
for (const auto& entry : models.files) {
items.push_back({{"id", entry.first}, {"name", entry.second.name}, {"category", entry.second.category}, {"root", entry.second.root.id}, {"relative_path", entry.second.relative_path}});
}
res.set_content(json({{"files", items}, {"errors", models.catalog_errors}}).dump(), "application/json");
});
svr.Post("/sdcpp/v1/models/refresh", [&runtime](const httplib::Request&, httplib::Response& res) {
refresh_model_catalog(runtime);
std::lock_guard<std::mutex> lock(runtime.models->catalog_mutex);
res.set_content(json({{"errors", runtime.models->catalog_errors}}).dump(), "application/json");
});
svr.Get("/sdcpp/v1/models/status", [&runtime](const httplib::Request&, httplib::Response& res) {
std::shared_lock<std::shared_mutex> lock(runtime.models->model_mutex);
res.set_content(model_status(runtime).dump(), "application/json");
});
}