Skip to content

Commit

Permalink
fix: fix the issue with dynamic linking
Browse files Browse the repository at this point in the history
  • Loading branch information
leejet committed Feb 25, 2024
1 parent 730585d commit 4a81904
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion clip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions control.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 20 additions & 1 deletion examples/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions preprocessing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<size_t>(10 * 1024 * 1024); // 10
params.mem_buffer = NULL;
Expand Down Expand Up @@ -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++) {
Expand Down
9 changes: 9 additions & 0 deletions stable-diffusion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4a81904

Please sign in to comment.