From 4a8190405ac32930678ce030dff6289ed680b6fc Mon Sep 17 00:00:00 2001 From: leejet Date: Sun, 25 Feb 2024 21:39:01 +0800 Subject: [PATCH] fix: fix the issue with dynamic linking --- clip.hpp | 2 +- control.hpp | 4 ++-- examples/cli/main.cpp | 21 ++++++++++++++++++++- preprocessing.hpp | 10 +++++----- stable-diffusion.h | 9 +++++++++ util.cpp | 2 +- util.h | 2 -- 7 files changed, 38 insertions(+), 12 deletions(-) diff --git a/clip.hpp b/clip.hpp index 9e6bd2ca..31efa5fe 100644 --- a/clip.hpp +++ b/clip.hpp @@ -964,7 +964,7 @@ struct FrozenCLIPEmbedderWithCustomWords : public GGMLModule { input_ids2 = to_backend(input_ids2); if (!return_pooled) { - input_ids = to_backend(input_ids); + input_ids = to_backend(input_ids); } struct ggml_tensor* embeddings = NULL; diff --git a/control.hpp b/control.hpp index cb818ff8..1dcd8430 100644 --- a/control.hpp +++ b/control.hpp @@ -388,11 +388,11 @@ struct ControlNet : public GGMLModule { struct ggml_tensor* y = NULL) { struct ggml_cgraph* gf = ggml_new_graph_custom(compute_ctx, CONTROL_NET_GRAPH_SIZE, false); - x = to_backend(x); + x = to_backend(x); if (guided_hint_cached) { hint = NULL; } else { - hint = to_backend(hint); + hint = to_backend(hint); } context = to_backend(context); y = to_backend(y); diff --git a/examples/cli/main.cpp b/examples/cli/main.cpp index 5d324845..13725e74 100644 --- a/examples/cli/main.cpp +++ b/examples/cli/main.cpp @@ -499,6 +499,18 @@ void parse_args(int argc, const char** argv, SDParams& params) { } } +static std::string sd_basename(const std::string& path) { + size_t pos = path.find_last_of('/'); + if (pos != std::string::npos) { + return path.substr(pos + 1); + } + pos = path.find_last_of('\\'); + if (pos != std::string::npos) { + return path.substr(pos + 1); + } + return path; +} + std::string get_image_params(SDParams params, int64_t seed) { std::string parameter_string = params.prompt + "\n"; if (params.negative_prompt.size() != 0) { @@ -631,7 +643,14 @@ int main(int argc, const char* argv[]) { input_image_buffer}; if (params.canny_preprocess) { // apply preprocessor LOG_INFO("Applying canny preprocessor"); - control_image->data = preprocess_canny(control_image->data, control_image->width, control_image->height); + control_image->data = preprocess_canny(control_image->data, + control_image->width, + control_image->height, + 0.08f, + 0.08f, + 0.8f, + 1.0f, + false); } } results = txt2img(sd_ctx, diff --git a/preprocessing.hpp b/preprocessing.hpp index d0e899ca..4ea1dbab 100644 --- a/preprocessing.hpp +++ b/preprocessing.hpp @@ -117,15 +117,15 @@ void non_max_supression(struct ggml_tensor* result, struct ggml_tensor* G, struc } } -void threshold_hystersis(struct ggml_tensor* img, float highThreshold, float lowThreshold, float weak, float strong) { +void threshold_hystersis(struct ggml_tensor* img, float high_threshold, float low_threshold, float weak, float strong) { int n_elements = ggml_nelements(img); float* imd = (float*)img->data; float max = -INFINITY; for (int i = 0; i < n_elements; i++) { max = imd[i] > max ? imd[i] : max; } - float ht = max * highThreshold; - float lt = ht * lowThreshold; + float ht = max * high_threshold; + float lt = ht * low_threshold; for (int i = 0; i < n_elements; i++) { float img_v = imd[i]; if (img_v >= ht) { // strong pixel @@ -162,7 +162,7 @@ void threshold_hystersis(struct ggml_tensor* img, float highThreshold, float low } } -uint8_t* preprocess_canny(uint8_t* img, int width, int height, float highThreshold = 0.08f, float lowThreshold = 0.08f, float weak = 0.8f, float strong = 1.0f, bool inverse = false) { +uint8_t* preprocess_canny(uint8_t* img, int width, int height, float high_threshold, float low_threshold, float weak, float strong, bool inverse) { struct ggml_init_params params; params.mem_size = static_cast(10 * 1024 * 1024); // 10 params.mem_buffer = NULL; @@ -207,7 +207,7 @@ uint8_t* preprocess_canny(uint8_t* img, int width, int height, float highThresho normalize_tensor(G); prop_arctan2(iX, iY, tetha); non_max_supression(image_gray, G, tetha); - threshold_hystersis(image_gray, highThreshold, lowThreshold, weak, strong); + threshold_hystersis(image_gray, high_threshold, low_threshold, weak, strong); // to RGB channels for (int iy = 0; iy < height; iy++) { for (int ix = 0; ix < width; ix++) { diff --git a/stable-diffusion.h b/stable-diffusion.h index ea042262..99eba433 100644 --- a/stable-diffusion.h +++ b/stable-diffusion.h @@ -177,6 +177,15 @@ SD_API sd_image_t upscale(upscaler_ctx_t* upscaler_ctx, sd_image_t input_image, SD_API bool convert(const char* input_path, const char* vae_path, const char* output_path, sd_type_t output_type); +SD_API uint8_t* preprocess_canny(uint8_t* img, + int width, + int height, + float high_threshold, + float low_threshold, + float weak, + float strong, + bool inverse); + #ifdef __cplusplus } #endif diff --git a/util.cpp b/util.cpp index f68607f0..27173c6a 100644 --- a/util.cpp +++ b/util.cpp @@ -175,7 +175,7 @@ std::u32string unicode_value_to_utf32(int unicode_value) { return utf32_string; } -std::string sd_basename(const std::string& path) { +static std::string sd_basename(const std::string& path) { size_t pos = path.find_last_of('/'); if (pos != std::string::npos) { return path.substr(pos + 1); diff --git a/util.h b/util.h index ca580d04..c562ba5d 100644 --- a/util.h +++ b/util.h @@ -21,8 +21,6 @@ std::u32string utf8_to_utf32(const std::string& utf8_str); std::string utf32_to_utf8(const std::u32string& utf32_str); std::u32string unicode_value_to_utf32(int unicode_value); -std::string sd_basename(const std::string& path); - typedef struct { uint32_t width; uint32_t height;