diff --git a/CMakeLists.txt b/CMakeLists.txt index dc86b6b7..952cfb77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,20 @@ if(SD_FLASH_ATTN) add_definitions(-DSD_USE_FLASH_ATTENTION) endif() +set(SD_LIB stable-diffusion) + +add_library(${SD_LIB} stable-diffusion.h stable-diffusion.cpp model.h model.cpp util.h util.cpp upscaler.cpp + ggml_extend.hpp clip.hpp common.hpp unet.hpp tae.hpp esrgan.hpp lora.hpp denoiser.hpp rng.hpp rng_philox.hpp) + +if(BUILD_SHARED_LIBS) + message("Build shared library") + add_definitions(-DSD_BUILD_SHARED_LIB) + target_compile_definitions(${SD_LIB} PRIVATE -DSD_BUILD_DLL) + set(CMAKE_POSITION_INDEPENDENT_CODE ON) +else() + message("Build static library") +endif() + set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) # deps @@ -58,10 +72,6 @@ add_subdirectory(ggml) add_subdirectory(thirdparty) -set(SD_LIB stable-diffusion) - -add_library(${SD_LIB} stable-diffusion.h stable-diffusion.cpp model.h model.cpp util.h util.cpp upscaler.cpp - ggml_extend.hpp clip.hpp common.hpp unet.hpp tae.hpp esrgan.hpp lora.hpp denoiser.hpp rng.hpp rng_philox.hpp) target_link_libraries(${SD_LIB} PUBLIC ggml zip) target_include_directories(${SD_LIB} PUBLIC . thirdparty) target_compile_features(${SD_LIB} PUBLIC cxx_std_11) diff --git a/examples/cli/main.cpp b/examples/cli/main.cpp index d01e8635..25ed7664 100644 --- a/examples/cli/main.cpp +++ b/examples/cli/main.cpp @@ -460,13 +460,13 @@ void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) { return; } if (level <= SD_LOG_INFO) { - fprintf(stdout, log); + fputs(log, stdout); fflush(stdout); } else { - fprintf(stderr, log); + fputs(log, stderr); fflush(stderr); } -}; +} int main(int argc, const char* argv[]) { SDParams params; @@ -560,6 +560,7 @@ int main(int argc, const char* argv[]) { if (results == NULL) { printf("generate failed\n"); + free_sd_ctx(sd_ctx); return 1; } @@ -600,6 +601,8 @@ int main(int argc, const char* argv[]) { free(results[i].data); results[i].data = NULL; } + free(results); + free_sd_ctx(sd_ctx); return 0; } diff --git a/model.h b/model.h index 865c9b0d..3b512223 100644 --- a/model.h +++ b/model.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "ggml/ggml-backend.h" #include "ggml/ggml.h" diff --git a/stable-diffusion.cpp b/stable-diffusion.cpp index f67d3382..10e24585 100644 --- a/stable-diffusion.cpp +++ b/stable-diffusion.cpp @@ -101,6 +101,7 @@ class StableDiffusionGGML { } ~StableDiffusionGGML() { + ggml_backend_free(backend); } bool load_from_file(const std::string& model_path, @@ -626,7 +627,7 @@ class StableDiffusionGGML { // get_ancestral_step float sigma_up = std::min(sigmas[i + 1], - std::sqrt(sigmas[i + 1] * sigmas[i + 1] * (sigmas[i] * sigmas[i] - sigmas[i + 1] * sigmas[i + 1]) / (sigmas[i] * sigmas[i]))); + std::sqrt(sigmas[i + 1] * sigmas[i + 1] * (sigmas[i] * sigmas[i] - sigmas[i + 1] * sigmas[i + 1]) / (sigmas[i] * sigmas[i]))); float sigma_down = std::sqrt(sigmas[i + 1] * sigmas[i + 1] - sigma_up * sigma_up); // Euler method @@ -802,7 +803,7 @@ class StableDiffusionGGML { // get_ancestral_step float sigma_up = std::min(sigmas[i + 1], - std::sqrt(sigmas[i + 1] * sigmas[i + 1] * (sigmas[i] * sigmas[i] - sigmas[i + 1] * sigmas[i + 1]) / (sigmas[i] * sigmas[i]))); + std::sqrt(sigmas[i + 1] * sigmas[i + 1] * (sigmas[i] * sigmas[i] - sigmas[i + 1] * sigmas[i + 1]) / (sigmas[i] * sigmas[i]))); float sigma_down = std::sqrt(sigmas[i + 1] * sigmas[i + 1] - sigma_up * sigma_up); auto t_fn = [](float sigma) -> float { return -log(sigma); }; auto sigma_fn = [](float t) -> float { return exp(-t); };