File size: 11,928 Bytes
a489d73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
PyTorch Hugging Face implementation of SAM-AI Frontier Foundation Model.
Compatible with Hugging Face transformers AutoModelForCausalLM via trust_remote_code=True.
"""

from __future__ import annotations
import math
from typing import Dict, List, Optional, Tuple, Union, Any
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers.modeling_utils import PreTrainedModel
from transformers.modeling_outputs import CausalLMOutputWithPast

try:
    from .configuration_sam import SAMConfig
except (ImportError, ValueError):
    from configuration_sam import SAMConfig


# ==============================================================================
# 1. Rotary Position Embeddings (RoPE)
# ==============================================================================

class RotaryEmbedding(nn.Module):
    def __init__(self, dim: int, max_seq_len: int = 32768, base: float = 10000.0):
        super().__init__()
        self.dim = dim
        self.max_seq_len = max_seq_len
        self.base = base
        inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
        self.register_buffer("inv_freq", inv_freq, persistent=False)
        self._build_cache(max_seq_len)

    def _build_cache(self, seq_len: int):
        t = torch.arange(seq_len, dtype=torch.float32, device=self.inv_freq.device)
        freqs = torch.outer(t, self.inv_freq)
        emb = torch.cat((freqs, freqs), dim=-1)
        self.register_buffer("cos_cached", emb.cos(), persistent=False)
        self.register_buffer("sin_cached", emb.sin(), persistent=False)

    def forward(self, seq_len: int) -> Tuple[torch.Tensor, torch.Tensor]:
        if seq_len > self.cos_cached.shape[0]:
            self._build_cache(seq_len)
        return self.cos_cached[:seq_len], self.sin_cached[:seq_len]


def rotate_half(x: torch.Tensor) -> torch.Tensor:
    x1 = x[..., : x.shape[-1] // 2]
    x2 = x[..., x.shape[-1] // 2 :]
    return torch.cat((-x2, x1), dim=-1)


def apply_rotary_pos_emb(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
    while cos.dim() < x.dim():
        cos = cos.unsqueeze(0)
        sin = sin.unsqueeze(0)
    return (x * cos) + (rotate_half(x) * sin)


# ==============================================================================
# 2. RMSNorm & SwiGLU
# ==============================================================================

class RMSNorm(nn.Module):
    def __init__(self, dim: int, eps: float = 1e-6):
        super().__init__()
        self.eps = eps
        self.weight = nn.Parameter(torch.ones(dim))

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        variance = x.pow(2).mean(-1, keepdim=True)
        return self.weight * (x * torch.rsqrt(variance + self.eps))


class SwiGLU(nn.Module):
    def __init__(self, d_model: int, d_ff: int):
        super().__init__()
        self.w_gate = nn.Linear(d_model, d_ff, bias=False)
        self.w_up = nn.Linear(d_model, d_ff, bias=False)
        self.w_down = nn.Linear(d_ff, d_model, bias=False)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.w_down(F.silu(self.w_gate(x)) * self.w_up(x))


# ==============================================================================
# 3. Sliding Window Attention (SWA) & Multi-Head Latent Attention (MLA)
# ==============================================================================

class SlidingWindowAttention(nn.Module):
    def __init__(self, config: SAMConfig):
        super().__init__()
        self.d_model = config.d_model
        self.n_heads = config.n_heads
        self.n_kv_heads = config.n_kv_heads
        self.head_dim = config.head_dim
        self.window_size = config.window_size
        self.num_queries_per_kv = self.n_heads // self.n_kv_heads
        self.scale = 1.0 / math.sqrt(self.head_dim)

        self.q_proj = nn.Linear(config.d_model, config.n_heads * self.head_dim, bias=False)
        self.k_proj = nn.Linear(config.d_model, self.n_kv_heads * self.head_dim, bias=False)
        self.v_proj = nn.Linear(config.d_model, self.n_kv_heads * self.head_dim, bias=False)
        self.o_proj = nn.Linear(config.n_heads * self.head_dim, config.d_model, bias=False)
        self.rope = RotaryEmbedding(self.head_dim, max_seq_len=config.max_seq_len)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        B, T, D = x.shape
        q = self.q_proj(x).view(B, T, self.n_heads, self.head_dim).transpose(1, 2)
        k = self.k_proj(x).view(B, T, self.n_kv_heads, self.head_dim).transpose(1, 2)
        v = self.v_proj(x).view(B, T, self.n_kv_heads, self.head_dim).transpose(1, 2)

        cos, sin = self.rope(T)
        q = apply_rotary_pos_emb(q, cos, sin)
        k = apply_rotary_pos_emb(k, cos, sin)

        if self.num_queries_per_kv > 1:
            k = k.repeat_interleave(self.num_queries_per_kv, dim=1)
            v = v.repeat_interleave(self.num_queries_per_kv, dim=1)

        scores = torch.matmul(q, k.transpose(-2, -1)) * self.scale

        row = torch.arange(T, device=x.device).unsqueeze(1)
        col = torch.arange(T, device=x.device).unsqueeze(0)
        diff = row - col
        valid = (diff >= 0) & (diff < self.window_size)
        mask = torch.full((T, T), float("-inf"), device=x.device)
        mask[valid] = 0.0

        scores = scores + mask.unsqueeze(0).unsqueeze(0)
        probs = F.softmax(scores, dim=-1)
        out = torch.matmul(probs, v).transpose(1, 2).contiguous().view(B, T, -1)
        return self.o_proj(out)


class MultiHeadLatentAttention(nn.Module):
    def __init__(self, config: SAMConfig):
        super().__init__()
        self.d_model = config.d_model
        self.n_heads = config.n_heads
        self.d_latent_kv = config.d_latent_kv
        self.head_dim = config.head_dim
        self.rope_dim = config.mla_rope_dim
        self.scale = 1.0 / math.sqrt(self.head_dim + self.rope_dim)

        self.w_q = nn.Linear(config.d_model, config.n_heads * self.head_dim, bias=False)
        self.w_qr = nn.Linear(config.d_model, config.n_heads * self.rope_dim, bias=False)
        self.w_dkv = nn.Linear(config.d_model, config.d_latent_kv, bias=False)
        self.kv_norm = RMSNorm(config.d_latent_kv, eps=config.rms_norm_eps)
        self.w_uk = nn.Linear(config.d_latent_kv, config.n_heads * self.head_dim, bias=False)
        self.w_uv = nn.Linear(config.d_latent_kv, config.n_heads * self.head_dim, bias=False)
        self.w_kr = nn.Linear(config.d_model, self.rope_dim, bias=False)
        self.rope = RotaryEmbedding(self.rope_dim, max_seq_len=config.max_seq_len)
        self.w_o = nn.Linear(config.n_heads * self.head_dim, config.d_model, bias=False)

    def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
        B, T, D = x.shape
        q_c = self.w_q(x).view(B, T, self.n_heads, self.head_dim).transpose(1, 2)
        q_r = self.w_qr(x).view(B, T, self.n_heads, self.rope_dim).transpose(1, 2)

        cos, sin = self.rope(T)
        q_r = apply_rotary_pos_emb(q_r, cos, sin)
        k_r = self.w_kr(x).view(B, T, 1, self.rope_dim).transpose(1, 2)
        k_r = apply_rotary_pos_emb(k_r, cos, sin).expand(B, self.n_heads, T, self.rope_dim)

        c_kv = self.kv_norm(self.w_dkv(x))
        k_c = self.w_uk(c_kv).view(B, T, self.n_heads, self.head_dim).transpose(1, 2)
        v = self.w_uv(c_kv).view(B, T, self.n_heads, self.head_dim).transpose(1, 2)

        q_full = torch.cat([q_c, q_r], dim=-1)
        k_full = torch.cat([k_c, k_r], dim=-1)

        scores = torch.matmul(q_full, k_full.transpose(-2, -1)) * self.scale
        causal_mask = torch.triu(torch.full((T, T), float("-inf"), device=x.device), diagonal=1)
        scores = scores + causal_mask.unsqueeze(0).unsqueeze(0)

        probs = F.softmax(scores, dim=-1)
        out = torch.matmul(probs, v).transpose(1, 2).contiguous().view(B, T, -1)
        return self.w_o(out), c_kv


# ==============================================================================
# 4. Decoder Block & Multi-Token Prediction
# ==============================================================================

class SAMDecoderLayer(nn.Module):
    def __init__(self, config: SAMConfig, layer_idx: int):
        super().__init__()
        self.attn_norm = RMSNorm(config.d_model, eps=config.rms_norm_eps)
        self.ffn_norm = RMSNorm(config.d_model, eps=config.rms_norm_eps)

        if config.attention_type == "mla":
            self.attention = MultiHeadLatentAttention(config)
        elif config.attention_type == "swa":
            self.attention = SlidingWindowAttention(config)
        else:
            self.attention = SlidingWindowAttention(config) if layer_idx % 2 == 0 else MultiHeadLatentAttention(config)

        self.feed_forward = SwiGLU(config.d_model, config.d_ff)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        norm_x = self.attn_norm(x)
        if isinstance(self.attention, MultiHeadLatentAttention):
            attn_out, _ = self.attention(norm_x)
        else:
            attn_out = self.attention(norm_x)
        x = x + attn_out
        x = x + self.feed_forward(self.ffn_norm(x))
        return x


class SAMPreTrainedModel(PreTrainedModel):
    config_class = SAMConfig
    base_model_prefix = "model"
    supports_gradient_checkpointing = True
    _no_split_modules = ["SAMDecoderLayer"]

    def _init_weights(self, module: nn.Module):
        if isinstance(module, nn.Linear):
            nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
            if module.bias is not None:
                nn.init.zeros_(module.bias)
        elif isinstance(module, nn.Embedding):
            nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)


class SAMModel(SAMPreTrainedModel):
    def __init__(self, config: SAMConfig):
        super().__init__(config)
        self.embed_tokens = nn.Embedding(config.vocab_size, config.d_model)
        self.layers = nn.ModuleList([SAMDecoderLayer(config, i) for i in range(config.n_layers)])
        self.norm = RMSNorm(config.d_model, eps=config.rms_norm_eps)
        self.post_init()

    def forward(self, input_ids: torch.Tensor) -> torch.Tensor:
        h = self.embed_tokens(input_ids)
        for layer in self.layers:
            h = layer(h)
        return self.norm(h)


class SAMForCausalLM(SAMPreTrainedModel):
    def __init__(self, config: SAMConfig):
        super().__init__(config)
        self.model = SAMModel(config)
        self.lm_head = nn.Linear(config.d_model, config.vocab_size, bias=False)
        if config.tie_word_embeddings:
            self.lm_head.weight = self.model.embed_tokens.weight
        self.post_init()

    def get_input_embeddings(self):
        return self.model.embed_tokens

    def set_input_embeddings(self, value):
        self.model.embed_tokens = value

    def get_output_embeddings(self):
        return self.lm_head

    def set_output_embeddings(self, new_embeddings):
        self.lm_head = new_embeddings

    def forward(
        self,
        input_ids: Optional[torch.LongTensor] = None,
        labels: Optional[torch.LongTensor] = None,
        return_dict: Optional[bool] = None,
        **kwargs,
    ) -> Union[Tuple, CausalLMOutputWithPast]:
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        hidden_states = self.model(input_ids)
        logits = self.lm_head(hidden_states)

        loss = None
        if labels is not None:
            loss = F.cross_entropy(
                logits.reshape(-1, self.config.vocab_size),
                labels.reshape(-1),
                ignore_index=-100,
            )

        if not return_dict:
            output = (logits,)
            return ((loss,) + output) if loss is not None else output

        return CausalLMOutputWithPast(
            loss=loss,
            logits=logits,
            hidden_states=None,
            attentions=None,
        )