Skip to content

Commit 5495c93

Browse files
committed
Tiling: fix edge cases for adaptative overlap
1 parent e23ecce commit 5495c93

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

ggml_extend.hpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,20 @@ __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 tile_overlap = (input_tile_size * tile_overlap_factor);
512+
int non_tile_overlap = input_tile_size - tile_overlap;
513+
514+
int num_tiles_x = (input_width - tile_overlap) / non_tile_overlap;
515+
int overshoot_x = ((num_tiles_x + 1) * non_tile_overlap + tile_overlap) % input_width;
516+
517+
if ((overshoot_x != non_tile_overlap) && (overshoot_x <= num_tiles_x * (input_tile_size / 2 - tile_overlap))) {
518+
// if tiles don't fit perfectly using the desired overlap
519+
// and there is enough room to squeeze an extra tile without overlap becoming >0.5
520+
num_tiles_x++;
521+
}
522+
512523
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) {
524+
if (num_tiles_x <= 2) {
514525
if (input_width == input_tile_size) {
515526
num_tiles_x = 1;
516527
tile_overlap_factor_x = 0;
@@ -520,9 +531,17 @@ __STATIC_INLINE__ void sd_tiling(ggml_tensor* input, ggml_tensor* output, const
520531
}
521532
}
522533

523-
int num_tiles_y = (input_height - (int)(input_tile_size * tile_overlap_factor)) / (int)(input_tile_size * (1 - tile_overlap_factor));
534+
int num_tiles_y = (input_height - tile_overlap) / non_tile_overlap;
535+
int overshoot_y = ((num_tiles_y + 1) * non_tile_overlap + tile_overlap) % input_height;
536+
537+
if ((overshoot_y != non_tile_overlap) && (overshoot_y <= num_tiles_y * (input_tile_size / 2 - tile_overlap))) {
538+
// if tiles don't fit perfectly using the desired overlap
539+
// and there is enough room to squeeze an extra tile without overlap becoming >0.5
540+
num_tiles_y++;
541+
}
542+
524543
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) {
544+
if (num_tiles_y <= 2) {
526545
if (input_height == input_tile_size) {
527546
num_tiles_y = 1;
528547
tile_overlap_factor_y = 0;

0 commit comments

Comments
 (0)