# coding=utf-8
# Configuration class for Kanana-2 PD-series (Qwen3 architecture with
# sliding/full alternating attention and per-attention-type RoPE).
#
# Difference vs Qwen3:
#   * `rope_parameters` is a dict keyed by attention type (`full_attention` /
#     `sliding_attention`).  Each entry is a self-contained RoPE config
#     understood by `transformers.modeling_rope_utils.ROPE_INIT_FUNCTIONS`.
#     This lets us apply YaRN to global-attention layers while keeping
#     unscaled RoPE for sliding-attention layers.
#   * Top-level `rope_scaling` is unused on this config; the modeling code
#     builds per-attention-type sub-configs at construction time and sets
#     `rope_scaling` on each sub-config so HF's standard rope init functions
#     (which read `config.rope_scaling`) work unchanged.

from transformers.configuration_utils import PretrainedConfig, layer_type_validation
from transformers.modeling_rope_utils import rope_config_validation
from transformers.utils import logging


logger = logging.get_logger(__name__)


class Kanana2TinyConfig(PretrainedConfig):
    """Configuration for the Kanana-2 PD-series (Qwen3 + per-type RoPE)."""

    model_type = "kanana2_tiny"
    keys_to_ignore_at_inference = ["past_key_values"]

    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.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"]),
    }

    def __init__(
        self,
        vocab_size=128256,
        hidden_size=1024,
        intermediate_size=4608,
        num_hidden_layers=32,
        num_attention_heads=32,
        num_key_value_heads=8,
        head_dim=128,
        hidden_act="silu",
        max_position_embeddings=35000,
        initializer_range=0.02,
        rms_norm_eps=1e-6,
        use_cache=True,
        tie_word_embeddings=True,
        rope_theta=10000.0,
        rope_parameters=None,
        rope_scaling=None,
        attention_bias=False,
        use_sliding_window=True,
        sliding_window=1024,
        max_window_layers=32,
        layer_types=None,
        attention_dropout=0.0,
        **kwargs,
    ):
        # Standard Qwen3-ish fields
        self.vocab_size = vocab_size
        self.max_position_embeddings = max_position_embeddings
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.use_sliding_window = use_sliding_window
        self.sliding_window = sliding_window if self.use_sliding_window else None
        self.max_window_layers = max_window_layers

        if num_key_value_heads is None:
            num_key_value_heads = num_attention_heads
        self.num_key_value_heads = num_key_value_heads
        self.head_dim = head_dim
        self.hidden_act = hidden_act
        self.initializer_range = initializer_range
        self.rms_norm_eps = rms_norm_eps
        self.use_cache = use_cache
        self.rope_theta = rope_theta
        self.attention_bias = attention_bias
        self.attention_dropout = attention_dropout
        # Kept for HF helpers that probe the attribute. The per-attention RoPE
        # config lives in `rope_parameters`; modeling code constructs sub-configs
        # whose `rope_scaling` is the per-type dict at init time.
        self.rope_scaling = rope_scaling

        # Per-attention-type RoPE.
        # Expected shape (defaults match the kanana-2-pd-series checkpoints):
        # {
        #   "full_attention":   {"rope_type": "yarn", "rope_theta": 10000,
        #                         "factor": 40.0, "original_max_position_embeddings": 4096},
        #   "sliding_attention": {"rope_type": "default", "rope_theta": 10000.0},
        # }
        if rope_parameters is None:
            rope_parameters = {
                "full_attention": {
                    "rope_type": "default",
                    "rope_theta": rope_theta,
                },
                "sliding_attention": {
                    "rope_type": "default",
                    "rope_theta": rope_theta,
                },
            }
        self.rope_parameters = rope_parameters

        for attn_type, params in self.rope_parameters.items():
            if not isinstance(params, dict) or "rope_type" not in params:
                raise ValueError(
                    f"rope_parameters[{attn_type!r}] must be a dict with a 'rope_type' key, got {params!r}"
                )

        # Set layer_types BEFORE per-type rope validation: the layer types must
        # exist for the validators that gate on layer_types.
        self.layer_types = layer_types
        if self.layer_types is None:
            self.layer_types = [
                "sliding_attention"
                if self.sliding_window is not None and i >= self.max_window_layers
                else "full_attention"
                for i in range(self.num_hidden_layers)
            ]
        layer_type_validation(self.layer_types, self.num_hidden_layers)

        # Per-attention-type rope validation. The 4.57.1 validators read off
        # `config.rope_scaling` (flat dict) and `config.rope_theta` (top-level),
        # so for each per-type sub-dict we present it in that shape, run the
        # validator, then restore. `rope_theta` is filtered out of the temporary
        # `rope_scaling` because in 4.57.1's schema it lives at the top level.
        for attn_type, params in self.rope_parameters.items():
            if attn_type not in set(self.layer_types):
                continue
            saved_rope_scaling = self.rope_scaling
            saved_rope_theta = self.rope_theta
            try:
                self.rope_scaling = {k: v for k, v in params.items() if k != "rope_theta"}
                self.rope_theta = params.get("rope_theta", saved_rope_theta)
                rope_config_validation(self)
            finally:
                self.rope_scaling = saved_rope_scaling
                self.rope_theta = saved_rope_theta

        super().__init__(
            tie_word_embeddings=tie_word_embeddings,
            **kwargs,
        )


__all__ = ["Kanana2TinyConfig"]
