Skip to content

Commit 1aeb4a1

Browse files
committed
Tiling: fix crash when less than 2 tiles per dim
1 parent e0cf08b commit 1aeb4a1

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

ggml_extend.hpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ __STATIC_INLINE__ void ggml_merge_tensor_2d(struct ggml_tensor* input,
484484
for (int ix = x_skip; ix < width; ix++) {
485485
for (int k = 0; k < channels; k++) {
486486
float new_value = ggml_tensor_get_f32(input, ix, iy, k);
487-
if (overlap_x > 0 && overlap_y > 0) { // blend colors in overlapped area
487+
if (overlap_x > 0 || overlap_y > 0) { // blend colors in overlapped area
488488
float old_value = ggml_tensor_get_f32(output, x + ix, y + iy, k);
489489

490490
const float x_f_0 = (overlap_x > 0 && x > 0) ? (ix - x_skip) / float(overlap_x) : 1;
@@ -620,12 +620,31 @@ __STATIC_INLINE__ void sd_tiling(ggml_tensor* input, ggml_tensor* output, const
620620
input_tile_size = tile_size * scale;
621621
output_tile_size = tile_size;
622622
}
623-
int num_tiles_x = (input_width - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
623+
int num_tiles_x = (input_width - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1. - tile_overlap_factor));
624624
float tile_overlap_factor_x = (float)(input_tile_size * num_tiles_x - input_width) / (float)(input_tile_size * (num_tiles_x - 1));
625+
if (num_tiles_x <= 1) {
626+
if (input_width == input_tile_size) {
627+
num_tiles_x = 1;
628+
tile_overlap_factor_x = 0;
629+
} else {
630+
num_tiles_x = 2;
631+
tile_overlap_factor_x = (2 * input_tile_size - input_width) / (float)input_tile_size;
632+
}
633+
}
625634

626635
int num_tiles_y = (input_height - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
627636
float tile_overlap_factor_y = (float)(input_tile_size * num_tiles_y - input_height) / (float)(input_tile_size * (num_tiles_y - 1));
637+
if (num_tiles_y <= 1) {
638+
if (input_height == input_tile_size) {
639+
num_tiles_y = 1;
640+
tile_overlap_factor_y = 0;
641+
} else {
642+
num_tiles_y = 2;
643+
tile_overlap_factor_y = (2 * input_tile_size - input_height) / (float)input_tile_size;
644+
}
645+
}
628646

647+
LOG_DEBUG("num tiles : %d, %d ", num_tiles_x, num_tiles_y);
629648
LOG_DEBUG("optimal overlap : %f, %f (targeting %f)", tile_overlap_factor_x, tile_overlap_factor_y, tile_overlap_factor);
630649

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