#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/olmo_hybrid/modular_olmo_hybrid.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_olmo_hybrid.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
from ...utils.type_validators import interval


@auto_docstring(checkpoint="allenai/Olmo-Hybrid-7B")
@strict
class OlmoHybridConfig(PreTrainedConfig):
    r"""
    linear_num_key_heads (`int`, *optional*):
        Number of key heads for the linear attention layers. Defaults to `num_attention_heads`.
    linear_num_value_heads (`int`, *optional*):
        Number of value heads for the linear attention layers. Defaults to `num_attention_heads`.
    linear_key_head_dim (`int`, *optional*):
        Dimension of each key head in linear attention layers. Defaults to `0.75 * hidden_size / linear_num_key_heads`.
    linear_value_head_dim (`int`, *optional*):
        Dimension of each value head in linear attention layers. Defaults to `2 * linear_key_head_dim`.
    linear_a_log_min (`float`, *optional*, defaults to 0.0):
        Minimum value for uniform initialization of A_log in GatedDeltaNet layers.
    linear_a_log_max (`float`, *optional*, defaults to 16.0):
        Maximum value for uniform initialization of A_log in GatedDeltaNet layers.
    linear_dt_min (`float`, *optional*, defaults to 0.001):
        Minimum value for dt initialization in GatedDeltaNet layers.
    linear_dt_max (`float`, *optional*, defaults to 0.1):
        Maximum value for dt initialization in GatedDeltaNet layers.
    linear_dt_init_floor (`float`, *optional*, defaults to 0.0001):
        Floor value for clamping dt during initialization in GatedDeltaNet layers.
    linear_conv_kernel_dim (`int`, *optional*, defaults to 4):
        Kernel size for the short convolution applied to queries, keys, and values in linear attention layers.
    linear_allow_neg_eigval (`bool`, *optional*, defaults to `True`):
        Whether to allow negative eigenvalues in the GatedDeltaNet recurrence. When `True`, the beta
        parameter is scaled by 2.0 to allow values in range [0, 2] instead of [0, 1].

    Example:

    ```python
    >>> from transformers import OlmoHybridModel, OlmoHybridConfig

    >>> # Initializing an OlmoHybrid style configuration
    >>> configuration = OlmoHybridConfig()

    >>> # Initializing a model from the OlmoHybrid style configuration
    >>> model = OlmoHybridModel(configuration)

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

    model_type = "olmo_hybrid"
    keys_to_ignore_at_inference = ["past_key_values"]
    base_model_tp_plan = {
        "layers.*.self_attn.q_proj": "colwise_gather_output",  # we need to replicate here due to the added norm on q and k
        "layers.*.self_attn.k_proj": "colwise_gather_output",  # we need to replicate here due to the added norm on q and k
        "layers.*.self_attn.v_proj": "colwise_gather_output",  # we need to replicate here due to the added norm on q and k
        "layers.*.self_attn.o_proj": "rowwise_split_input",  # input is replicated due to the added norm on q and k
        "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 = 100352
    hidden_size: int = 3840
    intermediate_size: int = 11008
    num_hidden_layers: int = 32
    num_attention_heads: int = 30
    num_key_value_heads: int | None = None
    hidden_act: str = "silu"
    max_position_embeddings: int = 65536
    initializer_range: float = interval(min=0.0, max=1.0)(default=0.02)
    rms_norm_eps: float = 1e-06
    use_cache: bool = True
    pad_token_id: int | None = 100277
    bos_token_id: int | None = None
    eos_token_id: int | list[int] | None = 100257
    tie_word_embeddings: bool = False
    rope_parameters: RopeParameters | dict | None = None
    attention_bias: bool = False
    attention_dropout: int | float | None = 0.0
    layer_types: list[str] | None = None
    linear_num_key_heads: int | None = None
    linear_num_value_heads: int | None = None
    linear_key_head_dim: int | None = None
    linear_value_head_dim: int | None = None
    linear_a_log_min: float = 0.0
    linear_a_log_max: float = 16.0
    linear_dt_min: float = 0.001
    linear_dt_max: float = 0.1
    linear_dt_init_floor: float = 1e-4
    linear_conv_kernel_dim: int = 4
    linear_allow_neg_eigval: bool = True

    def __post_init__(self, **kwargs):
        if self.layer_types is None:
            # Default: linear attention for most layers, full attention every 4th layer
            self.layer_types = ["linear_attention"] * int(self.num_hidden_layers)
            for i in range(int(self.num_hidden_layers)):
                if i % 4 == 3:
                    self.layer_types[i] = "full_attention"
            # Ensure at least one full attention layer for small num_hidden_layers
            if "full_attention" not in self.layer_types:
                self.layer_types[-1] = "full_attention"

        if self.linear_num_key_heads is None:
            self.linear_num_key_heads = self.num_attention_heads
        if self.linear_num_value_heads is None:
            self.linear_num_value_heads = self.num_attention_heads
        if self.linear_key_head_dim is None:
            self.linear_key_head_dim = int(0.75 * self.hidden_size / self.linear_num_key_heads)
        if self.linear_value_head_dim is None:
            self.linear_value_head_dim = 2 * self.linear_key_head_dim
        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 "linear_attention" not in self.layer_types:
            raise ValueError("OLMoHybrid expects at least one 'linear_attention' layer.")
        if all(t == "linear_attention" for t in self.layer_types):
            raise ValueError("OLMoHybrid expects at least one attention layer.")


__all__ = ["OlmoHybridConfig"]
