Skip to content

Commit 5cd13b3

Browse files
stduhpfStéphane du Hamel
authored andcommitted
Tiling: fix crash when less than 2 tiles per dim
1 parent cc246fc commit 5cd13b3

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
@@ -412,7 +412,7 @@ __STATIC_INLINE__ void ggml_merge_tensor_2d(struct ggml_tensor* input,
412412
for (int ix = x_skip; ix < width; ix++) {
413413
for (int k = 0; k < channels; k++) {
414414
float new_value = ggml_tensor_get_f32(input, ix, iy, k);
415-
if (overlap_x > 0 && overlap_y > 0) { // blend colors in overlapped area
415+
if (overlap_x > 0 || overlap_y > 0) { // blend colors in overlapped area
416416
float old_value = ggml_tensor_get_f32(output, x + ix, y + iy, k);
417417

418418
const float x_f_0 = (overlap_x > 0 && x > 0) ? (ix - x_skip) / float(overlap_x) : 1;
@@ -546,12 +546,31 @@ __STATIC_INLINE__ void sd_tiling(ggml_tensor* input, ggml_tensor* output, const
546546
input_tile_size = tile_size * scale;
547547
output_tile_size = tile_size;
548548
}
549-
int num_tiles_x = (input_width - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
549+
int num_tiles_x = (input_width - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1. - tile_overlap_factor));
550550
float tile_overlap_factor_x = (float)(input_tile_size * num_tiles_x - input_width) / (float)(input_tile_size * (num_tiles_x - 1));
551+
if (num_tiles_x <= 1) {
552+
if (input_width == input_tile_size) {
553+
num_tiles_x = 1;
554+
tile_overlap_factor_x = 0;
555+
} else {
556+
num_tiles_x = 2;
557+
tile_overlap_factor_x = (2 * input_tile_size - input_width) / (float)input_tile_size;
558+
}
559+
}
551560

552561
int num_tiles_y = (input_height - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
553562
float tile_overlap_factor_y = (float)(input_tile_size * num_tiles_y - input_height) / (float)(input_tile_size * (num_tiles_y - 1));
563+
if (num_tiles_y <= 1) {
564+
if (input_height == input_tile_size) {
565+
num_tiles_y = 1;
566+
tile_overlap_factor_y = 0;
567+
} else {
568+
num_tiles_y = 2;
569+
tile_overlap_factor_y = (2 * input_tile_size - input_height) / (float)input_tile_size;
570+
}
571+
}
554572

573+
LOG_DEBUG("num tiles : %d, %d ", num_tiles_x, num_tiles_y);
555574
LOG_DEBUG("optimal overlap : %f, %f (targeting %f)", tile_overlap_factor_x, tile_overlap_factor_y, tile_overlap_factor);
556575

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