eyad-silx commited on
Commit
75b6d7f
·
verified ·
1 Parent(s): 9940f6c

Upload modeling_quasar.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. modeling_quasar.py +688 -0
modeling_quasar.py ADDED
@@ -0,0 +1,688 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Callable, Optional, Union
2
+
3
+ import torch
4
+ import torch.nn.functional as F
5
+ from torch import nn
6
+
7
+ from transformers.activations import ACT2FN
8
+ from transformers.cache_utils import Cache, DynamicCache
9
+ from transformers.generation import GenerationMixin
10
+ from transformers.integrations import use_kernel_forward_from_hub
11
+ from transformers.masking_utils import create_causal_mask, create_sliding_window_causal_mask
12
+ from transformers.modeling_flash_attention_utils import FlashAttentionKwargs
13
+ from transformers.modeling_layers import (
14
+ GenericForQuestionAnswering,
15
+ GenericForSequenceClassification,
16
+ GenericForTokenClassification,
17
+ GradientCheckpointingLayer,
18
+ )
19
+ from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update
20
+ from transformers.modeling_outputs import MoeCausalLMOutputWithPast, MoeModelOutputWithPast
21
+ from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel
22
+ from transformers.processing_utils import Unpack
23
+ from transformers.utils import TransformersKwargs, auto_docstring, can_return_tuple
24
+ from transformers.utils.generic import OutputRecorder, check_model_inputs
25
+ from .configuration_quasar import QuasarConfig
26
+
27
+
28
+ def rotate_half(x):
29
+ """Rotates half the hidden dims of the input."""
30
+ x1 = x[..., : x.shape[-1] // 2]
31
+ x2 = x[..., x.shape[-1] // 2 :]
32
+ return torch.cat((-x2, x1), dim=-1)
33
+
34
+
35
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
36
+ """Applies Rotary Position Embedding to the query and key tensors.
37
+ Args:
38
+ q (`torch.Tensor`): The query tensor.
39
+ k (`torch.Tensor`): The key tensor.
40
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
41
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
42
+ position_ids (`torch.Tensor`, *optional*):
43
+ Deprecated and unused.
44
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
45
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
46
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
47
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
48
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
49
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
50
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
51
+ Returns:
52
+ `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
53
+ """
54
+ cos = cos.unsqueeze(unsqueeze_dim)
55
+ sin = sin.unsqueeze(unsqueeze_dim)
56
+ q_embed = (q * cos) + (rotate_half(q) * sin)
57
+ k_embed = (k * cos) + (rotate_half(k) * sin)
58
+ return q_embed, k_embed
59
+
60
+
61
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
62
+ """
63
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
64
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
65
+ """
66
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
67
+ if n_rep == 1:
68
+ return hidden_states
69
+ hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
70
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
71
+
72
+
73
+ def eager_attention_forward(
74
+ module: nn.Module,
75
+ query: torch.Tensor,
76
+ key: torch.Tensor,
77
+ value: torch.Tensor,
78
+ attention_mask: Optional[torch.Tensor],
79
+ scaling: float,
80
+ dropout: float = 0.0,
81
+ **kwargs: Unpack[TransformersKwargs],
82
+ ):
83
+ key_states = repeat_kv(key, module.num_key_value_groups)
84
+ value_states = repeat_kv(value, module.num_key_value_groups)
85
+
86
+ attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling
87
+ if attention_mask is not None:
88
+ causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
89
+ attn_weights = attn_weights + causal_mask
90
+
91
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype)
92
+ attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training)
93
+ attn_output = torch.matmul(attn_weights, value_states)
94
+ attn_output = attn_output.transpose(1, 2).contiguous()
95
+
96
+ return attn_output, attn_weights
97
+
98
+
99
+ class QuasarAttention(nn.Module):
100
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
101
+
102
+ def __init__(self, config: QuasarConfig, layer_idx: int):
103
+ super().__init__()
104
+ self.config = config
105
+ self.layer_idx = layer_idx
106
+ self.head_dim = getattr(config, "head_dim", config.hidden_size // config.num_attention_heads)
107
+ self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads
108
+ self.scaling = self.head_dim**-0.5
109
+ self.attention_dropout = config.attention_dropout
110
+ self.is_causal = True
111
+
112
+ self.q_proj = nn.Linear(
113
+ config.hidden_size, config.num_attention_heads * self.head_dim, bias=config.attention_bias
114
+ )
115
+ self.k_proj = nn.Linear(
116
+ config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias
117
+ )
118
+ self.v_proj = nn.Linear(
119
+ config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias
120
+ )
121
+ self.o_proj = nn.Linear(
122
+ config.num_attention_heads * self.head_dim, config.hidden_size, bias=config.attention_bias
123
+ )
124
+ self.q_norm = QuasarRMSNorm(self.head_dim, eps=config.rms_norm_eps) # unlike olmo, only on the head dim!
125
+ self.k_norm = QuasarRMSNorm(self.head_dim, eps=config.rms_norm_eps) # thus post q_norm does not need reshape
126
+ self.sliding_window = getattr(config, "sliding_window", None)
127
+
128
+ def forward(
129
+ self,
130
+ hidden_states: torch.Tensor,
131
+ position_embeddings: tuple[torch.Tensor, torch.Tensor],
132
+ attention_mask: Optional[torch.Tensor],
133
+ past_key_value: Optional[Cache] = None,
134
+ cache_position: Optional[torch.LongTensor] = None,
135
+ **kwargs: Unpack[FlashAttentionKwargs],
136
+ ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]:
137
+ input_shape = hidden_states.shape[:-1]
138
+ hidden_shape = (*input_shape, -1, self.head_dim)
139
+
140
+ query_states = self.q_norm(self.q_proj(hidden_states).view(hidden_shape)).transpose(1, 2)
141
+ key_states = self.k_norm(self.k_proj(hidden_states).view(hidden_shape)).transpose(1, 2)
142
+ value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)
143
+
144
+ cos, sin = position_embeddings
145
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
146
+
147
+ if past_key_value is not None:
148
+ # sin and cos are specific to RoPE models; cache_position needed for the static cache
149
+ cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
150
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
151
+
152
+ attention_interface: Callable = eager_attention_forward
153
+ if self.config._attn_implementation != "eager":
154
+ attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]
155
+
156
+ attn_output, attn_weights = attention_interface(
157
+ self,
158
+ query_states,
159
+ key_states,
160
+ value_states,
161
+ attention_mask,
162
+ dropout=0.0 if not self.training else self.attention_dropout,
163
+ scaling=self.scaling,
164
+ sliding_window=self.sliding_window, # diff with Llama
165
+ **kwargs,
166
+ )
167
+
168
+ attn_output = attn_output.reshape(*input_shape, -1).contiguous()
169
+ attn_output = self.o_proj(attn_output)
170
+ return attn_output, attn_weights
171
+
172
+
173
+ class QuasarMLP(nn.Module):
174
+ def __init__(self, config, intermediate_size=None):
175
+ super().__init__()
176
+ self.config = config
177
+ self.hidden_size = config.hidden_size
178
+ self.intermediate_size = intermediate_size if intermediate_size is not None else config.intermediate_size
179
+ self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
180
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
181
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
182
+ self.act_fn = ACT2FN[config.hidden_act]
183
+
184
+ def forward(self, x):
185
+ down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
186
+ return down_proj
187
+
188
+
189
+ class QuasarSparseMoeBlock(nn.Module):
190
+ def __init__(self, config):
191
+ super().__init__()
192
+ self.config = config
193
+ self.num_experts = config.num_experts
194
+ self.top_k = config.num_experts_per_tok
195
+ self.norm_topk_prob = config.norm_topk_prob
196
+
197
+ # router
198
+ self.gate = nn.Linear(config.hidden_size, config.num_experts, bias=False)
199
+ self.experts = nn.ModuleList(
200
+ [QuasarMLP(config, intermediate_size=config.moe_intermediate_size) for _ in range(config.num_experts)]
201
+ )
202
+ self.shared_experts = QuasarMLP(
203
+ config=config, intermediate_size=config.moe_intermediate_size * config.n_shared_experts
204
+ )
205
+
206
+ self.coefficient = nn.Linear(config.hidden_size, 2)
207
+ self.register_buffer("expert_bias", torch.zeros(self.num_experts, dtype=torch.float32))
208
+
209
+ def forward(self, hidden_states):
210
+ residuals = hidden_states
211
+ batch_size, sequence_length, hidden_dim = hidden_states.shape
212
+ hidden_states = hidden_states.view(-1, hidden_dim)
213
+ # router_logits: (batch * sequence_length, n_experts)
214
+ router_logits = nn.functional.linear(hidden_states.to(torch.float32), self.gate.weight.to(torch.float32))
215
+
216
+ routing_weights = F.sigmoid(router_logits)
217
+ ori_routing_weights = routing_weights
218
+
219
+ # using bias
220
+ biasd_routing_weights = routing_weights + self.expert_bias.unsqueeze(0)
221
+ _, selected_experts = torch.topk(biasd_routing_weights, self.top_k, dim=-1)
222
+
223
+ # Extract corresponding original probabilities
224
+ ori_routing_weights = torch.gather(ori_routing_weights, dim=-1, index=selected_experts)
225
+
226
+ if self.norm_topk_prob: # only diff with mixtral sparse moe block!
227
+ ori_routing_weights /= ori_routing_weights.sum(dim=-1, keepdim=True)
228
+ # we cast back to the input dtype
229
+ ori_routing_weights = ori_routing_weights.to(hidden_states.dtype)
230
+
231
+ final_hidden_states = torch.zeros(
232
+ (batch_size * sequence_length, hidden_dim), dtype=hidden_states.dtype, device=hidden_states.device
233
+ )
234
+
235
+ # One hot encode the selected experts to create an expert mask
236
+ # this will be used to easily index which expert is going to be sollicitated
237
+ expert_mask = torch.nn.functional.one_hot(selected_experts, num_classes=self.num_experts).permute(2, 1, 0)
238
+
239
+ # Loop over all available experts in the model and perform the computation on each expert
240
+ expert_hitted = torch.greater(expert_mask.sum(dim=(-1, -2)), 0).nonzero()
241
+ for expert_idx in expert_hitted:
242
+ expert_layer = self.experts[expert_idx]
243
+ idx, top_x = torch.where(expert_mask[expert_idx].squeeze(0))
244
+
245
+ # Index the correct hidden states and compute the expert hidden state for
246
+ # the current expert. We need to make sure to multiply the output hidden
247
+ # states by `ori_routing_weights` on the corresponding tokens (top-1 and top-2)
248
+ current_state = hidden_states[None, top_x].reshape(-1, hidden_dim)
249
+ current_hidden_states = expert_layer(current_state) * ori_routing_weights[top_x, idx, None]
250
+
251
+ # However `index_add_` only support torch tensors for indexing so we'll use
252
+ # the `top_x` tensor here.
253
+ final_hidden_states.index_add_(0, top_x, current_hidden_states.to(hidden_states.dtype))
254
+ final_hidden_states = final_hidden_states.reshape(batch_size, sequence_length, hidden_dim)
255
+
256
+ coef = self.coefficient(residuals).softmax(dim=-1)
257
+ final_hidden_states = final_hidden_states * coef[..., :1] + self.shared_experts(residuals) * coef[..., 1:]
258
+
259
+ return final_hidden_states, router_logits
260
+
261
+
262
+ @use_kernel_forward_from_hub("RMSNorm")
263
+ class QuasarRMSNorm(nn.Module):
264
+ def __init__(self, hidden_size, eps=1e-6):
265
+ """
266
+ QuasarRMSNorm is equivalent to T5LayerNorm
267
+ """
268
+ super().__init__()
269
+ self.weight = nn.Parameter(torch.ones(hidden_size))
270
+ self.variance_epsilon = eps
271
+
272
+ def forward(self, hidden_states):
273
+ input_dtype = hidden_states.dtype
274
+ hidden_states = hidden_states.to(torch.float32)
275
+ variance = hidden_states.pow(2).mean(-1, keepdim=True)
276
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
277
+ return self.weight * hidden_states.to(input_dtype)
278
+
279
+ def extra_repr(self):
280
+ return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}"
281
+
282
+
283
+ class QuasarDecoderLayer(GradientCheckpointingLayer):
284
+ def __init__(self, config: QuasarConfig, layer_idx: int):
285
+ super().__init__()
286
+ self.hidden_size = config.hidden_size
287
+
288
+ self.self_attn = QuasarAttention(config, layer_idx)
289
+
290
+ if (layer_idx not in config.mlp_only_layers) and (
291
+ config.num_experts > 0 and (layer_idx + 1) % config.decoder_sparse_step == 0
292
+ ):
293
+ self.mlp = QuasarSparseMoeBlock(config)
294
+ else:
295
+ self.mlp = QuasarMLP(config, intermediate_size=config.intermediate_size)
296
+
297
+ self.input_layernorm = QuasarRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
298
+ self.post_attention_layernorm = QuasarRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
299
+
300
+ def forward(
301
+ self,
302
+ hidden_states: torch.Tensor,
303
+ attention_mask: Optional[torch.Tensor] = None,
304
+ position_ids: Optional[torch.LongTensor] = None,
305
+ past_key_value: Optional[tuple[torch.Tensor]] = None,
306
+ output_attentions: Optional[bool] = False,
307
+ output_router_logits: Optional[bool] = False,
308
+ use_cache: Optional[bool] = False,
309
+ cache_position: Optional[torch.LongTensor] = None,
310
+ position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC
311
+ **kwargs: Unpack[FlashAttentionKwargs],
312
+ ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]:
313
+ """
314
+ Args:
315
+ hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
316
+ attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
317
+ `(batch, sequence_length)` where padding elements are indicated by 0.
318
+ output_attentions (`bool`, *optional*):
319
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
320
+ returned tensors for more detail.
321
+ output_router_logits (`bool`, *optional*):
322
+ Whether or not to return the logits of all the routers. They are useful for computing the router loss,
323
+ and should not be returned during inference.
324
+ use_cache (`bool`, *optional*):
325
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
326
+ (see `past_key_values`).
327
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
328
+ cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
329
+ Indices depicting the position of the input sequence tokens in the sequence.
330
+ position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*):
331
+ Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`,
332
+ with `head_dim` being the embedding dimension of each attention head.
333
+ kwargs (`dict`, *optional*):
334
+ Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code
335
+ into the model
336
+ """
337
+
338
+ residual = hidden_states
339
+ hidden_states = self.input_layernorm(hidden_states)
340
+
341
+ # Self Attention
342
+ hidden_states, self_attn_weights = self.self_attn(
343
+ hidden_states=hidden_states,
344
+ attention_mask=attention_mask,
345
+ position_ids=position_ids,
346
+ past_key_value=past_key_value,
347
+ output_attentions=output_attentions,
348
+ use_cache=use_cache,
349
+ cache_position=cache_position,
350
+ position_embeddings=position_embeddings,
351
+ **kwargs,
352
+ )
353
+ hidden_states = residual + hidden_states
354
+
355
+ # Fully Connected
356
+ residual = hidden_states
357
+ hidden_states = self.post_attention_layernorm(hidden_states)
358
+
359
+ hidden_states = self.mlp(hidden_states)
360
+ if isinstance(hidden_states, tuple):
361
+ hidden_states, router_logits = hidden_states
362
+ else:
363
+ router_logits = None
364
+
365
+ hidden_states = residual + hidden_states
366
+
367
+ outputs = (hidden_states,)
368
+
369
+ if output_attentions:
370
+ outputs += (self_attn_weights,)
371
+
372
+ if output_router_logits:
373
+ outputs += (router_logits,)
374
+
375
+ return outputs
376
+
377
+
378
+ class QuasarRotaryEmbedding(nn.Module):
379
+ def __init__(self, config: QuasarConfig, device=None):
380
+ super().__init__()
381
+ # BC: "rope_type" was originally "type"
382
+ if hasattr(config, "rope_scaling") and isinstance(config.rope_scaling, dict):
383
+ self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type"))
384
+ else:
385
+ self.rope_type = "default"
386
+ self.max_seq_len_cached = config.max_position_embeddings
387
+ self.original_max_seq_len = config.max_position_embeddings
388
+
389
+ self.config = config
390
+ self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
391
+
392
+ inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device)
393
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
394
+ self.original_inv_freq = self.inv_freq
395
+
396
+ @torch.no_grad()
397
+ @dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)
398
+ def forward(self, x, position_ids):
399
+ inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1).to(x.device)
400
+ position_ids_expanded = position_ids[:, None, :].float()
401
+
402
+ device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu"
403
+ with torch.autocast(device_type=device_type, enabled=False): # Force float32
404
+ freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
405
+ emb = torch.cat((freqs, freqs), dim=-1)
406
+ cos = emb.cos() * self.attention_scaling
407
+ sin = emb.sin() * self.attention_scaling
408
+
409
+ return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
410
+
411
+
412
+ @auto_docstring
413
+ class QuasarPreTrainedModel(PreTrainedModel):
414
+ config: QuasarConfig
415
+ base_model_prefix = "model"
416
+ supports_gradient_checkpointing = True
417
+ _no_split_modules = ["QuasarDecoderLayer"]
418
+ _skip_keys_device_placement = ["past_key_values"]
419
+ _supports_flash_attn = True
420
+ _supports_sdpa = True
421
+ _supports_flex_attn = True
422
+ _can_compile_fullgraph = False # MoE models don't work with torch.compile (`torch.where(condition)` not supported)
423
+ _supports_attention_backend = True
424
+ _can_record_outputs = {
425
+ "router_logits": OutputRecorder(QuasarSparseMoeBlock, index=1),
426
+ "hidden_states": QuasarDecoderLayer,
427
+ "attentions": QuasarAttention,
428
+ }
429
+
430
+
431
+ @auto_docstring
432
+ class QuasarModel(QuasarPreTrainedModel):
433
+ def __init__(self, config: QuasarConfig):
434
+ super().__init__(config)
435
+ self.padding_idx = config.pad_token_id
436
+ self.vocab_size = config.vocab_size
437
+
438
+ self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
439
+ self.layers = nn.ModuleList(
440
+ [QuasarDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
441
+ )
442
+ self.norm = QuasarRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
443
+ self.rotary_emb = QuasarRotaryEmbedding(config=config)
444
+ self.gradient_checkpointing = False
445
+
446
+ # Initialize weights and apply final processing
447
+ self.post_init()
448
+
449
+ @check_model_inputs
450
+ @auto_docstring
451
+ def forward(
452
+ self,
453
+ input_ids: Optional[torch.LongTensor] = None,
454
+ attention_mask: Optional[torch.Tensor] = None,
455
+ position_ids: Optional[torch.LongTensor] = None,
456
+ past_key_values: Optional[list[torch.FloatTensor]] = None,
457
+ inputs_embeds: Optional[torch.FloatTensor] = None,
458
+ use_cache: Optional[bool] = None,
459
+ output_attentions: Optional[bool] = None,
460
+ output_hidden_states: Optional[bool] = None,
461
+ output_router_logits: Optional[bool] = None,
462
+ cache_position: Optional[torch.LongTensor] = None,
463
+ **kwargs: Unpack[TransformersKwargs],
464
+ ) -> MoeModelOutputWithPast:
465
+ if (input_ids is None) ^ (inputs_embeds is not None):
466
+ raise ValueError("You must specify exactly one of input_ids or inputs_embeds")
467
+
468
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
469
+ output_router_logits = (
470
+ output_router_logits if output_router_logits is not None else self.config.output_router_logits
471
+ )
472
+ output_hidden_states = (
473
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
474
+ )
475
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
476
+
477
+ if use_cache and past_key_values is None:
478
+ past_key_values = DynamicCache()
479
+
480
+ if inputs_embeds is None:
481
+ inputs_embeds = self.embed_tokens(input_ids)
482
+
483
+ if cache_position is None:
484
+ past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
485
+ cache_position = torch.arange(
486
+ past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
487
+ )
488
+ if position_ids is None:
489
+ position_ids = cache_position.unsqueeze(0)
490
+
491
+ mask_function = create_causal_mask if self.config.sliding_window is None else create_sliding_window_causal_mask
492
+ causal_mask = mask_function(
493
+ config=self.config,
494
+ input_embeds=inputs_embeds,
495
+ attention_mask=attention_mask,
496
+ cache_position=cache_position,
497
+ past_key_values=past_key_values,
498
+ position_ids=position_ids,
499
+ )
500
+
501
+ hidden_states = inputs_embeds
502
+
503
+ # create position embeddings to be shared across the decoder layers
504
+ position_embeddings = self.rotary_emb(hidden_states, position_ids)
505
+
506
+ # decoder layers
507
+ all_hidden_states = () if output_hidden_states else None
508
+ all_self_attns = () if output_attentions else None
509
+ all_router_logits = () if output_router_logits else None
510
+
511
+ for decoder_layer in self.layers:
512
+ if output_hidden_states:
513
+ all_hidden_states += (hidden_states,)
514
+
515
+ layer_outputs = decoder_layer(
516
+ hidden_states,
517
+ attention_mask=causal_mask,
518
+ position_ids=position_ids,
519
+ past_key_value=past_key_values,
520
+ output_attentions=output_attentions,
521
+ output_router_logits=output_router_logits,
522
+ use_cache=use_cache,
523
+ cache_position=cache_position,
524
+ position_embeddings=position_embeddings,
525
+ **kwargs,
526
+ )
527
+
528
+ hidden_states = layer_outputs[0]
529
+
530
+ if output_attentions:
531
+ all_self_attns += (layer_outputs[1],)
532
+
533
+ if output_router_logits:
534
+ all_router_logits += (layer_outputs[-1],)
535
+
536
+ hidden_states = self.norm(hidden_states)
537
+
538
+ return MoeModelOutputWithPast(
539
+ last_hidden_state=hidden_states,
540
+ past_key_values=past_key_values,
541
+ hidden_states=all_hidden_states,
542
+ attentions=all_self_attns,
543
+ router_logits=all_router_logits,
544
+ )
545
+
546
+
547
+ @auto_docstring
548
+ class QuasarMoeForCausalLM(QuasarPreTrainedModel, GenerationMixin):
549
+ _tied_weights_keys = ["lm_head.weight"]
550
+ _tp_plan = {"lm_head": "colwise_rep"}
551
+ _pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
552
+
553
+ def __init__(self, config):
554
+ super().__init__(config)
555
+ self.model = QuasarModel(config)
556
+ self.vocab_size = config.vocab_size
557
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
558
+ self.router_aux_loss_coef = config.router_aux_loss_coef
559
+ self.num_experts = config.num_experts
560
+ self.num_experts_per_tok = config.num_experts_per_tok
561
+ self.moe_aux_loss_coeff = getattr(config, "moe_aux_loss_coeff", 1.0)
562
+
563
+ # Initialize weights and apply final processing
564
+ self.post_init()
565
+
566
+ def set_decoder(self, decoder):
567
+ self.model = decoder
568
+
569
+ def get_decoder(self):
570
+ return self.model
571
+
572
+ @can_return_tuple
573
+ @auto_docstring
574
+ def forward(
575
+ self,
576
+ input_ids: Optional[torch.LongTensor] = None,
577
+ attention_mask: Optional[torch.Tensor] = None,
578
+ position_ids: Optional[torch.LongTensor] = None,
579
+ past_key_values: Optional[list[torch.FloatTensor]] = None,
580
+ inputs_embeds: Optional[torch.FloatTensor] = None,
581
+ labels: Optional[torch.LongTensor] = None,
582
+ use_cache: Optional[bool] = None,
583
+ output_attentions: Optional[bool] = None,
584
+ output_hidden_states: Optional[bool] = None,
585
+ output_router_logits: Optional[bool] = None,
586
+ cache_position: Optional[torch.LongTensor] = None,
587
+ logits_to_keep: Union[int, torch.Tensor] = 0,
588
+ **kwargs: Unpack[TransformersKwargs],
589
+ ) -> MoeCausalLMOutputWithPast:
590
+ r"""
591
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
592
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
593
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
594
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
595
+ Example:
596
+ ```python
597
+ >>> from transformers import AutoTokenizer, QuasarMoeForCausalLM
598
+ >>> model = QuasarMoeForCausalLM.from_pretrained("Quasar-kwaii/Quasar-MoE")
599
+ >>> tokenizer = AutoTokenizer.from_pretrained("Quasar-kwaii/Quasar-MoE")
600
+ >>> prompt = "Hey, are you conscious? Can you talk to me?"
601
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
602
+ >>> # Generate
603
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
604
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
605
+ "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
606
+ ```"""
607
+
608
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
609
+ output_router_logits = (
610
+ output_router_logits if output_router_logits is not None else self.config.output_router_logits
611
+ )
612
+
613
+ output_hidden_states = (
614
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
615
+ )
616
+
617
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
618
+ outputs: MoeModelOutputWithPast = self.model(
619
+ input_ids=input_ids,
620
+ attention_mask=attention_mask,
621
+ position_ids=position_ids,
622
+ past_key_values=past_key_values,
623
+ inputs_embeds=inputs_embeds,
624
+ use_cache=use_cache,
625
+ output_attentions=output_attentions,
626
+ output_hidden_states=output_hidden_states,
627
+ output_router_logits=output_router_logits,
628
+ cache_position=cache_position,
629
+ **kwargs,
630
+ )
631
+
632
+ hidden_states = outputs.last_hidden_state
633
+ # Only compute necessary logits, and do not upcast them to float if we are not computing the loss
634
+ slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
635
+ logits = self.lm_head(hidden_states[:, slice_indices, :])
636
+
637
+ loss = None
638
+ if labels is not None:
639
+ loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)
640
+
641
+ aux_loss = None
642
+ if output_router_logits:
643
+ pass
644
+
645
+ return MoeCausalLMOutputWithPast(
646
+ loss=loss,
647
+ aux_loss=aux_loss,
648
+ logits=logits,
649
+ past_key_values=outputs.past_key_values,
650
+ hidden_states=outputs.hidden_states,
651
+ attentions=outputs.attentions,
652
+ router_logits=outputs.router_logits,
653
+ )
654
+
655
+
656
+ class QuasarForSequenceClassification(GenericForSequenceClassification, QuasarPreTrainedModel):
657
+ pass
658
+
659
+
660
+ class QuasarForTokenClassification(GenericForTokenClassification, QuasarPreTrainedModel):
661
+ pass
662
+
663
+
664
+ class QuasarForQuestionAnswering(GenericForQuestionAnswering, QuasarPreTrainedModel):
665
+ base_model_prefix = "transformer" # For BC, where `transformer` was used instead of `model`
666
+
667
+
668
+ __all__ = [
669
+ "QuasarMoeForCausalLM",
670
+ "QuasarForQuestionAnswering",
671
+ "QuasarModel",
672
+ "QuasarPreTrainedModel",
673
+ "QuasarForSequenceClassification",
674
+ "QuasarForTokenClassification",
675
+ ]
676
+
677
+ # Quasar aliases - dynamically create Quasar versions of all Quasar classes
678
+ import sys
679
+ current_module = sys.modules[__name__]
680
+
681
+ # Create Quasar aliases for all classes in this module
682
+ for name in dir(current_module):
683
+ if name.startswith('Quasar') and not name.startswith('_'):
684
+ obj = getattr(current_module, name)
685
+ if isinstance(obj, type): # It's a class
686
+ quasar_name = name.replace('Quasar', 'Quasar')
687
+ setattr(current_module, quasar_name, obj)
688
+ __all__.append(quasar_name)