# coding=utf-8
# Copyright 2026 Microsoft and the HuggingFace Inc. team. All rights reserved.
#
# Licensed under the MIT License.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""AionInstruct model configuration"""

from huggingface_hub.dataclasses import strict
from transformers.configuration_utils import PreTrainedConfig

ATTENTION_LAYER_TYPES = ("full_attention", "sliding_attention")


@strict
class AionInstructConfig(PreTrainedConfig):
    r"""
    This is the configuration class to store the configuration of an [`AionInstructModel`]. It is used to instantiate
    an AionInstruct model according to the specified arguments, defining the model architecture. The defaults
    correspond to the 2.3B-parameter Aion 1.1 Instruct model, except for `layer_types`, which defaults to full
    attention in every layer.

    AionInstruct is a decoder-only Transformer with grouped-query attention, RMSNorm on every query and key head,
    a SwiGLU MLP, tied input/output embeddings, and a per-layer choice between full and sliding-window attention.
    Each attention type has its own rotary position embedding parameters: Aion 1.1 extends the context of its
    full-attention layers with YaRN and keeps un-scaled RoPE in its sliding-window layers.

    Configuration objects inherit from [`PreTrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PreTrainedConfig`] for more information.

    Args:
        vocab_size (`int`, *optional*, defaults to 200029):
            Vocabulary size of the model.
        hidden_size (`int`, *optional*, defaults to 2048):
            Dimension of the hidden representations.
        intermediate_size (`int`, *optional*, defaults to 8192):
            Dimension of the MLP representations.
        num_hidden_layers (`int`, *optional*, defaults to 28):
            Number of decoder layers.
        num_attention_heads (`int`, *optional*, defaults to 24):
            Number of query heads in each attention layer.
        num_key_value_heads (`int`, *optional*, defaults to 8):
            Number of key/value heads for grouped-query attention. `None` means `num_attention_heads`.
        head_dim (`int`, *optional*, defaults to 128):
            Dimension of each attention head. It is independent of `hidden_size // num_attention_heads`.
        hidden_act (`str`, *optional*, defaults to `"silu"`):
            Activation of the gated MLP.
        max_position_embeddings (`int`, *optional*, defaults to 32768):
            Maximum sequence length the model supports.
        initializer_range (`float`, *optional*, defaults to 0.02):
            Standard deviation of the normal initializer of the weight matrices.
        rms_norm_eps (`float`, *optional*, defaults to 1e-06):
            Epsilon of the RMS normalization layers.
        use_cache (`bool`, *optional*, defaults to `True`):
            Whether the model returns the last key/value states.
        tie_word_embeddings (`bool`, *optional*, defaults to `True`):
            Whether the output projection shares the input embedding matrix.
        rope_parameters (`dict`, *optional*):
            Rotary position embedding parameters for each attention type: a dictionary whose keys are the layer
            types in `layer_types` and whose values are [`~modeling_rope_utils.RopeParameters`] dictionaries. A
            single, un-nested dictionary applies to every layer type. A layer type without parameters uses un-scaled
            RoPE. Defaults to YaRN (factor 16 over an original context of 2048 positions, base 32000) for
            full-attention layers and un-scaled RoPE with the same base for sliding-window layers.
        attention_bias (`bool`, *optional*, defaults to `False`):
            Whether the query, key, value and output projections have a bias.
        attention_dropout (`float`, *optional*, defaults to 0.0):
            Dropout ratio of the attention probabilities.
        sliding_window (`int`, *optional*, defaults to 2048):
            Attention window of the `"sliding_attention"` layers. Unused by `"full_attention"` layers.
        layer_types (`list[str]`, *optional*):
            Attention type of each layer, `"full_attention"` or `"sliding_attention"`. Defaults to full attention in
            every layer.
        pad_token_id (`int`, *optional*):
            Padding token id.
        bos_token_id (`int`, *optional*):
            Beginning of stream token id.
        eos_token_id (`int` or `list[int]`, *optional*):
            End of stream token id(s).

    ```python
    >>> from transformers import AionInstructConfig, AionInstructModel

    >>> # Initializing an AionInstruct configuration
    >>> configuration = AionInstructConfig()

    >>> # Initializing a model (with random weights) from the configuration
    >>> model = AionInstructModel(configuration)

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

    model_type = "aion_instruct"
    keys_to_ignore_at_inference = ["past_key_values"]
    default_theta = 32_000.0

    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.q_norm": "replicated_with_grad_allreduce",
        "layers.*.self_attn.k_norm": "replicated_with_grad_allreduce",
        "layers.*.self_attn.o_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 = 200029
    hidden_size: int = 2048
    intermediate_size: int = 8192
    num_hidden_layers: int = 28
    num_attention_heads: int = 24
    num_key_value_heads: int | None = 8
    head_dim: int = 128
    hidden_act: str = "silu"
    max_position_embeddings: int = 32768
    initializer_range: float = 0.02
    rms_norm_eps: float = 1e-6
    use_cache: bool = True
    tie_word_embeddings: bool = True
    rope_parameters: dict | None = None
    attention_bias: bool = False
    attention_dropout: float | int = 0.0
    sliding_window: int | None = 2048
    layer_types: list[str] | None = None
    pad_token_id: int | None = None
    bos_token_id: int | None = None
    eos_token_id: int | list[int] | None = None

    def __post_init__(self, **kwargs):
        if self.num_key_value_heads is None:
            self.num_key_value_heads = self.num_attention_heads
        if self.layer_types is None:
            self.layer_types = ["full_attention"] * self.num_hidden_layers
        super().__post_init__(**kwargs)

    def convert_rope_params_to_dict(self, **kwargs):
        rope_scaling = kwargs.pop("rope_scaling", None)
        rope_theta = kwargs.pop("rope_theta", self.default_theta)
        rope_parameters = rope_scaling or self.rope_parameters
        if rope_parameters is None:
            rope_parameters = {
                "full_attention": {
                    "rope_type": "yarn",
                    "factor": 16.0,
                    "original_max_position_embeddings": 2048,
                    "beta_fast": 32.0,
                    "beta_slow": 1.0,
                },
                "sliding_attention": {"rope_type": "default"},
            }
        elif set(rope_parameters).isdisjoint(ATTENTION_LAYER_TYPES):
            rope_parameters = dict.fromkeys(ATTENTION_LAYER_TYPES, rope_parameters)

        # Keep exactly the layer types in use: transformers recognizes per-layer-type parameters by their keys.
        self.rope_parameters = {}
        for layer_type in sorted(set(self.layer_types)):
            layer_rope_parameters = dict(rope_parameters.get(layer_type) or {"rope_type": "default"})
            layer_rope_parameters.setdefault("rope_theta", rope_theta)
            self.rope_parameters[layer_type] = layer_rope_parameters
        self.standardize_rope_params()
        return kwargs

    def validate_architecture(self):
        """Part of `@strict`-powered validation. Validates the architecture of the config."""
        super().validate_architecture()
        if "sliding_attention" in self.layer_types and self.sliding_window is None:
            raise ValueError("`sliding_window` must be set when `layer_types` contains sliding-attention layers.")
        if self.num_attention_heads % self.num_key_value_heads != 0:
            raise ValueError(
                f"`num_attention_heads` ({self.num_attention_heads}) must be a multiple of `num_key_value_heads` "
                f"({self.num_key_value_heads})."
            )

    def validate_layer_type(self):
        """Part of `@strict`-powered validation. Restricts `layer_types` to `ATTENTION_LAYER_TYPES`."""
        super().validate_layer_type()
        if unsupported := set(self.layer_types) - set(ATTENTION_LAYER_TYPES):
            raise ValueError(f"`layer_types` entries must be in {ATTENTION_LAYER_TYPES}, got {sorted(unsupported)}.")


__all__ = ["AionInstructConfig"]
