Skip to content

Commit 3b8bb98

Browse files
stduhpfthxCode
authored andcommitted
Tiling: fix crash when less than 2 tiles per dim
1 parent 3f06c3a commit 3b8bb98

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
@@ -384,7 +384,7 @@ __STATIC_INLINE__ void ggml_merge_tensor_2d(struct ggml_tensor* input,
384384
for (int ix = x_skip; ix < width; ix++) {
385385
for (int k = 0; k < channels; k++) {
386386
float new_value = ggml_tensor_get_f32(input, ix, iy, k);
387-
if (overlap_x > 0 && overlap_y > 0) { // blend colors in overlapped area
387+
if (overlap_x > 0 || overlap_y > 0) { // blend colors in overlapped area
388388
float old_value = ggml_tensor_get_f32(output, x + ix, y + iy, k);
389389

390390
const float x_f_0 = (overlap_x > 0 && x > 0) ? (ix - x_skip) / float(overlap_x) : 1;
@@ -518,12 +518,31 @@ __STATIC_INLINE__ void sd_tiling(ggml_tensor* input, ggml_tensor* output, const
518518
input_tile_size = tile_size * scale;
519519
output_tile_size = tile_size;
520520
}
521-
int num_tiles_x = (input_width - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
521+
int num_tiles_x = (input_width - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1. - tile_overlap_factor));
522522
float tile_overlap_factor_x = (float)(input_tile_size * num_tiles_x - input_width) / (float)(input_tile_size * (num_tiles_x - 1));
523+
if (num_tiles_x <= 1) {
524+
if (input_width == input_tile_size) {
525+
num_tiles_x = 1;
526+
tile_overlap_factor_x = 0;
527+
} else {
528+
num_tiles_x = 2;
529+
tile_overlap_factor_x = (2 * input_tile_size - input_width) / (float)input_tile_size;
530+
}
531+
}
523532

524533
int num_tiles_y = (input_height - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
525534
float tile_overlap_factor_y = (float)(input_tile_size * num_tiles_y - input_height) / (float)(input_tile_size * (num_tiles_y - 1));
535+
if (num_tiles_y <= 1) {
536+
if (input_height == input_tile_size) {
537+
num_tiles_y = 1;
538+
tile_overlap_factor_y = 0;
539+
} else {
540+
num_tiles_y = 2;
541+
tile_overlap_factor_y = (2 * input_tile_size - input_height) / (float)input_tile_size;
542+
}
543+
}
526544

545+
LOG_DEBUG("num tiles : %d, %d ", num_tiles_x, num_tiles_y);
527546
LOG_DEBUG("optimal overlap : %f, %f (targeting %f)", tile_overlap_factor_x, tile_overlap_factor_y, tile_overlap_factor);
528547

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