File size: 8,918 Bytes
eca5751
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Nexus Coder Model - Model AI MoE chính
========================================
Model: Nexus Coder v0.1
Tác giả: Hieu Louis (2026)

Đặc điểm:
- 10 tỷ tham số tổng (10B total)
- 1.5 tỷ tham số kích hoạt (1.5B active per token)
- Context window: 50,000 tokens
- Kiến trúc: MoE Transformer với 24 experts, 3 active
- RoPE position embedding
- RMSNorm (pre-norm)
- SwiGLU activation
- GQA (Grouped Query Attention)
"""
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Optional, Tuple, Dict, List, Union

from ..config import NexusConfig
from .layers import RMSNorm
from .transformer import NexusDecoderLayer
from .moe import load_balancing_loss_func
from .sliding_window import get_layer_attention_pattern


class NexusCoder(nn.Module):
    """Base Nexus Coder model - trả về hidden states."""

    def __init__(self, config: NexusConfig):
        super().__init__()
        self.config = config

        # Token embeddings
        self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)

        # Per-layer attention pattern: alternating SWA / global
        layer_patterns = get_layer_attention_pattern(
            num_layers=config.num_hidden_layers,
            use_sliding_window=config.use_sliding_window,
            sliding_window_layers=config.sliding_window_layers,
        )

        # Decoder layers
        self.layers = nn.ModuleList([
            NexusDecoderLayer(
                config,
                layer_idx=i,
                attention_pattern=layer_patterns[i],
            )
            for i in range(config.num_hidden_layers)
        ])

        # Final norm
        self.norm = RMSNorm(config.hidden_size, eps=config.layer_norm_epsilon)

    def enable_gradient_checkpointing(self):
        """Enable gradient checkpointing on all layers."""
        for layer in self.layers:
            layer.gradient_checkpointing = True

    def disable_gradient_checkpointing(self):
        """Disable gradient checkpointing on all layers."""
        for layer in self.layers:
            layer.gradient_checkpointing = False

    def forward(
        self,
        input_ids: torch.Tensor,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.Tensor] = None,
        past_key_values: Optional[List[Tuple[torch.Tensor, torch.Tensor]]] = None,
        use_cache: bool = False,
    ) -> Tuple[torch.Tensor, Dict]:
        bsz, seq_len = input_ids.shape

        if position_ids is None:
            position_ids = torch.arange(seq_len, device=input_ids.device).unsqueeze(0).expand(bsz, -1)

        # Embedding
        hidden_states = self.embed_tokens(input_ids)

        # Prepare attention mask (causal)
        if attention_mask is None:
            # Default causal mask
            attn_mask = torch.triu(
                torch.full((seq_len, seq_len), float("-inf"), device=hidden_states.device),
                diagonal=1,
            )
            attn_mask = attn_mask.unsqueeze(0).unsqueeze(0)
        else:
            attn_mask = self._prepare_attention_mask(attention_mask, seq_len)

        # Through layers
        all_aux_loss = torch.tensor(0.0, device=hidden_states.device)
        new_kv_list = []
        for i, layer in enumerate(self.layers):
            past_kv = past_key_values[i] if past_key_values is not None else None
            hidden_states, new_kv, aux_loss = layer(
                hidden_states,
                attention_mask=attn_mask,
                position_ids=position_ids,
                past_key_value=past_kv,
                use_cache=use_cache,
            )
            all_aux_loss = all_aux_loss + aux_loss
            new_kv_list.append(new_kv)

        # Final norm
        hidden_states = self.norm(hidden_states)

        outputs = {
            "last_hidden_state": hidden_states,
            "aux_loss": all_aux_loss / len(self.layers),
            "past_key_values": new_kv_list if use_cache else None,
        }
        return hidden_states, outputs

    def _prepare_attention_mask(self, attention_mask: torch.Tensor, seq_len: int) -> torch.Tensor:
        """Tạo attention mask 4D từ mask 2D."""
        # attention_mask: [B, seq_len] (1 = valid, 0 = padding)
        extended = attention_mask[:, None, None, :]
        extended = extended.to(dtype=torch.float32)
        extended = (1.0 - extended) * torch.finfo(torch.float32).min
        return extended


class NexusCoderForCausalLM(nn.Module):
    """Nexus Coder cho causal language modeling (next-token prediction)."""

    def __init__(self, config: NexusConfig):
        super().__init__()
        self.config = config
        self.model = NexusCoder(config)

        # LM head (không tie weights)
        self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)

    def forward(
        self,
        input_ids: torch.Tensor,
        attention_mask: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.Tensor] = None,
        past_key_values: Optional[List[Tuple[torch.Tensor, torch.Tensor]]] = None,
        labels: Optional[torch.Tensor] = None,
        use_cache: bool = False,
    ) -> Dict[str, torch.Tensor]:
        hidden_states, outputs = self.model(
            input_ids=input_ids,
            attention_mask=attention_mask,
            position_ids=position_ids,
            past_key_values=past_key_values,
            use_cache=use_cache,
        )

        # LM head
        logits = self.lm_head(hidden_states)

        loss = None
        if labels is not None:
            # Shift for next token prediction
            shift_logits = logits[..., :-1, :].contiguous()
            shift_labels = labels[..., 1:].contiguous()

            loss_fct = nn.CrossEntropyLoss()
            loss = loss_fct(
                shift_logits.view(-1, self.config.vocab_size),
                shift_labels.view(-1),
            )

            # Add aux loss
            loss = loss + self.config.router_aux_loss_coef * outputs["aux_loss"]

        return {
            "loss": loss,
            "logits": logits,
            "aux_loss": outputs["aux_loss"],
            "past_key_values": outputs["past_key_values"],
        }

    @torch.no_grad()
    def generate(
        self,
        input_ids: torch.Tensor,
        max_new_tokens: int = 100,
        temperature: float = 0.8,
        top_k: int = 50,
        top_p: float = 0.9,
        do_sample: bool = True,
        pad_token_id: int = 0,
        eos_token_id: int = 2,
    ) -> torch.Tensor:
        """Hàm generate đơn giản với top-k và top-p sampling."""
        self.eval()
        device = input_ids.device

        for _ in range(max_new_tokens):
            # Forward pass
            outputs = self.forward(
                input_ids=input_ids,
                use_cache=False,
            )
            logits = outputs["logits"]
            next_logits = logits[:, -1, :] / max(temperature, 1e-8)

            # Top-k
            if top_k > 0:
                top_k = min(top_k, next_logits.size(-1))
                values, _ = torch.topk(next_logits, top_k)
                min_values = values[:, -1].unsqueeze(-1)
                next_logits = torch.where(
                    next_logits < min_values,
                    torch.full_like(next_logits, float("-inf")),
                    next_logits,
                )

            # Top-p
            if 0 < top_p < 1.0:
                sorted_logits, sorted_indices = torch.sort(next_logits, descending=True)
                cum_probs = F.softmax(sorted_logits, dim=-1).cumsum(dim=-1)
                sorted_indices_to_remove = cum_probs > top_p
                sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
                sorted_indices_to_remove[..., 0] = False
                indices_to_remove = sorted_indices_to_remove.scatter(
                    1, sorted_indices, sorted_indices_to_remove
                )
                next_logits = next_logits.masked_fill(indices_to_remove, float("-inf"))

            # Sample
            if do_sample:
                probs = F.softmax(next_logits, dim=-1)
                next_token = torch.multinomial(probs, num_samples=1)
            else:
                next_token = torch.argmax(next_logits, dim=-1, keepdim=True)

            input_ids = torch.cat([input_ids, next_token], dim=-1)

            if next_token.item() == eos_token_id:
                break

        return input_ids

    def count_parameters(self) -> dict:
        """Đếm tham số."""
        total = sum(p.numel() for p in self.parameters())
        trainable = sum(p.numel() for p in self.parameters() if p.requires_grad)
        return {
            "total": total,
            "trainable": trainable,
            "total_billion": total / 1e9,
            "trainable_billion": trainable / 1e9,
        }