File size: 6,853 Bytes
c6552d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# AetherMind — configuration_aethermind.py
# Copyright 2026 AetherMind Project. Apache License 2.0.
"""AetherMind model configuration.

AetherMind is a fully open, trainable family of decoder-only Transformer
language models with a modern architecture:

    * RMSNorm pre-normalization
    * Rotary Position Embeddings (RoPE, with optional linear scaling)
    * SwiGLU feed-forward blocks
    * Grouped-Query Attention (GQA)
    * Scaled-dot-product / Flash attention (via torch sdpa on CUDA)
    * Dynamic KV cache for fast generation
    * Optional Mixture-of-Experts (MoE) feed-forward for very large configs
    * Optional gradient checkpointing (enable via ``model.gradient_checkpointing_enable()``)

The same code path scales from the CPU-trainable ``tiny`` prototype up to
70B-class configs; every hyper-parameter lives here.
"""

from __future__ import annotations

from typing import Any, Dict, Optional

from transformers import PretrainedConfig


# ----------------------------------------------------------------------
# Named size presets (scaling path: tiny -> 70B)
# ----------------------------------------------------------------------
SIZE_PRESETS: Dict[str, Dict[str, Any]] = {
    # CPU-trainable prototype used to validate the full pipeline.
    "tiny": dict(
        hidden_size=256, intermediate_size=768, num_hidden_layers=6,
        num_attention_heads=8, num_key_value_heads=4, head_dim=32,
        max_position_embeddings=1024, tie_word_embeddings=True,
    ),
    "1B": dict(
        hidden_size=2048, intermediate_size=5632, num_hidden_layers=24,
        num_attention_heads=16, num_key_value_heads=4, head_dim=128,
        max_position_embeddings=32768, vocab_size=65536,
    ),
    "3B": dict(
        hidden_size=3200, intermediate_size=8704, num_hidden_layers=32,
        num_attention_heads=25, num_key_value_heads=5, head_dim=128,
        max_position_embeddings=32768, vocab_size=65536,
    ),
    "7B": dict(
        hidden_size=4096, intermediate_size=11008, num_hidden_layers=32,
        num_attention_heads=32, num_key_value_heads=8, head_dim=128,
        max_position_embeddings=32768, vocab_size=65536,
    ),
    "14B": dict(
        hidden_size=5120, intermediate_size=13824, num_hidden_layers=40,
        num_attention_heads=40, num_key_value_heads=8, head_dim=128,
        max_position_embeddings=32768, vocab_size=65536,
    ),
    # From here up, sparse MoE feed-forward keeps FLOPs per token bounded.
    "32B": dict(
        hidden_size=5120, intermediate_size=13824, num_hidden_layers=48,
        num_attention_heads=40, num_key_value_heads=8, head_dim=128,
        max_position_embeddings=32768, vocab_size=65536,
        use_moe=True, num_experts=64, num_experts_per_tok=6,
        moe_intermediate_size=1408, moe_layers_freq=1,
    ),
    "70B": dict(
        hidden_size=8192, intermediate_size=28672, num_hidden_layers=80,
        num_attention_heads=64, num_key_value_heads=8, head_dim=128,
        max_position_embeddings=32768, vocab_size=65536,
        use_moe=True, num_experts=128, num_experts_per_tok=8,
        moe_intermediate_size=2048, moe_layers_freq=1,
        rope_scaling={"type": "linear", "factor": 4.0},  # 32K -> 128K effective
    ),
}


class AetherMindConfig(PretrainedConfig):
    r"""Configuration class for :class:`~model.modeling_aethermind.AetherMindModel`."""

    model_type = "aethermind"
    keys_to_ignore_at_inference = ["past_key_values"]

    def __init__(
        self,
        vocab_size: int = 32768,
        hidden_size: int = 512,
        intermediate_size: int = 1376,
        num_hidden_layers: int = 8,
        num_attention_heads: int = 8,
        num_key_value_heads: int = 4,
        head_dim: Optional[int] = None,
        hidden_act: str = "silu",
        max_position_embeddings: int = 4096,
        initializer_range: float = 0.02,
        rms_norm_eps: float = 1e-6,
        use_cache: bool = True,
        pad_token_id: Optional[int] = None,
        bos_token_id: Optional[int] = None,
        eos_token_id: Optional[int] = None,
        tie_word_embeddings: bool = False,
        rope_theta: float = 10000.0,
        rope_scaling: Optional[Dict[str, Any]] = None,
        attention_bias: bool = False,
        attention_dropout: float = 0.0,
        # ---- Mixture of Experts (for large-scale configs) ----
        use_moe: bool = False,
        num_experts: int = 8,
        num_experts_per_tok: int = 2,
        moe_intermediate_size: Optional[int] = None,
        moe_layers_freq: int = 1,
        moe_aux_loss_coeff: float = 0.01,
        **kwargs,
    ) -> None:
        self.vocab_size = vocab_size
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        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 if head_dim is not None else hidden_size // num_attention_heads
        self.hidden_act = hidden_act
        self.max_position_embeddings = max_position_embeddings
        self.initializer_range = initializer_range
        self.rms_norm_eps = rms_norm_eps
        self.use_cache = use_cache
        self.rope_theta = rope_theta
        self.rope_scaling = rope_scaling
        if rope_scaling is not None and rope_scaling.get("type", rope_scaling.get("rope_type")) not in (None, "linear"):
            raise ValueError("AetherMind currently supports rope_scaling type='linear' (or None).")
        self.attention_bias = attention_bias
        self.attention_dropout = attention_dropout

        self.use_moe = use_moe
        self.num_experts = num_experts
        self.num_experts_per_tok = num_experts_per_tok
        self.moe_intermediate_size = moe_intermediate_size or intermediate_size
        self.moe_layers_freq = moe_layers_freq
        self.moe_aux_loss_coeff = moe_aux_loss_coeff

        super().__init__(
            pad_token_id=pad_token_id,
            bos_token_id=bos_token_id,
            eos_token_id=eos_token_id,
            tie_word_embeddings=tie_word_embeddings,
            **kwargs,
        )

    @classmethod
    def from_preset(
        cls,
        name: str = "tiny",
        vocab_size: Optional[int] = None,
        **overrides: Any,
    ) -> "AetherMindConfig":
        """Build a config from a named preset (tiny, 1B, 3B, 7B, 14B, 32B, 70B)."""
        if name not in SIZE_PRESETS:
            raise KeyError(f"Unknown preset '{name}'. Available: {sorted(SIZE_PRESETS)}")
        params: Dict[str, Any] = dict(SIZE_PRESETS[name])
        if vocab_size is not None:
            params["vocab_size"] = vocab_size
        params.update(overrides)
        return cls(**params)


__all__ = ["AetherMindConfig", "SIZE_PRESETS"]