Skip to content

Commit b73a85f

Browse files
committed
Tiling: fix crash when less than 2 tiles per dim
1 parent 028f798 commit b73a85f

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
@@ -498,7 +498,7 @@ __STATIC_INLINE__ void ggml_merge_tensor_2d(struct ggml_tensor* input,
498498
for (int ix = x_skip; ix < width; ix++) {
499499
for (int k = 0; k < channels; k++) {
500500
float new_value = ggml_tensor_get_f32(input, ix, iy, k);
501-
if (overlap_x > 0 && overlap_y > 0) { // blend colors in overlapped area
501+
if (overlap_x > 0 || overlap_y > 0) { // blend colors in overlapped area
502502
float old_value = ggml_tensor_get_f32(output, x + ix, y + iy, k);
503503

504504
const float x_f_0 = (overlap_x > 0 && x > 0) ? (ix - x_skip) / float(overlap_x) : 1;
@@ -753,12 +753,31 @@ __STATIC_INLINE__ void sd_tiling(ggml_tensor* input, ggml_tensor* output, const
753753
input_tile_size = tile_size * scale;
754754
output_tile_size = tile_size;
755755
}
756-
int num_tiles_x = (input_width - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
756+
int num_tiles_x = (input_width - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1. - tile_overlap_factor));
757757
float tile_overlap_factor_x = (float)(input_tile_size * num_tiles_x - input_width) / (float)(input_tile_size * (num_tiles_x - 1));
758+
if (num_tiles_x <= 1) {
759+
if (input_width == input_tile_size) {
760+
num_tiles_x = 1;
761+
tile_overlap_factor_x = 0;
762+
} else {
763+
num_tiles_x = 2;
764+
tile_overlap_factor_x = (2 * input_tile_size - input_width) / (float)input_tile_size;
765+
}
766+
}
758767

759768
int num_tiles_y = (input_height - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
760769
float tile_overlap_factor_y = (float)(input_tile_size * num_tiles_y - input_height) / (float)(input_tile_size * (num_tiles_y - 1));
770+
if (num_tiles_y <= 1) {
771+
if (input_height == input_tile_size) {
772+
num_tiles_y = 1;
773+
tile_overlap_factor_y = 0;
774+
} else {
775+
num_tiles_y = 2;
776+
tile_overlap_factor_y = (2 * input_tile_size - input_height) / (float)input_tile_size;
777+
}
778+
}
761779

780+
LOG_DEBUG("num tiles : %d, %d ", num_tiles_x, num_tiles_y);
762781
LOG_DEBUG("optimal overlap : %f, %f (targeting %f)", tile_overlap_factor_x, tile_overlap_factor_y, tile_overlap_factor);
763782

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