#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/granite_docling/modular_granite_docling.py.
#               Do NOT edit this file manually as any edits will be overwritten by the generation of
#             the file from the modular. If any change should be done, please apply the change to the
#                          modular_granite_docling.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 IBM and The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from functools import lru_cache

import torch
from torch.nn.utils.rnn import pad_sequence
from torchvision.transforms.v2 import functional as tvF

from ...image_processing_backends import TorchvisionBackend
from ...image_processing_utils import BatchFeature
from ...image_transforms import group_images_by_shape, reorder_images
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ImageInput,
    PILImageResampling,
    SizeDict,
    make_nested_list_of_images,
)
from ...processing_utils import ImagesKwargs, Unpack
from ...utils import TensorType, auto_docstring


class GraniteDoclingImageProcessorKwargs(ImagesKwargs, total=False):
    r"""
    crop_to_patches (`bool`, *optional*, defaults to `True`):
        Whether to split the page into 512x512 tiles laid out on the grid that best matches its aspect ratio (at
        most 16 tiles per side). Can be overridden by the `crop_to_patches` parameter in the `preprocess` method.
    min_patches (`int`, *optional*, defaults to 1):
        The minimum number of tiles per page. Only has an effect if `crop_to_patches` is set to `True`. Can be
        overridden by the `min_patches` parameter in the `preprocess` method.
    max_patches (`int`, *optional*, defaults to 32):
        The maximum number of tiles per page. Only has an effect if `crop_to_patches` is set to `True`. Can be
        overridden by the `max_patches` parameter in the `preprocess` method.
    dense_max_patches (`int`, *optional*, defaults to 0):
        The maximum number of tiles for the pages the spread rule selects (0 disables the rule). A page is tiled with
        up to `dense_max_patches` tiles instead of `max_patches` when the `max_patches` grid shrinks it below
        `dense_max_scale` and its width/height ratio is above `dense_min_aspect` (and at most `dense_max_aspect` when
        that is set). The checkpoint must have been trained with the same rule.
    dense_max_scale (`float`, *optional*, defaults to 0.0):
        Ratio of the tiled canvas width to the page width at `max_patches` below which a page counts as
        under-resolved. 0 skips this test.
    dense_min_aspect (`float`, *optional*, defaults to 0.0):
        Width/height ratio a page must exceed to get `dense_max_patches`. 0 skips this test.
    dense_max_aspect (`float`, *optional*, defaults to 0.0):
        Width/height ratio a page may not exceed to get `dense_max_patches`, which keeps strips such as single
        formula lines on `max_patches`. 0 means no upper bound.
    fine_route (`bool`, *optional*, defaults to `False`):
        Whether the pages take the fine connector path, which yields four times as many image tokens per tile.
        Returned as `tile_fine_mask`, one value per sample.
    """

    crop_to_patches: bool
    min_patches: int
    max_patches: int

    fine_route: bool
    dense_max_patches: int
    dense_max_scale: float
    dense_min_aspect: float
    dense_max_aspect: float


@lru_cache(maxsize=100)
def get_optimal_tiled_canvas(
    original_image_size: tuple[int, int],
    target_tile_size: tuple[int, int],
    min_image_tiles: int,
    max_image_tiles: int,
) -> tuple[int, int]:
    """
    Given a minimum and maximum number of tiles, find the canvas with the closest aspect ratio to the
    original image aspect ratio.
    In case of tie-breaking condition when two canvases have the same aspect ratio difference, we favor the canvas with
    more tiles, until the area covered by the tiles is more than twice the target area, in order to avoid unnecessarily
    excessive tiling.
    """
    possible_tile_arrangements = get_all_supported_aspect_ratios(min_image_tiles, max_image_tiles)

    original_height, original_width = original_image_size
    target_tile_height, target_tile_width = target_tile_size
    aspect_ratio = original_width / original_height
    area = original_width * original_height

    # find the grid with the best aspect ratio
    best_ratio_diff = float("inf")
    best_grid = (1, 1)
    for grid in possible_tile_arrangements:
        grid_aspect_ratio = grid[0] / grid[1]
        ratio_diff = abs(aspect_ratio - grid_aspect_ratio)
        if ratio_diff < best_ratio_diff:
            best_ratio_diff = ratio_diff
            best_grid = grid
        elif ratio_diff == best_ratio_diff:
            # if the aspect ratio difference is the same, we favor the grid with more patches
            # until the area covered by the patches is more than twice the original image area
            if area > 0.5 * target_tile_height * target_tile_width * grid[0] * grid[1]:
                best_grid = grid

    return best_grid


@lru_cache(maxsize=10)
def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) -> list[tuple[int, int]]:
    """
    Tile grids allowed between `min_image_tiles` and `max_image_tiles`. Each side stops at 16: the tokenizer only
    has `<row_i_col_j>` markers up to 16x16.
    """
    max_side = min(max_image_tiles, 16)
    ratios = [
        (width, height)
        for width in range(1, max_side + 1)
        for height in range(1, max_side + 1)
        if min_image_tiles <= width * height <= max_image_tiles
    ]
    return sorted(ratios, key=lambda grid: grid[0] * grid[1])


def get_tile_budget(
    original_image_size: tuple[int, int],
    target_tile_size: tuple[int, int],
    min_image_tiles: int,
    max_image_tiles: int,
    dense_max_patches: int = 0,
    dense_max_scale: float = 0.0,
    dense_min_aspect: float = 0.0,
    dense_max_aspect: float = 0.0,
) -> int:
    """
    The maximum number of tiles of one page: `dense_max_patches` when the page is under-resolved at `max_image_tiles`
    (tiled canvas width / page width below `dense_max_scale`) and its width/height ratio lies in (`dense_min_aspect`,
    `dense_max_aspect`], otherwise `max_image_tiles`. Newspaper spreads and large landscape scans are the pages this
    selects.
    """
    if not dense_max_patches or (dense_max_scale <= 0 and dense_min_aspect <= 0):
        return max_image_tiles
    original_height, original_width = original_image_size
    if original_height <= 0 or original_width <= 0:
        return max_image_tiles
    aspect_ratio = original_width / original_height
    if dense_min_aspect > 0 and not aspect_ratio > dense_min_aspect:
        return max_image_tiles
    if dense_max_aspect > 0 and aspect_ratio > dense_max_aspect:
        return max_image_tiles
    if dense_max_scale > 0:
        num_columns, _ = get_optimal_tiled_canvas(
            original_image_size, target_tile_size, min_image_tiles, max_image_tiles
        )
        if not num_columns * target_tile_size[1] / original_width < dense_max_scale:
            return max_image_tiles
    return dense_max_patches


def get_page_max_patches(
    height: int,
    width: int,
    size: SizeDict | dict,
    crop_to_patches: bool,
    min_patches: int,
    max_patches: int,
    dense_max_patches: int = 0,
    dense_max_scale: float = 0.0,
    dense_min_aspect: float = 0.0,
    dense_max_aspect: float = 0.0,
) -> int:
    """
    The most tiles a page may be split into: 1 when pages are not split, `dense_max_patches` when the spread rule
    selects the page, otherwise `max_patches`.
    """
    if not crop_to_patches or max_patches <= 1:
        return 1
    return get_tile_budget(
        (height, width),
        (size["height"], size["width"]),
        min_patches,
        max_patches,
        dense_max_patches,
        dense_max_scale,
        dense_min_aspect,
        dense_max_aspect,
    )


def get_page_tile_grid(
    height: int,
    width: int,
    size: SizeDict | dict,
    crop_to_patches: bool,
    min_patches: int,
    max_patches: int,
    dense_max_patches: int = 0,
    dense_max_scale: float = 0.0,
    dense_min_aspect: float = 0.0,
    dense_max_aspect: float = 0.0,
) -> tuple[int, int]:
    """The `(num_rows, num_columns)` tile grid of a page. A page that is not split is a 1x1 grid."""
    max_patches = get_page_max_patches(
        height,
        width,
        size,
        crop_to_patches,
        min_patches,
        max_patches,
        dense_max_patches,
        dense_max_scale,
        dense_min_aspect,
        dense_max_aspect,
    )
    if max_patches <= 1:
        return 1, 1
    tile_size = (size["height"], size["width"])
    num_columns, num_rows = get_optimal_tiled_canvas((height, width), tile_size, min_patches, max_patches)
    return num_rows, num_columns


@auto_docstring(
    custom_intro="""
    Splits a page into 512x512 tiles on the grid that best matches its aspect ratio (`crop_to_patches`, at most
    `max_patches` tiles and 16 per side), adds a thumbnail of the whole page when there is more than one tile, and
    pads the samples of a batch to the same number of tiles. `pixel_attention_mask` marks the real tiles.
    `fine_route=True` marks the samples for the fine connector path (`tile_fine_mask`, one value per sample).
    """
)
class GraniteDoclingImageProcessor(TorchvisionBackend):
    valid_kwargs = GraniteDoclingImageProcessorKwargs
    resample = PILImageResampling.BICUBIC
    image_mean = OPENAI_CLIP_MEAN
    image_std = OPENAI_CLIP_STD
    size = {"height": 512, "width": 512}
    do_resize = True
    do_rescale = True
    do_normalize = True
    do_convert_rgb = True
    crop_to_patches = True
    min_patches = 1
    max_patches = 32
    fine_route = False
    dense_max_patches = 0
    dense_max_scale = 0.0
    dense_min_aspect = 0.0
    dense_max_aspect = 0.0
    model_input_names = ["pixel_values", "pixel_attention_mask"]

    def __init__(self, **kwargs: Unpack[GraniteDoclingImageProcessorKwargs]):
        super().__init__(**kwargs)

    def crop_image_to_patches(
        self,
        images: "torch.Tensor",
        min_patches: int,
        max_patches: int,
        use_thumbnail: bool = True,
        patch_size: SizeDict | None = None,
        resample: "PILImageResampling | tvF.InterpolationMode | int | None" = None,
    ):
        """
        Crop the images to patches and return a list of cropped images.
        The number of patches and their grid arrangement are determined by the original image size,
        the target patch size and the minimum and maximum number of patches.
        The aspect ratio of the patches grid is chosen to be the closest to the original image aspect ratio.

        Args:
            images (`torch.Tensor`):
                The images to be cropped.
            min_patches (`int`):
                The minimum number of patches to be extracted from the image.
            max_patches (`int`):
                The maximum number of patches to be extracted from the image.
            use_thumbnail (`bool`, *optional*, defaults to `True`):
                Whether to add a thumbnail image to the list of cropped patches.
            patch_size (`SizeDict`, *optional*):
                The size of the output patches.
            resample (`PILImageResampling | tvF.InterpolationMode | int | None`, *optional*):
                Resampling filter to use when resizing.
        """
        patch_size_height, patch_size_width = patch_size.height, patch_size.width
        original_height, original_width = images.shape[-2:]
        # find the closest aspect ratio to the target
        num_columns, num_rows = get_optimal_tiled_canvas(
            (original_height, original_width), (patch_size_height, patch_size_width), min_patches, max_patches
        )

        # calculate the target width and height
        target_width = patch_size_width * num_columns
        target_height = patch_size_height * num_rows
        num_blocks = num_columns * num_rows

        # resize the image so that each patch is of patch_size
        resized_image = self.resize(images, SizeDict(height=target_height, width=target_width), resample=resample)
        # split the image into patches
        processed_images = []
        for i in range(num_blocks):
            column = i % num_columns
            row = i // num_columns
            box = (
                column * patch_size_width,
                row * patch_size_height,
                (column + 1) * patch_size_width,
                (row + 1) * patch_size_height,
            )
            # split the image
            patch_image = resized_image[..., box[1] : box[3], box[0] : box[2]]
            processed_images.append(patch_image)

        if use_thumbnail and len(processed_images) != 1:
            thumbnail_img = self.resize(images, patch_size, resample=resample)
            processed_images.append(thumbnail_img)

        processed_images = torch.stack(processed_images, dim=0).transpose(0, 1).contiguous()

        return processed_images

    def _preprocess(
        self,
        images: list[list["torch.Tensor"]],
        size: SizeDict,
        resample,
        do_rescale: bool,
        rescale_factor: float,
        do_normalize: bool,
        image_mean: float | list[float] | None,
        image_std: float | list[float] | None,
        disable_grouping: bool | None,
        return_tensors: str | TensorType | None,
        crop_to_patches: bool = True,
        min_patches: int = 1,
        max_patches: int = 32,
        fine_route: bool = False,
        dense_max_patches: int = 0,
        dense_max_scale: float = 0.0,
        dense_min_aspect: float = 0.0,
        dense_max_aspect: float = 0.0,
        **kwargs,
    ) -> BatchFeature:
        grid_kwargs = {
            "crop_to_patches": crop_to_patches,
            "min_patches": min_patches,
            "max_patches": max_patches,
            "size": size,
            "dense_max_patches": dense_max_patches,
            "dense_max_scale": dense_max_scale,
            "dense_min_aspect": dense_min_aspect,
            "dense_max_aspect": dense_max_aspect,
        }
        # Pages of the same size get the same grid, so they are tiled together
        grouped_images, grouped_images_index = group_images_by_shape(
            images, disable_grouping=disable_grouping, is_nested=True
        )
        tiles_grouped, grids_grouped = {}, {}
        for shape, stacked_images in grouped_images.items():
            height, width = stacked_images.shape[-2:]
            num_rows, num_cols = self.get_tile_grid(height, width, grid_kwargs)
            # GotOcr2's crop selects the grid itself, from the same tile budget as `get_tile_grid`
            max_tiles = get_page_max_patches(height, width, **grid_kwargs)
            stacked_tiles = self.crop_image_to_patches(
                stacked_images, min(min_patches, max_tiles), max_tiles, patch_size=size, resample=resample
            )
            stacked_tiles = self.rescale_and_normalize(
                stacked_tiles, do_rescale, rescale_factor, do_normalize, image_mean, image_std
            )
            tiles_grouped[shape] = stacked_tiles
            grids_grouped[shape] = [(num_rows, num_cols)] * stacked_tiles.shape[0]
        tiles = reorder_images(tiles_grouped, grouped_images_index, is_nested=True)
        grids = reorder_images(grids_grouped, grouped_images_index, is_nested=True)

        # One row of tiles per sample. A text-only sample in a mixed batch (`images=[[], [image]]`) has none.
        reference = next(iter(tiles_grouped.values()), None)
        if reference is not None:
            empty = reference.new_zeros(0, *reference.shape[2:])
        else:
            empty = torch.zeros(0, 3, size.height, size.width)
        sample_tiles = [torch.cat(tiles_list) if tiles_list else empty for tiles_list in tiles]
        data = {
            "pixel_values": pad_sequence(sample_tiles, batch_first=True),
            "pixel_attention_mask": pad_sequence(
                [torch.ones(len(tiles), dtype=torch.bool) for tiles in sample_tiles], batch_first=True
            ),
        }
        if fine_route:
            data["tile_fine_mask"] = torch.ones(len(sample_tiles), dtype=torch.bool)
        # The tile grids are lists of different lengths, only the processor reads them to build the prompt
        data["rows"] = [[grid[0] for grid in sample] for sample in grids]
        data["cols"] = [[grid[1] for grid in sample] for sample in grids]
        return BatchFeature(data=data, tensor_type=return_tensors, skip_tensor_conversion=["rows", "cols"])

    def get_number_of_image_patches(self, height: int, width: int, images_kwargs: dict | None = None) -> int:
        """The number of tiles a page of this size yields, including the thumbnail when it is split."""
        num_rows, num_cols = self.get_tile_grid(height, width, images_kwargs)
        return num_rows * num_cols + 1 if num_rows * num_cols > 1 else 1

    def _prepare_images_structure(self, images: ImageInput, expected_ndims: int = 3) -> ImageInput:
        images = self.fetch_images(images)
        return make_nested_list_of_images(images, expected_ndims=expected_ndims)

    def get_tile_grid(self, height: int, width: int, images_kwargs: dict | None = None) -> tuple[int, int]:
        """The `(num_rows, num_columns)` tile grid a page of this size is split into."""
        images_kwargs = images_kwargs or {}
        return get_page_tile_grid(
            height,
            width,
            images_kwargs.get("size", self.size),
            images_kwargs.get("crop_to_patches", self.crop_to_patches),
            images_kwargs.get("min_patches", self.min_patches),
            images_kwargs.get("max_patches", self.max_patches),
            images_kwargs.get("dense_max_patches", self.dense_max_patches),
            images_kwargs.get("dense_max_scale", self.dense_max_scale),
            images_kwargs.get("dense_min_aspect", self.dense_min_aspect),
            images_kwargs.get("dense_max_aspect", self.dense_max_aspect),
        )


__all__ = ["GraniteDoclingImageProcessor"]
