#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
#           This file was automatically generated from src/transformers/models/beam/modular_beam.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_beam.py file directly. One of our CI enforces this.
#                🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
# Copyright 2026 Reflection AI and the HuggingFace Inc. 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="reflection/Beam-1.0-501B-A23B")
@strict
class BeamConfig(PreTrainedConfig):
    r"""
    mlp_layer_types (`list[str]`, *optional*):
        Per-layer MLP type, `"dense"` for a SwiGLU MLP or `"sparse"` for the MoE block. Length must
        equal `num_hidden_layers`. Defaults to three dense layers followed by sparse ones.
    n_group (`int`, *optional*, defaults to 8):
        Number of groups for routed experts.

    ```python
    >>> from transformers import BeamConfig

    >>> # Initializing a Beam configuration
    >>> configuration = BeamConfig()
    ```
    """

    model_type = "beam"
    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.q_norm": "replicated_with_grad_allreduce",
        "layers.*.self_attn.k_norm": "replicated_with_grad_allreduce",
        "layers.*.self_attn.gate_proj": "colwise",
        "layers.*.self_attn.gate_bias": "colwise",
        "layers.*.self_attn.o_proj": "rowwise",
        "layers.*.self_attn": "local_params",
        "layers.*.mlp.experts.gate_up_proj": "packed_colwise",
        "layers.*.mlp.experts.down_proj": "rowwise",
        "layers.*.mlp.experts": "moe_tp_experts",
        "layers.*.mlp.gate_proj": "colwise",
        "layers.*.mlp.up_proj": "colwise",
        "layers.*.mlp.down_proj": "rowwise",
    }
    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",
    }
    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 = 151936

    hidden_size: int = 5120
    intermediate_size: int = 20480
    num_hidden_layers: int = 52
    num_attention_heads: int = 48
    num_key_value_heads: int = 8
    hidden_act: str = "silu"
    max_position_embeddings: int = 8192
    initializer_range: float = 0.02
    rms_norm_eps: float = 1e-6
    use_cache: bool = True
    tie_word_embeddings: bool = False
    rope_parameters: RopeParameters | dict | None = None
    attention_bias: bool = False
    sliding_window: int | None = 512
    attention_dropout: float | int = 0.0
    moe_intermediate_size: int = 2560
    num_experts_per_tok: int = 8
    num_experts: int = 256
    norm_topk_prob: bool = True
    output_router_logits: bool = False
    pad_token_id: int | None = None
    bos_token_id: int | None = None
    eos_token_id: int | list[int] | None = None
    head_dim: int = 128

    layer_types: list[str] | None = None
    mlp_layer_types: list[str] | None = None

    n_group: int | None = 8
    topk_group: int | None = 4
    routed_scaling_factor: int | float = 2.5

    final_logit_softcapping: int | float | None = 30.0

    def __post_init__(self, **kwargs):
        if self.layer_types is None:
            # Every sixth layer runs full attention, and so does the last one.
            self.layer_types = [
                "sliding_attention" if bool((i + 1) % 6) else "full_attention" for i in range(self.num_hidden_layers)
            ]
            self.layer_types[-1] = "full_attention"
        if self.mlp_layer_types is None:
            num_dense_layers = min(3, self.num_hidden_layers)
            self.mlp_layer_types = ["dense"] * num_dense_layers + ["sparse"] * (
                self.num_hidden_layers - num_dense_layers
            )
        if self.rope_parameters is None:
            # Keyed by layer type; `None` marks a layer type that runs without positional encoding.
            self.rope_parameters = {
                "sliding_attention": {"rope_type": "default", "rope_theta": self.default_theta},
                "full_attention": None,
            }
        super().__post_init__(**kwargs)

    def convert_rope_params_to_dict(self, **kwargs):
        # The generic converter assumes a flat mapping and would add top-level RoPE keys.
        return kwargs

    def validate_architecture(self):
        if self.num_attention_heads % self.num_key_value_heads:
            raise ValueError(
                "num_attention_heads must be divisible by num_key_value_heads: "
                f"{self.num_attention_heads} % {self.num_key_value_heads} != 0"
            )


__all__ = ["BeamConfig"]
