File size: 9,546 Bytes
a317604
d2f646e
331719a
5931924
a317604
d2f646e
 
 
 
 
 
 
 
 
a317604
d2f646e
a317604
 
d2f646e
 
 
a317604
d2f646e
 
 
a317604
d2f646e
 
 
 
 
a317604
d2f646e
a317604
 
 
 
d2f646e
a317604
 
 
 
 
 
 
d2f646e
 
 
a317604
d2f646e
 
a317604
d2f646e
 
 
a317604
d2f646e
 
 
 
a317604
d2f646e
 
a317604
d2f646e
 
a317604
 
d2f646e
a317604
 
d2f646e
 
 
 
 
 
 
 
a317604
 
d2f646e
a317604
d2f646e
a317604
d2f646e
 
a317604
d2f646e
 
 
a317604
 
 
 
d2f646e
a317604
 
d2f646e
 
 
a317604
 
d2f646e
a317604
d2f646e
 
 
 
 
 
a317604
d2f646e
a317604
d2f646e
 
 
a317604
 
d2f646e
 
a317604
 
d2f646e
a317604
d2f646e
a317604
d2f646e
 
 
a317604
d2f646e
 
 
 
a317604
 
 
 
 
d2f646e
 
 
a317604
d2f646e
 
 
 
a317604
d2f646e
a317604
d2f646e
a317604
d2f646e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a317604
 
d2f646e
 
 
a317604
 
 
d2f646e
 
 
 
 
 
a317604
d2f646e
 
 
 
a317604
d2f646e
 
 
a317604
d2f646e
 
a317604
 
d2f646e
 
 
 
 
 
a317604
d2f646e
 
 
a317604
d2f646e
a317604
d2f646e
a317604
 
 
d2f646e
a317604
 
d2f646e
a317604
d2f646e
a317604
 
d2f646e
 
 
 
 
a317604
 
 
 
d2f646e
a317604
d2f646e
a317604
d2f646e
a317604
 
 
 
d2f646e
 
 
 
 
 
 
 
 
 
 
 
a317604
 
d2f646e
a317604
 
d2f646e
a317604
d2f646e
a317604
d2f646e
 
a317604
d2f646e
 
 
 
 
a317604
 
d2f646e
a317604
d2f646e
a317604
 
 
d2f646e
 
 
 
 
 
 
a317604
 
 
d2f646e
 
a317604
d2f646e
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# coding=utf-8
# Copyright 2025 Arsh Team
# MIT License

import torch
import torch.nn as nn
from transformers import (
    PreTrainedModel,
    GenerationMixin,
    Cache,
    DynamicCache,
    StaticCache,
    FlashAttentionKwargs,
    ArshConfig
)
from typing import Optional, Tuple, Union, List


class ArshRMSNorm(nn.Module):
    """
    RMS Normalization layer customized for Arsh architecture

    Args:
        hidden_size (int): Dimension of hidden states
        eps (float): Epsilon value for numerical stability

    Example:
        >>> norm = ArshRMSNorm(768)
        >>> x = torch.randn(1, 10, 768)
        >>> output = norm(x)
    """

    def __init__(self, hidden_size: int, eps: float = 1e-6):
        super().__init__()
        self.weight = nn.Parameter(torch.ones(hidden_size))
        self.variance_epsilon = eps

    def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
        input_dtype = hidden_states.dtype
        hidden_states = hidden_states.to(torch.float32)
        variance = hidden_states.pow(2).mean(-1, keepdim=True)
        hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
        return self.weight * hidden_states.to(input_dtype)


class ArshRotaryEmbedding(nn.Module):
    """
    Rotary Position Embedding implementation for Arsh model

    Args:
        config (ArshConfig): Model configuration

    Attributes:
        max_seq_len_cached (int): Maximum cached sequence length
        attention_scaling (float): Scaling factor for attention

    Example:
        >>> config = ArshConfig()
        >>> rotary_emb = ArshRotaryEmbedding(config)
    """

    def __init__(self, config: ArshConfig):
        super().__init__()
        self.config = config
        self.max_seq_len_cached = config.max_position_embeddings
        self.rope_type = config.rope_scaling.get("type", "default")
        self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]

        inv_freq, self.attention_scaling = self.rope_init_fn(config)
        self.register_buffer("inv_freq", inv_freq, persistent=False)

    def _update_frequency(self, position_ids: torch.Tensor):
        """Dynamically update frequency based on input sequence length"""
        seq_len = position_ids.max() + 1
        if seq_len > self.max_seq_len_cached:
            inv_freq, self.attention_scaling = self.rope_init_fn(
                self.config, seq_len=seq_len
            )
            self.register_buffer("inv_freq", inv_freq)
            self.max_seq_len_cached = seq_len

    def forward(self, x: torch.Tensor, position_ids: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
        if "dynamic" in self.rope_type:
            self._update_frequency(position_ids)

        # Compute cosine and sine embeddings
        inv_freq_expanded = self.inv_freq[None, :, None]
        position_ids_expanded = position_ids[:, None, :].float()

        with torch.autocast(device_type=x.device.type, enabled=False):
            freqs = (inv_freq_expanded @ position_ids_expanded).transpose(1, 2)
            emb = torch.cat((freqs, freqs), dim=-1)
            cos = emb.cos()
            sin = emb.sin()

        return cos * self.attention_scaling, sin * self.attention_scaling


class ArshMLP(nn.Module):
    """
    Gated MLP Block for Arsh model

    Args:
        config (ArshConfig): Model configuration

    Example:
        >>> config = ArshConfig()
        >>> mlp = ArshMLP(config)
        >>> x = torch.randn(1, 10, 768)
        >>> output = mlp(x)
    """

    def __init__(self, config: ArshConfig):
        super().__init__()
        self.gate_proj = nn.Linear(config.hidden_size, config.intermediate_size)
        self.up_proj = nn.Linear(config.hidden_size, config.intermediate_size)
        self.down_proj = nn.Linear(config.intermediate_size, config.hidden_size)
        self.act_fn = ACT2FN[config.hidden_act]

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))


class ArshAttention(nn.Module):
    """
    Multi-head Attention layer with RoPE support

    Args:
        config (ArshConfig): Model configuration
        layer_idx (int): Layer index

    Example:
        >>> config = ArshConfig()
        >>> attn = ArshAttention(config, layer_idx=0)
    """

    def __init__(self, config: ArshConfig, layer_idx: int):
        super().__init__()
        self.config = config
        self.layer_idx = layer_idx
        self.hidden_size = config.hidden_size
        self.num_heads = config.num_attention_heads
        self.head_dim = self.hidden_size // self.num_heads

        self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim)
        self.k_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim)
        self.v_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim)
        self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size)

        self.rotary_emb = ArshRotaryEmbedding(config)

    def forward(
            self,
            hidden_states: torch.Tensor,
            attention_mask: Optional[torch.Tensor] = None,
            position_ids: Optional[torch.Tensor] = None,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        batch_size, seq_len, _ = hidden_states.shape

        # Project inputs
        q = self.q_proj(hidden_states).view(batch_size, seq_len, self.num_heads, self.head_dim)
        k = self.k_proj(hidden_states).view(batch_size, seq_len, self.num_heads, self.head_dim)
        v = self.v_proj(hidden_states).view(batch_size, seq_len, self.num_heads, self.head_dim)

        # Apply rotary embeddings
        cos, sin = self.rotary_emb(hidden_states, position_ids)
        q, k = apply_rotary_pos_emb(q, k, cos, sin)

        # Attention computation
        attn_output, attn_weights = scaled_dot_product_attention(
            q, k, v, attention_mask=attention_mask
        )

        # Output projection
        output = self.o_proj(attn_output.view(batch_size, seq_len, -1))
        return output, attn_weights


class ArshDecoderLayer(nn.Module):
    """
    Transformer Decoder Layer

    Args:
        config (ArshConfig): Model configuration
        layer_idx (int): Layer index

    Example:
        >>> config = ArshConfig()
        >>> layer = ArshDecoderLayer(config, layer_idx=0)
    """

    def __init__(self, config: ArshConfig, layer_idx: int):
        super().__init__()
        self.self_attn = ArshAttention(config, layer_idx)
        self.mlp = ArshMLP(config)
        self.input_norm = ArshRMSNorm(config.hidden_size)
        self.post_attn_norm = ArshRMSNorm(config.hidden_size)

    def forward(
            self,
            hidden_states: torch.Tensor,
            attention_mask: Optional[torch.Tensor] = None,
            position_ids: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        # Self-attention block
        residual = hidden_states
        hidden_states = self.input_norm(hidden_states)
        attn_output, _ = self.self_attn(hidden_states, attention_mask, position_ids)
        hidden_states = residual + attn_output

        # MLP block
        residual = hidden_states
        hidden_states = self.post_attn_norm(hidden_states)
        hidden_states = self.mlp(hidden_states)
        hidden_states = residual + hidden_states

        return hidden_states


class ArshModel(PreTrainedModel):
    """
    Main Arsh model architecture

    Args:
        config (ArshConfig): Model configuration

    Example:
        >>> config = ArshConfig()
        >>> model = ArshModel(config)
    """

    def __init__(self, config: ArshConfig):
        super().__init__(config)
        self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
        self.layers = nn.ModuleList(
            [ArshDecoderLayer(config, i) for i in range(config.num_hidden_layers)]
        )
        self.norm = ArshRMSNorm(config.hidden_size)

        self.post_init()

    def forward(
            self,
            input_ids: torch.Tensor,
            attention_mask: Optional[torch.Tensor] = None,
            position_ids: Optional[torch.Tensor] = None,
    ) -> torch.Tensor:
        hidden_states = self.embed_tokens(input_ids)

        for layer in self.layers:
            hidden_states = layer(
                hidden_states,
                attention_mask=attention_mask,
                position_ids=position_ids
            )

        return self.norm(hidden_states)


class ArshForCausalLM(ArshModel, GenerationMixin):
    """
    Arsh model for causal language modeling

    Args:
        config (ArshConfig): Model configuration

    Example:
        >>> config = ArshConfig()
        >>> model = ArshForCausalLM(config)
        >>> inputs = {"input_ids": torch.randint(0, 100, (1, 10))}
        >>> outputs = model(**inputs)
    """

    def __init__(self, config: ArshConfig):
        super().__init__(config)
        self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
        self.post_init()

    def forward(
            self,
            input_ids: torch.Tensor,
            attention_mask: Optional[torch.Tensor] = None,
            labels: Optional[torch.Tensor] = None,
    ) -> dict:
        hidden_states = super().forward(input_ids, attention_mask)
        logits = self.lm_head(hidden_states)

        loss = None
        if labels is not None:
            loss_fct = nn.CrossEntropyLoss()
            loss = loss_fct(logits.view(-1, self.config.vocab_size), labels.view(-1))

        return {"loss": loss, "logits": logits}