#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/unlimited_ocr/modular_unlimited_ocr.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_unlimited_ocr.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.
from huggingface_hub.dataclasses import strict

from ...configuration_utils import PreTrainedConfig
from ...modeling_rope_utils import RopeParameters
from ...utils import auto_docstring


@auto_docstring(checkpoint="baidu/Unlimited-OCR")
@strict
class UnlimitedOcrSamVisionConfig(PreTrainedConfig):
    r"""
    output_channels (`int`, *optional*, defaults to 256):
        Dimensionality of the output channels in the Patch Encoder.
    use_abs_pos (`bool`, *optional*, defaults to `True`):
        Whether to use absolute position embedding.
    use_rel_pos (`bool`, *optional*, defaults to `True`):
        Whether to use relative position embedding.
    window_size (`int`, *optional*, defaults to 14):
        Window size for relative position.
    global_attn_indexes (`list[int]`, *optional*, defaults to `[2, 5, 8, 11]`):
        The indexes of the global attention layers.
    mlp_dim (`int`, *optional*, defaults to 3072):
        The dimensionality of the MLP layer in the Transformer encoder.
    downsample_channels (`list[int]`, *optional*, defaults to `(512, 1024)`):
        The channel dimensions for the multi-scale downsampling neck layers.
    """

    base_config_key = "sam_config"

    model_type = "unlimited_ocr_sam_vision_model"

    hidden_size: int = 768
    output_channels: int = 256
    num_hidden_layers: int = 12
    num_attention_heads: int = 12
    num_channels: int = 3
    image_size: int | list[int] | tuple[int, int] = 1024
    patch_size: int | list[int] | tuple[int, int] = 16
    hidden_act: str = "gelu"
    layer_norm_eps: float = 1e-06
    attention_dropout: float | int = 0.0
    initializer_range: float = 1e-10
    qkv_bias: bool = True
    use_abs_pos: bool = True
    use_rel_pos: bool = True
    window_size: int = 14
    global_attn_indexes: list[int] | tuple[int, ...] = (2, 5, 8, 11)
    mlp_dim: int = 3072

    downsample_channels: list[int] | tuple[int, ...] = (512, 1024)


@auto_docstring(checkpoint="baidu/Unlimited-OCR")
@strict
class UnlimitedOcrVisionEncoderConfig(PreTrainedConfig):
    r"""
    Example:

    ```python
    >>> from transformers import UnlimitedOcrVisionEncoderConfig, UnlimitedOcrVisionEncoder

    >>> configuration = UnlimitedOcrVisionEncoderConfig()
    >>> model = UnlimitedOcrVisionEncoder(configuration)
    >>> configuration = model.config
    ```"""

    model_type = "unlimited_ocr_vision_encoder"
    base_config_key = "encoder_config"
    hidden_size: int = 1024
    intermediate_size: int = 4096
    num_hidden_layers: int = 24
    num_attention_heads: int = 16
    num_channels: int = 3
    image_size: int | list[int] | tuple[int, int] | None = 224
    patch_size: int | list[int] | tuple[int, int] | None = 14
    hidden_act: str = "quick_gelu"
    layer_norm_eps: float = 1e-5
    attention_dropout: int | float | None = 0.0
    initializer_range: float = 0.02
    initializer_factor: float = 1.0

    def validate_architecture(self):
        """Part of `@strict`-powered validation. Validates the architecture of the config."""
        if self.hidden_size % self.num_attention_heads != 0:
            raise ValueError(
                f"The hidden size ({self.hidden_size}) is not a multiple of the number of attention "
                f"heads ({self.num_attention_heads})."
            )


@auto_docstring(checkpoint="baidu/Unlimited-OCR")
@strict
class UnlimitedOcrVisionConfig(PreTrainedConfig):
    r"""
    sam_config (`dict` or `UnlimitedOcrSamVisionConfig`, *optional*):
        Configuration for the SAM vision encoder. Defaults to `UnlimitedOcrSamVisionConfig()`.
    encoder_config (`dict` or `UnlimitedOcrVisionEncoderConfig`, *optional*):
        Configuration for the vision encoder. Defaults to `UnlimitedOcrVisionEncoderConfig()`.

    Example:

    ```python
    >>> from transformers import UnlimitedOcrVisionConfig, UnlimitedOcrVisionModel

    >>> configuration = UnlimitedOcrVisionConfig()
    >>> model = UnlimitedOcrVisionModel(configuration)
    >>> configuration = model.config
    ```"""

    model_type = "unlimited_ocr_vision"
    base_config_key = "vision_config"
    sub_configs_defaults = {
        "sam_config": UnlimitedOcrSamVisionConfig,
        "encoder_config": UnlimitedOcrVisionEncoderConfig,
    }
    sam_config: dict | PreTrainedConfig | None = None
    encoder_config: dict | PreTrainedConfig | None = None


@auto_docstring(checkpoint="baidu/Unlimited-OCR")
@strict
class UnlimitedOcrTextConfig(PreTrainedConfig):
    r"""
    n_group (`int`, *optional*):
        Number of groups for grouped top-k expert routing.
    topk_method (`str`, *optional*, defaults to `"greedy"`):
        Method for selecting top-k experts in MoE layers.
    mlp_layer_types (`list[str]`, *optional*):
        MLP type (`"dense"` or `"sparse"`) for each decoder layer, e.g. `["dense", "sparse", "sparse", ...]`.
    layer_types (`list[str]`, *optional*):
        Attention type for each decoder layer. Defaults to `"reference_sliding_attention"` on every layer.
    sliding_window (`int`, *optional*, defaults to `128`):
        Sliding window size for reference sliding window attention. If set, every token attends to the last
        `sliding_window` and all image and prompt tokens.

    Example:

    ```python
    >>> from transformers import UnlimitedOcrTextConfig, UnlimitedOcrTextModel

    >>> configuration = UnlimitedOcrTextConfig()
    >>> model = UnlimitedOcrTextModel(configuration)
    >>> configuration = model.config
    ```"""

    model_type = "unlimited_ocr_text"
    keys_to_ignore_at_inference = ["past_key_values"]

    # Override DeepseekV2's MLA TP plan with standard MHA projections
    base_model_tp_plan = {
        "layers.*.self_attn.q_proj": "colwise",
        "layers.*.self_attn.k_proj": "colwise",
        "layers.*.self_attn.v_proj": "colwise",
        "layers.*.self_attn.o_proj": "rowwise",
        "layers.*.mlp.experts.gate_up_proj": "packed_colwise",
        "layers.*.mlp.experts.down_proj": "rowwise",
        "layers.*.mlp.experts": "moe_tp_experts",
        "layers.*.mlp.shared_experts.gate_proj": "colwise",
        "layers.*.mlp.shared_experts.up_proj": "colwise",
        "layers.*.mlp.shared_experts.down_proj": "rowwise",
        "layers.*.mlp.gate_proj": "colwise",
        "layers.*.mlp.up_proj": "colwise",
        "layers.*.mlp.down_proj": "rowwise",
    }
    base_model_pp_plan = {
        "embed_tokens": (["input_ids"], ["inputs_embeds"]),
        "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
        "norm": (["hidden_states"], ["hidden_states"]),
    }
    vocab_size: int = 129280
    hidden_size: int = 1280
    intermediate_size: int = 6848
    num_hidden_layers: int = 12
    num_attention_heads: int = 10
    num_key_value_heads: int | None = 10
    hidden_act: str = "silu"
    max_position_embeddings: int = 32768
    initializer_range: float = 0.02
    rms_norm_eps: float = 1e-6
    use_cache: bool = True
    pad_token_id: int | None = None
    bos_token_id: int | None = 0
    eos_token_id: int | list[int] | None = 1
    pretraining_tp: int | None = 1
    tie_word_embeddings: bool = False
    rope_parameters: RopeParameters | dict | None = None
    attention_bias: bool = False
    attention_dropout: float | None = 0.0
    mlp_bias: bool = False
    head_dim: int | None = None
    base_model_ep_plan = {
        "layers.*.mlp.experts.gate_up_proj": "grouped_gemm",
        "layers.*.mlp.experts.down_proj": "grouped_gemm",
        "layers.*.mlp.experts": "ep_dispatch_experts",
    }
    attribute_map = {
        "num_experts": "n_routed_experts",
    }
    n_group: int | None = 1
    n_routed_experts: int = 64
    output_router_logits: bool = False
    n_shared_experts: int = 2
    routed_scaling_factor: float = 1.0
    topk_group: int | None = 1
    topk_method: str | None = "greedy"
    num_experts_per_tok: int | None = 6
    moe_intermediate_size: int = 896
    base_config_key = "text_config"
    mlp_layer_types: list[str] | None = None
    layer_types: list[str] | None = None
    sliding_window: int | None = 128

    def __post_init__(self, **kwargs):
        if self.layer_types is None:
            self.layer_types = [
                "reference_sliding_attention" if self.sliding_window is not None else "full_attention"
            ] * self.num_hidden_layers
        if self.mlp_layer_types is None:
            # Some configs may use `first_k_dense_replace` instead of `layer_types`/`mlp_layer_types`
            first_k_dense_replace = kwargs.pop("first_k_dense_replace", 1)
            self.mlp_layer_types = [
                "sparse" if layer_idx >= first_k_dense_replace else "dense"
                for layer_idx in range(self.num_hidden_layers)
            ]
        self.head_dim = self.hidden_size // self.num_attention_heads
        if self.num_key_value_heads is None:
            self.num_key_value_heads = self.num_attention_heads
        super().__post_init__(**kwargs)

    def validate_architecture(self):
        """Part of `@strict`-powered validation. Validates the architecture of the config."""
        if self.hidden_size % self.num_attention_heads != 0:
            raise ValueError(
                f"The hidden size ({self.hidden_size}) is not a multiple of the number of attention "
                f"heads ({self.num_attention_heads})."
            )
        if self.sliding_window is None:
            raise ValueError("`sliding_window` cannot be `None` for this model.")


@auto_docstring(checkpoint="baidu/Unlimited-OCR")
@strict
class UnlimitedOcrConfig(PreTrainedConfig):
    r"""
    vision_config (`dict` or `UnlimitedOcrVisionConfig`, *optional*):
        Configuration for the vision encoders. Defaults to `UnlimitedOcrVisionConfig()`.

    Example:

    ```python
    >>> from transformers import UnlimitedOcrConfig, UnlimitedOcrModel

    >>> configuration = UnlimitedOcrConfig()
    >>> model = UnlimitedOcrModel(configuration)
    >>> configuration = model.config
    ```"""

    model_type = "unlimited_ocr"
    sub_configs_defaults = {
        "vision_config": UnlimitedOcrVisionConfig,
        "text_config": UnlimitedOcrTextConfig,
    }

    vision_config: dict | PreTrainedConfig | None = None
    text_config: dict | PreTrainedConfig | None = None
    image_token_id: int = 128815
    tie_word_embeddings: bool = False

    def __post_init__(self, **kwargs):
        if self.text_config is None:
            # For backward compatibility.
            # The original checkpoint stores the text config under `language_config`, and `sliding_window` only on the main config
            self.text_config = kwargs.pop("language_config", {})
            if "sliding_window" in kwargs:
                self.text_config["sliding_window"] = kwargs.pop("sliding_window")
        super().__post_init__(**kwargs)


__all__ = [
    "UnlimitedOcrConfig",
    "UnlimitedOcrTextConfig",
    "UnlimitedOcrVisionConfig",
    "UnlimitedOcrVisionEncoderConfig",
    "UnlimitedOcrSamVisionConfig",
]
