Compare commits

...

5 Commits

Author SHA1 Message Date
leejet
7cb41b190f fix: avoid encountering 'std::set undefined' in some environments 2024-01-02 22:37:01 +08:00
leejet
7fb8a51318 chore: make SD_BUILD_DLL visible only to SD_LIB 2024-01-02 22:31:40 +08:00
leejet
2c5f3fc53a chore: add support for building shared library 2024-01-02 21:05:44 +08:00
Erik Scholz
f2e4d9793b fix: avoid some memory leaks (#136)
---------

Co-authored-by: leejet <leejet714@gmail.com>
2024-01-01 23:27:29 +08:00
Erik Scholz
4a5e7b58e2 fix: never use a log message as a format string (#135) 2024-01-01 20:43:47 +08:00
4 changed files with 24 additions and 9 deletions

View File

@@ -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)

View File

@@ -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;
}

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <string>
#include <vector>
#include <set>
#include "ggml/ggml-backend.h"
#include "ggml/ggml.h"

View File

@@ -101,6 +101,7 @@ public:
}
~StableDiffusionGGML() {
ggml_backend_free(backend);
}
bool load_from_file(const std::string& model_path,
@@ -626,7 +627,7 @@ public:
// 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 @@ public:
// 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); };