#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/sam2_video/modular_sam2_video.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_sam2_video.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2025 The Meta AI Authors 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 huggingface_hub.dataclasses import strict

from ...configuration_utils import PreTrainedConfig
from ...utils import auto_docstring
from ..auto import CONFIG_MAPPING, AutoConfig


@auto_docstring(checkpoint="facebook/sam2_video.1-hiera-tiny")
@strict
class Sam2VideoPromptEncoderConfig(PreTrainedConfig):
    r"""
    mask_input_channels (`int`, *optional*, defaults to 16):
        The number of channels to be fed to the `MaskDecoder` module.
    num_point_embeddings (`int`, *optional*, defaults to 4):
        The number of point embeddings to be used.
    scale (`float`, *optional*, defaults to 1):
        The scale factor for the prompt encoder.
    """

    base_config_key = "prompt_encoder_config"

    hidden_size: int = 256
    image_size: int | list[int] | tuple[int, int] = 1024
    patch_size: int | list[int] | tuple[int, int] = 16
    mask_input_channels: int = 16
    num_point_embeddings: int = 4
    hidden_act: str = "gelu"
    layer_norm_eps: float = 1e-6
    scale: int = 1


@auto_docstring(checkpoint="facebook/sam2_video.1-hiera-tiny")
@strict
class Sam2VideoMaskDecoderConfig(PreTrainedConfig):
    r"""
    mlp_dim (`int`, *optional*, defaults to 2048):
        The dimension of the MLP in the two-way transformer.
    attention_downsample_rate (`int`, *optional*, defaults to 2):
        The downsample rate for the attention layers.
    num_multimask_outputs (`int`, *optional*, defaults to 3):
        The number of multimask outputs.
    iou_head_depth (`int`, *optional*, defaults to 3):
        The depth of the IoU head.
    iou_head_hidden_dim (`int`, *optional*, defaults to 256):
        The hidden dimension of the IoU head.
    dynamic_multimask_via_stability (`bool`, *optional*, defaults to `True`):
        Whether to use dynamic multimask via stability.
    dynamic_multimask_stability_delta (`float`, *optional*, defaults to 0.05):
        The stability delta for the dynamic multimask.
    dynamic_multimask_stability_thresh (`float`, *optional*, defaults to 0.98):
        The stability threshold for the dynamic multimask.
    """

    base_config_key = "mask_decoder_config"

    hidden_size: int = 256
    hidden_act: str = "gelu"
    mlp_dim: int = 2048
    num_hidden_layers: int = 2
    num_attention_heads: int = 8
    attention_downsample_rate: int = 2
    num_multimask_outputs: int = 3
    iou_head_depth: int = 3
    iou_head_hidden_dim: int = 256
    dynamic_multimask_via_stability: bool = True
    dynamic_multimask_stability_delta: float = 0.05
    dynamic_multimask_stability_thresh: float = 0.98


@auto_docstring(checkpoint="facebook/sam2.1-hiera-tiny")
@strict
class Sam2VideoConfig(PreTrainedConfig):
    r"""
    prompt_encoder_config (Union[`dict`, `Sam2PromptEncoderConfig`], *optional*):
        Dictionary of configuration options used to initialize [`Sam2PromptEncoderConfig`].
    mask_decoder_config (Union[`dict`, `Sam2MaskDecoderConfig`], *optional*):
        Dictionary of configuration options used to initialize [`Sam2MaskDecoderConfig`].
    initializer_range (`float`, *optional*, defaults to 0.02):
        Standard deviation for parameter initialization.
    num_maskmem (`int`, *optional*, defaults to 7):
        The number of memory slots for the mask memory.
    sigmoid_scale_for_mem_enc (`float`, *optional*, defaults to 20.0):
        Scale factor for the sigmoid function in the memory encoder.
    sigmoid_bias_for_mem_enc (`float`, *optional*, defaults to -10.0):
        Bias for the sigmoid function in the memory encoder.
    enable_occlusion_spatial_embedding (`bool`, *optional*, defaults to `True`):
        Whether to enable spatial embedding for occlusions.
    multimask_output_in_sam (`bool`, *optional*, defaults to `True`):
        Whether to output multiple masks from the SAM head.
    multimask_min_pt_num (`int`, *optional*, defaults to 0):
        The minimum number of points to trigger multimask output.
    multimask_max_pt_num (`int`, *optional*, defaults to 1):
        The maximum number of points to trigger multimask output.
    multimask_output_for_tracking (`bool`, *optional*, defaults to `True`):
        Whether to use multimask output for tracking.
    max_object_pointers_in_encoder (`int`, *optional*, defaults to 16):
        The maximum number of object pointers in the encoder.
    max_cond_frame_num (`int`, *optional*, defaults to -1):
        Maximum number of conditioning frames to use in memory attention. Set to -1 to use all conditioning frames.
    enable_temporal_pos_encoding_for_object_pointers (`bool`, *optional*, defaults to `True`):
        Whether to enable temporal positional encoding for object pointers.
    memory_attention_hidden_size (`int`, *optional*, defaults to 256):
        Dimensionality of the memory attention hidden states.
    memory_attention_num_layers (`int`, *optional*, defaults to 4):
        The number of layers in the memory attention module.
    memory_attention_num_attention_heads (`int`, *optional*, defaults to 1):
        Number of attention heads for each attention layer in the memory attention.
    memory_attention_downsample_rate (`int`, *optional*, defaults to 1):
        The downsample rate for the attention layers.
    memory_attention_feed_forward_hidden_size (`int`, *optional*, defaults to 2048):
        The dimension of the feedforward network in the memory attention module.
    memory_attention_feed_forward_hidden_act (`str`, *optional*, defaults to `"relu"`):
        The non-linear activation function in the feedforward network in the memory attention module.
    memory_attention_dropout (`float`, *optional*, defaults to 0.1):
        The dropout rate for the memory attention module.
    memory_attention_rope_theta (`float`, *optional*, defaults to 10000):
        The Rope theta parameter.
    memory_attention_rope_feat_sizes (`list[int]`, *optional*, defaults to `[64, 64]`):
        The feature sizes for the Rope positional encoding.
    memory_attention_rope_dropout (`float`, *optional*, defaults to 0.1):
        The dropout rate for the Rope positional encoding.
    memory_encoder_hidden_size (`int`, *optional*, defaults to 256):
        Dimensionality of the memory encoder hidden states.
    memory_encoder_output_channels (`int`, *optional*, defaults to 64):
        The number of output channels for the memory encoder.
    mask_downsampler_embed_dim (`int`, *optional*, defaults to 256):
        The dimension of the mask downsampler embedding.
    mask_downsampler_kernel_size (`int`, *optional*, defaults to 3):
        The kernel size for the mask downsampler.
    mask_downsampler_stride (`int`, *optional*, defaults to 2):
        The stride for the mask downsampler.
    mask_downsampler_padding (`int`, *optional*, defaults to 1):
        The padding for the mask downsampler.
    mask_downsampler_total_stride (`int`, *optional*, defaults to 16):
        The total stride for the mask downsampler.
    mask_downsampler_hidden_act (`str`, *optional*, defaults to `"gelu"`):
        The non-linear activation function in the mask downsampler.
    memory_fuser_num_layers (`int`, *optional*, defaults to 2):
        The number of layers in the memory fuser.
    memory_fuser_embed_dim (`int`, *optional*, defaults to 256):
        The dimension of the embedding layer in the memory fuser.
    memory_fuser_intermediate_dim (`int`, *optional*, defaults to 1024):
        The dimension of the intermediate layer in the memory fuser.
    memory_fuser_kernel_size (`int`, *optional*, defaults to 7):
        The kernel size for the memory fuser.
    memory_fuser_padding (`int`, *optional*, defaults to 3):
        The padding for the memory fuser.
    memory_fuser_layer_scale_init_value (`float`, *optional*, defaults to 1e-06):
        The initial value for the layer scale in the memory fuser.
    memory_fuser_hidden_act (`str`, *optional*, defaults to `"gelu"`):
        The non-linear activation function in the memory fuser..

    Example:

    ```python
    >>> from transformers import (
    ...     Sam2VisionConfig,
    ...     Sam2PromptEncoderConfig,
    ...     Sam2MaskDecoderConfig,
    ...     Sam2Model,
    ... )

    >>> # Initializing a Sam2Config with `"facebook/sam2.1_hiera_tiny"` style configuration
    >>> configuration = Sam2config()

    >>> # Initializing a Sam2Model (with random weights) from the `"facebook/sam2.1_hiera_tiny"` style configuration
    >>> model = Sam2Model(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config

    >>> # We can also initialize a Sam2Config from a Sam2VisionConfig, Sam2PromptEncoderConfig, and Sam2MaskDecoderConfig

    >>> # Initializing SAM2 vision encoder, memory attention, and memory encoder configurations
    >>> vision_config = Sam2VisionConfig()
    >>> prompt_encoder_config = Sam2PromptEncoderConfig()
    >>> mask_decoder_config = Sam2MaskDecoderConfig()

    >>> config = Sam2Config(vision_config, prompt_encoder_config, mask_decoder_config)
    ```"""

    model_type = "sam2_video"
    sub_configs = {
        "vision_config": AutoConfig,
        "prompt_encoder_config": Sam2VideoPromptEncoderConfig,
        "mask_decoder_config": Sam2VideoMaskDecoderConfig,
    }

    vision_config: dict | PreTrainedConfig | None = None
    prompt_encoder_config: dict | PreTrainedConfig | None = None
    mask_decoder_config: dict | PreTrainedConfig | None = None
    initializer_range: float = 0.02
    num_maskmem: int = 7
    image_size: int | list[int] | tuple[int, int] = 1024
    sigmoid_scale_for_mem_enc: float = 20.0
    sigmoid_bias_for_mem_enc: float = -10.0
    enable_occlusion_spatial_embedding: bool = True
    multimask_output_in_sam: bool = True
    multimask_min_pt_num: int = 0
    multimask_max_pt_num: int = 1
    multimask_output_for_tracking: bool = True
    max_object_pointers_in_encoder: int = 16
    max_cond_frame_num: int = -1
    enable_temporal_pos_encoding_for_object_pointers: bool = True
    memory_attention_hidden_size: int = 256
    memory_attention_num_layers: int = 4
    memory_attention_num_attention_heads: int = 1
    memory_attention_downsample_rate: int = 1
    memory_attention_feed_forward_hidden_size: int = 2048
    memory_attention_feed_forward_hidden_act: str = "relu"
    memory_attention_dropout: float | int = 0.1
    memory_attention_rope_theta: int = 10000
    memory_attention_rope_feat_sizes: list[int] | None = None
    memory_attention_rope_dropout: float | int = 0.1
    memory_encoder_hidden_size: int = 256
    memory_encoder_output_channels: int = 64
    mask_downsampler_embed_dim: int = 256
    mask_downsampler_kernel_size: int = 3
    mask_downsampler_stride: int = 2
    mask_downsampler_padding: int = 1
    mask_downsampler_total_stride: int = 16
    mask_downsampler_hidden_act: str = "gelu"
    memory_fuser_num_layers: int = 2
    memory_fuser_embed_dim: int = 256
    memory_fuser_intermediate_dim: int = 1024
    memory_fuser_kernel_size: int = 7
    memory_fuser_padding: int = 3
    memory_fuser_layer_scale_init_value: float = 1e-6
    memory_fuser_hidden_act: str = "gelu"

    def __post_init__(self, **kwargs):
        self.memory_attention_rope_feat_sizes = (
            [64, 64] if self.memory_attention_rope_feat_sizes is None else self.memory_attention_rope_feat_sizes
        )

        if isinstance(self.vision_config, dict):
            self.vision_config["model_type"] = self.vision_config.get("model_type", "sam2_vision_model")
            self.vision_config = CONFIG_MAPPING[self.vision_config["model_type"]](**self.vision_config)
        elif self.vision_config is None:
            self.vision_config = CONFIG_MAPPING["sam2_vision_model"]()

        if isinstance(self.prompt_encoder_config, dict):
            self.prompt_encoder_config = Sam2VideoPromptEncoderConfig(**self.prompt_encoder_config)
        elif self.prompt_encoder_config is None:
            self.prompt_encoder_config = Sam2VideoPromptEncoderConfig()

        if isinstance(self.mask_decoder_config, dict):
            self.mask_decoder_config = Sam2VideoPromptEncoderConfig(**self.mask_decoder_config)
        elif self.mask_decoder_config is None:
            self.mask_decoder_config = Sam2VideoMaskDecoderConfig()

        super().__post_init__(**kwargs)


__all__ = ["Sam2VideoMaskDecoderConfig", "Sam2VideoPromptEncoderConfig", "Sam2VideoConfig"]
