mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-27 07:40:36 +00:00
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
174 lines
5.6 KiB
C++
174 lines
5.6 KiB
C++
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <mutex>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
// cpp-httplib must include Winsock2 before other headers include windows.h.
|
|
#include "server_access.h"
|
|
|
|
#include "async_jobs.h"
|
|
#include "common/common.h"
|
|
#include "common/resource_owners.hpp"
|
|
#include "model_catalog.h"
|
|
#include "routes.h"
|
|
#include "runtime.h"
|
|
|
|
#ifdef HAVE_INDEX_HTML
|
|
#include "frontend/dist/gen_index_html.h"
|
|
#endif
|
|
|
|
static void print_usage(const char* argv0, const std::vector<ArgOptions>& options_list) {
|
|
std::cout << version_string() << "\n";
|
|
std::cout << "Usage: " << argv0 << " [options]\n\n";
|
|
std::cout << "Svr Options:\n";
|
|
options_list[0].print();
|
|
std::cout << "\nContext Options:\n";
|
|
options_list[1].print();
|
|
std::cout << "\nDefault Generation Options:\n";
|
|
options_list[2].print();
|
|
}
|
|
|
|
static void parse_args(int argc,
|
|
const char** argv,
|
|
SDSvrParams& svr_params,
|
|
SDContextParams& ctx_params,
|
|
SDGenerationParams& default_gen_params) {
|
|
std::vector<ArgOptions> options_vec = {
|
|
svr_params.get_options(),
|
|
ctx_params.get_options(),
|
|
default_gen_params.get_options(),
|
|
};
|
|
svr_params.add_access_options(options_vec[0], options_vec);
|
|
|
|
if (!parse_options(argc, argv, options_vec)) {
|
|
print_usage(argv[0], options_vec);
|
|
exit(svr_params.normal_exit ? 0 : 1);
|
|
}
|
|
|
|
log_level = svr_params.log_level;
|
|
log_color = svr_params.color;
|
|
|
|
if (ctx_params.list_devices) {
|
|
exit(ctx_params.print_devices() ? 0 : 1);
|
|
}
|
|
|
|
if (!svr_params.resolve_and_validate() ||
|
|
!ctx_params.resolve_and_validate(ctx_params.model_path.empty() && ctx_params.diffusion_model_path.empty() ? METADATA : IMG_GEN) ||
|
|
!default_gen_params.resolve_and_validate(IMG_GEN,
|
|
ctx_params.lora_model_dir,
|
|
ctx_params.hires_upscalers_dir)) {
|
|
print_usage(argv[0], options_vec);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
|
|
SDSvrParams* svr_params = (SDSvrParams*)data;
|
|
log_print(level, log, svr_params->log_level, svr_params->color);
|
|
}
|
|
|
|
int main(int argc, const char** argv) {
|
|
if (argc > 1 && std::string(argv[1]) == "--version") {
|
|
std::cout << version_string() << "\n";
|
|
return EXIT_SUCCESS;
|
|
}
|
|
SDSvrParams svr_params;
|
|
SDContextParams ctx_params;
|
|
// Preserve the CLI default while disabling cwd scans for sd-server.
|
|
ctx_params.lora_model_dir.clear();
|
|
SDGenerationParams default_gen_params;
|
|
|
|
sd_set_log_callback(sd_log_cb, (void*)&svr_params);
|
|
parse_args(argc, argv, svr_params, ctx_params, default_gen_params);
|
|
|
|
LOG_VERBOSE("version: %s", version_string().c_str());
|
|
LOG_VERBOSE("%s", sd_get_system_info());
|
|
LOG_DEBUG("%s", svr_params.to_string().c_str());
|
|
LOG_DEBUG("%s", ctx_params.to_string().c_str());
|
|
LOG_DEBUG("%s", default_gen_params.to_string().c_str());
|
|
|
|
if (!ctx_params.register_rpc_devices()) {
|
|
return 1;
|
|
}
|
|
ctx_params.rpc_servers.reset();
|
|
|
|
SDOptPtr sd_ctx_params = ctx_params.to_context_opt();
|
|
ServerModels models;
|
|
models.context.reset(create_sd_context(sd_ctx_params));
|
|
if (!models.context) {
|
|
LOG_ERROR("sd_ctx_create failed: %s", sd_get_last_error());
|
|
return 1;
|
|
}
|
|
try {
|
|
configure_server_file_access(models, svr_params, ctx_params);
|
|
if (!sd_dgl_ctx_set(sd_ctx_get_dgl_ctx(models.context.get()), "console_progress", "on"))
|
|
throw std::runtime_error(sd_dgl_error());
|
|
} catch (const std::exception& error) {
|
|
LOG_ERROR("Context setup failed: %s", error.what());
|
|
return 1;
|
|
}
|
|
|
|
std::mutex sd_ctx_mutex;
|
|
|
|
std::vector<LoraEntry> lora_cache;
|
|
std::mutex lora_mutex;
|
|
std::vector<UpscalerEntry> upscaler_cache;
|
|
std::mutex upscaler_mutex;
|
|
AsyncJobManager async_job_manager;
|
|
async_job_manager.max_pending_jobs =
|
|
static_cast<size_t>(svr_params.max_pending_jobs);
|
|
async_job_manager.completed_ttl_seconds =
|
|
static_cast<int64_t>(svr_params.completed_ttl_seconds);
|
|
ServerRuntime runtime = {
|
|
models.context.get(),
|
|
&sd_ctx_mutex,
|
|
&svr_params,
|
|
&ctx_params,
|
|
&default_gen_params,
|
|
&lora_cache,
|
|
&lora_mutex,
|
|
&upscaler_cache,
|
|
&upscaler_mutex,
|
|
&async_job_manager,
|
|
&models,
|
|
};
|
|
|
|
try {
|
|
initialize_server_models(runtime);
|
|
} catch (const std::exception& e) {
|
|
LOG_ERROR("model catalog initialization failed: %s", e.what());
|
|
return 1;
|
|
}
|
|
|
|
std::thread async_worker(async_job_worker, std::ref(runtime));
|
|
|
|
SDHttpServer svr(svr_params);
|
|
|
|
std::string index_html;
|
|
#ifdef HAVE_INDEX_HTML
|
|
index_html.assign(reinterpret_cast<const char*>(index_html_bytes), index_html_size);
|
|
#else
|
|
index_html = "Stable Diffusion Server is running";
|
|
#endif
|
|
register_index_endpoints(svr, svr_params, index_html);
|
|
register_media_view_endpoint(svr, svr_params);
|
|
register_openai_api_endpoints(svr, runtime);
|
|
register_sdapi_endpoints(svr, runtime);
|
|
register_sdcpp_api_endpoints(svr, runtime);
|
|
register_dgl_endpoints(svr, runtime);
|
|
register_model_endpoints(svr, runtime);
|
|
|
|
LOG_INFO("listening on: http://%s:%d\n", svr_params.listen_ip.c_str(), svr_params.listen_port);
|
|
svr.listen(svr_params.listen_ip, svr_params.listen_port);
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(async_job_manager.mutex);
|
|
async_job_manager.stop = true;
|
|
}
|
|
async_job_manager.cv.notify_all();
|
|
async_worker.join();
|
|
return 0;
|
|
}
|