#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/embedding_gemma2/modular_embedding_gemma2.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_embedding_gemma2.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 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.

import math

import numpy as np
import torch

from ...image_processing_utils import BatchFeature
from ...processing_utils import Unpack, VideosKwargs
from ...utils import (
    TensorType,
    auto_docstring,
    is_torchvision_available,
    is_torchvision_v2_available,
    is_vision_available,
    logging,
)
from ...video_processing_utils import BaseVideoProcessor, VideoMetadata
from ...video_utils import VideoInput


if is_vision_available():
    from ...image_utils import PILImageResampling

if is_torchvision_v2_available():
    from torchvision.transforms.v2 import functional as tvF
elif is_torchvision_available():
    from torchvision.transforms import functional as tvF


logger = logging.get_logger(__name__)


class EmbeddingGemma2VideoProcessorKwargs(VideosKwargs, total=False):
    """
    patch_size (`int`, *optional*):
        Size of each image patch in pixels.
    max_soft_tokens (`int`, *optional*):
        Maximum number of soft (vision) tokens per video frame.
        Must be one of {70, 140, 280, 560, 1120}.
    pooling_kernel_size (`int`, *optional*):
        Spatial pooling kernel size applied after patchification.
    add_timestamps (`bool`, *optional*):
        Whether to prefix each frame in the video placeholder expansion with its `mm:ss` timestamp.
        Requires `VideoMetadata` with a valid `fps`, since timestamps cannot be inferred from
        already-decoded frames.
    max_frames (`int`, *optional*):
        The maximum number of frames to sample. If set, the sampled indices will
        be uniformly re-sampled to fit the budget.
    overflow_strategy (`str`, *optional*):
        The strategy used to cut the total number of sampled frames down to fit into the budget.
        Can be set only to "uniform" or "truncate". Applied after FPS-based sampling, and on its
        own when FPS-based sampling is off or not applicable.
    load_audio_from_video (`bool`, *optional*):
        Deprecated, use [`OmniModalProcessingKwargs.load_audio_from_video`] instead.
        Whether to load the audio track of an input video or not.
    """

    patch_size: int
    max_soft_tokens: int
    pooling_kernel_size: int

    add_timestamps: bool
    max_frames: int | None
    overflow_strategy: str | None
    load_audio_from_video: bool


_SUPPORTED_SOFT_TOKENS = (70, 140, 280, 560, 1120)


def get_aspect_ratio_preserving_size(
    height: int,
    width: int,
    patch_size: int,
    max_patches: int,
    pooling_kernel_size: int,
) -> tuple[int, int]:
    """
    Image is resized to preserve aspect ratio so it fits within the patch budget.
    Target dimensions are the largest that:
    1) Produce at most `max_patches` patches when patchified with `patch_size`
    2) Have height and width divisible by `pooling_kernel_size * patch_size`
    """
    total_px = height * width
    target_px = max_patches * (patch_size**2)
    factor = math.sqrt(target_px / total_px)
    ideal_height = factor * height
    ideal_width = factor * width
    side_mult = pooling_kernel_size * patch_size

    # Round down to nearest multiple of side_mult
    target_height = int(math.floor(ideal_height / side_mult)) * side_mult
    target_width = int(math.floor(ideal_width / side_mult)) * side_mult

    # Handle edge cases where one or both dimensions round to 0
    if target_height == 0 and target_width == 0:
        raise ValueError(
            "Attempting to resize to a 0 x 0 image. Resized height should be divisible by "
            f"`pooling_kernel_size * patch_size`={pooling_kernel_size * patch_size}."
        )

    max_side_length = (max_patches // pooling_kernel_size**2) * side_mult
    if target_height == 0:
        target_height = side_mult
        target_width = min(
            int(math.floor(width / height)) * side_mult,
            max_side_length,
        )
    elif target_width == 0:
        target_width = side_mult
        target_height = min(
            int(math.floor(height / width)) * side_mult,
            max_side_length,
        )

    if target_height * target_width > target_px:
        raise ValueError(
            f"Resizing [{height}x{width}] to [{target_height}x{target_width}] "
            f"but this exceeds {max_patches} patches with patch_size {patch_size}"
        )

    return target_height, target_width


def convert_video_to_patches(video: "torch.Tensor", patch_size: int) -> "torch.Tensor":
    """
    Convert 4D tensor video of shape (num_frames, num_channels, height, width) into 3D tensor of patches of shape
    (num_frames, num_patches_height * num_patches_width, patch_size * patch_size * num_channels).
    """
    num_frames, num_channels, height, width = video.shape
    num_patches_height = height // patch_size
    num_patches_width = width // patch_size
    patched_video = video.reshape(
        num_frames, num_channels, num_patches_height, patch_size, num_patches_width, patch_size
    )
    patched_video = patched_video.permute(0, 2, 4, 3, 5, 1)
    patched_video = patched_video.reshape(num_frames, num_patches_height * num_patches_width, -1)
    return patched_video


def pad_to_max_patches(
    video: "torch.Tensor", positions: "torch.Tensor", target_length: int
) -> tuple["torch.Tensor", "torch.Tensor"]:
    """
    Pad the video along to max number of patches
    """
    current_length = video.shape[1]
    padding_length = target_length - current_length
    if padding_length > 0:
        padding = [0, 0, 0, padding_length, 0, 0]
        pos_padding = (0, 0, 0, padding_length, 0, 0)
        video = torch.nn.functional.pad(video, padding, mode="constant", value=0)
        positions = torch.nn.functional.pad(positions, pos_padding, mode="constant", value=-1)
    return video, positions


@auto_docstring
class EmbeddingGemma2VideoProcessor(BaseVideoProcessor):
    resample = PILImageResampling.BICUBIC
    image_mean = [0.0, 0.0, 0.0]
    image_std = [1.0, 1.0, 1.0]
    size = None
    default_to_square = True
    do_convert_rgb = True
    do_resize = True
    do_rescale = True
    do_normalize = True
    do_sample_frames = True
    patch_size = 16
    max_soft_tokens = 70
    pooling_kernel_size = 3

    valid_kwargs = EmbeddingGemma2VideoProcessorKwargs
    model_input_names = ["pixel_values_videos", "video_position_ids", "num_frames_per_video"]
    # unlike Gemma4 - by default sample 1 fps uniformly
    fps = 1
    max_frames = 32
    overflow_strategy = "uniform"
    add_timestamps = False

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

        if self.max_soft_tokens not in _SUPPORTED_SOFT_TOKENS:
            raise ValueError(f"`max_soft_tokens` must be one of {_SUPPORTED_SOFT_TOKENS}, got {self.max_soft_tokens}.")

    def _validate_preprocess_kwargs(self, **kwargs):
        # EmbeddingGemma2 uses aspect_ratio_preserving_resize driven by patch_size,
        # max_soft_tokens, and pooling_kernel_size — not the standard `size`
        # parameter. Temporarily disable do_resize so the base validation
        # doesn't require `size` to be set.
        kwargs["do_resize"] = False
        super()._validate_preprocess_kwargs(**kwargs)

    def aspect_ratio_preserving_resize(
        self,
        video: torch.Tensor,
        patch_size: int,
        max_patches: int,
        pooling_kernel_size: int,
        resample: tvF.InterpolationMode,
    ) -> torch.Tensor:
        height, width = video.shape[-2], video.shape[-1]
        target_height, target_width = get_aspect_ratio_preserving_size(
            height=height,
            width=width,
            patch_size=patch_size,
            max_patches=max_patches,
            pooling_kernel_size=pooling_kernel_size,
        )

        if target_height == height and target_width == width:
            return video

        return tvF.resize(
            video,
            size=[target_height, target_width],
            interpolation=resample,
            antialias=True,
        )

    @auto_docstring
    def preprocess(
        self,
        videos: VideoInput,
        **kwargs: Unpack[EmbeddingGemma2VideoProcessorKwargs],
    ) -> BatchFeature:
        return super().preprocess(videos, **kwargs)

    def _preprocess(
        self,
        videos: list["torch.Tensor"],
        do_resize: bool,
        resample: "tvF.InterpolationMode | int | None",
        do_rescale: bool,
        rescale_factor: float,
        do_normalize: bool,
        image_mean: float | list[float] | None,
        image_std: float | list[float] | None,
        return_tensors: str | TensorType | None,
        patch_size: int | None = None,
        max_soft_tokens: int | None = None,
        pooling_kernel_size: int | None = None,
        **kwargs,
    ) -> BatchFeature:
        if max_soft_tokens not in _SUPPORTED_SOFT_TOKENS:
            raise ValueError(f"`max_soft_tokens` must be one of {_SUPPORTED_SOFT_TOKENS}, got {max_soft_tokens}.")

        max_patches = max_soft_tokens * pooling_kernel_size**2

        pixel_values = []
        position_ids = []
        num_soft_tokens_per_video = []
        num_frames_per_video = []

        for video in videos:
            if do_resize:
                video = self.aspect_ratio_preserving_resize(
                    video=video,
                    patch_size=patch_size,
                    max_patches=max_patches,
                    pooling_kernel_size=pooling_kernel_size,
                    resample=resample,
                )

            video = self.rescale_and_normalize(video, do_rescale, rescale_factor, do_normalize, image_mean, image_std)

            num_frames = video.shape[0]
            patch_height = video.shape[-2] // patch_size
            patch_width = video.shape[-1] // patch_size
            patches = convert_video_to_patches(video, patch_size)
            num_soft_tokens_per_video.append(patches.shape[1] // pooling_kernel_size**2)
            num_frames_per_video.append(num_frames)

            device = video.device
            patch_grid = torch.meshgrid(
                torch.arange(patch_width, device=device),
                torch.arange(patch_height, device=device),
                indexing="xy",
            )
            stacked_grid = torch.stack(patch_grid, dim=-1)
            real_positions = stacked_grid.reshape(patches.shape[1], 2)
            real_positions = real_positions[None, ...].repeat(num_frames, 1, 1)

            patches, positions = pad_to_max_patches(patches, real_positions, max_patches)
            pixel_values.append(patches)
            position_ids.append(positions)

        # Concatenate along the frame axis rather than stacking on a new video axis, so that videos with
        # different frame counts can be batched together. `num_frames_per_video` splits it back per video.
        pixel_values = torch.cat(pixel_values, dim=0)  # (total_num_frames, max_patches, patch_pixels)
        position_ids = torch.cat(position_ids, dim=0)  # (total_num_frames, max_patches, 2)

        data = {
            "pixel_values_videos": pixel_values,
            "video_position_ids": position_ids,
            "num_frames_per_video": num_frames_per_video,
            "num_soft_tokens_per_video": num_soft_tokens_per_video,
        }
        return BatchFeature(data=data, tensor_type=return_tensors)

    def sample_frames(
        self,
        metadata: VideoMetadata,
        fps: int | float | None = None,
        max_frames: int | None = None,
        overflow_strategy: str | None = None,
        **kwargs,
    ) -> np.ndarray:
        if kwargs.get("num_frames") is not None:
            raise ValueError(
                f"Sampling with `num_frames` is not supported for {self.__class__.__name__}. "
                "Please use `fps` and `max_frames` to control video sampling."
            )

        # 1) Sample to match the target `fps` if it is set, otherwise keep the whole video.
        # A decoded array carries no frame rate, and neither `fps` nor `duration` can be inferred
        # from one, so rate-based sampling is simply not applicable to that input. Skip it rather
        # than guess a source rate: a wrong guess silently discards frames.
        if fps is not None and (metadata.fps is None or metadata.duration is None):
            logger.warning_once(
                "Asked to sample uniformly with `fps`, but the video metadata has no `fps` or `duration`. "
                "Keeping every frame and applying only the `max_frames` budget. Pass a `VideoMetadata` "
                "object with a valid `fps` and `duration` to sample at a target frame rate."
            )
            fps = None

        if fps is None:
            indices = np.arange(metadata.total_num_frames, dtype=int)
        else:
            step = metadata.fps / fps  # native frames per sampled frame
            num_sampled = max(1, int(metadata.duration * fps))
            indices = np.array(
                [min(metadata.total_num_frames - 1, int(i * step)) for i in range(num_sampled)], dtype=int
            )

        # 2) Cap total number of frames to `max_frames` checking the input `overflow_strategy`
        if overflow_strategy is not None:
            if max_frames is None:
                raise ValueError(
                    f"You must pass `max_frames` when requesting an overflow_strategy={overflow_strategy}!"
                )

            # If video is too short, do no accidentally pad inputs when trying to re-sample
            if len(indices) <= max_frames:
                pass
            elif overflow_strategy == "truncate":
                indices = indices[:max_frames]
            elif overflow_strategy == "uniform":
                linspace_idx = np.linspace(0, len(indices) - 1, max_frames, dtype=int)
                indices = np.array([indices[i] for i in linspace_idx], dtype=int)
            else:
                raise ValueError(
                    f"You passed `overflow_strategy={overflow_strategy}` but expected one of ['truncate', 'uniform']"
                )

        return indices


__all__ = ["EmbeddingGemma2VideoProcessor"]
