Skip to content

Commit 5436f6b

Browse files
authored
fix: correct canny preprocessor (#861)
1 parent 1c32fa0 commit 5436f6b

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

ggml_extend.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,14 @@ __STATIC_INLINE__ float sigmoid(float x) {
372372

373373
// SPECIAL OPERATIONS WITH TENSORS
374374

375-
__STATIC_INLINE__ uint8_t* sd_tensor_to_image(struct ggml_tensor* input) {
375+
__STATIC_INLINE__ uint8_t* sd_tensor_to_image(struct ggml_tensor* input, uint8_t* image_data = nullptr) {
376376
int64_t width = input->ne[0];
377377
int64_t height = input->ne[1];
378378
int64_t channels = input->ne[2];
379379
GGML_ASSERT(channels == 3 && input->type == GGML_TYPE_F32);
380-
uint8_t* image_data = (uint8_t*)malloc(width * height * channels);
380+
if (image_data == nullptr) {
381+
image_data = (uint8_t*)malloc(width * height * channels);
382+
}
381383
for (int iy = 0; iy < height; iy++) {
382384
for (int ix = 0; ix < width; ix++) {
383385
for (int k = 0; k < channels; k++) {

preprocessing.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
void convolve(struct ggml_tensor* input, struct ggml_tensor* output, struct ggml_tensor* kernel, int padding) {
88
struct ggml_init_params params;
9-
params.mem_size = 20 * 1024 * 1024; // 10
9+
params.mem_size = 80 * input->ne[0] * input->ne[1]; // 20M for 512x512
1010
params.mem_buffer = NULL;
1111
params.no_alloc = false;
1212
struct ggml_context* ctx0 = ggml_init(params);
@@ -164,7 +164,7 @@ void threshold_hystersis(struct ggml_tensor* img, float high_threshold, float lo
164164

165165
bool preprocess_canny(sd_image_t img, float high_threshold, float low_threshold, float weak, float strong, bool inverse) {
166166
struct ggml_init_params params;
167-
params.mem_size = static_cast<size_t>(10 * 1024 * 1024); // 10MB
167+
params.mem_size = static_cast<size_t>(40 * img.width * img.height); // 10MB for 512x512
168168
params.mem_buffer = NULL;
169169
params.no_alloc = false;
170170
struct ggml_context* work_ctx = ggml_init(params);
@@ -218,9 +218,7 @@ bool preprocess_canny(sd_image_t img, float high_threshold, float low_threshold,
218218
ggml_tensor_set_f32(image, gray, ix, iy, 2);
219219
}
220220
}
221-
uint8_t* output = sd_tensor_to_image(image);
222-
free(img.data);
223-
img.data = output;
221+
sd_tensor_to_image(image, img.data);
224222
ggml_free(work_ctx);
225223
return true;
226224
}

stable-diffusion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ class StableDiffusionGGML {
14341434
int ne3;
14351435
if (sd_version_is_qwen_image(version)) {
14361436
ne2 = 1;
1437-
ne3 = C*x->ne[3];
1437+
ne3 = C * x->ne[3];
14381438
} else {
14391439
if (!use_tiny_autoencoder) {
14401440
C *= 2;

0 commit comments

Comments
 (0)