Skip to content

Commit 6f96903

Browse files
committed
Tiling: fix crash when less than 2 tiles per dim
1 parent cc16b02 commit 6f96903

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

ggml_extend.hpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ __STATIC_INLINE__ void ggml_merge_tensor_2d(struct ggml_tensor* input,
374374
for (int ix = x_skip; ix < width; ix++) {
375375
for (int k = 0; k < channels; k++) {
376376
float new_value = ggml_tensor_get_f32(input, ix, iy, k);
377-
if (overlap_x > 0 && overlap_y > 0) { // blend colors in overlapped area
377+
if (overlap_x > 0 || overlap_y > 0) { // blend colors in overlapped area
378378
float old_value = ggml_tensor_get_f32(output, x + ix, y + iy, k);
379379

380380
const float x_f_0 = (overlap_x > 0 && x > 0) ? (ix - x_skip) / float(overlap_x) : 1;
@@ -508,12 +508,31 @@ __STATIC_INLINE__ void sd_tiling(ggml_tensor* input, ggml_tensor* output, const
508508
input_tile_size = tile_size * scale;
509509
output_tile_size = tile_size;
510510
}
511-
int num_tiles_x = (input_width - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
511+
int num_tiles_x = (input_width - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1. - tile_overlap_factor));
512512
float tile_overlap_factor_x = (float)(input_tile_size * num_tiles_x - input_width) / (float)(input_tile_size * (num_tiles_x - 1));
513+
if (num_tiles_x <= 1) {
514+
if (input_width == input_tile_size) {
515+
num_tiles_x = 1;
516+
tile_overlap_factor_x = 0;
517+
} else {
518+
num_tiles_x = 2;
519+
tile_overlap_factor_x = (2 * input_tile_size - input_width) / (float)input_tile_size;
520+
}
521+
}
513522

514523
int num_tiles_y = (input_height - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
515524
float tile_overlap_factor_y = (float)(input_tile_size * num_tiles_y - input_height) / (float)(input_tile_size * (num_tiles_y - 1));
525+
if (num_tiles_y <= 1) {
526+
if (input_height == input_tile_size) {
527+
num_tiles_y = 1;
528+
tile_overlap_factor_y = 0;
529+
} else {
530+
num_tiles_y = 2;
531+
tile_overlap_factor_y = (2 * input_tile_size - input_height) / (float)input_tile_size;
532+
}
533+
}
516534

535+
LOG_DEBUG("num tiles : %d, %d ", num_tiles_x, num_tiles_y);
517536
LOG_DEBUG("optimal overlap : %f, %f (targeting %f)", tile_overlap_factor_x, tile_overlap_factor_y, tile_overlap_factor);
518537

519538
GGML_ASSERT(input_width % 2 == 0 && input_height % 2 == 0 && output_width % 2 == 0 && output_height % 2 == 0); // should be multiple of 2

0 commit comments

Comments
 (0)